xref: /linux/fs/ntfs3/super.c (revision a5d1079c28a5bc6caa30ef4099ef04ed17d2c6aa)
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 	sb_set_blocksize(sb, min_t(u32, sbi->cluster_size, PAGE_SIZE));
1178 
1179 	sbi->block_mask = sb->s_blocksize - 1;
1180 	sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits;
1181 	sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits;
1182 
1183 	/* Maximum size for normal files. */
1184 	sbi->maxbytes = (clusters << cluster_bits) - 1;
1185 
1186 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
1187 	if (clusters >= (1ull << (64 - cluster_bits)))
1188 		sbi->maxbytes = -1;
1189 	sbi->maxbytes_sparse = -1;
1190 	sb->s_maxbytes = MAX_LFS_FILESIZE;
1191 #else
1192 	/* Maximum size for sparse file. */
1193 	sbi->maxbytes_sparse = (1ull << (cluster_bits + 32)) - 1;
1194 	sb->s_maxbytes = 0xFFFFFFFFull << cluster_bits;
1195 #endif
1196 
1197 	/*
1198 	 * Compute the MFT zone at two steps.
1199 	 * It would be nice if we are able to allocate 1/8 of
1200 	 * total clusters for MFT but not more then 512 MB.
1201 	 */
1202 	sbi->zone_max = min_t(CLST, 0x20000000 >> cluster_bits, clusters >> 3);
1203 
1204 	err = 0;
1205 
1206 	if (bh->b_blocknr && !sb_rdonly(sb)) {
1207 		/*
1208 	 	 * Alternative boot is ok but primary is not ok.
1209 	 	 * Do not update primary boot here 'cause it may be faked boot.
1210 	 	 * Let ntfs to be mounted and update boot later.
1211 		 */
1212 		*boot2 = kmemdup(boot, sizeof(*boot), GFP_NOFS | __GFP_NOWARN);
1213 	}
1214 
1215 out:
1216 	brelse(bh);
1217 
1218 	if (err == -EINVAL && !boot_block && dev_size0 > PAGE_SHIFT) {
1219 		u32 block_size = min_t(u32, sector_size, PAGE_SIZE);
1220 		u64 lbo = dev_size0 - sizeof(*boot);
1221 
1222 		boot_block = lbo >> blksize_bits(block_size);
1223 		boot_off = lbo & (block_size - 1);
1224 		if (boot_block && block_size >= boot_off + sizeof(*boot)) {
1225 			/*
1226 			 * Try alternative boot (last sector)
1227 			 */
1228 			sb_set_blocksize(sb, block_size);
1229 			hint = "Alternative boot";
1230 			dev_size = dev_size0; /* restore original size. */
1231 			goto read_boot;
1232 		}
1233 	}
1234 
1235 	return err;
1236 }
1237 
1238 /*
1239  * ntfs_fill_super - Try to mount.
1240  */
1241 static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
1242 {
1243 	int err;
1244 	struct ntfs_sb_info *sbi = sb->s_fs_info;
1245 	struct block_device *bdev = sb->s_bdev;
1246 	struct ntfs_mount_options *fc_opts;
1247 	struct ntfs_mount_options *options = NULL;
1248 	struct inode *inode;
1249 	struct ntfs_inode *ni;
1250 	size_t i, tt, bad_len, bad_frags;
1251 	CLST vcn, lcn, len;
1252 	struct ATTRIB *attr;
1253 	const struct VOLUME_INFO *info;
1254 	u32 done, bytes;
1255 	struct ATTR_DEF_ENTRY *t;
1256 	u16 *shared;
1257 	struct MFT_REF ref;
1258 	bool ro = sb_rdonly(sb);
1259 	struct NTFS_BOOT *boot2 = NULL;
1260 
1261 	ref.high = 0;
1262 
1263 	sbi->sb = sb;
1264 	fc_opts = fc->fs_private;
1265 	if (!fc_opts) {
1266 		errorf(fc, "missing mount options");
1267 		return -EINVAL;
1268 	}
1269 	options = kmemdup(fc_opts, sizeof(*fc_opts), GFP_KERNEL);
1270 	if (!options)
1271 		return -ENOMEM;
1272 
1273 	if (fc_opts->nls_name) {
1274 		options->nls_name = kstrdup(fc_opts->nls_name, GFP_KERNEL);
1275 		if (!options->nls_name) {
1276 			kfree(options);
1277 			return -ENOMEM;
1278 		}
1279 	}
1280 	sbi->options = options;
1281 	sb->s_flags |= SB_NODIRATIME;
1282 	sb->s_magic = 0x7366746e; // "ntfs"
1283 	sb->s_op = &ntfs_sops;
1284 	sb->s_export_op = &ntfs_export_ops;
1285 	sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec
1286 	sb->s_xattr = ntfs_xattr_handlers;
1287 	set_default_d_op(sb, options->nocase ? &ntfs_dentry_ops : NULL);
1288 
1289 	options->nls = ntfs_load_nls(options->nls_name);
1290 	if (IS_ERR(options->nls)) {
1291 		options->nls = NULL;
1292 		errorf(fc, "Cannot load nls %s", options->nls_name);
1293 		err = -EINVAL;
1294 		goto out;
1295 	}
1296 
1297 	if (bdev_max_discard_sectors(bdev) && bdev_discard_granularity(bdev)) {
1298 		sbi->discard_granularity = bdev_discard_granularity(bdev);
1299 		sbi->discard_granularity_mask_inv =
1300 			~(u64)(sbi->discard_granularity - 1);
1301 	}
1302 
1303 	/* Parse boot. */
1304 	err = ntfs_init_from_boot(sb, bdev_logical_block_size(bdev),
1305 				  bdev_nr_bytes(bdev), &boot2);
1306 	if (err)
1307 		goto out;
1308 
1309 	/*
1310 	 * Load $Volume. This should be done before $LogFile
1311 	 * 'cause 'sbi->volume.ni' is used in 'ntfs_set_state'.
1312 	 */
1313 	ref.low = cpu_to_le32(MFT_REC_VOL);
1314 	ref.seq = cpu_to_le16(MFT_REC_VOL);
1315 	inode = ntfs_iget5(sb, &ref, &NAME_VOLUME);
1316 	if (IS_ERR(inode)) {
1317 		err = PTR_ERR(inode);
1318 		ntfs_err(sb, "Failed to load $Volume (%d).", err);
1319 		goto out;
1320 	}
1321 
1322 	ni = ntfs_i(inode);
1323 
1324 	/* Load and save label (not necessary). */
1325 	attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL);
1326 
1327 	if (!attr) {
1328 		/* It is ok if no ATTR_LABEL */
1329 	} else if (!attr->non_res && !is_attr_ext(attr)) {
1330 		/* $AttrDef allows labels to be up to 128 symbols. */
1331 		err = utf16s_to_utf8s(resident_data(attr),
1332 				      le32_to_cpu(attr->res.data_size) >> 1,
1333 				      UTF16_LITTLE_ENDIAN, sbi->volume.label,
1334 				      sizeof(sbi->volume.label));
1335 		if (err < 0) {
1336 			sbi->volume.label[0] = 0;
1337 		} else if (err >= sizeof(sbi->volume.label)) {
1338 			sbi->volume.label[sizeof(sbi->volume.label) - 1] = 0;
1339 		} else {
1340 			sbi->volume.label[err] = 0;
1341 		}
1342 	} else {
1343 		/* Should we break mounting here? */
1344 		//err = -EINVAL;
1345 		//goto put_inode_out;
1346 	}
1347 
1348 	attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL);
1349 	if (!attr || is_attr_ext(attr) ||
1350 	    !(info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO))) {
1351 		ntfs_err(sb, "$Volume is corrupted.");
1352 		err = -EINVAL;
1353 		goto put_inode_out;
1354 	}
1355 
1356 	sbi->volume.major_ver = info->major_ver;
1357 	sbi->volume.minor_ver = info->minor_ver;
1358 	sbi->volume.flags = info->flags;
1359 	sbi->volume.ni = ni;
1360 	if (info->flags & VOLUME_FLAG_DIRTY) {
1361 		sbi->volume.real_dirty = true;
1362 		ntfs_info(sb, "It is recommended to use chkdsk.");
1363 	}
1364 
1365 	/* Load $MFTMirr to estimate recs_mirr. */
1366 	ref.low = cpu_to_le32(MFT_REC_MIRR);
1367 	ref.seq = cpu_to_le16(MFT_REC_MIRR);
1368 	inode = ntfs_iget5(sb, &ref, &NAME_MIRROR);
1369 	if (IS_ERR(inode)) {
1370 		err = PTR_ERR(inode);
1371 		ntfs_err(sb, "Failed to load $MFTMirr (%d).", err);
1372 		goto out;
1373 	}
1374 
1375 	sbi->mft.recs_mirr = ntfs_up_cluster(sbi, inode->i_size) >>
1376 			     sbi->record_bits;
1377 
1378 	iput(inode);
1379 
1380 	/* Load LogFile to replay. */
1381 	ref.low = cpu_to_le32(MFT_REC_LOG);
1382 	ref.seq = cpu_to_le16(MFT_REC_LOG);
1383 	inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE);
1384 	if (IS_ERR(inode)) {
1385 		err = PTR_ERR(inode);
1386 		ntfs_err(sb, "Failed to load \x24LogFile (%d).", err);
1387 		goto out;
1388 	}
1389 
1390 	ni = ntfs_i(inode);
1391 
1392 	err = ntfs_loadlog_and_replay(ni, sbi);
1393 	if (err)
1394 		goto put_inode_out;
1395 
1396 	iput(inode);
1397 
1398 	if ((sbi->flags & NTFS_FLAGS_NEED_REPLAY) && !ro) {
1399 		ntfs_warn(sb, "failed to replay log file. Can't mount rw!");
1400 		err = -EINVAL;
1401 		goto out;
1402 	}
1403 
1404 	if ((sbi->volume.flags & VOLUME_FLAG_DIRTY) && !ro && !options->force) {
1405 		ntfs_warn(sb, "volume is dirty and \"force\" flag is not set!");
1406 		err = -EINVAL;
1407 		goto out;
1408 	}
1409 
1410 	/* Load $MFT. */
1411 	ref.low = cpu_to_le32(MFT_REC_MFT);
1412 	ref.seq = cpu_to_le16(1);
1413 
1414 	inode = ntfs_iget5(sb, &ref, &NAME_MFT);
1415 	if (IS_ERR(inode)) {
1416 		err = PTR_ERR(inode);
1417 		ntfs_err(sb, "Failed to load $MFT (%d).", err);
1418 		goto out;
1419 	}
1420 
1421 	ni = ntfs_i(inode);
1422 
1423 	sbi->mft.used = ni->i_valid >> sbi->record_bits;
1424 	tt = inode->i_size >> sbi->record_bits;
1425 	sbi->mft.next_free = MFT_REC_USER;
1426 
1427 	err = ni_load_all_mi(ni);
1428 	if (err) {
1429 		ntfs_err(sb, "Failed to load $MFT's subrecords (%d).", err);
1430 		goto put_inode_out;
1431 	}
1432 
1433 	/* Merge MFT bitmap runs from extent records loaded by ni_load_all_mi. */
1434 	{
1435 		struct ATTRIB *a = NULL;
1436 		struct ATTR_LIST_ENTRY *le = NULL;
1437 
1438 		while ((a = ni_enum_attr_ex(ni, a, &le, NULL))) {
1439 			CLST svcn, evcn;
1440 			u16 roff;
1441 
1442 			if (a->type != ATTR_BITMAP || !a->non_res)
1443 				continue;
1444 
1445 			svcn = le64_to_cpu(a->nres.svcn);
1446 			if (!svcn)
1447 				continue; /* Base record runs already loaded. */
1448 
1449 			evcn = le64_to_cpu(a->nres.evcn);
1450 			roff = le16_to_cpu(a->nres.run_off);
1451 
1452 			err = run_unpack_ex(&sbi->mft.bitmap.run, sbi,
1453 					    MFT_REC_MFT, svcn, evcn, svcn,
1454 					    Add2Ptr(a, roff),
1455 					    le32_to_cpu(a->size) - roff);
1456 			if (err < 0) {
1457 				ntfs_err(sb, "Failed to unpack $MFT bitmap extent (%d).", err);
1458 				goto put_inode_out;
1459 			}
1460 			err = 0;
1461 		}
1462 	}
1463 
1464 	err = wnd_init(&sbi->mft.bitmap, sb, tt);
1465 	if (err)
1466 		goto put_inode_out;
1467 
1468 	sbi->mft.ni = ni;
1469 
1470 	/* Load $Bitmap. */
1471 	ref.low = cpu_to_le32(MFT_REC_BITMAP);
1472 	ref.seq = cpu_to_le16(MFT_REC_BITMAP);
1473 	inode = ntfs_iget5(sb, &ref, &NAME_BITMAP);
1474 	if (IS_ERR(inode)) {
1475 		err = PTR_ERR(inode);
1476 		ntfs_err(sb, "Failed to load $Bitmap (%d).", err);
1477 		goto out;
1478 	}
1479 
1480 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
1481 	if (inode->i_size >> 32) {
1482 		err = -EINVAL;
1483 		goto put_inode_out;
1484 	}
1485 #endif
1486 
1487 	/* Check bitmap boundary. */
1488 	tt = sbi->used.bitmap.nbits;
1489 	if (inode->i_size < ntfs3_bitmap_size(tt)) {
1490 		ntfs_err(sb, "$Bitmap is corrupted.");
1491 		err = -EINVAL;
1492 		goto put_inode_out;
1493 	}
1494 
1495 	err = wnd_init(&sbi->used.bitmap, sb, tt);
1496 	if (err) {
1497 		ntfs_err(sb, "Failed to initialize $Bitmap (%d).", err);
1498 		goto put_inode_out;
1499 	}
1500 
1501 	iput(inode);
1502 
1503 	/* Compute the MFT zone. */
1504 	err = ntfs_refresh_zone(sbi);
1505 	if (err) {
1506 		ntfs_err(sb, "Failed to initialize MFT zone (%d).", err);
1507 		goto out;
1508 	}
1509 
1510 	/* Load $BadClus. */
1511 	ref.low = cpu_to_le32(MFT_REC_BADCLUST);
1512 	ref.seq = cpu_to_le16(MFT_REC_BADCLUST);
1513 	inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS);
1514 	if (IS_ERR(inode)) {
1515 		err = PTR_ERR(inode);
1516 		ntfs_err(sb, "Failed to load $BadClus (%d).", err);
1517 		goto out;
1518 	}
1519 
1520 	ni = ntfs_i(inode);
1521 	bad_len = bad_frags = 0;
1522 	for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) {
1523 		if (lcn == SPARSE_LCN)
1524 			continue;
1525 
1526 		bad_len += len;
1527 		bad_frags += 1;
1528 		if (ro)
1529 			continue;
1530 
1531 		if (wnd_set_used_safe(&sbi->used.bitmap, lcn, len, &tt) || tt) {
1532 			/* Bad blocks marked as free in bitmap. */
1533 			ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1534 		}
1535 	}
1536 	if (bad_len) {
1537 		/*
1538 		 * Notice about bad blocks.
1539 		 * In normal cases these blocks are marked as used in bitmap.
1540 		 * And we never allocate space in it.
1541 		 */
1542 		ntfs_notice(sb,
1543 			    "Volume contains %zu bad blocks in %zu fragments.",
1544 			    bad_len, bad_frags);
1545 	}
1546 	iput(inode);
1547 
1548 	/* Load $AttrDef. */
1549 	ref.low = cpu_to_le32(MFT_REC_ATTR);
1550 	ref.seq = cpu_to_le16(MFT_REC_ATTR);
1551 	inode = ntfs_iget5(sb, &ref, &NAME_ATTRDEF);
1552 	if (IS_ERR(inode)) {
1553 		err = PTR_ERR(inode);
1554 		ntfs_err(sb, "Failed to load $AttrDef (%d)", err);
1555 		goto out;
1556 	}
1557 
1558 	/*
1559 	 * Typical $AttrDef contains up to 20 entries.
1560 	 * Check for extremely large/small size.
1561 	 */
1562 	if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY) ||
1563 	    inode->i_size > 100 * sizeof(struct ATTR_DEF_ENTRY)) {
1564 		ntfs_err(sb, "Looks like $AttrDef is corrupted (size=%llu).",
1565 			 inode->i_size);
1566 		err = -EINVAL;
1567 		goto put_inode_out;
1568 	}
1569 
1570 	bytes = inode->i_size;
1571 	sbi->def_table = t = kvmalloc(bytes, GFP_KERNEL);
1572 	if (!t) {
1573 		err = -ENOMEM;
1574 		goto put_inode_out;
1575 	}
1576 
1577 	/* Read the entire file. */
1578 	err = inode_read_data(inode, sbi->def_table, bytes);
1579 	if (err) {
1580 		ntfs_err(sb, "Failed to read $AttrDef (%d).", err);
1581 		goto put_inode_out;
1582 	}
1583 
1584 	if (ATTR_STD != t->type) {
1585 		ntfs_err(sb, "$AttrDef is corrupted.");
1586 		err = -EINVAL;
1587 		goto put_inode_out;
1588 	}
1589 
1590 	t += 1;
1591 	sbi->def_entries = 1;
1592 	done = sizeof(struct ATTR_DEF_ENTRY);
1593 
1594 	while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) {
1595 		u32 t32 = le32_to_cpu(t->type);
1596 		u64 sz = le64_to_cpu(t->max_sz);
1597 
1598 		if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32)
1599 			break;
1600 
1601 		if (t->type == ATTR_REPARSE)
1602 			sbi->reparse.max_size = sz;
1603 		else if (t->type == ATTR_EA)
1604 			sbi->ea_max_size = sz;
1605 
1606 		done += sizeof(struct ATTR_DEF_ENTRY);
1607 		t += 1;
1608 		sbi->def_entries += 1;
1609 	}
1610 	iput(inode);
1611 
1612 	/* Load $UpCase. */
1613 	ref.low = cpu_to_le32(MFT_REC_UPCASE);
1614 	ref.seq = cpu_to_le16(MFT_REC_UPCASE);
1615 	inode = ntfs_iget5(sb, &ref, &NAME_UPCASE);
1616 	if (IS_ERR(inode)) {
1617 		err = PTR_ERR(inode);
1618 		ntfs_err(sb, "Failed to load $UpCase (%d).", err);
1619 		goto out;
1620 	}
1621 
1622 	if (inode->i_size != 0x10000 * sizeof(short)) {
1623 		err = -EINVAL;
1624 		ntfs_err(sb, "$UpCase is corrupted.");
1625 		goto put_inode_out;
1626 	}
1627 
1628 	/* Read the entire file. */
1629 	err = inode_read_data(inode, sbi->upcase, 0x10000 * sizeof(short));
1630 	if (err) {
1631 		ntfs_err(sb, "Failed to read $UpCase (%d).", err);
1632 		goto put_inode_out;
1633 	}
1634 
1635 #ifdef __BIG_ENDIAN
1636 	{
1637 		u16 *dst = sbi->upcase;
1638 
1639 		for (i = 0; i < 0x10000; i++)
1640 			__swab16s(dst++);
1641 	}
1642 #endif
1643 
1644 	shared = ntfs_set_shared(sbi->upcase, 0x10000 * sizeof(short));
1645 	if (shared && sbi->upcase != shared) {
1646 		kvfree(sbi->upcase);
1647 		sbi->upcase = shared;
1648 	}
1649 
1650 	iput(inode);
1651 
1652 	if (is_ntfs3(sbi)) {
1653 		/* Load $Secure. */
1654 		err = ntfs_security_init(sbi);
1655 		if (err) {
1656 			ntfs_err(sb, "Failed to initialize $Secure (%d).", err);
1657 			goto out;
1658 		}
1659 
1660 		/* Load $Extend. */
1661 		err = ntfs_extend_init(sbi);
1662 		if (err) {
1663 			ntfs_warn(sb, "Failed to initialize $Extend.");
1664 			goto load_root;
1665 		}
1666 
1667 		/* Load $Extend/$Reparse. */
1668 		err = ntfs_reparse_init(sbi);
1669 		if (err) {
1670 			ntfs_warn(sb, "Failed to initialize $Extend/$Reparse.");
1671 			goto load_root;
1672 		}
1673 
1674 		/* Load $Extend/$ObjId. */
1675 		err = ntfs_objid_init(sbi);
1676 		if (err) {
1677 			ntfs_warn(sb, "Failed to initialize $Extend/$ObjId.");
1678 			goto load_root;
1679 		}
1680 	}
1681 
1682 load_root:
1683 	/* Load root. */
1684 	ref.low = cpu_to_le32(MFT_REC_ROOT);
1685 	ref.seq = cpu_to_le16(MFT_REC_ROOT);
1686 	inode = ntfs_iget5(sb, &ref, &NAME_ROOT);
1687 	if (IS_ERR(inode)) {
1688 		err = PTR_ERR(inode);
1689 		ntfs_err(sb, "Failed to load root (%d).", err);
1690 		goto out;
1691 	}
1692 
1693 	/*
1694 	 * Final check. Looks like this case should never occurs.
1695 	 */
1696 	if (!inode->i_op) {
1697 		err = -EINVAL;
1698 		ntfs_err(sb, "Failed to load root (%d).", err);
1699 		goto put_inode_out;
1700 	}
1701 
1702 	sb->s_root = d_make_root(inode);
1703 	if (!sb->s_root) {
1704 		err = -ENOMEM;
1705 		goto out;
1706 	}
1707 
1708 	if (boot2) {
1709 		/*
1710 	 	 * Alternative boot is ok but primary is not ok.
1711 	 	 * Volume is recognized as NTFS. Update primary boot.
1712 		 */
1713 		struct buffer_head *bh0 = sb_getblk(sb, 0);
1714 		if (bh0) {
1715 			wait_on_buffer(bh0);
1716 			lock_buffer(bh0);
1717 			memcpy(bh0->b_data, boot2, sizeof(*boot2));
1718 			set_buffer_uptodate(bh0);
1719 			mark_buffer_dirty(bh0);
1720 			unlock_buffer(bh0);
1721 			if (!sync_dirty_buffer(bh0))
1722 				ntfs_warn(sb, "primary boot is updated");
1723 			put_bh(bh0);
1724 		}
1725 
1726 		kfree(boot2);
1727 	}
1728 
1729 	ntfs_create_procdir(sb);
1730 
1731 	return 0;
1732 
1733 put_inode_out:
1734 	iput(inode);
1735 out:
1736 	/* sbi->options == options */
1737 	if (options) {
1738 		put_mount_options(sbi->options);
1739 		sbi->options = NULL;
1740 	}
1741 
1742 	ntfs3_put_sbi(sbi);
1743 	kfree(boot2);
1744 	return err;
1745 }
1746 
1747 void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len)
1748 {
1749 	struct ntfs_sb_info *sbi = sb->s_fs_info;
1750 	struct block_device *bdev = sb->s_bdev;
1751 	sector_t devblock = (u64)lcn * sbi->blocks_per_cluster;
1752 	unsigned long blocks = (u64)len * sbi->blocks_per_cluster;
1753 	unsigned long cnt = 0;
1754 	unsigned long limit = global_zone_page_state(NR_FREE_PAGES)
1755 			      << (PAGE_SHIFT - sb->s_blocksize_bits);
1756 
1757 	if (limit >= 0x2000)
1758 		limit -= 0x1000;
1759 	else if (limit < 32)
1760 		limit = 32;
1761 	else
1762 		limit >>= 1;
1763 
1764 	while (blocks--) {
1765 		clean_bdev_aliases(bdev, devblock++, 1);
1766 		if (cnt++ >= limit) {
1767 			sync_blockdev(bdev);
1768 			cnt = 0;
1769 		}
1770 	}
1771 }
1772 
1773 /*
1774  * ntfs_discard - Issue a discard request (trim for SSD).
1775  */
1776 int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len)
1777 {
1778 	int err;
1779 	u64 lbo, bytes, start, end;
1780 	struct super_block *sb;
1781 
1782 	if (sbi->used.next_free_lcn == lcn + len)
1783 		sbi->used.next_free_lcn = lcn;
1784 
1785 	if (sbi->flags & NTFS_FLAGS_NODISCARD)
1786 		return -EOPNOTSUPP;
1787 
1788 	if (!sbi->options->discard)
1789 		return -EOPNOTSUPP;
1790 
1791 	lbo = (u64)lcn << sbi->cluster_bits;
1792 	bytes = (u64)len << sbi->cluster_bits;
1793 
1794 	/* Align up 'start' on discard_granularity. */
1795 	start = (lbo + sbi->discard_granularity - 1) &
1796 		sbi->discard_granularity_mask_inv;
1797 	/* Align down 'end' on discard_granularity. */
1798 	end = (lbo + bytes) & sbi->discard_granularity_mask_inv;
1799 
1800 	sb = sbi->sb;
1801 	if (start >= end)
1802 		return 0;
1803 
1804 	err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9,
1805 				   GFP_NOFS);
1806 
1807 	if (err == -EOPNOTSUPP)
1808 		sbi->flags |= NTFS_FLAGS_NODISCARD;
1809 
1810 	return err;
1811 }
1812 
1813 static int ntfs_fs_get_tree(struct fs_context *fc)
1814 {
1815 	return get_tree_bdev(fc, ntfs_fill_super);
1816 }
1817 
1818 /*
1819  * ntfs_fs_free - Free fs_context.
1820  *
1821  * Note that this will be called after fill_super and reconfigure
1822  * even when they pass. So they have to take pointers if they pass.
1823  */
1824 static void ntfs_fs_free(struct fs_context *fc)
1825 {
1826 	struct ntfs_mount_options *opts = fc->fs_private;
1827 	struct ntfs_sb_info *sbi = fc->s_fs_info;
1828 
1829 	if (sbi) {
1830 		ntfs3_put_sbi(sbi);
1831 		ntfs3_free_sbi(sbi);
1832 	}
1833 
1834 	if (opts)
1835 		put_mount_options(opts);
1836 }
1837 
1838 // clang-format off
1839 static const struct fs_context_operations ntfs_context_ops = {
1840 	.parse_param	= ntfs_fs_parse_param,
1841 	.get_tree	= ntfs_fs_get_tree,
1842 	.reconfigure	= ntfs_fs_reconfigure,
1843 	.free		= ntfs_fs_free,
1844 };
1845 // clang-format on
1846 
1847 /*
1848  * ntfs_init_fs_context - Initialize sbi and opts
1849  *
1850  * This will called when mount/remount. We will first initialize
1851  * options so that if remount we can use just that.
1852  */
1853 static int ntfs_init_fs_context(struct fs_context *fc)
1854 {
1855 	struct ntfs_mount_options *opts;
1856 	struct ntfs_sb_info *sbi;
1857 
1858 	opts = kzalloc_obj(struct ntfs_mount_options, GFP_NOFS);
1859 	if (!opts)
1860 		return -ENOMEM;
1861 
1862 	/* Default options. */
1863 	opts->fs_uid = current_uid();
1864 	opts->fs_gid = current_gid();
1865 	opts->fs_fmask_inv = ~current_umask();
1866 	opts->fs_dmask_inv = ~current_umask();
1867 	opts->prealloc = 1;
1868 
1869 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1870 	/* Set the default value 'acl' */
1871 	fc->sb_flags |= SB_POSIXACL;
1872 #endif
1873 
1874 	if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
1875 		goto ok;
1876 
1877 	sbi = kzalloc_obj(struct ntfs_sb_info, GFP_NOFS);
1878 	if (!sbi)
1879 		goto free_opts;
1880 
1881 	sbi->upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL);
1882 	if (!sbi->upcase)
1883 		goto free_sbi;
1884 
1885 	ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1886 			     DEFAULT_RATELIMIT_BURST);
1887 
1888 	mutex_init(&sbi->compress.mtx_lznt);
1889 #ifdef CONFIG_NTFS3_LZX_XPRESS
1890 	mutex_init(&sbi->compress.mtx_xpress);
1891 	mutex_init(&sbi->compress.mtx_lzx);
1892 #endif
1893 
1894 	fc->s_fs_info = sbi;
1895 ok:
1896 	fc->fs_private = opts;
1897 	fc->ops = &ntfs_context_ops;
1898 
1899 	return 0;
1900 free_sbi:
1901 	kfree(sbi);
1902 free_opts:
1903 	kfree(opts);
1904 	return -ENOMEM;
1905 }
1906 
1907 static void ntfs3_kill_sb(struct super_block *sb)
1908 {
1909 	struct ntfs_sb_info *sbi = sb->s_fs_info;
1910 
1911 	kill_block_super(sb);
1912 
1913 	if (sbi->options)
1914 		put_mount_options(sbi->options);
1915 	ntfs3_free_sbi(sbi);
1916 }
1917 
1918 // clang-format off
1919 static struct file_system_type ntfs_fs_type = {
1920 	.owner			= THIS_MODULE,
1921 	.name			= "ntfs3",
1922 	.init_fs_context	= ntfs_init_fs_context,
1923 	.parameters		= ntfs_fs_parameters,
1924 	.kill_sb		= ntfs3_kill_sb,
1925 	.fs_flags		= FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
1926 };
1927 
1928 // clang-format on
1929 
1930 static int __init init_ntfs_fs(void)
1931 {
1932 	int err;
1933 
1934 	if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL))
1935 		pr_info("ntfs3: Enabled Linux POSIX ACLs support\n");
1936 	if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER))
1937 		pr_notice(
1938 			"ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n");
1939 	if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS))
1940 		pr_info("ntfs3: Read-only LZX/Xpress compression included\n");
1941 
1942 	ntfs_create_proc_root();
1943 
1944 	err = ntfs3_init_bitmap();
1945 	if (err)
1946 		goto out2;
1947 
1948 	ntfs_inode_cachep = kmem_cache_create(
1949 		"ntfs_inode_cache", sizeof(struct ntfs_inode), 0,
1950 		(SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT), init_once);
1951 	if (!ntfs_inode_cachep) {
1952 		err = -ENOMEM;
1953 		goto out1;
1954 	}
1955 
1956 	err = register_filesystem(&ntfs_fs_type);
1957 	if (err)
1958 		goto out;
1959 
1960 	return 0;
1961 out:
1962 	kmem_cache_destroy(ntfs_inode_cachep);
1963 out1:
1964 	ntfs3_exit_bitmap();
1965 out2:
1966 	ntfs_remove_proc_root();
1967 	return err;
1968 }
1969 
1970 static void __exit exit_ntfs_fs(void)
1971 {
1972 	rcu_barrier();
1973 	kmem_cache_destroy(ntfs_inode_cachep);
1974 	unregister_filesystem(&ntfs_fs_type);
1975 	ntfs3_exit_bitmap();
1976 	ntfs_remove_proc_root();
1977 }
1978 
1979 MODULE_LICENSE("GPL");
1980 MODULE_DESCRIPTION("ntfs3 read/write filesystem");
1981 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1982 MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support");
1983 #endif
1984 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
1985 MODULE_INFO(
1986 	cluster,
1987 	"Warning: Activated 64 bits per cluster. Windows does not support this");
1988 #endif
1989 #ifdef CONFIG_NTFS3_LZX_XPRESS
1990 MODULE_INFO(compression, "Read-only lzx/xpress compression included");
1991 #endif
1992 
1993 MODULE_AUTHOR("Konstantin Komarov");
1994 MODULE_ALIAS_FS("ntfs3");
1995 
1996 module_init(init_ntfs_fs);
1997 module_exit(exit_ntfs_fs);
1998