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