blob: afa7ef0c4ba7be21b0fc87013bac5c30a5db2f76 [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
115 write_checkpoint(sbi, false, true);
116
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)
139 write_checkpoint(sbi, false, false);
140
Namjae Jeon64c576f2012-12-22 12:10:27 +0900141 return 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900142}
143
144static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
145{
146 struct super_block *sb = dentry->d_sb;
147 struct f2fs_sb_info *sbi = F2FS_SB(sb);
148 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
149 block_t total_count, user_block_count, start_count, ovp_count;
150
151 total_count = le64_to_cpu(sbi->raw_super->block_count);
152 user_block_count = sbi->user_block_count;
153 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
154 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
155 buf->f_type = F2FS_SUPER_MAGIC;
156 buf->f_bsize = sbi->blocksize;
157
158 buf->f_blocks = total_count - start_count;
159 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
160 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
161
Jaegeuk Kim1362b5e2012-12-12 19:45:49 +0900162 buf->f_files = sbi->total_node_count;
163 buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900164
165 buf->f_namelen = F2FS_MAX_NAME_LEN;
166 buf->f_fsid.val[0] = (u32)id;
167 buf->f_fsid.val[1] = (u32)(id >> 32);
168
169 return 0;
170}
171
172static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
173{
174 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
175
176 if (test_opt(sbi, BG_GC))
177 seq_puts(seq, ",background_gc_on");
178 else
179 seq_puts(seq, ",background_gc_off");
180 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
181 seq_puts(seq, ",disable_roll_forward");
182 if (test_opt(sbi, DISCARD))
183 seq_puts(seq, ",discard");
184 if (test_opt(sbi, NOHEAP))
185 seq_puts(seq, ",no_heap_alloc");
186#ifdef CONFIG_F2FS_FS_XATTR
187 if (test_opt(sbi, XATTR_USER))
188 seq_puts(seq, ",user_xattr");
189 else
190 seq_puts(seq, ",nouser_xattr");
191#endif
192#ifdef CONFIG_F2FS_FS_POSIX_ACL
193 if (test_opt(sbi, POSIX_ACL))
194 seq_puts(seq, ",acl");
195 else
196 seq_puts(seq, ",noacl");
197#endif
198 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
199 seq_puts(seq, ",disable_ext_indentify");
200
201 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
202
203 return 0;
204}
205
206static struct super_operations f2fs_sops = {
207 .alloc_inode = f2fs_alloc_inode,
208 .destroy_inode = f2fs_destroy_inode,
209 .write_inode = f2fs_write_inode,
210 .show_options = f2fs_show_options,
211 .evict_inode = f2fs_evict_inode,
212 .put_super = f2fs_put_super,
213 .sync_fs = f2fs_sync_fs,
214 .statfs = f2fs_statfs,
215};
216
217static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
218 u64 ino, u32 generation)
219{
220 struct f2fs_sb_info *sbi = F2FS_SB(sb);
221 struct inode *inode;
222
223 if (ino < F2FS_ROOT_INO(sbi))
224 return ERR_PTR(-ESTALE);
225
226 /*
227 * f2fs_iget isn't quite right if the inode is currently unallocated!
228 * However f2fs_iget currently does appropriate checks to handle stale
229 * inodes so everything is OK.
230 */
231 inode = f2fs_iget(sb, ino);
232 if (IS_ERR(inode))
233 return ERR_CAST(inode);
234 if (generation && inode->i_generation != generation) {
235 /* we didn't find the right inode.. */
236 iput(inode);
237 return ERR_PTR(-ESTALE);
238 }
239 return inode;
240}
241
242static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
243 int fh_len, int fh_type)
244{
245 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
246 f2fs_nfs_get_inode);
247}
248
249static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
250 int fh_len, int fh_type)
251{
252 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
253 f2fs_nfs_get_inode);
254}
255
256static const struct export_operations f2fs_export_ops = {
257 .fh_to_dentry = f2fs_fh_to_dentry,
258 .fh_to_parent = f2fs_fh_to_parent,
259 .get_parent = f2fs_get_parent,
260};
261
Namjae Jeona07ef782012-12-30 14:52:05 +0900262static int parse_options(struct super_block *sb, struct f2fs_sb_info *sbi,
263 char *options)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900264{
265 substring_t args[MAX_OPT_ARGS];
266 char *p;
267 int arg = 0;
268
269 if (!options)
270 return 0;
271
272 while ((p = strsep(&options, ",")) != NULL) {
273 int token;
274 if (!*p)
275 continue;
276 /*
277 * Initialize args struct so we know whether arg was
278 * found; some options take optional arguments.
279 */
280 args[0].to = args[0].from = NULL;
281 token = match_token(p, f2fs_tokens, args);
282
283 switch (token) {
284 case Opt_gc_background_off:
285 clear_opt(sbi, BG_GC);
286 break;
287 case Opt_disable_roll_forward:
288 set_opt(sbi, DISABLE_ROLL_FORWARD);
289 break;
290 case Opt_discard:
291 set_opt(sbi, DISCARD);
292 break;
293 case Opt_noheap:
294 set_opt(sbi, NOHEAP);
295 break;
296#ifdef CONFIG_F2FS_FS_XATTR
297 case Opt_nouser_xattr:
298 clear_opt(sbi, XATTR_USER);
299 break;
300#else
301 case Opt_nouser_xattr:
Namjae Jeona07ef782012-12-30 14:52:05 +0900302 f2fs_msg(sb, KERN_INFO,
303 "nouser_xattr options not supported");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900304 break;
305#endif
306#ifdef CONFIG_F2FS_FS_POSIX_ACL
307 case Opt_noacl:
308 clear_opt(sbi, POSIX_ACL);
309 break;
310#else
311 case Opt_noacl:
Namjae Jeona07ef782012-12-30 14:52:05 +0900312 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900313 break;
314#endif
315 case Opt_active_logs:
316 if (args->from && match_int(args, &arg))
317 return -EINVAL;
Jaegeuk Kim12a67142012-12-21 11:47:05 +0900318 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900319 return -EINVAL;
320 sbi->active_logs = arg;
321 break;
322 case Opt_disable_ext_identify:
323 set_opt(sbi, DISABLE_EXT_IDENTIFY);
324 break;
325 default:
Namjae Jeona07ef782012-12-30 14:52:05 +0900326 f2fs_msg(sb, KERN_ERR,
327 "Unrecognized mount option \"%s\" or missing value",
328 p);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900329 return -EINVAL;
330 }
331 }
332 return 0;
333}
334
335static loff_t max_file_size(unsigned bits)
336{
337 loff_t result = ADDRS_PER_INODE;
338 loff_t leaf_count = ADDRS_PER_BLOCK;
339
340 /* two direct node blocks */
341 result += (leaf_count * 2);
342
343 /* two indirect node blocks */
344 leaf_count *= NIDS_PER_BLOCK;
345 result += (leaf_count * 2);
346
347 /* one double indirect node block */
348 leaf_count *= NIDS_PER_BLOCK;
349 result += leaf_count;
350
351 result <<= bits;
352 return result;
353}
354
Namjae Jeona07ef782012-12-30 14:52:05 +0900355static int sanity_check_raw_super(struct super_block *sb,
356 struct f2fs_super_block *raw_super)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900357{
358 unsigned int blocksize;
359
Namjae Jeona07ef782012-12-30 14:52:05 +0900360 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
361 f2fs_msg(sb, KERN_INFO,
362 "Magic Mismatch, valid(0x%x) - read(0x%x)",
363 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900364 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900365 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900366
367 /* Currently, support only 4KB block size */
368 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
Namjae Jeona07ef782012-12-30 14:52:05 +0900369 if (blocksize != PAGE_CACHE_SIZE) {
370 f2fs_msg(sb, KERN_INFO,
371 "Invalid blocksize (%u), supports only 4KB\n",
372 blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900373 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900374 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900375 if (le32_to_cpu(raw_super->log_sectorsize) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900376 F2FS_LOG_SECTOR_SIZE) {
377 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900378 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900379 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900380 if (le32_to_cpu(raw_super->log_sectors_per_block) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900381 F2FS_LOG_SECTORS_PER_BLOCK) {
382 f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900383 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900384 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900385 return 0;
386}
387
388static int sanity_check_ckpt(struct f2fs_super_block *raw_super,
389 struct f2fs_checkpoint *ckpt)
390{
391 unsigned int total, fsmeta;
392
393 total = le32_to_cpu(raw_super->segment_count);
394 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
395 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
396 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
397 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
398 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
399
400 if (fsmeta >= total)
401 return 1;
402 return 0;
403}
404
405static void init_sb_info(struct f2fs_sb_info *sbi)
406{
407 struct f2fs_super_block *raw_super = sbi->raw_super;
408 int i;
409
410 sbi->log_sectors_per_block =
411 le32_to_cpu(raw_super->log_sectors_per_block);
412 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
413 sbi->blocksize = 1 << sbi->log_blocksize;
414 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
415 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
416 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
417 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
418 sbi->total_sections = le32_to_cpu(raw_super->section_count);
419 sbi->total_node_count =
420 (le32_to_cpu(raw_super->segment_count_nat) / 2)
421 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
422 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
423 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
424 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
425
426 for (i = 0; i < NR_COUNT_TYPE; i++)
427 atomic_set(&sbi->nr_pages[i], 0);
428}
429
430static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
431{
432 struct f2fs_sb_info *sbi;
433 struct f2fs_super_block *raw_super;
434 struct buffer_head *raw_super_buf;
435 struct inode *root;
436 long err = -EINVAL;
437 int i;
438
439 /* allocate memory for f2fs-specific super block info */
440 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
441 if (!sbi)
442 return -ENOMEM;
443
444 /* set a temporary block size */
Namjae Jeona07ef782012-12-30 14:52:05 +0900445 if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
446 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900447 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +0900448 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900449
450 /* read f2fs raw super block */
451 raw_super_buf = sb_bread(sb, 0);
452 if (!raw_super_buf) {
453 err = -EIO;
Namjae Jeona07ef782012-12-30 14:52:05 +0900454 f2fs_msg(sb, KERN_ERR, "unable to read superblock");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900455 goto free_sbi;
456 }
457 raw_super = (struct f2fs_super_block *)
458 ((char *)raw_super_buf->b_data + F2FS_SUPER_OFFSET);
459
460 /* init some FS parameters */
461 sbi->active_logs = NR_CURSEG_TYPE;
462
463 set_opt(sbi, BG_GC);
464
465#ifdef CONFIG_F2FS_FS_XATTR
466 set_opt(sbi, XATTR_USER);
467#endif
468#ifdef CONFIG_F2FS_FS_POSIX_ACL
469 set_opt(sbi, POSIX_ACL);
470#endif
471 /* parse mount options */
Namjae Jeona07ef782012-12-30 14:52:05 +0900472 if (parse_options(sb, sbi, (char *)data))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900473 goto free_sb_buf;
474
475 /* sanity checking of raw super */
Namjae Jeona07ef782012-12-30 14:52:05 +0900476 if (sanity_check_raw_super(sb, raw_super)) {
477 f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900478 goto free_sb_buf;
Namjae Jeona07ef782012-12-30 14:52:05 +0900479 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900480
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900481 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900482 sb->s_max_links = F2FS_LINK_MAX;
483 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
484
485 sb->s_op = &f2fs_sops;
486 sb->s_xattr = f2fs_xattr_handlers;
487 sb->s_export_op = &f2fs_export_ops;
488 sb->s_magic = F2FS_SUPER_MAGIC;
489 sb->s_fs_info = sbi;
490 sb->s_time_gran = 1;
491 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
492 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
493 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
494
495 /* init f2fs-specific super block info */
496 sbi->sb = sb;
497 sbi->raw_super = raw_super;
498 sbi->raw_super_buf = raw_super_buf;
499 mutex_init(&sbi->gc_mutex);
500 mutex_init(&sbi->write_inode);
501 mutex_init(&sbi->writepages);
502 mutex_init(&sbi->cp_mutex);
503 for (i = 0; i < NR_LOCK_TYPE; i++)
504 mutex_init(&sbi->fs_lock[i]);
505 sbi->por_doing = 0;
506 spin_lock_init(&sbi->stat_lock);
507 init_rwsem(&sbi->bio_sem);
508 init_sb_info(sbi);
509
510 /* get an inode for meta space */
511 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
512 if (IS_ERR(sbi->meta_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900513 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900514 err = PTR_ERR(sbi->meta_inode);
515 goto free_sb_buf;
516 }
517
518 err = get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900519 if (err) {
520 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900521 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +0900522 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900523
524 /* sanity checking of checkpoint */
525 err = -EINVAL;
Namjae Jeona07ef782012-12-30 14:52:05 +0900526 if (sanity_check_ckpt(raw_super, sbi->ckpt)) {
527 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900528 goto free_cp;
Namjae Jeona07ef782012-12-30 14:52:05 +0900529 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900530
531 sbi->total_valid_node_count =
532 le32_to_cpu(sbi->ckpt->valid_node_count);
533 sbi->total_valid_inode_count =
534 le32_to_cpu(sbi->ckpt->valid_inode_count);
535 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
536 sbi->total_valid_block_count =
537 le64_to_cpu(sbi->ckpt->valid_block_count);
538 sbi->last_valid_block_count = sbi->total_valid_block_count;
539 sbi->alloc_valid_block_count = 0;
540 INIT_LIST_HEAD(&sbi->dir_inode_list);
541 spin_lock_init(&sbi->dir_inode_lock);
542
543 /* init super block */
544 if (!sb_set_blocksize(sb, sbi->blocksize))
545 goto free_cp;
546
547 init_orphan_info(sbi);
548
549 /* setup f2fs internal modules */
550 err = build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900551 if (err) {
552 f2fs_msg(sb, KERN_ERR,
553 "Failed to initialize F2FS segment manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900554 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900555 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900556 err = build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900557 if (err) {
558 f2fs_msg(sb, KERN_ERR,
559 "Failed to initialize F2FS node manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900560 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900561 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900562
563 build_gc_manager(sbi);
564
565 /* get an inode for node space */
566 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
567 if (IS_ERR(sbi->node_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900568 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900569 err = PTR_ERR(sbi->node_inode);
570 goto free_nm;
571 }
572
573 /* if there are nt orphan nodes free them */
574 err = -EINVAL;
Jaegeuk Kim30f0c752012-12-19 16:09:19 +0900575 if (recover_orphan_inodes(sbi))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900576 goto free_node_inode;
577
578 /* read root inode and dentry */
579 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
580 if (IS_ERR(root)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900581 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900582 err = PTR_ERR(root);
583 goto free_node_inode;
584 }
585 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
586 goto free_root_inode;
587
588 sb->s_root = d_make_root(root); /* allocate root dentry */
589 if (!sb->s_root) {
590 err = -ENOMEM;
591 goto free_root_inode;
592 }
593
594 /* recover fsynced data */
Jaegeuk Kim30f0c752012-12-19 16:09:19 +0900595 if (!test_opt(sbi, DISABLE_ROLL_FORWARD))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900596 recover_fsync_data(sbi);
597
598 /* After POR, we can run background GC thread */
599 err = start_gc_thread(sbi);
600 if (err)
601 goto fail;
602
603 err = f2fs_build_stats(sbi);
604 if (err)
605 goto fail;
606
607 return 0;
608fail:
609 stop_gc_thread(sbi);
610free_root_inode:
611 dput(sb->s_root);
612 sb->s_root = NULL;
613free_node_inode:
614 iput(sbi->node_inode);
615free_nm:
616 destroy_node_manager(sbi);
617free_sm:
618 destroy_segment_manager(sbi);
619free_cp:
620 kfree(sbi->ckpt);
621free_meta_inode:
622 make_bad_inode(sbi->meta_inode);
623 iput(sbi->meta_inode);
624free_sb_buf:
625 brelse(raw_super_buf);
626free_sbi:
627 kfree(sbi);
628 return err;
629}
630
631static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
632 const char *dev_name, void *data)
633{
634 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
635}
636
637static struct file_system_type f2fs_fs_type = {
638 .owner = THIS_MODULE,
639 .name = "f2fs",
640 .mount = f2fs_mount,
641 .kill_sb = kill_block_super,
642 .fs_flags = FS_REQUIRES_DEV,
643};
644
645static int init_inodecache(void)
646{
647 f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
648 sizeof(struct f2fs_inode_info), NULL);
649 if (f2fs_inode_cachep == NULL)
650 return -ENOMEM;
651 return 0;
652}
653
654static void destroy_inodecache(void)
655{
656 /*
657 * Make sure all delayed rcu free inodes are flushed before we
658 * destroy cache.
659 */
660 rcu_barrier();
661 kmem_cache_destroy(f2fs_inode_cachep);
662}
663
664static int __init init_f2fs_fs(void)
665{
666 int err;
667
668 err = init_inodecache();
669 if (err)
670 goto fail;
671 err = create_node_manager_caches();
672 if (err)
673 goto fail;
674 err = create_gc_caches();
675 if (err)
676 goto fail;
677 err = create_checkpoint_caches();
678 if (err)
679 goto fail;
680 return register_filesystem(&f2fs_fs_type);
681fail:
682 return err;
683}
684
685static void __exit exit_f2fs_fs(void)
686{
687 destroy_root_stats();
688 unregister_filesystem(&f2fs_fs_type);
689 destroy_checkpoint_caches();
690 destroy_gc_caches();
691 destroy_node_manager_caches();
692 destroy_inodecache();
693}
694
695module_init(init_f2fs_fs)
696module_exit(exit_f2fs_fs)
697
698MODULE_AUTHOR("Samsung Electronics's Praesto Team");
699MODULE_DESCRIPTION("Flash Friendly File System");
700MODULE_LICENSE("GPL");