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