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