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