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