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