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