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