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