exfat: remove the assignment of 0 to bool variable
[linux-2.6-microblaze.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
21 #include "exfat_raw.h"
22 #include "exfat_fs.h"
23
24 static char exfat_default_iocharset[] = CONFIG_EXFAT_DEFAULT_IOCHARSET;
25 static struct kmem_cache *exfat_inode_cachep;
26
27 static 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
33 static 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
43 static 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);
52         brelse(sbi->pbr_bh);
53         mutex_unlock(&sbi->s_lock);
54
55         call_rcu(&sbi->rcu, exfat_delayed_free);
56 }
57
58 static 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
74 static 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
101 int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flag)
102 {
103         struct exfat_sb_info *sbi = EXFAT_SB(sb);
104         struct pbr64 *bpb = (struct pbr64 *)sbi->pbr_bh->b_data;
105         bool sync;
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
119         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
134 static int exfat_show_options(struct seq_file *m, struct dentry *root)
135 {
136         struct super_block *sb = root->d_sb;
137         struct exfat_sb_info *sbi = EXFAT_SB(sb);
138         struct exfat_mount_options *opts = &sbi->options;
139
140         /* Show partition info */
141         if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID))
142                 seq_printf(m, ",uid=%u",
143                                 from_kuid_munged(&init_user_ns, opts->fs_uid));
144         if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID))
145                 seq_printf(m, ",gid=%u",
146                                 from_kgid_munged(&init_user_ns, opts->fs_gid));
147         seq_printf(m, ",fmask=%04o,dmask=%04o", opts->fs_fmask, opts->fs_dmask);
148         if (opts->allow_utime)
149                 seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
150         if (opts->utf8)
151                 seq_puts(m, ",iocharset=utf8");
152         else if (sbi->nls_io)
153                 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
154         if (opts->errors == EXFAT_ERRORS_CONT)
155                 seq_puts(m, ",errors=continue");
156         else if (opts->errors == EXFAT_ERRORS_PANIC)
157                 seq_puts(m, ",errors=panic");
158         else
159                 seq_puts(m, ",errors=remount-ro");
160         if (opts->discard)
161                 seq_puts(m, ",discard");
162         if (opts->time_offset)
163                 seq_printf(m, ",time_offset=%d", opts->time_offset);
164         return 0;
165 }
166
167 static 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
179 static void exfat_free_inode(struct inode *inode)
180 {
181         kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode));
182 }
183
184 static 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
195 enum {
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         /* Deprecated options */
208         Opt_utf8,
209         Opt_debug,
210         Opt_namecase,
211         Opt_codepage,
212 };
213
214 static const struct constant_table exfat_param_enums[] = {
215         { "continue",           EXFAT_ERRORS_CONT },
216         { "panic",              EXFAT_ERRORS_PANIC },
217         { "remount-ro",         EXFAT_ERRORS_RO },
218         {}
219 };
220
221 static const struct fs_parameter_spec exfat_parameters[] = {
222         fsparam_u32("uid",                      Opt_uid),
223         fsparam_u32("gid",                      Opt_gid),
224         fsparam_u32oct("umask",                 Opt_umask),
225         fsparam_u32oct("dmask",                 Opt_dmask),
226         fsparam_u32oct("fmask",                 Opt_fmask),
227         fsparam_u32oct("allow_utime",           Opt_allow_utime),
228         fsparam_string("iocharset",             Opt_charset),
229         fsparam_enum("errors",                  Opt_errors, exfat_param_enums),
230         fsparam_flag("discard",                 Opt_discard),
231         fsparam_s32("time_offset",              Opt_time_offset),
232         __fsparam(NULL, "utf8",                 Opt_utf8, fs_param_deprecated,
233                   NULL),
234         __fsparam(NULL, "debug",                Opt_debug, fs_param_deprecated,
235                   NULL),
236         __fsparam(fs_param_is_u32, "namecase",  Opt_namecase,
237                   fs_param_deprecated, NULL),
238         __fsparam(fs_param_is_u32, "codepage",  Opt_codepage,
239                   fs_param_deprecated, NULL),
240         {}
241 };
242
243 static int exfat_parse_param(struct fs_context *fc, struct fs_parameter *param)
244 {
245         struct exfat_sb_info *sbi = fc->s_fs_info;
246         struct exfat_mount_options *opts = &sbi->options;
247         struct fs_parse_result result;
248         int opt;
249
250         opt = fs_parse(fc, exfat_parameters, param, &result);
251         if (opt < 0)
252                 return opt;
253
254         switch (opt) {
255         case Opt_uid:
256                 opts->fs_uid = make_kuid(current_user_ns(), result.uint_32);
257                 break;
258         case Opt_gid:
259                 opts->fs_gid = make_kgid(current_user_ns(), result.uint_32);
260                 break;
261         case Opt_umask:
262                 opts->fs_fmask = result.uint_32;
263                 opts->fs_dmask = result.uint_32;
264                 break;
265         case Opt_dmask:
266                 opts->fs_dmask = result.uint_32;
267                 break;
268         case Opt_fmask:
269                 opts->fs_fmask = result.uint_32;
270                 break;
271         case Opt_allow_utime:
272                 opts->allow_utime = result.uint_32 & 0022;
273                 break;
274         case Opt_charset:
275                 exfat_free_iocharset(sbi);
276                 opts->iocharset = kstrdup(param->string, GFP_KERNEL);
277                 if (!opts->iocharset)
278                         return -ENOMEM;
279                 break;
280         case Opt_errors:
281                 opts->errors = result.uint_32;
282                 break;
283         case Opt_discard:
284                 opts->discard = 1;
285                 break;
286         case Opt_time_offset:
287                 /*
288                  * Make the limit 24 just in case someone invents something
289                  * unusual.
290                  */
291                 if (result.int_32 < -24 * 60 || result.int_32 > 24 * 60)
292                         return -EINVAL;
293                 opts->time_offset = result.int_32;
294                 break;
295         case Opt_utf8:
296         case Opt_debug:
297         case Opt_namecase:
298         case Opt_codepage:
299                 break;
300         default:
301                 return -EINVAL;
302         }
303
304         return 0;
305 }
306
307 static void exfat_hash_init(struct super_block *sb)
308 {
309         struct exfat_sb_info *sbi = EXFAT_SB(sb);
310         int i;
311
312         spin_lock_init(&sbi->inode_hash_lock);
313         for (i = 0; i < EXFAT_HASH_SIZE; i++)
314                 INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
315 }
316
317 static int exfat_read_root(struct inode *inode)
318 {
319         struct super_block *sb = inode->i_sb;
320         struct exfat_sb_info *sbi = EXFAT_SB(sb);
321         struct exfat_inode_info *ei = EXFAT_I(inode);
322         struct exfat_chain cdir;
323         int num_subdirs, num_clu = 0;
324
325         exfat_chain_set(&ei->dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
326         ei->entry = -1;
327         ei->start_clu = sbi->root_dir;
328         ei->flags = ALLOC_FAT_CHAIN;
329         ei->type = TYPE_DIR;
330         ei->version = 0;
331         ei->rwoffset = 0;
332         ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
333         ei->hint_stat.eidx = 0;
334         ei->hint_stat.clu = sbi->root_dir;
335         ei->hint_femp.eidx = EXFAT_HINT_NONE;
336
337         exfat_chain_set(&cdir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
338         if (exfat_count_num_clusters(sb, &cdir, &num_clu))
339                 return -EIO;
340         i_size_write(inode, num_clu << sbi->cluster_size_bits);
341
342         num_subdirs = exfat_count_dir_entries(sb, &cdir);
343         if (num_subdirs < 0)
344                 return -EIO;
345         set_nlink(inode, num_subdirs + EXFAT_MIN_SUBDIR);
346
347         inode->i_uid = sbi->options.fs_uid;
348         inode->i_gid = sbi->options.fs_gid;
349         inode_inc_iversion(inode);
350         inode->i_generation = 0;
351         inode->i_mode = exfat_make_mode(sbi, ATTR_SUBDIR, 0777);
352         inode->i_op = &exfat_dir_inode_operations;
353         inode->i_fop = &exfat_dir_operations;
354
355         inode->i_blocks = ((i_size_read(inode) + (sbi->cluster_size - 1))
356                         & ~(sbi->cluster_size - 1)) >> inode->i_blkbits;
357         EXFAT_I(inode)->i_pos = ((loff_t)sbi->root_dir << 32) | 0xffffffff;
358         EXFAT_I(inode)->i_size_aligned = i_size_read(inode);
359         EXFAT_I(inode)->i_size_ondisk = i_size_read(inode);
360
361         exfat_save_attr(inode, ATTR_SUBDIR);
362         inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
363                 current_time(inode);
364         exfat_truncate_atime(&inode->i_atime);
365         exfat_cache_init_inode(inode);
366         return 0;
367 }
368
369 static struct pbr *exfat_read_pbr_with_logical_sector(struct super_block *sb)
370 {
371         struct exfat_sb_info *sbi = EXFAT_SB(sb);
372         struct pbr *p_pbr = (struct pbr *) (sbi->pbr_bh)->b_data;
373         unsigned short logical_sect = 0;
374
375         logical_sect = 1 << p_pbr->bsx.f64.sect_size_bits;
376
377         if (!is_power_of_2(logical_sect) ||
378             logical_sect < 512 || logical_sect > 4096) {
379                 exfat_err(sb, "bogus logical sector size %u", logical_sect);
380                 return NULL;
381         }
382
383         if (logical_sect < sb->s_blocksize) {
384                 exfat_err(sb, "logical sector size too small for device (logical sector size = %u)",
385                           logical_sect);
386                 return NULL;
387         }
388
389         if (logical_sect > sb->s_blocksize) {
390                 brelse(sbi->pbr_bh);
391                 sbi->pbr_bh = NULL;
392
393                 if (!sb_set_blocksize(sb, logical_sect)) {
394                         exfat_err(sb, "unable to set blocksize %u",
395                                   logical_sect);
396                         return NULL;
397                 }
398                 sbi->pbr_bh = sb_bread(sb, 0);
399                 if (!sbi->pbr_bh) {
400                         exfat_err(sb, "unable to read boot sector (logical sector size = %lu)",
401                                   sb->s_blocksize);
402                         return NULL;
403                 }
404
405                 p_pbr = (struct pbr *)sbi->pbr_bh->b_data;
406         }
407         return p_pbr;
408 }
409
410 /* mount the file system volume */
411 static int __exfat_fill_super(struct super_block *sb)
412 {
413         int ret;
414         struct pbr *p_pbr;
415         struct pbr64 *p_bpb;
416         struct exfat_sb_info *sbi = EXFAT_SB(sb);
417
418         /* set block size to read super block */
419         sb_min_blocksize(sb, 512);
420
421         /* read boot sector */
422         sbi->pbr_bh = sb_bread(sb, 0);
423         if (!sbi->pbr_bh) {
424                 exfat_err(sb, "unable to read boot sector");
425                 return -EIO;
426         }
427
428         /* PRB is read */
429         p_pbr = (struct pbr *)sbi->pbr_bh->b_data;
430
431         /* check the validity of PBR */
432         if (le16_to_cpu((p_pbr->signature)) != PBR_SIGNATURE) {
433                 exfat_err(sb, "invalid boot record signature");
434                 ret = -EINVAL;
435                 goto free_bh;
436         }
437
438
439         /* check logical sector size */
440         p_pbr = exfat_read_pbr_with_logical_sector(sb);
441         if (!p_pbr) {
442                 ret = -EIO;
443                 goto free_bh;
444         }
445
446         /*
447          * res_zero field must be filled with zero to prevent mounting
448          * from FAT volume.
449          */
450         if (memchr_inv(p_pbr->bpb.f64.res_zero, 0,
451                         sizeof(p_pbr->bpb.f64.res_zero))) {
452                 ret = -EINVAL;
453                 goto free_bh;
454         }
455
456         p_bpb = (struct pbr64 *)p_pbr;
457         if (!p_bpb->bsx.num_fats) {
458                 exfat_err(sb, "bogus number of FAT structure");
459                 ret = -EINVAL;
460                 goto free_bh;
461         }
462
463         sbi->sect_per_clus = 1 << p_bpb->bsx.sect_per_clus_bits;
464         sbi->sect_per_clus_bits = p_bpb->bsx.sect_per_clus_bits;
465         sbi->cluster_size_bits = sbi->sect_per_clus_bits + sb->s_blocksize_bits;
466         sbi->cluster_size = 1 << sbi->cluster_size_bits;
467         sbi->num_FAT_sectors = le32_to_cpu(p_bpb->bsx.fat_length);
468         sbi->FAT1_start_sector = le32_to_cpu(p_bpb->bsx.fat_offset);
469         sbi->FAT2_start_sector = p_bpb->bsx.num_fats == 1 ?
470                 sbi->FAT1_start_sector :
471                         sbi->FAT1_start_sector + sbi->num_FAT_sectors;
472         sbi->data_start_sector = le32_to_cpu(p_bpb->bsx.clu_offset);
473         sbi->num_sectors = le64_to_cpu(p_bpb->bsx.vol_length);
474         /* because the cluster index starts with 2 */
475         sbi->num_clusters = le32_to_cpu(p_bpb->bsx.clu_count) +
476                 EXFAT_RESERVED_CLUSTERS;
477
478         sbi->root_dir = le32_to_cpu(p_bpb->bsx.root_cluster);
479         sbi->dentries_per_clu = 1 <<
480                 (sbi->cluster_size_bits - DENTRY_SIZE_BITS);
481
482         sbi->vol_flag = le16_to_cpu(p_bpb->bsx.vol_flags);
483         sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
484         sbi->used_clusters = EXFAT_CLUSTERS_UNTRACKED;
485
486         if (le16_to_cpu(p_bpb->bsx.vol_flags) & VOL_DIRTY) {
487                 sbi->vol_flag |= VOL_DIRTY;
488                 exfat_warn(sb, "Volume was not properly unmounted. Some data may be corrupt. Please run fsck.");
489         }
490
491         /* exFAT file size is limited by a disk volume size */
492         sb->s_maxbytes = (u64)(sbi->num_clusters - EXFAT_RESERVED_CLUSTERS) <<
493                 sbi->cluster_size_bits;
494
495         ret = exfat_create_upcase_table(sb);
496         if (ret) {
497                 exfat_err(sb, "failed to load upcase table");
498                 goto free_bh;
499         }
500
501         ret = exfat_load_bitmap(sb);
502         if (ret) {
503                 exfat_err(sb, "failed to load alloc-bitmap");
504                 goto free_upcase_table;
505         }
506
507         ret = exfat_count_used_clusters(sb, &sbi->used_clusters);
508         if (ret) {
509                 exfat_err(sb, "failed to scan clusters");
510                 goto free_alloc_bitmap;
511         }
512
513         return 0;
514
515 free_alloc_bitmap:
516         exfat_free_bitmap(sbi);
517 free_upcase_table:
518         exfat_free_upcase_table(sbi);
519 free_bh:
520         brelse(sbi->pbr_bh);
521         return ret;
522 }
523
524 static int exfat_fill_super(struct super_block *sb, struct fs_context *fc)
525 {
526         struct exfat_sb_info *sbi = sb->s_fs_info;
527         struct exfat_mount_options *opts = &sbi->options;
528         struct inode *root_inode;
529         int err;
530
531         if (opts->allow_utime == (unsigned short)-1)
532                 opts->allow_utime = ~opts->fs_dmask & 0022;
533
534         if (opts->discard) {
535                 struct request_queue *q = bdev_get_queue(sb->s_bdev);
536
537                 if (!blk_queue_discard(q)) {
538                         exfat_warn(sb, "mounting with \"discard\" option, but the device does not support discard");
539                         opts->discard = 0;
540                 }
541         }
542
543         sb->s_flags |= SB_NODIRATIME;
544         sb->s_magic = EXFAT_SUPER_MAGIC;
545         sb->s_op = &exfat_sops;
546
547         sb->s_time_gran = 10 * NSEC_PER_MSEC;
548         sb->s_time_min = EXFAT_MIN_TIMESTAMP_SECS;
549         sb->s_time_max = EXFAT_MAX_TIMESTAMP_SECS;
550
551         err = __exfat_fill_super(sb);
552         if (err) {
553                 exfat_err(sb, "failed to recognize exfat type");
554                 goto check_nls_io;
555         }
556
557         /* set up enough so that it can read an inode */
558         exfat_hash_init(sb);
559
560         if (!strcmp(sbi->options.iocharset, "utf8"))
561                 opts->utf8 = 1;
562         else {
563                 sbi->nls_io = load_nls(sbi->options.iocharset);
564                 if (!sbi->nls_io) {
565                         exfat_err(sb, "IO charset %s not found",
566                                   sbi->options.iocharset);
567                         err = -EINVAL;
568                         goto free_table;
569                 }
570         }
571
572         if (sbi->options.utf8)
573                 sb->s_d_op = &exfat_utf8_dentry_ops;
574         else
575                 sb->s_d_op = &exfat_dentry_ops;
576
577         root_inode = new_inode(sb);
578         if (!root_inode) {
579                 exfat_err(sb, "failed to allocate root inode");
580                 err = -ENOMEM;
581                 goto free_table;
582         }
583
584         root_inode->i_ino = EXFAT_ROOT_INO;
585         inode_set_iversion(root_inode, 1);
586         err = exfat_read_root(root_inode);
587         if (err) {
588                 exfat_err(sb, "failed to initialize root inode");
589                 goto put_inode;
590         }
591
592         exfat_hash_inode(root_inode, EXFAT_I(root_inode)->i_pos);
593         insert_inode_hash(root_inode);
594
595         sb->s_root = d_make_root(root_inode);
596         if (!sb->s_root) {
597                 exfat_err(sb, "failed to get the root dentry");
598                 err = -ENOMEM;
599                 goto put_inode;
600         }
601
602         return 0;
603
604 put_inode:
605         iput(root_inode);
606         sb->s_root = NULL;
607
608 free_table:
609         exfat_free_upcase_table(sbi);
610         exfat_free_bitmap(sbi);
611         brelse(sbi->pbr_bh);
612
613 check_nls_io:
614         unload_nls(sbi->nls_io);
615         exfat_free_iocharset(sbi);
616         sb->s_fs_info = NULL;
617         kfree(sbi);
618         return err;
619 }
620
621 static int exfat_get_tree(struct fs_context *fc)
622 {
623         return get_tree_bdev(fc, exfat_fill_super);
624 }
625
626 static void exfat_free(struct fs_context *fc)
627 {
628         kfree(fc->s_fs_info);
629 }
630
631 static const struct fs_context_operations exfat_context_ops = {
632         .parse_param    = exfat_parse_param,
633         .get_tree       = exfat_get_tree,
634         .free           = exfat_free,
635 };
636
637 static int exfat_init_fs_context(struct fs_context *fc)
638 {
639         struct exfat_sb_info *sbi;
640
641         sbi = kzalloc(sizeof(struct exfat_sb_info), GFP_KERNEL);
642         if (!sbi)
643                 return -ENOMEM;
644
645         mutex_init(&sbi->s_lock);
646         ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
647                         DEFAULT_RATELIMIT_BURST);
648
649         sbi->options.fs_uid = current_uid();
650         sbi->options.fs_gid = current_gid();
651         sbi->options.fs_fmask = current->fs->umask;
652         sbi->options.fs_dmask = current->fs->umask;
653         sbi->options.allow_utime = -1;
654         sbi->options.iocharset = exfat_default_iocharset;
655         sbi->options.errors = EXFAT_ERRORS_RO;
656
657         fc->s_fs_info = sbi;
658         fc->ops = &exfat_context_ops;
659         return 0;
660 }
661
662 static struct file_system_type exfat_fs_type = {
663         .owner                  = THIS_MODULE,
664         .name                   = "exfat",
665         .init_fs_context        = exfat_init_fs_context,
666         .parameters             = exfat_parameters,
667         .kill_sb                = kill_block_super,
668         .fs_flags               = FS_REQUIRES_DEV,
669 };
670
671 static void exfat_inode_init_once(void *foo)
672 {
673         struct exfat_inode_info *ei = (struct exfat_inode_info *)foo;
674
675         INIT_HLIST_NODE(&ei->i_hash_fat);
676         inode_init_once(&ei->vfs_inode);
677 }
678
679 static int __init init_exfat_fs(void)
680 {
681         int err;
682
683         err = exfat_cache_init();
684         if (err)
685                 return err;
686
687         exfat_inode_cachep = kmem_cache_create("exfat_inode_cache",
688                         sizeof(struct exfat_inode_info),
689                         0, SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
690                         exfat_inode_init_once);
691         if (!exfat_inode_cachep) {
692                 err = -ENOMEM;
693                 goto shutdown_cache;
694         }
695
696         err = register_filesystem(&exfat_fs_type);
697         if (err)
698                 goto destroy_cache;
699
700         return 0;
701
702 destroy_cache:
703         kmem_cache_destroy(exfat_inode_cachep);
704 shutdown_cache:
705         exfat_cache_shutdown();
706         return err;
707 }
708
709 static void __exit exit_exfat_fs(void)
710 {
711         /*
712          * Make sure all delayed rcu free inodes are flushed before we
713          * destroy cache.
714          */
715         rcu_barrier();
716         kmem_cache_destroy(exfat_inode_cachep);
717         unregister_filesystem(&exfat_fs_type);
718         exfat_cache_shutdown();
719 }
720
721 module_init(init_exfat_fs);
722 module_exit(exit_exfat_fs);
723
724 MODULE_ALIAS_FS("exfat");
725 MODULE_LICENSE("GPL");
726 MODULE_DESCRIPTION("exFAT filesystem support");
727 MODULE_AUTHOR("Samsung Electronics Co., Ltd.");