f2fs: account for orphan inodes during recovery
[deliverable/linux.git] / fs / f2fs / super.c
CommitLineData
0a8165d7 1/*
aff063e2
JK
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>
aff063e2
JK
15#include <linux/buffer_head.h>
16#include <linux/backing-dev.h>
17#include <linux/kthread.h>
18#include <linux/parser.h>
19#include <linux/mount.h>
20#include <linux/seq_file.h>
5e176d54 21#include <linux/proc_fs.h>
aff063e2
JK
22#include <linux/random.h>
23#include <linux/exportfs.h>
d3ee456d 24#include <linux/blkdev.h>
aff063e2 25#include <linux/f2fs_fs.h>
b59d0bae 26#include <linux/sysfs.h>
aff063e2
JK
27
28#include "f2fs.h"
29#include "node.h"
5ec4e49f 30#include "segment.h"
aff063e2 31#include "xattr.h"
b59d0bae 32#include "gc.h"
aff063e2 33
a2a4a7e4
NJ
34#define CREATE_TRACE_POINTS
35#include <trace/events/f2fs.h>
36
5e176d54 37static struct proc_dir_entry *f2fs_proc_root;
aff063e2 38static struct kmem_cache *f2fs_inode_cachep;
b59d0bae 39static struct kset *f2fs_kset;
aff063e2
JK
40
41enum {
696c018c 42 Opt_gc_background,
aff063e2
JK
43 Opt_disable_roll_forward,
44 Opt_discard,
45 Opt_noheap,
46 Opt_nouser_xattr,
47 Opt_noacl,
48 Opt_active_logs,
49 Opt_disable_ext_identify,
444c580f 50 Opt_inline_xattr,
aff063e2
JK
51 Opt_err,
52};
53
54static match_table_t f2fs_tokens = {
696c018c 55 {Opt_gc_background, "background_gc=%s"},
aff063e2
JK
56 {Opt_disable_roll_forward, "disable_roll_forward"},
57 {Opt_discard, "discard"},
58 {Opt_noheap, "no_heap"},
59 {Opt_nouser_xattr, "nouser_xattr"},
60 {Opt_noacl, "noacl"},
61 {Opt_active_logs, "active_logs=%u"},
62 {Opt_disable_ext_identify, "disable_ext_identify"},
444c580f 63 {Opt_inline_xattr, "inline_xattr"},
aff063e2
JK
64 {Opt_err, NULL},
65};
66
b59d0bae
NJ
67/* Sysfs support for f2fs */
68struct f2fs_attr {
69 struct attribute attr;
70 ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
71 ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
72 const char *, size_t);
73 int offset;
74};
75
76static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
77 struct f2fs_sb_info *sbi, char *buf)
78{
79 struct f2fs_gc_kthread *gc_kth = sbi->gc_thread;
80 unsigned int *ui;
81
82 if (!gc_kth)
83 return -EINVAL;
84
85 ui = (unsigned int *)(((char *)gc_kth) + a->offset);
86
87 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
88}
89
90static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
91 struct f2fs_sb_info *sbi,
92 const char *buf, size_t count)
93{
94 struct f2fs_gc_kthread *gc_kth = sbi->gc_thread;
95 unsigned long t;
96 unsigned int *ui;
97 ssize_t ret;
98
99 if (!gc_kth)
100 return -EINVAL;
101
102 ui = (unsigned int *)(((char *)gc_kth) + a->offset);
103
104 ret = kstrtoul(skip_spaces(buf), 0, &t);
105 if (ret < 0)
106 return ret;
107 *ui = t;
108 return count;
109}
110
111static ssize_t f2fs_attr_show(struct kobject *kobj,
112 struct attribute *attr, char *buf)
113{
114 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
115 s_kobj);
116 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
117
118 return a->show ? a->show(a, sbi, buf) : 0;
119}
120
121static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
122 const char *buf, size_t len)
123{
124 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
125 s_kobj);
126 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
127
128 return a->store ? a->store(a, sbi, buf, len) : 0;
129}
130
131static void f2fs_sb_release(struct kobject *kobj)
132{
133 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
134 s_kobj);
135 complete(&sbi->s_kobj_unregister);
136}
137
138#define F2FS_ATTR_OFFSET(_name, _mode, _show, _store, _elname) \
139static struct f2fs_attr f2fs_attr_##_name = { \
140 .attr = {.name = __stringify(_name), .mode = _mode }, \
141 .show = _show, \
142 .store = _store, \
143 .offset = offsetof(struct f2fs_gc_kthread, _elname), \
144}
145
146#define F2FS_RW_ATTR(name, elname) \
147 F2FS_ATTR_OFFSET(name, 0644, f2fs_sbi_show, f2fs_sbi_store, elname)
148
149F2FS_RW_ATTR(gc_min_sleep_time, min_sleep_time);
150F2FS_RW_ATTR(gc_max_sleep_time, max_sleep_time);
151F2FS_RW_ATTR(gc_no_gc_sleep_time, no_gc_sleep_time);
d2dc095f 152F2FS_RW_ATTR(gc_idle, gc_idle);
b59d0bae
NJ
153
154#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
155static struct attribute *f2fs_attrs[] = {
156 ATTR_LIST(gc_min_sleep_time),
157 ATTR_LIST(gc_max_sleep_time),
158 ATTR_LIST(gc_no_gc_sleep_time),
d2dc095f 159 ATTR_LIST(gc_idle),
b59d0bae
NJ
160 NULL,
161};
162
163static const struct sysfs_ops f2fs_attr_ops = {
164 .show = f2fs_attr_show,
165 .store = f2fs_attr_store,
166};
167
168static struct kobj_type f2fs_ktype = {
169 .default_attrs = f2fs_attrs,
170 .sysfs_ops = &f2fs_attr_ops,
171 .release = f2fs_sb_release,
172};
173
a07ef784
NJ
174void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
175{
176 struct va_format vaf;
177 va_list args;
178
179 va_start(args, fmt);
180 vaf.fmt = fmt;
181 vaf.va = &args;
182 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
183 va_end(args);
184}
185
aff063e2
JK
186static void init_once(void *foo)
187{
188 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
189
aff063e2
JK
190 inode_init_once(&fi->vfs_inode);
191}
192
696c018c
NJ
193static int parse_options(struct super_block *sb, char *options)
194{
195 struct f2fs_sb_info *sbi = F2FS_SB(sb);
196 substring_t args[MAX_OPT_ARGS];
197 char *p, *name;
198 int arg = 0;
199
200 if (!options)
201 return 0;
202
203 while ((p = strsep(&options, ",")) != NULL) {
204 int token;
205 if (!*p)
206 continue;
207 /*
208 * Initialize args struct so we know whether arg was
209 * found; some options take optional arguments.
210 */
211 args[0].to = args[0].from = NULL;
212 token = match_token(p, f2fs_tokens, args);
213
214 switch (token) {
215 case Opt_gc_background:
216 name = match_strdup(&args[0]);
217
218 if (!name)
219 return -ENOMEM;
220 if (!strncmp(name, "on", 2))
221 set_opt(sbi, BG_GC);
222 else if (!strncmp(name, "off", 3))
223 clear_opt(sbi, BG_GC);
224 else {
225 kfree(name);
226 return -EINVAL;
227 }
228 kfree(name);
229 break;
230 case Opt_disable_roll_forward:
231 set_opt(sbi, DISABLE_ROLL_FORWARD);
232 break;
233 case Opt_discard:
234 set_opt(sbi, DISCARD);
235 break;
236 case Opt_noheap:
237 set_opt(sbi, NOHEAP);
238 break;
239#ifdef CONFIG_F2FS_FS_XATTR
240 case Opt_nouser_xattr:
241 clear_opt(sbi, XATTR_USER);
242 break;
444c580f
JK
243 case Opt_inline_xattr:
244 set_opt(sbi, INLINE_XATTR);
245 break;
696c018c
NJ
246#else
247 case Opt_nouser_xattr:
248 f2fs_msg(sb, KERN_INFO,
249 "nouser_xattr options not supported");
250 break;
444c580f
JK
251 case Opt_inline_xattr:
252 f2fs_msg(sb, KERN_INFO,
253 "inline_xattr options not supported");
254 break;
696c018c
NJ
255#endif
256#ifdef CONFIG_F2FS_FS_POSIX_ACL
257 case Opt_noacl:
258 clear_opt(sbi, POSIX_ACL);
259 break;
260#else
261 case Opt_noacl:
262 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
263 break;
264#endif
265 case Opt_active_logs:
266 if (args->from && match_int(args, &arg))
267 return -EINVAL;
268 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
269 return -EINVAL;
270 sbi->active_logs = arg;
271 break;
272 case Opt_disable_ext_identify:
273 set_opt(sbi, DISABLE_EXT_IDENTIFY);
274 break;
275 default:
276 f2fs_msg(sb, KERN_ERR,
277 "Unrecognized mount option \"%s\" or missing value",
278 p);
279 return -EINVAL;
280 }
281 }
282 return 0;
283}
284
aff063e2
JK
285static struct inode *f2fs_alloc_inode(struct super_block *sb)
286{
287 struct f2fs_inode_info *fi;
288
289 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO);
290 if (!fi)
291 return NULL;
292
293 init_once((void *) fi);
294
434720fa 295 /* Initialize f2fs-specific inode info */
aff063e2
JK
296 fi->vfs_inode.i_version = 1;
297 atomic_set(&fi->dirty_dents, 0);
298 fi->i_current_depth = 1;
299 fi->i_advise = 0;
300 rwlock_init(&fi->ext.ext_lock);
301
302 set_inode_flag(fi, FI_NEW_INODE);
303
444c580f
JK
304 if (test_opt(F2FS_SB(sb), INLINE_XATTR))
305 set_inode_flag(fi, FI_INLINE_XATTR);
306
aff063e2
JK
307 return &fi->vfs_inode;
308}
309
531ad7d5
JK
310static int f2fs_drop_inode(struct inode *inode)
311{
312 /*
313 * This is to avoid a deadlock condition like below.
314 * writeback_single_inode(inode)
315 * - f2fs_write_data_page
316 * - f2fs_gc -> iput -> evict
317 * - inode_wait_for_writeback(inode)
318 */
319 if (!inode_unhashed(inode) && inode->i_state & I_SYNC)
320 return 0;
321 return generic_drop_inode(inode);
322}
323
b3783873
JK
324/*
325 * f2fs_dirty_inode() is called from __mark_inode_dirty()
326 *
327 * We should call set_dirty_inode to write the dirty inode through write_inode.
328 */
329static void f2fs_dirty_inode(struct inode *inode, int flags)
330{
331 set_inode_flag(F2FS_I(inode), FI_DIRTY_INODE);
b3783873
JK
332}
333
aff063e2
JK
334static void f2fs_i_callback(struct rcu_head *head)
335{
336 struct inode *inode = container_of(head, struct inode, i_rcu);
337 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
338}
339
25ca923b 340static void f2fs_destroy_inode(struct inode *inode)
aff063e2
JK
341{
342 call_rcu(&inode->i_rcu, f2fs_i_callback);
343}
344
345static void f2fs_put_super(struct super_block *sb)
346{
347 struct f2fs_sb_info *sbi = F2FS_SB(sb);
348
5e176d54
JK
349 if (sbi->s_proc) {
350 remove_proc_entry("segment_info", sbi->s_proc);
351 remove_proc_entry(sb->s_id, f2fs_proc_root);
352 }
b59d0bae 353 kobject_del(&sbi->s_kobj);
5e176d54 354
aff063e2
JK
355 f2fs_destroy_stats(sbi);
356 stop_gc_thread(sbi);
357
43727527 358 write_checkpoint(sbi, true);
aff063e2
JK
359
360 iput(sbi->node_inode);
361 iput(sbi->meta_inode);
362
363 /* destroy f2fs internal modules */
364 destroy_node_manager(sbi);
365 destroy_segment_manager(sbi);
366
367 kfree(sbi->ckpt);
b59d0bae
NJ
368 kobject_put(&sbi->s_kobj);
369 wait_for_completion(&sbi->s_kobj_unregister);
aff063e2
JK
370
371 sb->s_fs_info = NULL;
372 brelse(sbi->raw_super_buf);
373 kfree(sbi);
374}
375
376int f2fs_sync_fs(struct super_block *sb, int sync)
377{
378 struct f2fs_sb_info *sbi = F2FS_SB(sb);
aff063e2 379
a2a4a7e4
NJ
380 trace_f2fs_sync_fs(sb, sync);
381
aff063e2
JK
382 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
383 return 0;
384
b7473754
JK
385 if (sync) {
386 mutex_lock(&sbi->gc_mutex);
43727527 387 write_checkpoint(sbi, false);
b7473754
JK
388 mutex_unlock(&sbi->gc_mutex);
389 } else {
7d82db83 390 f2fs_balance_fs(sbi);
b7473754 391 }
aff063e2 392
64c576fe 393 return 0;
aff063e2
JK
394}
395
d6212a5f
CL
396static int f2fs_freeze(struct super_block *sb)
397{
398 int err;
399
77888c1e 400 if (f2fs_readonly(sb))
d6212a5f
CL
401 return 0;
402
403 err = f2fs_sync_fs(sb, 1);
404 return err;
405}
406
407static int f2fs_unfreeze(struct super_block *sb)
408{
409 return 0;
410}
411
aff063e2
JK
412static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
413{
414 struct super_block *sb = dentry->d_sb;
415 struct f2fs_sb_info *sbi = F2FS_SB(sb);
416 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
417 block_t total_count, user_block_count, start_count, ovp_count;
418
419 total_count = le64_to_cpu(sbi->raw_super->block_count);
420 user_block_count = sbi->user_block_count;
421 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
422 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
423 buf->f_type = F2FS_SUPER_MAGIC;
424 buf->f_bsize = sbi->blocksize;
425
426 buf->f_blocks = total_count - start_count;
427 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
428 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
429
1362b5e3
JK
430 buf->f_files = sbi->total_node_count;
431 buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
aff063e2 432
5a20d339 433 buf->f_namelen = F2FS_NAME_LEN;
aff063e2
JK
434 buf->f_fsid.val[0] = (u32)id;
435 buf->f_fsid.val[1] = (u32)(id >> 32);
436
437 return 0;
438}
439
440static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
441{
442 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
443
696c018c
NJ
444 if (!(root->d_sb->s_flags & MS_RDONLY) && test_opt(sbi, BG_GC))
445 seq_printf(seq, ",background_gc=%s", "on");
aff063e2 446 else
696c018c 447 seq_printf(seq, ",background_gc=%s", "off");
aff063e2
JK
448 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
449 seq_puts(seq, ",disable_roll_forward");
450 if (test_opt(sbi, DISCARD))
451 seq_puts(seq, ",discard");
452 if (test_opt(sbi, NOHEAP))
453 seq_puts(seq, ",no_heap_alloc");
454#ifdef CONFIG_F2FS_FS_XATTR
455 if (test_opt(sbi, XATTR_USER))
456 seq_puts(seq, ",user_xattr");
457 else
458 seq_puts(seq, ",nouser_xattr");
444c580f
JK
459 if (test_opt(sbi, INLINE_XATTR))
460 seq_puts(seq, ",inline_xattr");
aff063e2
JK
461#endif
462#ifdef CONFIG_F2FS_FS_POSIX_ACL
463 if (test_opt(sbi, POSIX_ACL))
464 seq_puts(seq, ",acl");
465 else
466 seq_puts(seq, ",noacl");
467#endif
468 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
aa43507f 469 seq_puts(seq, ",disable_ext_identify");
aff063e2
JK
470
471 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
472
473 return 0;
474}
475
5e176d54
JK
476static int segment_info_seq_show(struct seq_file *seq, void *offset)
477{
478 struct super_block *sb = seq->private;
479 struct f2fs_sb_info *sbi = F2FS_SB(sb);
480 unsigned int total_segs = le32_to_cpu(sbi->raw_super->segment_count_main);
481 int i;
482
483 for (i = 0; i < total_segs; i++) {
484 seq_printf(seq, "%u", get_valid_blocks(sbi, i, 1));
485 if (i != 0 && (i % 10) == 0)
486 seq_puts(seq, "\n");
487 else
488 seq_puts(seq, " ");
489 }
490 return 0;
491}
492
493static int segment_info_open_fs(struct inode *inode, struct file *file)
494{
495 return single_open(file, segment_info_seq_show, PDE_DATA(inode));
496}
497
498static const struct file_operations f2fs_seq_segment_info_fops = {
499 .owner = THIS_MODULE,
500 .open = segment_info_open_fs,
501 .read = seq_read,
502 .llseek = seq_lseek,
503 .release = single_release,
504};
505
696c018c
NJ
506static int f2fs_remount(struct super_block *sb, int *flags, char *data)
507{
508 struct f2fs_sb_info *sbi = F2FS_SB(sb);
509 struct f2fs_mount_info org_mount_opt;
510 int err, active_logs;
511
512 /*
513 * Save the old mount options in case we
514 * need to restore them.
515 */
516 org_mount_opt = sbi->mount_opt;
517 active_logs = sbi->active_logs;
518
519 /* parse mount options */
520 err = parse_options(sb, data);
521 if (err)
522 goto restore_opts;
523
524 /*
525 * Previous and new state of filesystem is RO,
526 * so no point in checking GC conditions.
527 */
528 if ((sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY))
529 goto skip;
530
531 /*
532 * We stop the GC thread if FS is mounted as RO
533 * or if background_gc = off is passed in mount
534 * option. Also sync the filesystem.
535 */
536 if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
537 if (sbi->gc_thread) {
538 stop_gc_thread(sbi);
539 f2fs_sync_fs(sb, 1);
540 }
541 } else if (test_opt(sbi, BG_GC) && !sbi->gc_thread) {
542 err = start_gc_thread(sbi);
543 if (err)
544 goto restore_opts;
545 }
546skip:
547 /* Update the POSIXACL Flag */
548 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
549 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
550 return 0;
551
552restore_opts:
553 sbi->mount_opt = org_mount_opt;
554 sbi->active_logs = active_logs;
555 return err;
556}
557
aff063e2
JK
558static struct super_operations f2fs_sops = {
559 .alloc_inode = f2fs_alloc_inode,
531ad7d5 560 .drop_inode = f2fs_drop_inode,
aff063e2
JK
561 .destroy_inode = f2fs_destroy_inode,
562 .write_inode = f2fs_write_inode,
b3783873 563 .dirty_inode = f2fs_dirty_inode,
aff063e2
JK
564 .show_options = f2fs_show_options,
565 .evict_inode = f2fs_evict_inode,
566 .put_super = f2fs_put_super,
567 .sync_fs = f2fs_sync_fs,
d6212a5f
CL
568 .freeze_fs = f2fs_freeze,
569 .unfreeze_fs = f2fs_unfreeze,
aff063e2 570 .statfs = f2fs_statfs,
696c018c 571 .remount_fs = f2fs_remount,
aff063e2
JK
572};
573
574static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
575 u64 ino, u32 generation)
576{
577 struct f2fs_sb_info *sbi = F2FS_SB(sb);
578 struct inode *inode;
579
580 if (ino < F2FS_ROOT_INO(sbi))
581 return ERR_PTR(-ESTALE);
582
583 /*
584 * f2fs_iget isn't quite right if the inode is currently unallocated!
585 * However f2fs_iget currently does appropriate checks to handle stale
586 * inodes so everything is OK.
587 */
588 inode = f2fs_iget(sb, ino);
589 if (IS_ERR(inode))
590 return ERR_CAST(inode);
591 if (generation && inode->i_generation != generation) {
592 /* we didn't find the right inode.. */
593 iput(inode);
594 return ERR_PTR(-ESTALE);
595 }
596 return inode;
597}
598
599static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
600 int fh_len, int fh_type)
601{
602 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
603 f2fs_nfs_get_inode);
604}
605
606static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
607 int fh_len, int fh_type)
608{
609 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
610 f2fs_nfs_get_inode);
611}
612
613static const struct export_operations f2fs_export_ops = {
614 .fh_to_dentry = f2fs_fh_to_dentry,
615 .fh_to_parent = f2fs_fh_to_parent,
616 .get_parent = f2fs_get_parent,
617};
618
aff063e2
JK
619static loff_t max_file_size(unsigned bits)
620{
de93653f 621 loff_t result = (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS);
aff063e2
JK
622 loff_t leaf_count = ADDRS_PER_BLOCK;
623
624 /* two direct node blocks */
625 result += (leaf_count * 2);
626
627 /* two indirect node blocks */
628 leaf_count *= NIDS_PER_BLOCK;
629 result += (leaf_count * 2);
630
631 /* one double indirect node block */
632 leaf_count *= NIDS_PER_BLOCK;
633 result += leaf_count;
634
635 result <<= bits;
636 return result;
637}
638
a07ef784
NJ
639static int sanity_check_raw_super(struct super_block *sb,
640 struct f2fs_super_block *raw_super)
aff063e2
JK
641{
642 unsigned int blocksize;
643
a07ef784
NJ
644 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
645 f2fs_msg(sb, KERN_INFO,
646 "Magic Mismatch, valid(0x%x) - read(0x%x)",
647 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
aff063e2 648 return 1;
a07ef784 649 }
aff063e2 650
5c9b4692 651 /* Currently, support only 4KB page cache size */
652 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
653 f2fs_msg(sb, KERN_INFO,
14d7e9de 654 "Invalid page_cache_size (%lu), supports only 4KB\n",
5c9b4692 655 PAGE_CACHE_SIZE);
656 return 1;
657 }
658
aff063e2
JK
659 /* Currently, support only 4KB block size */
660 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
5c9b4692 661 if (blocksize != F2FS_BLKSIZE) {
a07ef784
NJ
662 f2fs_msg(sb, KERN_INFO,
663 "Invalid blocksize (%u), supports only 4KB\n",
664 blocksize);
aff063e2 665 return 1;
a07ef784 666 }
5c9b4692 667
aff063e2 668 if (le32_to_cpu(raw_super->log_sectorsize) !=
a07ef784
NJ
669 F2FS_LOG_SECTOR_SIZE) {
670 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
aff063e2 671 return 1;
a07ef784 672 }
aff063e2 673 if (le32_to_cpu(raw_super->log_sectors_per_block) !=
a07ef784
NJ
674 F2FS_LOG_SECTORS_PER_BLOCK) {
675 f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
aff063e2 676 return 1;
a07ef784 677 }
aff063e2
JK
678 return 0;
679}
680
577e3495 681static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
aff063e2
JK
682{
683 unsigned int total, fsmeta;
577e3495
JK
684 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
685 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
aff063e2
JK
686
687 total = le32_to_cpu(raw_super->segment_count);
688 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
689 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
690 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
691 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
692 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
693
694 if (fsmeta >= total)
695 return 1;
577e3495
JK
696
697 if (is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
698 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
699 return 1;
700 }
aff063e2
JK
701 return 0;
702}
703
704static void init_sb_info(struct f2fs_sb_info *sbi)
705{
706 struct f2fs_super_block *raw_super = sbi->raw_super;
707 int i;
708
709 sbi->log_sectors_per_block =
710 le32_to_cpu(raw_super->log_sectors_per_block);
711 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
712 sbi->blocksize = 1 << sbi->log_blocksize;
713 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
714 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
715 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
716 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
717 sbi->total_sections = le32_to_cpu(raw_super->section_count);
718 sbi->total_node_count =
719 (le32_to_cpu(raw_super->segment_count_nat) / 2)
720 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
721 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
722 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
723 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
5ec4e49f 724 sbi->cur_victim_sec = NULL_SECNO;
aff063e2
JK
725
726 for (i = 0; i < NR_COUNT_TYPE; i++)
727 atomic_set(&sbi->nr_pages[i], 0);
728}
729
14d7e9de 730static int validate_superblock(struct super_block *sb,
731 struct f2fs_super_block **raw_super,
732 struct buffer_head **raw_super_buf, sector_t block)
733{
734 const char *super = (block == 0 ? "first" : "second");
735
736 /* read f2fs raw super block */
737 *raw_super_buf = sb_bread(sb, block);
738 if (!*raw_super_buf) {
739 f2fs_msg(sb, KERN_ERR, "unable to read %s superblock",
740 super);
c0d39e65 741 return -EIO;
14d7e9de 742 }
743
744 *raw_super = (struct f2fs_super_block *)
745 ((char *)(*raw_super_buf)->b_data + F2FS_SUPER_OFFSET);
746
747 /* sanity checking of raw super */
748 if (!sanity_check_raw_super(sb, *raw_super))
749 return 0;
750
751 f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem "
752 "in %s superblock", super);
c0d39e65 753 return -EINVAL;
14d7e9de 754}
755
aff063e2
JK
756static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
757{
758 struct f2fs_sb_info *sbi;
759 struct f2fs_super_block *raw_super;
760 struct buffer_head *raw_super_buf;
761 struct inode *root;
762 long err = -EINVAL;
763 int i;
764
765 /* allocate memory for f2fs-specific super block info */
766 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
767 if (!sbi)
768 return -ENOMEM;
769
ff9234ad 770 /* set a block size */
a07ef784
NJ
771 if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
772 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
aff063e2 773 goto free_sbi;
a07ef784 774 }
aff063e2 775
c0d39e65
NJ
776 err = validate_superblock(sb, &raw_super, &raw_super_buf, 0);
777 if (err) {
14d7e9de 778 brelse(raw_super_buf);
c0d39e65
NJ
779 /* check secondary superblock when primary failed */
780 err = validate_superblock(sb, &raw_super, &raw_super_buf, 1);
781 if (err)
14d7e9de 782 goto free_sb_buf;
aff063e2 783 }
5fb08372 784 sb->s_fs_info = sbi;
aff063e2
JK
785 /* init some FS parameters */
786 sbi->active_logs = NR_CURSEG_TYPE;
787
788 set_opt(sbi, BG_GC);
789
790#ifdef CONFIG_F2FS_FS_XATTR
791 set_opt(sbi, XATTR_USER);
792#endif
793#ifdef CONFIG_F2FS_FS_POSIX_ACL
794 set_opt(sbi, POSIX_ACL);
795#endif
796 /* parse mount options */
5fb08372 797 err = parse_options(sb, (char *)data);
66348b72 798 if (err)
aff063e2
JK
799 goto free_sb_buf;
800
25ca923b 801 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
aff063e2
JK
802 sb->s_max_links = F2FS_LINK_MAX;
803 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
804
805 sb->s_op = &f2fs_sops;
806 sb->s_xattr = f2fs_xattr_handlers;
807 sb->s_export_op = &f2fs_export_ops;
808 sb->s_magic = F2FS_SUPER_MAGIC;
aff063e2
JK
809 sb->s_time_gran = 1;
810 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
811 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
812 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
813
814 /* init f2fs-specific super block info */
815 sbi->sb = sb;
816 sbi->raw_super = raw_super;
817 sbi->raw_super_buf = raw_super_buf;
818 mutex_init(&sbi->gc_mutex);
aff063e2
JK
819 mutex_init(&sbi->writepages);
820 mutex_init(&sbi->cp_mutex);
39936837 821 for (i = 0; i < NR_GLOBAL_LOCKS; i++)
aff063e2 822 mutex_init(&sbi->fs_lock[i]);
39936837 823 mutex_init(&sbi->node_write);
aff063e2
JK
824 sbi->por_doing = 0;
825 spin_lock_init(&sbi->stat_lock);
826 init_rwsem(&sbi->bio_sem);
827 init_sb_info(sbi);
828
829 /* get an inode for meta space */
830 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
831 if (IS_ERR(sbi->meta_inode)) {
a07ef784 832 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
aff063e2
JK
833 err = PTR_ERR(sbi->meta_inode);
834 goto free_sb_buf;
835 }
836
837 err = get_valid_checkpoint(sbi);
a07ef784
NJ
838 if (err) {
839 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
aff063e2 840 goto free_meta_inode;
a07ef784 841 }
aff063e2
JK
842
843 /* sanity checking of checkpoint */
844 err = -EINVAL;
577e3495 845 if (sanity_check_ckpt(sbi)) {
a07ef784 846 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
aff063e2 847 goto free_cp;
a07ef784 848 }
aff063e2
JK
849
850 sbi->total_valid_node_count =
851 le32_to_cpu(sbi->ckpt->valid_node_count);
852 sbi->total_valid_inode_count =
853 le32_to_cpu(sbi->ckpt->valid_inode_count);
854 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
855 sbi->total_valid_block_count =
856 le64_to_cpu(sbi->ckpt->valid_block_count);
857 sbi->last_valid_block_count = sbi->total_valid_block_count;
858 sbi->alloc_valid_block_count = 0;
859 INIT_LIST_HEAD(&sbi->dir_inode_list);
860 spin_lock_init(&sbi->dir_inode_lock);
861
aff063e2
JK
862 init_orphan_info(sbi);
863
864 /* setup f2fs internal modules */
865 err = build_segment_manager(sbi);
a07ef784
NJ
866 if (err) {
867 f2fs_msg(sb, KERN_ERR,
868 "Failed to initialize F2FS segment manager");
aff063e2 869 goto free_sm;
a07ef784 870 }
aff063e2 871 err = build_node_manager(sbi);
a07ef784
NJ
872 if (err) {
873 f2fs_msg(sb, KERN_ERR,
874 "Failed to initialize F2FS node manager");
aff063e2 875 goto free_nm;
a07ef784 876 }
aff063e2
JK
877
878 build_gc_manager(sbi);
879
880 /* get an inode for node space */
881 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
882 if (IS_ERR(sbi->node_inode)) {
a07ef784 883 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
aff063e2
JK
884 err = PTR_ERR(sbi->node_inode);
885 goto free_nm;
886 }
887
888 /* if there are nt orphan nodes free them */
889 err = -EINVAL;
30f0c758 890 if (recover_orphan_inodes(sbi))
aff063e2
JK
891 goto free_node_inode;
892
893 /* read root inode and dentry */
894 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
895 if (IS_ERR(root)) {
a07ef784 896 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
aff063e2
JK
897 err = PTR_ERR(root);
898 goto free_node_inode;
899 }
900 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
901 goto free_root_inode;
902
903 sb->s_root = d_make_root(root); /* allocate root dentry */
904 if (!sb->s_root) {
905 err = -ENOMEM;
906 goto free_root_inode;
907 }
908
909 /* recover fsynced data */
6ead1142
JK
910 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
911 err = recover_fsync_data(sbi);
bde582b2
CF
912 if (err)
913 f2fs_msg(sb, KERN_ERR,
914 "Cannot recover all fsync data errno=%ld", err);
6ead1142 915 }
aff063e2 916
696c018c
NJ
917 /*
918 * If filesystem is not mounted as read-only then
919 * do start the gc_thread.
920 */
921 if (!(sb->s_flags & MS_RDONLY)) {
922 /* After POR, we can run background GC thread.*/
923 err = start_gc_thread(sbi);
924 if (err)
925 goto fail;
926 }
aff063e2
JK
927
928 err = f2fs_build_stats(sbi);
929 if (err)
930 goto fail;
931
5e176d54
JK
932 if (f2fs_proc_root)
933 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
934
935 if (sbi->s_proc)
936 proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
937 &f2fs_seq_segment_info_fops, sb);
938
d3ee456d
NJ
939 if (test_opt(sbi, DISCARD)) {
940 struct request_queue *q = bdev_get_queue(sb->s_bdev);
941 if (!blk_queue_discard(q))
942 f2fs_msg(sb, KERN_WARNING,
943 "mounting with \"discard\" option, but "
944 "the device does not support discard");
945 }
946
b59d0bae
NJ
947 sbi->s_kobj.kset = f2fs_kset;
948 init_completion(&sbi->s_kobj_unregister);
949 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_ktype, NULL,
950 "%s", sb->s_id);
951 if (err)
952 goto fail;
953
aff063e2
JK
954 return 0;
955fail:
956 stop_gc_thread(sbi);
957free_root_inode:
958 dput(sb->s_root);
959 sb->s_root = NULL;
960free_node_inode:
961 iput(sbi->node_inode);
962free_nm:
963 destroy_node_manager(sbi);
964free_sm:
965 destroy_segment_manager(sbi);
966free_cp:
967 kfree(sbi->ckpt);
968free_meta_inode:
969 make_bad_inode(sbi->meta_inode);
970 iput(sbi->meta_inode);
971free_sb_buf:
972 brelse(raw_super_buf);
973free_sbi:
974 kfree(sbi);
975 return err;
976}
977
978static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
979 const char *dev_name, void *data)
980{
981 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
982}
983
984static struct file_system_type f2fs_fs_type = {
985 .owner = THIS_MODULE,
986 .name = "f2fs",
987 .mount = f2fs_mount,
988 .kill_sb = kill_block_super,
989 .fs_flags = FS_REQUIRES_DEV,
990};
7f78e035 991MODULE_ALIAS_FS("f2fs");
aff063e2 992
6e6093a8 993static int __init init_inodecache(void)
aff063e2
JK
994{
995 f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
996 sizeof(struct f2fs_inode_info), NULL);
997 if (f2fs_inode_cachep == NULL)
998 return -ENOMEM;
999 return 0;
1000}
1001
1002static void destroy_inodecache(void)
1003{
1004 /*
1005 * Make sure all delayed rcu free inodes are flushed before we
1006 * destroy cache.
1007 */
1008 rcu_barrier();
1009 kmem_cache_destroy(f2fs_inode_cachep);
1010}
1011
1012static int __init init_f2fs_fs(void)
1013{
1014 int err;
1015
1016 err = init_inodecache();
1017 if (err)
1018 goto fail;
1019 err = create_node_manager_caches();
1020 if (err)
9890ff3f 1021 goto free_inodecache;
aff063e2
JK
1022 err = create_gc_caches();
1023 if (err)
9890ff3f 1024 goto free_node_manager_caches;
aff063e2
JK
1025 err = create_checkpoint_caches();
1026 if (err)
9890ff3f 1027 goto free_gc_caches;
b59d0bae 1028 f2fs_kset = kset_create_and_add("f2fs", NULL, fs_kobj);
6e6b978c
WY
1029 if (!f2fs_kset) {
1030 err = -ENOMEM;
9890ff3f 1031 goto free_checkpoint_caches;
6e6b978c 1032 }
4589d25d
NJ
1033 err = register_filesystem(&f2fs_fs_type);
1034 if (err)
9890ff3f 1035 goto free_kset;
4589d25d 1036 f2fs_create_root_stats();
5e176d54 1037 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
9890ff3f
ZH
1038 return 0;
1039
1040free_kset:
1041 kset_unregister(f2fs_kset);
1042free_checkpoint_caches:
1043 destroy_checkpoint_caches();
1044free_gc_caches:
1045 destroy_gc_caches();
1046free_node_manager_caches:
1047 destroy_node_manager_caches();
1048free_inodecache:
1049 destroy_inodecache();
aff063e2
JK
1050fail:
1051 return err;
1052}
1053
1054static void __exit exit_f2fs_fs(void)
1055{
5e176d54 1056 remove_proc_entry("fs/f2fs", NULL);
4589d25d 1057 f2fs_destroy_root_stats();
aff063e2
JK
1058 unregister_filesystem(&f2fs_fs_type);
1059 destroy_checkpoint_caches();
1060 destroy_gc_caches();
1061 destroy_node_manager_caches();
1062 destroy_inodecache();
b59d0bae 1063 kset_unregister(f2fs_kset);
aff063e2
JK
1064}
1065
1066module_init(init_f2fs_fs)
1067module_exit(exit_f2fs_fs)
1068
1069MODULE_AUTHOR("Samsung Electronics's Praesto Team");
1070MODULE_DESCRIPTION("Flash Friendly File System");
1071MODULE_LICENSE("GPL");
This page took 0.106153 seconds and 5 git commands to generate.