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