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