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