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