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