Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. |
| 4 | */ |
| 5 | |
| 6 | #include <linux/fs_context.h> |
| 7 | #include <linux/fs_parser.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/time.h> |
| 11 | #include <linux/mount.h> |
| 12 | #include <linux/cred.h> |
| 13 | #include <linux/statfs.h> |
| 14 | #include <linux/seq_file.h> |
| 15 | #include <linux/blkdev.h> |
| 16 | #include <linux/fs_struct.h> |
| 17 | #include <linux/iversion.h> |
| 18 | #include <linux/nls.h> |
| 19 | #include <linux/buffer_head.h> |
| 20 | |
| 21 | #include "exfat_raw.h" |
| 22 | #include "exfat_fs.h" |
| 23 | |
| 24 | static char exfat_default_iocharset[] = CONFIG_EXFAT_DEFAULT_IOCHARSET; |
| 25 | static struct kmem_cache *exfat_inode_cachep; |
| 26 | |
| 27 | static void exfat_free_iocharset(struct exfat_sb_info *sbi) |
| 28 | { |
| 29 | if (sbi->options.iocharset != exfat_default_iocharset) |
| 30 | kfree(sbi->options.iocharset); |
| 31 | } |
| 32 | |
| 33 | static void exfat_delayed_free(struct rcu_head *p) |
| 34 | { |
| 35 | struct exfat_sb_info *sbi = container_of(p, struct exfat_sb_info, rcu); |
| 36 | |
| 37 | unload_nls(sbi->nls_io); |
| 38 | exfat_free_iocharset(sbi); |
| 39 | exfat_free_upcase_table(sbi); |
| 40 | kfree(sbi); |
| 41 | } |
| 42 | |
| 43 | static void exfat_put_super(struct super_block *sb) |
| 44 | { |
| 45 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 46 | |
| 47 | mutex_lock(&sbi->s_lock); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 48 | exfat_free_bitmap(sbi); |
Tetsuhiro Kohada | 181a9e8 | 2020-05-29 19:14:56 +0900 | [diff] [blame] | 49 | brelse(sbi->boot_bh); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 50 | mutex_unlock(&sbi->s_lock); |
| 51 | |
| 52 | call_rcu(&sbi->rcu, exfat_delayed_free); |
| 53 | } |
| 54 | |
| 55 | static int exfat_sync_fs(struct super_block *sb, int wait) |
| 56 | { |
| 57 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 58 | int err = 0; |
| 59 | |
Tetsuhiro Kohada | 2c7f893 | 2020-06-16 11:18:07 +0900 | [diff] [blame] | 60 | if (!wait) |
| 61 | return 0; |
| 62 | |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 63 | /* If there are some dirty buffers in the bdev inode */ |
| 64 | mutex_lock(&sbi->s_lock); |
Tetsuhiro Kohada | 2c7f893 | 2020-06-16 11:18:07 +0900 | [diff] [blame] | 65 | sync_blockdev(sb->s_bdev); |
Tetsuhiro Kohada | 7018ec6 | 2020-07-31 14:58:26 +0900 | [diff] [blame] | 66 | if (exfat_clear_volume_dirty(sb)) |
Tetsuhiro Kohada | 2c7f893 | 2020-06-16 11:18:07 +0900 | [diff] [blame] | 67 | err = -EIO; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 68 | mutex_unlock(&sbi->s_lock); |
| 69 | return err; |
| 70 | } |
| 71 | |
| 72 | static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 73 | { |
| 74 | struct super_block *sb = dentry->d_sb; |
| 75 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 76 | unsigned long long id = huge_encode_dev(sb->s_bdev->bd_dev); |
| 77 | |
| 78 | if (sbi->used_clusters == EXFAT_CLUSTERS_UNTRACKED) { |
| 79 | mutex_lock(&sbi->s_lock); |
| 80 | if (exfat_count_used_clusters(sb, &sbi->used_clusters)) { |
| 81 | mutex_unlock(&sbi->s_lock); |
| 82 | return -EIO; |
| 83 | } |
| 84 | mutex_unlock(&sbi->s_lock); |
| 85 | } |
| 86 | |
| 87 | buf->f_type = sb->s_magic; |
| 88 | buf->f_bsize = sbi->cluster_size; |
| 89 | buf->f_blocks = sbi->num_clusters - 2; /* clu 0 & 1 */ |
| 90 | buf->f_bfree = buf->f_blocks - sbi->used_clusters; |
| 91 | buf->f_bavail = buf->f_bfree; |
Al Viro | 6d1349c | 2020-09-18 16:45:50 -0400 | [diff] [blame] | 92 | buf->f_fsid = u64_to_fsid(id); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 93 | /* Unicode utf16 255 characters */ |
| 94 | buf->f_namelen = EXFAT_MAX_FILE_LEN * NLS_MAX_CHARSET_SIZE; |
| 95 | return 0; |
| 96 | } |
| 97 | |
Tetsuhiro Kohada | 7018ec6 | 2020-07-31 14:58:26 +0900 | [diff] [blame] | 98 | static int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flags) |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 99 | { |
| 100 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
Tetsuhiro Kohada | 181a9e8 | 2020-05-29 19:14:56 +0900 | [diff] [blame] | 101 | struct boot_sector *p_boot = (struct boot_sector *)sbi->boot_bh->b_data; |
Jason Yan | cdc0612 | 2020-04-16 13:34:26 +0900 | [diff] [blame] | 102 | bool sync; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 103 | |
Tetsuhiro Kohada | 7018ec6 | 2020-07-31 14:58:26 +0900 | [diff] [blame] | 104 | /* retain persistent-flags */ |
| 105 | new_flags |= sbi->vol_flags_persistent; |
| 106 | |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 107 | /* flags are not changed */ |
Tetsuhiro Kohada | 7018ec6 | 2020-07-31 14:58:26 +0900 | [diff] [blame] | 108 | if (sbi->vol_flags == new_flags) |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 109 | return 0; |
| 110 | |
Tetsuhiro Kohada | 7018ec6 | 2020-07-31 14:58:26 +0900 | [diff] [blame] | 111 | sbi->vol_flags = new_flags; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 112 | |
| 113 | /* skip updating volume dirty flag, |
| 114 | * if this volume has been mounted with read-only |
| 115 | */ |
| 116 | if (sb_rdonly(sb)) |
| 117 | return 0; |
| 118 | |
Tetsuhiro Kohada | 7018ec6 | 2020-07-31 14:58:26 +0900 | [diff] [blame] | 119 | p_boot->vol_flags = cpu_to_le16(new_flags); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 120 | |
Tetsuhiro Kohada | 7018ec6 | 2020-07-31 14:58:26 +0900 | [diff] [blame] | 121 | if ((new_flags & VOLUME_DIRTY) && !buffer_dirty(sbi->boot_bh)) |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 122 | sync = true; |
| 123 | else |
| 124 | sync = false; |
| 125 | |
Tetsuhiro Kohada | 181a9e8 | 2020-05-29 19:14:56 +0900 | [diff] [blame] | 126 | set_buffer_uptodate(sbi->boot_bh); |
| 127 | mark_buffer_dirty(sbi->boot_bh); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 128 | |
| 129 | if (sync) |
Tetsuhiro Kohada | 181a9e8 | 2020-05-29 19:14:56 +0900 | [diff] [blame] | 130 | sync_dirty_buffer(sbi->boot_bh); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
Tetsuhiro Kohada | 7018ec6 | 2020-07-31 14:58:26 +0900 | [diff] [blame] | 134 | int exfat_set_volume_dirty(struct super_block *sb) |
| 135 | { |
| 136 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 137 | |
| 138 | return exfat_set_vol_flags(sb, sbi->vol_flags | VOLUME_DIRTY); |
| 139 | } |
| 140 | |
| 141 | int exfat_clear_volume_dirty(struct super_block *sb) |
| 142 | { |
| 143 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 144 | |
| 145 | return exfat_set_vol_flags(sb, sbi->vol_flags & ~VOLUME_DIRTY); |
| 146 | } |
| 147 | |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 148 | static int exfat_show_options(struct seq_file *m, struct dentry *root) |
| 149 | { |
| 150 | struct super_block *sb = root->d_sb; |
| 151 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 152 | struct exfat_mount_options *opts = &sbi->options; |
| 153 | |
| 154 | /* Show partition info */ |
| 155 | if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID)) |
| 156 | seq_printf(m, ",uid=%u", |
| 157 | from_kuid_munged(&init_user_ns, opts->fs_uid)); |
| 158 | if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID)) |
| 159 | seq_printf(m, ",gid=%u", |
| 160 | from_kgid_munged(&init_user_ns, opts->fs_gid)); |
| 161 | seq_printf(m, ",fmask=%04o,dmask=%04o", opts->fs_fmask, opts->fs_dmask); |
| 162 | if (opts->allow_utime) |
| 163 | seq_printf(m, ",allow_utime=%04o", opts->allow_utime); |
| 164 | if (opts->utf8) |
| 165 | seq_puts(m, ",iocharset=utf8"); |
| 166 | else if (sbi->nls_io) |
| 167 | seq_printf(m, ",iocharset=%s", sbi->nls_io->charset); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 168 | if (opts->errors == EXFAT_ERRORS_CONT) |
| 169 | seq_puts(m, ",errors=continue"); |
| 170 | else if (opts->errors == EXFAT_ERRORS_PANIC) |
| 171 | seq_puts(m, ",errors=panic"); |
| 172 | else |
| 173 | seq_puts(m, ",errors=remount-ro"); |
| 174 | if (opts->discard) |
| 175 | seq_puts(m, ",discard"); |
| 176 | if (opts->time_offset) |
| 177 | seq_printf(m, ",time_offset=%d", opts->time_offset); |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static struct inode *exfat_alloc_inode(struct super_block *sb) |
| 182 | { |
| 183 | struct exfat_inode_info *ei; |
| 184 | |
| 185 | ei = kmem_cache_alloc(exfat_inode_cachep, GFP_NOFS); |
| 186 | if (!ei) |
| 187 | return NULL; |
| 188 | |
| 189 | init_rwsem(&ei->truncate_lock); |
| 190 | return &ei->vfs_inode; |
| 191 | } |
| 192 | |
| 193 | static void exfat_free_inode(struct inode *inode) |
| 194 | { |
| 195 | kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode)); |
| 196 | } |
| 197 | |
| 198 | static const struct super_operations exfat_sops = { |
| 199 | .alloc_inode = exfat_alloc_inode, |
| 200 | .free_inode = exfat_free_inode, |
| 201 | .write_inode = exfat_write_inode, |
| 202 | .evict_inode = exfat_evict_inode, |
| 203 | .put_super = exfat_put_super, |
| 204 | .sync_fs = exfat_sync_fs, |
| 205 | .statfs = exfat_statfs, |
| 206 | .show_options = exfat_show_options, |
| 207 | }; |
| 208 | |
| 209 | enum { |
| 210 | Opt_uid, |
| 211 | Opt_gid, |
| 212 | Opt_umask, |
| 213 | Opt_dmask, |
| 214 | Opt_fmask, |
| 215 | Opt_allow_utime, |
| 216 | Opt_charset, |
| 217 | Opt_errors, |
| 218 | Opt_discard, |
| 219 | Opt_time_offset, |
Namjae Jeon | 907fa89 | 2020-05-22 08:10:10 +0900 | [diff] [blame] | 220 | |
| 221 | /* Deprecated options */ |
| 222 | Opt_utf8, |
| 223 | Opt_debug, |
| 224 | Opt_namecase, |
| 225 | Opt_codepage, |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 226 | }; |
| 227 | |
Valdis Kletnieks | 9acd0d5 | 2020-03-02 15:21:45 +0900 | [diff] [blame] | 228 | static const struct constant_table exfat_param_enums[] = { |
| 229 | { "continue", EXFAT_ERRORS_CONT }, |
| 230 | { "panic", EXFAT_ERRORS_PANIC }, |
| 231 | { "remount-ro", EXFAT_ERRORS_RO }, |
| 232 | {} |
| 233 | }; |
| 234 | |
| 235 | static const struct fs_parameter_spec exfat_parameters[] = { |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 236 | fsparam_u32("uid", Opt_uid), |
| 237 | fsparam_u32("gid", Opt_gid), |
| 238 | fsparam_u32oct("umask", Opt_umask), |
| 239 | fsparam_u32oct("dmask", Opt_dmask), |
| 240 | fsparam_u32oct("fmask", Opt_fmask), |
| 241 | fsparam_u32oct("allow_utime", Opt_allow_utime), |
| 242 | fsparam_string("iocharset", Opt_charset), |
Valdis Kletnieks | 9acd0d5 | 2020-03-02 15:21:45 +0900 | [diff] [blame] | 243 | fsparam_enum("errors", Opt_errors, exfat_param_enums), |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 244 | fsparam_flag("discard", Opt_discard), |
| 245 | fsparam_s32("time_offset", Opt_time_offset), |
Namjae Jeon | 907fa89 | 2020-05-22 08:10:10 +0900 | [diff] [blame] | 246 | __fsparam(NULL, "utf8", Opt_utf8, fs_param_deprecated, |
| 247 | NULL), |
| 248 | __fsparam(NULL, "debug", Opt_debug, fs_param_deprecated, |
| 249 | NULL), |
| 250 | __fsparam(fs_param_is_u32, "namecase", Opt_namecase, |
| 251 | fs_param_deprecated, NULL), |
| 252 | __fsparam(fs_param_is_u32, "codepage", Opt_codepage, |
| 253 | fs_param_deprecated, NULL), |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 254 | {} |
| 255 | }; |
| 256 | |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 257 | static int exfat_parse_param(struct fs_context *fc, struct fs_parameter *param) |
| 258 | { |
| 259 | struct exfat_sb_info *sbi = fc->s_fs_info; |
| 260 | struct exfat_mount_options *opts = &sbi->options; |
| 261 | struct fs_parse_result result; |
| 262 | int opt; |
| 263 | |
Valdis Kletnieks | 9acd0d5 | 2020-03-02 15:21:45 +0900 | [diff] [blame] | 264 | opt = fs_parse(fc, exfat_parameters, param, &result); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 265 | if (opt < 0) |
| 266 | return opt; |
| 267 | |
| 268 | switch (opt) { |
| 269 | case Opt_uid: |
| 270 | opts->fs_uid = make_kuid(current_user_ns(), result.uint_32); |
| 271 | break; |
| 272 | case Opt_gid: |
| 273 | opts->fs_gid = make_kgid(current_user_ns(), result.uint_32); |
| 274 | break; |
| 275 | case Opt_umask: |
| 276 | opts->fs_fmask = result.uint_32; |
| 277 | opts->fs_dmask = result.uint_32; |
| 278 | break; |
| 279 | case Opt_dmask: |
| 280 | opts->fs_dmask = result.uint_32; |
| 281 | break; |
| 282 | case Opt_fmask: |
| 283 | opts->fs_fmask = result.uint_32; |
| 284 | break; |
| 285 | case Opt_allow_utime: |
| 286 | opts->allow_utime = result.uint_32 & 0022; |
| 287 | break; |
| 288 | case Opt_charset: |
| 289 | exfat_free_iocharset(sbi); |
Al Viro | f341a7d | 2020-06-03 09:48:36 +0900 | [diff] [blame] | 290 | opts->iocharset = param->string; |
| 291 | param->string = NULL; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 292 | break; |
| 293 | case Opt_errors: |
| 294 | opts->errors = result.uint_32; |
| 295 | break; |
| 296 | case Opt_discard: |
| 297 | opts->discard = 1; |
| 298 | break; |
| 299 | case Opt_time_offset: |
| 300 | /* |
| 301 | * Make the limit 24 just in case someone invents something |
| 302 | * unusual. |
| 303 | */ |
| 304 | if (result.int_32 < -24 * 60 || result.int_32 > 24 * 60) |
| 305 | return -EINVAL; |
| 306 | opts->time_offset = result.int_32; |
| 307 | break; |
Namjae Jeon | 907fa89 | 2020-05-22 08:10:10 +0900 | [diff] [blame] | 308 | case Opt_utf8: |
| 309 | case Opt_debug: |
| 310 | case Opt_namecase: |
| 311 | case Opt_codepage: |
| 312 | break; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 313 | default: |
| 314 | return -EINVAL; |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static void exfat_hash_init(struct super_block *sb) |
| 321 | { |
| 322 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 323 | int i; |
| 324 | |
| 325 | spin_lock_init(&sbi->inode_hash_lock); |
| 326 | for (i = 0; i < EXFAT_HASH_SIZE; i++) |
| 327 | INIT_HLIST_HEAD(&sbi->inode_hashtable[i]); |
| 328 | } |
| 329 | |
| 330 | static int exfat_read_root(struct inode *inode) |
| 331 | { |
| 332 | struct super_block *sb = inode->i_sb; |
| 333 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 334 | struct exfat_inode_info *ei = EXFAT_I(inode); |
| 335 | struct exfat_chain cdir; |
| 336 | int num_subdirs, num_clu = 0; |
| 337 | |
| 338 | exfat_chain_set(&ei->dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN); |
| 339 | ei->entry = -1; |
| 340 | ei->start_clu = sbi->root_dir; |
| 341 | ei->flags = ALLOC_FAT_CHAIN; |
| 342 | ei->type = TYPE_DIR; |
| 343 | ei->version = 0; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 344 | ei->hint_bmap.off = EXFAT_EOF_CLUSTER; |
| 345 | ei->hint_stat.eidx = 0; |
| 346 | ei->hint_stat.clu = sbi->root_dir; |
| 347 | ei->hint_femp.eidx = EXFAT_HINT_NONE; |
| 348 | |
| 349 | exfat_chain_set(&cdir, sbi->root_dir, 0, ALLOC_FAT_CHAIN); |
| 350 | if (exfat_count_num_clusters(sb, &cdir, &num_clu)) |
| 351 | return -EIO; |
| 352 | i_size_write(inode, num_clu << sbi->cluster_size_bits); |
| 353 | |
| 354 | num_subdirs = exfat_count_dir_entries(sb, &cdir); |
| 355 | if (num_subdirs < 0) |
| 356 | return -EIO; |
| 357 | set_nlink(inode, num_subdirs + EXFAT_MIN_SUBDIR); |
| 358 | |
| 359 | inode->i_uid = sbi->options.fs_uid; |
| 360 | inode->i_gid = sbi->options.fs_gid; |
| 361 | inode_inc_iversion(inode); |
| 362 | inode->i_generation = 0; |
| 363 | inode->i_mode = exfat_make_mode(sbi, ATTR_SUBDIR, 0777); |
| 364 | inode->i_op = &exfat_dir_inode_operations; |
| 365 | inode->i_fop = &exfat_dir_operations; |
| 366 | |
| 367 | inode->i_blocks = ((i_size_read(inode) + (sbi->cluster_size - 1)) |
| 368 | & ~(sbi->cluster_size - 1)) >> inode->i_blkbits; |
Christophe Vu-Brugier | 7dee6f57 | 2021-11-02 22:23:58 +0100 | [diff] [blame^] | 369 | ei->i_pos = ((loff_t)sbi->root_dir << 32) | 0xffffffff; |
| 370 | ei->i_size_aligned = i_size_read(inode); |
| 371 | ei->i_size_ondisk = i_size_read(inode); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 372 | |
| 373 | exfat_save_attr(inode, ATTR_SUBDIR); |
| 374 | inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = |
| 375 | current_time(inode); |
Eric Sandeen | 81df1ad | 2020-04-21 11:13:10 +0900 | [diff] [blame] | 376 | exfat_truncate_atime(&inode->i_atime); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 377 | return 0; |
| 378 | } |
| 379 | |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 380 | static int exfat_calibrate_blocksize(struct super_block *sb, int logical_sect) |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 381 | { |
Tetsuhiro Kohada | b051683 | 2020-04-21 10:58:58 +0900 | [diff] [blame] | 382 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 383 | |
Namjae Jeon | 78c276f | 2021-02-01 09:23:37 +0900 | [diff] [blame] | 384 | if (!is_power_of_2(logical_sect)) { |
Joe Perches | d1727d5 | 2020-04-24 13:31:12 +0900 | [diff] [blame] | 385 | exfat_err(sb, "bogus logical sector size %u", logical_sect); |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 386 | return -EIO; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | if (logical_sect < sb->s_blocksize) { |
Joe Perches | d1727d5 | 2020-04-24 13:31:12 +0900 | [diff] [blame] | 390 | exfat_err(sb, "logical sector size too small for device (logical sector size = %u)", |
| 391 | logical_sect); |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 392 | return -EIO; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | if (logical_sect > sb->s_blocksize) { |
Tetsuhiro Kohada | 181a9e8 | 2020-05-29 19:14:56 +0900 | [diff] [blame] | 396 | brelse(sbi->boot_bh); |
| 397 | sbi->boot_bh = NULL; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 398 | |
| 399 | if (!sb_set_blocksize(sb, logical_sect)) { |
Joe Perches | d1727d5 | 2020-04-24 13:31:12 +0900 | [diff] [blame] | 400 | exfat_err(sb, "unable to set blocksize %u", |
| 401 | logical_sect); |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 402 | return -EIO; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 403 | } |
Tetsuhiro Kohada | 181a9e8 | 2020-05-29 19:14:56 +0900 | [diff] [blame] | 404 | sbi->boot_bh = sb_bread(sb, 0); |
| 405 | if (!sbi->boot_bh) { |
Joe Perches | d1727d5 | 2020-04-24 13:31:12 +0900 | [diff] [blame] | 406 | exfat_err(sb, "unable to read boot sector (logical sector size = %lu)", |
| 407 | sb->s_blocksize); |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 408 | return -EIO; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 409 | } |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 410 | } |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 411 | return 0; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 412 | } |
| 413 | |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 414 | static int exfat_read_boot_sector(struct super_block *sb) |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 415 | { |
Tetsuhiro Kohada | 181a9e8 | 2020-05-29 19:14:56 +0900 | [diff] [blame] | 416 | struct boot_sector *p_boot; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 417 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 418 | |
| 419 | /* set block size to read super block */ |
| 420 | sb_min_blocksize(sb, 512); |
| 421 | |
| 422 | /* read boot sector */ |
Tetsuhiro Kohada | 181a9e8 | 2020-05-29 19:14:56 +0900 | [diff] [blame] | 423 | sbi->boot_bh = sb_bread(sb, 0); |
| 424 | if (!sbi->boot_bh) { |
Joe Perches | d1727d5 | 2020-04-24 13:31:12 +0900 | [diff] [blame] | 425 | exfat_err(sb, "unable to read boot sector"); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 426 | return -EIO; |
| 427 | } |
Tetsuhiro Kohada | 181a9e8 | 2020-05-29 19:14:56 +0900 | [diff] [blame] | 428 | p_boot = (struct boot_sector *)sbi->boot_bh->b_data; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 429 | |
Tetsuhiro Kohada | 181a9e8 | 2020-05-29 19:14:56 +0900 | [diff] [blame] | 430 | /* check the validity of BOOT */ |
| 431 | if (le16_to_cpu((p_boot->signature)) != BOOT_SIGNATURE) { |
Joe Perches | d1727d5 | 2020-04-24 13:31:12 +0900 | [diff] [blame] | 432 | exfat_err(sb, "invalid boot record signature"); |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 433 | return -EINVAL; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 434 | } |
| 435 | |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 436 | if (memcmp(p_boot->fs_name, STR_EXFAT, BOOTSEC_FS_NAME_LEN)) { |
| 437 | exfat_err(sb, "invalid fs_name"); /* fs_name may unprintable */ |
| 438 | return -EINVAL; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /* |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 442 | * must_be_zero field must be filled with zero to prevent mounting |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 443 | * from FAT volume. |
| 444 | */ |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 445 | if (memchr_inv(p_boot->must_be_zero, 0, sizeof(p_boot->must_be_zero))) |
| 446 | return -EINVAL; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 447 | |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 448 | if (p_boot->num_fats != 1 && p_boot->num_fats != 2) { |
Joe Perches | d1727d5 | 2020-04-24 13:31:12 +0900 | [diff] [blame] | 449 | exfat_err(sb, "bogus number of FAT structure"); |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 450 | return -EINVAL; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 451 | } |
| 452 | |
Namjae Jeon | 78c276f | 2021-02-01 09:23:37 +0900 | [diff] [blame] | 453 | /* |
| 454 | * sect_size_bits could be at least 9 and at most 12. |
| 455 | */ |
| 456 | if (p_boot->sect_size_bits < EXFAT_MIN_SECT_SIZE_BITS || |
| 457 | p_boot->sect_size_bits > EXFAT_MAX_SECT_SIZE_BITS) { |
| 458 | exfat_err(sb, "bogus sector size bits : %u\n", |
| 459 | p_boot->sect_size_bits); |
| 460 | return -EINVAL; |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * sect_per_clus_bits could be at least 0 and at most 25 - sect_size_bits. |
| 465 | */ |
| 466 | if (p_boot->sect_per_clus_bits > EXFAT_MAX_SECT_PER_CLUS_BITS(p_boot)) { |
| 467 | exfat_err(sb, "bogus sectors bits per cluster : %u\n", |
| 468 | p_boot->sect_per_clus_bits); |
| 469 | return -EINVAL; |
| 470 | } |
| 471 | |
Tetsuhiro Kohada | 181a9e8 | 2020-05-29 19:14:56 +0900 | [diff] [blame] | 472 | sbi->sect_per_clus = 1 << p_boot->sect_per_clus_bits; |
| 473 | sbi->sect_per_clus_bits = p_boot->sect_per_clus_bits; |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 474 | sbi->cluster_size_bits = p_boot->sect_per_clus_bits + |
| 475 | p_boot->sect_size_bits; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 476 | sbi->cluster_size = 1 << sbi->cluster_size_bits; |
Tetsuhiro Kohada | 181a9e8 | 2020-05-29 19:14:56 +0900 | [diff] [blame] | 477 | sbi->num_FAT_sectors = le32_to_cpu(p_boot->fat_length); |
| 478 | sbi->FAT1_start_sector = le32_to_cpu(p_boot->fat_offset); |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 479 | sbi->FAT2_start_sector = le32_to_cpu(p_boot->fat_offset); |
| 480 | if (p_boot->num_fats == 2) |
| 481 | sbi->FAT2_start_sector += sbi->num_FAT_sectors; |
Tetsuhiro Kohada | 181a9e8 | 2020-05-29 19:14:56 +0900 | [diff] [blame] | 482 | sbi->data_start_sector = le32_to_cpu(p_boot->clu_offset); |
| 483 | sbi->num_sectors = le64_to_cpu(p_boot->vol_length); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 484 | /* because the cluster index starts with 2 */ |
Tetsuhiro Kohada | 181a9e8 | 2020-05-29 19:14:56 +0900 | [diff] [blame] | 485 | sbi->num_clusters = le32_to_cpu(p_boot->clu_count) + |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 486 | EXFAT_RESERVED_CLUSTERS; |
| 487 | |
Tetsuhiro Kohada | 181a9e8 | 2020-05-29 19:14:56 +0900 | [diff] [blame] | 488 | sbi->root_dir = le32_to_cpu(p_boot->root_cluster); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 489 | sbi->dentries_per_clu = 1 << |
| 490 | (sbi->cluster_size_bits - DENTRY_SIZE_BITS); |
| 491 | |
Tetsuhiro Kohada | 7018ec6 | 2020-07-31 14:58:26 +0900 | [diff] [blame] | 492 | sbi->vol_flags = le16_to_cpu(p_boot->vol_flags); |
| 493 | sbi->vol_flags_persistent = sbi->vol_flags & (VOLUME_DIRTY | MEDIA_FAILURE); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 494 | sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER; |
| 495 | sbi->used_clusters = EXFAT_CLUSTERS_UNTRACKED; |
| 496 | |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 497 | /* check consistencies */ |
Namjae Jeon | 78c276f | 2021-02-01 09:23:37 +0900 | [diff] [blame] | 498 | if ((u64)sbi->num_FAT_sectors << p_boot->sect_size_bits < |
| 499 | (u64)sbi->num_clusters * 4) { |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 500 | exfat_err(sb, "bogus fat length"); |
| 501 | return -EINVAL; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 502 | } |
Namjae Jeon | 78c276f | 2021-02-01 09:23:37 +0900 | [diff] [blame] | 503 | |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 504 | if (sbi->data_start_sector < |
Namjae Jeon | 78c276f | 2021-02-01 09:23:37 +0900 | [diff] [blame] | 505 | (u64)sbi->FAT1_start_sector + |
| 506 | (u64)sbi->num_FAT_sectors * p_boot->num_fats) { |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 507 | exfat_err(sb, "bogus data start sector"); |
| 508 | return -EINVAL; |
| 509 | } |
Namjae Jeon | 78c276f | 2021-02-01 09:23:37 +0900 | [diff] [blame] | 510 | |
Tetsuhiro Kohada | 7018ec6 | 2020-07-31 14:58:26 +0900 | [diff] [blame] | 511 | if (sbi->vol_flags & VOLUME_DIRTY) |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 512 | exfat_warn(sb, "Volume was not properly unmounted. Some data may be corrupt. Please run fsck."); |
Tetsuhiro Kohada | 7018ec6 | 2020-07-31 14:58:26 +0900 | [diff] [blame] | 513 | if (sbi->vol_flags & MEDIA_FAILURE) |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 514 | exfat_warn(sb, "Medium has reported failures. Some data may be lost."); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 515 | |
| 516 | /* exFAT file size is limited by a disk volume size */ |
| 517 | sb->s_maxbytes = (u64)(sbi->num_clusters - EXFAT_RESERVED_CLUSTERS) << |
| 518 | sbi->cluster_size_bits; |
| 519 | |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 520 | /* check logical sector size */ |
| 521 | if (exfat_calibrate_blocksize(sb, 1 << p_boot->sect_size_bits)) |
| 522 | return -EIO; |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
Tetsuhiro Kohada | 476189c | 2020-05-31 18:30:17 +0900 | [diff] [blame] | 527 | static int exfat_verify_boot_region(struct super_block *sb) |
| 528 | { |
| 529 | struct buffer_head *bh = NULL; |
| 530 | u32 chksum = 0; |
| 531 | __le32 *p_sig, *p_chksum; |
| 532 | int sn, i; |
| 533 | |
| 534 | /* read boot sector sub-regions */ |
| 535 | for (sn = 0; sn < 11; sn++) { |
| 536 | bh = sb_bread(sb, sn); |
| 537 | if (!bh) |
| 538 | return -EIO; |
| 539 | |
| 540 | if (sn != 0 && sn <= 8) { |
| 541 | /* extended boot sector sub-regions */ |
| 542 | p_sig = (__le32 *)&bh->b_data[sb->s_blocksize - 4]; |
| 543 | if (le32_to_cpu(*p_sig) != EXBOOT_SIGNATURE) |
| 544 | exfat_warn(sb, "Invalid exboot-signature(sector = %d): 0x%08x", |
| 545 | sn, le32_to_cpu(*p_sig)); |
| 546 | } |
| 547 | |
| 548 | chksum = exfat_calc_chksum32(bh->b_data, sb->s_blocksize, |
| 549 | chksum, sn ? CS_DEFAULT : CS_BOOT_SECTOR); |
| 550 | brelse(bh); |
| 551 | } |
| 552 | |
| 553 | /* boot checksum sub-regions */ |
| 554 | bh = sb_bread(sb, sn); |
| 555 | if (!bh) |
| 556 | return -EIO; |
| 557 | |
| 558 | for (i = 0; i < sb->s_blocksize; i += sizeof(u32)) { |
| 559 | p_chksum = (__le32 *)&bh->b_data[i]; |
| 560 | if (le32_to_cpu(*p_chksum) != chksum) { |
| 561 | exfat_err(sb, "Invalid boot checksum (boot checksum : 0x%08x, checksum : 0x%08x)", |
| 562 | le32_to_cpu(*p_chksum), chksum); |
| 563 | brelse(bh); |
| 564 | return -EINVAL; |
| 565 | } |
| 566 | } |
| 567 | brelse(bh); |
| 568 | return 0; |
| 569 | } |
| 570 | |
Tetsuhiro Kohada | 33404a1 | 2020-05-29 19:14:57 +0900 | [diff] [blame] | 571 | /* mount the file system volume */ |
| 572 | static int __exfat_fill_super(struct super_block *sb) |
| 573 | { |
| 574 | int ret; |
| 575 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 576 | |
| 577 | ret = exfat_read_boot_sector(sb); |
| 578 | if (ret) { |
| 579 | exfat_err(sb, "failed to read boot sector"); |
| 580 | goto free_bh; |
| 581 | } |
| 582 | |
Tetsuhiro Kohada | 476189c | 2020-05-31 18:30:17 +0900 | [diff] [blame] | 583 | ret = exfat_verify_boot_region(sb); |
| 584 | if (ret) { |
| 585 | exfat_err(sb, "invalid boot region"); |
| 586 | goto free_bh; |
| 587 | } |
| 588 | |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 589 | ret = exfat_create_upcase_table(sb); |
| 590 | if (ret) { |
Joe Perches | d1727d5 | 2020-04-24 13:31:12 +0900 | [diff] [blame] | 591 | exfat_err(sb, "failed to load upcase table"); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 592 | goto free_bh; |
| 593 | } |
| 594 | |
| 595 | ret = exfat_load_bitmap(sb); |
| 596 | if (ret) { |
Joe Perches | d1727d5 | 2020-04-24 13:31:12 +0900 | [diff] [blame] | 597 | exfat_err(sb, "failed to load alloc-bitmap"); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 598 | goto free_upcase_table; |
| 599 | } |
| 600 | |
| 601 | ret = exfat_count_used_clusters(sb, &sbi->used_clusters); |
| 602 | if (ret) { |
Joe Perches | d1727d5 | 2020-04-24 13:31:12 +0900 | [diff] [blame] | 603 | exfat_err(sb, "failed to scan clusters"); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 604 | goto free_alloc_bitmap; |
| 605 | } |
| 606 | |
| 607 | return 0; |
| 608 | |
| 609 | free_alloc_bitmap: |
| 610 | exfat_free_bitmap(sbi); |
| 611 | free_upcase_table: |
| 612 | exfat_free_upcase_table(sbi); |
| 613 | free_bh: |
Tetsuhiro Kohada | 181a9e8 | 2020-05-29 19:14:56 +0900 | [diff] [blame] | 614 | brelse(sbi->boot_bh); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 615 | return ret; |
| 616 | } |
| 617 | |
| 618 | static int exfat_fill_super(struct super_block *sb, struct fs_context *fc) |
| 619 | { |
| 620 | struct exfat_sb_info *sbi = sb->s_fs_info; |
| 621 | struct exfat_mount_options *opts = &sbi->options; |
| 622 | struct inode *root_inode; |
| 623 | int err; |
| 624 | |
| 625 | if (opts->allow_utime == (unsigned short)-1) |
| 626 | opts->allow_utime = ~opts->fs_dmask & 0022; |
| 627 | |
| 628 | if (opts->discard) { |
| 629 | struct request_queue *q = bdev_get_queue(sb->s_bdev); |
| 630 | |
Pali Rohár | b7e038a | 2020-03-17 22:46:52 +0100 | [diff] [blame] | 631 | if (!blk_queue_discard(q)) { |
Joe Perches | d1727d5 | 2020-04-24 13:31:12 +0900 | [diff] [blame] | 632 | exfat_warn(sb, "mounting with \"discard\" option, but the device does not support discard"); |
Pali Rohár | b7e038a | 2020-03-17 22:46:52 +0100 | [diff] [blame] | 633 | opts->discard = 0; |
| 634 | } |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | sb->s_flags |= SB_NODIRATIME; |
| 638 | sb->s_magic = EXFAT_SUPER_MAGIC; |
| 639 | sb->s_op = &exfat_sops; |
| 640 | |
Eric Sandeen | 674a998 | 2020-04-17 14:43:49 +0900 | [diff] [blame] | 641 | sb->s_time_gran = 10 * NSEC_PER_MSEC; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 642 | sb->s_time_min = EXFAT_MIN_TIMESTAMP_SECS; |
| 643 | sb->s_time_max = EXFAT_MAX_TIMESTAMP_SECS; |
| 644 | |
| 645 | err = __exfat_fill_super(sb); |
| 646 | if (err) { |
Joe Perches | d1727d5 | 2020-04-24 13:31:12 +0900 | [diff] [blame] | 647 | exfat_err(sb, "failed to recognize exfat type"); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 648 | goto check_nls_io; |
| 649 | } |
| 650 | |
| 651 | /* set up enough so that it can read an inode */ |
| 652 | exfat_hash_init(sb); |
| 653 | |
| 654 | if (!strcmp(sbi->options.iocharset, "utf8")) |
| 655 | opts->utf8 = 1; |
| 656 | else { |
| 657 | sbi->nls_io = load_nls(sbi->options.iocharset); |
| 658 | if (!sbi->nls_io) { |
Joe Perches | d1727d5 | 2020-04-24 13:31:12 +0900 | [diff] [blame] | 659 | exfat_err(sb, "IO charset %s not found", |
| 660 | sbi->options.iocharset); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 661 | err = -EINVAL; |
| 662 | goto free_table; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | if (sbi->options.utf8) |
| 667 | sb->s_d_op = &exfat_utf8_dentry_ops; |
| 668 | else |
| 669 | sb->s_d_op = &exfat_dentry_ops; |
| 670 | |
| 671 | root_inode = new_inode(sb); |
| 672 | if (!root_inode) { |
Joe Perches | d1727d5 | 2020-04-24 13:31:12 +0900 | [diff] [blame] | 673 | exfat_err(sb, "failed to allocate root inode"); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 674 | err = -ENOMEM; |
| 675 | goto free_table; |
| 676 | } |
| 677 | |
| 678 | root_inode->i_ino = EXFAT_ROOT_INO; |
| 679 | inode_set_iversion(root_inode, 1); |
| 680 | err = exfat_read_root(root_inode); |
| 681 | if (err) { |
Joe Perches | d1727d5 | 2020-04-24 13:31:12 +0900 | [diff] [blame] | 682 | exfat_err(sb, "failed to initialize root inode"); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 683 | goto put_inode; |
| 684 | } |
| 685 | |
| 686 | exfat_hash_inode(root_inode, EXFAT_I(root_inode)->i_pos); |
| 687 | insert_inode_hash(root_inode); |
| 688 | |
| 689 | sb->s_root = d_make_root(root_inode); |
| 690 | if (!sb->s_root) { |
Joe Perches | d1727d5 | 2020-04-24 13:31:12 +0900 | [diff] [blame] | 691 | exfat_err(sb, "failed to get the root dentry"); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 692 | err = -ENOMEM; |
Chen Li | 839a534 | 2021-06-09 11:48:55 +0800 | [diff] [blame] | 693 | goto free_table; |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | return 0; |
| 697 | |
| 698 | put_inode: |
| 699 | iput(root_inode); |
| 700 | sb->s_root = NULL; |
| 701 | |
| 702 | free_table: |
| 703 | exfat_free_upcase_table(sbi); |
| 704 | exfat_free_bitmap(sbi); |
Tetsuhiro Kohada | 181a9e8 | 2020-05-29 19:14:56 +0900 | [diff] [blame] | 705 | brelse(sbi->boot_bh); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 706 | |
| 707 | check_nls_io: |
| 708 | unload_nls(sbi->nls_io); |
| 709 | exfat_free_iocharset(sbi); |
| 710 | sb->s_fs_info = NULL; |
| 711 | kfree(sbi); |
| 712 | return err; |
| 713 | } |
| 714 | |
| 715 | static int exfat_get_tree(struct fs_context *fc) |
| 716 | { |
| 717 | return get_tree_bdev(fc, exfat_fill_super); |
| 718 | } |
| 719 | |
| 720 | static void exfat_free(struct fs_context *fc) |
| 721 | { |
Al Viro | f341a7d | 2020-06-03 09:48:36 +0900 | [diff] [blame] | 722 | struct exfat_sb_info *sbi = fc->s_fs_info; |
| 723 | |
| 724 | if (sbi) { |
| 725 | exfat_free_iocharset(sbi); |
| 726 | kfree(sbi); |
| 727 | } |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 728 | } |
| 729 | |
Hyunchul Lee | a0271a1 | 2020-06-16 14:34:45 +0900 | [diff] [blame] | 730 | static int exfat_reconfigure(struct fs_context *fc) |
| 731 | { |
| 732 | fc->sb_flags |= SB_NODIRATIME; |
| 733 | |
| 734 | /* volume flag will be updated in exfat_sync_fs */ |
| 735 | sync_filesystem(fc->root->d_sb); |
| 736 | return 0; |
| 737 | } |
| 738 | |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 739 | static const struct fs_context_operations exfat_context_ops = { |
| 740 | .parse_param = exfat_parse_param, |
| 741 | .get_tree = exfat_get_tree, |
| 742 | .free = exfat_free, |
Hyunchul Lee | a0271a1 | 2020-06-16 14:34:45 +0900 | [diff] [blame] | 743 | .reconfigure = exfat_reconfigure, |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 744 | }; |
| 745 | |
| 746 | static int exfat_init_fs_context(struct fs_context *fc) |
| 747 | { |
| 748 | struct exfat_sb_info *sbi; |
| 749 | |
| 750 | sbi = kzalloc(sizeof(struct exfat_sb_info), GFP_KERNEL); |
| 751 | if (!sbi) |
| 752 | return -ENOMEM; |
| 753 | |
| 754 | mutex_init(&sbi->s_lock); |
Hyeongseok Kim | 5c2d728 | 2021-03-02 14:05:20 +0900 | [diff] [blame] | 755 | mutex_init(&sbi->bitmap_lock); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 756 | ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL, |
| 757 | DEFAULT_RATELIMIT_BURST); |
| 758 | |
| 759 | sbi->options.fs_uid = current_uid(); |
| 760 | sbi->options.fs_gid = current_gid(); |
| 761 | sbi->options.fs_fmask = current->fs->umask; |
| 762 | sbi->options.fs_dmask = current->fs->umask; |
| 763 | sbi->options.allow_utime = -1; |
| 764 | sbi->options.iocharset = exfat_default_iocharset; |
| 765 | sbi->options.errors = EXFAT_ERRORS_RO; |
| 766 | |
| 767 | fc->s_fs_info = sbi; |
| 768 | fc->ops = &exfat_context_ops; |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | static struct file_system_type exfat_fs_type = { |
| 773 | .owner = THIS_MODULE, |
| 774 | .name = "exfat", |
| 775 | .init_fs_context = exfat_init_fs_context, |
Valdis Kletnieks | 9acd0d5 | 2020-03-02 15:21:45 +0900 | [diff] [blame] | 776 | .parameters = exfat_parameters, |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 777 | .kill_sb = kill_block_super, |
| 778 | .fs_flags = FS_REQUIRES_DEV, |
| 779 | }; |
| 780 | |
| 781 | static void exfat_inode_init_once(void *foo) |
| 782 | { |
| 783 | struct exfat_inode_info *ei = (struct exfat_inode_info *)foo; |
| 784 | |
Namjae Jeon | 8ff006e | 2020-09-29 09:09:49 +0900 | [diff] [blame] | 785 | spin_lock_init(&ei->cache_lru_lock); |
| 786 | ei->nr_caches = 0; |
| 787 | ei->cache_valid_id = EXFAT_CACHE_VALID + 1; |
| 788 | INIT_LIST_HEAD(&ei->cache_lru); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 789 | INIT_HLIST_NODE(&ei->i_hash_fat); |
| 790 | inode_init_once(&ei->vfs_inode); |
| 791 | } |
| 792 | |
| 793 | static int __init init_exfat_fs(void) |
| 794 | { |
| 795 | int err; |
| 796 | |
| 797 | err = exfat_cache_init(); |
| 798 | if (err) |
| 799 | return err; |
| 800 | |
| 801 | exfat_inode_cachep = kmem_cache_create("exfat_inode_cache", |
| 802 | sizeof(struct exfat_inode_info), |
| 803 | 0, SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 804 | exfat_inode_init_once); |
| 805 | if (!exfat_inode_cachep) { |
| 806 | err = -ENOMEM; |
| 807 | goto shutdown_cache; |
| 808 | } |
| 809 | |
| 810 | err = register_filesystem(&exfat_fs_type); |
| 811 | if (err) |
| 812 | goto destroy_cache; |
| 813 | |
| 814 | return 0; |
| 815 | |
| 816 | destroy_cache: |
| 817 | kmem_cache_destroy(exfat_inode_cachep); |
| 818 | shutdown_cache: |
| 819 | exfat_cache_shutdown(); |
| 820 | return err; |
| 821 | } |
| 822 | |
| 823 | static void __exit exit_exfat_fs(void) |
| 824 | { |
| 825 | /* |
| 826 | * Make sure all delayed rcu free inodes are flushed before we |
| 827 | * destroy cache. |
| 828 | */ |
| 829 | rcu_barrier(); |
| 830 | kmem_cache_destroy(exfat_inode_cachep); |
| 831 | unregister_filesystem(&exfat_fs_type); |
| 832 | exfat_cache_shutdown(); |
| 833 | } |
| 834 | |
| 835 | module_init(init_exfat_fs); |
| 836 | module_exit(exit_exfat_fs); |
| 837 | |
Thomas Backlund | cd76ac2 | 2020-04-04 23:29:43 +0300 | [diff] [blame] | 838 | MODULE_ALIAS_FS("exfat"); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 839 | MODULE_LICENSE("GPL"); |
| 840 | MODULE_DESCRIPTION("exFAT filesystem support"); |
| 841 | MODULE_AUTHOR("Samsung Electronics Co., Ltd."); |