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