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