1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
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>
21 #include "exfat_raw.h"
24 static char exfat_default_iocharset
[] = CONFIG_EXFAT_DEFAULT_IOCHARSET
;
25 static struct kmem_cache
*exfat_inode_cachep
;
27 static void exfat_free_iocharset(struct exfat_sb_info
*sbi
)
29 if (sbi
->options
.iocharset
!= exfat_default_iocharset
)
30 kfree(sbi
->options
.iocharset
);
33 static void exfat_delayed_free(struct rcu_head
*p
)
35 struct exfat_sb_info
*sbi
= container_of(p
, struct exfat_sb_info
, rcu
);
37 unload_nls(sbi
->nls_io
);
38 exfat_free_iocharset(sbi
);
39 exfat_free_upcase_table(sbi
);
43 static void exfat_put_super(struct super_block
*sb
)
45 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
47 mutex_lock(&sbi
->s_lock
);
48 if (test_and_clear_bit(EXFAT_SB_DIRTY
, &sbi
->s_state
))
49 sync_blockdev(sb
->s_bdev
);
50 exfat_set_vol_flags(sb
, VOL_CLEAN
);
51 exfat_free_bitmap(sbi
);
53 mutex_unlock(&sbi
->s_lock
);
55 call_rcu(&sbi
->rcu
, exfat_delayed_free
);
58 static int exfat_sync_fs(struct super_block
*sb
, int wait
)
60 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
63 /* If there are some dirty buffers in the bdev inode */
64 mutex_lock(&sbi
->s_lock
);
65 if (test_and_clear_bit(EXFAT_SB_DIRTY
, &sbi
->s_state
)) {
66 sync_blockdev(sb
->s_bdev
);
67 if (exfat_set_vol_flags(sb
, VOL_CLEAN
))
70 mutex_unlock(&sbi
->s_lock
);
74 static int exfat_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
76 struct super_block
*sb
= dentry
->d_sb
;
77 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
78 unsigned long long id
= huge_encode_dev(sb
->s_bdev
->bd_dev
);
80 if (sbi
->used_clusters
== EXFAT_CLUSTERS_UNTRACKED
) {
81 mutex_lock(&sbi
->s_lock
);
82 if (exfat_count_used_clusters(sb
, &sbi
->used_clusters
)) {
83 mutex_unlock(&sbi
->s_lock
);
86 mutex_unlock(&sbi
->s_lock
);
89 buf
->f_type
= sb
->s_magic
;
90 buf
->f_bsize
= sbi
->cluster_size
;
91 buf
->f_blocks
= sbi
->num_clusters
- 2; /* clu 0 & 1 */
92 buf
->f_bfree
= buf
->f_blocks
- sbi
->used_clusters
;
93 buf
->f_bavail
= buf
->f_bfree
;
94 buf
->f_fsid
.val
[0] = (unsigned int)id
;
95 buf
->f_fsid
.val
[1] = (unsigned int)(id
>> 32);
96 /* Unicode utf16 255 characters */
97 buf
->f_namelen
= EXFAT_MAX_FILE_LEN
* NLS_MAX_CHARSET_SIZE
;
101 int exfat_set_vol_flags(struct super_block
*sb
, unsigned short new_flag
)
103 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
104 struct pbr64
*bpb
= (struct pbr64
*)sbi
->pbr_bh
->b_data
;
107 /* flags are not changed */
108 if (sbi
->vol_flag
== new_flag
)
111 sbi
->vol_flag
= new_flag
;
113 /* skip updating volume dirty flag,
114 * if this volume has been mounted with read-only
119 bpb
->bsx
.vol_flags
= cpu_to_le16(new_flag
);
121 if (new_flag
== VOL_DIRTY
&& !buffer_dirty(sbi
->pbr_bh
))
126 set_buffer_uptodate(sbi
->pbr_bh
);
127 mark_buffer_dirty(sbi
->pbr_bh
);
130 sync_dirty_buffer(sbi
->pbr_bh
);
134 static int exfat_show_options(struct seq_file
*m
, struct dentry
*root
)
136 struct super_block
*sb
= root
->d_sb
;
137 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
138 struct exfat_mount_options
*opts
= &sbi
->options
;
140 /* Show partition info */
141 if (!uid_eq(opts
->fs_uid
, GLOBAL_ROOT_UID
))
142 seq_printf(m
, ",uid=%u",
143 from_kuid_munged(&init_user_ns
, opts
->fs_uid
));
144 if (!gid_eq(opts
->fs_gid
, GLOBAL_ROOT_GID
))
145 seq_printf(m
, ",gid=%u",
146 from_kgid_munged(&init_user_ns
, opts
->fs_gid
));
147 seq_printf(m
, ",fmask=%04o,dmask=%04o", opts
->fs_fmask
, opts
->fs_dmask
);
148 if (opts
->allow_utime
)
149 seq_printf(m
, ",allow_utime=%04o", opts
->allow_utime
);
151 seq_puts(m
, ",iocharset=utf8");
152 else if (sbi
->nls_io
)
153 seq_printf(m
, ",iocharset=%s", sbi
->nls_io
->charset
);
154 if (opts
->errors
== EXFAT_ERRORS_CONT
)
155 seq_puts(m
, ",errors=continue");
156 else if (opts
->errors
== EXFAT_ERRORS_PANIC
)
157 seq_puts(m
, ",errors=panic");
159 seq_puts(m
, ",errors=remount-ro");
161 seq_puts(m
, ",discard");
162 if (opts
->time_offset
)
163 seq_printf(m
, ",time_offset=%d", opts
->time_offset
);
167 static struct inode
*exfat_alloc_inode(struct super_block
*sb
)
169 struct exfat_inode_info
*ei
;
171 ei
= kmem_cache_alloc(exfat_inode_cachep
, GFP_NOFS
);
175 init_rwsem(&ei
->truncate_lock
);
176 return &ei
->vfs_inode
;
179 static void exfat_free_inode(struct inode
*inode
)
181 kmem_cache_free(exfat_inode_cachep
, EXFAT_I(inode
));
184 static const struct super_operations exfat_sops
= {
185 .alloc_inode
= exfat_alloc_inode
,
186 .free_inode
= exfat_free_inode
,
187 .write_inode
= exfat_write_inode
,
188 .evict_inode
= exfat_evict_inode
,
189 .put_super
= exfat_put_super
,
190 .sync_fs
= exfat_sync_fs
,
191 .statfs
= exfat_statfs
,
192 .show_options
= exfat_show_options
,
207 /* Deprecated options */
214 static const struct constant_table exfat_param_enums
[] = {
215 { "continue", EXFAT_ERRORS_CONT
},
216 { "panic", EXFAT_ERRORS_PANIC
},
217 { "remount-ro", EXFAT_ERRORS_RO
},
221 static const struct fs_parameter_spec exfat_parameters
[] = {
222 fsparam_u32("uid", Opt_uid
),
223 fsparam_u32("gid", Opt_gid
),
224 fsparam_u32oct("umask", Opt_umask
),
225 fsparam_u32oct("dmask", Opt_dmask
),
226 fsparam_u32oct("fmask", Opt_fmask
),
227 fsparam_u32oct("allow_utime", Opt_allow_utime
),
228 fsparam_string("iocharset", Opt_charset
),
229 fsparam_enum("errors", Opt_errors
, exfat_param_enums
),
230 fsparam_flag("discard", Opt_discard
),
231 fsparam_s32("time_offset", Opt_time_offset
),
232 __fsparam(NULL
, "utf8", Opt_utf8
, fs_param_deprecated
,
234 __fsparam(NULL
, "debug", Opt_debug
, fs_param_deprecated
,
236 __fsparam(fs_param_is_u32
, "namecase", Opt_namecase
,
237 fs_param_deprecated
, NULL
),
238 __fsparam(fs_param_is_u32
, "codepage", Opt_codepage
,
239 fs_param_deprecated
, NULL
),
243 static int exfat_parse_param(struct fs_context
*fc
, struct fs_parameter
*param
)
245 struct exfat_sb_info
*sbi
= fc
->s_fs_info
;
246 struct exfat_mount_options
*opts
= &sbi
->options
;
247 struct fs_parse_result result
;
250 opt
= fs_parse(fc
, exfat_parameters
, param
, &result
);
256 opts
->fs_uid
= make_kuid(current_user_ns(), result
.uint_32
);
259 opts
->fs_gid
= make_kgid(current_user_ns(), result
.uint_32
);
262 opts
->fs_fmask
= result
.uint_32
;
263 opts
->fs_dmask
= result
.uint_32
;
266 opts
->fs_dmask
= result
.uint_32
;
269 opts
->fs_fmask
= result
.uint_32
;
271 case Opt_allow_utime
:
272 opts
->allow_utime
= result
.uint_32
& 0022;
275 exfat_free_iocharset(sbi
);
276 opts
->iocharset
= param
->string
;
277 param
->string
= NULL
;
280 opts
->errors
= result
.uint_32
;
285 case Opt_time_offset
:
287 * Make the limit 24 just in case someone invents something
290 if (result
.int_32
< -24 * 60 || result
.int_32
> 24 * 60)
292 opts
->time_offset
= result
.int_32
;
306 static void exfat_hash_init(struct super_block
*sb
)
308 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
311 spin_lock_init(&sbi
->inode_hash_lock
);
312 for (i
= 0; i
< EXFAT_HASH_SIZE
; i
++)
313 INIT_HLIST_HEAD(&sbi
->inode_hashtable
[i
]);
316 static int exfat_read_root(struct inode
*inode
)
318 struct super_block
*sb
= inode
->i_sb
;
319 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
320 struct exfat_inode_info
*ei
= EXFAT_I(inode
);
321 struct exfat_chain cdir
;
322 int num_subdirs
, num_clu
= 0;
324 exfat_chain_set(&ei
->dir
, sbi
->root_dir
, 0, ALLOC_FAT_CHAIN
);
326 ei
->start_clu
= sbi
->root_dir
;
327 ei
->flags
= ALLOC_FAT_CHAIN
;
331 ei
->hint_bmap
.off
= EXFAT_EOF_CLUSTER
;
332 ei
->hint_stat
.eidx
= 0;
333 ei
->hint_stat
.clu
= sbi
->root_dir
;
334 ei
->hint_femp
.eidx
= EXFAT_HINT_NONE
;
336 exfat_chain_set(&cdir
, sbi
->root_dir
, 0, ALLOC_FAT_CHAIN
);
337 if (exfat_count_num_clusters(sb
, &cdir
, &num_clu
))
339 i_size_write(inode
, num_clu
<< sbi
->cluster_size_bits
);
341 num_subdirs
= exfat_count_dir_entries(sb
, &cdir
);
344 set_nlink(inode
, num_subdirs
+ EXFAT_MIN_SUBDIR
);
346 inode
->i_uid
= sbi
->options
.fs_uid
;
347 inode
->i_gid
= sbi
->options
.fs_gid
;
348 inode_inc_iversion(inode
);
349 inode
->i_generation
= 0;
350 inode
->i_mode
= exfat_make_mode(sbi
, ATTR_SUBDIR
, 0777);
351 inode
->i_op
= &exfat_dir_inode_operations
;
352 inode
->i_fop
= &exfat_dir_operations
;
354 inode
->i_blocks
= ((i_size_read(inode
) + (sbi
->cluster_size
- 1))
355 & ~(sbi
->cluster_size
- 1)) >> inode
->i_blkbits
;
356 EXFAT_I(inode
)->i_pos
= ((loff_t
)sbi
->root_dir
<< 32) | 0xffffffff;
357 EXFAT_I(inode
)->i_size_aligned
= i_size_read(inode
);
358 EXFAT_I(inode
)->i_size_ondisk
= i_size_read(inode
);
360 exfat_save_attr(inode
, ATTR_SUBDIR
);
361 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= ei
->i_crtime
=
363 exfat_truncate_atime(&inode
->i_atime
);
364 exfat_cache_init_inode(inode
);
368 static struct pbr
*exfat_read_pbr_with_logical_sector(struct super_block
*sb
)
370 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
371 struct pbr
*p_pbr
= (struct pbr
*) (sbi
->pbr_bh
)->b_data
;
372 unsigned short logical_sect
= 0;
374 logical_sect
= 1 << p_pbr
->bsx
.f64
.sect_size_bits
;
376 if (!is_power_of_2(logical_sect
) ||
377 logical_sect
< 512 || logical_sect
> 4096) {
378 exfat_msg(sb
, KERN_ERR
, "bogus logical sector size %u",
383 if (logical_sect
< sb
->s_blocksize
) {
384 exfat_msg(sb
, KERN_ERR
,
385 "logical sector size too small for device (logical sector size = %u)",
390 if (logical_sect
> sb
->s_blocksize
) {
394 if (!sb_set_blocksize(sb
, logical_sect
)) {
395 exfat_msg(sb
, KERN_ERR
,
396 "unable to set blocksize %u", logical_sect
);
399 sbi
->pbr_bh
= sb_bread(sb
, 0);
401 exfat_msg(sb
, KERN_ERR
,
402 "unable to read boot sector (logical sector size = %lu)",
407 p_pbr
= (struct pbr
*)sbi
->pbr_bh
->b_data
;
412 /* mount the file system volume */
413 static int __exfat_fill_super(struct super_block
*sb
)
418 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
420 /* set block size to read super block */
421 sb_min_blocksize(sb
, 512);
423 /* read boot sector */
424 sbi
->pbr_bh
= sb_bread(sb
, 0);
426 exfat_msg(sb
, KERN_ERR
, "unable to read boot sector");
431 p_pbr
= (struct pbr
*)sbi
->pbr_bh
->b_data
;
433 /* check the validity of PBR */
434 if (le16_to_cpu((p_pbr
->signature
)) != PBR_SIGNATURE
) {
435 exfat_msg(sb
, KERN_ERR
, "invalid boot record signature");
441 /* check logical sector size */
442 p_pbr
= exfat_read_pbr_with_logical_sector(sb
);
449 * res_zero field must be filled with zero to prevent mounting
452 if (memchr_inv(p_pbr
->bpb
.f64
.res_zero
, 0,
453 sizeof(p_pbr
->bpb
.f64
.res_zero
))) {
458 p_bpb
= (struct pbr64
*)p_pbr
;
459 if (!p_bpb
->bsx
.num_fats
) {
460 exfat_msg(sb
, KERN_ERR
, "bogus number of FAT structure");
465 sbi
->sect_per_clus
= 1 << p_bpb
->bsx
.sect_per_clus_bits
;
466 sbi
->sect_per_clus_bits
= p_bpb
->bsx
.sect_per_clus_bits
;
467 sbi
->cluster_size_bits
= sbi
->sect_per_clus_bits
+ sb
->s_blocksize_bits
;
468 sbi
->cluster_size
= 1 << sbi
->cluster_size_bits
;
469 sbi
->num_FAT_sectors
= le32_to_cpu(p_bpb
->bsx
.fat_length
);
470 sbi
->FAT1_start_sector
= le32_to_cpu(p_bpb
->bsx
.fat_offset
);
471 sbi
->FAT2_start_sector
= p_bpb
->bsx
.num_fats
== 1 ?
472 sbi
->FAT1_start_sector
:
473 sbi
->FAT1_start_sector
+ sbi
->num_FAT_sectors
;
474 sbi
->data_start_sector
= le32_to_cpu(p_bpb
->bsx
.clu_offset
);
475 sbi
->num_sectors
= le64_to_cpu(p_bpb
->bsx
.vol_length
);
476 /* because the cluster index starts with 2 */
477 sbi
->num_clusters
= le32_to_cpu(p_bpb
->bsx
.clu_count
) +
478 EXFAT_RESERVED_CLUSTERS
;
480 sbi
->root_dir
= le32_to_cpu(p_bpb
->bsx
.root_cluster
);
481 sbi
->dentries_per_clu
= 1 <<
482 (sbi
->cluster_size_bits
- DENTRY_SIZE_BITS
);
484 sbi
->vol_flag
= le16_to_cpu(p_bpb
->bsx
.vol_flags
);
485 sbi
->clu_srch_ptr
= EXFAT_FIRST_CLUSTER
;
486 sbi
->used_clusters
= EXFAT_CLUSTERS_UNTRACKED
;
488 if (le16_to_cpu(p_bpb
->bsx
.vol_flags
) & VOL_DIRTY
) {
489 sbi
->vol_flag
|= VOL_DIRTY
;
490 exfat_msg(sb
, KERN_WARNING
,
491 "Volume was not properly unmounted. Some data may be corrupt. Please run fsck.");
494 /* exFAT file size is limited by a disk volume size */
495 sb
->s_maxbytes
= (u64
)(sbi
->num_clusters
- EXFAT_RESERVED_CLUSTERS
) <<
496 sbi
->cluster_size_bits
;
498 ret
= exfat_create_upcase_table(sb
);
500 exfat_msg(sb
, KERN_ERR
, "failed to load upcase table");
504 ret
= exfat_load_bitmap(sb
);
506 exfat_msg(sb
, KERN_ERR
, "failed to load alloc-bitmap");
507 goto free_upcase_table
;
510 ret
= exfat_count_used_clusters(sb
, &sbi
->used_clusters
);
512 exfat_msg(sb
, KERN_ERR
, "failed to scan clusters");
513 goto free_alloc_bitmap
;
519 exfat_free_bitmap(sbi
);
521 exfat_free_upcase_table(sbi
);
527 static int exfat_fill_super(struct super_block
*sb
, struct fs_context
*fc
)
529 struct exfat_sb_info
*sbi
= sb
->s_fs_info
;
530 struct exfat_mount_options
*opts
= &sbi
->options
;
531 struct inode
*root_inode
;
534 if (opts
->allow_utime
== (unsigned short)-1)
535 opts
->allow_utime
= ~opts
->fs_dmask
& 0022;
538 struct request_queue
*q
= bdev_get_queue(sb
->s_bdev
);
540 if (!blk_queue_discard(q
)) {
541 exfat_msg(sb
, KERN_WARNING
,
542 "mounting with \"discard\" option, but the device does not support discard");
547 sb
->s_flags
|= SB_NODIRATIME
;
548 sb
->s_magic
= EXFAT_SUPER_MAGIC
;
549 sb
->s_op
= &exfat_sops
;
551 sb
->s_time_gran
= 10 * NSEC_PER_MSEC
;
552 sb
->s_time_min
= EXFAT_MIN_TIMESTAMP_SECS
;
553 sb
->s_time_max
= EXFAT_MAX_TIMESTAMP_SECS
;
555 err
= __exfat_fill_super(sb
);
557 exfat_msg(sb
, KERN_ERR
, "failed to recognize exfat type");
561 /* set up enough so that it can read an inode */
564 if (!strcmp(sbi
->options
.iocharset
, "utf8"))
567 sbi
->nls_io
= load_nls(sbi
->options
.iocharset
);
569 exfat_msg(sb
, KERN_ERR
, "IO charset %s not found",
570 sbi
->options
.iocharset
);
576 if (sbi
->options
.utf8
)
577 sb
->s_d_op
= &exfat_utf8_dentry_ops
;
579 sb
->s_d_op
= &exfat_dentry_ops
;
581 root_inode
= new_inode(sb
);
583 exfat_msg(sb
, KERN_ERR
, "failed to allocate root inode.");
588 root_inode
->i_ino
= EXFAT_ROOT_INO
;
589 inode_set_iversion(root_inode
, 1);
590 err
= exfat_read_root(root_inode
);
592 exfat_msg(sb
, KERN_ERR
, "failed to initialize root inode.");
596 exfat_hash_inode(root_inode
, EXFAT_I(root_inode
)->i_pos
);
597 insert_inode_hash(root_inode
);
599 sb
->s_root
= d_make_root(root_inode
);
601 exfat_msg(sb
, KERN_ERR
, "failed to get the root dentry");
613 exfat_free_upcase_table(sbi
);
614 exfat_free_bitmap(sbi
);
618 unload_nls(sbi
->nls_io
);
619 exfat_free_iocharset(sbi
);
620 sb
->s_fs_info
= NULL
;
625 static int exfat_get_tree(struct fs_context
*fc
)
627 return get_tree_bdev(fc
, exfat_fill_super
);
630 static void exfat_free(struct fs_context
*fc
)
632 struct exfat_sb_info
*sbi
= fc
->s_fs_info
;
635 exfat_free_iocharset(sbi
);
640 static const struct fs_context_operations exfat_context_ops
= {
641 .parse_param
= exfat_parse_param
,
642 .get_tree
= exfat_get_tree
,
646 static int exfat_init_fs_context(struct fs_context
*fc
)
648 struct exfat_sb_info
*sbi
;
650 sbi
= kzalloc(sizeof(struct exfat_sb_info
), GFP_KERNEL
);
654 mutex_init(&sbi
->s_lock
);
655 ratelimit_state_init(&sbi
->ratelimit
, DEFAULT_RATELIMIT_INTERVAL
,
656 DEFAULT_RATELIMIT_BURST
);
658 sbi
->options
.fs_uid
= current_uid();
659 sbi
->options
.fs_gid
= current_gid();
660 sbi
->options
.fs_fmask
= current
->fs
->umask
;
661 sbi
->options
.fs_dmask
= current
->fs
->umask
;
662 sbi
->options
.allow_utime
= -1;
663 sbi
->options
.iocharset
= exfat_default_iocharset
;
664 sbi
->options
.errors
= EXFAT_ERRORS_RO
;
667 fc
->ops
= &exfat_context_ops
;
671 static struct file_system_type exfat_fs_type
= {
672 .owner
= THIS_MODULE
,
674 .init_fs_context
= exfat_init_fs_context
,
675 .parameters
= exfat_parameters
,
676 .kill_sb
= kill_block_super
,
677 .fs_flags
= FS_REQUIRES_DEV
,
680 static void exfat_inode_init_once(void *foo
)
682 struct exfat_inode_info
*ei
= (struct exfat_inode_info
*)foo
;
684 INIT_HLIST_NODE(&ei
->i_hash_fat
);
685 inode_init_once(&ei
->vfs_inode
);
688 static int __init
init_exfat_fs(void)
692 err
= exfat_cache_init();
696 exfat_inode_cachep
= kmem_cache_create("exfat_inode_cache",
697 sizeof(struct exfat_inode_info
),
698 0, SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
,
699 exfat_inode_init_once
);
700 if (!exfat_inode_cachep
) {
705 err
= register_filesystem(&exfat_fs_type
);
712 kmem_cache_destroy(exfat_inode_cachep
);
714 exfat_cache_shutdown();
718 static void __exit
exit_exfat_fs(void)
721 * Make sure all delayed rcu free inodes are flushed before we
725 kmem_cache_destroy(exfat_inode_cachep
);
726 unregister_filesystem(&exfat_fs_type
);
727 exfat_cache_shutdown();
730 module_init(init_exfat_fs
);
731 module_exit(exit_exfat_fs
);
733 MODULE_ALIAS_FS("exfat");
734 MODULE_LICENSE("GPL");
735 MODULE_DESCRIPTION("exFAT filesystem support");
736 MODULE_AUTHOR("Samsung Electronics Co., Ltd.");