blob: 70dbb313a7cad6ff357e803d4ee93c322ba45ee8 [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>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090015#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>
Jaegeuk Kim5e176d52013-06-28 12:47:01 +090021#include <linux/proc_fs.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090022#include <linux/random.h>
23#include <linux/exportfs.h>
Namjae Jeond3ee4562013-03-17 17:26:14 +090024#include <linux/blkdev.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090025#include <linux/f2fs_fs.h>
26
27#include "f2fs.h"
28#include "node.h"
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +090029#include "segment.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090030#include "xattr.h"
31
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090032#define CREATE_TRACE_POINTS
33#include <trace/events/f2fs.h>
34
Jaegeuk Kim5e176d52013-06-28 12:47:01 +090035static struct proc_dir_entry *f2fs_proc_root;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090036static struct kmem_cache *f2fs_inode_cachep;
37
38enum {
Namjae Jeon696c0182013-06-16 09:48:48 +090039 Opt_gc_background,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090040 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 = {
Namjae Jeon696c0182013-06-16 09:48:48 +090051 {Opt_gc_background, "background_gc=%s"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090052 {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
Namjae Jeona07ef782012-12-30 14:52:05 +090062void 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
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090074static void init_once(void *foo)
75{
76 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
77
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090078 inode_init_once(&fi->vfs_inode);
79}
80
Namjae Jeon696c0182013-06-16 09:48:48 +090081static 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
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900166static 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
Masanari Iida434720f2013-03-19 08:03:35 +0900176 /* Initialize f2fs-specific inode info */
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900177 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
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900188static 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
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900202/*
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
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900213static 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
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900219static void f2fs_destroy_inode(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900220{
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
Jaegeuk Kim5e176d52013-06-28 12:47:01 +0900228 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
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900233 f2fs_destroy_stats(sbi);
234 stop_gc_thread(sbi);
235
Jaegeuk Kim43727522013-02-04 15:11:17 +0900236 write_checkpoint(sbi, true);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900237
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);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900255
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900256 trace_f2fs_sync_fs(sb, sync);
257
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900258 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
259 return 0;
260
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900261 if (sync) {
262 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim43727522013-02-04 15:11:17 +0900263 write_checkpoint(sbi, false);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900264 mutex_unlock(&sbi->gc_mutex);
265 } else {
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900266 f2fs_balance_fs(sbi);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900267 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900268
Namjae Jeon64c576f2012-12-22 12:10:27 +0900269 return 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900270}
271
Changman Leed6212a52013-01-29 18:30:07 +0900272static int f2fs_freeze(struct super_block *sb)
273{
274 int err;
275
Jaegeuk Kim77888c12013-05-20 20:28:47 +0900276 if (f2fs_readonly(sb))
Changman Leed6212a52013-01-29 18:30:07 +0900277 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
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900288static 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
Jaegeuk Kim1362b5e2012-12-12 19:45:49 +0900306 buf->f_files = sbi->total_node_count;
307 buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900308
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900309 buf->f_namelen = F2FS_NAME_LEN;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900310 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
Namjae Jeon696c0182013-06-16 09:48:48 +0900320 if (!(root->d_sb->s_flags & MS_RDONLY) && test_opt(sbi, BG_GC))
321 seq_printf(seq, ",background_gc=%s", "on");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900322 else
Namjae Jeon696c0182013-06-16 09:48:48 +0900323 seq_printf(seq, ",background_gc=%s", "off");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900324 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))
Alejandro Martinez Ruizaa435072013-01-25 19:08:59 +0100343 seq_puts(seq, ",disable_ext_identify");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900344
345 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
346
347 return 0;
348}
349
Jaegeuk Kim5e176d52013-06-28 12:47:01 +0900350static 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
Namjae Jeon696c0182013-06-16 09:48:48 +0900380static 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
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900432static struct super_operations f2fs_sops = {
433 .alloc_inode = f2fs_alloc_inode,
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900434 .drop_inode = f2fs_drop_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900435 .destroy_inode = f2fs_destroy_inode,
436 .write_inode = f2fs_write_inode,
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900437 .dirty_inode = f2fs_dirty_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900438 .show_options = f2fs_show_options,
439 .evict_inode = f2fs_evict_inode,
440 .put_super = f2fs_put_super,
441 .sync_fs = f2fs_sync_fs,
Changman Leed6212a52013-01-29 18:30:07 +0900442 .freeze_fs = f2fs_freeze,
443 .unfreeze_fs = f2fs_unfreeze,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900444 .statfs = f2fs_statfs,
Namjae Jeon696c0182013-06-16 09:48:48 +0900445 .remount_fs = f2fs_remount,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900446};
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
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900493static 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
Namjae Jeona07ef782012-12-30 14:52:05 +0900513static int sanity_check_raw_super(struct super_block *sb,
514 struct f2fs_super_block *raw_super)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900515{
516 unsigned int blocksize;
517
Namjae Jeona07ef782012-12-30 14:52:05 +0900518 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));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900522 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900523 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900524
majianpeng5c9b4692013-02-01 19:07:57 +0800525 /* Currently, support only 4KB page cache size */
526 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
527 f2fs_msg(sb, KERN_INFO,
majianpeng14d7e9d2013-02-01 19:07:03 +0800528 "Invalid page_cache_size (%lu), supports only 4KB\n",
majianpeng5c9b4692013-02-01 19:07:57 +0800529 PAGE_CACHE_SIZE);
530 return 1;
531 }
532
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900533 /* Currently, support only 4KB block size */
534 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
majianpeng5c9b4692013-02-01 19:07:57 +0800535 if (blocksize != F2FS_BLKSIZE) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900536 f2fs_msg(sb, KERN_INFO,
537 "Invalid blocksize (%u), supports only 4KB\n",
538 blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900539 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900540 }
majianpeng5c9b4692013-02-01 19:07:57 +0800541
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900542 if (le32_to_cpu(raw_super->log_sectorsize) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900543 F2FS_LOG_SECTOR_SIZE) {
544 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900545 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900546 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900547 if (le32_to_cpu(raw_super->log_sectors_per_block) !=
Namjae Jeona07ef782012-12-30 14:52:05 +0900548 F2FS_LOG_SECTORS_PER_BLOCK) {
549 f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900550 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +0900551 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900552 return 0;
553}
554
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900555static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900556{
557 unsigned int total, fsmeta;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900558 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
559 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900560
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;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900570
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 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900575 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);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900598 sbi->cur_victim_sec = NULL_SECNO;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900599
600 for (i = 0; i < NR_COUNT_TYPE; i++)
601 atomic_set(&sbi->nr_pages[i], 0);
602}
603
majianpeng14d7e9d2013-02-01 19:07:03 +0800604static 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);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900615 return -EIO;
majianpeng14d7e9d2013-02-01 19:07:03 +0800616 }
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);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900627 return -EINVAL;
majianpeng14d7e9d2013-02-01 19:07:03 +0800628}
629
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900630static 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
Namjae Jeonff9234a2013-01-12 14:41:13 +0900644 /* set a block size */
Namjae Jeona07ef782012-12-30 14:52:05 +0900645 if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
646 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900647 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +0900648 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900649
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900650 err = validate_superblock(sb, &raw_super, &raw_super_buf, 0);
651 if (err) {
majianpeng14d7e9d2013-02-01 19:07:03 +0800652 brelse(raw_super_buf);
Namjae Jeonc0d39e62013-03-17 17:26:53 +0900653 /* check secondary superblock when primary failed */
654 err = validate_superblock(sb, &raw_super, &raw_super_buf, 1);
655 if (err)
majianpeng14d7e9d2013-02-01 19:07:03 +0800656 goto free_sb_buf;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900657 }
Gu Zheng5fb08372013-06-07 14:16:53 +0800658 sb->s_fs_info = sbi;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900659 /* 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 */
Gu Zheng5fb08372013-06-07 14:16:53 +0800671 err = parse_options(sb, (char *)data);
Wei Yongjun66348b72013-04-12 10:23:18 +0800672 if (err)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900673 goto free_sb_buf;
674
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900675 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900676 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;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900683 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);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900693 mutex_init(&sbi->writepages);
694 mutex_init(&sbi->cp_mutex);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900695 for (i = 0; i < NR_GLOBAL_LOCKS; i++)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900696 mutex_init(&sbi->fs_lock[i]);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900697 mutex_init(&sbi->node_write);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900698 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)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900706 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900707 err = PTR_ERR(sbi->meta_inode);
708 goto free_sb_buf;
709 }
710
711 err = get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900712 if (err) {
713 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900714 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +0900715 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900716
717 /* sanity checking of checkpoint */
718 err = -EINVAL;
Jaegeuk Kim577e3492013-01-24 19:56:11 +0900719 if (sanity_check_ckpt(sbi)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900720 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900721 goto free_cp;
Namjae Jeona07ef782012-12-30 14:52:05 +0900722 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900723
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
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900736 init_orphan_info(sbi);
737
738 /* setup f2fs internal modules */
739 err = build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900740 if (err) {
741 f2fs_msg(sb, KERN_ERR,
742 "Failed to initialize F2FS segment manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900743 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900744 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900745 err = build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +0900746 if (err) {
747 f2fs_msg(sb, KERN_ERR,
748 "Failed to initialize F2FS node manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900749 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +0900750 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900751
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)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900757 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900758 err = PTR_ERR(sbi->node_inode);
759 goto free_nm;
760 }
761
762 /* if there are nt orphan nodes free them */
763 err = -EINVAL;
Jaegeuk Kim30f0c752012-12-19 16:09:19 +0900764 if (recover_orphan_inodes(sbi))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900765 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)) {
Namjae Jeona07ef782012-12-30 14:52:05 +0900770 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900771 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 */
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900784 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
785 err = recover_fsync_data(sbi);
Chris Friesbde582b2013-05-02 16:07:34 -0500786 if (err)
787 f2fs_msg(sb, KERN_ERR,
788 "Cannot recover all fsync data errno=%ld", err);
Jaegeuk Kim6ead1142013-03-20 19:01:06 +0900789 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900790
Namjae Jeon696c0182013-06-16 09:48:48 +0900791 /*
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 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900801
802 err = f2fs_build_stats(sbi);
803 if (err)
804 goto fail;
805
Jaegeuk Kim5e176d52013-06-28 12:47:01 +0900806 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
Namjae Jeond3ee4562013-03-17 17:26:14 +0900813 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
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900821 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};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800858MODULE_ALIAS_FS("f2fs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900859
Namjae Jeon6e6093a2013-01-17 00:08:30 +0900860static int __init init_inodecache(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900861{
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;
Namjae Jeon4589d252013-01-15 19:58:47 +0900895 err = register_filesystem(&f2fs_fs_type);
896 if (err)
897 goto fail;
898 f2fs_create_root_stats();
Jaegeuk Kim5e176d52013-06-28 12:47:01 +0900899 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900900fail:
901 return err;
902}
903
904static void __exit exit_f2fs_fs(void)
905{
Jaegeuk Kim5e176d52013-06-28 12:47:01 +0900906 remove_proc_entry("fs/f2fs", NULL);
Namjae Jeon4589d252013-01-15 19:58:47 +0900907 f2fs_destroy_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900908 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");