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 * Trace warnings/notices/errors
49 * Thanks Joe Perches <joe@perches.com> for implementation
51 void ntfs_printk(const struct super_block *sb, const char *fmt, ...)
56 struct ntfs_sb_info *sbi = sb->s_fs_info;
58 /*should we use different ratelimits for warnings/notices/errors? */
59 if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
64 level = printk_get_level(fmt);
65 vaf.fmt = printk_skip_level(fmt);
67 printk("%c%cntfs3: %s: %pV\n", KERN_SOH_ASCII, level, sb->s_id, &vaf);
72 static char s_name_buf[512];
73 static atomic_t s_name_buf_cnt = ATOMIC_INIT(1); // 1 means 'free s_name_buf'
75 /* print warnings/notices/errors about inode using name or inode number */
76 void ntfs_inode_printk(struct inode *inode, const char *fmt, ...)
78 struct super_block *sb = inode->i_sb;
79 struct ntfs_sb_info *sbi = sb->s_fs_info;
85 if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
88 /* use static allocated buffer, if possible */
89 name = atomic_dec_and_test(&s_name_buf_cnt)
91 : kmalloc(sizeof(s_name_buf), GFP_NOFS);
94 struct dentry *de = d_find_alias(inode);
95 const u32 name_len = ARRAY_SIZE(s_name_buf) - 1;
98 spin_lock(&de->d_lock);
99 snprintf(name, name_len, " \"%s\"", de->d_name.name);
100 spin_unlock(&de->d_lock);
101 name[name_len] = 0; /* to be sure*/
105 dput(de); /* cocci warns if placed in branch "if (de)" */
110 level = printk_get_level(fmt);
111 vaf.fmt = printk_skip_level(fmt);
114 printk("%c%cntfs3: %s: ino=%lx,%s %pV\n", KERN_SOH_ASCII, level,
115 sb->s_id, inode->i_ino, name ? name : "", &vaf);
119 atomic_inc(&s_name_buf_cnt);
120 if (name != s_name_buf)
126 * Shared memory struct.
128 * on-disk ntfs's upcase table is created by ntfs formatter
129 * 'upcase' table is 128K bytes of memory
130 * we should read it into memory when mounting
131 * Several ntfs volumes likely use the same 'upcase' table
132 * It is good idea to share in-memory 'upcase' table between different volumes
133 * Unfortunately winxp/vista/win7 use different upcase tables
135 static DEFINE_SPINLOCK(s_shared_lock);
146 * Returns 'ptr' if pointer was saved in shared memory
147 * Returns NULL if pointer was not shared
149 void *ntfs_set_shared(void *ptr, u32 bytes)
154 spin_lock(&s_shared_lock);
155 for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
156 if (!s_shared[i].cnt) {
158 } else if (bytes == s_shared[i].len &&
159 !memcmp(s_shared[i].ptr, ptr, bytes)) {
160 s_shared[i].cnt += 1;
161 ret = s_shared[i].ptr;
166 if (!ret && j != -1) {
167 s_shared[j].ptr = ptr;
168 s_shared[j].len = bytes;
172 spin_unlock(&s_shared_lock);
180 * Returns 'ptr' if pointer is not shared anymore
181 * Returns NULL if pointer is still shared
183 void *ntfs_put_shared(void *ptr)
188 spin_lock(&s_shared_lock);
189 for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
190 if (s_shared[i].cnt && s_shared[i].ptr == ptr) {
191 if (--s_shared[i].cnt)
196 spin_unlock(&s_shared_lock);
201 static inline void clear_mount_options(struct ntfs_mount_options *options)
203 unload_nls(options->nls);
226 static const match_table_t ntfs_tokens = {
227 { Opt_uid, "uid=%u" },
228 { Opt_gid, "gid=%u" },
229 { Opt_umask, "umask=%o" },
230 { Opt_dmask, "dmask=%o" },
231 { Opt_fmask, "fmask=%o" },
232 { Opt_immutable, "sys_immutable" },
233 { Opt_discard, "discard" },
234 { Opt_force, "force" },
235 { Opt_sparse, "sparse" },
236 { Opt_nohidden, "nohidden" },
238 { Opt_noatime, "noatime" },
239 { Opt_showmeta, "showmeta" },
240 { Opt_nls, "nls=%s" },
241 { Opt_prealloc, "prealloc" },
242 { Opt_no_acs_rules, "no_acs_rules" },
246 static noinline int ntfs_parse_options(struct super_block *sb, char *options,
248 struct ntfs_mount_options *opts)
251 substring_t args[MAX_OPT_ARGS];
254 struct nls_table *nls;
256 opts->fs_uid = current_uid();
257 opts->fs_gid = current_gid();
258 opts->fs_fmask_inv = opts->fs_dmask_inv = ~current_umask();
264 while ((p = strsep(&options, ","))) {
270 token = match_token(p, ntfs_tokens, args);
273 opts->sys_immutable = 1;
276 if (match_int(&args[0], &option))
278 opts->fs_uid = make_kuid(current_user_ns(), option);
279 if (!uid_valid(opts->fs_uid))
284 if (match_int(&args[0], &option))
286 opts->fs_gid = make_kgid(current_user_ns(), option);
287 if (!gid_valid(opts->fs_gid))
292 if (match_octal(&args[0], &option))
294 opts->fs_fmask_inv = opts->fs_dmask_inv = ~option;
295 opts->fmask = opts->dmask = 1;
298 if (match_octal(&args[0], &option))
300 opts->fs_dmask_inv = ~option;
304 if (match_octal(&args[0], &option))
306 opts->fs_fmask_inv = ~option;
322 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
323 sb->s_flags |= SB_POSIXACL;
326 ntfs_err(sb, "support for ACL not compiled in!");
330 sb->s_flags |= SB_NOATIME;
336 match_strlcpy(nls_name, &args[0], sizeof(nls_name));
341 case Opt_no_acs_rules:
342 opts->no_acs_rules = 1;
348 "Unrecognized mount option \"%s\" or missing value",
355 if (!strcmp(nls_name[0] ? nls_name : CONFIG_NLS_DEFAULT, "utf8")) {
356 /* For UTF-8 use utf16s_to_utf8s/utf8s_to_utf16s instead of nls */
358 } else if (nls_name[0]) {
359 nls = load_nls(nls_name);
361 ntfs_err(sb, "failed to load \"%s\"", nls_name);
365 nls = load_nls_default();
367 ntfs_err(sb, "failed to load default nls");
376 static int ntfs_remount(struct super_block *sb, int *flags, char *data)
379 struct ntfs_sb_info *sbi = sb->s_fs_info;
380 struct ntfs_mount_options old_opts;
381 char *orig_data = kstrdup(data, GFP_KERNEL);
383 if (data && !orig_data)
386 /* Store original options */
387 memcpy(&old_opts, &sbi->options, sizeof(old_opts));
388 clear_mount_options(&sbi->options);
389 memset(&sbi->options, 0, sizeof(sbi->options));
391 err = ntfs_parse_options(sb, data, 0, &sbi->options);
395 ro_rw = sb_rdonly(sb) && !(*flags & SB_RDONLY);
396 if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) {
399 "Couldn't remount rw because journal is not replayed. Please umount/remount instead\n");
406 if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) &&
407 !sbi->options.force) {
408 ntfs_warn(sb, "volume is dirty and \"force\" flag is not set!");
413 clear_mount_options(&old_opts);
415 *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME) |
416 SB_NODIRATIME | SB_NOATIME;
417 ntfs_info(sb, "re-mounted. Opts: %s", orig_data);
422 clear_mount_options(&sbi->options);
423 memcpy(&sbi->options, &old_opts, sizeof(old_opts));
430 static struct kmem_cache *ntfs_inode_cachep;
432 static struct inode *ntfs_alloc_inode(struct super_block *sb)
434 struct ntfs_inode *ni = kmem_cache_alloc(ntfs_inode_cachep, GFP_NOFS);
439 memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode));
441 mutex_init(&ni->ni_lock);
443 return &ni->vfs_inode;
446 static void ntfs_i_callback(struct rcu_head *head)
448 struct inode *inode = container_of(head, struct inode, i_rcu);
449 struct ntfs_inode *ni = ntfs_i(inode);
451 mutex_destroy(&ni->ni_lock);
453 kmem_cache_free(ntfs_inode_cachep, ni);
456 static void ntfs_destroy_inode(struct inode *inode)
458 call_rcu(&inode->i_rcu, ntfs_i_callback);
461 static void init_once(void *foo)
463 struct ntfs_inode *ni = foo;
465 inode_init_once(&ni->vfs_inode);
468 /* noinline to reduce binary size*/
469 static noinline void put_ntfs(struct ntfs_sb_info *sbi)
472 kvfree(ntfs_put_shared(sbi->upcase));
473 kfree(sbi->def_table);
475 wnd_close(&sbi->mft.bitmap);
476 wnd_close(&sbi->used.bitmap);
479 iput(&sbi->mft.ni->vfs_inode);
481 if (sbi->security.ni)
482 iput(&sbi->security.ni->vfs_inode);
485 iput(&sbi->reparse.ni->vfs_inode);
488 iput(&sbi->objid.ni->vfs_inode);
491 iput(&sbi->volume.ni->vfs_inode);
493 ntfs_update_mftmirr(sbi, 0);
495 indx_clear(&sbi->security.index_sii);
496 indx_clear(&sbi->security.index_sdh);
497 indx_clear(&sbi->reparse.index_r);
498 indx_clear(&sbi->objid.index_o);
499 kfree(sbi->compress.lznt);
500 #ifdef CONFIG_NTFS3_LZX_XPRESS
501 xpress_free_decompressor(sbi->compress.xpress);
502 lzx_free_decompressor(sbi->compress.lzx);
504 clear_mount_options(&sbi->options);
509 static void ntfs_put_super(struct super_block *sb)
511 struct ntfs_sb_info *sbi = sb->s_fs_info;
513 /*mark rw ntfs as clear, if possible*/
514 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
518 sync_blockdev(sb->s_bdev);
521 static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf)
523 struct super_block *sb = dentry->d_sb;
524 struct ntfs_sb_info *sbi = sb->s_fs_info;
525 struct wnd_bitmap *wnd = &sbi->used.bitmap;
527 buf->f_type = sb->s_magic;
528 buf->f_bsize = sbi->cluster_size;
529 buf->f_blocks = wnd->nbits;
531 buf->f_bfree = buf->f_bavail = wnd_zeroes(wnd);
532 buf->f_fsid.val[0] = sbi->volume.ser_num;
533 buf->f_fsid.val[1] = (sbi->volume.ser_num >> 32);
534 buf->f_namelen = NTFS_NAME_LEN;
539 static int ntfs_show_options(struct seq_file *m, struct dentry *root)
541 struct super_block *sb = root->d_sb;
542 struct ntfs_sb_info *sbi = sb->s_fs_info;
543 struct ntfs_mount_options *opts = &sbi->options;
544 struct user_namespace *user_ns = seq_user_ns(m);
547 seq_printf(m, ",uid=%u",
548 from_kuid_munged(user_ns, opts->fs_uid));
550 seq_printf(m, ",gid=%u",
551 from_kgid_munged(user_ns, opts->fs_gid));
553 seq_printf(m, ",fmask=%04o", ~opts->fs_fmask_inv);
555 seq_printf(m, ",dmask=%04o", ~opts->fs_dmask_inv);
557 seq_printf(m, ",nls=%s", opts->nls->charset);
559 seq_puts(m, ",nls=utf8");
560 if (opts->sys_immutable)
561 seq_puts(m, ",sys_immutable");
563 seq_puts(m, ",discard");
565 seq_puts(m, ",sparse");
567 seq_puts(m, ",showmeta");
569 seq_puts(m, ",nohidden");
571 seq_puts(m, ",force");
572 if (opts->no_acs_rules)
573 seq_puts(m, ",no_acs_rules");
575 seq_puts(m, ",prealloc");
576 if (sb->s_flags & SB_POSIXACL)
578 if (sb->s_flags & SB_NOATIME)
579 seq_puts(m, ",noatime");
584 /*super_operations::sync_fs*/
585 static int ntfs_sync_fs(struct super_block *sb, int wait)
588 struct ntfs_sb_info *sbi = sb->s_fs_info;
589 struct ntfs_inode *ni;
592 ni = sbi->security.ni;
594 inode = &ni->vfs_inode;
595 err2 = _ni_write_inode(inode, wait);
602 inode = &ni->vfs_inode;
603 err2 = _ni_write_inode(inode, wait);
608 ni = sbi->reparse.ni;
610 inode = &ni->vfs_inode;
611 err2 = _ni_write_inode(inode, wait);
617 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
619 ntfs_update_mftmirr(sbi, wait);
624 static const struct super_operations ntfs_sops = {
625 .alloc_inode = ntfs_alloc_inode,
626 .destroy_inode = ntfs_destroy_inode,
627 .evict_inode = ntfs_evict_inode,
628 .put_super = ntfs_put_super,
629 .statfs = ntfs_statfs,
630 .show_options = ntfs_show_options,
631 .sync_fs = ntfs_sync_fs,
632 .remount_fs = ntfs_remount,
633 .write_inode = ntfs3_write_inode,
636 static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino,
642 ref.low = cpu_to_le32(ino);
643 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
644 ref.high = cpu_to_le16(ino >> 32);
648 ref.seq = cpu_to_le16(generation);
650 inode = ntfs_iget5(sb, &ref, NULL);
651 if (!IS_ERR(inode) && is_bad_inode(inode)) {
653 inode = ERR_PTR(-ESTALE);
659 static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
660 int fh_len, int fh_type)
662 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
663 ntfs_export_get_inode);
666 static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
667 int fh_len, int fh_type)
669 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
670 ntfs_export_get_inode);
673 /* TODO: == ntfs_sync_inode */
674 static int ntfs_nfs_commit_metadata(struct inode *inode)
676 return _ni_write_inode(inode, 1);
679 static const struct export_operations ntfs_export_ops = {
680 .fh_to_dentry = ntfs_fh_to_dentry,
681 .fh_to_parent = ntfs_fh_to_parent,
682 .get_parent = ntfs3_get_parent,
683 .commit_metadata = ntfs_nfs_commit_metadata,
686 /* Returns Gb,Mb to print with "%u.%02u Gb" */
687 static u32 format_size_gb(const u64 bytes, u32 *mb)
689 /* Do simple right 30 bit shift of 64 bit value */
690 u64 kbytes = bytes >> 10;
691 u32 kbytes32 = kbytes;
693 *mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20;
697 return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12);
700 static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot)
702 return boot->sectors_per_clusters <= 0x80
703 ? boot->sectors_per_clusters
704 : (1u << (0 - boot->sectors_per_clusters));
707 /* inits internal info from on-disk boot sector*/
708 static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
711 struct ntfs_sb_info *sbi = sb->s_fs_info;
713 u32 mb, gb, boot_sector_size, sct_per_clst, record_size;
714 u64 sectors, clusters, fs_size, mlcn, mlcn2;
715 struct NTFS_BOOT *boot;
716 struct buffer_head *bh;
720 sbi->volume.blocks = dev_size >> PAGE_SHIFT;
722 bh = ntfs_bread(sb, 0);
727 boot = (struct NTFS_BOOT *)bh->b_data;
729 if (memcmp(boot->system_id, "NTFS ", sizeof("NTFS ") - 1))
732 /* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/
733 /*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1])
737 boot_sector_size = (u32)boot->bytes_per_sector[1] << 8;
738 if (boot->bytes_per_sector[0] || boot_sector_size < SECTOR_SIZE ||
739 !is_power_of_2(boot_sector_size)) {
743 /* cluster size: 512, 1K, 2K, 4K, ... 2M */
744 sct_per_clst = true_sectors_per_clst(boot);
745 if (!is_power_of_2(sct_per_clst))
748 mlcn = le64_to_cpu(boot->mft_clst);
749 mlcn2 = le64_to_cpu(boot->mft2_clst);
750 sectors = le64_to_cpu(boot->sectors_per_volume);
752 if (mlcn * sct_per_clst >= sectors)
755 if (mlcn2 * sct_per_clst >= sectors)
758 /* Check MFT record size */
759 if ((boot->record_size < 0 &&
760 SECTOR_SIZE > (2U << (-boot->record_size))) ||
761 (boot->record_size >= 0 && !is_power_of_2(boot->record_size))) {
765 /* Check index record size */
766 if ((boot->index_size < 0 &&
767 SECTOR_SIZE > (2U << (-boot->index_size))) ||
768 (boot->index_size >= 0 && !is_power_of_2(boot->index_size))) {
772 sbi->sector_size = boot_sector_size;
773 sbi->sector_bits = blksize_bits(boot_sector_size);
774 fs_size = (sectors + 1) << sbi->sector_bits;
776 gb = format_size_gb(fs_size, &mb);
779 * - Volume formatted and mounted with the same sector size
780 * - Volume formatted 4K and mounted as 512
781 * - Volume formatted 512 and mounted as 4K
783 if (sbi->sector_size != sector_size) {
785 "Different NTFS' sector size and media sector size");
786 dev_size += sector_size - 1;
789 sbi->cluster_size = boot_sector_size * sct_per_clst;
790 sbi->cluster_bits = blksize_bits(sbi->cluster_size);
792 sbi->mft.lbo = mlcn << sbi->cluster_bits;
793 sbi->mft.lbo2 = mlcn2 << sbi->cluster_bits;
795 if (sbi->cluster_size < sbi->sector_size)
798 sbi->cluster_mask = sbi->cluster_size - 1;
799 sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask;
800 sbi->record_size = record_size = boot->record_size < 0
801 ? 1 << (-boot->record_size)
802 : (u32)boot->record_size
803 << sbi->cluster_bits;
805 if (record_size > MAXIMUM_BYTES_PER_MFT)
808 sbi->record_bits = blksize_bits(record_size);
809 sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes
811 sbi->max_bytes_per_attr =
812 record_size - ALIGN(MFTRECORD_FIXUP_OFFSET_1, 8) -
813 ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) -
814 ALIGN(sizeof(enum ATTR_TYPE), 8);
816 sbi->index_size = boot->index_size < 0
817 ? 1u << (-boot->index_size)
818 : (u32)boot->index_size << sbi->cluster_bits;
820 sbi->volume.ser_num = le64_to_cpu(boot->serial_num);
821 sbi->volume.size = sectors << sbi->sector_bits;
823 /* warning if RAW volume */
824 if (dev_size < fs_size) {
827 gb0 = format_size_gb(dev_size, &mb0);
830 "RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only",
832 sb->s_flags |= SB_RDONLY;
835 clusters = sbi->volume.size >> sbi->cluster_bits;
836 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
837 /* 32 bits per cluster */
838 if (clusters >> 32) {
841 "NTFS %u.%02u Gb is too big to use 32 bits per cluster",
845 #elif BITS_PER_LONG < 64
846 #error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS"
849 sbi->used.bitmap.nbits = clusters;
851 rec = kzalloc(record_size, GFP_NOFS);
858 rec->rhdr.sign = NTFS_FILE_SIGNATURE;
859 rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET_1);
860 fn = (sbi->record_size >> SECTOR_SHIFT) + 1;
861 rec->rhdr.fix_num = cpu_to_le16(fn);
862 ao = ALIGN(MFTRECORD_FIXUP_OFFSET_1 + sizeof(short) * fn, 8);
863 rec->attr_off = cpu_to_le16(ao);
864 rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8));
865 rec->total = cpu_to_le32(sbi->record_size);
866 ((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END;
868 if (sbi->cluster_size < PAGE_SIZE)
869 sb_set_blocksize(sb, sbi->cluster_size);
871 sbi->block_mask = sb->s_blocksize - 1;
872 sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits;
873 sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits;
875 /* Maximum size for normal files */
876 sbi->maxbytes = (clusters << sbi->cluster_bits) - 1;
878 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
879 if (clusters >= (1ull << (64 - sbi->cluster_bits)))
881 sbi->maxbytes_sparse = -1;
883 /* Maximum size for sparse file */
884 sbi->maxbytes_sparse = (1ull << (sbi->cluster_bits + 32)) - 1;
896 static int ntfs_fill_super(struct super_block *sb, void *data, int silent)
899 struct ntfs_sb_info *sbi;
900 struct block_device *bdev = sb->s_bdev;
901 struct inode *bd_inode = bdev->bd_inode;
902 struct request_queue *rq = bdev_get_queue(bdev);
903 struct inode *inode = NULL;
904 struct ntfs_inode *ni;
908 const struct VOLUME_INFO *info;
909 u32 idx, done, bytes;
910 struct ATTR_DEF_ENTRY *t;
918 sbi = kzalloc(sizeof(struct ntfs_sb_info), GFP_NOFS);
924 sb->s_flags |= SB_NODIRATIME;
925 sb->s_magic = 0x7366746e; // "ntfs"
926 sb->s_op = &ntfs_sops;
927 sb->s_export_op = &ntfs_export_ops;
928 sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec
929 sb->s_xattr = ntfs_xattr_handlers;
931 ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL,
932 DEFAULT_RATELIMIT_BURST);
934 err = ntfs_parse_options(sb, data, silent, &sbi->options);
938 if (!rq || !blk_queue_discard(rq) || !rq->limits.discard_granularity) {
941 sbi->discard_granularity = rq->limits.discard_granularity;
942 sbi->discard_granularity_mask_inv =
943 ~(u64)(sbi->discard_granularity - 1);
946 sb_set_blocksize(sb, PAGE_SIZE);
949 err = ntfs_init_from_boot(sb, rq ? queue_logical_block_size(rq) : 512,
954 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
955 sb->s_maxbytes = MAX_LFS_FILESIZE;
957 sb->s_maxbytes = 0xFFFFFFFFull << sbi->cluster_bits;
960 mutex_init(&sbi->compress.mtx_lznt);
961 #ifdef CONFIG_NTFS3_LZX_XPRESS
962 mutex_init(&sbi->compress.mtx_xpress);
963 mutex_init(&sbi->compress.mtx_lzx);
967 * Load $Volume. This should be done before LogFile
968 * 'cause 'sbi->volume.ni' is used 'ntfs_set_state'
970 ref.low = cpu_to_le32(MFT_REC_VOL);
971 ref.seq = cpu_to_le16(MFT_REC_VOL);
972 inode = ntfs_iget5(sb, &ref, &NAME_VOLUME);
974 err = PTR_ERR(inode);
975 ntfs_err(sb, "Failed to load $Volume.");
982 /* Load and save label (not necessary) */
983 attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL);
986 /* It is ok if no ATTR_LABEL */
987 } else if (!attr->non_res && !is_attr_ext(attr)) {
988 /* $AttrDef allows labels to be up to 128 symbols */
989 err = utf16s_to_utf8s(resident_data(attr),
990 le32_to_cpu(attr->res.data_size) >> 1,
991 UTF16_LITTLE_ENDIAN, sbi->volume.label,
992 sizeof(sbi->volume.label));
994 sbi->volume.label[0] = 0;
996 /* should we break mounting here? */
1001 attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL);
1002 if (!attr || is_attr_ext(attr)) {
1007 info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO);
1013 sbi->volume.major_ver = info->major_ver;
1014 sbi->volume.minor_ver = info->minor_ver;
1015 sbi->volume.flags = info->flags;
1017 sbi->volume.ni = ni;
1020 /* Load $MFTMirr to estimate recs_mirr */
1021 ref.low = cpu_to_le32(MFT_REC_MIRR);
1022 ref.seq = cpu_to_le16(MFT_REC_MIRR);
1023 inode = ntfs_iget5(sb, &ref, &NAME_MIRROR);
1024 if (IS_ERR(inode)) {
1025 err = PTR_ERR(inode);
1026 ntfs_err(sb, "Failed to load $MFTMirr.");
1031 sbi->mft.recs_mirr =
1032 ntfs_up_cluster(sbi, inode->i_size) >> sbi->record_bits;
1036 /* Load LogFile to replay */
1037 ref.low = cpu_to_le32(MFT_REC_LOG);
1038 ref.seq = cpu_to_le16(MFT_REC_LOG);
1039 inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE);
1040 if (IS_ERR(inode)) {
1041 err = PTR_ERR(inode);
1042 ntfs_err(sb, "Failed to load \x24LogFile.");
1049 err = ntfs_loadlog_and_replay(ni, sbi);
1056 is_ro = sb_rdonly(sbi->sb);
1058 if (sbi->flags & NTFS_FLAGS_NEED_REPLAY) {
1061 "failed to replay log file. Can't mount rw!");
1065 } else if (sbi->volume.flags & VOLUME_FLAG_DIRTY) {
1066 if (!is_ro && !sbi->options.force) {
1069 "volume is dirty and \"force\" flag is not set!");
1076 ref.low = cpu_to_le32(MFT_REC_MFT);
1077 ref.seq = cpu_to_le16(1);
1079 inode = ntfs_iget5(sb, &ref, &NAME_MFT);
1080 if (IS_ERR(inode)) {
1081 err = PTR_ERR(inode);
1082 ntfs_err(sb, "Failed to load $MFT.");
1089 sbi->mft.used = ni->i_valid >> sbi->record_bits;
1090 tt = inode->i_size >> sbi->record_bits;
1091 sbi->mft.next_free = MFT_REC_USER;
1093 err = wnd_init(&sbi->mft.bitmap, sb, tt);
1097 err = ni_load_all_mi(ni);
1104 ref.low = cpu_to_le32(MFT_REC_BADCLUST);
1105 ref.seq = cpu_to_le16(MFT_REC_BADCLUST);
1106 inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS);
1107 if (IS_ERR(inode)) {
1108 err = PTR_ERR(inode);
1109 ntfs_err(sb, "Failed to load $BadClus.");
1116 for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) {
1117 if (lcn == SPARSE_LCN)
1120 if (!sbi->bad_clusters)
1121 ntfs_notice(sb, "Volume contains bad blocks");
1123 sbi->bad_clusters += len;
1129 ref.low = cpu_to_le32(MFT_REC_BITMAP);
1130 ref.seq = cpu_to_le16(MFT_REC_BITMAP);
1131 inode = ntfs_iget5(sb, &ref, &NAME_BITMAP);
1132 if (IS_ERR(inode)) {
1133 err = PTR_ERR(inode);
1134 ntfs_err(sb, "Failed to load $Bitmap.");
1141 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
1142 if (inode->i_size >> 32) {
1148 /* Check bitmap boundary */
1149 tt = sbi->used.bitmap.nbits;
1150 if (inode->i_size < bitmap_size(tt)) {
1156 sbi->used.bitmap.set_tail = true;
1157 err = wnd_init(&sbi->used.bitmap, sbi->sb, tt);
1163 /* Compute the mft zone */
1164 err = ntfs_refresh_zone(sbi);
1169 ref.low = cpu_to_le32(MFT_REC_ATTR);
1170 ref.seq = cpu_to_le16(MFT_REC_ATTR);
1171 inode = ntfs_iget5(sbi->sb, &ref, &NAME_ATTRDEF);
1172 if (IS_ERR(inode)) {
1173 err = PTR_ERR(inode);
1174 ntfs_err(sb, "Failed to load $AttrDef -> %d", err);
1179 if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY)) {
1183 bytes = inode->i_size;
1184 sbi->def_table = t = kmalloc(bytes, GFP_NOFS);
1190 for (done = idx = 0; done < bytes; done += PAGE_SIZE, idx++) {
1191 unsigned long tail = bytes - done;
1192 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1195 err = PTR_ERR(page);
1198 memcpy(Add2Ptr(t, done), page_address(page),
1199 min(PAGE_SIZE, tail));
1200 ntfs_unmap_page(page);
1202 if (!idx && ATTR_STD != t->type) {
1209 sbi->def_entries = 1;
1210 done = sizeof(struct ATTR_DEF_ENTRY);
1211 sbi->reparse.max_size = MAXIMUM_REPARSE_DATA_BUFFER_SIZE;
1212 sbi->ea_max_size = 0x10000; /* default formatter value */
1214 while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) {
1215 u32 t32 = le32_to_cpu(t->type);
1216 u64 sz = le64_to_cpu(t->max_sz);
1218 if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32)
1221 if (t->type == ATTR_REPARSE)
1222 sbi->reparse.max_size = sz;
1223 else if (t->type == ATTR_EA)
1224 sbi->ea_max_size = sz;
1226 done += sizeof(struct ATTR_DEF_ENTRY);
1228 sbi->def_entries += 1;
1233 ref.low = cpu_to_le32(MFT_REC_UPCASE);
1234 ref.seq = cpu_to_le16(MFT_REC_UPCASE);
1235 inode = ntfs_iget5(sb, &ref, &NAME_UPCASE);
1236 if (IS_ERR(inode)) {
1237 err = PTR_ERR(inode);
1238 ntfs_err(sb, "Failed to load \x24LogFile.");
1245 if (inode->i_size != 0x10000 * sizeof(short)) {
1250 sbi->upcase = upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL);
1256 for (idx = 0; idx < (0x10000 * sizeof(short) >> PAGE_SHIFT); idx++) {
1258 u16 *dst = Add2Ptr(upcase, idx << PAGE_SHIFT);
1259 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1262 err = PTR_ERR(page);
1266 src = page_address(page);
1269 for (i = 0; i < PAGE_SIZE / sizeof(u16); i++)
1270 *dst++ = le16_to_cpu(*src++);
1272 memcpy(dst, src, PAGE_SIZE);
1274 ntfs_unmap_page(page);
1277 shared = ntfs_set_shared(upcase, 0x10000 * sizeof(short));
1278 if (shared && upcase != shared) {
1279 sbi->upcase = shared;
1286 if (is_ntfs3(sbi)) {
1288 err = ntfs_security_init(sbi);
1293 err = ntfs_extend_init(sbi);
1297 /* Load $Extend\$Reparse */
1298 err = ntfs_reparse_init(sbi);
1302 /* Load $Extend\$ObjId */
1303 err = ntfs_objid_init(sbi);
1310 ref.low = cpu_to_le32(MFT_REC_ROOT);
1311 ref.seq = cpu_to_le16(MFT_REC_ROOT);
1312 inode = ntfs_iget5(sb, &ref, &NAME_ROOT);
1313 if (IS_ERR(inode)) {
1314 err = PTR_ERR(inode);
1315 ntfs_err(sb, "Failed to load root.");
1322 sb->s_root = d_make_root(inode);
1341 sb->s_fs_info = NULL;
1345 void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len)
1347 struct ntfs_sb_info *sbi = sb->s_fs_info;
1348 struct block_device *bdev = sb->s_bdev;
1349 sector_t devblock = (u64)lcn * sbi->blocks_per_cluster;
1350 unsigned long blocks = (u64)len * sbi->blocks_per_cluster;
1351 unsigned long cnt = 0;
1352 unsigned long limit = global_zone_page_state(NR_FREE_PAGES)
1353 << (PAGE_SHIFT - sb->s_blocksize_bits);
1355 if (limit >= 0x2000)
1357 else if (limit < 32)
1363 clean_bdev_aliases(bdev, devblock++, 1);
1364 if (cnt++ >= limit) {
1365 sync_blockdev(bdev);
1374 * issue a discard request (trim for SSD)
1376 int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len)
1379 u64 lbo, bytes, start, end;
1380 struct super_block *sb;
1382 if (sbi->used.next_free_lcn == lcn + len)
1383 sbi->used.next_free_lcn = lcn;
1385 if (sbi->flags & NTFS_FLAGS_NODISCARD)
1388 if (!sbi->options.discard)
1391 lbo = (u64)lcn << sbi->cluster_bits;
1392 bytes = (u64)len << sbi->cluster_bits;
1394 /* Align up 'start' on discard_granularity */
1395 start = (lbo + sbi->discard_granularity - 1) &
1396 sbi->discard_granularity_mask_inv;
1397 /* Align down 'end' on discard_granularity */
1398 end = (lbo + bytes) & sbi->discard_granularity_mask_inv;
1404 err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9,
1407 if (err == -EOPNOTSUPP)
1408 sbi->flags |= NTFS_FLAGS_NODISCARD;
1413 static struct dentry *ntfs_mount(struct file_system_type *fs_type, int flags,
1414 const char *dev_name, void *data)
1416 return mount_bdev(fs_type, flags, dev_name, data, ntfs_fill_super);
1420 static struct file_system_type ntfs_fs_type = {
1421 .owner = THIS_MODULE,
1423 .mount = ntfs_mount,
1424 .kill_sb = kill_block_super,
1425 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
1429 static int __init init_ntfs_fs(void)
1433 pr_notice("ntfs3: Index binary search\n");
1434 pr_notice("ntfs3: Hot fix free clusters\n");
1435 pr_notice("ntfs3: Max link count %u\n", NTFS_LINK_MAX);
1437 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1438 pr_notice("ntfs3: Enabled Linux POSIX ACLs support\n");
1440 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
1441 pr_notice("ntfs3: Activated 64 bits per cluster\n");
1443 pr_notice("ntfs3: Activated 32 bits per cluster\n");
1445 #ifdef CONFIG_NTFS3_LZX_XPRESS
1446 pr_notice("ntfs3: Read-only lzx/xpress compression included\n");
1449 err = ntfs3_init_bitmap();
1453 ntfs_inode_cachep = kmem_cache_create(
1454 "ntfs_inode_cache", sizeof(struct ntfs_inode), 0,
1455 (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1457 if (!ntfs_inode_cachep) {
1462 err = register_filesystem(&ntfs_fs_type);
1468 kmem_cache_destroy(ntfs_inode_cachep);
1470 ntfs3_exit_bitmap();
1474 static void __exit exit_ntfs_fs(void)
1476 if (ntfs_inode_cachep) {
1478 kmem_cache_destroy(ntfs_inode_cachep);
1481 unregister_filesystem(&ntfs_fs_type);
1482 ntfs3_exit_bitmap();
1485 MODULE_LICENSE("GPL");
1486 MODULE_DESCRIPTION("ntfs3 read/write filesystem");
1487 MODULE_INFO(behaviour, "Index binary search");
1488 MODULE_INFO(behaviour, "Hot fix free clusters");
1489 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1490 MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support");
1492 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
1493 MODULE_INFO(cluster, "Activated 64 bits per cluster");
1495 MODULE_INFO(cluster, "Activated 32 bits per cluster");
1497 #ifdef CONFIG_NTFS3_LZX_XPRESS
1498 MODULE_INFO(compression, "Read-only lzx/xpress compression included");
1501 MODULE_AUTHOR("Konstantin Komarov");
1502 MODULE_ALIAS_FS("ntfs3");
1504 module_init(init_ntfs_fs);
1505 module_exit(exit_ntfs_fs);