Merge tag 'mm-hotfixes-stable-2023-05-03-16-27' of git://git.kernel.org/pub/scm/linux...
[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>
82cae269
KK
60#include <linux/seq_file.h>
61#include <linux/statfs.h>
62
63#include "debug.h"
64#include "ntfs.h"
65#include "ntfs_fs.h"
66#ifdef CONFIG_NTFS3_LZX_XPRESS
67#include "lib/lib.h"
68#endif
69
70#ifdef CONFIG_PRINTK
71/*
e8b8e97f
KA
72 * ntfs_printk - Trace warnings/notices/errors.
73 *
82cae269
KK
74 * Thanks Joe Perches <joe@perches.com> for implementation
75 */
76void ntfs_printk(const struct super_block *sb, const char *fmt, ...)
77{
78 struct va_format vaf;
79 va_list args;
80 int level;
81 struct ntfs_sb_info *sbi = sb->s_fs_info;
82
e8b8e97f 83 /* Should we use different ratelimits for warnings/notices/errors? */
82cae269
KK
84 if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
85 return;
86
87 va_start(args, fmt);
88
89 level = printk_get_level(fmt);
90 vaf.fmt = printk_skip_level(fmt);
91 vaf.va = &args;
92 printk("%c%cntfs3: %s: %pV\n", KERN_SOH_ASCII, level, sb->s_id, &vaf);
93
94 va_end(args);
95}
96
97static char s_name_buf[512];
e8b8e97f 98static atomic_t s_name_buf_cnt = ATOMIC_INIT(1); // 1 means 'free s_name_buf'.
82cae269 99
e8b8e97f
KA
100/*
101 * ntfs_inode_printk
102 *
103 * Print warnings/notices/errors about inode using name or inode number.
104 */
82cae269
KK
105void ntfs_inode_printk(struct inode *inode, const char *fmt, ...)
106{
107 struct super_block *sb = inode->i_sb;
108 struct ntfs_sb_info *sbi = sb->s_fs_info;
109 char *name;
110 va_list args;
111 struct va_format vaf;
112 int level;
113
114 if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
115 return;
116
e8b8e97f 117 /* Use static allocated buffer, if possible. */
96de65a9
KK
118 name = atomic_dec_and_test(&s_name_buf_cnt) ?
119 s_name_buf :
120 kmalloc(sizeof(s_name_buf), GFP_NOFS);
82cae269
KK
121
122 if (name) {
123 struct dentry *de = d_find_alias(inode);
124 const u32 name_len = ARRAY_SIZE(s_name_buf) - 1;
125
126 if (de) {
127 spin_lock(&de->d_lock);
128 snprintf(name, name_len, " \"%s\"", de->d_name.name);
129 spin_unlock(&de->d_lock);
e8b8e97f 130 name[name_len] = 0; /* To be sure. */
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
610f8f5a
KA
260static const struct fs_parameter_spec ntfs_fs_parameters[] = {
261 fsparam_u32("uid", Opt_uid),
262 fsparam_u32("gid", Opt_gid),
263 fsparam_u32oct("umask", Opt_umask),
264 fsparam_u32oct("dmask", Opt_dmask),
265 fsparam_u32oct("fmask", Opt_fmask),
266 fsparam_flag_no("sys_immutable", Opt_immutable),
267 fsparam_flag_no("discard", Opt_discard),
268 fsparam_flag_no("force", Opt_force),
269 fsparam_flag_no("sparse", Opt_sparse),
9d1939f4 270 fsparam_flag_no("hidden", Opt_nohidden),
dc0fcc99 271 fsparam_flag_no("hide_dot_files", Opt_hide_dot_files),
1d07a9df 272 fsparam_flag_no("windows_names", Opt_windows_names),
610f8f5a 273 fsparam_flag_no("showmeta", Opt_showmeta),
16b3dbfb
KK
274 fsparam_flag_no("acl", Opt_acl),
275 fsparam_string("iocharset", Opt_iocharset),
610f8f5a 276 fsparam_flag_no("prealloc", Opt_prealloc),
a3a956c7 277 fsparam_flag_no("nocase", Opt_nocase),
610f8f5a 278 {}
82cae269
KK
279};
280
610f8f5a
KA
281/*
282 * Load nls table or if @nls is utf8 then return NULL.
283 */
284static struct nls_table *ntfs_load_nls(char *nls)
82cae269 285{
610f8f5a 286 struct nls_table *ret;
82cae269 287
610f8f5a
KA
288 if (!nls)
289 nls = CONFIG_NLS_DEFAULT;
82cae269 290
610f8f5a
KA
291 if (strcmp(nls, "utf8") == 0)
292 return NULL;
82cae269 293
610f8f5a
KA
294 if (strcmp(nls, CONFIG_NLS_DEFAULT) == 0)
295 return load_nls_default();
82cae269 296
610f8f5a
KA
297 ret = load_nls(nls);
298 if (ret)
299 return ret;
82cae269 300
610f8f5a
KA
301 return ERR_PTR(-EINVAL);
302}
303
304static int ntfs_fs_parse_param(struct fs_context *fc,
305 struct fs_parameter *param)
306{
307 struct ntfs_mount_options *opts = fc->fs_private;
308 struct fs_parse_result result;
309 int opt;
310
311 opt = fs_parse(fc, ntfs_fs_parameters, param, &result);
312 if (opt < 0)
313 return opt;
314
315 switch (opt) {
316 case Opt_uid:
317 opts->fs_uid = make_kuid(current_user_ns(), result.uint_32);
318 if (!uid_valid(opts->fs_uid))
319 return invalf(fc, "ntfs3: Invalid value for uid.");
610f8f5a
KA
320 break;
321 case Opt_gid:
322 opts->fs_gid = make_kgid(current_user_ns(), result.uint_32);
323 if (!gid_valid(opts->fs_gid))
324 return invalf(fc, "ntfs3: Invalid value for gid.");
610f8f5a
KA
325 break;
326 case Opt_umask:
327 if (result.uint_32 & ~07777)
328 return invalf(fc, "ntfs3: Invalid value for umask.");
329 opts->fs_fmask_inv = ~result.uint_32;
330 opts->fs_dmask_inv = ~result.uint_32;
331 opts->fmask = 1;
332 opts->dmask = 1;
333 break;
334 case Opt_dmask:
335 if (result.uint_32 & ~07777)
336 return invalf(fc, "ntfs3: Invalid value for dmask.");
337 opts->fs_dmask_inv = ~result.uint_32;
338 opts->dmask = 1;
339 break;
340 case Opt_fmask:
341 if (result.uint_32 & ~07777)
342 return invalf(fc, "ntfs3: Invalid value for fmask.");
343 opts->fs_fmask_inv = ~result.uint_32;
344 opts->fmask = 1;
345 break;
346 case Opt_immutable:
347 opts->sys_immutable = result.negated ? 0 : 1;
348 break;
349 case Opt_discard:
350 opts->discard = result.negated ? 0 : 1;
351 break;
352 case Opt_force:
353 opts->force = result.negated ? 0 : 1;
354 break;
355 case Opt_sparse:
356 opts->sparse = result.negated ? 0 : 1;
357 break;
358 case Opt_nohidden:
9d1939f4 359 opts->nohidden = result.negated ? 1 : 0;
610f8f5a 360 break;
098250db 361 case Opt_hide_dot_files:
4c9ba192 362 opts->hide_dot_files = result.negated ? 0 : 1;
098250db 363 break;
1d07a9df
DP
364 case Opt_windows_names:
365 opts->windows_names = result.negated ? 0 : 1;
366 break;
16b3dbfb
KK
367 case Opt_showmeta:
368 opts->showmeta = result.negated ? 0 : 1;
369 break;
610f8f5a
KA
370 case Opt_acl:
371 if (!result.negated)
82cae269 372#ifdef CONFIG_NTFS3_FS_POSIX_ACL
610f8f5a 373 fc->sb_flags |= SB_POSIXACL;
82cae269 374#else
96de65a9
KK
375 return invalf(
376 fc, "ntfs3: Support for ACL not compiled in!");
82cae269 377#endif
610f8f5a
KA
378 else
379 fc->sb_flags &= ~SB_POSIXACL;
380 break;
e274cde8 381 case Opt_iocharset:
610f8f5a
KA
382 kfree(opts->nls_name);
383 opts->nls_name = param->string;
384 param->string = NULL;
385 break;
386 case Opt_prealloc:
387 opts->prealloc = result.negated ? 0 : 1;
388 break;
a3a956c7
KK
389 case Opt_nocase:
390 opts->nocase = result.negated ? 1 : 0;
391 break;
610f8f5a
KA
392 default:
393 /* Should not be here unless we forget add case. */
394 return -EINVAL;
82cae269 395 }
82cae269
KK
396 return 0;
397}
398
610f8f5a 399static int ntfs_fs_reconfigure(struct fs_context *fc)
82cae269 400{
610f8f5a 401 struct super_block *sb = fc->root->d_sb;
82cae269 402 struct ntfs_sb_info *sbi = sb->s_fs_info;
610f8f5a
KA
403 struct ntfs_mount_options *new_opts = fc->fs_private;
404 int ro_rw;
82cae269 405
610f8f5a 406 ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY);
82cae269 407 if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) {
96de65a9
KK
408 errorf(fc,
409 "ntfs3: Couldn't remount rw because journal is not replayed. Please umount/remount instead\n");
610f8f5a
KA
410 return -EINVAL;
411 }
412
413 new_opts->nls = ntfs_load_nls(new_opts->nls_name);
414 if (IS_ERR(new_opts->nls)) {
415 new_opts->nls = NULL;
96de65a9
KK
416 errorf(fc, "ntfs3: Cannot load iocharset %s",
417 new_opts->nls_name);
610f8f5a 418 return -EINVAL;
82cae269 419 }
610f8f5a 420 if (new_opts->nls != sbi->options->nls)
96de65a9
KK
421 return invalf(
422 fc,
423 "ntfs3: Cannot use different iocharset when remounting!");
82cae269
KK
424
425 sync_filesystem(sb);
426
427 if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) &&
610f8f5a 428 !new_opts->force) {
96de65a9
KK
429 errorf(fc,
430 "ntfs3: Volume is dirty and \"force\" flag is not set!");
610f8f5a 431 return -EINVAL;
82cae269
KK
432 }
433
cd39981f 434 swap(sbi->options, fc->fs_private);
82cae269 435
610f8f5a 436 return 0;
82cae269
KK
437}
438
439static struct kmem_cache *ntfs_inode_cachep;
440
441static struct inode *ntfs_alloc_inode(struct super_block *sb)
442{
fd60b288 443 struct ntfs_inode *ni = alloc_inode_sb(sb, ntfs_inode_cachep, GFP_NOFS);
82cae269
KK
444
445 if (!ni)
446 return NULL;
447
448 memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode));
82cae269 449 mutex_init(&ni->ni_lock);
82cae269
KK
450 return &ni->vfs_inode;
451}
452
ae6b47b5 453static void ntfs_free_inode(struct inode *inode)
82cae269 454{
82cae269
KK
455 struct ntfs_inode *ni = ntfs_i(inode);
456
457 mutex_destroy(&ni->ni_lock);
82cae269
KK
458 kmem_cache_free(ntfs_inode_cachep, ni);
459}
460
82cae269
KK
461static void init_once(void *foo)
462{
463 struct ntfs_inode *ni = foo;
464
465 inode_init_once(&ni->vfs_inode);
466}
467
e8b8e97f
KA
468/*
469 * put_ntfs - Noinline to reduce binary size.
470 */
82cae269
KK
471static noinline void put_ntfs(struct ntfs_sb_info *sbi)
472{
195c52bd
KA
473 kfree(sbi->new_rec);
474 kvfree(ntfs_put_shared(sbi->upcase));
475 kfree(sbi->def_table);
82cae269
KK
476
477 wnd_close(&sbi->mft.bitmap);
478 wnd_close(&sbi->used.bitmap);
479
480 if (sbi->mft.ni)
481 iput(&sbi->mft.ni->vfs_inode);
482
483 if (sbi->security.ni)
484 iput(&sbi->security.ni->vfs_inode);
485
486 if (sbi->reparse.ni)
487 iput(&sbi->reparse.ni->vfs_inode);
488
489 if (sbi->objid.ni)
490 iput(&sbi->objid.ni->vfs_inode);
491
492 if (sbi->volume.ni)
493 iput(&sbi->volume.ni->vfs_inode);
494
495 ntfs_update_mftmirr(sbi, 0);
496
497 indx_clear(&sbi->security.index_sii);
498 indx_clear(&sbi->security.index_sdh);
499 indx_clear(&sbi->reparse.index_r);
500 indx_clear(&sbi->objid.index_o);
195c52bd 501 kfree(sbi->compress.lznt);
82cae269
KK
502#ifdef CONFIG_NTFS3_LZX_XPRESS
503 xpress_free_decompressor(sbi->compress.xpress);
504 lzx_free_decompressor(sbi->compress.lzx);
505#endif
195c52bd 506 kfree(sbi);
82cae269
KK
507}
508
509static void ntfs_put_super(struct super_block *sb)
510{
511 struct ntfs_sb_info *sbi = sb->s_fs_info;
512
e8b8e97f 513 /* Mark rw ntfs as clear, if possible. */
82cae269
KK
514 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
515
610f8f5a 516 put_mount_options(sbi->options);
82cae269 517 put_ntfs(sbi);
610f8f5a 518 sb->s_fs_info = NULL;
82cae269
KK
519
520 sync_blockdev(sb->s_bdev);
521}
522
523static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf)
524{
525 struct super_block *sb = dentry->d_sb;
526 struct ntfs_sb_info *sbi = sb->s_fs_info;
527 struct wnd_bitmap *wnd = &sbi->used.bitmap;
528
529 buf->f_type = sb->s_magic;
530 buf->f_bsize = sbi->cluster_size;
531 buf->f_blocks = wnd->nbits;
532
533 buf->f_bfree = buf->f_bavail = wnd_zeroes(wnd);
534 buf->f_fsid.val[0] = sbi->volume.ser_num;
535 buf->f_fsid.val[1] = (sbi->volume.ser_num >> 32);
536 buf->f_namelen = NTFS_NAME_LEN;
537
538 return 0;
539}
540
541static int ntfs_show_options(struct seq_file *m, struct dentry *root)
542{
543 struct super_block *sb = root->d_sb;
544 struct ntfs_sb_info *sbi = sb->s_fs_info;
564c97bd 545 struct ntfs_mount_options *opts = sbi->options;
82cae269
KK
546 struct user_namespace *user_ns = seq_user_ns(m);
547
96de65a9
KK
548 seq_printf(m, ",uid=%u", from_kuid_munged(user_ns, opts->fs_uid));
549 seq_printf(m, ",gid=%u", from_kgid_munged(user_ns, opts->fs_gid));
82cae269 550 if (opts->dmask)
f27b92ec 551 seq_printf(m, ",dmask=%04o", opts->fs_dmask_inv ^ 0xffff);
16b3dbfb
KK
552 if (opts->fmask)
553 seq_printf(m, ",fmask=%04o", opts->fs_fmask_inv ^ 0xffff);
82cae269
KK
554 if (opts->sys_immutable)
555 seq_puts(m, ",sys_immutable");
556 if (opts->discard)
557 seq_puts(m, ",discard");
16b3dbfb
KK
558 if (opts->force)
559 seq_puts(m, ",force");
82cae269
KK
560 if (opts->sparse)
561 seq_puts(m, ",sparse");
82cae269
KK
562 if (opts->nohidden)
563 seq_puts(m, ",nohidden");
19b42450 564 if (opts->hide_dot_files)
dc0fcc99 565 seq_puts(m, ",hide_dot_files");
16b3dbfb
KK
566 if (opts->windows_names)
567 seq_puts(m, ",windows_names");
568 if (opts->showmeta)
569 seq_puts(m, ",showmeta");
82cae269
KK
570 if (sb->s_flags & SB_POSIXACL)
571 seq_puts(m, ",acl");
16b3dbfb
KK
572 if (opts->nls)
573 seq_printf(m, ",iocharset=%s", opts->nls->charset);
574 else
575 seq_puts(m, ",iocharset=utf8");
576 if (opts->prealloc)
577 seq_puts(m, ",prealloc");
578 if (opts->nocase)
579 seq_puts(m, ",nocase");
82cae269
KK
580
581 return 0;
582}
583
e8b8e97f
KA
584/*
585 * ntfs_sync_fs - super_operations::sync_fs
586 */
82cae269
KK
587static int ntfs_sync_fs(struct super_block *sb, int wait)
588{
589 int err = 0, err2;
590 struct ntfs_sb_info *sbi = sb->s_fs_info;
591 struct ntfs_inode *ni;
592 struct inode *inode;
593
594 ni = sbi->security.ni;
595 if (ni) {
596 inode = &ni->vfs_inode;
597 err2 = _ni_write_inode(inode, wait);
598 if (err2 && !err)
599 err = err2;
600 }
601
602 ni = sbi->objid.ni;
603 if (ni) {
604 inode = &ni->vfs_inode;
605 err2 = _ni_write_inode(inode, wait);
606 if (err2 && !err)
607 err = err2;
608 }
609
610 ni = sbi->reparse.ni;
611 if (ni) {
612 inode = &ni->vfs_inode;
613 err2 = _ni_write_inode(inode, wait);
614 if (err2 && !err)
615 err = err2;
616 }
617
618 if (!err)
619 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
620
621 ntfs_update_mftmirr(sbi, wait);
622
623 return err;
624}
625
626static const struct super_operations ntfs_sops = {
627 .alloc_inode = ntfs_alloc_inode,
ae6b47b5 628 .free_inode = ntfs_free_inode,
82cae269
KK
629 .evict_inode = ntfs_evict_inode,
630 .put_super = ntfs_put_super,
631 .statfs = ntfs_statfs,
632 .show_options = ntfs_show_options,
633 .sync_fs = ntfs_sync_fs,
82cae269
KK
634 .write_inode = ntfs3_write_inode,
635};
636
637static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino,
638 u32 generation)
639{
640 struct MFT_REF ref;
641 struct inode *inode;
642
643 ref.low = cpu_to_le32(ino);
644#ifdef CONFIG_NTFS3_64BIT_CLUSTER
645 ref.high = cpu_to_le16(ino >> 32);
646#else
647 ref.high = 0;
648#endif
649 ref.seq = cpu_to_le16(generation);
650
651 inode = ntfs_iget5(sb, &ref, NULL);
652 if (!IS_ERR(inode) && is_bad_inode(inode)) {
653 iput(inode);
654 inode = ERR_PTR(-ESTALE);
655 }
656
657 return inode;
658}
659
660static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
661 int fh_len, int fh_type)
662{
663 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
664 ntfs_export_get_inode);
665}
666
667static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
668 int fh_len, int fh_type)
669{
670 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
671 ntfs_export_get_inode);
672}
673
674/* TODO: == ntfs_sync_inode */
675static int ntfs_nfs_commit_metadata(struct inode *inode)
676{
677 return _ni_write_inode(inode, 1);
678}
679
680static const struct export_operations ntfs_export_ops = {
681 .fh_to_dentry = ntfs_fh_to_dentry,
682 .fh_to_parent = ntfs_fh_to_parent,
683 .get_parent = ntfs3_get_parent,
684 .commit_metadata = ntfs_nfs_commit_metadata,
685};
686
e8b8e97f
KA
687/*
688 * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb".
689 */
82cae269
KK
690static u32 format_size_gb(const u64 bytes, u32 *mb)
691{
e8b8e97f 692 /* Do simple right 30 bit shift of 64 bit value. */
82cae269
KK
693 u64 kbytes = bytes >> 10;
694 u32 kbytes32 = kbytes;
695
696 *mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20;
697 if (*mb >= 100)
698 *mb = 99;
699
700 return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12);
701}
702
703static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot)
704{
a3b77434
RD
705 if (boot->sectors_per_clusters <= 0x80)
706 return boot->sectors_per_clusters;
707 if (boot->sectors_per_clusters >= 0xf4) /* limit shift to 2MB max */
96de65a9 708 return 1U << (-(s8)boot->sectors_per_clusters);
a3b77434 709 return -EINVAL;
82cae269
KK
710}
711
e8b8e97f
KA
712/*
713 * ntfs_init_from_boot - Init internal info from on-disk boot sector.
714 */
82cae269
KK
715static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
716 u64 dev_size)
717{
718 struct ntfs_sb_info *sbi = sb->s_fs_info;
719 int err;
720 u32 mb, gb, boot_sector_size, sct_per_clst, record_size;
dbf59e2a 721 u64 sectors, clusters, mlcn, mlcn2;
82cae269
KK
722 struct NTFS_BOOT *boot;
723 struct buffer_head *bh;
724 struct MFT_REC *rec;
725 u16 fn, ao;
96de65a9 726 u8 cluster_bits;
82cae269
KK
727
728 sbi->volume.blocks = dev_size >> PAGE_SHIFT;
729
730 bh = ntfs_bread(sb, 0);
731 if (!bh)
732 return -EIO;
733
734 err = -EINVAL;
735 boot = (struct NTFS_BOOT *)bh->b_data;
736
e43f6ec2
KK
737 if (memcmp(boot->system_id, "NTFS ", sizeof("NTFS ") - 1)) {
738 ntfs_err(sb, "Boot's signature is not NTFS.");
82cae269 739 goto out;
e43f6ec2 740 }
82cae269
KK
741
742 /* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/
743 /*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1])
744 * goto out;
745 */
746
e43f6ec2
KK
747 boot_sector_size = ((u32)boot->bytes_per_sector[1] << 8) |
748 boot->bytes_per_sector[0];
749 if (boot_sector_size < SECTOR_SIZE ||
528c9b3d 750 !is_power_of_2(boot_sector_size)) {
e43f6ec2 751 ntfs_err(sb, "Invalid bytes per sector %u.", boot_sector_size);
82cae269
KK
752 goto out;
753 }
754
755 /* cluster size: 512, 1K, 2K, 4K, ... 2M */
756 sct_per_clst = true_sectors_per_clst(boot);
e43f6ec2
KK
757 if ((int)sct_per_clst < 0 || !is_power_of_2(sct_per_clst)) {
758 ntfs_err(sb, "Invalid sectors per cluster %u.", sct_per_clst);
82cae269 759 goto out;
e43f6ec2
KK
760 }
761
762 sbi->cluster_size = boot_sector_size * sct_per_clst;
763 sbi->cluster_bits = cluster_bits = blksize_bits(sbi->cluster_size);
764 sbi->cluster_mask = sbi->cluster_size - 1;
765 sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask;
82cae269
KK
766
767 mlcn = le64_to_cpu(boot->mft_clst);
768 mlcn2 = le64_to_cpu(boot->mft2_clst);
769 sectors = le64_to_cpu(boot->sectors_per_volume);
770
e43f6ec2
KK
771 if (mlcn * sct_per_clst >= sectors || mlcn2 * sct_per_clst >= sectors) {
772 ntfs_err(
773 sb,
774 "Start of MFT 0x%llx (0x%llx) is out of volume 0x%llx.",
775 mlcn, mlcn2, sectors);
82cae269 776 goto out;
e43f6ec2 777 }
82cae269 778
e43f6ec2
KK
779 sbi->record_size = record_size =
780 boot->record_size < 0 ? 1 << (-boot->record_size) :
781 (u32)boot->record_size << cluster_bits;
782 sbi->record_bits = blksize_bits(record_size);
783 sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes
82cae269 784
e8b8e97f 785 /* Check MFT record size. */
e43f6ec2
KK
786 if (record_size < SECTOR_SIZE || !is_power_of_2(record_size)) {
787 ntfs_err(sb, "Invalid bytes per MFT record %u (%d).",
788 record_size, boot->record_size);
789 goto out;
790 }
791
792 if (record_size > MAXIMUM_BYTES_PER_MFT) {
793 ntfs_err(sb, "Unsupported bytes per MFT record %u.",
794 record_size);
82cae269
KK
795 goto out;
796 }
797
e43f6ec2
KK
798 sbi->index_size = boot->index_size < 0 ?
799 1u << (-boot->index_size) :
800 (u32)boot->index_size << cluster_bits;
801
e8b8e97f 802 /* Check index record size. */
e43f6ec2
KK
803 if (sbi->index_size < SECTOR_SIZE || !is_power_of_2(sbi->index_size)) {
804 ntfs_err(sb, "Invalid bytes per index %u(%d).", sbi->index_size,
805 boot->index_size);
806 goto out;
807 }
808
809 if (sbi->index_size > MAXIMUM_BYTES_PER_INDEX) {
810 ntfs_err(sb, "Unsupported bytes per index %u.",
811 sbi->index_size);
82cae269
KK
812 goto out;
813 }
814
dbf59e2a 815 sbi->volume.size = sectors * boot_sector_size;
82cae269 816
dbf59e2a 817 gb = format_size_gb(sbi->volume.size + boot_sector_size, &mb);
82cae269
KK
818
819 /*
e8b8e97f
KA
820 * - Volume formatted and mounted with the same sector size.
821 * - Volume formatted 4K and mounted as 512.
822 * - Volume formatted 512 and mounted as 4K.
82cae269 823 */
dbf59e2a
KK
824 if (boot_sector_size != sector_size) {
825 ntfs_warn(
826 sb,
96de65a9 827 "Different NTFS sector size (%u) and media sector size (%u).",
dbf59e2a 828 boot_sector_size, sector_size);
82cae269
KK
829 dev_size += sector_size - 1;
830 }
831
96de65a9
KK
832 sbi->mft.lbo = mlcn << cluster_bits;
833 sbi->mft.lbo2 = mlcn2 << cluster_bits;
82cae269 834
09f7c338 835 /* Compare boot's cluster and sector. */
e43f6ec2
KK
836 if (sbi->cluster_size < boot_sector_size) {
837 ntfs_err(sb, "Invalid bytes per cluster (%u).",
838 sbi->cluster_size);
82cae269 839 goto out;
e43f6ec2 840 }
82cae269 841
09f7c338
KK
842 /* Compare boot's cluster and media sector. */
843 if (sbi->cluster_size < sector_size) {
844 /* No way to use ntfs_get_block in this case. */
845 ntfs_err(
846 sb,
96de65a9 847 "Failed to mount 'cause NTFS's cluster size (%u) is less than media sector size (%u).",
09f7c338
KK
848 sbi->cluster_size, sector_size);
849 goto out;
850 }
851
82cae269 852 sbi->max_bytes_per_attr =
fa3cacf5
KA
853 record_size - ALIGN(MFTRECORD_FIXUP_OFFSET_1, 8) -
854 ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) -
855 ALIGN(sizeof(enum ATTR_TYPE), 8);
82cae269 856
82cae269 857 sbi->volume.ser_num = le64_to_cpu(boot->serial_num);
82cae269 858
e8b8e97f 859 /* Warning if RAW volume. */
dbf59e2a 860 if (dev_size < sbi->volume.size + boot_sector_size) {
82cae269
KK
861 u32 mb0, gb0;
862
863 gb0 = format_size_gb(dev_size, &mb0);
864 ntfs_warn(
865 sb,
96de65a9 866 "RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only.",
82cae269
KK
867 gb, mb, gb0, mb0);
868 sb->s_flags |= SB_RDONLY;
869 }
870
96de65a9 871 clusters = sbi->volume.size >> cluster_bits;
82cae269 872#ifndef CONFIG_NTFS3_64BIT_CLUSTER
e8b8e97f 873 /* 32 bits per cluster. */
82cae269
KK
874 if (clusters >> 32) {
875 ntfs_notice(
876 sb,
96de65a9 877 "NTFS %u.%02u Gb is too big to use 32 bits per cluster.",
82cae269
KK
878 gb, mb);
879 goto out;
880 }
881#elif BITS_PER_LONG < 64
882#error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS"
883#endif
884
885 sbi->used.bitmap.nbits = clusters;
886
195c52bd 887 rec = kzalloc(record_size, GFP_NOFS);
82cae269
KK
888 if (!rec) {
889 err = -ENOMEM;
890 goto out;
891 }
892
893 sbi->new_rec = rec;
894 rec->rhdr.sign = NTFS_FILE_SIGNATURE;
895 rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET_1);
896 fn = (sbi->record_size >> SECTOR_SHIFT) + 1;
897 rec->rhdr.fix_num = cpu_to_le16(fn);
fa3cacf5 898 ao = ALIGN(MFTRECORD_FIXUP_OFFSET_1 + sizeof(short) * fn, 8);
82cae269 899 rec->attr_off = cpu_to_le16(ao);
fa3cacf5 900 rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8));
82cae269
KK
901 rec->total = cpu_to_le32(sbi->record_size);
902 ((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END;
903
28861e3b 904 sb_set_blocksize(sb, min_t(u32, sbi->cluster_size, PAGE_SIZE));
82cae269
KK
905
906 sbi->block_mask = sb->s_blocksize - 1;
907 sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits;
908 sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits;
909
e8b8e97f 910 /* Maximum size for normal files. */
96de65a9 911 sbi->maxbytes = (clusters << cluster_bits) - 1;
82cae269
KK
912
913#ifdef CONFIG_NTFS3_64BIT_CLUSTER
96de65a9 914 if (clusters >= (1ull << (64 - cluster_bits)))
82cae269
KK
915 sbi->maxbytes = -1;
916 sbi->maxbytes_sparse = -1;
28861e3b 917 sb->s_maxbytes = MAX_LFS_FILESIZE;
82cae269 918#else
e8b8e97f 919 /* Maximum size for sparse file. */
96de65a9
KK
920 sbi->maxbytes_sparse = (1ull << (cluster_bits + 32)) - 1;
921 sb->s_maxbytes = 0xFFFFFFFFull << cluster_bits;
82cae269
KK
922#endif
923
8335ebe1
KK
924 /*
925 * Compute the MFT zone at two steps.
926 * It would be nice if we are able to allocate 1/8 of
927 * total clusters for MFT but not more then 512 MB.
928 */
96de65a9 929 sbi->zone_max = min_t(CLST, 0x20000000 >> cluster_bits, clusters >> 3);
8335ebe1 930
82cae269
KK
931 err = 0;
932
933out:
934 brelse(bh);
935
936 return err;
937}
938
e8b8e97f
KA
939/*
940 * ntfs_fill_super - Try to mount.
941 */
610f8f5a 942static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
82cae269
KK
943{
944 int err;
610f8f5a 945 struct ntfs_sb_info *sbi = sb->s_fs_info;
82cae269 946 struct block_device *bdev = sb->s_bdev;
e43f6ec2 947 struct ntfs_mount_options *options;
10b4f12c 948 struct inode *inode;
82cae269 949 struct ntfs_inode *ni;
ec5fc720 950 size_t i, tt, bad_len, bad_frags;
82cae269
KK
951 CLST vcn, lcn, len;
952 struct ATTRIB *attr;
953 const struct VOLUME_INFO *info;
954 u32 idx, done, bytes;
955 struct ATTR_DEF_ENTRY *t;
82cae269 956 u16 *shared;
82cae269
KK
957 struct MFT_REF ref;
958
959 ref.high = 0;
960
82cae269 961 sbi->sb = sb;
e43f6ec2 962 sbi->options = options = fc->fs_private;
cd39981f 963 fc->fs_private = NULL;
82cae269
KK
964 sb->s_flags |= SB_NODIRATIME;
965 sb->s_magic = 0x7366746e; // "ntfs"
966 sb->s_op = &ntfs_sops;
967 sb->s_export_op = &ntfs_export_ops;
968 sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec
969 sb->s_xattr = ntfs_xattr_handlers;
e43f6ec2 970 sb->s_d_op = options->nocase ? &ntfs_dentry_ops : NULL;
82cae269 971
e43f6ec2
KK
972 options->nls = ntfs_load_nls(options->nls_name);
973 if (IS_ERR(options->nls)) {
974 options->nls = NULL;
975 errorf(fc, "Cannot load nls %s", options->nls_name);
9b75450d
KK
976 err = -EINVAL;
977 goto out;
610f8f5a 978 }
82cae269 979
7b47ef52
CH
980 if (bdev_max_discard_sectors(bdev) && bdev_discard_granularity(bdev)) {
981 sbi->discard_granularity = bdev_discard_granularity(bdev);
82cae269
KK
982 sbi->discard_granularity_mask_inv =
983 ~(u64)(sbi->discard_granularity - 1);
984 }
985
e8b8e97f 986 /* Parse boot. */
f09dac9a 987 err = ntfs_init_from_boot(sb, bdev_logical_block_size(bdev),
d54f13a8 988 bdev_nr_bytes(bdev));
82cae269 989 if (err)
9b75450d 990 goto out;
82cae269 991
82cae269 992 /*
e8b8e97f
KA
993 * Load $Volume. This should be done before $LogFile
994 * 'cause 'sbi->volume.ni' is used 'ntfs_set_state'.
82cae269
KK
995 */
996 ref.low = cpu_to_le32(MFT_REC_VOL);
997 ref.seq = cpu_to_le16(MFT_REC_VOL);
998 inode = ntfs_iget5(sb, &ref, &NAME_VOLUME);
999 if (IS_ERR(inode)) {
9b75450d 1000 err = PTR_ERR(inode);
e43f6ec2 1001 ntfs_err(sb, "Failed to load $Volume (%d).", err);
9b75450d 1002 goto out;
82cae269
KK
1003 }
1004
1005 ni = ntfs_i(inode);
1006
e8b8e97f 1007 /* Load and save label (not necessary). */
82cae269
KK
1008 attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL);
1009
1010 if (!attr) {
1011 /* It is ok if no ATTR_LABEL */
1012 } else if (!attr->non_res && !is_attr_ext(attr)) {
e8b8e97f 1013 /* $AttrDef allows labels to be up to 128 symbols. */
82cae269
KK
1014 err = utf16s_to_utf8s(resident_data(attr),
1015 le32_to_cpu(attr->res.data_size) >> 1,
1016 UTF16_LITTLE_ENDIAN, sbi->volume.label,
1017 sizeof(sbi->volume.label));
1018 if (err < 0)
1019 sbi->volume.label[0] = 0;
1020 } else {
e8b8e97f 1021 /* Should we break mounting here? */
82cae269 1022 //err = -EINVAL;
9b75450d 1023 //goto put_inode_out;
82cae269
KK
1024 }
1025
1026 attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL);
e43f6ec2
KK
1027 if (!attr || is_attr_ext(attr) ||
1028 !(info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO))) {
1029 ntfs_err(sb, "$Volume is corrupted.");
82cae269 1030 err = -EINVAL;
9b75450d 1031 goto put_inode_out;
82cae269
KK
1032 }
1033
1034 sbi->volume.major_ver = info->major_ver;
1035 sbi->volume.minor_ver = info->minor_ver;
1036 sbi->volume.flags = info->flags;
82cae269 1037 sbi->volume.ni = ni;
82cae269 1038
e8b8e97f 1039 /* Load $MFTMirr to estimate recs_mirr. */
82cae269
KK
1040 ref.low = cpu_to_le32(MFT_REC_MIRR);
1041 ref.seq = cpu_to_le16(MFT_REC_MIRR);
1042 inode = ntfs_iget5(sb, &ref, &NAME_MIRROR);
1043 if (IS_ERR(inode)) {
9b75450d 1044 err = PTR_ERR(inode);
e43f6ec2 1045 ntfs_err(sb, "Failed to load $MFTMirr (%d).", err);
9b75450d 1046 goto out;
82cae269
KK
1047 }
1048
e43f6ec2
KK
1049 sbi->mft.recs_mirr = ntfs_up_cluster(sbi, inode->i_size) >>
1050 sbi->record_bits;
82cae269
KK
1051
1052 iput(inode);
1053
d3624466 1054 /* Load LogFile to replay. */
82cae269
KK
1055 ref.low = cpu_to_le32(MFT_REC_LOG);
1056 ref.seq = cpu_to_le16(MFT_REC_LOG);
1057 inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE);
1058 if (IS_ERR(inode)) {
9b75450d 1059 err = PTR_ERR(inode);
e43f6ec2 1060 ntfs_err(sb, "Failed to load \x24LogFile (%d).", err);
9b75450d 1061 goto out;
82cae269
KK
1062 }
1063
1064 ni = ntfs_i(inode);
1065
1066 err = ntfs_loadlog_and_replay(ni, sbi);
1067 if (err)
9b75450d 1068 goto put_inode_out;
82cae269
KK
1069
1070 iput(inode);
82cae269 1071
82cae269 1072 if (sbi->flags & NTFS_FLAGS_NEED_REPLAY) {
0cde7e81 1073 if (!sb_rdonly(sb)) {
82cae269
KK
1074 ntfs_warn(sb,
1075 "failed to replay log file. Can't mount rw!");
9b75450d
KK
1076 err = -EINVAL;
1077 goto out;
82cae269
KK
1078 }
1079 } else if (sbi->volume.flags & VOLUME_FLAG_DIRTY) {
e43f6ec2 1080 if (!sb_rdonly(sb) && !options->force) {
82cae269
KK
1081 ntfs_warn(
1082 sb,
1083 "volume is dirty and \"force\" flag is not set!");
9b75450d
KK
1084 err = -EINVAL;
1085 goto out;
82cae269
KK
1086 }
1087 }
1088
e8b8e97f 1089 /* Load $MFT. */
82cae269
KK
1090 ref.low = cpu_to_le32(MFT_REC_MFT);
1091 ref.seq = cpu_to_le16(1);
1092
1093 inode = ntfs_iget5(sb, &ref, &NAME_MFT);
1094 if (IS_ERR(inode)) {
9b75450d 1095 err = PTR_ERR(inode);
e43f6ec2 1096 ntfs_err(sb, "Failed to load $MFT (%d).", err);
9b75450d 1097 goto out;
82cae269
KK
1098 }
1099
1100 ni = ntfs_i(inode);
1101
1102 sbi->mft.used = ni->i_valid >> sbi->record_bits;
1103 tt = inode->i_size >> sbi->record_bits;
1104 sbi->mft.next_free = MFT_REC_USER;
1105
1106 err = wnd_init(&sbi->mft.bitmap, sb, tt);
1107 if (err)
9b75450d 1108 goto put_inode_out;
82cae269
KK
1109
1110 err = ni_load_all_mi(ni);
e43f6ec2
KK
1111 if (err) {
1112 ntfs_err(sb, "Failed to load $MFT's subrecords (%d).", err);
9b75450d 1113 goto put_inode_out;
e43f6ec2 1114 }
82cae269
KK
1115
1116 sbi->mft.ni = ni;
1117
e8b8e97f 1118 /* Load $Bitmap. */
82cae269
KK
1119 ref.low = cpu_to_le32(MFT_REC_BITMAP);
1120 ref.seq = cpu_to_le16(MFT_REC_BITMAP);
1121 inode = ntfs_iget5(sb, &ref, &NAME_BITMAP);
1122 if (IS_ERR(inode)) {
9b75450d 1123 err = PTR_ERR(inode);
e43f6ec2 1124 ntfs_err(sb, "Failed to load $Bitmap (%d).", err);
9b75450d 1125 goto out;
82cae269
KK
1126 }
1127
82cae269
KK
1128#ifndef CONFIG_NTFS3_64BIT_CLUSTER
1129 if (inode->i_size >> 32) {
1130 err = -EINVAL;
9b75450d 1131 goto put_inode_out;
82cae269
KK
1132 }
1133#endif
1134
e8b8e97f 1135 /* Check bitmap boundary. */
82cae269
KK
1136 tt = sbi->used.bitmap.nbits;
1137 if (inode->i_size < bitmap_size(tt)) {
e43f6ec2 1138 ntfs_err(sb, "$Bitmap is corrupted.");
82cae269 1139 err = -EINVAL;
9b75450d 1140 goto put_inode_out;
82cae269
KK
1141 }
1142
b4f110d6 1143 err = wnd_init(&sbi->used.bitmap, sb, tt);
e43f6ec2
KK
1144 if (err) {
1145 ntfs_err(sb, "Failed to initialize $Bitmap (%d).", err);
9b75450d 1146 goto put_inode_out;
e43f6ec2 1147 }
82cae269
KK
1148
1149 iput(inode);
1150
e8b8e97f 1151 /* Compute the MFT zone. */
82cae269 1152 err = ntfs_refresh_zone(sbi);
e43f6ec2
KK
1153 if (err) {
1154 ntfs_err(sb, "Failed to initialize MFT zone (%d).", err);
9b75450d 1155 goto out;
e43f6ec2 1156 }
82cae269 1157
ec5fc720
KK
1158 /* Load $BadClus. */
1159 ref.low = cpu_to_le32(MFT_REC_BADCLUST);
1160 ref.seq = cpu_to_le16(MFT_REC_BADCLUST);
1161 inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS);
1162 if (IS_ERR(inode)) {
1163 err = PTR_ERR(inode);
1164 ntfs_err(sb, "Failed to load $BadClus (%d).", err);
1165 goto out;
1166 }
1167
1168 ni = ntfs_i(inode);
1169 bad_len = bad_frags = 0;
1170 for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) {
1171 if (lcn == SPARSE_LCN)
1172 continue;
1173
1174 bad_len += len;
1175 bad_frags += 1;
1176 if (sb_rdonly(sb))
1177 continue;
1178
1179 if (wnd_set_used_safe(&sbi->used.bitmap, lcn, len, &tt) || tt) {
1180 /* Bad blocks marked as free in bitmap. */
1181 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1182 }
1183 }
1184 if (bad_len) {
1185 /*
1186 * Notice about bad blocks.
1187 * In normal cases these blocks are marked as used in bitmap.
1188 * And we never allocate space in it.
1189 */
1190 ntfs_notice(sb,
1191 "Volume contains %zu bad blocks in %zu fragments.",
1192 bad_len, bad_frags);
1193 }
1194 iput(inode);
1195
e8b8e97f 1196 /* Load $AttrDef. */
82cae269
KK
1197 ref.low = cpu_to_le32(MFT_REC_ATTR);
1198 ref.seq = cpu_to_le16(MFT_REC_ATTR);
b4f110d6 1199 inode = ntfs_iget5(sb, &ref, &NAME_ATTRDEF);
82cae269 1200 if (IS_ERR(inode)) {
9b75450d 1201 err = PTR_ERR(inode);
e43f6ec2 1202 ntfs_err(sb, "Failed to load $AttrDef (%d)", err);
9b75450d 1203 goto out;
82cae269
KK
1204 }
1205
318d016e
KK
1206 /*
1207 * Typical $AttrDef contains up to 20 entries.
30200ef8 1208 * Check for extremely large/small size.
318d016e
KK
1209 */
1210 if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY) ||
1211 inode->i_size > 100 * sizeof(struct ATTR_DEF_ENTRY)) {
1212 ntfs_err(sb, "Looks like $AttrDef is corrupted (size=%llu).",
1213 inode->i_size);
82cae269 1214 err = -EINVAL;
9b75450d 1215 goto put_inode_out;
82cae269 1216 }
318d016e 1217
82cae269 1218 bytes = inode->i_size;
59bfd7a4 1219 sbi->def_table = t = kmalloc(bytes, GFP_NOFS | __GFP_NOWARN);
82cae269
KK
1220 if (!t) {
1221 err = -ENOMEM;
9b75450d 1222 goto put_inode_out;
82cae269
KK
1223 }
1224
1225 for (done = idx = 0; done < bytes; done += PAGE_SIZE, idx++) {
1226 unsigned long tail = bytes - done;
1227 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1228
1229 if (IS_ERR(page)) {
1230 err = PTR_ERR(page);
e43f6ec2 1231 ntfs_err(sb, "Failed to read $AttrDef (%d).", err);
9b75450d 1232 goto put_inode_out;
82cae269
KK
1233 }
1234 memcpy(Add2Ptr(t, done), page_address(page),
1235 min(PAGE_SIZE, tail));
1236 ntfs_unmap_page(page);
1237
1238 if (!idx && ATTR_STD != t->type) {
e43f6ec2 1239 ntfs_err(sb, "$AttrDef is corrupted.");
82cae269 1240 err = -EINVAL;
9b75450d 1241 goto put_inode_out;
82cae269
KK
1242 }
1243 }
1244
1245 t += 1;
1246 sbi->def_entries = 1;
1247 done = sizeof(struct ATTR_DEF_ENTRY);
1248 sbi->reparse.max_size = MAXIMUM_REPARSE_DATA_BUFFER_SIZE;
f8d87ed9 1249 sbi->ea_max_size = 0x10000; /* default formatter value */
82cae269
KK
1250
1251 while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) {
1252 u32 t32 = le32_to_cpu(t->type);
1253 u64 sz = le64_to_cpu(t->max_sz);
1254
1255 if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32)
1256 break;
1257
1258 if (t->type == ATTR_REPARSE)
1259 sbi->reparse.max_size = sz;
1260 else if (t->type == ATTR_EA)
1261 sbi->ea_max_size = sz;
1262
1263 done += sizeof(struct ATTR_DEF_ENTRY);
1264 t += 1;
1265 sbi->def_entries += 1;
1266 }
1267 iput(inode);
1268
e8b8e97f 1269 /* Load $UpCase. */
82cae269
KK
1270 ref.low = cpu_to_le32(MFT_REC_UPCASE);
1271 ref.seq = cpu_to_le16(MFT_REC_UPCASE);
1272 inode = ntfs_iget5(sb, &ref, &NAME_UPCASE);
1273 if (IS_ERR(inode)) {
9b75450d 1274 err = PTR_ERR(inode);
e43f6ec2 1275 ntfs_err(sb, "Failed to load $UpCase (%d).", err);
9b75450d 1276 goto out;
82cae269
KK
1277 }
1278
82cae269
KK
1279 if (inode->i_size != 0x10000 * sizeof(short)) {
1280 err = -EINVAL;
e43f6ec2 1281 ntfs_err(sb, "$UpCase is corrupted.");
9b75450d 1282 goto put_inode_out;
82cae269
KK
1283 }
1284
82cae269
KK
1285 for (idx = 0; idx < (0x10000 * sizeof(short) >> PAGE_SHIFT); idx++) {
1286 const __le16 *src;
0056b273 1287 u16 *dst = Add2Ptr(sbi->upcase, idx << PAGE_SHIFT);
82cae269
KK
1288 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1289
1290 if (IS_ERR(page)) {
1291 err = PTR_ERR(page);
e43f6ec2 1292 ntfs_err(sb, "Failed to read $UpCase (%d).", err);
9b75450d 1293 goto put_inode_out;
82cae269
KK
1294 }
1295
1296 src = page_address(page);
1297
1298#ifdef __BIG_ENDIAN
1299 for (i = 0; i < PAGE_SIZE / sizeof(u16); i++)
1300 *dst++ = le16_to_cpu(*src++);
1301#else
1302 memcpy(dst, src, PAGE_SIZE);
1303#endif
1304 ntfs_unmap_page(page);
1305 }
1306
0056b273
KA
1307 shared = ntfs_set_shared(sbi->upcase, 0x10000 * sizeof(short));
1308 if (shared && sbi->upcase != shared) {
1309 kvfree(sbi->upcase);
82cae269 1310 sbi->upcase = shared;
82cae269
KK
1311 }
1312
1313 iput(inode);
82cae269
KK
1314
1315 if (is_ntfs3(sbi)) {
e8b8e97f 1316 /* Load $Secure. */
82cae269 1317 err = ntfs_security_init(sbi);
e43f6ec2
KK
1318 if (err) {
1319 ntfs_err(sb, "Failed to initialize $Secure (%d).", err);
9b75450d 1320 goto out;
e43f6ec2 1321 }
82cae269 1322
e8b8e97f 1323 /* Load $Extend. */
82cae269 1324 err = ntfs_extend_init(sbi);
e43f6ec2
KK
1325 if (err) {
1326 ntfs_warn(sb, "Failed to initialize $Extend.");
82cae269 1327 goto load_root;
e43f6ec2 1328 }
82cae269 1329
e43f6ec2 1330 /* Load $Extend/$Reparse. */
82cae269 1331 err = ntfs_reparse_init(sbi);
e43f6ec2
KK
1332 if (err) {
1333 ntfs_warn(sb, "Failed to initialize $Extend/$Reparse.");
82cae269 1334 goto load_root;
e43f6ec2 1335 }
82cae269 1336
e43f6ec2 1337 /* Load $Extend/$ObjId. */
82cae269 1338 err = ntfs_objid_init(sbi);
e43f6ec2
KK
1339 if (err) {
1340 ntfs_warn(sb, "Failed to initialize $Extend/$ObjId.");
82cae269 1341 goto load_root;
e43f6ec2 1342 }
82cae269
KK
1343 }
1344
1345load_root:
e8b8e97f 1346 /* Load root. */
82cae269
KK
1347 ref.low = cpu_to_le32(MFT_REC_ROOT);
1348 ref.seq = cpu_to_le16(MFT_REC_ROOT);
1349 inode = ntfs_iget5(sb, &ref, &NAME_ROOT);
788ee160 1350 if (IS_ERR(inode)) {
e43f6ec2
KK
1351 err = PTR_ERR(inode);
1352 ntfs_err(sb, "Failed to load root (%d).", err);
9b75450d 1353 goto out;
82cae269
KK
1354 }
1355
788ee160
KK
1356 /*
1357 * Final check. Looks like this case should never occurs.
1358 */
1359 if (!inode->i_op) {
1360 err = -EINVAL;
1361 ntfs_err(sb, "Failed to load root (%d).", err);
1362 goto put_inode_out;
1363 }
1364
82cae269 1365 sb->s_root = d_make_root(inode);
9b75450d
KK
1366 if (!sb->s_root) {
1367 err = -ENOMEM;
1368 goto put_inode_out;
1369 }
82cae269
KK
1370
1371 return 0;
9b75450d
KK
1372
1373put_inode_out:
82cae269 1374 iput(inode);
9b75450d
KK
1375out:
1376 /*
1377 * Free resources here.
1378 * ntfs_fs_free will be called with fc->s_fs_info = NULL
1379 */
51e76a23 1380 put_mount_options(sbi->options);
9b75450d
KK
1381 put_ntfs(sbi);
1382 sb->s_fs_info = NULL;
1383
82cae269
KK
1384 return err;
1385}
1386
1387void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len)
1388{
1389 struct ntfs_sb_info *sbi = sb->s_fs_info;
1390 struct block_device *bdev = sb->s_bdev;
1391 sector_t devblock = (u64)lcn * sbi->blocks_per_cluster;
1392 unsigned long blocks = (u64)len * sbi->blocks_per_cluster;
1393 unsigned long cnt = 0;
1394 unsigned long limit = global_zone_page_state(NR_FREE_PAGES)
1395 << (PAGE_SHIFT - sb->s_blocksize_bits);
1396
1397 if (limit >= 0x2000)
1398 limit -= 0x1000;
1399 else if (limit < 32)
1400 limit = 32;
1401 else
1402 limit >>= 1;
1403
1404 while (blocks--) {
1405 clean_bdev_aliases(bdev, devblock++, 1);
1406 if (cnt++ >= limit) {
1407 sync_blockdev(bdev);
1408 cnt = 0;
1409 }
1410 }
1411}
1412
1413/*
e8b8e97f 1414 * ntfs_discard - Issue a discard request (trim for SSD).
82cae269
KK
1415 */
1416int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len)
1417{
1418 int err;
1419 u64 lbo, bytes, start, end;
1420 struct super_block *sb;
1421
1422 if (sbi->used.next_free_lcn == lcn + len)
1423 sbi->used.next_free_lcn = lcn;
1424
1425 if (sbi->flags & NTFS_FLAGS_NODISCARD)
1426 return -EOPNOTSUPP;
1427
564c97bd 1428 if (!sbi->options->discard)
82cae269
KK
1429 return -EOPNOTSUPP;
1430
1431 lbo = (u64)lcn << sbi->cluster_bits;
1432 bytes = (u64)len << sbi->cluster_bits;
1433
e8b8e97f 1434 /* Align up 'start' on discard_granularity. */
82cae269
KK
1435 start = (lbo + sbi->discard_granularity - 1) &
1436 sbi->discard_granularity_mask_inv;
e8b8e97f 1437 /* Align down 'end' on discard_granularity. */
82cae269
KK
1438 end = (lbo + bytes) & sbi->discard_granularity_mask_inv;
1439
1440 sb = sbi->sb;
1441 if (start >= end)
1442 return 0;
1443
1444 err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9,
44abff2c 1445 GFP_NOFS);
82cae269
KK
1446
1447 if (err == -EOPNOTSUPP)
1448 sbi->flags |= NTFS_FLAGS_NODISCARD;
1449
1450 return err;
1451}
1452
610f8f5a
KA
1453static int ntfs_fs_get_tree(struct fs_context *fc)
1454{
1455 return get_tree_bdev(fc, ntfs_fill_super);
1456}
1457
1458/*
1459 * ntfs_fs_free - Free fs_context.
1460 *
1461 * Note that this will be called after fill_super and reconfigure
1462 * even when they pass. So they have to take pointers if they pass.
1463 */
1464static void ntfs_fs_free(struct fs_context *fc)
1465{
1466 struct ntfs_mount_options *opts = fc->fs_private;
1467 struct ntfs_sb_info *sbi = fc->s_fs_info;
1468
1469 if (sbi)
1470 put_ntfs(sbi);
1471
1472 if (opts)
1473 put_mount_options(opts);
1474}
1475
1476static const struct fs_context_operations ntfs_context_ops = {
1477 .parse_param = ntfs_fs_parse_param,
1478 .get_tree = ntfs_fs_get_tree,
1479 .reconfigure = ntfs_fs_reconfigure,
1480 .free = ntfs_fs_free,
1481};
1482
1483/*
96de65a9 1484 * ntfs_init_fs_context - Initialize sbi and opts
610f8f5a 1485 *
6700eabb 1486 * This will called when mount/remount. We will first initialize
610f8f5a
KA
1487 * options so that if remount we can use just that.
1488 */
1489static int ntfs_init_fs_context(struct fs_context *fc)
82cae269 1490{
610f8f5a
KA
1491 struct ntfs_mount_options *opts;
1492 struct ntfs_sb_info *sbi;
1493
1494 opts = kzalloc(sizeof(struct ntfs_mount_options), GFP_NOFS);
1495 if (!opts)
1496 return -ENOMEM;
1497
1498 /* Default options. */
1499 opts->fs_uid = current_uid();
1500 opts->fs_gid = current_gid();
1501 opts->fs_fmask_inv = ~current_umask();
1502 opts->fs_dmask_inv = ~current_umask();
1503
1504 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
1505 goto ok;
1506
1507 sbi = kzalloc(sizeof(struct ntfs_sb_info), GFP_NOFS);
27fac777
KA
1508 if (!sbi)
1509 goto free_opts;
1510
1511 sbi->upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL);
1512 if (!sbi->upcase)
1513 goto free_sbi;
1514
1515 ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1516 DEFAULT_RATELIMIT_BURST);
1517
1518 mutex_init(&sbi->compress.mtx_lznt);
1519#ifdef CONFIG_NTFS3_LZX_XPRESS
1520 mutex_init(&sbi->compress.mtx_xpress);
1521 mutex_init(&sbi->compress.mtx_lzx);
1522#endif
610f8f5a 1523
610f8f5a
KA
1524 fc->s_fs_info = sbi;
1525ok:
1526 fc->fs_private = opts;
1527 fc->ops = &ntfs_context_ops;
1528
1529 return 0;
27fac777
KA
1530free_sbi:
1531 kfree(sbi);
880301bb
CIK
1532free_opts:
1533 kfree(opts);
27fac777 1534 return -ENOMEM;
82cae269
KK
1535}
1536
1537// clang-format off
1538static struct file_system_type ntfs_fs_type = {
610f8f5a
KA
1539 .owner = THIS_MODULE,
1540 .name = "ntfs3",
1541 .init_fs_context = ntfs_init_fs_context,
1542 .parameters = ntfs_fs_parameters,
1543 .kill_sb = kill_block_super,
1544 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
82cae269
KK
1545};
1546// clang-format on
1547
1548static int __init init_ntfs_fs(void)
1549{
1550 int err;
1551
2e3a51b5 1552 pr_info("ntfs3: Max link count %u\n", NTFS_LINK_MAX);
82cae269 1553
2e3a51b5
KA
1554 if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL))
1555 pr_info("ntfs3: Enabled Linux POSIX ACLs support\n");
1556 if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER))
96de65a9
KK
1557 pr_notice(
1558 "ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n");
2e3a51b5
KA
1559 if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS))
1560 pr_info("ntfs3: Read-only LZX/Xpress compression included\n");
82cae269
KK
1561
1562 err = ntfs3_init_bitmap();
1563 if (err)
1564 return err;
1565
1566 ntfs_inode_cachep = kmem_cache_create(
1567 "ntfs_inode_cache", sizeof(struct ntfs_inode), 0,
1568 (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1569 init_once);
1570 if (!ntfs_inode_cachep) {
1571 err = -ENOMEM;
1572 goto out1;
1573 }
1574
1575 err = register_filesystem(&ntfs_fs_type);
1576 if (err)
1577 goto out;
1578
1579 return 0;
1580out:
1581 kmem_cache_destroy(ntfs_inode_cachep);
1582out1:
1583 ntfs3_exit_bitmap();
1584 return err;
1585}
1586
1587static void __exit exit_ntfs_fs(void)
1588{
ae6b47b5
KK
1589 rcu_barrier();
1590 kmem_cache_destroy(ntfs_inode_cachep);
82cae269
KK
1591 unregister_filesystem(&ntfs_fs_type);
1592 ntfs3_exit_bitmap();
1593}
1594
1595MODULE_LICENSE("GPL");
1596MODULE_DESCRIPTION("ntfs3 read/write filesystem");
82cae269
KK
1597#ifdef CONFIG_NTFS3_FS_POSIX_ACL
1598MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support");
1599#endif
1600#ifdef CONFIG_NTFS3_64BIT_CLUSTER
96de65a9
KK
1601MODULE_INFO(
1602 cluster,
1603 "Warning: Activated 64 bits per cluster. Windows does not support this");
82cae269
KK
1604#endif
1605#ifdef CONFIG_NTFS3_LZX_XPRESS
1606MODULE_INFO(compression, "Read-only lzx/xpress compression included");
1607#endif
1608
1609MODULE_AUTHOR("Konstantin Komarov");
1610MODULE_ALIAS_FS("ntfs3");
1611
1612module_init(init_ntfs_fs);
1613module_exit(exit_ntfs_fs);