b015b6cd7bd4f59af8b22600fc407347ce46a3a8
[platform/adaptation/renesas_rcar/renesas_kernel.git] / fs / f2fs / super.c
1 /*
2  * 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/blkdev.h>
25 #include <linux/f2fs_fs.h>
26
27 #include "f2fs.h"
28 #include "node.h"
29 #include "segment.h"
30 #include "xattr.h"
31
32 #define CREATE_TRACE_POINTS
33 #include <trace/events/f2fs.h>
34
35 static struct kmem_cache *f2fs_inode_cachep;
36
37 enum {
38         Opt_gc_background_off,
39         Opt_disable_roll_forward,
40         Opt_discard,
41         Opt_noheap,
42         Opt_nouser_xattr,
43         Opt_noacl,
44         Opt_active_logs,
45         Opt_disable_ext_identify,
46         Opt_err,
47 };
48
49 static match_table_t f2fs_tokens = {
50         {Opt_gc_background_off, "background_gc_off"},
51         {Opt_disable_roll_forward, "disable_roll_forward"},
52         {Opt_discard, "discard"},
53         {Opt_noheap, "no_heap"},
54         {Opt_nouser_xattr, "nouser_xattr"},
55         {Opt_noacl, "noacl"},
56         {Opt_active_logs, "active_logs=%u"},
57         {Opt_disable_ext_identify, "disable_ext_identify"},
58         {Opt_err, NULL},
59 };
60
61 void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
62 {
63         struct va_format vaf;
64         va_list args;
65
66         va_start(args, fmt);
67         vaf.fmt = fmt;
68         vaf.va = &args;
69         printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
70         va_end(args);
71 }
72
73 static void init_once(void *foo)
74 {
75         struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
76
77         inode_init_once(&fi->vfs_inode);
78 }
79
80 static struct inode *f2fs_alloc_inode(struct super_block *sb)
81 {
82         struct f2fs_inode_info *fi;
83
84         fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO);
85         if (!fi)
86                 return NULL;
87
88         init_once((void *) fi);
89
90         /* Initialize f2fs-specific inode info */
91         fi->vfs_inode.i_version = 1;
92         atomic_set(&fi->dirty_dents, 0);
93         fi->i_current_depth = 1;
94         fi->i_advise = 0;
95         rwlock_init(&fi->ext.ext_lock);
96
97         set_inode_flag(fi, FI_NEW_INODE);
98
99         return &fi->vfs_inode;
100 }
101
102 static void f2fs_i_callback(struct rcu_head *head)
103 {
104         struct inode *inode = container_of(head, struct inode, i_rcu);
105         kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
106 }
107
108 static void f2fs_destroy_inode(struct inode *inode)
109 {
110         call_rcu(&inode->i_rcu, f2fs_i_callback);
111 }
112
113 static void f2fs_put_super(struct super_block *sb)
114 {
115         struct f2fs_sb_info *sbi = F2FS_SB(sb);
116
117         f2fs_destroy_stats(sbi);
118         stop_gc_thread(sbi);
119
120         write_checkpoint(sbi, true);
121
122         iput(sbi->node_inode);
123         iput(sbi->meta_inode);
124
125         /* destroy f2fs internal modules */
126         destroy_node_manager(sbi);
127         destroy_segment_manager(sbi);
128
129         kfree(sbi->ckpt);
130
131         sb->s_fs_info = NULL;
132         brelse(sbi->raw_super_buf);
133         kfree(sbi);
134 }
135
136 int f2fs_sync_fs(struct super_block *sb, int sync)
137 {
138         struct f2fs_sb_info *sbi = F2FS_SB(sb);
139
140         trace_f2fs_sync_fs(sb, sync);
141
142         if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
143                 return 0;
144
145         if (sync) {
146                 mutex_lock(&sbi->gc_mutex);
147                 write_checkpoint(sbi, false);
148                 mutex_unlock(&sbi->gc_mutex);
149         } else {
150                 f2fs_balance_fs(sbi);
151         }
152
153         return 0;
154 }
155
156 static int f2fs_freeze(struct super_block *sb)
157 {
158         int err;
159
160         if (sb->s_flags & MS_RDONLY)
161                 return 0;
162
163         err = f2fs_sync_fs(sb, 1);
164         return err;
165 }
166
167 static int f2fs_unfreeze(struct super_block *sb)
168 {
169         return 0;
170 }
171
172 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
173 {
174         struct super_block *sb = dentry->d_sb;
175         struct f2fs_sb_info *sbi = F2FS_SB(sb);
176         u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
177         block_t total_count, user_block_count, start_count, ovp_count;
178
179         total_count = le64_to_cpu(sbi->raw_super->block_count);
180         user_block_count = sbi->user_block_count;
181         start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
182         ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
183         buf->f_type = F2FS_SUPER_MAGIC;
184         buf->f_bsize = sbi->blocksize;
185
186         buf->f_blocks = total_count - start_count;
187         buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
188         buf->f_bavail = user_block_count - valid_user_blocks(sbi);
189
190         buf->f_files = sbi->total_node_count;
191         buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
192
193         buf->f_namelen = F2FS_NAME_LEN;
194         buf->f_fsid.val[0] = (u32)id;
195         buf->f_fsid.val[1] = (u32)(id >> 32);
196
197         return 0;
198 }
199
200 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
201 {
202         struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
203
204         if (test_opt(sbi, BG_GC))
205                 seq_puts(seq, ",background_gc_on");
206         else
207                 seq_puts(seq, ",background_gc_off");
208         if (test_opt(sbi, DISABLE_ROLL_FORWARD))
209                 seq_puts(seq, ",disable_roll_forward");
210         if (test_opt(sbi, DISCARD))
211                 seq_puts(seq, ",discard");
212         if (test_opt(sbi, NOHEAP))
213                 seq_puts(seq, ",no_heap_alloc");
214 #ifdef CONFIG_F2FS_FS_XATTR
215         if (test_opt(sbi, XATTR_USER))
216                 seq_puts(seq, ",user_xattr");
217         else
218                 seq_puts(seq, ",nouser_xattr");
219 #endif
220 #ifdef CONFIG_F2FS_FS_POSIX_ACL
221         if (test_opt(sbi, POSIX_ACL))
222                 seq_puts(seq, ",acl");
223         else
224                 seq_puts(seq, ",noacl");
225 #endif
226         if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
227                 seq_puts(seq, ",disable_ext_identify");
228
229         seq_printf(seq, ",active_logs=%u", sbi->active_logs);
230
231         return 0;
232 }
233
234 static struct super_operations f2fs_sops = {
235         .alloc_inode    = f2fs_alloc_inode,
236         .destroy_inode  = f2fs_destroy_inode,
237         .write_inode    = f2fs_write_inode,
238         .show_options   = f2fs_show_options,
239         .evict_inode    = f2fs_evict_inode,
240         .put_super      = f2fs_put_super,
241         .sync_fs        = f2fs_sync_fs,
242         .freeze_fs      = f2fs_freeze,
243         .unfreeze_fs    = f2fs_unfreeze,
244         .statfs         = f2fs_statfs,
245 };
246
247 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
248                 u64 ino, u32 generation)
249 {
250         struct f2fs_sb_info *sbi = F2FS_SB(sb);
251         struct inode *inode;
252
253         if (ino < F2FS_ROOT_INO(sbi))
254                 return ERR_PTR(-ESTALE);
255
256         /*
257          * f2fs_iget isn't quite right if the inode is currently unallocated!
258          * However f2fs_iget currently does appropriate checks to handle stale
259          * inodes so everything is OK.
260          */
261         inode = f2fs_iget(sb, ino);
262         if (IS_ERR(inode))
263                 return ERR_CAST(inode);
264         if (generation && inode->i_generation != generation) {
265                 /* we didn't find the right inode.. */
266                 iput(inode);
267                 return ERR_PTR(-ESTALE);
268         }
269         return inode;
270 }
271
272 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
273                 int fh_len, int fh_type)
274 {
275         return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
276                                     f2fs_nfs_get_inode);
277 }
278
279 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
280                 int fh_len, int fh_type)
281 {
282         return generic_fh_to_parent(sb, fid, fh_len, fh_type,
283                                     f2fs_nfs_get_inode);
284 }
285
286 static const struct export_operations f2fs_export_ops = {
287         .fh_to_dentry = f2fs_fh_to_dentry,
288         .fh_to_parent = f2fs_fh_to_parent,
289         .get_parent = f2fs_get_parent,
290 };
291
292 static int parse_options(struct super_block *sb, struct f2fs_sb_info *sbi,
293                                 char *options)
294 {
295         substring_t args[MAX_OPT_ARGS];
296         char *p;
297         int arg = 0;
298
299         if (!options)
300                 return 0;
301
302         while ((p = strsep(&options, ",")) != NULL) {
303                 int token;
304                 if (!*p)
305                         continue;
306                 /*
307                  * Initialize args struct so we know whether arg was
308                  * found; some options take optional arguments.
309                  */
310                 args[0].to = args[0].from = NULL;
311                 token = match_token(p, f2fs_tokens, args);
312
313                 switch (token) {
314                 case Opt_gc_background_off:
315                         clear_opt(sbi, BG_GC);
316                         break;
317                 case Opt_disable_roll_forward:
318                         set_opt(sbi, DISABLE_ROLL_FORWARD);
319                         break;
320                 case Opt_discard:
321                         set_opt(sbi, DISCARD);
322                         break;
323                 case Opt_noheap:
324                         set_opt(sbi, NOHEAP);
325                         break;
326 #ifdef CONFIG_F2FS_FS_XATTR
327                 case Opt_nouser_xattr:
328                         clear_opt(sbi, XATTR_USER);
329                         break;
330 #else
331                 case Opt_nouser_xattr:
332                         f2fs_msg(sb, KERN_INFO,
333                                 "nouser_xattr options not supported");
334                         break;
335 #endif
336 #ifdef CONFIG_F2FS_FS_POSIX_ACL
337                 case Opt_noacl:
338                         clear_opt(sbi, POSIX_ACL);
339                         break;
340 #else
341                 case Opt_noacl:
342                         f2fs_msg(sb, KERN_INFO, "noacl options not supported");
343                         break;
344 #endif
345                 case Opt_active_logs:
346                         if (args->from && match_int(args, &arg))
347                                 return -EINVAL;
348                         if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
349                                 return -EINVAL;
350                         sbi->active_logs = arg;
351                         break;
352                 case Opt_disable_ext_identify:
353                         set_opt(sbi, DISABLE_EXT_IDENTIFY);
354                         break;
355                 default:
356                         f2fs_msg(sb, KERN_ERR,
357                                 "Unrecognized mount option \"%s\" or missing value",
358                                 p);
359                         return -EINVAL;
360                 }
361         }
362         return 0;
363 }
364
365 static loff_t max_file_size(unsigned bits)
366 {
367         loff_t result = ADDRS_PER_INODE;
368         loff_t leaf_count = ADDRS_PER_BLOCK;
369
370         /* two direct node blocks */
371         result += (leaf_count * 2);
372
373         /* two indirect node blocks */
374         leaf_count *= NIDS_PER_BLOCK;
375         result += (leaf_count * 2);
376
377         /* one double indirect node block */
378         leaf_count *= NIDS_PER_BLOCK;
379         result += leaf_count;
380
381         result <<= bits;
382         return result;
383 }
384
385 static int sanity_check_raw_super(struct super_block *sb,
386                         struct f2fs_super_block *raw_super)
387 {
388         unsigned int blocksize;
389
390         if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
391                 f2fs_msg(sb, KERN_INFO,
392                         "Magic Mismatch, valid(0x%x) - read(0x%x)",
393                         F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
394                 return 1;
395         }
396
397         /* Currently, support only 4KB page cache size */
398         if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
399                 f2fs_msg(sb, KERN_INFO,
400                         "Invalid page_cache_size (%lu), supports only 4KB\n",
401                         PAGE_CACHE_SIZE);
402                 return 1;
403         }
404
405         /* Currently, support only 4KB block size */
406         blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
407         if (blocksize != F2FS_BLKSIZE) {
408                 f2fs_msg(sb, KERN_INFO,
409                         "Invalid blocksize (%u), supports only 4KB\n",
410                         blocksize);
411                 return 1;
412         }
413
414         if (le32_to_cpu(raw_super->log_sectorsize) !=
415                                         F2FS_LOG_SECTOR_SIZE) {
416                 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
417                 return 1;
418         }
419         if (le32_to_cpu(raw_super->log_sectors_per_block) !=
420                                         F2FS_LOG_SECTORS_PER_BLOCK) {
421                 f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
422                 return 1;
423         }
424         return 0;
425 }
426
427 static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
428 {
429         unsigned int total, fsmeta;
430         struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
431         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
432
433         total = le32_to_cpu(raw_super->segment_count);
434         fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
435         fsmeta += le32_to_cpu(raw_super->segment_count_sit);
436         fsmeta += le32_to_cpu(raw_super->segment_count_nat);
437         fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
438         fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
439
440         if (fsmeta >= total)
441                 return 1;
442
443         if (is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
444                 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
445                 return 1;
446         }
447         return 0;
448 }
449
450 static void init_sb_info(struct f2fs_sb_info *sbi)
451 {
452         struct f2fs_super_block *raw_super = sbi->raw_super;
453         int i;
454
455         sbi->log_sectors_per_block =
456                 le32_to_cpu(raw_super->log_sectors_per_block);
457         sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
458         sbi->blocksize = 1 << sbi->log_blocksize;
459         sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
460         sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
461         sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
462         sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
463         sbi->total_sections = le32_to_cpu(raw_super->section_count);
464         sbi->total_node_count =
465                 (le32_to_cpu(raw_super->segment_count_nat) / 2)
466                         * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
467         sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
468         sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
469         sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
470         sbi->cur_victim_sec = NULL_SECNO;
471
472         for (i = 0; i < NR_COUNT_TYPE; i++)
473                 atomic_set(&sbi->nr_pages[i], 0);
474 }
475
476 static int validate_superblock(struct super_block *sb,
477                 struct f2fs_super_block **raw_super,
478                 struct buffer_head **raw_super_buf, sector_t block)
479 {
480         const char *super = (block == 0 ? "first" : "second");
481
482         /* read f2fs raw super block */
483         *raw_super_buf = sb_bread(sb, block);
484         if (!*raw_super_buf) {
485                 f2fs_msg(sb, KERN_ERR, "unable to read %s superblock",
486                                 super);
487                 return -EIO;
488         }
489
490         *raw_super = (struct f2fs_super_block *)
491                 ((char *)(*raw_super_buf)->b_data + F2FS_SUPER_OFFSET);
492
493         /* sanity checking of raw super */
494         if (!sanity_check_raw_super(sb, *raw_super))
495                 return 0;
496
497         f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem "
498                                 "in %s superblock", super);
499         return -EINVAL;
500 }
501
502 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
503 {
504         struct f2fs_sb_info *sbi;
505         struct f2fs_super_block *raw_super;
506         struct buffer_head *raw_super_buf;
507         struct inode *root;
508         long err = -EINVAL;
509         int i;
510
511         /* allocate memory for f2fs-specific super block info */
512         sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
513         if (!sbi)
514                 return -ENOMEM;
515
516         /* set a block size */
517         if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
518                 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
519                 goto free_sbi;
520         }
521
522         err = validate_superblock(sb, &raw_super, &raw_super_buf, 0);
523         if (err) {
524                 brelse(raw_super_buf);
525                 /* check secondary superblock when primary failed */
526                 err = validate_superblock(sb, &raw_super, &raw_super_buf, 1);
527                 if (err)
528                         goto free_sb_buf;
529         }
530         /* init some FS parameters */
531         sbi->active_logs = NR_CURSEG_TYPE;
532
533         set_opt(sbi, BG_GC);
534
535 #ifdef CONFIG_F2FS_FS_XATTR
536         set_opt(sbi, XATTR_USER);
537 #endif
538 #ifdef CONFIG_F2FS_FS_POSIX_ACL
539         set_opt(sbi, POSIX_ACL);
540 #endif
541         /* parse mount options */
542         err = parse_options(sb, sbi, (char *)data);
543         if (err)
544                 goto free_sb_buf;
545
546         sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
547         sb->s_max_links = F2FS_LINK_MAX;
548         get_random_bytes(&sbi->s_next_generation, sizeof(u32));
549
550         sb->s_op = &f2fs_sops;
551         sb->s_xattr = f2fs_xattr_handlers;
552         sb->s_export_op = &f2fs_export_ops;
553         sb->s_magic = F2FS_SUPER_MAGIC;
554         sb->s_fs_info = sbi;
555         sb->s_time_gran = 1;
556         sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
557                 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
558         memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
559
560         /* init f2fs-specific super block info */
561         sbi->sb = sb;
562         sbi->raw_super = raw_super;
563         sbi->raw_super_buf = raw_super_buf;
564         mutex_init(&sbi->gc_mutex);
565         mutex_init(&sbi->writepages);
566         mutex_init(&sbi->cp_mutex);
567         for (i = 0; i < NR_GLOBAL_LOCKS; i++)
568                 mutex_init(&sbi->fs_lock[i]);
569         mutex_init(&sbi->node_write);
570         sbi->por_doing = 0;
571         spin_lock_init(&sbi->stat_lock);
572         init_rwsem(&sbi->bio_sem);
573         init_sb_info(sbi);
574
575         /* get an inode for meta space */
576         sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
577         if (IS_ERR(sbi->meta_inode)) {
578                 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
579                 err = PTR_ERR(sbi->meta_inode);
580                 goto free_sb_buf;
581         }
582
583         err = get_valid_checkpoint(sbi);
584         if (err) {
585                 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
586                 goto free_meta_inode;
587         }
588
589         /* sanity checking of checkpoint */
590         err = -EINVAL;
591         if (sanity_check_ckpt(sbi)) {
592                 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
593                 goto free_cp;
594         }
595
596         sbi->total_valid_node_count =
597                                 le32_to_cpu(sbi->ckpt->valid_node_count);
598         sbi->total_valid_inode_count =
599                                 le32_to_cpu(sbi->ckpt->valid_inode_count);
600         sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
601         sbi->total_valid_block_count =
602                                 le64_to_cpu(sbi->ckpt->valid_block_count);
603         sbi->last_valid_block_count = sbi->total_valid_block_count;
604         sbi->alloc_valid_block_count = 0;
605         INIT_LIST_HEAD(&sbi->dir_inode_list);
606         spin_lock_init(&sbi->dir_inode_lock);
607
608         init_orphan_info(sbi);
609
610         /* setup f2fs internal modules */
611         err = build_segment_manager(sbi);
612         if (err) {
613                 f2fs_msg(sb, KERN_ERR,
614                         "Failed to initialize F2FS segment manager");
615                 goto free_sm;
616         }
617         err = build_node_manager(sbi);
618         if (err) {
619                 f2fs_msg(sb, KERN_ERR,
620                         "Failed to initialize F2FS node manager");
621                 goto free_nm;
622         }
623
624         build_gc_manager(sbi);
625
626         /* get an inode for node space */
627         sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
628         if (IS_ERR(sbi->node_inode)) {
629                 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
630                 err = PTR_ERR(sbi->node_inode);
631                 goto free_nm;
632         }
633
634         /* if there are nt orphan nodes free them */
635         err = -EINVAL;
636         if (recover_orphan_inodes(sbi))
637                 goto free_node_inode;
638
639         /* read root inode and dentry */
640         root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
641         if (IS_ERR(root)) {
642                 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
643                 err = PTR_ERR(root);
644                 goto free_node_inode;
645         }
646         if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
647                 goto free_root_inode;
648
649         sb->s_root = d_make_root(root); /* allocate root dentry */
650         if (!sb->s_root) {
651                 err = -ENOMEM;
652                 goto free_root_inode;
653         }
654
655         /* recover fsynced data */
656         if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
657                 err = recover_fsync_data(sbi);
658                 if (err) {
659                         f2fs_msg(sb, KERN_ERR, "Failed to recover fsync data");
660                         goto free_root_inode;
661                 }
662         }
663
664         /* After POR, we can run background GC thread */
665         err = start_gc_thread(sbi);
666         if (err)
667                 goto fail;
668
669         err = f2fs_build_stats(sbi);
670         if (err)
671                 goto fail;
672
673         if (test_opt(sbi, DISCARD)) {
674                 struct request_queue *q = bdev_get_queue(sb->s_bdev);
675                 if (!blk_queue_discard(q))
676                         f2fs_msg(sb, KERN_WARNING,
677                                         "mounting with \"discard\" option, but "
678                                         "the device does not support discard");
679         }
680
681         return 0;
682 fail:
683         stop_gc_thread(sbi);
684 free_root_inode:
685         dput(sb->s_root);
686         sb->s_root = NULL;
687 free_node_inode:
688         iput(sbi->node_inode);
689 free_nm:
690         destroy_node_manager(sbi);
691 free_sm:
692         destroy_segment_manager(sbi);
693 free_cp:
694         kfree(sbi->ckpt);
695 free_meta_inode:
696         make_bad_inode(sbi->meta_inode);
697         iput(sbi->meta_inode);
698 free_sb_buf:
699         brelse(raw_super_buf);
700 free_sbi:
701         kfree(sbi);
702         return err;
703 }
704
705 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
706                         const char *dev_name, void *data)
707 {
708         return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
709 }
710
711 static struct file_system_type f2fs_fs_type = {
712         .owner          = THIS_MODULE,
713         .name           = "f2fs",
714         .mount          = f2fs_mount,
715         .kill_sb        = kill_block_super,
716         .fs_flags       = FS_REQUIRES_DEV,
717 };
718
719 static int __init init_inodecache(void)
720 {
721         f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
722                         sizeof(struct f2fs_inode_info), NULL);
723         if (f2fs_inode_cachep == NULL)
724                 return -ENOMEM;
725         return 0;
726 }
727
728 static void destroy_inodecache(void)
729 {
730         /*
731          * Make sure all delayed rcu free inodes are flushed before we
732          * destroy cache.
733          */
734         rcu_barrier();
735         kmem_cache_destroy(f2fs_inode_cachep);
736 }
737
738 static int __init init_f2fs_fs(void)
739 {
740         int err;
741
742         err = init_inodecache();
743         if (err)
744                 goto fail;
745         err = create_node_manager_caches();
746         if (err)
747                 goto fail;
748         err = create_gc_caches();
749         if (err)
750                 goto fail;
751         err = create_checkpoint_caches();
752         if (err)
753                 goto fail;
754         err = register_filesystem(&f2fs_fs_type);
755         if (err)
756                 goto fail;
757         f2fs_create_root_stats();
758 fail:
759         return err;
760 }
761
762 static void __exit exit_f2fs_fs(void)
763 {
764         f2fs_destroy_root_stats();
765         unregister_filesystem(&f2fs_fs_type);
766         destroy_checkpoint_caches();
767         destroy_gc_caches();
768         destroy_node_manager_caches();
769         destroy_inodecache();
770 }
771
772 module_init(init_f2fs_fs)
773 module_exit(exit_f2fs_fs)
774
775 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
776 MODULE_DESCRIPTION("Flash Friendly File System");
777 MODULE_LICENSE("GPL");