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