projects/linux·blob master · refs

943 lines · 23.8 KiB

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