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