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