xref: /openbmc/linux/fs/ntfs3/super.c (revision 86cd4631)
182cae269SKonstantin Komarov // SPDX-License-Identifier: GPL-2.0
282cae269SKonstantin Komarov /*
382cae269SKonstantin Komarov  *
482cae269SKonstantin Komarov  * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
582cae269SKonstantin Komarov  *
682cae269SKonstantin Komarov  *
782cae269SKonstantin Komarov  *                 terminology
882cae269SKonstantin Komarov  *
982cae269SKonstantin Komarov  * cluster - allocation unit     - 512,1K,2K,4K,...,2M
10e8b8e97fSKari Argillander  * vcn - virtual cluster number  - Offset inside the file in clusters.
11e8b8e97fSKari Argillander  * vbo - virtual byte offset     - Offset inside the file in bytes.
12e8b8e97fSKari Argillander  * lcn - logical cluster number  - 0 based cluster in clusters heap.
13e8b8e97fSKari Argillander  * lbo - logical byte offset     - Absolute position inside volume.
14e8b8e97fSKari Argillander  * run - maps VCN to LCN         - Stored in attributes in packed form.
15e8b8e97fSKari Argillander  * attr - attribute segment      - std/name/data etc records inside MFT.
16e8b8e97fSKari Argillander  * mi  - MFT inode               - One MFT record(usually 1024 bytes or 4K), consists of attributes.
17e8b8e97fSKari Argillander  * ni  - NTFS inode              - Extends linux inode. consists of one or more mft inodes.
18e8b8e97fSKari Argillander  * index - unit inside directory - 2K, 4K, <=page size, does not depend on cluster size.
1982cae269SKonstantin Komarov  *
2082cae269SKonstantin Komarov  * WSL - Windows Subsystem for Linux
2182cae269SKonstantin Komarov  * https://docs.microsoft.com/en-us/windows/wsl/file-permissions
2282cae269SKonstantin Komarov  * It stores uid/gid/mode/dev in xattr
2382cae269SKonstantin Komarov  *
24bd6ae049SKonstantin Komarov  * ntfs allows up to 2^64 clusters per volume.
25bd6ae049SKonstantin Komarov  * It means you should use 64 bits lcn to operate with ntfs.
26bd6ae049SKonstantin Komarov  * Implementation of ntfs.sys uses only 32 bits lcn.
27bd6ae049SKonstantin Komarov  * Default ntfs3 uses 32 bits lcn too.
28bd6ae049SKonstantin Komarov  * ntfs3 built with CONFIG_NTFS3_64BIT_CLUSTER (ntfs3_64) uses 64 bits per lcn.
29bd6ae049SKonstantin Komarov  *
30bd6ae049SKonstantin Komarov  *
31bd6ae049SKonstantin Komarov  *     ntfs limits, cluster size is 4K (2^12)
32bd6ae049SKonstantin Komarov  * -----------------------------------------------------------------------------
33bd6ae049SKonstantin Komarov  * | Volume size   | Clusters | ntfs.sys | ntfs3  | ntfs3_64 | mkntfs | chkdsk |
34bd6ae049SKonstantin Komarov  * -----------------------------------------------------------------------------
35bd6ae049SKonstantin Komarov  * | < 16T, 2^44   |  < 2^32  |  yes     |  yes   |   yes    |  yes   |  yes   |
36bd6ae049SKonstantin Komarov  * | > 16T, 2^44   |  > 2^32  |  no      |  no    |   yes    |  yes   |  yes   |
37bd6ae049SKonstantin Komarov  * ----------------------------------------------------------|------------------
38bd6ae049SKonstantin Komarov  *
39bd6ae049SKonstantin Komarov  * To mount large volumes as ntfs one should use large cluster size (up to 2M)
40bd6ae049SKonstantin Komarov  * The maximum volume size in this case is 2^32 * 2^21 = 2^53 = 8P
41bd6ae049SKonstantin Komarov  *
4296de65a9SKonstantin Komarov  *     ntfs limits, cluster size is 2M (2^21)
43bd6ae049SKonstantin Komarov  * -----------------------------------------------------------------------------
4496de65a9SKonstantin Komarov  * | < 8P, 2^53    |  < 2^32  |  yes     |  yes   |   yes    |  yes   |  yes   |
4596de65a9SKonstantin Komarov  * | > 8P, 2^53    |  > 2^32  |  no      |  no    |   yes    |  yes   |  yes   |
46bd6ae049SKonstantin Komarov  * ----------------------------------------------------------|------------------
47bd6ae049SKonstantin Komarov  *
4882cae269SKonstantin Komarov  */
4982cae269SKonstantin Komarov 
5082cae269SKonstantin Komarov #include <linux/blkdev.h>
5182cae269SKonstantin Komarov #include <linux/buffer_head.h>
5282cae269SKonstantin Komarov #include <linux/exportfs.h>
5382cae269SKonstantin Komarov #include <linux/fs.h>
54610f8f5aSKari Argillander #include <linux/fs_context.h>
55610f8f5aSKari Argillander #include <linux/fs_parser.h>
56528c9b3dSKari Argillander #include <linux/log2.h>
57cd39981fSKonstantin Komarov #include <linux/minmax.h>
5882cae269SKonstantin Komarov #include <linux/module.h>
5982cae269SKonstantin Komarov #include <linux/nls.h>
607832e123SKonstantin Komarov #include <linux/proc_fs.h>
6182cae269SKonstantin Komarov #include <linux/seq_file.h>
6282cae269SKonstantin Komarov #include <linux/statfs.h>
6382cae269SKonstantin Komarov 
6482cae269SKonstantin Komarov #include "debug.h"
6582cae269SKonstantin Komarov #include "ntfs.h"
6682cae269SKonstantin Komarov #include "ntfs_fs.h"
6782cae269SKonstantin Komarov #ifdef CONFIG_NTFS3_LZX_XPRESS
6882cae269SKonstantin Komarov #include "lib/lib.h"
6982cae269SKonstantin Komarov #endif
7082cae269SKonstantin Komarov 
7182cae269SKonstantin Komarov #ifdef CONFIG_PRINTK
7282cae269SKonstantin Komarov /*
73e8b8e97fSKari Argillander  * ntfs_printk - Trace warnings/notices/errors.
74e8b8e97fSKari Argillander  *
7582cae269SKonstantin Komarov  * Thanks Joe Perches <joe@perches.com> for implementation
7682cae269SKonstantin Komarov  */
ntfs_printk(const struct super_block * sb,const char * fmt,...)7782cae269SKonstantin Komarov void ntfs_printk(const struct super_block *sb, const char *fmt, ...)
7882cae269SKonstantin Komarov {
7982cae269SKonstantin Komarov 	struct va_format vaf;
8082cae269SKonstantin Komarov 	va_list args;
8182cae269SKonstantin Komarov 	int level;
8282cae269SKonstantin Komarov 	struct ntfs_sb_info *sbi = sb->s_fs_info;
8382cae269SKonstantin Komarov 
84e8b8e97fSKari Argillander 	/* Should we use different ratelimits for warnings/notices/errors? */
8582cae269SKonstantin Komarov 	if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
8682cae269SKonstantin Komarov 		return;
8782cae269SKonstantin Komarov 
8882cae269SKonstantin Komarov 	va_start(args, fmt);
8982cae269SKonstantin Komarov 
9082cae269SKonstantin Komarov 	level = printk_get_level(fmt);
9182cae269SKonstantin Komarov 	vaf.fmt = printk_skip_level(fmt);
9282cae269SKonstantin Komarov 	vaf.va = &args;
9382cae269SKonstantin Komarov 	printk("%c%cntfs3: %s: %pV\n", KERN_SOH_ASCII, level, sb->s_id, &vaf);
9482cae269SKonstantin Komarov 
9582cae269SKonstantin Komarov 	va_end(args);
9682cae269SKonstantin Komarov }
9782cae269SKonstantin Komarov 
9882cae269SKonstantin Komarov static char s_name_buf[512];
99e8b8e97fSKari Argillander static atomic_t s_name_buf_cnt = ATOMIC_INIT(1); // 1 means 'free s_name_buf'.
10082cae269SKonstantin Komarov 
101e8b8e97fSKari Argillander /*
102e8b8e97fSKari Argillander  * ntfs_inode_printk
103e8b8e97fSKari Argillander  *
104e8b8e97fSKari Argillander  * Print warnings/notices/errors about inode using name or inode number.
105e8b8e97fSKari Argillander  */
ntfs_inode_printk(struct inode * inode,const char * fmt,...)10682cae269SKonstantin Komarov void ntfs_inode_printk(struct inode *inode, const char *fmt, ...)
10782cae269SKonstantin Komarov {
10882cae269SKonstantin Komarov 	struct super_block *sb = inode->i_sb;
10982cae269SKonstantin Komarov 	struct ntfs_sb_info *sbi = sb->s_fs_info;
11082cae269SKonstantin Komarov 	char *name;
11182cae269SKonstantin Komarov 	va_list args;
11282cae269SKonstantin Komarov 	struct va_format vaf;
11382cae269SKonstantin Komarov 	int level;
11482cae269SKonstantin Komarov 
11582cae269SKonstantin Komarov 	if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
11682cae269SKonstantin Komarov 		return;
11782cae269SKonstantin Komarov 
118e8b8e97fSKari Argillander 	/* Use static allocated buffer, if possible. */
11996de65a9SKonstantin Komarov 	name = atomic_dec_and_test(&s_name_buf_cnt) ?
12096de65a9SKonstantin Komarov 		       s_name_buf :
12196de65a9SKonstantin Komarov 		       kmalloc(sizeof(s_name_buf), GFP_NOFS);
12282cae269SKonstantin Komarov 
12382cae269SKonstantin Komarov 	if (name) {
12482cae269SKonstantin Komarov 		struct dentry *de = d_find_alias(inode);
12582cae269SKonstantin Komarov 		const u32 name_len = ARRAY_SIZE(s_name_buf) - 1;
12682cae269SKonstantin Komarov 
12782cae269SKonstantin Komarov 		if (de) {
12882cae269SKonstantin Komarov 			spin_lock(&de->d_lock);
12982cae269SKonstantin Komarov 			snprintf(name, name_len, " \"%s\"", de->d_name.name);
13082cae269SKonstantin Komarov 			spin_unlock(&de->d_lock);
131e8b8e97fSKari Argillander 			name[name_len] = 0; /* To be sure. */
13282cae269SKonstantin Komarov 		} else {
13382cae269SKonstantin Komarov 			name[0] = 0;
13482cae269SKonstantin Komarov 		}
135e8b8e97fSKari Argillander 		dput(de); /* Cocci warns if placed in branch "if (de)" */
13682cae269SKonstantin Komarov 	}
13782cae269SKonstantin Komarov 
13882cae269SKonstantin Komarov 	va_start(args, fmt);
13982cae269SKonstantin Komarov 
14082cae269SKonstantin Komarov 	level = printk_get_level(fmt);
14182cae269SKonstantin Komarov 	vaf.fmt = printk_skip_level(fmt);
14282cae269SKonstantin Komarov 	vaf.va = &args;
14382cae269SKonstantin Komarov 
14482cae269SKonstantin Komarov 	printk("%c%cntfs3: %s: ino=%lx,%s %pV\n", KERN_SOH_ASCII, level,
14582cae269SKonstantin Komarov 	       sb->s_id, inode->i_ino, name ? name : "", &vaf);
14682cae269SKonstantin Komarov 
14782cae269SKonstantin Komarov 	va_end(args);
14882cae269SKonstantin Komarov 
14982cae269SKonstantin Komarov 	atomic_inc(&s_name_buf_cnt);
15082cae269SKonstantin Komarov 	if (name != s_name_buf)
15182cae269SKonstantin Komarov 		kfree(name);
15282cae269SKonstantin Komarov }
15382cae269SKonstantin Komarov #endif
15482cae269SKonstantin Komarov 
15582cae269SKonstantin Komarov /*
15682cae269SKonstantin Komarov  * Shared memory struct.
15782cae269SKonstantin Komarov  *
158e8b8e97fSKari Argillander  * On-disk ntfs's upcase table is created by ntfs formatter.
159e8b8e97fSKari Argillander  * 'upcase' table is 128K bytes of memory.
160e8b8e97fSKari Argillander  * We should read it into memory when mounting.
161e8b8e97fSKari Argillander  * Several ntfs volumes likely use the same 'upcase' table.
162e8b8e97fSKari Argillander  * It is good idea to share in-memory 'upcase' table between different volumes.
163e8b8e97fSKari Argillander  * Unfortunately winxp/vista/win7 use different upcase tables.
16482cae269SKonstantin Komarov  */
16582cae269SKonstantin Komarov static DEFINE_SPINLOCK(s_shared_lock);
16682cae269SKonstantin Komarov 
16782cae269SKonstantin Komarov static struct {
16882cae269SKonstantin Komarov 	void *ptr;
16982cae269SKonstantin Komarov 	u32 len;
17082cae269SKonstantin Komarov 	int cnt;
17182cae269SKonstantin Komarov } s_shared[8];
17282cae269SKonstantin Komarov 
17382cae269SKonstantin Komarov /*
17482cae269SKonstantin Komarov  * ntfs_set_shared
17582cae269SKonstantin Komarov  *
176e8b8e97fSKari Argillander  * Return:
177e8b8e97fSKari Argillander  * * @ptr - If pointer was saved in shared memory.
178e8b8e97fSKari Argillander  * * NULL - If pointer was not shared.
17982cae269SKonstantin Komarov  */
ntfs_set_shared(void * ptr,u32 bytes)18082cae269SKonstantin Komarov void *ntfs_set_shared(void *ptr, u32 bytes)
18182cae269SKonstantin Komarov {
18282cae269SKonstantin Komarov 	void *ret = NULL;
18382cae269SKonstantin Komarov 	int i, j = -1;
18482cae269SKonstantin Komarov 
18582cae269SKonstantin Komarov 	spin_lock(&s_shared_lock);
18682cae269SKonstantin Komarov 	for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
18782cae269SKonstantin Komarov 		if (!s_shared[i].cnt) {
18882cae269SKonstantin Komarov 			j = i;
18982cae269SKonstantin Komarov 		} else if (bytes == s_shared[i].len &&
19082cae269SKonstantin Komarov 			   !memcmp(s_shared[i].ptr, ptr, bytes)) {
19182cae269SKonstantin Komarov 			s_shared[i].cnt += 1;
19282cae269SKonstantin Komarov 			ret = s_shared[i].ptr;
19382cae269SKonstantin Komarov 			break;
19482cae269SKonstantin Komarov 		}
19582cae269SKonstantin Komarov 	}
19682cae269SKonstantin Komarov 
19782cae269SKonstantin Komarov 	if (!ret && j != -1) {
19882cae269SKonstantin Komarov 		s_shared[j].ptr = ptr;
19982cae269SKonstantin Komarov 		s_shared[j].len = bytes;
20082cae269SKonstantin Komarov 		s_shared[j].cnt = 1;
20182cae269SKonstantin Komarov 		ret = ptr;
20282cae269SKonstantin Komarov 	}
20382cae269SKonstantin Komarov 	spin_unlock(&s_shared_lock);
20482cae269SKonstantin Komarov 
20582cae269SKonstantin Komarov 	return ret;
20682cae269SKonstantin Komarov }
20782cae269SKonstantin Komarov 
20882cae269SKonstantin Komarov /*
20982cae269SKonstantin Komarov  * ntfs_put_shared
21082cae269SKonstantin Komarov  *
211e8b8e97fSKari Argillander  * Return:
212e8b8e97fSKari Argillander  * * @ptr - If pointer is not shared anymore.
213e8b8e97fSKari Argillander  * * NULL - If pointer is still shared.
21482cae269SKonstantin Komarov  */
ntfs_put_shared(void * ptr)21582cae269SKonstantin Komarov void *ntfs_put_shared(void *ptr)
21682cae269SKonstantin Komarov {
21782cae269SKonstantin Komarov 	void *ret = ptr;
21882cae269SKonstantin Komarov 	int i;
21982cae269SKonstantin Komarov 
22082cae269SKonstantin Komarov 	spin_lock(&s_shared_lock);
22182cae269SKonstantin Komarov 	for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
22282cae269SKonstantin Komarov 		if (s_shared[i].cnt && s_shared[i].ptr == ptr) {
22382cae269SKonstantin Komarov 			if (--s_shared[i].cnt)
22482cae269SKonstantin Komarov 				ret = NULL;
22582cae269SKonstantin Komarov 			break;
22682cae269SKonstantin Komarov 		}
22782cae269SKonstantin Komarov 	}
22882cae269SKonstantin Komarov 	spin_unlock(&s_shared_lock);
22982cae269SKonstantin Komarov 
23082cae269SKonstantin Komarov 	return ret;
23182cae269SKonstantin Komarov }
23282cae269SKonstantin Komarov 
put_mount_options(struct ntfs_mount_options * options)233610f8f5aSKari Argillander static inline void put_mount_options(struct ntfs_mount_options *options)
23482cae269SKonstantin Komarov {
235610f8f5aSKari Argillander 	kfree(options->nls_name);
23682cae269SKonstantin Komarov 	unload_nls(options->nls);
237610f8f5aSKari Argillander 	kfree(options);
23882cae269SKonstantin Komarov }
23982cae269SKonstantin Komarov 
24082cae269SKonstantin Komarov enum Opt {
24182cae269SKonstantin Komarov 	Opt_uid,
24282cae269SKonstantin Komarov 	Opt_gid,
24382cae269SKonstantin Komarov 	Opt_umask,
24482cae269SKonstantin Komarov 	Opt_dmask,
24582cae269SKonstantin Komarov 	Opt_fmask,
24682cae269SKonstantin Komarov 	Opt_immutable,
24782cae269SKonstantin Komarov 	Opt_discard,
24882cae269SKonstantin Komarov 	Opt_force,
24982cae269SKonstantin Komarov 	Opt_sparse,
25082cae269SKonstantin Komarov 	Opt_nohidden,
251098250dbSKonstantin Komarov 	Opt_hide_dot_files,
2521d07a9dfSDaniel Pinto 	Opt_windows_names,
25382cae269SKonstantin Komarov 	Opt_showmeta,
25482cae269SKonstantin Komarov 	Opt_acl,
255e274cde8SKari Argillander 	Opt_iocharset,
25682cae269SKonstantin Komarov 	Opt_prealloc,
257a3a956c7SKonstantin Komarov 	Opt_nocase,
25882cae269SKonstantin Komarov 	Opt_err,
25982cae269SKonstantin Komarov };
26082cae269SKonstantin Komarov 
261f0377761SKonstantin Komarov // clang-format off
262610f8f5aSKari Argillander static const struct fs_parameter_spec ntfs_fs_parameters[] = {
263610f8f5aSKari Argillander 	fsparam_u32("uid",			Opt_uid),
264610f8f5aSKari Argillander 	fsparam_u32("gid",			Opt_gid),
265610f8f5aSKari Argillander 	fsparam_u32oct("umask",			Opt_umask),
266610f8f5aSKari Argillander 	fsparam_u32oct("dmask",			Opt_dmask),
267610f8f5aSKari Argillander 	fsparam_u32oct("fmask",			Opt_fmask),
268610f8f5aSKari Argillander 	fsparam_flag_no("sys_immutable",	Opt_immutable),
269610f8f5aSKari Argillander 	fsparam_flag_no("discard",		Opt_discard),
270610f8f5aSKari Argillander 	fsparam_flag_no("force",		Opt_force),
271610f8f5aSKari Argillander 	fsparam_flag_no("sparse",		Opt_sparse),
2729d1939f4SKari Argillander 	fsparam_flag_no("hidden",		Opt_nohidden),
273dc0fcc99SDaniel Pinto 	fsparam_flag_no("hide_dot_files",	Opt_hide_dot_files),
2741d07a9dfSDaniel Pinto 	fsparam_flag_no("windows_names",	Opt_windows_names),
275610f8f5aSKari Argillander 	fsparam_flag_no("showmeta",		Opt_showmeta),
27616b3dbfbSKonstantin Komarov 	fsparam_flag_no("acl",			Opt_acl),
27716b3dbfbSKonstantin Komarov 	fsparam_string("iocharset",		Opt_iocharset),
278610f8f5aSKari Argillander 	fsparam_flag_no("prealloc",		Opt_prealloc),
279a3a956c7SKonstantin Komarov 	fsparam_flag_no("nocase",		Opt_nocase),
280610f8f5aSKari Argillander 	{}
28182cae269SKonstantin Komarov };
282f0377761SKonstantin Komarov // clang-format on
28382cae269SKonstantin Komarov 
284610f8f5aSKari Argillander /*
285610f8f5aSKari Argillander  * Load nls table or if @nls is utf8 then return NULL.
286f0377761SKonstantin Komarov  *
287f0377761SKonstantin Komarov  * It is good idea to use here "const char *nls".
288f0377761SKonstantin Komarov  * But load_nls accepts "char*".
289610f8f5aSKari Argillander  */
ntfs_load_nls(char * nls)290610f8f5aSKari Argillander static struct nls_table *ntfs_load_nls(char *nls)
29182cae269SKonstantin Komarov {
292610f8f5aSKari Argillander 	struct nls_table *ret;
29382cae269SKonstantin Komarov 
294610f8f5aSKari Argillander 	if (!nls)
295610f8f5aSKari Argillander 		nls = CONFIG_NLS_DEFAULT;
29682cae269SKonstantin Komarov 
297610f8f5aSKari Argillander 	if (strcmp(nls, "utf8") == 0)
298610f8f5aSKari Argillander 		return NULL;
29982cae269SKonstantin Komarov 
300610f8f5aSKari Argillander 	if (strcmp(nls, CONFIG_NLS_DEFAULT) == 0)
301610f8f5aSKari Argillander 		return load_nls_default();
30282cae269SKonstantin Komarov 
303610f8f5aSKari Argillander 	ret = load_nls(nls);
304610f8f5aSKari Argillander 	if (ret)
305610f8f5aSKari Argillander 		return ret;
30682cae269SKonstantin Komarov 
307610f8f5aSKari Argillander 	return ERR_PTR(-EINVAL);
308610f8f5aSKari Argillander }
309610f8f5aSKari Argillander 
ntfs_fs_parse_param(struct fs_context * fc,struct fs_parameter * param)310610f8f5aSKari Argillander static int ntfs_fs_parse_param(struct fs_context *fc,
311610f8f5aSKari Argillander 			       struct fs_parameter *param)
312610f8f5aSKari Argillander {
313610f8f5aSKari Argillander 	struct ntfs_mount_options *opts = fc->fs_private;
314610f8f5aSKari Argillander 	struct fs_parse_result result;
315610f8f5aSKari Argillander 	int opt;
316610f8f5aSKari Argillander 
317610f8f5aSKari Argillander 	opt = fs_parse(fc, ntfs_fs_parameters, param, &result);
318610f8f5aSKari Argillander 	if (opt < 0)
319610f8f5aSKari Argillander 		return opt;
320610f8f5aSKari Argillander 
321610f8f5aSKari Argillander 	switch (opt) {
32282cae269SKonstantin Komarov 	case Opt_uid:
323610f8f5aSKari Argillander 		opts->fs_uid = make_kuid(current_user_ns(), result.uint_32);
32482cae269SKonstantin Komarov 		if (!uid_valid(opts->fs_uid))
325610f8f5aSKari Argillander 			return invalf(fc, "ntfs3: Invalid value for uid.");
32682cae269SKonstantin Komarov 		break;
32782cae269SKonstantin Komarov 	case Opt_gid:
328610f8f5aSKari Argillander 		opts->fs_gid = make_kgid(current_user_ns(), result.uint_32);
32982cae269SKonstantin Komarov 		if (!gid_valid(opts->fs_gid))
330610f8f5aSKari Argillander 			return invalf(fc, "ntfs3: Invalid value for gid.");
33182cae269SKonstantin Komarov 		break;
33282cae269SKonstantin Komarov 	case Opt_umask:
333610f8f5aSKari Argillander 		if (result.uint_32 & ~07777)
334610f8f5aSKari Argillander 			return invalf(fc, "ntfs3: Invalid value for umask.");
335610f8f5aSKari Argillander 		opts->fs_fmask_inv = ~result.uint_32;
336610f8f5aSKari Argillander 		opts->fs_dmask_inv = ~result.uint_32;
337610f8f5aSKari Argillander 		opts->fmask = 1;
338610f8f5aSKari Argillander 		opts->dmask = 1;
33982cae269SKonstantin Komarov 		break;
34082cae269SKonstantin Komarov 	case Opt_dmask:
341610f8f5aSKari Argillander 		if (result.uint_32 & ~07777)
342610f8f5aSKari Argillander 			return invalf(fc, "ntfs3: Invalid value for dmask.");
343610f8f5aSKari Argillander 		opts->fs_dmask_inv = ~result.uint_32;
34482cae269SKonstantin Komarov 		opts->dmask = 1;
34582cae269SKonstantin Komarov 		break;
34682cae269SKonstantin Komarov 	case Opt_fmask:
347610f8f5aSKari Argillander 		if (result.uint_32 & ~07777)
348610f8f5aSKari Argillander 			return invalf(fc, "ntfs3: Invalid value for fmask.");
349610f8f5aSKari Argillander 		opts->fs_fmask_inv = ~result.uint_32;
35082cae269SKonstantin Komarov 		opts->fmask = 1;
35182cae269SKonstantin Komarov 		break;
352610f8f5aSKari Argillander 	case Opt_immutable:
353610f8f5aSKari Argillander 		opts->sys_immutable = result.negated ? 0 : 1;
354610f8f5aSKari Argillander 		break;
35582cae269SKonstantin Komarov 	case Opt_discard:
356610f8f5aSKari Argillander 		opts->discard = result.negated ? 0 : 1;
35782cae269SKonstantin Komarov 		break;
35882cae269SKonstantin Komarov 	case Opt_force:
359610f8f5aSKari Argillander 		opts->force = result.negated ? 0 : 1;
36082cae269SKonstantin Komarov 		break;
36182cae269SKonstantin Komarov 	case Opt_sparse:
362610f8f5aSKari Argillander 		opts->sparse = result.negated ? 0 : 1;
36382cae269SKonstantin Komarov 		break;
36482cae269SKonstantin Komarov 	case Opt_nohidden:
3659d1939f4SKari Argillander 		opts->nohidden = result.negated ? 1 : 0;
36682cae269SKonstantin Komarov 		break;
367098250dbSKonstantin Komarov 	case Opt_hide_dot_files:
3684c9ba192SDaniel Pinto 		opts->hide_dot_files = result.negated ? 0 : 1;
369098250dbSKonstantin Komarov 		break;
3701d07a9dfSDaniel Pinto 	case Opt_windows_names:
3711d07a9dfSDaniel Pinto 		opts->windows_names = result.negated ? 0 : 1;
3721d07a9dfSDaniel Pinto 		break;
37316b3dbfbSKonstantin Komarov 	case Opt_showmeta:
37416b3dbfbSKonstantin Komarov 		opts->showmeta = result.negated ? 0 : 1;
37516b3dbfbSKonstantin Komarov 		break;
37682cae269SKonstantin Komarov 	case Opt_acl:
377610f8f5aSKari Argillander 		if (!result.negated)
37882cae269SKonstantin Komarov #ifdef CONFIG_NTFS3_FS_POSIX_ACL
379610f8f5aSKari Argillander 			fc->sb_flags |= SB_POSIXACL;
38082cae269SKonstantin Komarov #else
38196de65a9SKonstantin Komarov 			return invalf(
38296de65a9SKonstantin Komarov 				fc, "ntfs3: Support for ACL not compiled in!");
38382cae269SKonstantin Komarov #endif
384610f8f5aSKari Argillander 		else
385610f8f5aSKari Argillander 			fc->sb_flags &= ~SB_POSIXACL;
386610f8f5aSKari Argillander 		break;
387e274cde8SKari Argillander 	case Opt_iocharset:
388610f8f5aSKari Argillander 		kfree(opts->nls_name);
389610f8f5aSKari Argillander 		opts->nls_name = param->string;
390610f8f5aSKari Argillander 		param->string = NULL;
39182cae269SKonstantin Komarov 		break;
39282cae269SKonstantin Komarov 	case Opt_prealloc:
393610f8f5aSKari Argillander 		opts->prealloc = result.negated ? 0 : 1;
39482cae269SKonstantin Komarov 		break;
395a3a956c7SKonstantin Komarov 	case Opt_nocase:
396a3a956c7SKonstantin Komarov 		opts->nocase = result.negated ? 1 : 0;
397a3a956c7SKonstantin Komarov 		break;
39882cae269SKonstantin Komarov 	default:
399610f8f5aSKari Argillander 		/* Should not be here unless we forget add case. */
40082cae269SKonstantin Komarov 		return -EINVAL;
40182cae269SKonstantin Komarov 	}
40282cae269SKonstantin Komarov 	return 0;
40382cae269SKonstantin Komarov }
40482cae269SKonstantin Komarov 
ntfs_fs_reconfigure(struct fs_context * fc)405610f8f5aSKari Argillander static int ntfs_fs_reconfigure(struct fs_context *fc)
40682cae269SKonstantin Komarov {
407610f8f5aSKari Argillander 	struct super_block *sb = fc->root->d_sb;
40882cae269SKonstantin Komarov 	struct ntfs_sb_info *sbi = sb->s_fs_info;
409610f8f5aSKari Argillander 	struct ntfs_mount_options *new_opts = fc->fs_private;
410610f8f5aSKari Argillander 	int ro_rw;
41182cae269SKonstantin Komarov 
412610f8f5aSKari Argillander 	ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY);
41382cae269SKonstantin Komarov 	if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) {
41496de65a9SKonstantin Komarov 		errorf(fc,
41596de65a9SKonstantin Komarov 		       "ntfs3: Couldn't remount rw because journal is not replayed. Please umount/remount instead\n");
416610f8f5aSKari Argillander 		return -EINVAL;
41782cae269SKonstantin Komarov 	}
41882cae269SKonstantin Komarov 
419610f8f5aSKari Argillander 	new_opts->nls = ntfs_load_nls(new_opts->nls_name);
420610f8f5aSKari Argillander 	if (IS_ERR(new_opts->nls)) {
421610f8f5aSKari Argillander 		new_opts->nls = NULL;
42296de65a9SKonstantin Komarov 		errorf(fc, "ntfs3: Cannot load iocharset %s",
42396de65a9SKonstantin Komarov 		       new_opts->nls_name);
424610f8f5aSKari Argillander 		return -EINVAL;
425610f8f5aSKari Argillander 	}
426610f8f5aSKari Argillander 	if (new_opts->nls != sbi->options->nls)
42796de65a9SKonstantin Komarov 		return invalf(
42896de65a9SKonstantin Komarov 			fc,
42996de65a9SKonstantin Komarov 			"ntfs3: Cannot use different iocharset when remounting!");
430610f8f5aSKari Argillander 
43182cae269SKonstantin Komarov 	sync_filesystem(sb);
43282cae269SKonstantin Komarov 
43382cae269SKonstantin Komarov 	if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) &&
434610f8f5aSKari Argillander 	    !new_opts->force) {
43596de65a9SKonstantin Komarov 		errorf(fc,
43696de65a9SKonstantin Komarov 		       "ntfs3: Volume is dirty and \"force\" flag is not set!");
437610f8f5aSKari Argillander 		return -EINVAL;
43882cae269SKonstantin Komarov 	}
43982cae269SKonstantin Komarov 
440cd39981fSKonstantin Komarov 	swap(sbi->options, fc->fs_private);
44182cae269SKonstantin Komarov 
442610f8f5aSKari Argillander 	return 0;
44382cae269SKonstantin Komarov }
44482cae269SKonstantin Komarov 
4457832e123SKonstantin Komarov #ifdef CONFIG_PROC_FS
4467832e123SKonstantin Komarov static struct proc_dir_entry *proc_info_root;
4477832e123SKonstantin Komarov 
4487832e123SKonstantin Komarov /*
4497832e123SKonstantin Komarov  * ntfs3_volinfo:
4507832e123SKonstantin Komarov  *
4517832e123SKonstantin Komarov  * The content of /proc/fs/ntfs3/<dev>/volinfo
4527832e123SKonstantin Komarov  *
4537832e123SKonstantin Komarov  * ntfs3.1
4547832e123SKonstantin Komarov  * cluster size
4557832e123SKonstantin Komarov  * number of clusters
456d27e202bSKonstantin Komarov  * total number of mft records
457d27e202bSKonstantin Komarov  * number of used mft records ~= number of files + folders
458d27e202bSKonstantin Komarov  * real state of ntfs "dirty"/"clean"
459d27e202bSKonstantin Komarov  * current state of ntfs "dirty"/"clean"
4607832e123SKonstantin Komarov */
ntfs3_volinfo(struct seq_file * m,void * o)4617832e123SKonstantin Komarov static int ntfs3_volinfo(struct seq_file *m, void *o)
4627832e123SKonstantin Komarov {
4637832e123SKonstantin Komarov 	struct super_block *sb = m->private;
4647832e123SKonstantin Komarov 	struct ntfs_sb_info *sbi = sb->s_fs_info;
4657832e123SKonstantin Komarov 
466d27e202bSKonstantin Komarov 	seq_printf(m, "ntfs%d.%d\n%u\n%zu\n\%zu\n%zu\n%s\n%s\n",
467d27e202bSKonstantin Komarov 		   sbi->volume.major_ver, sbi->volume.minor_ver,
468d27e202bSKonstantin Komarov 		   sbi->cluster_size, sbi->used.bitmap.nbits,
469d27e202bSKonstantin Komarov 		   sbi->mft.bitmap.nbits,
470d27e202bSKonstantin Komarov 		   sbi->mft.bitmap.nbits - wnd_zeroes(&sbi->mft.bitmap),
471d27e202bSKonstantin Komarov 		   sbi->volume.real_dirty ? "dirty" : "clean",
472d27e202bSKonstantin Komarov 		   (sbi->volume.flags & VOLUME_FLAG_DIRTY) ? "dirty" : "clean");
4737832e123SKonstantin Komarov 
4747832e123SKonstantin Komarov 	return 0;
4757832e123SKonstantin Komarov }
4767832e123SKonstantin Komarov 
ntfs3_volinfo_open(struct inode * inode,struct file * file)4777832e123SKonstantin Komarov static int ntfs3_volinfo_open(struct inode *inode, struct file *file)
4787832e123SKonstantin Komarov {
4797832e123SKonstantin Komarov 	return single_open(file, ntfs3_volinfo, pde_data(inode));
4807832e123SKonstantin Komarov }
4817832e123SKonstantin Komarov 
4827832e123SKonstantin Komarov /* read /proc/fs/ntfs3/<dev>/label */
ntfs3_label_show(struct seq_file * m,void * o)4837832e123SKonstantin Komarov static int ntfs3_label_show(struct seq_file *m, void *o)
4847832e123SKonstantin Komarov {
4857832e123SKonstantin Komarov 	struct super_block *sb = m->private;
4867832e123SKonstantin Komarov 	struct ntfs_sb_info *sbi = sb->s_fs_info;
4877832e123SKonstantin Komarov 
4887832e123SKonstantin Komarov 	seq_printf(m, "%s\n", sbi->volume.label);
4897832e123SKonstantin Komarov 
4907832e123SKonstantin Komarov 	return 0;
4917832e123SKonstantin Komarov }
4927832e123SKonstantin Komarov 
4937832e123SKonstantin Komarov /* write /proc/fs/ntfs3/<dev>/label */
ntfs3_label_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)4947832e123SKonstantin Komarov static ssize_t ntfs3_label_write(struct file *file, const char __user *buffer,
4957832e123SKonstantin Komarov 				 size_t count, loff_t *ppos)
4967832e123SKonstantin Komarov {
4977832e123SKonstantin Komarov 	int err;
4987832e123SKonstantin Komarov 	struct super_block *sb = pde_data(file_inode(file));
4997832e123SKonstantin Komarov 	ssize_t ret = count;
500e52dce61SKonstantin Komarov 	u8 *label;
501e52dce61SKonstantin Komarov 
502e52dce61SKonstantin Komarov 	if (sb_rdonly(sb))
503e52dce61SKonstantin Komarov 		return -EROFS;
504e52dce61SKonstantin Komarov 
505e52dce61SKonstantin Komarov 	label = kmalloc(count, GFP_NOFS);
5067832e123SKonstantin Komarov 
5077832e123SKonstantin Komarov 	if (!label)
5087832e123SKonstantin Komarov 		return -ENOMEM;
5097832e123SKonstantin Komarov 
5107832e123SKonstantin Komarov 	if (copy_from_user(label, buffer, ret)) {
5117832e123SKonstantin Komarov 		ret = -EFAULT;
5127832e123SKonstantin Komarov 		goto out;
5137832e123SKonstantin Komarov 	}
5147832e123SKonstantin Komarov 	while (ret > 0 && label[ret - 1] == '\n')
5157832e123SKonstantin Komarov 		ret -= 1;
5167832e123SKonstantin Komarov 
517f684073cSKonstantin Komarov 	err = ntfs_set_label(sb->s_fs_info, label, ret);
5187832e123SKonstantin Komarov 
5197832e123SKonstantin Komarov 	if (err < 0) {
5207832e123SKonstantin Komarov 		ntfs_err(sb, "failed (%d) to write label", err);
5217832e123SKonstantin Komarov 		ret = err;
5227832e123SKonstantin Komarov 		goto out;
5237832e123SKonstantin Komarov 	}
5247832e123SKonstantin Komarov 
5257832e123SKonstantin Komarov 	*ppos += count;
5267832e123SKonstantin Komarov 	ret = count;
5277832e123SKonstantin Komarov out:
5287832e123SKonstantin Komarov 	kfree(label);
5297832e123SKonstantin Komarov 	return ret;
5307832e123SKonstantin Komarov }
5317832e123SKonstantin Komarov 
ntfs3_label_open(struct inode * inode,struct file * file)5327832e123SKonstantin Komarov static int ntfs3_label_open(struct inode *inode, struct file *file)
5337832e123SKonstantin Komarov {
5347832e123SKonstantin Komarov 	return single_open(file, ntfs3_label_show, pde_data(inode));
5357832e123SKonstantin Komarov }
5367832e123SKonstantin Komarov 
5377832e123SKonstantin Komarov static const struct proc_ops ntfs3_volinfo_fops = {
5387832e123SKonstantin Komarov 	.proc_read = seq_read,
5397832e123SKonstantin Komarov 	.proc_lseek = seq_lseek,
5407832e123SKonstantin Komarov 	.proc_release = single_release,
5417832e123SKonstantin Komarov 	.proc_open = ntfs3_volinfo_open,
5427832e123SKonstantin Komarov };
5437832e123SKonstantin Komarov 
5447832e123SKonstantin Komarov static const struct proc_ops ntfs3_label_fops = {
5457832e123SKonstantin Komarov 	.proc_read = seq_read,
5467832e123SKonstantin Komarov 	.proc_lseek = seq_lseek,
5477832e123SKonstantin Komarov 	.proc_release = single_release,
5487832e123SKonstantin Komarov 	.proc_open = ntfs3_label_open,
5497832e123SKonstantin Komarov 	.proc_write = ntfs3_label_write,
5507832e123SKonstantin Komarov };
5517832e123SKonstantin Komarov 
5527832e123SKonstantin Komarov #endif
5537832e123SKonstantin Komarov 
55482cae269SKonstantin Komarov static struct kmem_cache *ntfs_inode_cachep;
55582cae269SKonstantin Komarov 
ntfs_alloc_inode(struct super_block * sb)55682cae269SKonstantin Komarov static struct inode *ntfs_alloc_inode(struct super_block *sb)
55782cae269SKonstantin Komarov {
558fd60b288SMuchun Song 	struct ntfs_inode *ni = alloc_inode_sb(sb, ntfs_inode_cachep, GFP_NOFS);
55982cae269SKonstantin Komarov 
56082cae269SKonstantin Komarov 	if (!ni)
56182cae269SKonstantin Komarov 		return NULL;
56282cae269SKonstantin Komarov 
56382cae269SKonstantin Komarov 	memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode));
56482cae269SKonstantin Komarov 	mutex_init(&ni->ni_lock);
56582cae269SKonstantin Komarov 	return &ni->vfs_inode;
56682cae269SKonstantin Komarov }
56782cae269SKonstantin Komarov 
ntfs_free_inode(struct inode * inode)568ae6b47b5SKonstantin Komarov static void ntfs_free_inode(struct inode *inode)
56982cae269SKonstantin Komarov {
57082cae269SKonstantin Komarov 	struct ntfs_inode *ni = ntfs_i(inode);
57182cae269SKonstantin Komarov 
57282cae269SKonstantin Komarov 	mutex_destroy(&ni->ni_lock);
57382cae269SKonstantin Komarov 	kmem_cache_free(ntfs_inode_cachep, ni);
57482cae269SKonstantin Komarov }
57582cae269SKonstantin Komarov 
init_once(void * foo)57682cae269SKonstantin Komarov static void init_once(void *foo)
57782cae269SKonstantin Komarov {
57882cae269SKonstantin Komarov 	struct ntfs_inode *ni = foo;
57982cae269SKonstantin Komarov 
58082cae269SKonstantin Komarov 	inode_init_once(&ni->vfs_inode);
58182cae269SKonstantin Komarov }
58282cae269SKonstantin Komarov 
583e8b8e97fSKari Argillander /*
584126dbf8aSChristoph Hellwig  * Noinline to reduce binary size.
585e8b8e97fSKari Argillander  */
ntfs3_put_sbi(struct ntfs_sb_info * sbi)58678a06688SChristian Brauner static noinline void ntfs3_put_sbi(struct ntfs_sb_info *sbi)
58782cae269SKonstantin Komarov {
58882cae269SKonstantin Komarov 	wnd_close(&sbi->mft.bitmap);
58982cae269SKonstantin Komarov 	wnd_close(&sbi->used.bitmap);
59082cae269SKonstantin Komarov 
5914ad5c924SKonstantin Komarov 	if (sbi->mft.ni) {
59282cae269SKonstantin Komarov 		iput(&sbi->mft.ni->vfs_inode);
5934ad5c924SKonstantin Komarov 		sbi->mft.ni = NULL;
5944ad5c924SKonstantin Komarov 	}
59582cae269SKonstantin Komarov 
5964ad5c924SKonstantin Komarov 	if (sbi->security.ni) {
59782cae269SKonstantin Komarov 		iput(&sbi->security.ni->vfs_inode);
5984ad5c924SKonstantin Komarov 		sbi->security.ni = NULL;
5994ad5c924SKonstantin Komarov 	}
60082cae269SKonstantin Komarov 
6014ad5c924SKonstantin Komarov 	if (sbi->reparse.ni) {
60282cae269SKonstantin Komarov 		iput(&sbi->reparse.ni->vfs_inode);
6034ad5c924SKonstantin Komarov 		sbi->reparse.ni = NULL;
6044ad5c924SKonstantin Komarov 	}
60582cae269SKonstantin Komarov 
6064ad5c924SKonstantin Komarov 	if (sbi->objid.ni) {
60782cae269SKonstantin Komarov 		iput(&sbi->objid.ni->vfs_inode);
6084ad5c924SKonstantin Komarov 		sbi->objid.ni = NULL;
6094ad5c924SKonstantin Komarov 	}
61082cae269SKonstantin Komarov 
6114ad5c924SKonstantin Komarov 	if (sbi->volume.ni) {
61282cae269SKonstantin Komarov 		iput(&sbi->volume.ni->vfs_inode);
6134ad5c924SKonstantin Komarov 		sbi->volume.ni = NULL;
6144ad5c924SKonstantin Komarov 	}
61582cae269SKonstantin Komarov 
61682cae269SKonstantin Komarov 	ntfs_update_mftmirr(sbi, 0);
61782cae269SKonstantin Komarov 
61882cae269SKonstantin Komarov 	indx_clear(&sbi->security.index_sii);
61982cae269SKonstantin Komarov 	indx_clear(&sbi->security.index_sdh);
62082cae269SKonstantin Komarov 	indx_clear(&sbi->reparse.index_r);
62182cae269SKonstantin Komarov 	indx_clear(&sbi->objid.index_o);
62278a06688SChristian Brauner }
62378a06688SChristian Brauner 
ntfs3_free_sbi(struct ntfs_sb_info * sbi)62478a06688SChristian Brauner static void ntfs3_free_sbi(struct ntfs_sb_info *sbi)
62578a06688SChristian Brauner {
62678a06688SChristian Brauner 	kfree(sbi->new_rec);
62778a06688SChristian Brauner 	kvfree(ntfs_put_shared(sbi->upcase));
628*86cd4631SKonstantin Komarov 	kvfree(sbi->def_table);
629195c52bdSKari Argillander 	kfree(sbi->compress.lznt);
63082cae269SKonstantin Komarov #ifdef CONFIG_NTFS3_LZX_XPRESS
63182cae269SKonstantin Komarov 	xpress_free_decompressor(sbi->compress.xpress);
63282cae269SKonstantin Komarov 	lzx_free_decompressor(sbi->compress.lzx);
63382cae269SKonstantin Komarov #endif
634195c52bdSKari Argillander 	kfree(sbi);
63582cae269SKonstantin Komarov }
63682cae269SKonstantin Komarov 
ntfs_put_super(struct super_block * sb)63782cae269SKonstantin Komarov static void ntfs_put_super(struct super_block *sb)
63882cae269SKonstantin Komarov {
63982cae269SKonstantin Komarov 	struct ntfs_sb_info *sbi = sb->s_fs_info;
64082cae269SKonstantin Komarov 
6417832e123SKonstantin Komarov #ifdef CONFIG_PROC_FS
6427832e123SKonstantin Komarov 	// Remove /proc/fs/ntfs3/..
6437832e123SKonstantin Komarov 	if (sbi->procdir) {
6447832e123SKonstantin Komarov 		remove_proc_entry("label", sbi->procdir);
6457832e123SKonstantin Komarov 		remove_proc_entry("volinfo", sbi->procdir);
6467832e123SKonstantin Komarov 		remove_proc_entry(sb->s_id, proc_info_root);
6477832e123SKonstantin Komarov 		sbi->procdir = NULL;
6487832e123SKonstantin Komarov 	}
6497832e123SKonstantin Komarov #endif
6507832e123SKonstantin Komarov 
651e8b8e97fSKari Argillander 	/* Mark rw ntfs as clear, if possible. */
65282cae269SKonstantin Komarov 	ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
65378a06688SChristian Brauner 	ntfs3_put_sbi(sbi);
65482cae269SKonstantin Komarov }
65582cae269SKonstantin Komarov 
ntfs_statfs(struct dentry * dentry,struct kstatfs * buf)65682cae269SKonstantin Komarov static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf)
65782cae269SKonstantin Komarov {
65882cae269SKonstantin Komarov 	struct super_block *sb = dentry->d_sb;
65982cae269SKonstantin Komarov 	struct ntfs_sb_info *sbi = sb->s_fs_info;
66082cae269SKonstantin Komarov 	struct wnd_bitmap *wnd = &sbi->used.bitmap;
66182cae269SKonstantin Komarov 
66282cae269SKonstantin Komarov 	buf->f_type = sb->s_magic;
66382cae269SKonstantin Komarov 	buf->f_bsize = sbi->cluster_size;
66482cae269SKonstantin Komarov 	buf->f_blocks = wnd->nbits;
66582cae269SKonstantin Komarov 
66682cae269SKonstantin Komarov 	buf->f_bfree = buf->f_bavail = wnd_zeroes(wnd);
66782cae269SKonstantin Komarov 	buf->f_fsid.val[0] = sbi->volume.ser_num;
66882cae269SKonstantin Komarov 	buf->f_fsid.val[1] = (sbi->volume.ser_num >> 32);
66982cae269SKonstantin Komarov 	buf->f_namelen = NTFS_NAME_LEN;
67082cae269SKonstantin Komarov 
67182cae269SKonstantin Komarov 	return 0;
67282cae269SKonstantin Komarov }
67382cae269SKonstantin Komarov 
ntfs_show_options(struct seq_file * m,struct dentry * root)67482cae269SKonstantin Komarov static int ntfs_show_options(struct seq_file *m, struct dentry *root)
67582cae269SKonstantin Komarov {
67682cae269SKonstantin Komarov 	struct super_block *sb = root->d_sb;
67782cae269SKonstantin Komarov 	struct ntfs_sb_info *sbi = sb->s_fs_info;
678564c97bdSKari Argillander 	struct ntfs_mount_options *opts = sbi->options;
67982cae269SKonstantin Komarov 	struct user_namespace *user_ns = seq_user_ns(m);
68082cae269SKonstantin Komarov 
68196de65a9SKonstantin Komarov 	seq_printf(m, ",uid=%u", from_kuid_munged(user_ns, opts->fs_uid));
68296de65a9SKonstantin Komarov 	seq_printf(m, ",gid=%u", from_kgid_munged(user_ns, opts->fs_gid));
68382cae269SKonstantin Komarov 	if (opts->dmask)
684f27b92ecSMarc Aurèle La France 		seq_printf(m, ",dmask=%04o", opts->fs_dmask_inv ^ 0xffff);
68516b3dbfbSKonstantin Komarov 	if (opts->fmask)
68616b3dbfbSKonstantin Komarov 		seq_printf(m, ",fmask=%04o", opts->fs_fmask_inv ^ 0xffff);
68782cae269SKonstantin Komarov 	if (opts->sys_immutable)
68882cae269SKonstantin Komarov 		seq_puts(m, ",sys_immutable");
68982cae269SKonstantin Komarov 	if (opts->discard)
69082cae269SKonstantin Komarov 		seq_puts(m, ",discard");
69182cae269SKonstantin Komarov 	if (opts->force)
69282cae269SKonstantin Komarov 		seq_puts(m, ",force");
69316b3dbfbSKonstantin Komarov 	if (opts->sparse)
69416b3dbfbSKonstantin Komarov 		seq_puts(m, ",sparse");
69516b3dbfbSKonstantin Komarov 	if (opts->nohidden)
69616b3dbfbSKonstantin Komarov 		seq_puts(m, ",nohidden");
69716b3dbfbSKonstantin Komarov 	if (opts->hide_dot_files)
69816b3dbfbSKonstantin Komarov 		seq_puts(m, ",hide_dot_files");
69916b3dbfbSKonstantin Komarov 	if (opts->windows_names)
70016b3dbfbSKonstantin Komarov 		seq_puts(m, ",windows_names");
70116b3dbfbSKonstantin Komarov 	if (opts->showmeta)
70216b3dbfbSKonstantin Komarov 		seq_puts(m, ",showmeta");
70382cae269SKonstantin Komarov 	if (sb->s_flags & SB_POSIXACL)
70482cae269SKonstantin Komarov 		seq_puts(m, ",acl");
70516b3dbfbSKonstantin Komarov 	if (opts->nls)
70616b3dbfbSKonstantin Komarov 		seq_printf(m, ",iocharset=%s", opts->nls->charset);
70716b3dbfbSKonstantin Komarov 	else
70816b3dbfbSKonstantin Komarov 		seq_puts(m, ",iocharset=utf8");
70916b3dbfbSKonstantin Komarov 	if (opts->prealloc)
71016b3dbfbSKonstantin Komarov 		seq_puts(m, ",prealloc");
71116b3dbfbSKonstantin Komarov 	if (opts->nocase)
71216b3dbfbSKonstantin Komarov 		seq_puts(m, ",nocase");
71382cae269SKonstantin Komarov 
71482cae269SKonstantin Komarov 	return 0;
71582cae269SKonstantin Komarov }
71682cae269SKonstantin Komarov 
717e8b8e97fSKari Argillander /*
718f73f9397SKonstantin Komarov  * ntfs_shutdown - super_operations::shutdown
719f73f9397SKonstantin Komarov  */
ntfs_shutdown(struct super_block * sb)720f73f9397SKonstantin Komarov static void ntfs_shutdown(struct super_block *sb)
721f73f9397SKonstantin Komarov {
722323b0ab3SKonstantin Komarov 	set_bit(NTFS_FLAGS_SHUTDOWN_BIT, &ntfs_sb(sb)->flags);
723f73f9397SKonstantin Komarov }
724f73f9397SKonstantin Komarov 
725f73f9397SKonstantin Komarov /*
726e8b8e97fSKari Argillander  * ntfs_sync_fs - super_operations::sync_fs
727e8b8e97fSKari Argillander  */
ntfs_sync_fs(struct super_block * sb,int wait)72882cae269SKonstantin Komarov static int ntfs_sync_fs(struct super_block *sb, int wait)
72982cae269SKonstantin Komarov {
73082cae269SKonstantin Komarov 	int err = 0, err2;
73182cae269SKonstantin Komarov 	struct ntfs_sb_info *sbi = sb->s_fs_info;
73282cae269SKonstantin Komarov 	struct ntfs_inode *ni;
73382cae269SKonstantin Komarov 	struct inode *inode;
73482cae269SKonstantin Komarov 
735f73f9397SKonstantin Komarov 	if (unlikely(ntfs3_forced_shutdown(sb)))
736f73f9397SKonstantin Komarov 		return -EIO;
737f73f9397SKonstantin Komarov 
73882cae269SKonstantin Komarov 	ni = sbi->security.ni;
73982cae269SKonstantin Komarov 	if (ni) {
74082cae269SKonstantin Komarov 		inode = &ni->vfs_inode;
74182cae269SKonstantin Komarov 		err2 = _ni_write_inode(inode, wait);
74282cae269SKonstantin Komarov 		if (err2 && !err)
74382cae269SKonstantin Komarov 			err = err2;
74482cae269SKonstantin Komarov 	}
74582cae269SKonstantin Komarov 
74682cae269SKonstantin Komarov 	ni = sbi->objid.ni;
74782cae269SKonstantin Komarov 	if (ni) {
74882cae269SKonstantin Komarov 		inode = &ni->vfs_inode;
74982cae269SKonstantin Komarov 		err2 = _ni_write_inode(inode, wait);
75082cae269SKonstantin Komarov 		if (err2 && !err)
75182cae269SKonstantin Komarov 			err = err2;
75282cae269SKonstantin Komarov 	}
75382cae269SKonstantin Komarov 
75482cae269SKonstantin Komarov 	ni = sbi->reparse.ni;
75582cae269SKonstantin Komarov 	if (ni) {
75682cae269SKonstantin Komarov 		inode = &ni->vfs_inode;
75782cae269SKonstantin Komarov 		err2 = _ni_write_inode(inode, wait);
75882cae269SKonstantin Komarov 		if (err2 && !err)
75982cae269SKonstantin Komarov 			err = err2;
76082cae269SKonstantin Komarov 	}
76182cae269SKonstantin Komarov 
76282cae269SKonstantin Komarov 	if (!err)
76382cae269SKonstantin Komarov 		ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
76482cae269SKonstantin Komarov 
76582cae269SKonstantin Komarov 	ntfs_update_mftmirr(sbi, wait);
76682cae269SKonstantin Komarov 
76782cae269SKonstantin Komarov 	return err;
76882cae269SKonstantin Komarov }
76982cae269SKonstantin Komarov 
77082cae269SKonstantin Komarov static const struct super_operations ntfs_sops = {
77182cae269SKonstantin Komarov 	.alloc_inode = ntfs_alloc_inode,
772ae6b47b5SKonstantin Komarov 	.free_inode = ntfs_free_inode,
77382cae269SKonstantin Komarov 	.evict_inode = ntfs_evict_inode,
77482cae269SKonstantin Komarov 	.put_super = ntfs_put_super,
77582cae269SKonstantin Komarov 	.statfs = ntfs_statfs,
77682cae269SKonstantin Komarov 	.show_options = ntfs_show_options,
777f73f9397SKonstantin Komarov 	.shutdown = ntfs_shutdown,
77882cae269SKonstantin Komarov 	.sync_fs = ntfs_sync_fs,
77982cae269SKonstantin Komarov 	.write_inode = ntfs3_write_inode,
78082cae269SKonstantin Komarov };
78182cae269SKonstantin Komarov 
ntfs_export_get_inode(struct super_block * sb,u64 ino,u32 generation)78282cae269SKonstantin Komarov static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino,
78382cae269SKonstantin Komarov 					   u32 generation)
78482cae269SKonstantin Komarov {
78582cae269SKonstantin Komarov 	struct MFT_REF ref;
78682cae269SKonstantin Komarov 	struct inode *inode;
78782cae269SKonstantin Komarov 
78882cae269SKonstantin Komarov 	ref.low = cpu_to_le32(ino);
78982cae269SKonstantin Komarov #ifdef CONFIG_NTFS3_64BIT_CLUSTER
79082cae269SKonstantin Komarov 	ref.high = cpu_to_le16(ino >> 32);
79182cae269SKonstantin Komarov #else
79282cae269SKonstantin Komarov 	ref.high = 0;
79382cae269SKonstantin Komarov #endif
79482cae269SKonstantin Komarov 	ref.seq = cpu_to_le16(generation);
79582cae269SKonstantin Komarov 
79682cae269SKonstantin Komarov 	inode = ntfs_iget5(sb, &ref, NULL);
79782cae269SKonstantin Komarov 	if (!IS_ERR(inode) && is_bad_inode(inode)) {
79882cae269SKonstantin Komarov 		iput(inode);
79982cae269SKonstantin Komarov 		inode = ERR_PTR(-ESTALE);
80082cae269SKonstantin Komarov 	}
80182cae269SKonstantin Komarov 
80282cae269SKonstantin Komarov 	return inode;
80382cae269SKonstantin Komarov }
80482cae269SKonstantin Komarov 
ntfs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)80582cae269SKonstantin Komarov static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
80682cae269SKonstantin Komarov 					int fh_len, int fh_type)
80782cae269SKonstantin Komarov {
80882cae269SKonstantin Komarov 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
80982cae269SKonstantin Komarov 				    ntfs_export_get_inode);
81082cae269SKonstantin Komarov }
81182cae269SKonstantin Komarov 
ntfs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)81282cae269SKonstantin Komarov static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
81382cae269SKonstantin Komarov 					int fh_len, int fh_type)
81482cae269SKonstantin Komarov {
81582cae269SKonstantin Komarov 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
81682cae269SKonstantin Komarov 				    ntfs_export_get_inode);
81782cae269SKonstantin Komarov }
81882cae269SKonstantin Komarov 
81982cae269SKonstantin Komarov /* TODO: == ntfs_sync_inode */
ntfs_nfs_commit_metadata(struct inode * inode)82082cae269SKonstantin Komarov static int ntfs_nfs_commit_metadata(struct inode *inode)
82182cae269SKonstantin Komarov {
82282cae269SKonstantin Komarov 	return _ni_write_inode(inode, 1);
82382cae269SKonstantin Komarov }
82482cae269SKonstantin Komarov 
82582cae269SKonstantin Komarov static const struct export_operations ntfs_export_ops = {
82682cae269SKonstantin Komarov 	.fh_to_dentry = ntfs_fh_to_dentry,
82782cae269SKonstantin Komarov 	.fh_to_parent = ntfs_fh_to_parent,
82882cae269SKonstantin Komarov 	.get_parent = ntfs3_get_parent,
82982cae269SKonstantin Komarov 	.commit_metadata = ntfs_nfs_commit_metadata,
83082cae269SKonstantin Komarov };
83182cae269SKonstantin Komarov 
832e8b8e97fSKari Argillander /*
833e8b8e97fSKari Argillander  * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb".
834e8b8e97fSKari Argillander  */
format_size_gb(const u64 bytes,u32 * mb)83582cae269SKonstantin Komarov static u32 format_size_gb(const u64 bytes, u32 *mb)
83682cae269SKonstantin Komarov {
837e8b8e97fSKari Argillander 	/* Do simple right 30 bit shift of 64 bit value. */
83882cae269SKonstantin Komarov 	u64 kbytes = bytes >> 10;
83982cae269SKonstantin Komarov 	u32 kbytes32 = kbytes;
84082cae269SKonstantin Komarov 
84182cae269SKonstantin Komarov 	*mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20;
84282cae269SKonstantin Komarov 	if (*mb >= 100)
84382cae269SKonstantin Komarov 		*mb = 99;
84482cae269SKonstantin Komarov 
84582cae269SKonstantin Komarov 	return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12);
84682cae269SKonstantin Komarov }
84782cae269SKonstantin Komarov 
true_sectors_per_clst(const struct NTFS_BOOT * boot)84882cae269SKonstantin Komarov static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot)
84982cae269SKonstantin Komarov {
850a3b77434SRandy Dunlap 	if (boot->sectors_per_clusters <= 0x80)
851a3b77434SRandy Dunlap 		return boot->sectors_per_clusters;
852a3b77434SRandy Dunlap 	if (boot->sectors_per_clusters >= 0xf4) /* limit shift to 2MB max */
85396de65a9SKonstantin Komarov 		return 1U << (-(s8)boot->sectors_per_clusters);
854a3b77434SRandy Dunlap 	return -EINVAL;
85582cae269SKonstantin Komarov }
85682cae269SKonstantin Komarov 
857e8b8e97fSKari Argillander /*
858e8b8e97fSKari Argillander  * ntfs_init_from_boot - Init internal info from on-disk boot sector.
859f1d325b8SKonstantin Komarov  *
860f1d325b8SKonstantin Komarov  * NTFS mount begins from boot - special formatted 512 bytes.
861f1d325b8SKonstantin Komarov  * There are two boots: the first and the last 512 bytes of volume.
862f1d325b8SKonstantin Komarov  * The content of boot is not changed during ntfs life.
863f1d325b8SKonstantin Komarov  *
864f1d325b8SKonstantin Komarov  * NOTE: ntfs.sys checks only first (primary) boot.
865f1d325b8SKonstantin Komarov  * chkdsk checks both boots.
866e8b8e97fSKari Argillander  */
ntfs_init_from_boot(struct super_block * sb,u32 sector_size,u64 dev_size,struct NTFS_BOOT ** boot2)86782cae269SKonstantin Komarov static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
868f1d325b8SKonstantin Komarov 			       u64 dev_size, struct NTFS_BOOT **boot2)
86982cae269SKonstantin Komarov {
87082cae269SKonstantin Komarov 	struct ntfs_sb_info *sbi = sb->s_fs_info;
87182cae269SKonstantin Komarov 	int err;
87282cae269SKonstantin Komarov 	u32 mb, gb, boot_sector_size, sct_per_clst, record_size;
873dcc852e5SKonstantin Komarov 	u64 sectors, clusters, mlcn, mlcn2, dev_size0;
87482cae269SKonstantin Komarov 	struct NTFS_BOOT *boot;
87582cae269SKonstantin Komarov 	struct buffer_head *bh;
87682cae269SKonstantin Komarov 	struct MFT_REC *rec;
87782cae269SKonstantin Komarov 	u16 fn, ao;
87896de65a9SKonstantin Komarov 	u8 cluster_bits;
8796a4cd3eaSKonstantin Komarov 	u32 boot_off = 0;
88015735a62SKonstantin Komarov 	sector_t boot_block = 0;
8816a4cd3eaSKonstantin Komarov 	const char *hint = "Primary boot";
88282cae269SKonstantin Komarov 
883dcc852e5SKonstantin Komarov 	/* Save original dev_size. Used with alternative boot. */
884dcc852e5SKonstantin Komarov 	dev_size0 = dev_size;
885dcc852e5SKonstantin Komarov 
88682cae269SKonstantin Komarov 	sbi->volume.blocks = dev_size >> PAGE_SHIFT;
88782cae269SKonstantin Komarov 
88815735a62SKonstantin Komarov read_boot:
88915735a62SKonstantin Komarov 	bh = ntfs_bread(sb, boot_block);
89082cae269SKonstantin Komarov 	if (!bh)
89115735a62SKonstantin Komarov 		return boot_block ? -EINVAL : -EIO;
89282cae269SKonstantin Komarov 
89382cae269SKonstantin Komarov 	err = -EINVAL;
89434e6552aSPavel Skripkin 
89534e6552aSPavel Skripkin 	/* Corrupted image; do not read OOB */
89634e6552aSPavel Skripkin 	if (bh->b_size - sizeof(*boot) < boot_off)
89734e6552aSPavel Skripkin 		goto out;
89834e6552aSPavel Skripkin 
8996a4cd3eaSKonstantin Komarov 	boot = (struct NTFS_BOOT *)Add2Ptr(bh->b_data, boot_off);
90082cae269SKonstantin Komarov 
901e43f6ec2SKonstantin Komarov 	if (memcmp(boot->system_id, "NTFS    ", sizeof("NTFS    ") - 1)) {
9026a4cd3eaSKonstantin Komarov 		ntfs_err(sb, "%s signature is not NTFS.", hint);
90382cae269SKonstantin Komarov 		goto out;
904e43f6ec2SKonstantin Komarov 	}
90582cae269SKonstantin Komarov 
90682cae269SKonstantin Komarov 	/* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/
90782cae269SKonstantin Komarov 	/*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1])
90882cae269SKonstantin Komarov 	 *	goto out;
90982cae269SKonstantin Komarov 	 */
91082cae269SKonstantin Komarov 
911e43f6ec2SKonstantin Komarov 	boot_sector_size = ((u32)boot->bytes_per_sector[1] << 8) |
912e43f6ec2SKonstantin Komarov 			   boot->bytes_per_sector[0];
913e43f6ec2SKonstantin Komarov 	if (boot_sector_size < SECTOR_SIZE ||
914528c9b3dSKari Argillander 	    !is_power_of_2(boot_sector_size)) {
9156a4cd3eaSKonstantin Komarov 		ntfs_err(sb, "%s: invalid bytes per sector %u.", hint,
9166a4cd3eaSKonstantin Komarov 			 boot_sector_size);
91782cae269SKonstantin Komarov 		goto out;
91882cae269SKonstantin Komarov 	}
91982cae269SKonstantin Komarov 
92082cae269SKonstantin Komarov 	/* cluster size: 512, 1K, 2K, 4K, ... 2M */
92182cae269SKonstantin Komarov 	sct_per_clst = true_sectors_per_clst(boot);
922e43f6ec2SKonstantin Komarov 	if ((int)sct_per_clst < 0 || !is_power_of_2(sct_per_clst)) {
9236a4cd3eaSKonstantin Komarov 		ntfs_err(sb, "%s: invalid sectors per cluster %u.", hint,
9246a4cd3eaSKonstantin Komarov 			 sct_per_clst);
925a3b77434SRandy Dunlap 		goto out;
926e43f6ec2SKonstantin Komarov 	}
927e43f6ec2SKonstantin Komarov 
928e43f6ec2SKonstantin Komarov 	sbi->cluster_size = boot_sector_size * sct_per_clst;
929e43f6ec2SKonstantin Komarov 	sbi->cluster_bits = cluster_bits = blksize_bits(sbi->cluster_size);
930e43f6ec2SKonstantin Komarov 	sbi->cluster_mask = sbi->cluster_size - 1;
931e43f6ec2SKonstantin Komarov 	sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask;
93282cae269SKonstantin Komarov 
93382cae269SKonstantin Komarov 	mlcn = le64_to_cpu(boot->mft_clst);
93482cae269SKonstantin Komarov 	mlcn2 = le64_to_cpu(boot->mft2_clst);
93582cae269SKonstantin Komarov 	sectors = le64_to_cpu(boot->sectors_per_volume);
93682cae269SKonstantin Komarov 
937e43f6ec2SKonstantin Komarov 	if (mlcn * sct_per_clst >= sectors || mlcn2 * sct_per_clst >= sectors) {
938e43f6ec2SKonstantin Komarov 		ntfs_err(
939e43f6ec2SKonstantin Komarov 			sb,
9406a4cd3eaSKonstantin Komarov 			"%s: start of MFT 0x%llx (0x%llx) is out of volume 0x%llx.",
9416a4cd3eaSKonstantin Komarov 			hint, mlcn, mlcn2, sectors);
94282cae269SKonstantin Komarov 		goto out;
94382cae269SKonstantin Komarov 	}
94482cae269SKonstantin Komarov 
94591a4b1eeSKonstantin Komarov 	if (boot->record_size >= 0) {
94691a4b1eeSKonstantin Komarov 		record_size = (u32)boot->record_size << cluster_bits;
94791a4b1eeSKonstantin Komarov 	} else if (-boot->record_size <= MAXIMUM_SHIFT_BYTES_PER_MFT) {
94891a4b1eeSKonstantin Komarov 		record_size = 1u << (-boot->record_size);
94991a4b1eeSKonstantin Komarov 	} else {
95091a4b1eeSKonstantin Komarov 		ntfs_err(sb, "%s: invalid record size %d.", hint,
95191a4b1eeSKonstantin Komarov 			 boot->record_size);
95291a4b1eeSKonstantin Komarov 		goto out;
95391a4b1eeSKonstantin Komarov 	}
95491a4b1eeSKonstantin Komarov 
95591a4b1eeSKonstantin Komarov 	sbi->record_size = record_size;
956e43f6ec2SKonstantin Komarov 	sbi->record_bits = blksize_bits(record_size);
957e43f6ec2SKonstantin Komarov 	sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes
958e43f6ec2SKonstantin Komarov 
959e43f6ec2SKonstantin Komarov 	/* Check MFT record size. */
960e43f6ec2SKonstantin Komarov 	if (record_size < SECTOR_SIZE || !is_power_of_2(record_size)) {
9616a4cd3eaSKonstantin Komarov 		ntfs_err(sb, "%s: invalid bytes per MFT record %u (%d).", hint,
962e43f6ec2SKonstantin Komarov 			 record_size, boot->record_size);
963e43f6ec2SKonstantin Komarov 		goto out;
964e43f6ec2SKonstantin Komarov 	}
965e43f6ec2SKonstantin Komarov 
966e43f6ec2SKonstantin Komarov 	if (record_size > MAXIMUM_BYTES_PER_MFT) {
967e43f6ec2SKonstantin Komarov 		ntfs_err(sb, "Unsupported bytes per MFT record %u.",
968e43f6ec2SKonstantin Komarov 			 record_size);
969e43f6ec2SKonstantin Komarov 		goto out;
970e43f6ec2SKonstantin Komarov 	}
971e43f6ec2SKonstantin Komarov 
97291a4b1eeSKonstantin Komarov 	if (boot->index_size >= 0) {
97391a4b1eeSKonstantin Komarov 		sbi->index_size = (u32)boot->index_size << cluster_bits;
97491a4b1eeSKonstantin Komarov 	} else if (-boot->index_size <= MAXIMUM_SHIFT_BYTES_PER_INDEX) {
97591a4b1eeSKonstantin Komarov 		sbi->index_size = 1u << (-boot->index_size);
97691a4b1eeSKonstantin Komarov 	} else {
97791a4b1eeSKonstantin Komarov 		ntfs_err(sb, "%s: invalid index size %d.", hint,
97891a4b1eeSKonstantin Komarov 			 boot->index_size);
97991a4b1eeSKonstantin Komarov 		goto out;
98091a4b1eeSKonstantin Komarov 	}
981e43f6ec2SKonstantin Komarov 
982e8b8e97fSKari Argillander 	/* Check index record size. */
983e43f6ec2SKonstantin Komarov 	if (sbi->index_size < SECTOR_SIZE || !is_power_of_2(sbi->index_size)) {
9846a4cd3eaSKonstantin Komarov 		ntfs_err(sb, "%s: invalid bytes per index %u(%d).", hint,
9856a4cd3eaSKonstantin Komarov 			 sbi->index_size, boot->index_size);
986e43f6ec2SKonstantin Komarov 		goto out;
987e43f6ec2SKonstantin Komarov 	}
988e43f6ec2SKonstantin Komarov 
989e43f6ec2SKonstantin Komarov 	if (sbi->index_size > MAXIMUM_BYTES_PER_INDEX) {
9906a4cd3eaSKonstantin Komarov 		ntfs_err(sb, "%s: unsupported bytes per index %u.", hint,
991e43f6ec2SKonstantin Komarov 			 sbi->index_size);
99282cae269SKonstantin Komarov 		goto out;
99382cae269SKonstantin Komarov 	}
99482cae269SKonstantin Komarov 
995dbf59e2aSKonstantin Komarov 	sbi->volume.size = sectors * boot_sector_size;
99682cae269SKonstantin Komarov 
997dbf59e2aSKonstantin Komarov 	gb = format_size_gb(sbi->volume.size + boot_sector_size, &mb);
99882cae269SKonstantin Komarov 
99982cae269SKonstantin Komarov 	/*
1000e8b8e97fSKari Argillander 	 * - Volume formatted and mounted with the same sector size.
1001e8b8e97fSKari Argillander 	 * - Volume formatted 4K and mounted as 512.
1002e8b8e97fSKari Argillander 	 * - Volume formatted 512 and mounted as 4K.
100382cae269SKonstantin Komarov 	 */
1004dbf59e2aSKonstantin Komarov 	if (boot_sector_size != sector_size) {
1005dbf59e2aSKonstantin Komarov 		ntfs_warn(
1006dbf59e2aSKonstantin Komarov 			sb,
100796de65a9SKonstantin Komarov 			"Different NTFS sector size (%u) and media sector size (%u).",
1008dbf59e2aSKonstantin Komarov 			boot_sector_size, sector_size);
100982cae269SKonstantin Komarov 		dev_size += sector_size - 1;
101082cae269SKonstantin Komarov 	}
101182cae269SKonstantin Komarov 
101296de65a9SKonstantin Komarov 	sbi->mft.lbo = mlcn << cluster_bits;
101396de65a9SKonstantin Komarov 	sbi->mft.lbo2 = mlcn2 << cluster_bits;
101482cae269SKonstantin Komarov 
101509f7c338SKonstantin Komarov 	/* Compare boot's cluster and sector. */
1016e43f6ec2SKonstantin Komarov 	if (sbi->cluster_size < boot_sector_size) {
10176a4cd3eaSKonstantin Komarov 		ntfs_err(sb, "%s: invalid bytes per cluster (%u).", hint,
1018e43f6ec2SKonstantin Komarov 			 sbi->cluster_size);
101982cae269SKonstantin Komarov 		goto out;
1020e43f6ec2SKonstantin Komarov 	}
102182cae269SKonstantin Komarov 
102209f7c338SKonstantin Komarov 	/* Compare boot's cluster and media sector. */
102309f7c338SKonstantin Komarov 	if (sbi->cluster_size < sector_size) {
102409f7c338SKonstantin Komarov 		/* No way to use ntfs_get_block in this case. */
102509f7c338SKonstantin Komarov 		ntfs_err(
102609f7c338SKonstantin Komarov 			sb,
102796de65a9SKonstantin Komarov 			"Failed to mount 'cause NTFS's cluster size (%u) is less than media sector size (%u).",
102809f7c338SKonstantin Komarov 			sbi->cluster_size, sector_size);
102909f7c338SKonstantin Komarov 		goto out;
103009f7c338SKonstantin Komarov 	}
103109f7c338SKonstantin Komarov 
103282cae269SKonstantin Komarov 	sbi->max_bytes_per_attr =
103333e70701SKonstantin Komarov 		record_size - ALIGN(MFTRECORD_FIXUP_OFFSET, 8) -
1034fa3cacf5SKari Argillander 		ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) -
1035fa3cacf5SKari Argillander 		ALIGN(sizeof(enum ATTR_TYPE), 8);
103682cae269SKonstantin Komarov 
103782cae269SKonstantin Komarov 	sbi->volume.ser_num = le64_to_cpu(boot->serial_num);
103882cae269SKonstantin Komarov 
1039e8b8e97fSKari Argillander 	/* Warning if RAW volume. */
1040dbf59e2aSKonstantin Komarov 	if (dev_size < sbi->volume.size + boot_sector_size) {
104182cae269SKonstantin Komarov 		u32 mb0, gb0;
104282cae269SKonstantin Komarov 
104382cae269SKonstantin Komarov 		gb0 = format_size_gb(dev_size, &mb0);
104482cae269SKonstantin Komarov 		ntfs_warn(
104582cae269SKonstantin Komarov 			sb,
104696de65a9SKonstantin Komarov 			"RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only.",
104782cae269SKonstantin Komarov 			gb, mb, gb0, mb0);
104882cae269SKonstantin Komarov 		sb->s_flags |= SB_RDONLY;
104982cae269SKonstantin Komarov 	}
105082cae269SKonstantin Komarov 
105196de65a9SKonstantin Komarov 	clusters = sbi->volume.size >> cluster_bits;
105282cae269SKonstantin Komarov #ifndef CONFIG_NTFS3_64BIT_CLUSTER
1053e8b8e97fSKari Argillander 	/* 32 bits per cluster. */
105482cae269SKonstantin Komarov 	if (clusters >> 32) {
105582cae269SKonstantin Komarov 		ntfs_notice(
105682cae269SKonstantin Komarov 			sb,
105796de65a9SKonstantin Komarov 			"NTFS %u.%02u Gb is too big to use 32 bits per cluster.",
105882cae269SKonstantin Komarov 			gb, mb);
105982cae269SKonstantin Komarov 		goto out;
106082cae269SKonstantin Komarov 	}
106182cae269SKonstantin Komarov #elif BITS_PER_LONG < 64
106282cae269SKonstantin Komarov #error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS"
106382cae269SKonstantin Komarov #endif
106482cae269SKonstantin Komarov 
106582cae269SKonstantin Komarov 	sbi->used.bitmap.nbits = clusters;
106682cae269SKonstantin Komarov 
1067195c52bdSKari Argillander 	rec = kzalloc(record_size, GFP_NOFS);
106882cae269SKonstantin Komarov 	if (!rec) {
106982cae269SKonstantin Komarov 		err = -ENOMEM;
107082cae269SKonstantin Komarov 		goto out;
107182cae269SKonstantin Komarov 	}
107282cae269SKonstantin Komarov 
107382cae269SKonstantin Komarov 	sbi->new_rec = rec;
107482cae269SKonstantin Komarov 	rec->rhdr.sign = NTFS_FILE_SIGNATURE;
107533e70701SKonstantin Komarov 	rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET);
107682cae269SKonstantin Komarov 	fn = (sbi->record_size >> SECTOR_SHIFT) + 1;
107782cae269SKonstantin Komarov 	rec->rhdr.fix_num = cpu_to_le16(fn);
107833e70701SKonstantin Komarov 	ao = ALIGN(MFTRECORD_FIXUP_OFFSET + sizeof(short) * fn, 8);
107982cae269SKonstantin Komarov 	rec->attr_off = cpu_to_le16(ao);
1080fa3cacf5SKari Argillander 	rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8));
108182cae269SKonstantin Komarov 	rec->total = cpu_to_le32(sbi->record_size);
108282cae269SKonstantin Komarov 	((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END;
108382cae269SKonstantin Komarov 
108428861e3bSKari Argillander 	sb_set_blocksize(sb, min_t(u32, sbi->cluster_size, PAGE_SIZE));
108582cae269SKonstantin Komarov 
108682cae269SKonstantin Komarov 	sbi->block_mask = sb->s_blocksize - 1;
108782cae269SKonstantin Komarov 	sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits;
108882cae269SKonstantin Komarov 	sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits;
108982cae269SKonstantin Komarov 
1090e8b8e97fSKari Argillander 	/* Maximum size for normal files. */
109196de65a9SKonstantin Komarov 	sbi->maxbytes = (clusters << cluster_bits) - 1;
109282cae269SKonstantin Komarov 
109382cae269SKonstantin Komarov #ifdef CONFIG_NTFS3_64BIT_CLUSTER
109496de65a9SKonstantin Komarov 	if (clusters >= (1ull << (64 - cluster_bits)))
109582cae269SKonstantin Komarov 		sbi->maxbytes = -1;
109682cae269SKonstantin Komarov 	sbi->maxbytes_sparse = -1;
109728861e3bSKari Argillander 	sb->s_maxbytes = MAX_LFS_FILESIZE;
109882cae269SKonstantin Komarov #else
1099e8b8e97fSKari Argillander 	/* Maximum size for sparse file. */
110096de65a9SKonstantin Komarov 	sbi->maxbytes_sparse = (1ull << (cluster_bits + 32)) - 1;
110196de65a9SKonstantin Komarov 	sb->s_maxbytes = 0xFFFFFFFFull << cluster_bits;
110282cae269SKonstantin Komarov #endif
110382cae269SKonstantin Komarov 
11048335ebe1SKonstantin Komarov 	/*
11058335ebe1SKonstantin Komarov 	 * Compute the MFT zone at two steps.
11068335ebe1SKonstantin Komarov 	 * It would be nice if we are able to allocate 1/8 of
11078335ebe1SKonstantin Komarov 	 * total clusters for MFT but not more then 512 MB.
11088335ebe1SKonstantin Komarov 	 */
110996de65a9SKonstantin Komarov 	sbi->zone_max = min_t(CLST, 0x20000000 >> cluster_bits, clusters >> 3);
11108335ebe1SKonstantin Komarov 
111182cae269SKonstantin Komarov 	err = 0;
111282cae269SKonstantin Komarov 
11136a4cd3eaSKonstantin Komarov 	if (bh->b_blocknr && !sb_rdonly(sb)) {
11146a4cd3eaSKonstantin Komarov 		/*
11156a4cd3eaSKonstantin Komarov 	 	 * Alternative boot is ok but primary is not ok.
1116f1d325b8SKonstantin Komarov 	 	 * Do not update primary boot here 'cause it may be faked boot.
1117f1d325b8SKonstantin Komarov 	 	 * Let ntfs to be mounted and update boot later.
11186a4cd3eaSKonstantin Komarov 		 */
1119f1d325b8SKonstantin Komarov 		*boot2 = kmemdup(boot, sizeof(*boot), GFP_NOFS | __GFP_NOWARN);
11206a4cd3eaSKonstantin Komarov 	}
11216a4cd3eaSKonstantin Komarov 
112282cae269SKonstantin Komarov out:
112315735a62SKonstantin Komarov 	brelse(bh);
112415735a62SKonstantin Komarov 
112515735a62SKonstantin Komarov 	if (err == -EINVAL && !boot_block && dev_size0 > PAGE_SHIFT) {
11266a4cd3eaSKonstantin Komarov 		u32 block_size = min_t(u32, sector_size, PAGE_SIZE);
1127dcc852e5SKonstantin Komarov 		u64 lbo = dev_size0 - sizeof(*boot);
11286a4cd3eaSKonstantin Komarov 
112915735a62SKonstantin Komarov 		boot_block = lbo >> blksize_bits(block_size);
113015735a62SKonstantin Komarov 		boot_off = lbo & (block_size - 1);
113115735a62SKonstantin Komarov 		if (boot_block && block_size >= boot_off + sizeof(*boot)) {
11326a4cd3eaSKonstantin Komarov 			/*
11336a4cd3eaSKonstantin Komarov 			 * Try alternative boot (last sector)
11346a4cd3eaSKonstantin Komarov 			 */
11356a4cd3eaSKonstantin Komarov 			sb_set_blocksize(sb, block_size);
11366a4cd3eaSKonstantin Komarov 			hint = "Alternative boot";
1137dcc852e5SKonstantin Komarov 			dev_size = dev_size0; /* restore original size. */
113815735a62SKonstantin Komarov 			goto read_boot;
11396a4cd3eaSKonstantin Komarov 		}
114015735a62SKonstantin Komarov 	}
114182cae269SKonstantin Komarov 
114282cae269SKonstantin Komarov 	return err;
114382cae269SKonstantin Komarov }
114482cae269SKonstantin Komarov 
1145e8b8e97fSKari Argillander /*
1146e8b8e97fSKari Argillander  * ntfs_fill_super - Try to mount.
1147e8b8e97fSKari Argillander  */
ntfs_fill_super(struct super_block * sb,struct fs_context * fc)1148610f8f5aSKari Argillander static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
114982cae269SKonstantin Komarov {
115082cae269SKonstantin Komarov 	int err;
1151610f8f5aSKari Argillander 	struct ntfs_sb_info *sbi = sb->s_fs_info;
115282cae269SKonstantin Komarov 	struct block_device *bdev = sb->s_bdev;
1153e43f6ec2SKonstantin Komarov 	struct ntfs_mount_options *options;
115410b4f12cSKari Argillander 	struct inode *inode;
115582cae269SKonstantin Komarov 	struct ntfs_inode *ni;
1156ec5fc720SKonstantin Komarov 	size_t i, tt, bad_len, bad_frags;
115782cae269SKonstantin Komarov 	CLST vcn, lcn, len;
115882cae269SKonstantin Komarov 	struct ATTRIB *attr;
115982cae269SKonstantin Komarov 	const struct VOLUME_INFO *info;
116082cae269SKonstantin Komarov 	u32 idx, done, bytes;
116182cae269SKonstantin Komarov 	struct ATTR_DEF_ENTRY *t;
116282cae269SKonstantin Komarov 	u16 *shared;
116382cae269SKonstantin Komarov 	struct MFT_REF ref;
11646a4cd3eaSKonstantin Komarov 	bool ro = sb_rdonly(sb);
1165f1d325b8SKonstantin Komarov 	struct NTFS_BOOT *boot2 = NULL;
116682cae269SKonstantin Komarov 
116782cae269SKonstantin Komarov 	ref.high = 0;
116882cae269SKonstantin Komarov 
116982cae269SKonstantin Komarov 	sbi->sb = sb;
1170e43f6ec2SKonstantin Komarov 	sbi->options = options = fc->fs_private;
1171cd39981fSKonstantin Komarov 	fc->fs_private = NULL;
117282cae269SKonstantin Komarov 	sb->s_flags |= SB_NODIRATIME;
117382cae269SKonstantin Komarov 	sb->s_magic = 0x7366746e; // "ntfs"
117482cae269SKonstantin Komarov 	sb->s_op = &ntfs_sops;
117582cae269SKonstantin Komarov 	sb->s_export_op = &ntfs_export_ops;
117682cae269SKonstantin Komarov 	sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec
117782cae269SKonstantin Komarov 	sb->s_xattr = ntfs_xattr_handlers;
1178e43f6ec2SKonstantin Komarov 	sb->s_d_op = options->nocase ? &ntfs_dentry_ops : NULL;
117982cae269SKonstantin Komarov 
1180e43f6ec2SKonstantin Komarov 	options->nls = ntfs_load_nls(options->nls_name);
1181e43f6ec2SKonstantin Komarov 	if (IS_ERR(options->nls)) {
1182e43f6ec2SKonstantin Komarov 		options->nls = NULL;
1183e43f6ec2SKonstantin Komarov 		errorf(fc, "Cannot load nls %s", options->nls_name);
11849b75450dSKonstantin Komarov 		err = -EINVAL;
11859b75450dSKonstantin Komarov 		goto out;
1186610f8f5aSKari Argillander 	}
118782cae269SKonstantin Komarov 
11887b47ef52SChristoph Hellwig 	if (bdev_max_discard_sectors(bdev) && bdev_discard_granularity(bdev)) {
11897b47ef52SChristoph Hellwig 		sbi->discard_granularity = bdev_discard_granularity(bdev);
119082cae269SKonstantin Komarov 		sbi->discard_granularity_mask_inv =
119182cae269SKonstantin Komarov 			~(u64)(sbi->discard_granularity - 1);
119282cae269SKonstantin Komarov 	}
119382cae269SKonstantin Komarov 
1194e8b8e97fSKari Argillander 	/* Parse boot. */
1195f09dac9aSChristoph Hellwig 	err = ntfs_init_from_boot(sb, bdev_logical_block_size(bdev),
1196f1d325b8SKonstantin Komarov 				  bdev_nr_bytes(bdev), &boot2);
119782cae269SKonstantin Komarov 	if (err)
11989b75450dSKonstantin Komarov 		goto out;
119982cae269SKonstantin Komarov 
120082cae269SKonstantin Komarov 	/*
1201e8b8e97fSKari Argillander 	 * Load $Volume. This should be done before $LogFile
1202e8b8e97fSKari Argillander 	 * 'cause 'sbi->volume.ni' is used 'ntfs_set_state'.
120382cae269SKonstantin Komarov 	 */
120482cae269SKonstantin Komarov 	ref.low = cpu_to_le32(MFT_REC_VOL);
120582cae269SKonstantin Komarov 	ref.seq = cpu_to_le16(MFT_REC_VOL);
120682cae269SKonstantin Komarov 	inode = ntfs_iget5(sb, &ref, &NAME_VOLUME);
120782cae269SKonstantin Komarov 	if (IS_ERR(inode)) {
12089b75450dSKonstantin Komarov 		err = PTR_ERR(inode);
1209e43f6ec2SKonstantin Komarov 		ntfs_err(sb, "Failed to load $Volume (%d).", err);
12109b75450dSKonstantin Komarov 		goto out;
121182cae269SKonstantin Komarov 	}
121282cae269SKonstantin Komarov 
121382cae269SKonstantin Komarov 	ni = ntfs_i(inode);
121482cae269SKonstantin Komarov 
1215e8b8e97fSKari Argillander 	/* Load and save label (not necessary). */
121682cae269SKonstantin Komarov 	attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL);
121782cae269SKonstantin Komarov 
121882cae269SKonstantin Komarov 	if (!attr) {
121982cae269SKonstantin Komarov 		/* It is ok if no ATTR_LABEL */
122082cae269SKonstantin Komarov 	} else if (!attr->non_res && !is_attr_ext(attr)) {
1221e8b8e97fSKari Argillander 		/* $AttrDef allows labels to be up to 128 symbols. */
122282cae269SKonstantin Komarov 		err = utf16s_to_utf8s(resident_data(attr),
122382cae269SKonstantin Komarov 				      le32_to_cpu(attr->res.data_size) >> 1,
122482cae269SKonstantin Komarov 				      UTF16_LITTLE_ENDIAN, sbi->volume.label,
122582cae269SKonstantin Komarov 				      sizeof(sbi->volume.label));
122682cae269SKonstantin Komarov 		if (err < 0)
122782cae269SKonstantin Komarov 			sbi->volume.label[0] = 0;
122882cae269SKonstantin Komarov 	} else {
1229e8b8e97fSKari Argillander 		/* Should we break mounting here? */
123082cae269SKonstantin Komarov 		//err = -EINVAL;
12319b75450dSKonstantin Komarov 		//goto put_inode_out;
123282cae269SKonstantin Komarov 	}
123382cae269SKonstantin Komarov 
123482cae269SKonstantin Komarov 	attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL);
1235e43f6ec2SKonstantin Komarov 	if (!attr || is_attr_ext(attr) ||
1236e43f6ec2SKonstantin Komarov 	    !(info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO))) {
1237e43f6ec2SKonstantin Komarov 		ntfs_err(sb, "$Volume is corrupted.");
123882cae269SKonstantin Komarov 		err = -EINVAL;
12399b75450dSKonstantin Komarov 		goto put_inode_out;
124082cae269SKonstantin Komarov 	}
124182cae269SKonstantin Komarov 
124282cae269SKonstantin Komarov 	sbi->volume.major_ver = info->major_ver;
124382cae269SKonstantin Komarov 	sbi->volume.minor_ver = info->minor_ver;
124482cae269SKonstantin Komarov 	sbi->volume.flags = info->flags;
124582cae269SKonstantin Komarov 	sbi->volume.ni = ni;
12466a4cd3eaSKonstantin Komarov 	if (info->flags & VOLUME_FLAG_DIRTY) {
12476a4cd3eaSKonstantin Komarov 		sbi->volume.real_dirty = true;
12486a4cd3eaSKonstantin Komarov 		ntfs_info(sb, "It is recommened to use chkdsk.");
12496a4cd3eaSKonstantin Komarov 	}
125082cae269SKonstantin Komarov 
1251e8b8e97fSKari Argillander 	/* Load $MFTMirr to estimate recs_mirr. */
125282cae269SKonstantin Komarov 	ref.low = cpu_to_le32(MFT_REC_MIRR);
125382cae269SKonstantin Komarov 	ref.seq = cpu_to_le16(MFT_REC_MIRR);
125482cae269SKonstantin Komarov 	inode = ntfs_iget5(sb, &ref, &NAME_MIRROR);
125582cae269SKonstantin Komarov 	if (IS_ERR(inode)) {
12569b75450dSKonstantin Komarov 		err = PTR_ERR(inode);
1257e43f6ec2SKonstantin Komarov 		ntfs_err(sb, "Failed to load $MFTMirr (%d).", err);
12589b75450dSKonstantin Komarov 		goto out;
125982cae269SKonstantin Komarov 	}
126082cae269SKonstantin Komarov 
1261e43f6ec2SKonstantin Komarov 	sbi->mft.recs_mirr = ntfs_up_cluster(sbi, inode->i_size) >>
1262e43f6ec2SKonstantin Komarov 			     sbi->record_bits;
126382cae269SKonstantin Komarov 
126482cae269SKonstantin Komarov 	iput(inode);
126582cae269SKonstantin Komarov 
1266d3624466SKonstantin Komarov 	/* Load LogFile to replay. */
126782cae269SKonstantin Komarov 	ref.low = cpu_to_le32(MFT_REC_LOG);
126882cae269SKonstantin Komarov 	ref.seq = cpu_to_le16(MFT_REC_LOG);
126982cae269SKonstantin Komarov 	inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE);
127082cae269SKonstantin Komarov 	if (IS_ERR(inode)) {
12719b75450dSKonstantin Komarov 		err = PTR_ERR(inode);
1272e43f6ec2SKonstantin Komarov 		ntfs_err(sb, "Failed to load \x24LogFile (%d).", err);
12739b75450dSKonstantin Komarov 		goto out;
127482cae269SKonstantin Komarov 	}
127582cae269SKonstantin Komarov 
127682cae269SKonstantin Komarov 	ni = ntfs_i(inode);
127782cae269SKonstantin Komarov 
127882cae269SKonstantin Komarov 	err = ntfs_loadlog_and_replay(ni, sbi);
127982cae269SKonstantin Komarov 	if (err)
12809b75450dSKonstantin Komarov 		goto put_inode_out;
128182cae269SKonstantin Komarov 
128282cae269SKonstantin Komarov 	iput(inode);
128382cae269SKonstantin Komarov 
12846a4cd3eaSKonstantin Komarov 	if ((sbi->flags & NTFS_FLAGS_NEED_REPLAY) && !ro) {
12856a4cd3eaSKonstantin Komarov 		ntfs_warn(sb, "failed to replay log file. Can't mount rw!");
12869b75450dSKonstantin Komarov 		err = -EINVAL;
12879b75450dSKonstantin Komarov 		goto out;
128882cae269SKonstantin Komarov 	}
12896a4cd3eaSKonstantin Komarov 
12906a4cd3eaSKonstantin Komarov 	if ((sbi->volume.flags & VOLUME_FLAG_DIRTY) && !ro && !options->force) {
12916a4cd3eaSKonstantin Komarov 		ntfs_warn(sb, "volume is dirty and \"force\" flag is not set!");
12929b75450dSKonstantin Komarov 		err = -EINVAL;
12939b75450dSKonstantin Komarov 		goto out;
129482cae269SKonstantin Komarov 	}
129582cae269SKonstantin Komarov 
1296e8b8e97fSKari Argillander 	/* Load $MFT. */
129782cae269SKonstantin Komarov 	ref.low = cpu_to_le32(MFT_REC_MFT);
129882cae269SKonstantin Komarov 	ref.seq = cpu_to_le16(1);
129982cae269SKonstantin Komarov 
130082cae269SKonstantin Komarov 	inode = ntfs_iget5(sb, &ref, &NAME_MFT);
130182cae269SKonstantin Komarov 	if (IS_ERR(inode)) {
13029b75450dSKonstantin Komarov 		err = PTR_ERR(inode);
1303e43f6ec2SKonstantin Komarov 		ntfs_err(sb, "Failed to load $MFT (%d).", err);
13049b75450dSKonstantin Komarov 		goto out;
130582cae269SKonstantin Komarov 	}
130682cae269SKonstantin Komarov 
130782cae269SKonstantin Komarov 	ni = ntfs_i(inode);
130882cae269SKonstantin Komarov 
130982cae269SKonstantin Komarov 	sbi->mft.used = ni->i_valid >> sbi->record_bits;
131082cae269SKonstantin Komarov 	tt = inode->i_size >> sbi->record_bits;
131182cae269SKonstantin Komarov 	sbi->mft.next_free = MFT_REC_USER;
131282cae269SKonstantin Komarov 
131382cae269SKonstantin Komarov 	err = wnd_init(&sbi->mft.bitmap, sb, tt);
131482cae269SKonstantin Komarov 	if (err)
13159b75450dSKonstantin Komarov 		goto put_inode_out;
131682cae269SKonstantin Komarov 
131782cae269SKonstantin Komarov 	err = ni_load_all_mi(ni);
1318e43f6ec2SKonstantin Komarov 	if (err) {
1319e43f6ec2SKonstantin Komarov 		ntfs_err(sb, "Failed to load $MFT's subrecords (%d).", err);
13209b75450dSKonstantin Komarov 		goto put_inode_out;
1321e43f6ec2SKonstantin Komarov 	}
132282cae269SKonstantin Komarov 
132382cae269SKonstantin Komarov 	sbi->mft.ni = ni;
132482cae269SKonstantin Komarov 
1325e8b8e97fSKari Argillander 	/* Load $Bitmap. */
132682cae269SKonstantin Komarov 	ref.low = cpu_to_le32(MFT_REC_BITMAP);
132782cae269SKonstantin Komarov 	ref.seq = cpu_to_le16(MFT_REC_BITMAP);
132882cae269SKonstantin Komarov 	inode = ntfs_iget5(sb, &ref, &NAME_BITMAP);
132982cae269SKonstantin Komarov 	if (IS_ERR(inode)) {
13309b75450dSKonstantin Komarov 		err = PTR_ERR(inode);
1331e43f6ec2SKonstantin Komarov 		ntfs_err(sb, "Failed to load $Bitmap (%d).", err);
13329b75450dSKonstantin Komarov 		goto out;
133382cae269SKonstantin Komarov 	}
133482cae269SKonstantin Komarov 
133582cae269SKonstantin Komarov #ifndef CONFIG_NTFS3_64BIT_CLUSTER
133682cae269SKonstantin Komarov 	if (inode->i_size >> 32) {
133782cae269SKonstantin Komarov 		err = -EINVAL;
13389b75450dSKonstantin Komarov 		goto put_inode_out;
133982cae269SKonstantin Komarov 	}
134082cae269SKonstantin Komarov #endif
134182cae269SKonstantin Komarov 
1342e8b8e97fSKari Argillander 	/* Check bitmap boundary. */
134382cae269SKonstantin Komarov 	tt = sbi->used.bitmap.nbits;
134482cae269SKonstantin Komarov 	if (inode->i_size < bitmap_size(tt)) {
1345e43f6ec2SKonstantin Komarov 		ntfs_err(sb, "$Bitmap is corrupted.");
134682cae269SKonstantin Komarov 		err = -EINVAL;
13479b75450dSKonstantin Komarov 		goto put_inode_out;
134882cae269SKonstantin Komarov 	}
134982cae269SKonstantin Komarov 
1350b4f110d6SKari Argillander 	err = wnd_init(&sbi->used.bitmap, sb, tt);
1351e43f6ec2SKonstantin Komarov 	if (err) {
1352e43f6ec2SKonstantin Komarov 		ntfs_err(sb, "Failed to initialize $Bitmap (%d).", err);
13539b75450dSKonstantin Komarov 		goto put_inode_out;
1354e43f6ec2SKonstantin Komarov 	}
135582cae269SKonstantin Komarov 
135682cae269SKonstantin Komarov 	iput(inode);
135782cae269SKonstantin Komarov 
1358e8b8e97fSKari Argillander 	/* Compute the MFT zone. */
135982cae269SKonstantin Komarov 	err = ntfs_refresh_zone(sbi);
1360e43f6ec2SKonstantin Komarov 	if (err) {
1361e43f6ec2SKonstantin Komarov 		ntfs_err(sb, "Failed to initialize MFT zone (%d).", err);
13629b75450dSKonstantin Komarov 		goto out;
1363e43f6ec2SKonstantin Komarov 	}
136482cae269SKonstantin Komarov 
1365ec5fc720SKonstantin Komarov 	/* Load $BadClus. */
1366ec5fc720SKonstantin Komarov 	ref.low = cpu_to_le32(MFT_REC_BADCLUST);
1367ec5fc720SKonstantin Komarov 	ref.seq = cpu_to_le16(MFT_REC_BADCLUST);
1368ec5fc720SKonstantin Komarov 	inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS);
1369ec5fc720SKonstantin Komarov 	if (IS_ERR(inode)) {
1370ec5fc720SKonstantin Komarov 		err = PTR_ERR(inode);
1371ec5fc720SKonstantin Komarov 		ntfs_err(sb, "Failed to load $BadClus (%d).", err);
1372ec5fc720SKonstantin Komarov 		goto out;
1373ec5fc720SKonstantin Komarov 	}
1374ec5fc720SKonstantin Komarov 
1375ec5fc720SKonstantin Komarov 	ni = ntfs_i(inode);
1376ec5fc720SKonstantin Komarov 	bad_len = bad_frags = 0;
1377ec5fc720SKonstantin Komarov 	for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) {
1378ec5fc720SKonstantin Komarov 		if (lcn == SPARSE_LCN)
1379ec5fc720SKonstantin Komarov 			continue;
1380ec5fc720SKonstantin Komarov 
1381ec5fc720SKonstantin Komarov 		bad_len += len;
1382ec5fc720SKonstantin Komarov 		bad_frags += 1;
13836a4cd3eaSKonstantin Komarov 		if (ro)
1384ec5fc720SKonstantin Komarov 			continue;
1385ec5fc720SKonstantin Komarov 
1386ec5fc720SKonstantin Komarov 		if (wnd_set_used_safe(&sbi->used.bitmap, lcn, len, &tt) || tt) {
1387ec5fc720SKonstantin Komarov 			/* Bad blocks marked as free in bitmap. */
1388ec5fc720SKonstantin Komarov 			ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1389ec5fc720SKonstantin Komarov 		}
1390ec5fc720SKonstantin Komarov 	}
1391ec5fc720SKonstantin Komarov 	if (bad_len) {
1392ec5fc720SKonstantin Komarov 		/*
1393ec5fc720SKonstantin Komarov 		 * Notice about bad blocks.
1394ec5fc720SKonstantin Komarov 		 * In normal cases these blocks are marked as used in bitmap.
1395ec5fc720SKonstantin Komarov 		 * And we never allocate space in it.
1396ec5fc720SKonstantin Komarov 		 */
1397ec5fc720SKonstantin Komarov 		ntfs_notice(sb,
1398ec5fc720SKonstantin Komarov 			    "Volume contains %zu bad blocks in %zu fragments.",
1399ec5fc720SKonstantin Komarov 			    bad_len, bad_frags);
1400ec5fc720SKonstantin Komarov 	}
1401ec5fc720SKonstantin Komarov 	iput(inode);
1402ec5fc720SKonstantin Komarov 
1403e8b8e97fSKari Argillander 	/* Load $AttrDef. */
140482cae269SKonstantin Komarov 	ref.low = cpu_to_le32(MFT_REC_ATTR);
140582cae269SKonstantin Komarov 	ref.seq = cpu_to_le16(MFT_REC_ATTR);
1406b4f110d6SKari Argillander 	inode = ntfs_iget5(sb, &ref, &NAME_ATTRDEF);
140782cae269SKonstantin Komarov 	if (IS_ERR(inode)) {
14089b75450dSKonstantin Komarov 		err = PTR_ERR(inode);
1409e43f6ec2SKonstantin Komarov 		ntfs_err(sb, "Failed to load $AttrDef (%d)", err);
14109b75450dSKonstantin Komarov 		goto out;
141182cae269SKonstantin Komarov 	}
141282cae269SKonstantin Komarov 
1413318d016eSKonstantin Komarov 	/*
1414318d016eSKonstantin Komarov 	 * Typical $AttrDef contains up to 20 entries.
141530200ef8SKonstantin Komarov 	 * Check for extremely large/small size.
1416318d016eSKonstantin Komarov 	 */
1417318d016eSKonstantin Komarov 	if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY) ||
1418318d016eSKonstantin Komarov 	    inode->i_size > 100 * sizeof(struct ATTR_DEF_ENTRY)) {
1419318d016eSKonstantin Komarov 		ntfs_err(sb, "Looks like $AttrDef is corrupted (size=%llu).",
1420318d016eSKonstantin Komarov 			 inode->i_size);
142182cae269SKonstantin Komarov 		err = -EINVAL;
14229b75450dSKonstantin Komarov 		goto put_inode_out;
142382cae269SKonstantin Komarov 	}
1424318d016eSKonstantin Komarov 
142582cae269SKonstantin Komarov 	bytes = inode->i_size;
1426fc471e39SKonstantin Komarov 	sbi->def_table = t = kvmalloc(bytes, GFP_KERNEL);
142782cae269SKonstantin Komarov 	if (!t) {
142882cae269SKonstantin Komarov 		err = -ENOMEM;
14299b75450dSKonstantin Komarov 		goto put_inode_out;
143082cae269SKonstantin Komarov 	}
143182cae269SKonstantin Komarov 
143282cae269SKonstantin Komarov 	for (done = idx = 0; done < bytes; done += PAGE_SIZE, idx++) {
143382cae269SKonstantin Komarov 		unsigned long tail = bytes - done;
143482cae269SKonstantin Komarov 		struct page *page = ntfs_map_page(inode->i_mapping, idx);
143582cae269SKonstantin Komarov 
143682cae269SKonstantin Komarov 		if (IS_ERR(page)) {
143782cae269SKonstantin Komarov 			err = PTR_ERR(page);
1438e43f6ec2SKonstantin Komarov 			ntfs_err(sb, "Failed to read $AttrDef (%d).", err);
14399b75450dSKonstantin Komarov 			goto put_inode_out;
144082cae269SKonstantin Komarov 		}
144182cae269SKonstantin Komarov 		memcpy(Add2Ptr(t, done), page_address(page),
144282cae269SKonstantin Komarov 		       min(PAGE_SIZE, tail));
144382cae269SKonstantin Komarov 		ntfs_unmap_page(page);
144482cae269SKonstantin Komarov 
144582cae269SKonstantin Komarov 		if (!idx && ATTR_STD != t->type) {
1446e43f6ec2SKonstantin Komarov 			ntfs_err(sb, "$AttrDef is corrupted.");
144782cae269SKonstantin Komarov 			err = -EINVAL;
14489b75450dSKonstantin Komarov 			goto put_inode_out;
144982cae269SKonstantin Komarov 		}
145082cae269SKonstantin Komarov 	}
145182cae269SKonstantin Komarov 
145282cae269SKonstantin Komarov 	t += 1;
145382cae269SKonstantin Komarov 	sbi->def_entries = 1;
145482cae269SKonstantin Komarov 	done = sizeof(struct ATTR_DEF_ENTRY);
145582cae269SKonstantin Komarov 	sbi->reparse.max_size = MAXIMUM_REPARSE_DATA_BUFFER_SIZE;
1456f8d87ed9SColin Ian King 	sbi->ea_max_size = 0x10000; /* default formatter value */
145782cae269SKonstantin Komarov 
145882cae269SKonstantin Komarov 	while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) {
145982cae269SKonstantin Komarov 		u32 t32 = le32_to_cpu(t->type);
146082cae269SKonstantin Komarov 		u64 sz = le64_to_cpu(t->max_sz);
146182cae269SKonstantin Komarov 
146282cae269SKonstantin Komarov 		if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32)
146382cae269SKonstantin Komarov 			break;
146482cae269SKonstantin Komarov 
146582cae269SKonstantin Komarov 		if (t->type == ATTR_REPARSE)
146682cae269SKonstantin Komarov 			sbi->reparse.max_size = sz;
146782cae269SKonstantin Komarov 		else if (t->type == ATTR_EA)
146882cae269SKonstantin Komarov 			sbi->ea_max_size = sz;
146982cae269SKonstantin Komarov 
147082cae269SKonstantin Komarov 		done += sizeof(struct ATTR_DEF_ENTRY);
147182cae269SKonstantin Komarov 		t += 1;
147282cae269SKonstantin Komarov 		sbi->def_entries += 1;
147382cae269SKonstantin Komarov 	}
147482cae269SKonstantin Komarov 	iput(inode);
147582cae269SKonstantin Komarov 
1476e8b8e97fSKari Argillander 	/* Load $UpCase. */
147782cae269SKonstantin Komarov 	ref.low = cpu_to_le32(MFT_REC_UPCASE);
147882cae269SKonstantin Komarov 	ref.seq = cpu_to_le16(MFT_REC_UPCASE);
147982cae269SKonstantin Komarov 	inode = ntfs_iget5(sb, &ref, &NAME_UPCASE);
148082cae269SKonstantin Komarov 	if (IS_ERR(inode)) {
14819b75450dSKonstantin Komarov 		err = PTR_ERR(inode);
1482e43f6ec2SKonstantin Komarov 		ntfs_err(sb, "Failed to load $UpCase (%d).", err);
14839b75450dSKonstantin Komarov 		goto out;
148482cae269SKonstantin Komarov 	}
148582cae269SKonstantin Komarov 
148682cae269SKonstantin Komarov 	if (inode->i_size != 0x10000 * sizeof(short)) {
148782cae269SKonstantin Komarov 		err = -EINVAL;
1488e43f6ec2SKonstantin Komarov 		ntfs_err(sb, "$UpCase is corrupted.");
14899b75450dSKonstantin Komarov 		goto put_inode_out;
149082cae269SKonstantin Komarov 	}
149182cae269SKonstantin Komarov 
149282cae269SKonstantin Komarov 	for (idx = 0; idx < (0x10000 * sizeof(short) >> PAGE_SHIFT); idx++) {
149382cae269SKonstantin Komarov 		const __le16 *src;
14940056b273SKari Argillander 		u16 *dst = Add2Ptr(sbi->upcase, idx << PAGE_SHIFT);
149582cae269SKonstantin Komarov 		struct page *page = ntfs_map_page(inode->i_mapping, idx);
149682cae269SKonstantin Komarov 
149782cae269SKonstantin Komarov 		if (IS_ERR(page)) {
149882cae269SKonstantin Komarov 			err = PTR_ERR(page);
1499e43f6ec2SKonstantin Komarov 			ntfs_err(sb, "Failed to read $UpCase (%d).", err);
15009b75450dSKonstantin Komarov 			goto put_inode_out;
150182cae269SKonstantin Komarov 		}
150282cae269SKonstantin Komarov 
150382cae269SKonstantin Komarov 		src = page_address(page);
150482cae269SKonstantin Komarov 
150582cae269SKonstantin Komarov #ifdef __BIG_ENDIAN
150682cae269SKonstantin Komarov 		for (i = 0; i < PAGE_SIZE / sizeof(u16); i++)
150782cae269SKonstantin Komarov 			*dst++ = le16_to_cpu(*src++);
150882cae269SKonstantin Komarov #else
150982cae269SKonstantin Komarov 		memcpy(dst, src, PAGE_SIZE);
151082cae269SKonstantin Komarov #endif
151182cae269SKonstantin Komarov 		ntfs_unmap_page(page);
151282cae269SKonstantin Komarov 	}
151382cae269SKonstantin Komarov 
15140056b273SKari Argillander 	shared = ntfs_set_shared(sbi->upcase, 0x10000 * sizeof(short));
15150056b273SKari Argillander 	if (shared && sbi->upcase != shared) {
15160056b273SKari Argillander 		kvfree(sbi->upcase);
151782cae269SKonstantin Komarov 		sbi->upcase = shared;
151882cae269SKonstantin Komarov 	}
151982cae269SKonstantin Komarov 
152082cae269SKonstantin Komarov 	iput(inode);
152182cae269SKonstantin Komarov 
152282cae269SKonstantin Komarov 	if (is_ntfs3(sbi)) {
1523e8b8e97fSKari Argillander 		/* Load $Secure. */
152482cae269SKonstantin Komarov 		err = ntfs_security_init(sbi);
1525e43f6ec2SKonstantin Komarov 		if (err) {
1526e43f6ec2SKonstantin Komarov 			ntfs_err(sb, "Failed to initialize $Secure (%d).", err);
15279b75450dSKonstantin Komarov 			goto out;
1528e43f6ec2SKonstantin Komarov 		}
152982cae269SKonstantin Komarov 
1530e8b8e97fSKari Argillander 		/* Load $Extend. */
153182cae269SKonstantin Komarov 		err = ntfs_extend_init(sbi);
1532e43f6ec2SKonstantin Komarov 		if (err) {
1533e43f6ec2SKonstantin Komarov 			ntfs_warn(sb, "Failed to initialize $Extend.");
153482cae269SKonstantin Komarov 			goto load_root;
1535e43f6ec2SKonstantin Komarov 		}
153682cae269SKonstantin Komarov 
1537e43f6ec2SKonstantin Komarov 		/* Load $Extend/$Reparse. */
153882cae269SKonstantin Komarov 		err = ntfs_reparse_init(sbi);
1539e43f6ec2SKonstantin Komarov 		if (err) {
1540e43f6ec2SKonstantin Komarov 			ntfs_warn(sb, "Failed to initialize $Extend/$Reparse.");
154182cae269SKonstantin Komarov 			goto load_root;
1542e43f6ec2SKonstantin Komarov 		}
154382cae269SKonstantin Komarov 
1544e43f6ec2SKonstantin Komarov 		/* Load $Extend/$ObjId. */
154582cae269SKonstantin Komarov 		err = ntfs_objid_init(sbi);
1546e43f6ec2SKonstantin Komarov 		if (err) {
1547e43f6ec2SKonstantin Komarov 			ntfs_warn(sb, "Failed to initialize $Extend/$ObjId.");
154882cae269SKonstantin Komarov 			goto load_root;
154982cae269SKonstantin Komarov 		}
1550e43f6ec2SKonstantin Komarov 	}
155182cae269SKonstantin Komarov 
155282cae269SKonstantin Komarov load_root:
1553e8b8e97fSKari Argillander 	/* Load root. */
155482cae269SKonstantin Komarov 	ref.low = cpu_to_le32(MFT_REC_ROOT);
155582cae269SKonstantin Komarov 	ref.seq = cpu_to_le16(MFT_REC_ROOT);
155682cae269SKonstantin Komarov 	inode = ntfs_iget5(sb, &ref, &NAME_ROOT);
1557788ee160SKonstantin Komarov 	if (IS_ERR(inode)) {
1558e43f6ec2SKonstantin Komarov 		err = PTR_ERR(inode);
1559e43f6ec2SKonstantin Komarov 		ntfs_err(sb, "Failed to load root (%d).", err);
15609b75450dSKonstantin Komarov 		goto out;
156182cae269SKonstantin Komarov 	}
156282cae269SKonstantin Komarov 
1563788ee160SKonstantin Komarov 	/*
1564788ee160SKonstantin Komarov 	 * Final check. Looks like this case should never occurs.
1565788ee160SKonstantin Komarov 	 */
1566788ee160SKonstantin Komarov 	if (!inode->i_op) {
1567788ee160SKonstantin Komarov 		err = -EINVAL;
1568788ee160SKonstantin Komarov 		ntfs_err(sb, "Failed to load root (%d).", err);
1569788ee160SKonstantin Komarov 		goto put_inode_out;
1570788ee160SKonstantin Komarov 	}
1571788ee160SKonstantin Komarov 
157282cae269SKonstantin Komarov 	sb->s_root = d_make_root(inode);
15739b75450dSKonstantin Komarov 	if (!sb->s_root) {
15749b75450dSKonstantin Komarov 		err = -ENOMEM;
15759b75450dSKonstantin Komarov 		goto put_inode_out;
15769b75450dSKonstantin Komarov 	}
157782cae269SKonstantin Komarov 
1578f1d325b8SKonstantin Komarov 	if (boot2) {
1579f1d325b8SKonstantin Komarov 		/*
1580f1d325b8SKonstantin Komarov 	 	 * Alternative boot is ok but primary is not ok.
1581f1d325b8SKonstantin Komarov 	 	 * Volume is recognized as NTFS. Update primary boot.
1582f1d325b8SKonstantin Komarov 		 */
1583f1d325b8SKonstantin Komarov 		struct buffer_head *bh0 = sb_getblk(sb, 0);
1584f1d325b8SKonstantin Komarov 		if (bh0) {
1585f1d325b8SKonstantin Komarov 			if (buffer_locked(bh0))
1586f1d325b8SKonstantin Komarov 				__wait_on_buffer(bh0);
1587f1d325b8SKonstantin Komarov 
1588f1d325b8SKonstantin Komarov 			lock_buffer(bh0);
1589f1d325b8SKonstantin Komarov 			memcpy(bh0->b_data, boot2, sizeof(*boot2));
1590f1d325b8SKonstantin Komarov 			set_buffer_uptodate(bh0);
1591f1d325b8SKonstantin Komarov 			mark_buffer_dirty(bh0);
1592f1d325b8SKonstantin Komarov 			unlock_buffer(bh0);
1593f1d325b8SKonstantin Komarov 			if (!sync_dirty_buffer(bh0))
1594f1d325b8SKonstantin Komarov 				ntfs_warn(sb, "primary boot is updated");
1595f1d325b8SKonstantin Komarov 			put_bh(bh0);
1596f1d325b8SKonstantin Komarov 		}
1597f1d325b8SKonstantin Komarov 
1598f1d325b8SKonstantin Komarov 		kfree(boot2);
1599f1d325b8SKonstantin Komarov 	}
1600f1d325b8SKonstantin Komarov 
16017832e123SKonstantin Komarov #ifdef CONFIG_PROC_FS
16027832e123SKonstantin Komarov 	/* Create /proc/fs/ntfs3/.. */
16037832e123SKonstantin Komarov 	if (proc_info_root) {
16047832e123SKonstantin Komarov 		struct proc_dir_entry *e = proc_mkdir(sb->s_id, proc_info_root);
160544b4494dSKonstantin Komarov 		static_assert((S_IRUGO | S_IWUSR) == 0644);
16067832e123SKonstantin Komarov 		if (e) {
160744b4494dSKonstantin Komarov 			proc_create_data("volinfo", S_IRUGO, e,
16087832e123SKonstantin Komarov 					 &ntfs3_volinfo_fops, sb);
160944b4494dSKonstantin Komarov 			proc_create_data("label", S_IRUGO | S_IWUSR, e,
161044b4494dSKonstantin Komarov 					 &ntfs3_label_fops, sb);
16117832e123SKonstantin Komarov 			sbi->procdir = e;
16127832e123SKonstantin Komarov 		}
16137832e123SKonstantin Komarov 	}
16147832e123SKonstantin Komarov #endif
16157832e123SKonstantin Komarov 
161682cae269SKonstantin Komarov 	return 0;
16179b75450dSKonstantin Komarov 
16189b75450dSKonstantin Komarov put_inode_out:
161982cae269SKonstantin Komarov 	iput(inode);
16209b75450dSKonstantin Komarov out:
1621493c7192SChristian Brauner 	ntfs3_put_sbi(sbi);
1622f1d325b8SKonstantin Komarov 	kfree(boot2);
16234ad5c924SKonstantin Komarov 	ntfs3_put_sbi(sbi);
162482cae269SKonstantin Komarov 	return err;
162582cae269SKonstantin Komarov }
162682cae269SKonstantin Komarov 
ntfs_unmap_meta(struct super_block * sb,CLST lcn,CLST len)162782cae269SKonstantin Komarov void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len)
162882cae269SKonstantin Komarov {
162982cae269SKonstantin Komarov 	struct ntfs_sb_info *sbi = sb->s_fs_info;
163082cae269SKonstantin Komarov 	struct block_device *bdev = sb->s_bdev;
163182cae269SKonstantin Komarov 	sector_t devblock = (u64)lcn * sbi->blocks_per_cluster;
163282cae269SKonstantin Komarov 	unsigned long blocks = (u64)len * sbi->blocks_per_cluster;
163382cae269SKonstantin Komarov 	unsigned long cnt = 0;
163482cae269SKonstantin Komarov 	unsigned long limit = global_zone_page_state(NR_FREE_PAGES)
163582cae269SKonstantin Komarov 			      << (PAGE_SHIFT - sb->s_blocksize_bits);
163682cae269SKonstantin Komarov 
163782cae269SKonstantin Komarov 	if (limit >= 0x2000)
163882cae269SKonstantin Komarov 		limit -= 0x1000;
163982cae269SKonstantin Komarov 	else if (limit < 32)
164082cae269SKonstantin Komarov 		limit = 32;
164182cae269SKonstantin Komarov 	else
164282cae269SKonstantin Komarov 		limit >>= 1;
164382cae269SKonstantin Komarov 
164482cae269SKonstantin Komarov 	while (blocks--) {
164582cae269SKonstantin Komarov 		clean_bdev_aliases(bdev, devblock++, 1);
164682cae269SKonstantin Komarov 		if (cnt++ >= limit) {
164782cae269SKonstantin Komarov 			sync_blockdev(bdev);
164882cae269SKonstantin Komarov 			cnt = 0;
164982cae269SKonstantin Komarov 		}
165082cae269SKonstantin Komarov 	}
165182cae269SKonstantin Komarov }
165282cae269SKonstantin Komarov 
165382cae269SKonstantin Komarov /*
1654e8b8e97fSKari Argillander  * ntfs_discard - Issue a discard request (trim for SSD).
165582cae269SKonstantin Komarov  */
ntfs_discard(struct ntfs_sb_info * sbi,CLST lcn,CLST len)165682cae269SKonstantin Komarov int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len)
165782cae269SKonstantin Komarov {
165882cae269SKonstantin Komarov 	int err;
165982cae269SKonstantin Komarov 	u64 lbo, bytes, start, end;
166082cae269SKonstantin Komarov 	struct super_block *sb;
166182cae269SKonstantin Komarov 
166282cae269SKonstantin Komarov 	if (sbi->used.next_free_lcn == lcn + len)
166382cae269SKonstantin Komarov 		sbi->used.next_free_lcn = lcn;
166482cae269SKonstantin Komarov 
166582cae269SKonstantin Komarov 	if (sbi->flags & NTFS_FLAGS_NODISCARD)
166682cae269SKonstantin Komarov 		return -EOPNOTSUPP;
166782cae269SKonstantin Komarov 
1668564c97bdSKari Argillander 	if (!sbi->options->discard)
166982cae269SKonstantin Komarov 		return -EOPNOTSUPP;
167082cae269SKonstantin Komarov 
167182cae269SKonstantin Komarov 	lbo = (u64)lcn << sbi->cluster_bits;
167282cae269SKonstantin Komarov 	bytes = (u64)len << sbi->cluster_bits;
167382cae269SKonstantin Komarov 
1674e8b8e97fSKari Argillander 	/* Align up 'start' on discard_granularity. */
167582cae269SKonstantin Komarov 	start = (lbo + sbi->discard_granularity - 1) &
167682cae269SKonstantin Komarov 		sbi->discard_granularity_mask_inv;
1677e8b8e97fSKari Argillander 	/* Align down 'end' on discard_granularity. */
167882cae269SKonstantin Komarov 	end = (lbo + bytes) & sbi->discard_granularity_mask_inv;
167982cae269SKonstantin Komarov 
168082cae269SKonstantin Komarov 	sb = sbi->sb;
168182cae269SKonstantin Komarov 	if (start >= end)
168282cae269SKonstantin Komarov 		return 0;
168382cae269SKonstantin Komarov 
168482cae269SKonstantin Komarov 	err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9,
168544abff2cSChristoph Hellwig 				   GFP_NOFS);
168682cae269SKonstantin Komarov 
168782cae269SKonstantin Komarov 	if (err == -EOPNOTSUPP)
168882cae269SKonstantin Komarov 		sbi->flags |= NTFS_FLAGS_NODISCARD;
168982cae269SKonstantin Komarov 
169082cae269SKonstantin Komarov 	return err;
169182cae269SKonstantin Komarov }
169282cae269SKonstantin Komarov 
ntfs_fs_get_tree(struct fs_context * fc)1693610f8f5aSKari Argillander static int ntfs_fs_get_tree(struct fs_context *fc)
169482cae269SKonstantin Komarov {
1695610f8f5aSKari Argillander 	return get_tree_bdev(fc, ntfs_fill_super);
1696610f8f5aSKari Argillander }
1697610f8f5aSKari Argillander 
1698610f8f5aSKari Argillander /*
1699610f8f5aSKari Argillander  * ntfs_fs_free - Free fs_context.
1700610f8f5aSKari Argillander  *
1701610f8f5aSKari Argillander  * Note that this will be called after fill_super and reconfigure
1702610f8f5aSKari Argillander  * even when they pass. So they have to take pointers if they pass.
1703610f8f5aSKari Argillander  */
ntfs_fs_free(struct fs_context * fc)1704610f8f5aSKari Argillander static void ntfs_fs_free(struct fs_context *fc)
1705610f8f5aSKari Argillander {
1706610f8f5aSKari Argillander 	struct ntfs_mount_options *opts = fc->fs_private;
1707610f8f5aSKari Argillander 	struct ntfs_sb_info *sbi = fc->s_fs_info;
1708610f8f5aSKari Argillander 
170978a06688SChristian Brauner 	if (sbi) {
171078a06688SChristian Brauner 		ntfs3_put_sbi(sbi);
1711126dbf8aSChristoph Hellwig 		ntfs3_free_sbi(sbi);
171278a06688SChristian Brauner 	}
1713610f8f5aSKari Argillander 
1714610f8f5aSKari Argillander 	if (opts)
1715610f8f5aSKari Argillander 		put_mount_options(opts);
1716610f8f5aSKari Argillander }
1717610f8f5aSKari Argillander 
1718f0377761SKonstantin Komarov // clang-format off
1719610f8f5aSKari Argillander static const struct fs_context_operations ntfs_context_ops = {
1720610f8f5aSKari Argillander 	.parse_param	= ntfs_fs_parse_param,
1721610f8f5aSKari Argillander 	.get_tree	= ntfs_fs_get_tree,
1722610f8f5aSKari Argillander 	.reconfigure	= ntfs_fs_reconfigure,
1723610f8f5aSKari Argillander 	.free		= ntfs_fs_free,
1724610f8f5aSKari Argillander };
1725f0377761SKonstantin Komarov // clang-format on
1726610f8f5aSKari Argillander 
1727610f8f5aSKari Argillander /*
172896de65a9SKonstantin Komarov  * ntfs_init_fs_context - Initialize sbi and opts
1729610f8f5aSKari Argillander  *
17306700eabbSKonstantin Komarov  * This will called when mount/remount. We will first initialize
1731610f8f5aSKari Argillander  * options so that if remount we can use just that.
1732610f8f5aSKari Argillander  */
ntfs_init_fs_context(struct fs_context * fc)1733610f8f5aSKari Argillander static int ntfs_init_fs_context(struct fs_context *fc)
1734610f8f5aSKari Argillander {
1735610f8f5aSKari Argillander 	struct ntfs_mount_options *opts;
1736610f8f5aSKari Argillander 	struct ntfs_sb_info *sbi;
1737610f8f5aSKari Argillander 
1738610f8f5aSKari Argillander 	opts = kzalloc(sizeof(struct ntfs_mount_options), GFP_NOFS);
1739610f8f5aSKari Argillander 	if (!opts)
1740610f8f5aSKari Argillander 		return -ENOMEM;
1741610f8f5aSKari Argillander 
1742610f8f5aSKari Argillander 	/* Default options. */
1743610f8f5aSKari Argillander 	opts->fs_uid = current_uid();
1744610f8f5aSKari Argillander 	opts->fs_gid = current_gid();
1745610f8f5aSKari Argillander 	opts->fs_fmask_inv = ~current_umask();
1746610f8f5aSKari Argillander 	opts->fs_dmask_inv = ~current_umask();
1747610f8f5aSKari Argillander 
1748610f8f5aSKari Argillander 	if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
1749610f8f5aSKari Argillander 		goto ok;
1750610f8f5aSKari Argillander 
1751610f8f5aSKari Argillander 	sbi = kzalloc(sizeof(struct ntfs_sb_info), GFP_NOFS);
175227fac777SKari Argillander 	if (!sbi)
175327fac777SKari Argillander 		goto free_opts;
175427fac777SKari Argillander 
175527fac777SKari Argillander 	sbi->upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL);
175627fac777SKari Argillander 	if (!sbi->upcase)
175727fac777SKari Argillander 		goto free_sbi;
175827fac777SKari Argillander 
175927fac777SKari Argillander 	ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL,
176027fac777SKari Argillander 			     DEFAULT_RATELIMIT_BURST);
176127fac777SKari Argillander 
176227fac777SKari Argillander 	mutex_init(&sbi->compress.mtx_lznt);
176327fac777SKari Argillander #ifdef CONFIG_NTFS3_LZX_XPRESS
176427fac777SKari Argillander 	mutex_init(&sbi->compress.mtx_xpress);
176527fac777SKari Argillander 	mutex_init(&sbi->compress.mtx_lzx);
176627fac777SKari Argillander #endif
1767610f8f5aSKari Argillander 
1768610f8f5aSKari Argillander 	fc->s_fs_info = sbi;
1769610f8f5aSKari Argillander ok:
1770610f8f5aSKari Argillander 	fc->fs_private = opts;
1771610f8f5aSKari Argillander 	fc->ops = &ntfs_context_ops;
1772610f8f5aSKari Argillander 
1773610f8f5aSKari Argillander 	return 0;
177427fac777SKari Argillander free_sbi:
177527fac777SKari Argillander 	kfree(sbi);
1776880301bbSColin Ian King free_opts:
1777880301bbSColin Ian King 	kfree(opts);
177827fac777SKari Argillander 	return -ENOMEM;
177982cae269SKonstantin Komarov }
178082cae269SKonstantin Komarov 
ntfs3_kill_sb(struct super_block * sb)1781a4f64a30SChristoph Hellwig static void ntfs3_kill_sb(struct super_block *sb)
1782a4f64a30SChristoph Hellwig {
1783a4f64a30SChristoph Hellwig 	struct ntfs_sb_info *sbi = sb->s_fs_info;
1784a4f64a30SChristoph Hellwig 
1785a4f64a30SChristoph Hellwig 	kill_block_super(sb);
1786a4f64a30SChristoph Hellwig 
1787a4f64a30SChristoph Hellwig 	if (sbi->options)
1788a4f64a30SChristoph Hellwig 		put_mount_options(sbi->options);
1789a4f64a30SChristoph Hellwig 	ntfs3_free_sbi(sbi);
1790a4f64a30SChristoph Hellwig }
1791a4f64a30SChristoph Hellwig 
179282cae269SKonstantin Komarov // clang-format off
179382cae269SKonstantin Komarov static struct file_system_type ntfs_fs_type = {
179482cae269SKonstantin Komarov 	.owner			= THIS_MODULE,
179582cae269SKonstantin Komarov 	.name			= "ntfs3",
1796610f8f5aSKari Argillander 	.init_fs_context	= ntfs_init_fs_context,
1797610f8f5aSKari Argillander 	.parameters		= ntfs_fs_parameters,
1798a4f64a30SChristoph Hellwig 	.kill_sb		= ntfs3_kill_sb,
179982cae269SKonstantin Komarov 	.fs_flags		= FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
180082cae269SKonstantin Komarov };
180182cae269SKonstantin Komarov // clang-format on
180282cae269SKonstantin Komarov 
init_ntfs_fs(void)180382cae269SKonstantin Komarov static int __init init_ntfs_fs(void)
180482cae269SKonstantin Komarov {
180582cae269SKonstantin Komarov 	int err;
180682cae269SKonstantin Komarov 
18072e3a51b5SKari Argillander 	pr_info("ntfs3: Max link count %u\n", NTFS_LINK_MAX);
180882cae269SKonstantin Komarov 
18092e3a51b5SKari Argillander 	if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL))
18102e3a51b5SKari Argillander 		pr_info("ntfs3: Enabled Linux POSIX ACLs support\n");
18112e3a51b5SKari Argillander 	if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER))
181296de65a9SKonstantin Komarov 		pr_notice(
181396de65a9SKonstantin Komarov 			"ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n");
18142e3a51b5SKari Argillander 	if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS))
18152e3a51b5SKari Argillander 		pr_info("ntfs3: Read-only LZX/Xpress compression included\n");
181682cae269SKonstantin Komarov 
18177832e123SKonstantin Komarov #ifdef CONFIG_PROC_FS
18187832e123SKonstantin Komarov 	/* Create "/proc/fs/ntfs3" */
18197832e123SKonstantin Komarov 	proc_info_root = proc_mkdir("fs/ntfs3", NULL);
18207832e123SKonstantin Komarov #endif
18217832e123SKonstantin Komarov 
182282cae269SKonstantin Komarov 	err = ntfs3_init_bitmap();
182382cae269SKonstantin Komarov 	if (err)
182482cae269SKonstantin Komarov 		return err;
182582cae269SKonstantin Komarov 
182682cae269SKonstantin Komarov 	ntfs_inode_cachep = kmem_cache_create(
182782cae269SKonstantin Komarov 		"ntfs_inode_cache", sizeof(struct ntfs_inode), 0,
182882cae269SKonstantin Komarov 		(SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
182982cae269SKonstantin Komarov 		init_once);
183082cae269SKonstantin Komarov 	if (!ntfs_inode_cachep) {
183182cae269SKonstantin Komarov 		err = -ENOMEM;
183282cae269SKonstantin Komarov 		goto out1;
183382cae269SKonstantin Komarov 	}
183482cae269SKonstantin Komarov 
183582cae269SKonstantin Komarov 	err = register_filesystem(&ntfs_fs_type);
183682cae269SKonstantin Komarov 	if (err)
183782cae269SKonstantin Komarov 		goto out;
183882cae269SKonstantin Komarov 
183982cae269SKonstantin Komarov 	return 0;
184082cae269SKonstantin Komarov out:
184182cae269SKonstantin Komarov 	kmem_cache_destroy(ntfs_inode_cachep);
184282cae269SKonstantin Komarov out1:
184382cae269SKonstantin Komarov 	ntfs3_exit_bitmap();
184482cae269SKonstantin Komarov 	return err;
184582cae269SKonstantin Komarov }
184682cae269SKonstantin Komarov 
exit_ntfs_fs(void)184782cae269SKonstantin Komarov static void __exit exit_ntfs_fs(void)
184882cae269SKonstantin Komarov {
184982cae269SKonstantin Komarov 	rcu_barrier();
185082cae269SKonstantin Komarov 	kmem_cache_destroy(ntfs_inode_cachep);
185182cae269SKonstantin Komarov 	unregister_filesystem(&ntfs_fs_type);
185282cae269SKonstantin Komarov 	ntfs3_exit_bitmap();
18537832e123SKonstantin Komarov 
18547832e123SKonstantin Komarov #ifdef CONFIG_PROC_FS
18557832e123SKonstantin Komarov 	if (proc_info_root)
18567832e123SKonstantin Komarov 		remove_proc_entry("fs/ntfs3", NULL);
18577832e123SKonstantin Komarov #endif
185882cae269SKonstantin Komarov }
185982cae269SKonstantin Komarov 
186082cae269SKonstantin Komarov MODULE_LICENSE("GPL");
186182cae269SKonstantin Komarov MODULE_DESCRIPTION("ntfs3 read/write filesystem");
186282cae269SKonstantin Komarov #ifdef CONFIG_NTFS3_FS_POSIX_ACL
186382cae269SKonstantin Komarov MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support");
186482cae269SKonstantin Komarov #endif
186582cae269SKonstantin Komarov #ifdef CONFIG_NTFS3_64BIT_CLUSTER
186696de65a9SKonstantin Komarov MODULE_INFO(
186796de65a9SKonstantin Komarov 	cluster,
186896de65a9SKonstantin Komarov 	"Warning: Activated 64 bits per cluster. Windows does not support this");
186982cae269SKonstantin Komarov #endif
187082cae269SKonstantin Komarov #ifdef CONFIG_NTFS3_LZX_XPRESS
187182cae269SKonstantin Komarov MODULE_INFO(compression, "Read-only lzx/xpress compression included");
187282cae269SKonstantin Komarov #endif
187382cae269SKonstantin Komarov 
187482cae269SKonstantin Komarov MODULE_AUTHOR("Konstantin Komarov");
187582cae269SKonstantin Komarov MODULE_ALIAS_FS("ntfs3");
187682cae269SKonstantin Komarov 
187782cae269SKonstantin Komarov module_init(init_ntfs_fs);
187882cae269SKonstantin Komarov module_exit(exit_ntfs_fs);
1879