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