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