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