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^31)
43 * -----------------------------------------------------------------------------
44 * | < 8P, 2^54 | < 2^32 | yes | yes | yes | yes | yes |
45 * | > 8P, 2^54 | > 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);
261 static const struct fs_parameter_spec ntfs_fs_parameters[] = {
262 fsparam_u32("uid", Opt_uid),
263 fsparam_u32("gid", Opt_gid),
264 fsparam_u32oct("umask", Opt_umask),
265 fsparam_u32oct("dmask", Opt_dmask),
266 fsparam_u32oct("fmask", Opt_fmask),
267 fsparam_flag_no("sys_immutable", Opt_immutable),
268 fsparam_flag_no("discard", Opt_discard),
269 fsparam_flag_no("force", Opt_force),
270 fsparam_flag_no("sparse", Opt_sparse),
271 fsparam_flag_no("hidden", Opt_nohidden),
272 fsparam_flag_no("hide_dot_files", Opt_hide_dot_files),
273 fsparam_flag_no("windows_names", Opt_windows_names),
274 fsparam_flag_no("acl", Opt_acl),
275 fsparam_flag_no("showmeta", Opt_showmeta),
276 fsparam_flag_no("prealloc", Opt_prealloc),
277 fsparam_flag_no("acsrules", Opt_noacsrules),
278 fsparam_flag_no("nocase", Opt_nocase),
279 fsparam_string("iocharset", Opt_iocharset),
284 * Load nls table or if @nls is utf8 then return NULL.
286 static struct nls_table *ntfs_load_nls(char *nls)
288 struct nls_table *ret;
291 nls = CONFIG_NLS_DEFAULT;
293 if (strcmp(nls, "utf8") == 0)
296 if (strcmp(nls, CONFIG_NLS_DEFAULT) == 0)
297 return load_nls_default();
303 return ERR_PTR(-EINVAL);
306 static int ntfs_fs_parse_param(struct fs_context *fc,
307 struct fs_parameter *param)
309 struct ntfs_mount_options *opts = fc->fs_private;
310 struct fs_parse_result result;
313 opt = fs_parse(fc, ntfs_fs_parameters, param, &result);
319 opts->fs_uid = make_kuid(current_user_ns(), result.uint_32);
320 if (!uid_valid(opts->fs_uid))
321 return invalf(fc, "ntfs3: Invalid value for uid.");
324 opts->fs_gid = make_kgid(current_user_ns(), result.uint_32);
325 if (!gid_valid(opts->fs_gid))
326 return invalf(fc, "ntfs3: Invalid value for gid.");
329 if (result.uint_32 & ~07777)
330 return invalf(fc, "ntfs3: Invalid value for umask.");
331 opts->fs_fmask_inv = ~result.uint_32;
332 opts->fs_dmask_inv = ~result.uint_32;
337 if (result.uint_32 & ~07777)
338 return invalf(fc, "ntfs3: Invalid value for dmask.");
339 opts->fs_dmask_inv = ~result.uint_32;
343 if (result.uint_32 & ~07777)
344 return invalf(fc, "ntfs3: Invalid value for fmask.");
345 opts->fs_fmask_inv = ~result.uint_32;
349 opts->sys_immutable = result.negated ? 0 : 1;
352 opts->discard = result.negated ? 0 : 1;
355 opts->force = result.negated ? 0 : 1;
358 opts->sparse = result.negated ? 0 : 1;
361 opts->nohidden = result.negated ? 1 : 0;
363 case Opt_hide_dot_files:
364 opts->hide_dot_files = result.negated ? 0 : 1;
366 case Opt_windows_names:
367 opts->windows_names = result.negated ? 0 : 1;
371 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
372 fc->sb_flags |= SB_POSIXACL;
374 return invalf(fc, "ntfs3: Support for ACL not compiled in!");
377 fc->sb_flags &= ~SB_POSIXACL;
380 opts->showmeta = result.negated ? 0 : 1;
383 kfree(opts->nls_name);
384 opts->nls_name = param->string;
385 param->string = NULL;
388 opts->prealloc = result.negated ? 0 : 1;
391 opts->noacsrules = result.negated ? 1 : 0;
394 opts->nocase = result.negated ? 1 : 0;
397 /* Should not be here unless we forget add case. */
403 static int ntfs_fs_reconfigure(struct fs_context *fc)
405 struct super_block *sb = fc->root->d_sb;
406 struct ntfs_sb_info *sbi = sb->s_fs_info;
407 struct ntfs_mount_options *new_opts = fc->fs_private;
410 ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY);
411 if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) {
412 errorf(fc, "ntfs3: Couldn't remount rw because journal is not replayed. Please umount/remount instead\n");
416 new_opts->nls = ntfs_load_nls(new_opts->nls_name);
417 if (IS_ERR(new_opts->nls)) {
418 new_opts->nls = NULL;
419 errorf(fc, "ntfs3: Cannot load iocharset %s", new_opts->nls_name);
422 if (new_opts->nls != sbi->options->nls)
423 return invalf(fc, "ntfs3: Cannot use different iocharset when remounting!");
427 if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) &&
429 errorf(fc, "ntfs3: Volume is dirty and \"force\" flag is not set!");
433 swap(sbi->options, fc->fs_private);
438 static struct kmem_cache *ntfs_inode_cachep;
440 static struct inode *ntfs_alloc_inode(struct super_block *sb)
442 struct ntfs_inode *ni = alloc_inode_sb(sb, ntfs_inode_cachep, GFP_NOFS);
447 memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode));
448 mutex_init(&ni->ni_lock);
449 return &ni->vfs_inode;
452 static void ntfs_free_inode(struct inode *inode)
454 struct ntfs_inode *ni = ntfs_i(inode);
456 mutex_destroy(&ni->ni_lock);
457 kmem_cache_free(ntfs_inode_cachep, ni);
460 static void init_once(void *foo)
462 struct ntfs_inode *ni = foo;
464 inode_init_once(&ni->vfs_inode);
468 * put_ntfs - Noinline to reduce binary size.
470 static noinline void put_ntfs(struct ntfs_sb_info *sbi)
473 kvfree(ntfs_put_shared(sbi->upcase));
474 kfree(sbi->def_table);
476 wnd_close(&sbi->mft.bitmap);
477 wnd_close(&sbi->used.bitmap);
480 iput(&sbi->mft.ni->vfs_inode);
482 if (sbi->security.ni)
483 iput(&sbi->security.ni->vfs_inode);
486 iput(&sbi->reparse.ni->vfs_inode);
489 iput(&sbi->objid.ni->vfs_inode);
492 iput(&sbi->volume.ni->vfs_inode);
494 ntfs_update_mftmirr(sbi, 0);
496 indx_clear(&sbi->security.index_sii);
497 indx_clear(&sbi->security.index_sdh);
498 indx_clear(&sbi->reparse.index_r);
499 indx_clear(&sbi->objid.index_o);
500 kfree(sbi->compress.lznt);
501 #ifdef CONFIG_NTFS3_LZX_XPRESS
502 xpress_free_decompressor(sbi->compress.xpress);
503 lzx_free_decompressor(sbi->compress.lzx);
508 static void ntfs_put_super(struct super_block *sb)
510 struct ntfs_sb_info *sbi = sb->s_fs_info;
512 /* Mark rw ntfs as clear, if possible. */
513 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
515 put_mount_options(sbi->options);
517 sb->s_fs_info = NULL;
519 sync_blockdev(sb->s_bdev);
522 static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf)
524 struct super_block *sb = dentry->d_sb;
525 struct ntfs_sb_info *sbi = sb->s_fs_info;
526 struct wnd_bitmap *wnd = &sbi->used.bitmap;
528 buf->f_type = sb->s_magic;
529 buf->f_bsize = sbi->cluster_size;
530 buf->f_blocks = wnd->nbits;
532 buf->f_bfree = buf->f_bavail = wnd_zeroes(wnd);
533 buf->f_fsid.val[0] = sbi->volume.ser_num;
534 buf->f_fsid.val[1] = (sbi->volume.ser_num >> 32);
535 buf->f_namelen = NTFS_NAME_LEN;
540 static int ntfs_show_options(struct seq_file *m, struct dentry *root)
542 struct super_block *sb = root->d_sb;
543 struct ntfs_sb_info *sbi = sb->s_fs_info;
544 struct ntfs_mount_options *opts = sbi->options;
545 struct user_namespace *user_ns = seq_user_ns(m);
547 seq_printf(m, ",uid=%u",
548 from_kuid_munged(user_ns, opts->fs_uid));
549 seq_printf(m, ",gid=%u",
550 from_kgid_munged(user_ns, opts->fs_gid));
552 seq_printf(m, ",fmask=%04o", opts->fs_fmask_inv ^ 0xffff);
554 seq_printf(m, ",dmask=%04o", opts->fs_dmask_inv ^ 0xffff);
556 seq_printf(m, ",iocharset=%s", opts->nls->charset);
558 seq_puts(m, ",iocharset=utf8");
559 if (opts->sys_immutable)
560 seq_puts(m, ",sys_immutable");
562 seq_puts(m, ",discard");
564 seq_puts(m, ",sparse");
566 seq_puts(m, ",showmeta");
568 seq_puts(m, ",nohidden");
569 if (opts->windows_names)
570 seq_puts(m, ",windows_names");
571 if (opts->hide_dot_files)
572 seq_puts(m, ",hide_dot_files");
574 seq_puts(m, ",force");
575 if (opts->noacsrules)
576 seq_puts(m, ",noacsrules");
578 seq_puts(m, ",prealloc");
579 if (sb->s_flags & SB_POSIXACL)
586 * ntfs_sync_fs - super_operations::sync_fs
588 static int ntfs_sync_fs(struct super_block *sb, int wait)
591 struct ntfs_sb_info *sbi = sb->s_fs_info;
592 struct ntfs_inode *ni;
595 ni = sbi->security.ni;
597 inode = &ni->vfs_inode;
598 err2 = _ni_write_inode(inode, wait);
605 inode = &ni->vfs_inode;
606 err2 = _ni_write_inode(inode, wait);
611 ni = sbi->reparse.ni;
613 inode = &ni->vfs_inode;
614 err2 = _ni_write_inode(inode, wait);
620 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
622 ntfs_update_mftmirr(sbi, wait);
627 static const struct super_operations ntfs_sops = {
628 .alloc_inode = ntfs_alloc_inode,
629 .free_inode = ntfs_free_inode,
630 .evict_inode = ntfs_evict_inode,
631 .put_super = ntfs_put_super,
632 .statfs = ntfs_statfs,
633 .show_options = ntfs_show_options,
634 .sync_fs = ntfs_sync_fs,
635 .write_inode = ntfs3_write_inode,
638 static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino,
644 ref.low = cpu_to_le32(ino);
645 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
646 ref.high = cpu_to_le16(ino >> 32);
650 ref.seq = cpu_to_le16(generation);
652 inode = ntfs_iget5(sb, &ref, NULL);
653 if (!IS_ERR(inode) && is_bad_inode(inode)) {
655 inode = ERR_PTR(-ESTALE);
661 static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
662 int fh_len, int fh_type)
664 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
665 ntfs_export_get_inode);
668 static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
669 int fh_len, int fh_type)
671 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
672 ntfs_export_get_inode);
675 /* TODO: == ntfs_sync_inode */
676 static int ntfs_nfs_commit_metadata(struct inode *inode)
678 return _ni_write_inode(inode, 1);
681 static const struct export_operations ntfs_export_ops = {
682 .fh_to_dentry = ntfs_fh_to_dentry,
683 .fh_to_parent = ntfs_fh_to_parent,
684 .get_parent = ntfs3_get_parent,
685 .commit_metadata = ntfs_nfs_commit_metadata,
689 * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb".
691 static u32 format_size_gb(const u64 bytes, u32 *mb)
693 /* Do simple right 30 bit shift of 64 bit value. */
694 u64 kbytes = bytes >> 10;
695 u32 kbytes32 = kbytes;
697 *mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20;
701 return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12);
704 static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot)
706 if (boot->sectors_per_clusters <= 0x80)
707 return boot->sectors_per_clusters;
708 if (boot->sectors_per_clusters >= 0xf4) /* limit shift to 2MB max */
709 return 1U << -(s8)boot->sectors_per_clusters;
714 * ntfs_init_from_boot - Init internal info from on-disk boot sector.
716 static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
719 struct ntfs_sb_info *sbi = sb->s_fs_info;
721 u32 mb, gb, boot_sector_size, sct_per_clst, record_size;
722 u64 sectors, clusters, mlcn, mlcn2;
723 struct NTFS_BOOT *boot;
724 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))
740 /* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/
741 /*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1])
745 boot_sector_size = (u32)boot->bytes_per_sector[1] << 8;
746 if (boot->bytes_per_sector[0] || boot_sector_size < SECTOR_SIZE ||
747 !is_power_of_2(boot_sector_size)) {
751 /* cluster size: 512, 1K, 2K, 4K, ... 2M */
752 sct_per_clst = true_sectors_per_clst(boot);
753 if ((int)sct_per_clst < 0)
755 if (!is_power_of_2(sct_per_clst))
758 mlcn = le64_to_cpu(boot->mft_clst);
759 mlcn2 = le64_to_cpu(boot->mft2_clst);
760 sectors = le64_to_cpu(boot->sectors_per_volume);
762 if (mlcn * sct_per_clst >= sectors)
765 if (mlcn2 * sct_per_clst >= sectors)
768 /* Check MFT record size. */
769 if ((boot->record_size < 0 &&
770 SECTOR_SIZE > (2U << (-boot->record_size))) ||
771 (boot->record_size >= 0 && !is_power_of_2(boot->record_size))) {
775 /* Check index record size. */
776 if ((boot->index_size < 0 &&
777 SECTOR_SIZE > (2U << (-boot->index_size))) ||
778 (boot->index_size >= 0 && !is_power_of_2(boot->index_size))) {
782 sbi->volume.size = sectors * boot_sector_size;
784 gb = format_size_gb(sbi->volume.size + boot_sector_size, &mb);
787 * - Volume formatted and mounted with the same sector size.
788 * - Volume formatted 4K and mounted as 512.
789 * - Volume formatted 512 and mounted as 4K.
791 if (boot_sector_size != sector_size) {
794 "Different NTFS' sector size (%u) and media sector size (%u)",
795 boot_sector_size, sector_size);
796 dev_size += sector_size - 1;
799 sbi->cluster_size = boot_sector_size * sct_per_clst;
800 sbi->cluster_bits = blksize_bits(sbi->cluster_size);
802 sbi->mft.lbo = mlcn << sbi->cluster_bits;
803 sbi->mft.lbo2 = mlcn2 << sbi->cluster_bits;
805 /* Compare boot's cluster and sector. */
806 if (sbi->cluster_size < boot_sector_size)
809 /* Compare boot's cluster and media sector. */
810 if (sbi->cluster_size < sector_size) {
811 /* No way to use ntfs_get_block in this case. */
814 "Failed to mount 'cause NTFS's cluster size (%u) is less than media sector size (%u)",
815 sbi->cluster_size, sector_size);
819 sbi->cluster_mask = sbi->cluster_size - 1;
820 sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask;
821 sbi->record_size = record_size = boot->record_size < 0
822 ? 1 << (-boot->record_size)
823 : (u32)boot->record_size
824 << sbi->cluster_bits;
826 if (record_size > MAXIMUM_BYTES_PER_MFT || record_size < SECTOR_SIZE)
829 sbi->record_bits = blksize_bits(record_size);
830 sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes
832 sbi->max_bytes_per_attr =
833 record_size - ALIGN(MFTRECORD_FIXUP_OFFSET_1, 8) -
834 ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) -
835 ALIGN(sizeof(enum ATTR_TYPE), 8);
837 sbi->index_size = boot->index_size < 0
838 ? 1u << (-boot->index_size)
839 : (u32)boot->index_size << sbi->cluster_bits;
841 sbi->volume.ser_num = le64_to_cpu(boot->serial_num);
843 /* Warning if RAW volume. */
844 if (dev_size < sbi->volume.size + boot_sector_size) {
847 gb0 = format_size_gb(dev_size, &mb0);
850 "RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only",
852 sb->s_flags |= SB_RDONLY;
855 clusters = sbi->volume.size >> sbi->cluster_bits;
856 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
857 /* 32 bits per cluster. */
858 if (clusters >> 32) {
861 "NTFS %u.%02u Gb is too big to use 32 bits per cluster",
865 #elif BITS_PER_LONG < 64
866 #error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS"
869 sbi->used.bitmap.nbits = clusters;
871 rec = kzalloc(record_size, GFP_NOFS);
878 rec->rhdr.sign = NTFS_FILE_SIGNATURE;
879 rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET_1);
880 fn = (sbi->record_size >> SECTOR_SHIFT) + 1;
881 rec->rhdr.fix_num = cpu_to_le16(fn);
882 ao = ALIGN(MFTRECORD_FIXUP_OFFSET_1 + sizeof(short) * fn, 8);
883 rec->attr_off = cpu_to_le16(ao);
884 rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8));
885 rec->total = cpu_to_le32(sbi->record_size);
886 ((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END;
888 sb_set_blocksize(sb, min_t(u32, sbi->cluster_size, PAGE_SIZE));
890 sbi->block_mask = sb->s_blocksize - 1;
891 sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits;
892 sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits;
894 /* Maximum size for normal files. */
895 sbi->maxbytes = (clusters << sbi->cluster_bits) - 1;
897 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
898 if (clusters >= (1ull << (64 - sbi->cluster_bits)))
900 sbi->maxbytes_sparse = -1;
901 sb->s_maxbytes = MAX_LFS_FILESIZE;
903 /* Maximum size for sparse file. */
904 sbi->maxbytes_sparse = (1ull << (sbi->cluster_bits + 32)) - 1;
905 sb->s_maxbytes = 0xFFFFFFFFull << sbi->cluster_bits;
909 * Compute the MFT zone at two steps.
910 * It would be nice if we are able to allocate 1/8 of
911 * total clusters for MFT but not more then 512 MB.
913 sbi->zone_max = min_t(CLST, 0x20000000 >> sbi->cluster_bits, clusters >> 3);
924 * ntfs_fill_super - Try to mount.
926 static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
929 struct ntfs_sb_info *sbi = sb->s_fs_info;
930 struct block_device *bdev = sb->s_bdev;
932 struct ntfs_inode *ni;
933 size_t i, tt, bad_len, bad_frags;
936 const struct VOLUME_INFO *info;
937 u32 idx, done, bytes;
938 struct ATTR_DEF_ENTRY *t;
945 sbi->options = fc->fs_private;
946 fc->fs_private = NULL;
947 sb->s_flags |= SB_NODIRATIME;
948 sb->s_magic = 0x7366746e; // "ntfs"
949 sb->s_op = &ntfs_sops;
950 sb->s_export_op = &ntfs_export_ops;
951 sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec
952 sb->s_xattr = ntfs_xattr_handlers;
953 sb->s_d_op = sbi->options->nocase ? &ntfs_dentry_ops : NULL;
955 sbi->options->nls = ntfs_load_nls(sbi->options->nls_name);
956 if (IS_ERR(sbi->options->nls)) {
957 sbi->options->nls = NULL;
958 errorf(fc, "Cannot load nls %s", sbi->options->nls_name);
963 if (bdev_max_discard_sectors(bdev) && bdev_discard_granularity(bdev)) {
964 sbi->discard_granularity = bdev_discard_granularity(bdev);
965 sbi->discard_granularity_mask_inv =
966 ~(u64)(sbi->discard_granularity - 1);
970 err = ntfs_init_from_boot(sb, bdev_logical_block_size(bdev),
971 bdev_nr_bytes(bdev));
976 * Load $Volume. This should be done before $LogFile
977 * 'cause 'sbi->volume.ni' is used 'ntfs_set_state'.
979 ref.low = cpu_to_le32(MFT_REC_VOL);
980 ref.seq = cpu_to_le16(MFT_REC_VOL);
981 inode = ntfs_iget5(sb, &ref, &NAME_VOLUME);
983 ntfs_err(sb, "Failed to load $Volume.");
984 err = PTR_ERR(inode);
990 /* Load and save label (not necessary). */
991 attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL);
994 /* It is ok if no ATTR_LABEL */
995 } else if (!attr->non_res && !is_attr_ext(attr)) {
996 /* $AttrDef allows labels to be up to 128 symbols. */
997 err = utf16s_to_utf8s(resident_data(attr),
998 le32_to_cpu(attr->res.data_size) >> 1,
999 UTF16_LITTLE_ENDIAN, sbi->volume.label,
1000 sizeof(sbi->volume.label));
1002 sbi->volume.label[0] = 0;
1004 /* Should we break mounting here? */
1006 //goto put_inode_out;
1009 attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL);
1010 if (!attr || is_attr_ext(attr)) {
1015 info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO);
1021 sbi->volume.major_ver = info->major_ver;
1022 sbi->volume.minor_ver = info->minor_ver;
1023 sbi->volume.flags = info->flags;
1024 sbi->volume.ni = ni;
1026 /* Load $MFTMirr to estimate recs_mirr. */
1027 ref.low = cpu_to_le32(MFT_REC_MIRR);
1028 ref.seq = cpu_to_le16(MFT_REC_MIRR);
1029 inode = ntfs_iget5(sb, &ref, &NAME_MIRROR);
1030 if (IS_ERR(inode)) {
1031 ntfs_err(sb, "Failed to load $MFTMirr.");
1032 err = PTR_ERR(inode);
1036 sbi->mft.recs_mirr =
1037 ntfs_up_cluster(sbi, inode->i_size) >> sbi->record_bits;
1041 /* Load LogFile to replay. */
1042 ref.low = cpu_to_le32(MFT_REC_LOG);
1043 ref.seq = cpu_to_le16(MFT_REC_LOG);
1044 inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE);
1045 if (IS_ERR(inode)) {
1046 ntfs_err(sb, "Failed to load \x24LogFile.");
1047 err = PTR_ERR(inode);
1053 err = ntfs_loadlog_and_replay(ni, sbi);
1059 if (sbi->flags & NTFS_FLAGS_NEED_REPLAY) {
1060 if (!sb_rdonly(sb)) {
1062 "failed to replay log file. Can't mount rw!");
1066 } else if (sbi->volume.flags & VOLUME_FLAG_DIRTY) {
1067 if (!sb_rdonly(sb) && !sbi->options->force) {
1070 "volume is dirty and \"force\" flag is not set!");
1077 ref.low = cpu_to_le32(MFT_REC_MFT);
1078 ref.seq = cpu_to_le16(1);
1080 inode = ntfs_iget5(sb, &ref, &NAME_MFT);
1081 if (IS_ERR(inode)) {
1082 ntfs_err(sb, "Failed to load $MFT.");
1083 err = PTR_ERR(inode);
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_BITMAP);
1105 ref.seq = cpu_to_le16(MFT_REC_BITMAP);
1106 inode = ntfs_iget5(sb, &ref, &NAME_BITMAP);
1107 if (IS_ERR(inode)) {
1108 ntfs_err(sb, "Failed to load $Bitmap.");
1109 err = PTR_ERR(inode);
1113 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
1114 if (inode->i_size >> 32) {
1120 /* Check bitmap boundary. */
1121 tt = sbi->used.bitmap.nbits;
1122 if (inode->i_size < bitmap_size(tt)) {
1127 /* Not necessary. */
1128 sbi->used.bitmap.set_tail = true;
1129 err = wnd_init(&sbi->used.bitmap, sb, tt);
1135 /* Compute the MFT zone. */
1136 err = ntfs_refresh_zone(sbi);
1140 /* Load $BadClus. */
1141 ref.low = cpu_to_le32(MFT_REC_BADCLUST);
1142 ref.seq = cpu_to_le16(MFT_REC_BADCLUST);
1143 inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS);
1144 if (IS_ERR(inode)) {
1145 err = PTR_ERR(inode);
1146 ntfs_err(sb, "Failed to load $BadClus (%d).", err);
1151 bad_len = bad_frags = 0;
1152 for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) {
1153 if (lcn == SPARSE_LCN)
1161 if (wnd_set_used_safe(&sbi->used.bitmap, lcn, len, &tt) || tt) {
1162 /* Bad blocks marked as free in bitmap. */
1163 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1168 * Notice about bad blocks.
1169 * In normal cases these blocks are marked as used in bitmap.
1170 * And we never allocate space in it.
1173 "Volume contains %zu bad blocks in %zu fragments.",
1174 bad_len, bad_frags);
1178 /* Load $AttrDef. */
1179 ref.low = cpu_to_le32(MFT_REC_ATTR);
1180 ref.seq = cpu_to_le16(MFT_REC_ATTR);
1181 inode = ntfs_iget5(sb, &ref, &NAME_ATTRDEF);
1182 if (IS_ERR(inode)) {
1183 ntfs_err(sb, "Failed to load $AttrDef -> %d", err);
1184 err = PTR_ERR(inode);
1188 if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY)) {
1192 bytes = inode->i_size;
1193 sbi->def_table = t = kmalloc(bytes, GFP_NOFS | __GFP_NOWARN);
1199 for (done = idx = 0; done < bytes; done += PAGE_SIZE, idx++) {
1200 unsigned long tail = bytes - done;
1201 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1204 err = PTR_ERR(page);
1207 memcpy(Add2Ptr(t, done), page_address(page),
1208 min(PAGE_SIZE, tail));
1209 ntfs_unmap_page(page);
1211 if (!idx && ATTR_STD != t->type) {
1218 sbi->def_entries = 1;
1219 done = sizeof(struct ATTR_DEF_ENTRY);
1220 sbi->reparse.max_size = MAXIMUM_REPARSE_DATA_BUFFER_SIZE;
1221 sbi->ea_max_size = 0x10000; /* default formatter value */
1223 while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) {
1224 u32 t32 = le32_to_cpu(t->type);
1225 u64 sz = le64_to_cpu(t->max_sz);
1227 if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32)
1230 if (t->type == ATTR_REPARSE)
1231 sbi->reparse.max_size = sz;
1232 else if (t->type == ATTR_EA)
1233 sbi->ea_max_size = sz;
1235 done += sizeof(struct ATTR_DEF_ENTRY);
1237 sbi->def_entries += 1;
1242 ref.low = cpu_to_le32(MFT_REC_UPCASE);
1243 ref.seq = cpu_to_le16(MFT_REC_UPCASE);
1244 inode = ntfs_iget5(sb, &ref, &NAME_UPCASE);
1245 if (IS_ERR(inode)) {
1246 ntfs_err(sb, "Failed to load $UpCase.");
1247 err = PTR_ERR(inode);
1251 if (inode->i_size != 0x10000 * sizeof(short)) {
1256 for (idx = 0; idx < (0x10000 * sizeof(short) >> PAGE_SHIFT); idx++) {
1258 u16 *dst = Add2Ptr(sbi->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(sbi->upcase, 0x10000 * sizeof(short));
1278 if (shared && sbi->upcase != shared) {
1279 kvfree(sbi->upcase);
1280 sbi->upcase = shared;
1285 if (is_ntfs3(sbi)) {
1287 err = ntfs_security_init(sbi);
1292 err = ntfs_extend_init(sbi);
1296 /* Load $Extend\$Reparse. */
1297 err = ntfs_reparse_init(sbi);
1301 /* Load $Extend\$ObjId. */
1302 err = ntfs_objid_init(sbi);
1309 ref.low = cpu_to_le32(MFT_REC_ROOT);
1310 ref.seq = cpu_to_le16(MFT_REC_ROOT);
1311 inode = ntfs_iget5(sb, &ref, &NAME_ROOT);
1312 if (IS_ERR(inode) || !inode->i_op) {
1313 ntfs_err(sb, "Failed to load root.");
1314 err = IS_ERR(inode) ? PTR_ERR(inode) : -EINVAL;
1318 sb->s_root = d_make_root(inode);
1330 * Free resources here.
1331 * ntfs_fs_free will be called with fc->s_fs_info = NULL
1333 put_mount_options(sbi->options);
1335 sb->s_fs_info = NULL;
1340 void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len)
1342 struct ntfs_sb_info *sbi = sb->s_fs_info;
1343 struct block_device *bdev = sb->s_bdev;
1344 sector_t devblock = (u64)lcn * sbi->blocks_per_cluster;
1345 unsigned long blocks = (u64)len * sbi->blocks_per_cluster;
1346 unsigned long cnt = 0;
1347 unsigned long limit = global_zone_page_state(NR_FREE_PAGES)
1348 << (PAGE_SHIFT - sb->s_blocksize_bits);
1350 if (limit >= 0x2000)
1352 else if (limit < 32)
1358 clean_bdev_aliases(bdev, devblock++, 1);
1359 if (cnt++ >= limit) {
1360 sync_blockdev(bdev);
1367 * ntfs_discard - Issue a discard request (trim for SSD).
1369 int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len)
1372 u64 lbo, bytes, start, end;
1373 struct super_block *sb;
1375 if (sbi->used.next_free_lcn == lcn + len)
1376 sbi->used.next_free_lcn = lcn;
1378 if (sbi->flags & NTFS_FLAGS_NODISCARD)
1381 if (!sbi->options->discard)
1384 lbo = (u64)lcn << sbi->cluster_bits;
1385 bytes = (u64)len << sbi->cluster_bits;
1387 /* Align up 'start' on discard_granularity. */
1388 start = (lbo + sbi->discard_granularity - 1) &
1389 sbi->discard_granularity_mask_inv;
1390 /* Align down 'end' on discard_granularity. */
1391 end = (lbo + bytes) & sbi->discard_granularity_mask_inv;
1397 err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9,
1400 if (err == -EOPNOTSUPP)
1401 sbi->flags |= NTFS_FLAGS_NODISCARD;
1406 static int ntfs_fs_get_tree(struct fs_context *fc)
1408 return get_tree_bdev(fc, ntfs_fill_super);
1412 * ntfs_fs_free - Free fs_context.
1414 * Note that this will be called after fill_super and reconfigure
1415 * even when they pass. So they have to take pointers if they pass.
1417 static void ntfs_fs_free(struct fs_context *fc)
1419 struct ntfs_mount_options *opts = fc->fs_private;
1420 struct ntfs_sb_info *sbi = fc->s_fs_info;
1426 put_mount_options(opts);
1429 static const struct fs_context_operations ntfs_context_ops = {
1430 .parse_param = ntfs_fs_parse_param,
1431 .get_tree = ntfs_fs_get_tree,
1432 .reconfigure = ntfs_fs_reconfigure,
1433 .free = ntfs_fs_free,
1437 * ntfs_init_fs_context - Initialize spi and opts
1439 * This will called when mount/remount. We will first initialize
1440 * options so that if remount we can use just that.
1442 static int ntfs_init_fs_context(struct fs_context *fc)
1444 struct ntfs_mount_options *opts;
1445 struct ntfs_sb_info *sbi;
1447 opts = kzalloc(sizeof(struct ntfs_mount_options), GFP_NOFS);
1451 /* Default options. */
1452 opts->fs_uid = current_uid();
1453 opts->fs_gid = current_gid();
1454 opts->fs_fmask_inv = ~current_umask();
1455 opts->fs_dmask_inv = ~current_umask();
1457 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
1460 sbi = kzalloc(sizeof(struct ntfs_sb_info), GFP_NOFS);
1464 sbi->upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL);
1468 ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1469 DEFAULT_RATELIMIT_BURST);
1471 mutex_init(&sbi->compress.mtx_lznt);
1472 #ifdef CONFIG_NTFS3_LZX_XPRESS
1473 mutex_init(&sbi->compress.mtx_xpress);
1474 mutex_init(&sbi->compress.mtx_lzx);
1477 fc->s_fs_info = sbi;
1479 fc->fs_private = opts;
1480 fc->ops = &ntfs_context_ops;
1491 static struct file_system_type ntfs_fs_type = {
1492 .owner = THIS_MODULE,
1494 .init_fs_context = ntfs_init_fs_context,
1495 .parameters = ntfs_fs_parameters,
1496 .kill_sb = kill_block_super,
1497 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
1501 static int __init init_ntfs_fs(void)
1505 pr_info("ntfs3: Max link count %u\n", NTFS_LINK_MAX);
1507 if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL))
1508 pr_info("ntfs3: Enabled Linux POSIX ACLs support\n");
1509 if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER))
1510 pr_notice("ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n");
1511 if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS))
1512 pr_info("ntfs3: Read-only LZX/Xpress compression included\n");
1514 err = ntfs3_init_bitmap();
1518 ntfs_inode_cachep = kmem_cache_create(
1519 "ntfs_inode_cache", sizeof(struct ntfs_inode), 0,
1520 (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1522 if (!ntfs_inode_cachep) {
1527 err = register_filesystem(&ntfs_fs_type);
1533 kmem_cache_destroy(ntfs_inode_cachep);
1535 ntfs3_exit_bitmap();
1539 static void __exit exit_ntfs_fs(void)
1542 kmem_cache_destroy(ntfs_inode_cachep);
1543 unregister_filesystem(&ntfs_fs_type);
1544 ntfs3_exit_bitmap();
1547 MODULE_LICENSE("GPL");
1548 MODULE_DESCRIPTION("ntfs3 read/write filesystem");
1549 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1550 MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support");
1552 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
1553 MODULE_INFO(cluster, "Warning: Activated 64 bits per cluster. Windows does not support this");
1555 #ifdef CONFIG_NTFS3_LZX_XPRESS
1556 MODULE_INFO(compression, "Read-only lzx/xpress compression included");
1559 MODULE_AUTHOR("Konstantin Komarov");
1560 MODULE_ALIAS_FS("ntfs3");
1562 module_init(init_ntfs_fs);
1563 module_exit(exit_ntfs_fs);