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