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