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); |
| 48 | if (test_and_clear_bit(EXFAT_SB_DIRTY, &sbi->s_state)) |
| 49 | sync_blockdev(sb->s_bdev); |
| 50 | exfat_set_vol_flags(sb, VOL_CLEAN); |
| 51 | exfat_free_bitmap(sbi); |
| 52 | mutex_unlock(&sbi->s_lock); |
| 53 | |
| 54 | call_rcu(&sbi->rcu, exfat_delayed_free); |
| 55 | } |
| 56 | |
| 57 | static int exfat_sync_fs(struct super_block *sb, int wait) |
| 58 | { |
| 59 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 60 | int err = 0; |
| 61 | |
| 62 | /* If there are some dirty buffers in the bdev inode */ |
| 63 | mutex_lock(&sbi->s_lock); |
| 64 | if (test_and_clear_bit(EXFAT_SB_DIRTY, &sbi->s_state)) { |
| 65 | sync_blockdev(sb->s_bdev); |
| 66 | if (exfat_set_vol_flags(sb, VOL_CLEAN)) |
| 67 | err = -EIO; |
| 68 | } |
| 69 | mutex_unlock(&sbi->s_lock); |
| 70 | return err; |
| 71 | } |
| 72 | |
| 73 | static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 74 | { |
| 75 | struct super_block *sb = dentry->d_sb; |
| 76 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 77 | unsigned long long id = huge_encode_dev(sb->s_bdev->bd_dev); |
| 78 | |
| 79 | if (sbi->used_clusters == EXFAT_CLUSTERS_UNTRACKED) { |
| 80 | mutex_lock(&sbi->s_lock); |
| 81 | if (exfat_count_used_clusters(sb, &sbi->used_clusters)) { |
| 82 | mutex_unlock(&sbi->s_lock); |
| 83 | return -EIO; |
| 84 | } |
| 85 | mutex_unlock(&sbi->s_lock); |
| 86 | } |
| 87 | |
| 88 | buf->f_type = sb->s_magic; |
| 89 | buf->f_bsize = sbi->cluster_size; |
| 90 | buf->f_blocks = sbi->num_clusters - 2; /* clu 0 & 1 */ |
| 91 | buf->f_bfree = buf->f_blocks - sbi->used_clusters; |
| 92 | buf->f_bavail = buf->f_bfree; |
| 93 | buf->f_fsid.val[0] = (unsigned int)id; |
| 94 | buf->f_fsid.val[1] = (unsigned int)(id >> 32); |
| 95 | /* Unicode utf16 255 characters */ |
| 96 | buf->f_namelen = EXFAT_MAX_FILE_LEN * NLS_MAX_CHARSET_SIZE; |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flag) |
| 101 | { |
| 102 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 103 | struct pbr64 *bpb; |
| 104 | bool sync = 0; |
| 105 | |
| 106 | /* flags are not changed */ |
| 107 | if (sbi->vol_flag == new_flag) |
| 108 | return 0; |
| 109 | |
| 110 | sbi->vol_flag = new_flag; |
| 111 | |
| 112 | /* skip updating volume dirty flag, |
| 113 | * if this volume has been mounted with read-only |
| 114 | */ |
| 115 | if (sb_rdonly(sb)) |
| 116 | return 0; |
| 117 | |
| 118 | if (!sbi->pbr_bh) { |
| 119 | sbi->pbr_bh = sb_bread(sb, 0); |
| 120 | if (!sbi->pbr_bh) { |
| 121 | exfat_msg(sb, KERN_ERR, "failed to read boot sector"); |
| 122 | return -ENOMEM; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | bpb = (struct pbr64 *)sbi->pbr_bh->b_data; |
| 127 | bpb->bsx.vol_flags = cpu_to_le16(new_flag); |
| 128 | |
| 129 | if (new_flag == VOL_DIRTY && !buffer_dirty(sbi->pbr_bh)) |
| 130 | sync = true; |
| 131 | else |
| 132 | sync = false; |
| 133 | |
| 134 | set_buffer_uptodate(sbi->pbr_bh); |
| 135 | mark_buffer_dirty(sbi->pbr_bh); |
| 136 | |
| 137 | if (sync) |
| 138 | sync_dirty_buffer(sbi->pbr_bh); |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int exfat_show_options(struct seq_file *m, struct dentry *root) |
| 143 | { |
| 144 | struct super_block *sb = root->d_sb; |
| 145 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 146 | struct exfat_mount_options *opts = &sbi->options; |
| 147 | |
| 148 | /* Show partition info */ |
| 149 | if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID)) |
| 150 | seq_printf(m, ",uid=%u", |
| 151 | from_kuid_munged(&init_user_ns, opts->fs_uid)); |
| 152 | if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID)) |
| 153 | seq_printf(m, ",gid=%u", |
| 154 | from_kgid_munged(&init_user_ns, opts->fs_gid)); |
| 155 | seq_printf(m, ",fmask=%04o,dmask=%04o", opts->fs_fmask, opts->fs_dmask); |
| 156 | if (opts->allow_utime) |
| 157 | seq_printf(m, ",allow_utime=%04o", opts->allow_utime); |
| 158 | if (opts->utf8) |
| 159 | seq_puts(m, ",iocharset=utf8"); |
| 160 | else if (sbi->nls_io) |
| 161 | seq_printf(m, ",iocharset=%s", sbi->nls_io->charset); |
| 162 | seq_printf(m, ",bps=%ld", sb->s_blocksize); |
| 163 | if (opts->errors == EXFAT_ERRORS_CONT) |
| 164 | seq_puts(m, ",errors=continue"); |
| 165 | else if (opts->errors == EXFAT_ERRORS_PANIC) |
| 166 | seq_puts(m, ",errors=panic"); |
| 167 | else |
| 168 | seq_puts(m, ",errors=remount-ro"); |
| 169 | if (opts->discard) |
| 170 | seq_puts(m, ",discard"); |
| 171 | if (opts->time_offset) |
| 172 | seq_printf(m, ",time_offset=%d", opts->time_offset); |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static struct inode *exfat_alloc_inode(struct super_block *sb) |
| 177 | { |
| 178 | struct exfat_inode_info *ei; |
| 179 | |
| 180 | ei = kmem_cache_alloc(exfat_inode_cachep, GFP_NOFS); |
| 181 | if (!ei) |
| 182 | return NULL; |
| 183 | |
| 184 | init_rwsem(&ei->truncate_lock); |
| 185 | return &ei->vfs_inode; |
| 186 | } |
| 187 | |
| 188 | static void exfat_free_inode(struct inode *inode) |
| 189 | { |
| 190 | kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode)); |
| 191 | } |
| 192 | |
| 193 | static const struct super_operations exfat_sops = { |
| 194 | .alloc_inode = exfat_alloc_inode, |
| 195 | .free_inode = exfat_free_inode, |
| 196 | .write_inode = exfat_write_inode, |
| 197 | .evict_inode = exfat_evict_inode, |
| 198 | .put_super = exfat_put_super, |
| 199 | .sync_fs = exfat_sync_fs, |
| 200 | .statfs = exfat_statfs, |
| 201 | .show_options = exfat_show_options, |
| 202 | }; |
| 203 | |
| 204 | enum { |
| 205 | Opt_uid, |
| 206 | Opt_gid, |
| 207 | Opt_umask, |
| 208 | Opt_dmask, |
| 209 | Opt_fmask, |
| 210 | Opt_allow_utime, |
| 211 | Opt_charset, |
| 212 | Opt_errors, |
| 213 | Opt_discard, |
| 214 | Opt_time_offset, |
| 215 | }; |
| 216 | |
Valdis Kletnieks | 9acd0d5 | 2020-03-02 15:21:45 +0900 | [diff] [blame] | 217 | static const struct constant_table exfat_param_enums[] = { |
| 218 | { "continue", EXFAT_ERRORS_CONT }, |
| 219 | { "panic", EXFAT_ERRORS_PANIC }, |
| 220 | { "remount-ro", EXFAT_ERRORS_RO }, |
| 221 | {} |
| 222 | }; |
| 223 | |
| 224 | static const struct fs_parameter_spec exfat_parameters[] = { |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 225 | fsparam_u32("uid", Opt_uid), |
| 226 | fsparam_u32("gid", Opt_gid), |
| 227 | fsparam_u32oct("umask", Opt_umask), |
| 228 | fsparam_u32oct("dmask", Opt_dmask), |
| 229 | fsparam_u32oct("fmask", Opt_fmask), |
| 230 | fsparam_u32oct("allow_utime", Opt_allow_utime), |
| 231 | fsparam_string("iocharset", Opt_charset), |
Valdis Kletnieks | 9acd0d5 | 2020-03-02 15:21:45 +0900 | [diff] [blame] | 232 | fsparam_enum("errors", Opt_errors, exfat_param_enums), |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 233 | fsparam_flag("discard", Opt_discard), |
| 234 | fsparam_s32("time_offset", Opt_time_offset), |
| 235 | {} |
| 236 | }; |
| 237 | |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 238 | static int exfat_parse_param(struct fs_context *fc, struct fs_parameter *param) |
| 239 | { |
| 240 | struct exfat_sb_info *sbi = fc->s_fs_info; |
| 241 | struct exfat_mount_options *opts = &sbi->options; |
| 242 | struct fs_parse_result result; |
| 243 | int opt; |
| 244 | |
Valdis Kletnieks | 9acd0d5 | 2020-03-02 15:21:45 +0900 | [diff] [blame] | 245 | opt = fs_parse(fc, exfat_parameters, param, &result); |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 246 | if (opt < 0) |
| 247 | return opt; |
| 248 | |
| 249 | switch (opt) { |
| 250 | case Opt_uid: |
| 251 | opts->fs_uid = make_kuid(current_user_ns(), result.uint_32); |
| 252 | break; |
| 253 | case Opt_gid: |
| 254 | opts->fs_gid = make_kgid(current_user_ns(), result.uint_32); |
| 255 | break; |
| 256 | case Opt_umask: |
| 257 | opts->fs_fmask = result.uint_32; |
| 258 | opts->fs_dmask = result.uint_32; |
| 259 | break; |
| 260 | case Opt_dmask: |
| 261 | opts->fs_dmask = result.uint_32; |
| 262 | break; |
| 263 | case Opt_fmask: |
| 264 | opts->fs_fmask = result.uint_32; |
| 265 | break; |
| 266 | case Opt_allow_utime: |
| 267 | opts->allow_utime = result.uint_32 & 0022; |
| 268 | break; |
| 269 | case Opt_charset: |
| 270 | exfat_free_iocharset(sbi); |
| 271 | opts->iocharset = kstrdup(param->string, GFP_KERNEL); |
| 272 | if (!opts->iocharset) |
| 273 | return -ENOMEM; |
| 274 | break; |
| 275 | case Opt_errors: |
| 276 | opts->errors = result.uint_32; |
| 277 | break; |
| 278 | case Opt_discard: |
| 279 | opts->discard = 1; |
| 280 | break; |
| 281 | case Opt_time_offset: |
| 282 | /* |
| 283 | * Make the limit 24 just in case someone invents something |
| 284 | * unusual. |
| 285 | */ |
| 286 | if (result.int_32 < -24 * 60 || result.int_32 > 24 * 60) |
| 287 | return -EINVAL; |
| 288 | opts->time_offset = result.int_32; |
| 289 | break; |
| 290 | default: |
| 291 | return -EINVAL; |
| 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static void exfat_hash_init(struct super_block *sb) |
| 298 | { |
| 299 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 300 | int i; |
| 301 | |
| 302 | spin_lock_init(&sbi->inode_hash_lock); |
| 303 | for (i = 0; i < EXFAT_HASH_SIZE; i++) |
| 304 | INIT_HLIST_HEAD(&sbi->inode_hashtable[i]); |
| 305 | } |
| 306 | |
| 307 | static int exfat_read_root(struct inode *inode) |
| 308 | { |
| 309 | struct super_block *sb = inode->i_sb; |
| 310 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 311 | struct exfat_inode_info *ei = EXFAT_I(inode); |
| 312 | struct exfat_chain cdir; |
| 313 | int num_subdirs, num_clu = 0; |
| 314 | |
| 315 | exfat_chain_set(&ei->dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN); |
| 316 | ei->entry = -1; |
| 317 | ei->start_clu = sbi->root_dir; |
| 318 | ei->flags = ALLOC_FAT_CHAIN; |
| 319 | ei->type = TYPE_DIR; |
| 320 | ei->version = 0; |
| 321 | ei->rwoffset = 0; |
| 322 | ei->hint_bmap.off = EXFAT_EOF_CLUSTER; |
| 323 | ei->hint_stat.eidx = 0; |
| 324 | ei->hint_stat.clu = sbi->root_dir; |
| 325 | ei->hint_femp.eidx = EXFAT_HINT_NONE; |
| 326 | |
| 327 | exfat_chain_set(&cdir, sbi->root_dir, 0, ALLOC_FAT_CHAIN); |
| 328 | if (exfat_count_num_clusters(sb, &cdir, &num_clu)) |
| 329 | return -EIO; |
| 330 | i_size_write(inode, num_clu << sbi->cluster_size_bits); |
| 331 | |
| 332 | num_subdirs = exfat_count_dir_entries(sb, &cdir); |
| 333 | if (num_subdirs < 0) |
| 334 | return -EIO; |
| 335 | set_nlink(inode, num_subdirs + EXFAT_MIN_SUBDIR); |
| 336 | |
| 337 | inode->i_uid = sbi->options.fs_uid; |
| 338 | inode->i_gid = sbi->options.fs_gid; |
| 339 | inode_inc_iversion(inode); |
| 340 | inode->i_generation = 0; |
| 341 | inode->i_mode = exfat_make_mode(sbi, ATTR_SUBDIR, 0777); |
| 342 | inode->i_op = &exfat_dir_inode_operations; |
| 343 | inode->i_fop = &exfat_dir_operations; |
| 344 | |
| 345 | inode->i_blocks = ((i_size_read(inode) + (sbi->cluster_size - 1)) |
| 346 | & ~(sbi->cluster_size - 1)) >> inode->i_blkbits; |
| 347 | EXFAT_I(inode)->i_pos = ((loff_t)sbi->root_dir << 32) | 0xffffffff; |
| 348 | EXFAT_I(inode)->i_size_aligned = i_size_read(inode); |
| 349 | EXFAT_I(inode)->i_size_ondisk = i_size_read(inode); |
| 350 | |
| 351 | exfat_save_attr(inode, ATTR_SUBDIR); |
| 352 | inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = |
| 353 | current_time(inode); |
| 354 | exfat_cache_init_inode(inode); |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static struct pbr *exfat_read_pbr_with_logical_sector(struct super_block *sb, |
| 359 | struct buffer_head **prev_bh) |
| 360 | { |
| 361 | struct pbr *p_pbr = (struct pbr *) (*prev_bh)->b_data; |
| 362 | unsigned short logical_sect = 0; |
| 363 | |
| 364 | logical_sect = 1 << p_pbr->bsx.f64.sect_size_bits; |
| 365 | |
| 366 | if (!is_power_of_2(logical_sect) || |
| 367 | logical_sect < 512 || logical_sect > 4096) { |
| 368 | exfat_msg(sb, KERN_ERR, "bogus logical sector size %u", |
| 369 | logical_sect); |
| 370 | return NULL; |
| 371 | } |
| 372 | |
| 373 | if (logical_sect < sb->s_blocksize) { |
| 374 | exfat_msg(sb, KERN_ERR, |
| 375 | "logical sector size too small for device (logical sector size = %u)", |
| 376 | logical_sect); |
| 377 | return NULL; |
| 378 | } |
| 379 | |
| 380 | if (logical_sect > sb->s_blocksize) { |
| 381 | struct buffer_head *bh = NULL; |
| 382 | |
| 383 | __brelse(*prev_bh); |
| 384 | *prev_bh = NULL; |
| 385 | |
| 386 | if (!sb_set_blocksize(sb, logical_sect)) { |
| 387 | exfat_msg(sb, KERN_ERR, |
| 388 | "unable to set blocksize %u", logical_sect); |
| 389 | return NULL; |
| 390 | } |
| 391 | bh = sb_bread(sb, 0); |
| 392 | if (!bh) { |
| 393 | exfat_msg(sb, KERN_ERR, |
| 394 | "unable to read boot sector (logical sector size = %lu)", |
| 395 | sb->s_blocksize); |
| 396 | return NULL; |
| 397 | } |
| 398 | |
| 399 | *prev_bh = bh; |
| 400 | p_pbr = (struct pbr *) bh->b_data; |
| 401 | } |
| 402 | return p_pbr; |
| 403 | } |
| 404 | |
| 405 | /* mount the file system volume */ |
| 406 | static int __exfat_fill_super(struct super_block *sb) |
| 407 | { |
| 408 | int ret; |
| 409 | struct pbr *p_pbr; |
| 410 | struct pbr64 *p_bpb; |
| 411 | struct buffer_head *bh; |
| 412 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 413 | |
| 414 | /* set block size to read super block */ |
| 415 | sb_min_blocksize(sb, 512); |
| 416 | |
| 417 | /* read boot sector */ |
| 418 | bh = sb_bread(sb, 0); |
| 419 | if (!bh) { |
| 420 | exfat_msg(sb, KERN_ERR, "unable to read boot sector"); |
| 421 | return -EIO; |
| 422 | } |
| 423 | |
| 424 | /* PRB is read */ |
| 425 | p_pbr = (struct pbr *)bh->b_data; |
| 426 | |
| 427 | /* check the validity of PBR */ |
| 428 | if (le16_to_cpu((p_pbr->signature)) != PBR_SIGNATURE) { |
| 429 | exfat_msg(sb, KERN_ERR, "invalid boot record signature"); |
| 430 | ret = -EINVAL; |
| 431 | goto free_bh; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | /* check logical sector size */ |
| 436 | p_pbr = exfat_read_pbr_with_logical_sector(sb, &bh); |
| 437 | if (!p_pbr) { |
| 438 | ret = -EIO; |
| 439 | goto free_bh; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * res_zero field must be filled with zero to prevent mounting |
| 444 | * from FAT volume. |
| 445 | */ |
| 446 | if (memchr_inv(p_pbr->bpb.f64.res_zero, 0, |
| 447 | sizeof(p_pbr->bpb.f64.res_zero))) { |
| 448 | ret = -EINVAL; |
| 449 | goto free_bh; |
| 450 | } |
| 451 | |
| 452 | p_bpb = (struct pbr64 *)p_pbr; |
| 453 | if (!p_bpb->bsx.num_fats) { |
| 454 | exfat_msg(sb, KERN_ERR, "bogus number of FAT structure"); |
| 455 | ret = -EINVAL; |
| 456 | goto free_bh; |
| 457 | } |
| 458 | |
| 459 | sbi->sect_per_clus = 1 << p_bpb->bsx.sect_per_clus_bits; |
| 460 | sbi->sect_per_clus_bits = p_bpb->bsx.sect_per_clus_bits; |
| 461 | sbi->cluster_size_bits = sbi->sect_per_clus_bits + sb->s_blocksize_bits; |
| 462 | sbi->cluster_size = 1 << sbi->cluster_size_bits; |
| 463 | sbi->num_FAT_sectors = le32_to_cpu(p_bpb->bsx.fat_length); |
| 464 | sbi->FAT1_start_sector = le32_to_cpu(p_bpb->bsx.fat_offset); |
| 465 | sbi->FAT2_start_sector = p_bpb->bsx.num_fats == 1 ? |
| 466 | sbi->FAT1_start_sector : |
| 467 | sbi->FAT1_start_sector + sbi->num_FAT_sectors; |
| 468 | sbi->data_start_sector = le32_to_cpu(p_bpb->bsx.clu_offset); |
| 469 | sbi->num_sectors = le64_to_cpu(p_bpb->bsx.vol_length); |
| 470 | /* because the cluster index starts with 2 */ |
| 471 | sbi->num_clusters = le32_to_cpu(p_bpb->bsx.clu_count) + |
| 472 | EXFAT_RESERVED_CLUSTERS; |
| 473 | |
| 474 | sbi->root_dir = le32_to_cpu(p_bpb->bsx.root_cluster); |
| 475 | sbi->dentries_per_clu = 1 << |
| 476 | (sbi->cluster_size_bits - DENTRY_SIZE_BITS); |
| 477 | |
| 478 | sbi->vol_flag = le16_to_cpu(p_bpb->bsx.vol_flags); |
| 479 | sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER; |
| 480 | sbi->used_clusters = EXFAT_CLUSTERS_UNTRACKED; |
| 481 | |
| 482 | if (le16_to_cpu(p_bpb->bsx.vol_flags) & VOL_DIRTY) { |
| 483 | sbi->vol_flag |= VOL_DIRTY; |
| 484 | exfat_msg(sb, KERN_WARNING, |
| 485 | "Volume was not properly unmounted. Some data may be corrupt. Please run fsck."); |
| 486 | } |
| 487 | |
| 488 | /* exFAT file size is limited by a disk volume size */ |
| 489 | sb->s_maxbytes = (u64)(sbi->num_clusters - EXFAT_RESERVED_CLUSTERS) << |
| 490 | sbi->cluster_size_bits; |
| 491 | |
| 492 | ret = exfat_create_upcase_table(sb); |
| 493 | if (ret) { |
| 494 | exfat_msg(sb, KERN_ERR, "failed to load upcase table"); |
| 495 | goto free_bh; |
| 496 | } |
| 497 | |
| 498 | ret = exfat_load_bitmap(sb); |
| 499 | if (ret) { |
| 500 | exfat_msg(sb, KERN_ERR, "failed to load alloc-bitmap"); |
| 501 | goto free_upcase_table; |
| 502 | } |
| 503 | |
| 504 | ret = exfat_count_used_clusters(sb, &sbi->used_clusters); |
| 505 | if (ret) { |
| 506 | exfat_msg(sb, KERN_ERR, "failed to scan clusters"); |
| 507 | goto free_alloc_bitmap; |
| 508 | } |
| 509 | |
| 510 | return 0; |
| 511 | |
| 512 | free_alloc_bitmap: |
| 513 | exfat_free_bitmap(sbi); |
| 514 | free_upcase_table: |
| 515 | exfat_free_upcase_table(sbi); |
| 516 | free_bh: |
| 517 | brelse(bh); |
| 518 | return ret; |
| 519 | } |
| 520 | |
| 521 | static int exfat_fill_super(struct super_block *sb, struct fs_context *fc) |
| 522 | { |
| 523 | struct exfat_sb_info *sbi = sb->s_fs_info; |
| 524 | struct exfat_mount_options *opts = &sbi->options; |
| 525 | struct inode *root_inode; |
| 526 | int err; |
| 527 | |
| 528 | if (opts->allow_utime == (unsigned short)-1) |
| 529 | opts->allow_utime = ~opts->fs_dmask & 0022; |
| 530 | |
| 531 | if (opts->discard) { |
| 532 | struct request_queue *q = bdev_get_queue(sb->s_bdev); |
| 533 | |
| 534 | if (!blk_queue_discard(q)) |
| 535 | exfat_msg(sb, KERN_WARNING, |
| 536 | "mounting with \"discard\" option, but the device does not support discard"); |
| 537 | opts->discard = 0; |
| 538 | } |
| 539 | |
| 540 | sb->s_flags |= SB_NODIRATIME; |
| 541 | sb->s_magic = EXFAT_SUPER_MAGIC; |
| 542 | sb->s_op = &exfat_sops; |
| 543 | |
| 544 | sb->s_time_gran = 1; |
| 545 | sb->s_time_min = EXFAT_MIN_TIMESTAMP_SECS; |
| 546 | sb->s_time_max = EXFAT_MAX_TIMESTAMP_SECS; |
| 547 | |
| 548 | err = __exfat_fill_super(sb); |
| 549 | if (err) { |
| 550 | exfat_msg(sb, KERN_ERR, "failed to recognize exfat type"); |
| 551 | goto check_nls_io; |
| 552 | } |
| 553 | |
| 554 | /* set up enough so that it can read an inode */ |
| 555 | exfat_hash_init(sb); |
| 556 | |
| 557 | if (!strcmp(sbi->options.iocharset, "utf8")) |
| 558 | opts->utf8 = 1; |
| 559 | else { |
| 560 | sbi->nls_io = load_nls(sbi->options.iocharset); |
| 561 | if (!sbi->nls_io) { |
| 562 | exfat_msg(sb, KERN_ERR, "IO charset %s not found", |
| 563 | sbi->options.iocharset); |
| 564 | err = -EINVAL; |
| 565 | goto free_table; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | if (sbi->options.utf8) |
| 570 | sb->s_d_op = &exfat_utf8_dentry_ops; |
| 571 | else |
| 572 | sb->s_d_op = &exfat_dentry_ops; |
| 573 | |
| 574 | root_inode = new_inode(sb); |
| 575 | if (!root_inode) { |
| 576 | exfat_msg(sb, KERN_ERR, "failed to allocate root inode."); |
| 577 | err = -ENOMEM; |
| 578 | goto free_table; |
| 579 | } |
| 580 | |
| 581 | root_inode->i_ino = EXFAT_ROOT_INO; |
| 582 | inode_set_iversion(root_inode, 1); |
| 583 | err = exfat_read_root(root_inode); |
| 584 | if (err) { |
| 585 | exfat_msg(sb, KERN_ERR, "failed to initialize root inode."); |
| 586 | goto put_inode; |
| 587 | } |
| 588 | |
| 589 | exfat_hash_inode(root_inode, EXFAT_I(root_inode)->i_pos); |
| 590 | insert_inode_hash(root_inode); |
| 591 | |
| 592 | sb->s_root = d_make_root(root_inode); |
| 593 | if (!sb->s_root) { |
| 594 | exfat_msg(sb, KERN_ERR, "failed to get the root dentry"); |
| 595 | err = -ENOMEM; |
| 596 | goto put_inode; |
| 597 | } |
| 598 | |
| 599 | return 0; |
| 600 | |
| 601 | put_inode: |
| 602 | iput(root_inode); |
| 603 | sb->s_root = NULL; |
| 604 | |
| 605 | free_table: |
| 606 | exfat_free_upcase_table(sbi); |
| 607 | exfat_free_bitmap(sbi); |
| 608 | |
| 609 | check_nls_io: |
| 610 | unload_nls(sbi->nls_io); |
| 611 | exfat_free_iocharset(sbi); |
| 612 | sb->s_fs_info = NULL; |
| 613 | kfree(sbi); |
| 614 | return err; |
| 615 | } |
| 616 | |
| 617 | static int exfat_get_tree(struct fs_context *fc) |
| 618 | { |
| 619 | return get_tree_bdev(fc, exfat_fill_super); |
| 620 | } |
| 621 | |
| 622 | static void exfat_free(struct fs_context *fc) |
| 623 | { |
| 624 | kfree(fc->s_fs_info); |
| 625 | } |
| 626 | |
| 627 | static const struct fs_context_operations exfat_context_ops = { |
| 628 | .parse_param = exfat_parse_param, |
| 629 | .get_tree = exfat_get_tree, |
| 630 | .free = exfat_free, |
| 631 | }; |
| 632 | |
| 633 | static int exfat_init_fs_context(struct fs_context *fc) |
| 634 | { |
| 635 | struct exfat_sb_info *sbi; |
| 636 | |
| 637 | sbi = kzalloc(sizeof(struct exfat_sb_info), GFP_KERNEL); |
| 638 | if (!sbi) |
| 639 | return -ENOMEM; |
| 640 | |
| 641 | mutex_init(&sbi->s_lock); |
| 642 | ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL, |
| 643 | DEFAULT_RATELIMIT_BURST); |
| 644 | |
| 645 | sbi->options.fs_uid = current_uid(); |
| 646 | sbi->options.fs_gid = current_gid(); |
| 647 | sbi->options.fs_fmask = current->fs->umask; |
| 648 | sbi->options.fs_dmask = current->fs->umask; |
| 649 | sbi->options.allow_utime = -1; |
| 650 | sbi->options.iocharset = exfat_default_iocharset; |
| 651 | sbi->options.errors = EXFAT_ERRORS_RO; |
| 652 | |
| 653 | fc->s_fs_info = sbi; |
| 654 | fc->ops = &exfat_context_ops; |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | static struct file_system_type exfat_fs_type = { |
| 659 | .owner = THIS_MODULE, |
| 660 | .name = "exfat", |
| 661 | .init_fs_context = exfat_init_fs_context, |
Valdis Kletnieks | 9acd0d5 | 2020-03-02 15:21:45 +0900 | [diff] [blame] | 662 | .parameters = exfat_parameters, |
Namjae Jeon | 719c1e1 | 2020-03-02 15:21:33 +0900 | [diff] [blame] | 663 | .kill_sb = kill_block_super, |
| 664 | .fs_flags = FS_REQUIRES_DEV, |
| 665 | }; |
| 666 | |
| 667 | static void exfat_inode_init_once(void *foo) |
| 668 | { |
| 669 | struct exfat_inode_info *ei = (struct exfat_inode_info *)foo; |
| 670 | |
| 671 | INIT_HLIST_NODE(&ei->i_hash_fat); |
| 672 | inode_init_once(&ei->vfs_inode); |
| 673 | } |
| 674 | |
| 675 | static int __init init_exfat_fs(void) |
| 676 | { |
| 677 | int err; |
| 678 | |
| 679 | err = exfat_cache_init(); |
| 680 | if (err) |
| 681 | return err; |
| 682 | |
| 683 | exfat_inode_cachep = kmem_cache_create("exfat_inode_cache", |
| 684 | sizeof(struct exfat_inode_info), |
| 685 | 0, SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 686 | exfat_inode_init_once); |
| 687 | if (!exfat_inode_cachep) { |
| 688 | err = -ENOMEM; |
| 689 | goto shutdown_cache; |
| 690 | } |
| 691 | |
| 692 | err = register_filesystem(&exfat_fs_type); |
| 693 | if (err) |
| 694 | goto destroy_cache; |
| 695 | |
| 696 | return 0; |
| 697 | |
| 698 | destroy_cache: |
| 699 | kmem_cache_destroy(exfat_inode_cachep); |
| 700 | shutdown_cache: |
| 701 | exfat_cache_shutdown(); |
| 702 | return err; |
| 703 | } |
| 704 | |
| 705 | static void __exit exit_exfat_fs(void) |
| 706 | { |
| 707 | /* |
| 708 | * Make sure all delayed rcu free inodes are flushed before we |
| 709 | * destroy cache. |
| 710 | */ |
| 711 | rcu_barrier(); |
| 712 | kmem_cache_destroy(exfat_inode_cachep); |
| 713 | unregister_filesystem(&exfat_fs_type); |
| 714 | exfat_cache_shutdown(); |
| 715 | } |
| 716 | |
| 717 | module_init(init_exfat_fs); |
| 718 | module_exit(exit_exfat_fs); |
| 719 | |
| 720 | MODULE_LICENSE("GPL"); |
| 721 | MODULE_DESCRIPTION("exFAT filesystem support"); |
| 722 | MODULE_AUTHOR("Samsung Electronics Co., Ltd."); |