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/backing-dev.h>
27 #include <linux/blkdev.h>
28 #include <linux/buffer_head.h>
29 #include <linux/exportfs.h>
31 #include <linux/iversion.h>
32 #include <linux/log2.h>
33 #include <linux/module.h>
34 #include <linux/nls.h>
35 #include <linux/parser.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.
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 clear_mount_options(struct ntfs_mount_options *options)
210 unload_nls(options->nls);
233 static const match_table_t ntfs_tokens = {
234 { Opt_uid, "uid=%u" },
235 { Opt_gid, "gid=%u" },
236 { Opt_umask, "umask=%o" },
237 { Opt_dmask, "dmask=%o" },
238 { Opt_fmask, "fmask=%o" },
239 { Opt_immutable, "sys_immutable" },
240 { Opt_discard, "discard" },
241 { Opt_force, "force" },
242 { Opt_sparse, "sparse" },
243 { Opt_nohidden, "nohidden" },
245 { Opt_noatime, "noatime" },
246 { Opt_showmeta, "showmeta" },
247 { Opt_nls, "nls=%s" },
248 { Opt_prealloc, "prealloc" },
249 { Opt_no_acs_rules, "no_acs_rules" },
253 static noinline int ntfs_parse_options(struct super_block *sb, char *options,
255 struct ntfs_mount_options *opts)
258 substring_t args[MAX_OPT_ARGS];
261 struct nls_table *nls;
263 opts->fs_uid = current_uid();
264 opts->fs_gid = current_gid();
265 opts->fs_fmask_inv = opts->fs_dmask_inv = ~current_umask();
271 while ((p = strsep(&options, ","))) {
277 token = match_token(p, ntfs_tokens, args);
280 opts->sys_immutable = 1;
283 if (match_int(&args[0], &option))
285 opts->fs_uid = make_kuid(current_user_ns(), option);
286 if (!uid_valid(opts->fs_uid))
291 if (match_int(&args[0], &option))
293 opts->fs_gid = make_kgid(current_user_ns(), option);
294 if (!gid_valid(opts->fs_gid))
299 if (match_octal(&args[0], &option))
301 opts->fs_fmask_inv = opts->fs_dmask_inv = ~option;
302 opts->fmask = opts->dmask = 1;
305 if (match_octal(&args[0], &option))
307 opts->fs_dmask_inv = ~option;
311 if (match_octal(&args[0], &option))
313 opts->fs_fmask_inv = ~option;
329 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
330 sb->s_flags |= SB_POSIXACL;
333 ntfs_err(sb, "support for ACL not compiled in!");
337 sb->s_flags |= SB_NOATIME;
343 match_strlcpy(nls_name, &args[0], sizeof(nls_name));
348 case Opt_no_acs_rules:
349 opts->no_acs_rules = 1;
355 "Unrecognized mount option \"%s\" or missing value",
362 if (!strcmp(nls_name[0] ? nls_name : CONFIG_NLS_DEFAULT, "utf8")) {
364 * For UTF-8 use utf16s_to_utf8s()/utf8s_to_utf16s()
368 } else if (nls_name[0]) {
369 nls = load_nls(nls_name);
371 ntfs_err(sb, "failed to load \"%s\"", nls_name);
375 nls = load_nls_default();
377 ntfs_err(sb, "failed to load default nls");
386 static int ntfs_remount(struct super_block *sb, int *flags, char *data)
389 struct ntfs_sb_info *sbi = sb->s_fs_info;
390 struct ntfs_mount_options old_opts;
391 char *orig_data = kstrdup(data, GFP_KERNEL);
393 if (data && !orig_data)
396 /* Store original options. */
397 memcpy(&old_opts, &sbi->options, sizeof(old_opts));
398 clear_mount_options(&sbi->options);
399 memset(&sbi->options, 0, sizeof(sbi->options));
401 err = ntfs_parse_options(sb, data, 0, &sbi->options);
405 ro_rw = sb_rdonly(sb) && !(*flags & SB_RDONLY);
406 if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) {
409 "Couldn't remount rw because journal is not replayed. Please umount/remount instead\n");
416 if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) &&
417 !sbi->options.force) {
418 ntfs_warn(sb, "volume is dirty and \"force\" flag is not set!");
423 clear_mount_options(&old_opts);
425 *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME) |
426 SB_NODIRATIME | SB_NOATIME;
427 ntfs_info(sb, "re-mounted. Opts: %s", orig_data);
432 clear_mount_options(&sbi->options);
433 memcpy(&sbi->options, &old_opts, sizeof(old_opts));
440 static struct kmem_cache *ntfs_inode_cachep;
442 static struct inode *ntfs_alloc_inode(struct super_block *sb)
444 struct ntfs_inode *ni = kmem_cache_alloc(ntfs_inode_cachep, GFP_NOFS);
449 memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode));
451 mutex_init(&ni->ni_lock);
453 return &ni->vfs_inode;
456 static void ntfs_i_callback(struct rcu_head *head)
458 struct inode *inode = container_of(head, struct inode, i_rcu);
459 struct ntfs_inode *ni = ntfs_i(inode);
461 mutex_destroy(&ni->ni_lock);
463 kmem_cache_free(ntfs_inode_cachep, ni);
466 static void ntfs_destroy_inode(struct inode *inode)
468 call_rcu(&inode->i_rcu, ntfs_i_callback);
471 static void init_once(void *foo)
473 struct ntfs_inode *ni = foo;
475 inode_init_once(&ni->vfs_inode);
479 * put_ntfs - Noinline to reduce binary size.
481 static noinline void put_ntfs(struct ntfs_sb_info *sbi)
484 kvfree(ntfs_put_shared(sbi->upcase));
485 kfree(sbi->def_table);
487 wnd_close(&sbi->mft.bitmap);
488 wnd_close(&sbi->used.bitmap);
491 iput(&sbi->mft.ni->vfs_inode);
493 if (sbi->security.ni)
494 iput(&sbi->security.ni->vfs_inode);
497 iput(&sbi->reparse.ni->vfs_inode);
500 iput(&sbi->objid.ni->vfs_inode);
503 iput(&sbi->volume.ni->vfs_inode);
505 ntfs_update_mftmirr(sbi, 0);
507 indx_clear(&sbi->security.index_sii);
508 indx_clear(&sbi->security.index_sdh);
509 indx_clear(&sbi->reparse.index_r);
510 indx_clear(&sbi->objid.index_o);
511 kfree(sbi->compress.lznt);
512 #ifdef CONFIG_NTFS3_LZX_XPRESS
513 xpress_free_decompressor(sbi->compress.xpress);
514 lzx_free_decompressor(sbi->compress.lzx);
516 clear_mount_options(&sbi->options);
521 static void ntfs_put_super(struct super_block *sb)
523 struct ntfs_sb_info *sbi = sb->s_fs_info;
525 /* Mark rw ntfs as clear, if possible. */
526 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
530 sync_blockdev(sb->s_bdev);
533 static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf)
535 struct super_block *sb = dentry->d_sb;
536 struct ntfs_sb_info *sbi = sb->s_fs_info;
537 struct wnd_bitmap *wnd = &sbi->used.bitmap;
539 buf->f_type = sb->s_magic;
540 buf->f_bsize = sbi->cluster_size;
541 buf->f_blocks = wnd->nbits;
543 buf->f_bfree = buf->f_bavail = wnd_zeroes(wnd);
544 buf->f_fsid.val[0] = sbi->volume.ser_num;
545 buf->f_fsid.val[1] = (sbi->volume.ser_num >> 32);
546 buf->f_namelen = NTFS_NAME_LEN;
551 static int ntfs_show_options(struct seq_file *m, struct dentry *root)
553 struct super_block *sb = root->d_sb;
554 struct ntfs_sb_info *sbi = sb->s_fs_info;
555 struct ntfs_mount_options *opts = &sbi->options;
556 struct user_namespace *user_ns = seq_user_ns(m);
559 seq_printf(m, ",uid=%u",
560 from_kuid_munged(user_ns, opts->fs_uid));
562 seq_printf(m, ",gid=%u",
563 from_kgid_munged(user_ns, opts->fs_gid));
565 seq_printf(m, ",fmask=%04o", ~opts->fs_fmask_inv);
567 seq_printf(m, ",dmask=%04o", ~opts->fs_dmask_inv);
569 seq_printf(m, ",nls=%s", opts->nls->charset);
571 seq_puts(m, ",nls=utf8");
572 if (opts->sys_immutable)
573 seq_puts(m, ",sys_immutable");
575 seq_puts(m, ",discard");
577 seq_puts(m, ",sparse");
579 seq_puts(m, ",showmeta");
581 seq_puts(m, ",nohidden");
583 seq_puts(m, ",force");
584 if (opts->no_acs_rules)
585 seq_puts(m, ",no_acs_rules");
587 seq_puts(m, ",prealloc");
588 if (sb->s_flags & SB_POSIXACL)
590 if (sb->s_flags & SB_NOATIME)
591 seq_puts(m, ",noatime");
597 * ntfs_sync_fs - super_operations::sync_fs
599 static int ntfs_sync_fs(struct super_block *sb, int wait)
602 struct ntfs_sb_info *sbi = sb->s_fs_info;
603 struct ntfs_inode *ni;
606 ni = sbi->security.ni;
608 inode = &ni->vfs_inode;
609 err2 = _ni_write_inode(inode, wait);
616 inode = &ni->vfs_inode;
617 err2 = _ni_write_inode(inode, wait);
622 ni = sbi->reparse.ni;
624 inode = &ni->vfs_inode;
625 err2 = _ni_write_inode(inode, wait);
631 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
633 ntfs_update_mftmirr(sbi, wait);
638 static const struct super_operations ntfs_sops = {
639 .alloc_inode = ntfs_alloc_inode,
640 .destroy_inode = ntfs_destroy_inode,
641 .evict_inode = ntfs_evict_inode,
642 .put_super = ntfs_put_super,
643 .statfs = ntfs_statfs,
644 .show_options = ntfs_show_options,
645 .sync_fs = ntfs_sync_fs,
646 .remount_fs = ntfs_remount,
647 .write_inode = ntfs3_write_inode,
650 static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino,
656 ref.low = cpu_to_le32(ino);
657 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
658 ref.high = cpu_to_le16(ino >> 32);
662 ref.seq = cpu_to_le16(generation);
664 inode = ntfs_iget5(sb, &ref, NULL);
665 if (!IS_ERR(inode) && is_bad_inode(inode)) {
667 inode = ERR_PTR(-ESTALE);
673 static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
674 int fh_len, int fh_type)
676 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
677 ntfs_export_get_inode);
680 static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
681 int fh_len, int fh_type)
683 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
684 ntfs_export_get_inode);
687 /* TODO: == ntfs_sync_inode */
688 static int ntfs_nfs_commit_metadata(struct inode *inode)
690 return _ni_write_inode(inode, 1);
693 static const struct export_operations ntfs_export_ops = {
694 .fh_to_dentry = ntfs_fh_to_dentry,
695 .fh_to_parent = ntfs_fh_to_parent,
696 .get_parent = ntfs3_get_parent,
697 .commit_metadata = ntfs_nfs_commit_metadata,
701 * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb".
703 static u32 format_size_gb(const u64 bytes, u32 *mb)
705 /* Do simple right 30 bit shift of 64 bit value. */
706 u64 kbytes = bytes >> 10;
707 u32 kbytes32 = kbytes;
709 *mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20;
713 return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12);
716 static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot)
718 return boot->sectors_per_clusters <= 0x80
719 ? boot->sectors_per_clusters
720 : (1u << (0 - boot->sectors_per_clusters));
724 * ntfs_init_from_boot - Init internal info from on-disk boot sector.
726 static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
729 struct ntfs_sb_info *sbi = sb->s_fs_info;
731 u32 mb, gb, boot_sector_size, sct_per_clst, record_size;
732 u64 sectors, clusters, fs_size, mlcn, mlcn2;
733 struct NTFS_BOOT *boot;
734 struct buffer_head *bh;
738 sbi->volume.blocks = dev_size >> PAGE_SHIFT;
740 bh = ntfs_bread(sb, 0);
745 boot = (struct NTFS_BOOT *)bh->b_data;
747 if (memcmp(boot->system_id, "NTFS ", sizeof("NTFS ") - 1))
750 /* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/
751 /*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1])
755 boot_sector_size = (u32)boot->bytes_per_sector[1] << 8;
756 if (boot->bytes_per_sector[0] || boot_sector_size < SECTOR_SIZE ||
757 !is_power_of_2(boot_sector_size)) {
761 /* cluster size: 512, 1K, 2K, 4K, ... 2M */
762 sct_per_clst = true_sectors_per_clst(boot);
763 if (!is_power_of_2(sct_per_clst))
766 mlcn = le64_to_cpu(boot->mft_clst);
767 mlcn2 = le64_to_cpu(boot->mft2_clst);
768 sectors = le64_to_cpu(boot->sectors_per_volume);
770 if (mlcn * sct_per_clst >= sectors)
773 if (mlcn2 * sct_per_clst >= sectors)
776 /* Check MFT record size. */
777 if ((boot->record_size < 0 &&
778 SECTOR_SIZE > (2U << (-boot->record_size))) ||
779 (boot->record_size >= 0 && !is_power_of_2(boot->record_size))) {
783 /* Check index record size. */
784 if ((boot->index_size < 0 &&
785 SECTOR_SIZE > (2U << (-boot->index_size))) ||
786 (boot->index_size >= 0 && !is_power_of_2(boot->index_size))) {
790 sbi->sector_size = boot_sector_size;
791 sbi->sector_bits = blksize_bits(boot_sector_size);
792 fs_size = (sectors + 1) << sbi->sector_bits;
794 gb = format_size_gb(fs_size, &mb);
797 * - Volume formatted and mounted with the same sector size.
798 * - Volume formatted 4K and mounted as 512.
799 * - Volume formatted 512 and mounted as 4K.
801 if (sbi->sector_size != sector_size) {
803 "Different NTFS' sector size and media sector size");
804 dev_size += sector_size - 1;
807 sbi->cluster_size = boot_sector_size * sct_per_clst;
808 sbi->cluster_bits = blksize_bits(sbi->cluster_size);
810 sbi->mft.lbo = mlcn << sbi->cluster_bits;
811 sbi->mft.lbo2 = mlcn2 << sbi->cluster_bits;
813 if (sbi->cluster_size < sbi->sector_size)
816 sbi->cluster_mask = sbi->cluster_size - 1;
817 sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask;
818 sbi->record_size = record_size = boot->record_size < 0
819 ? 1 << (-boot->record_size)
820 : (u32)boot->record_size
821 << sbi->cluster_bits;
823 if (record_size > MAXIMUM_BYTES_PER_MFT)
826 sbi->record_bits = blksize_bits(record_size);
827 sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes
829 sbi->max_bytes_per_attr =
830 record_size - ALIGN(MFTRECORD_FIXUP_OFFSET_1, 8) -
831 ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) -
832 ALIGN(sizeof(enum ATTR_TYPE), 8);
834 sbi->index_size = boot->index_size < 0
835 ? 1u << (-boot->index_size)
836 : (u32)boot->index_size << sbi->cluster_bits;
838 sbi->volume.ser_num = le64_to_cpu(boot->serial_num);
839 sbi->volume.size = sectors << sbi->sector_bits;
841 /* Warning if RAW volume. */
842 if (dev_size < fs_size) {
845 gb0 = format_size_gb(dev_size, &mb0);
848 "RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only",
850 sb->s_flags |= SB_RDONLY;
853 clusters = sbi->volume.size >> sbi->cluster_bits;
854 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
855 /* 32 bits per cluster. */
856 if (clusters >> 32) {
859 "NTFS %u.%02u Gb is too big to use 32 bits per cluster",
863 #elif BITS_PER_LONG < 64
864 #error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS"
867 sbi->used.bitmap.nbits = clusters;
869 rec = kzalloc(record_size, GFP_NOFS);
876 rec->rhdr.sign = NTFS_FILE_SIGNATURE;
877 rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET_1);
878 fn = (sbi->record_size >> SECTOR_SHIFT) + 1;
879 rec->rhdr.fix_num = cpu_to_le16(fn);
880 ao = ALIGN(MFTRECORD_FIXUP_OFFSET_1 + sizeof(short) * fn, 8);
881 rec->attr_off = cpu_to_le16(ao);
882 rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8));
883 rec->total = cpu_to_le32(sbi->record_size);
884 ((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END;
886 if (sbi->cluster_size < PAGE_SIZE)
887 sb_set_blocksize(sb, sbi->cluster_size);
889 sbi->block_mask = sb->s_blocksize - 1;
890 sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits;
891 sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits;
893 /* Maximum size for normal files. */
894 sbi->maxbytes = (clusters << sbi->cluster_bits) - 1;
896 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
897 if (clusters >= (1ull << (64 - sbi->cluster_bits)))
899 sbi->maxbytes_sparse = -1;
901 /* Maximum size for sparse file. */
902 sbi->maxbytes_sparse = (1ull << (sbi->cluster_bits + 32)) - 1;
914 * ntfs_fill_super - Try to mount.
916 static int ntfs_fill_super(struct super_block *sb, void *data, int silent)
919 struct ntfs_sb_info *sbi;
920 struct block_device *bdev = sb->s_bdev;
921 struct inode *bd_inode = bdev->bd_inode;
922 struct request_queue *rq = bdev_get_queue(bdev);
923 struct inode *inode = NULL;
924 struct ntfs_inode *ni;
928 const struct VOLUME_INFO *info;
929 u32 idx, done, bytes;
930 struct ATTR_DEF_ENTRY *t;
938 sbi = kzalloc(sizeof(struct ntfs_sb_info), GFP_NOFS);
944 sb->s_flags |= SB_NODIRATIME;
945 sb->s_magic = 0x7366746e; // "ntfs"
946 sb->s_op = &ntfs_sops;
947 sb->s_export_op = &ntfs_export_ops;
948 sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec
949 sb->s_xattr = ntfs_xattr_handlers;
951 ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL,
952 DEFAULT_RATELIMIT_BURST);
954 err = ntfs_parse_options(sb, data, silent, &sbi->options);
958 if (!rq || !blk_queue_discard(rq) || !rq->limits.discard_granularity) {
961 sbi->discard_granularity = rq->limits.discard_granularity;
962 sbi->discard_granularity_mask_inv =
963 ~(u64)(sbi->discard_granularity - 1);
966 sb_set_blocksize(sb, PAGE_SIZE);
969 err = ntfs_init_from_boot(sb, rq ? queue_logical_block_size(rq) : 512,
974 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
975 sb->s_maxbytes = MAX_LFS_FILESIZE;
977 sb->s_maxbytes = 0xFFFFFFFFull << sbi->cluster_bits;
980 mutex_init(&sbi->compress.mtx_lznt);
981 #ifdef CONFIG_NTFS3_LZX_XPRESS
982 mutex_init(&sbi->compress.mtx_xpress);
983 mutex_init(&sbi->compress.mtx_lzx);
987 * Load $Volume. This should be done before $LogFile
988 * 'cause 'sbi->volume.ni' is used 'ntfs_set_state'.
990 ref.low = cpu_to_le32(MFT_REC_VOL);
991 ref.seq = cpu_to_le16(MFT_REC_VOL);
992 inode = ntfs_iget5(sb, &ref, &NAME_VOLUME);
994 err = PTR_ERR(inode);
995 ntfs_err(sb, "Failed to load $Volume.");
1002 /* Load and save label (not necessary). */
1003 attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL);
1006 /* It is ok if no ATTR_LABEL */
1007 } else if (!attr->non_res && !is_attr_ext(attr)) {
1008 /* $AttrDef allows labels to be up to 128 symbols. */
1009 err = utf16s_to_utf8s(resident_data(attr),
1010 le32_to_cpu(attr->res.data_size) >> 1,
1011 UTF16_LITTLE_ENDIAN, sbi->volume.label,
1012 sizeof(sbi->volume.label));
1014 sbi->volume.label[0] = 0;
1016 /* Should we break mounting here? */
1021 attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL);
1022 if (!attr || is_attr_ext(attr)) {
1027 info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO);
1033 sbi->volume.major_ver = info->major_ver;
1034 sbi->volume.minor_ver = info->minor_ver;
1035 sbi->volume.flags = info->flags;
1037 sbi->volume.ni = ni;
1040 /* Load $MFTMirr to estimate recs_mirr. */
1041 ref.low = cpu_to_le32(MFT_REC_MIRR);
1042 ref.seq = cpu_to_le16(MFT_REC_MIRR);
1043 inode = ntfs_iget5(sb, &ref, &NAME_MIRROR);
1044 if (IS_ERR(inode)) {
1045 err = PTR_ERR(inode);
1046 ntfs_err(sb, "Failed to load $MFTMirr.");
1051 sbi->mft.recs_mirr =
1052 ntfs_up_cluster(sbi, inode->i_size) >> sbi->record_bits;
1056 /* Load LogFile to replay. */
1057 ref.low = cpu_to_le32(MFT_REC_LOG);
1058 ref.seq = cpu_to_le16(MFT_REC_LOG);
1059 inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE);
1060 if (IS_ERR(inode)) {
1061 err = PTR_ERR(inode);
1062 ntfs_err(sb, "Failed to load \x24LogFile.");
1069 err = ntfs_loadlog_and_replay(ni, sbi);
1076 is_ro = sb_rdonly(sbi->sb);
1078 if (sbi->flags & NTFS_FLAGS_NEED_REPLAY) {
1081 "failed to replay log file. Can't mount rw!");
1085 } else if (sbi->volume.flags & VOLUME_FLAG_DIRTY) {
1086 if (!is_ro && !sbi->options.force) {
1089 "volume is dirty and \"force\" flag is not set!");
1096 ref.low = cpu_to_le32(MFT_REC_MFT);
1097 ref.seq = cpu_to_le16(1);
1099 inode = ntfs_iget5(sb, &ref, &NAME_MFT);
1100 if (IS_ERR(inode)) {
1101 err = PTR_ERR(inode);
1102 ntfs_err(sb, "Failed to load $MFT.");
1109 sbi->mft.used = ni->i_valid >> sbi->record_bits;
1110 tt = inode->i_size >> sbi->record_bits;
1111 sbi->mft.next_free = MFT_REC_USER;
1113 err = wnd_init(&sbi->mft.bitmap, sb, tt);
1117 err = ni_load_all_mi(ni);
1123 /* Load $BadClus. */
1124 ref.low = cpu_to_le32(MFT_REC_BADCLUST);
1125 ref.seq = cpu_to_le16(MFT_REC_BADCLUST);
1126 inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS);
1127 if (IS_ERR(inode)) {
1128 err = PTR_ERR(inode);
1129 ntfs_err(sb, "Failed to load $BadClus.");
1136 for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) {
1137 if (lcn == SPARSE_LCN)
1140 if (!sbi->bad_clusters)
1141 ntfs_notice(sb, "Volume contains bad blocks");
1143 sbi->bad_clusters += len;
1149 ref.low = cpu_to_le32(MFT_REC_BITMAP);
1150 ref.seq = cpu_to_le16(MFT_REC_BITMAP);
1151 inode = ntfs_iget5(sb, &ref, &NAME_BITMAP);
1152 if (IS_ERR(inode)) {
1153 err = PTR_ERR(inode);
1154 ntfs_err(sb, "Failed to load $Bitmap.");
1161 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
1162 if (inode->i_size >> 32) {
1168 /* Check bitmap boundary. */
1169 tt = sbi->used.bitmap.nbits;
1170 if (inode->i_size < bitmap_size(tt)) {
1175 /* Not necessary. */
1176 sbi->used.bitmap.set_tail = true;
1177 err = wnd_init(&sbi->used.bitmap, sbi->sb, tt);
1183 /* Compute the MFT zone. */
1184 err = ntfs_refresh_zone(sbi);
1188 /* Load $AttrDef. */
1189 ref.low = cpu_to_le32(MFT_REC_ATTR);
1190 ref.seq = cpu_to_le16(MFT_REC_ATTR);
1191 inode = ntfs_iget5(sbi->sb, &ref, &NAME_ATTRDEF);
1192 if (IS_ERR(inode)) {
1193 err = PTR_ERR(inode);
1194 ntfs_err(sb, "Failed to load $AttrDef -> %d", err);
1199 if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY)) {
1203 bytes = inode->i_size;
1204 sbi->def_table = t = kmalloc(bytes, GFP_NOFS);
1210 for (done = idx = 0; done < bytes; done += PAGE_SIZE, idx++) {
1211 unsigned long tail = bytes - done;
1212 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1215 err = PTR_ERR(page);
1218 memcpy(Add2Ptr(t, done), page_address(page),
1219 min(PAGE_SIZE, tail));
1220 ntfs_unmap_page(page);
1222 if (!idx && ATTR_STD != t->type) {
1229 sbi->def_entries = 1;
1230 done = sizeof(struct ATTR_DEF_ENTRY);
1231 sbi->reparse.max_size = MAXIMUM_REPARSE_DATA_BUFFER_SIZE;
1232 sbi->ea_max_size = 0x10000; /* default formatter value */
1234 while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) {
1235 u32 t32 = le32_to_cpu(t->type);
1236 u64 sz = le64_to_cpu(t->max_sz);
1238 if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32)
1241 if (t->type == ATTR_REPARSE)
1242 sbi->reparse.max_size = sz;
1243 else if (t->type == ATTR_EA)
1244 sbi->ea_max_size = sz;
1246 done += sizeof(struct ATTR_DEF_ENTRY);
1248 sbi->def_entries += 1;
1253 ref.low = cpu_to_le32(MFT_REC_UPCASE);
1254 ref.seq = cpu_to_le16(MFT_REC_UPCASE);
1255 inode = ntfs_iget5(sb, &ref, &NAME_UPCASE);
1256 if (IS_ERR(inode)) {
1257 err = PTR_ERR(inode);
1258 ntfs_err(sb, "Failed to load \x24LogFile.");
1265 if (inode->i_size != 0x10000 * sizeof(short)) {
1270 sbi->upcase = upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL);
1276 for (idx = 0; idx < (0x10000 * sizeof(short) >> PAGE_SHIFT); idx++) {
1278 u16 *dst = Add2Ptr(upcase, idx << PAGE_SHIFT);
1279 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1282 err = PTR_ERR(page);
1286 src = page_address(page);
1289 for (i = 0; i < PAGE_SIZE / sizeof(u16); i++)
1290 *dst++ = le16_to_cpu(*src++);
1292 memcpy(dst, src, PAGE_SIZE);
1294 ntfs_unmap_page(page);
1297 shared = ntfs_set_shared(upcase, 0x10000 * sizeof(short));
1298 if (shared && upcase != shared) {
1299 sbi->upcase = shared;
1306 if (is_ntfs3(sbi)) {
1308 err = ntfs_security_init(sbi);
1313 err = ntfs_extend_init(sbi);
1317 /* Load $Extend\$Reparse. */
1318 err = ntfs_reparse_init(sbi);
1322 /* Load $Extend\$ObjId. */
1323 err = ntfs_objid_init(sbi);
1330 ref.low = cpu_to_le32(MFT_REC_ROOT);
1331 ref.seq = cpu_to_le16(MFT_REC_ROOT);
1332 inode = ntfs_iget5(sb, &ref, &NAME_ROOT);
1333 if (IS_ERR(inode)) {
1334 err = PTR_ERR(inode);
1335 ntfs_err(sb, "Failed to load root.");
1342 sb->s_root = d_make_root(inode);
1361 sb->s_fs_info = NULL;
1365 void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len)
1367 struct ntfs_sb_info *sbi = sb->s_fs_info;
1368 struct block_device *bdev = sb->s_bdev;
1369 sector_t devblock = (u64)lcn * sbi->blocks_per_cluster;
1370 unsigned long blocks = (u64)len * sbi->blocks_per_cluster;
1371 unsigned long cnt = 0;
1372 unsigned long limit = global_zone_page_state(NR_FREE_PAGES)
1373 << (PAGE_SHIFT - sb->s_blocksize_bits);
1375 if (limit >= 0x2000)
1377 else if (limit < 32)
1383 clean_bdev_aliases(bdev, devblock++, 1);
1384 if (cnt++ >= limit) {
1385 sync_blockdev(bdev);
1392 * ntfs_discard - Issue a discard request (trim for SSD).
1394 int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len)
1397 u64 lbo, bytes, start, end;
1398 struct super_block *sb;
1400 if (sbi->used.next_free_lcn == lcn + len)
1401 sbi->used.next_free_lcn = lcn;
1403 if (sbi->flags & NTFS_FLAGS_NODISCARD)
1406 if (!sbi->options.discard)
1409 lbo = (u64)lcn << sbi->cluster_bits;
1410 bytes = (u64)len << sbi->cluster_bits;
1412 /* Align up 'start' on discard_granularity. */
1413 start = (lbo + sbi->discard_granularity - 1) &
1414 sbi->discard_granularity_mask_inv;
1415 /* Align down 'end' on discard_granularity. */
1416 end = (lbo + bytes) & sbi->discard_granularity_mask_inv;
1422 err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9,
1425 if (err == -EOPNOTSUPP)
1426 sbi->flags |= NTFS_FLAGS_NODISCARD;
1431 static struct dentry *ntfs_mount(struct file_system_type *fs_type, int flags,
1432 const char *dev_name, void *data)
1434 return mount_bdev(fs_type, flags, dev_name, data, ntfs_fill_super);
1438 static struct file_system_type ntfs_fs_type = {
1439 .owner = THIS_MODULE,
1441 .mount = ntfs_mount,
1442 .kill_sb = kill_block_super,
1443 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
1447 static int __init init_ntfs_fs(void)
1451 pr_info("ntfs3: Max link count %u\n", NTFS_LINK_MAX);
1453 if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL))
1454 pr_info("ntfs3: Enabled Linux POSIX ACLs support\n");
1455 if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER))
1456 pr_notice("ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n");
1457 if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS))
1458 pr_info("ntfs3: Read-only LZX/Xpress compression included\n");
1460 err = ntfs3_init_bitmap();
1464 ntfs_inode_cachep = kmem_cache_create(
1465 "ntfs_inode_cache", sizeof(struct ntfs_inode), 0,
1466 (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1468 if (!ntfs_inode_cachep) {
1473 err = register_filesystem(&ntfs_fs_type);
1479 kmem_cache_destroy(ntfs_inode_cachep);
1481 ntfs3_exit_bitmap();
1485 static void __exit exit_ntfs_fs(void)
1487 if (ntfs_inode_cachep) {
1489 kmem_cache_destroy(ntfs_inode_cachep);
1492 unregister_filesystem(&ntfs_fs_type);
1493 ntfs3_exit_bitmap();
1496 MODULE_LICENSE("GPL");
1497 MODULE_DESCRIPTION("ntfs3 read/write filesystem");
1498 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1499 MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support");
1501 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
1502 MODULE_INFO(cluster, "Warning: Activated 64 bits per cluster. Windows does not support this");
1504 #ifdef CONFIG_NTFS3_LZX_XPRESS
1505 MODULE_INFO(compression, "Read-only lzx/xpress compression included");
1508 MODULE_AUTHOR("Konstantin Komarov");
1509 MODULE_ALIAS_FS("ntfs3");
1511 module_init(init_ntfs_fs);
1512 module_exit(exit_ntfs_fs);