blob: 8c9fb7dcec16a0938253f81e17c7bdb3d0363f67 [file] [log] [blame]
Namjae Jeon719c1e12020-03-02 15:21:33 +09001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
4 */
5
6#include <linux/fs_context.h>
7#include <linux/fs_parser.h>
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/time.h>
11#include <linux/mount.h>
12#include <linux/cred.h>
13#include <linux/statfs.h>
14#include <linux/seq_file.h>
15#include <linux/blkdev.h>
16#include <linux/fs_struct.h>
17#include <linux/iversion.h>
18#include <linux/nls.h>
19#include <linux/buffer_head.h>
Namjae Jeon1ed147e2021-11-25 21:01:11 +090020#include <linux/magic.h>
Namjae Jeon719c1e12020-03-02 15:21:33 +090021
22#include "exfat_raw.h"
23#include "exfat_fs.h"
24
25static char exfat_default_iocharset[] = CONFIG_EXFAT_DEFAULT_IOCHARSET;
26static struct kmem_cache *exfat_inode_cachep;
27
28static void exfat_free_iocharset(struct exfat_sb_info *sbi)
29{
30 if (sbi->options.iocharset != exfat_default_iocharset)
31 kfree(sbi->options.iocharset);
32}
33
34static void exfat_delayed_free(struct rcu_head *p)
35{
36 struct exfat_sb_info *sbi = container_of(p, struct exfat_sb_info, rcu);
37
38 unload_nls(sbi->nls_io);
39 exfat_free_iocharset(sbi);
40 exfat_free_upcase_table(sbi);
41 kfree(sbi);
42}
43
44static void exfat_put_super(struct super_block *sb)
45{
46 struct exfat_sb_info *sbi = EXFAT_SB(sb);
47
48 mutex_lock(&sbi->s_lock);
Namjae Jeon719c1e12020-03-02 15:21:33 +090049 exfat_free_bitmap(sbi);
Tetsuhiro Kohada181a9e82020-05-29 19:14:56 +090050 brelse(sbi->boot_bh);
Namjae Jeon719c1e12020-03-02 15:21:33 +090051 mutex_unlock(&sbi->s_lock);
52
53 call_rcu(&sbi->rcu, exfat_delayed_free);
54}
55
56static int exfat_sync_fs(struct super_block *sb, int wait)
57{
58 struct exfat_sb_info *sbi = EXFAT_SB(sb);
59 int err = 0;
60
Tetsuhiro Kohada2c7f8932020-06-16 11:18:07 +090061 if (!wait)
62 return 0;
63
Namjae Jeon719c1e12020-03-02 15:21:33 +090064 /* If there are some dirty buffers in the bdev inode */
65 mutex_lock(&sbi->s_lock);
Tetsuhiro Kohada2c7f8932020-06-16 11:18:07 +090066 sync_blockdev(sb->s_bdev);
Tetsuhiro Kohada7018ec62020-07-31 14:58:26 +090067 if (exfat_clear_volume_dirty(sb))
Tetsuhiro Kohada2c7f8932020-06-16 11:18:07 +090068 err = -EIO;
Namjae Jeon719c1e12020-03-02 15:21:33 +090069 mutex_unlock(&sbi->s_lock);
70 return err;
71}
72
73static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf)
74{
75 struct super_block *sb = dentry->d_sb;
76 struct exfat_sb_info *sbi = EXFAT_SB(sb);
77 unsigned long long id = huge_encode_dev(sb->s_bdev->bd_dev);
78
79 if (sbi->used_clusters == EXFAT_CLUSTERS_UNTRACKED) {
80 mutex_lock(&sbi->s_lock);
81 if (exfat_count_used_clusters(sb, &sbi->used_clusters)) {
82 mutex_unlock(&sbi->s_lock);
83 return -EIO;
84 }
85 mutex_unlock(&sbi->s_lock);
86 }
87
88 buf->f_type = sb->s_magic;
89 buf->f_bsize = sbi->cluster_size;
90 buf->f_blocks = sbi->num_clusters - 2; /* clu 0 & 1 */
91 buf->f_bfree = buf->f_blocks - sbi->used_clusters;
92 buf->f_bavail = buf->f_bfree;
Al Viro6d1349c2020-09-18 16:45:50 -040093 buf->f_fsid = u64_to_fsid(id);
Namjae Jeon719c1e12020-03-02 15:21:33 +090094 /* Unicode utf16 255 characters */
95 buf->f_namelen = EXFAT_MAX_FILE_LEN * NLS_MAX_CHARSET_SIZE;
96 return 0;
97}
98
Tetsuhiro Kohada7018ec62020-07-31 14:58:26 +090099static int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flags)
Namjae Jeon719c1e12020-03-02 15:21:33 +0900100{
101 struct exfat_sb_info *sbi = EXFAT_SB(sb);
Tetsuhiro Kohada181a9e82020-05-29 19:14:56 +0900102 struct boot_sector *p_boot = (struct boot_sector *)sbi->boot_bh->b_data;
Jason Yancdc06122020-04-16 13:34:26 +0900103 bool sync;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900104
Tetsuhiro Kohada7018ec62020-07-31 14:58:26 +0900105 /* retain persistent-flags */
106 new_flags |= sbi->vol_flags_persistent;
107
Namjae Jeon719c1e12020-03-02 15:21:33 +0900108 /* flags are not changed */
Tetsuhiro Kohada7018ec62020-07-31 14:58:26 +0900109 if (sbi->vol_flags == new_flags)
Namjae Jeon719c1e12020-03-02 15:21:33 +0900110 return 0;
111
Tetsuhiro Kohada7018ec62020-07-31 14:58:26 +0900112 sbi->vol_flags = new_flags;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900113
114 /* skip updating volume dirty flag,
115 * if this volume has been mounted with read-only
116 */
117 if (sb_rdonly(sb))
118 return 0;
119
Tetsuhiro Kohada7018ec62020-07-31 14:58:26 +0900120 p_boot->vol_flags = cpu_to_le16(new_flags);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900121
Tetsuhiro Kohada7018ec62020-07-31 14:58:26 +0900122 if ((new_flags & VOLUME_DIRTY) && !buffer_dirty(sbi->boot_bh))
Namjae Jeon719c1e12020-03-02 15:21:33 +0900123 sync = true;
124 else
125 sync = false;
126
Tetsuhiro Kohada181a9e82020-05-29 19:14:56 +0900127 set_buffer_uptodate(sbi->boot_bh);
128 mark_buffer_dirty(sbi->boot_bh);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900129
130 if (sync)
Tetsuhiro Kohada181a9e82020-05-29 19:14:56 +0900131 sync_dirty_buffer(sbi->boot_bh);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900132 return 0;
133}
134
Tetsuhiro Kohada7018ec62020-07-31 14:58:26 +0900135int exfat_set_volume_dirty(struct super_block *sb)
136{
137 struct exfat_sb_info *sbi = EXFAT_SB(sb);
138
139 return exfat_set_vol_flags(sb, sbi->vol_flags | VOLUME_DIRTY);
140}
141
142int exfat_clear_volume_dirty(struct super_block *sb)
143{
144 struct exfat_sb_info *sbi = EXFAT_SB(sb);
145
146 return exfat_set_vol_flags(sb, sbi->vol_flags & ~VOLUME_DIRTY);
147}
148
Namjae Jeon719c1e12020-03-02 15:21:33 +0900149static int exfat_show_options(struct seq_file *m, struct dentry *root)
150{
151 struct super_block *sb = root->d_sb;
152 struct exfat_sb_info *sbi = EXFAT_SB(sb);
153 struct exfat_mount_options *opts = &sbi->options;
154
155 /* Show partition info */
156 if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID))
157 seq_printf(m, ",uid=%u",
158 from_kuid_munged(&init_user_ns, opts->fs_uid));
159 if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID))
160 seq_printf(m, ",gid=%u",
161 from_kgid_munged(&init_user_ns, opts->fs_gid));
162 seq_printf(m, ",fmask=%04o,dmask=%04o", opts->fs_fmask, opts->fs_dmask);
163 if (opts->allow_utime)
164 seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
165 if (opts->utf8)
166 seq_puts(m, ",iocharset=utf8");
167 else if (sbi->nls_io)
168 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900169 if (opts->errors == EXFAT_ERRORS_CONT)
170 seq_puts(m, ",errors=continue");
171 else if (opts->errors == EXFAT_ERRORS_PANIC)
172 seq_puts(m, ",errors=panic");
173 else
174 seq_puts(m, ",errors=remount-ro");
175 if (opts->discard)
176 seq_puts(m, ",discard");
177 if (opts->time_offset)
178 seq_printf(m, ",time_offset=%d", opts->time_offset);
179 return 0;
180}
181
182static struct inode *exfat_alloc_inode(struct super_block *sb)
183{
184 struct exfat_inode_info *ei;
185
186 ei = kmem_cache_alloc(exfat_inode_cachep, GFP_NOFS);
187 if (!ei)
188 return NULL;
189
190 init_rwsem(&ei->truncate_lock);
191 return &ei->vfs_inode;
192}
193
194static void exfat_free_inode(struct inode *inode)
195{
196 kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode));
197}
198
199static const struct super_operations exfat_sops = {
200 .alloc_inode = exfat_alloc_inode,
201 .free_inode = exfat_free_inode,
202 .write_inode = exfat_write_inode,
203 .evict_inode = exfat_evict_inode,
204 .put_super = exfat_put_super,
205 .sync_fs = exfat_sync_fs,
206 .statfs = exfat_statfs,
207 .show_options = exfat_show_options,
208};
209
210enum {
211 Opt_uid,
212 Opt_gid,
213 Opt_umask,
214 Opt_dmask,
215 Opt_fmask,
216 Opt_allow_utime,
217 Opt_charset,
218 Opt_errors,
219 Opt_discard,
220 Opt_time_offset,
Namjae Jeon907fa892020-05-22 08:10:10 +0900221
222 /* Deprecated options */
223 Opt_utf8,
224 Opt_debug,
225 Opt_namecase,
226 Opt_codepage,
Namjae Jeon719c1e12020-03-02 15:21:33 +0900227};
228
Valdis Kletnieks9acd0d52020-03-02 15:21:45 +0900229static const struct constant_table exfat_param_enums[] = {
230 { "continue", EXFAT_ERRORS_CONT },
231 { "panic", EXFAT_ERRORS_PANIC },
232 { "remount-ro", EXFAT_ERRORS_RO },
233 {}
234};
235
236static const struct fs_parameter_spec exfat_parameters[] = {
Namjae Jeon719c1e12020-03-02 15:21:33 +0900237 fsparam_u32("uid", Opt_uid),
238 fsparam_u32("gid", Opt_gid),
239 fsparam_u32oct("umask", Opt_umask),
240 fsparam_u32oct("dmask", Opt_dmask),
241 fsparam_u32oct("fmask", Opt_fmask),
242 fsparam_u32oct("allow_utime", Opt_allow_utime),
243 fsparam_string("iocharset", Opt_charset),
Valdis Kletnieks9acd0d52020-03-02 15:21:45 +0900244 fsparam_enum("errors", Opt_errors, exfat_param_enums),
Namjae Jeon719c1e12020-03-02 15:21:33 +0900245 fsparam_flag("discard", Opt_discard),
246 fsparam_s32("time_offset", Opt_time_offset),
Namjae Jeon907fa892020-05-22 08:10:10 +0900247 __fsparam(NULL, "utf8", Opt_utf8, fs_param_deprecated,
248 NULL),
249 __fsparam(NULL, "debug", Opt_debug, fs_param_deprecated,
250 NULL),
251 __fsparam(fs_param_is_u32, "namecase", Opt_namecase,
252 fs_param_deprecated, NULL),
253 __fsparam(fs_param_is_u32, "codepage", Opt_codepage,
254 fs_param_deprecated, NULL),
Namjae Jeon719c1e12020-03-02 15:21:33 +0900255 {}
256};
257
Namjae Jeon719c1e12020-03-02 15:21:33 +0900258static int exfat_parse_param(struct fs_context *fc, struct fs_parameter *param)
259{
260 struct exfat_sb_info *sbi = fc->s_fs_info;
261 struct exfat_mount_options *opts = &sbi->options;
262 struct fs_parse_result result;
263 int opt;
264
Valdis Kletnieks9acd0d52020-03-02 15:21:45 +0900265 opt = fs_parse(fc, exfat_parameters, param, &result);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900266 if (opt < 0)
267 return opt;
268
269 switch (opt) {
270 case Opt_uid:
271 opts->fs_uid = make_kuid(current_user_ns(), result.uint_32);
272 break;
273 case Opt_gid:
274 opts->fs_gid = make_kgid(current_user_ns(), result.uint_32);
275 break;
276 case Opt_umask:
277 opts->fs_fmask = result.uint_32;
278 opts->fs_dmask = result.uint_32;
279 break;
280 case Opt_dmask:
281 opts->fs_dmask = result.uint_32;
282 break;
283 case Opt_fmask:
284 opts->fs_fmask = result.uint_32;
285 break;
286 case Opt_allow_utime:
287 opts->allow_utime = result.uint_32 & 0022;
288 break;
289 case Opt_charset:
290 exfat_free_iocharset(sbi);
Al Virof341a7d2020-06-03 09:48:36 +0900291 opts->iocharset = param->string;
292 param->string = NULL;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900293 break;
294 case Opt_errors:
295 opts->errors = result.uint_32;
296 break;
297 case Opt_discard:
298 opts->discard = 1;
299 break;
300 case Opt_time_offset:
301 /*
302 * Make the limit 24 just in case someone invents something
303 * unusual.
304 */
305 if (result.int_32 < -24 * 60 || result.int_32 > 24 * 60)
306 return -EINVAL;
307 opts->time_offset = result.int_32;
308 break;
Namjae Jeon907fa892020-05-22 08:10:10 +0900309 case Opt_utf8:
310 case Opt_debug:
311 case Opt_namecase:
312 case Opt_codepage:
313 break;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900314 default:
315 return -EINVAL;
316 }
317
318 return 0;
319}
320
321static void exfat_hash_init(struct super_block *sb)
322{
323 struct exfat_sb_info *sbi = EXFAT_SB(sb);
324 int i;
325
326 spin_lock_init(&sbi->inode_hash_lock);
327 for (i = 0; i < EXFAT_HASH_SIZE; i++)
328 INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
329}
330
331static int exfat_read_root(struct inode *inode)
332{
333 struct super_block *sb = inode->i_sb;
334 struct exfat_sb_info *sbi = EXFAT_SB(sb);
335 struct exfat_inode_info *ei = EXFAT_I(inode);
336 struct exfat_chain cdir;
337 int num_subdirs, num_clu = 0;
338
339 exfat_chain_set(&ei->dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
340 ei->entry = -1;
341 ei->start_clu = sbi->root_dir;
342 ei->flags = ALLOC_FAT_CHAIN;
343 ei->type = TYPE_DIR;
344 ei->version = 0;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900345 ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
346 ei->hint_stat.eidx = 0;
347 ei->hint_stat.clu = sbi->root_dir;
348 ei->hint_femp.eidx = EXFAT_HINT_NONE;
349
350 exfat_chain_set(&cdir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
351 if (exfat_count_num_clusters(sb, &cdir, &num_clu))
352 return -EIO;
353 i_size_write(inode, num_clu << sbi->cluster_size_bits);
354
355 num_subdirs = exfat_count_dir_entries(sb, &cdir);
356 if (num_subdirs < 0)
357 return -EIO;
358 set_nlink(inode, num_subdirs + EXFAT_MIN_SUBDIR);
359
360 inode->i_uid = sbi->options.fs_uid;
361 inode->i_gid = sbi->options.fs_gid;
362 inode_inc_iversion(inode);
363 inode->i_generation = 0;
364 inode->i_mode = exfat_make_mode(sbi, ATTR_SUBDIR, 0777);
365 inode->i_op = &exfat_dir_inode_operations;
366 inode->i_fop = &exfat_dir_operations;
367
Christophe Vu-Brugier92fba082021-11-22 22:02:37 +0900368 inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >>
369 inode->i_blkbits;
Christophe Vu-Brugier7dee6f572021-11-02 22:23:58 +0100370 ei->i_pos = ((loff_t)sbi->root_dir << 32) | 0xffffffff;
371 ei->i_size_aligned = i_size_read(inode);
372 ei->i_size_ondisk = i_size_read(inode);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900373
374 exfat_save_attr(inode, ATTR_SUBDIR);
375 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
376 current_time(inode);
Eric Sandeen81df1ad2020-04-21 11:13:10 +0900377 exfat_truncate_atime(&inode->i_atime);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900378 return 0;
379}
380
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900381static int exfat_calibrate_blocksize(struct super_block *sb, int logical_sect)
Namjae Jeon719c1e12020-03-02 15:21:33 +0900382{
Tetsuhiro Kohadab0516832020-04-21 10:58:58 +0900383 struct exfat_sb_info *sbi = EXFAT_SB(sb);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900384
Namjae Jeon78c276f2021-02-01 09:23:37 +0900385 if (!is_power_of_2(logical_sect)) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900386 exfat_err(sb, "bogus logical sector size %u", logical_sect);
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900387 return -EIO;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900388 }
389
390 if (logical_sect < sb->s_blocksize) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900391 exfat_err(sb, "logical sector size too small for device (logical sector size = %u)",
392 logical_sect);
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900393 return -EIO;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900394 }
395
396 if (logical_sect > sb->s_blocksize) {
Tetsuhiro Kohada181a9e82020-05-29 19:14:56 +0900397 brelse(sbi->boot_bh);
398 sbi->boot_bh = NULL;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900399
400 if (!sb_set_blocksize(sb, logical_sect)) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900401 exfat_err(sb, "unable to set blocksize %u",
402 logical_sect);
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900403 return -EIO;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900404 }
Tetsuhiro Kohada181a9e82020-05-29 19:14:56 +0900405 sbi->boot_bh = sb_bread(sb, 0);
406 if (!sbi->boot_bh) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900407 exfat_err(sb, "unable to read boot sector (logical sector size = %lu)",
408 sb->s_blocksize);
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900409 return -EIO;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900410 }
Namjae Jeon719c1e12020-03-02 15:21:33 +0900411 }
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900412 return 0;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900413}
414
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900415static int exfat_read_boot_sector(struct super_block *sb)
Namjae Jeon719c1e12020-03-02 15:21:33 +0900416{
Tetsuhiro Kohada181a9e82020-05-29 19:14:56 +0900417 struct boot_sector *p_boot;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900418 struct exfat_sb_info *sbi = EXFAT_SB(sb);
419
420 /* set block size to read super block */
421 sb_min_blocksize(sb, 512);
422
423 /* read boot sector */
Tetsuhiro Kohada181a9e82020-05-29 19:14:56 +0900424 sbi->boot_bh = sb_bread(sb, 0);
425 if (!sbi->boot_bh) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900426 exfat_err(sb, "unable to read boot sector");
Namjae Jeon719c1e12020-03-02 15:21:33 +0900427 return -EIO;
428 }
Tetsuhiro Kohada181a9e82020-05-29 19:14:56 +0900429 p_boot = (struct boot_sector *)sbi->boot_bh->b_data;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900430
Tetsuhiro Kohada181a9e82020-05-29 19:14:56 +0900431 /* check the validity of BOOT */
432 if (le16_to_cpu((p_boot->signature)) != BOOT_SIGNATURE) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900433 exfat_err(sb, "invalid boot record signature");
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900434 return -EINVAL;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900435 }
436
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900437 if (memcmp(p_boot->fs_name, STR_EXFAT, BOOTSEC_FS_NAME_LEN)) {
438 exfat_err(sb, "invalid fs_name"); /* fs_name may unprintable */
439 return -EINVAL;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900440 }
441
442 /*
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900443 * must_be_zero field must be filled with zero to prevent mounting
Namjae Jeon719c1e12020-03-02 15:21:33 +0900444 * from FAT volume.
445 */
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900446 if (memchr_inv(p_boot->must_be_zero, 0, sizeof(p_boot->must_be_zero)))
447 return -EINVAL;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900448
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900449 if (p_boot->num_fats != 1 && p_boot->num_fats != 2) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900450 exfat_err(sb, "bogus number of FAT structure");
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900451 return -EINVAL;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900452 }
453
Namjae Jeon78c276f2021-02-01 09:23:37 +0900454 /*
455 * sect_size_bits could be at least 9 and at most 12.
456 */
457 if (p_boot->sect_size_bits < EXFAT_MIN_SECT_SIZE_BITS ||
458 p_boot->sect_size_bits > EXFAT_MAX_SECT_SIZE_BITS) {
459 exfat_err(sb, "bogus sector size bits : %u\n",
460 p_boot->sect_size_bits);
461 return -EINVAL;
462 }
463
464 /*
465 * sect_per_clus_bits could be at least 0 and at most 25 - sect_size_bits.
466 */
467 if (p_boot->sect_per_clus_bits > EXFAT_MAX_SECT_PER_CLUS_BITS(p_boot)) {
468 exfat_err(sb, "bogus sectors bits per cluster : %u\n",
469 p_boot->sect_per_clus_bits);
470 return -EINVAL;
471 }
472
Tetsuhiro Kohada181a9e82020-05-29 19:14:56 +0900473 sbi->sect_per_clus = 1 << p_boot->sect_per_clus_bits;
474 sbi->sect_per_clus_bits = p_boot->sect_per_clus_bits;
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900475 sbi->cluster_size_bits = p_boot->sect_per_clus_bits +
476 p_boot->sect_size_bits;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900477 sbi->cluster_size = 1 << sbi->cluster_size_bits;
Tetsuhiro Kohada181a9e82020-05-29 19:14:56 +0900478 sbi->num_FAT_sectors = le32_to_cpu(p_boot->fat_length);
479 sbi->FAT1_start_sector = le32_to_cpu(p_boot->fat_offset);
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900480 sbi->FAT2_start_sector = le32_to_cpu(p_boot->fat_offset);
481 if (p_boot->num_fats == 2)
482 sbi->FAT2_start_sector += sbi->num_FAT_sectors;
Tetsuhiro Kohada181a9e82020-05-29 19:14:56 +0900483 sbi->data_start_sector = le32_to_cpu(p_boot->clu_offset);
484 sbi->num_sectors = le64_to_cpu(p_boot->vol_length);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900485 /* because the cluster index starts with 2 */
Tetsuhiro Kohada181a9e82020-05-29 19:14:56 +0900486 sbi->num_clusters = le32_to_cpu(p_boot->clu_count) +
Namjae Jeon719c1e12020-03-02 15:21:33 +0900487 EXFAT_RESERVED_CLUSTERS;
488
Tetsuhiro Kohada181a9e82020-05-29 19:14:56 +0900489 sbi->root_dir = le32_to_cpu(p_boot->root_cluster);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900490 sbi->dentries_per_clu = 1 <<
491 (sbi->cluster_size_bits - DENTRY_SIZE_BITS);
492
Tetsuhiro Kohada7018ec62020-07-31 14:58:26 +0900493 sbi->vol_flags = le16_to_cpu(p_boot->vol_flags);
494 sbi->vol_flags_persistent = sbi->vol_flags & (VOLUME_DIRTY | MEDIA_FAILURE);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900495 sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
496 sbi->used_clusters = EXFAT_CLUSTERS_UNTRACKED;
497
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900498 /* check consistencies */
Namjae Jeon78c276f2021-02-01 09:23:37 +0900499 if ((u64)sbi->num_FAT_sectors << p_boot->sect_size_bits <
500 (u64)sbi->num_clusters * 4) {
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900501 exfat_err(sb, "bogus fat length");
502 return -EINVAL;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900503 }
Namjae Jeon78c276f2021-02-01 09:23:37 +0900504
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900505 if (sbi->data_start_sector <
Namjae Jeon78c276f2021-02-01 09:23:37 +0900506 (u64)sbi->FAT1_start_sector +
507 (u64)sbi->num_FAT_sectors * p_boot->num_fats) {
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900508 exfat_err(sb, "bogus data start sector");
509 return -EINVAL;
510 }
Namjae Jeon78c276f2021-02-01 09:23:37 +0900511
Tetsuhiro Kohada7018ec62020-07-31 14:58:26 +0900512 if (sbi->vol_flags & VOLUME_DIRTY)
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900513 exfat_warn(sb, "Volume was not properly unmounted. Some data may be corrupt. Please run fsck.");
Tetsuhiro Kohada7018ec62020-07-31 14:58:26 +0900514 if (sbi->vol_flags & MEDIA_FAILURE)
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900515 exfat_warn(sb, "Medium has reported failures. Some data may be lost.");
Namjae Jeon719c1e12020-03-02 15:21:33 +0900516
517 /* exFAT file size is limited by a disk volume size */
518 sb->s_maxbytes = (u64)(sbi->num_clusters - EXFAT_RESERVED_CLUSTERS) <<
519 sbi->cluster_size_bits;
520
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900521 /* check logical sector size */
522 if (exfat_calibrate_blocksize(sb, 1 << p_boot->sect_size_bits))
523 return -EIO;
524
525 return 0;
526}
527
Tetsuhiro Kohada476189c2020-05-31 18:30:17 +0900528static int exfat_verify_boot_region(struct super_block *sb)
529{
530 struct buffer_head *bh = NULL;
531 u32 chksum = 0;
532 __le32 *p_sig, *p_chksum;
533 int sn, i;
534
535 /* read boot sector sub-regions */
536 for (sn = 0; sn < 11; sn++) {
537 bh = sb_bread(sb, sn);
538 if (!bh)
539 return -EIO;
540
541 if (sn != 0 && sn <= 8) {
542 /* extended boot sector sub-regions */
543 p_sig = (__le32 *)&bh->b_data[sb->s_blocksize - 4];
544 if (le32_to_cpu(*p_sig) != EXBOOT_SIGNATURE)
545 exfat_warn(sb, "Invalid exboot-signature(sector = %d): 0x%08x",
546 sn, le32_to_cpu(*p_sig));
547 }
548
549 chksum = exfat_calc_chksum32(bh->b_data, sb->s_blocksize,
550 chksum, sn ? CS_DEFAULT : CS_BOOT_SECTOR);
551 brelse(bh);
552 }
553
554 /* boot checksum sub-regions */
555 bh = sb_bread(sb, sn);
556 if (!bh)
557 return -EIO;
558
559 for (i = 0; i < sb->s_blocksize; i += sizeof(u32)) {
560 p_chksum = (__le32 *)&bh->b_data[i];
561 if (le32_to_cpu(*p_chksum) != chksum) {
562 exfat_err(sb, "Invalid boot checksum (boot checksum : 0x%08x, checksum : 0x%08x)",
563 le32_to_cpu(*p_chksum), chksum);
564 brelse(bh);
565 return -EINVAL;
566 }
567 }
568 brelse(bh);
569 return 0;
570}
571
Tetsuhiro Kohada33404a12020-05-29 19:14:57 +0900572/* mount the file system volume */
573static int __exfat_fill_super(struct super_block *sb)
574{
575 int ret;
576 struct exfat_sb_info *sbi = EXFAT_SB(sb);
577
578 ret = exfat_read_boot_sector(sb);
579 if (ret) {
580 exfat_err(sb, "failed to read boot sector");
581 goto free_bh;
582 }
583
Tetsuhiro Kohada476189c2020-05-31 18:30:17 +0900584 ret = exfat_verify_boot_region(sb);
585 if (ret) {
586 exfat_err(sb, "invalid boot region");
587 goto free_bh;
588 }
589
Namjae Jeon719c1e12020-03-02 15:21:33 +0900590 ret = exfat_create_upcase_table(sb);
591 if (ret) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900592 exfat_err(sb, "failed to load upcase table");
Namjae Jeon719c1e12020-03-02 15:21:33 +0900593 goto free_bh;
594 }
595
596 ret = exfat_load_bitmap(sb);
597 if (ret) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900598 exfat_err(sb, "failed to load alloc-bitmap");
Namjae Jeon719c1e12020-03-02 15:21:33 +0900599 goto free_upcase_table;
600 }
601
602 ret = exfat_count_used_clusters(sb, &sbi->used_clusters);
603 if (ret) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900604 exfat_err(sb, "failed to scan clusters");
Namjae Jeon719c1e12020-03-02 15:21:33 +0900605 goto free_alloc_bitmap;
606 }
607
608 return 0;
609
610free_alloc_bitmap:
611 exfat_free_bitmap(sbi);
612free_upcase_table:
613 exfat_free_upcase_table(sbi);
614free_bh:
Tetsuhiro Kohada181a9e82020-05-29 19:14:56 +0900615 brelse(sbi->boot_bh);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900616 return ret;
617}
618
619static int exfat_fill_super(struct super_block *sb, struct fs_context *fc)
620{
621 struct exfat_sb_info *sbi = sb->s_fs_info;
622 struct exfat_mount_options *opts = &sbi->options;
623 struct inode *root_inode;
624 int err;
625
626 if (opts->allow_utime == (unsigned short)-1)
627 opts->allow_utime = ~opts->fs_dmask & 0022;
628
629 if (opts->discard) {
630 struct request_queue *q = bdev_get_queue(sb->s_bdev);
631
Pali Rohárb7e038a2020-03-17 22:46:52 +0100632 if (!blk_queue_discard(q)) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900633 exfat_warn(sb, "mounting with \"discard\" option, but the device does not support discard");
Pali Rohárb7e038a2020-03-17 22:46:52 +0100634 opts->discard = 0;
635 }
Namjae Jeon719c1e12020-03-02 15:21:33 +0900636 }
637
638 sb->s_flags |= SB_NODIRATIME;
639 sb->s_magic = EXFAT_SUPER_MAGIC;
640 sb->s_op = &exfat_sops;
641
Eric Sandeen674a9982020-04-17 14:43:49 +0900642 sb->s_time_gran = 10 * NSEC_PER_MSEC;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900643 sb->s_time_min = EXFAT_MIN_TIMESTAMP_SECS;
644 sb->s_time_max = EXFAT_MAX_TIMESTAMP_SECS;
645
646 err = __exfat_fill_super(sb);
647 if (err) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900648 exfat_err(sb, "failed to recognize exfat type");
Namjae Jeon719c1e12020-03-02 15:21:33 +0900649 goto check_nls_io;
650 }
651
652 /* set up enough so that it can read an inode */
653 exfat_hash_init(sb);
654
655 if (!strcmp(sbi->options.iocharset, "utf8"))
656 opts->utf8 = 1;
657 else {
658 sbi->nls_io = load_nls(sbi->options.iocharset);
659 if (!sbi->nls_io) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900660 exfat_err(sb, "IO charset %s not found",
661 sbi->options.iocharset);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900662 err = -EINVAL;
663 goto free_table;
664 }
665 }
666
667 if (sbi->options.utf8)
668 sb->s_d_op = &exfat_utf8_dentry_ops;
669 else
670 sb->s_d_op = &exfat_dentry_ops;
671
672 root_inode = new_inode(sb);
673 if (!root_inode) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900674 exfat_err(sb, "failed to allocate root inode");
Namjae Jeon719c1e12020-03-02 15:21:33 +0900675 err = -ENOMEM;
676 goto free_table;
677 }
678
679 root_inode->i_ino = EXFAT_ROOT_INO;
680 inode_set_iversion(root_inode, 1);
681 err = exfat_read_root(root_inode);
682 if (err) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900683 exfat_err(sb, "failed to initialize root inode");
Namjae Jeon719c1e12020-03-02 15:21:33 +0900684 goto put_inode;
685 }
686
687 exfat_hash_inode(root_inode, EXFAT_I(root_inode)->i_pos);
688 insert_inode_hash(root_inode);
689
690 sb->s_root = d_make_root(root_inode);
691 if (!sb->s_root) {
Joe Perchesd1727d52020-04-24 13:31:12 +0900692 exfat_err(sb, "failed to get the root dentry");
Namjae Jeon719c1e12020-03-02 15:21:33 +0900693 err = -ENOMEM;
Chen Li839a5342021-06-09 11:48:55 +0800694 goto free_table;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900695 }
696
697 return 0;
698
699put_inode:
700 iput(root_inode);
701 sb->s_root = NULL;
702
703free_table:
704 exfat_free_upcase_table(sbi);
705 exfat_free_bitmap(sbi);
Tetsuhiro Kohada181a9e82020-05-29 19:14:56 +0900706 brelse(sbi->boot_bh);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900707
708check_nls_io:
709 unload_nls(sbi->nls_io);
710 exfat_free_iocharset(sbi);
711 sb->s_fs_info = NULL;
712 kfree(sbi);
713 return err;
714}
715
716static int exfat_get_tree(struct fs_context *fc)
717{
718 return get_tree_bdev(fc, exfat_fill_super);
719}
720
721static void exfat_free(struct fs_context *fc)
722{
Al Virof341a7d2020-06-03 09:48:36 +0900723 struct exfat_sb_info *sbi = fc->s_fs_info;
724
725 if (sbi) {
726 exfat_free_iocharset(sbi);
727 kfree(sbi);
728 }
Namjae Jeon719c1e12020-03-02 15:21:33 +0900729}
730
Hyunchul Leea0271a12020-06-16 14:34:45 +0900731static int exfat_reconfigure(struct fs_context *fc)
732{
733 fc->sb_flags |= SB_NODIRATIME;
734
735 /* volume flag will be updated in exfat_sync_fs */
736 sync_filesystem(fc->root->d_sb);
737 return 0;
738}
739
Namjae Jeon719c1e12020-03-02 15:21:33 +0900740static const struct fs_context_operations exfat_context_ops = {
741 .parse_param = exfat_parse_param,
742 .get_tree = exfat_get_tree,
743 .free = exfat_free,
Hyunchul Leea0271a12020-06-16 14:34:45 +0900744 .reconfigure = exfat_reconfigure,
Namjae Jeon719c1e12020-03-02 15:21:33 +0900745};
746
747static int exfat_init_fs_context(struct fs_context *fc)
748{
749 struct exfat_sb_info *sbi;
750
751 sbi = kzalloc(sizeof(struct exfat_sb_info), GFP_KERNEL);
752 if (!sbi)
753 return -ENOMEM;
754
755 mutex_init(&sbi->s_lock);
Hyeongseok Kim5c2d7282021-03-02 14:05:20 +0900756 mutex_init(&sbi->bitmap_lock);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900757 ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
758 DEFAULT_RATELIMIT_BURST);
759
760 sbi->options.fs_uid = current_uid();
761 sbi->options.fs_gid = current_gid();
762 sbi->options.fs_fmask = current->fs->umask;
763 sbi->options.fs_dmask = current->fs->umask;
764 sbi->options.allow_utime = -1;
765 sbi->options.iocharset = exfat_default_iocharset;
766 sbi->options.errors = EXFAT_ERRORS_RO;
767
768 fc->s_fs_info = sbi;
769 fc->ops = &exfat_context_ops;
770 return 0;
771}
772
773static struct file_system_type exfat_fs_type = {
774 .owner = THIS_MODULE,
775 .name = "exfat",
776 .init_fs_context = exfat_init_fs_context,
Valdis Kletnieks9acd0d52020-03-02 15:21:45 +0900777 .parameters = exfat_parameters,
Namjae Jeon719c1e12020-03-02 15:21:33 +0900778 .kill_sb = kill_block_super,
779 .fs_flags = FS_REQUIRES_DEV,
780};
781
782static void exfat_inode_init_once(void *foo)
783{
784 struct exfat_inode_info *ei = (struct exfat_inode_info *)foo;
785
Namjae Jeon8ff006e2020-09-29 09:09:49 +0900786 spin_lock_init(&ei->cache_lru_lock);
787 ei->nr_caches = 0;
788 ei->cache_valid_id = EXFAT_CACHE_VALID + 1;
789 INIT_LIST_HEAD(&ei->cache_lru);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900790 INIT_HLIST_NODE(&ei->i_hash_fat);
791 inode_init_once(&ei->vfs_inode);
792}
793
794static int __init init_exfat_fs(void)
795{
796 int err;
797
798 err = exfat_cache_init();
799 if (err)
800 return err;
801
802 exfat_inode_cachep = kmem_cache_create("exfat_inode_cache",
803 sizeof(struct exfat_inode_info),
804 0, SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
805 exfat_inode_init_once);
806 if (!exfat_inode_cachep) {
807 err = -ENOMEM;
808 goto shutdown_cache;
809 }
810
811 err = register_filesystem(&exfat_fs_type);
812 if (err)
813 goto destroy_cache;
814
815 return 0;
816
817destroy_cache:
818 kmem_cache_destroy(exfat_inode_cachep);
819shutdown_cache:
820 exfat_cache_shutdown();
821 return err;
822}
823
824static void __exit exit_exfat_fs(void)
825{
826 /*
827 * Make sure all delayed rcu free inodes are flushed before we
828 * destroy cache.
829 */
830 rcu_barrier();
831 kmem_cache_destroy(exfat_inode_cachep);
832 unregister_filesystem(&exfat_fs_type);
833 exfat_cache_shutdown();
834}
835
836module_init(init_exfat_fs);
837module_exit(exit_exfat_fs);
838
Thomas Backlundcd76ac22020-04-04 23:29:43 +0300839MODULE_ALIAS_FS("exfat");
Namjae Jeon719c1e12020-03-02 15:21:33 +0900840MODULE_LICENSE("GPL");
841MODULE_DESCRIPTION("exFAT filesystem support");
842MODULE_AUTHOR("Samsung Electronics Co., Ltd.");