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