0001
0002
0003
0004
0005
0006 #include <linux/fs_context.h>
0007 #include <linux/fs_parser.h>
0008 #include <linux/module.h>
0009 #include <linux/init.h>
0010 #include <linux/time.h>
0011 #include <linux/mount.h>
0012 #include <linux/cred.h>
0013 #include <linux/statfs.h>
0014 #include <linux/seq_file.h>
0015 #include <linux/blkdev.h>
0016 #include <linux/fs_struct.h>
0017 #include <linux/iversion.h>
0018 #include <linux/nls.h>
0019 #include <linux/buffer_head.h>
0020 #include <linux/magic.h>
0021
0022 #include "exfat_raw.h"
0023 #include "exfat_fs.h"
0024
0025 static char exfat_default_iocharset[] = CONFIG_EXFAT_DEFAULT_IOCHARSET;
0026 static struct kmem_cache *exfat_inode_cachep;
0027
0028 static void exfat_free_iocharset(struct exfat_sb_info *sbi)
0029 {
0030 if (sbi->options.iocharset != exfat_default_iocharset)
0031 kfree(sbi->options.iocharset);
0032 }
0033
0034 static void exfat_delayed_free(struct rcu_head *p)
0035 {
0036 struct exfat_sb_info *sbi = container_of(p, struct exfat_sb_info, rcu);
0037
0038 unload_nls(sbi->nls_io);
0039 exfat_free_iocharset(sbi);
0040 exfat_free_upcase_table(sbi);
0041 kfree(sbi);
0042 }
0043
0044 static void exfat_put_super(struct super_block *sb)
0045 {
0046 struct exfat_sb_info *sbi = EXFAT_SB(sb);
0047
0048 mutex_lock(&sbi->s_lock);
0049 exfat_free_bitmap(sbi);
0050 brelse(sbi->boot_bh);
0051 mutex_unlock(&sbi->s_lock);
0052
0053 call_rcu(&sbi->rcu, exfat_delayed_free);
0054 }
0055
0056 static int exfat_sync_fs(struct super_block *sb, int wait)
0057 {
0058 struct exfat_sb_info *sbi = EXFAT_SB(sb);
0059 int err = 0;
0060
0061 if (!wait)
0062 return 0;
0063
0064
0065 mutex_lock(&sbi->s_lock);
0066 sync_blockdev(sb->s_bdev);
0067 if (exfat_clear_volume_dirty(sb))
0068 err = -EIO;
0069 mutex_unlock(&sbi->s_lock);
0070 return err;
0071 }
0072
0073 static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf)
0074 {
0075 struct super_block *sb = dentry->d_sb;
0076 struct exfat_sb_info *sbi = EXFAT_SB(sb);
0077 unsigned long long id = huge_encode_dev(sb->s_bdev->bd_dev);
0078
0079 if (sbi->used_clusters == EXFAT_CLUSTERS_UNTRACKED) {
0080 mutex_lock(&sbi->s_lock);
0081 if (exfat_count_used_clusters(sb, &sbi->used_clusters)) {
0082 mutex_unlock(&sbi->s_lock);
0083 return -EIO;
0084 }
0085 mutex_unlock(&sbi->s_lock);
0086 }
0087
0088 buf->f_type = sb->s_magic;
0089 buf->f_bsize = sbi->cluster_size;
0090 buf->f_blocks = sbi->num_clusters - 2;
0091 buf->f_bfree = buf->f_blocks - sbi->used_clusters;
0092 buf->f_bavail = buf->f_bfree;
0093 buf->f_fsid = u64_to_fsid(id);
0094
0095 buf->f_namelen = EXFAT_MAX_FILE_LEN * NLS_MAX_CHARSET_SIZE;
0096 return 0;
0097 }
0098
0099 static int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flags)
0100 {
0101 struct exfat_sb_info *sbi = EXFAT_SB(sb);
0102 struct boot_sector *p_boot = (struct boot_sector *)sbi->boot_bh->b_data;
0103
0104
0105 new_flags |= sbi->vol_flags_persistent;
0106
0107
0108 if (sbi->vol_flags == new_flags)
0109 return 0;
0110
0111 sbi->vol_flags = new_flags;
0112
0113
0114
0115
0116 if (sb_rdonly(sb))
0117 return 0;
0118
0119 p_boot->vol_flags = cpu_to_le16(new_flags);
0120
0121 set_buffer_uptodate(sbi->boot_bh);
0122 mark_buffer_dirty(sbi->boot_bh);
0123
0124 __sync_dirty_buffer(sbi->boot_bh, REQ_SYNC | REQ_FUA | REQ_PREFLUSH);
0125
0126 return 0;
0127 }
0128
0129 int exfat_set_volume_dirty(struct super_block *sb)
0130 {
0131 struct exfat_sb_info *sbi = EXFAT_SB(sb);
0132
0133 return exfat_set_vol_flags(sb, sbi->vol_flags | VOLUME_DIRTY);
0134 }
0135
0136 int exfat_clear_volume_dirty(struct super_block *sb)
0137 {
0138 struct exfat_sb_info *sbi = EXFAT_SB(sb);
0139
0140 return exfat_set_vol_flags(sb, sbi->vol_flags & ~VOLUME_DIRTY);
0141 }
0142
0143 static int exfat_show_options(struct seq_file *m, struct dentry *root)
0144 {
0145 struct super_block *sb = root->d_sb;
0146 struct exfat_sb_info *sbi = EXFAT_SB(sb);
0147 struct exfat_mount_options *opts = &sbi->options;
0148
0149
0150 if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID))
0151 seq_printf(m, ",uid=%u",
0152 from_kuid_munged(&init_user_ns, opts->fs_uid));
0153 if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID))
0154 seq_printf(m, ",gid=%u",
0155 from_kgid_munged(&init_user_ns, opts->fs_gid));
0156 seq_printf(m, ",fmask=%04o,dmask=%04o", opts->fs_fmask, opts->fs_dmask);
0157 if (opts->allow_utime)
0158 seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
0159 if (opts->utf8)
0160 seq_puts(m, ",iocharset=utf8");
0161 else if (sbi->nls_io)
0162 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
0163 if (opts->errors == EXFAT_ERRORS_CONT)
0164 seq_puts(m, ",errors=continue");
0165 else if (opts->errors == EXFAT_ERRORS_PANIC)
0166 seq_puts(m, ",errors=panic");
0167 else
0168 seq_puts(m, ",errors=remount-ro");
0169 if (opts->discard)
0170 seq_puts(m, ",discard");
0171 if (opts->keep_last_dots)
0172 seq_puts(m, ",keep_last_dots");
0173 if (opts->sys_tz)
0174 seq_puts(m, ",sys_tz");
0175 else if (opts->time_offset)
0176 seq_printf(m, ",time_offset=%d", opts->time_offset);
0177 return 0;
0178 }
0179
0180 static struct inode *exfat_alloc_inode(struct super_block *sb)
0181 {
0182 struct exfat_inode_info *ei;
0183
0184 ei = alloc_inode_sb(sb, exfat_inode_cachep, GFP_NOFS);
0185 if (!ei)
0186 return NULL;
0187
0188 init_rwsem(&ei->truncate_lock);
0189 return &ei->vfs_inode;
0190 }
0191
0192 static void exfat_free_inode(struct inode *inode)
0193 {
0194 kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode));
0195 }
0196
0197 static const struct super_operations exfat_sops = {
0198 .alloc_inode = exfat_alloc_inode,
0199 .free_inode = exfat_free_inode,
0200 .write_inode = exfat_write_inode,
0201 .evict_inode = exfat_evict_inode,
0202 .put_super = exfat_put_super,
0203 .sync_fs = exfat_sync_fs,
0204 .statfs = exfat_statfs,
0205 .show_options = exfat_show_options,
0206 };
0207
0208 enum {
0209 Opt_uid,
0210 Opt_gid,
0211 Opt_umask,
0212 Opt_dmask,
0213 Opt_fmask,
0214 Opt_allow_utime,
0215 Opt_charset,
0216 Opt_errors,
0217 Opt_discard,
0218 Opt_keep_last_dots,
0219 Opt_sys_tz,
0220 Opt_time_offset,
0221
0222
0223 Opt_utf8,
0224 Opt_debug,
0225 Opt_namecase,
0226 Opt_codepage,
0227 };
0228
0229 static const struct constant_table exfat_param_enums[] = {
0230 { "continue", EXFAT_ERRORS_CONT },
0231 { "panic", EXFAT_ERRORS_PANIC },
0232 { "remount-ro", EXFAT_ERRORS_RO },
0233 {}
0234 };
0235
0236 static const struct fs_parameter_spec exfat_parameters[] = {
0237 fsparam_u32("uid", Opt_uid),
0238 fsparam_u32("gid", Opt_gid),
0239 fsparam_u32oct("umask", Opt_umask),
0240 fsparam_u32oct("dmask", Opt_dmask),
0241 fsparam_u32oct("fmask", Opt_fmask),
0242 fsparam_u32oct("allow_utime", Opt_allow_utime),
0243 fsparam_string("iocharset", Opt_charset),
0244 fsparam_enum("errors", Opt_errors, exfat_param_enums),
0245 fsparam_flag("discard", Opt_discard),
0246 fsparam_flag("keep_last_dots", Opt_keep_last_dots),
0247 fsparam_flag("sys_tz", Opt_sys_tz),
0248 fsparam_s32("time_offset", Opt_time_offset),
0249 __fsparam(NULL, "utf8", Opt_utf8, fs_param_deprecated,
0250 NULL),
0251 __fsparam(NULL, "debug", Opt_debug, fs_param_deprecated,
0252 NULL),
0253 __fsparam(fs_param_is_u32, "namecase", Opt_namecase,
0254 fs_param_deprecated, NULL),
0255 __fsparam(fs_param_is_u32, "codepage", Opt_codepage,
0256 fs_param_deprecated, NULL),
0257 {}
0258 };
0259
0260 static int exfat_parse_param(struct fs_context *fc, struct fs_parameter *param)
0261 {
0262 struct exfat_sb_info *sbi = fc->s_fs_info;
0263 struct exfat_mount_options *opts = &sbi->options;
0264 struct fs_parse_result result;
0265 int opt;
0266
0267 opt = fs_parse(fc, exfat_parameters, param, &result);
0268 if (opt < 0)
0269 return opt;
0270
0271 switch (opt) {
0272 case Opt_uid:
0273 opts->fs_uid = make_kuid(current_user_ns(), result.uint_32);
0274 break;
0275 case Opt_gid:
0276 opts->fs_gid = make_kgid(current_user_ns(), result.uint_32);
0277 break;
0278 case Opt_umask:
0279 opts->fs_fmask = result.uint_32;
0280 opts->fs_dmask = result.uint_32;
0281 break;
0282 case Opt_dmask:
0283 opts->fs_dmask = result.uint_32;
0284 break;
0285 case Opt_fmask:
0286 opts->fs_fmask = result.uint_32;
0287 break;
0288 case Opt_allow_utime:
0289 opts->allow_utime = result.uint_32 & 0022;
0290 break;
0291 case Opt_charset:
0292 exfat_free_iocharset(sbi);
0293 opts->iocharset = param->string;
0294 param->string = NULL;
0295 break;
0296 case Opt_errors:
0297 opts->errors = result.uint_32;
0298 break;
0299 case Opt_discard:
0300 opts->discard = 1;
0301 break;
0302 case Opt_keep_last_dots:
0303 opts->keep_last_dots = 1;
0304 break;
0305 case Opt_sys_tz:
0306 opts->sys_tz = 1;
0307 break;
0308 case Opt_time_offset:
0309
0310
0311
0312
0313 if (result.int_32 < -24 * 60 || result.int_32 > 24 * 60)
0314 return -EINVAL;
0315 opts->time_offset = result.int_32;
0316 break;
0317 case Opt_utf8:
0318 case Opt_debug:
0319 case Opt_namecase:
0320 case Opt_codepage:
0321 break;
0322 default:
0323 return -EINVAL;
0324 }
0325
0326 return 0;
0327 }
0328
0329 static void exfat_hash_init(struct super_block *sb)
0330 {
0331 struct exfat_sb_info *sbi = EXFAT_SB(sb);
0332 int i;
0333
0334 spin_lock_init(&sbi->inode_hash_lock);
0335 for (i = 0; i < EXFAT_HASH_SIZE; i++)
0336 INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
0337 }
0338
0339 static int exfat_read_root(struct inode *inode)
0340 {
0341 struct super_block *sb = inode->i_sb;
0342 struct exfat_sb_info *sbi = EXFAT_SB(sb);
0343 struct exfat_inode_info *ei = EXFAT_I(inode);
0344 struct exfat_chain cdir;
0345 int num_subdirs, num_clu = 0;
0346
0347 exfat_chain_set(&ei->dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
0348 ei->entry = -1;
0349 ei->start_clu = sbi->root_dir;
0350 ei->flags = ALLOC_FAT_CHAIN;
0351 ei->type = TYPE_DIR;
0352 ei->version = 0;
0353 ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
0354 ei->hint_stat.eidx = 0;
0355 ei->hint_stat.clu = sbi->root_dir;
0356 ei->hint_femp.eidx = EXFAT_HINT_NONE;
0357
0358 exfat_chain_set(&cdir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
0359 if (exfat_count_num_clusters(sb, &cdir, &num_clu))
0360 return -EIO;
0361 i_size_write(inode, num_clu << sbi->cluster_size_bits);
0362
0363 num_subdirs = exfat_count_dir_entries(sb, &cdir);
0364 if (num_subdirs < 0)
0365 return -EIO;
0366 set_nlink(inode, num_subdirs + EXFAT_MIN_SUBDIR);
0367
0368 inode->i_uid = sbi->options.fs_uid;
0369 inode->i_gid = sbi->options.fs_gid;
0370 inode_inc_iversion(inode);
0371 inode->i_generation = 0;
0372 inode->i_mode = exfat_make_mode(sbi, ATTR_SUBDIR, 0777);
0373 inode->i_op = &exfat_dir_inode_operations;
0374 inode->i_fop = &exfat_dir_operations;
0375
0376 inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >>
0377 inode->i_blkbits;
0378 ei->i_pos = ((loff_t)sbi->root_dir << 32) | 0xffffffff;
0379 ei->i_size_aligned = i_size_read(inode);
0380 ei->i_size_ondisk = i_size_read(inode);
0381
0382 exfat_save_attr(inode, ATTR_SUBDIR);
0383 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
0384 current_time(inode);
0385 exfat_truncate_atime(&inode->i_atime);
0386 return 0;
0387 }
0388
0389 static int exfat_calibrate_blocksize(struct super_block *sb, int logical_sect)
0390 {
0391 struct exfat_sb_info *sbi = EXFAT_SB(sb);
0392
0393 if (!is_power_of_2(logical_sect)) {
0394 exfat_err(sb, "bogus logical sector size %u", logical_sect);
0395 return -EIO;
0396 }
0397
0398 if (logical_sect < sb->s_blocksize) {
0399 exfat_err(sb, "logical sector size too small for device (logical sector size = %u)",
0400 logical_sect);
0401 return -EIO;
0402 }
0403
0404 if (logical_sect > sb->s_blocksize) {
0405 brelse(sbi->boot_bh);
0406 sbi->boot_bh = NULL;
0407
0408 if (!sb_set_blocksize(sb, logical_sect)) {
0409 exfat_err(sb, "unable to set blocksize %u",
0410 logical_sect);
0411 return -EIO;
0412 }
0413 sbi->boot_bh = sb_bread(sb, 0);
0414 if (!sbi->boot_bh) {
0415 exfat_err(sb, "unable to read boot sector (logical sector size = %lu)",
0416 sb->s_blocksize);
0417 return -EIO;
0418 }
0419 }
0420 return 0;
0421 }
0422
0423 static int exfat_read_boot_sector(struct super_block *sb)
0424 {
0425 struct boot_sector *p_boot;
0426 struct exfat_sb_info *sbi = EXFAT_SB(sb);
0427
0428
0429 sb_min_blocksize(sb, 512);
0430
0431
0432 sbi->boot_bh = sb_bread(sb, 0);
0433 if (!sbi->boot_bh) {
0434 exfat_err(sb, "unable to read boot sector");
0435 return -EIO;
0436 }
0437 p_boot = (struct boot_sector *)sbi->boot_bh->b_data;
0438
0439
0440 if (le16_to_cpu((p_boot->signature)) != BOOT_SIGNATURE) {
0441 exfat_err(sb, "invalid boot record signature");
0442 return -EINVAL;
0443 }
0444
0445 if (memcmp(p_boot->fs_name, STR_EXFAT, BOOTSEC_FS_NAME_LEN)) {
0446 exfat_err(sb, "invalid fs_name");
0447 return -EINVAL;
0448 }
0449
0450
0451
0452
0453
0454 if (memchr_inv(p_boot->must_be_zero, 0, sizeof(p_boot->must_be_zero)))
0455 return -EINVAL;
0456
0457 if (p_boot->num_fats != 1 && p_boot->num_fats != 2) {
0458 exfat_err(sb, "bogus number of FAT structure");
0459 return -EINVAL;
0460 }
0461
0462
0463
0464
0465 if (p_boot->sect_size_bits < EXFAT_MIN_SECT_SIZE_BITS ||
0466 p_boot->sect_size_bits > EXFAT_MAX_SECT_SIZE_BITS) {
0467 exfat_err(sb, "bogus sector size bits : %u",
0468 p_boot->sect_size_bits);
0469 return -EINVAL;
0470 }
0471
0472
0473
0474
0475 if (p_boot->sect_per_clus_bits > EXFAT_MAX_SECT_PER_CLUS_BITS(p_boot)) {
0476 exfat_err(sb, "bogus sectors bits per cluster : %u",
0477 p_boot->sect_per_clus_bits);
0478 return -EINVAL;
0479 }
0480
0481 sbi->sect_per_clus = 1 << p_boot->sect_per_clus_bits;
0482 sbi->sect_per_clus_bits = p_boot->sect_per_clus_bits;
0483 sbi->cluster_size_bits = p_boot->sect_per_clus_bits +
0484 p_boot->sect_size_bits;
0485 sbi->cluster_size = 1 << sbi->cluster_size_bits;
0486 sbi->num_FAT_sectors = le32_to_cpu(p_boot->fat_length);
0487 sbi->FAT1_start_sector = le32_to_cpu(p_boot->fat_offset);
0488 sbi->FAT2_start_sector = le32_to_cpu(p_boot->fat_offset);
0489 if (p_boot->num_fats == 2)
0490 sbi->FAT2_start_sector += sbi->num_FAT_sectors;
0491 sbi->data_start_sector = le32_to_cpu(p_boot->clu_offset);
0492 sbi->num_sectors = le64_to_cpu(p_boot->vol_length);
0493
0494 sbi->num_clusters = le32_to_cpu(p_boot->clu_count) +
0495 EXFAT_RESERVED_CLUSTERS;
0496
0497 sbi->root_dir = le32_to_cpu(p_boot->root_cluster);
0498 sbi->dentries_per_clu = 1 <<
0499 (sbi->cluster_size_bits - DENTRY_SIZE_BITS);
0500
0501 sbi->vol_flags = le16_to_cpu(p_boot->vol_flags);
0502 sbi->vol_flags_persistent = sbi->vol_flags & (VOLUME_DIRTY | MEDIA_FAILURE);
0503 sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
0504 sbi->used_clusters = EXFAT_CLUSTERS_UNTRACKED;
0505
0506
0507 if ((u64)sbi->num_FAT_sectors << p_boot->sect_size_bits <
0508 (u64)sbi->num_clusters * 4) {
0509 exfat_err(sb, "bogus fat length");
0510 return -EINVAL;
0511 }
0512
0513 if (sbi->data_start_sector <
0514 (u64)sbi->FAT1_start_sector +
0515 (u64)sbi->num_FAT_sectors * p_boot->num_fats) {
0516 exfat_err(sb, "bogus data start sector");
0517 return -EINVAL;
0518 }
0519
0520 if (sbi->vol_flags & VOLUME_DIRTY)
0521 exfat_warn(sb, "Volume was not properly unmounted. Some data may be corrupt. Please run fsck.");
0522 if (sbi->vol_flags & MEDIA_FAILURE)
0523 exfat_warn(sb, "Medium has reported failures. Some data may be lost.");
0524
0525
0526 sb->s_maxbytes = (u64)(sbi->num_clusters - EXFAT_RESERVED_CLUSTERS) <<
0527 sbi->cluster_size_bits;
0528
0529
0530 if (exfat_calibrate_blocksize(sb, 1 << p_boot->sect_size_bits))
0531 return -EIO;
0532
0533 return 0;
0534 }
0535
0536 static int exfat_verify_boot_region(struct super_block *sb)
0537 {
0538 struct buffer_head *bh = NULL;
0539 u32 chksum = 0;
0540 __le32 *p_sig, *p_chksum;
0541 int sn, i;
0542
0543
0544 for (sn = 0; sn < 11; sn++) {
0545 bh = sb_bread(sb, sn);
0546 if (!bh)
0547 return -EIO;
0548
0549 if (sn != 0 && sn <= 8) {
0550
0551 p_sig = (__le32 *)&bh->b_data[sb->s_blocksize - 4];
0552 if (le32_to_cpu(*p_sig) != EXBOOT_SIGNATURE)
0553 exfat_warn(sb, "Invalid exboot-signature(sector = %d): 0x%08x",
0554 sn, le32_to_cpu(*p_sig));
0555 }
0556
0557 chksum = exfat_calc_chksum32(bh->b_data, sb->s_blocksize,
0558 chksum, sn ? CS_DEFAULT : CS_BOOT_SECTOR);
0559 brelse(bh);
0560 }
0561
0562
0563 bh = sb_bread(sb, sn);
0564 if (!bh)
0565 return -EIO;
0566
0567 for (i = 0; i < sb->s_blocksize; i += sizeof(u32)) {
0568 p_chksum = (__le32 *)&bh->b_data[i];
0569 if (le32_to_cpu(*p_chksum) != chksum) {
0570 exfat_err(sb, "Invalid boot checksum (boot checksum : 0x%08x, checksum : 0x%08x)",
0571 le32_to_cpu(*p_chksum), chksum);
0572 brelse(bh);
0573 return -EINVAL;
0574 }
0575 }
0576 brelse(bh);
0577 return 0;
0578 }
0579
0580
0581 static int __exfat_fill_super(struct super_block *sb)
0582 {
0583 int ret;
0584 struct exfat_sb_info *sbi = EXFAT_SB(sb);
0585
0586 ret = exfat_read_boot_sector(sb);
0587 if (ret) {
0588 exfat_err(sb, "failed to read boot sector");
0589 goto free_bh;
0590 }
0591
0592 ret = exfat_verify_boot_region(sb);
0593 if (ret) {
0594 exfat_err(sb, "invalid boot region");
0595 goto free_bh;
0596 }
0597
0598 ret = exfat_create_upcase_table(sb);
0599 if (ret) {
0600 exfat_err(sb, "failed to load upcase table");
0601 goto free_bh;
0602 }
0603
0604 ret = exfat_load_bitmap(sb);
0605 if (ret) {
0606 exfat_err(sb, "failed to load alloc-bitmap");
0607 goto free_upcase_table;
0608 }
0609
0610 ret = exfat_count_used_clusters(sb, &sbi->used_clusters);
0611 if (ret) {
0612 exfat_err(sb, "failed to scan clusters");
0613 goto free_alloc_bitmap;
0614 }
0615
0616 return 0;
0617
0618 free_alloc_bitmap:
0619 exfat_free_bitmap(sbi);
0620 free_upcase_table:
0621 exfat_free_upcase_table(sbi);
0622 free_bh:
0623 brelse(sbi->boot_bh);
0624 return ret;
0625 }
0626
0627 static int exfat_fill_super(struct super_block *sb, struct fs_context *fc)
0628 {
0629 struct exfat_sb_info *sbi = sb->s_fs_info;
0630 struct exfat_mount_options *opts = &sbi->options;
0631 struct inode *root_inode;
0632 int err;
0633
0634 if (opts->allow_utime == (unsigned short)-1)
0635 opts->allow_utime = ~opts->fs_dmask & 0022;
0636
0637 if (opts->discard && !bdev_max_discard_sectors(sb->s_bdev)) {
0638 exfat_warn(sb, "mounting with \"discard\" option, but the device does not support discard");
0639 opts->discard = 0;
0640 }
0641
0642 sb->s_flags |= SB_NODIRATIME;
0643 sb->s_magic = EXFAT_SUPER_MAGIC;
0644 sb->s_op = &exfat_sops;
0645
0646 sb->s_time_gran = 10 * NSEC_PER_MSEC;
0647 sb->s_time_min = EXFAT_MIN_TIMESTAMP_SECS;
0648 sb->s_time_max = EXFAT_MAX_TIMESTAMP_SECS;
0649
0650 err = __exfat_fill_super(sb);
0651 if (err) {
0652 exfat_err(sb, "failed to recognize exfat type");
0653 goto check_nls_io;
0654 }
0655
0656
0657 exfat_hash_init(sb);
0658
0659 if (!strcmp(sbi->options.iocharset, "utf8"))
0660 opts->utf8 = 1;
0661 else {
0662 sbi->nls_io = load_nls(sbi->options.iocharset);
0663 if (!sbi->nls_io) {
0664 exfat_err(sb, "IO charset %s not found",
0665 sbi->options.iocharset);
0666 err = -EINVAL;
0667 goto free_table;
0668 }
0669 }
0670
0671 if (sbi->options.utf8)
0672 sb->s_d_op = &exfat_utf8_dentry_ops;
0673 else
0674 sb->s_d_op = &exfat_dentry_ops;
0675
0676 root_inode = new_inode(sb);
0677 if (!root_inode) {
0678 exfat_err(sb, "failed to allocate root inode");
0679 err = -ENOMEM;
0680 goto free_table;
0681 }
0682
0683 root_inode->i_ino = EXFAT_ROOT_INO;
0684 inode_set_iversion(root_inode, 1);
0685 err = exfat_read_root(root_inode);
0686 if (err) {
0687 exfat_err(sb, "failed to initialize root inode");
0688 goto put_inode;
0689 }
0690
0691 exfat_hash_inode(root_inode, EXFAT_I(root_inode)->i_pos);
0692 insert_inode_hash(root_inode);
0693
0694 sb->s_root = d_make_root(root_inode);
0695 if (!sb->s_root) {
0696 exfat_err(sb, "failed to get the root dentry");
0697 err = -ENOMEM;
0698 goto free_table;
0699 }
0700
0701 return 0;
0702
0703 put_inode:
0704 iput(root_inode);
0705 sb->s_root = NULL;
0706
0707 free_table:
0708 exfat_free_upcase_table(sbi);
0709 exfat_free_bitmap(sbi);
0710 brelse(sbi->boot_bh);
0711
0712 check_nls_io:
0713 unload_nls(sbi->nls_io);
0714 exfat_free_iocharset(sbi);
0715 sb->s_fs_info = NULL;
0716 kfree(sbi);
0717 return err;
0718 }
0719
0720 static int exfat_get_tree(struct fs_context *fc)
0721 {
0722 return get_tree_bdev(fc, exfat_fill_super);
0723 }
0724
0725 static void exfat_free(struct fs_context *fc)
0726 {
0727 struct exfat_sb_info *sbi = fc->s_fs_info;
0728
0729 if (sbi) {
0730 exfat_free_iocharset(sbi);
0731 kfree(sbi);
0732 }
0733 }
0734
0735 static int exfat_reconfigure(struct fs_context *fc)
0736 {
0737 fc->sb_flags |= SB_NODIRATIME;
0738
0739
0740 sync_filesystem(fc->root->d_sb);
0741 return 0;
0742 }
0743
0744 static const struct fs_context_operations exfat_context_ops = {
0745 .parse_param = exfat_parse_param,
0746 .get_tree = exfat_get_tree,
0747 .free = exfat_free,
0748 .reconfigure = exfat_reconfigure,
0749 };
0750
0751 static int exfat_init_fs_context(struct fs_context *fc)
0752 {
0753 struct exfat_sb_info *sbi;
0754
0755 sbi = kzalloc(sizeof(struct exfat_sb_info), GFP_KERNEL);
0756 if (!sbi)
0757 return -ENOMEM;
0758
0759 mutex_init(&sbi->s_lock);
0760 mutex_init(&sbi->bitmap_lock);
0761 ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
0762 DEFAULT_RATELIMIT_BURST);
0763
0764 sbi->options.fs_uid = current_uid();
0765 sbi->options.fs_gid = current_gid();
0766 sbi->options.fs_fmask = current->fs->umask;
0767 sbi->options.fs_dmask = current->fs->umask;
0768 sbi->options.allow_utime = -1;
0769 sbi->options.iocharset = exfat_default_iocharset;
0770 sbi->options.errors = EXFAT_ERRORS_RO;
0771
0772 fc->s_fs_info = sbi;
0773 fc->ops = &exfat_context_ops;
0774 return 0;
0775 }
0776
0777 static struct file_system_type exfat_fs_type = {
0778 .owner = THIS_MODULE,
0779 .name = "exfat",
0780 .init_fs_context = exfat_init_fs_context,
0781 .parameters = exfat_parameters,
0782 .kill_sb = kill_block_super,
0783 .fs_flags = FS_REQUIRES_DEV,
0784 };
0785
0786 static void exfat_inode_init_once(void *foo)
0787 {
0788 struct exfat_inode_info *ei = (struct exfat_inode_info *)foo;
0789
0790 spin_lock_init(&ei->cache_lru_lock);
0791 ei->nr_caches = 0;
0792 ei->cache_valid_id = EXFAT_CACHE_VALID + 1;
0793 INIT_LIST_HEAD(&ei->cache_lru);
0794 INIT_HLIST_NODE(&ei->i_hash_fat);
0795 inode_init_once(&ei->vfs_inode);
0796 }
0797
0798 static int __init init_exfat_fs(void)
0799 {
0800 int err;
0801
0802 err = exfat_cache_init();
0803 if (err)
0804 return err;
0805
0806 exfat_inode_cachep = kmem_cache_create("exfat_inode_cache",
0807 sizeof(struct exfat_inode_info),
0808 0, SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
0809 exfat_inode_init_once);
0810 if (!exfat_inode_cachep) {
0811 err = -ENOMEM;
0812 goto shutdown_cache;
0813 }
0814
0815 err = register_filesystem(&exfat_fs_type);
0816 if (err)
0817 goto destroy_cache;
0818
0819 return 0;
0820
0821 destroy_cache:
0822 kmem_cache_destroy(exfat_inode_cachep);
0823 shutdown_cache:
0824 exfat_cache_shutdown();
0825 return err;
0826 }
0827
0828 static void __exit exit_exfat_fs(void)
0829 {
0830
0831
0832
0833
0834 rcu_barrier();
0835 kmem_cache_destroy(exfat_inode_cachep);
0836 unregister_filesystem(&exfat_fs_type);
0837 exfat_cache_shutdown();
0838 }
0839
0840 module_init(init_exfat_fs);
0841 module_exit(exit_exfat_fs);
0842
0843 MODULE_ALIAS_FS("exfat");
0844 MODULE_LICENSE("GPL");
0845 MODULE_DESCRIPTION("exFAT filesystem support");
0846 MODULE_AUTHOR("Samsung Electronics Co., Ltd.");