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