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