1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
9 * cluster - allocation unit - 512,1K,2K,4K,...,2M
10 * vcn - virtual cluster number - Offset inside the file in clusters.
11 * vbo - virtual byte offset - Offset inside the file in bytes.
12 * lcn - logical cluster number - 0 based cluster in clusters heap.
13 * lbo - logical byte offset - Absolute position inside volume.
14 * run - maps VCN to LCN - Stored in attributes in packed form.
15 * attr - attribute segment - std/name/data etc records inside MFT.
16 * mi - MFT inode - One MFT record(usually 1024 bytes or 4K), consists of attributes.
17 * ni - NTFS inode - Extends linux inode. consists of one or more mft inodes.
18 * index - unit inside directory - 2K, 4K, <=page size, does not depend on cluster size.
20 * WSL - Windows Subsystem for Linux
21 * https://docs.microsoft.com/en-us/windows/wsl/file-permissions
22 * It stores uid/gid/mode/dev in xattr
26 #include <linux/blkdev.h>
27 #include <linux/buffer_head.h>
28 #include <linux/exportfs.h>
30 #include <linux/fs_context.h>
31 #include <linux/fs_parser.h>
32 #include <linux/log2.h>
33 #include <linux/minmax.h>
34 #include <linux/module.h>
35 #include <linux/nls.h>
36 #include <linux/seq_file.h>
37 #include <linux/statfs.h>
42 #ifdef CONFIG_NTFS3_LZX_XPRESS
48 * ntfs_printk - Trace warnings/notices/errors.
50 * Thanks Joe Perches <joe@perches.com> for implementation
52 void ntfs_printk(const struct super_block *sb, const char *fmt, ...)
57 struct ntfs_sb_info *sbi = sb->s_fs_info;
59 /* Should we use different ratelimits for warnings/notices/errors? */
60 if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
65 level = printk_get_level(fmt);
66 vaf.fmt = printk_skip_level(fmt);
68 printk("%c%cntfs3: %s: %pV\n", KERN_SOH_ASCII, level, sb->s_id, &vaf);
73 static char s_name_buf[512];
74 static atomic_t s_name_buf_cnt = ATOMIC_INIT(1); // 1 means 'free s_name_buf'.
79 * Print warnings/notices/errors about inode using name or inode number.
81 void ntfs_inode_printk(struct inode *inode, const char *fmt, ...)
83 struct super_block *sb = inode->i_sb;
84 struct ntfs_sb_info *sbi = sb->s_fs_info;
90 if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
93 /* Use static allocated buffer, if possible. */
94 name = atomic_dec_and_test(&s_name_buf_cnt)
96 : kmalloc(sizeof(s_name_buf), GFP_NOFS);
99 struct dentry *de = d_find_alias(inode);
100 const u32 name_len = ARRAY_SIZE(s_name_buf) - 1;
103 spin_lock(&de->d_lock);
104 snprintf(name, name_len, " \"%s\"", de->d_name.name);
105 spin_unlock(&de->d_lock);
106 name[name_len] = 0; /* To be sure. */
110 dput(de); /* Cocci warns if placed in branch "if (de)" */
115 level = printk_get_level(fmt);
116 vaf.fmt = printk_skip_level(fmt);
119 printk("%c%cntfs3: %s: ino=%lx,%s %pV\n", KERN_SOH_ASCII, level,
120 sb->s_id, inode->i_ino, name ? name : "", &vaf);
124 atomic_inc(&s_name_buf_cnt);
125 if (name != s_name_buf)
131 * Shared memory struct.
133 * On-disk ntfs's upcase table is created by ntfs formatter.
134 * 'upcase' table is 128K bytes of memory.
135 * We should read it into memory when mounting.
136 * Several ntfs volumes likely use the same 'upcase' table.
137 * It is good idea to share in-memory 'upcase' table between different volumes.
138 * Unfortunately winxp/vista/win7 use different upcase tables.
140 static DEFINE_SPINLOCK(s_shared_lock);
152 * * @ptr - If pointer was saved in shared memory.
153 * * NULL - If pointer was not shared.
155 void *ntfs_set_shared(void *ptr, u32 bytes)
160 spin_lock(&s_shared_lock);
161 for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
162 if (!s_shared[i].cnt) {
164 } else if (bytes == s_shared[i].len &&
165 !memcmp(s_shared[i].ptr, ptr, bytes)) {
166 s_shared[i].cnt += 1;
167 ret = s_shared[i].ptr;
172 if (!ret && j != -1) {
173 s_shared[j].ptr = ptr;
174 s_shared[j].len = bytes;
178 spin_unlock(&s_shared_lock);
187 * * @ptr - If pointer is not shared anymore.
188 * * NULL - If pointer is still shared.
190 void *ntfs_put_shared(void *ptr)
195 spin_lock(&s_shared_lock);
196 for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
197 if (s_shared[i].cnt && s_shared[i].ptr == ptr) {
198 if (--s_shared[i].cnt)
203 spin_unlock(&s_shared_lock);
208 static inline void put_mount_options(struct ntfs_mount_options *options)
210 kfree(options->nls_name);
211 unload_nls(options->nls);
234 static const struct fs_parameter_spec ntfs_fs_parameters[] = {
235 fsparam_u32("uid", Opt_uid),
236 fsparam_u32("gid", Opt_gid),
237 fsparam_u32oct("umask", Opt_umask),
238 fsparam_u32oct("dmask", Opt_dmask),
239 fsparam_u32oct("fmask", Opt_fmask),
240 fsparam_flag_no("sys_immutable", Opt_immutable),
241 fsparam_flag_no("discard", Opt_discard),
242 fsparam_flag_no("force", Opt_force),
243 fsparam_flag_no("sparse", Opt_sparse),
244 fsparam_flag_no("hidden", Opt_nohidden),
245 fsparam_flag_no("acl", Opt_acl),
246 fsparam_flag_no("showmeta", Opt_showmeta),
247 fsparam_flag_no("prealloc", Opt_prealloc),
248 fsparam_flag_no("acsrules", Opt_noacsrules),
249 fsparam_string("iocharset", Opt_iocharset),
254 * Load nls table or if @nls is utf8 then return NULL.
256 static struct nls_table *ntfs_load_nls(char *nls)
258 struct nls_table *ret;
261 nls = CONFIG_NLS_DEFAULT;
263 if (strcmp(nls, "utf8") == 0)
266 if (strcmp(nls, CONFIG_NLS_DEFAULT) == 0)
267 return load_nls_default();
273 return ERR_PTR(-EINVAL);
276 static int ntfs_fs_parse_param(struct fs_context *fc,
277 struct fs_parameter *param)
279 struct ntfs_mount_options *opts = fc->fs_private;
280 struct fs_parse_result result;
283 opt = fs_parse(fc, ntfs_fs_parameters, param, &result);
289 opts->fs_uid = make_kuid(current_user_ns(), result.uint_32);
290 if (!uid_valid(opts->fs_uid))
291 return invalf(fc, "ntfs3: Invalid value for uid.");
294 opts->fs_gid = make_kgid(current_user_ns(), result.uint_32);
295 if (!gid_valid(opts->fs_gid))
296 return invalf(fc, "ntfs3: Invalid value for gid.");
299 if (result.uint_32 & ~07777)
300 return invalf(fc, "ntfs3: Invalid value for umask.");
301 opts->fs_fmask_inv = ~result.uint_32;
302 opts->fs_dmask_inv = ~result.uint_32;
307 if (result.uint_32 & ~07777)
308 return invalf(fc, "ntfs3: Invalid value for dmask.");
309 opts->fs_dmask_inv = ~result.uint_32;
313 if (result.uint_32 & ~07777)
314 return invalf(fc, "ntfs3: Invalid value for fmask.");
315 opts->fs_fmask_inv = ~result.uint_32;
319 opts->sys_immutable = result.negated ? 0 : 1;
322 opts->discard = result.negated ? 0 : 1;
325 opts->force = result.negated ? 0 : 1;
328 opts->sparse = result.negated ? 0 : 1;
331 opts->nohidden = result.negated ? 1 : 0;
335 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
336 fc->sb_flags |= SB_POSIXACL;
338 return invalf(fc, "ntfs3: Support for ACL not compiled in!");
341 fc->sb_flags &= ~SB_POSIXACL;
344 opts->showmeta = result.negated ? 0 : 1;
347 kfree(opts->nls_name);
348 opts->nls_name = param->string;
349 param->string = NULL;
352 opts->prealloc = result.negated ? 0 : 1;
355 opts->noacsrules = result.negated ? 1 : 0;
358 /* Should not be here unless we forget add case. */
364 static int ntfs_fs_reconfigure(struct fs_context *fc)
366 struct super_block *sb = fc->root->d_sb;
367 struct ntfs_sb_info *sbi = sb->s_fs_info;
368 struct ntfs_mount_options *new_opts = fc->fs_private;
371 ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY);
372 if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) {
373 errorf(fc, "ntfs3: Couldn't remount rw because journal is not replayed. Please umount/remount instead\n");
377 new_opts->nls = ntfs_load_nls(new_opts->nls_name);
378 if (IS_ERR(new_opts->nls)) {
379 new_opts->nls = NULL;
380 errorf(fc, "ntfs3: Cannot load iocharset %s", new_opts->nls_name);
383 if (new_opts->nls != sbi->options->nls)
384 return invalf(fc, "ntfs3: Cannot use different iocharset when remounting!");
388 if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) &&
390 errorf(fc, "ntfs3: Volume is dirty and \"force\" flag is not set!");
394 swap(sbi->options, fc->fs_private);
399 static struct kmem_cache *ntfs_inode_cachep;
401 static struct inode *ntfs_alloc_inode(struct super_block *sb)
403 struct ntfs_inode *ni = alloc_inode_sb(sb, ntfs_inode_cachep, GFP_NOFS);
408 memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode));
410 mutex_init(&ni->ni_lock);
412 return &ni->vfs_inode;
415 static void ntfs_i_callback(struct rcu_head *head)
417 struct inode *inode = container_of(head, struct inode, i_rcu);
418 struct ntfs_inode *ni = ntfs_i(inode);
420 mutex_destroy(&ni->ni_lock);
422 kmem_cache_free(ntfs_inode_cachep, ni);
425 static void ntfs_destroy_inode(struct inode *inode)
427 call_rcu(&inode->i_rcu, ntfs_i_callback);
430 static void init_once(void *foo)
432 struct ntfs_inode *ni = foo;
434 inode_init_once(&ni->vfs_inode);
438 * put_ntfs - Noinline to reduce binary size.
440 static noinline void put_ntfs(struct ntfs_sb_info *sbi)
443 kvfree(ntfs_put_shared(sbi->upcase));
444 kfree(sbi->def_table);
446 wnd_close(&sbi->mft.bitmap);
447 wnd_close(&sbi->used.bitmap);
450 iput(&sbi->mft.ni->vfs_inode);
452 if (sbi->security.ni)
453 iput(&sbi->security.ni->vfs_inode);
456 iput(&sbi->reparse.ni->vfs_inode);
459 iput(&sbi->objid.ni->vfs_inode);
462 iput(&sbi->volume.ni->vfs_inode);
464 ntfs_update_mftmirr(sbi, 0);
466 indx_clear(&sbi->security.index_sii);
467 indx_clear(&sbi->security.index_sdh);
468 indx_clear(&sbi->reparse.index_r);
469 indx_clear(&sbi->objid.index_o);
470 kfree(sbi->compress.lznt);
471 #ifdef CONFIG_NTFS3_LZX_XPRESS
472 xpress_free_decompressor(sbi->compress.xpress);
473 lzx_free_decompressor(sbi->compress.lzx);
478 static void ntfs_put_super(struct super_block *sb)
480 struct ntfs_sb_info *sbi = sb->s_fs_info;
482 /* Mark rw ntfs as clear, if possible. */
483 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
485 put_mount_options(sbi->options);
487 sb->s_fs_info = NULL;
489 sync_blockdev(sb->s_bdev);
492 static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf)
494 struct super_block *sb = dentry->d_sb;
495 struct ntfs_sb_info *sbi = sb->s_fs_info;
496 struct wnd_bitmap *wnd = &sbi->used.bitmap;
498 buf->f_type = sb->s_magic;
499 buf->f_bsize = sbi->cluster_size;
500 buf->f_blocks = wnd->nbits;
502 buf->f_bfree = buf->f_bavail = wnd_zeroes(wnd);
503 buf->f_fsid.val[0] = sbi->volume.ser_num;
504 buf->f_fsid.val[1] = (sbi->volume.ser_num >> 32);
505 buf->f_namelen = NTFS_NAME_LEN;
510 static int ntfs_show_options(struct seq_file *m, struct dentry *root)
512 struct super_block *sb = root->d_sb;
513 struct ntfs_sb_info *sbi = sb->s_fs_info;
514 struct ntfs_mount_options *opts = sbi->options;
515 struct user_namespace *user_ns = seq_user_ns(m);
517 seq_printf(m, ",uid=%u",
518 from_kuid_munged(user_ns, opts->fs_uid));
519 seq_printf(m, ",gid=%u",
520 from_kgid_munged(user_ns, opts->fs_gid));
522 seq_printf(m, ",fmask=%04o", ~opts->fs_fmask_inv);
524 seq_printf(m, ",dmask=%04o", ~opts->fs_dmask_inv);
526 seq_printf(m, ",iocharset=%s", opts->nls->charset);
528 seq_puts(m, ",iocharset=utf8");
529 if (opts->sys_immutable)
530 seq_puts(m, ",sys_immutable");
532 seq_puts(m, ",discard");
534 seq_puts(m, ",sparse");
536 seq_puts(m, ",showmeta");
538 seq_puts(m, ",nohidden");
540 seq_puts(m, ",force");
541 if (opts->noacsrules)
542 seq_puts(m, ",noacsrules");
544 seq_puts(m, ",prealloc");
545 if (sb->s_flags & SB_POSIXACL)
552 * ntfs_sync_fs - super_operations::sync_fs
554 static int ntfs_sync_fs(struct super_block *sb, int wait)
557 struct ntfs_sb_info *sbi = sb->s_fs_info;
558 struct ntfs_inode *ni;
561 ni = sbi->security.ni;
563 inode = &ni->vfs_inode;
564 err2 = _ni_write_inode(inode, wait);
571 inode = &ni->vfs_inode;
572 err2 = _ni_write_inode(inode, wait);
577 ni = sbi->reparse.ni;
579 inode = &ni->vfs_inode;
580 err2 = _ni_write_inode(inode, wait);
586 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
588 ntfs_update_mftmirr(sbi, wait);
593 static const struct super_operations ntfs_sops = {
594 .alloc_inode = ntfs_alloc_inode,
595 .destroy_inode = ntfs_destroy_inode,
596 .evict_inode = ntfs_evict_inode,
597 .put_super = ntfs_put_super,
598 .statfs = ntfs_statfs,
599 .show_options = ntfs_show_options,
600 .sync_fs = ntfs_sync_fs,
601 .write_inode = ntfs3_write_inode,
604 static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino,
610 ref.low = cpu_to_le32(ino);
611 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
612 ref.high = cpu_to_le16(ino >> 32);
616 ref.seq = cpu_to_le16(generation);
618 inode = ntfs_iget5(sb, &ref, NULL);
619 if (!IS_ERR(inode) && is_bad_inode(inode)) {
621 inode = ERR_PTR(-ESTALE);
627 static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
628 int fh_len, int fh_type)
630 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
631 ntfs_export_get_inode);
634 static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
635 int fh_len, int fh_type)
637 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
638 ntfs_export_get_inode);
641 /* TODO: == ntfs_sync_inode */
642 static int ntfs_nfs_commit_metadata(struct inode *inode)
644 return _ni_write_inode(inode, 1);
647 static const struct export_operations ntfs_export_ops = {
648 .fh_to_dentry = ntfs_fh_to_dentry,
649 .fh_to_parent = ntfs_fh_to_parent,
650 .get_parent = ntfs3_get_parent,
651 .commit_metadata = ntfs_nfs_commit_metadata,
655 * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb".
657 static u32 format_size_gb(const u64 bytes, u32 *mb)
659 /* Do simple right 30 bit shift of 64 bit value. */
660 u64 kbytes = bytes >> 10;
661 u32 kbytes32 = kbytes;
663 *mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20;
667 return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12);
670 static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot)
672 if (boot->sectors_per_clusters <= 0x80)
673 return boot->sectors_per_clusters;
674 if (boot->sectors_per_clusters >= 0xf4) /* limit shift to 2MB max */
675 return 1U << -(s8)boot->sectors_per_clusters;
680 * ntfs_init_from_boot - Init internal info from on-disk boot sector.
682 static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
685 struct ntfs_sb_info *sbi = sb->s_fs_info;
687 u32 mb, gb, boot_sector_size, sct_per_clst, record_size;
688 u64 sectors, clusters, mlcn, mlcn2;
689 struct NTFS_BOOT *boot;
690 struct buffer_head *bh;
694 sbi->volume.blocks = dev_size >> PAGE_SHIFT;
696 bh = ntfs_bread(sb, 0);
701 boot = (struct NTFS_BOOT *)bh->b_data;
703 if (memcmp(boot->system_id, "NTFS ", sizeof("NTFS ") - 1))
706 /* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/
707 /*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1])
711 boot_sector_size = (u32)boot->bytes_per_sector[1] << 8;
712 if (boot->bytes_per_sector[0] || boot_sector_size < SECTOR_SIZE ||
713 !is_power_of_2(boot_sector_size)) {
717 /* cluster size: 512, 1K, 2K, 4K, ... 2M */
718 sct_per_clst = true_sectors_per_clst(boot);
719 if ((int)sct_per_clst < 0)
721 if (!is_power_of_2(sct_per_clst))
724 mlcn = le64_to_cpu(boot->mft_clst);
725 mlcn2 = le64_to_cpu(boot->mft2_clst);
726 sectors = le64_to_cpu(boot->sectors_per_volume);
728 if (mlcn * sct_per_clst >= sectors)
731 if (mlcn2 * sct_per_clst >= sectors)
734 /* Check MFT record size. */
735 if ((boot->record_size < 0 &&
736 SECTOR_SIZE > (2U << (-boot->record_size))) ||
737 (boot->record_size >= 0 && !is_power_of_2(boot->record_size))) {
741 /* Check index record size. */
742 if ((boot->index_size < 0 &&
743 SECTOR_SIZE > (2U << (-boot->index_size))) ||
744 (boot->index_size >= 0 && !is_power_of_2(boot->index_size))) {
748 sbi->volume.size = sectors * boot_sector_size;
750 gb = format_size_gb(sbi->volume.size + boot_sector_size, &mb);
753 * - Volume formatted and mounted with the same sector size.
754 * - Volume formatted 4K and mounted as 512.
755 * - Volume formatted 512 and mounted as 4K.
757 if (boot_sector_size != sector_size) {
760 "Different NTFS' sector size (%u) and media sector size (%u)",
761 boot_sector_size, sector_size);
762 dev_size += sector_size - 1;
765 sbi->cluster_size = boot_sector_size * sct_per_clst;
766 sbi->cluster_bits = blksize_bits(sbi->cluster_size);
768 sbi->mft.lbo = mlcn << sbi->cluster_bits;
769 sbi->mft.lbo2 = mlcn2 << sbi->cluster_bits;
771 /* Compare boot's cluster and sector. */
772 if (sbi->cluster_size < boot_sector_size)
775 /* Compare boot's cluster and media sector. */
776 if (sbi->cluster_size < sector_size) {
777 /* No way to use ntfs_get_block in this case. */
780 "Failed to mount 'cause NTFS's cluster size (%u) is less than media sector size (%u)",
781 sbi->cluster_size, sector_size);
785 sbi->cluster_mask = sbi->cluster_size - 1;
786 sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask;
787 sbi->record_size = record_size = boot->record_size < 0
788 ? 1 << (-boot->record_size)
789 : (u32)boot->record_size
790 << sbi->cluster_bits;
792 if (record_size > MAXIMUM_BYTES_PER_MFT || record_size < SECTOR_SIZE)
795 sbi->record_bits = blksize_bits(record_size);
796 sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes
798 sbi->max_bytes_per_attr =
799 record_size - ALIGN(MFTRECORD_FIXUP_OFFSET_1, 8) -
800 ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) -
801 ALIGN(sizeof(enum ATTR_TYPE), 8);
803 sbi->index_size = boot->index_size < 0
804 ? 1u << (-boot->index_size)
805 : (u32)boot->index_size << sbi->cluster_bits;
807 sbi->volume.ser_num = le64_to_cpu(boot->serial_num);
809 /* Warning if RAW volume. */
810 if (dev_size < sbi->volume.size + boot_sector_size) {
813 gb0 = format_size_gb(dev_size, &mb0);
816 "RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only",
818 sb->s_flags |= SB_RDONLY;
821 clusters = sbi->volume.size >> sbi->cluster_bits;
822 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
823 /* 32 bits per cluster. */
824 if (clusters >> 32) {
827 "NTFS %u.%02u Gb is too big to use 32 bits per cluster",
831 #elif BITS_PER_LONG < 64
832 #error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS"
835 sbi->used.bitmap.nbits = clusters;
837 rec = kzalloc(record_size, GFP_NOFS);
844 rec->rhdr.sign = NTFS_FILE_SIGNATURE;
845 rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET_1);
846 fn = (sbi->record_size >> SECTOR_SHIFT) + 1;
847 rec->rhdr.fix_num = cpu_to_le16(fn);
848 ao = ALIGN(MFTRECORD_FIXUP_OFFSET_1 + sizeof(short) * fn, 8);
849 rec->attr_off = cpu_to_le16(ao);
850 rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8));
851 rec->total = cpu_to_le32(sbi->record_size);
852 ((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END;
854 sb_set_blocksize(sb, min_t(u32, sbi->cluster_size, PAGE_SIZE));
856 sbi->block_mask = sb->s_blocksize - 1;
857 sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits;
858 sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits;
860 /* Maximum size for normal files. */
861 sbi->maxbytes = (clusters << sbi->cluster_bits) - 1;
863 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
864 if (clusters >= (1ull << (64 - sbi->cluster_bits)))
866 sbi->maxbytes_sparse = -1;
867 sb->s_maxbytes = MAX_LFS_FILESIZE;
869 /* Maximum size for sparse file. */
870 sbi->maxbytes_sparse = (1ull << (sbi->cluster_bits + 32)) - 1;
871 sb->s_maxbytes = 0xFFFFFFFFull << sbi->cluster_bits;
875 * Compute the MFT zone at two steps.
876 * It would be nice if we are able to allocate 1/8 of
877 * total clusters for MFT but not more then 512 MB.
879 sbi->zone_max = min_t(CLST, 0x20000000 >> sbi->cluster_bits, clusters >> 3);
890 * ntfs_fill_super - Try to mount.
892 static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
895 struct ntfs_sb_info *sbi = sb->s_fs_info;
896 struct block_device *bdev = sb->s_bdev;
898 struct ntfs_inode *ni;
902 const struct VOLUME_INFO *info;
903 u32 idx, done, bytes;
904 struct ATTR_DEF_ENTRY *t;
911 sbi->options = fc->fs_private;
912 fc->fs_private = NULL;
913 sb->s_flags |= SB_NODIRATIME;
914 sb->s_magic = 0x7366746e; // "ntfs"
915 sb->s_op = &ntfs_sops;
916 sb->s_export_op = &ntfs_export_ops;
917 sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec
918 sb->s_xattr = ntfs_xattr_handlers;
920 sbi->options->nls = ntfs_load_nls(sbi->options->nls_name);
921 if (IS_ERR(sbi->options->nls)) {
922 sbi->options->nls = NULL;
923 errorf(fc, "Cannot load nls %s", sbi->options->nls_name);
928 if (bdev_max_discard_sectors(bdev) && bdev_discard_granularity(bdev)) {
929 sbi->discard_granularity = bdev_discard_granularity(bdev);
930 sbi->discard_granularity_mask_inv =
931 ~(u64)(sbi->discard_granularity - 1);
935 err = ntfs_init_from_boot(sb, bdev_logical_block_size(bdev),
936 bdev_nr_bytes(bdev));
941 * Load $Volume. This should be done before $LogFile
942 * 'cause 'sbi->volume.ni' is used 'ntfs_set_state'.
944 ref.low = cpu_to_le32(MFT_REC_VOL);
945 ref.seq = cpu_to_le16(MFT_REC_VOL);
946 inode = ntfs_iget5(sb, &ref, &NAME_VOLUME);
948 ntfs_err(sb, "Failed to load $Volume.");
949 err = PTR_ERR(inode);
955 /* Load and save label (not necessary). */
956 attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL);
959 /* It is ok if no ATTR_LABEL */
960 } else if (!attr->non_res && !is_attr_ext(attr)) {
961 /* $AttrDef allows labels to be up to 128 symbols. */
962 err = utf16s_to_utf8s(resident_data(attr),
963 le32_to_cpu(attr->res.data_size) >> 1,
964 UTF16_LITTLE_ENDIAN, sbi->volume.label,
965 sizeof(sbi->volume.label));
967 sbi->volume.label[0] = 0;
969 /* Should we break mounting here? */
971 //goto put_inode_out;
974 attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL);
975 if (!attr || is_attr_ext(attr)) {
980 info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO);
986 sbi->volume.major_ver = info->major_ver;
987 sbi->volume.minor_ver = info->minor_ver;
988 sbi->volume.flags = info->flags;
991 /* Load $MFTMirr to estimate recs_mirr. */
992 ref.low = cpu_to_le32(MFT_REC_MIRR);
993 ref.seq = cpu_to_le16(MFT_REC_MIRR);
994 inode = ntfs_iget5(sb, &ref, &NAME_MIRROR);
996 ntfs_err(sb, "Failed to load $MFTMirr.");
997 err = PTR_ERR(inode);
1001 sbi->mft.recs_mirr =
1002 ntfs_up_cluster(sbi, inode->i_size) >> sbi->record_bits;
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);
1018 err = ntfs_loadlog_and_replay(ni, sbi);
1024 if (sbi->flags & NTFS_FLAGS_NEED_REPLAY) {
1025 if (!sb_rdonly(sb)) {
1027 "failed to replay log file. Can't mount rw!");
1031 } else if (sbi->volume.flags & VOLUME_FLAG_DIRTY) {
1032 if (!sb_rdonly(sb) && !sbi->options->force) {
1035 "volume is dirty and \"force\" flag is not set!");
1042 ref.low = cpu_to_le32(MFT_REC_MFT);
1043 ref.seq = cpu_to_le16(1);
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);
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;
1058 err = wnd_init(&sbi->mft.bitmap, sb, tt);
1062 err = ni_load_all_mi(ni);
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);
1080 for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) {
1081 if (lcn == SPARSE_LCN)
1084 if (!sbi->bad_clusters)
1085 ntfs_notice(sb, "Volume contains bad blocks");
1087 sbi->bad_clusters += len;
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);
1102 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
1103 if (inode->i_size >> 32) {
1109 /* Check bitmap boundary. */
1110 tt = sbi->used.bitmap.nbits;
1111 if (inode->i_size < bitmap_size(tt)) {
1116 /* Not necessary. */
1117 sbi->used.bitmap.set_tail = true;
1118 err = wnd_init(&sbi->used.bitmap, sb, tt);
1124 /* Compute the MFT zone. */
1125 err = ntfs_refresh_zone(sbi);
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);
1139 if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY)) {
1143 bytes = inode->i_size;
1144 sbi->def_table = t = kmalloc(bytes, GFP_NOFS | __GFP_NOWARN);
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);
1155 err = PTR_ERR(page);
1158 memcpy(Add2Ptr(t, done), page_address(page),
1159 min(PAGE_SIZE, tail));
1160 ntfs_unmap_page(page);
1162 if (!idx && ATTR_STD != t->type) {
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 */
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);
1178 if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32)
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;
1186 done += sizeof(struct ATTR_DEF_ENTRY);
1188 sbi->def_entries += 1;
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);
1202 if (inode->i_size != 0x10000 * sizeof(short)) {
1207 for (idx = 0; idx < (0x10000 * sizeof(short) >> PAGE_SHIFT); idx++) {
1209 u16 *dst = Add2Ptr(sbi->upcase, idx << PAGE_SHIFT);
1210 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1213 err = PTR_ERR(page);
1217 src = page_address(page);
1220 for (i = 0; i < PAGE_SIZE / sizeof(u16); i++)
1221 *dst++ = le16_to_cpu(*src++);
1223 memcpy(dst, src, PAGE_SIZE);
1225 ntfs_unmap_page(page);
1228 shared = ntfs_set_shared(sbi->upcase, 0x10000 * sizeof(short));
1229 if (shared && sbi->upcase != shared) {
1230 kvfree(sbi->upcase);
1231 sbi->upcase = shared;
1236 if (is_ntfs3(sbi)) {
1238 err = ntfs_security_init(sbi);
1243 err = ntfs_extend_init(sbi);
1247 /* Load $Extend\$Reparse. */
1248 err = ntfs_reparse_init(sbi);
1252 /* Load $Extend\$ObjId. */
1253 err = ntfs_objid_init(sbi);
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) || !inode->i_op) {
1264 ntfs_err(sb, "Failed to load root.");
1265 err = IS_ERR(inode) ? PTR_ERR(inode) : -EINVAL;
1269 sb->s_root = d_make_root(inode);
1281 * Free resources here.
1282 * ntfs_fs_free will be called with fc->s_fs_info = NULL
1284 put_mount_options(sbi->options);
1286 sb->s_fs_info = NULL;
1291 void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len)
1293 struct ntfs_sb_info *sbi = sb->s_fs_info;
1294 struct block_device *bdev = sb->s_bdev;
1295 sector_t devblock = (u64)lcn * sbi->blocks_per_cluster;
1296 unsigned long blocks = (u64)len * sbi->blocks_per_cluster;
1297 unsigned long cnt = 0;
1298 unsigned long limit = global_zone_page_state(NR_FREE_PAGES)
1299 << (PAGE_SHIFT - sb->s_blocksize_bits);
1301 if (limit >= 0x2000)
1303 else if (limit < 32)
1309 clean_bdev_aliases(bdev, devblock++, 1);
1310 if (cnt++ >= limit) {
1311 sync_blockdev(bdev);
1318 * ntfs_discard - Issue a discard request (trim for SSD).
1320 int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len)
1323 u64 lbo, bytes, start, end;
1324 struct super_block *sb;
1326 if (sbi->used.next_free_lcn == lcn + len)
1327 sbi->used.next_free_lcn = lcn;
1329 if (sbi->flags & NTFS_FLAGS_NODISCARD)
1332 if (!sbi->options->discard)
1335 lbo = (u64)lcn << sbi->cluster_bits;
1336 bytes = (u64)len << sbi->cluster_bits;
1338 /* Align up 'start' on discard_granularity. */
1339 start = (lbo + sbi->discard_granularity - 1) &
1340 sbi->discard_granularity_mask_inv;
1341 /* Align down 'end' on discard_granularity. */
1342 end = (lbo + bytes) & sbi->discard_granularity_mask_inv;
1348 err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9,
1351 if (err == -EOPNOTSUPP)
1352 sbi->flags |= NTFS_FLAGS_NODISCARD;
1357 static int ntfs_fs_get_tree(struct fs_context *fc)
1359 return get_tree_bdev(fc, ntfs_fill_super);
1363 * ntfs_fs_free - Free fs_context.
1365 * Note that this will be called after fill_super and reconfigure
1366 * even when they pass. So they have to take pointers if they pass.
1368 static void ntfs_fs_free(struct fs_context *fc)
1370 struct ntfs_mount_options *opts = fc->fs_private;
1371 struct ntfs_sb_info *sbi = fc->s_fs_info;
1377 put_mount_options(opts);
1380 static const struct fs_context_operations ntfs_context_ops = {
1381 .parse_param = ntfs_fs_parse_param,
1382 .get_tree = ntfs_fs_get_tree,
1383 .reconfigure = ntfs_fs_reconfigure,
1384 .free = ntfs_fs_free,
1388 * ntfs_init_fs_context - Initialize spi and opts
1390 * This will called when mount/remount. We will first initialize
1391 * options so that if remount we can use just that.
1393 static int ntfs_init_fs_context(struct fs_context *fc)
1395 struct ntfs_mount_options *opts;
1396 struct ntfs_sb_info *sbi;
1398 opts = kzalloc(sizeof(struct ntfs_mount_options), GFP_NOFS);
1402 /* Default options. */
1403 opts->fs_uid = current_uid();
1404 opts->fs_gid = current_gid();
1405 opts->fs_fmask_inv = ~current_umask();
1406 opts->fs_dmask_inv = ~current_umask();
1408 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
1411 sbi = kzalloc(sizeof(struct ntfs_sb_info), GFP_NOFS);
1415 sbi->upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL);
1419 ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1420 DEFAULT_RATELIMIT_BURST);
1422 mutex_init(&sbi->compress.mtx_lznt);
1423 #ifdef CONFIG_NTFS3_LZX_XPRESS
1424 mutex_init(&sbi->compress.mtx_xpress);
1425 mutex_init(&sbi->compress.mtx_lzx);
1428 fc->s_fs_info = sbi;
1430 fc->fs_private = opts;
1431 fc->ops = &ntfs_context_ops;
1442 static struct file_system_type ntfs_fs_type = {
1443 .owner = THIS_MODULE,
1445 .init_fs_context = ntfs_init_fs_context,
1446 .parameters = ntfs_fs_parameters,
1447 .kill_sb = kill_block_super,
1448 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
1452 static int __init init_ntfs_fs(void)
1456 pr_info("ntfs3: Max link count %u\n", NTFS_LINK_MAX);
1458 if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL))
1459 pr_info("ntfs3: Enabled Linux POSIX ACLs support\n");
1460 if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER))
1461 pr_notice("ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n");
1462 if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS))
1463 pr_info("ntfs3: Read-only LZX/Xpress compression included\n");
1465 err = ntfs3_init_bitmap();
1469 ntfs_inode_cachep = kmem_cache_create(
1470 "ntfs_inode_cache", sizeof(struct ntfs_inode), 0,
1471 (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1473 if (!ntfs_inode_cachep) {
1478 err = register_filesystem(&ntfs_fs_type);
1484 kmem_cache_destroy(ntfs_inode_cachep);
1486 ntfs3_exit_bitmap();
1490 static void __exit exit_ntfs_fs(void)
1492 if (ntfs_inode_cachep) {
1494 kmem_cache_destroy(ntfs_inode_cachep);
1497 unregister_filesystem(&ntfs_fs_type);
1498 ntfs3_exit_bitmap();
1501 MODULE_LICENSE("GPL");
1502 MODULE_DESCRIPTION("ntfs3 read/write filesystem");
1503 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1504 MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support");
1506 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
1507 MODULE_INFO(cluster, "Warning: Activated 64 bits per cluster. Windows does not support this");
1509 #ifdef CONFIG_NTFS3_LZX_XPRESS
1510 MODULE_INFO(compression, "Read-only lzx/xpress compression included");
1513 MODULE_AUTHOR("Konstantin Komarov");
1514 MODULE_ALIAS_FS("ntfs3");
1516 module_init(init_ntfs_fs);
1517 module_exit(exit_ntfs_fs);