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