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
24 * ntfs allows up to 2^64 clusters per volume.
25 * It means you should use 64 bits lcn to operate with ntfs.
26 * Implementation of ntfs.sys uses only 32 bits lcn.
27 * Default ntfs3 uses 32 bits lcn too.
28 * ntfs3 built with CONFIG_NTFS3_64BIT_CLUSTER (ntfs3_64) uses 64 bits per lcn.
31 * ntfs limits, cluster size is 4K (2^12)
32 * -----------------------------------------------------------------------------
33 * | Volume size | Clusters | ntfs.sys | ntfs3 | ntfs3_64 | mkntfs | chkdsk |
34 * -----------------------------------------------------------------------------
35 * | < 16T, 2^44 | < 2^32 | yes | yes | yes | yes | yes |
36 * | > 16T, 2^44 | > 2^32 | no | no | yes | yes | yes |
37 * ----------------------------------------------------------|------------------
39 * To mount large volumes as ntfs one should use large cluster size (up to 2M)
40 * The maximum volume size in this case is 2^32 * 2^21 = 2^53 = 8P
42 * ntfs limits, cluster size is 2M (2^21)
43 * -----------------------------------------------------------------------------
44 * | < 8P, 2^53 | < 2^32 | yes | yes | yes | yes | yes |
45 * | > 8P, 2^53 | > 2^32 | no | no | yes | yes | yes |
46 * ----------------------------------------------------------|------------------
50 #include <linux/blkdev.h>
51 #include <linux/buffer_head.h>
52 #include <linux/exportfs.h>
54 #include <linux/fs_context.h>
55 #include <linux/fs_parser.h>
56 #include <linux/log2.h>
57 #include <linux/minmax.h>
58 #include <linux/module.h>
59 #include <linux/nls.h>
60 #include <linux/seq_file.h>
61 #include <linux/statfs.h>
66 #ifdef CONFIG_NTFS3_LZX_XPRESS
72 * ntfs_printk - Trace warnings/notices/errors.
74 * Thanks Joe Perches <joe@perches.com> for implementation
76 void ntfs_printk(const struct super_block *sb, const char *fmt, ...)
81 struct ntfs_sb_info *sbi = sb->s_fs_info;
83 /* Should we use different ratelimits for warnings/notices/errors? */
84 if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
89 level = printk_get_level(fmt);
90 vaf.fmt = printk_skip_level(fmt);
92 printk("%c%cntfs3: %s: %pV\n", KERN_SOH_ASCII, level, sb->s_id, &vaf);
97 static char s_name_buf[512];
98 static atomic_t s_name_buf_cnt = ATOMIC_INIT(1); // 1 means 'free s_name_buf'.
103 * Print warnings/notices/errors about inode using name or inode number.
105 void ntfs_inode_printk(struct inode *inode, const char *fmt, ...)
107 struct super_block *sb = inode->i_sb;
108 struct ntfs_sb_info *sbi = sb->s_fs_info;
111 struct va_format vaf;
114 if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
117 /* Use static allocated buffer, if possible. */
118 name = atomic_dec_and_test(&s_name_buf_cnt) ?
120 kmalloc(sizeof(s_name_buf), GFP_NOFS);
123 struct dentry *de = d_find_alias(inode);
124 const u32 name_len = ARRAY_SIZE(s_name_buf) - 1;
127 spin_lock(&de->d_lock);
128 snprintf(name, name_len, " \"%s\"", de->d_name.name);
129 spin_unlock(&de->d_lock);
130 name[name_len] = 0; /* To be sure. */
134 dput(de); /* Cocci warns if placed in branch "if (de)" */
139 level = printk_get_level(fmt);
140 vaf.fmt = printk_skip_level(fmt);
143 printk("%c%cntfs3: %s: ino=%lx,%s %pV\n", KERN_SOH_ASCII, level,
144 sb->s_id, inode->i_ino, name ? name : "", &vaf);
148 atomic_inc(&s_name_buf_cnt);
149 if (name != s_name_buf)
155 * Shared memory struct.
157 * On-disk ntfs's upcase table is created by ntfs formatter.
158 * 'upcase' table is 128K bytes of memory.
159 * We should read it into memory when mounting.
160 * Several ntfs volumes likely use the same 'upcase' table.
161 * It is good idea to share in-memory 'upcase' table between different volumes.
162 * Unfortunately winxp/vista/win7 use different upcase tables.
164 static DEFINE_SPINLOCK(s_shared_lock);
176 * * @ptr - If pointer was saved in shared memory.
177 * * NULL - If pointer was not shared.
179 void *ntfs_set_shared(void *ptr, u32 bytes)
184 spin_lock(&s_shared_lock);
185 for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
186 if (!s_shared[i].cnt) {
188 } else if (bytes == s_shared[i].len &&
189 !memcmp(s_shared[i].ptr, ptr, bytes)) {
190 s_shared[i].cnt += 1;
191 ret = s_shared[i].ptr;
196 if (!ret && j != -1) {
197 s_shared[j].ptr = ptr;
198 s_shared[j].len = bytes;
202 spin_unlock(&s_shared_lock);
211 * * @ptr - If pointer is not shared anymore.
212 * * NULL - If pointer is still shared.
214 void *ntfs_put_shared(void *ptr)
219 spin_lock(&s_shared_lock);
220 for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
221 if (s_shared[i].cnt && s_shared[i].ptr == ptr) {
222 if (--s_shared[i].cnt)
227 spin_unlock(&s_shared_lock);
232 static inline void put_mount_options(struct ntfs_mount_options *options)
234 kfree(options->nls_name);
235 unload_nls(options->nls);
260 static const struct fs_parameter_spec ntfs_fs_parameters[] = {
261 fsparam_u32("uid", Opt_uid),
262 fsparam_u32("gid", Opt_gid),
263 fsparam_u32oct("umask", Opt_umask),
264 fsparam_u32oct("dmask", Opt_dmask),
265 fsparam_u32oct("fmask", Opt_fmask),
266 fsparam_flag_no("sys_immutable", Opt_immutable),
267 fsparam_flag_no("discard", Opt_discard),
268 fsparam_flag_no("force", Opt_force),
269 fsparam_flag_no("sparse", Opt_sparse),
270 fsparam_flag_no("hidden", Opt_nohidden),
271 fsparam_flag_no("hide_dot_files", Opt_hide_dot_files),
272 fsparam_flag_no("windows_names", Opt_windows_names),
273 fsparam_flag_no("showmeta", Opt_showmeta),
274 fsparam_flag_no("acl", Opt_acl),
275 fsparam_string("iocharset", Opt_iocharset),
276 fsparam_flag_no("prealloc", Opt_prealloc),
277 fsparam_flag_no("nocase", Opt_nocase),
282 * Load nls table or if @nls is utf8 then return NULL.
284 static struct nls_table *ntfs_load_nls(char *nls)
286 struct nls_table *ret;
289 nls = CONFIG_NLS_DEFAULT;
291 if (strcmp(nls, "utf8") == 0)
294 if (strcmp(nls, CONFIG_NLS_DEFAULT) == 0)
295 return load_nls_default();
301 return ERR_PTR(-EINVAL);
304 static int ntfs_fs_parse_param(struct fs_context *fc,
305 struct fs_parameter *param)
307 struct ntfs_mount_options *opts = fc->fs_private;
308 struct fs_parse_result result;
311 opt = fs_parse(fc, ntfs_fs_parameters, param, &result);
317 opts->fs_uid = make_kuid(current_user_ns(), result.uint_32);
318 if (!uid_valid(opts->fs_uid))
319 return invalf(fc, "ntfs3: Invalid value for uid.");
322 opts->fs_gid = make_kgid(current_user_ns(), result.uint_32);
323 if (!gid_valid(opts->fs_gid))
324 return invalf(fc, "ntfs3: Invalid value for gid.");
327 if (result.uint_32 & ~07777)
328 return invalf(fc, "ntfs3: Invalid value for umask.");
329 opts->fs_fmask_inv = ~result.uint_32;
330 opts->fs_dmask_inv = ~result.uint_32;
335 if (result.uint_32 & ~07777)
336 return invalf(fc, "ntfs3: Invalid value for dmask.");
337 opts->fs_dmask_inv = ~result.uint_32;
341 if (result.uint_32 & ~07777)
342 return invalf(fc, "ntfs3: Invalid value for fmask.");
343 opts->fs_fmask_inv = ~result.uint_32;
347 opts->sys_immutable = result.negated ? 0 : 1;
350 opts->discard = result.negated ? 0 : 1;
353 opts->force = result.negated ? 0 : 1;
356 opts->sparse = result.negated ? 0 : 1;
359 opts->nohidden = result.negated ? 1 : 0;
361 case Opt_hide_dot_files:
362 opts->hide_dot_files = result.negated ? 0 : 1;
364 case Opt_windows_names:
365 opts->windows_names = result.negated ? 0 : 1;
368 opts->showmeta = result.negated ? 0 : 1;
372 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
373 fc->sb_flags |= SB_POSIXACL;
376 fc, "ntfs3: Support for ACL not compiled in!");
379 fc->sb_flags &= ~SB_POSIXACL;
382 kfree(opts->nls_name);
383 opts->nls_name = param->string;
384 param->string = NULL;
387 opts->prealloc = result.negated ? 0 : 1;
390 opts->nocase = result.negated ? 1 : 0;
393 /* Should not be here unless we forget add case. */
399 static int ntfs_fs_reconfigure(struct fs_context *fc)
401 struct super_block *sb = fc->root->d_sb;
402 struct ntfs_sb_info *sbi = sb->s_fs_info;
403 struct ntfs_mount_options *new_opts = fc->fs_private;
406 ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY);
407 if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) {
409 "ntfs3: Couldn't remount rw because journal is not replayed. Please umount/remount instead\n");
413 new_opts->nls = ntfs_load_nls(new_opts->nls_name);
414 if (IS_ERR(new_opts->nls)) {
415 new_opts->nls = NULL;
416 errorf(fc, "ntfs3: Cannot load iocharset %s",
420 if (new_opts->nls != sbi->options->nls)
423 "ntfs3: Cannot use different iocharset when remounting!");
427 if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) &&
430 "ntfs3: Volume is dirty and \"force\" flag is not set!");
434 swap(sbi->options, fc->fs_private);
439 static struct kmem_cache *ntfs_inode_cachep;
441 static struct inode *ntfs_alloc_inode(struct super_block *sb)
443 struct ntfs_inode *ni = alloc_inode_sb(sb, ntfs_inode_cachep, GFP_NOFS);
448 memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode));
449 mutex_init(&ni->ni_lock);
450 return &ni->vfs_inode;
453 static void ntfs_free_inode(struct inode *inode)
455 struct ntfs_inode *ni = ntfs_i(inode);
457 mutex_destroy(&ni->ni_lock);
458 kmem_cache_free(ntfs_inode_cachep, ni);
461 static void init_once(void *foo)
463 struct ntfs_inode *ni = foo;
465 inode_init_once(&ni->vfs_inode);
469 * put_ntfs - Noinline to reduce binary size.
471 static noinline void put_ntfs(struct ntfs_sb_info *sbi)
474 kvfree(ntfs_put_shared(sbi->upcase));
475 kfree(sbi->def_table);
477 wnd_close(&sbi->mft.bitmap);
478 wnd_close(&sbi->used.bitmap);
481 iput(&sbi->mft.ni->vfs_inode);
483 if (sbi->security.ni)
484 iput(&sbi->security.ni->vfs_inode);
487 iput(&sbi->reparse.ni->vfs_inode);
490 iput(&sbi->objid.ni->vfs_inode);
493 iput(&sbi->volume.ni->vfs_inode);
495 ntfs_update_mftmirr(sbi, 0);
497 indx_clear(&sbi->security.index_sii);
498 indx_clear(&sbi->security.index_sdh);
499 indx_clear(&sbi->reparse.index_r);
500 indx_clear(&sbi->objid.index_o);
501 kfree(sbi->compress.lznt);
502 #ifdef CONFIG_NTFS3_LZX_XPRESS
503 xpress_free_decompressor(sbi->compress.xpress);
504 lzx_free_decompressor(sbi->compress.lzx);
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);
516 put_mount_options(sbi->options);
518 sb->s_fs_info = NULL;
520 sync_blockdev(sb->s_bdev);
523 static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf)
525 struct super_block *sb = dentry->d_sb;
526 struct ntfs_sb_info *sbi = sb->s_fs_info;
527 struct wnd_bitmap *wnd = &sbi->used.bitmap;
529 buf->f_type = sb->s_magic;
530 buf->f_bsize = sbi->cluster_size;
531 buf->f_blocks = wnd->nbits;
533 buf->f_bfree = buf->f_bavail = wnd_zeroes(wnd);
534 buf->f_fsid.val[0] = sbi->volume.ser_num;
535 buf->f_fsid.val[1] = (sbi->volume.ser_num >> 32);
536 buf->f_namelen = NTFS_NAME_LEN;
541 static int ntfs_show_options(struct seq_file *m, struct dentry *root)
543 struct super_block *sb = root->d_sb;
544 struct ntfs_sb_info *sbi = sb->s_fs_info;
545 struct ntfs_mount_options *opts = sbi->options;
546 struct user_namespace *user_ns = seq_user_ns(m);
548 seq_printf(m, ",uid=%u", from_kuid_munged(user_ns, opts->fs_uid));
549 seq_printf(m, ",gid=%u", from_kgid_munged(user_ns, opts->fs_gid));
551 seq_printf(m, ",dmask=%04o", opts->fs_dmask_inv ^ 0xffff);
553 seq_printf(m, ",fmask=%04o", opts->fs_fmask_inv ^ 0xffff);
554 if (opts->sys_immutable)
555 seq_puts(m, ",sys_immutable");
557 seq_puts(m, ",discard");
559 seq_puts(m, ",force");
561 seq_puts(m, ",sparse");
563 seq_puts(m, ",nohidden");
564 if (opts->hide_dot_files)
565 seq_puts(m, ",hide_dot_files");
566 if (opts->windows_names)
567 seq_puts(m, ",windows_names");
569 seq_puts(m, ",showmeta");
570 if (sb->s_flags & SB_POSIXACL)
573 seq_printf(m, ",iocharset=%s", opts->nls->charset);
575 seq_puts(m, ",iocharset=utf8");
577 seq_puts(m, ",prealloc");
579 seq_puts(m, ",nocase");
585 * ntfs_sync_fs - super_operations::sync_fs
587 static int ntfs_sync_fs(struct super_block *sb, int wait)
590 struct ntfs_sb_info *sbi = sb->s_fs_info;
591 struct ntfs_inode *ni;
594 ni = sbi->security.ni;
596 inode = &ni->vfs_inode;
597 err2 = _ni_write_inode(inode, wait);
604 inode = &ni->vfs_inode;
605 err2 = _ni_write_inode(inode, wait);
610 ni = sbi->reparse.ni;
612 inode = &ni->vfs_inode;
613 err2 = _ni_write_inode(inode, wait);
619 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
621 ntfs_update_mftmirr(sbi, wait);
626 static const struct super_operations ntfs_sops = {
627 .alloc_inode = ntfs_alloc_inode,
628 .free_inode = ntfs_free_inode,
629 .evict_inode = ntfs_evict_inode,
630 .put_super = ntfs_put_super,
631 .statfs = ntfs_statfs,
632 .show_options = ntfs_show_options,
633 .sync_fs = ntfs_sync_fs,
634 .write_inode = ntfs3_write_inode,
637 static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino,
643 ref.low = cpu_to_le32(ino);
644 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
645 ref.high = cpu_to_le16(ino >> 32);
649 ref.seq = cpu_to_le16(generation);
651 inode = ntfs_iget5(sb, &ref, NULL);
652 if (!IS_ERR(inode) && is_bad_inode(inode)) {
654 inode = ERR_PTR(-ESTALE);
660 static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
661 int fh_len, int fh_type)
663 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
664 ntfs_export_get_inode);
667 static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
668 int fh_len, int fh_type)
670 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
671 ntfs_export_get_inode);
674 /* TODO: == ntfs_sync_inode */
675 static int ntfs_nfs_commit_metadata(struct inode *inode)
677 return _ni_write_inode(inode, 1);
680 static const struct export_operations ntfs_export_ops = {
681 .fh_to_dentry = ntfs_fh_to_dentry,
682 .fh_to_parent = ntfs_fh_to_parent,
683 .get_parent = ntfs3_get_parent,
684 .commit_metadata = ntfs_nfs_commit_metadata,
688 * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb".
690 static u32 format_size_gb(const u64 bytes, u32 *mb)
692 /* Do simple right 30 bit shift of 64 bit value. */
693 u64 kbytes = bytes >> 10;
694 u32 kbytes32 = kbytes;
696 *mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20;
700 return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12);
703 static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot)
705 if (boot->sectors_per_clusters <= 0x80)
706 return boot->sectors_per_clusters;
707 if (boot->sectors_per_clusters >= 0xf4) /* limit shift to 2MB max */
708 return 1U << (-(s8)boot->sectors_per_clusters);
713 * ntfs_init_from_boot - Init internal info from on-disk boot sector.
715 static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
718 struct ntfs_sb_info *sbi = sb->s_fs_info;
720 u32 mb, gb, boot_sector_size, sct_per_clst, record_size;
721 u64 sectors, clusters, mlcn, mlcn2;
722 struct NTFS_BOOT *boot;
723 struct buffer_head *bh;
728 sbi->volume.blocks = dev_size >> PAGE_SHIFT;
730 bh = ntfs_bread(sb, 0);
735 boot = (struct NTFS_BOOT *)bh->b_data;
737 if (memcmp(boot->system_id, "NTFS ", sizeof("NTFS ") - 1)) {
738 ntfs_err(sb, "Boot's signature is not NTFS.");
742 /* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/
743 /*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1])
747 boot_sector_size = ((u32)boot->bytes_per_sector[1] << 8) |
748 boot->bytes_per_sector[0];
749 if (boot_sector_size < SECTOR_SIZE ||
750 !is_power_of_2(boot_sector_size)) {
751 ntfs_err(sb, "Invalid bytes per sector %u.", boot_sector_size);
755 /* cluster size: 512, 1K, 2K, 4K, ... 2M */
756 sct_per_clst = true_sectors_per_clst(boot);
757 if ((int)sct_per_clst < 0 || !is_power_of_2(sct_per_clst)) {
758 ntfs_err(sb, "Invalid sectors per cluster %u.", sct_per_clst);
762 sbi->cluster_size = boot_sector_size * sct_per_clst;
763 sbi->cluster_bits = cluster_bits = blksize_bits(sbi->cluster_size);
764 sbi->cluster_mask = sbi->cluster_size - 1;
765 sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask;
767 mlcn = le64_to_cpu(boot->mft_clst);
768 mlcn2 = le64_to_cpu(boot->mft2_clst);
769 sectors = le64_to_cpu(boot->sectors_per_volume);
771 if (mlcn * sct_per_clst >= sectors || mlcn2 * sct_per_clst >= sectors) {
774 "Start of MFT 0x%llx (0x%llx) is out of volume 0x%llx.",
775 mlcn, mlcn2, sectors);
779 sbi->record_size = record_size =
780 boot->record_size < 0 ? 1 << (-boot->record_size) :
781 (u32)boot->record_size << cluster_bits;
782 sbi->record_bits = blksize_bits(record_size);
783 sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes
785 /* Check MFT record size. */
786 if (record_size < SECTOR_SIZE || !is_power_of_2(record_size)) {
787 ntfs_err(sb, "Invalid bytes per MFT record %u (%d).",
788 record_size, boot->record_size);
792 if (record_size > MAXIMUM_BYTES_PER_MFT) {
793 ntfs_err(sb, "Unsupported bytes per MFT record %u.",
798 sbi->index_size = boot->index_size < 0 ?
799 1u << (-boot->index_size) :
800 (u32)boot->index_size << cluster_bits;
802 /* Check index record size. */
803 if (sbi->index_size < SECTOR_SIZE || !is_power_of_2(sbi->index_size)) {
804 ntfs_err(sb, "Invalid bytes per index %u(%d).", sbi->index_size,
809 if (sbi->index_size > MAXIMUM_BYTES_PER_INDEX) {
810 ntfs_err(sb, "Unsupported bytes per index %u.",
815 sbi->volume.size = sectors * boot_sector_size;
817 gb = format_size_gb(sbi->volume.size + boot_sector_size, &mb);
820 * - Volume formatted and mounted with the same sector size.
821 * - Volume formatted 4K and mounted as 512.
822 * - Volume formatted 512 and mounted as 4K.
824 if (boot_sector_size != sector_size) {
827 "Different NTFS sector size (%u) and media sector size (%u).",
828 boot_sector_size, sector_size);
829 dev_size += sector_size - 1;
832 sbi->mft.lbo = mlcn << cluster_bits;
833 sbi->mft.lbo2 = mlcn2 << cluster_bits;
835 /* Compare boot's cluster and sector. */
836 if (sbi->cluster_size < boot_sector_size) {
837 ntfs_err(sb, "Invalid bytes per cluster (%u).",
842 /* Compare boot's cluster and media sector. */
843 if (sbi->cluster_size < sector_size) {
844 /* No way to use ntfs_get_block in this case. */
847 "Failed to mount 'cause NTFS's cluster size (%u) is less than media sector size (%u).",
848 sbi->cluster_size, sector_size);
852 sbi->max_bytes_per_attr =
853 record_size - ALIGN(MFTRECORD_FIXUP_OFFSET_1, 8) -
854 ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) -
855 ALIGN(sizeof(enum ATTR_TYPE), 8);
857 sbi->volume.ser_num = le64_to_cpu(boot->serial_num);
859 /* Warning if RAW volume. */
860 if (dev_size < sbi->volume.size + boot_sector_size) {
863 gb0 = format_size_gb(dev_size, &mb0);
866 "RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only.",
868 sb->s_flags |= SB_RDONLY;
871 clusters = sbi->volume.size >> cluster_bits;
872 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
873 /* 32 bits per cluster. */
874 if (clusters >> 32) {
877 "NTFS %u.%02u Gb is too big to use 32 bits per cluster.",
881 #elif BITS_PER_LONG < 64
882 #error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS"
885 sbi->used.bitmap.nbits = clusters;
887 rec = kzalloc(record_size, GFP_NOFS);
894 rec->rhdr.sign = NTFS_FILE_SIGNATURE;
895 rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET_1);
896 fn = (sbi->record_size >> SECTOR_SHIFT) + 1;
897 rec->rhdr.fix_num = cpu_to_le16(fn);
898 ao = ALIGN(MFTRECORD_FIXUP_OFFSET_1 + sizeof(short) * fn, 8);
899 rec->attr_off = cpu_to_le16(ao);
900 rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8));
901 rec->total = cpu_to_le32(sbi->record_size);
902 ((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END;
904 sb_set_blocksize(sb, min_t(u32, sbi->cluster_size, PAGE_SIZE));
906 sbi->block_mask = sb->s_blocksize - 1;
907 sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits;
908 sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits;
910 /* Maximum size for normal files. */
911 sbi->maxbytes = (clusters << cluster_bits) - 1;
913 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
914 if (clusters >= (1ull << (64 - cluster_bits)))
916 sbi->maxbytes_sparse = -1;
917 sb->s_maxbytes = MAX_LFS_FILESIZE;
919 /* Maximum size for sparse file. */
920 sbi->maxbytes_sparse = (1ull << (cluster_bits + 32)) - 1;
921 sb->s_maxbytes = 0xFFFFFFFFull << cluster_bits;
925 * Compute the MFT zone at two steps.
926 * It would be nice if we are able to allocate 1/8 of
927 * total clusters for MFT but not more then 512 MB.
929 sbi->zone_max = min_t(CLST, 0x20000000 >> cluster_bits, clusters >> 3);
940 * ntfs_fill_super - Try to mount.
942 static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
945 struct ntfs_sb_info *sbi = sb->s_fs_info;
946 struct block_device *bdev = sb->s_bdev;
947 struct ntfs_mount_options *options;
949 struct ntfs_inode *ni;
950 size_t i, tt, bad_len, bad_frags;
953 const struct VOLUME_INFO *info;
954 u32 idx, done, bytes;
955 struct ATTR_DEF_ENTRY *t;
962 sbi->options = options = fc->fs_private;
963 fc->fs_private = NULL;
964 sb->s_flags |= SB_NODIRATIME;
965 sb->s_magic = 0x7366746e; // "ntfs"
966 sb->s_op = &ntfs_sops;
967 sb->s_export_op = &ntfs_export_ops;
968 sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec
969 sb->s_xattr = ntfs_xattr_handlers;
970 sb->s_d_op = options->nocase ? &ntfs_dentry_ops : NULL;
972 options->nls = ntfs_load_nls(options->nls_name);
973 if (IS_ERR(options->nls)) {
975 errorf(fc, "Cannot load nls %s", options->nls_name);
980 if (bdev_max_discard_sectors(bdev) && bdev_discard_granularity(bdev)) {
981 sbi->discard_granularity = bdev_discard_granularity(bdev);
982 sbi->discard_granularity_mask_inv =
983 ~(u64)(sbi->discard_granularity - 1);
987 err = ntfs_init_from_boot(sb, bdev_logical_block_size(bdev),
988 bdev_nr_bytes(bdev));
993 * Load $Volume. This should be done before $LogFile
994 * 'cause 'sbi->volume.ni' is used 'ntfs_set_state'.
996 ref.low = cpu_to_le32(MFT_REC_VOL);
997 ref.seq = cpu_to_le16(MFT_REC_VOL);
998 inode = ntfs_iget5(sb, &ref, &NAME_VOLUME);
1000 err = PTR_ERR(inode);
1001 ntfs_err(sb, "Failed to load $Volume (%d).", err);
1007 /* Load and save label (not necessary). */
1008 attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL);
1011 /* It is ok if no ATTR_LABEL */
1012 } else if (!attr->non_res && !is_attr_ext(attr)) {
1013 /* $AttrDef allows labels to be up to 128 symbols. */
1014 err = utf16s_to_utf8s(resident_data(attr),
1015 le32_to_cpu(attr->res.data_size) >> 1,
1016 UTF16_LITTLE_ENDIAN, sbi->volume.label,
1017 sizeof(sbi->volume.label));
1019 sbi->volume.label[0] = 0;
1021 /* Should we break mounting here? */
1023 //goto put_inode_out;
1026 attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL);
1027 if (!attr || is_attr_ext(attr) ||
1028 !(info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO))) {
1029 ntfs_err(sb, "$Volume is corrupted.");
1034 sbi->volume.major_ver = info->major_ver;
1035 sbi->volume.minor_ver = info->minor_ver;
1036 sbi->volume.flags = info->flags;
1037 sbi->volume.ni = ni;
1039 /* Load $MFTMirr to estimate recs_mirr. */
1040 ref.low = cpu_to_le32(MFT_REC_MIRR);
1041 ref.seq = cpu_to_le16(MFT_REC_MIRR);
1042 inode = ntfs_iget5(sb, &ref, &NAME_MIRROR);
1043 if (IS_ERR(inode)) {
1044 err = PTR_ERR(inode);
1045 ntfs_err(sb, "Failed to load $MFTMirr (%d).", err);
1049 sbi->mft.recs_mirr = ntfs_up_cluster(sbi, inode->i_size) >>
1054 /* Load LogFile to replay. */
1055 ref.low = cpu_to_le32(MFT_REC_LOG);
1056 ref.seq = cpu_to_le16(MFT_REC_LOG);
1057 inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE);
1058 if (IS_ERR(inode)) {
1059 err = PTR_ERR(inode);
1060 ntfs_err(sb, "Failed to load \x24LogFile (%d).", err);
1066 err = ntfs_loadlog_and_replay(ni, sbi);
1072 if (sbi->flags & NTFS_FLAGS_NEED_REPLAY) {
1073 if (!sb_rdonly(sb)) {
1075 "failed to replay log file. Can't mount rw!");
1079 } else if (sbi->volume.flags & VOLUME_FLAG_DIRTY) {
1080 if (!sb_rdonly(sb) && !options->force) {
1083 "volume is dirty and \"force\" flag is not set!");
1090 ref.low = cpu_to_le32(MFT_REC_MFT);
1091 ref.seq = cpu_to_le16(1);
1093 inode = ntfs_iget5(sb, &ref, &NAME_MFT);
1094 if (IS_ERR(inode)) {
1095 err = PTR_ERR(inode);
1096 ntfs_err(sb, "Failed to load $MFT (%d).", err);
1102 sbi->mft.used = ni->i_valid >> sbi->record_bits;
1103 tt = inode->i_size >> sbi->record_bits;
1104 sbi->mft.next_free = MFT_REC_USER;
1106 err = wnd_init(&sbi->mft.bitmap, sb, tt);
1110 err = ni_load_all_mi(ni);
1112 ntfs_err(sb, "Failed to load $MFT's subrecords (%d).", err);
1119 ref.low = cpu_to_le32(MFT_REC_BITMAP);
1120 ref.seq = cpu_to_le16(MFT_REC_BITMAP);
1121 inode = ntfs_iget5(sb, &ref, &NAME_BITMAP);
1122 if (IS_ERR(inode)) {
1123 err = PTR_ERR(inode);
1124 ntfs_err(sb, "Failed to load $Bitmap (%d).", err);
1128 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
1129 if (inode->i_size >> 32) {
1135 /* Check bitmap boundary. */
1136 tt = sbi->used.bitmap.nbits;
1137 if (inode->i_size < bitmap_size(tt)) {
1138 ntfs_err(sb, "$Bitmap is corrupted.");
1143 err = wnd_init(&sbi->used.bitmap, sb, tt);
1145 ntfs_err(sb, "Failed to initialize $Bitmap (%d).", err);
1151 /* Compute the MFT zone. */
1152 err = ntfs_refresh_zone(sbi);
1154 ntfs_err(sb, "Failed to initialize MFT zone (%d).", err);
1158 /* Load $BadClus. */
1159 ref.low = cpu_to_le32(MFT_REC_BADCLUST);
1160 ref.seq = cpu_to_le16(MFT_REC_BADCLUST);
1161 inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS);
1162 if (IS_ERR(inode)) {
1163 err = PTR_ERR(inode);
1164 ntfs_err(sb, "Failed to load $BadClus (%d).", err);
1169 bad_len = bad_frags = 0;
1170 for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) {
1171 if (lcn == SPARSE_LCN)
1179 if (wnd_set_used_safe(&sbi->used.bitmap, lcn, len, &tt) || tt) {
1180 /* Bad blocks marked as free in bitmap. */
1181 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1186 * Notice about bad blocks.
1187 * In normal cases these blocks are marked as used in bitmap.
1188 * And we never allocate space in it.
1191 "Volume contains %zu bad blocks in %zu fragments.",
1192 bad_len, bad_frags);
1196 /* Load $AttrDef. */
1197 ref.low = cpu_to_le32(MFT_REC_ATTR);
1198 ref.seq = cpu_to_le16(MFT_REC_ATTR);
1199 inode = ntfs_iget5(sb, &ref, &NAME_ATTRDEF);
1200 if (IS_ERR(inode)) {
1201 err = PTR_ERR(inode);
1202 ntfs_err(sb, "Failed to load $AttrDef (%d)", err);
1207 * Typical $AttrDef contains up to 20 entries.
1208 * Check for extremely large/small size.
1210 if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY) ||
1211 inode->i_size > 100 * sizeof(struct ATTR_DEF_ENTRY)) {
1212 ntfs_err(sb, "Looks like $AttrDef is corrupted (size=%llu).",
1218 bytes = inode->i_size;
1219 sbi->def_table = t = kmalloc(bytes, GFP_NOFS | __GFP_NOWARN);
1225 for (done = idx = 0; done < bytes; done += PAGE_SIZE, idx++) {
1226 unsigned long tail = bytes - done;
1227 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1230 err = PTR_ERR(page);
1231 ntfs_err(sb, "Failed to read $AttrDef (%d).", err);
1234 memcpy(Add2Ptr(t, done), page_address(page),
1235 min(PAGE_SIZE, tail));
1236 ntfs_unmap_page(page);
1238 if (!idx && ATTR_STD != t->type) {
1239 ntfs_err(sb, "$AttrDef is corrupted.");
1246 sbi->def_entries = 1;
1247 done = sizeof(struct ATTR_DEF_ENTRY);
1248 sbi->reparse.max_size = MAXIMUM_REPARSE_DATA_BUFFER_SIZE;
1249 sbi->ea_max_size = 0x10000; /* default formatter value */
1251 while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) {
1252 u32 t32 = le32_to_cpu(t->type);
1253 u64 sz = le64_to_cpu(t->max_sz);
1255 if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32)
1258 if (t->type == ATTR_REPARSE)
1259 sbi->reparse.max_size = sz;
1260 else if (t->type == ATTR_EA)
1261 sbi->ea_max_size = sz;
1263 done += sizeof(struct ATTR_DEF_ENTRY);
1265 sbi->def_entries += 1;
1270 ref.low = cpu_to_le32(MFT_REC_UPCASE);
1271 ref.seq = cpu_to_le16(MFT_REC_UPCASE);
1272 inode = ntfs_iget5(sb, &ref, &NAME_UPCASE);
1273 if (IS_ERR(inode)) {
1274 err = PTR_ERR(inode);
1275 ntfs_err(sb, "Failed to load $UpCase (%d).", err);
1279 if (inode->i_size != 0x10000 * sizeof(short)) {
1281 ntfs_err(sb, "$UpCase is corrupted.");
1285 for (idx = 0; idx < (0x10000 * sizeof(short) >> PAGE_SHIFT); idx++) {
1287 u16 *dst = Add2Ptr(sbi->upcase, idx << PAGE_SHIFT);
1288 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1291 err = PTR_ERR(page);
1292 ntfs_err(sb, "Failed to read $UpCase (%d).", err);
1296 src = page_address(page);
1299 for (i = 0; i < PAGE_SIZE / sizeof(u16); i++)
1300 *dst++ = le16_to_cpu(*src++);
1302 memcpy(dst, src, PAGE_SIZE);
1304 ntfs_unmap_page(page);
1307 shared = ntfs_set_shared(sbi->upcase, 0x10000 * sizeof(short));
1308 if (shared && sbi->upcase != shared) {
1309 kvfree(sbi->upcase);
1310 sbi->upcase = shared;
1315 if (is_ntfs3(sbi)) {
1317 err = ntfs_security_init(sbi);
1319 ntfs_err(sb, "Failed to initialize $Secure (%d).", err);
1324 err = ntfs_extend_init(sbi);
1326 ntfs_warn(sb, "Failed to initialize $Extend.");
1330 /* Load $Extend/$Reparse. */
1331 err = ntfs_reparse_init(sbi);
1333 ntfs_warn(sb, "Failed to initialize $Extend/$Reparse.");
1337 /* Load $Extend/$ObjId. */
1338 err = ntfs_objid_init(sbi);
1340 ntfs_warn(sb, "Failed to initialize $Extend/$ObjId.");
1347 ref.low = cpu_to_le32(MFT_REC_ROOT);
1348 ref.seq = cpu_to_le16(MFT_REC_ROOT);
1349 inode = ntfs_iget5(sb, &ref, &NAME_ROOT);
1350 if (IS_ERR(inode)) {
1351 err = PTR_ERR(inode);
1352 ntfs_err(sb, "Failed to load root (%d).", err);
1357 * Final check. Looks like this case should never occurs.
1361 ntfs_err(sb, "Failed to load root (%d).", err);
1365 sb->s_root = d_make_root(inode);
1377 * Free resources here.
1378 * ntfs_fs_free will be called with fc->s_fs_info = NULL
1380 put_mount_options(sbi->options);
1382 sb->s_fs_info = NULL;
1387 void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len)
1389 struct ntfs_sb_info *sbi = sb->s_fs_info;
1390 struct block_device *bdev = sb->s_bdev;
1391 sector_t devblock = (u64)lcn * sbi->blocks_per_cluster;
1392 unsigned long blocks = (u64)len * sbi->blocks_per_cluster;
1393 unsigned long cnt = 0;
1394 unsigned long limit = global_zone_page_state(NR_FREE_PAGES)
1395 << (PAGE_SHIFT - sb->s_blocksize_bits);
1397 if (limit >= 0x2000)
1399 else if (limit < 32)
1405 clean_bdev_aliases(bdev, devblock++, 1);
1406 if (cnt++ >= limit) {
1407 sync_blockdev(bdev);
1414 * ntfs_discard - Issue a discard request (trim for SSD).
1416 int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len)
1419 u64 lbo, bytes, start, end;
1420 struct super_block *sb;
1422 if (sbi->used.next_free_lcn == lcn + len)
1423 sbi->used.next_free_lcn = lcn;
1425 if (sbi->flags & NTFS_FLAGS_NODISCARD)
1428 if (!sbi->options->discard)
1431 lbo = (u64)lcn << sbi->cluster_bits;
1432 bytes = (u64)len << sbi->cluster_bits;
1434 /* Align up 'start' on discard_granularity. */
1435 start = (lbo + sbi->discard_granularity - 1) &
1436 sbi->discard_granularity_mask_inv;
1437 /* Align down 'end' on discard_granularity. */
1438 end = (lbo + bytes) & sbi->discard_granularity_mask_inv;
1444 err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9,
1447 if (err == -EOPNOTSUPP)
1448 sbi->flags |= NTFS_FLAGS_NODISCARD;
1453 static int ntfs_fs_get_tree(struct fs_context *fc)
1455 return get_tree_bdev(fc, ntfs_fill_super);
1459 * ntfs_fs_free - Free fs_context.
1461 * Note that this will be called after fill_super and reconfigure
1462 * even when they pass. So they have to take pointers if they pass.
1464 static void ntfs_fs_free(struct fs_context *fc)
1466 struct ntfs_mount_options *opts = fc->fs_private;
1467 struct ntfs_sb_info *sbi = fc->s_fs_info;
1473 put_mount_options(opts);
1476 static const struct fs_context_operations ntfs_context_ops = {
1477 .parse_param = ntfs_fs_parse_param,
1478 .get_tree = ntfs_fs_get_tree,
1479 .reconfigure = ntfs_fs_reconfigure,
1480 .free = ntfs_fs_free,
1484 * ntfs_init_fs_context - Initialize sbi and opts
1486 * This will called when mount/remount. We will first initialize
1487 * options so that if remount we can use just that.
1489 static int ntfs_init_fs_context(struct fs_context *fc)
1491 struct ntfs_mount_options *opts;
1492 struct ntfs_sb_info *sbi;
1494 opts = kzalloc(sizeof(struct ntfs_mount_options), GFP_NOFS);
1498 /* Default options. */
1499 opts->fs_uid = current_uid();
1500 opts->fs_gid = current_gid();
1501 opts->fs_fmask_inv = ~current_umask();
1502 opts->fs_dmask_inv = ~current_umask();
1504 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
1507 sbi = kzalloc(sizeof(struct ntfs_sb_info), GFP_NOFS);
1511 sbi->upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL);
1515 ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1516 DEFAULT_RATELIMIT_BURST);
1518 mutex_init(&sbi->compress.mtx_lznt);
1519 #ifdef CONFIG_NTFS3_LZX_XPRESS
1520 mutex_init(&sbi->compress.mtx_xpress);
1521 mutex_init(&sbi->compress.mtx_lzx);
1524 fc->s_fs_info = sbi;
1526 fc->fs_private = opts;
1527 fc->ops = &ntfs_context_ops;
1538 static struct file_system_type ntfs_fs_type = {
1539 .owner = THIS_MODULE,
1541 .init_fs_context = ntfs_init_fs_context,
1542 .parameters = ntfs_fs_parameters,
1543 .kill_sb = kill_block_super,
1544 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
1548 static int __init init_ntfs_fs(void)
1552 pr_info("ntfs3: Max link count %u\n", NTFS_LINK_MAX);
1554 if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL))
1555 pr_info("ntfs3: Enabled Linux POSIX ACLs support\n");
1556 if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER))
1558 "ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n");
1559 if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS))
1560 pr_info("ntfs3: Read-only LZX/Xpress compression included\n");
1562 err = ntfs3_init_bitmap();
1566 ntfs_inode_cachep = kmem_cache_create(
1567 "ntfs_inode_cache", sizeof(struct ntfs_inode), 0,
1568 (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1570 if (!ntfs_inode_cachep) {
1575 err = register_filesystem(&ntfs_fs_type);
1581 kmem_cache_destroy(ntfs_inode_cachep);
1583 ntfs3_exit_bitmap();
1587 static void __exit exit_ntfs_fs(void)
1590 kmem_cache_destroy(ntfs_inode_cachep);
1591 unregister_filesystem(&ntfs_fs_type);
1592 ntfs3_exit_bitmap();
1595 MODULE_LICENSE("GPL");
1596 MODULE_DESCRIPTION("ntfs3 read/write filesystem");
1597 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1598 MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support");
1600 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
1603 "Warning: Activated 64 bits per cluster. Windows does not support this");
1605 #ifdef CONFIG_NTFS3_LZX_XPRESS
1606 MODULE_INFO(compression, "Read-only lzx/xpress compression included");
1609 MODULE_AUTHOR("Konstantin Komarov");
1610 MODULE_ALIAS_FS("ntfs3");
1612 module_init(init_ntfs_fs);
1613 module_exit(exit_ntfs_fs);