blob: 1c7f595ca47c18aa15ae4eb952cfee17742b99db [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>
24#include <linux/f2fs_fs.h>
25
26#include "f2fs.h"
27#include "node.h"
28#include "xattr.h"
29
30static struct kmem_cache *f2fs_inode_cachep;
31
32enum {
33 Opt_gc_background_off,
34 Opt_disable_roll_forward,
35 Opt_discard,
36 Opt_noheap,
37 Opt_nouser_xattr,
38 Opt_noacl,
39 Opt_active_logs,
40 Opt_disable_ext_identify,
41 Opt_err,
42};
43
44static match_table_t f2fs_tokens = {
45 {Opt_gc_background_off, "background_gc_off"},
46 {Opt_disable_roll_forward, "disable_roll_forward"},
47 {Opt_discard, "discard"},
48 {Opt_noheap, "no_heap"},
49 {Opt_nouser_xattr, "nouser_xattr"},
50 {Opt_noacl, "noacl"},
51 {Opt_active_logs, "active_logs=%u"},
52 {Opt_disable_ext_identify, "disable_ext_identify"},
53 {Opt_err, NULL},
54};
55
Namjae Jeona07ef782012-12-30 14:52:05 +090056void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
57{
58 struct va_format vaf;
59 va_list args;
60
61 va_start(args, fmt);
62 vaf.fmt = fmt;
63 vaf.va = &args;
64 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
65 va_end(args);
66}
67
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090068static void init_once(void *foo)
69{
70 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
71
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090072 inode_init_once(&fi->vfs_inode);
73}
74
75static struct inode *f2fs_alloc_inode(struct super_block *sb)
76{
77 struct f2fs_inode_info *fi;
78
79 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO);
80 if (!fi)
81 return NULL;
82
83 init_once((void *) fi);
84
85 /* Initilize f2fs-specific inode info */
86 fi->vfs_inode.i_version = 1;
87 atomic_set(&fi->dirty_dents, 0);
88 fi->i_current_depth = 1;
89 fi->i_advise = 0;
90 rwlock_init(&fi->ext.ext_lock);
91
92 set_inode_flag(fi, FI_NEW_INODE);
93
94 return &fi->vfs_inode;
95}
96
97static void f2fs_i_callback(struct rcu_head *head)
98{
99 struct inode *inode = container_of(head, struct inode, i_rcu);
100 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
101}
102
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900103static void f2fs_destroy_inode(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900104{
105 call_rcu(&inode->i_rcu, f2fs_i_callback);
106}
107
108static void f2fs_put_super(struct super_block *sb)
109{
110 struct f2fs_sb_info *sbi = F2FS_SB(sb);
111
112 f2fs_destroy_stats(sbi);
113 stop_gc_thread(sbi);
114
Jaegeuk Kim43727522013-02-04 15:11:17 +0900115 write_checkpoint(sbi, true);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900116
117 iput(sbi->node_inode);
118 iput(sbi->meta_inode);
119
120 /* destroy f2fs internal modules */
121 destroy_node_manager(sbi);
122 destroy_segment_manager(sbi);
123
124 kfree(sbi->ckpt);
125
126 sb->s_fs_info = NULL;
127 brelse(sbi->raw_super_buf);
128 kfree(sbi);
129}
130
131int f2fs_sync_fs(struct super_block *sb, int sync)
132{
133 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900134
135 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
136 return 0;
137
138 if (sync)
Jaegeuk Kim43727522013-02-04 15:11:17 +0900139 write_checkpoint(sbi, false);
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900140 else
141 f2fs_balance_fs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900142
Namjae Jeon64c576f2012-12-22 12:10:27 +0900143 return 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900144}
145
Changman Leed6212a52013-01-29 18:30:07 +0900146static int f2fs_freeze(struct super_block *sb)
147{
148 int err;
149
150 if (sb->s_flags & MS_RDONLY)
151 return 0;
152
153 err = f2fs_sync_fs(sb, 1);
154 return err;
155}
156
157static int f2fs_unfreeze(struct super_block *sb)
158{
159 return 0;
160}
161
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900162static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
163{
164 struct super_block *sb = dentry->d_sb;
165 struct f2fs_sb_info *sbi = F2FS_SB(sb);
166 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
167 block_t total_count, user_block_count, start_count, ovp_count;
168
169 total_count = le64_to_cpu(sbi->raw_super->block_count);
170 user_block_count = sbi->user_block_count;
171 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
172 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
173 buf->f_type = F2FS_SUPER_MAGIC;
174 buf->f_bsize = sbi->blocksize;
175
176 buf->f_blocks = total_count - start_count;
177 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
178 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
179
Jaegeuk Kim1362b5e2012-12-12 19:45:49 +0900180 buf->f_files = sbi->total_node_count;
181 buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900182
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900183 buf->f_namelen = F2FS_NAME_LEN;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900184 buf->f_fsid.val[0] = (u32)id;
185 buf->f_fsid.val[1] = (u32)(id >> 32);
186
187 return 0;
188}
189
190static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
191{
192 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
193
194 if (test_opt(sbi, BG_GC))
195 seq_puts(seq, ",background_gc_on");
196 else
197 seq_puts(seq, ",background_gc_off");
198 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
199 seq_puts(seq, ",disable_roll_forward");
200 if (test_opt(sbi, DISCARD))
201 seq_puts(seq, ",discard");
202 if (test_opt(sbi, NOHEAP))
203 seq_puts(seq, ",no_heap_alloc");
204#ifdef CONFIG_F2FS_FS_XATTR
205 if (test_opt(sbi, XATTR_USER))
206 seq_puts(seq, ",user_xattr");
207 else
208 seq_puts(seq, ",nouser_xattr");
209#endif
210#ifdef CONFIG_F2FS_FS_POSIX_ACL
211 if (test_opt(sbi, POSIX_ACL))
212 seq_puts(seq, ",acl");
213 else
214 seq_puts(seq, ",noacl");
215#endif
216 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
Alejandro Martinez Ruizaa435072013-01-25 19:08:59 +0100217 seq_puts(seq, ",disable_ext_identify");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900218
219 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
220
221 return 0;
222}
223
224static struct super_operations f2fs_sops = {
225 .alloc_inode = f2fs_alloc_inode,
226 .destroy_inode = f2fs_destroy_inode,
227 .write_inode = f2fs_write_inode,
228 .show_options = f2fs_show_options,
229 .evict_inode = f2fs_evict_inode,
230 .put_super = f2fs_put_super,
231 .sync_fs = f2fs_sync_fs,
Changman Leed6212a52013-01-29 18:30:07 +0900232 .freeze_fs = f2fs_freeze,
233 .unfreeze_fs = f2fs_unfreeze,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900234 .statfs = f2fs_statfs,
235};
236
237static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
238 u64 ino, u32 generation)
239{
240 struct f2fs_sb_info *sbi = F2FS_SB(sb);
241 struct inode *inode;
242
243 if (ino < F2FS_ROOT_INO(sbi))
244 return ERR_PTR(-ESTALE);
245
246 /*
247 * f2fs_iget isn't quite right if the inode is currently unallocated!
248 * However f2fs_iget currently does appropriate checks to handle stale
249 * inodes so everything is OK.
250 */
251 inode = f2fs_iget(sb, ino);
252 if (IS_ERR(inode))
253 return ERR_CAST(inode);
254 if (generation && inode->i_generation != generation) {
255 /* we didn't find the right inode.. */
256 iput(inode);
257 return ERR_PTR(-ESTALE);
258 }
259 return inode;
260}
261
262static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
263 int fh_len, int fh_type)
264{
265 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
266 f2fs_nfs_get_inode);
267}
268
269static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
270 int fh_len, int fh_type)
271{
272 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
273 f2fs_nfs_get_inode);
274}
275
276static const struct export_operations f2fs_export_ops = {
277 .fh_to_dentry = f2fs_fh_to_dentry,
278 .fh_to_parent = f2fs_fh_to_parent,
279 .get_parent = f2fs_get_parent,
280};
281
Namjae Jeona07ef782012-12-30 14:52:05 +0900282static int parse_options(struct super_block *sb, struct f2fs_sb_info *sbi,
283 char *options)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900284{
285 substring_t args[MAX_OPT_ARGS];
286 char *p;
287 int arg = 0;
288
289 if (!options)
290 return 0;
291
292 while ((p = strsep(&options, ",")) != NULL) {
293 int token;
294 if (!*p)
295 continue;
296 /*
297 * Initialize args struct so we know whether arg was
298 * found; some options take optional arguments.
299 */
300 args[0].to = args[0].from = NULL;
301 token = match_token(p, f2fs_tokens, args);
302
303 switch (token) {
304 case Opt_gc_background_off:
305 clear_opt(sbi, BG_GC);
306 break;
307 case Opt_disable_roll_forward:
308 set_opt(sbi, DISABLE_ROLL_FORWARD);
309 break;
310 case Opt_discard:
311 set_opt(sbi, DISCARD);
312 break;
313 case Opt_noheap:
314 set_opt(sbi, NOHEAP);
315 break;
316#ifdef CONFIG_F2FS_FS_XATTR
317 case Opt_nouser_xattr:
318 clear_opt(sbi, XATTR_USER);
319 break;
320#else
321 case Opt_nouser_xattr:
Namjae Jeona07ef782012-12-30 14:52:05 +0900322 f2fs_msg(sb, KERN_INFO,
323 "nouser_xattr options not supported");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900324 break;
325#endif
326#ifdef CONFIG_F2FS_FS_POSIX_ACL
327 case Opt_noacl:
328 clear_opt(sbi, POSIX_ACL);
329 break;
330#else
331 case Opt_noacl:
Namjae Jeona07ef782012-12-30 14:52:05 +0900332 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900333 break;
334#endif
335 case Opt_active_logs:
336 if (args->from && match_int(args, &arg))
337 return -EINVAL;
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900338 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900339 return -EINVAL;
340 sbi->active_logs = arg;
341 break;
342 case Opt_disable_ext_identify:
343 set_opt(sbi, DISABLE_EXT_IDENTIFY);
344 break;
345 default:
Namjae Jeona07ef782012-12-30 14:52:05 +0900346 f2fs_msg(sb, KERN_ERR,
347 "Unrecognized mount option \"%s\" or missing value",
348 p);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900349 return -EINVAL;
350 }
351 }
352 return 0;
353}
354
355static loff_t max_file_size(unsigned bits)
356{
357 loff_t result = ADDRS_PER_INODE;
358 loff_t leaf_count = ADDRS_PER_BLOCK;
359
360 /* two direct node blocks */
361 result += (leaf_count * 2);
362
363 /* two indirect node blocks */
364 leaf_count *= NIDS_PER_BLOCK;
365 result += (leaf_count * 2);
366
367 /* one double indirect node block */
368 leaf_count *= NIDS_PER_BLOCK;
369 result += leaf_count;
370
371 result <<= bits;
372 return result;
373}
374
Namjae Jeona07ef782012-12-30 14:52:05 +0900375static int sanity_check_raw_super(struct super_block *sb,
376 struct f2fs_super_block *raw_super)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900377{
378 unsigned int blocksize;
379
Namjae Jeona07ef782012-12-30 14:52:05 +0900380 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
381 f2fs_msg(sb, KERN_INFO,
382 "Magic Mismatch, valid(0x%x) - read(0x%x)",
383 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900384 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900385 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900386
majianpeng5c9b4692013-02-01 19:07:57 +0800387 /* Currently, support only 4KB page cache size */
388 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
389 f2fs_msg(sb, KERN_INFO,
majianpeng14d7e9d2013-02-01 19:07:03 +0800390 "Invalid page_cache_size (%lu), supports only 4KB\n",
majianpeng5c9b4692013-02-01 19:07:57 +0800391 PAGE_CACHE_SIZE);
392 return 1;
393 }
394
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900395 /* Currently, support only 4KB block size */
396 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
majianpeng5c9b4692013-02-01 19:07:57 +0800397 if (blocksize != F2FS_BLKSIZE) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900398 f2fs_msg(sb, KERN_INFO,
399 "Invalid blocksize (%u), supports only 4KB\n",
400 blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900401 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900402 }
majianpeng5c9b4692013-02-01 19:07:57 +0800403
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900404 if (le32_to_cpu(raw_super->log_sectorsize) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900405 F2FS_LOG_SECTOR_SIZE) {
406 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900407 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900408 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900409 if (le32_to_cpu(raw_super->log_sectors_per_block) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900410 F2FS_LOG_SECTORS_PER_BLOCK) {
411 f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900412 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900413 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900414 return 0;
415}
416
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900417static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900418{
419 unsigned int total, fsmeta;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900420 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
421 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900422
423 total = le32_to_cpu(raw_super->segment_count);
424 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
425 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
426 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
427 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
428 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
429
430 if (fsmeta >= total)
431 return 1;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900432
433 if (is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
434 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
435 return 1;
436 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900437 return 0;
438}
439
440static void init_sb_info(struct f2fs_sb_info *sbi)
441{
442 struct f2fs_super_block *raw_super = sbi->raw_super;
443 int i;
444
445 sbi->log_sectors_per_block =
446 le32_to_cpu(raw_super->log_sectors_per_block);
447 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
448 sbi->blocksize = 1 << sbi->log_blocksize;
449 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
450 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
451 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
452 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
453 sbi->total_sections = le32_to_cpu(raw_super->section_count);
454 sbi->total_node_count =
455 (le32_to_cpu(raw_super->segment_count_nat) / 2)
456 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
457 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
458 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
459 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
460
461 for (i = 0; i < NR_COUNT_TYPE; i++)
462 atomic_set(&sbi->nr_pages[i], 0);
463}
464
majianpeng14d7e9d2013-02-01 19:07:03 +0800465static int validate_superblock(struct super_block *sb,
466 struct f2fs_super_block **raw_super,
467 struct buffer_head **raw_super_buf, sector_t block)
468{
469 const char *super = (block == 0 ? "first" : "second");
470
471 /* read f2fs raw super block */
472 *raw_super_buf = sb_bread(sb, block);
473 if (!*raw_super_buf) {
474 f2fs_msg(sb, KERN_ERR, "unable to read %s superblock",
475 super);
476 return 1;
477 }
478
479 *raw_super = (struct f2fs_super_block *)
480 ((char *)(*raw_super_buf)->b_data + F2FS_SUPER_OFFSET);
481
482 /* sanity checking of raw super */
483 if (!sanity_check_raw_super(sb, *raw_super))
484 return 0;
485
486 f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem "
487 "in %s superblock", super);
488 return 1;
489}
490
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900491static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
492{
493 struct f2fs_sb_info *sbi;
494 struct f2fs_super_block *raw_super;
495 struct buffer_head *raw_super_buf;
496 struct inode *root;
497 long err = -EINVAL;
498 int i;
499
500 /* allocate memory for f2fs-specific super block info */
501 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
502 if (!sbi)
503 return -ENOMEM;
504
Namjae Jeonff9234a2013-01-12 14:41:13 +0900505 /* set a block size */
Namjae Jeona07ef782012-12-30 14:52:05 +0900506 if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
507 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900508 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +0900509 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900510
majianpeng14d7e9d2013-02-01 19:07:03 +0800511 if (validate_superblock(sb, &raw_super, &raw_super_buf, 0)) {
512 brelse(raw_super_buf);
513 if (validate_superblock(sb, &raw_super, &raw_super_buf, 1))
514 goto free_sb_buf;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900515 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900516 /* init some FS parameters */
517 sbi->active_logs = NR_CURSEG_TYPE;
518
519 set_opt(sbi, BG_GC);
520
521#ifdef CONFIG_F2FS_FS_XATTR
522 set_opt(sbi, XATTR_USER);
523#endif
524#ifdef CONFIG_F2FS_FS_POSIX_ACL
525 set_opt(sbi, POSIX_ACL);
526#endif
527 /* parse mount options */
Namjae Jeona07ef782012-12-30 14:52:05 +0900528 if (parse_options(sb, sbi, (char *)data))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900529 goto free_sb_buf;
530
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900531 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900532 sb->s_max_links = F2FS_LINK_MAX;
533 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
534
535 sb->s_op = &f2fs_sops;
536 sb->s_xattr = f2fs_xattr_handlers;
537 sb->s_export_op = &f2fs_export_ops;
538 sb->s_magic = F2FS_SUPER_MAGIC;
539 sb->s_fs_info = sbi;
540 sb->s_time_gran = 1;
541 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
542 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
543 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
544
545 /* init f2fs-specific super block info */
546 sbi->sb = sb;
547 sbi->raw_super = raw_super;
548 sbi->raw_super_buf = raw_super_buf;
549 mutex_init(&sbi->gc_mutex);
550 mutex_init(&sbi->write_inode);
551 mutex_init(&sbi->writepages);
552 mutex_init(&sbi->cp_mutex);
553 for (i = 0; i < NR_LOCK_TYPE; i++)
554 mutex_init(&sbi->fs_lock[i]);
555 sbi->por_doing = 0;
556 spin_lock_init(&sbi->stat_lock);
557 init_rwsem(&sbi->bio_sem);
558 init_sb_info(sbi);
559
560 /* get an inode for meta space */
561 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
562 if (IS_ERR(sbi->meta_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900563 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900564 err = PTR_ERR(sbi->meta_inode);
565 goto free_sb_buf;
566 }
567
568 err = get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900569 if (err) {
570 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900571 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +0900572 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900573
574 /* sanity checking of checkpoint */
575 err = -EINVAL;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900576 if (sanity_check_ckpt(sbi)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900577 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900578 goto free_cp;
Namjae Jeona07ef782012-12-30 14:52:05 +0900579 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900580
581 sbi->total_valid_node_count =
582 le32_to_cpu(sbi->ckpt->valid_node_count);
583 sbi->total_valid_inode_count =
584 le32_to_cpu(sbi->ckpt->valid_inode_count);
585 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
586 sbi->total_valid_block_count =
587 le64_to_cpu(sbi->ckpt->valid_block_count);
588 sbi->last_valid_block_count = sbi->total_valid_block_count;
589 sbi->alloc_valid_block_count = 0;
590 INIT_LIST_HEAD(&sbi->dir_inode_list);
591 spin_lock_init(&sbi->dir_inode_lock);
592
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900593 init_orphan_info(sbi);
594
595 /* setup f2fs internal modules */
596 err = build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900597 if (err) {
598 f2fs_msg(sb, KERN_ERR,
599 "Failed to initialize F2FS segment manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900600 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900601 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900602 err = build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900603 if (err) {
604 f2fs_msg(sb, KERN_ERR,
605 "Failed to initialize F2FS node manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900606 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900607 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900608
609 build_gc_manager(sbi);
610
611 /* get an inode for node space */
612 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
613 if (IS_ERR(sbi->node_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900614 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900615 err = PTR_ERR(sbi->node_inode);
616 goto free_nm;
617 }
618
619 /* if there are nt orphan nodes free them */
620 err = -EINVAL;
Jaegeuk Kim30f0c752012-12-19 16:09:19 +0900621 if (recover_orphan_inodes(sbi))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900622 goto free_node_inode;
623
624 /* read root inode and dentry */
625 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
626 if (IS_ERR(root)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900627 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900628 err = PTR_ERR(root);
629 goto free_node_inode;
630 }
631 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
632 goto free_root_inode;
633
634 sb->s_root = d_make_root(root); /* allocate root dentry */
635 if (!sb->s_root) {
636 err = -ENOMEM;
637 goto free_root_inode;
638 }
639
640 /* recover fsynced data */
Jaegeuk Kim30f0c752012-12-19 16:09:19 +0900641 if (!test_opt(sbi, DISABLE_ROLL_FORWARD))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900642 recover_fsync_data(sbi);
643
644 /* After POR, we can run background GC thread */
645 err = start_gc_thread(sbi);
646 if (err)
647 goto fail;
648
649 err = f2fs_build_stats(sbi);
650 if (err)
651 goto fail;
652
653 return 0;
654fail:
655 stop_gc_thread(sbi);
656free_root_inode:
657 dput(sb->s_root);
658 sb->s_root = NULL;
659free_node_inode:
660 iput(sbi->node_inode);
661free_nm:
662 destroy_node_manager(sbi);
663free_sm:
664 destroy_segment_manager(sbi);
665free_cp:
666 kfree(sbi->ckpt);
667free_meta_inode:
668 make_bad_inode(sbi->meta_inode);
669 iput(sbi->meta_inode);
670free_sb_buf:
671 brelse(raw_super_buf);
672free_sbi:
673 kfree(sbi);
674 return err;
675}
676
677static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
678 const char *dev_name, void *data)
679{
680 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
681}
682
683static struct file_system_type f2fs_fs_type = {
684 .owner = THIS_MODULE,
685 .name = "f2fs",
686 .mount = f2fs_mount,
687 .kill_sb = kill_block_super,
688 .fs_flags = FS_REQUIRES_DEV,
689};
690
Namjae Jeon6e6093a2013-01-17 00:08:30 +0900691static int __init init_inodecache(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900692{
693 f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
694 sizeof(struct f2fs_inode_info), NULL);
695 if (f2fs_inode_cachep == NULL)
696 return -ENOMEM;
697 return 0;
698}
699
700static void destroy_inodecache(void)
701{
702 /*
703 * Make sure all delayed rcu free inodes are flushed before we
704 * destroy cache.
705 */
706 rcu_barrier();
707 kmem_cache_destroy(f2fs_inode_cachep);
708}
709
710static int __init init_f2fs_fs(void)
711{
712 int err;
713
714 err = init_inodecache();
715 if (err)
716 goto fail;
717 err = create_node_manager_caches();
718 if (err)
719 goto fail;
720 err = create_gc_caches();
721 if (err)
722 goto fail;
723 err = create_checkpoint_caches();
724 if (err)
725 goto fail;
Namjae Jeon4589d252013-01-15 19:58:47 +0900726 err = register_filesystem(&f2fs_fs_type);
727 if (err)
728 goto fail;
729 f2fs_create_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900730fail:
731 return err;
732}
733
734static void __exit exit_f2fs_fs(void)
735{
Namjae Jeon4589d252013-01-15 19:58:47 +0900736 f2fs_destroy_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900737 unregister_filesystem(&f2fs_fs_type);
738 destroy_checkpoint_caches();
739 destroy_gc_caches();
740 destroy_node_manager_caches();
741 destroy_inodecache();
742}
743
744module_init(init_f2fs_fs)
745module_exit(exit_f2fs_fs)
746
747MODULE_AUTHOR("Samsung Electronics's Praesto Team");
748MODULE_DESCRIPTION("Flash Friendly File System");
749MODULE_LICENSE("GPL");