blob: ee42ecf59059a6c9bf61f5b6ec19efde21a4d897 [file] [log] [blame]
Namjae Jeon719c1e12020-03-02 15:21:33 +09001// 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
21#include "exfat_raw.h"
22#include "exfat_fs.h"
23
24static char exfat_default_iocharset[] = CONFIG_EXFAT_DEFAULT_IOCHARSET;
25static struct kmem_cache *exfat_inode_cachep;
26
27static void exfat_free_iocharset(struct exfat_sb_info *sbi)
28{
29 if (sbi->options.iocharset != exfat_default_iocharset)
30 kfree(sbi->options.iocharset);
31}
32
33static void exfat_delayed_free(struct rcu_head *p)
34{
35 struct exfat_sb_info *sbi = container_of(p, struct exfat_sb_info, rcu);
36
37 unload_nls(sbi->nls_io);
38 exfat_free_iocharset(sbi);
39 exfat_free_upcase_table(sbi);
40 kfree(sbi);
41}
42
43static void exfat_put_super(struct super_block *sb)
44{
45 struct exfat_sb_info *sbi = EXFAT_SB(sb);
46
47 mutex_lock(&sbi->s_lock);
48 if (test_and_clear_bit(EXFAT_SB_DIRTY, &sbi->s_state))
49 sync_blockdev(sb->s_bdev);
50 exfat_set_vol_flags(sb, VOL_CLEAN);
51 exfat_free_bitmap(sbi);
Tetsuhiro Kohadab0516832020-04-21 10:58:58 +090052 brelse(sbi->pbr_bh);
Namjae Jeon719c1e12020-03-02 15:21:33 +090053 mutex_unlock(&sbi->s_lock);
54
55 call_rcu(&sbi->rcu, exfat_delayed_free);
56}
57
58static int exfat_sync_fs(struct super_block *sb, int wait)
59{
60 struct exfat_sb_info *sbi = EXFAT_SB(sb);
61 int err = 0;
62
63 /* If there are some dirty buffers in the bdev inode */
64 mutex_lock(&sbi->s_lock);
65 if (test_and_clear_bit(EXFAT_SB_DIRTY, &sbi->s_state)) {
66 sync_blockdev(sb->s_bdev);
67 if (exfat_set_vol_flags(sb, VOL_CLEAN))
68 err = -EIO;
69 }
70 mutex_unlock(&sbi->s_lock);
71 return err;
72}
73
74static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf)
75{
76 struct super_block *sb = dentry->d_sb;
77 struct exfat_sb_info *sbi = EXFAT_SB(sb);
78 unsigned long long id = huge_encode_dev(sb->s_bdev->bd_dev);
79
80 if (sbi->used_clusters == EXFAT_CLUSTERS_UNTRACKED) {
81 mutex_lock(&sbi->s_lock);
82 if (exfat_count_used_clusters(sb, &sbi->used_clusters)) {
83 mutex_unlock(&sbi->s_lock);
84 return -EIO;
85 }
86 mutex_unlock(&sbi->s_lock);
87 }
88
89 buf->f_type = sb->s_magic;
90 buf->f_bsize = sbi->cluster_size;
91 buf->f_blocks = sbi->num_clusters - 2; /* clu 0 & 1 */
92 buf->f_bfree = buf->f_blocks - sbi->used_clusters;
93 buf->f_bavail = buf->f_bfree;
94 buf->f_fsid.val[0] = (unsigned int)id;
95 buf->f_fsid.val[1] = (unsigned int)(id >> 32);
96 /* Unicode utf16 255 characters */
97 buf->f_namelen = EXFAT_MAX_FILE_LEN * NLS_MAX_CHARSET_SIZE;
98 return 0;
99}
100
101int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flag)
102{
103 struct exfat_sb_info *sbi = EXFAT_SB(sb);
Tetsuhiro Kohadab0516832020-04-21 10:58:58 +0900104 struct pbr64 *bpb = (struct pbr64 *)sbi->pbr_bh->b_data;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900105 bool sync = 0;
106
107 /* flags are not changed */
108 if (sbi->vol_flag == new_flag)
109 return 0;
110
111 sbi->vol_flag = new_flag;
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
Namjae Jeon719c1e12020-03-02 15:21:33 +0900119 bpb->bsx.vol_flags = cpu_to_le16(new_flag);
120
121 if (new_flag == VOL_DIRTY && !buffer_dirty(sbi->pbr_bh))
122 sync = true;
123 else
124 sync = false;
125
126 set_buffer_uptodate(sbi->pbr_bh);
127 mark_buffer_dirty(sbi->pbr_bh);
128
129 if (sync)
130 sync_dirty_buffer(sbi->pbr_bh);
131 return 0;
132}
133
134static 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);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900154 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->time_offset)
163 seq_printf(m, ",time_offset=%d", opts->time_offset);
164 return 0;
165}
166
167static struct inode *exfat_alloc_inode(struct super_block *sb)
168{
169 struct exfat_inode_info *ei;
170
171 ei = kmem_cache_alloc(exfat_inode_cachep, GFP_NOFS);
172 if (!ei)
173 return NULL;
174
175 init_rwsem(&ei->truncate_lock);
176 return &ei->vfs_inode;
177}
178
179static void exfat_free_inode(struct inode *inode)
180{
181 kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode));
182}
183
184static const struct super_operations exfat_sops = {
185 .alloc_inode = exfat_alloc_inode,
186 .free_inode = exfat_free_inode,
187 .write_inode = exfat_write_inode,
188 .evict_inode = exfat_evict_inode,
189 .put_super = exfat_put_super,
190 .sync_fs = exfat_sync_fs,
191 .statfs = exfat_statfs,
192 .show_options = exfat_show_options,
193};
194
195enum {
196 Opt_uid,
197 Opt_gid,
198 Opt_umask,
199 Opt_dmask,
200 Opt_fmask,
201 Opt_allow_utime,
202 Opt_charset,
203 Opt_errors,
204 Opt_discard,
205 Opt_time_offset,
206};
207
Valdis Kletnieks9acd0d52020-03-02 15:21:45 +0900208static const struct constant_table exfat_param_enums[] = {
209 { "continue", EXFAT_ERRORS_CONT },
210 { "panic", EXFAT_ERRORS_PANIC },
211 { "remount-ro", EXFAT_ERRORS_RO },
212 {}
213};
214
215static const struct fs_parameter_spec exfat_parameters[] = {
Namjae Jeon719c1e12020-03-02 15:21:33 +0900216 fsparam_u32("uid", Opt_uid),
217 fsparam_u32("gid", Opt_gid),
218 fsparam_u32oct("umask", Opt_umask),
219 fsparam_u32oct("dmask", Opt_dmask),
220 fsparam_u32oct("fmask", Opt_fmask),
221 fsparam_u32oct("allow_utime", Opt_allow_utime),
222 fsparam_string("iocharset", Opt_charset),
Valdis Kletnieks9acd0d52020-03-02 15:21:45 +0900223 fsparam_enum("errors", Opt_errors, exfat_param_enums),
Namjae Jeon719c1e12020-03-02 15:21:33 +0900224 fsparam_flag("discard", Opt_discard),
225 fsparam_s32("time_offset", Opt_time_offset),
226 {}
227};
228
Namjae Jeon719c1e12020-03-02 15:21:33 +0900229static int exfat_parse_param(struct fs_context *fc, struct fs_parameter *param)
230{
231 struct exfat_sb_info *sbi = fc->s_fs_info;
232 struct exfat_mount_options *opts = &sbi->options;
233 struct fs_parse_result result;
234 int opt;
235
Valdis Kletnieks9acd0d52020-03-02 15:21:45 +0900236 opt = fs_parse(fc, exfat_parameters, param, &result);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900237 if (opt < 0)
238 return opt;
239
240 switch (opt) {
241 case Opt_uid:
242 opts->fs_uid = make_kuid(current_user_ns(), result.uint_32);
243 break;
244 case Opt_gid:
245 opts->fs_gid = make_kgid(current_user_ns(), result.uint_32);
246 break;
247 case Opt_umask:
248 opts->fs_fmask = result.uint_32;
249 opts->fs_dmask = result.uint_32;
250 break;
251 case Opt_dmask:
252 opts->fs_dmask = result.uint_32;
253 break;
254 case Opt_fmask:
255 opts->fs_fmask = result.uint_32;
256 break;
257 case Opt_allow_utime:
258 opts->allow_utime = result.uint_32 & 0022;
259 break;
260 case Opt_charset:
261 exfat_free_iocharset(sbi);
262 opts->iocharset = kstrdup(param->string, GFP_KERNEL);
263 if (!opts->iocharset)
264 return -ENOMEM;
265 break;
266 case Opt_errors:
267 opts->errors = result.uint_32;
268 break;
269 case Opt_discard:
270 opts->discard = 1;
271 break;
272 case Opt_time_offset:
273 /*
274 * Make the limit 24 just in case someone invents something
275 * unusual.
276 */
277 if (result.int_32 < -24 * 60 || result.int_32 > 24 * 60)
278 return -EINVAL;
279 opts->time_offset = result.int_32;
280 break;
281 default:
282 return -EINVAL;
283 }
284
285 return 0;
286}
287
288static void exfat_hash_init(struct super_block *sb)
289{
290 struct exfat_sb_info *sbi = EXFAT_SB(sb);
291 int i;
292
293 spin_lock_init(&sbi->inode_hash_lock);
294 for (i = 0; i < EXFAT_HASH_SIZE; i++)
295 INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
296}
297
298static int exfat_read_root(struct inode *inode)
299{
300 struct super_block *sb = inode->i_sb;
301 struct exfat_sb_info *sbi = EXFAT_SB(sb);
302 struct exfat_inode_info *ei = EXFAT_I(inode);
303 struct exfat_chain cdir;
304 int num_subdirs, num_clu = 0;
305
306 exfat_chain_set(&ei->dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
307 ei->entry = -1;
308 ei->start_clu = sbi->root_dir;
309 ei->flags = ALLOC_FAT_CHAIN;
310 ei->type = TYPE_DIR;
311 ei->version = 0;
312 ei->rwoffset = 0;
313 ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
314 ei->hint_stat.eidx = 0;
315 ei->hint_stat.clu = sbi->root_dir;
316 ei->hint_femp.eidx = EXFAT_HINT_NONE;
317
318 exfat_chain_set(&cdir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
319 if (exfat_count_num_clusters(sb, &cdir, &num_clu))
320 return -EIO;
321 i_size_write(inode, num_clu << sbi->cluster_size_bits);
322
323 num_subdirs = exfat_count_dir_entries(sb, &cdir);
324 if (num_subdirs < 0)
325 return -EIO;
326 set_nlink(inode, num_subdirs + EXFAT_MIN_SUBDIR);
327
328 inode->i_uid = sbi->options.fs_uid;
329 inode->i_gid = sbi->options.fs_gid;
330 inode_inc_iversion(inode);
331 inode->i_generation = 0;
332 inode->i_mode = exfat_make_mode(sbi, ATTR_SUBDIR, 0777);
333 inode->i_op = &exfat_dir_inode_operations;
334 inode->i_fop = &exfat_dir_operations;
335
336 inode->i_blocks = ((i_size_read(inode) + (sbi->cluster_size - 1))
337 & ~(sbi->cluster_size - 1)) >> inode->i_blkbits;
338 EXFAT_I(inode)->i_pos = ((loff_t)sbi->root_dir << 32) | 0xffffffff;
339 EXFAT_I(inode)->i_size_aligned = i_size_read(inode);
340 EXFAT_I(inode)->i_size_ondisk = i_size_read(inode);
341
342 exfat_save_attr(inode, ATTR_SUBDIR);
343 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
344 current_time(inode);
345 exfat_cache_init_inode(inode);
346 return 0;
347}
348
Tetsuhiro Kohadab0516832020-04-21 10:58:58 +0900349static struct pbr *exfat_read_pbr_with_logical_sector(struct super_block *sb)
Namjae Jeon719c1e12020-03-02 15:21:33 +0900350{
Tetsuhiro Kohadab0516832020-04-21 10:58:58 +0900351 struct exfat_sb_info *sbi = EXFAT_SB(sb);
352 struct pbr *p_pbr = (struct pbr *) (sbi->pbr_bh)->b_data;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900353 unsigned short logical_sect = 0;
354
355 logical_sect = 1 << p_pbr->bsx.f64.sect_size_bits;
356
357 if (!is_power_of_2(logical_sect) ||
358 logical_sect < 512 || logical_sect > 4096) {
359 exfat_msg(sb, KERN_ERR, "bogus logical sector size %u",
360 logical_sect);
361 return NULL;
362 }
363
364 if (logical_sect < sb->s_blocksize) {
365 exfat_msg(sb, KERN_ERR,
366 "logical sector size too small for device (logical sector size = %u)",
367 logical_sect);
368 return NULL;
369 }
370
371 if (logical_sect > sb->s_blocksize) {
Tetsuhiro Kohadab0516832020-04-21 10:58:58 +0900372 brelse(sbi->pbr_bh);
373 sbi->pbr_bh = NULL;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900374
375 if (!sb_set_blocksize(sb, logical_sect)) {
376 exfat_msg(sb, KERN_ERR,
377 "unable to set blocksize %u", logical_sect);
378 return NULL;
379 }
Tetsuhiro Kohadab0516832020-04-21 10:58:58 +0900380 sbi->pbr_bh = sb_bread(sb, 0);
381 if (!sbi->pbr_bh) {
Namjae Jeon719c1e12020-03-02 15:21:33 +0900382 exfat_msg(sb, KERN_ERR,
383 "unable to read boot sector (logical sector size = %lu)",
384 sb->s_blocksize);
385 return NULL;
386 }
387
Tetsuhiro Kohadab0516832020-04-21 10:58:58 +0900388 p_pbr = (struct pbr *)sbi->pbr_bh->b_data;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900389 }
390 return p_pbr;
391}
392
393/* mount the file system volume */
394static int __exfat_fill_super(struct super_block *sb)
395{
396 int ret;
397 struct pbr *p_pbr;
398 struct pbr64 *p_bpb;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900399 struct exfat_sb_info *sbi = EXFAT_SB(sb);
400
401 /* set block size to read super block */
402 sb_min_blocksize(sb, 512);
403
404 /* read boot sector */
Tetsuhiro Kohadab0516832020-04-21 10:58:58 +0900405 sbi->pbr_bh = sb_bread(sb, 0);
406 if (!sbi->pbr_bh) {
Namjae Jeon719c1e12020-03-02 15:21:33 +0900407 exfat_msg(sb, KERN_ERR, "unable to read boot sector");
408 return -EIO;
409 }
410
411 /* PRB is read */
Tetsuhiro Kohadab0516832020-04-21 10:58:58 +0900412 p_pbr = (struct pbr *)sbi->pbr_bh->b_data;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900413
414 /* check the validity of PBR */
415 if (le16_to_cpu((p_pbr->signature)) != PBR_SIGNATURE) {
416 exfat_msg(sb, KERN_ERR, "invalid boot record signature");
417 ret = -EINVAL;
418 goto free_bh;
419 }
420
421
422 /* check logical sector size */
Tetsuhiro Kohadab0516832020-04-21 10:58:58 +0900423 p_pbr = exfat_read_pbr_with_logical_sector(sb);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900424 if (!p_pbr) {
425 ret = -EIO;
426 goto free_bh;
427 }
428
429 /*
430 * res_zero field must be filled with zero to prevent mounting
431 * from FAT volume.
432 */
433 if (memchr_inv(p_pbr->bpb.f64.res_zero, 0,
434 sizeof(p_pbr->bpb.f64.res_zero))) {
435 ret = -EINVAL;
436 goto free_bh;
437 }
438
439 p_bpb = (struct pbr64 *)p_pbr;
440 if (!p_bpb->bsx.num_fats) {
441 exfat_msg(sb, KERN_ERR, "bogus number of FAT structure");
442 ret = -EINVAL;
443 goto free_bh;
444 }
445
446 sbi->sect_per_clus = 1 << p_bpb->bsx.sect_per_clus_bits;
447 sbi->sect_per_clus_bits = p_bpb->bsx.sect_per_clus_bits;
448 sbi->cluster_size_bits = sbi->sect_per_clus_bits + sb->s_blocksize_bits;
449 sbi->cluster_size = 1 << sbi->cluster_size_bits;
450 sbi->num_FAT_sectors = le32_to_cpu(p_bpb->bsx.fat_length);
451 sbi->FAT1_start_sector = le32_to_cpu(p_bpb->bsx.fat_offset);
452 sbi->FAT2_start_sector = p_bpb->bsx.num_fats == 1 ?
453 sbi->FAT1_start_sector :
454 sbi->FAT1_start_sector + sbi->num_FAT_sectors;
455 sbi->data_start_sector = le32_to_cpu(p_bpb->bsx.clu_offset);
456 sbi->num_sectors = le64_to_cpu(p_bpb->bsx.vol_length);
457 /* because the cluster index starts with 2 */
458 sbi->num_clusters = le32_to_cpu(p_bpb->bsx.clu_count) +
459 EXFAT_RESERVED_CLUSTERS;
460
461 sbi->root_dir = le32_to_cpu(p_bpb->bsx.root_cluster);
462 sbi->dentries_per_clu = 1 <<
463 (sbi->cluster_size_bits - DENTRY_SIZE_BITS);
464
465 sbi->vol_flag = le16_to_cpu(p_bpb->bsx.vol_flags);
466 sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
467 sbi->used_clusters = EXFAT_CLUSTERS_UNTRACKED;
468
469 if (le16_to_cpu(p_bpb->bsx.vol_flags) & VOL_DIRTY) {
470 sbi->vol_flag |= VOL_DIRTY;
471 exfat_msg(sb, KERN_WARNING,
472 "Volume was not properly unmounted. Some data may be corrupt. Please run fsck.");
473 }
474
475 /* exFAT file size is limited by a disk volume size */
476 sb->s_maxbytes = (u64)(sbi->num_clusters - EXFAT_RESERVED_CLUSTERS) <<
477 sbi->cluster_size_bits;
478
479 ret = exfat_create_upcase_table(sb);
480 if (ret) {
481 exfat_msg(sb, KERN_ERR, "failed to load upcase table");
482 goto free_bh;
483 }
484
485 ret = exfat_load_bitmap(sb);
486 if (ret) {
487 exfat_msg(sb, KERN_ERR, "failed to load alloc-bitmap");
488 goto free_upcase_table;
489 }
490
491 ret = exfat_count_used_clusters(sb, &sbi->used_clusters);
492 if (ret) {
493 exfat_msg(sb, KERN_ERR, "failed to scan clusters");
494 goto free_alloc_bitmap;
495 }
496
497 return 0;
498
499free_alloc_bitmap:
500 exfat_free_bitmap(sbi);
501free_upcase_table:
502 exfat_free_upcase_table(sbi);
503free_bh:
Tetsuhiro Kohadab0516832020-04-21 10:58:58 +0900504 brelse(sbi->pbr_bh);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900505 return ret;
506}
507
508static int exfat_fill_super(struct super_block *sb, struct fs_context *fc)
509{
510 struct exfat_sb_info *sbi = sb->s_fs_info;
511 struct exfat_mount_options *opts = &sbi->options;
512 struct inode *root_inode;
513 int err;
514
515 if (opts->allow_utime == (unsigned short)-1)
516 opts->allow_utime = ~opts->fs_dmask & 0022;
517
518 if (opts->discard) {
519 struct request_queue *q = bdev_get_queue(sb->s_bdev);
520
Pali Rohárb7e038a2020-03-17 22:46:52 +0100521 if (!blk_queue_discard(q)) {
Namjae Jeon719c1e12020-03-02 15:21:33 +0900522 exfat_msg(sb, KERN_WARNING,
523 "mounting with \"discard\" option, but the device does not support discard");
Pali Rohárb7e038a2020-03-17 22:46:52 +0100524 opts->discard = 0;
525 }
Namjae Jeon719c1e12020-03-02 15:21:33 +0900526 }
527
528 sb->s_flags |= SB_NODIRATIME;
529 sb->s_magic = EXFAT_SUPER_MAGIC;
530 sb->s_op = &exfat_sops;
531
Eric Sandeen674a9982020-04-17 14:43:49 +0900532 sb->s_time_gran = 10 * NSEC_PER_MSEC;
Namjae Jeon719c1e12020-03-02 15:21:33 +0900533 sb->s_time_min = EXFAT_MIN_TIMESTAMP_SECS;
534 sb->s_time_max = EXFAT_MAX_TIMESTAMP_SECS;
535
536 err = __exfat_fill_super(sb);
537 if (err) {
538 exfat_msg(sb, KERN_ERR, "failed to recognize exfat type");
539 goto check_nls_io;
540 }
541
542 /* set up enough so that it can read an inode */
543 exfat_hash_init(sb);
544
545 if (!strcmp(sbi->options.iocharset, "utf8"))
546 opts->utf8 = 1;
547 else {
548 sbi->nls_io = load_nls(sbi->options.iocharset);
549 if (!sbi->nls_io) {
550 exfat_msg(sb, KERN_ERR, "IO charset %s not found",
551 sbi->options.iocharset);
552 err = -EINVAL;
553 goto free_table;
554 }
555 }
556
557 if (sbi->options.utf8)
558 sb->s_d_op = &exfat_utf8_dentry_ops;
559 else
560 sb->s_d_op = &exfat_dentry_ops;
561
562 root_inode = new_inode(sb);
563 if (!root_inode) {
564 exfat_msg(sb, KERN_ERR, "failed to allocate root inode.");
565 err = -ENOMEM;
566 goto free_table;
567 }
568
569 root_inode->i_ino = EXFAT_ROOT_INO;
570 inode_set_iversion(root_inode, 1);
571 err = exfat_read_root(root_inode);
572 if (err) {
573 exfat_msg(sb, KERN_ERR, "failed to initialize root inode.");
574 goto put_inode;
575 }
576
577 exfat_hash_inode(root_inode, EXFAT_I(root_inode)->i_pos);
578 insert_inode_hash(root_inode);
579
580 sb->s_root = d_make_root(root_inode);
581 if (!sb->s_root) {
582 exfat_msg(sb, KERN_ERR, "failed to get the root dentry");
583 err = -ENOMEM;
584 goto put_inode;
585 }
586
587 return 0;
588
589put_inode:
590 iput(root_inode);
591 sb->s_root = NULL;
592
593free_table:
594 exfat_free_upcase_table(sbi);
595 exfat_free_bitmap(sbi);
Tetsuhiro Kohadab0516832020-04-21 10:58:58 +0900596 brelse(sbi->pbr_bh);
Namjae Jeon719c1e12020-03-02 15:21:33 +0900597
598check_nls_io:
599 unload_nls(sbi->nls_io);
600 exfat_free_iocharset(sbi);
601 sb->s_fs_info = NULL;
602 kfree(sbi);
603 return err;
604}
605
606static int exfat_get_tree(struct fs_context *fc)
607{
608 return get_tree_bdev(fc, exfat_fill_super);
609}
610
611static void exfat_free(struct fs_context *fc)
612{
613 kfree(fc->s_fs_info);
614}
615
616static const struct fs_context_operations exfat_context_ops = {
617 .parse_param = exfat_parse_param,
618 .get_tree = exfat_get_tree,
619 .free = exfat_free,
620};
621
622static int exfat_init_fs_context(struct fs_context *fc)
623{
624 struct exfat_sb_info *sbi;
625
626 sbi = kzalloc(sizeof(struct exfat_sb_info), GFP_KERNEL);
627 if (!sbi)
628 return -ENOMEM;
629
630 mutex_init(&sbi->s_lock);
631 ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
632 DEFAULT_RATELIMIT_BURST);
633
634 sbi->options.fs_uid = current_uid();
635 sbi->options.fs_gid = current_gid();
636 sbi->options.fs_fmask = current->fs->umask;
637 sbi->options.fs_dmask = current->fs->umask;
638 sbi->options.allow_utime = -1;
639 sbi->options.iocharset = exfat_default_iocharset;
640 sbi->options.errors = EXFAT_ERRORS_RO;
641
642 fc->s_fs_info = sbi;
643 fc->ops = &exfat_context_ops;
644 return 0;
645}
646
647static struct file_system_type exfat_fs_type = {
648 .owner = THIS_MODULE,
649 .name = "exfat",
650 .init_fs_context = exfat_init_fs_context,
Valdis Kletnieks9acd0d52020-03-02 15:21:45 +0900651 .parameters = exfat_parameters,
Namjae Jeon719c1e12020-03-02 15:21:33 +0900652 .kill_sb = kill_block_super,
653 .fs_flags = FS_REQUIRES_DEV,
654};
655
656static void exfat_inode_init_once(void *foo)
657{
658 struct exfat_inode_info *ei = (struct exfat_inode_info *)foo;
659
660 INIT_HLIST_NODE(&ei->i_hash_fat);
661 inode_init_once(&ei->vfs_inode);
662}
663
664static int __init init_exfat_fs(void)
665{
666 int err;
667
668 err = exfat_cache_init();
669 if (err)
670 return err;
671
672 exfat_inode_cachep = kmem_cache_create("exfat_inode_cache",
673 sizeof(struct exfat_inode_info),
674 0, SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
675 exfat_inode_init_once);
676 if (!exfat_inode_cachep) {
677 err = -ENOMEM;
678 goto shutdown_cache;
679 }
680
681 err = register_filesystem(&exfat_fs_type);
682 if (err)
683 goto destroy_cache;
684
685 return 0;
686
687destroy_cache:
688 kmem_cache_destroy(exfat_inode_cachep);
689shutdown_cache:
690 exfat_cache_shutdown();
691 return err;
692}
693
694static void __exit exit_exfat_fs(void)
695{
696 /*
697 * Make sure all delayed rcu free inodes are flushed before we
698 * destroy cache.
699 */
700 rcu_barrier();
701 kmem_cache_destroy(exfat_inode_cachep);
702 unregister_filesystem(&exfat_fs_type);
703 exfat_cache_shutdown();
704}
705
706module_init(init_exfat_fs);
707module_exit(exit_exfat_fs);
708
Thomas Backlundcd76ac22020-04-04 23:29:43 +0300709MODULE_ALIAS_FS("exfat");
Namjae Jeon719c1e12020-03-02 15:21:33 +0900710MODULE_LICENSE("GPL");
711MODULE_DESCRIPTION("exFAT filesystem support");
712MODULE_AUTHOR("Samsung Electronics Co., Ltd.");