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