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/proc_fs.h>
61 #include <linux/seq_file.h>
62 #include <linux/statfs.h>
67 #ifdef CONFIG_NTFS3_LZX_XPRESS
73 * ntfs_printk - Trace warnings/notices/errors.
75 * Thanks Joe Perches <joe@perches.com> for implementation
77 void ntfs_printk(const struct super_block
*sb
, const char *fmt
, ...)
82 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
84 /* Should we use different ratelimits for warnings/notices/errors? */
85 if (!___ratelimit(&sbi
->msg_ratelimit
, "ntfs3"))
90 level
= printk_get_level(fmt
);
91 vaf
.fmt
= printk_skip_level(fmt
);
93 printk("%c%cntfs3: %s: %pV\n", KERN_SOH_ASCII
, level
, sb
->s_id
, &vaf
);
98 static char s_name_buf
[512];
99 static atomic_t s_name_buf_cnt
= ATOMIC_INIT(1); // 1 means 'free s_name_buf'.
104 * Print warnings/notices/errors about inode using name or inode number.
106 void ntfs_inode_printk(struct inode
*inode
, const char *fmt
, ...)
108 struct super_block
*sb
= inode
->i_sb
;
109 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
112 struct va_format vaf
;
115 if (!___ratelimit(&sbi
->msg_ratelimit
, "ntfs3"))
118 /* Use static allocated buffer, if possible. */
119 name
= atomic_dec_and_test(&s_name_buf_cnt
) ?
121 kmalloc(sizeof(s_name_buf
), GFP_NOFS
);
124 struct dentry
*de
= d_find_alias(inode
);
125 const u32 name_len
= ARRAY_SIZE(s_name_buf
) - 1;
128 spin_lock(&de
->d_lock
);
129 snprintf(name
, name_len
, " \"%s\"", de
->d_name
.name
);
130 spin_unlock(&de
->d_lock
);
131 name
[name_len
] = 0; /* To be sure. */
135 dput(de
); /* Cocci warns if placed in branch "if (de)" */
140 level
= printk_get_level(fmt
);
141 vaf
.fmt
= printk_skip_level(fmt
);
144 printk("%c%cntfs3: %s: ino=%lx,%s %pV\n", KERN_SOH_ASCII
, level
,
145 sb
->s_id
, inode
->i_ino
, name
? name
: "", &vaf
);
149 atomic_inc(&s_name_buf_cnt
);
150 if (name
!= s_name_buf
)
156 * Shared memory struct.
158 * On-disk ntfs's upcase table is created by ntfs formatter.
159 * 'upcase' table is 128K bytes of memory.
160 * We should read it into memory when mounting.
161 * Several ntfs volumes likely use the same 'upcase' table.
162 * It is good idea to share in-memory 'upcase' table between different volumes.
163 * Unfortunately winxp/vista/win7 use different upcase tables.
165 static DEFINE_SPINLOCK(s_shared_lock
);
177 * * @ptr - If pointer was saved in shared memory.
178 * * NULL - If pointer was not shared.
180 void *ntfs_set_shared(void *ptr
, u32 bytes
)
185 spin_lock(&s_shared_lock
);
186 for (i
= 0; i
< ARRAY_SIZE(s_shared
); i
++) {
187 if (!s_shared
[i
].cnt
) {
189 } else if (bytes
== s_shared
[i
].len
&&
190 !memcmp(s_shared
[i
].ptr
, ptr
, bytes
)) {
191 s_shared
[i
].cnt
+= 1;
192 ret
= s_shared
[i
].ptr
;
197 if (!ret
&& j
!= -1) {
198 s_shared
[j
].ptr
= ptr
;
199 s_shared
[j
].len
= bytes
;
203 spin_unlock(&s_shared_lock
);
212 * * @ptr - If pointer is not shared anymore.
213 * * NULL - If pointer is still shared.
215 void *ntfs_put_shared(void *ptr
)
220 spin_lock(&s_shared_lock
);
221 for (i
= 0; i
< ARRAY_SIZE(s_shared
); i
++) {
222 if (s_shared
[i
].cnt
&& s_shared
[i
].ptr
== ptr
) {
223 if (--s_shared
[i
].cnt
)
228 spin_unlock(&s_shared_lock
);
233 static inline void put_mount_options(struct ntfs_mount_options
*options
)
235 kfree(options
->nls_name
);
236 unload_nls(options
->nls
);
262 static const struct fs_parameter_spec ntfs_fs_parameters
[] = {
263 fsparam_u32("uid", Opt_uid
),
264 fsparam_u32("gid", Opt_gid
),
265 fsparam_u32oct("umask", Opt_umask
),
266 fsparam_u32oct("dmask", Opt_dmask
),
267 fsparam_u32oct("fmask", Opt_fmask
),
268 fsparam_flag_no("sys_immutable", Opt_immutable
),
269 fsparam_flag_no("discard", Opt_discard
),
270 fsparam_flag_no("force", Opt_force
),
271 fsparam_flag_no("sparse", Opt_sparse
),
272 fsparam_flag_no("hidden", Opt_nohidden
),
273 fsparam_flag_no("hide_dot_files", Opt_hide_dot_files
),
274 fsparam_flag_no("windows_names", Opt_windows_names
),
275 fsparam_flag_no("showmeta", Opt_showmeta
),
276 fsparam_flag_no("acl", Opt_acl
),
277 fsparam_string("iocharset", Opt_iocharset
),
278 fsparam_flag_no("prealloc", Opt_prealloc
),
279 fsparam_flag_no("nocase", Opt_nocase
),
285 * Load nls table or if @nls is utf8 then return NULL.
287 * It is good idea to use here "const char *nls".
288 * But load_nls accepts "char*".
290 static struct nls_table
*ntfs_load_nls(char *nls
)
292 struct nls_table
*ret
;
295 nls
= CONFIG_NLS_DEFAULT
;
297 if (strcmp(nls
, "utf8") == 0)
300 if (strcmp(nls
, CONFIG_NLS_DEFAULT
) == 0)
301 return load_nls_default();
307 return ERR_PTR(-EINVAL
);
310 static int ntfs_fs_parse_param(struct fs_context
*fc
,
311 struct fs_parameter
*param
)
313 struct ntfs_mount_options
*opts
= fc
->fs_private
;
314 struct fs_parse_result result
;
317 opt
= fs_parse(fc
, ntfs_fs_parameters
, param
, &result
);
323 opts
->fs_uid
= make_kuid(current_user_ns(), result
.uint_32
);
324 if (!uid_valid(opts
->fs_uid
))
325 return invalf(fc
, "ntfs3: Invalid value for uid.");
328 opts
->fs_gid
= make_kgid(current_user_ns(), result
.uint_32
);
329 if (!gid_valid(opts
->fs_gid
))
330 return invalf(fc
, "ntfs3: Invalid value for gid.");
333 if (result
.uint_32
& ~07777)
334 return invalf(fc
, "ntfs3: Invalid value for umask.");
335 opts
->fs_fmask_inv
= ~result
.uint_32
;
336 opts
->fs_dmask_inv
= ~result
.uint_32
;
341 if (result
.uint_32
& ~07777)
342 return invalf(fc
, "ntfs3: Invalid value for dmask.");
343 opts
->fs_dmask_inv
= ~result
.uint_32
;
347 if (result
.uint_32
& ~07777)
348 return invalf(fc
, "ntfs3: Invalid value for fmask.");
349 opts
->fs_fmask_inv
= ~result
.uint_32
;
353 opts
->sys_immutable
= result
.negated
? 0 : 1;
356 opts
->discard
= result
.negated
? 0 : 1;
359 opts
->force
= result
.negated
? 0 : 1;
362 opts
->sparse
= result
.negated
? 0 : 1;
365 opts
->nohidden
= result
.negated
? 1 : 0;
367 case Opt_hide_dot_files
:
368 opts
->hide_dot_files
= result
.negated
? 0 : 1;
370 case Opt_windows_names
:
371 opts
->windows_names
= result
.negated
? 0 : 1;
374 opts
->showmeta
= result
.negated
? 0 : 1;
378 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
379 fc
->sb_flags
|= SB_POSIXACL
;
382 fc
, "ntfs3: Support for ACL not compiled in!");
385 fc
->sb_flags
&= ~SB_POSIXACL
;
388 kfree(opts
->nls_name
);
389 opts
->nls_name
= param
->string
;
390 param
->string
= NULL
;
393 opts
->prealloc
= result
.negated
? 0 : 1;
396 opts
->nocase
= result
.negated
? 1 : 0;
399 /* Should not be here unless we forget add case. */
405 static int ntfs_fs_reconfigure(struct fs_context
*fc
)
407 struct super_block
*sb
= fc
->root
->d_sb
;
408 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
409 struct ntfs_mount_options
*new_opts
= fc
->fs_private
;
412 ro_rw
= sb_rdonly(sb
) && !(fc
->sb_flags
& SB_RDONLY
);
413 if (ro_rw
&& (sbi
->flags
& NTFS_FLAGS_NEED_REPLAY
)) {
415 "ntfs3: Couldn't remount rw because journal is not replayed. Please umount/remount instead\n");
419 new_opts
->nls
= ntfs_load_nls(new_opts
->nls_name
);
420 if (IS_ERR(new_opts
->nls
)) {
421 new_opts
->nls
= NULL
;
422 errorf(fc
, "ntfs3: Cannot load iocharset %s",
426 if (new_opts
->nls
!= sbi
->options
->nls
)
429 "ntfs3: Cannot use different iocharset when remounting!");
433 if (ro_rw
&& (sbi
->volume
.flags
& VOLUME_FLAG_DIRTY
) &&
436 "ntfs3: Volume is dirty and \"force\" flag is not set!");
440 swap(sbi
->options
, fc
->fs_private
);
445 #ifdef CONFIG_PROC_FS
446 static struct proc_dir_entry
*proc_info_root
;
451 * The content of /proc/fs/ntfs3/<dev>/volinfo
457 static int ntfs3_volinfo(struct seq_file
*m
, void *o
)
459 struct super_block
*sb
= m
->private;
460 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
462 seq_printf(m
, "ntfs%d.%d\n%u\n%zu\n", sbi
->volume
.major_ver
,
463 sbi
->volume
.minor_ver
, sbi
->cluster_size
,
464 sbi
->used
.bitmap
.nbits
);
469 static int ntfs3_volinfo_open(struct inode
*inode
, struct file
*file
)
471 return single_open(file
, ntfs3_volinfo
, pde_data(inode
));
474 /* read /proc/fs/ntfs3/<dev>/label */
475 static int ntfs3_label_show(struct seq_file
*m
, void *o
)
477 struct super_block
*sb
= m
->private;
478 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
480 seq_printf(m
, "%s\n", sbi
->volume
.label
);
485 /* write /proc/fs/ntfs3/<dev>/label */
486 static ssize_t
ntfs3_label_write(struct file
*file
, const char __user
*buffer
,
487 size_t count
, loff_t
*ppos
)
490 struct super_block
*sb
= pde_data(file_inode(file
));
491 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
493 u8
*label
= kmalloc(count
, GFP_NOFS
);
498 if (copy_from_user(label
, buffer
, ret
)) {
502 while (ret
> 0 && label
[ret
- 1] == '\n')
505 err
= ntfs_set_label(sbi
, label
, ret
);
508 ntfs_err(sb
, "failed (%d) to write label", err
);
520 static int ntfs3_label_open(struct inode
*inode
, struct file
*file
)
522 return single_open(file
, ntfs3_label_show
, pde_data(inode
));
525 static const struct proc_ops ntfs3_volinfo_fops
= {
526 .proc_read
= seq_read
,
527 .proc_lseek
= seq_lseek
,
528 .proc_release
= single_release
,
529 .proc_open
= ntfs3_volinfo_open
,
532 static const struct proc_ops ntfs3_label_fops
= {
533 .proc_read
= seq_read
,
534 .proc_lseek
= seq_lseek
,
535 .proc_release
= single_release
,
536 .proc_open
= ntfs3_label_open
,
537 .proc_write
= ntfs3_label_write
,
542 static struct kmem_cache
*ntfs_inode_cachep
;
544 static struct inode
*ntfs_alloc_inode(struct super_block
*sb
)
546 struct ntfs_inode
*ni
= alloc_inode_sb(sb
, ntfs_inode_cachep
, GFP_NOFS
);
551 memset(ni
, 0, offsetof(struct ntfs_inode
, vfs_inode
));
552 mutex_init(&ni
->ni_lock
);
553 return &ni
->vfs_inode
;
556 static void ntfs_free_inode(struct inode
*inode
)
558 struct ntfs_inode
*ni
= ntfs_i(inode
);
560 mutex_destroy(&ni
->ni_lock
);
561 kmem_cache_free(ntfs_inode_cachep
, ni
);
564 static void init_once(void *foo
)
566 struct ntfs_inode
*ni
= foo
;
568 inode_init_once(&ni
->vfs_inode
);
572 * Noinline to reduce binary size.
574 static noinline
void ntfs3_put_sbi(struct ntfs_sb_info
*sbi
)
576 wnd_close(&sbi
->mft
.bitmap
);
577 wnd_close(&sbi
->used
.bitmap
);
580 iput(&sbi
->mft
.ni
->vfs_inode
);
582 if (sbi
->security
.ni
)
583 iput(&sbi
->security
.ni
->vfs_inode
);
586 iput(&sbi
->reparse
.ni
->vfs_inode
);
589 iput(&sbi
->objid
.ni
->vfs_inode
);
592 iput(&sbi
->volume
.ni
->vfs_inode
);
594 ntfs_update_mftmirr(sbi
, 0);
596 indx_clear(&sbi
->security
.index_sii
);
597 indx_clear(&sbi
->security
.index_sdh
);
598 indx_clear(&sbi
->reparse
.index_r
);
599 indx_clear(&sbi
->objid
.index_o
);
602 static void ntfs3_free_sbi(struct ntfs_sb_info
*sbi
)
605 kvfree(ntfs_put_shared(sbi
->upcase
));
606 kfree(sbi
->def_table
);
607 kfree(sbi
->compress
.lznt
);
608 #ifdef CONFIG_NTFS3_LZX_XPRESS
609 xpress_free_decompressor(sbi
->compress
.xpress
);
610 lzx_free_decompressor(sbi
->compress
.lzx
);
615 static void ntfs_put_super(struct super_block
*sb
)
617 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
619 #ifdef CONFIG_PROC_FS
620 // Remove /proc/fs/ntfs3/..
622 remove_proc_entry("label", sbi
->procdir
);
623 remove_proc_entry("volinfo", sbi
->procdir
);
624 remove_proc_entry(sb
->s_id
, proc_info_root
);
629 /* Mark rw ntfs as clear, if possible. */
630 ntfs_set_state(sbi
, NTFS_DIRTY_CLEAR
);
634 static int ntfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
636 struct super_block
*sb
= dentry
->d_sb
;
637 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
638 struct wnd_bitmap
*wnd
= &sbi
->used
.bitmap
;
640 buf
->f_type
= sb
->s_magic
;
641 buf
->f_bsize
= sbi
->cluster_size
;
642 buf
->f_blocks
= wnd
->nbits
;
644 buf
->f_bfree
= buf
->f_bavail
= wnd_zeroes(wnd
);
645 buf
->f_fsid
.val
[0] = sbi
->volume
.ser_num
;
646 buf
->f_fsid
.val
[1] = (sbi
->volume
.ser_num
>> 32);
647 buf
->f_namelen
= NTFS_NAME_LEN
;
652 static int ntfs_show_options(struct seq_file
*m
, struct dentry
*root
)
654 struct super_block
*sb
= root
->d_sb
;
655 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
656 struct ntfs_mount_options
*opts
= sbi
->options
;
657 struct user_namespace
*user_ns
= seq_user_ns(m
);
659 seq_printf(m
, ",uid=%u", from_kuid_munged(user_ns
, opts
->fs_uid
));
660 seq_printf(m
, ",gid=%u", from_kgid_munged(user_ns
, opts
->fs_gid
));
662 seq_printf(m
, ",dmask=%04o", opts
->fs_dmask_inv
^ 0xffff);
664 seq_printf(m
, ",fmask=%04o", opts
->fs_fmask_inv
^ 0xffff);
665 if (opts
->sys_immutable
)
666 seq_puts(m
, ",sys_immutable");
668 seq_puts(m
, ",discard");
670 seq_puts(m
, ",force");
672 seq_puts(m
, ",sparse");
674 seq_puts(m
, ",nohidden");
675 if (opts
->hide_dot_files
)
676 seq_puts(m
, ",hide_dot_files");
677 if (opts
->windows_names
)
678 seq_puts(m
, ",windows_names");
680 seq_puts(m
, ",showmeta");
681 if (sb
->s_flags
& SB_POSIXACL
)
684 seq_printf(m
, ",iocharset=%s", opts
->nls
->charset
);
686 seq_puts(m
, ",iocharset=utf8");
688 seq_puts(m
, ",prealloc");
690 seq_puts(m
, ",nocase");
696 * ntfs_sync_fs - super_operations::sync_fs
698 static int ntfs_sync_fs(struct super_block
*sb
, int wait
)
701 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
702 struct ntfs_inode
*ni
;
705 ni
= sbi
->security
.ni
;
707 inode
= &ni
->vfs_inode
;
708 err2
= _ni_write_inode(inode
, wait
);
715 inode
= &ni
->vfs_inode
;
716 err2
= _ni_write_inode(inode
, wait
);
721 ni
= sbi
->reparse
.ni
;
723 inode
= &ni
->vfs_inode
;
724 err2
= _ni_write_inode(inode
, wait
);
730 ntfs_set_state(sbi
, NTFS_DIRTY_CLEAR
);
732 ntfs_update_mftmirr(sbi
, wait
);
737 static const struct super_operations ntfs_sops
= {
738 .alloc_inode
= ntfs_alloc_inode
,
739 .free_inode
= ntfs_free_inode
,
740 .evict_inode
= ntfs_evict_inode
,
741 .put_super
= ntfs_put_super
,
742 .statfs
= ntfs_statfs
,
743 .show_options
= ntfs_show_options
,
744 .sync_fs
= ntfs_sync_fs
,
745 .write_inode
= ntfs3_write_inode
,
748 static struct inode
*ntfs_export_get_inode(struct super_block
*sb
, u64 ino
,
754 ref
.low
= cpu_to_le32(ino
);
755 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
756 ref
.high
= cpu_to_le16(ino
>> 32);
760 ref
.seq
= cpu_to_le16(generation
);
762 inode
= ntfs_iget5(sb
, &ref
, NULL
);
763 if (!IS_ERR(inode
) && is_bad_inode(inode
)) {
765 inode
= ERR_PTR(-ESTALE
);
771 static struct dentry
*ntfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fid
,
772 int fh_len
, int fh_type
)
774 return generic_fh_to_dentry(sb
, fid
, fh_len
, fh_type
,
775 ntfs_export_get_inode
);
778 static struct dentry
*ntfs_fh_to_parent(struct super_block
*sb
, struct fid
*fid
,
779 int fh_len
, int fh_type
)
781 return generic_fh_to_parent(sb
, fid
, fh_len
, fh_type
,
782 ntfs_export_get_inode
);
785 /* TODO: == ntfs_sync_inode */
786 static int ntfs_nfs_commit_metadata(struct inode
*inode
)
788 return _ni_write_inode(inode
, 1);
791 static const struct export_operations ntfs_export_ops
= {
792 .fh_to_dentry
= ntfs_fh_to_dentry
,
793 .fh_to_parent
= ntfs_fh_to_parent
,
794 .get_parent
= ntfs3_get_parent
,
795 .commit_metadata
= ntfs_nfs_commit_metadata
,
799 * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb".
801 static u32
format_size_gb(const u64 bytes
, u32
*mb
)
803 /* Do simple right 30 bit shift of 64 bit value. */
804 u64 kbytes
= bytes
>> 10;
805 u32 kbytes32
= kbytes
;
807 *mb
= (100 * (kbytes32
& 0xfffff) + 0x7ffff) >> 20;
811 return (kbytes32
>> 20) | (((u32
)(kbytes
>> 32)) << 12);
814 static u32
true_sectors_per_clst(const struct NTFS_BOOT
*boot
)
816 if (boot
->sectors_per_clusters
<= 0x80)
817 return boot
->sectors_per_clusters
;
818 if (boot
->sectors_per_clusters
>= 0xf4) /* limit shift to 2MB max */
819 return 1U << (-(s8
)boot
->sectors_per_clusters
);
824 * ntfs_init_from_boot - Init internal info from on-disk boot sector.
826 * NTFS mount begins from boot - special formatted 512 bytes.
827 * There are two boots: the first and the last 512 bytes of volume.
828 * The content of boot is not changed during ntfs life.
830 * NOTE: ntfs.sys checks only first (primary) boot.
831 * chkdsk checks both boots.
833 static int ntfs_init_from_boot(struct super_block
*sb
, u32 sector_size
,
834 u64 dev_size
, struct NTFS_BOOT
**boot2
)
836 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
838 u32 mb
, gb
, boot_sector_size
, sct_per_clst
, record_size
;
839 u64 sectors
, clusters
, mlcn
, mlcn2
;
840 struct NTFS_BOOT
*boot
;
841 struct buffer_head
*bh
;
846 const char *hint
= "Primary boot";
848 sbi
->volume
.blocks
= dev_size
>> PAGE_SHIFT
;
850 bh
= ntfs_bread(sb
, 0);
856 boot
= (struct NTFS_BOOT
*)Add2Ptr(bh
->b_data
, boot_off
);
858 if (memcmp(boot
->system_id
, "NTFS ", sizeof("NTFS ") - 1)) {
859 ntfs_err(sb
, "%s signature is not NTFS.", hint
);
863 /* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/
864 /*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1])
868 boot_sector_size
= ((u32
)boot
->bytes_per_sector
[1] << 8) |
869 boot
->bytes_per_sector
[0];
870 if (boot_sector_size
< SECTOR_SIZE
||
871 !is_power_of_2(boot_sector_size
)) {
872 ntfs_err(sb
, "%s: invalid bytes per sector %u.", hint
,
877 /* cluster size: 512, 1K, 2K, 4K, ... 2M */
878 sct_per_clst
= true_sectors_per_clst(boot
);
879 if ((int)sct_per_clst
< 0 || !is_power_of_2(sct_per_clst
)) {
880 ntfs_err(sb
, "%s: invalid sectors per cluster %u.", hint
,
885 sbi
->cluster_size
= boot_sector_size
* sct_per_clst
;
886 sbi
->cluster_bits
= cluster_bits
= blksize_bits(sbi
->cluster_size
);
887 sbi
->cluster_mask
= sbi
->cluster_size
- 1;
888 sbi
->cluster_mask_inv
= ~(u64
)sbi
->cluster_mask
;
890 mlcn
= le64_to_cpu(boot
->mft_clst
);
891 mlcn2
= le64_to_cpu(boot
->mft2_clst
);
892 sectors
= le64_to_cpu(boot
->sectors_per_volume
);
894 if (mlcn
* sct_per_clst
>= sectors
|| mlcn2
* sct_per_clst
>= sectors
) {
897 "%s: start of MFT 0x%llx (0x%llx) is out of volume 0x%llx.",
898 hint
, mlcn
, mlcn2
, sectors
);
902 sbi
->record_size
= record_size
=
903 boot
->record_size
< 0 ? 1 << (-boot
->record_size
) :
904 (u32
)boot
->record_size
<< cluster_bits
;
905 sbi
->record_bits
= blksize_bits(record_size
);
906 sbi
->attr_size_tr
= (5 * record_size
>> 4); // ~320 bytes
908 /* Check MFT record size. */
909 if (record_size
< SECTOR_SIZE
|| !is_power_of_2(record_size
)) {
910 ntfs_err(sb
, "%s: invalid bytes per MFT record %u (%d).", hint
,
911 record_size
, boot
->record_size
);
915 if (record_size
> MAXIMUM_BYTES_PER_MFT
) {
916 ntfs_err(sb
, "Unsupported bytes per MFT record %u.",
921 sbi
->index_size
= boot
->index_size
< 0 ?
922 1u << (-boot
->index_size
) :
923 (u32
)boot
->index_size
<< cluster_bits
;
925 /* Check index record size. */
926 if (sbi
->index_size
< SECTOR_SIZE
|| !is_power_of_2(sbi
->index_size
)) {
927 ntfs_err(sb
, "%s: invalid bytes per index %u(%d).", hint
,
928 sbi
->index_size
, boot
->index_size
);
932 if (sbi
->index_size
> MAXIMUM_BYTES_PER_INDEX
) {
933 ntfs_err(sb
, "%s: unsupported bytes per index %u.", hint
,
938 sbi
->volume
.size
= sectors
* boot_sector_size
;
940 gb
= format_size_gb(sbi
->volume
.size
+ boot_sector_size
, &mb
);
943 * - Volume formatted and mounted with the same sector size.
944 * - Volume formatted 4K and mounted as 512.
945 * - Volume formatted 512 and mounted as 4K.
947 if (boot_sector_size
!= sector_size
) {
950 "Different NTFS sector size (%u) and media sector size (%u).",
951 boot_sector_size
, sector_size
);
952 dev_size
+= sector_size
- 1;
955 sbi
->mft
.lbo
= mlcn
<< cluster_bits
;
956 sbi
->mft
.lbo2
= mlcn2
<< cluster_bits
;
958 /* Compare boot's cluster and sector. */
959 if (sbi
->cluster_size
< boot_sector_size
) {
960 ntfs_err(sb
, "%s: invalid bytes per cluster (%u).", hint
,
965 /* Compare boot's cluster and media sector. */
966 if (sbi
->cluster_size
< sector_size
) {
967 /* No way to use ntfs_get_block in this case. */
970 "Failed to mount 'cause NTFS's cluster size (%u) is less than media sector size (%u).",
971 sbi
->cluster_size
, sector_size
);
975 sbi
->max_bytes_per_attr
=
976 record_size
- ALIGN(MFTRECORD_FIXUP_OFFSET
, 8) -
977 ALIGN(((record_size
>> SECTOR_SHIFT
) * sizeof(short)), 8) -
978 ALIGN(sizeof(enum ATTR_TYPE
), 8);
980 sbi
->volume
.ser_num
= le64_to_cpu(boot
->serial_num
);
982 /* Warning if RAW volume. */
983 if (dev_size
< sbi
->volume
.size
+ boot_sector_size
) {
986 gb0
= format_size_gb(dev_size
, &mb0
);
989 "RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only.",
991 sb
->s_flags
|= SB_RDONLY
;
994 clusters
= sbi
->volume
.size
>> cluster_bits
;
995 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
996 /* 32 bits per cluster. */
997 if (clusters
>> 32) {
1000 "NTFS %u.%02u Gb is too big to use 32 bits per cluster.",
1004 #elif BITS_PER_LONG < 64
1005 #error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS"
1008 sbi
->used
.bitmap
.nbits
= clusters
;
1010 rec
= kzalloc(record_size
, GFP_NOFS
);
1017 rec
->rhdr
.sign
= NTFS_FILE_SIGNATURE
;
1018 rec
->rhdr
.fix_off
= cpu_to_le16(MFTRECORD_FIXUP_OFFSET
);
1019 fn
= (sbi
->record_size
>> SECTOR_SHIFT
) + 1;
1020 rec
->rhdr
.fix_num
= cpu_to_le16(fn
);
1021 ao
= ALIGN(MFTRECORD_FIXUP_OFFSET
+ sizeof(short) * fn
, 8);
1022 rec
->attr_off
= cpu_to_le16(ao
);
1023 rec
->used
= cpu_to_le32(ao
+ ALIGN(sizeof(enum ATTR_TYPE
), 8));
1024 rec
->total
= cpu_to_le32(sbi
->record_size
);
1025 ((struct ATTRIB
*)Add2Ptr(rec
, ao
))->type
= ATTR_END
;
1027 sb_set_blocksize(sb
, min_t(u32
, sbi
->cluster_size
, PAGE_SIZE
));
1029 sbi
->block_mask
= sb
->s_blocksize
- 1;
1030 sbi
->blocks_per_cluster
= sbi
->cluster_size
>> sb
->s_blocksize_bits
;
1031 sbi
->volume
.blocks
= sbi
->volume
.size
>> sb
->s_blocksize_bits
;
1033 /* Maximum size for normal files. */
1034 sbi
->maxbytes
= (clusters
<< cluster_bits
) - 1;
1036 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
1037 if (clusters
>= (1ull << (64 - cluster_bits
)))
1039 sbi
->maxbytes_sparse
= -1;
1040 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
1042 /* Maximum size for sparse file. */
1043 sbi
->maxbytes_sparse
= (1ull << (cluster_bits
+ 32)) - 1;
1044 sb
->s_maxbytes
= 0xFFFFFFFFull
<< cluster_bits
;
1048 * Compute the MFT zone at two steps.
1049 * It would be nice if we are able to allocate 1/8 of
1050 * total clusters for MFT but not more then 512 MB.
1052 sbi
->zone_max
= min_t(CLST
, 0x20000000 >> cluster_bits
, clusters
>> 3);
1056 if (bh
->b_blocknr
&& !sb_rdonly(sb
)) {
1058 * Alternative boot is ok but primary is not ok.
1059 * Do not update primary boot here 'cause it may be faked boot.
1060 * Let ntfs to be mounted and update boot later.
1062 *boot2
= kmemdup(boot
, sizeof(*boot
), GFP_NOFS
| __GFP_NOWARN
);
1066 if (err
== -EINVAL
&& !bh
->b_blocknr
&& dev_size
> PAGE_SHIFT
) {
1067 u32 block_size
= min_t(u32
, sector_size
, PAGE_SIZE
);
1068 u64 lbo
= dev_size
- sizeof(*boot
);
1071 * Try alternative boot (last sector)
1075 sb_set_blocksize(sb
, block_size
);
1076 bh
= ntfs_bread(sb
, lbo
>> blksize_bits(block_size
));
1080 boot_off
= lbo
& (block_size
- 1);
1081 hint
= "Alternative boot";
1090 * ntfs_fill_super - Try to mount.
1092 static int ntfs_fill_super(struct super_block
*sb
, struct fs_context
*fc
)
1095 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
1096 struct block_device
*bdev
= sb
->s_bdev
;
1097 struct ntfs_mount_options
*options
;
1098 struct inode
*inode
;
1099 struct ntfs_inode
*ni
;
1100 size_t i
, tt
, bad_len
, bad_frags
;
1102 struct ATTRIB
*attr
;
1103 const struct VOLUME_INFO
*info
;
1104 u32 idx
, done
, bytes
;
1105 struct ATTR_DEF_ENTRY
*t
;
1108 bool ro
= sb_rdonly(sb
);
1109 struct NTFS_BOOT
*boot2
= NULL
;
1114 sbi
->options
= options
= fc
->fs_private
;
1115 fc
->fs_private
= NULL
;
1116 sb
->s_flags
|= SB_NODIRATIME
;
1117 sb
->s_magic
= 0x7366746e; // "ntfs"
1118 sb
->s_op
= &ntfs_sops
;
1119 sb
->s_export_op
= &ntfs_export_ops
;
1120 sb
->s_time_gran
= NTFS_TIME_GRAN
; // 100 nsec
1121 sb
->s_xattr
= ntfs_xattr_handlers
;
1122 sb
->s_d_op
= options
->nocase
? &ntfs_dentry_ops
: NULL
;
1124 options
->nls
= ntfs_load_nls(options
->nls_name
);
1125 if (IS_ERR(options
->nls
)) {
1126 options
->nls
= NULL
;
1127 errorf(fc
, "Cannot load nls %s", options
->nls_name
);
1132 if (bdev_max_discard_sectors(bdev
) && bdev_discard_granularity(bdev
)) {
1133 sbi
->discard_granularity
= bdev_discard_granularity(bdev
);
1134 sbi
->discard_granularity_mask_inv
=
1135 ~(u64
)(sbi
->discard_granularity
- 1);
1139 err
= ntfs_init_from_boot(sb
, bdev_logical_block_size(bdev
),
1140 bdev_nr_bytes(bdev
), &boot2
);
1145 * Load $Volume. This should be done before $LogFile
1146 * 'cause 'sbi->volume.ni' is used 'ntfs_set_state'.
1148 ref
.low
= cpu_to_le32(MFT_REC_VOL
);
1149 ref
.seq
= cpu_to_le16(MFT_REC_VOL
);
1150 inode
= ntfs_iget5(sb
, &ref
, &NAME_VOLUME
);
1151 if (IS_ERR(inode
)) {
1152 err
= PTR_ERR(inode
);
1153 ntfs_err(sb
, "Failed to load $Volume (%d).", err
);
1159 /* Load and save label (not necessary). */
1160 attr
= ni_find_attr(ni
, NULL
, NULL
, ATTR_LABEL
, NULL
, 0, NULL
, NULL
);
1163 /* It is ok if no ATTR_LABEL */
1164 } else if (!attr
->non_res
&& !is_attr_ext(attr
)) {
1165 /* $AttrDef allows labels to be up to 128 symbols. */
1166 err
= utf16s_to_utf8s(resident_data(attr
),
1167 le32_to_cpu(attr
->res
.data_size
) >> 1,
1168 UTF16_LITTLE_ENDIAN
, sbi
->volume
.label
,
1169 sizeof(sbi
->volume
.label
));
1171 sbi
->volume
.label
[0] = 0;
1173 /* Should we break mounting here? */
1175 //goto put_inode_out;
1178 attr
= ni_find_attr(ni
, attr
, NULL
, ATTR_VOL_INFO
, NULL
, 0, NULL
, NULL
);
1179 if (!attr
|| is_attr_ext(attr
) ||
1180 !(info
= resident_data_ex(attr
, SIZEOF_ATTRIBUTE_VOLUME_INFO
))) {
1181 ntfs_err(sb
, "$Volume is corrupted.");
1186 sbi
->volume
.major_ver
= info
->major_ver
;
1187 sbi
->volume
.minor_ver
= info
->minor_ver
;
1188 sbi
->volume
.flags
= info
->flags
;
1189 sbi
->volume
.ni
= ni
;
1190 if (info
->flags
& VOLUME_FLAG_DIRTY
) {
1191 sbi
->volume
.real_dirty
= true;
1192 ntfs_info(sb
, "It is recommened to use chkdsk.");
1195 /* Load $MFTMirr to estimate recs_mirr. */
1196 ref
.low
= cpu_to_le32(MFT_REC_MIRR
);
1197 ref
.seq
= cpu_to_le16(MFT_REC_MIRR
);
1198 inode
= ntfs_iget5(sb
, &ref
, &NAME_MIRROR
);
1199 if (IS_ERR(inode
)) {
1200 err
= PTR_ERR(inode
);
1201 ntfs_err(sb
, "Failed to load $MFTMirr (%d).", err
);
1205 sbi
->mft
.recs_mirr
= ntfs_up_cluster(sbi
, inode
->i_size
) >>
1210 /* Load LogFile to replay. */
1211 ref
.low
= cpu_to_le32(MFT_REC_LOG
);
1212 ref
.seq
= cpu_to_le16(MFT_REC_LOG
);
1213 inode
= ntfs_iget5(sb
, &ref
, &NAME_LOGFILE
);
1214 if (IS_ERR(inode
)) {
1215 err
= PTR_ERR(inode
);
1216 ntfs_err(sb
, "Failed to load \x24LogFile (%d).", err
);
1222 err
= ntfs_loadlog_and_replay(ni
, sbi
);
1228 if ((sbi
->flags
& NTFS_FLAGS_NEED_REPLAY
) && !ro
) {
1229 ntfs_warn(sb
, "failed to replay log file. Can't mount rw!");
1234 if ((sbi
->volume
.flags
& VOLUME_FLAG_DIRTY
) && !ro
&& !options
->force
) {
1235 ntfs_warn(sb
, "volume is dirty and \"force\" flag is not set!");
1241 ref
.low
= cpu_to_le32(MFT_REC_MFT
);
1242 ref
.seq
= cpu_to_le16(1);
1244 inode
= ntfs_iget5(sb
, &ref
, &NAME_MFT
);
1245 if (IS_ERR(inode
)) {
1246 err
= PTR_ERR(inode
);
1247 ntfs_err(sb
, "Failed to load $MFT (%d).", err
);
1253 sbi
->mft
.used
= ni
->i_valid
>> sbi
->record_bits
;
1254 tt
= inode
->i_size
>> sbi
->record_bits
;
1255 sbi
->mft
.next_free
= MFT_REC_USER
;
1257 err
= wnd_init(&sbi
->mft
.bitmap
, sb
, tt
);
1261 err
= ni_load_all_mi(ni
);
1263 ntfs_err(sb
, "Failed to load $MFT's subrecords (%d).", err
);
1270 ref
.low
= cpu_to_le32(MFT_REC_BITMAP
);
1271 ref
.seq
= cpu_to_le16(MFT_REC_BITMAP
);
1272 inode
= ntfs_iget5(sb
, &ref
, &NAME_BITMAP
);
1273 if (IS_ERR(inode
)) {
1274 err
= PTR_ERR(inode
);
1275 ntfs_err(sb
, "Failed to load $Bitmap (%d).", err
);
1279 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
1280 if (inode
->i_size
>> 32) {
1286 /* Check bitmap boundary. */
1287 tt
= sbi
->used
.bitmap
.nbits
;
1288 if (inode
->i_size
< bitmap_size(tt
)) {
1289 ntfs_err(sb
, "$Bitmap is corrupted.");
1294 err
= wnd_init(&sbi
->used
.bitmap
, sb
, tt
);
1296 ntfs_err(sb
, "Failed to initialize $Bitmap (%d).", err
);
1302 /* Compute the MFT zone. */
1303 err
= ntfs_refresh_zone(sbi
);
1305 ntfs_err(sb
, "Failed to initialize MFT zone (%d).", err
);
1309 /* Load $BadClus. */
1310 ref
.low
= cpu_to_le32(MFT_REC_BADCLUST
);
1311 ref
.seq
= cpu_to_le16(MFT_REC_BADCLUST
);
1312 inode
= ntfs_iget5(sb
, &ref
, &NAME_BADCLUS
);
1313 if (IS_ERR(inode
)) {
1314 err
= PTR_ERR(inode
);
1315 ntfs_err(sb
, "Failed to load $BadClus (%d).", err
);
1320 bad_len
= bad_frags
= 0;
1321 for (i
= 0; run_get_entry(&ni
->file
.run
, i
, &vcn
, &lcn
, &len
); i
++) {
1322 if (lcn
== SPARSE_LCN
)
1330 if (wnd_set_used_safe(&sbi
->used
.bitmap
, lcn
, len
, &tt
) || tt
) {
1331 /* Bad blocks marked as free in bitmap. */
1332 ntfs_set_state(sbi
, NTFS_DIRTY_ERROR
);
1337 * Notice about bad blocks.
1338 * In normal cases these blocks are marked as used in bitmap.
1339 * And we never allocate space in it.
1342 "Volume contains %zu bad blocks in %zu fragments.",
1343 bad_len
, bad_frags
);
1347 /* Load $AttrDef. */
1348 ref
.low
= cpu_to_le32(MFT_REC_ATTR
);
1349 ref
.seq
= cpu_to_le16(MFT_REC_ATTR
);
1350 inode
= ntfs_iget5(sb
, &ref
, &NAME_ATTRDEF
);
1351 if (IS_ERR(inode
)) {
1352 err
= PTR_ERR(inode
);
1353 ntfs_err(sb
, "Failed to load $AttrDef (%d)", err
);
1358 * Typical $AttrDef contains up to 20 entries.
1359 * Check for extremely large/small size.
1361 if (inode
->i_size
< sizeof(struct ATTR_DEF_ENTRY
) ||
1362 inode
->i_size
> 100 * sizeof(struct ATTR_DEF_ENTRY
)) {
1363 ntfs_err(sb
, "Looks like $AttrDef is corrupted (size=%llu).",
1369 bytes
= inode
->i_size
;
1370 sbi
->def_table
= t
= kmalloc(bytes
, GFP_NOFS
| __GFP_NOWARN
);
1376 for (done
= idx
= 0; done
< bytes
; done
+= PAGE_SIZE
, idx
++) {
1377 unsigned long tail
= bytes
- done
;
1378 struct page
*page
= ntfs_map_page(inode
->i_mapping
, idx
);
1381 err
= PTR_ERR(page
);
1382 ntfs_err(sb
, "Failed to read $AttrDef (%d).", err
);
1385 memcpy(Add2Ptr(t
, done
), page_address(page
),
1386 min(PAGE_SIZE
, tail
));
1387 ntfs_unmap_page(page
);
1389 if (!idx
&& ATTR_STD
!= t
->type
) {
1390 ntfs_err(sb
, "$AttrDef is corrupted.");
1397 sbi
->def_entries
= 1;
1398 done
= sizeof(struct ATTR_DEF_ENTRY
);
1399 sbi
->reparse
.max_size
= MAXIMUM_REPARSE_DATA_BUFFER_SIZE
;
1400 sbi
->ea_max_size
= 0x10000; /* default formatter value */
1402 while (done
+ sizeof(struct ATTR_DEF_ENTRY
) <= bytes
) {
1403 u32 t32
= le32_to_cpu(t
->type
);
1404 u64 sz
= le64_to_cpu(t
->max_sz
);
1406 if ((t32
& 0xF) || le32_to_cpu(t
[-1].type
) >= t32
)
1409 if (t
->type
== ATTR_REPARSE
)
1410 sbi
->reparse
.max_size
= sz
;
1411 else if (t
->type
== ATTR_EA
)
1412 sbi
->ea_max_size
= sz
;
1414 done
+= sizeof(struct ATTR_DEF_ENTRY
);
1416 sbi
->def_entries
+= 1;
1421 ref
.low
= cpu_to_le32(MFT_REC_UPCASE
);
1422 ref
.seq
= cpu_to_le16(MFT_REC_UPCASE
);
1423 inode
= ntfs_iget5(sb
, &ref
, &NAME_UPCASE
);
1424 if (IS_ERR(inode
)) {
1425 err
= PTR_ERR(inode
);
1426 ntfs_err(sb
, "Failed to load $UpCase (%d).", err
);
1430 if (inode
->i_size
!= 0x10000 * sizeof(short)) {
1432 ntfs_err(sb
, "$UpCase is corrupted.");
1436 for (idx
= 0; idx
< (0x10000 * sizeof(short) >> PAGE_SHIFT
); idx
++) {
1438 u16
*dst
= Add2Ptr(sbi
->upcase
, idx
<< PAGE_SHIFT
);
1439 struct page
*page
= ntfs_map_page(inode
->i_mapping
, idx
);
1442 err
= PTR_ERR(page
);
1443 ntfs_err(sb
, "Failed to read $UpCase (%d).", err
);
1447 src
= page_address(page
);
1450 for (i
= 0; i
< PAGE_SIZE
/ sizeof(u16
); i
++)
1451 *dst
++ = le16_to_cpu(*src
++);
1453 memcpy(dst
, src
, PAGE_SIZE
);
1455 ntfs_unmap_page(page
);
1458 shared
= ntfs_set_shared(sbi
->upcase
, 0x10000 * sizeof(short));
1459 if (shared
&& sbi
->upcase
!= shared
) {
1460 kvfree(sbi
->upcase
);
1461 sbi
->upcase
= shared
;
1466 if (is_ntfs3(sbi
)) {
1468 err
= ntfs_security_init(sbi
);
1470 ntfs_err(sb
, "Failed to initialize $Secure (%d).", err
);
1475 err
= ntfs_extend_init(sbi
);
1477 ntfs_warn(sb
, "Failed to initialize $Extend.");
1481 /* Load $Extend/$Reparse. */
1482 err
= ntfs_reparse_init(sbi
);
1484 ntfs_warn(sb
, "Failed to initialize $Extend/$Reparse.");
1488 /* Load $Extend/$ObjId. */
1489 err
= ntfs_objid_init(sbi
);
1491 ntfs_warn(sb
, "Failed to initialize $Extend/$ObjId.");
1498 ref
.low
= cpu_to_le32(MFT_REC_ROOT
);
1499 ref
.seq
= cpu_to_le16(MFT_REC_ROOT
);
1500 inode
= ntfs_iget5(sb
, &ref
, &NAME_ROOT
);
1501 if (IS_ERR(inode
)) {
1502 err
= PTR_ERR(inode
);
1503 ntfs_err(sb
, "Failed to load root (%d).", err
);
1508 * Final check. Looks like this case should never occurs.
1512 ntfs_err(sb
, "Failed to load root (%d).", err
);
1516 sb
->s_root
= d_make_root(inode
);
1524 * Alternative boot is ok but primary is not ok.
1525 * Volume is recognized as NTFS. Update primary boot.
1527 struct buffer_head
*bh0
= sb_getblk(sb
, 0);
1529 if (buffer_locked(bh0
))
1530 __wait_on_buffer(bh0
);
1533 memcpy(bh0
->b_data
, boot2
, sizeof(*boot2
));
1534 set_buffer_uptodate(bh0
);
1535 mark_buffer_dirty(bh0
);
1537 if (!sync_dirty_buffer(bh0
))
1538 ntfs_warn(sb
, "primary boot is updated");
1545 #ifdef CONFIG_PROC_FS
1546 /* Create /proc/fs/ntfs3/.. */
1547 if (proc_info_root
) {
1548 struct proc_dir_entry
*e
= proc_mkdir(sb
->s_id
, proc_info_root
);
1549 static_assert((S_IRUGO
| S_IWUSR
) == 0644);
1551 proc_create_data("volinfo", S_IRUGO
, e
,
1552 &ntfs3_volinfo_fops
, sb
);
1553 proc_create_data("label", S_IRUGO
| S_IWUSR
, e
,
1554 &ntfs3_label_fops
, sb
);
1569 void ntfs_unmap_meta(struct super_block
*sb
, CLST lcn
, CLST len
)
1571 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
1572 struct block_device
*bdev
= sb
->s_bdev
;
1573 sector_t devblock
= (u64
)lcn
* sbi
->blocks_per_cluster
;
1574 unsigned long blocks
= (u64
)len
* sbi
->blocks_per_cluster
;
1575 unsigned long cnt
= 0;
1576 unsigned long limit
= global_zone_page_state(NR_FREE_PAGES
)
1577 << (PAGE_SHIFT
- sb
->s_blocksize_bits
);
1579 if (limit
>= 0x2000)
1581 else if (limit
< 32)
1587 clean_bdev_aliases(bdev
, devblock
++, 1);
1588 if (cnt
++ >= limit
) {
1589 sync_blockdev(bdev
);
1596 * ntfs_discard - Issue a discard request (trim for SSD).
1598 int ntfs_discard(struct ntfs_sb_info
*sbi
, CLST lcn
, CLST len
)
1601 u64 lbo
, bytes
, start
, end
;
1602 struct super_block
*sb
;
1604 if (sbi
->used
.next_free_lcn
== lcn
+ len
)
1605 sbi
->used
.next_free_lcn
= lcn
;
1607 if (sbi
->flags
& NTFS_FLAGS_NODISCARD
)
1610 if (!sbi
->options
->discard
)
1613 lbo
= (u64
)lcn
<< sbi
->cluster_bits
;
1614 bytes
= (u64
)len
<< sbi
->cluster_bits
;
1616 /* Align up 'start' on discard_granularity. */
1617 start
= (lbo
+ sbi
->discard_granularity
- 1) &
1618 sbi
->discard_granularity_mask_inv
;
1619 /* Align down 'end' on discard_granularity. */
1620 end
= (lbo
+ bytes
) & sbi
->discard_granularity_mask_inv
;
1626 err
= blkdev_issue_discard(sb
->s_bdev
, start
>> 9, (end
- start
) >> 9,
1629 if (err
== -EOPNOTSUPP
)
1630 sbi
->flags
|= NTFS_FLAGS_NODISCARD
;
1635 static int ntfs_fs_get_tree(struct fs_context
*fc
)
1637 return get_tree_bdev(fc
, ntfs_fill_super
);
1641 * ntfs_fs_free - Free fs_context.
1643 * Note that this will be called after fill_super and reconfigure
1644 * even when they pass. So they have to take pointers if they pass.
1646 static void ntfs_fs_free(struct fs_context
*fc
)
1648 struct ntfs_mount_options
*opts
= fc
->fs_private
;
1649 struct ntfs_sb_info
*sbi
= fc
->s_fs_info
;
1653 ntfs3_free_sbi(sbi
);
1657 put_mount_options(opts
);
1661 static const struct fs_context_operations ntfs_context_ops
= {
1662 .parse_param
= ntfs_fs_parse_param
,
1663 .get_tree
= ntfs_fs_get_tree
,
1664 .reconfigure
= ntfs_fs_reconfigure
,
1665 .free
= ntfs_fs_free
,
1670 * ntfs_init_fs_context - Initialize sbi and opts
1672 * This will called when mount/remount. We will first initialize
1673 * options so that if remount we can use just that.
1675 static int ntfs_init_fs_context(struct fs_context
*fc
)
1677 struct ntfs_mount_options
*opts
;
1678 struct ntfs_sb_info
*sbi
;
1680 opts
= kzalloc(sizeof(struct ntfs_mount_options
), GFP_NOFS
);
1684 /* Default options. */
1685 opts
->fs_uid
= current_uid();
1686 opts
->fs_gid
= current_gid();
1687 opts
->fs_fmask_inv
= ~current_umask();
1688 opts
->fs_dmask_inv
= ~current_umask();
1690 if (fc
->purpose
== FS_CONTEXT_FOR_RECONFIGURE
)
1693 sbi
= kzalloc(sizeof(struct ntfs_sb_info
), GFP_NOFS
);
1697 sbi
->upcase
= kvmalloc(0x10000 * sizeof(short), GFP_KERNEL
);
1701 ratelimit_state_init(&sbi
->msg_ratelimit
, DEFAULT_RATELIMIT_INTERVAL
,
1702 DEFAULT_RATELIMIT_BURST
);
1704 mutex_init(&sbi
->compress
.mtx_lznt
);
1705 #ifdef CONFIG_NTFS3_LZX_XPRESS
1706 mutex_init(&sbi
->compress
.mtx_xpress
);
1707 mutex_init(&sbi
->compress
.mtx_lzx
);
1710 fc
->s_fs_info
= sbi
;
1712 fc
->fs_private
= opts
;
1713 fc
->ops
= &ntfs_context_ops
;
1723 static void ntfs3_kill_sb(struct super_block
*sb
)
1725 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
1727 kill_block_super(sb
);
1730 put_mount_options(sbi
->options
);
1731 ntfs3_free_sbi(sbi
);
1735 static struct file_system_type ntfs_fs_type
= {
1736 .owner
= THIS_MODULE
,
1738 .init_fs_context
= ntfs_init_fs_context
,
1739 .parameters
= ntfs_fs_parameters
,
1740 .kill_sb
= ntfs3_kill_sb
,
1741 .fs_flags
= FS_REQUIRES_DEV
| FS_ALLOW_IDMAP
,
1745 static int __init
init_ntfs_fs(void)
1749 pr_info("ntfs3: Max link count %u\n", NTFS_LINK_MAX
);
1751 if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL
))
1752 pr_info("ntfs3: Enabled Linux POSIX ACLs support\n");
1753 if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER
))
1755 "ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n");
1756 if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS
))
1757 pr_info("ntfs3: Read-only LZX/Xpress compression included\n");
1760 #ifdef CONFIG_PROC_FS
1761 /* Create "/proc/fs/ntfs3" */
1762 proc_info_root
= proc_mkdir("fs/ntfs3", NULL
);
1765 err
= ntfs3_init_bitmap();
1769 ntfs_inode_cachep
= kmem_cache_create(
1770 "ntfs_inode_cache", sizeof(struct ntfs_inode
), 0,
1771 (SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
| SLAB_ACCOUNT
),
1773 if (!ntfs_inode_cachep
) {
1778 err
= register_filesystem(&ntfs_fs_type
);
1784 kmem_cache_destroy(ntfs_inode_cachep
);
1786 ntfs3_exit_bitmap();
1790 static void __exit
exit_ntfs_fs(void)
1793 kmem_cache_destroy(ntfs_inode_cachep
);
1794 unregister_filesystem(&ntfs_fs_type
);
1795 ntfs3_exit_bitmap();
1797 #ifdef CONFIG_PROC_FS
1799 remove_proc_entry("fs/ntfs3", NULL
);
1804 MODULE_LICENSE("GPL");
1805 MODULE_DESCRIPTION("ntfs3 read/write filesystem");
1806 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1807 MODULE_INFO(behaviour
, "Enabled Linux POSIX ACLs support");
1809 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
1812 "Warning: Activated 64 bits per cluster. Windows does not support this");
1814 #ifdef CONFIG_NTFS3_LZX_XPRESS
1815 MODULE_INFO(compression
, "Read-only lzx/xpress compression included");
1818 MODULE_AUTHOR("Konstantin Komarov");
1819 MODULE_ALIAS_FS("ntfs3");
1821 module_init(init_ntfs_fs
);
1822 module_exit(exit_ntfs_fs
);