xref: /linux/fs/exfat/super.c (revision b1a54551dd9ed5ef1763b97b35a0999ca002b95c)
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 
25 static char exfat_default_iocharset[] = CONFIG_EXFAT_DEFAULT_IOCHARSET;
26 static struct kmem_cache *exfat_inode_cachep;
27 
28 static 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 
34 static 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 	unload_nls(sbi->nls_io);
44 	exfat_free_upcase_table(sbi);
45 }
46 
47 static int exfat_sync_fs(struct super_block *sb, int wait)
48 {
49 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
50 	int err = 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 
64 static 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 
90 static 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 
120 int 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 
127 int 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 
134 static 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 
173 static struct inode *exfat_alloc_inode(struct super_block *sb)
174 {
175 	struct exfat_inode_info *ei;
176 
177 	ei = alloc_inode_sb(sb, exfat_inode_cachep, GFP_NOFS);
178 	if (!ei)
179 		return NULL;
180 
181 	init_rwsem(&ei->truncate_lock);
182 	return &ei->vfs_inode;
183 }
184 
185 static void exfat_free_inode(struct inode *inode)
186 {
187 	kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode));
188 }
189 
190 static const struct super_operations exfat_sops = {
191 	.alloc_inode	= exfat_alloc_inode,
192 	.free_inode	= exfat_free_inode,
193 	.write_inode	= exfat_write_inode,
194 	.evict_inode	= exfat_evict_inode,
195 	.put_super	= exfat_put_super,
196 	.sync_fs	= exfat_sync_fs,
197 	.statfs		= exfat_statfs,
198 	.show_options	= exfat_show_options,
199 };
200 
201 enum {
202 	Opt_uid,
203 	Opt_gid,
204 	Opt_umask,
205 	Opt_dmask,
206 	Opt_fmask,
207 	Opt_allow_utime,
208 	Opt_charset,
209 	Opt_errors,
210 	Opt_discard,
211 	Opt_keep_last_dots,
212 	Opt_sys_tz,
213 	Opt_time_offset,
214 	Opt_zero_size_dir,
215 
216 	/* Deprecated options */
217 	Opt_utf8,
218 	Opt_debug,
219 	Opt_namecase,
220 	Opt_codepage,
221 };
222 
223 static const struct constant_table exfat_param_enums[] = {
224 	{ "continue",		EXFAT_ERRORS_CONT },
225 	{ "panic",		EXFAT_ERRORS_PANIC },
226 	{ "remount-ro",		EXFAT_ERRORS_RO },
227 	{}
228 };
229 
230 static const struct fs_parameter_spec exfat_parameters[] = {
231 	fsparam_u32("uid",			Opt_uid),
232 	fsparam_u32("gid",			Opt_gid),
233 	fsparam_u32oct("umask",			Opt_umask),
234 	fsparam_u32oct("dmask",			Opt_dmask),
235 	fsparam_u32oct("fmask",			Opt_fmask),
236 	fsparam_u32oct("allow_utime",		Opt_allow_utime),
237 	fsparam_string("iocharset",		Opt_charset),
238 	fsparam_enum("errors",			Opt_errors, exfat_param_enums),
239 	fsparam_flag("discard",			Opt_discard),
240 	fsparam_flag("keep_last_dots",		Opt_keep_last_dots),
241 	fsparam_flag("sys_tz",			Opt_sys_tz),
242 	fsparam_s32("time_offset",		Opt_time_offset),
243 	fsparam_flag("zero_size_dir",		Opt_zero_size_dir),
244 	__fsparam(NULL, "utf8",			Opt_utf8, fs_param_deprecated,
245 		  NULL),
246 	__fsparam(NULL, "debug",		Opt_debug, fs_param_deprecated,
247 		  NULL),
248 	__fsparam(fs_param_is_u32, "namecase",	Opt_namecase,
249 		  fs_param_deprecated, NULL),
250 	__fsparam(fs_param_is_u32, "codepage",	Opt_codepage,
251 		  fs_param_deprecated, NULL),
252 	{}
253 };
254 
255 static int exfat_parse_param(struct fs_context *fc, struct fs_parameter *param)
256 {
257 	struct exfat_sb_info *sbi = fc->s_fs_info;
258 	struct exfat_mount_options *opts = &sbi->options;
259 	struct fs_parse_result result;
260 	int opt;
261 
262 	opt = fs_parse(fc, exfat_parameters, param, &result);
263 	if (opt < 0)
264 		return opt;
265 
266 	switch (opt) {
267 	case Opt_uid:
268 		opts->fs_uid = make_kuid(current_user_ns(), result.uint_32);
269 		break;
270 	case Opt_gid:
271 		opts->fs_gid = make_kgid(current_user_ns(), result.uint_32);
272 		break;
273 	case Opt_umask:
274 		opts->fs_fmask = result.uint_32;
275 		opts->fs_dmask = result.uint_32;
276 		break;
277 	case Opt_dmask:
278 		opts->fs_dmask = result.uint_32;
279 		break;
280 	case Opt_fmask:
281 		opts->fs_fmask = result.uint_32;
282 		break;
283 	case Opt_allow_utime:
284 		opts->allow_utime = result.uint_32 & 0022;
285 		break;
286 	case Opt_charset:
287 		exfat_free_iocharset(sbi);
288 		opts->iocharset = param->string;
289 		param->string = NULL;
290 		break;
291 	case Opt_errors:
292 		opts->errors = result.uint_32;
293 		break;
294 	case Opt_discard:
295 		opts->discard = 1;
296 		break;
297 	case Opt_keep_last_dots:
298 		opts->keep_last_dots = 1;
299 		break;
300 	case Opt_sys_tz:
301 		opts->sys_tz = 1;
302 		break;
303 	case Opt_time_offset:
304 		/*
305 		 * Make the limit 24 just in case someone invents something
306 		 * unusual.
307 		 */
308 		if (result.int_32 < -24 * 60 || result.int_32 > 24 * 60)
309 			return -EINVAL;
310 		opts->time_offset = result.int_32;
311 		break;
312 	case Opt_zero_size_dir:
313 		opts->zero_size_dir = true;
314 		break;
315 	case Opt_utf8:
316 	case Opt_debug:
317 	case Opt_namecase:
318 	case Opt_codepage:
319 		break;
320 	default:
321 		return -EINVAL;
322 	}
323 
324 	return 0;
325 }
326 
327 static void exfat_hash_init(struct super_block *sb)
328 {
329 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
330 	int i;
331 
332 	spin_lock_init(&sbi->inode_hash_lock);
333 	for (i = 0; i < EXFAT_HASH_SIZE; i++)
334 		INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
335 }
336 
337 static int exfat_read_root(struct inode *inode)
338 {
339 	struct super_block *sb = inode->i_sb;
340 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
341 	struct exfat_inode_info *ei = EXFAT_I(inode);
342 	struct exfat_chain cdir;
343 	int num_subdirs, num_clu = 0;
344 
345 	exfat_chain_set(&ei->dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
346 	ei->entry = -1;
347 	ei->start_clu = sbi->root_dir;
348 	ei->flags = ALLOC_FAT_CHAIN;
349 	ei->type = TYPE_DIR;
350 	ei->version = 0;
351 	ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
352 	ei->hint_stat.eidx = 0;
353 	ei->hint_stat.clu = sbi->root_dir;
354 	ei->hint_femp.eidx = EXFAT_HINT_NONE;
355 
356 	exfat_chain_set(&cdir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
357 	if (exfat_count_num_clusters(sb, &cdir, &num_clu))
358 		return -EIO;
359 	i_size_write(inode, num_clu << sbi->cluster_size_bits);
360 
361 	num_subdirs = exfat_count_dir_entries(sb, &cdir);
362 	if (num_subdirs < 0)
363 		return -EIO;
364 	set_nlink(inode, num_subdirs + EXFAT_MIN_SUBDIR);
365 
366 	inode->i_uid = sbi->options.fs_uid;
367 	inode->i_gid = sbi->options.fs_gid;
368 	inode_inc_iversion(inode);
369 	inode->i_generation = 0;
370 	inode->i_mode = exfat_make_mode(sbi, EXFAT_ATTR_SUBDIR, 0777);
371 	inode->i_op = &exfat_dir_inode_operations;
372 	inode->i_fop = &exfat_dir_operations;
373 
374 	inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
375 	ei->i_pos = ((loff_t)sbi->root_dir << 32) | 0xffffffff;
376 	ei->i_size_aligned = i_size_read(inode);
377 	ei->i_size_ondisk = i_size_read(inode);
378 
379 	exfat_save_attr(inode, EXFAT_ATTR_SUBDIR);
380 	ei->i_crtime = simple_inode_init_ts(inode);
381 	exfat_truncate_inode_atime(inode);
382 	return 0;
383 }
384 
385 static int exfat_calibrate_blocksize(struct super_block *sb, int logical_sect)
386 {
387 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
388 
389 	if (!is_power_of_2(logical_sect)) {
390 		exfat_err(sb, "bogus logical sector size %u", logical_sect);
391 		return -EIO;
392 	}
393 
394 	if (logical_sect < sb->s_blocksize) {
395 		exfat_err(sb, "logical sector size too small for device (logical sector size = %u)",
396 			  logical_sect);
397 		return -EIO;
398 	}
399 
400 	if (logical_sect > sb->s_blocksize) {
401 		brelse(sbi->boot_bh);
402 		sbi->boot_bh = NULL;
403 
404 		if (!sb_set_blocksize(sb, logical_sect)) {
405 			exfat_err(sb, "unable to set blocksize %u",
406 				  logical_sect);
407 			return -EIO;
408 		}
409 		sbi->boot_bh = sb_bread(sb, 0);
410 		if (!sbi->boot_bh) {
411 			exfat_err(sb, "unable to read boot sector (logical sector size = %lu)",
412 				  sb->s_blocksize);
413 			return -EIO;
414 		}
415 	}
416 	return 0;
417 }
418 
419 static int exfat_read_boot_sector(struct super_block *sb)
420 {
421 	struct boot_sector *p_boot;
422 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
423 
424 	/* set block size to read super block */
425 	sb_min_blocksize(sb, 512);
426 
427 	/* read boot sector */
428 	sbi->boot_bh = sb_bread(sb, 0);
429 	if (!sbi->boot_bh) {
430 		exfat_err(sb, "unable to read boot sector");
431 		return -EIO;
432 	}
433 	p_boot = (struct boot_sector *)sbi->boot_bh->b_data;
434 
435 	/* check the validity of BOOT */
436 	if (le16_to_cpu((p_boot->signature)) != BOOT_SIGNATURE) {
437 		exfat_err(sb, "invalid boot record signature");
438 		return -EINVAL;
439 	}
440 
441 	if (memcmp(p_boot->fs_name, STR_EXFAT, BOOTSEC_FS_NAME_LEN)) {
442 		exfat_err(sb, "invalid fs_name"); /* fs_name may unprintable */
443 		return -EINVAL;
444 	}
445 
446 	/*
447 	 * must_be_zero field must be filled with zero to prevent mounting
448 	 * from FAT volume.
449 	 */
450 	if (memchr_inv(p_boot->must_be_zero, 0, sizeof(p_boot->must_be_zero)))
451 		return -EINVAL;
452 
453 	if (p_boot->num_fats != 1 && p_boot->num_fats != 2) {
454 		exfat_err(sb, "bogus number of FAT structure");
455 		return -EINVAL;
456 	}
457 
458 	/*
459 	 * sect_size_bits could be at least 9 and at most 12.
460 	 */
461 	if (p_boot->sect_size_bits < EXFAT_MIN_SECT_SIZE_BITS ||
462 	    p_boot->sect_size_bits > EXFAT_MAX_SECT_SIZE_BITS) {
463 		exfat_err(sb, "bogus sector size bits : %u",
464 				p_boot->sect_size_bits);
465 		return -EINVAL;
466 	}
467 
468 	/*
469 	 * sect_per_clus_bits could be at least 0 and at most 25 - sect_size_bits.
470 	 */
471 	if (p_boot->sect_per_clus_bits > EXFAT_MAX_SECT_PER_CLUS_BITS(p_boot)) {
472 		exfat_err(sb, "bogus sectors bits per cluster : %u",
473 				p_boot->sect_per_clus_bits);
474 		return -EINVAL;
475 	}
476 
477 	sbi->sect_per_clus = 1 << p_boot->sect_per_clus_bits;
478 	sbi->sect_per_clus_bits = p_boot->sect_per_clus_bits;
479 	sbi->cluster_size_bits = p_boot->sect_per_clus_bits +
480 		p_boot->sect_size_bits;
481 	sbi->cluster_size = 1 << sbi->cluster_size_bits;
482 	sbi->num_FAT_sectors = le32_to_cpu(p_boot->fat_length);
483 	sbi->FAT1_start_sector = le32_to_cpu(p_boot->fat_offset);
484 	sbi->FAT2_start_sector = le32_to_cpu(p_boot->fat_offset);
485 	if (p_boot->num_fats == 2)
486 		sbi->FAT2_start_sector += sbi->num_FAT_sectors;
487 	sbi->data_start_sector = le32_to_cpu(p_boot->clu_offset);
488 	sbi->num_sectors = le64_to_cpu(p_boot->vol_length);
489 	/* because the cluster index starts with 2 */
490 	sbi->num_clusters = le32_to_cpu(p_boot->clu_count) +
491 		EXFAT_RESERVED_CLUSTERS;
492 
493 	sbi->root_dir = le32_to_cpu(p_boot->root_cluster);
494 	sbi->dentries_per_clu = 1 <<
495 		(sbi->cluster_size_bits - DENTRY_SIZE_BITS);
496 
497 	sbi->vol_flags = le16_to_cpu(p_boot->vol_flags);
498 	sbi->vol_flags_persistent = sbi->vol_flags & (VOLUME_DIRTY | MEDIA_FAILURE);
499 	sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
500 	sbi->used_clusters = EXFAT_CLUSTERS_UNTRACKED;
501 
502 	/* check consistencies */
503 	if ((u64)sbi->num_FAT_sectors << p_boot->sect_size_bits <
504 	    (u64)sbi->num_clusters * 4) {
505 		exfat_err(sb, "bogus fat length");
506 		return -EINVAL;
507 	}
508 
509 	if (sbi->data_start_sector <
510 	    (u64)sbi->FAT1_start_sector +
511 	    (u64)sbi->num_FAT_sectors * p_boot->num_fats) {
512 		exfat_err(sb, "bogus data start sector");
513 		return -EINVAL;
514 	}
515 
516 	if (sbi->vol_flags & VOLUME_DIRTY)
517 		exfat_warn(sb, "Volume was not properly unmounted. Some data may be corrupt. Please run fsck.");
518 	if (sbi->vol_flags & MEDIA_FAILURE)
519 		exfat_warn(sb, "Medium has reported failures. Some data may be lost.");
520 
521 	/* exFAT file size is limited by a disk volume size */
522 	sb->s_maxbytes = (u64)(sbi->num_clusters - EXFAT_RESERVED_CLUSTERS) <<
523 		sbi->cluster_size_bits;
524 
525 	/* check logical sector size */
526 	if (exfat_calibrate_blocksize(sb, 1 << p_boot->sect_size_bits))
527 		return -EIO;
528 
529 	return 0;
530 }
531 
532 static int exfat_verify_boot_region(struct super_block *sb)
533 {
534 	struct buffer_head *bh = NULL;
535 	u32 chksum = 0;
536 	__le32 *p_sig, *p_chksum;
537 	int sn, i;
538 
539 	/* read boot sector sub-regions */
540 	for (sn = 0; sn < 11; sn++) {
541 		bh = sb_bread(sb, sn);
542 		if (!bh)
543 			return -EIO;
544 
545 		if (sn != 0 && sn <= 8) {
546 			/* extended boot sector sub-regions */
547 			p_sig = (__le32 *)&bh->b_data[sb->s_blocksize - 4];
548 			if (le32_to_cpu(*p_sig) != EXBOOT_SIGNATURE)
549 				exfat_warn(sb, "Invalid exboot-signature(sector = %d): 0x%08x",
550 					   sn, le32_to_cpu(*p_sig));
551 		}
552 
553 		chksum = exfat_calc_chksum32(bh->b_data, sb->s_blocksize,
554 			chksum, sn ? CS_DEFAULT : CS_BOOT_SECTOR);
555 		brelse(bh);
556 	}
557 
558 	/* boot checksum sub-regions */
559 	bh = sb_bread(sb, sn);
560 	if (!bh)
561 		return -EIO;
562 
563 	for (i = 0; i < sb->s_blocksize; i += sizeof(u32)) {
564 		p_chksum = (__le32 *)&bh->b_data[i];
565 		if (le32_to_cpu(*p_chksum) != chksum) {
566 			exfat_err(sb, "Invalid boot checksum (boot checksum : 0x%08x, checksum : 0x%08x)",
567 				  le32_to_cpu(*p_chksum), chksum);
568 			brelse(bh);
569 			return -EINVAL;
570 		}
571 	}
572 	brelse(bh);
573 	return 0;
574 }
575 
576 /* mount the file system volume */
577 static int __exfat_fill_super(struct super_block *sb)
578 {
579 	int ret;
580 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
581 
582 	ret = exfat_read_boot_sector(sb);
583 	if (ret) {
584 		exfat_err(sb, "failed to read boot sector");
585 		goto free_bh;
586 	}
587 
588 	ret = exfat_verify_boot_region(sb);
589 	if (ret) {
590 		exfat_err(sb, "invalid boot region");
591 		goto free_bh;
592 	}
593 
594 	ret = exfat_create_upcase_table(sb);
595 	if (ret) {
596 		exfat_err(sb, "failed to load upcase table");
597 		goto free_bh;
598 	}
599 
600 	ret = exfat_load_bitmap(sb);
601 	if (ret) {
602 		exfat_err(sb, "failed to load alloc-bitmap");
603 		goto free_upcase_table;
604 	}
605 
606 	ret = exfat_count_used_clusters(sb, &sbi->used_clusters);
607 	if (ret) {
608 		exfat_err(sb, "failed to scan clusters");
609 		goto free_alloc_bitmap;
610 	}
611 
612 	return 0;
613 
614 free_alloc_bitmap:
615 	exfat_free_bitmap(sbi);
616 free_upcase_table:
617 	exfat_free_upcase_table(sbi);
618 free_bh:
619 	brelse(sbi->boot_bh);
620 	return ret;
621 }
622 
623 static int exfat_fill_super(struct super_block *sb, struct fs_context *fc)
624 {
625 	struct exfat_sb_info *sbi = sb->s_fs_info;
626 	struct exfat_mount_options *opts = &sbi->options;
627 	struct inode *root_inode;
628 	int err;
629 
630 	if (opts->allow_utime == (unsigned short)-1)
631 		opts->allow_utime = ~opts->fs_dmask & 0022;
632 
633 	if (opts->discard && !bdev_max_discard_sectors(sb->s_bdev)) {
634 		exfat_warn(sb, "mounting with \"discard\" option, but the device does not support discard");
635 		opts->discard = 0;
636 	}
637 
638 	sb->s_flags |= SB_NODIRATIME;
639 	sb->s_magic = EXFAT_SUPER_MAGIC;
640 	sb->s_op = &exfat_sops;
641 
642 	sb->s_time_gran = 10 * NSEC_PER_MSEC;
643 	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) {
648 		exfat_err(sb, "failed to recognize exfat type");
649 		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) {
660 			exfat_err(sb, "IO charset %s not found",
661 				  sbi->options.iocharset);
662 			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) {
674 		exfat_err(sb, "failed to allocate root inode");
675 		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) {
683 		exfat_err(sb, "failed to initialize root inode");
684 		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) {
692 		exfat_err(sb, "failed to get the root dentry");
693 		err = -ENOMEM;
694 		goto free_table;
695 	}
696 
697 	return 0;
698 
699 put_inode:
700 	iput(root_inode);
701 	sb->s_root = NULL;
702 
703 free_table:
704 	exfat_free_upcase_table(sbi);
705 	exfat_free_bitmap(sbi);
706 	brelse(sbi->boot_bh);
707 
708 check_nls_io:
709 	unload_nls(sbi->nls_io);
710 	return err;
711 }
712 
713 static int exfat_get_tree(struct fs_context *fc)
714 {
715 	return get_tree_bdev(fc, exfat_fill_super);
716 }
717 
718 static void exfat_free_sbi(struct exfat_sb_info *sbi)
719 {
720 	exfat_free_iocharset(sbi);
721 	kfree(sbi);
722 }
723 
724 static void exfat_free(struct fs_context *fc)
725 {
726 	struct exfat_sb_info *sbi = fc->s_fs_info;
727 
728 	if (sbi)
729 		exfat_free_sbi(sbi);
730 }
731 
732 static int exfat_reconfigure(struct fs_context *fc)
733 {
734 	fc->sb_flags |= SB_NODIRATIME;
735 
736 	/* volume flag will be updated in exfat_sync_fs */
737 	sync_filesystem(fc->root->d_sb);
738 	return 0;
739 }
740 
741 static const struct fs_context_operations exfat_context_ops = {
742 	.parse_param	= exfat_parse_param,
743 	.get_tree	= exfat_get_tree,
744 	.free		= exfat_free,
745 	.reconfigure	= exfat_reconfigure,
746 };
747 
748 static int exfat_init_fs_context(struct fs_context *fc)
749 {
750 	struct exfat_sb_info *sbi;
751 
752 	sbi = kzalloc(sizeof(struct exfat_sb_info), GFP_KERNEL);
753 	if (!sbi)
754 		return -ENOMEM;
755 
756 	mutex_init(&sbi->s_lock);
757 	mutex_init(&sbi->bitmap_lock);
758 	ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
759 			DEFAULT_RATELIMIT_BURST);
760 
761 	sbi->options.fs_uid = current_uid();
762 	sbi->options.fs_gid = current_gid();
763 	sbi->options.fs_fmask = current->fs->umask;
764 	sbi->options.fs_dmask = current->fs->umask;
765 	sbi->options.allow_utime = -1;
766 	sbi->options.iocharset = exfat_default_iocharset;
767 	sbi->options.errors = EXFAT_ERRORS_RO;
768 
769 	fc->s_fs_info = sbi;
770 	fc->ops = &exfat_context_ops;
771 	return 0;
772 }
773 
774 static void exfat_kill_sb(struct super_block *sb)
775 {
776 	struct exfat_sb_info *sbi = sb->s_fs_info;
777 
778 	kill_block_super(sb);
779 	if (sbi)
780 		exfat_free_sbi(sbi);
781 }
782 
783 static struct file_system_type exfat_fs_type = {
784 	.owner			= THIS_MODULE,
785 	.name			= "exfat",
786 	.init_fs_context	= exfat_init_fs_context,
787 	.parameters		= exfat_parameters,
788 	.kill_sb		= exfat_kill_sb,
789 	.fs_flags		= FS_REQUIRES_DEV,
790 };
791 
792 static void exfat_inode_init_once(void *foo)
793 {
794 	struct exfat_inode_info *ei = (struct exfat_inode_info *)foo;
795 
796 	spin_lock_init(&ei->cache_lru_lock);
797 	ei->nr_caches = 0;
798 	ei->cache_valid_id = EXFAT_CACHE_VALID + 1;
799 	INIT_LIST_HEAD(&ei->cache_lru);
800 	INIT_HLIST_NODE(&ei->i_hash_fat);
801 	inode_init_once(&ei->vfs_inode);
802 }
803 
804 static int __init init_exfat_fs(void)
805 {
806 	int err;
807 
808 	err = exfat_cache_init();
809 	if (err)
810 		return err;
811 
812 	exfat_inode_cachep = kmem_cache_create("exfat_inode_cache",
813 			sizeof(struct exfat_inode_info),
814 			0, SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
815 			exfat_inode_init_once);
816 	if (!exfat_inode_cachep) {
817 		err = -ENOMEM;
818 		goto shutdown_cache;
819 	}
820 
821 	err = register_filesystem(&exfat_fs_type);
822 	if (err)
823 		goto destroy_cache;
824 
825 	return 0;
826 
827 destroy_cache:
828 	kmem_cache_destroy(exfat_inode_cachep);
829 shutdown_cache:
830 	exfat_cache_shutdown();
831 	return err;
832 }
833 
834 static void __exit exit_exfat_fs(void)
835 {
836 	/*
837 	 * Make sure all delayed rcu free inodes are flushed before we
838 	 * destroy cache.
839 	 */
840 	rcu_barrier();
841 	kmem_cache_destroy(exfat_inode_cachep);
842 	unregister_filesystem(&exfat_fs_type);
843 	exfat_cache_shutdown();
844 }
845 
846 module_init(init_exfat_fs);
847 module_exit(exit_exfat_fs);
848 
849 MODULE_ALIAS_FS("exfat");
850 MODULE_LICENSE("GPL");
851 MODULE_DESCRIPTION("exFAT filesystem support");
852 MODULE_AUTHOR("Samsung Electronics Co., Ltd.");
853