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