xref: /openbmc/linux/fs/ntfs3/super.c (revision 0e407915)
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  */
25 
26 #include <linux/blkdev.h>
27 #include <linux/buffer_head.h>
28 #include <linux/exportfs.h>
29 #include <linux/fs.h>
30 #include <linux/fs_context.h>
31 #include <linux/fs_parser.h>
32 #include <linux/log2.h>
33 #include <linux/minmax.h>
34 #include <linux/module.h>
35 #include <linux/nls.h>
36 #include <linux/seq_file.h>
37 #include <linux/statfs.h>
38 
39 #include "debug.h"
40 #include "ntfs.h"
41 #include "ntfs_fs.h"
42 #ifdef CONFIG_NTFS3_LZX_XPRESS
43 #include "lib/lib.h"
44 #endif
45 
46 #ifdef CONFIG_PRINTK
47 /*
48  * ntfs_printk - Trace warnings/notices/errors.
49  *
50  * Thanks Joe Perches <joe@perches.com> for implementation
51  */
52 void ntfs_printk(const struct super_block *sb, const char *fmt, ...)
53 {
54 	struct va_format vaf;
55 	va_list args;
56 	int level;
57 	struct ntfs_sb_info *sbi = sb->s_fs_info;
58 
59 	/* Should we use different ratelimits for warnings/notices/errors? */
60 	if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
61 		return;
62 
63 	va_start(args, fmt);
64 
65 	level = printk_get_level(fmt);
66 	vaf.fmt = printk_skip_level(fmt);
67 	vaf.va = &args;
68 	printk("%c%cntfs3: %s: %pV\n", KERN_SOH_ASCII, level, sb->s_id, &vaf);
69 
70 	va_end(args);
71 }
72 
73 static char s_name_buf[512];
74 static atomic_t s_name_buf_cnt = ATOMIC_INIT(1); // 1 means 'free s_name_buf'.
75 
76 /*
77  * ntfs_inode_printk
78  *
79  * Print warnings/notices/errors about inode using name or inode number.
80  */
81 void ntfs_inode_printk(struct inode *inode, const char *fmt, ...)
82 {
83 	struct super_block *sb = inode->i_sb;
84 	struct ntfs_sb_info *sbi = sb->s_fs_info;
85 	char *name;
86 	va_list args;
87 	struct va_format vaf;
88 	int level;
89 
90 	if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
91 		return;
92 
93 	/* Use static allocated buffer, if possible. */
94 	name = atomic_dec_and_test(&s_name_buf_cnt)
95 		       ? s_name_buf
96 		       : kmalloc(sizeof(s_name_buf), GFP_NOFS);
97 
98 	if (name) {
99 		struct dentry *de = d_find_alias(inode);
100 		const u32 name_len = ARRAY_SIZE(s_name_buf) - 1;
101 
102 		if (de) {
103 			spin_lock(&de->d_lock);
104 			snprintf(name, name_len, " \"%s\"", de->d_name.name);
105 			spin_unlock(&de->d_lock);
106 			name[name_len] = 0; /* To be sure. */
107 		} else {
108 			name[0] = 0;
109 		}
110 		dput(de); /* Cocci warns if placed in branch "if (de)" */
111 	}
112 
113 	va_start(args, fmt);
114 
115 	level = printk_get_level(fmt);
116 	vaf.fmt = printk_skip_level(fmt);
117 	vaf.va = &args;
118 
119 	printk("%c%cntfs3: %s: ino=%lx,%s %pV\n", KERN_SOH_ASCII, level,
120 	       sb->s_id, inode->i_ino, name ? name : "", &vaf);
121 
122 	va_end(args);
123 
124 	atomic_inc(&s_name_buf_cnt);
125 	if (name != s_name_buf)
126 		kfree(name);
127 }
128 #endif
129 
130 /*
131  * Shared memory struct.
132  *
133  * On-disk ntfs's upcase table is created by ntfs formatter.
134  * 'upcase' table is 128K bytes of memory.
135  * We should read it into memory when mounting.
136  * Several ntfs volumes likely use the same 'upcase' table.
137  * It is good idea to share in-memory 'upcase' table between different volumes.
138  * Unfortunately winxp/vista/win7 use different upcase tables.
139  */
140 static DEFINE_SPINLOCK(s_shared_lock);
141 
142 static struct {
143 	void *ptr;
144 	u32 len;
145 	int cnt;
146 } s_shared[8];
147 
148 /*
149  * ntfs_set_shared
150  *
151  * Return:
152  * * @ptr - If pointer was saved in shared memory.
153  * * NULL - If pointer was not shared.
154  */
155 void *ntfs_set_shared(void *ptr, u32 bytes)
156 {
157 	void *ret = NULL;
158 	int i, j = -1;
159 
160 	spin_lock(&s_shared_lock);
161 	for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
162 		if (!s_shared[i].cnt) {
163 			j = i;
164 		} else if (bytes == s_shared[i].len &&
165 			   !memcmp(s_shared[i].ptr, ptr, bytes)) {
166 			s_shared[i].cnt += 1;
167 			ret = s_shared[i].ptr;
168 			break;
169 		}
170 	}
171 
172 	if (!ret && j != -1) {
173 		s_shared[j].ptr = ptr;
174 		s_shared[j].len = bytes;
175 		s_shared[j].cnt = 1;
176 		ret = ptr;
177 	}
178 	spin_unlock(&s_shared_lock);
179 
180 	return ret;
181 }
182 
183 /*
184  * ntfs_put_shared
185  *
186  * Return:
187  * * @ptr - If pointer is not shared anymore.
188  * * NULL - If pointer is still shared.
189  */
190 void *ntfs_put_shared(void *ptr)
191 {
192 	void *ret = ptr;
193 	int i;
194 
195 	spin_lock(&s_shared_lock);
196 	for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
197 		if (s_shared[i].cnt && s_shared[i].ptr == ptr) {
198 			if (--s_shared[i].cnt)
199 				ret = NULL;
200 			break;
201 		}
202 	}
203 	spin_unlock(&s_shared_lock);
204 
205 	return ret;
206 }
207 
208 static inline void put_mount_options(struct ntfs_mount_options *options)
209 {
210 	kfree(options->nls_name);
211 	unload_nls(options->nls);
212 	kfree(options);
213 }
214 
215 enum Opt {
216 	Opt_uid,
217 	Opt_gid,
218 	Opt_umask,
219 	Opt_dmask,
220 	Opt_fmask,
221 	Opt_immutable,
222 	Opt_discard,
223 	Opt_force,
224 	Opt_sparse,
225 	Opt_nohidden,
226 	Opt_showmeta,
227 	Opt_acl,
228 	Opt_iocharset,
229 	Opt_prealloc,
230 	Opt_noacsrules,
231 	Opt_err,
232 };
233 
234 static const struct fs_parameter_spec ntfs_fs_parameters[] = {
235 	fsparam_u32("uid",			Opt_uid),
236 	fsparam_u32("gid",			Opt_gid),
237 	fsparam_u32oct("umask",			Opt_umask),
238 	fsparam_u32oct("dmask",			Opt_dmask),
239 	fsparam_u32oct("fmask",			Opt_fmask),
240 	fsparam_flag_no("sys_immutable",	Opt_immutable),
241 	fsparam_flag_no("discard",		Opt_discard),
242 	fsparam_flag_no("force",		Opt_force),
243 	fsparam_flag_no("sparse",		Opt_sparse),
244 	fsparam_flag_no("hidden",		Opt_nohidden),
245 	fsparam_flag_no("acl",			Opt_acl),
246 	fsparam_flag_no("showmeta",		Opt_showmeta),
247 	fsparam_flag_no("prealloc",		Opt_prealloc),
248 	fsparam_flag_no("acsrules",		Opt_noacsrules),
249 	fsparam_string("iocharset",		Opt_iocharset),
250 	{}
251 };
252 
253 /*
254  * Load nls table or if @nls is utf8 then return NULL.
255  */
256 static struct nls_table *ntfs_load_nls(char *nls)
257 {
258 	struct nls_table *ret;
259 
260 	if (!nls)
261 		nls = CONFIG_NLS_DEFAULT;
262 
263 	if (strcmp(nls, "utf8") == 0)
264 		return NULL;
265 
266 	if (strcmp(nls, CONFIG_NLS_DEFAULT) == 0)
267 		return load_nls_default();
268 
269 	ret = load_nls(nls);
270 	if (ret)
271 		return ret;
272 
273 	return ERR_PTR(-EINVAL);
274 }
275 
276 static int ntfs_fs_parse_param(struct fs_context *fc,
277 			       struct fs_parameter *param)
278 {
279 	struct ntfs_mount_options *opts = fc->fs_private;
280 	struct fs_parse_result result;
281 	int opt;
282 
283 	opt = fs_parse(fc, ntfs_fs_parameters, param, &result);
284 	if (opt < 0)
285 		return opt;
286 
287 	switch (opt) {
288 	case Opt_uid:
289 		opts->fs_uid = make_kuid(current_user_ns(), result.uint_32);
290 		if (!uid_valid(opts->fs_uid))
291 			return invalf(fc, "ntfs3: Invalid value for uid.");
292 		break;
293 	case Opt_gid:
294 		opts->fs_gid = make_kgid(current_user_ns(), result.uint_32);
295 		if (!gid_valid(opts->fs_gid))
296 			return invalf(fc, "ntfs3: Invalid value for gid.");
297 		break;
298 	case Opt_umask:
299 		if (result.uint_32 & ~07777)
300 			return invalf(fc, "ntfs3: Invalid value for umask.");
301 		opts->fs_fmask_inv = ~result.uint_32;
302 		opts->fs_dmask_inv = ~result.uint_32;
303 		opts->fmask = 1;
304 		opts->dmask = 1;
305 		break;
306 	case Opt_dmask:
307 		if (result.uint_32 & ~07777)
308 			return invalf(fc, "ntfs3: Invalid value for dmask.");
309 		opts->fs_dmask_inv = ~result.uint_32;
310 		opts->dmask = 1;
311 		break;
312 	case Opt_fmask:
313 		if (result.uint_32 & ~07777)
314 			return invalf(fc, "ntfs3: Invalid value for fmask.");
315 		opts->fs_fmask_inv = ~result.uint_32;
316 		opts->fmask = 1;
317 		break;
318 	case Opt_immutable:
319 		opts->sys_immutable = result.negated ? 0 : 1;
320 		break;
321 	case Opt_discard:
322 		opts->discard = result.negated ? 0 : 1;
323 		break;
324 	case Opt_force:
325 		opts->force = result.negated ? 0 : 1;
326 		break;
327 	case Opt_sparse:
328 		opts->sparse = result.negated ? 0 : 1;
329 		break;
330 	case Opt_nohidden:
331 		opts->nohidden = result.negated ? 1 : 0;
332 		break;
333 	case Opt_acl:
334 		if (!result.negated)
335 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
336 			fc->sb_flags |= SB_POSIXACL;
337 #else
338 			return invalf(fc, "ntfs3: Support for ACL not compiled in!");
339 #endif
340 		else
341 			fc->sb_flags &= ~SB_POSIXACL;
342 		break;
343 	case Opt_showmeta:
344 		opts->showmeta = result.negated ? 0 : 1;
345 		break;
346 	case Opt_iocharset:
347 		kfree(opts->nls_name);
348 		opts->nls_name = param->string;
349 		param->string = NULL;
350 		break;
351 	case Opt_prealloc:
352 		opts->prealloc = result.negated ? 0 : 1;
353 		break;
354 	case Opt_noacsrules:
355 		opts->noacsrules = result.negated ? 1 : 0;
356 		break;
357 	default:
358 		/* Should not be here unless we forget add case. */
359 		return -EINVAL;
360 	}
361 	return 0;
362 }
363 
364 static int ntfs_fs_reconfigure(struct fs_context *fc)
365 {
366 	struct super_block *sb = fc->root->d_sb;
367 	struct ntfs_sb_info *sbi = sb->s_fs_info;
368 	struct ntfs_mount_options *new_opts = fc->fs_private;
369 	int ro_rw;
370 
371 	ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY);
372 	if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) {
373 		errorf(fc, "ntfs3: Couldn't remount rw because journal is not replayed. Please umount/remount instead\n");
374 		return -EINVAL;
375 	}
376 
377 	new_opts->nls = ntfs_load_nls(new_opts->nls_name);
378 	if (IS_ERR(new_opts->nls)) {
379 		new_opts->nls = NULL;
380 		errorf(fc, "ntfs3: Cannot load iocharset %s", new_opts->nls_name);
381 		return -EINVAL;
382 	}
383 	if (new_opts->nls != sbi->options->nls)
384 		return invalf(fc, "ntfs3: Cannot use different iocharset when remounting!");
385 
386 	sync_filesystem(sb);
387 
388 	if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) &&
389 	    !new_opts->force) {
390 		errorf(fc, "ntfs3: Volume is dirty and \"force\" flag is not set!");
391 		return -EINVAL;
392 	}
393 
394 	swap(sbi->options, fc->fs_private);
395 
396 	return 0;
397 }
398 
399 static struct kmem_cache *ntfs_inode_cachep;
400 
401 static struct inode *ntfs_alloc_inode(struct super_block *sb)
402 {
403 	struct ntfs_inode *ni = alloc_inode_sb(sb, ntfs_inode_cachep, GFP_NOFS);
404 
405 	if (!ni)
406 		return NULL;
407 
408 	memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode));
409 
410 	mutex_init(&ni->ni_lock);
411 
412 	return &ni->vfs_inode;
413 }
414 
415 static void ntfs_i_callback(struct rcu_head *head)
416 {
417 	struct inode *inode = container_of(head, struct inode, i_rcu);
418 	struct ntfs_inode *ni = ntfs_i(inode);
419 
420 	mutex_destroy(&ni->ni_lock);
421 
422 	kmem_cache_free(ntfs_inode_cachep, ni);
423 }
424 
425 static void ntfs_destroy_inode(struct inode *inode)
426 {
427 	call_rcu(&inode->i_rcu, ntfs_i_callback);
428 }
429 
430 static void init_once(void *foo)
431 {
432 	struct ntfs_inode *ni = foo;
433 
434 	inode_init_once(&ni->vfs_inode);
435 }
436 
437 /*
438  * put_ntfs - Noinline to reduce binary size.
439  */
440 static noinline void put_ntfs(struct ntfs_sb_info *sbi)
441 {
442 	kfree(sbi->new_rec);
443 	kvfree(ntfs_put_shared(sbi->upcase));
444 	kfree(sbi->def_table);
445 
446 	wnd_close(&sbi->mft.bitmap);
447 	wnd_close(&sbi->used.bitmap);
448 
449 	if (sbi->mft.ni)
450 		iput(&sbi->mft.ni->vfs_inode);
451 
452 	if (sbi->security.ni)
453 		iput(&sbi->security.ni->vfs_inode);
454 
455 	if (sbi->reparse.ni)
456 		iput(&sbi->reparse.ni->vfs_inode);
457 
458 	if (sbi->objid.ni)
459 		iput(&sbi->objid.ni->vfs_inode);
460 
461 	if (sbi->volume.ni)
462 		iput(&sbi->volume.ni->vfs_inode);
463 
464 	ntfs_update_mftmirr(sbi, 0);
465 
466 	indx_clear(&sbi->security.index_sii);
467 	indx_clear(&sbi->security.index_sdh);
468 	indx_clear(&sbi->reparse.index_r);
469 	indx_clear(&sbi->objid.index_o);
470 	kfree(sbi->compress.lznt);
471 #ifdef CONFIG_NTFS3_LZX_XPRESS
472 	xpress_free_decompressor(sbi->compress.xpress);
473 	lzx_free_decompressor(sbi->compress.lzx);
474 #endif
475 	kfree(sbi);
476 }
477 
478 static void ntfs_put_super(struct super_block *sb)
479 {
480 	struct ntfs_sb_info *sbi = sb->s_fs_info;
481 
482 	/* Mark rw ntfs as clear, if possible. */
483 	ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
484 
485 	put_mount_options(sbi->options);
486 	put_ntfs(sbi);
487 	sb->s_fs_info = NULL;
488 
489 	sync_blockdev(sb->s_bdev);
490 }
491 
492 static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf)
493 {
494 	struct super_block *sb = dentry->d_sb;
495 	struct ntfs_sb_info *sbi = sb->s_fs_info;
496 	struct wnd_bitmap *wnd = &sbi->used.bitmap;
497 
498 	buf->f_type = sb->s_magic;
499 	buf->f_bsize = sbi->cluster_size;
500 	buf->f_blocks = wnd->nbits;
501 
502 	buf->f_bfree = buf->f_bavail = wnd_zeroes(wnd);
503 	buf->f_fsid.val[0] = sbi->volume.ser_num;
504 	buf->f_fsid.val[1] = (sbi->volume.ser_num >> 32);
505 	buf->f_namelen = NTFS_NAME_LEN;
506 
507 	return 0;
508 }
509 
510 static int ntfs_show_options(struct seq_file *m, struct dentry *root)
511 {
512 	struct super_block *sb = root->d_sb;
513 	struct ntfs_sb_info *sbi = sb->s_fs_info;
514 	struct ntfs_mount_options *opts = sbi->options;
515 	struct user_namespace *user_ns = seq_user_ns(m);
516 
517 	seq_printf(m, ",uid=%u",
518 		  from_kuid_munged(user_ns, opts->fs_uid));
519 	seq_printf(m, ",gid=%u",
520 		  from_kgid_munged(user_ns, opts->fs_gid));
521 	if (opts->fmask)
522 		seq_printf(m, ",fmask=%04o", ~opts->fs_fmask_inv);
523 	if (opts->dmask)
524 		seq_printf(m, ",dmask=%04o", ~opts->fs_dmask_inv);
525 	if (opts->nls)
526 		seq_printf(m, ",iocharset=%s", opts->nls->charset);
527 	else
528 		seq_puts(m, ",iocharset=utf8");
529 	if (opts->sys_immutable)
530 		seq_puts(m, ",sys_immutable");
531 	if (opts->discard)
532 		seq_puts(m, ",discard");
533 	if (opts->sparse)
534 		seq_puts(m, ",sparse");
535 	if (opts->showmeta)
536 		seq_puts(m, ",showmeta");
537 	if (opts->nohidden)
538 		seq_puts(m, ",nohidden");
539 	if (opts->force)
540 		seq_puts(m, ",force");
541 	if (opts->noacsrules)
542 		seq_puts(m, ",noacsrules");
543 	if (opts->prealloc)
544 		seq_puts(m, ",prealloc");
545 	if (sb->s_flags & SB_POSIXACL)
546 		seq_puts(m, ",acl");
547 
548 	return 0;
549 }
550 
551 /*
552  * ntfs_sync_fs - super_operations::sync_fs
553  */
554 static int ntfs_sync_fs(struct super_block *sb, int wait)
555 {
556 	int err = 0, err2;
557 	struct ntfs_sb_info *sbi = sb->s_fs_info;
558 	struct ntfs_inode *ni;
559 	struct inode *inode;
560 
561 	ni = sbi->security.ni;
562 	if (ni) {
563 		inode = &ni->vfs_inode;
564 		err2 = _ni_write_inode(inode, wait);
565 		if (err2 && !err)
566 			err = err2;
567 	}
568 
569 	ni = sbi->objid.ni;
570 	if (ni) {
571 		inode = &ni->vfs_inode;
572 		err2 = _ni_write_inode(inode, wait);
573 		if (err2 && !err)
574 			err = err2;
575 	}
576 
577 	ni = sbi->reparse.ni;
578 	if (ni) {
579 		inode = &ni->vfs_inode;
580 		err2 = _ni_write_inode(inode, wait);
581 		if (err2 && !err)
582 			err = err2;
583 	}
584 
585 	if (!err)
586 		ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
587 
588 	ntfs_update_mftmirr(sbi, wait);
589 
590 	return err;
591 }
592 
593 static const struct super_operations ntfs_sops = {
594 	.alloc_inode = ntfs_alloc_inode,
595 	.destroy_inode = ntfs_destroy_inode,
596 	.evict_inode = ntfs_evict_inode,
597 	.put_super = ntfs_put_super,
598 	.statfs = ntfs_statfs,
599 	.show_options = ntfs_show_options,
600 	.sync_fs = ntfs_sync_fs,
601 	.write_inode = ntfs3_write_inode,
602 };
603 
604 static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino,
605 					   u32 generation)
606 {
607 	struct MFT_REF ref;
608 	struct inode *inode;
609 
610 	ref.low = cpu_to_le32(ino);
611 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
612 	ref.high = cpu_to_le16(ino >> 32);
613 #else
614 	ref.high = 0;
615 #endif
616 	ref.seq = cpu_to_le16(generation);
617 
618 	inode = ntfs_iget5(sb, &ref, NULL);
619 	if (!IS_ERR(inode) && is_bad_inode(inode)) {
620 		iput(inode);
621 		inode = ERR_PTR(-ESTALE);
622 	}
623 
624 	return inode;
625 }
626 
627 static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
628 					int fh_len, int fh_type)
629 {
630 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
631 				    ntfs_export_get_inode);
632 }
633 
634 static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
635 					int fh_len, int fh_type)
636 {
637 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
638 				    ntfs_export_get_inode);
639 }
640 
641 /* TODO: == ntfs_sync_inode */
642 static int ntfs_nfs_commit_metadata(struct inode *inode)
643 {
644 	return _ni_write_inode(inode, 1);
645 }
646 
647 static const struct export_operations ntfs_export_ops = {
648 	.fh_to_dentry = ntfs_fh_to_dentry,
649 	.fh_to_parent = ntfs_fh_to_parent,
650 	.get_parent = ntfs3_get_parent,
651 	.commit_metadata = ntfs_nfs_commit_metadata,
652 };
653 
654 /*
655  * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb".
656  */
657 static u32 format_size_gb(const u64 bytes, u32 *mb)
658 {
659 	/* Do simple right 30 bit shift of 64 bit value. */
660 	u64 kbytes = bytes >> 10;
661 	u32 kbytes32 = kbytes;
662 
663 	*mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20;
664 	if (*mb >= 100)
665 		*mb = 99;
666 
667 	return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12);
668 }
669 
670 static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot)
671 {
672 	if (boot->sectors_per_clusters <= 0x80)
673 		return boot->sectors_per_clusters;
674 	if (boot->sectors_per_clusters >= 0xf4) /* limit shift to 2MB max */
675 		return 1U << (0 - boot->sectors_per_clusters);
676 	return -EINVAL;
677 }
678 
679 /*
680  * ntfs_init_from_boot - Init internal info from on-disk boot sector.
681  */
682 static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
683 			       u64 dev_size)
684 {
685 	struct ntfs_sb_info *sbi = sb->s_fs_info;
686 	int err;
687 	u32 mb, gb, boot_sector_size, sct_per_clst, record_size;
688 	u64 sectors, clusters, mlcn, mlcn2;
689 	struct NTFS_BOOT *boot;
690 	struct buffer_head *bh;
691 	struct MFT_REC *rec;
692 	u16 fn, ao;
693 
694 	sbi->volume.blocks = dev_size >> PAGE_SHIFT;
695 
696 	bh = ntfs_bread(sb, 0);
697 	if (!bh)
698 		return -EIO;
699 
700 	err = -EINVAL;
701 	boot = (struct NTFS_BOOT *)bh->b_data;
702 
703 	if (memcmp(boot->system_id, "NTFS    ", sizeof("NTFS    ") - 1))
704 		goto out;
705 
706 	/* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/
707 	/*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1])
708 	 *	goto out;
709 	 */
710 
711 	boot_sector_size = (u32)boot->bytes_per_sector[1] << 8;
712 	if (boot->bytes_per_sector[0] || boot_sector_size < SECTOR_SIZE ||
713 	    !is_power_of_2(boot_sector_size)) {
714 		goto out;
715 	}
716 
717 	/* cluster size: 512, 1K, 2K, 4K, ... 2M */
718 	sct_per_clst = true_sectors_per_clst(boot);
719 	if ((int)sct_per_clst < 0)
720 		goto out;
721 	if (!is_power_of_2(sct_per_clst))
722 		goto out;
723 
724 	mlcn = le64_to_cpu(boot->mft_clst);
725 	mlcn2 = le64_to_cpu(boot->mft2_clst);
726 	sectors = le64_to_cpu(boot->sectors_per_volume);
727 
728 	if (mlcn * sct_per_clst >= sectors)
729 		goto out;
730 
731 	if (mlcn2 * sct_per_clst >= sectors)
732 		goto out;
733 
734 	/* Check MFT record size. */
735 	if ((boot->record_size < 0 &&
736 	     SECTOR_SIZE > (2U << (-boot->record_size))) ||
737 	    (boot->record_size >= 0 && !is_power_of_2(boot->record_size))) {
738 		goto out;
739 	}
740 
741 	/* Check index record size. */
742 	if ((boot->index_size < 0 &&
743 	     SECTOR_SIZE > (2U << (-boot->index_size))) ||
744 	    (boot->index_size >= 0 && !is_power_of_2(boot->index_size))) {
745 		goto out;
746 	}
747 
748 	sbi->volume.size = sectors * boot_sector_size;
749 
750 	gb = format_size_gb(sbi->volume.size + boot_sector_size, &mb);
751 
752 	/*
753 	 * - Volume formatted and mounted with the same sector size.
754 	 * - Volume formatted 4K and mounted as 512.
755 	 * - Volume formatted 512 and mounted as 4K.
756 	 */
757 	if (boot_sector_size != sector_size) {
758 		ntfs_warn(
759 			sb,
760 			"Different NTFS' sector size (%u) and media sector size (%u)",
761 			boot_sector_size, sector_size);
762 		dev_size += sector_size - 1;
763 	}
764 
765 	sbi->cluster_size = boot_sector_size * sct_per_clst;
766 	sbi->cluster_bits = blksize_bits(sbi->cluster_size);
767 
768 	sbi->mft.lbo = mlcn << sbi->cluster_bits;
769 	sbi->mft.lbo2 = mlcn2 << sbi->cluster_bits;
770 
771 	/* Compare boot's cluster and sector. */
772 	if (sbi->cluster_size < boot_sector_size)
773 		goto out;
774 
775 	/* Compare boot's cluster and media sector. */
776 	if (sbi->cluster_size < sector_size) {
777 		/* No way to use ntfs_get_block in this case. */
778 		ntfs_err(
779 			sb,
780 			"Failed to mount 'cause NTFS's cluster size (%u) is less than media sector size (%u)",
781 			sbi->cluster_size, sector_size);
782 		goto out;
783 	}
784 
785 	sbi->cluster_mask = sbi->cluster_size - 1;
786 	sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask;
787 	sbi->record_size = record_size = boot->record_size < 0
788 						 ? 1 << (-boot->record_size)
789 						 : (u32)boot->record_size
790 							   << sbi->cluster_bits;
791 
792 	if (record_size > MAXIMUM_BYTES_PER_MFT)
793 		goto out;
794 
795 	sbi->record_bits = blksize_bits(record_size);
796 	sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes
797 
798 	sbi->max_bytes_per_attr =
799 		record_size - ALIGN(MFTRECORD_FIXUP_OFFSET_1, 8) -
800 		ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) -
801 		ALIGN(sizeof(enum ATTR_TYPE), 8);
802 
803 	sbi->index_size = boot->index_size < 0
804 				  ? 1u << (-boot->index_size)
805 				  : (u32)boot->index_size << sbi->cluster_bits;
806 
807 	sbi->volume.ser_num = le64_to_cpu(boot->serial_num);
808 
809 	/* Warning if RAW volume. */
810 	if (dev_size < sbi->volume.size + boot_sector_size) {
811 		u32 mb0, gb0;
812 
813 		gb0 = format_size_gb(dev_size, &mb0);
814 		ntfs_warn(
815 			sb,
816 			"RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only",
817 			gb, mb, gb0, mb0);
818 		sb->s_flags |= SB_RDONLY;
819 	}
820 
821 	clusters = sbi->volume.size >> sbi->cluster_bits;
822 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
823 	/* 32 bits per cluster. */
824 	if (clusters >> 32) {
825 		ntfs_notice(
826 			sb,
827 			"NTFS %u.%02u Gb is too big to use 32 bits per cluster",
828 			gb, mb);
829 		goto out;
830 	}
831 #elif BITS_PER_LONG < 64
832 #error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS"
833 #endif
834 
835 	sbi->used.bitmap.nbits = clusters;
836 
837 	rec = kzalloc(record_size, GFP_NOFS);
838 	if (!rec) {
839 		err = -ENOMEM;
840 		goto out;
841 	}
842 
843 	sbi->new_rec = rec;
844 	rec->rhdr.sign = NTFS_FILE_SIGNATURE;
845 	rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET_1);
846 	fn = (sbi->record_size >> SECTOR_SHIFT) + 1;
847 	rec->rhdr.fix_num = cpu_to_le16(fn);
848 	ao = ALIGN(MFTRECORD_FIXUP_OFFSET_1 + sizeof(short) * fn, 8);
849 	rec->attr_off = cpu_to_le16(ao);
850 	rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8));
851 	rec->total = cpu_to_le32(sbi->record_size);
852 	((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END;
853 
854 	sb_set_blocksize(sb, min_t(u32, sbi->cluster_size, PAGE_SIZE));
855 
856 	sbi->block_mask = sb->s_blocksize - 1;
857 	sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits;
858 	sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits;
859 
860 	/* Maximum size for normal files. */
861 	sbi->maxbytes = (clusters << sbi->cluster_bits) - 1;
862 
863 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
864 	if (clusters >= (1ull << (64 - sbi->cluster_bits)))
865 		sbi->maxbytes = -1;
866 	sbi->maxbytes_sparse = -1;
867 	sb->s_maxbytes = MAX_LFS_FILESIZE;
868 #else
869 	/* Maximum size for sparse file. */
870 	sbi->maxbytes_sparse = (1ull << (sbi->cluster_bits + 32)) - 1;
871 	sb->s_maxbytes = 0xFFFFFFFFull << sbi->cluster_bits;
872 #endif
873 
874 	/*
875 	 * Compute the MFT zone at two steps.
876 	 * It would be nice if we are able to allocate 1/8 of
877 	 * total clusters for MFT but not more then 512 MB.
878 	 */
879 	sbi->zone_max = min_t(CLST, 0x20000000 >> sbi->cluster_bits, clusters >> 3);
880 
881 	err = 0;
882 
883 out:
884 	brelse(bh);
885 
886 	return err;
887 }
888 
889 /*
890  * ntfs_fill_super - Try to mount.
891  */
892 static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
893 {
894 	int err;
895 	struct ntfs_sb_info *sbi = sb->s_fs_info;
896 	struct block_device *bdev = sb->s_bdev;
897 	struct inode *inode;
898 	struct ntfs_inode *ni;
899 	size_t i, tt;
900 	CLST vcn, lcn, len;
901 	struct ATTRIB *attr;
902 	const struct VOLUME_INFO *info;
903 	u32 idx, done, bytes;
904 	struct ATTR_DEF_ENTRY *t;
905 	u16 *shared;
906 	struct MFT_REF ref;
907 
908 	ref.high = 0;
909 
910 	sbi->sb = sb;
911 	sbi->options = fc->fs_private;
912 	fc->fs_private = NULL;
913 	sb->s_flags |= SB_NODIRATIME;
914 	sb->s_magic = 0x7366746e; // "ntfs"
915 	sb->s_op = &ntfs_sops;
916 	sb->s_export_op = &ntfs_export_ops;
917 	sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec
918 	sb->s_xattr = ntfs_xattr_handlers;
919 
920 	sbi->options->nls = ntfs_load_nls(sbi->options->nls_name);
921 	if (IS_ERR(sbi->options->nls)) {
922 		sbi->options->nls = NULL;
923 		errorf(fc, "Cannot load nls %s", sbi->options->nls_name);
924 		err = -EINVAL;
925 		goto out;
926 	}
927 
928 	if (bdev_max_discard_sectors(bdev) && bdev_discard_granularity(bdev)) {
929 		sbi->discard_granularity = bdev_discard_granularity(bdev);
930 		sbi->discard_granularity_mask_inv =
931 			~(u64)(sbi->discard_granularity - 1);
932 	}
933 
934 	/* Parse boot. */
935 	err = ntfs_init_from_boot(sb, bdev_logical_block_size(bdev),
936 				  bdev_nr_bytes(bdev));
937 	if (err)
938 		goto out;
939 
940 	/*
941 	 * Load $Volume. This should be done before $LogFile
942 	 * 'cause 'sbi->volume.ni' is used 'ntfs_set_state'.
943 	 */
944 	ref.low = cpu_to_le32(MFT_REC_VOL);
945 	ref.seq = cpu_to_le16(MFT_REC_VOL);
946 	inode = ntfs_iget5(sb, &ref, &NAME_VOLUME);
947 	if (IS_ERR(inode)) {
948 		ntfs_err(sb, "Failed to load $Volume.");
949 		err = PTR_ERR(inode);
950 		goto out;
951 	}
952 
953 	ni = ntfs_i(inode);
954 
955 	/* Load and save label (not necessary). */
956 	attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL);
957 
958 	if (!attr) {
959 		/* It is ok if no ATTR_LABEL */
960 	} else if (!attr->non_res && !is_attr_ext(attr)) {
961 		/* $AttrDef allows labels to be up to 128 symbols. */
962 		err = utf16s_to_utf8s(resident_data(attr),
963 				      le32_to_cpu(attr->res.data_size) >> 1,
964 				      UTF16_LITTLE_ENDIAN, sbi->volume.label,
965 				      sizeof(sbi->volume.label));
966 		if (err < 0)
967 			sbi->volume.label[0] = 0;
968 	} else {
969 		/* Should we break mounting here? */
970 		//err = -EINVAL;
971 		//goto put_inode_out;
972 	}
973 
974 	attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL);
975 	if (!attr || is_attr_ext(attr)) {
976 		err = -EINVAL;
977 		goto put_inode_out;
978 	}
979 
980 	info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO);
981 	if (!info) {
982 		err = -EINVAL;
983 		goto put_inode_out;
984 	}
985 
986 	sbi->volume.major_ver = info->major_ver;
987 	sbi->volume.minor_ver = info->minor_ver;
988 	sbi->volume.flags = info->flags;
989 	sbi->volume.ni = ni;
990 
991 	/* Load $MFTMirr to estimate recs_mirr. */
992 	ref.low = cpu_to_le32(MFT_REC_MIRR);
993 	ref.seq = cpu_to_le16(MFT_REC_MIRR);
994 	inode = ntfs_iget5(sb, &ref, &NAME_MIRROR);
995 	if (IS_ERR(inode)) {
996 		ntfs_err(sb, "Failed to load $MFTMirr.");
997 		err = PTR_ERR(inode);
998 		goto out;
999 	}
1000 
1001 	sbi->mft.recs_mirr =
1002 		ntfs_up_cluster(sbi, inode->i_size) >> sbi->record_bits;
1003 
1004 	iput(inode);
1005 
1006 	/* Load LogFile to replay. */
1007 	ref.low = cpu_to_le32(MFT_REC_LOG);
1008 	ref.seq = cpu_to_le16(MFT_REC_LOG);
1009 	inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE);
1010 	if (IS_ERR(inode)) {
1011 		ntfs_err(sb, "Failed to load \x24LogFile.");
1012 		err = PTR_ERR(inode);
1013 		goto out;
1014 	}
1015 
1016 	ni = ntfs_i(inode);
1017 
1018 	err = ntfs_loadlog_and_replay(ni, sbi);
1019 	if (err)
1020 		goto put_inode_out;
1021 
1022 	iput(inode);
1023 
1024 	if (sbi->flags & NTFS_FLAGS_NEED_REPLAY) {
1025 		if (!sb_rdonly(sb)) {
1026 			ntfs_warn(sb,
1027 				  "failed to replay log file. Can't mount rw!");
1028 			err = -EINVAL;
1029 			goto out;
1030 		}
1031 	} else if (sbi->volume.flags & VOLUME_FLAG_DIRTY) {
1032 		if (!sb_rdonly(sb) && !sbi->options->force) {
1033 			ntfs_warn(
1034 				sb,
1035 				"volume is dirty and \"force\" flag is not set!");
1036 			err = -EINVAL;
1037 			goto out;
1038 		}
1039 	}
1040 
1041 	/* Load $MFT. */
1042 	ref.low = cpu_to_le32(MFT_REC_MFT);
1043 	ref.seq = cpu_to_le16(1);
1044 
1045 	inode = ntfs_iget5(sb, &ref, &NAME_MFT);
1046 	if (IS_ERR(inode)) {
1047 		ntfs_err(sb, "Failed to load $MFT.");
1048 		err = PTR_ERR(inode);
1049 		goto out;
1050 	}
1051 
1052 	ni = ntfs_i(inode);
1053 
1054 	sbi->mft.used = ni->i_valid >> sbi->record_bits;
1055 	tt = inode->i_size >> sbi->record_bits;
1056 	sbi->mft.next_free = MFT_REC_USER;
1057 
1058 	err = wnd_init(&sbi->mft.bitmap, sb, tt);
1059 	if (err)
1060 		goto put_inode_out;
1061 
1062 	err = ni_load_all_mi(ni);
1063 	if (err)
1064 		goto put_inode_out;
1065 
1066 	sbi->mft.ni = ni;
1067 
1068 	/* Load $BadClus. */
1069 	ref.low = cpu_to_le32(MFT_REC_BADCLUST);
1070 	ref.seq = cpu_to_le16(MFT_REC_BADCLUST);
1071 	inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS);
1072 	if (IS_ERR(inode)) {
1073 		ntfs_err(sb, "Failed to load $BadClus.");
1074 		err = PTR_ERR(inode);
1075 		goto out;
1076 	}
1077 
1078 	ni = ntfs_i(inode);
1079 
1080 	for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) {
1081 		if (lcn == SPARSE_LCN)
1082 			continue;
1083 
1084 		if (!sbi->bad_clusters)
1085 			ntfs_notice(sb, "Volume contains bad blocks");
1086 
1087 		sbi->bad_clusters += len;
1088 	}
1089 
1090 	iput(inode);
1091 
1092 	/* Load $Bitmap. */
1093 	ref.low = cpu_to_le32(MFT_REC_BITMAP);
1094 	ref.seq = cpu_to_le16(MFT_REC_BITMAP);
1095 	inode = ntfs_iget5(sb, &ref, &NAME_BITMAP);
1096 	if (IS_ERR(inode)) {
1097 		ntfs_err(sb, "Failed to load $Bitmap.");
1098 		err = PTR_ERR(inode);
1099 		goto out;
1100 	}
1101 
1102 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
1103 	if (inode->i_size >> 32) {
1104 		err = -EINVAL;
1105 		goto put_inode_out;
1106 	}
1107 #endif
1108 
1109 	/* Check bitmap boundary. */
1110 	tt = sbi->used.bitmap.nbits;
1111 	if (inode->i_size < bitmap_size(tt)) {
1112 		err = -EINVAL;
1113 		goto put_inode_out;
1114 	}
1115 
1116 	/* Not necessary. */
1117 	sbi->used.bitmap.set_tail = true;
1118 	err = wnd_init(&sbi->used.bitmap, sb, tt);
1119 	if (err)
1120 		goto put_inode_out;
1121 
1122 	iput(inode);
1123 
1124 	/* Compute the MFT zone. */
1125 	err = ntfs_refresh_zone(sbi);
1126 	if (err)
1127 		goto out;
1128 
1129 	/* Load $AttrDef. */
1130 	ref.low = cpu_to_le32(MFT_REC_ATTR);
1131 	ref.seq = cpu_to_le16(MFT_REC_ATTR);
1132 	inode = ntfs_iget5(sb, &ref, &NAME_ATTRDEF);
1133 	if (IS_ERR(inode)) {
1134 		ntfs_err(sb, "Failed to load $AttrDef -> %d", err);
1135 		err = PTR_ERR(inode);
1136 		goto out;
1137 	}
1138 
1139 	if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY)) {
1140 		err = -EINVAL;
1141 		goto put_inode_out;
1142 	}
1143 	bytes = inode->i_size;
1144 	sbi->def_table = t = kmalloc(bytes, GFP_NOFS);
1145 	if (!t) {
1146 		err = -ENOMEM;
1147 		goto put_inode_out;
1148 	}
1149 
1150 	for (done = idx = 0; done < bytes; done += PAGE_SIZE, idx++) {
1151 		unsigned long tail = bytes - done;
1152 		struct page *page = ntfs_map_page(inode->i_mapping, idx);
1153 
1154 		if (IS_ERR(page)) {
1155 			err = PTR_ERR(page);
1156 			goto put_inode_out;
1157 		}
1158 		memcpy(Add2Ptr(t, done), page_address(page),
1159 		       min(PAGE_SIZE, tail));
1160 		ntfs_unmap_page(page);
1161 
1162 		if (!idx && ATTR_STD != t->type) {
1163 			err = -EINVAL;
1164 			goto put_inode_out;
1165 		}
1166 	}
1167 
1168 	t += 1;
1169 	sbi->def_entries = 1;
1170 	done = sizeof(struct ATTR_DEF_ENTRY);
1171 	sbi->reparse.max_size = MAXIMUM_REPARSE_DATA_BUFFER_SIZE;
1172 	sbi->ea_max_size = 0x10000; /* default formatter value */
1173 
1174 	while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) {
1175 		u32 t32 = le32_to_cpu(t->type);
1176 		u64 sz = le64_to_cpu(t->max_sz);
1177 
1178 		if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32)
1179 			break;
1180 
1181 		if (t->type == ATTR_REPARSE)
1182 			sbi->reparse.max_size = sz;
1183 		else if (t->type == ATTR_EA)
1184 			sbi->ea_max_size = sz;
1185 
1186 		done += sizeof(struct ATTR_DEF_ENTRY);
1187 		t += 1;
1188 		sbi->def_entries += 1;
1189 	}
1190 	iput(inode);
1191 
1192 	/* Load $UpCase. */
1193 	ref.low = cpu_to_le32(MFT_REC_UPCASE);
1194 	ref.seq = cpu_to_le16(MFT_REC_UPCASE);
1195 	inode = ntfs_iget5(sb, &ref, &NAME_UPCASE);
1196 	if (IS_ERR(inode)) {
1197 		ntfs_err(sb, "Failed to load $UpCase.");
1198 		err = PTR_ERR(inode);
1199 		goto out;
1200 	}
1201 
1202 	if (inode->i_size != 0x10000 * sizeof(short)) {
1203 		err = -EINVAL;
1204 		goto put_inode_out;
1205 	}
1206 
1207 	for (idx = 0; idx < (0x10000 * sizeof(short) >> PAGE_SHIFT); idx++) {
1208 		const __le16 *src;
1209 		u16 *dst = Add2Ptr(sbi->upcase, idx << PAGE_SHIFT);
1210 		struct page *page = ntfs_map_page(inode->i_mapping, idx);
1211 
1212 		if (IS_ERR(page)) {
1213 			err = PTR_ERR(page);
1214 			goto put_inode_out;
1215 		}
1216 
1217 		src = page_address(page);
1218 
1219 #ifdef __BIG_ENDIAN
1220 		for (i = 0; i < PAGE_SIZE / sizeof(u16); i++)
1221 			*dst++ = le16_to_cpu(*src++);
1222 #else
1223 		memcpy(dst, src, PAGE_SIZE);
1224 #endif
1225 		ntfs_unmap_page(page);
1226 	}
1227 
1228 	shared = ntfs_set_shared(sbi->upcase, 0x10000 * sizeof(short));
1229 	if (shared && sbi->upcase != shared) {
1230 		kvfree(sbi->upcase);
1231 		sbi->upcase = shared;
1232 	}
1233 
1234 	iput(inode);
1235 
1236 	if (is_ntfs3(sbi)) {
1237 		/* Load $Secure. */
1238 		err = ntfs_security_init(sbi);
1239 		if (err)
1240 			goto out;
1241 
1242 		/* Load $Extend. */
1243 		err = ntfs_extend_init(sbi);
1244 		if (err)
1245 			goto load_root;
1246 
1247 		/* Load $Extend\$Reparse. */
1248 		err = ntfs_reparse_init(sbi);
1249 		if (err)
1250 			goto load_root;
1251 
1252 		/* Load $Extend\$ObjId. */
1253 		err = ntfs_objid_init(sbi);
1254 		if (err)
1255 			goto load_root;
1256 	}
1257 
1258 load_root:
1259 	/* Load root. */
1260 	ref.low = cpu_to_le32(MFT_REC_ROOT);
1261 	ref.seq = cpu_to_le16(MFT_REC_ROOT);
1262 	inode = ntfs_iget5(sb, &ref, &NAME_ROOT);
1263 	if (IS_ERR(inode)) {
1264 		ntfs_err(sb, "Failed to load root.");
1265 		err = PTR_ERR(inode);
1266 		goto out;
1267 	}
1268 
1269 	sb->s_root = d_make_root(inode);
1270 	if (!sb->s_root) {
1271 		err = -ENOMEM;
1272 		goto put_inode_out;
1273 	}
1274 
1275 	return 0;
1276 
1277 put_inode_out:
1278 	iput(inode);
1279 out:
1280 	/*
1281 	 * Free resources here.
1282 	 * ntfs_fs_free will be called with fc->s_fs_info = NULL
1283 	 */
1284 	put_ntfs(sbi);
1285 	sb->s_fs_info = NULL;
1286 
1287 	return err;
1288 }
1289 
1290 void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len)
1291 {
1292 	struct ntfs_sb_info *sbi = sb->s_fs_info;
1293 	struct block_device *bdev = sb->s_bdev;
1294 	sector_t devblock = (u64)lcn * sbi->blocks_per_cluster;
1295 	unsigned long blocks = (u64)len * sbi->blocks_per_cluster;
1296 	unsigned long cnt = 0;
1297 	unsigned long limit = global_zone_page_state(NR_FREE_PAGES)
1298 			      << (PAGE_SHIFT - sb->s_blocksize_bits);
1299 
1300 	if (limit >= 0x2000)
1301 		limit -= 0x1000;
1302 	else if (limit < 32)
1303 		limit = 32;
1304 	else
1305 		limit >>= 1;
1306 
1307 	while (blocks--) {
1308 		clean_bdev_aliases(bdev, devblock++, 1);
1309 		if (cnt++ >= limit) {
1310 			sync_blockdev(bdev);
1311 			cnt = 0;
1312 		}
1313 	}
1314 }
1315 
1316 /*
1317  * ntfs_discard - Issue a discard request (trim for SSD).
1318  */
1319 int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len)
1320 {
1321 	int err;
1322 	u64 lbo, bytes, start, end;
1323 	struct super_block *sb;
1324 
1325 	if (sbi->used.next_free_lcn == lcn + len)
1326 		sbi->used.next_free_lcn = lcn;
1327 
1328 	if (sbi->flags & NTFS_FLAGS_NODISCARD)
1329 		return -EOPNOTSUPP;
1330 
1331 	if (!sbi->options->discard)
1332 		return -EOPNOTSUPP;
1333 
1334 	lbo = (u64)lcn << sbi->cluster_bits;
1335 	bytes = (u64)len << sbi->cluster_bits;
1336 
1337 	/* Align up 'start' on discard_granularity. */
1338 	start = (lbo + sbi->discard_granularity - 1) &
1339 		sbi->discard_granularity_mask_inv;
1340 	/* Align down 'end' on discard_granularity. */
1341 	end = (lbo + bytes) & sbi->discard_granularity_mask_inv;
1342 
1343 	sb = sbi->sb;
1344 	if (start >= end)
1345 		return 0;
1346 
1347 	err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9,
1348 				   GFP_NOFS);
1349 
1350 	if (err == -EOPNOTSUPP)
1351 		sbi->flags |= NTFS_FLAGS_NODISCARD;
1352 
1353 	return err;
1354 }
1355 
1356 static int ntfs_fs_get_tree(struct fs_context *fc)
1357 {
1358 	return get_tree_bdev(fc, ntfs_fill_super);
1359 }
1360 
1361 /*
1362  * ntfs_fs_free - Free fs_context.
1363  *
1364  * Note that this will be called after fill_super and reconfigure
1365  * even when they pass. So they have to take pointers if they pass.
1366  */
1367 static void ntfs_fs_free(struct fs_context *fc)
1368 {
1369 	struct ntfs_mount_options *opts = fc->fs_private;
1370 	struct ntfs_sb_info *sbi = fc->s_fs_info;
1371 
1372 	if (sbi)
1373 		put_ntfs(sbi);
1374 
1375 	if (opts)
1376 		put_mount_options(opts);
1377 }
1378 
1379 static const struct fs_context_operations ntfs_context_ops = {
1380 	.parse_param	= ntfs_fs_parse_param,
1381 	.get_tree	= ntfs_fs_get_tree,
1382 	.reconfigure	= ntfs_fs_reconfigure,
1383 	.free		= ntfs_fs_free,
1384 };
1385 
1386 /*
1387  * ntfs_init_fs_context - Initialize spi and opts
1388  *
1389  * This will called when mount/remount. We will first initialize
1390  * options so that if remount we can use just that.
1391  */
1392 static int ntfs_init_fs_context(struct fs_context *fc)
1393 {
1394 	struct ntfs_mount_options *opts;
1395 	struct ntfs_sb_info *sbi;
1396 
1397 	opts = kzalloc(sizeof(struct ntfs_mount_options), GFP_NOFS);
1398 	if (!opts)
1399 		return -ENOMEM;
1400 
1401 	/* Default options. */
1402 	opts->fs_uid = current_uid();
1403 	opts->fs_gid = current_gid();
1404 	opts->fs_fmask_inv = ~current_umask();
1405 	opts->fs_dmask_inv = ~current_umask();
1406 
1407 	if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
1408 		goto ok;
1409 
1410 	sbi = kzalloc(sizeof(struct ntfs_sb_info), GFP_NOFS);
1411 	if (!sbi)
1412 		goto free_opts;
1413 
1414 	sbi->upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL);
1415 	if (!sbi->upcase)
1416 		goto free_sbi;
1417 
1418 	ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1419 			     DEFAULT_RATELIMIT_BURST);
1420 
1421 	mutex_init(&sbi->compress.mtx_lznt);
1422 #ifdef CONFIG_NTFS3_LZX_XPRESS
1423 	mutex_init(&sbi->compress.mtx_xpress);
1424 	mutex_init(&sbi->compress.mtx_lzx);
1425 #endif
1426 
1427 	fc->s_fs_info = sbi;
1428 ok:
1429 	fc->fs_private = opts;
1430 	fc->ops = &ntfs_context_ops;
1431 
1432 	return 0;
1433 free_sbi:
1434 	kfree(sbi);
1435 free_opts:
1436 	kfree(opts);
1437 	return -ENOMEM;
1438 }
1439 
1440 // clang-format off
1441 static struct file_system_type ntfs_fs_type = {
1442 	.owner			= THIS_MODULE,
1443 	.name			= "ntfs3",
1444 	.init_fs_context	= ntfs_init_fs_context,
1445 	.parameters		= ntfs_fs_parameters,
1446 	.kill_sb		= kill_block_super,
1447 	.fs_flags		= FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
1448 };
1449 // clang-format on
1450 
1451 static int __init init_ntfs_fs(void)
1452 {
1453 	int err;
1454 
1455 	pr_info("ntfs3: Max link count %u\n", NTFS_LINK_MAX);
1456 
1457 	if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL))
1458 		pr_info("ntfs3: Enabled Linux POSIX ACLs support\n");
1459 	if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER))
1460 		pr_notice("ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n");
1461 	if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS))
1462 		pr_info("ntfs3: Read-only LZX/Xpress compression included\n");
1463 
1464 	err = ntfs3_init_bitmap();
1465 	if (err)
1466 		return err;
1467 
1468 	ntfs_inode_cachep = kmem_cache_create(
1469 		"ntfs_inode_cache", sizeof(struct ntfs_inode), 0,
1470 		(SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1471 		init_once);
1472 	if (!ntfs_inode_cachep) {
1473 		err = -ENOMEM;
1474 		goto out1;
1475 	}
1476 
1477 	err = register_filesystem(&ntfs_fs_type);
1478 	if (err)
1479 		goto out;
1480 
1481 	return 0;
1482 out:
1483 	kmem_cache_destroy(ntfs_inode_cachep);
1484 out1:
1485 	ntfs3_exit_bitmap();
1486 	return err;
1487 }
1488 
1489 static void __exit exit_ntfs_fs(void)
1490 {
1491 	if (ntfs_inode_cachep) {
1492 		rcu_barrier();
1493 		kmem_cache_destroy(ntfs_inode_cachep);
1494 	}
1495 
1496 	unregister_filesystem(&ntfs_fs_type);
1497 	ntfs3_exit_bitmap();
1498 }
1499 
1500 MODULE_LICENSE("GPL");
1501 MODULE_DESCRIPTION("ntfs3 read/write filesystem");
1502 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1503 MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support");
1504 #endif
1505 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
1506 MODULE_INFO(cluster, "Warning: Activated 64 bits per cluster. Windows does not support this");
1507 #endif
1508 #ifdef CONFIG_NTFS3_LZX_XPRESS
1509 MODULE_INFO(compression, "Read-only lzx/xpress compression included");
1510 #endif
1511 
1512 MODULE_AUTHOR("Konstantin Komarov");
1513 MODULE_ALIAS_FS("ntfs3");
1514 
1515 module_init(init_ntfs_fs);
1516 module_exit(exit_ntfs_fs);
1517