blob: 5830e537c376e0138be98054bb89831be5097bfa [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
56static void init_once(void *foo)
57{
58 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
59
60 memset(fi, 0, sizeof(*fi));
61 inode_init_once(&fi->vfs_inode);
62}
63
64static struct inode *f2fs_alloc_inode(struct super_block *sb)
65{
66 struct f2fs_inode_info *fi;
67
68 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO);
69 if (!fi)
70 return NULL;
71
72 init_once((void *) fi);
73
74 /* Initilize f2fs-specific inode info */
75 fi->vfs_inode.i_version = 1;
76 atomic_set(&fi->dirty_dents, 0);
77 fi->i_current_depth = 1;
78 fi->i_advise = 0;
79 rwlock_init(&fi->ext.ext_lock);
80
81 set_inode_flag(fi, FI_NEW_INODE);
82
83 return &fi->vfs_inode;
84}
85
86static void f2fs_i_callback(struct rcu_head *head)
87{
88 struct inode *inode = container_of(head, struct inode, i_rcu);
89 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
90}
91
Jaegeuk Kim25ca9232012-11-28 16:12:41 +090092static void f2fs_destroy_inode(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090093{
94 call_rcu(&inode->i_rcu, f2fs_i_callback);
95}
96
97static void f2fs_put_super(struct super_block *sb)
98{
99 struct f2fs_sb_info *sbi = F2FS_SB(sb);
100
101 f2fs_destroy_stats(sbi);
102 stop_gc_thread(sbi);
103
104 write_checkpoint(sbi, false, true);
105
106 iput(sbi->node_inode);
107 iput(sbi->meta_inode);
108
109 /* destroy f2fs internal modules */
110 destroy_node_manager(sbi);
111 destroy_segment_manager(sbi);
112
113 kfree(sbi->ckpt);
114
115 sb->s_fs_info = NULL;
116 brelse(sbi->raw_super_buf);
117 kfree(sbi);
118}
119
120int f2fs_sync_fs(struct super_block *sb, int sync)
121{
122 struct f2fs_sb_info *sbi = F2FS_SB(sb);
123 int ret = 0;
124
125 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
126 return 0;
127
128 if (sync)
129 write_checkpoint(sbi, false, false);
130
131 return ret;
132}
133
134static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
135{
136 struct super_block *sb = dentry->d_sb;
137 struct f2fs_sb_info *sbi = F2FS_SB(sb);
138 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
139 block_t total_count, user_block_count, start_count, ovp_count;
140
141 total_count = le64_to_cpu(sbi->raw_super->block_count);
142 user_block_count = sbi->user_block_count;
143 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
144 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
145 buf->f_type = F2FS_SUPER_MAGIC;
146 buf->f_bsize = sbi->blocksize;
147
148 buf->f_blocks = total_count - start_count;
149 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
150 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
151
152 buf->f_files = valid_inode_count(sbi);
153 buf->f_ffree = sbi->total_node_count - valid_node_count(sbi);
154
155 buf->f_namelen = F2FS_MAX_NAME_LEN;
156 buf->f_fsid.val[0] = (u32)id;
157 buf->f_fsid.val[1] = (u32)(id >> 32);
158
159 return 0;
160}
161
162static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
163{
164 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
165
166 if (test_opt(sbi, BG_GC))
167 seq_puts(seq, ",background_gc_on");
168 else
169 seq_puts(seq, ",background_gc_off");
170 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
171 seq_puts(seq, ",disable_roll_forward");
172 if (test_opt(sbi, DISCARD))
173 seq_puts(seq, ",discard");
174 if (test_opt(sbi, NOHEAP))
175 seq_puts(seq, ",no_heap_alloc");
176#ifdef CONFIG_F2FS_FS_XATTR
177 if (test_opt(sbi, XATTR_USER))
178 seq_puts(seq, ",user_xattr");
179 else
180 seq_puts(seq, ",nouser_xattr");
181#endif
182#ifdef CONFIG_F2FS_FS_POSIX_ACL
183 if (test_opt(sbi, POSIX_ACL))
184 seq_puts(seq, ",acl");
185 else
186 seq_puts(seq, ",noacl");
187#endif
188 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
189 seq_puts(seq, ",disable_ext_indentify");
190
191 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
192
193 return 0;
194}
195
196static struct super_operations f2fs_sops = {
197 .alloc_inode = f2fs_alloc_inode,
198 .destroy_inode = f2fs_destroy_inode,
199 .write_inode = f2fs_write_inode,
200 .show_options = f2fs_show_options,
201 .evict_inode = f2fs_evict_inode,
202 .put_super = f2fs_put_super,
203 .sync_fs = f2fs_sync_fs,
204 .statfs = f2fs_statfs,
205};
206
207static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
208 u64 ino, u32 generation)
209{
210 struct f2fs_sb_info *sbi = F2FS_SB(sb);
211 struct inode *inode;
212
213 if (ino < F2FS_ROOT_INO(sbi))
214 return ERR_PTR(-ESTALE);
215
216 /*
217 * f2fs_iget isn't quite right if the inode is currently unallocated!
218 * However f2fs_iget currently does appropriate checks to handle stale
219 * inodes so everything is OK.
220 */
221 inode = f2fs_iget(sb, ino);
222 if (IS_ERR(inode))
223 return ERR_CAST(inode);
224 if (generation && inode->i_generation != generation) {
225 /* we didn't find the right inode.. */
226 iput(inode);
227 return ERR_PTR(-ESTALE);
228 }
229 return inode;
230}
231
232static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
233 int fh_len, int fh_type)
234{
235 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
236 f2fs_nfs_get_inode);
237}
238
239static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
240 int fh_len, int fh_type)
241{
242 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
243 f2fs_nfs_get_inode);
244}
245
246static const struct export_operations f2fs_export_ops = {
247 .fh_to_dentry = f2fs_fh_to_dentry,
248 .fh_to_parent = f2fs_fh_to_parent,
249 .get_parent = f2fs_get_parent,
250};
251
252static int parse_options(struct f2fs_sb_info *sbi, char *options)
253{
254 substring_t args[MAX_OPT_ARGS];
255 char *p;
256 int arg = 0;
257
258 if (!options)
259 return 0;
260
261 while ((p = strsep(&options, ",")) != NULL) {
262 int token;
263 if (!*p)
264 continue;
265 /*
266 * Initialize args struct so we know whether arg was
267 * found; some options take optional arguments.
268 */
269 args[0].to = args[0].from = NULL;
270 token = match_token(p, f2fs_tokens, args);
271
272 switch (token) {
273 case Opt_gc_background_off:
274 clear_opt(sbi, BG_GC);
275 break;
276 case Opt_disable_roll_forward:
277 set_opt(sbi, DISABLE_ROLL_FORWARD);
278 break;
279 case Opt_discard:
280 set_opt(sbi, DISCARD);
281 break;
282 case Opt_noheap:
283 set_opt(sbi, NOHEAP);
284 break;
285#ifdef CONFIG_F2FS_FS_XATTR
286 case Opt_nouser_xattr:
287 clear_opt(sbi, XATTR_USER);
288 break;
289#else
290 case Opt_nouser_xattr:
291 pr_info("nouser_xattr options not supported\n");
292 break;
293#endif
294#ifdef CONFIG_F2FS_FS_POSIX_ACL
295 case Opt_noacl:
296 clear_opt(sbi, POSIX_ACL);
297 break;
298#else
299 case Opt_noacl:
300 pr_info("noacl options not supported\n");
301 break;
302#endif
303 case Opt_active_logs:
304 if (args->from && match_int(args, &arg))
305 return -EINVAL;
306 if (arg != 2 && arg != 4 && arg != 6)
307 return -EINVAL;
308 sbi->active_logs = arg;
309 break;
310 case Opt_disable_ext_identify:
311 set_opt(sbi, DISABLE_EXT_IDENTIFY);
312 break;
313 default:
Namjae Jeon72ce6092012-12-01 10:55:32 +0900314 pr_err("Unrecognized mount option \"%s\" or missing value\n",
315 p);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900316 return -EINVAL;
317 }
318 }
319 return 0;
320}
321
322static loff_t max_file_size(unsigned bits)
323{
324 loff_t result = ADDRS_PER_INODE;
325 loff_t leaf_count = ADDRS_PER_BLOCK;
326
327 /* two direct node blocks */
328 result += (leaf_count * 2);
329
330 /* two indirect node blocks */
331 leaf_count *= NIDS_PER_BLOCK;
332 result += (leaf_count * 2);
333
334 /* one double indirect node block */
335 leaf_count *= NIDS_PER_BLOCK;
336 result += leaf_count;
337
338 result <<= bits;
339 return result;
340}
341
342static int sanity_check_raw_super(struct f2fs_super_block *raw_super)
343{
344 unsigned int blocksize;
345
346 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic))
347 return 1;
348
349 /* Currently, support only 4KB block size */
350 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
351 if (blocksize != PAGE_CACHE_SIZE)
352 return 1;
353 if (le32_to_cpu(raw_super->log_sectorsize) !=
354 F2FS_LOG_SECTOR_SIZE)
355 return 1;
356 if (le32_to_cpu(raw_super->log_sectors_per_block) !=
357 F2FS_LOG_SECTORS_PER_BLOCK)
358 return 1;
359 return 0;
360}
361
362static int sanity_check_ckpt(struct f2fs_super_block *raw_super,
363 struct f2fs_checkpoint *ckpt)
364{
365 unsigned int total, fsmeta;
366
367 total = le32_to_cpu(raw_super->segment_count);
368 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
369 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
370 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
371 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
372 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
373
374 if (fsmeta >= total)
375 return 1;
376 return 0;
377}
378
379static void init_sb_info(struct f2fs_sb_info *sbi)
380{
381 struct f2fs_super_block *raw_super = sbi->raw_super;
382 int i;
383
384 sbi->log_sectors_per_block =
385 le32_to_cpu(raw_super->log_sectors_per_block);
386 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
387 sbi->blocksize = 1 << sbi->log_blocksize;
388 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
389 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
390 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
391 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
392 sbi->total_sections = le32_to_cpu(raw_super->section_count);
393 sbi->total_node_count =
394 (le32_to_cpu(raw_super->segment_count_nat) / 2)
395 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
396 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
397 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
398 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
399
400 for (i = 0; i < NR_COUNT_TYPE; i++)
401 atomic_set(&sbi->nr_pages[i], 0);
402}
403
404static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
405{
406 struct f2fs_sb_info *sbi;
407 struct f2fs_super_block *raw_super;
408 struct buffer_head *raw_super_buf;
409 struct inode *root;
410 long err = -EINVAL;
411 int i;
412
413 /* allocate memory for f2fs-specific super block info */
414 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
415 if (!sbi)
416 return -ENOMEM;
417
418 /* set a temporary block size */
419 if (!sb_set_blocksize(sb, F2FS_BLKSIZE))
420 goto free_sbi;
421
422 /* read f2fs raw super block */
423 raw_super_buf = sb_bread(sb, 0);
424 if (!raw_super_buf) {
425 err = -EIO;
426 goto free_sbi;
427 }
428 raw_super = (struct f2fs_super_block *)
429 ((char *)raw_super_buf->b_data + F2FS_SUPER_OFFSET);
430
431 /* init some FS parameters */
432 sbi->active_logs = NR_CURSEG_TYPE;
433
434 set_opt(sbi, BG_GC);
435
436#ifdef CONFIG_F2FS_FS_XATTR
437 set_opt(sbi, XATTR_USER);
438#endif
439#ifdef CONFIG_F2FS_FS_POSIX_ACL
440 set_opt(sbi, POSIX_ACL);
441#endif
442 /* parse mount options */
443 if (parse_options(sbi, (char *)data))
444 goto free_sb_buf;
445
446 /* sanity checking of raw super */
447 if (sanity_check_raw_super(raw_super))
448 goto free_sb_buf;
449
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900450 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900451 sb->s_max_links = F2FS_LINK_MAX;
452 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
453
454 sb->s_op = &f2fs_sops;
455 sb->s_xattr = f2fs_xattr_handlers;
456 sb->s_export_op = &f2fs_export_ops;
457 sb->s_magic = F2FS_SUPER_MAGIC;
458 sb->s_fs_info = sbi;
459 sb->s_time_gran = 1;
460 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
461 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
462 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
463
464 /* init f2fs-specific super block info */
465 sbi->sb = sb;
466 sbi->raw_super = raw_super;
467 sbi->raw_super_buf = raw_super_buf;
468 mutex_init(&sbi->gc_mutex);
469 mutex_init(&sbi->write_inode);
470 mutex_init(&sbi->writepages);
471 mutex_init(&sbi->cp_mutex);
472 for (i = 0; i < NR_LOCK_TYPE; i++)
473 mutex_init(&sbi->fs_lock[i]);
474 sbi->por_doing = 0;
475 spin_lock_init(&sbi->stat_lock);
476 init_rwsem(&sbi->bio_sem);
477 init_sb_info(sbi);
478
479 /* get an inode for meta space */
480 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
481 if (IS_ERR(sbi->meta_inode)) {
482 err = PTR_ERR(sbi->meta_inode);
483 goto free_sb_buf;
484 }
485
486 err = get_valid_checkpoint(sbi);
487 if (err)
488 goto free_meta_inode;
489
490 /* sanity checking of checkpoint */
491 err = -EINVAL;
492 if (sanity_check_ckpt(raw_super, sbi->ckpt))
493 goto free_cp;
494
495 sbi->total_valid_node_count =
496 le32_to_cpu(sbi->ckpt->valid_node_count);
497 sbi->total_valid_inode_count =
498 le32_to_cpu(sbi->ckpt->valid_inode_count);
499 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
500 sbi->total_valid_block_count =
501 le64_to_cpu(sbi->ckpt->valid_block_count);
502 sbi->last_valid_block_count = sbi->total_valid_block_count;
503 sbi->alloc_valid_block_count = 0;
504 INIT_LIST_HEAD(&sbi->dir_inode_list);
505 spin_lock_init(&sbi->dir_inode_lock);
506
507 /* init super block */
508 if (!sb_set_blocksize(sb, sbi->blocksize))
509 goto free_cp;
510
511 init_orphan_info(sbi);
512
513 /* setup f2fs internal modules */
514 err = build_segment_manager(sbi);
515 if (err)
516 goto free_sm;
517 err = build_node_manager(sbi);
518 if (err)
519 goto free_nm;
520
521 build_gc_manager(sbi);
522
523 /* get an inode for node space */
524 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
525 if (IS_ERR(sbi->node_inode)) {
526 err = PTR_ERR(sbi->node_inode);
527 goto free_nm;
528 }
529
530 /* if there are nt orphan nodes free them */
531 err = -EINVAL;
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900532 if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG) &&
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900533 recover_orphan_inodes(sbi))
534 goto free_node_inode;
535
536 /* read root inode and dentry */
537 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
538 if (IS_ERR(root)) {
539 err = PTR_ERR(root);
540 goto free_node_inode;
541 }
542 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
543 goto free_root_inode;
544
545 sb->s_root = d_make_root(root); /* allocate root dentry */
546 if (!sb->s_root) {
547 err = -ENOMEM;
548 goto free_root_inode;
549 }
550
551 /* recover fsynced data */
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900552 if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG) &&
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900553 !test_opt(sbi, DISABLE_ROLL_FORWARD))
554 recover_fsync_data(sbi);
555
556 /* After POR, we can run background GC thread */
557 err = start_gc_thread(sbi);
558 if (err)
559 goto fail;
560
561 err = f2fs_build_stats(sbi);
562 if (err)
563 goto fail;
564
565 return 0;
566fail:
567 stop_gc_thread(sbi);
568free_root_inode:
569 dput(sb->s_root);
570 sb->s_root = NULL;
571free_node_inode:
572 iput(sbi->node_inode);
573free_nm:
574 destroy_node_manager(sbi);
575free_sm:
576 destroy_segment_manager(sbi);
577free_cp:
578 kfree(sbi->ckpt);
579free_meta_inode:
580 make_bad_inode(sbi->meta_inode);
581 iput(sbi->meta_inode);
582free_sb_buf:
583 brelse(raw_super_buf);
584free_sbi:
585 kfree(sbi);
586 return err;
587}
588
589static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
590 const char *dev_name, void *data)
591{
592 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
593}
594
595static struct file_system_type f2fs_fs_type = {
596 .owner = THIS_MODULE,
597 .name = "f2fs",
598 .mount = f2fs_mount,
599 .kill_sb = kill_block_super,
600 .fs_flags = FS_REQUIRES_DEV,
601};
602
603static int init_inodecache(void)
604{
605 f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
606 sizeof(struct f2fs_inode_info), NULL);
607 if (f2fs_inode_cachep == NULL)
608 return -ENOMEM;
609 return 0;
610}
611
612static void destroy_inodecache(void)
613{
614 /*
615 * Make sure all delayed rcu free inodes are flushed before we
616 * destroy cache.
617 */
618 rcu_barrier();
619 kmem_cache_destroy(f2fs_inode_cachep);
620}
621
622static int __init init_f2fs_fs(void)
623{
624 int err;
625
626 err = init_inodecache();
627 if (err)
628 goto fail;
629 err = create_node_manager_caches();
630 if (err)
631 goto fail;
632 err = create_gc_caches();
633 if (err)
634 goto fail;
635 err = create_checkpoint_caches();
636 if (err)
637 goto fail;
638 return register_filesystem(&f2fs_fs_type);
639fail:
640 return err;
641}
642
643static void __exit exit_f2fs_fs(void)
644{
645 destroy_root_stats();
646 unregister_filesystem(&f2fs_fs_type);
647 destroy_checkpoint_caches();
648 destroy_gc_caches();
649 destroy_node_manager_caches();
650 destroy_inodecache();
651}
652
653module_init(init_f2fs_fs)
654module_exit(exit_f2fs_fs)
655
656MODULE_AUTHOR("Samsung Electronics's Praesto Team");
657MODULE_DESCRIPTION("Flash Friendly File System");
658MODULE_LICENSE("GPL");