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 | db9f7c1 | 2014-12-17 20:04:08 -0800 | [diff] [blame] | 33 | #include "trace.h" |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 34 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 35 | #define CREATE_TRACE_POINTS |
| 36 | #include <trace/events/f2fs.h> |
| 37 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 38 | static struct kmem_cache *f2fs_inode_cachep; |
| 39 | |
Jaegeuk Kim | 73faec4 | 2016-04-29 15:34:32 -0700 | [diff] [blame] | 40 | #ifdef CONFIG_F2FS_FAULT_INJECTION |
Jaegeuk Kim | 2c63fea | 2016-04-29 15:49:56 -0700 | [diff] [blame] | 41 | |
| 42 | char *fault_name[FAULT_MAX] = { |
| 43 | [FAULT_KMALLOC] = "kmalloc", |
Jaegeuk Kim | c41f3cc3 | 2016-04-29 16:17:09 -0700 | [diff] [blame] | 44 | [FAULT_PAGE_ALLOC] = "page alloc", |
Jaegeuk Kim | cb78942 | 2016-04-29 16:29:22 -0700 | [diff] [blame] | 45 | [FAULT_ALLOC_NID] = "alloc nid", |
| 46 | [FAULT_ORPHAN] = "orphan", |
| 47 | [FAULT_BLOCK] = "no more block", |
| 48 | [FAULT_DIR_DEPTH] = "too big dir depth", |
Jaegeuk Kim | 53aa6bb | 2016-05-25 15:24:18 -0700 | [diff] [blame] | 49 | [FAULT_EVICT_INODE] = "evict_inode fail", |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 50 | [FAULT_TRUNCATE] = "truncate fail", |
Chao Yu | 8b038c7 | 2016-09-18 23:30:07 +0800 | [diff] [blame] | 51 | [FAULT_IO] = "IO error", |
Chao Yu | 0f34802 | 2016-09-26 19:45:55 +0800 | [diff] [blame] | 52 | [FAULT_CHECKPOINT] = "checkpoint error", |
Jaegeuk Kim | 2c63fea | 2016-04-29 15:49:56 -0700 | [diff] [blame] | 53 | }; |
Sheng Yong | 0879689 | 2016-05-16 12:38:50 +0800 | [diff] [blame] | 54 | |
Chao Yu | 1ecc0c5 | 2016-09-23 21:30:09 +0800 | [diff] [blame] | 55 | static void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, |
| 56 | unsigned int rate) |
Sheng Yong | 0879689 | 2016-05-16 12:38:50 +0800 | [diff] [blame] | 57 | { |
Chao Yu | 1ecc0c5 | 2016-09-23 21:30:09 +0800 | [diff] [blame] | 58 | struct f2fs_fault_info *ffi = &sbi->fault_info; |
| 59 | |
Sheng Yong | 0879689 | 2016-05-16 12:38:50 +0800 | [diff] [blame] | 60 | if (rate) { |
Chao Yu | 1ecc0c5 | 2016-09-23 21:30:09 +0800 | [diff] [blame] | 61 | atomic_set(&ffi->inject_ops, 0); |
| 62 | ffi->inject_rate = rate; |
| 63 | ffi->inject_type = (1 << FAULT_MAX) - 1; |
Sheng Yong | 0879689 | 2016-05-16 12:38:50 +0800 | [diff] [blame] | 64 | } else { |
Chao Yu | 1ecc0c5 | 2016-09-23 21:30:09 +0800 | [diff] [blame] | 65 | memset(ffi, 0, sizeof(struct f2fs_fault_info)); |
Sheng Yong | 0879689 | 2016-05-16 12:38:50 +0800 | [diff] [blame] | 66 | } |
| 67 | } |
Jaegeuk Kim | 73faec4 | 2016-04-29 15:34:32 -0700 | [diff] [blame] | 68 | #endif |
| 69 | |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 70 | /* f2fs-wide shrinker description */ |
| 71 | static struct shrinker f2fs_shrinker_info = { |
| 72 | .scan_objects = f2fs_shrink_scan, |
| 73 | .count_objects = f2fs_shrink_count, |
| 74 | .seeks = DEFAULT_SEEKS, |
| 75 | }; |
| 76 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 77 | enum { |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 78 | Opt_gc_background, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 79 | Opt_disable_roll_forward, |
Jaegeuk Kim | 2d834bf | 2015-01-23 18:33:46 -0800 | [diff] [blame] | 80 | Opt_norecovery, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 81 | Opt_discard, |
Chao Yu | 64058be | 2016-07-03 22:05:14 +0800 | [diff] [blame] | 82 | Opt_nodiscard, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 83 | Opt_noheap, |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 84 | Opt_heap, |
Kelly Anderson | 4058c51 | 2013-10-07 11:36:20 +0900 | [diff] [blame] | 85 | Opt_user_xattr, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 86 | Opt_nouser_xattr, |
Kelly Anderson | 4058c51 | 2013-10-07 11:36:20 +0900 | [diff] [blame] | 87 | Opt_acl, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 88 | Opt_noacl, |
| 89 | Opt_active_logs, |
| 90 | Opt_disable_ext_identify, |
Jaegeuk Kim | 444c580 | 2013-08-08 15:16:22 +0900 | [diff] [blame] | 91 | Opt_inline_xattr, |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 92 | Opt_noinline_xattr, |
Huajun Li | 8274de7 | 2013-11-10 23:13:17 +0800 | [diff] [blame] | 93 | Opt_inline_data, |
Chao Yu | 5efd3c6 | 2014-09-24 18:16:13 +0800 | [diff] [blame] | 94 | Opt_inline_dentry, |
Chao Yu | 97c1794 | 2016-05-09 19:56:34 +0800 | [diff] [blame] | 95 | Opt_noinline_dentry, |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 96 | Opt_flush_merge, |
Jaegeuk Kim | 69e9e42 | 2016-05-20 22:39:20 -0700 | [diff] [blame] | 97 | Opt_noflush_merge, |
Jaegeuk Kim | 0f7b2ab | 2014-07-23 09:57:31 -0700 | [diff] [blame] | 98 | Opt_nobarrier, |
Jaegeuk Kim | d5053a34 | 2014-10-30 22:47:03 -0700 | [diff] [blame] | 99 | Opt_fastboot, |
Chao Yu | 8967215 | 2015-02-05 17:55:51 +0800 | [diff] [blame] | 100 | Opt_extent_cache, |
Jaegeuk Kim | 7daaea2 | 2015-06-25 17:43:04 -0700 | [diff] [blame] | 101 | Opt_noextent_cache, |
Wanpeng Li | 7534279 | 2015-03-24 10:20:27 +0800 | [diff] [blame] | 102 | Opt_noinline_data, |
Chao Yu | 343f40f | 2015-12-16 13:12:16 +0800 | [diff] [blame] | 103 | Opt_data_flush, |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 104 | Opt_mode, |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 105 | Opt_io_size_bits, |
Jaegeuk Kim | 73faec4 | 2016-04-29 15:34:32 -0700 | [diff] [blame] | 106 | Opt_fault_injection, |
Jaegeuk Kim | 6d94c74 | 2016-05-20 21:47:24 -0700 | [diff] [blame] | 107 | Opt_lazytime, |
| 108 | Opt_nolazytime, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 109 | Opt_err, |
| 110 | }; |
| 111 | |
| 112 | static match_table_t f2fs_tokens = { |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 113 | {Opt_gc_background, "background_gc=%s"}, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 114 | {Opt_disable_roll_forward, "disable_roll_forward"}, |
Jaegeuk Kim | 2d834bf | 2015-01-23 18:33:46 -0800 | [diff] [blame] | 115 | {Opt_norecovery, "norecovery"}, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 116 | {Opt_discard, "discard"}, |
Chao Yu | 64058be | 2016-07-03 22:05:14 +0800 | [diff] [blame] | 117 | {Opt_nodiscard, "nodiscard"}, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 118 | {Opt_noheap, "no_heap"}, |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 119 | {Opt_heap, "heap"}, |
Kelly Anderson | 4058c51 | 2013-10-07 11:36:20 +0900 | [diff] [blame] | 120 | {Opt_user_xattr, "user_xattr"}, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 121 | {Opt_nouser_xattr, "nouser_xattr"}, |
Kelly Anderson | 4058c51 | 2013-10-07 11:36:20 +0900 | [diff] [blame] | 122 | {Opt_acl, "acl"}, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 123 | {Opt_noacl, "noacl"}, |
| 124 | {Opt_active_logs, "active_logs=%u"}, |
| 125 | {Opt_disable_ext_identify, "disable_ext_identify"}, |
Jaegeuk Kim | 444c580 | 2013-08-08 15:16:22 +0900 | [diff] [blame] | 126 | {Opt_inline_xattr, "inline_xattr"}, |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 127 | {Opt_noinline_xattr, "noinline_xattr"}, |
Huajun Li | 8274de7 | 2013-11-10 23:13:17 +0800 | [diff] [blame] | 128 | {Opt_inline_data, "inline_data"}, |
Chao Yu | 5efd3c6 | 2014-09-24 18:16:13 +0800 | [diff] [blame] | 129 | {Opt_inline_dentry, "inline_dentry"}, |
Chao Yu | 97c1794 | 2016-05-09 19:56:34 +0800 | [diff] [blame] | 130 | {Opt_noinline_dentry, "noinline_dentry"}, |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 131 | {Opt_flush_merge, "flush_merge"}, |
Jaegeuk Kim | 69e9e42 | 2016-05-20 22:39:20 -0700 | [diff] [blame] | 132 | {Opt_noflush_merge, "noflush_merge"}, |
Jaegeuk Kim | 0f7b2ab | 2014-07-23 09:57:31 -0700 | [diff] [blame] | 133 | {Opt_nobarrier, "nobarrier"}, |
Jaegeuk Kim | d5053a34 | 2014-10-30 22:47:03 -0700 | [diff] [blame] | 134 | {Opt_fastboot, "fastboot"}, |
Chao Yu | 8967215 | 2015-02-05 17:55:51 +0800 | [diff] [blame] | 135 | {Opt_extent_cache, "extent_cache"}, |
Jaegeuk Kim | 7daaea2 | 2015-06-25 17:43:04 -0700 | [diff] [blame] | 136 | {Opt_noextent_cache, "noextent_cache"}, |
Wanpeng Li | 7534279 | 2015-03-24 10:20:27 +0800 | [diff] [blame] | 137 | {Opt_noinline_data, "noinline_data"}, |
Chao Yu | 343f40f | 2015-12-16 13:12:16 +0800 | [diff] [blame] | 138 | {Opt_data_flush, "data_flush"}, |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 139 | {Opt_mode, "mode=%s"}, |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 140 | {Opt_io_size_bits, "io_bits=%u"}, |
Jaegeuk Kim | 73faec4 | 2016-04-29 15:34:32 -0700 | [diff] [blame] | 141 | {Opt_fault_injection, "fault_injection=%u"}, |
Jaegeuk Kim | 6d94c74 | 2016-05-20 21:47:24 -0700 | [diff] [blame] | 142 | {Opt_lazytime, "lazytime"}, |
| 143 | {Opt_nolazytime, "nolazytime"}, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 144 | {Opt_err, NULL}, |
| 145 | }; |
| 146 | |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 147 | void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...) |
| 148 | { |
| 149 | struct va_format vaf; |
| 150 | va_list args; |
| 151 | |
| 152 | va_start(args, fmt); |
| 153 | vaf.fmt = fmt; |
| 154 | vaf.va = &args; |
| 155 | printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf); |
| 156 | va_end(args); |
| 157 | } |
| 158 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 159 | static void init_once(void *foo) |
| 160 | { |
| 161 | struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo; |
| 162 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 163 | inode_init_once(&fi->vfs_inode); |
| 164 | } |
| 165 | |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 166 | static int parse_options(struct super_block *sb, char *options) |
| 167 | { |
| 168 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
Chao Yu | 09d54cd | 2015-06-08 13:20:10 +0800 | [diff] [blame] | 169 | struct request_queue *q; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 170 | substring_t args[MAX_OPT_ARGS]; |
| 171 | char *p, *name; |
| 172 | int arg = 0; |
| 173 | |
| 174 | if (!options) |
| 175 | return 0; |
| 176 | |
| 177 | while ((p = strsep(&options, ",")) != NULL) { |
| 178 | int token; |
| 179 | if (!*p) |
| 180 | continue; |
| 181 | /* |
| 182 | * Initialize args struct so we know whether arg was |
| 183 | * found; some options take optional arguments. |
| 184 | */ |
| 185 | args[0].to = args[0].from = NULL; |
| 186 | token = match_token(p, f2fs_tokens, args); |
| 187 | |
| 188 | switch (token) { |
| 189 | case Opt_gc_background: |
| 190 | name = match_strdup(&args[0]); |
| 191 | |
| 192 | if (!name) |
| 193 | return -ENOMEM; |
Jaegeuk Kim | 6aefd93 | 2015-10-05 11:02:54 -0700 | [diff] [blame] | 194 | if (strlen(name) == 2 && !strncmp(name, "on", 2)) { |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 195 | set_opt(sbi, BG_GC); |
Jaegeuk Kim | 6aefd93 | 2015-10-05 11:02:54 -0700 | [diff] [blame] | 196 | clear_opt(sbi, FORCE_FG_GC); |
| 197 | } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) { |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 198 | clear_opt(sbi, BG_GC); |
Jaegeuk Kim | 6aefd93 | 2015-10-05 11:02:54 -0700 | [diff] [blame] | 199 | clear_opt(sbi, FORCE_FG_GC); |
| 200 | } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) { |
| 201 | set_opt(sbi, BG_GC); |
| 202 | set_opt(sbi, FORCE_FG_GC); |
| 203 | } else { |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 204 | kfree(name); |
| 205 | return -EINVAL; |
| 206 | } |
| 207 | kfree(name); |
| 208 | break; |
| 209 | case Opt_disable_roll_forward: |
| 210 | set_opt(sbi, DISABLE_ROLL_FORWARD); |
| 211 | break; |
Jaegeuk Kim | 2d834bf | 2015-01-23 18:33:46 -0800 | [diff] [blame] | 212 | case Opt_norecovery: |
| 213 | /* this option mounts f2fs with ro */ |
| 214 | set_opt(sbi, DISABLE_ROLL_FORWARD); |
| 215 | if (!f2fs_readonly(sb)) |
| 216 | return -EINVAL; |
| 217 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 218 | case Opt_discard: |
Chao Yu | 09d54cd | 2015-06-08 13:20:10 +0800 | [diff] [blame] | 219 | q = bdev_get_queue(sb->s_bdev); |
| 220 | if (blk_queue_discard(q)) { |
| 221 | set_opt(sbi, DISCARD); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 222 | } else if (!f2fs_sb_mounted_blkzoned(sb)) { |
Chao Yu | 09d54cd | 2015-06-08 13:20:10 +0800 | [diff] [blame] | 223 | f2fs_msg(sb, KERN_WARNING, |
| 224 | "mounting with \"discard\" option, but " |
| 225 | "the device does not support discard"); |
| 226 | } |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 227 | break; |
Chao Yu | 64058be | 2016-07-03 22:05:14 +0800 | [diff] [blame] | 228 | case Opt_nodiscard: |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 229 | if (f2fs_sb_mounted_blkzoned(sb)) { |
| 230 | f2fs_msg(sb, KERN_WARNING, |
| 231 | "discard is required for zoned block devices"); |
| 232 | return -EINVAL; |
| 233 | } |
Chao Yu | 64058be | 2016-07-03 22:05:14 +0800 | [diff] [blame] | 234 | clear_opt(sbi, DISCARD); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 235 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 236 | case Opt_noheap: |
| 237 | set_opt(sbi, NOHEAP); |
| 238 | break; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 239 | case Opt_heap: |
| 240 | clear_opt(sbi, NOHEAP); |
| 241 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 242 | #ifdef CONFIG_F2FS_FS_XATTR |
Kelly Anderson | 4058c51 | 2013-10-07 11:36:20 +0900 | [diff] [blame] | 243 | case Opt_user_xattr: |
| 244 | set_opt(sbi, XATTR_USER); |
| 245 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 246 | case Opt_nouser_xattr: |
| 247 | clear_opt(sbi, XATTR_USER); |
| 248 | break; |
Jaegeuk Kim | 444c580 | 2013-08-08 15:16:22 +0900 | [diff] [blame] | 249 | case Opt_inline_xattr: |
| 250 | set_opt(sbi, INLINE_XATTR); |
| 251 | break; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 252 | case Opt_noinline_xattr: |
| 253 | clear_opt(sbi, INLINE_XATTR); |
| 254 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 255 | #else |
Kelly Anderson | 4058c51 | 2013-10-07 11:36:20 +0900 | [diff] [blame] | 256 | case Opt_user_xattr: |
| 257 | f2fs_msg(sb, KERN_INFO, |
| 258 | "user_xattr options not supported"); |
| 259 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 260 | case Opt_nouser_xattr: |
| 261 | f2fs_msg(sb, KERN_INFO, |
| 262 | "nouser_xattr options not supported"); |
| 263 | break; |
Jaegeuk Kim | 444c580 | 2013-08-08 15:16:22 +0900 | [diff] [blame] | 264 | case Opt_inline_xattr: |
| 265 | f2fs_msg(sb, KERN_INFO, |
| 266 | "inline_xattr options not supported"); |
| 267 | break; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 268 | case Opt_noinline_xattr: |
| 269 | f2fs_msg(sb, KERN_INFO, |
| 270 | "noinline_xattr options not supported"); |
| 271 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 272 | #endif |
| 273 | #ifdef CONFIG_F2FS_FS_POSIX_ACL |
Kelly Anderson | 4058c51 | 2013-10-07 11:36:20 +0900 | [diff] [blame] | 274 | case Opt_acl: |
| 275 | set_opt(sbi, POSIX_ACL); |
| 276 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 277 | case Opt_noacl: |
| 278 | clear_opt(sbi, POSIX_ACL); |
| 279 | break; |
| 280 | #else |
Kelly Anderson | 4058c51 | 2013-10-07 11:36:20 +0900 | [diff] [blame] | 281 | case Opt_acl: |
| 282 | f2fs_msg(sb, KERN_INFO, "acl options not supported"); |
| 283 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 284 | case Opt_noacl: |
| 285 | f2fs_msg(sb, KERN_INFO, "noacl options not supported"); |
| 286 | break; |
| 287 | #endif |
| 288 | case Opt_active_logs: |
| 289 | if (args->from && match_int(args, &arg)) |
| 290 | return -EINVAL; |
| 291 | if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE) |
| 292 | return -EINVAL; |
| 293 | sbi->active_logs = arg; |
| 294 | break; |
| 295 | case Opt_disable_ext_identify: |
| 296 | set_opt(sbi, DISABLE_EXT_IDENTIFY); |
| 297 | break; |
Huajun Li | 8274de7 | 2013-11-10 23:13:17 +0800 | [diff] [blame] | 298 | case Opt_inline_data: |
| 299 | set_opt(sbi, INLINE_DATA); |
| 300 | break; |
Chao Yu | 5efd3c6 | 2014-09-24 18:16:13 +0800 | [diff] [blame] | 301 | case Opt_inline_dentry: |
| 302 | set_opt(sbi, INLINE_DENTRY); |
| 303 | break; |
Chao Yu | 97c1794 | 2016-05-09 19:56:34 +0800 | [diff] [blame] | 304 | case Opt_noinline_dentry: |
| 305 | clear_opt(sbi, INLINE_DENTRY); |
| 306 | break; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 307 | case Opt_flush_merge: |
| 308 | set_opt(sbi, FLUSH_MERGE); |
| 309 | break; |
Jaegeuk Kim | 69e9e42 | 2016-05-20 22:39:20 -0700 | [diff] [blame] | 310 | case Opt_noflush_merge: |
| 311 | clear_opt(sbi, FLUSH_MERGE); |
| 312 | break; |
Jaegeuk Kim | 0f7b2ab | 2014-07-23 09:57:31 -0700 | [diff] [blame] | 313 | case Opt_nobarrier: |
| 314 | set_opt(sbi, NOBARRIER); |
| 315 | break; |
Jaegeuk Kim | d5053a34 | 2014-10-30 22:47:03 -0700 | [diff] [blame] | 316 | case Opt_fastboot: |
| 317 | set_opt(sbi, FASTBOOT); |
| 318 | break; |
Chao Yu | 8967215 | 2015-02-05 17:55:51 +0800 | [diff] [blame] | 319 | case Opt_extent_cache: |
| 320 | set_opt(sbi, EXTENT_CACHE); |
| 321 | break; |
Jaegeuk Kim | 7daaea2 | 2015-06-25 17:43:04 -0700 | [diff] [blame] | 322 | case Opt_noextent_cache: |
| 323 | clear_opt(sbi, EXTENT_CACHE); |
| 324 | break; |
Wanpeng Li | 7534279 | 2015-03-24 10:20:27 +0800 | [diff] [blame] | 325 | case Opt_noinline_data: |
| 326 | clear_opt(sbi, INLINE_DATA); |
| 327 | break; |
Chao Yu | 343f40f | 2015-12-16 13:12:16 +0800 | [diff] [blame] | 328 | case Opt_data_flush: |
| 329 | set_opt(sbi, DATA_FLUSH); |
| 330 | break; |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 331 | case Opt_mode: |
| 332 | name = match_strdup(&args[0]); |
| 333 | |
| 334 | if (!name) |
| 335 | return -ENOMEM; |
| 336 | if (strlen(name) == 8 && |
| 337 | !strncmp(name, "adaptive", 8)) { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 338 | if (f2fs_sb_mounted_blkzoned(sb)) { |
| 339 | f2fs_msg(sb, KERN_WARNING, |
| 340 | "adaptive mode is not allowed with " |
| 341 | "zoned block device feature"); |
| 342 | kfree(name); |
| 343 | return -EINVAL; |
| 344 | } |
Jaegeuk Kim | 52763a4 | 2016-06-13 09:47:48 -0700 | [diff] [blame] | 345 | set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE); |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 346 | } else if (strlen(name) == 3 && |
| 347 | !strncmp(name, "lfs", 3)) { |
Jaegeuk Kim | 52763a4 | 2016-06-13 09:47:48 -0700 | [diff] [blame] | 348 | set_opt_mode(sbi, F2FS_MOUNT_LFS); |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 349 | } else { |
| 350 | kfree(name); |
| 351 | return -EINVAL; |
| 352 | } |
| 353 | kfree(name); |
| 354 | break; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 355 | case Opt_io_size_bits: |
| 356 | if (args->from && match_int(args, &arg)) |
| 357 | return -EINVAL; |
| 358 | if (arg > __ilog2_u32(BIO_MAX_PAGES)) { |
| 359 | f2fs_msg(sb, KERN_WARNING, |
| 360 | "Not support %d, larger than %d", |
| 361 | 1 << arg, BIO_MAX_PAGES); |
| 362 | return -EINVAL; |
| 363 | } |
| 364 | sbi->write_io_size_bits = arg; |
| 365 | break; |
Jaegeuk Kim | 73faec4 | 2016-04-29 15:34:32 -0700 | [diff] [blame] | 366 | case Opt_fault_injection: |
| 367 | if (args->from && match_int(args, &arg)) |
| 368 | return -EINVAL; |
| 369 | #ifdef CONFIG_F2FS_FAULT_INJECTION |
Chao Yu | 1ecc0c5 | 2016-09-23 21:30:09 +0800 | [diff] [blame] | 370 | f2fs_build_fault_attr(sbi, arg); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 371 | set_opt(sbi, FAULT_INJECTION); |
Jaegeuk Kim | 73faec4 | 2016-04-29 15:34:32 -0700 | [diff] [blame] | 372 | #else |
| 373 | f2fs_msg(sb, KERN_INFO, |
| 374 | "FAULT_INJECTION was not selected"); |
| 375 | #endif |
| 376 | break; |
Jaegeuk Kim | 6d94c74 | 2016-05-20 21:47:24 -0700 | [diff] [blame] | 377 | case Opt_lazytime: |
| 378 | sb->s_flags |= MS_LAZYTIME; |
| 379 | break; |
| 380 | case Opt_nolazytime: |
| 381 | sb->s_flags &= ~MS_LAZYTIME; |
| 382 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 383 | default: |
| 384 | f2fs_msg(sb, KERN_ERR, |
| 385 | "Unrecognized mount option \"%s\" or missing value", |
| 386 | p); |
| 387 | return -EINVAL; |
| 388 | } |
| 389 | } |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 390 | |
| 391 | if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) { |
| 392 | f2fs_msg(sb, KERN_ERR, |
| 393 | "Should set mode=lfs with %uKB-sized IO", |
| 394 | F2FS_IO_SIZE_KB(sbi)); |
| 395 | return -EINVAL; |
| 396 | } |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 400 | static struct inode *f2fs_alloc_inode(struct super_block *sb) |
| 401 | { |
| 402 | struct f2fs_inode_info *fi; |
| 403 | |
Chao Yu | a0acdfe | 2013-12-05 09:54:00 +0800 | [diff] [blame] | 404 | fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 405 | if (!fi) |
| 406 | return NULL; |
| 407 | |
| 408 | init_once((void *) fi); |
| 409 | |
Masanari Iida | 434720f | 2013-03-19 08:03:35 +0900 | [diff] [blame] | 410 | /* Initialize f2fs-specific inode info */ |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 411 | fi->vfs_inode.i_version = 1; |
Jaegeuk Kim | a43e1c4 | 2016-12-02 15:11:32 -0800 | [diff] [blame] | 412 | atomic_set(&fi->dirty_pages, 0); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 413 | fi->i_current_depth = 1; |
| 414 | fi->i_advise = 0; |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 415 | init_rwsem(&fi->i_sem); |
Chao Yu | 2710fd7 | 2015-12-15 13:30:45 +0800 | [diff] [blame] | 416 | INIT_LIST_HEAD(&fi->dirty_list); |
Jaegeuk Kim | 0f18b46 | 2016-05-20 11:10:10 -0700 | [diff] [blame] | 417 | INIT_LIST_HEAD(&fi->gdirty_list); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 418 | INIT_LIST_HEAD(&fi->inmem_pages); |
| 419 | mutex_init(&fi->inmem_lock); |
Chao Yu | 82e0a5a | 2016-07-13 09:18:29 +0800 | [diff] [blame] | 420 | init_rwsem(&fi->dio_rwsem[READ]); |
| 421 | init_rwsem(&fi->dio_rwsem[WRITE]); |
Qiuyang Sun | 7b23ea1 | 2017-05-18 11:06:45 +0800 | [diff] [blame] | 422 | init_rwsem(&fi->i_mmap_sem); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 423 | |
Jaegeuk Kim | ab9fa66 | 2014-02-27 20:09:05 +0900 | [diff] [blame] | 424 | /* Will be used by directory only */ |
| 425 | fi->i_dir_level = F2FS_SB(sb)->dir_level; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 426 | return &fi->vfs_inode; |
| 427 | } |
| 428 | |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 429 | static int f2fs_drop_inode(struct inode *inode) |
| 430 | { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 431 | int ret; |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 432 | /* |
| 433 | * This is to avoid a deadlock condition like below. |
| 434 | * writeback_single_inode(inode) |
| 435 | * - f2fs_write_data_page |
| 436 | * - f2fs_gc -> iput -> evict |
| 437 | * - inode_wait_for_writeback(inode) |
| 438 | */ |
Jaegeuk Kim | 0f18b46 | 2016-05-20 11:10:10 -0700 | [diff] [blame] | 439 | if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) { |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 440 | if (!inode->i_nlink && !is_bad_inode(inode)) { |
Jaegeuk Kim | 3e72f72 | 2015-06-19 17:53:26 -0700 | [diff] [blame] | 441 | /* to avoid evict_inode call simultaneously */ |
| 442 | atomic_inc(&inode->i_count); |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 443 | spin_unlock(&inode->i_lock); |
| 444 | |
| 445 | /* some remained atomic pages should discarded */ |
| 446 | if (f2fs_is_atomic_file(inode)) |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 447 | drop_inmem_pages(inode); |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 448 | |
Jaegeuk Kim | 3e72f72 | 2015-06-19 17:53:26 -0700 | [diff] [blame] | 449 | /* should remain fi->extent_tree for writepage */ |
| 450 | f2fs_destroy_extent_node(inode); |
| 451 | |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 452 | sb_start_intwrite(inode->i_sb); |
Jaegeuk Kim | fc9581c | 2016-05-20 09:22:03 -0700 | [diff] [blame] | 453 | f2fs_i_size_write(inode, 0); |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 454 | |
| 455 | if (F2FS_HAS_BLOCKS(inode)) |
Jaegeuk Kim | 9a449e9 | 2016-06-02 13:49:38 -0700 | [diff] [blame] | 456 | f2fs_truncate(inode); |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 457 | |
| 458 | sb_end_intwrite(inode->i_sb); |
| 459 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 460 | fscrypt_put_encryption_info(inode, NULL); |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 461 | spin_lock(&inode->i_lock); |
Jaegeuk Kim | 3e72f72 | 2015-06-19 17:53:26 -0700 | [diff] [blame] | 462 | atomic_dec(&inode->i_count); |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 463 | } |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 464 | trace_f2fs_drop_inode(inode, 0); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 465 | return 0; |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 466 | } |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 467 | ret = generic_drop_inode(inode); |
| 468 | trace_f2fs_drop_inode(inode, ret); |
| 469 | return ret; |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 470 | } |
| 471 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 472 | int f2fs_inode_dirtied(struct inode *inode, bool sync) |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 473 | { |
| 474 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 475 | int ret = 0; |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 476 | |
| 477 | spin_lock(&sbi->inode_lock[DIRTY_META]); |
| 478 | if (is_inode_flag_set(inode, FI_DIRTY_INODE)) { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 479 | ret = 1; |
| 480 | } else { |
| 481 | set_inode_flag(inode, FI_DIRTY_INODE); |
| 482 | stat_inc_dirty_inode(sbi, DIRTY_META); |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 483 | } |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 484 | if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) { |
| 485 | list_add_tail(&F2FS_I(inode)->gdirty_list, |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 486 | &sbi->inode_list[DIRTY_META]); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 487 | inc_page_count(sbi, F2FS_DIRTY_IMETA); |
| 488 | } |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 489 | spin_unlock(&sbi->inode_lock[DIRTY_META]); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 490 | return ret; |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | void f2fs_inode_synced(struct inode *inode) |
| 494 | { |
| 495 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 496 | |
| 497 | spin_lock(&sbi->inode_lock[DIRTY_META]); |
| 498 | if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) { |
| 499 | spin_unlock(&sbi->inode_lock[DIRTY_META]); |
| 500 | return; |
| 501 | } |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 502 | if (!list_empty(&F2FS_I(inode)->gdirty_list)) { |
| 503 | list_del_init(&F2FS_I(inode)->gdirty_list); |
| 504 | dec_page_count(sbi, F2FS_DIRTY_IMETA); |
| 505 | } |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 506 | clear_inode_flag(inode, FI_DIRTY_INODE); |
| 507 | clear_inode_flag(inode, FI_AUTO_RECOVER); |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 508 | stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META); |
| 509 | spin_unlock(&sbi->inode_lock[DIRTY_META]); |
| 510 | } |
| 511 | |
Jaegeuk Kim | b378387 | 2013-06-10 09:17:01 +0900 | [diff] [blame] | 512 | /* |
| 513 | * f2fs_dirty_inode() is called from __mark_inode_dirty() |
| 514 | * |
| 515 | * We should call set_dirty_inode to write the dirty inode through write_inode. |
| 516 | */ |
| 517 | static void f2fs_dirty_inode(struct inode *inode, int flags) |
| 518 | { |
Jaegeuk Kim | 0f18b46 | 2016-05-20 11:10:10 -0700 | [diff] [blame] | 519 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 520 | |
| 521 | if (inode->i_ino == F2FS_NODE_INO(sbi) || |
| 522 | inode->i_ino == F2FS_META_INO(sbi)) |
| 523 | return; |
| 524 | |
Jaegeuk Kim | 6d94c74 | 2016-05-20 21:47:24 -0700 | [diff] [blame] | 525 | if (flags == I_DIRTY_TIME) |
| 526 | return; |
| 527 | |
Jaegeuk Kim | 26de9b1 | 2016-05-20 20:42:37 -0700 | [diff] [blame] | 528 | if (is_inode_flag_set(inode, FI_AUTO_RECOVER)) |
| 529 | clear_inode_flag(inode, FI_AUTO_RECOVER); |
| 530 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 531 | f2fs_inode_dirtied(inode, false); |
Jaegeuk Kim | b378387 | 2013-06-10 09:17:01 +0900 | [diff] [blame] | 532 | } |
| 533 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 534 | static void f2fs_i_callback(struct rcu_head *head) |
| 535 | { |
| 536 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 537 | kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode)); |
| 538 | } |
| 539 | |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 540 | static void f2fs_destroy_inode(struct inode *inode) |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 541 | { |
| 542 | call_rcu(&inode->i_rcu, f2fs_i_callback); |
| 543 | } |
| 544 | |
Jaegeuk Kim | 523be8a | 2016-05-13 12:36:58 -0700 | [diff] [blame] | 545 | static void destroy_percpu_info(struct f2fs_sb_info *sbi) |
| 546 | { |
Jaegeuk Kim | 41382ec | 2016-05-16 11:06:50 -0700 | [diff] [blame] | 547 | percpu_counter_destroy(&sbi->alloc_valid_block_count); |
Jaegeuk Kim | 513c5f3 | 2016-05-16 11:42:32 -0700 | [diff] [blame] | 548 | percpu_counter_destroy(&sbi->total_valid_inode_count); |
Jaegeuk Kim | 523be8a | 2016-05-13 12:36:58 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 551 | static void destroy_device_list(struct f2fs_sb_info *sbi) |
| 552 | { |
| 553 | int i; |
| 554 | |
| 555 | for (i = 0; i < sbi->s_ndevs; i++) { |
| 556 | blkdev_put(FDEV(i).bdev, FMODE_EXCL); |
| 557 | #ifdef CONFIG_BLK_DEV_ZONED |
| 558 | kfree(FDEV(i).blkz_type); |
| 559 | #endif |
| 560 | } |
| 561 | kfree(sbi->devs); |
| 562 | } |
| 563 | |
Chao Yu | 17a3fb5 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 564 | static void f2fs_put_super(struct super_block *sb) |
| 565 | { |
| 566 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 567 | int i; |
Jaegeuk Kim | 5e176d5 | 2013-06-28 12:47:01 +0900 | [diff] [blame] | 568 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 569 | stop_gc_thread(sbi); |
| 570 | |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 571 | /* prevent remaining shrinker jobs */ |
| 572 | mutex_lock(&sbi->umount_mutex); |
| 573 | |
Jaegeuk Kim | 85dc2f2 | 2015-01-14 17:41:41 -0800 | [diff] [blame] | 574 | /* |
| 575 | * We don't need to do checkpoint when superblock is clean. |
| 576 | * But, the previous checkpoint was not done by umount, it needs to do |
| 577 | * clean checkpoint again. |
| 578 | */ |
Chao Yu | caf0047 | 2015-01-28 17:48:42 +0800 | [diff] [blame] | 579 | if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) || |
Chao Yu | aaec2b1 | 2016-09-20 11:04:18 +0800 | [diff] [blame] | 580 | !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) { |
Jaegeuk Kim | 75ab4cb | 2014-09-20 21:57:51 -0700 | [diff] [blame] | 581 | struct cp_control cpc = { |
| 582 | .reason = CP_UMOUNT, |
| 583 | }; |
| 584 | write_checkpoint(sbi, &cpc); |
| 585 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 586 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 587 | /* be sure to wait for any on-going discard commands */ |
| 588 | f2fs_wait_discard_bios(sbi); |
| 589 | |
Chao Yu | a88cedf | 2017-06-14 23:00:55 +0800 | [diff] [blame] | 590 | if (f2fs_discard_en(sbi) && !sbi->discard_blks) { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 591 | struct cp_control cpc = { |
| 592 | .reason = CP_UMOUNT | CP_TRIMMED, |
| 593 | }; |
| 594 | write_checkpoint(sbi, &cpc); |
| 595 | } |
| 596 | |
Jaegeuk Kim | eca616f | 2015-06-15 14:52:29 -0700 | [diff] [blame] | 597 | /* write_checkpoint can update stat informaion */ |
| 598 | f2fs_destroy_stats(sbi); |
| 599 | |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 600 | /* |
| 601 | * normally superblock is clean, so we need to release this. |
| 602 | * In addition, EIO will skip do checkpoint, we need this as well. |
| 603 | */ |
Jaegeuk Kim | 74ef924 | 2016-05-02 22:09:56 -0700 | [diff] [blame] | 604 | release_ino_entry(sbi, true); |
Jaegeuk Kim | 6f12ac2 | 2014-08-19 09:48:22 -0700 | [diff] [blame] | 605 | |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 606 | f2fs_leave_shrinker(sbi); |
| 607 | mutex_unlock(&sbi->umount_mutex); |
| 608 | |
Jaegeuk Kim | 17c1912 | 2016-01-29 08:57:59 -0800 | [diff] [blame] | 609 | /* our cp_error case, we can wait for any writeback page */ |
Jaegeuk Kim | 9bc576a | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 610 | f2fs_flush_merged_writes(sbi); |
Jaegeuk Kim | 17c1912 | 2016-01-29 08:57:59 -0800 | [diff] [blame] | 611 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 612 | iput(sbi->node_inode); |
| 613 | iput(sbi->meta_inode); |
| 614 | |
| 615 | /* destroy f2fs internal modules */ |
| 616 | destroy_node_manager(sbi); |
| 617 | destroy_segment_manager(sbi); |
| 618 | |
| 619 | kfree(sbi->ckpt); |
Chao Yu | 17a3fb5 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 620 | |
| 621 | f2fs_exit_sysfs(sbi); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 622 | |
| 623 | sb->s_fs_info = NULL; |
Keith Mok | 43b6573 | 2016-03-02 12:04:24 -0800 | [diff] [blame] | 624 | if (sbi->s_chksum_driver) |
| 625 | crypto_free_shash(sbi->s_chksum_driver); |
Yunlei He | b39f0de | 2015-12-15 17:17:20 +0800 | [diff] [blame] | 626 | kfree(sbi->raw_super); |
Jaegeuk Kim | 523be8a | 2016-05-13 12:36:58 -0700 | [diff] [blame] | 627 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 628 | destroy_device_list(sbi); |
| 629 | mempool_destroy(sbi->write_io_dummy); |
Jaegeuk Kim | 523be8a | 2016-05-13 12:36:58 -0700 | [diff] [blame] | 630 | destroy_percpu_info(sbi); |
Jaegeuk Kim | c412726 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 631 | for (i = 0; i < NR_PAGE_TYPE; i++) |
| 632 | kfree(sbi->write_io[i]); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 633 | kfree(sbi); |
| 634 | } |
| 635 | |
| 636 | int f2fs_sync_fs(struct super_block *sb, int sync) |
| 637 | { |
| 638 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
Chao Yu | c34f42e | 2015-12-23 17:50:30 +0800 | [diff] [blame] | 639 | int err = 0; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 640 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 641 | trace_f2fs_sync_fs(sb, sync); |
| 642 | |
Jaegeuk Kim | b747375 | 2013-04-01 08:32:21 +0900 | [diff] [blame] | 643 | if (sync) { |
Jaegeuk Kim | d5053a34 | 2014-10-30 22:47:03 -0700 | [diff] [blame] | 644 | struct cp_control cpc; |
| 645 | |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 646 | cpc.reason = __get_cp_reason(sbi); |
| 647 | |
Jaegeuk Kim | b747375 | 2013-04-01 08:32:21 +0900 | [diff] [blame] | 648 | mutex_lock(&sbi->gc_mutex); |
Chao Yu | c34f42e | 2015-12-23 17:50:30 +0800 | [diff] [blame] | 649 | err = write_checkpoint(sbi, &cpc); |
Jaegeuk Kim | b747375 | 2013-04-01 08:32:21 +0900 | [diff] [blame] | 650 | mutex_unlock(&sbi->gc_mutex); |
Jaegeuk Kim | b747375 | 2013-04-01 08:32:21 +0900 | [diff] [blame] | 651 | } |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 652 | f2fs_trace_ios(NULL, 1); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 653 | |
Chao Yu | c34f42e | 2015-12-23 17:50:30 +0800 | [diff] [blame] | 654 | return err; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 655 | } |
| 656 | |
Changman Lee | d6212a5 | 2013-01-29 18:30:07 +0900 | [diff] [blame] | 657 | static int f2fs_freeze(struct super_block *sb) |
| 658 | { |
Jaegeuk Kim | 77888c1 | 2013-05-20 20:28:47 +0900 | [diff] [blame] | 659 | if (f2fs_readonly(sb)) |
Changman Lee | d6212a5 | 2013-01-29 18:30:07 +0900 | [diff] [blame] | 660 | return 0; |
| 661 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 662 | /* IO error happened before */ |
| 663 | if (unlikely(f2fs_cp_error(F2FS_SB(sb)))) |
| 664 | return -EIO; |
| 665 | |
| 666 | /* must be clean, since sync_filesystem() was already called */ |
| 667 | if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY)) |
| 668 | return -EINVAL; |
| 669 | return 0; |
Changman Lee | d6212a5 | 2013-01-29 18:30:07 +0900 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | static int f2fs_unfreeze(struct super_block *sb) |
| 673 | { |
| 674 | return 0; |
| 675 | } |
| 676 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 677 | static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 678 | { |
| 679 | struct super_block *sb = dentry->d_sb; |
| 680 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 681 | u64 id = huge_encode_dev(sb->s_bdev->bd_dev); |
| 682 | block_t total_count, user_block_count, start_count, ovp_count; |
Jaegeuk Kim | 711acfd | 2017-06-21 20:55:55 -0700 | [diff] [blame] | 683 | u64 avail_node_count; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 684 | |
| 685 | total_count = le64_to_cpu(sbi->raw_super->block_count); |
| 686 | user_block_count = sbi->user_block_count; |
| 687 | start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr); |
| 688 | ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg; |
| 689 | buf->f_type = F2FS_SUPER_MAGIC; |
| 690 | buf->f_bsize = sbi->blocksize; |
| 691 | |
| 692 | buf->f_blocks = total_count - start_count; |
Chao Yu | 3e6d0b4 | 2016-07-06 14:13:07 +0800 | [diff] [blame] | 693 | buf->f_bfree = user_block_count - valid_user_blocks(sbi) + ovp_count; |
Chao Yu | 026bd9d | 2017-06-26 16:24:41 +0800 | [diff] [blame^] | 694 | buf->f_bavail = user_block_count - valid_user_blocks(sbi) - |
| 695 | sbi->reserved_blocks; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 696 | |
Jaegeuk Kim | 711acfd | 2017-06-21 20:55:55 -0700 | [diff] [blame] | 697 | avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM; |
| 698 | |
| 699 | if (avail_node_count > user_block_count) { |
| 700 | buf->f_files = user_block_count; |
| 701 | buf->f_ffree = buf->f_bavail; |
| 702 | } else { |
| 703 | buf->f_files = avail_node_count; |
| 704 | buf->f_ffree = min(avail_node_count - valid_node_count(sbi), |
| 705 | buf->f_bavail); |
| 706 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 707 | |
Jaegeuk Kim | 5a20d33 | 2013-03-03 13:58:05 +0900 | [diff] [blame] | 708 | buf->f_namelen = F2FS_NAME_LEN; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 709 | buf->f_fsid.val[0] = (u32)id; |
| 710 | buf->f_fsid.val[1] = (u32)(id >> 32); |
| 711 | |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | static int f2fs_show_options(struct seq_file *seq, struct dentry *root) |
| 716 | { |
| 717 | struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb); |
| 718 | |
Jaegeuk Kim | 6aefd93 | 2015-10-05 11:02:54 -0700 | [diff] [blame] | 719 | if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) { |
| 720 | if (test_opt(sbi, FORCE_FG_GC)) |
| 721 | seq_printf(seq, ",background_gc=%s", "sync"); |
| 722 | else |
| 723 | seq_printf(seq, ",background_gc=%s", "on"); |
| 724 | } else { |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 725 | seq_printf(seq, ",background_gc=%s", "off"); |
Jaegeuk Kim | 6aefd93 | 2015-10-05 11:02:54 -0700 | [diff] [blame] | 726 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 727 | if (test_opt(sbi, DISABLE_ROLL_FORWARD)) |
| 728 | seq_puts(seq, ",disable_roll_forward"); |
| 729 | if (test_opt(sbi, DISCARD)) |
| 730 | seq_puts(seq, ",discard"); |
| 731 | if (test_opt(sbi, NOHEAP)) |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 732 | seq_puts(seq, ",no_heap"); |
| 733 | else |
| 734 | seq_puts(seq, ",heap"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 735 | #ifdef CONFIG_F2FS_FS_XATTR |
| 736 | if (test_opt(sbi, XATTR_USER)) |
| 737 | seq_puts(seq, ",user_xattr"); |
| 738 | else |
| 739 | seq_puts(seq, ",nouser_xattr"); |
Jaegeuk Kim | 444c580 | 2013-08-08 15:16:22 +0900 | [diff] [blame] | 740 | if (test_opt(sbi, INLINE_XATTR)) |
| 741 | seq_puts(seq, ",inline_xattr"); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 742 | else |
| 743 | seq_puts(seq, ",noinline_xattr"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 744 | #endif |
| 745 | #ifdef CONFIG_F2FS_FS_POSIX_ACL |
| 746 | if (test_opt(sbi, POSIX_ACL)) |
| 747 | seq_puts(seq, ",acl"); |
| 748 | else |
| 749 | seq_puts(seq, ",noacl"); |
| 750 | #endif |
| 751 | if (test_opt(sbi, DISABLE_EXT_IDENTIFY)) |
Alejandro Martinez Ruiz | aa43507 | 2013-01-25 19:08:59 +0100 | [diff] [blame] | 752 | seq_puts(seq, ",disable_ext_identify"); |
Huajun Li | 8274de7 | 2013-11-10 23:13:17 +0800 | [diff] [blame] | 753 | if (test_opt(sbi, INLINE_DATA)) |
| 754 | seq_puts(seq, ",inline_data"); |
Wanpeng Li | 7534279 | 2015-03-24 10:20:27 +0800 | [diff] [blame] | 755 | else |
| 756 | seq_puts(seq, ",noinline_data"); |
Chao Yu | 5efd3c6 | 2014-09-24 18:16:13 +0800 | [diff] [blame] | 757 | if (test_opt(sbi, INLINE_DENTRY)) |
| 758 | seq_puts(seq, ",inline_dentry"); |
Chao Yu | 97c1794 | 2016-05-09 19:56:34 +0800 | [diff] [blame] | 759 | else |
| 760 | seq_puts(seq, ",noinline_dentry"); |
Gu Zheng | b270ad6 | 2014-04-11 17:49:55 +0800 | [diff] [blame] | 761 | if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE)) |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 762 | seq_puts(seq, ",flush_merge"); |
Jaegeuk Kim | 0f7b2ab | 2014-07-23 09:57:31 -0700 | [diff] [blame] | 763 | if (test_opt(sbi, NOBARRIER)) |
| 764 | seq_puts(seq, ",nobarrier"); |
Jaegeuk Kim | d5053a34 | 2014-10-30 22:47:03 -0700 | [diff] [blame] | 765 | if (test_opt(sbi, FASTBOOT)) |
| 766 | seq_puts(seq, ",fastboot"); |
Chao Yu | 8967215 | 2015-02-05 17:55:51 +0800 | [diff] [blame] | 767 | if (test_opt(sbi, EXTENT_CACHE)) |
| 768 | seq_puts(seq, ",extent_cache"); |
Jaegeuk Kim | 7daaea2 | 2015-06-25 17:43:04 -0700 | [diff] [blame] | 769 | else |
| 770 | seq_puts(seq, ",noextent_cache"); |
Chao Yu | 343f40f | 2015-12-16 13:12:16 +0800 | [diff] [blame] | 771 | if (test_opt(sbi, DATA_FLUSH)) |
| 772 | seq_puts(seq, ",data_flush"); |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 773 | |
| 774 | seq_puts(seq, ",mode="); |
| 775 | if (test_opt(sbi, ADAPTIVE)) |
| 776 | seq_puts(seq, "adaptive"); |
| 777 | else if (test_opt(sbi, LFS)) |
| 778 | seq_puts(seq, "lfs"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 779 | seq_printf(seq, ",active_logs=%u", sbi->active_logs); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 780 | if (F2FS_IO_SIZE_BITS(sbi)) |
| 781 | seq_printf(seq, ",io_size=%uKB", F2FS_IO_SIZE_KB(sbi)); |
| 782 | #ifdef CONFIG_F2FS_FAULT_INJECTION |
| 783 | if (test_opt(sbi, FAULT_INJECTION)) |
Chao Yu | 93c0e2f | 2017-06-12 09:44:24 +0800 | [diff] [blame] | 784 | seq_printf(seq, ",fault_injection=%u", |
| 785 | sbi->fault_info.inject_rate); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 786 | #endif |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 787 | |
| 788 | return 0; |
| 789 | } |
| 790 | |
Yunlei He | 498c5e9 | 2015-05-07 18:11:37 +0800 | [diff] [blame] | 791 | static void default_options(struct f2fs_sb_info *sbi) |
| 792 | { |
| 793 | /* init some FS parameters */ |
| 794 | sbi->active_logs = NR_CURSEG_TYPE; |
| 795 | |
| 796 | set_opt(sbi, BG_GC); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 797 | set_opt(sbi, INLINE_XATTR); |
Yunlei He | 498c5e9 | 2015-05-07 18:11:37 +0800 | [diff] [blame] | 798 | set_opt(sbi, INLINE_DATA); |
Chao Yu | 97c1794 | 2016-05-09 19:56:34 +0800 | [diff] [blame] | 799 | set_opt(sbi, INLINE_DENTRY); |
Jaegeuk Kim | 3e72f72 | 2015-06-19 17:53:26 -0700 | [diff] [blame] | 800 | set_opt(sbi, EXTENT_CACHE); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 801 | set_opt(sbi, NOHEAP); |
Jaegeuk Kim | 6d94c74 | 2016-05-20 21:47:24 -0700 | [diff] [blame] | 802 | sbi->sb->s_flags |= MS_LAZYTIME; |
Jaegeuk Kim | 69e9e42 | 2016-05-20 22:39:20 -0700 | [diff] [blame] | 803 | set_opt(sbi, FLUSH_MERGE); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 804 | if (f2fs_sb_mounted_blkzoned(sbi->sb)) { |
Jaegeuk Kim | 52763a4 | 2016-06-13 09:47:48 -0700 | [diff] [blame] | 805 | set_opt_mode(sbi, F2FS_MOUNT_LFS); |
| 806 | set_opt(sbi, DISCARD); |
| 807 | } else { |
| 808 | set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE); |
| 809 | } |
Yunlei He | 498c5e9 | 2015-05-07 18:11:37 +0800 | [diff] [blame] | 810 | |
| 811 | #ifdef CONFIG_F2FS_FS_XATTR |
| 812 | set_opt(sbi, XATTR_USER); |
| 813 | #endif |
| 814 | #ifdef CONFIG_F2FS_FS_POSIX_ACL |
| 815 | set_opt(sbi, POSIX_ACL); |
| 816 | #endif |
Chao Yu | 36dbd32 | 2016-09-26 19:45:05 +0800 | [diff] [blame] | 817 | |
| 818 | #ifdef CONFIG_F2FS_FAULT_INJECTION |
| 819 | f2fs_build_fault_attr(sbi, 0); |
| 820 | #endif |
Yunlei He | 498c5e9 | 2015-05-07 18:11:37 +0800 | [diff] [blame] | 821 | } |
| 822 | |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 823 | static int f2fs_remount(struct super_block *sb, int *flags, char *data) |
| 824 | { |
| 825 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 826 | struct f2fs_mount_info org_mount_opt; |
| 827 | int err, active_logs; |
Gu Zheng | 876dc59 | 2014-04-11 17:50:00 +0800 | [diff] [blame] | 828 | bool need_restart_gc = false; |
| 829 | bool need_stop_gc = false; |
Chao Yu | 9cd81ce | 2015-09-18 16:55:26 +0800 | [diff] [blame] | 830 | bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE); |
Chao Yu | 2443b8b | 2016-09-26 19:45:06 +0800 | [diff] [blame] | 831 | #ifdef CONFIG_F2FS_FAULT_INJECTION |
| 832 | struct f2fs_fault_info ffi = sbi->fault_info; |
| 833 | #endif |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 834 | |
| 835 | /* |
| 836 | * Save the old mount options in case we |
| 837 | * need to restore them. |
| 838 | */ |
| 839 | org_mount_opt = sbi->mount_opt; |
| 840 | active_logs = sbi->active_logs; |
| 841 | |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 842 | /* recover superblocks we couldn't write due to previous RO mount */ |
| 843 | if (!(*flags & MS_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) { |
| 844 | err = f2fs_commit_super(sbi, false); |
| 845 | f2fs_msg(sb, KERN_INFO, |
| 846 | "Try to recover all the superblocks, ret: %d", err); |
| 847 | if (!err) |
| 848 | clear_sbi_flag(sbi, SBI_NEED_SB_WRITE); |
| 849 | } |
| 850 | |
Chao Yu | 26666c8 | 2014-09-15 18:04:44 +0800 | [diff] [blame] | 851 | sbi->mount_opt.opt = 0; |
Yunlei He | 498c5e9 | 2015-05-07 18:11:37 +0800 | [diff] [blame] | 852 | default_options(sbi); |
Chao Yu | 26666c8 | 2014-09-15 18:04:44 +0800 | [diff] [blame] | 853 | |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 854 | /* parse mount options */ |
| 855 | err = parse_options(sb, data); |
| 856 | if (err) |
| 857 | goto restore_opts; |
| 858 | |
| 859 | /* |
| 860 | * Previous and new state of filesystem is RO, |
Gu Zheng | 876dc59 | 2014-04-11 17:50:00 +0800 | [diff] [blame] | 861 | * so skip checking GC and FLUSH_MERGE conditions. |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 862 | */ |
Chao Yu | 6b2920a | 2014-07-07 11:21:59 +0800 | [diff] [blame] | 863 | if (f2fs_readonly(sb) && (*flags & MS_RDONLY)) |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 864 | goto skip; |
| 865 | |
Chao Yu | 9cd81ce | 2015-09-18 16:55:26 +0800 | [diff] [blame] | 866 | /* disallow enable/disable extent_cache dynamically */ |
| 867 | if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) { |
| 868 | err = -EINVAL; |
| 869 | f2fs_msg(sbi->sb, KERN_WARNING, |
| 870 | "switch extent_cache option is not allowed"); |
| 871 | goto restore_opts; |
| 872 | } |
| 873 | |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 874 | /* |
| 875 | * We stop the GC thread if FS is mounted as RO |
| 876 | * or if background_gc = off is passed in mount |
| 877 | * option. Also sync the filesystem. |
| 878 | */ |
| 879 | if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) { |
| 880 | if (sbi->gc_thread) { |
| 881 | stop_gc_thread(sbi); |
Gu Zheng | 876dc59 | 2014-04-11 17:50:00 +0800 | [diff] [blame] | 882 | need_restart_gc = true; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 883 | } |
Chao Yu | aba291b | 2014-11-18 11:17:20 +0800 | [diff] [blame] | 884 | } else if (!sbi->gc_thread) { |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 885 | err = start_gc_thread(sbi); |
| 886 | if (err) |
| 887 | goto restore_opts; |
Gu Zheng | 876dc59 | 2014-04-11 17:50:00 +0800 | [diff] [blame] | 888 | need_stop_gc = true; |
| 889 | } |
| 890 | |
Jaegeuk Kim | faa0e55 | 2016-03-24 10:29:39 -0700 | [diff] [blame] | 891 | if (*flags & MS_RDONLY) { |
| 892 | writeback_inodes_sb(sb, WB_REASON_SYNC); |
| 893 | sync_inodes_sb(sb); |
| 894 | |
| 895 | set_sbi_flag(sbi, SBI_IS_DIRTY); |
| 896 | set_sbi_flag(sbi, SBI_IS_CLOSE); |
| 897 | f2fs_sync_fs(sb, 1); |
| 898 | clear_sbi_flag(sbi, SBI_IS_CLOSE); |
| 899 | } |
| 900 | |
Gu Zheng | 876dc59 | 2014-04-11 17:50:00 +0800 | [diff] [blame] | 901 | /* |
| 902 | * We stop issue flush thread if FS is mounted as RO |
| 903 | * or if flush_merge is not passed in mount option. |
| 904 | */ |
| 905 | if ((*flags & MS_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 906 | clear_opt(sbi, FLUSH_MERGE); |
| 907 | destroy_flush_cmd_control(sbi, false); |
| 908 | } else { |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 909 | err = create_flush_cmd_control(sbi); |
| 910 | if (err) |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 911 | goto restore_gc; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 912 | } |
| 913 | skip: |
| 914 | /* Update the POSIXACL Flag */ |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 915 | sb->s_flags = (sb->s_flags & ~MS_POSIXACL) | |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 916 | (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0); |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 917 | |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 918 | return 0; |
Gu Zheng | 876dc59 | 2014-04-11 17:50:00 +0800 | [diff] [blame] | 919 | restore_gc: |
| 920 | if (need_restart_gc) { |
| 921 | if (start_gc_thread(sbi)) |
| 922 | f2fs_msg(sbi->sb, KERN_WARNING, |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 923 | "background gc thread has stopped"); |
Gu Zheng | 876dc59 | 2014-04-11 17:50:00 +0800 | [diff] [blame] | 924 | } else if (need_stop_gc) { |
| 925 | stop_gc_thread(sbi); |
| 926 | } |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 927 | restore_opts: |
| 928 | sbi->mount_opt = org_mount_opt; |
| 929 | sbi->active_logs = active_logs; |
Chao Yu | 2443b8b | 2016-09-26 19:45:06 +0800 | [diff] [blame] | 930 | #ifdef CONFIG_F2FS_FAULT_INJECTION |
| 931 | sbi->fault_info = ffi; |
| 932 | #endif |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 933 | return err; |
| 934 | } |
| 935 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 936 | static struct super_operations f2fs_sops = { |
| 937 | .alloc_inode = f2fs_alloc_inode, |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 938 | .drop_inode = f2fs_drop_inode, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 939 | .destroy_inode = f2fs_destroy_inode, |
| 940 | .write_inode = f2fs_write_inode, |
Jaegeuk Kim | b378387 | 2013-06-10 09:17:01 +0900 | [diff] [blame] | 941 | .dirty_inode = f2fs_dirty_inode, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 942 | .show_options = f2fs_show_options, |
| 943 | .evict_inode = f2fs_evict_inode, |
| 944 | .put_super = f2fs_put_super, |
| 945 | .sync_fs = f2fs_sync_fs, |
Changman Lee | d6212a5 | 2013-01-29 18:30:07 +0900 | [diff] [blame] | 946 | .freeze_fs = f2fs_freeze, |
| 947 | .unfreeze_fs = f2fs_unfreeze, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 948 | .statfs = f2fs_statfs, |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 949 | .remount_fs = f2fs_remount, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 950 | }; |
| 951 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 952 | #ifdef CONFIG_F2FS_FS_ENCRYPTION |
| 953 | static int f2fs_get_context(struct inode *inode, void *ctx, size_t len) |
| 954 | { |
| 955 | return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION, |
| 956 | F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, |
| 957 | ctx, len, NULL); |
| 958 | } |
| 959 | |
| 960 | static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len, |
| 961 | void *fs_data) |
| 962 | { |
| 963 | return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION, |
| 964 | F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, |
| 965 | ctx, len, fs_data, XATTR_CREATE); |
| 966 | } |
| 967 | |
| 968 | static unsigned f2fs_max_namelen(struct inode *inode) |
| 969 | { |
| 970 | return S_ISLNK(inode->i_mode) ? |
| 971 | inode->i_sb->s_blocksize : F2FS_NAME_LEN; |
| 972 | } |
| 973 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 974 | static const struct fscrypt_operations f2fs_cryptops = { |
| 975 | .key_prefix = "f2fs:", |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 976 | .get_context = f2fs_get_context, |
| 977 | .set_context = f2fs_set_context, |
| 978 | .is_encrypted = f2fs_encrypted_inode, |
| 979 | .empty_dir = f2fs_empty_dir, |
| 980 | .max_namelen = f2fs_max_namelen, |
| 981 | }; |
| 982 | #else |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 983 | static const struct fscrypt_operations f2fs_cryptops = { |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 984 | .is_encrypted = f2fs_encrypted_inode, |
| 985 | }; |
| 986 | #endif |
| 987 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 988 | static struct inode *f2fs_nfs_get_inode(struct super_block *sb, |
| 989 | u64 ino, u32 generation) |
| 990 | { |
| 991 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 992 | struct inode *inode; |
| 993 | |
Chao Yu | d6b7d4b | 2014-06-12 13:23:41 +0800 | [diff] [blame] | 994 | if (check_nid_range(sbi, ino)) |
Chao Yu | 910bb12 | 2014-03-12 17:08:36 +0800 | [diff] [blame] | 995 | return ERR_PTR(-ESTALE); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 996 | |
| 997 | /* |
| 998 | * f2fs_iget isn't quite right if the inode is currently unallocated! |
| 999 | * However f2fs_iget currently does appropriate checks to handle stale |
| 1000 | * inodes so everything is OK. |
| 1001 | */ |
| 1002 | inode = f2fs_iget(sb, ino); |
| 1003 | if (IS_ERR(inode)) |
| 1004 | return ERR_CAST(inode); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1005 | if (unlikely(generation && inode->i_generation != generation)) { |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1006 | /* we didn't find the right inode.. */ |
| 1007 | iput(inode); |
| 1008 | return ERR_PTR(-ESTALE); |
| 1009 | } |
| 1010 | return inode; |
| 1011 | } |
| 1012 | |
| 1013 | static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid, |
| 1014 | int fh_len, int fh_type) |
| 1015 | { |
| 1016 | return generic_fh_to_dentry(sb, fid, fh_len, fh_type, |
| 1017 | f2fs_nfs_get_inode); |
| 1018 | } |
| 1019 | |
| 1020 | static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid, |
| 1021 | int fh_len, int fh_type) |
| 1022 | { |
| 1023 | return generic_fh_to_parent(sb, fid, fh_len, fh_type, |
| 1024 | f2fs_nfs_get_inode); |
| 1025 | } |
| 1026 | |
| 1027 | static const struct export_operations f2fs_export_ops = { |
| 1028 | .fh_to_dentry = f2fs_fh_to_dentry, |
| 1029 | .fh_to_parent = f2fs_fh_to_parent, |
| 1030 | .get_parent = f2fs_get_parent, |
| 1031 | }; |
| 1032 | |
Chao Yu | e0afc4d | 2015-12-31 14:35:37 +0800 | [diff] [blame] | 1033 | static loff_t max_file_blocks(void) |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1034 | { |
Jaegeuk Kim | de93653 | 2013-08-12 21:08:03 +0900 | [diff] [blame] | 1035 | loff_t result = (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1036 | loff_t leaf_count = ADDRS_PER_BLOCK; |
| 1037 | |
| 1038 | /* two direct node blocks */ |
| 1039 | result += (leaf_count * 2); |
| 1040 | |
| 1041 | /* two indirect node blocks */ |
| 1042 | leaf_count *= NIDS_PER_BLOCK; |
| 1043 | result += (leaf_count * 2); |
| 1044 | |
| 1045 | /* one double indirect node block */ |
| 1046 | leaf_count *= NIDS_PER_BLOCK; |
| 1047 | result += leaf_count; |
| 1048 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1049 | return result; |
| 1050 | } |
| 1051 | |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1052 | static int __f2fs_commit_super(struct buffer_head *bh, |
| 1053 | struct f2fs_super_block *super) |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 1054 | { |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1055 | lock_buffer(bh); |
| 1056 | if (super) |
| 1057 | memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super)); |
| 1058 | set_buffer_uptodate(bh); |
| 1059 | set_buffer_dirty(bh); |
| 1060 | unlock_buffer(bh); |
| 1061 | |
| 1062 | /* it's rare case, we can do fua all the time */ |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1063 | return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA); |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1066 | static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1067 | struct buffer_head *bh) |
| 1068 | { |
| 1069 | struct f2fs_super_block *raw_super = (struct f2fs_super_block *) |
| 1070 | (bh->b_data + F2FS_SUPER_OFFSET); |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1071 | struct super_block *sb = sbi->sb; |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 1072 | u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr); |
| 1073 | u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr); |
| 1074 | u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr); |
| 1075 | u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr); |
| 1076 | u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr); |
| 1077 | u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr); |
| 1078 | u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt); |
| 1079 | u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit); |
| 1080 | u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat); |
| 1081 | u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa); |
| 1082 | u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main); |
| 1083 | u32 segment_count = le32_to_cpu(raw_super->segment_count); |
| 1084 | u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg); |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1085 | u64 main_end_blkaddr = main_blkaddr + |
| 1086 | (segment_count_main << log_blocks_per_seg); |
| 1087 | u64 seg_end_blkaddr = segment0_blkaddr + |
| 1088 | (segment_count << log_blocks_per_seg); |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 1089 | |
| 1090 | if (segment0_blkaddr != cp_blkaddr) { |
| 1091 | f2fs_msg(sb, KERN_INFO, |
| 1092 | "Mismatch start address, segment0(%u) cp_blkaddr(%u)", |
| 1093 | segment0_blkaddr, cp_blkaddr); |
| 1094 | return true; |
| 1095 | } |
| 1096 | |
| 1097 | if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) != |
| 1098 | sit_blkaddr) { |
| 1099 | f2fs_msg(sb, KERN_INFO, |
| 1100 | "Wrong CP boundary, start(%u) end(%u) blocks(%u)", |
| 1101 | cp_blkaddr, sit_blkaddr, |
| 1102 | segment_count_ckpt << log_blocks_per_seg); |
| 1103 | return true; |
| 1104 | } |
| 1105 | |
| 1106 | if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) != |
| 1107 | nat_blkaddr) { |
| 1108 | f2fs_msg(sb, KERN_INFO, |
| 1109 | "Wrong SIT boundary, start(%u) end(%u) blocks(%u)", |
| 1110 | sit_blkaddr, nat_blkaddr, |
| 1111 | segment_count_sit << log_blocks_per_seg); |
| 1112 | return true; |
| 1113 | } |
| 1114 | |
| 1115 | if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) != |
| 1116 | ssa_blkaddr) { |
| 1117 | f2fs_msg(sb, KERN_INFO, |
| 1118 | "Wrong NAT boundary, start(%u) end(%u) blocks(%u)", |
| 1119 | nat_blkaddr, ssa_blkaddr, |
| 1120 | segment_count_nat << log_blocks_per_seg); |
| 1121 | return true; |
| 1122 | } |
| 1123 | |
| 1124 | if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) != |
| 1125 | main_blkaddr) { |
| 1126 | f2fs_msg(sb, KERN_INFO, |
| 1127 | "Wrong SSA boundary, start(%u) end(%u) blocks(%u)", |
| 1128 | ssa_blkaddr, main_blkaddr, |
| 1129 | segment_count_ssa << log_blocks_per_seg); |
| 1130 | return true; |
| 1131 | } |
| 1132 | |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1133 | if (main_end_blkaddr > seg_end_blkaddr) { |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 1134 | f2fs_msg(sb, KERN_INFO, |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1135 | "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)", |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 1136 | main_blkaddr, |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1137 | segment0_blkaddr + |
| 1138 | (segment_count << log_blocks_per_seg), |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 1139 | segment_count_main << log_blocks_per_seg); |
| 1140 | return true; |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1141 | } else if (main_end_blkaddr < seg_end_blkaddr) { |
| 1142 | int err = 0; |
| 1143 | char *res; |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 1144 | |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1145 | /* fix in-memory information all the time */ |
| 1146 | raw_super->segment_count = cpu_to_le32((main_end_blkaddr - |
| 1147 | segment0_blkaddr) >> log_blocks_per_seg); |
| 1148 | |
| 1149 | if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) { |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1150 | set_sbi_flag(sbi, SBI_NEED_SB_WRITE); |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1151 | res = "internally"; |
| 1152 | } else { |
| 1153 | err = __f2fs_commit_super(bh, NULL); |
| 1154 | res = err ? "failed" : "done"; |
| 1155 | } |
| 1156 | f2fs_msg(sb, KERN_INFO, |
| 1157 | "Fix alignment : %s, start(%u) end(%u) block(%u)", |
| 1158 | res, main_blkaddr, |
| 1159 | segment0_blkaddr + |
| 1160 | (segment_count << log_blocks_per_seg), |
| 1161 | segment_count_main << log_blocks_per_seg); |
| 1162 | if (err) |
| 1163 | return true; |
| 1164 | } |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 1165 | return false; |
| 1166 | } |
| 1167 | |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1168 | static int sanity_check_raw_super(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1169 | struct buffer_head *bh) |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1170 | { |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1171 | struct f2fs_super_block *raw_super = (struct f2fs_super_block *) |
| 1172 | (bh->b_data + F2FS_SUPER_OFFSET); |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1173 | struct super_block *sb = sbi->sb; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1174 | unsigned int blocksize; |
| 1175 | |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 1176 | if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) { |
| 1177 | f2fs_msg(sb, KERN_INFO, |
| 1178 | "Magic Mismatch, valid(0x%x) - read(0x%x)", |
| 1179 | F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic)); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1180 | return 1; |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 1181 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1182 | |
majianpeng | 5c9b469 | 2013-02-01 19:07:57 +0800 | [diff] [blame] | 1183 | /* Currently, support only 4KB page cache size */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1184 | if (F2FS_BLKSIZE != PAGE_SIZE) { |
majianpeng | 5c9b469 | 2013-02-01 19:07:57 +0800 | [diff] [blame] | 1185 | f2fs_msg(sb, KERN_INFO, |
majianpeng | 14d7e9d | 2013-02-01 19:07:03 +0800 | [diff] [blame] | 1186 | "Invalid page_cache_size (%lu), supports only 4KB\n", |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1187 | PAGE_SIZE); |
majianpeng | 5c9b469 | 2013-02-01 19:07:57 +0800 | [diff] [blame] | 1188 | return 1; |
| 1189 | } |
| 1190 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1191 | /* Currently, support only 4KB block size */ |
| 1192 | blocksize = 1 << le32_to_cpu(raw_super->log_blocksize); |
majianpeng | 5c9b469 | 2013-02-01 19:07:57 +0800 | [diff] [blame] | 1193 | if (blocksize != F2FS_BLKSIZE) { |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 1194 | f2fs_msg(sb, KERN_INFO, |
| 1195 | "Invalid blocksize (%u), supports only 4KB\n", |
| 1196 | blocksize); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1197 | return 1; |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 1198 | } |
majianpeng | 5c9b469 | 2013-02-01 19:07:57 +0800 | [diff] [blame] | 1199 | |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 1200 | /* check log blocks per segment */ |
| 1201 | if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) { |
| 1202 | f2fs_msg(sb, KERN_INFO, |
| 1203 | "Invalid log blocks per segment (%u)\n", |
| 1204 | le32_to_cpu(raw_super->log_blocks_per_seg)); |
| 1205 | return 1; |
| 1206 | } |
| 1207 | |
Chao Yu | 55cf9cb | 2014-09-15 18:01:10 +0800 | [diff] [blame] | 1208 | /* Currently, support 512/1024/2048/4096 bytes sector size */ |
| 1209 | if (le32_to_cpu(raw_super->log_sectorsize) > |
| 1210 | F2FS_MAX_LOG_SECTOR_SIZE || |
| 1211 | le32_to_cpu(raw_super->log_sectorsize) < |
| 1212 | F2FS_MIN_LOG_SECTOR_SIZE) { |
| 1213 | f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)", |
| 1214 | le32_to_cpu(raw_super->log_sectorsize)); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1215 | return 1; |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 1216 | } |
Chao Yu | 55cf9cb | 2014-09-15 18:01:10 +0800 | [diff] [blame] | 1217 | if (le32_to_cpu(raw_super->log_sectors_per_block) + |
| 1218 | le32_to_cpu(raw_super->log_sectorsize) != |
| 1219 | F2FS_MAX_LOG_SECTOR_SIZE) { |
| 1220 | f2fs_msg(sb, KERN_INFO, |
| 1221 | "Invalid log sectors per block(%u) log sectorsize(%u)", |
| 1222 | le32_to_cpu(raw_super->log_sectors_per_block), |
| 1223 | le32_to_cpu(raw_super->log_sectorsize)); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1224 | return 1; |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 1225 | } |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 1226 | |
| 1227 | /* check reserved ino info */ |
| 1228 | if (le32_to_cpu(raw_super->node_ino) != 1 || |
| 1229 | le32_to_cpu(raw_super->meta_ino) != 2 || |
| 1230 | le32_to_cpu(raw_super->root_ino) != 3) { |
| 1231 | f2fs_msg(sb, KERN_INFO, |
| 1232 | "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)", |
| 1233 | le32_to_cpu(raw_super->node_ino), |
| 1234 | le32_to_cpu(raw_super->meta_ino), |
| 1235 | le32_to_cpu(raw_super->root_ino)); |
| 1236 | return 1; |
| 1237 | } |
| 1238 | |
Jin Qian | 9386295 | 2017-04-25 16:28:48 -0700 | [diff] [blame] | 1239 | if (le32_to_cpu(raw_super->segment_count) > F2FS_MAX_SEGMENT) { |
| 1240 | f2fs_msg(sb, KERN_INFO, |
| 1241 | "Invalid segment count (%u)", |
| 1242 | le32_to_cpu(raw_super->segment_count)); |
| 1243 | return 1; |
| 1244 | } |
| 1245 | |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 1246 | /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */ |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1247 | if (sanity_check_area_boundary(sbi, bh)) |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 1248 | return 1; |
| 1249 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1250 | return 0; |
| 1251 | } |
| 1252 | |
Shawn Lin | 984ec63 | 2016-02-17 11:26:32 +0800 | [diff] [blame] | 1253 | int sanity_check_ckpt(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1254 | { |
| 1255 | unsigned int total, fsmeta; |
Jaegeuk Kim | 577e349 | 2013-01-24 19:56:11 +0900 | [diff] [blame] | 1256 | struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); |
| 1257 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1258 | unsigned int ovp_segments, reserved_segments; |
Jin Qian | d90659f | 2017-05-15 10:45:08 -0700 | [diff] [blame] | 1259 | unsigned int main_segs, blocks_per_seg; |
| 1260 | int i; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1261 | |
| 1262 | total = le32_to_cpu(raw_super->segment_count); |
| 1263 | fsmeta = le32_to_cpu(raw_super->segment_count_ckpt); |
| 1264 | fsmeta += le32_to_cpu(raw_super->segment_count_sit); |
| 1265 | fsmeta += le32_to_cpu(raw_super->segment_count_nat); |
| 1266 | fsmeta += le32_to_cpu(ckpt->rsvd_segment_count); |
| 1267 | fsmeta += le32_to_cpu(raw_super->segment_count_ssa); |
| 1268 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1269 | if (unlikely(fsmeta >= total)) |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1270 | return 1; |
Jaegeuk Kim | 577e349 | 2013-01-24 19:56:11 +0900 | [diff] [blame] | 1271 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1272 | ovp_segments = le32_to_cpu(ckpt->overprov_segment_count); |
| 1273 | reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count); |
| 1274 | |
| 1275 | if (unlikely(fsmeta < F2FS_MIN_SEGMENTS || |
| 1276 | ovp_segments == 0 || reserved_segments == 0)) { |
| 1277 | f2fs_msg(sbi->sb, KERN_ERR, |
| 1278 | "Wrong layout: check mkfs.f2fs version"); |
| 1279 | return 1; |
| 1280 | } |
| 1281 | |
Jin Qian | d90659f | 2017-05-15 10:45:08 -0700 | [diff] [blame] | 1282 | main_segs = le32_to_cpu(raw_super->segment_count_main); |
| 1283 | blocks_per_seg = sbi->blocks_per_seg; |
| 1284 | |
| 1285 | for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { |
| 1286 | if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs || |
| 1287 | le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg) |
| 1288 | return 1; |
| 1289 | } |
| 1290 | for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { |
| 1291 | if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs || |
| 1292 | le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg) |
| 1293 | return 1; |
| 1294 | } |
| 1295 | |
Jaegeuk Kim | 1e968fd | 2014-08-11 16:49:25 -0700 | [diff] [blame] | 1296 | if (unlikely(f2fs_cp_error(sbi))) { |
Jaegeuk Kim | 577e349 | 2013-01-24 19:56:11 +0900 | [diff] [blame] | 1297 | f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck"); |
| 1298 | return 1; |
| 1299 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1300 | return 0; |
| 1301 | } |
| 1302 | |
| 1303 | static void init_sb_info(struct f2fs_sb_info *sbi) |
| 1304 | { |
| 1305 | struct f2fs_super_block *raw_super = sbi->raw_super; |
Chao Yu | 0650468 | 2017-05-19 23:37:00 +0800 | [diff] [blame] | 1306 | int i, j; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1307 | |
| 1308 | sbi->log_sectors_per_block = |
| 1309 | le32_to_cpu(raw_super->log_sectors_per_block); |
| 1310 | sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize); |
| 1311 | sbi->blocksize = 1 << sbi->log_blocksize; |
| 1312 | sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg); |
| 1313 | sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg; |
| 1314 | sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec); |
| 1315 | sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone); |
| 1316 | sbi->total_sections = le32_to_cpu(raw_super->section_count); |
| 1317 | sbi->total_node_count = |
| 1318 | (le32_to_cpu(raw_super->segment_count_nat) / 2) |
| 1319 | * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK; |
| 1320 | sbi->root_ino_num = le32_to_cpu(raw_super->root_ino); |
| 1321 | sbi->node_ino_num = le32_to_cpu(raw_super->node_ino); |
| 1322 | sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino); |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 1323 | sbi->cur_victim_sec = NULL_SECNO; |
Jaegeuk Kim | b1c57c1 | 2014-01-08 13:45:08 +0900 | [diff] [blame] | 1324 | sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1325 | |
Jaegeuk Kim | ab9fa66 | 2014-02-27 20:09:05 +0900 | [diff] [blame] | 1326 | sbi->dir_level = DEF_DIR_LEVEL; |
Jaegeuk Kim | 6beceb5 | 2016-01-08 15:51:50 -0800 | [diff] [blame] | 1327 | sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL; |
Jaegeuk Kim | d0239e1 | 2016-01-08 16:57:48 -0800 | [diff] [blame] | 1328 | sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL; |
Chao Yu | caf0047 | 2015-01-28 17:48:42 +0800 | [diff] [blame] | 1329 | clear_sbi_flag(sbi, SBI_NEED_FSCK); |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 1330 | |
Jaegeuk Kim | 725ba1a | 2016-10-20 19:09:57 -0700 | [diff] [blame] | 1331 | for (i = 0; i < NR_COUNT_TYPE; i++) |
| 1332 | atomic_set(&sbi->nr_pages[i], 0); |
| 1333 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1334 | atomic_set(&sbi->wb_sync_req, 0); |
| 1335 | |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 1336 | INIT_LIST_HEAD(&sbi->s_list); |
| 1337 | mutex_init(&sbi->umount_mutex); |
Chao Yu | 0650468 | 2017-05-19 23:37:00 +0800 | [diff] [blame] | 1338 | for (i = 0; i < NR_PAGE_TYPE - 1; i++) |
| 1339 | for (j = HOT; j < NR_TEMP_TYPE; j++) |
| 1340 | mutex_init(&sbi->wio_mutex[i][j]); |
Chao Yu | aaec2b1 | 2016-09-20 11:04:18 +0800 | [diff] [blame] | 1341 | spin_lock_init(&sbi->cp_lock); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1342 | } |
| 1343 | |
Jaegeuk Kim | 523be8a | 2016-05-13 12:36:58 -0700 | [diff] [blame] | 1344 | static int init_percpu_info(struct f2fs_sb_info *sbi) |
| 1345 | { |
Jaegeuk Kim | 725ba1a | 2016-10-20 19:09:57 -0700 | [diff] [blame] | 1346 | int err; |
Jaegeuk Kim | 41382ec | 2016-05-16 11:06:50 -0700 | [diff] [blame] | 1347 | |
Jaegeuk Kim | 513c5f3 | 2016-05-16 11:42:32 -0700 | [diff] [blame] | 1348 | err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL); |
| 1349 | if (err) |
| 1350 | return err; |
| 1351 | |
| 1352 | return percpu_counter_init(&sbi->total_valid_inode_count, 0, |
Jaegeuk Kim | 41382ec | 2016-05-16 11:06:50 -0700 | [diff] [blame] | 1353 | GFP_KERNEL); |
Jaegeuk Kim | 523be8a | 2016-05-13 12:36:58 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1356 | #ifdef CONFIG_BLK_DEV_ZONED |
| 1357 | static int init_blkz_info(struct f2fs_sb_info *sbi, int devi) |
| 1358 | { |
| 1359 | struct block_device *bdev = FDEV(devi).bdev; |
| 1360 | sector_t nr_sectors = bdev->bd_part->nr_sects; |
| 1361 | sector_t sector = 0; |
| 1362 | struct blk_zone *zones; |
| 1363 | unsigned int i, nr_zones; |
| 1364 | unsigned int n = 0; |
| 1365 | int err = -EIO; |
| 1366 | |
| 1367 | if (!f2fs_sb_mounted_blkzoned(sbi->sb)) |
| 1368 | return 0; |
| 1369 | |
| 1370 | if (sbi->blocks_per_blkz && sbi->blocks_per_blkz != |
| 1371 | SECTOR_TO_BLOCK(bdev_zone_sectors(bdev))) |
| 1372 | return -EINVAL; |
| 1373 | sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)); |
| 1374 | if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz != |
| 1375 | __ilog2_u32(sbi->blocks_per_blkz)) |
| 1376 | return -EINVAL; |
| 1377 | sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz); |
| 1378 | FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >> |
| 1379 | sbi->log_blocks_per_blkz; |
| 1380 | if (nr_sectors & (bdev_zone_sectors(bdev) - 1)) |
| 1381 | FDEV(devi).nr_blkz++; |
| 1382 | |
| 1383 | FDEV(devi).blkz_type = kmalloc(FDEV(devi).nr_blkz, GFP_KERNEL); |
| 1384 | if (!FDEV(devi).blkz_type) |
| 1385 | return -ENOMEM; |
| 1386 | |
| 1387 | #define F2FS_REPORT_NR_ZONES 4096 |
| 1388 | |
| 1389 | zones = kcalloc(F2FS_REPORT_NR_ZONES, sizeof(struct blk_zone), |
| 1390 | GFP_KERNEL); |
| 1391 | if (!zones) |
| 1392 | return -ENOMEM; |
| 1393 | |
| 1394 | /* Get block zones type */ |
| 1395 | while (zones && sector < nr_sectors) { |
| 1396 | |
| 1397 | nr_zones = F2FS_REPORT_NR_ZONES; |
| 1398 | err = blkdev_report_zones(bdev, sector, |
| 1399 | zones, &nr_zones, |
| 1400 | GFP_KERNEL); |
| 1401 | if (err) |
| 1402 | break; |
| 1403 | if (!nr_zones) { |
| 1404 | err = -EIO; |
| 1405 | break; |
| 1406 | } |
| 1407 | |
| 1408 | for (i = 0; i < nr_zones; i++) { |
| 1409 | FDEV(devi).blkz_type[n] = zones[i].type; |
| 1410 | sector += zones[i].len; |
| 1411 | n++; |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | kfree(zones); |
| 1416 | |
| 1417 | return err; |
| 1418 | } |
| 1419 | #endif |
| 1420 | |
Gu Zheng | 9076a75 | 2013-10-14 18:47:11 +0800 | [diff] [blame] | 1421 | /* |
| 1422 | * Read f2fs raw super block. |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 1423 | * Because we have two copies of super block, so read both of them |
| 1424 | * to get the first valid one. If any one of them is broken, we pass |
| 1425 | * them recovery flag back to the caller. |
Gu Zheng | 9076a75 | 2013-10-14 18:47:11 +0800 | [diff] [blame] | 1426 | */ |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1427 | static int read_raw_super_block(struct f2fs_sb_info *sbi, |
Gu Zheng | 9076a75 | 2013-10-14 18:47:11 +0800 | [diff] [blame] | 1428 | struct f2fs_super_block **raw_super, |
Chao Yu | e8240f6 | 2015-12-15 17:19:26 +0800 | [diff] [blame] | 1429 | int *valid_super_block, int *recovery) |
majianpeng | 14d7e9d | 2013-02-01 19:07:03 +0800 | [diff] [blame] | 1430 | { |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1431 | struct super_block *sb = sbi->sb; |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 1432 | int block; |
Chao Yu | e8240f6 | 2015-12-15 17:19:26 +0800 | [diff] [blame] | 1433 | struct buffer_head *bh; |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1434 | struct f2fs_super_block *super; |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 1435 | int err = 0; |
majianpeng | 14d7e9d | 2013-02-01 19:07:03 +0800 | [diff] [blame] | 1436 | |
Yunlei He | b39f0de | 2015-12-15 17:17:20 +0800 | [diff] [blame] | 1437 | super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL); |
| 1438 | if (!super) |
| 1439 | return -ENOMEM; |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 1440 | |
| 1441 | for (block = 0; block < 2; block++) { |
| 1442 | bh = sb_bread(sb, block); |
| 1443 | if (!bh) { |
| 1444 | f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock", |
Gu Zheng | 9076a75 | 2013-10-14 18:47:11 +0800 | [diff] [blame] | 1445 | block + 1); |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 1446 | err = -EIO; |
| 1447 | continue; |
| 1448 | } |
majianpeng | 14d7e9d | 2013-02-01 19:07:03 +0800 | [diff] [blame] | 1449 | |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 1450 | /* sanity checking of raw super */ |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1451 | if (sanity_check_raw_super(sbi, bh)) { |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 1452 | f2fs_msg(sb, KERN_ERR, |
| 1453 | "Can't find valid F2FS filesystem in %dth superblock", |
| 1454 | block + 1); |
| 1455 | err = -EINVAL; |
| 1456 | brelse(bh); |
| 1457 | continue; |
| 1458 | } |
| 1459 | |
| 1460 | if (!*raw_super) { |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1461 | memcpy(super, bh->b_data + F2FS_SUPER_OFFSET, |
| 1462 | sizeof(*super)); |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 1463 | *valid_super_block = block; |
| 1464 | *raw_super = super; |
| 1465 | } |
Chao Yu | e8240f6 | 2015-12-15 17:19:26 +0800 | [diff] [blame] | 1466 | brelse(bh); |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | /* Fail to read any one of the superblocks*/ |
| 1470 | if (err < 0) |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 1471 | *recovery = 1; |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 1472 | |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 1473 | /* No valid superblock */ |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 1474 | if (!*raw_super) |
Yunlei He | b39f0de | 2015-12-15 17:17:20 +0800 | [diff] [blame] | 1475 | kfree(super); |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 1476 | else |
| 1477 | err = 0; |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 1478 | |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 1479 | return err; |
majianpeng | 14d7e9d | 2013-02-01 19:07:03 +0800 | [diff] [blame] | 1480 | } |
| 1481 | |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1482 | int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover) |
Jaegeuk Kim | 26d815ad | 2015-04-20 18:49:51 -0700 | [diff] [blame] | 1483 | { |
Jaegeuk Kim | 5d909cd | 2015-12-07 10:16:58 -0800 | [diff] [blame] | 1484 | struct buffer_head *bh; |
Jaegeuk Kim | 26d815ad | 2015-04-20 18:49:51 -0700 | [diff] [blame] | 1485 | int err; |
| 1486 | |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1487 | if ((recover && f2fs_readonly(sbi->sb)) || |
| 1488 | bdev_read_only(sbi->sb->s_bdev)) { |
| 1489 | set_sbi_flag(sbi, SBI_NEED_SB_WRITE); |
Jaegeuk Kim | f2353d7 | 2016-03-23 10:42:01 -0700 | [diff] [blame] | 1490 | return -EROFS; |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1491 | } |
Jaegeuk Kim | f2353d7 | 2016-03-23 10:42:01 -0700 | [diff] [blame] | 1492 | |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1493 | /* write back-up superblock first */ |
| 1494 | bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1); |
Jaegeuk Kim | 5d909cd | 2015-12-07 10:16:58 -0800 | [diff] [blame] | 1495 | if (!bh) |
| 1496 | return -EIO; |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1497 | err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi)); |
Jaegeuk Kim | 5d909cd | 2015-12-07 10:16:58 -0800 | [diff] [blame] | 1498 | brelse(bh); |
Chao Yu | c5bda1c | 2015-06-08 13:28:03 +0800 | [diff] [blame] | 1499 | |
| 1500 | /* if we are in recovery path, skip writing valid superblock */ |
| 1501 | if (recover || err) |
Jaegeuk Kim | 5d909cd | 2015-12-07 10:16:58 -0800 | [diff] [blame] | 1502 | return err; |
Jaegeuk Kim | 26d815ad | 2015-04-20 18:49:51 -0700 | [diff] [blame] | 1503 | |
Chao Yu | e8240f6 | 2015-12-15 17:19:26 +0800 | [diff] [blame] | 1504 | /* write current valid superblock */ |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1505 | bh = sb_getblk(sbi->sb, sbi->valid_super_block); |
| 1506 | if (!bh) |
| 1507 | return -EIO; |
| 1508 | err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi)); |
| 1509 | brelse(bh); |
| 1510 | return err; |
Jaegeuk Kim | 26d815ad | 2015-04-20 18:49:51 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1513 | static int f2fs_scan_devices(struct f2fs_sb_info *sbi) |
| 1514 | { |
| 1515 | struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); |
| 1516 | unsigned int max_devices = MAX_DEVICES; |
| 1517 | int i; |
| 1518 | |
| 1519 | /* Initialize single device information */ |
| 1520 | if (!RDEV(0).path[0]) { |
| 1521 | #ifdef CONFIG_BLK_DEV_ZONED |
| 1522 | if (bdev_zoned_model(sbi->sb->s_bdev) == BLK_ZONED_NONE) |
| 1523 | return 0; |
| 1524 | max_devices = 1; |
| 1525 | #else |
| 1526 | return 0; |
| 1527 | #endif |
| 1528 | } |
| 1529 | |
| 1530 | /* |
| 1531 | * Initialize multiple devices information, or single |
| 1532 | * zoned block device information. |
| 1533 | */ |
| 1534 | sbi->devs = kcalloc(max_devices, sizeof(struct f2fs_dev_info), |
| 1535 | GFP_KERNEL); |
| 1536 | if (!sbi->devs) |
| 1537 | return -ENOMEM; |
| 1538 | |
| 1539 | for (i = 0; i < max_devices; i++) { |
| 1540 | |
| 1541 | if (i > 0 && !RDEV(i).path[0]) |
| 1542 | break; |
| 1543 | |
| 1544 | if (max_devices == 1) { |
| 1545 | /* Single zoned block device mount */ |
| 1546 | FDEV(0).bdev = |
| 1547 | blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev, |
| 1548 | sbi->sb->s_mode, sbi->sb->s_type); |
| 1549 | } else { |
| 1550 | /* Multi-device mount */ |
| 1551 | memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN); |
| 1552 | FDEV(i).total_segments = |
| 1553 | le32_to_cpu(RDEV(i).total_segments); |
| 1554 | if (i == 0) { |
| 1555 | FDEV(i).start_blk = 0; |
| 1556 | FDEV(i).end_blk = FDEV(i).start_blk + |
| 1557 | (FDEV(i).total_segments << |
| 1558 | sbi->log_blocks_per_seg) - 1 + |
| 1559 | le32_to_cpu(raw_super->segment0_blkaddr); |
| 1560 | } else { |
| 1561 | FDEV(i).start_blk = FDEV(i - 1).end_blk + 1; |
| 1562 | FDEV(i).end_blk = FDEV(i).start_blk + |
| 1563 | (FDEV(i).total_segments << |
| 1564 | sbi->log_blocks_per_seg) - 1; |
| 1565 | } |
| 1566 | FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path, |
| 1567 | sbi->sb->s_mode, sbi->sb->s_type); |
| 1568 | } |
| 1569 | if (IS_ERR(FDEV(i).bdev)) |
| 1570 | return PTR_ERR(FDEV(i).bdev); |
| 1571 | |
| 1572 | /* to release errored devices */ |
| 1573 | sbi->s_ndevs = i + 1; |
| 1574 | |
| 1575 | #ifdef CONFIG_BLK_DEV_ZONED |
| 1576 | if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM && |
| 1577 | !f2fs_sb_mounted_blkzoned(sbi->sb)) { |
| 1578 | f2fs_msg(sbi->sb, KERN_ERR, |
| 1579 | "Zoned block device feature not enabled\n"); |
| 1580 | return -EINVAL; |
| 1581 | } |
| 1582 | if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) { |
| 1583 | if (init_blkz_info(sbi, i)) { |
| 1584 | f2fs_msg(sbi->sb, KERN_ERR, |
| 1585 | "Failed to initialize F2FS blkzone information"); |
| 1586 | return -EINVAL; |
| 1587 | } |
| 1588 | if (max_devices == 1) |
| 1589 | break; |
| 1590 | f2fs_msg(sbi->sb, KERN_INFO, |
| 1591 | "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)", |
| 1592 | i, FDEV(i).path, |
| 1593 | FDEV(i).total_segments, |
| 1594 | FDEV(i).start_blk, FDEV(i).end_blk, |
| 1595 | bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ? |
| 1596 | "Host-aware" : "Host-managed"); |
| 1597 | continue; |
| 1598 | } |
| 1599 | #endif |
| 1600 | f2fs_msg(sbi->sb, KERN_INFO, |
| 1601 | "Mount Device [%2d]: %20s, %8u, %8x - %8x", |
| 1602 | i, FDEV(i).path, |
| 1603 | FDEV(i).total_segments, |
| 1604 | FDEV(i).start_blk, FDEV(i).end_blk); |
| 1605 | } |
| 1606 | f2fs_msg(sbi->sb, KERN_INFO, |
| 1607 | "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi)); |
| 1608 | return 0; |
| 1609 | } |
| 1610 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1611 | static int f2fs_fill_super(struct super_block *sb, void *data, int silent) |
| 1612 | { |
| 1613 | struct f2fs_sb_info *sbi; |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 1614 | struct f2fs_super_block *raw_super; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1615 | struct inode *root; |
Sheng Yong | 99e3e85 | 2016-05-11 17:08:14 +0800 | [diff] [blame] | 1616 | int err; |
Chao Yu | 2adc350 | 2015-03-16 21:08:44 +0800 | [diff] [blame] | 1617 | bool retry = true, need_fsck = false; |
Jaegeuk Kim | dabc4a5 | 2015-01-23 17:41:39 -0800 | [diff] [blame] | 1618 | char *options = NULL; |
Chao Yu | e8240f6 | 2015-12-15 17:19:26 +0800 | [diff] [blame] | 1619 | int recovery, i, valid_super_block; |
Shuoran Liu | 8f1dbbb | 2016-01-27 09:57:30 +0800 | [diff] [blame] | 1620 | struct curseg_info *seg_i; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1621 | |
Jaegeuk Kim | ed2e621 | 2014-08-08 15:37:41 -0700 | [diff] [blame] | 1622 | try_onemore: |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 1623 | err = -EINVAL; |
| 1624 | raw_super = NULL; |
Chao Yu | e8240f6 | 2015-12-15 17:19:26 +0800 | [diff] [blame] | 1625 | valid_super_block = -1; |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 1626 | recovery = 0; |
| 1627 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1628 | /* allocate memory for f2fs-specific super block info */ |
| 1629 | sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL); |
| 1630 | if (!sbi) |
| 1631 | return -ENOMEM; |
| 1632 | |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1633 | sbi->sb = sb; |
| 1634 | |
Keith Mok | 43b6573 | 2016-03-02 12:04:24 -0800 | [diff] [blame] | 1635 | /* Load the checksum driver */ |
| 1636 | sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0); |
| 1637 | if (IS_ERR(sbi->s_chksum_driver)) { |
| 1638 | f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver."); |
| 1639 | err = PTR_ERR(sbi->s_chksum_driver); |
| 1640 | sbi->s_chksum_driver = NULL; |
| 1641 | goto free_sbi; |
| 1642 | } |
| 1643 | |
Namjae Jeon | ff9234a | 2013-01-12 14:41:13 +0900 | [diff] [blame] | 1644 | /* set a block size */ |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1645 | if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) { |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 1646 | f2fs_msg(sb, KERN_ERR, "unable to set blocksize"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1647 | goto free_sbi; |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 1648 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1649 | |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1650 | err = read_raw_super_block(sbi, &raw_super, &valid_super_block, |
Chao Yu | e8240f6 | 2015-12-15 17:19:26 +0800 | [diff] [blame] | 1651 | &recovery); |
Gu Zheng | 9076a75 | 2013-10-14 18:47:11 +0800 | [diff] [blame] | 1652 | if (err) |
| 1653 | goto free_sbi; |
| 1654 | |
Gu Zheng | 5fb0837 | 2013-06-07 14:16:53 +0800 | [diff] [blame] | 1655 | sb->s_fs_info = sbi; |
Jaegeuk Kim | 52763a4 | 2016-06-13 09:47:48 -0700 | [diff] [blame] | 1656 | sbi->raw_super = raw_super; |
| 1657 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1658 | /* |
| 1659 | * The BLKZONED feature indicates that the drive was formatted with |
| 1660 | * zone alignment optimization. This is optional for host-aware |
| 1661 | * devices, but mandatory for host-managed zoned block devices. |
| 1662 | */ |
| 1663 | #ifndef CONFIG_BLK_DEV_ZONED |
| 1664 | if (f2fs_sb_mounted_blkzoned(sb)) { |
| 1665 | f2fs_msg(sb, KERN_ERR, |
| 1666 | "Zoned block device support is not enabled\n"); |
Chao Yu | 3b0de56 | 2017-06-12 09:44:27 +0800 | [diff] [blame] | 1667 | err = -EOPNOTSUPP; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1668 | goto free_sb_buf; |
| 1669 | } |
| 1670 | #endif |
Yunlei He | 498c5e9 | 2015-05-07 18:11:37 +0800 | [diff] [blame] | 1671 | default_options(sbi); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1672 | /* parse mount options */ |
Jaegeuk Kim | dabc4a5 | 2015-01-23 17:41:39 -0800 | [diff] [blame] | 1673 | options = kstrdup((const char *)data, GFP_KERNEL); |
| 1674 | if (data && !options) { |
| 1675 | err = -ENOMEM; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1676 | goto free_sb_buf; |
Jaegeuk Kim | dabc4a5 | 2015-01-23 17:41:39 -0800 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | err = parse_options(sb, options); |
| 1680 | if (err) |
| 1681 | goto free_options; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1682 | |
Chao Yu | e0afc4d | 2015-12-31 14:35:37 +0800 | [diff] [blame] | 1683 | sbi->max_file_blocks = max_file_blocks(); |
| 1684 | sb->s_maxbytes = sbi->max_file_blocks << |
| 1685 | le32_to_cpu(raw_super->log_blocksize); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1686 | sb->s_max_links = F2FS_LINK_MAX; |
| 1687 | get_random_bytes(&sbi->s_next_generation, sizeof(u32)); |
| 1688 | |
| 1689 | sb->s_op = &f2fs_sops; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 1690 | sb->s_cop = &f2fs_cryptops; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1691 | sb->s_xattr = f2fs_xattr_handlers; |
| 1692 | sb->s_export_op = &f2fs_export_ops; |
| 1693 | sb->s_magic = F2FS_SUPER_MAGIC; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1694 | sb->s_time_gran = 1; |
| 1695 | sb->s_flags = (sb->s_flags & ~MS_POSIXACL) | |
| 1696 | (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1697 | memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid)); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1698 | |
| 1699 | /* init f2fs-specific super block info */ |
Chao Yu | e8240f6 | 2015-12-15 17:19:26 +0800 | [diff] [blame] | 1700 | sbi->valid_super_block = valid_super_block; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1701 | mutex_init(&sbi->gc_mutex); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1702 | mutex_init(&sbi->cp_mutex); |
Chao Yu | b3582c6 | 2014-07-03 18:58:39 +0800 | [diff] [blame] | 1703 | init_rwsem(&sbi->node_write); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1704 | init_rwsem(&sbi->node_change); |
Jaegeuk Kim | 315df83 | 2015-08-11 12:45:39 -0700 | [diff] [blame] | 1705 | |
| 1706 | /* disallow all the data/node/meta page writes */ |
| 1707 | set_sbi_flag(sbi, SBI_POR_DOING); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1708 | spin_lock_init(&sbi->stat_lock); |
Jaegeuk Kim | 971767c | 2013-11-18 17:16:17 +0900 | [diff] [blame] | 1709 | |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1710 | for (i = 0; i < NR_PAGE_TYPE; i++) { |
Jaegeuk Kim | c412726 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 1711 | int n = (i == META) ? 1: NR_TEMP_TYPE; |
| 1712 | int j; |
| 1713 | |
| 1714 | sbi->write_io[i] = kmalloc(n * sizeof(struct f2fs_bio_info), |
| 1715 | GFP_KERNEL); |
Christophe JAILLET | d0917a4 | 2017-06-11 09:21:11 +0200 | [diff] [blame] | 1716 | if (!sbi->write_io[i]) { |
| 1717 | err = -ENOMEM; |
Jaegeuk Kim | c412726 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 1718 | goto free_options; |
Christophe JAILLET | d0917a4 | 2017-06-11 09:21:11 +0200 | [diff] [blame] | 1719 | } |
Jaegeuk Kim | c412726 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 1720 | |
| 1721 | for (j = HOT; j < n; j++) { |
| 1722 | init_rwsem(&sbi->write_io[i][j].io_rwsem); |
| 1723 | sbi->write_io[i][j].sbi = sbi; |
| 1724 | sbi->write_io[i][j].bio = NULL; |
Chao Yu | c52dc0f | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 1725 | spin_lock_init(&sbi->write_io[i][j].io_lock); |
| 1726 | INIT_LIST_HEAD(&sbi->write_io[i][j].io_list); |
Jaegeuk Kim | c412726 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 1727 | } |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1728 | } |
Jaegeuk Kim | 971767c | 2013-11-18 17:16:17 +0900 | [diff] [blame] | 1729 | |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 1730 | init_rwsem(&sbi->cp_rwsem); |
Changman Lee | fb51b5e | 2013-11-07 12:48:25 +0900 | [diff] [blame] | 1731 | init_waitqueue_head(&sbi->cp_wait); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1732 | init_sb_info(sbi); |
| 1733 | |
Jaegeuk Kim | 523be8a | 2016-05-13 12:36:58 -0700 | [diff] [blame] | 1734 | err = init_percpu_info(sbi); |
| 1735 | if (err) |
| 1736 | goto free_options; |
| 1737 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1738 | if (F2FS_IO_SIZE(sbi) > 1) { |
| 1739 | sbi->write_io_dummy = |
| 1740 | mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0); |
Chao Yu | 3b0de56 | 2017-06-12 09:44:27 +0800 | [diff] [blame] | 1741 | if (!sbi->write_io_dummy) { |
| 1742 | err = -ENOMEM; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1743 | goto free_options; |
Chao Yu | 3b0de56 | 2017-06-12 09:44:27 +0800 | [diff] [blame] | 1744 | } |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1745 | } |
| 1746 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1747 | /* get an inode for meta space */ |
| 1748 | sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi)); |
| 1749 | if (IS_ERR(sbi->meta_inode)) { |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 1750 | f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1751 | err = PTR_ERR(sbi->meta_inode); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1752 | goto free_io_dummy; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | err = get_valid_checkpoint(sbi); |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 1756 | if (err) { |
| 1757 | f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1758 | goto free_meta_inode; |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 1759 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1760 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1761 | /* Initialize device list */ |
| 1762 | err = f2fs_scan_devices(sbi); |
| 1763 | if (err) { |
| 1764 | f2fs_msg(sb, KERN_ERR, "Failed to find devices"); |
| 1765 | goto free_devices; |
| 1766 | } |
| 1767 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1768 | sbi->total_valid_node_count = |
| 1769 | le32_to_cpu(sbi->ckpt->valid_node_count); |
Jaegeuk Kim | 513c5f3 | 2016-05-16 11:42:32 -0700 | [diff] [blame] | 1770 | percpu_counter_set(&sbi->total_valid_inode_count, |
| 1771 | le32_to_cpu(sbi->ckpt->valid_inode_count)); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1772 | sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count); |
| 1773 | sbi->total_valid_block_count = |
| 1774 | le64_to_cpu(sbi->ckpt->valid_block_count); |
| 1775 | sbi->last_valid_block_count = sbi->total_valid_block_count; |
Chao Yu | 026bd9d | 2017-06-26 16:24:41 +0800 | [diff] [blame^] | 1776 | sbi->reserved_blocks = 0; |
Jaegeuk Kim | 41382ec | 2016-05-16 11:06:50 -0700 | [diff] [blame] | 1777 | |
Chao Yu | c227f91 | 2015-12-16 13:09:20 +0800 | [diff] [blame] | 1778 | for (i = 0; i < NR_INODE_TYPE; i++) { |
| 1779 | INIT_LIST_HEAD(&sbi->inode_list[i]); |
| 1780 | spin_lock_init(&sbi->inode_lock[i]); |
| 1781 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1782 | |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 1783 | init_extent_cache_info(sbi); |
| 1784 | |
Jaegeuk Kim | 6451e04 | 2014-07-25 15:47:17 -0700 | [diff] [blame] | 1785 | init_ino_entry_info(sbi); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1786 | |
| 1787 | /* setup f2fs internal modules */ |
| 1788 | err = build_segment_manager(sbi); |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 1789 | if (err) { |
| 1790 | f2fs_msg(sb, KERN_ERR, |
| 1791 | "Failed to initialize F2FS segment manager"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1792 | goto free_sm; |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 1793 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1794 | err = build_node_manager(sbi); |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 1795 | if (err) { |
| 1796 | f2fs_msg(sb, KERN_ERR, |
| 1797 | "Failed to initialize F2FS node manager"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1798 | goto free_nm; |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 1799 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1800 | |
Shuoran Liu | 8f1dbbb | 2016-01-27 09:57:30 +0800 | [diff] [blame] | 1801 | /* For write statistics */ |
| 1802 | if (sb->s_bdev->bd_part) |
| 1803 | sbi->sectors_written_start = |
| 1804 | (u64)part_stat_read(sb->s_bdev->bd_part, sectors[1]); |
| 1805 | |
| 1806 | /* Read accumulated write IO statistics if exists */ |
| 1807 | seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE); |
| 1808 | if (__exist_node_summaries(sbi)) |
| 1809 | sbi->kbytes_written = |
Shuoran Liu | b2dde6f | 2016-03-29 18:00:15 +0800 | [diff] [blame] | 1810 | le64_to_cpu(seg_i->journal->info.kbytes_written); |
Shuoran Liu | 8f1dbbb | 2016-01-27 09:57:30 +0800 | [diff] [blame] | 1811 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1812 | build_gc_manager(sbi); |
| 1813 | |
| 1814 | /* get an inode for node space */ |
| 1815 | sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi)); |
| 1816 | if (IS_ERR(sbi->node_inode)) { |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 1817 | f2fs_msg(sb, KERN_ERR, "Failed to read node inode"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1818 | err = PTR_ERR(sbi->node_inode); |
| 1819 | goto free_nm; |
| 1820 | } |
| 1821 | |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 1822 | f2fs_join_shrinker(sbi); |
| 1823 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1824 | err = f2fs_build_stats(sbi); |
| 1825 | if (err) |
| 1826 | goto free_nm; |
| 1827 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1828 | /* if there are nt orphan nodes free them */ |
Chao Yu | 8c14bfa | 2015-08-07 17:58:43 +0800 | [diff] [blame] | 1829 | err = recover_orphan_inodes(sbi); |
| 1830 | if (err) |
| 1831 | goto free_node_inode; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1832 | |
| 1833 | /* read root inode and dentry */ |
| 1834 | root = f2fs_iget(sb, F2FS_ROOT_INO(sbi)); |
| 1835 | if (IS_ERR(root)) { |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 1836 | f2fs_msg(sb, KERN_ERR, "Failed to read root inode"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1837 | err = PTR_ERR(root); |
| 1838 | goto free_node_inode; |
| 1839 | } |
Chao Yu | 8f99a94 | 2013-11-28 15:43:43 +0800 | [diff] [blame] | 1840 | if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) { |
Chao Yu | 9d84795 | 2014-07-25 12:55:09 +0800 | [diff] [blame] | 1841 | iput(root); |
Chao Yu | 8f99a94 | 2013-11-28 15:43:43 +0800 | [diff] [blame] | 1842 | err = -EINVAL; |
Chao Yu | 9d84795 | 2014-07-25 12:55:09 +0800 | [diff] [blame] | 1843 | goto free_node_inode; |
Chao Yu | 8f99a94 | 2013-11-28 15:43:43 +0800 | [diff] [blame] | 1844 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1845 | |
| 1846 | sb->s_root = d_make_root(root); /* allocate root dentry */ |
| 1847 | if (!sb->s_root) { |
| 1848 | err = -ENOMEM; |
| 1849 | goto free_root_inode; |
| 1850 | } |
| 1851 | |
Chao Yu | 17a3fb5 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 1852 | err = f2fs_init_sysfs(sbi); |
Namjae Jeon | b59d0ba | 2013-08-04 23:09:40 +0900 | [diff] [blame] | 1853 | if (err) |
Chao Yu | 17a3fb5 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 1854 | goto free_root_inode; |
Namjae Jeon | b59d0ba | 2013-08-04 23:09:40 +0900 | [diff] [blame] | 1855 | |
Jaegeuk Kim | 6437d1b | 2014-02-19 18:23:32 +0900 | [diff] [blame] | 1856 | /* recover fsynced data */ |
| 1857 | if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) { |
Jaegeuk Kim | 081d78c | 2015-01-23 19:16:59 -0800 | [diff] [blame] | 1858 | /* |
| 1859 | * mount should be failed, when device has readonly mode, and |
| 1860 | * previous checkpoint was not done by clean system shutdown. |
| 1861 | */ |
| 1862 | if (bdev_read_only(sb->s_bdev) && |
Chao Yu | aaec2b1 | 2016-09-20 11:04:18 +0800 | [diff] [blame] | 1863 | !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) { |
Jaegeuk Kim | 081d78c | 2015-01-23 19:16:59 -0800 | [diff] [blame] | 1864 | err = -EROFS; |
Chao Yu | 17a3fb5 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 1865 | goto free_sysfs; |
Jaegeuk Kim | 081d78c | 2015-01-23 19:16:59 -0800 | [diff] [blame] | 1866 | } |
Chao Yu | 2adc350 | 2015-03-16 21:08:44 +0800 | [diff] [blame] | 1867 | |
| 1868 | if (need_fsck) |
| 1869 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
| 1870 | |
Jaegeuk Kim | a468f0e | 2016-09-19 17:55:10 -0700 | [diff] [blame] | 1871 | if (!retry) |
| 1872 | goto skip_recovery; |
| 1873 | |
Jaegeuk Kim | 6781eab | 2016-03-23 16:12:58 -0700 | [diff] [blame] | 1874 | err = recover_fsync_data(sbi, false); |
| 1875 | if (err < 0) { |
Chao Yu | 2adc350 | 2015-03-16 21:08:44 +0800 | [diff] [blame] | 1876 | need_fsck = true; |
Jaegeuk Kim | 6437d1b | 2014-02-19 18:23:32 +0900 | [diff] [blame] | 1877 | f2fs_msg(sb, KERN_ERR, |
Sheng Yong | 99e3e85 | 2016-05-11 17:08:14 +0800 | [diff] [blame] | 1878 | "Cannot recover all fsync data errno=%d", err); |
Chao Yu | 17a3fb5 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 1879 | goto free_sysfs; |
Jaegeuk Kim | ed2e621 | 2014-08-08 15:37:41 -0700 | [diff] [blame] | 1880 | } |
Jaegeuk Kim | 6781eab | 2016-03-23 16:12:58 -0700 | [diff] [blame] | 1881 | } else { |
| 1882 | err = recover_fsync_data(sbi, true); |
| 1883 | |
| 1884 | if (!f2fs_readonly(sb) && err > 0) { |
| 1885 | err = -EINVAL; |
| 1886 | f2fs_msg(sb, KERN_ERR, |
| 1887 | "Need to recover fsync data"); |
Chao Yu | 17a3fb5 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 1888 | goto free_sysfs; |
Jaegeuk Kim | 6781eab | 2016-03-23 16:12:58 -0700 | [diff] [blame] | 1889 | } |
Jaegeuk Kim | 6437d1b | 2014-02-19 18:23:32 +0900 | [diff] [blame] | 1890 | } |
Jaegeuk Kim | a468f0e | 2016-09-19 17:55:10 -0700 | [diff] [blame] | 1891 | skip_recovery: |
Jaegeuk Kim | 315df83 | 2015-08-11 12:45:39 -0700 | [diff] [blame] | 1892 | /* recover_fsync_data() cleared this already */ |
| 1893 | clear_sbi_flag(sbi, SBI_POR_DOING); |
Jaegeuk Kim | 6437d1b | 2014-02-19 18:23:32 +0900 | [diff] [blame] | 1894 | |
| 1895 | /* |
| 1896 | * If filesystem is not mounted as read-only then |
| 1897 | * do start the gc_thread. |
| 1898 | */ |
Chao Yu | 6c02993 | 2014-11-18 11:16:01 +0800 | [diff] [blame] | 1899 | if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) { |
Jaegeuk Kim | 6437d1b | 2014-02-19 18:23:32 +0900 | [diff] [blame] | 1900 | /* After POR, we can run background GC thread.*/ |
| 1901 | err = start_gc_thread(sbi); |
| 1902 | if (err) |
Chao Yu | 17a3fb5 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 1903 | goto free_sysfs; |
Jaegeuk Kim | 6437d1b | 2014-02-19 18:23:32 +0900 | [diff] [blame] | 1904 | } |
Jaegeuk Kim | dabc4a5 | 2015-01-23 17:41:39 -0800 | [diff] [blame] | 1905 | kfree(options); |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 1906 | |
| 1907 | /* recover broken superblock */ |
Jaegeuk Kim | f2353d7 | 2016-03-23 10:42:01 -0700 | [diff] [blame] | 1908 | if (recovery) { |
Chao Yu | 41214b3 | 2016-02-22 18:33:20 +0800 | [diff] [blame] | 1909 | err = f2fs_commit_super(sbi, true); |
| 1910 | f2fs_msg(sb, KERN_INFO, |
Sheng Yong | 99e3e85 | 2016-05-11 17:08:14 +0800 | [diff] [blame] | 1911 | "Try to recover %dth superblock, ret: %d", |
Chao Yu | 41214b3 | 2016-02-22 18:33:20 +0800 | [diff] [blame] | 1912 | sbi->valid_super_block ? 1 : 2, err); |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 1913 | } |
| 1914 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1915 | f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx", |
| 1916 | cur_cp_version(F2FS_CKPT(sbi))); |
Jaegeuk Kim | 6beceb5 | 2016-01-08 15:51:50 -0800 | [diff] [blame] | 1917 | f2fs_update_time(sbi, CP_TIME); |
Jaegeuk Kim | d0239e1 | 2016-01-08 16:57:48 -0800 | [diff] [blame] | 1918 | f2fs_update_time(sbi, REQ_TIME); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1919 | return 0; |
Jaegeuk Kim | 6437d1b | 2014-02-19 18:23:32 +0900 | [diff] [blame] | 1920 | |
Chao Yu | 17a3fb5 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 1921 | free_sysfs: |
Jaegeuk Kim | 0f18b46 | 2016-05-20 11:10:10 -0700 | [diff] [blame] | 1922 | f2fs_sync_inode_meta(sbi); |
Chao Yu | 17a3fb5 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 1923 | f2fs_exit_sysfs(sbi); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1924 | free_root_inode: |
| 1925 | dput(sb->s_root); |
| 1926 | sb->s_root = NULL; |
| 1927 | free_node_inode: |
Jaegeuk Kim | bb5dada | 2016-09-23 11:29:00 -0700 | [diff] [blame] | 1928 | truncate_inode_pages_final(NODE_MAPPING(sbi)); |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 1929 | mutex_lock(&sbi->umount_mutex); |
Jaegeuk Kim | d41065e | 2016-09-21 11:39:42 -0700 | [diff] [blame] | 1930 | release_ino_entry(sbi, true); |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 1931 | f2fs_leave_shrinker(sbi); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1932 | /* |
| 1933 | * Some dirty meta pages can be produced by recover_orphan_inodes() |
| 1934 | * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg() |
| 1935 | * followed by write_checkpoint() through f2fs_write_node_pages(), which |
| 1936 | * falls into an infinite loop in sync_meta_pages(). |
| 1937 | */ |
| 1938 | truncate_inode_pages_final(META_MAPPING(sbi)); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1939 | iput(sbi->node_inode); |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 1940 | mutex_unlock(&sbi->umount_mutex); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1941 | f2fs_destroy_stats(sbi); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1942 | free_nm: |
| 1943 | destroy_node_manager(sbi); |
| 1944 | free_sm: |
| 1945 | destroy_segment_manager(sbi); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1946 | free_devices: |
| 1947 | destroy_device_list(sbi); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1948 | kfree(sbi->ckpt); |
| 1949 | free_meta_inode: |
| 1950 | make_bad_inode(sbi->meta_inode); |
| 1951 | iput(sbi->meta_inode); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1952 | free_io_dummy: |
| 1953 | mempool_destroy(sbi->write_io_dummy); |
Jaegeuk Kim | dabc4a5 | 2015-01-23 17:41:39 -0800 | [diff] [blame] | 1954 | free_options: |
Jaegeuk Kim | c412726 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 1955 | for (i = 0; i < NR_PAGE_TYPE; i++) |
| 1956 | kfree(sbi->write_io[i]); |
Jaegeuk Kim | 523be8a | 2016-05-13 12:36:58 -0700 | [diff] [blame] | 1957 | destroy_percpu_info(sbi); |
Jaegeuk Kim | dabc4a5 | 2015-01-23 17:41:39 -0800 | [diff] [blame] | 1958 | kfree(options); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1959 | free_sb_buf: |
Yunlei He | b39f0de | 2015-12-15 17:17:20 +0800 | [diff] [blame] | 1960 | kfree(raw_super); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1961 | free_sbi: |
Keith Mok | 43b6573 | 2016-03-02 12:04:24 -0800 | [diff] [blame] | 1962 | if (sbi->s_chksum_driver) |
| 1963 | crypto_free_shash(sbi->s_chksum_driver); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1964 | kfree(sbi); |
Jaegeuk Kim | ed2e621 | 2014-08-08 15:37:41 -0700 | [diff] [blame] | 1965 | |
| 1966 | /* give only one another chance */ |
| 1967 | if (retry) { |
Taehee Yoo | 9df47ba | 2015-04-13 21:48:06 +0900 | [diff] [blame] | 1968 | retry = false; |
Jaegeuk Kim | ed2e621 | 2014-08-08 15:37:41 -0700 | [diff] [blame] | 1969 | shrink_dcache_sb(sb); |
| 1970 | goto try_onemore; |
| 1971 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1972 | return err; |
| 1973 | } |
| 1974 | |
| 1975 | static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags, |
| 1976 | const char *dev_name, void *data) |
| 1977 | { |
| 1978 | return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super); |
| 1979 | } |
| 1980 | |
Jaegeuk Kim | 30a5537 | 2015-01-14 16:34:24 -0800 | [diff] [blame] | 1981 | static void kill_f2fs_super(struct super_block *sb) |
| 1982 | { |
| 1983 | if (sb->s_root) |
Chao Yu | caf0047 | 2015-01-28 17:48:42 +0800 | [diff] [blame] | 1984 | set_sbi_flag(F2FS_SB(sb), SBI_IS_CLOSE); |
Jaegeuk Kim | 30a5537 | 2015-01-14 16:34:24 -0800 | [diff] [blame] | 1985 | kill_block_super(sb); |
| 1986 | } |
| 1987 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1988 | static struct file_system_type f2fs_fs_type = { |
| 1989 | .owner = THIS_MODULE, |
| 1990 | .name = "f2fs", |
| 1991 | .mount = f2fs_mount, |
Jaegeuk Kim | 30a5537 | 2015-01-14 16:34:24 -0800 | [diff] [blame] | 1992 | .kill_sb = kill_f2fs_super, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1993 | .fs_flags = FS_REQUIRES_DEV, |
| 1994 | }; |
Eric W. Biederman | 7f78e03 | 2013-03-02 19:39:14 -0800 | [diff] [blame] | 1995 | MODULE_ALIAS_FS("f2fs"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1996 | |
Namjae Jeon | 6e6093a | 2013-01-17 00:08:30 +0900 | [diff] [blame] | 1997 | static int __init init_inodecache(void) |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1998 | { |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 1999 | f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache", |
| 2000 | sizeof(struct f2fs_inode_info), 0, |
| 2001 | SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 2002 | if (!f2fs_inode_cachep) |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2003 | return -ENOMEM; |
| 2004 | return 0; |
| 2005 | } |
| 2006 | |
| 2007 | static void destroy_inodecache(void) |
| 2008 | { |
| 2009 | /* |
| 2010 | * Make sure all delayed rcu free inodes are flushed before we |
| 2011 | * destroy cache. |
| 2012 | */ |
| 2013 | rcu_barrier(); |
| 2014 | kmem_cache_destroy(f2fs_inode_cachep); |
| 2015 | } |
| 2016 | |
| 2017 | static int __init init_f2fs_fs(void) |
| 2018 | { |
| 2019 | int err; |
| 2020 | |
Jaegeuk Kim | c050865 | 2015-01-07 14:07:36 -0800 | [diff] [blame] | 2021 | f2fs_build_trace_ios(); |
| 2022 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2023 | err = init_inodecache(); |
| 2024 | if (err) |
| 2025 | goto fail; |
| 2026 | err = create_node_manager_caches(); |
| 2027 | if (err) |
Zhao Hongjiang | 9890ff3 | 2013-08-20 16:49:51 +0800 | [diff] [blame] | 2028 | goto free_inodecache; |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 2029 | err = create_segment_manager_caches(); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2030 | if (err) |
Zhao Hongjiang | 9890ff3 | 2013-08-20 16:49:51 +0800 | [diff] [blame] | 2031 | goto free_node_manager_caches; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2032 | err = create_checkpoint_caches(); |
| 2033 | if (err) |
Chao Yu | 0629207 | 2014-12-29 15:56:18 +0800 | [diff] [blame] | 2034 | goto free_segment_manager_caches; |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 2035 | err = create_extent_cache(); |
| 2036 | if (err) |
| 2037 | goto free_checkpoint_caches; |
Chao Yu | 17a3fb5 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 2038 | err = f2fs_register_sysfs(); |
| 2039 | if (err) |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 2040 | goto free_extent_cache; |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 2041 | err = register_shrinker(&f2fs_shrinker_info); |
Jaegeuk Kim | cfc4d97 | 2015-05-15 15:37:24 -0700 | [diff] [blame] | 2042 | if (err) |
Chao Yu | 17a3fb5 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 2043 | goto free_sysfs; |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 2044 | err = register_filesystem(&f2fs_fs_type); |
| 2045 | if (err) |
| 2046 | goto free_shrinker; |
Chao Yu | 787c7b8c | 2015-10-29 09:13:04 +0800 | [diff] [blame] | 2047 | err = f2fs_create_root_stats(); |
| 2048 | if (err) |
| 2049 | goto free_filesystem; |
Zhao Hongjiang | 9890ff3 | 2013-08-20 16:49:51 +0800 | [diff] [blame] | 2050 | return 0; |
| 2051 | |
Chao Yu | 787c7b8c | 2015-10-29 09:13:04 +0800 | [diff] [blame] | 2052 | free_filesystem: |
| 2053 | unregister_filesystem(&f2fs_fs_type); |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 2054 | free_shrinker: |
| 2055 | unregister_shrinker(&f2fs_shrinker_info); |
Chao Yu | 17a3fb5 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 2056 | free_sysfs: |
| 2057 | f2fs_unregister_sysfs(); |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 2058 | free_extent_cache: |
| 2059 | destroy_extent_cache(); |
Zhao Hongjiang | 9890ff3 | 2013-08-20 16:49:51 +0800 | [diff] [blame] | 2060 | free_checkpoint_caches: |
| 2061 | destroy_checkpoint_caches(); |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 2062 | free_segment_manager_caches: |
| 2063 | destroy_segment_manager_caches(); |
Zhao Hongjiang | 9890ff3 | 2013-08-20 16:49:51 +0800 | [diff] [blame] | 2064 | free_node_manager_caches: |
| 2065 | destroy_node_manager_caches(); |
| 2066 | free_inodecache: |
| 2067 | destroy_inodecache(); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2068 | fail: |
| 2069 | return err; |
| 2070 | } |
| 2071 | |
| 2072 | static void __exit exit_f2fs_fs(void) |
| 2073 | { |
Namjae Jeon | 4589d25 | 2013-01-15 19:58:47 +0900 | [diff] [blame] | 2074 | f2fs_destroy_root_stats(); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2075 | unregister_filesystem(&f2fs_fs_type); |
Tiezhu Yang | b8bef79 | 2016-05-18 08:02:25 +0800 | [diff] [blame] | 2076 | unregister_shrinker(&f2fs_shrinker_info); |
Chao Yu | 17a3fb5 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 2077 | f2fs_unregister_sysfs(); |
Wanpeng Li | fdf6c8b | 2015-03-06 15:00:54 +0800 | [diff] [blame] | 2078 | destroy_extent_cache(); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2079 | destroy_checkpoint_caches(); |
Changman Lee | 5dcd8a7 | 2013-12-11 14:32:13 +0900 | [diff] [blame] | 2080 | destroy_segment_manager_caches(); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2081 | destroy_node_manager_caches(); |
| 2082 | destroy_inodecache(); |
Jaegeuk Kim | 351f4fb | 2015-01-07 14:09:48 -0800 | [diff] [blame] | 2083 | f2fs_destroy_trace_ios(); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | module_init(init_f2fs_fs) |
| 2087 | module_exit(exit_f2fs_fs) |
| 2088 | |
| 2089 | MODULE_AUTHOR("Samsung Electronics's Praesto Team"); |
| 2090 | MODULE_DESCRIPTION("Flash Friendly File System"); |
| 2091 | MODULE_LICENSE("GPL"); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2092 | |