Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *
0004  * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
0005  *
0006  *
0007  *                 terminology
0008  *
0009  * cluster - allocation unit     - 512,1K,2K,4K,...,2M
0010  * vcn - virtual cluster number  - Offset inside the file in clusters.
0011  * vbo - virtual byte offset     - Offset inside the file in bytes.
0012  * lcn - logical cluster number  - 0 based cluster in clusters heap.
0013  * lbo - logical byte offset     - Absolute position inside volume.
0014  * run - maps VCN to LCN         - Stored in attributes in packed form.
0015  * attr - attribute segment      - std/name/data etc records inside MFT.
0016  * mi  - MFT inode               - One MFT record(usually 1024 bytes or 4K), consists of attributes.
0017  * ni  - NTFS inode              - Extends linux inode. consists of one or more mft inodes.
0018  * index - unit inside directory - 2K, 4K, <=page size, does not depend on cluster size.
0019  *
0020  * WSL - Windows Subsystem for Linux
0021  * https://docs.microsoft.com/en-us/windows/wsl/file-permissions
0022  * It stores uid/gid/mode/dev in xattr
0023  *
0024  */
0025 
0026 #include <linux/blkdev.h>
0027 #include <linux/buffer_head.h>
0028 #include <linux/exportfs.h>
0029 #include <linux/fs.h>
0030 #include <linux/fs_context.h>
0031 #include <linux/fs_parser.h>
0032 #include <linux/log2.h>
0033 #include <linux/minmax.h>
0034 #include <linux/module.h>
0035 #include <linux/nls.h>
0036 #include <linux/seq_file.h>
0037 #include <linux/statfs.h>
0038 
0039 #include "debug.h"
0040 #include "ntfs.h"
0041 #include "ntfs_fs.h"
0042 #ifdef CONFIG_NTFS3_LZX_XPRESS
0043 #include "lib/lib.h"
0044 #endif
0045 
0046 #ifdef CONFIG_PRINTK
0047 /*
0048  * ntfs_printk - Trace warnings/notices/errors.
0049  *
0050  * Thanks Joe Perches <joe@perches.com> for implementation
0051  */
0052 void ntfs_printk(const struct super_block *sb, const char *fmt, ...)
0053 {
0054     struct va_format vaf;
0055     va_list args;
0056     int level;
0057     struct ntfs_sb_info *sbi = sb->s_fs_info;
0058 
0059     /* Should we use different ratelimits for warnings/notices/errors? */
0060     if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
0061         return;
0062 
0063     va_start(args, fmt);
0064 
0065     level = printk_get_level(fmt);
0066     vaf.fmt = printk_skip_level(fmt);
0067     vaf.va = &args;
0068     printk("%c%cntfs3: %s: %pV\n", KERN_SOH_ASCII, level, sb->s_id, &vaf);
0069 
0070     va_end(args);
0071 }
0072 
0073 static char s_name_buf[512];
0074 static atomic_t s_name_buf_cnt = ATOMIC_INIT(1); // 1 means 'free s_name_buf'.
0075 
0076 /*
0077  * ntfs_inode_printk
0078  *
0079  * Print warnings/notices/errors about inode using name or inode number.
0080  */
0081 void ntfs_inode_printk(struct inode *inode, const char *fmt, ...)
0082 {
0083     struct super_block *sb = inode->i_sb;
0084     struct ntfs_sb_info *sbi = sb->s_fs_info;
0085     char *name;
0086     va_list args;
0087     struct va_format vaf;
0088     int level;
0089 
0090     if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
0091         return;
0092 
0093     /* Use static allocated buffer, if possible. */
0094     name = atomic_dec_and_test(&s_name_buf_cnt)
0095                ? s_name_buf
0096                : kmalloc(sizeof(s_name_buf), GFP_NOFS);
0097 
0098     if (name) {
0099         struct dentry *de = d_find_alias(inode);
0100         const u32 name_len = ARRAY_SIZE(s_name_buf) - 1;
0101 
0102         if (de) {
0103             spin_lock(&de->d_lock);
0104             snprintf(name, name_len, " \"%s\"", de->d_name.name);
0105             spin_unlock(&de->d_lock);
0106             name[name_len] = 0; /* To be sure. */
0107         } else {
0108             name[0] = 0;
0109         }
0110         dput(de); /* Cocci warns if placed in branch "if (de)" */
0111     }
0112 
0113     va_start(args, fmt);
0114 
0115     level = printk_get_level(fmt);
0116     vaf.fmt = printk_skip_level(fmt);
0117     vaf.va = &args;
0118 
0119     printk("%c%cntfs3: %s: ino=%lx,%s %pV\n", KERN_SOH_ASCII, level,
0120            sb->s_id, inode->i_ino, name ? name : "", &vaf);
0121 
0122     va_end(args);
0123 
0124     atomic_inc(&s_name_buf_cnt);
0125     if (name != s_name_buf)
0126         kfree(name);
0127 }
0128 #endif
0129 
0130 /*
0131  * Shared memory struct.
0132  *
0133  * On-disk ntfs's upcase table is created by ntfs formatter.
0134  * 'upcase' table is 128K bytes of memory.
0135  * We should read it into memory when mounting.
0136  * Several ntfs volumes likely use the same 'upcase' table.
0137  * It is good idea to share in-memory 'upcase' table between different volumes.
0138  * Unfortunately winxp/vista/win7 use different upcase tables.
0139  */
0140 static DEFINE_SPINLOCK(s_shared_lock);
0141 
0142 static struct {
0143     void *ptr;
0144     u32 len;
0145     int cnt;
0146 } s_shared[8];
0147 
0148 /*
0149  * ntfs_set_shared
0150  *
0151  * Return:
0152  * * @ptr - If pointer was saved in shared memory.
0153  * * NULL - If pointer was not shared.
0154  */
0155 void *ntfs_set_shared(void *ptr, u32 bytes)
0156 {
0157     void *ret = NULL;
0158     int i, j = -1;
0159 
0160     spin_lock(&s_shared_lock);
0161     for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
0162         if (!s_shared[i].cnt) {
0163             j = i;
0164         } else if (bytes == s_shared[i].len &&
0165                !memcmp(s_shared[i].ptr, ptr, bytes)) {
0166             s_shared[i].cnt += 1;
0167             ret = s_shared[i].ptr;
0168             break;
0169         }
0170     }
0171 
0172     if (!ret && j != -1) {
0173         s_shared[j].ptr = ptr;
0174         s_shared[j].len = bytes;
0175         s_shared[j].cnt = 1;
0176         ret = ptr;
0177     }
0178     spin_unlock(&s_shared_lock);
0179 
0180     return ret;
0181 }
0182 
0183 /*
0184  * ntfs_put_shared
0185  *
0186  * Return:
0187  * * @ptr - If pointer is not shared anymore.
0188  * * NULL - If pointer is still shared.
0189  */
0190 void *ntfs_put_shared(void *ptr)
0191 {
0192     void *ret = ptr;
0193     int i;
0194 
0195     spin_lock(&s_shared_lock);
0196     for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
0197         if (s_shared[i].cnt && s_shared[i].ptr == ptr) {
0198             if (--s_shared[i].cnt)
0199                 ret = NULL;
0200             break;
0201         }
0202     }
0203     spin_unlock(&s_shared_lock);
0204 
0205     return ret;
0206 }
0207 
0208 static inline void put_mount_options(struct ntfs_mount_options *options)
0209 {
0210     kfree(options->nls_name);
0211     unload_nls(options->nls);
0212     kfree(options);
0213 }
0214 
0215 enum Opt {
0216     Opt_uid,
0217     Opt_gid,
0218     Opt_umask,
0219     Opt_dmask,
0220     Opt_fmask,
0221     Opt_immutable,
0222     Opt_discard,
0223     Opt_force,
0224     Opt_sparse,
0225     Opt_nohidden,
0226     Opt_showmeta,
0227     Opt_acl,
0228     Opt_iocharset,
0229     Opt_prealloc,
0230     Opt_noacsrules,
0231     Opt_err,
0232 };
0233 
0234 static const struct fs_parameter_spec ntfs_fs_parameters[] = {
0235     fsparam_u32("uid",          Opt_uid),
0236     fsparam_u32("gid",          Opt_gid),
0237     fsparam_u32oct("umask",         Opt_umask),
0238     fsparam_u32oct("dmask",         Opt_dmask),
0239     fsparam_u32oct("fmask",         Opt_fmask),
0240     fsparam_flag_no("sys_immutable",    Opt_immutable),
0241     fsparam_flag_no("discard",      Opt_discard),
0242     fsparam_flag_no("force",        Opt_force),
0243     fsparam_flag_no("sparse",       Opt_sparse),
0244     fsparam_flag_no("hidden",       Opt_nohidden),
0245     fsparam_flag_no("acl",          Opt_acl),
0246     fsparam_flag_no("showmeta",     Opt_showmeta),
0247     fsparam_flag_no("prealloc",     Opt_prealloc),
0248     fsparam_flag_no("acsrules",     Opt_noacsrules),
0249     fsparam_string("iocharset",     Opt_iocharset),
0250     {}
0251 };
0252 
0253 /*
0254  * Load nls table or if @nls is utf8 then return NULL.
0255  */
0256 static struct nls_table *ntfs_load_nls(char *nls)
0257 {
0258     struct nls_table *ret;
0259 
0260     if (!nls)
0261         nls = CONFIG_NLS_DEFAULT;
0262 
0263     if (strcmp(nls, "utf8") == 0)
0264         return NULL;
0265 
0266     if (strcmp(nls, CONFIG_NLS_DEFAULT) == 0)
0267         return load_nls_default();
0268 
0269     ret = load_nls(nls);
0270     if (ret)
0271         return ret;
0272 
0273     return ERR_PTR(-EINVAL);
0274 }
0275 
0276 static int ntfs_fs_parse_param(struct fs_context *fc,
0277                    struct fs_parameter *param)
0278 {
0279     struct ntfs_mount_options *opts = fc->fs_private;
0280     struct fs_parse_result result;
0281     int opt;
0282 
0283     opt = fs_parse(fc, ntfs_fs_parameters, param, &result);
0284     if (opt < 0)
0285         return opt;
0286 
0287     switch (opt) {
0288     case Opt_uid:
0289         opts->fs_uid = make_kuid(current_user_ns(), result.uint_32);
0290         if (!uid_valid(opts->fs_uid))
0291             return invalf(fc, "ntfs3: Invalid value for uid.");
0292         break;
0293     case Opt_gid:
0294         opts->fs_gid = make_kgid(current_user_ns(), result.uint_32);
0295         if (!gid_valid(opts->fs_gid))
0296             return invalf(fc, "ntfs3: Invalid value for gid.");
0297         break;
0298     case Opt_umask:
0299         if (result.uint_32 & ~07777)
0300             return invalf(fc, "ntfs3: Invalid value for umask.");
0301         opts->fs_fmask_inv = ~result.uint_32;
0302         opts->fs_dmask_inv = ~result.uint_32;
0303         opts->fmask = 1;
0304         opts->dmask = 1;
0305         break;
0306     case Opt_dmask:
0307         if (result.uint_32 & ~07777)
0308             return invalf(fc, "ntfs3: Invalid value for dmask.");
0309         opts->fs_dmask_inv = ~result.uint_32;
0310         opts->dmask = 1;
0311         break;
0312     case Opt_fmask:
0313         if (result.uint_32 & ~07777)
0314             return invalf(fc, "ntfs3: Invalid value for fmask.");
0315         opts->fs_fmask_inv = ~result.uint_32;
0316         opts->fmask = 1;
0317         break;
0318     case Opt_immutable:
0319         opts->sys_immutable = result.negated ? 0 : 1;
0320         break;
0321     case Opt_discard:
0322         opts->discard = result.negated ? 0 : 1;
0323         break;
0324     case Opt_force:
0325         opts->force = result.negated ? 0 : 1;
0326         break;
0327     case Opt_sparse:
0328         opts->sparse = result.negated ? 0 : 1;
0329         break;
0330     case Opt_nohidden:
0331         opts->nohidden = result.negated ? 1 : 0;
0332         break;
0333     case Opt_acl:
0334         if (!result.negated)
0335 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
0336             fc->sb_flags |= SB_POSIXACL;
0337 #else
0338             return invalf(fc, "ntfs3: Support for ACL not compiled in!");
0339 #endif
0340         else
0341             fc->sb_flags &= ~SB_POSIXACL;
0342         break;
0343     case Opt_showmeta:
0344         opts->showmeta = result.negated ? 0 : 1;
0345         break;
0346     case Opt_iocharset:
0347         kfree(opts->nls_name);
0348         opts->nls_name = param->string;
0349         param->string = NULL;
0350         break;
0351     case Opt_prealloc:
0352         opts->prealloc = result.negated ? 0 : 1;
0353         break;
0354     case Opt_noacsrules:
0355         opts->noacsrules = result.negated ? 1 : 0;
0356         break;
0357     default:
0358         /* Should not be here unless we forget add case. */
0359         return -EINVAL;
0360     }
0361     return 0;
0362 }
0363 
0364 static int ntfs_fs_reconfigure(struct fs_context *fc)
0365 {
0366     struct super_block *sb = fc->root->d_sb;
0367     struct ntfs_sb_info *sbi = sb->s_fs_info;
0368     struct ntfs_mount_options *new_opts = fc->fs_private;
0369     int ro_rw;
0370 
0371     ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY);
0372     if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) {
0373         errorf(fc, "ntfs3: Couldn't remount rw because journal is not replayed. Please umount/remount instead\n");
0374         return -EINVAL;
0375     }
0376 
0377     new_opts->nls = ntfs_load_nls(new_opts->nls_name);
0378     if (IS_ERR(new_opts->nls)) {
0379         new_opts->nls = NULL;
0380         errorf(fc, "ntfs3: Cannot load iocharset %s", new_opts->nls_name);
0381         return -EINVAL;
0382     }
0383     if (new_opts->nls != sbi->options->nls)
0384         return invalf(fc, "ntfs3: Cannot use different iocharset when remounting!");
0385 
0386     sync_filesystem(sb);
0387 
0388     if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) &&
0389         !new_opts->force) {
0390         errorf(fc, "ntfs3: Volume is dirty and \"force\" flag is not set!");
0391         return -EINVAL;
0392     }
0393 
0394     swap(sbi->options, fc->fs_private);
0395 
0396     return 0;
0397 }
0398 
0399 static struct kmem_cache *ntfs_inode_cachep;
0400 
0401 static struct inode *ntfs_alloc_inode(struct super_block *sb)
0402 {
0403     struct ntfs_inode *ni = alloc_inode_sb(sb, ntfs_inode_cachep, GFP_NOFS);
0404 
0405     if (!ni)
0406         return NULL;
0407 
0408     memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode));
0409 
0410     mutex_init(&ni->ni_lock);
0411 
0412     return &ni->vfs_inode;
0413 }
0414 
0415 static void ntfs_i_callback(struct rcu_head *head)
0416 {
0417     struct inode *inode = container_of(head, struct inode, i_rcu);
0418     struct ntfs_inode *ni = ntfs_i(inode);
0419 
0420     mutex_destroy(&ni->ni_lock);
0421 
0422     kmem_cache_free(ntfs_inode_cachep, ni);
0423 }
0424 
0425 static void ntfs_destroy_inode(struct inode *inode)
0426 {
0427     call_rcu(&inode->i_rcu, ntfs_i_callback);
0428 }
0429 
0430 static void init_once(void *foo)
0431 {
0432     struct ntfs_inode *ni = foo;
0433 
0434     inode_init_once(&ni->vfs_inode);
0435 }
0436 
0437 /*
0438  * put_ntfs - Noinline to reduce binary size.
0439  */
0440 static noinline void put_ntfs(struct ntfs_sb_info *sbi)
0441 {
0442     kfree(sbi->new_rec);
0443     kvfree(ntfs_put_shared(sbi->upcase));
0444     kfree(sbi->def_table);
0445 
0446     wnd_close(&sbi->mft.bitmap);
0447     wnd_close(&sbi->used.bitmap);
0448 
0449     if (sbi->mft.ni)
0450         iput(&sbi->mft.ni->vfs_inode);
0451 
0452     if (sbi->security.ni)
0453         iput(&sbi->security.ni->vfs_inode);
0454 
0455     if (sbi->reparse.ni)
0456         iput(&sbi->reparse.ni->vfs_inode);
0457 
0458     if (sbi->objid.ni)
0459         iput(&sbi->objid.ni->vfs_inode);
0460 
0461     if (sbi->volume.ni)
0462         iput(&sbi->volume.ni->vfs_inode);
0463 
0464     ntfs_update_mftmirr(sbi, 0);
0465 
0466     indx_clear(&sbi->security.index_sii);
0467     indx_clear(&sbi->security.index_sdh);
0468     indx_clear(&sbi->reparse.index_r);
0469     indx_clear(&sbi->objid.index_o);
0470     kfree(sbi->compress.lznt);
0471 #ifdef CONFIG_NTFS3_LZX_XPRESS
0472     xpress_free_decompressor(sbi->compress.xpress);
0473     lzx_free_decompressor(sbi->compress.lzx);
0474 #endif
0475     kfree(sbi);
0476 }
0477 
0478 static void ntfs_put_super(struct super_block *sb)
0479 {
0480     struct ntfs_sb_info *sbi = sb->s_fs_info;
0481 
0482     /* Mark rw ntfs as clear, if possible. */
0483     ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
0484 
0485     put_mount_options(sbi->options);
0486     put_ntfs(sbi);
0487     sb->s_fs_info = NULL;
0488 
0489     sync_blockdev(sb->s_bdev);
0490 }
0491 
0492 static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf)
0493 {
0494     struct super_block *sb = dentry->d_sb;
0495     struct ntfs_sb_info *sbi = sb->s_fs_info;
0496     struct wnd_bitmap *wnd = &sbi->used.bitmap;
0497 
0498     buf->f_type = sb->s_magic;
0499     buf->f_bsize = sbi->cluster_size;
0500     buf->f_blocks = wnd->nbits;
0501 
0502     buf->f_bfree = buf->f_bavail = wnd_zeroes(wnd);
0503     buf->f_fsid.val[0] = sbi->volume.ser_num;
0504     buf->f_fsid.val[1] = (sbi->volume.ser_num >> 32);
0505     buf->f_namelen = NTFS_NAME_LEN;
0506 
0507     return 0;
0508 }
0509 
0510 static int ntfs_show_options(struct seq_file *m, struct dentry *root)
0511 {
0512     struct super_block *sb = root->d_sb;
0513     struct ntfs_sb_info *sbi = sb->s_fs_info;
0514     struct ntfs_mount_options *opts = sbi->options;
0515     struct user_namespace *user_ns = seq_user_ns(m);
0516 
0517     seq_printf(m, ",uid=%u",
0518           from_kuid_munged(user_ns, opts->fs_uid));
0519     seq_printf(m, ",gid=%u",
0520           from_kgid_munged(user_ns, opts->fs_gid));
0521     if (opts->fmask)
0522         seq_printf(m, ",fmask=%04o", ~opts->fs_fmask_inv);
0523     if (opts->dmask)
0524         seq_printf(m, ",dmask=%04o", ~opts->fs_dmask_inv);
0525     if (opts->nls)
0526         seq_printf(m, ",iocharset=%s", opts->nls->charset);
0527     else
0528         seq_puts(m, ",iocharset=utf8");
0529     if (opts->sys_immutable)
0530         seq_puts(m, ",sys_immutable");
0531     if (opts->discard)
0532         seq_puts(m, ",discard");
0533     if (opts->sparse)
0534         seq_puts(m, ",sparse");
0535     if (opts->showmeta)
0536         seq_puts(m, ",showmeta");
0537     if (opts->nohidden)
0538         seq_puts(m, ",nohidden");
0539     if (opts->force)
0540         seq_puts(m, ",force");
0541     if (opts->noacsrules)
0542         seq_puts(m, ",noacsrules");
0543     if (opts->prealloc)
0544         seq_puts(m, ",prealloc");
0545     if (sb->s_flags & SB_POSIXACL)
0546         seq_puts(m, ",acl");
0547 
0548     return 0;
0549 }
0550 
0551 /*
0552  * ntfs_sync_fs - super_operations::sync_fs
0553  */
0554 static int ntfs_sync_fs(struct super_block *sb, int wait)
0555 {
0556     int err = 0, err2;
0557     struct ntfs_sb_info *sbi = sb->s_fs_info;
0558     struct ntfs_inode *ni;
0559     struct inode *inode;
0560 
0561     ni = sbi->security.ni;
0562     if (ni) {
0563         inode = &ni->vfs_inode;
0564         err2 = _ni_write_inode(inode, wait);
0565         if (err2 && !err)
0566             err = err2;
0567     }
0568 
0569     ni = sbi->objid.ni;
0570     if (ni) {
0571         inode = &ni->vfs_inode;
0572         err2 = _ni_write_inode(inode, wait);
0573         if (err2 && !err)
0574             err = err2;
0575     }
0576 
0577     ni = sbi->reparse.ni;
0578     if (ni) {
0579         inode = &ni->vfs_inode;
0580         err2 = _ni_write_inode(inode, wait);
0581         if (err2 && !err)
0582             err = err2;
0583     }
0584 
0585     if (!err)
0586         ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
0587 
0588     ntfs_update_mftmirr(sbi, wait);
0589 
0590     return err;
0591 }
0592 
0593 static const struct super_operations ntfs_sops = {
0594     .alloc_inode = ntfs_alloc_inode,
0595     .destroy_inode = ntfs_destroy_inode,
0596     .evict_inode = ntfs_evict_inode,
0597     .put_super = ntfs_put_super,
0598     .statfs = ntfs_statfs,
0599     .show_options = ntfs_show_options,
0600     .sync_fs = ntfs_sync_fs,
0601     .write_inode = ntfs3_write_inode,
0602 };
0603 
0604 static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino,
0605                        u32 generation)
0606 {
0607     struct MFT_REF ref;
0608     struct inode *inode;
0609 
0610     ref.low = cpu_to_le32(ino);
0611 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
0612     ref.high = cpu_to_le16(ino >> 32);
0613 #else
0614     ref.high = 0;
0615 #endif
0616     ref.seq = cpu_to_le16(generation);
0617 
0618     inode = ntfs_iget5(sb, &ref, NULL);
0619     if (!IS_ERR(inode) && is_bad_inode(inode)) {
0620         iput(inode);
0621         inode = ERR_PTR(-ESTALE);
0622     }
0623 
0624     return inode;
0625 }
0626 
0627 static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
0628                     int fh_len, int fh_type)
0629 {
0630     return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
0631                     ntfs_export_get_inode);
0632 }
0633 
0634 static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
0635                     int fh_len, int fh_type)
0636 {
0637     return generic_fh_to_parent(sb, fid, fh_len, fh_type,
0638                     ntfs_export_get_inode);
0639 }
0640 
0641 /* TODO: == ntfs_sync_inode */
0642 static int ntfs_nfs_commit_metadata(struct inode *inode)
0643 {
0644     return _ni_write_inode(inode, 1);
0645 }
0646 
0647 static const struct export_operations ntfs_export_ops = {
0648     .fh_to_dentry = ntfs_fh_to_dentry,
0649     .fh_to_parent = ntfs_fh_to_parent,
0650     .get_parent = ntfs3_get_parent,
0651     .commit_metadata = ntfs_nfs_commit_metadata,
0652 };
0653 
0654 /*
0655  * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb".
0656  */
0657 static u32 format_size_gb(const u64 bytes, u32 *mb)
0658 {
0659     /* Do simple right 30 bit shift of 64 bit value. */
0660     u64 kbytes = bytes >> 10;
0661     u32 kbytes32 = kbytes;
0662 
0663     *mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20;
0664     if (*mb >= 100)
0665         *mb = 99;
0666 
0667     return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12);
0668 }
0669 
0670 static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot)
0671 {
0672     if (boot->sectors_per_clusters <= 0x80)
0673         return boot->sectors_per_clusters;
0674     if (boot->sectors_per_clusters >= 0xf4) /* limit shift to 2MB max */
0675         return 1U << (0 - boot->sectors_per_clusters);
0676     return -EINVAL;
0677 }
0678 
0679 /*
0680  * ntfs_init_from_boot - Init internal info from on-disk boot sector.
0681  */
0682 static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
0683                    u64 dev_size)
0684 {
0685     struct ntfs_sb_info *sbi = sb->s_fs_info;
0686     int err;
0687     u32 mb, gb, boot_sector_size, sct_per_clst, record_size;
0688     u64 sectors, clusters, mlcn, mlcn2;
0689     struct NTFS_BOOT *boot;
0690     struct buffer_head *bh;
0691     struct MFT_REC *rec;
0692     u16 fn, ao;
0693 
0694     sbi->volume.blocks = dev_size >> PAGE_SHIFT;
0695 
0696     bh = ntfs_bread(sb, 0);
0697     if (!bh)
0698         return -EIO;
0699 
0700     err = -EINVAL;
0701     boot = (struct NTFS_BOOT *)bh->b_data;
0702 
0703     if (memcmp(boot->system_id, "NTFS    ", sizeof("NTFS    ") - 1))
0704         goto out;
0705 
0706     /* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/
0707     /*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1])
0708      *  goto out;
0709      */
0710 
0711     boot_sector_size = (u32)boot->bytes_per_sector[1] << 8;
0712     if (boot->bytes_per_sector[0] || boot_sector_size < SECTOR_SIZE ||
0713         !is_power_of_2(boot_sector_size)) {
0714         goto out;
0715     }
0716 
0717     /* cluster size: 512, 1K, 2K, 4K, ... 2M */
0718     sct_per_clst = true_sectors_per_clst(boot);
0719     if ((int)sct_per_clst < 0)
0720         goto out;
0721     if (!is_power_of_2(sct_per_clst))
0722         goto out;
0723 
0724     mlcn = le64_to_cpu(boot->mft_clst);
0725     mlcn2 = le64_to_cpu(boot->mft2_clst);
0726     sectors = le64_to_cpu(boot->sectors_per_volume);
0727 
0728     if (mlcn * sct_per_clst >= sectors)
0729         goto out;
0730 
0731     if (mlcn2 * sct_per_clst >= sectors)
0732         goto out;
0733 
0734     /* Check MFT record size. */
0735     if ((boot->record_size < 0 &&
0736          SECTOR_SIZE > (2U << (-boot->record_size))) ||
0737         (boot->record_size >= 0 && !is_power_of_2(boot->record_size))) {
0738         goto out;
0739     }
0740 
0741     /* Check index record size. */
0742     if ((boot->index_size < 0 &&
0743          SECTOR_SIZE > (2U << (-boot->index_size))) ||
0744         (boot->index_size >= 0 && !is_power_of_2(boot->index_size))) {
0745         goto out;
0746     }
0747 
0748     sbi->volume.size = sectors * boot_sector_size;
0749 
0750     gb = format_size_gb(sbi->volume.size + boot_sector_size, &mb);
0751 
0752     /*
0753      * - Volume formatted and mounted with the same sector size.
0754      * - Volume formatted 4K and mounted as 512.
0755      * - Volume formatted 512 and mounted as 4K.
0756      */
0757     if (boot_sector_size != sector_size) {
0758         ntfs_warn(
0759             sb,
0760             "Different NTFS' sector size (%u) and media sector size (%u)",
0761             boot_sector_size, sector_size);
0762         dev_size += sector_size - 1;
0763     }
0764 
0765     sbi->cluster_size = boot_sector_size * sct_per_clst;
0766     sbi->cluster_bits = blksize_bits(sbi->cluster_size);
0767 
0768     sbi->mft.lbo = mlcn << sbi->cluster_bits;
0769     sbi->mft.lbo2 = mlcn2 << sbi->cluster_bits;
0770 
0771     /* Compare boot's cluster and sector. */
0772     if (sbi->cluster_size < boot_sector_size)
0773         goto out;
0774 
0775     /* Compare boot's cluster and media sector. */
0776     if (sbi->cluster_size < sector_size) {
0777         /* No way to use ntfs_get_block in this case. */
0778         ntfs_err(
0779             sb,
0780             "Failed to mount 'cause NTFS's cluster size (%u) is less than media sector size (%u)",
0781             sbi->cluster_size, sector_size);
0782         goto out;
0783     }
0784 
0785     sbi->cluster_mask = sbi->cluster_size - 1;
0786     sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask;
0787     sbi->record_size = record_size = boot->record_size < 0
0788                          ? 1 << (-boot->record_size)
0789                          : (u32)boot->record_size
0790                                << sbi->cluster_bits;
0791 
0792     if (record_size > MAXIMUM_BYTES_PER_MFT)
0793         goto out;
0794 
0795     sbi->record_bits = blksize_bits(record_size);
0796     sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes
0797 
0798     sbi->max_bytes_per_attr =
0799         record_size - ALIGN(MFTRECORD_FIXUP_OFFSET_1, 8) -
0800         ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) -
0801         ALIGN(sizeof(enum ATTR_TYPE), 8);
0802 
0803     sbi->index_size = boot->index_size < 0
0804                   ? 1u << (-boot->index_size)
0805                   : (u32)boot->index_size << sbi->cluster_bits;
0806 
0807     sbi->volume.ser_num = le64_to_cpu(boot->serial_num);
0808 
0809     /* Warning if RAW volume. */
0810     if (dev_size < sbi->volume.size + boot_sector_size) {
0811         u32 mb0, gb0;
0812 
0813         gb0 = format_size_gb(dev_size, &mb0);
0814         ntfs_warn(
0815             sb,
0816             "RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only",
0817             gb, mb, gb0, mb0);
0818         sb->s_flags |= SB_RDONLY;
0819     }
0820 
0821     clusters = sbi->volume.size >> sbi->cluster_bits;
0822 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
0823     /* 32 bits per cluster. */
0824     if (clusters >> 32) {
0825         ntfs_notice(
0826             sb,
0827             "NTFS %u.%02u Gb is too big to use 32 bits per cluster",
0828             gb, mb);
0829         goto out;
0830     }
0831 #elif BITS_PER_LONG < 64
0832 #error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS"
0833 #endif
0834 
0835     sbi->used.bitmap.nbits = clusters;
0836 
0837     rec = kzalloc(record_size, GFP_NOFS);
0838     if (!rec) {
0839         err = -ENOMEM;
0840         goto out;
0841     }
0842 
0843     sbi->new_rec = rec;
0844     rec->rhdr.sign = NTFS_FILE_SIGNATURE;
0845     rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET_1);
0846     fn = (sbi->record_size >> SECTOR_SHIFT) + 1;
0847     rec->rhdr.fix_num = cpu_to_le16(fn);
0848     ao = ALIGN(MFTRECORD_FIXUP_OFFSET_1 + sizeof(short) * fn, 8);
0849     rec->attr_off = cpu_to_le16(ao);
0850     rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8));
0851     rec->total = cpu_to_le32(sbi->record_size);
0852     ((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END;
0853 
0854     sb_set_blocksize(sb, min_t(u32, sbi->cluster_size, PAGE_SIZE));
0855 
0856     sbi->block_mask = sb->s_blocksize - 1;
0857     sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits;
0858     sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits;
0859 
0860     /* Maximum size for normal files. */
0861     sbi->maxbytes = (clusters << sbi->cluster_bits) - 1;
0862 
0863 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
0864     if (clusters >= (1ull << (64 - sbi->cluster_bits)))
0865         sbi->maxbytes = -1;
0866     sbi->maxbytes_sparse = -1;
0867     sb->s_maxbytes = MAX_LFS_FILESIZE;
0868 #else
0869     /* Maximum size for sparse file. */
0870     sbi->maxbytes_sparse = (1ull << (sbi->cluster_bits + 32)) - 1;
0871     sb->s_maxbytes = 0xFFFFFFFFull << sbi->cluster_bits;
0872 #endif
0873 
0874     /*
0875      * Compute the MFT zone at two steps.
0876      * It would be nice if we are able to allocate 1/8 of
0877      * total clusters for MFT but not more then 512 MB.
0878      */
0879     sbi->zone_max = min_t(CLST, 0x20000000 >> sbi->cluster_bits, clusters >> 3);
0880 
0881     err = 0;
0882 
0883 out:
0884     brelse(bh);
0885 
0886     return err;
0887 }
0888 
0889 /*
0890  * ntfs_fill_super - Try to mount.
0891  */
0892 static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
0893 {
0894     int err;
0895     struct ntfs_sb_info *sbi = sb->s_fs_info;
0896     struct block_device *bdev = sb->s_bdev;
0897     struct inode *inode;
0898     struct ntfs_inode *ni;
0899     size_t i, tt;
0900     CLST vcn, lcn, len;
0901     struct ATTRIB *attr;
0902     const struct VOLUME_INFO *info;
0903     u32 idx, done, bytes;
0904     struct ATTR_DEF_ENTRY *t;
0905     u16 *shared;
0906     struct MFT_REF ref;
0907 
0908     ref.high = 0;
0909 
0910     sbi->sb = sb;
0911     sbi->options = fc->fs_private;
0912     fc->fs_private = NULL;
0913     sb->s_flags |= SB_NODIRATIME;
0914     sb->s_magic = 0x7366746e; // "ntfs"
0915     sb->s_op = &ntfs_sops;
0916     sb->s_export_op = &ntfs_export_ops;
0917     sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec
0918     sb->s_xattr = ntfs_xattr_handlers;
0919 
0920     sbi->options->nls = ntfs_load_nls(sbi->options->nls_name);
0921     if (IS_ERR(sbi->options->nls)) {
0922         sbi->options->nls = NULL;
0923         errorf(fc, "Cannot load nls %s", sbi->options->nls_name);
0924         err = -EINVAL;
0925         goto out;
0926     }
0927 
0928     if (bdev_max_discard_sectors(bdev) && bdev_discard_granularity(bdev)) {
0929         sbi->discard_granularity = bdev_discard_granularity(bdev);
0930         sbi->discard_granularity_mask_inv =
0931             ~(u64)(sbi->discard_granularity - 1);
0932     }
0933 
0934     /* Parse boot. */
0935     err = ntfs_init_from_boot(sb, bdev_logical_block_size(bdev),
0936                   bdev_nr_bytes(bdev));
0937     if (err)
0938         goto out;
0939 
0940     /*
0941      * Load $Volume. This should be done before $LogFile
0942      * 'cause 'sbi->volume.ni' is used 'ntfs_set_state'.
0943      */
0944     ref.low = cpu_to_le32(MFT_REC_VOL);
0945     ref.seq = cpu_to_le16(MFT_REC_VOL);
0946     inode = ntfs_iget5(sb, &ref, &NAME_VOLUME);
0947     if (IS_ERR(inode)) {
0948         ntfs_err(sb, "Failed to load $Volume.");
0949         err = PTR_ERR(inode);
0950         goto out;
0951     }
0952 
0953     ni = ntfs_i(inode);
0954 
0955     /* Load and save label (not necessary). */
0956     attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL);
0957 
0958     if (!attr) {
0959         /* It is ok if no ATTR_LABEL */
0960     } else if (!attr->non_res && !is_attr_ext(attr)) {
0961         /* $AttrDef allows labels to be up to 128 symbols. */
0962         err = utf16s_to_utf8s(resident_data(attr),
0963                       le32_to_cpu(attr->res.data_size) >> 1,
0964                       UTF16_LITTLE_ENDIAN, sbi->volume.label,
0965                       sizeof(sbi->volume.label));
0966         if (err < 0)
0967             sbi->volume.label[0] = 0;
0968     } else {
0969         /* Should we break mounting here? */
0970         //err = -EINVAL;
0971         //goto put_inode_out;
0972     }
0973 
0974     attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL);
0975     if (!attr || is_attr_ext(attr)) {
0976         err = -EINVAL;
0977         goto put_inode_out;
0978     }
0979 
0980     info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO);
0981     if (!info) {
0982         err = -EINVAL;
0983         goto put_inode_out;
0984     }
0985 
0986     sbi->volume.major_ver = info->major_ver;
0987     sbi->volume.minor_ver = info->minor_ver;
0988     sbi->volume.flags = info->flags;
0989     sbi->volume.ni = ni;
0990 
0991     /* Load $MFTMirr to estimate recs_mirr. */
0992     ref.low = cpu_to_le32(MFT_REC_MIRR);
0993     ref.seq = cpu_to_le16(MFT_REC_MIRR);
0994     inode = ntfs_iget5(sb, &ref, &NAME_MIRROR);
0995     if (IS_ERR(inode)) {
0996         ntfs_err(sb, "Failed to load $MFTMirr.");
0997         err = PTR_ERR(inode);
0998         goto out;
0999     }
1000 
1001     sbi->mft.recs_mirr =
1002         ntfs_up_cluster(sbi, inode->i_size) >> sbi->record_bits;
1003 
1004     iput(inode);
1005 
1006     /* Load LogFile to replay. */
1007     ref.low = cpu_to_le32(MFT_REC_LOG);
1008     ref.seq = cpu_to_le16(MFT_REC_LOG);
1009     inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE);
1010     if (IS_ERR(inode)) {
1011         ntfs_err(sb, "Failed to load \x24LogFile.");
1012         err = PTR_ERR(inode);
1013         goto out;
1014     }
1015 
1016     ni = ntfs_i(inode);
1017 
1018     err = ntfs_loadlog_and_replay(ni, sbi);
1019     if (err)
1020         goto put_inode_out;
1021 
1022     iput(inode);
1023 
1024     if (sbi->flags & NTFS_FLAGS_NEED_REPLAY) {
1025         if (!sb_rdonly(sb)) {
1026             ntfs_warn(sb,
1027                   "failed to replay log file. Can't mount rw!");
1028             err = -EINVAL;
1029             goto out;
1030         }
1031     } else if (sbi->volume.flags & VOLUME_FLAG_DIRTY) {
1032         if (!sb_rdonly(sb) && !sbi->options->force) {
1033             ntfs_warn(
1034                 sb,
1035                 "volume is dirty and \"force\" flag is not set!");
1036             err = -EINVAL;
1037             goto out;
1038         }
1039     }
1040 
1041     /* Load $MFT. */
1042     ref.low = cpu_to_le32(MFT_REC_MFT);
1043     ref.seq = cpu_to_le16(1);
1044 
1045     inode = ntfs_iget5(sb, &ref, &NAME_MFT);
1046     if (IS_ERR(inode)) {
1047         ntfs_err(sb, "Failed to load $MFT.");
1048         err = PTR_ERR(inode);
1049         goto out;
1050     }
1051 
1052     ni = ntfs_i(inode);
1053 
1054     sbi->mft.used = ni->i_valid >> sbi->record_bits;
1055     tt = inode->i_size >> sbi->record_bits;
1056     sbi->mft.next_free = MFT_REC_USER;
1057 
1058     err = wnd_init(&sbi->mft.bitmap, sb, tt);
1059     if (err)
1060         goto put_inode_out;
1061 
1062     err = ni_load_all_mi(ni);
1063     if (err)
1064         goto put_inode_out;
1065 
1066     sbi->mft.ni = ni;
1067 
1068     /* Load $BadClus. */
1069     ref.low = cpu_to_le32(MFT_REC_BADCLUST);
1070     ref.seq = cpu_to_le16(MFT_REC_BADCLUST);
1071     inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS);
1072     if (IS_ERR(inode)) {
1073         ntfs_err(sb, "Failed to load $BadClus.");
1074         err = PTR_ERR(inode);
1075         goto out;
1076     }
1077 
1078     ni = ntfs_i(inode);
1079 
1080     for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) {
1081         if (lcn == SPARSE_LCN)
1082             continue;
1083 
1084         if (!sbi->bad_clusters)
1085             ntfs_notice(sb, "Volume contains bad blocks");
1086 
1087         sbi->bad_clusters += len;
1088     }
1089 
1090     iput(inode);
1091 
1092     /* Load $Bitmap. */
1093     ref.low = cpu_to_le32(MFT_REC_BITMAP);
1094     ref.seq = cpu_to_le16(MFT_REC_BITMAP);
1095     inode = ntfs_iget5(sb, &ref, &NAME_BITMAP);
1096     if (IS_ERR(inode)) {
1097         ntfs_err(sb, "Failed to load $Bitmap.");
1098         err = PTR_ERR(inode);
1099         goto out;
1100     }
1101 
1102 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
1103     if (inode->i_size >> 32) {
1104         err = -EINVAL;
1105         goto put_inode_out;
1106     }
1107 #endif
1108 
1109     /* Check bitmap boundary. */
1110     tt = sbi->used.bitmap.nbits;
1111     if (inode->i_size < bitmap_size(tt)) {
1112         err = -EINVAL;
1113         goto put_inode_out;
1114     }
1115 
1116     /* Not necessary. */
1117     sbi->used.bitmap.set_tail = true;
1118     err = wnd_init(&sbi->used.bitmap, sb, tt);
1119     if (err)
1120         goto put_inode_out;
1121 
1122     iput(inode);
1123 
1124     /* Compute the MFT zone. */
1125     err = ntfs_refresh_zone(sbi);
1126     if (err)
1127         goto out;
1128 
1129     /* Load $AttrDef. */
1130     ref.low = cpu_to_le32(MFT_REC_ATTR);
1131     ref.seq = cpu_to_le16(MFT_REC_ATTR);
1132     inode = ntfs_iget5(sb, &ref, &NAME_ATTRDEF);
1133     if (IS_ERR(inode)) {
1134         ntfs_err(sb, "Failed to load $AttrDef -> %d", err);
1135         err = PTR_ERR(inode);
1136         goto out;
1137     }
1138 
1139     if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY)) {
1140         err = -EINVAL;
1141         goto put_inode_out;
1142     }
1143     bytes = inode->i_size;
1144     sbi->def_table = t = kmalloc(bytes, GFP_NOFS);
1145     if (!t) {
1146         err = -ENOMEM;
1147         goto put_inode_out;
1148     }
1149 
1150     for (done = idx = 0; done < bytes; done += PAGE_SIZE, idx++) {
1151         unsigned long tail = bytes - done;
1152         struct page *page = ntfs_map_page(inode->i_mapping, idx);
1153 
1154         if (IS_ERR(page)) {
1155             err = PTR_ERR(page);
1156             goto put_inode_out;
1157         }
1158         memcpy(Add2Ptr(t, done), page_address(page),
1159                min(PAGE_SIZE, tail));
1160         ntfs_unmap_page(page);
1161 
1162         if (!idx && ATTR_STD != t->type) {
1163             err = -EINVAL;
1164             goto put_inode_out;
1165         }
1166     }
1167 
1168     t += 1;
1169     sbi->def_entries = 1;
1170     done = sizeof(struct ATTR_DEF_ENTRY);
1171     sbi->reparse.max_size = MAXIMUM_REPARSE_DATA_BUFFER_SIZE;
1172     sbi->ea_max_size = 0x10000; /* default formatter value */
1173 
1174     while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) {
1175         u32 t32 = le32_to_cpu(t->type);
1176         u64 sz = le64_to_cpu(t->max_sz);
1177 
1178         if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32)
1179             break;
1180 
1181         if (t->type == ATTR_REPARSE)
1182             sbi->reparse.max_size = sz;
1183         else if (t->type == ATTR_EA)
1184             sbi->ea_max_size = sz;
1185 
1186         done += sizeof(struct ATTR_DEF_ENTRY);
1187         t += 1;
1188         sbi->def_entries += 1;
1189     }
1190     iput(inode);
1191 
1192     /* Load $UpCase. */
1193     ref.low = cpu_to_le32(MFT_REC_UPCASE);
1194     ref.seq = cpu_to_le16(MFT_REC_UPCASE);
1195     inode = ntfs_iget5(sb, &ref, &NAME_UPCASE);
1196     if (IS_ERR(inode)) {
1197         ntfs_err(sb, "Failed to load $UpCase.");
1198         err = PTR_ERR(inode);
1199         goto out;
1200     }
1201 
1202     if (inode->i_size != 0x10000 * sizeof(short)) {
1203         err = -EINVAL;
1204         goto put_inode_out;
1205     }
1206 
1207     for (idx = 0; idx < (0x10000 * sizeof(short) >> PAGE_SHIFT); idx++) {
1208         const __le16 *src;
1209         u16 *dst = Add2Ptr(sbi->upcase, idx << PAGE_SHIFT);
1210         struct page *page = ntfs_map_page(inode->i_mapping, idx);
1211 
1212         if (IS_ERR(page)) {
1213             err = PTR_ERR(page);
1214             goto put_inode_out;
1215         }
1216 
1217         src = page_address(page);
1218 
1219 #ifdef __BIG_ENDIAN
1220         for (i = 0; i < PAGE_SIZE / sizeof(u16); i++)
1221             *dst++ = le16_to_cpu(*src++);
1222 #else
1223         memcpy(dst, src, PAGE_SIZE);
1224 #endif
1225         ntfs_unmap_page(page);
1226     }
1227 
1228     shared = ntfs_set_shared(sbi->upcase, 0x10000 * sizeof(short));
1229     if (shared && sbi->upcase != shared) {
1230         kvfree(sbi->upcase);
1231         sbi->upcase = shared;
1232     }
1233 
1234     iput(inode);
1235 
1236     if (is_ntfs3(sbi)) {
1237         /* Load $Secure. */
1238         err = ntfs_security_init(sbi);
1239         if (err)
1240             goto out;
1241 
1242         /* Load $Extend. */
1243         err = ntfs_extend_init(sbi);
1244         if (err)
1245             goto load_root;
1246 
1247         /* Load $Extend\$Reparse. */
1248         err = ntfs_reparse_init(sbi);
1249         if (err)
1250             goto load_root;
1251 
1252         /* Load $Extend\$ObjId. */
1253         err = ntfs_objid_init(sbi);
1254         if (err)
1255             goto load_root;
1256     }
1257 
1258 load_root:
1259     /* Load root. */
1260     ref.low = cpu_to_le32(MFT_REC_ROOT);
1261     ref.seq = cpu_to_le16(MFT_REC_ROOT);
1262     inode = ntfs_iget5(sb, &ref, &NAME_ROOT);
1263     if (IS_ERR(inode)) {
1264         ntfs_err(sb, "Failed to load root.");
1265         err = PTR_ERR(inode);
1266         goto out;
1267     }
1268 
1269     sb->s_root = d_make_root(inode);
1270     if (!sb->s_root) {
1271         err = -ENOMEM;
1272         goto put_inode_out;
1273     }
1274 
1275     return 0;
1276 
1277 put_inode_out:
1278     iput(inode);
1279 out:
1280     /*
1281      * Free resources here.
1282      * ntfs_fs_free will be called with fc->s_fs_info = NULL
1283      */
1284     put_ntfs(sbi);
1285     sb->s_fs_info = NULL;
1286 
1287     return err;
1288 }
1289 
1290 void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len)
1291 {
1292     struct ntfs_sb_info *sbi = sb->s_fs_info;
1293     struct block_device *bdev = sb->s_bdev;
1294     sector_t devblock = (u64)lcn * sbi->blocks_per_cluster;
1295     unsigned long blocks = (u64)len * sbi->blocks_per_cluster;
1296     unsigned long cnt = 0;
1297     unsigned long limit = global_zone_page_state(NR_FREE_PAGES)
1298                   << (PAGE_SHIFT - sb->s_blocksize_bits);
1299 
1300     if (limit >= 0x2000)
1301         limit -= 0x1000;
1302     else if (limit < 32)
1303         limit = 32;
1304     else
1305         limit >>= 1;
1306 
1307     while (blocks--) {
1308         clean_bdev_aliases(bdev, devblock++, 1);
1309         if (cnt++ >= limit) {
1310             sync_blockdev(bdev);
1311             cnt = 0;
1312         }
1313     }
1314 }
1315 
1316 /*
1317  * ntfs_discard - Issue a discard request (trim for SSD).
1318  */
1319 int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len)
1320 {
1321     int err;
1322     u64 lbo, bytes, start, end;
1323     struct super_block *sb;
1324 
1325     if (sbi->used.next_free_lcn == lcn + len)
1326         sbi->used.next_free_lcn = lcn;
1327 
1328     if (sbi->flags & NTFS_FLAGS_NODISCARD)
1329         return -EOPNOTSUPP;
1330 
1331     if (!sbi->options->discard)
1332         return -EOPNOTSUPP;
1333 
1334     lbo = (u64)lcn << sbi->cluster_bits;
1335     bytes = (u64)len << sbi->cluster_bits;
1336 
1337     /* Align up 'start' on discard_granularity. */
1338     start = (lbo + sbi->discard_granularity - 1) &
1339         sbi->discard_granularity_mask_inv;
1340     /* Align down 'end' on discard_granularity. */
1341     end = (lbo + bytes) & sbi->discard_granularity_mask_inv;
1342 
1343     sb = sbi->sb;
1344     if (start >= end)
1345         return 0;
1346 
1347     err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9,
1348                    GFP_NOFS);
1349 
1350     if (err == -EOPNOTSUPP)
1351         sbi->flags |= NTFS_FLAGS_NODISCARD;
1352 
1353     return err;
1354 }
1355 
1356 static int ntfs_fs_get_tree(struct fs_context *fc)
1357 {
1358     return get_tree_bdev(fc, ntfs_fill_super);
1359 }
1360 
1361 /*
1362  * ntfs_fs_free - Free fs_context.
1363  *
1364  * Note that this will be called after fill_super and reconfigure
1365  * even when they pass. So they have to take pointers if they pass.
1366  */
1367 static void ntfs_fs_free(struct fs_context *fc)
1368 {
1369     struct ntfs_mount_options *opts = fc->fs_private;
1370     struct ntfs_sb_info *sbi = fc->s_fs_info;
1371 
1372     if (sbi)
1373         put_ntfs(sbi);
1374 
1375     if (opts)
1376         put_mount_options(opts);
1377 }
1378 
1379 static const struct fs_context_operations ntfs_context_ops = {
1380     .parse_param    = ntfs_fs_parse_param,
1381     .get_tree   = ntfs_fs_get_tree,
1382     .reconfigure    = ntfs_fs_reconfigure,
1383     .free       = ntfs_fs_free,
1384 };
1385 
1386 /*
1387  * ntfs_init_fs_context - Initialize spi and opts
1388  *
1389  * This will called when mount/remount. We will first initialize
1390  * options so that if remount we can use just that.
1391  */
1392 static int ntfs_init_fs_context(struct fs_context *fc)
1393 {
1394     struct ntfs_mount_options *opts;
1395     struct ntfs_sb_info *sbi;
1396 
1397     opts = kzalloc(sizeof(struct ntfs_mount_options), GFP_NOFS);
1398     if (!opts)
1399         return -ENOMEM;
1400 
1401     /* Default options. */
1402     opts->fs_uid = current_uid();
1403     opts->fs_gid = current_gid();
1404     opts->fs_fmask_inv = ~current_umask();
1405     opts->fs_dmask_inv = ~current_umask();
1406 
1407     if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
1408         goto ok;
1409 
1410     sbi = kzalloc(sizeof(struct ntfs_sb_info), GFP_NOFS);
1411     if (!sbi)
1412         goto free_opts;
1413 
1414     sbi->upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL);
1415     if (!sbi->upcase)
1416         goto free_sbi;
1417 
1418     ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1419                  DEFAULT_RATELIMIT_BURST);
1420 
1421     mutex_init(&sbi->compress.mtx_lznt);
1422 #ifdef CONFIG_NTFS3_LZX_XPRESS
1423     mutex_init(&sbi->compress.mtx_xpress);
1424     mutex_init(&sbi->compress.mtx_lzx);
1425 #endif
1426 
1427     fc->s_fs_info = sbi;
1428 ok:
1429     fc->fs_private = opts;
1430     fc->ops = &ntfs_context_ops;
1431 
1432     return 0;
1433 free_sbi:
1434     kfree(sbi);
1435 free_opts:
1436     kfree(opts);
1437     return -ENOMEM;
1438 }
1439 
1440 // clang-format off
1441 static struct file_system_type ntfs_fs_type = {
1442     .owner          = THIS_MODULE,
1443     .name           = "ntfs3",
1444     .init_fs_context    = ntfs_init_fs_context,
1445     .parameters     = ntfs_fs_parameters,
1446     .kill_sb        = kill_block_super,
1447     .fs_flags       = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
1448 };
1449 // clang-format on
1450 
1451 static int __init init_ntfs_fs(void)
1452 {
1453     int err;
1454 
1455     pr_info("ntfs3: Max link count %u\n", NTFS_LINK_MAX);
1456 
1457     if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL))
1458         pr_info("ntfs3: Enabled Linux POSIX ACLs support\n");
1459     if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER))
1460         pr_notice("ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n");
1461     if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS))
1462         pr_info("ntfs3: Read-only LZX/Xpress compression included\n");
1463 
1464     err = ntfs3_init_bitmap();
1465     if (err)
1466         return err;
1467 
1468     ntfs_inode_cachep = kmem_cache_create(
1469         "ntfs_inode_cache", sizeof(struct ntfs_inode), 0,
1470         (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1471         init_once);
1472     if (!ntfs_inode_cachep) {
1473         err = -ENOMEM;
1474         goto out1;
1475     }
1476 
1477     err = register_filesystem(&ntfs_fs_type);
1478     if (err)
1479         goto out;
1480 
1481     return 0;
1482 out:
1483     kmem_cache_destroy(ntfs_inode_cachep);
1484 out1:
1485     ntfs3_exit_bitmap();
1486     return err;
1487 }
1488 
1489 static void __exit exit_ntfs_fs(void)
1490 {
1491     if (ntfs_inode_cachep) {
1492         rcu_barrier();
1493         kmem_cache_destroy(ntfs_inode_cachep);
1494     }
1495 
1496     unregister_filesystem(&ntfs_fs_type);
1497     ntfs3_exit_bitmap();
1498 }
1499 
1500 MODULE_LICENSE("GPL");
1501 MODULE_DESCRIPTION("ntfs3 read/write filesystem");
1502 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1503 MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support");
1504 #endif
1505 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
1506 MODULE_INFO(cluster, "Warning: Activated 64 bits per cluster. Windows does not support this");
1507 #endif
1508 #ifdef CONFIG_NTFS3_LZX_XPRESS
1509 MODULE_INFO(compression, "Read-only lzx/xpress compression included");
1510 #endif
1511 
1512 MODULE_AUTHOR("Konstantin Komarov");
1513 MODULE_ALIAS_FS("ntfs3");
1514 
1515 module_init(init_ntfs_fs);
1516 module_exit(exit_ntfs_fs);