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