blob: 3cba0b5e7ac72c444a55de38b791faa87732049f [file] [log] [blame]
Konstantin Komarov82cae262021-08-13 17:21:29 +03001// 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
Kari Argillandere8b8e972021-08-03 14:57:09 +030010 * 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.
Konstantin Komarov82cae262021-08-13 17:21:29 +030019 *
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
26#include <linux/backing-dev.h>
27#include <linux/blkdev.h>
28#include <linux/buffer_head.h>
29#include <linux/exportfs.h>
30#include <linux/fs.h>
Kari Argillander610f8f52021-09-07 18:35:52 +030031#include <linux/fs_context.h>
32#include <linux/fs_parser.h>
Konstantin Komarov82cae262021-08-13 17:21:29 +030033#include <linux/iversion.h>
Kari Argillander528c9b32021-08-16 13:37:32 +030034#include <linux/log2.h>
Konstantin Komarov82cae262021-08-13 17:21:29 +030035#include <linux/module.h>
36#include <linux/nls.h>
Konstantin Komarov82cae262021-08-13 17:21:29 +030037#include <linux/seq_file.h>
38#include <linux/statfs.h>
39
40#include "debug.h"
41#include "ntfs.h"
42#include "ntfs_fs.h"
43#ifdef CONFIG_NTFS3_LZX_XPRESS
44#include "lib/lib.h"
45#endif
46
47#ifdef CONFIG_PRINTK
48/*
Kari Argillandere8b8e972021-08-03 14:57:09 +030049 * ntfs_printk - Trace warnings/notices/errors.
50 *
Konstantin Komarov82cae262021-08-13 17:21:29 +030051 * Thanks Joe Perches <joe@perches.com> for implementation
52 */
53void ntfs_printk(const struct super_block *sb, const char *fmt, ...)
54{
55 struct va_format vaf;
56 va_list args;
57 int level;
58 struct ntfs_sb_info *sbi = sb->s_fs_info;
59
Kari Argillandere8b8e972021-08-03 14:57:09 +030060 /* Should we use different ratelimits for warnings/notices/errors? */
Konstantin Komarov82cae262021-08-13 17:21:29 +030061 if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
62 return;
63
64 va_start(args, fmt);
65
66 level = printk_get_level(fmt);
67 vaf.fmt = printk_skip_level(fmt);
68 vaf.va = &args;
69 printk("%c%cntfs3: %s: %pV\n", KERN_SOH_ASCII, level, sb->s_id, &vaf);
70
71 va_end(args);
72}
73
74static char s_name_buf[512];
Kari Argillandere8b8e972021-08-03 14:57:09 +030075static atomic_t s_name_buf_cnt = ATOMIC_INIT(1); // 1 means 'free s_name_buf'.
Konstantin Komarov82cae262021-08-13 17:21:29 +030076
Kari Argillandere8b8e972021-08-03 14:57:09 +030077/*
78 * ntfs_inode_printk
79 *
80 * Print warnings/notices/errors about inode using name or inode number.
81 */
Konstantin Komarov82cae262021-08-13 17:21:29 +030082void ntfs_inode_printk(struct inode *inode, const char *fmt, ...)
83{
84 struct super_block *sb = inode->i_sb;
85 struct ntfs_sb_info *sbi = sb->s_fs_info;
86 char *name;
87 va_list args;
88 struct va_format vaf;
89 int level;
90
91 if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
92 return;
93
Kari Argillandere8b8e972021-08-03 14:57:09 +030094 /* Use static allocated buffer, if possible. */
Konstantin Komarov82cae262021-08-13 17:21:29 +030095 name = atomic_dec_and_test(&s_name_buf_cnt)
96 ? s_name_buf
97 : kmalloc(sizeof(s_name_buf), GFP_NOFS);
98
99 if (name) {
100 struct dentry *de = d_find_alias(inode);
101 const u32 name_len = ARRAY_SIZE(s_name_buf) - 1;
102
103 if (de) {
104 spin_lock(&de->d_lock);
105 snprintf(name, name_len, " \"%s\"", de->d_name.name);
106 spin_unlock(&de->d_lock);
Kari Argillandere8b8e972021-08-03 14:57:09 +0300107 name[name_len] = 0; /* To be sure. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300108 } else {
109 name[0] = 0;
110 }
Kari Argillandere8b8e972021-08-03 14:57:09 +0300111 dput(de); /* Cocci warns if placed in branch "if (de)" */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300112 }
113
114 va_start(args, fmt);
115
116 level = printk_get_level(fmt);
117 vaf.fmt = printk_skip_level(fmt);
118 vaf.va = &args;
119
120 printk("%c%cntfs3: %s: ino=%lx,%s %pV\n", KERN_SOH_ASCII, level,
121 sb->s_id, inode->i_ino, name ? name : "", &vaf);
122
123 va_end(args);
124
125 atomic_inc(&s_name_buf_cnt);
126 if (name != s_name_buf)
127 kfree(name);
128}
129#endif
130
131/*
132 * Shared memory struct.
133 *
Kari Argillandere8b8e972021-08-03 14:57:09 +0300134 * On-disk ntfs's upcase table is created by ntfs formatter.
135 * 'upcase' table is 128K bytes of memory.
136 * We should read it into memory when mounting.
137 * Several ntfs volumes likely use the same 'upcase' table.
138 * It is good idea to share in-memory 'upcase' table between different volumes.
139 * Unfortunately winxp/vista/win7 use different upcase tables.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300140 */
141static DEFINE_SPINLOCK(s_shared_lock);
142
143static struct {
144 void *ptr;
145 u32 len;
146 int cnt;
147} s_shared[8];
148
149/*
150 * ntfs_set_shared
151 *
Kari Argillandere8b8e972021-08-03 14:57:09 +0300152 * Return:
153 * * @ptr - If pointer was saved in shared memory.
154 * * NULL - If pointer was not shared.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300155 */
156void *ntfs_set_shared(void *ptr, u32 bytes)
157{
158 void *ret = NULL;
159 int i, j = -1;
160
161 spin_lock(&s_shared_lock);
162 for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
163 if (!s_shared[i].cnt) {
164 j = i;
165 } else if (bytes == s_shared[i].len &&
166 !memcmp(s_shared[i].ptr, ptr, bytes)) {
167 s_shared[i].cnt += 1;
168 ret = s_shared[i].ptr;
169 break;
170 }
171 }
172
173 if (!ret && j != -1) {
174 s_shared[j].ptr = ptr;
175 s_shared[j].len = bytes;
176 s_shared[j].cnt = 1;
177 ret = ptr;
178 }
179 spin_unlock(&s_shared_lock);
180
181 return ret;
182}
183
184/*
185 * ntfs_put_shared
186 *
Kari Argillandere8b8e972021-08-03 14:57:09 +0300187 * Return:
188 * * @ptr - If pointer is not shared anymore.
189 * * NULL - If pointer is still shared.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300190 */
191void *ntfs_put_shared(void *ptr)
192{
193 void *ret = ptr;
194 int i;
195
196 spin_lock(&s_shared_lock);
197 for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
198 if (s_shared[i].cnt && s_shared[i].ptr == ptr) {
199 if (--s_shared[i].cnt)
200 ret = NULL;
201 break;
202 }
203 }
204 spin_unlock(&s_shared_lock);
205
206 return ret;
207}
208
Kari Argillander610f8f52021-09-07 18:35:52 +0300209static inline void put_mount_options(struct ntfs_mount_options *options)
Konstantin Komarov82cae262021-08-13 17:21:29 +0300210{
Kari Argillander610f8f52021-09-07 18:35:52 +0300211 kfree(options->nls_name);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300212 unload_nls(options->nls);
Kari Argillander610f8f52021-09-07 18:35:52 +0300213 kfree(options);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300214}
215
216enum Opt {
217 Opt_uid,
218 Opt_gid,
219 Opt_umask,
220 Opt_dmask,
221 Opt_fmask,
222 Opt_immutable,
223 Opt_discard,
224 Opt_force,
225 Opt_sparse,
226 Opt_nohidden,
227 Opt_showmeta,
228 Opt_acl,
Kari Argillandere274cde2021-09-07 18:35:55 +0300229 Opt_iocharset,
Konstantin Komarov82cae262021-08-13 17:21:29 +0300230 Opt_prealloc,
Kari Argillander28a941f2021-09-07 18:35:56 +0300231 Opt_noacsrules,
Konstantin Komarov82cae262021-08-13 17:21:29 +0300232 Opt_err,
233};
234
Kari Argillander610f8f52021-09-07 18:35:52 +0300235static const struct fs_parameter_spec ntfs_fs_parameters[] = {
236 fsparam_u32("uid", Opt_uid),
237 fsparam_u32("gid", Opt_gid),
238 fsparam_u32oct("umask", Opt_umask),
239 fsparam_u32oct("dmask", Opt_dmask),
240 fsparam_u32oct("fmask", Opt_fmask),
241 fsparam_flag_no("sys_immutable", Opt_immutable),
242 fsparam_flag_no("discard", Opt_discard),
243 fsparam_flag_no("force", Opt_force),
244 fsparam_flag_no("sparse", Opt_sparse),
Kari Argillander9d1939f2021-09-07 18:35:54 +0300245 fsparam_flag_no("hidden", Opt_nohidden),
Kari Argillander610f8f52021-09-07 18:35:52 +0300246 fsparam_flag_no("acl", Opt_acl),
247 fsparam_flag_no("showmeta", Opt_showmeta),
Kari Argillander610f8f52021-09-07 18:35:52 +0300248 fsparam_flag_no("prealloc", Opt_prealloc),
Kari Argillander28a941f2021-09-07 18:35:56 +0300249 fsparam_flag_no("acsrules", Opt_noacsrules),
Kari Argillandere274cde2021-09-07 18:35:55 +0300250 fsparam_string("iocharset", Opt_iocharset),
251
252 __fsparam(fs_param_is_string,
253 "nls", Opt_iocharset,
254 fs_param_deprecated, NULL),
Kari Argillander610f8f52021-09-07 18:35:52 +0300255 {}
Konstantin Komarov82cae262021-08-13 17:21:29 +0300256};
257
Kari Argillander610f8f52021-09-07 18:35:52 +0300258/*
259 * Load nls table or if @nls is utf8 then return NULL.
260 */
261static struct nls_table *ntfs_load_nls(char *nls)
Konstantin Komarov82cae262021-08-13 17:21:29 +0300262{
Kari Argillander610f8f52021-09-07 18:35:52 +0300263 struct nls_table *ret;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300264
Kari Argillander610f8f52021-09-07 18:35:52 +0300265 if (!nls)
266 nls = CONFIG_NLS_DEFAULT;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300267
Kari Argillander610f8f52021-09-07 18:35:52 +0300268 if (strcmp(nls, "utf8") == 0)
269 return NULL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300270
Kari Argillander610f8f52021-09-07 18:35:52 +0300271 if (strcmp(nls, CONFIG_NLS_DEFAULT) == 0)
272 return load_nls_default();
Konstantin Komarov82cae262021-08-13 17:21:29 +0300273
Kari Argillander610f8f52021-09-07 18:35:52 +0300274 ret = load_nls(nls);
275 if (ret)
276 return ret;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300277
Kari Argillander610f8f52021-09-07 18:35:52 +0300278 return ERR_PTR(-EINVAL);
279}
280
281static int ntfs_fs_parse_param(struct fs_context *fc,
282 struct fs_parameter *param)
283{
284 struct ntfs_mount_options *opts = fc->fs_private;
285 struct fs_parse_result result;
286 int opt;
287
288 opt = fs_parse(fc, ntfs_fs_parameters, param, &result);
289 if (opt < 0)
290 return opt;
291
292 switch (opt) {
293 case Opt_uid:
294 opts->fs_uid = make_kuid(current_user_ns(), result.uint_32);
295 if (!uid_valid(opts->fs_uid))
296 return invalf(fc, "ntfs3: Invalid value for uid.");
Kari Argillander610f8f52021-09-07 18:35:52 +0300297 break;
298 case Opt_gid:
299 opts->fs_gid = make_kgid(current_user_ns(), result.uint_32);
300 if (!gid_valid(opts->fs_gid))
301 return invalf(fc, "ntfs3: Invalid value for gid.");
Kari Argillander610f8f52021-09-07 18:35:52 +0300302 break;
303 case Opt_umask:
304 if (result.uint_32 & ~07777)
305 return invalf(fc, "ntfs3: Invalid value for umask.");
306 opts->fs_fmask_inv = ~result.uint_32;
307 opts->fs_dmask_inv = ~result.uint_32;
308 opts->fmask = 1;
309 opts->dmask = 1;
310 break;
311 case Opt_dmask:
312 if (result.uint_32 & ~07777)
313 return invalf(fc, "ntfs3: Invalid value for dmask.");
314 opts->fs_dmask_inv = ~result.uint_32;
315 opts->dmask = 1;
316 break;
317 case Opt_fmask:
318 if (result.uint_32 & ~07777)
319 return invalf(fc, "ntfs3: Invalid value for fmask.");
320 opts->fs_fmask_inv = ~result.uint_32;
321 opts->fmask = 1;
322 break;
323 case Opt_immutable:
324 opts->sys_immutable = result.negated ? 0 : 1;
325 break;
326 case Opt_discard:
327 opts->discard = result.negated ? 0 : 1;
328 break;
329 case Opt_force:
330 opts->force = result.negated ? 0 : 1;
331 break;
332 case Opt_sparse:
333 opts->sparse = result.negated ? 0 : 1;
334 break;
335 case Opt_nohidden:
Kari Argillander9d1939f2021-09-07 18:35:54 +0300336 opts->nohidden = result.negated ? 1 : 0;
Kari Argillander610f8f52021-09-07 18:35:52 +0300337 break;
338 case Opt_acl:
339 if (!result.negated)
Konstantin Komarov82cae262021-08-13 17:21:29 +0300340#ifdef CONFIG_NTFS3_FS_POSIX_ACL
Kari Argillander610f8f52021-09-07 18:35:52 +0300341 fc->sb_flags |= SB_POSIXACL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300342#else
Kari Argillander610f8f52021-09-07 18:35:52 +0300343 return invalf(fc, "ntfs3: Support for ACL not compiled in!");
Konstantin Komarov82cae262021-08-13 17:21:29 +0300344#endif
Kari Argillander610f8f52021-09-07 18:35:52 +0300345 else
346 fc->sb_flags &= ~SB_POSIXACL;
347 break;
348 case Opt_showmeta:
349 opts->showmeta = result.negated ? 0 : 1;
350 break;
Kari Argillandere274cde2021-09-07 18:35:55 +0300351 case Opt_iocharset:
Kari Argillander610f8f52021-09-07 18:35:52 +0300352 kfree(opts->nls_name);
353 opts->nls_name = param->string;
354 param->string = NULL;
355 break;
356 case Opt_prealloc:
357 opts->prealloc = result.negated ? 0 : 1;
358 break;
Kari Argillander28a941f2021-09-07 18:35:56 +0300359 case Opt_noacsrules:
360 opts->noacsrules = result.negated ? 1 : 0;
Kari Argillander610f8f52021-09-07 18:35:52 +0300361 break;
362 default:
363 /* Should not be here unless we forget add case. */
364 return -EINVAL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300365 }
Konstantin Komarov82cae262021-08-13 17:21:29 +0300366 return 0;
367}
368
Kari Argillander610f8f52021-09-07 18:35:52 +0300369static int ntfs_fs_reconfigure(struct fs_context *fc)
Konstantin Komarov82cae262021-08-13 17:21:29 +0300370{
Kari Argillander610f8f52021-09-07 18:35:52 +0300371 struct super_block *sb = fc->root->d_sb;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300372 struct ntfs_sb_info *sbi = sb->s_fs_info;
Kari Argillander610f8f52021-09-07 18:35:52 +0300373 struct ntfs_mount_options *new_opts = fc->fs_private;
374 int ro_rw;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300375
Kari Argillander610f8f52021-09-07 18:35:52 +0300376 ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300377 if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) {
Kari Argillander610f8f52021-09-07 18:35:52 +0300378 errorf(fc, "ntfs3: Couldn't remount rw because journal is not replayed. Please umount/remount instead\n");
379 return -EINVAL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300380 }
381
Kari Argillander610f8f52021-09-07 18:35:52 +0300382 new_opts->nls = ntfs_load_nls(new_opts->nls_name);
383 if (IS_ERR(new_opts->nls)) {
384 new_opts->nls = NULL;
Kari Argillandere274cde2021-09-07 18:35:55 +0300385 errorf(fc, "ntfs3: Cannot load iocharset %s", new_opts->nls_name);
Kari Argillander610f8f52021-09-07 18:35:52 +0300386 return -EINVAL;
387 }
388 if (new_opts->nls != sbi->options->nls)
Kari Argillandere274cde2021-09-07 18:35:55 +0300389 return invalf(fc, "ntfs3: Cannot use different iocharset when remounting!");
Kari Argillander610f8f52021-09-07 18:35:52 +0300390
Konstantin Komarov82cae262021-08-13 17:21:29 +0300391 sync_filesystem(sb);
392
393 if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) &&
Kari Argillander610f8f52021-09-07 18:35:52 +0300394 !new_opts->force) {
395 errorf(fc, "ntfs3: Volume is dirty and \"force\" flag is not set!");
396 return -EINVAL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300397 }
398
Kari Argillander610f8f52021-09-07 18:35:52 +0300399 memcpy(sbi->options, new_opts, sizeof(*new_opts));
Konstantin Komarov82cae262021-08-13 17:21:29 +0300400
Kari Argillander610f8f52021-09-07 18:35:52 +0300401 return 0;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300402}
403
404static struct kmem_cache *ntfs_inode_cachep;
405
406static struct inode *ntfs_alloc_inode(struct super_block *sb)
407{
408 struct ntfs_inode *ni = kmem_cache_alloc(ntfs_inode_cachep, GFP_NOFS);
409
410 if (!ni)
411 return NULL;
412
413 memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode));
414
415 mutex_init(&ni->ni_lock);
416
417 return &ni->vfs_inode;
418}
419
420static void ntfs_i_callback(struct rcu_head *head)
421{
422 struct inode *inode = container_of(head, struct inode, i_rcu);
423 struct ntfs_inode *ni = ntfs_i(inode);
424
425 mutex_destroy(&ni->ni_lock);
426
427 kmem_cache_free(ntfs_inode_cachep, ni);
428}
429
430static void ntfs_destroy_inode(struct inode *inode)
431{
432 call_rcu(&inode->i_rcu, ntfs_i_callback);
433}
434
435static void init_once(void *foo)
436{
437 struct ntfs_inode *ni = foo;
438
439 inode_init_once(&ni->vfs_inode);
440}
441
Kari Argillandere8b8e972021-08-03 14:57:09 +0300442/*
443 * put_ntfs - Noinline to reduce binary size.
444 */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300445static noinline void put_ntfs(struct ntfs_sb_info *sbi)
446{
Kari Argillander195c52b2021-08-24 21:37:07 +0300447 kfree(sbi->new_rec);
448 kvfree(ntfs_put_shared(sbi->upcase));
449 kfree(sbi->def_table);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300450
451 wnd_close(&sbi->mft.bitmap);
452 wnd_close(&sbi->used.bitmap);
453
454 if (sbi->mft.ni)
455 iput(&sbi->mft.ni->vfs_inode);
456
457 if (sbi->security.ni)
458 iput(&sbi->security.ni->vfs_inode);
459
460 if (sbi->reparse.ni)
461 iput(&sbi->reparse.ni->vfs_inode);
462
463 if (sbi->objid.ni)
464 iput(&sbi->objid.ni->vfs_inode);
465
466 if (sbi->volume.ni)
467 iput(&sbi->volume.ni->vfs_inode);
468
469 ntfs_update_mftmirr(sbi, 0);
470
471 indx_clear(&sbi->security.index_sii);
472 indx_clear(&sbi->security.index_sdh);
473 indx_clear(&sbi->reparse.index_r);
474 indx_clear(&sbi->objid.index_o);
Kari Argillander195c52b2021-08-24 21:37:07 +0300475 kfree(sbi->compress.lznt);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300476#ifdef CONFIG_NTFS3_LZX_XPRESS
477 xpress_free_decompressor(sbi->compress.xpress);
478 lzx_free_decompressor(sbi->compress.lzx);
479#endif
Kari Argillander195c52b2021-08-24 21:37:07 +0300480 kfree(sbi);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300481}
482
483static void ntfs_put_super(struct super_block *sb)
484{
485 struct ntfs_sb_info *sbi = sb->s_fs_info;
486
Kari Argillandere8b8e972021-08-03 14:57:09 +0300487 /* Mark rw ntfs as clear, if possible. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300488 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
489
Kari Argillander610f8f52021-09-07 18:35:52 +0300490 put_mount_options(sbi->options);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300491 put_ntfs(sbi);
Kari Argillander610f8f52021-09-07 18:35:52 +0300492 sb->s_fs_info = NULL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300493
494 sync_blockdev(sb->s_bdev);
495}
496
497static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf)
498{
499 struct super_block *sb = dentry->d_sb;
500 struct ntfs_sb_info *sbi = sb->s_fs_info;
501 struct wnd_bitmap *wnd = &sbi->used.bitmap;
502
503 buf->f_type = sb->s_magic;
504 buf->f_bsize = sbi->cluster_size;
505 buf->f_blocks = wnd->nbits;
506
507 buf->f_bfree = buf->f_bavail = wnd_zeroes(wnd);
508 buf->f_fsid.val[0] = sbi->volume.ser_num;
509 buf->f_fsid.val[1] = (sbi->volume.ser_num >> 32);
510 buf->f_namelen = NTFS_NAME_LEN;
511
512 return 0;
513}
514
515static int ntfs_show_options(struct seq_file *m, struct dentry *root)
516{
517 struct super_block *sb = root->d_sb;
518 struct ntfs_sb_info *sbi = sb->s_fs_info;
Kari Argillander564c97b2021-09-07 18:35:51 +0300519 struct ntfs_mount_options *opts = sbi->options;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300520 struct user_namespace *user_ns = seq_user_ns(m);
521
Kari Argillander15b2ae72021-09-07 18:35:57 +0300522 seq_printf(m, ",uid=%u",
523 from_kuid_munged(user_ns, opts->fs_uid));
524 seq_printf(m, ",gid=%u",
525 from_kgid_munged(user_ns, opts->fs_gid));
Konstantin Komarov82cae262021-08-13 17:21:29 +0300526 if (opts->fmask)
527 seq_printf(m, ",fmask=%04o", ~opts->fs_fmask_inv);
528 if (opts->dmask)
529 seq_printf(m, ",dmask=%04o", ~opts->fs_dmask_inv);
530 if (opts->nls)
Kari Argillandere274cde2021-09-07 18:35:55 +0300531 seq_printf(m, ",iocharset=%s", opts->nls->charset);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300532 else
Kari Argillandere274cde2021-09-07 18:35:55 +0300533 seq_puts(m, ",iocharset=utf8");
Konstantin Komarov82cae262021-08-13 17:21:29 +0300534 if (opts->sys_immutable)
535 seq_puts(m, ",sys_immutable");
536 if (opts->discard)
537 seq_puts(m, ",discard");
538 if (opts->sparse)
539 seq_puts(m, ",sparse");
540 if (opts->showmeta)
541 seq_puts(m, ",showmeta");
542 if (opts->nohidden)
543 seq_puts(m, ",nohidden");
544 if (opts->force)
545 seq_puts(m, ",force");
Kari Argillander28a941f2021-09-07 18:35:56 +0300546 if (opts->noacsrules)
547 seq_puts(m, ",noacsrules");
Konstantin Komarov82cae262021-08-13 17:21:29 +0300548 if (opts->prealloc)
549 seq_puts(m, ",prealloc");
550 if (sb->s_flags & SB_POSIXACL)
551 seq_puts(m, ",acl");
Konstantin Komarov82cae262021-08-13 17:21:29 +0300552
553 return 0;
554}
555
Kari Argillandere8b8e972021-08-03 14:57:09 +0300556/*
557 * ntfs_sync_fs - super_operations::sync_fs
558 */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300559static int ntfs_sync_fs(struct super_block *sb, int wait)
560{
561 int err = 0, err2;
562 struct ntfs_sb_info *sbi = sb->s_fs_info;
563 struct ntfs_inode *ni;
564 struct inode *inode;
565
566 ni = sbi->security.ni;
567 if (ni) {
568 inode = &ni->vfs_inode;
569 err2 = _ni_write_inode(inode, wait);
570 if (err2 && !err)
571 err = err2;
572 }
573
574 ni = sbi->objid.ni;
575 if (ni) {
576 inode = &ni->vfs_inode;
577 err2 = _ni_write_inode(inode, wait);
578 if (err2 && !err)
579 err = err2;
580 }
581
582 ni = sbi->reparse.ni;
583 if (ni) {
584 inode = &ni->vfs_inode;
585 err2 = _ni_write_inode(inode, wait);
586 if (err2 && !err)
587 err = err2;
588 }
589
590 if (!err)
591 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
592
593 ntfs_update_mftmirr(sbi, wait);
594
595 return err;
596}
597
598static const struct super_operations ntfs_sops = {
599 .alloc_inode = ntfs_alloc_inode,
600 .destroy_inode = ntfs_destroy_inode,
601 .evict_inode = ntfs_evict_inode,
602 .put_super = ntfs_put_super,
603 .statfs = ntfs_statfs,
604 .show_options = ntfs_show_options,
605 .sync_fs = ntfs_sync_fs,
Konstantin Komarov82cae262021-08-13 17:21:29 +0300606 .write_inode = ntfs3_write_inode,
607};
608
609static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino,
610 u32 generation)
611{
612 struct MFT_REF ref;
613 struct inode *inode;
614
615 ref.low = cpu_to_le32(ino);
616#ifdef CONFIG_NTFS3_64BIT_CLUSTER
617 ref.high = cpu_to_le16(ino >> 32);
618#else
619 ref.high = 0;
620#endif
621 ref.seq = cpu_to_le16(generation);
622
623 inode = ntfs_iget5(sb, &ref, NULL);
624 if (!IS_ERR(inode) && is_bad_inode(inode)) {
625 iput(inode);
626 inode = ERR_PTR(-ESTALE);
627 }
628
629 return inode;
630}
631
632static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
633 int fh_len, int fh_type)
634{
635 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
636 ntfs_export_get_inode);
637}
638
639static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
640 int fh_len, int fh_type)
641{
642 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
643 ntfs_export_get_inode);
644}
645
646/* TODO: == ntfs_sync_inode */
647static int ntfs_nfs_commit_metadata(struct inode *inode)
648{
649 return _ni_write_inode(inode, 1);
650}
651
652static const struct export_operations ntfs_export_ops = {
653 .fh_to_dentry = ntfs_fh_to_dentry,
654 .fh_to_parent = ntfs_fh_to_parent,
655 .get_parent = ntfs3_get_parent,
656 .commit_metadata = ntfs_nfs_commit_metadata,
657};
658
Kari Argillandere8b8e972021-08-03 14:57:09 +0300659/*
660 * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb".
661 */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300662static u32 format_size_gb(const u64 bytes, u32 *mb)
663{
Kari Argillandere8b8e972021-08-03 14:57:09 +0300664 /* Do simple right 30 bit shift of 64 bit value. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300665 u64 kbytes = bytes >> 10;
666 u32 kbytes32 = kbytes;
667
668 *mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20;
669 if (*mb >= 100)
670 *mb = 99;
671
672 return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12);
673}
674
675static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot)
676{
677 return boot->sectors_per_clusters <= 0x80
678 ? boot->sectors_per_clusters
679 : (1u << (0 - boot->sectors_per_clusters));
680}
681
Kari Argillandere8b8e972021-08-03 14:57:09 +0300682/*
683 * ntfs_init_from_boot - Init internal info from on-disk boot sector.
684 */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300685static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
686 u64 dev_size)
687{
688 struct ntfs_sb_info *sbi = sb->s_fs_info;
689 int err;
690 u32 mb, gb, boot_sector_size, sct_per_clst, record_size;
691 u64 sectors, clusters, fs_size, mlcn, mlcn2;
692 struct NTFS_BOOT *boot;
693 struct buffer_head *bh;
694 struct MFT_REC *rec;
695 u16 fn, ao;
696
697 sbi->volume.blocks = dev_size >> PAGE_SHIFT;
698
699 bh = ntfs_bread(sb, 0);
700 if (!bh)
701 return -EIO;
702
703 err = -EINVAL;
704 boot = (struct NTFS_BOOT *)bh->b_data;
705
706 if (memcmp(boot->system_id, "NTFS ", sizeof("NTFS ") - 1))
707 goto out;
708
709 /* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/
710 /*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1])
711 * goto out;
712 */
713
714 boot_sector_size = (u32)boot->bytes_per_sector[1] << 8;
715 if (boot->bytes_per_sector[0] || boot_sector_size < SECTOR_SIZE ||
Kari Argillander528c9b32021-08-16 13:37:32 +0300716 !is_power_of_2(boot_sector_size)) {
Konstantin Komarov82cae262021-08-13 17:21:29 +0300717 goto out;
718 }
719
720 /* cluster size: 512, 1K, 2K, 4K, ... 2M */
721 sct_per_clst = true_sectors_per_clst(boot);
Kari Argillander528c9b32021-08-16 13:37:32 +0300722 if (!is_power_of_2(sct_per_clst))
Konstantin Komarov82cae262021-08-13 17:21:29 +0300723 goto out;
724
725 mlcn = le64_to_cpu(boot->mft_clst);
726 mlcn2 = le64_to_cpu(boot->mft2_clst);
727 sectors = le64_to_cpu(boot->sectors_per_volume);
728
729 if (mlcn * sct_per_clst >= sectors)
730 goto out;
731
732 if (mlcn2 * sct_per_clst >= sectors)
733 goto out;
734
Kari Argillandere8b8e972021-08-03 14:57:09 +0300735 /* Check MFT record size. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300736 if ((boot->record_size < 0 &&
737 SECTOR_SIZE > (2U << (-boot->record_size))) ||
Kari Argillander528c9b32021-08-16 13:37:32 +0300738 (boot->record_size >= 0 && !is_power_of_2(boot->record_size))) {
Konstantin Komarov82cae262021-08-13 17:21:29 +0300739 goto out;
740 }
741
Kari Argillandere8b8e972021-08-03 14:57:09 +0300742 /* Check index record size. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300743 if ((boot->index_size < 0 &&
744 SECTOR_SIZE > (2U << (-boot->index_size))) ||
Kari Argillander528c9b32021-08-16 13:37:32 +0300745 (boot->index_size >= 0 && !is_power_of_2(boot->index_size))) {
Konstantin Komarov82cae262021-08-13 17:21:29 +0300746 goto out;
747 }
748
749 sbi->sector_size = boot_sector_size;
750 sbi->sector_bits = blksize_bits(boot_sector_size);
751 fs_size = (sectors + 1) << sbi->sector_bits;
752
753 gb = format_size_gb(fs_size, &mb);
754
755 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300756 * - Volume formatted and mounted with the same sector size.
757 * - Volume formatted 4K and mounted as 512.
758 * - Volume formatted 512 and mounted as 4K.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300759 */
760 if (sbi->sector_size != sector_size) {
761 ntfs_warn(sb,
762 "Different NTFS' sector size and media sector size");
763 dev_size += sector_size - 1;
764 }
765
766 sbi->cluster_size = boot_sector_size * sct_per_clst;
767 sbi->cluster_bits = blksize_bits(sbi->cluster_size);
768
769 sbi->mft.lbo = mlcn << sbi->cluster_bits;
770 sbi->mft.lbo2 = mlcn2 << sbi->cluster_bits;
771
772 if (sbi->cluster_size < sbi->sector_size)
773 goto out;
774
775 sbi->cluster_mask = sbi->cluster_size - 1;
776 sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask;
777 sbi->record_size = record_size = boot->record_size < 0
778 ? 1 << (-boot->record_size)
779 : (u32)boot->record_size
780 << sbi->cluster_bits;
781
782 if (record_size > MAXIMUM_BYTES_PER_MFT)
783 goto out;
784
785 sbi->record_bits = blksize_bits(record_size);
786 sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes
787
788 sbi->max_bytes_per_attr =
Kari Argillanderfa3cacf2021-08-26 11:56:29 +0300789 record_size - ALIGN(MFTRECORD_FIXUP_OFFSET_1, 8) -
790 ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) -
791 ALIGN(sizeof(enum ATTR_TYPE), 8);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300792
793 sbi->index_size = boot->index_size < 0
794 ? 1u << (-boot->index_size)
795 : (u32)boot->index_size << sbi->cluster_bits;
796
797 sbi->volume.ser_num = le64_to_cpu(boot->serial_num);
798 sbi->volume.size = sectors << sbi->sector_bits;
799
Kari Argillandere8b8e972021-08-03 14:57:09 +0300800 /* Warning if RAW volume. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300801 if (dev_size < fs_size) {
802 u32 mb0, gb0;
803
804 gb0 = format_size_gb(dev_size, &mb0);
805 ntfs_warn(
806 sb,
807 "RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only",
808 gb, mb, gb0, mb0);
809 sb->s_flags |= SB_RDONLY;
810 }
811
812 clusters = sbi->volume.size >> sbi->cluster_bits;
813#ifndef CONFIG_NTFS3_64BIT_CLUSTER
Kari Argillandere8b8e972021-08-03 14:57:09 +0300814 /* 32 bits per cluster. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300815 if (clusters >> 32) {
816 ntfs_notice(
817 sb,
818 "NTFS %u.%02u Gb is too big to use 32 bits per cluster",
819 gb, mb);
820 goto out;
821 }
822#elif BITS_PER_LONG < 64
823#error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS"
824#endif
825
826 sbi->used.bitmap.nbits = clusters;
827
Kari Argillander195c52b2021-08-24 21:37:07 +0300828 rec = kzalloc(record_size, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300829 if (!rec) {
830 err = -ENOMEM;
831 goto out;
832 }
833
834 sbi->new_rec = rec;
835 rec->rhdr.sign = NTFS_FILE_SIGNATURE;
836 rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET_1);
837 fn = (sbi->record_size >> SECTOR_SHIFT) + 1;
838 rec->rhdr.fix_num = cpu_to_le16(fn);
Kari Argillanderfa3cacf2021-08-26 11:56:29 +0300839 ao = ALIGN(MFTRECORD_FIXUP_OFFSET_1 + sizeof(short) * fn, 8);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300840 rec->attr_off = cpu_to_le16(ao);
Kari Argillanderfa3cacf2021-08-26 11:56:29 +0300841 rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8));
Konstantin Komarov82cae262021-08-13 17:21:29 +0300842 rec->total = cpu_to_le32(sbi->record_size);
843 ((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END;
844
845 if (sbi->cluster_size < PAGE_SIZE)
846 sb_set_blocksize(sb, sbi->cluster_size);
847
848 sbi->block_mask = sb->s_blocksize - 1;
849 sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits;
850 sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits;
851
Kari Argillandere8b8e972021-08-03 14:57:09 +0300852 /* Maximum size for normal files. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300853 sbi->maxbytes = (clusters << sbi->cluster_bits) - 1;
854
855#ifdef CONFIG_NTFS3_64BIT_CLUSTER
856 if (clusters >= (1ull << (64 - sbi->cluster_bits)))
857 sbi->maxbytes = -1;
858 sbi->maxbytes_sparse = -1;
859#else
Kari Argillandere8b8e972021-08-03 14:57:09 +0300860 /* Maximum size for sparse file. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300861 sbi->maxbytes_sparse = (1ull << (sbi->cluster_bits + 32)) - 1;
862#endif
863
864 err = 0;
865
866out:
867 brelse(bh);
868
869 return err;
870}
871
Kari Argillandere8b8e972021-08-03 14:57:09 +0300872/*
873 * ntfs_fill_super - Try to mount.
874 */
Kari Argillander610f8f52021-09-07 18:35:52 +0300875static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
Konstantin Komarov82cae262021-08-13 17:21:29 +0300876{
877 int err;
Kari Argillander610f8f52021-09-07 18:35:52 +0300878 struct ntfs_sb_info *sbi = sb->s_fs_info;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300879 struct block_device *bdev = sb->s_bdev;
880 struct inode *bd_inode = bdev->bd_inode;
881 struct request_queue *rq = bdev_get_queue(bdev);
882 struct inode *inode = NULL;
883 struct ntfs_inode *ni;
884 size_t i, tt;
885 CLST vcn, lcn, len;
886 struct ATTRIB *attr;
887 const struct VOLUME_INFO *info;
888 u32 idx, done, bytes;
889 struct ATTR_DEF_ENTRY *t;
Kari Argillander27fac772021-09-07 18:35:53 +0300890 u16 *upcase;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300891 u16 *shared;
892 bool is_ro;
893 struct MFT_REF ref;
894
895 ref.high = 0;
896
Konstantin Komarov82cae262021-08-13 17:21:29 +0300897 sbi->sb = sb;
898 sb->s_flags |= SB_NODIRATIME;
899 sb->s_magic = 0x7366746e; // "ntfs"
900 sb->s_op = &ntfs_sops;
901 sb->s_export_op = &ntfs_export_ops;
902 sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec
903 sb->s_xattr = ntfs_xattr_handlers;
904
Kari Argillander610f8f52021-09-07 18:35:52 +0300905 sbi->options->nls = ntfs_load_nls(sbi->options->nls_name);
906 if (IS_ERR(sbi->options->nls)) {
907 sbi->options->nls = NULL;
908 errorf(fc, "Cannot load nls %s", sbi->options->nls_name);
909 return -EINVAL;
910 }
Konstantin Komarov82cae262021-08-13 17:21:29 +0300911
912 if (!rq || !blk_queue_discard(rq) || !rq->limits.discard_granularity) {
913 ;
914 } else {
915 sbi->discard_granularity = rq->limits.discard_granularity;
916 sbi->discard_granularity_mask_inv =
917 ~(u64)(sbi->discard_granularity - 1);
918 }
919
920 sb_set_blocksize(sb, PAGE_SIZE);
921
Kari Argillandere8b8e972021-08-03 14:57:09 +0300922 /* Parse boot. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300923 err = ntfs_init_from_boot(sb, rq ? queue_logical_block_size(rq) : 512,
924 bd_inode->i_size);
925 if (err)
926 goto out;
927
928#ifdef CONFIG_NTFS3_64BIT_CLUSTER
929 sb->s_maxbytes = MAX_LFS_FILESIZE;
930#else
931 sb->s_maxbytes = 0xFFFFFFFFull << sbi->cluster_bits;
932#endif
933
Konstantin Komarov82cae262021-08-13 17:21:29 +0300934 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300935 * Load $Volume. This should be done before $LogFile
936 * 'cause 'sbi->volume.ni' is used 'ntfs_set_state'.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300937 */
938 ref.low = cpu_to_le32(MFT_REC_VOL);
939 ref.seq = cpu_to_le16(MFT_REC_VOL);
940 inode = ntfs_iget5(sb, &ref, &NAME_VOLUME);
941 if (IS_ERR(inode)) {
942 err = PTR_ERR(inode);
943 ntfs_err(sb, "Failed to load $Volume.");
944 inode = NULL;
945 goto out;
946 }
947
948 ni = ntfs_i(inode);
949
Kari Argillandere8b8e972021-08-03 14:57:09 +0300950 /* Load and save label (not necessary). */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300951 attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL);
952
953 if (!attr) {
954 /* It is ok if no ATTR_LABEL */
955 } else if (!attr->non_res && !is_attr_ext(attr)) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300956 /* $AttrDef allows labels to be up to 128 symbols. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300957 err = utf16s_to_utf8s(resident_data(attr),
958 le32_to_cpu(attr->res.data_size) >> 1,
959 UTF16_LITTLE_ENDIAN, sbi->volume.label,
960 sizeof(sbi->volume.label));
961 if (err < 0)
962 sbi->volume.label[0] = 0;
963 } else {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300964 /* Should we break mounting here? */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300965 //err = -EINVAL;
966 //goto out;
967 }
968
969 attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL);
970 if (!attr || is_attr_ext(attr)) {
971 err = -EINVAL;
972 goto out;
973 }
974
975 info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO);
976 if (!info) {
977 err = -EINVAL;
978 goto out;
979 }
980
981 sbi->volume.major_ver = info->major_ver;
982 sbi->volume.minor_ver = info->minor_ver;
983 sbi->volume.flags = info->flags;
984
985 sbi->volume.ni = ni;
986 inode = NULL;
987
Kari Argillandere8b8e972021-08-03 14:57:09 +0300988 /* Load $MFTMirr to estimate recs_mirr. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300989 ref.low = cpu_to_le32(MFT_REC_MIRR);
990 ref.seq = cpu_to_le16(MFT_REC_MIRR);
991 inode = ntfs_iget5(sb, &ref, &NAME_MIRROR);
992 if (IS_ERR(inode)) {
993 err = PTR_ERR(inode);
994 ntfs_err(sb, "Failed to load $MFTMirr.");
995 inode = NULL;
996 goto out;
997 }
998
999 sbi->mft.recs_mirr =
1000 ntfs_up_cluster(sbi, inode->i_size) >> sbi->record_bits;
1001
1002 iput(inode);
1003
Konstantin Komarovd3624462021-08-31 16:57:40 +03001004 /* Load LogFile to replay. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001005 ref.low = cpu_to_le32(MFT_REC_LOG);
1006 ref.seq = cpu_to_le16(MFT_REC_LOG);
1007 inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE);
1008 if (IS_ERR(inode)) {
1009 err = PTR_ERR(inode);
1010 ntfs_err(sb, "Failed to load \x24LogFile.");
1011 inode = NULL;
1012 goto out;
1013 }
1014
1015 ni = ntfs_i(inode);
1016
1017 err = ntfs_loadlog_and_replay(ni, sbi);
1018 if (err)
1019 goto out;
1020
1021 iput(inode);
1022 inode = NULL;
1023
1024 is_ro = sb_rdonly(sbi->sb);
1025
1026 if (sbi->flags & NTFS_FLAGS_NEED_REPLAY) {
1027 if (!is_ro) {
1028 ntfs_warn(sb,
1029 "failed to replay log file. Can't mount rw!");
1030 err = -EINVAL;
1031 goto out;
1032 }
1033 } else if (sbi->volume.flags & VOLUME_FLAG_DIRTY) {
Kari Argillander564c97b2021-09-07 18:35:51 +03001034 if (!is_ro && !sbi->options->force) {
Konstantin Komarov82cae262021-08-13 17:21:29 +03001035 ntfs_warn(
1036 sb,
1037 "volume is dirty and \"force\" flag is not set!");
1038 err = -EINVAL;
1039 goto out;
1040 }
1041 }
1042
Kari Argillandere8b8e972021-08-03 14:57:09 +03001043 /* Load $MFT. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001044 ref.low = cpu_to_le32(MFT_REC_MFT);
1045 ref.seq = cpu_to_le16(1);
1046
1047 inode = ntfs_iget5(sb, &ref, &NAME_MFT);
1048 if (IS_ERR(inode)) {
1049 err = PTR_ERR(inode);
1050 ntfs_err(sb, "Failed to load $MFT.");
1051 inode = NULL;
1052 goto out;
1053 }
1054
1055 ni = ntfs_i(inode);
1056
1057 sbi->mft.used = ni->i_valid >> sbi->record_bits;
1058 tt = inode->i_size >> sbi->record_bits;
1059 sbi->mft.next_free = MFT_REC_USER;
1060
1061 err = wnd_init(&sbi->mft.bitmap, sb, tt);
1062 if (err)
1063 goto out;
1064
1065 err = ni_load_all_mi(ni);
1066 if (err)
1067 goto out;
1068
1069 sbi->mft.ni = ni;
1070
Kari Argillandere8b8e972021-08-03 14:57:09 +03001071 /* Load $BadClus. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001072 ref.low = cpu_to_le32(MFT_REC_BADCLUST);
1073 ref.seq = cpu_to_le16(MFT_REC_BADCLUST);
1074 inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS);
1075 if (IS_ERR(inode)) {
1076 err = PTR_ERR(inode);
1077 ntfs_err(sb, "Failed to load $BadClus.");
1078 inode = NULL;
1079 goto out;
1080 }
1081
1082 ni = ntfs_i(inode);
1083
1084 for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) {
1085 if (lcn == SPARSE_LCN)
1086 continue;
1087
1088 if (!sbi->bad_clusters)
1089 ntfs_notice(sb, "Volume contains bad blocks");
1090
1091 sbi->bad_clusters += len;
1092 }
1093
1094 iput(inode);
1095
Kari Argillandere8b8e972021-08-03 14:57:09 +03001096 /* Load $Bitmap. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001097 ref.low = cpu_to_le32(MFT_REC_BITMAP);
1098 ref.seq = cpu_to_le16(MFT_REC_BITMAP);
1099 inode = ntfs_iget5(sb, &ref, &NAME_BITMAP);
1100 if (IS_ERR(inode)) {
1101 err = PTR_ERR(inode);
1102 ntfs_err(sb, "Failed to load $Bitmap.");
1103 inode = NULL;
1104 goto out;
1105 }
1106
1107 ni = ntfs_i(inode);
1108
1109#ifndef CONFIG_NTFS3_64BIT_CLUSTER
1110 if (inode->i_size >> 32) {
1111 err = -EINVAL;
1112 goto out;
1113 }
1114#endif
1115
Kari Argillandere8b8e972021-08-03 14:57:09 +03001116 /* Check bitmap boundary. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001117 tt = sbi->used.bitmap.nbits;
1118 if (inode->i_size < bitmap_size(tt)) {
1119 err = -EINVAL;
1120 goto out;
1121 }
1122
Kari Argillandere8b8e972021-08-03 14:57:09 +03001123 /* Not necessary. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001124 sbi->used.bitmap.set_tail = true;
1125 err = wnd_init(&sbi->used.bitmap, sbi->sb, tt);
1126 if (err)
1127 goto out;
1128
1129 iput(inode);
1130
Kari Argillandere8b8e972021-08-03 14:57:09 +03001131 /* Compute the MFT zone. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001132 err = ntfs_refresh_zone(sbi);
1133 if (err)
1134 goto out;
1135
Kari Argillandere8b8e972021-08-03 14:57:09 +03001136 /* Load $AttrDef. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001137 ref.low = cpu_to_le32(MFT_REC_ATTR);
1138 ref.seq = cpu_to_le16(MFT_REC_ATTR);
1139 inode = ntfs_iget5(sbi->sb, &ref, &NAME_ATTRDEF);
1140 if (IS_ERR(inode)) {
1141 err = PTR_ERR(inode);
1142 ntfs_err(sb, "Failed to load $AttrDef -> %d", err);
1143 inode = NULL;
1144 goto out;
1145 }
1146
1147 if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY)) {
1148 err = -EINVAL;
1149 goto out;
1150 }
1151 bytes = inode->i_size;
Kari Argillander195c52b2021-08-24 21:37:07 +03001152 sbi->def_table = t = kmalloc(bytes, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001153 if (!t) {
1154 err = -ENOMEM;
1155 goto out;
1156 }
1157
1158 for (done = idx = 0; done < bytes; done += PAGE_SIZE, idx++) {
1159 unsigned long tail = bytes - done;
1160 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1161
1162 if (IS_ERR(page)) {
1163 err = PTR_ERR(page);
1164 goto out;
1165 }
1166 memcpy(Add2Ptr(t, done), page_address(page),
1167 min(PAGE_SIZE, tail));
1168 ntfs_unmap_page(page);
1169
1170 if (!idx && ATTR_STD != t->type) {
1171 err = -EINVAL;
1172 goto out;
1173 }
1174 }
1175
1176 t += 1;
1177 sbi->def_entries = 1;
1178 done = sizeof(struct ATTR_DEF_ENTRY);
1179 sbi->reparse.max_size = MAXIMUM_REPARSE_DATA_BUFFER_SIZE;
Colin Ian Kingf8d87ed2021-08-16 11:13:08 +01001180 sbi->ea_max_size = 0x10000; /* default formatter value */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001181
1182 while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) {
1183 u32 t32 = le32_to_cpu(t->type);
1184 u64 sz = le64_to_cpu(t->max_sz);
1185
1186 if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32)
1187 break;
1188
1189 if (t->type == ATTR_REPARSE)
1190 sbi->reparse.max_size = sz;
1191 else if (t->type == ATTR_EA)
1192 sbi->ea_max_size = sz;
1193
1194 done += sizeof(struct ATTR_DEF_ENTRY);
1195 t += 1;
1196 sbi->def_entries += 1;
1197 }
1198 iput(inode);
1199
Kari Argillandere8b8e972021-08-03 14:57:09 +03001200 /* Load $UpCase. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001201 ref.low = cpu_to_le32(MFT_REC_UPCASE);
1202 ref.seq = cpu_to_le16(MFT_REC_UPCASE);
1203 inode = ntfs_iget5(sb, &ref, &NAME_UPCASE);
1204 if (IS_ERR(inode)) {
1205 err = PTR_ERR(inode);
1206 ntfs_err(sb, "Failed to load \x24LogFile.");
1207 inode = NULL;
1208 goto out;
1209 }
1210
1211 ni = ntfs_i(inode);
1212
1213 if (inode->i_size != 0x10000 * sizeof(short)) {
1214 err = -EINVAL;
1215 goto out;
1216 }
1217
Kari Argillander27fac772021-09-07 18:35:53 +03001218 upcase = sbi->upcase;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001219
1220 for (idx = 0; idx < (0x10000 * sizeof(short) >> PAGE_SHIFT); idx++) {
1221 const __le16 *src;
1222 u16 *dst = Add2Ptr(upcase, idx << PAGE_SHIFT);
1223 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1224
1225 if (IS_ERR(page)) {
1226 err = PTR_ERR(page);
1227 goto out;
1228 }
1229
1230 src = page_address(page);
1231
1232#ifdef __BIG_ENDIAN
1233 for (i = 0; i < PAGE_SIZE / sizeof(u16); i++)
1234 *dst++ = le16_to_cpu(*src++);
1235#else
1236 memcpy(dst, src, PAGE_SIZE);
1237#endif
1238 ntfs_unmap_page(page);
1239 }
1240
1241 shared = ntfs_set_shared(upcase, 0x10000 * sizeof(short));
1242 if (shared && upcase != shared) {
1243 sbi->upcase = shared;
Kari Argillander195c52b2021-08-24 21:37:07 +03001244 kvfree(upcase);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001245 }
1246
1247 iput(inode);
1248 inode = NULL;
1249
1250 if (is_ntfs3(sbi)) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001251 /* Load $Secure. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001252 err = ntfs_security_init(sbi);
1253 if (err)
1254 goto out;
1255
Kari Argillandere8b8e972021-08-03 14:57:09 +03001256 /* Load $Extend. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001257 err = ntfs_extend_init(sbi);
1258 if (err)
1259 goto load_root;
1260
Kari Argillandere8b8e972021-08-03 14:57:09 +03001261 /* Load $Extend\$Reparse. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001262 err = ntfs_reparse_init(sbi);
1263 if (err)
1264 goto load_root;
1265
Kari Argillandere8b8e972021-08-03 14:57:09 +03001266 /* Load $Extend\$ObjId. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001267 err = ntfs_objid_init(sbi);
1268 if (err)
1269 goto load_root;
1270 }
1271
1272load_root:
Kari Argillandere8b8e972021-08-03 14:57:09 +03001273 /* Load root. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001274 ref.low = cpu_to_le32(MFT_REC_ROOT);
1275 ref.seq = cpu_to_le16(MFT_REC_ROOT);
1276 inode = ntfs_iget5(sb, &ref, &NAME_ROOT);
1277 if (IS_ERR(inode)) {
1278 err = PTR_ERR(inode);
1279 ntfs_err(sb, "Failed to load root.");
1280 inode = NULL;
1281 goto out;
1282 }
1283
1284 ni = ntfs_i(inode);
1285
1286 sb->s_root = d_make_root(inode);
1287
1288 if (!sb->s_root) {
1289 err = -EINVAL;
1290 goto out;
1291 }
1292
Kari Argillander610f8f52021-09-07 18:35:52 +03001293 fc->fs_private = NULL;
1294 fc->s_fs_info = NULL;
1295
Konstantin Komarov82cae262021-08-13 17:21:29 +03001296 return 0;
1297
1298out:
1299 iput(inode);
1300
1301 if (sb->s_root) {
1302 d_drop(sb->s_root);
1303 sb->s_root = NULL;
1304 }
1305
Konstantin Komarov82cae262021-08-13 17:21:29 +03001306 return err;
1307}
1308
1309void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len)
1310{
1311 struct ntfs_sb_info *sbi = sb->s_fs_info;
1312 struct block_device *bdev = sb->s_bdev;
1313 sector_t devblock = (u64)lcn * sbi->blocks_per_cluster;
1314 unsigned long blocks = (u64)len * sbi->blocks_per_cluster;
1315 unsigned long cnt = 0;
1316 unsigned long limit = global_zone_page_state(NR_FREE_PAGES)
1317 << (PAGE_SHIFT - sb->s_blocksize_bits);
1318
1319 if (limit >= 0x2000)
1320 limit -= 0x1000;
1321 else if (limit < 32)
1322 limit = 32;
1323 else
1324 limit >>= 1;
1325
1326 while (blocks--) {
1327 clean_bdev_aliases(bdev, devblock++, 1);
1328 if (cnt++ >= limit) {
1329 sync_blockdev(bdev);
1330 cnt = 0;
1331 }
1332 }
1333}
1334
1335/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001336 * ntfs_discard - Issue a discard request (trim for SSD).
Konstantin Komarov82cae262021-08-13 17:21:29 +03001337 */
1338int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len)
1339{
1340 int err;
1341 u64 lbo, bytes, start, end;
1342 struct super_block *sb;
1343
1344 if (sbi->used.next_free_lcn == lcn + len)
1345 sbi->used.next_free_lcn = lcn;
1346
1347 if (sbi->flags & NTFS_FLAGS_NODISCARD)
1348 return -EOPNOTSUPP;
1349
Kari Argillander564c97b2021-09-07 18:35:51 +03001350 if (!sbi->options->discard)
Konstantin Komarov82cae262021-08-13 17:21:29 +03001351 return -EOPNOTSUPP;
1352
1353 lbo = (u64)lcn << sbi->cluster_bits;
1354 bytes = (u64)len << sbi->cluster_bits;
1355
Kari Argillandere8b8e972021-08-03 14:57:09 +03001356 /* Align up 'start' on discard_granularity. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001357 start = (lbo + sbi->discard_granularity - 1) &
1358 sbi->discard_granularity_mask_inv;
Kari Argillandere8b8e972021-08-03 14:57:09 +03001359 /* Align down 'end' on discard_granularity. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001360 end = (lbo + bytes) & sbi->discard_granularity_mask_inv;
1361
1362 sb = sbi->sb;
1363 if (start >= end)
1364 return 0;
1365
1366 err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9,
1367 GFP_NOFS, 0);
1368
1369 if (err == -EOPNOTSUPP)
1370 sbi->flags |= NTFS_FLAGS_NODISCARD;
1371
1372 return err;
1373}
1374
Kari Argillander610f8f52021-09-07 18:35:52 +03001375static int ntfs_fs_get_tree(struct fs_context *fc)
Konstantin Komarov82cae262021-08-13 17:21:29 +03001376{
Kari Argillander610f8f52021-09-07 18:35:52 +03001377 return get_tree_bdev(fc, ntfs_fill_super);
1378}
1379
1380/*
1381 * ntfs_fs_free - Free fs_context.
1382 *
1383 * Note that this will be called after fill_super and reconfigure
1384 * even when they pass. So they have to take pointers if they pass.
1385 */
1386static void ntfs_fs_free(struct fs_context *fc)
1387{
1388 struct ntfs_mount_options *opts = fc->fs_private;
1389 struct ntfs_sb_info *sbi = fc->s_fs_info;
1390
1391 if (sbi)
1392 put_ntfs(sbi);
1393
1394 if (opts)
1395 put_mount_options(opts);
1396}
1397
1398static const struct fs_context_operations ntfs_context_ops = {
1399 .parse_param = ntfs_fs_parse_param,
1400 .get_tree = ntfs_fs_get_tree,
1401 .reconfigure = ntfs_fs_reconfigure,
1402 .free = ntfs_fs_free,
1403};
1404
1405/*
1406 * ntfs_init_fs_context - Initialize spi and opts
1407 *
1408 * This will called when mount/remount. We will first initiliaze
1409 * options so that if remount we can use just that.
1410 */
1411static int ntfs_init_fs_context(struct fs_context *fc)
1412{
1413 struct ntfs_mount_options *opts;
1414 struct ntfs_sb_info *sbi;
1415
1416 opts = kzalloc(sizeof(struct ntfs_mount_options), GFP_NOFS);
1417 if (!opts)
1418 return -ENOMEM;
1419
1420 /* Default options. */
1421 opts->fs_uid = current_uid();
1422 opts->fs_gid = current_gid();
1423 opts->fs_fmask_inv = ~current_umask();
1424 opts->fs_dmask_inv = ~current_umask();
1425
1426 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
1427 goto ok;
1428
1429 sbi = kzalloc(sizeof(struct ntfs_sb_info), GFP_NOFS);
Kari Argillander27fac772021-09-07 18:35:53 +03001430 if (!sbi)
1431 goto free_opts;
1432
1433 sbi->upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL);
1434 if (!sbi->upcase)
1435 goto free_sbi;
1436
1437 ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1438 DEFAULT_RATELIMIT_BURST);
1439
1440 mutex_init(&sbi->compress.mtx_lznt);
1441#ifdef CONFIG_NTFS3_LZX_XPRESS
1442 mutex_init(&sbi->compress.mtx_xpress);
1443 mutex_init(&sbi->compress.mtx_lzx);
1444#endif
Kari Argillander610f8f52021-09-07 18:35:52 +03001445
1446 sbi->options = opts;
1447 fc->s_fs_info = sbi;
1448ok:
1449 fc->fs_private = opts;
1450 fc->ops = &ntfs_context_ops;
1451
1452 return 0;
Kari Argillander27fac772021-09-07 18:35:53 +03001453free_opts:
1454 kfree(opts);
1455free_sbi:
1456 kfree(sbi);
1457 return -ENOMEM;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001458}
1459
1460// clang-format off
1461static struct file_system_type ntfs_fs_type = {
Kari Argillander610f8f52021-09-07 18:35:52 +03001462 .owner = THIS_MODULE,
1463 .name = "ntfs3",
1464 .init_fs_context = ntfs_init_fs_context,
1465 .parameters = ntfs_fs_parameters,
1466 .kill_sb = kill_block_super,
1467 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
Konstantin Komarov82cae262021-08-13 17:21:29 +03001468};
1469// clang-format on
1470
1471static int __init init_ntfs_fs(void)
1472{
1473 int err;
1474
Kari Argillander2e3a51b2021-08-29 17:42:39 +03001475 pr_info("ntfs3: Max link count %u\n", NTFS_LINK_MAX);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001476
Kari Argillander2e3a51b2021-08-29 17:42:39 +03001477 if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL))
1478 pr_info("ntfs3: Enabled Linux POSIX ACLs support\n");
1479 if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER))
1480 pr_notice("ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n");
1481 if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS))
1482 pr_info("ntfs3: Read-only LZX/Xpress compression included\n");
Konstantin Komarov82cae262021-08-13 17:21:29 +03001483
1484 err = ntfs3_init_bitmap();
1485 if (err)
1486 return err;
1487
1488 ntfs_inode_cachep = kmem_cache_create(
1489 "ntfs_inode_cache", sizeof(struct ntfs_inode), 0,
1490 (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1491 init_once);
1492 if (!ntfs_inode_cachep) {
1493 err = -ENOMEM;
1494 goto out1;
1495 }
1496
1497 err = register_filesystem(&ntfs_fs_type);
1498 if (err)
1499 goto out;
1500
1501 return 0;
1502out:
1503 kmem_cache_destroy(ntfs_inode_cachep);
1504out1:
1505 ntfs3_exit_bitmap();
1506 return err;
1507}
1508
1509static void __exit exit_ntfs_fs(void)
1510{
1511 if (ntfs_inode_cachep) {
1512 rcu_barrier();
1513 kmem_cache_destroy(ntfs_inode_cachep);
1514 }
1515
1516 unregister_filesystem(&ntfs_fs_type);
1517 ntfs3_exit_bitmap();
1518}
1519
1520MODULE_LICENSE("GPL");
1521MODULE_DESCRIPTION("ntfs3 read/write filesystem");
Konstantin Komarov82cae262021-08-13 17:21:29 +03001522#ifdef CONFIG_NTFS3_FS_POSIX_ACL
1523MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support");
1524#endif
1525#ifdef CONFIG_NTFS3_64BIT_CLUSTER
Kari Argillander2e3a51b2021-08-29 17:42:39 +03001526MODULE_INFO(cluster, "Warning: Activated 64 bits per cluster. Windows does not support this");
Konstantin Komarov82cae262021-08-13 17:21:29 +03001527#endif
1528#ifdef CONFIG_NTFS3_LZX_XPRESS
1529MODULE_INFO(compression, "Read-only lzx/xpress compression included");
1530#endif
1531
1532MODULE_AUTHOR("Konstantin Komarov");
1533MODULE_ALIAS_FS("ntfs3");
1534
1535module_init(init_ntfs_fs);
1536module_exit(exit_ntfs_fs);