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