File Index
Files and directories
Sign in
FILE

super.c

File preview with syntax highlighting.

linux-6.19.14/fs/ntfs3/super.c C
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 *
   4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
   5 *
   6 *
   7 *                 terminology
   8 *
   9 * cluster - allocation unit     - 512,1K,2K,4K,...,2M
  10 * vcn - virtual cluster number  - Offset inside the file in clusters.
  11 * vbo - virtual byte offset     - Offset inside the file in bytes.
  12 * lcn - logical cluster number  - 0 based cluster in clusters heap.
  13 * lbo - logical byte offset     - Absolute position inside volume.
  14 * run - maps VCN to LCN         - Stored in attributes in packed form.
  15 * attr - attribute segment      - std/name/data etc records inside MFT.
  16 * mi  - MFT inode               - One MFT record(usually 1024 bytes or 4K), consists of attributes.
  17 * ni  - NTFS inode              - Extends linux inode. consists of one or more mft inodes.
  18 * index - unit inside directory - 2K, 4K, <=page size, does not depend on cluster size.
  19 * resident attribute            - Attribute with content stored directly in the MFT record
  20 * non-resident attribute        - Attribute with content stored in clusters
  21 * data_size                     - Size of attribute content in bytes. Equal to inode->i_size
  22 * valid_size                    - Number of bytes written to the non-resident attribute
  23 * allocated_size                - Total size of clusters allocated for non-resident content
  24 * total_size                    - Actual size of allocated clusters for sparse or compressed attributes
  25 *                               - Constraint: valid_size <= data_size <= allocated_size
  26 *
  27 * WSL - Windows Subsystem for Linux
  28 * https://docs.microsoft.com/en-us/windows/wsl/file-permissions
  29 * It stores uid/gid/mode/dev in xattr
  30 *
  31 * ntfs allows up to 2^64 clusters per volume.
  32 * It means you should use 64 bits lcn to operate with ntfs.
  33 * Implementation of ntfs.sys uses only 32 bits lcn.
  34 * Default ntfs3 uses 32 bits lcn too.
  35 * ntfs3 built with CONFIG_NTFS3_64BIT_CLUSTER (ntfs3_64) uses 64 bits per lcn.
  36 *
  37 *
  38 *     ntfs limits, cluster size is 4K (2^12)
  39 * -----------------------------------------------------------------------------
  40 * | Volume size   | Clusters | ntfs.sys | ntfs3  | ntfs3_64 | mkntfs | chkdsk |
  41 * -----------------------------------------------------------------------------
  42 * | < 16T, 2^44   |  < 2^32  |  yes     |  yes   |   yes    |  yes   |  yes   |
  43 * | > 16T, 2^44   |  > 2^32  |  no      |  no    |   yes    |  yes   |  yes   |
  44 * ----------------------------------------------------------|------------------
  45 *
  46 * To mount large volumes as ntfs one should use large cluster size (up to 2M)
  47 * The maximum volume size in this case is 2^32 * 2^21 = 2^53 = 8P
  48 *
  49 *     ntfs limits, cluster size is 2M (2^21)
  50 * -----------------------------------------------------------------------------
  51 * | < 8P, 2^53    |  < 2^32  |  yes     |  yes   |   yes    |  yes   |  yes   |
  52 * | > 8P, 2^53    |  > 2^32  |  no      |  no    |   yes    |  yes   |  yes   |
  53 * ----------------------------------------------------------|------------------
  54 *
  55 */
  56
  57#include <linux/blkdev.h>
  58#include <linux/buffer_head.h>
  59#include <linux/exportfs.h>
  60#include <linux/fs.h>
  61#include <linux/fs_struct.h>
  62#include <linux/fs_context.h>
  63#include <linux/fs_parser.h>
  64#include <linux/log2.h>
  65#include <linux/minmax.h>
  66#include <linux/module.h>
  67#include <linux/nls.h>
  68#include <linux/proc_fs.h>
  69#include <linux/seq_file.h>
  70#include <linux/statfs.h>
  71
  72#include "debug.h"
  73#include "ntfs.h"
  74#include "ntfs_fs.h"
  75#ifdef CONFIG_NTFS3_LZX_XPRESS
  76#include "lib/lib.h"
  77#endif
  78
  79#ifdef CONFIG_PRINTK
  80/*
  81 * ntfs_printk - Trace warnings/notices/errors.
  82 *
  83 * Thanks Joe Perches <joe@perches.com> for implementation
  84 */
  85void ntfs_printk(const struct super_block *sb, const char *fmt, ...)
  86{
  87	struct va_format vaf;
  88	va_list args;
  89	int level;
  90	struct ntfs_sb_info *sbi = sb->s_fs_info;
  91
  92	/* Should we use different ratelimits for warnings/notices/errors? */
  93	if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
  94		return;
  95
  96	va_start(args, fmt);
  97
  98	level = printk_get_level(fmt);
  99	vaf.fmt = printk_skip_level(fmt);
 100	vaf.va = &args;
 101	printk("%c%cntfs3(%s): %pV\n", KERN_SOH_ASCII, level, sb->s_id, &vaf);
 102
 103	va_end(args);
 104}
 105
 106static char s_name_buf[512];
 107static atomic_t s_name_buf_cnt = ATOMIC_INIT(1); // 1 means 'free s_name_buf'.
 108
 109/*
 110 * ntfs_inode_printk
 111 *
 112 * Print warnings/notices/errors about inode using name or inode number.
 113 */
 114void ntfs_inode_printk(struct inode *inode, const char *fmt, ...)
 115{
 116	struct super_block *sb = inode->i_sb;
 117	struct ntfs_sb_info *sbi = sb->s_fs_info;
 118	char *name;
 119	va_list args;
 120	struct va_format vaf;
 121	int level;
 122
 123	if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
 124		return;
 125
 126	/* Use static allocated buffer, if possible. */
 127	name = atomic_dec_and_test(&s_name_buf_cnt) ?
 128		       s_name_buf :
 129		       kmalloc(sizeof(s_name_buf), GFP_NOFS);
 130
 131	if (name) {
 132		struct dentry *de = d_find_alias(inode);
 133
 134		if (de) {
 135			int len;
 136			spin_lock(&de->d_lock);
 137			len = snprintf(name, sizeof(s_name_buf), " \"%s\"",
 138				       de->d_name.name);
 139			spin_unlock(&de->d_lock);
 140			if (len <= 0)
 141				name[0] = 0;
 142			else if (len >= sizeof(s_name_buf))
 143				name[sizeof(s_name_buf) - 1] = 0;
 144		} else {
 145			name[0] = 0;
 146		}
 147		dput(de); /* Cocci warns if placed in branch "if (de)" */
 148	}
 149
 150	va_start(args, fmt);
 151
 152	level = printk_get_level(fmt);
 153	vaf.fmt = printk_skip_level(fmt);
 154	vaf.va = &args;
 155
 156	printk("%c%cntfs3(%s): ino=%lx,%s %pV\n", KERN_SOH_ASCII, level,
 157	       sb->s_id, inode->i_ino, name ? name : "", &vaf);
 158
 159	va_end(args);
 160
 161	atomic_inc(&s_name_buf_cnt);
 162	if (name != s_name_buf)
 163		kfree(name);
 164}
 165#endif
 166
 167/*
 168 * Shared memory struct.
 169 *
 170 * On-disk ntfs's upcase table is created by ntfs formatter.
 171 * 'upcase' table is 128K bytes of memory.
 172 * We should read it into memory when mounting.
 173 * Several ntfs volumes likely use the same 'upcase' table.
 174 * It is good idea to share in-memory 'upcase' table between different volumes.
 175 * Unfortunately winxp/vista/win7 use different upcase tables.
 176 */
 177static DEFINE_SPINLOCK(s_shared_lock);
 178
 179static struct {
 180	void *ptr;
 181	u32 len;
 182	int cnt;
 183} s_shared[8];
 184
 185/*
 186 * ntfs_set_shared
 187 *
 188 * Return:
 189 * * @ptr - If pointer was saved in shared memory.
 190 * * NULL - If pointer was not shared.
 191 */
 192void *ntfs_set_shared(void *ptr, u32 bytes)
 193{
 194	void *ret = NULL;
 195	int i, j = -1;
 196
 197	spin_lock(&s_shared_lock);
 198	for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
 199		if (!s_shared[i].cnt) {
 200			j = i;
 201		} else if (bytes == s_shared[i].len &&
 202			   !memcmp(s_shared[i].ptr, ptr, bytes)) {
 203			s_shared[i].cnt += 1;
 204			ret = s_shared[i].ptr;
 205			break;
 206		}
 207	}
 208
 209	if (!ret && j != -1) {
 210		s_shared[j].ptr = ptr;
 211		s_shared[j].len = bytes;
 212		s_shared[j].cnt = 1;
 213		ret = ptr;
 214	}
 215	spin_unlock(&s_shared_lock);
 216
 217	return ret;
 218}
 219
 220/*
 221 * ntfs_put_shared
 222 *
 223 * Return:
 224 * * @ptr - If pointer is not shared anymore.
 225 * * NULL - If pointer is still shared.
 226 */
 227void *ntfs_put_shared(void *ptr)
 228{
 229	void *ret = ptr;
 230	int i;
 231
 232	spin_lock(&s_shared_lock);
 233	for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
 234		if (s_shared[i].cnt && s_shared[i].ptr == ptr) {
 235			if (--s_shared[i].cnt)
 236				ret = NULL;
 237			break;
 238		}
 239	}
 240	spin_unlock(&s_shared_lock);
 241
 242	return ret;
 243}
 244
 245static inline void put_mount_options(struct ntfs_mount_options *options)
 246{
 247	kfree(options->nls_name);
 248	unload_nls(options->nls);
 249	kfree(options);
 250}
 251
 252enum Opt {
 253	Opt_uid,
 254	Opt_gid,
 255	Opt_umask,
 256	Opt_dmask,
 257	Opt_fmask,
 258	Opt_immutable,
 259	Opt_discard,
 260	Opt_force,
 261	Opt_sparse,
 262	Opt_nohidden,
 263	Opt_hide_dot_files,
 264	Opt_windows_names,
 265	Opt_showmeta,
 266	Opt_acl,
 267	Opt_iocharset,
 268	Opt_prealloc,
 269	Opt_nocase,
 270	Opt_err,
 271};
 272
 273// clang-format off
 274static const struct fs_parameter_spec ntfs_fs_parameters[] = {
 275	fsparam_uid("uid",		Opt_uid),
 276	fsparam_gid("gid",		Opt_gid),
 277	fsparam_u32oct("umask",		Opt_umask),
 278	fsparam_u32oct("dmask",		Opt_dmask),
 279	fsparam_u32oct("fmask",		Opt_fmask),
 280	fsparam_flag("sys_immutable",	Opt_immutable),
 281	fsparam_flag("discard",		Opt_discard),
 282	fsparam_flag("force",		Opt_force),
 283	fsparam_flag("sparse",		Opt_sparse),
 284	fsparam_flag("nohidden",	Opt_nohidden),
 285	fsparam_flag("hide_dot_files",	Opt_hide_dot_files),
 286	fsparam_flag("windows_names",	Opt_windows_names),
 287	fsparam_flag("showmeta",	Opt_showmeta),
 288	fsparam_flag_no("acl",		Opt_acl),
 289	fsparam_string("iocharset",	Opt_iocharset),
 290	fsparam_flag_no("prealloc",	Opt_prealloc),
 291	fsparam_flag("nocase",		Opt_nocase),
 292	{}
 293};
 294// clang-format on
 295
 296/*
 297 * Load nls table or if @nls is utf8 then return NULL.
 298 *
 299 */
 300static struct nls_table *ntfs_load_nls(const char *nls)
 301{
 302	struct nls_table *ret;
 303
 304	if (!nls)
 305		nls = CONFIG_NLS_DEFAULT;
 306
 307	if (strcmp(nls, "utf8") == 0)
 308		return NULL;
 309
 310	if (strcmp(nls, CONFIG_NLS_DEFAULT) == 0)
 311		return load_nls_default();
 312
 313	ret = load_nls(nls);
 314	if (ret)
 315		return ret;
 316
 317	return ERR_PTR(-EINVAL);
 318}
 319
 320static int ntfs_fs_parse_param(struct fs_context *fc,
 321			       struct fs_parameter *param)
 322{
 323	struct ntfs_mount_options *opts = fc->fs_private;
 324	struct fs_parse_result result;
 325	int opt;
 326
 327	opt = fs_parse(fc, ntfs_fs_parameters, param, &result);
 328	if (opt < 0)
 329		return opt;
 330
 331	switch (opt) {
 332	case Opt_uid:
 333		opts->fs_uid = result.uid;
 334		break;
 335	case Opt_gid:
 336		opts->fs_gid = result.gid;
 337		break;
 338	case Opt_umask:
 339		if (result.uint_32 & ~07777)
 340			return invalf(fc, "ntfs3: Invalid value for umask.");
 341		opts->fs_fmask_inv = ~result.uint_32;
 342		opts->fs_dmask_inv = ~result.uint_32;
 343		opts->fmask = 1;
 344		opts->dmask = 1;
 345		break;
 346	case Opt_dmask:
 347		if (result.uint_32 & ~07777)
 348			return invalf(fc, "ntfs3: Invalid value for dmask.");
 349		opts->fs_dmask_inv = ~result.uint_32;
 350		opts->dmask = 1;
 351		break;
 352	case Opt_fmask:
 353		if (result.uint_32 & ~07777)
 354			return invalf(fc, "ntfs3: Invalid value for fmask.");
 355		opts->fs_fmask_inv = ~result.uint_32;
 356		opts->fmask = 1;
 357		break;
 358	case Opt_immutable:
 359		opts->sys_immutable = 1;
 360		break;
 361	case Opt_discard:
 362		opts->discard = 1;
 363		break;
 364	case Opt_force:
 365		opts->force = 1;
 366		break;
 367	case Opt_sparse:
 368		opts->sparse = 1;
 369		break;
 370	case Opt_nohidden:
 371		opts->nohidden = 1;
 372		break;
 373	case Opt_hide_dot_files:
 374		opts->hide_dot_files = 1;
 375		break;
 376	case Opt_windows_names:
 377		opts->windows_names = 1;
 378		break;
 379	case Opt_showmeta:
 380		opts->showmeta = 1;
 381		break;
 382	case Opt_acl:
 383		if (!result.negated)
 384#ifdef CONFIG_NTFS3_FS_POSIX_ACL
 385			fc->sb_flags |= SB_POSIXACL;
 386#else
 387			return invalf(
 388				fc, "ntfs3: Support for ACL not compiled in!");
 389#endif
 390		else
 391			fc->sb_flags &= ~SB_POSIXACL;
 392		break;
 393	case Opt_iocharset:
 394		kfree(opts->nls_name);
 395		opts->nls_name = param->string;
 396		param->string = NULL;
 397		break;
 398	case Opt_prealloc:
 399		opts->prealloc = !result.negated;
 400		break;
 401	case Opt_nocase:
 402		opts->nocase = 1;
 403		break;
 404	default:
 405		/* Should not be here unless we forget add case. */
 406		return -EINVAL;
 407	}
 408	return 0;
 409}
 410
 411static int ntfs_fs_reconfigure(struct fs_context *fc)
 412{
 413	struct super_block *sb = fc->root->d_sb;
 414	struct ntfs_sb_info *sbi = sb->s_fs_info;
 415	struct ntfs_mount_options *new_opts = fc->fs_private;
 416	int ro_rw;
 417
 418	/* If ntfs3 is used as legacy ntfs enforce read-only mode. */
 419	if (is_legacy_ntfs(sb)) {
 420		fc->sb_flags |= SB_RDONLY;
 421		goto out;
 422	}
 423
 424	ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY);
 425	if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) {
 426		errorf(fc,
 427		       "ntfs3: Couldn't remount rw because journal is not replayed. Please umount/remount instead\n");
 428		return -EINVAL;
 429	}
 430
 431	new_opts->nls = ntfs_load_nls(new_opts->nls_name);
 432	if (IS_ERR(new_opts->nls)) {
 433		new_opts->nls = NULL;
 434		errorf(fc, "ntfs3: Cannot load iocharset %s",
 435		       new_opts->nls_name);
 436		return -EINVAL;
 437	}
 438	if (new_opts->nls != sbi->options->nls)
 439		return invalf(
 440			fc,
 441			"ntfs3: Cannot use different iocharset when remounting!");
 442
 443	if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) &&
 444	    !new_opts->force) {
 445		errorf(fc,
 446		       "ntfs3: Volume is dirty and \"force\" flag is not set!");
 447		return -EINVAL;
 448	}
 449
 450out:
 451	sync_filesystem(sb);
 452	swap(sbi->options, fc->fs_private);
 453
 454	return 0;
 455}
 456
 457#ifdef CONFIG_PROC_FS
 458static struct proc_dir_entry *proc_info_root;
 459
 460/*
 461 * ntfs3_volinfo:
 462 *
 463 * The content of /proc/fs/ntfs3/<dev>/volinfo
 464 *
 465 * ntfs3.1
 466 * cluster size
 467 * number of clusters
 468 * total number of mft records
 469 * number of used mft records ~= number of files + folders
 470 * real state of ntfs "dirty"/"clean"
 471 * current state of ntfs "dirty"/"clean"
 472*/
 473static int ntfs3_volinfo(struct seq_file *m, void *o)
 474{
 475	struct super_block *sb = m->private;
 476	struct ntfs_sb_info *sbi = sb->s_fs_info;
 477
 478	seq_printf(m, "ntfs%d.%d\n%u\n%zu\n%zu\n%zu\n%s\n%s\n",
 479		   sbi->volume.major_ver, sbi->volume.minor_ver,
 480		   sbi->cluster_size, sbi->used.bitmap.nbits,
 481		   sbi->mft.bitmap.nbits,
 482		   sbi->mft.bitmap.nbits - wnd_zeroes(&sbi->mft.bitmap),
 483		   sbi->volume.real_dirty ? "dirty" : "clean",
 484		   (sbi->volume.flags & VOLUME_FLAG_DIRTY) ? "dirty" : "clean");
 485
 486	return 0;
 487}
 488
 489static int ntfs3_volinfo_open(struct inode *inode, struct file *file)
 490{
 491	return single_open(file, ntfs3_volinfo, pde_data(inode));
 492}
 493
 494/* read /proc/fs/ntfs3/<dev>/label */
 495static int ntfs3_label_show(struct seq_file *m, void *o)
 496{
 497	struct super_block *sb = m->private;
 498	struct ntfs_sb_info *sbi = sb->s_fs_info;
 499
 500	seq_printf(m, "%s\n", sbi->volume.label);
 501
 502	return 0;
 503}
 504
 505/* write /proc/fs/ntfs3/<dev>/label */
 506static ssize_t ntfs3_label_write(struct file *file, const char __user *buffer,
 507				 size_t count, loff_t *ppos)
 508{
 509	int err;
 510	struct super_block *sb = pde_data(file_inode(file));
 511	ssize_t ret = count;
 512	u8 *label;
 513
 514	if (sb_rdonly(sb))
 515		return -EROFS;
 516
 517	label = kmalloc(count, GFP_NOFS);
 518
 519	if (!label)
 520		return -ENOMEM;
 521
 522	if (copy_from_user(label, buffer, ret)) {
 523		ret = -EFAULT;
 524		goto out;
 525	}
 526	while (ret > 0 && label[ret - 1] == '\n')
 527		ret -= 1;
 528
 529	err = ntfs_set_label(sb->s_fs_info, label, ret);
 530
 531	if (err < 0) {
 532		ntfs_err(sb, "failed (%d) to write label", err);
 533		ret = err;
 534		goto out;
 535	}
 536
 537	*ppos += count;
 538	ret = count;
 539out:
 540	kfree(label);
 541	return ret;
 542}
 543
 544static int ntfs3_label_open(struct inode *inode, struct file *file)
 545{
 546	return single_open(file, ntfs3_label_show, pde_data(inode));
 547}
 548
 549static const struct proc_ops ntfs3_volinfo_fops = {
 550	.proc_read = seq_read,
 551	.proc_lseek = seq_lseek,
 552	.proc_release = single_release,
 553	.proc_open = ntfs3_volinfo_open,
 554};
 555
 556static const struct proc_ops ntfs3_label_fops = {
 557	.proc_read = seq_read,
 558	.proc_lseek = seq_lseek,
 559	.proc_release = single_release,
 560	.proc_open = ntfs3_label_open,
 561	.proc_write = ntfs3_label_write,
 562};
 563
 564static void ntfs_create_procdir(struct super_block *sb)
 565{
 566	struct proc_dir_entry *e;
 567
 568	if (!proc_info_root)
 569		return;
 570
 571	e = proc_mkdir(sb->s_id, proc_info_root);
 572	if (e) {
 573		struct ntfs_sb_info *sbi = sb->s_fs_info;
 574
 575		proc_create_data("volinfo", 0444, e, &ntfs3_volinfo_fops, sb);
 576		proc_create_data("label", 0644, e, &ntfs3_label_fops, sb);
 577		sbi->procdir = e;
 578	}
 579}
 580
 581static void ntfs_remove_procdir(struct super_block *sb)
 582{
 583	struct ntfs_sb_info *sbi = sb->s_fs_info;
 584
 585	if (!sbi->procdir)
 586		return;
 587
 588	remove_proc_entry("label", sbi->procdir);
 589	remove_proc_entry("volinfo", sbi->procdir);
 590	remove_proc_entry(sb->s_id, proc_info_root);
 591	sbi->procdir = NULL;
 592}
 593
 594static void ntfs_create_proc_root(void)
 595{
 596	proc_info_root = proc_mkdir("fs/ntfs3", NULL);
 597}
 598
 599static void ntfs_remove_proc_root(void)
 600{
 601	if (proc_info_root) {
 602		remove_proc_entry("fs/ntfs3", NULL);
 603		proc_info_root = NULL;
 604	}
 605}
 606#else
 607// clang-format off
 608static void ntfs_create_procdir(struct super_block *sb){}
 609static void ntfs_remove_procdir(struct super_block *sb){}
 610static void ntfs_create_proc_root(void){}
 611static void ntfs_remove_proc_root(void){}
 612// clang-format on
 613#endif
 614
 615static struct kmem_cache *ntfs_inode_cachep;
 616
 617static struct inode *ntfs_alloc_inode(struct super_block *sb)
 618{
 619	struct ntfs_inode *ni = alloc_inode_sb(sb, ntfs_inode_cachep, GFP_NOFS);
 620
 621	if (!ni)
 622		return NULL;
 623
 624	memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode));
 625	mutex_init(&ni->ni_lock);
 626	return &ni->vfs_inode;
 627}
 628
 629static void ntfs_free_inode(struct inode *inode)
 630{
 631	struct ntfs_inode *ni = ntfs_i(inode);
 632
 633	mutex_destroy(&ni->ni_lock);
 634	kmem_cache_free(ntfs_inode_cachep, ni);
 635}
 636
 637static void init_once(void *foo)
 638{
 639	struct ntfs_inode *ni = foo;
 640
 641	inode_init_once(&ni->vfs_inode);
 642}
 643
 644/*
 645 * Noinline to reduce binary size.
 646 */
 647static noinline void ntfs3_put_sbi(struct ntfs_sb_info *sbi)
 648{
 649	wnd_close(&sbi->mft.bitmap);
 650	wnd_close(&sbi->used.bitmap);
 651
 652	if (sbi->mft.ni) {
 653		iput(&sbi->mft.ni->vfs_inode);
 654		sbi->mft.ni = NULL;
 655	}
 656
 657	if (sbi->security.ni) {
 658		iput(&sbi->security.ni->vfs_inode);
 659		sbi->security.ni = NULL;
 660	}
 661
 662	if (sbi->reparse.ni) {
 663		iput(&sbi->reparse.ni->vfs_inode);
 664		sbi->reparse.ni = NULL;
 665	}
 666
 667	if (sbi->objid.ni) {
 668		iput(&sbi->objid.ni->vfs_inode);
 669		sbi->objid.ni = NULL;
 670	}
 671
 672	if (sbi->volume.ni) {
 673		iput(&sbi->volume.ni->vfs_inode);
 674		sbi->volume.ni = NULL;
 675	}
 676
 677	ntfs_update_mftmirr(sbi, 0);
 678
 679	indx_clear(&sbi->security.index_sii);
 680	indx_clear(&sbi->security.index_sdh);
 681	indx_clear(&sbi->reparse.index_r);
 682	indx_clear(&sbi->objid.index_o);
 683}
 684
 685static void ntfs3_free_sbi(struct ntfs_sb_info *sbi)
 686{
 687	kfree(sbi->new_rec);
 688	kvfree(ntfs_put_shared(sbi->upcase));
 689	kvfree(sbi->def_table);
 690	kfree(sbi->compress.lznt);
 691#ifdef CONFIG_NTFS3_LZX_XPRESS
 692	xpress_free_decompressor(sbi->compress.xpress);
 693	lzx_free_decompressor(sbi->compress.lzx);
 694#endif
 695	kfree(sbi);
 696}
 697
 698static void ntfs_put_super(struct super_block *sb)
 699{
 700	struct ntfs_sb_info *sbi = sb->s_fs_info;
 701
 702	ntfs_remove_procdir(sb);
 703
 704	/* Mark rw ntfs as clear, if possible. */
 705	ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
 706
 707	if (sbi->options) {
 708		put_mount_options(sbi->options);
 709		sbi->options = NULL;
 710	}
 711
 712	ntfs3_put_sbi(sbi);
 713}
 714
 715static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 716{
 717	struct super_block *sb = dentry->d_sb;
 718	struct ntfs_sb_info *sbi = sb->s_fs_info;
 719	struct wnd_bitmap *wnd = &sbi->used.bitmap;
 720
 721	buf->f_type = sb->s_magic;
 722	buf->f_bsize = sbi->cluster_size;
 723	buf->f_blocks = wnd->nbits;
 724
 725	buf->f_bfree = buf->f_bavail = wnd_zeroes(wnd);
 726	buf->f_fsid.val[0] = sbi->volume.ser_num;
 727	buf->f_fsid.val[1] = (sbi->volume.ser_num >> 32);
 728	buf->f_namelen = NTFS_NAME_LEN;
 729
 730	return 0;
 731}
 732
 733static int ntfs_show_options(struct seq_file *m, struct dentry *root)
 734{
 735	struct super_block *sb = root->d_sb;
 736	struct ntfs_sb_info *sbi = sb->s_fs_info;
 737	struct ntfs_mount_options *opts = sbi->options;
 738	struct user_namespace *user_ns = seq_user_ns(m);
 739
 740	seq_printf(m, ",uid=%u", from_kuid_munged(user_ns, opts->fs_uid));
 741	seq_printf(m, ",gid=%u", from_kgid_munged(user_ns, opts->fs_gid));
 742	if (opts->dmask)
 743		seq_printf(m, ",dmask=%04o", opts->fs_dmask_inv ^ 0xffff);
 744	if (opts->fmask)
 745		seq_printf(m, ",fmask=%04o", opts->fs_fmask_inv ^ 0xffff);
 746	if (opts->sys_immutable)
 747		seq_puts(m, ",sys_immutable");
 748	if (opts->discard)
 749		seq_puts(m, ",discard");
 750	if (opts->force)
 751		seq_puts(m, ",force");
 752	if (opts->sparse)
 753		seq_puts(m, ",sparse");
 754	if (opts->nohidden)
 755		seq_puts(m, ",nohidden");
 756	if (opts->hide_dot_files)
 757		seq_puts(m, ",hide_dot_files");
 758	if (opts->windows_names)
 759		seq_puts(m, ",windows_names");
 760	if (opts->showmeta)
 761		seq_puts(m, ",showmeta");
 762	if (sb->s_flags & SB_POSIXACL)
 763		seq_puts(m, ",acl");
 764	if (opts->nls)
 765		seq_printf(m, ",iocharset=%s", opts->nls->charset);
 766	else
 767		seq_puts(m, ",iocharset=utf8");
 768	if (opts->prealloc)
 769		seq_puts(m, ",prealloc");
 770	if (opts->nocase)
 771		seq_puts(m, ",nocase");
 772
 773	return 0;
 774}
 775
 776/*
 777 * ntfs_shutdown - super_operations::shutdown
 778 */
 779static void ntfs_shutdown(struct super_block *sb)
 780{
 781	set_bit(NTFS_FLAGS_SHUTDOWN_BIT, &ntfs_sb(sb)->flags);
 782}
 783
 784/*
 785 * ntfs_sync_fs - super_operations::sync_fs
 786 */
 787static int ntfs_sync_fs(struct super_block *sb, int wait)
 788{
 789	int err = 0, err2;
 790	struct ntfs_sb_info *sbi = sb->s_fs_info;
 791	struct ntfs_inode *ni;
 792	struct inode *inode;
 793
 794	if (unlikely(ntfs3_forced_shutdown(sb)))
 795		return -EIO;
 796
 797	ni = sbi->security.ni;
 798	if (ni) {
 799		inode = &ni->vfs_inode;
 800		err2 = _ni_write_inode(inode, wait);
 801		if (err2 && !err)
 802			err = err2;
 803	}
 804
 805	ni = sbi->objid.ni;
 806	if (ni) {
 807		inode = &ni->vfs_inode;
 808		err2 = _ni_write_inode(inode, wait);
 809		if (err2 && !err)
 810			err = err2;
 811	}
 812
 813	ni = sbi->reparse.ni;
 814	if (ni) {
 815		inode = &ni->vfs_inode;
 816		err2 = _ni_write_inode(inode, wait);
 817		if (err2 && !err)
 818			err = err2;
 819	}
 820
 821	if (!err)
 822		ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
 823
 824	ntfs_update_mftmirr(sbi, wait);
 825
 826	return err;
 827}
 828
 829static const struct super_operations ntfs_sops = {
 830	.alloc_inode = ntfs_alloc_inode,
 831	.free_inode = ntfs_free_inode,
 832	.evict_inode = ntfs_evict_inode,
 833	.put_super = ntfs_put_super,
 834	.statfs = ntfs_statfs,
 835	.show_options = ntfs_show_options,
 836	.shutdown = ntfs_shutdown,
 837	.sync_fs = ntfs_sync_fs,
 838	.write_inode = ntfs3_write_inode,
 839};
 840
 841static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino,
 842					   u32 generation)
 843{
 844	struct MFT_REF ref;
 845	struct inode *inode;
 846
 847	ref.low = cpu_to_le32(ino);
 848#ifdef CONFIG_NTFS3_64BIT_CLUSTER
 849	ref.high = cpu_to_le16(ino >> 32);
 850#else
 851	ref.high = 0;
 852#endif
 853	ref.seq = cpu_to_le16(generation);
 854
 855	inode = ntfs_iget5(sb, &ref, NULL);
 856	if (!IS_ERR(inode) && is_bad_inode(inode)) {
 857		iput(inode);
 858		inode = ERR_PTR(-ESTALE);
 859	}
 860
 861	return inode;
 862}
 863
 864static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
 865					int fh_len, int fh_type)
 866{
 867	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
 868				    ntfs_export_get_inode);
 869}
 870
 871static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
 872					int fh_len, int fh_type)
 873{
 874	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
 875				    ntfs_export_get_inode);
 876}
 877
 878/* TODO: == ntfs_sync_inode */
 879static int ntfs_nfs_commit_metadata(struct inode *inode)
 880{
 881	return _ni_write_inode(inode, 1);
 882}
 883
 884static const struct export_operations ntfs_export_ops = {
 885	.encode_fh = generic_encode_ino32_fh,
 886	.fh_to_dentry = ntfs_fh_to_dentry,
 887	.fh_to_parent = ntfs_fh_to_parent,
 888	.get_parent = ntfs3_get_parent,
 889	.commit_metadata = ntfs_nfs_commit_metadata,
 890};
 891
 892/*
 893 * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb".
 894 */
 895static u32 format_size_gb(const u64 bytes, u32 *mb)
 896{
 897	/* Do simple right 30 bit shift of 64 bit value. */
 898	u64 kbytes = bytes >> 10;
 899	u32 kbytes32 = kbytes;
 900
 901	*mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20;
 902	if (*mb >= 100)
 903		*mb = 99;
 904
 905	return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12);
 906}
 907
 908static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot)
 909{
 910	if (boot->sectors_per_clusters <= 0x80)
 911		return boot->sectors_per_clusters;
 912	if (boot->sectors_per_clusters >= 0xf4) /* limit shift to 2MB max */
 913		return 1U << (-(s8)boot->sectors_per_clusters);
 914	return -EINVAL;
 915}
 916
 917/*
 918 * ntfs_init_from_boot - Init internal info from on-disk boot sector.
 919 *
 920 * NTFS mount begins from boot - special formatted 512 bytes.
 921 * There are two boots: the first and the last 512 bytes of volume.
 922 * The content of boot is not changed during ntfs life.
 923 *
 924 * NOTE: ntfs.sys checks only first (primary) boot.
 925 * chkdsk checks both boots.
 926 */
 927static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
 928			       u64 dev_size, struct NTFS_BOOT **boot2)
 929{
 930	struct ntfs_sb_info *sbi = sb->s_fs_info;
 931	int err;
 932	u32 mb, gb, boot_sector_size, sct_per_clst, record_size;
 933	u64 sectors, clusters, mlcn, mlcn2, dev_size0;
 934	struct NTFS_BOOT *boot;
 935	struct buffer_head *bh;
 936	struct MFT_REC *rec;
 937	u16 fn, ao;
 938	u8 cluster_bits;
 939	u32 boot_off = 0;
 940	sector_t boot_block = 0;
 941	const char *hint = "Primary boot";
 942
 943	/* Save original dev_size. Used with alternative boot. */
 944	dev_size0 = dev_size;
 945
 946	sbi->volume.blocks = dev_size >> PAGE_SHIFT;
 947
 948	/* Set dummy blocksize to read boot_block. */
 949	if (!sb_min_blocksize(sb, PAGE_SIZE)) {
 950		return -EINVAL;
 951	}
 952
 953read_boot:
 954	bh = ntfs_bread(sb, boot_block);
 955	if (!bh)
 956		return boot_block ? -EINVAL : -EIO;
 957
 958	err = -EINVAL;
 959
 960	/* Corrupted image; do not read OOB */
 961	if (bh->b_size - sizeof(*boot) < boot_off)
 962		goto out;
 963
 964	boot = (struct NTFS_BOOT *)Add2Ptr(bh->b_data, boot_off);
 965
 966	if (memcmp(boot->system_id, "NTFS    ", sizeof("NTFS    ") - 1)) {
 967		ntfs_err(sb, "%s signature is not NTFS.", hint);
 968		goto out;
 969	}
 970
 971	/* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/
 972	/*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1])
 973	 *	goto out;
 974	 */
 975
 976	boot_sector_size = ((u32)boot->bytes_per_sector[1] << 8) |
 977			   boot->bytes_per_sector[0];
 978	if (boot_sector_size < SECTOR_SIZE ||
 979	    !is_power_of_2(boot_sector_size)) {
 980		ntfs_err(sb, "%s: invalid bytes per sector %u.", hint,
 981			 boot_sector_size);
 982		goto out;
 983	}
 984
 985	/* cluster size: 512, 1K, 2K, 4K, ... 2M */
 986	sct_per_clst = true_sectors_per_clst(boot);
 987	if ((int)sct_per_clst < 0 || !is_power_of_2(sct_per_clst)) {
 988		ntfs_err(sb, "%s: invalid sectors per cluster %u.", hint,
 989			 sct_per_clst);
 990		goto out;
 991	}
 992
 993	sbi->cluster_size = boot_sector_size * sct_per_clst;
 994	sbi->cluster_bits = cluster_bits = blksize_bits(sbi->cluster_size);
 995	sbi->cluster_mask = sbi->cluster_size - 1;
 996	sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask;
 997
 998	mlcn = le64_to_cpu(boot->mft_clst);
 999	mlcn2 = le64_to_cpu(boot->mft2_clst);
1000	sectors = le64_to_cpu(boot->sectors_per_volume);
1001
1002	if (mlcn * sct_per_clst >= sectors || mlcn2 * sct_per_clst >= sectors) {
1003		ntfs_err(
1004			sb,
1005			"%s: start of MFT 0x%llx (0x%llx) is out of volume 0x%llx.",
1006			hint, mlcn, mlcn2, sectors);
1007		goto out;
1008	}
1009
1010	if (boot->record_size >= 0) {
1011		record_size = (u32)boot->record_size << cluster_bits;
1012	} else if (-boot->record_size <= MAXIMUM_SHIFT_BYTES_PER_MFT) {
1013		record_size = 1u << (-boot->record_size);
1014	} else {
1015		ntfs_err(sb, "%s: invalid record size %d.", hint,
1016			 boot->record_size);
1017		goto out;
1018	}
1019
1020	sbi->record_size = record_size;
1021	sbi->record_bits = blksize_bits(record_size);
1022	sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes
1023
1024	/* Check MFT record size. */
1025	if (record_size < SECTOR_SIZE || !is_power_of_2(record_size)) {
1026		ntfs_err(sb, "%s: invalid bytes per MFT record %u (%d).", hint,
1027			 record_size, boot->record_size);
1028		goto out;
1029	}
1030
1031	if (record_size > MAXIMUM_BYTES_PER_MFT) {
1032		ntfs_err(sb, "Unsupported bytes per MFT record %u.",
1033			 record_size);
1034		goto out;
1035	}
1036
1037	if (boot->index_size >= 0) {
1038		sbi->index_size = (u32)boot->index_size << cluster_bits;
1039	} else if (-boot->index_size <= MAXIMUM_SHIFT_BYTES_PER_INDEX) {
1040		sbi->index_size = 1u << (-boot->index_size);
1041	} else {
1042		ntfs_err(sb, "%s: invalid index size %d.", hint,
1043			 boot->index_size);
1044		goto out;
1045	}
1046
1047	/* Check index record size. */
1048	if (sbi->index_size < SECTOR_SIZE || !is_power_of_2(sbi->index_size)) {
1049		ntfs_err(sb, "%s: invalid bytes per index %u(%d).", hint,
1050			 sbi->index_size, boot->index_size);
1051		goto out;
1052	}
1053
1054	if (sbi->index_size > MAXIMUM_BYTES_PER_INDEX) {
1055		ntfs_err(sb, "%s: unsupported bytes per index %u.", hint,
1056			 sbi->index_size);
1057		goto out;
1058	}
1059
1060	sbi->volume.size = sectors * boot_sector_size;
1061
1062	gb = format_size_gb(sbi->volume.size + boot_sector_size, &mb);
1063
1064	/*
1065	 * - Volume formatted and mounted with the same sector size.
1066	 * - Volume formatted 4K and mounted as 512.
1067	 * - Volume formatted 512 and mounted as 4K.
1068	 */
1069	if (boot_sector_size != sector_size) {
1070		ntfs_warn(
1071			sb,
1072			"Different NTFS sector size (%u) and media sector size (%u).",
1073			boot_sector_size, sector_size);
1074		dev_size += sector_size - 1;
1075	}
1076
1077	sbi->bdev_blocksize_mask = max(boot_sector_size, sector_size) - 1;
1078	sbi->mft.lbo = mlcn << cluster_bits;
1079	sbi->mft.lbo2 = mlcn2 << cluster_bits;
1080
1081	/* Compare boot's cluster and sector. */
1082	if (sbi->cluster_size < boot_sector_size) {
1083		ntfs_err(sb, "%s: invalid bytes per cluster (%u).", hint,
1084			 sbi->cluster_size);
1085		goto out;
1086	}
1087
1088	/* Compare boot's cluster and media sector. */
1089	if (sbi->cluster_size < sector_size) {
1090		/* No way to use ntfs_get_block in this case. */
1091		ntfs_err(
1092			sb,
1093			"Failed to mount 'cause NTFS's cluster size (%u) is less than media sector size (%u).",
1094			sbi->cluster_size, sector_size);
1095		goto out;
1096	}
1097
1098	sbi->max_bytes_per_attr =
1099		record_size - ALIGN(MFTRECORD_FIXUP_OFFSET, 8) -
1100		ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) -
1101		ALIGN(sizeof(enum ATTR_TYPE), 8);
1102
1103	sbi->volume.ser_num = le64_to_cpu(boot->serial_num);
1104
1105	/* Warning if RAW volume. */
1106	if (dev_size < sbi->volume.size + boot_sector_size) {
1107		u32 mb0, gb0;
1108
1109		gb0 = format_size_gb(dev_size, &mb0);
1110		ntfs_warn(
1111			sb,
1112			"RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only.",
1113			gb, mb, gb0, mb0);
1114		sb->s_flags |= SB_RDONLY;
1115	}
1116
1117	clusters = sbi->volume.size >> cluster_bits;
1118#ifndef CONFIG_NTFS3_64BIT_CLUSTER
1119	/* 32 bits per cluster. */
1120	if (clusters >> 32) {
1121		ntfs_notice(
1122			sb,
1123			"NTFS %u.%02u Gb is too big to use 32 bits per cluster.",
1124			gb, mb);
1125		goto out;
1126	}
1127#elif BITS_PER_LONG < 64
1128#error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS"
1129#endif
1130
1131	sbi->used.bitmap.nbits = clusters;
1132
1133	rec = kzalloc(record_size, GFP_NOFS);
1134	if (!rec) {
1135		err = -ENOMEM;
1136		goto out;
1137	}
1138
1139	sbi->new_rec = rec;
1140	rec->rhdr.sign = NTFS_FILE_SIGNATURE;
1141	rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET);
1142	fn = (sbi->record_size >> SECTOR_SHIFT) + 1;
1143	rec->rhdr.fix_num = cpu_to_le16(fn);
1144	ao = ALIGN(MFTRECORD_FIXUP_OFFSET + sizeof(short) * fn, 8);
1145	rec->attr_off = cpu_to_le16(ao);
1146	rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8));
1147	rec->total = cpu_to_le32(sbi->record_size);
1148	((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END;
1149
1150	sb_set_blocksize(sb, min_t(u32, sbi->cluster_size, PAGE_SIZE));
1151
1152	sbi->block_mask = sb->s_blocksize - 1;
1153	sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits;
1154	sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits;
1155
1156	/* Maximum size for normal files. */
1157	sbi->maxbytes = (clusters << cluster_bits) - 1;
1158
1159#ifdef CONFIG_NTFS3_64BIT_CLUSTER
1160	if (clusters >= (1ull << (64 - cluster_bits)))
1161		sbi->maxbytes = -1;
1162	sbi->maxbytes_sparse = -1;
1163	sb->s_maxbytes = MAX_LFS_FILESIZE;
1164#else
1165	/* Maximum size for sparse file. */
1166	sbi->maxbytes_sparse = (1ull << (cluster_bits + 32)) - 1;
1167	sb->s_maxbytes = 0xFFFFFFFFull << cluster_bits;
1168#endif
1169
1170	/*
1171	 * Compute the MFT zone at two steps.
1172	 * It would be nice if we are able to allocate 1/8 of
1173	 * total clusters for MFT but not more then 512 MB.
1174	 */
1175	sbi->zone_max = min_t(CLST, 0x20000000 >> cluster_bits, clusters >> 3);
1176
1177	err = 0;
1178
1179	if (bh->b_blocknr && !sb_rdonly(sb)) {
1180		/*
1181	 	 * Alternative boot is ok but primary is not ok.
1182	 	 * Do not update primary boot here 'cause it may be faked boot.
1183	 	 * Let ntfs to be mounted and update boot later.
1184		 */
1185		*boot2 = kmemdup(boot, sizeof(*boot), GFP_NOFS | __GFP_NOWARN);
1186	}
1187
1188out:
1189	brelse(bh);
1190
1191	if (err == -EINVAL && !boot_block && dev_size0 > PAGE_SHIFT) {
1192		u32 block_size = min_t(u32, sector_size, PAGE_SIZE);
1193		u64 lbo = dev_size0 - sizeof(*boot);
1194
1195		boot_block = lbo >> blksize_bits(block_size);
1196		boot_off = lbo & (block_size - 1);
1197		if (boot_block && block_size >= boot_off + sizeof(*boot)) {
1198			/*
1199			 * Try alternative boot (last sector)
1200			 */
1201			sb_set_blocksize(sb, block_size);
1202			hint = "Alternative boot";
1203			dev_size = dev_size0; /* restore original size. */
1204			goto read_boot;
1205		}
1206	}
1207
1208	return err;
1209}
1210
1211/*
1212 * ntfs_fill_super - Try to mount.
1213 */
1214static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
1215{
1216	int err;
1217	struct ntfs_sb_info *sbi = sb->s_fs_info;
1218	struct block_device *bdev = sb->s_bdev;
1219	struct ntfs_mount_options *fc_opts;
1220	struct ntfs_mount_options *options = NULL;
1221	struct inode *inode;
1222	struct ntfs_inode *ni;
1223	size_t i, tt, bad_len, bad_frags;
1224	CLST vcn, lcn, len;
1225	struct ATTRIB *attr;
1226	const struct VOLUME_INFO *info;
1227	u32 done, bytes;
1228	struct ATTR_DEF_ENTRY *t;
1229	u16 *shared;
1230	struct MFT_REF ref;
1231	bool ro = sb_rdonly(sb);
1232	struct NTFS_BOOT *boot2 = NULL;
1233
1234	ref.high = 0;
1235
1236	sbi->sb = sb;
1237	fc_opts = fc->fs_private;
1238	if (!fc_opts) {
1239		errorf(fc, "missing mount options");
1240		return -EINVAL;
1241	}
1242	options = kmemdup(fc_opts, sizeof(*fc_opts), GFP_KERNEL);
1243	if (!options)
1244		return -ENOMEM;
1245
1246	if (fc_opts->nls_name) {
1247		options->nls_name = kstrdup(fc_opts->nls_name, GFP_KERNEL);
1248		if (!options->nls_name) {
1249			kfree(options);
1250			return -ENOMEM;
1251		}
1252	}
1253	sbi->options = options;
1254	sb->s_flags |= SB_NODIRATIME;
1255	sb->s_magic = 0x7366746e; // "ntfs"
1256	sb->s_op = &ntfs_sops;
1257	sb->s_export_op = &ntfs_export_ops;
1258	sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec
1259	sb->s_xattr = ntfs_xattr_handlers;
1260	set_default_d_op(sb, options->nocase ? &ntfs_dentry_ops : NULL);
1261
1262	options->nls = ntfs_load_nls(options->nls_name);
1263	if (IS_ERR(options->nls)) {
1264		options->nls = NULL;
1265		errorf(fc, "Cannot load nls %s", options->nls_name);
1266		err = -EINVAL;
1267		goto out;
1268	}
1269
1270	if (bdev_max_discard_sectors(bdev) && bdev_discard_granularity(bdev)) {
1271		sbi->discard_granularity = bdev_discard_granularity(bdev);
1272		sbi->discard_granularity_mask_inv =
1273			~(u64)(sbi->discard_granularity - 1);
1274	}
1275
1276	/* Parse boot. */
1277	err = ntfs_init_from_boot(sb, bdev_logical_block_size(bdev),
1278				  bdev_nr_bytes(bdev), &boot2);
1279	if (err)
1280		goto out;
1281
1282	/*
1283	 * Load $Volume. This should be done before $LogFile
1284	 * 'cause 'sbi->volume.ni' is used in 'ntfs_set_state'.
1285	 */
1286	ref.low = cpu_to_le32(MFT_REC_VOL);
1287	ref.seq = cpu_to_le16(MFT_REC_VOL);
1288	inode = ntfs_iget5(sb, &ref, &NAME_VOLUME);
1289	if (IS_ERR(inode)) {
1290		err = PTR_ERR(inode);
1291		ntfs_err(sb, "Failed to load $Volume (%d).", err);
1292		goto out;
1293	}
1294
1295	ni = ntfs_i(inode);
1296
1297	/* Load and save label (not necessary). */
1298	attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL);
1299
1300	if (!attr) {
1301		/* It is ok if no ATTR_LABEL */
1302	} else if (!attr->non_res && !is_attr_ext(attr)) {
1303		/* $AttrDef allows labels to be up to 128 symbols. */
1304		err = utf16s_to_utf8s(resident_data(attr),
1305				      le32_to_cpu(attr->res.data_size) >> 1,
1306				      UTF16_LITTLE_ENDIAN, sbi->volume.label,
1307				      sizeof(sbi->volume.label));
1308		if (err < 0)
1309			sbi->volume.label[0] = 0;
1310	} else {
1311		/* Should we break mounting here? */
1312		//err = -EINVAL;
1313		//goto put_inode_out;
1314	}
1315
1316	attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL);
1317	if (!attr || is_attr_ext(attr) ||
1318	    !(info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO))) {
1319		ntfs_err(sb, "$Volume is corrupted.");
1320		err = -EINVAL;
1321		goto put_inode_out;
1322	}
1323
1324	sbi->volume.major_ver = info->major_ver;
1325	sbi->volume.minor_ver = info->minor_ver;
1326	sbi->volume.flags = info->flags;
1327	sbi->volume.ni = ni;
1328	if (info->flags & VOLUME_FLAG_DIRTY) {
1329		sbi->volume.real_dirty = true;
1330		ntfs_info(sb, "It is recommended to use chkdsk.");
1331	}
1332
1333	/* Load $MFTMirr to estimate recs_mirr. */
1334	ref.low = cpu_to_le32(MFT_REC_MIRR);
1335	ref.seq = cpu_to_le16(MFT_REC_MIRR);
1336	inode = ntfs_iget5(sb, &ref, &NAME_MIRROR);
1337	if (IS_ERR(inode)) {
1338		err = PTR_ERR(inode);
1339		ntfs_err(sb, "Failed to load $MFTMirr (%d).", err);
1340		goto out;
1341	}
1342
1343	sbi->mft.recs_mirr = ntfs_up_cluster(sbi, inode->i_size) >>
1344			     sbi->record_bits;
1345
1346	iput(inode);
1347
1348	/* Load LogFile to replay. */
1349	ref.low = cpu_to_le32(MFT_REC_LOG);
1350	ref.seq = cpu_to_le16(MFT_REC_LOG);
1351	inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE);
1352	if (IS_ERR(inode)) {
1353		err = PTR_ERR(inode);
1354		ntfs_err(sb, "Failed to load \x24LogFile (%d).", err);
1355		goto out;
1356	}
1357
1358	ni = ntfs_i(inode);
1359
1360	err = ntfs_loadlog_and_replay(ni, sbi);
1361	if (err)
1362		goto put_inode_out;
1363
1364	iput(inode);
1365
1366	if ((sbi->flags & NTFS_FLAGS_NEED_REPLAY) && !ro) {
1367		ntfs_warn(sb, "failed to replay log file. Can't mount rw!");
1368		err = -EINVAL;
1369		goto out;
1370	}
1371
1372	if ((sbi->volume.flags & VOLUME_FLAG_DIRTY) && !ro && !options->force) {
1373		ntfs_warn(sb, "volume is dirty and \"force\" flag is not set!");
1374		err = -EINVAL;
1375		goto out;
1376	}
1377
1378	/* Load $MFT. */
1379	ref.low = cpu_to_le32(MFT_REC_MFT);
1380	ref.seq = cpu_to_le16(1);
1381
1382	inode = ntfs_iget5(sb, &ref, &NAME_MFT);
1383	if (IS_ERR(inode)) {
1384		err = PTR_ERR(inode);
1385		ntfs_err(sb, "Failed to load $MFT (%d).", err);
1386		goto out;
1387	}
1388
1389	ni = ntfs_i(inode);
1390
1391	sbi->mft.used = ni->i_valid >> sbi->record_bits;
1392	tt = inode->i_size >> sbi->record_bits;
1393	sbi->mft.next_free = MFT_REC_USER;
1394
1395	err = wnd_init(&sbi->mft.bitmap, sb, tt);
1396	if (err)
1397		goto put_inode_out;
1398
1399	err = ni_load_all_mi(ni);
1400	if (err) {
1401		ntfs_err(sb, "Failed to load $MFT's subrecords (%d).", err);
1402		goto put_inode_out;
1403	}
1404
1405	sbi->mft.ni = ni;
1406
1407	/* Load $Bitmap. */
1408	ref.low = cpu_to_le32(MFT_REC_BITMAP);
1409	ref.seq = cpu_to_le16(MFT_REC_BITMAP);
1410	inode = ntfs_iget5(sb, &ref, &NAME_BITMAP);
1411	if (IS_ERR(inode)) {
1412		err = PTR_ERR(inode);
1413		ntfs_err(sb, "Failed to load $Bitmap (%d).", err);
1414		goto out;
1415	}
1416
1417#ifndef CONFIG_NTFS3_64BIT_CLUSTER
1418	if (inode->i_size >> 32) {
1419		err = -EINVAL;
1420		goto put_inode_out;
1421	}
1422#endif
1423
1424	/* Check bitmap boundary. */
1425	tt = sbi->used.bitmap.nbits;
1426	if (inode->i_size < ntfs3_bitmap_size(tt)) {
1427		ntfs_err(sb, "$Bitmap is corrupted.");
1428		err = -EINVAL;
1429		goto put_inode_out;
1430	}
1431
1432	err = wnd_init(&sbi->used.bitmap, sb, tt);
1433	if (err) {
1434		ntfs_err(sb, "Failed to initialize $Bitmap (%d).", err);
1435		goto put_inode_out;
1436	}
1437
1438	iput(inode);
1439
1440	/* Compute the MFT zone. */
1441	err = ntfs_refresh_zone(sbi);
1442	if (err) {
1443		ntfs_err(sb, "Failed to initialize MFT zone (%d).", err);
1444		goto out;
1445	}
1446
1447	/* Load $BadClus. */
1448	ref.low = cpu_to_le32(MFT_REC_BADCLUST);
1449	ref.seq = cpu_to_le16(MFT_REC_BADCLUST);
1450	inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS);
1451	if (IS_ERR(inode)) {
1452		err = PTR_ERR(inode);
1453		ntfs_err(sb, "Failed to load $BadClus (%d).", err);
1454		goto out;
1455	}
1456
1457	ni = ntfs_i(inode);
1458	bad_len = bad_frags = 0;
1459	for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) {
1460		if (lcn == SPARSE_LCN)
1461			continue;
1462
1463		bad_len += len;
1464		bad_frags += 1;
1465		if (ro)
1466			continue;
1467
1468		if (wnd_set_used_safe(&sbi->used.bitmap, lcn, len, &tt) || tt) {
1469			/* Bad blocks marked as free in bitmap. */
1470			ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1471		}
1472	}
1473	if (bad_len) {
1474		/*
1475		 * Notice about bad blocks.
1476		 * In normal cases these blocks are marked as used in bitmap.
1477		 * And we never allocate space in it.
1478		 */
1479		ntfs_notice(sb,
1480			    "Volume contains %zu bad blocks in %zu fragments.",
1481			    bad_len, bad_frags);
1482	}
1483	iput(inode);
1484
1485	/* Load $AttrDef. */
1486	ref.low = cpu_to_le32(MFT_REC_ATTR);
1487	ref.seq = cpu_to_le16(MFT_REC_ATTR);
1488	inode = ntfs_iget5(sb, &ref, &NAME_ATTRDEF);
1489	if (IS_ERR(inode)) {
1490		err = PTR_ERR(inode);
1491		ntfs_err(sb, "Failed to load $AttrDef (%d)", err);
1492		goto out;
1493	}
1494
1495	/*
1496	 * Typical $AttrDef contains up to 20 entries.
1497	 * Check for extremely large/small size.
1498	 */
1499	if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY) ||
1500	    inode->i_size > 100 * sizeof(struct ATTR_DEF_ENTRY)) {
1501		ntfs_err(sb, "Looks like $AttrDef is corrupted (size=%llu).",
1502			 inode->i_size);
1503		err = -EINVAL;
1504		goto put_inode_out;
1505	}
1506
1507	bytes = inode->i_size;
1508	sbi->def_table = t = kvmalloc(bytes, GFP_KERNEL);
1509	if (!t) {
1510		err = -ENOMEM;
1511		goto put_inode_out;
1512	}
1513
1514	/* Read the entire file. */
1515	err = inode_read_data(inode, sbi->def_table, bytes);
1516	if (err) {
1517		ntfs_err(sb, "Failed to read $AttrDef (%d).", err);
1518		goto put_inode_out;
1519	}
1520
1521	if (ATTR_STD != t->type) {
1522		ntfs_err(sb, "$AttrDef is corrupted.");
1523		err = -EINVAL;
1524		goto put_inode_out;
1525	}
1526
1527	t += 1;
1528	sbi->def_entries = 1;
1529	done = sizeof(struct ATTR_DEF_ENTRY);
1530
1531	while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) {
1532		u32 t32 = le32_to_cpu(t->type);
1533		u64 sz = le64_to_cpu(t->max_sz);
1534
1535		if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32)
1536			break;
1537
1538		if (t->type == ATTR_REPARSE)
1539			sbi->reparse.max_size = sz;
1540		else if (t->type == ATTR_EA)
1541			sbi->ea_max_size = sz;
1542
1543		done += sizeof(struct ATTR_DEF_ENTRY);
1544		t += 1;
1545		sbi->def_entries += 1;
1546	}
1547	iput(inode);
1548
1549	/* Load $UpCase. */
1550	ref.low = cpu_to_le32(MFT_REC_UPCASE);
1551	ref.seq = cpu_to_le16(MFT_REC_UPCASE);
1552	inode = ntfs_iget5(sb, &ref, &NAME_UPCASE);
1553	if (IS_ERR(inode)) {
1554		err = PTR_ERR(inode);
1555		ntfs_err(sb, "Failed to load $UpCase (%d).", err);
1556		goto out;
1557	}
1558
1559	if (inode->i_size != 0x10000 * sizeof(short)) {
1560		err = -EINVAL;
1561		ntfs_err(sb, "$UpCase is corrupted.");
1562		goto put_inode_out;
1563	}
1564
1565	/* Read the entire file. */
1566	err = inode_read_data(inode, sbi->upcase, 0x10000 * sizeof(short));
1567	if (err) {
1568		ntfs_err(sb, "Failed to read $UpCase (%d).", err);
1569		goto put_inode_out;
1570	}
1571
1572#ifdef __BIG_ENDIAN
1573	{
1574		u16 *dst = sbi->upcase;
1575
1576		for (i = 0; i < 0x10000; i++)
1577			__swab16s(dst++);
1578	}
1579#endif
1580
1581	shared = ntfs_set_shared(sbi->upcase, 0x10000 * sizeof(short));
1582	if (shared && sbi->upcase != shared) {
1583		kvfree(sbi->upcase);
1584		sbi->upcase = shared;
1585	}
1586
1587	iput(inode);
1588
1589	if (is_ntfs3(sbi)) {
1590		/* Load $Secure. */
1591		err = ntfs_security_init(sbi);
1592		if (err) {
1593			ntfs_err(sb, "Failed to initialize $Secure (%d).", err);
1594			goto out;
1595		}
1596
1597		/* Load $Extend. */
1598		err = ntfs_extend_init(sbi);
1599		if (err) {
1600			ntfs_warn(sb, "Failed to initialize $Extend.");
1601			goto load_root;
1602		}
1603
1604		/* Load $Extend/$Reparse. */
1605		err = ntfs_reparse_init(sbi);
1606		if (err) {
1607			ntfs_warn(sb, "Failed to initialize $Extend/$Reparse.");
1608			goto load_root;
1609		}
1610
1611		/* Load $Extend/$ObjId. */
1612		err = ntfs_objid_init(sbi);
1613		if (err) {
1614			ntfs_warn(sb, "Failed to initialize $Extend/$ObjId.");
1615			goto load_root;
1616		}
1617	}
1618
1619load_root:
1620	/* Load root. */
1621	ref.low = cpu_to_le32(MFT_REC_ROOT);
1622	ref.seq = cpu_to_le16(MFT_REC_ROOT);
1623	inode = ntfs_iget5(sb, &ref, &NAME_ROOT);
1624	if (IS_ERR(inode)) {
1625		err = PTR_ERR(inode);
1626		ntfs_err(sb, "Failed to load root (%d).", err);
1627		goto out;
1628	}
1629
1630	/*
1631	 * Final check. Looks like this case should never occurs.
1632	 */
1633	if (!inode->i_op) {
1634		err = -EINVAL;
1635		ntfs_err(sb, "Failed to load root (%d).", err);
1636		goto put_inode_out;
1637	}
1638
1639	sb->s_root = d_make_root(inode);
1640	if (!sb->s_root) {
1641		err = -ENOMEM;
1642		goto put_inode_out;
1643	}
1644
1645	if (boot2) {
1646		/*
1647	 	 * Alternative boot is ok but primary is not ok.
1648	 	 * Volume is recognized as NTFS. Update primary boot.
1649		 */
1650		struct buffer_head *bh0 = sb_getblk(sb, 0);
1651		if (bh0) {
1652			if (buffer_locked(bh0))
1653				__wait_on_buffer(bh0);
1654
1655			lock_buffer(bh0);
1656			memcpy(bh0->b_data, boot2, sizeof(*boot2));
1657			set_buffer_uptodate(bh0);
1658			mark_buffer_dirty(bh0);
1659			unlock_buffer(bh0);
1660			if (!sync_dirty_buffer(bh0))
1661				ntfs_warn(sb, "primary boot is updated");
1662			put_bh(bh0);
1663		}
1664
1665		kfree(boot2);
1666	}
1667
1668	ntfs_create_procdir(sb);
1669
1670	if (is_legacy_ntfs(sb))
1671		sb->s_flags |= SB_RDONLY;
1672	return 0;
1673
1674put_inode_out:
1675	iput(inode);
1676out:
1677	/* sbi->options == options */
1678	if (options) {
1679		put_mount_options(sbi->options);
1680		sbi->options = NULL;
1681	}
1682
1683	ntfs3_put_sbi(sbi);
1684	kfree(boot2);
1685	return err;
1686}
1687
1688void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len)
1689{
1690	struct ntfs_sb_info *sbi = sb->s_fs_info;
1691	struct block_device *bdev = sb->s_bdev;
1692	sector_t devblock = (u64)lcn * sbi->blocks_per_cluster;
1693	unsigned long blocks = (u64)len * sbi->blocks_per_cluster;
1694	unsigned long cnt = 0;
1695	unsigned long limit = global_zone_page_state(NR_FREE_PAGES)
1696			      << (PAGE_SHIFT - sb->s_blocksize_bits);
1697
1698	if (limit >= 0x2000)
1699		limit -= 0x1000;
1700	else if (limit < 32)
1701		limit = 32;
1702	else
1703		limit >>= 1;
1704
1705	while (blocks--) {
1706		clean_bdev_aliases(bdev, devblock++, 1);
1707		if (cnt++ >= limit) {
1708			sync_blockdev(bdev);
1709			cnt = 0;
1710		}
1711	}
1712}
1713
1714/*
1715 * ntfs_discard - Issue a discard request (trim for SSD).
1716 */
1717int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len)
1718{
1719	int err;
1720	u64 lbo, bytes, start, end;
1721	struct super_block *sb;
1722
1723	if (sbi->used.next_free_lcn == lcn + len)
1724		sbi->used.next_free_lcn = lcn;
1725
1726	if (sbi->flags & NTFS_FLAGS_NODISCARD)
1727		return -EOPNOTSUPP;
1728
1729	if (!sbi->options->discard)
1730		return -EOPNOTSUPP;
1731
1732	lbo = (u64)lcn << sbi->cluster_bits;
1733	bytes = (u64)len << sbi->cluster_bits;
1734
1735	/* Align up 'start' on discard_granularity. */
1736	start = (lbo + sbi->discard_granularity - 1) &
1737		sbi->discard_granularity_mask_inv;
1738	/* Align down 'end' on discard_granularity. */
1739	end = (lbo + bytes) & sbi->discard_granularity_mask_inv;
1740
1741	sb = sbi->sb;
1742	if (start >= end)
1743		return 0;
1744
1745	err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9,
1746				   GFP_NOFS);
1747
1748	if (err == -EOPNOTSUPP)
1749		sbi->flags |= NTFS_FLAGS_NODISCARD;
1750
1751	return err;
1752}
1753
1754static int ntfs_fs_get_tree(struct fs_context *fc)
1755{
1756	return get_tree_bdev(fc, ntfs_fill_super);
1757}
1758
1759/*
1760 * ntfs_fs_free - Free fs_context.
1761 *
1762 * Note that this will be called after fill_super and reconfigure
1763 * even when they pass. So they have to take pointers if they pass.
1764 */
1765static void ntfs_fs_free(struct fs_context *fc)
1766{
1767	struct ntfs_mount_options *opts = fc->fs_private;
1768	struct ntfs_sb_info *sbi = fc->s_fs_info;
1769
1770	if (sbi) {
1771		ntfs3_put_sbi(sbi);
1772		ntfs3_free_sbi(sbi);
1773	}
1774
1775	if (opts)
1776		put_mount_options(opts);
1777}
1778
1779// clang-format off
1780static const struct fs_context_operations ntfs_context_ops = {
1781	.parse_param	= ntfs_fs_parse_param,
1782	.get_tree	= ntfs_fs_get_tree,
1783	.reconfigure	= ntfs_fs_reconfigure,
1784	.free		= ntfs_fs_free,
1785};
1786// clang-format on
1787
1788/*
1789 * ntfs_init_fs_context - Initialize sbi and opts
1790 *
1791 * This will called when mount/remount. We will first initialize
1792 * options so that if remount we can use just that.
1793 */
1794static int __ntfs_init_fs_context(struct fs_context *fc)
1795{
1796	struct ntfs_mount_options *opts;
1797	struct ntfs_sb_info *sbi;
1798
1799	opts = kzalloc(sizeof(struct ntfs_mount_options), GFP_NOFS);
1800	if (!opts)
1801		return -ENOMEM;
1802
1803	/* Default options. */
1804	opts->fs_uid = current_uid();
1805	opts->fs_gid = current_gid();
1806	opts->fs_fmask_inv = ~current_umask();
1807	opts->fs_dmask_inv = ~current_umask();
1808	opts->prealloc = 1;
1809
1810#ifdef CONFIG_NTFS3_FS_POSIX_ACL
1811	/* Set the default value 'acl' */
1812	fc->sb_flags |= SB_POSIXACL;
1813#endif
1814
1815	if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
1816		goto ok;
1817
1818	sbi = kzalloc(sizeof(struct ntfs_sb_info), GFP_NOFS);
1819	if (!sbi)
1820		goto free_opts;
1821
1822	sbi->upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL);
1823	if (!sbi->upcase)
1824		goto free_sbi;
1825
1826	ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1827			     DEFAULT_RATELIMIT_BURST);
1828
1829	mutex_init(&sbi->compress.mtx_lznt);
1830#ifdef CONFIG_NTFS3_LZX_XPRESS
1831	mutex_init(&sbi->compress.mtx_xpress);
1832	mutex_init(&sbi->compress.mtx_lzx);
1833#endif
1834
1835	fc->s_fs_info = sbi;
1836ok:
1837	fc->fs_private = opts;
1838	fc->ops = &ntfs_context_ops;
1839
1840	return 0;
1841free_sbi:
1842	kfree(sbi);
1843free_opts:
1844	kfree(opts);
1845	return -ENOMEM;
1846}
1847
1848static int ntfs_init_fs_context(struct fs_context *fc)
1849{
1850	return __ntfs_init_fs_context(fc);
1851}
1852
1853static void ntfs3_kill_sb(struct super_block *sb)
1854{
1855	struct ntfs_sb_info *sbi = sb->s_fs_info;
1856
1857	kill_block_super(sb);
1858
1859	if (sbi->options)
1860		put_mount_options(sbi->options);
1861	ntfs3_free_sbi(sbi);
1862}
1863
1864// clang-format off
1865static struct file_system_type ntfs_fs_type = {
1866	.owner			= THIS_MODULE,
1867	.name			= "ntfs3",
1868	.init_fs_context	= ntfs_init_fs_context,
1869	.parameters		= ntfs_fs_parameters,
1870	.kill_sb		= ntfs3_kill_sb,
1871	.fs_flags		= FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
1872};
1873
1874#if IS_ENABLED(CONFIG_NTFS_FS)
1875static int ntfs_legacy_init_fs_context(struct fs_context *fc)
1876{
1877	int ret;
1878
1879	ret = __ntfs_init_fs_context(fc);
1880	/* If ntfs3 is used as legacy ntfs enforce read-only mode. */
1881	fc->sb_flags |= SB_RDONLY;
1882	return ret;
1883}
1884
1885static struct file_system_type ntfs_legacy_fs_type = {
1886	.owner			= THIS_MODULE,
1887	.name			= "ntfs",
1888	.init_fs_context	= ntfs_legacy_init_fs_context,
1889	.parameters		= ntfs_fs_parameters,
1890	.kill_sb		= ntfs3_kill_sb,
1891	.fs_flags		= FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
1892};
1893MODULE_ALIAS_FS("ntfs");
1894
1895static inline void register_as_ntfs_legacy(void)
1896{
1897	int err = register_filesystem(&ntfs_legacy_fs_type);
1898	if (err)
1899		pr_warn("ntfs3: Failed to register legacy ntfs filesystem driver: %d\n", err);
1900}
1901
1902static inline void unregister_as_ntfs_legacy(void)
1903{
1904	unregister_filesystem(&ntfs_legacy_fs_type);
1905}
1906bool is_legacy_ntfs(struct super_block *sb)
1907{
1908	return sb->s_type == &ntfs_legacy_fs_type;
1909}
1910#else
1911static inline void register_as_ntfs_legacy(void) {}
1912static inline void unregister_as_ntfs_legacy(void) {}
1913#endif
1914
1915// clang-format on
1916
1917static int __init init_ntfs_fs(void)
1918{
1919	int err;
1920
1921	if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL))
1922		pr_info("ntfs3: Enabled Linux POSIX ACLs support\n");
1923	if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER))
1924		pr_notice(
1925			"ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n");
1926	if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS))
1927		pr_info("ntfs3: Read-only LZX/Xpress compression included\n");
1928
1929	ntfs_create_proc_root();
1930
1931	err = ntfs3_init_bitmap();
1932	if (err)
1933		goto out2;
1934
1935	ntfs_inode_cachep = kmem_cache_create(
1936		"ntfs_inode_cache", sizeof(struct ntfs_inode), 0,
1937		(SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT), init_once);
1938	if (!ntfs_inode_cachep) {
1939		err = -ENOMEM;
1940		goto out1;
1941	}
1942
1943	register_as_ntfs_legacy();
1944	err = register_filesystem(&ntfs_fs_type);
1945	if (err)
1946		goto out;
1947
1948	return 0;
1949out:
1950	kmem_cache_destroy(ntfs_inode_cachep);
1951out1:
1952	ntfs3_exit_bitmap();
1953out2:
1954	ntfs_remove_proc_root();
1955	return err;
1956}
1957
1958static void __exit exit_ntfs_fs(void)
1959{
1960	rcu_barrier();
1961	kmem_cache_destroy(ntfs_inode_cachep);
1962	unregister_filesystem(&ntfs_fs_type);
1963	unregister_as_ntfs_legacy();
1964	ntfs3_exit_bitmap();
1965	ntfs_remove_proc_root();
1966}
1967
1968MODULE_LICENSE("GPL");
1969MODULE_DESCRIPTION("ntfs3 read/write filesystem");
1970#ifdef CONFIG_NTFS3_FS_POSIX_ACL
1971MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support");
1972#endif
1973#ifdef CONFIG_NTFS3_64BIT_CLUSTER
1974MODULE_INFO(
1975	cluster,
1976	"Warning: Activated 64 bits per cluster. Windows does not support this");
1977#endif
1978#ifdef CONFIG_NTFS3_LZX_XPRESS
1979MODULE_INFO(compression, "Read-only lzx/xpress compression included");
1980#endif
1981
1982MODULE_AUTHOR("Konstantin Komarov");
1983MODULE_ALIAS_FS("ntfs3");
1984
1985module_init(init_ntfs_fs);
1986module_exit(exit_ntfs_fs);
~/ Read-only file preview.