blob: ef4ea3f21905fc12ccb6f0f57d9d10626b1e1d27 [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 *
Konstantin Komarovbd6ae042022-09-09 19:12:31 +030024 * ntfs allows up to 2^64 clusters per volume.
25 * It means you should use 64 bits lcn to operate with ntfs.
26 * Implementation of ntfs.sys uses only 32 bits lcn.
27 * Default ntfs3 uses 32 bits lcn too.
28 * ntfs3 built with CONFIG_NTFS3_64BIT_CLUSTER (ntfs3_64) uses 64 bits per lcn.
29 *
30 *
31 * ntfs limits, cluster size is 4K (2^12)
32 * -----------------------------------------------------------------------------
33 * | Volume size | Clusters | ntfs.sys | ntfs3 | ntfs3_64 | mkntfs | chkdsk |
34 * -----------------------------------------------------------------------------
35 * | < 16T, 2^44 | < 2^32 | yes | yes | yes | yes | yes |
36 * | > 16T, 2^44 | > 2^32 | no | no | yes | yes | yes |
37 * ----------------------------------------------------------|------------------
38 *
39 * To mount large volumes as ntfs one should use large cluster size (up to 2M)
40 * The maximum volume size in this case is 2^32 * 2^21 = 2^53 = 8P
41 *
42 * ntfs limits, cluster size is 2M (2^31)
43 * -----------------------------------------------------------------------------
44 * | < 8P, 2^54 | < 2^32 | yes | yes | yes | yes | yes |
45 * | > 8P, 2^54 | > 2^32 | no | no | yes | yes | yes |
46 * ----------------------------------------------------------|------------------
47 *
Konstantin Komarov82cae262021-08-13 17:21:29 +030048 */
49
Konstantin Komarov82cae262021-08-13 17:21:29 +030050#include <linux/blkdev.h>
51#include <linux/buffer_head.h>
52#include <linux/exportfs.h>
53#include <linux/fs.h>
Kari Argillander610f8f52021-09-07 18:35:52 +030054#include <linux/fs_context.h>
55#include <linux/fs_parser.h>
Kari Argillander528c9b32021-08-16 13:37:32 +030056#include <linux/log2.h>
Konstantin Komarovcd399812022-05-11 19:58:36 +030057#include <linux/minmax.h>
Konstantin Komarov82cae262021-08-13 17:21:29 +030058#include <linux/module.h>
59#include <linux/nls.h>
Konstantin Komarov82cae262021-08-13 17:21:29 +030060#include <linux/seq_file.h>
61#include <linux/statfs.h>
62
63#include "debug.h"
64#include "ntfs.h"
65#include "ntfs_fs.h"
66#ifdef CONFIG_NTFS3_LZX_XPRESS
67#include "lib/lib.h"
68#endif
69
70#ifdef CONFIG_PRINTK
71/*
Kari Argillandere8b8e972021-08-03 14:57:09 +030072 * ntfs_printk - Trace warnings/notices/errors.
73 *
Konstantin Komarov82cae262021-08-13 17:21:29 +030074 * Thanks Joe Perches <joe@perches.com> for implementation
75 */
76void ntfs_printk(const struct super_block *sb, const char *fmt, ...)
77{
78 struct va_format vaf;
79 va_list args;
80 int level;
81 struct ntfs_sb_info *sbi = sb->s_fs_info;
82
Kari Argillandere8b8e972021-08-03 14:57:09 +030083 /* Should we use different ratelimits for warnings/notices/errors? */
Konstantin Komarov82cae262021-08-13 17:21:29 +030084 if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
85 return;
86
87 va_start(args, fmt);
88
89 level = printk_get_level(fmt);
90 vaf.fmt = printk_skip_level(fmt);
91 vaf.va = &args;
92 printk("%c%cntfs3: %s: %pV\n", KERN_SOH_ASCII, level, sb->s_id, &vaf);
93
94 va_end(args);
95}
96
97static char s_name_buf[512];
Kari Argillandere8b8e972021-08-03 14:57:09 +030098static atomic_t s_name_buf_cnt = ATOMIC_INIT(1); // 1 means 'free s_name_buf'.
Konstantin Komarov82cae262021-08-13 17:21:29 +030099
Kari Argillandere8b8e972021-08-03 14:57:09 +0300100/*
101 * ntfs_inode_printk
102 *
103 * Print warnings/notices/errors about inode using name or inode number.
104 */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300105void ntfs_inode_printk(struct inode *inode, const char *fmt, ...)
106{
107 struct super_block *sb = inode->i_sb;
108 struct ntfs_sb_info *sbi = sb->s_fs_info;
109 char *name;
110 va_list args;
111 struct va_format vaf;
112 int level;
113
114 if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
115 return;
116
Kari Argillandere8b8e972021-08-03 14:57:09 +0300117 /* Use static allocated buffer, if possible. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300118 name = atomic_dec_and_test(&s_name_buf_cnt)
119 ? s_name_buf
120 : kmalloc(sizeof(s_name_buf), GFP_NOFS);
121
122 if (name) {
123 struct dentry *de = d_find_alias(inode);
124 const u32 name_len = ARRAY_SIZE(s_name_buf) - 1;
125
126 if (de) {
127 spin_lock(&de->d_lock);
128 snprintf(name, name_len, " \"%s\"", de->d_name.name);
129 spin_unlock(&de->d_lock);
Kari Argillandere8b8e972021-08-03 14:57:09 +0300130 name[name_len] = 0; /* To be sure. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300131 } else {
132 name[0] = 0;
133 }
Kari Argillandere8b8e972021-08-03 14:57:09 +0300134 dput(de); /* Cocci warns if placed in branch "if (de)" */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300135 }
136
137 va_start(args, fmt);
138
139 level = printk_get_level(fmt);
140 vaf.fmt = printk_skip_level(fmt);
141 vaf.va = &args;
142
143 printk("%c%cntfs3: %s: ino=%lx,%s %pV\n", KERN_SOH_ASCII, level,
144 sb->s_id, inode->i_ino, name ? name : "", &vaf);
145
146 va_end(args);
147
148 atomic_inc(&s_name_buf_cnt);
149 if (name != s_name_buf)
150 kfree(name);
151}
152#endif
153
154/*
155 * Shared memory struct.
156 *
Kari Argillandere8b8e972021-08-03 14:57:09 +0300157 * On-disk ntfs's upcase table is created by ntfs formatter.
158 * 'upcase' table is 128K bytes of memory.
159 * We should read it into memory when mounting.
160 * Several ntfs volumes likely use the same 'upcase' table.
161 * It is good idea to share in-memory 'upcase' table between different volumes.
162 * Unfortunately winxp/vista/win7 use different upcase tables.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300163 */
164static DEFINE_SPINLOCK(s_shared_lock);
165
166static struct {
167 void *ptr;
168 u32 len;
169 int cnt;
170} s_shared[8];
171
172/*
173 * ntfs_set_shared
174 *
Kari Argillandere8b8e972021-08-03 14:57:09 +0300175 * Return:
176 * * @ptr - If pointer was saved in shared memory.
177 * * NULL - If pointer was not shared.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300178 */
179void *ntfs_set_shared(void *ptr, u32 bytes)
180{
181 void *ret = NULL;
182 int i, j = -1;
183
184 spin_lock(&s_shared_lock);
185 for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
186 if (!s_shared[i].cnt) {
187 j = i;
188 } else if (bytes == s_shared[i].len &&
189 !memcmp(s_shared[i].ptr, ptr, bytes)) {
190 s_shared[i].cnt += 1;
191 ret = s_shared[i].ptr;
192 break;
193 }
194 }
195
196 if (!ret && j != -1) {
197 s_shared[j].ptr = ptr;
198 s_shared[j].len = bytes;
199 s_shared[j].cnt = 1;
200 ret = ptr;
201 }
202 spin_unlock(&s_shared_lock);
203
204 return ret;
205}
206
207/*
208 * ntfs_put_shared
209 *
Kari Argillandere8b8e972021-08-03 14:57:09 +0300210 * Return:
211 * * @ptr - If pointer is not shared anymore.
212 * * NULL - If pointer is still shared.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300213 */
214void *ntfs_put_shared(void *ptr)
215{
216 void *ret = ptr;
217 int i;
218
219 spin_lock(&s_shared_lock);
220 for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
221 if (s_shared[i].cnt && s_shared[i].ptr == ptr) {
222 if (--s_shared[i].cnt)
223 ret = NULL;
224 break;
225 }
226 }
227 spin_unlock(&s_shared_lock);
228
229 return ret;
230}
231
Kari Argillander610f8f52021-09-07 18:35:52 +0300232static inline void put_mount_options(struct ntfs_mount_options *options)
Konstantin Komarov82cae262021-08-13 17:21:29 +0300233{
Kari Argillander610f8f52021-09-07 18:35:52 +0300234 kfree(options->nls_name);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300235 unload_nls(options->nls);
Kari Argillander610f8f52021-09-07 18:35:52 +0300236 kfree(options);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300237}
238
239enum Opt {
240 Opt_uid,
241 Opt_gid,
242 Opt_umask,
243 Opt_dmask,
244 Opt_fmask,
245 Opt_immutable,
246 Opt_discard,
247 Opt_force,
248 Opt_sparse,
249 Opt_nohidden,
Konstantin Komarov098250d2022-09-12 18:28:51 +0300250 Opt_hide_dot_files,
Daniel Pinto1d07a9d2022-10-10 12:14:31 +0100251 Opt_windows_names,
Konstantin Komarov82cae262021-08-13 17:21:29 +0300252 Opt_showmeta,
253 Opt_acl,
Kari Argillandere274cde2021-09-07 18:35:55 +0300254 Opt_iocharset,
Konstantin Komarov82cae262021-08-13 17:21:29 +0300255 Opt_prealloc,
Kari Argillander28a941f2021-09-07 18:35:56 +0300256 Opt_noacsrules,
Konstantin Komarova3a956c2022-09-23 12:42:18 +0300257 Opt_nocase,
Konstantin Komarov82cae262021-08-13 17:21:29 +0300258 Opt_err,
259};
260
Kari Argillander610f8f52021-09-07 18:35:52 +0300261static const struct fs_parameter_spec ntfs_fs_parameters[] = {
262 fsparam_u32("uid", Opt_uid),
263 fsparam_u32("gid", Opt_gid),
264 fsparam_u32oct("umask", Opt_umask),
265 fsparam_u32oct("dmask", Opt_dmask),
266 fsparam_u32oct("fmask", Opt_fmask),
267 fsparam_flag_no("sys_immutable", Opt_immutable),
268 fsparam_flag_no("discard", Opt_discard),
269 fsparam_flag_no("force", Opt_force),
270 fsparam_flag_no("sparse", Opt_sparse),
Kari Argillander9d1939f2021-09-07 18:35:54 +0300271 fsparam_flag_no("hidden", Opt_nohidden),
Daniel Pintodc0fcc92022-10-10 12:37:13 +0100272 fsparam_flag_no("hide_dot_files", Opt_hide_dot_files),
Daniel Pinto1d07a9d2022-10-10 12:14:31 +0100273 fsparam_flag_no("windows_names", Opt_windows_names),
Kari Argillander610f8f52021-09-07 18:35:52 +0300274 fsparam_flag_no("acl", Opt_acl),
275 fsparam_flag_no("showmeta", Opt_showmeta),
Kari Argillander610f8f52021-09-07 18:35:52 +0300276 fsparam_flag_no("prealloc", Opt_prealloc),
Kari Argillander28a941f2021-09-07 18:35:56 +0300277 fsparam_flag_no("acsrules", Opt_noacsrules),
Konstantin Komarova3a956c2022-09-23 12:42:18 +0300278 fsparam_flag_no("nocase", Opt_nocase),
Kari Argillandere274cde2021-09-07 18:35:55 +0300279 fsparam_string("iocharset", Opt_iocharset),
Kari Argillander610f8f52021-09-07 18:35:52 +0300280 {}
Konstantin Komarov82cae262021-08-13 17:21:29 +0300281};
282
Kari Argillander610f8f52021-09-07 18:35:52 +0300283/*
284 * Load nls table or if @nls is utf8 then return NULL.
285 */
286static struct nls_table *ntfs_load_nls(char *nls)
Konstantin Komarov82cae262021-08-13 17:21:29 +0300287{
Kari Argillander610f8f52021-09-07 18:35:52 +0300288 struct nls_table *ret;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300289
Kari Argillander610f8f52021-09-07 18:35:52 +0300290 if (!nls)
291 nls = CONFIG_NLS_DEFAULT;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300292
Kari Argillander610f8f52021-09-07 18:35:52 +0300293 if (strcmp(nls, "utf8") == 0)
294 return NULL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300295
Kari Argillander610f8f52021-09-07 18:35:52 +0300296 if (strcmp(nls, CONFIG_NLS_DEFAULT) == 0)
297 return load_nls_default();
Konstantin Komarov82cae262021-08-13 17:21:29 +0300298
Kari Argillander610f8f52021-09-07 18:35:52 +0300299 ret = load_nls(nls);
300 if (ret)
301 return ret;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300302
Kari Argillander610f8f52021-09-07 18:35:52 +0300303 return ERR_PTR(-EINVAL);
304}
305
306static int ntfs_fs_parse_param(struct fs_context *fc,
307 struct fs_parameter *param)
308{
309 struct ntfs_mount_options *opts = fc->fs_private;
310 struct fs_parse_result result;
311 int opt;
312
313 opt = fs_parse(fc, ntfs_fs_parameters, param, &result);
314 if (opt < 0)
315 return opt;
316
317 switch (opt) {
318 case Opt_uid:
319 opts->fs_uid = make_kuid(current_user_ns(), result.uint_32);
320 if (!uid_valid(opts->fs_uid))
321 return invalf(fc, "ntfs3: Invalid value for uid.");
Kari Argillander610f8f52021-09-07 18:35:52 +0300322 break;
323 case Opt_gid:
324 opts->fs_gid = make_kgid(current_user_ns(), result.uint_32);
325 if (!gid_valid(opts->fs_gid))
326 return invalf(fc, "ntfs3: Invalid value for gid.");
Kari Argillander610f8f52021-09-07 18:35:52 +0300327 break;
328 case Opt_umask:
329 if (result.uint_32 & ~07777)
330 return invalf(fc, "ntfs3: Invalid value for umask.");
331 opts->fs_fmask_inv = ~result.uint_32;
332 opts->fs_dmask_inv = ~result.uint_32;
333 opts->fmask = 1;
334 opts->dmask = 1;
335 break;
336 case Opt_dmask:
337 if (result.uint_32 & ~07777)
338 return invalf(fc, "ntfs3: Invalid value for dmask.");
339 opts->fs_dmask_inv = ~result.uint_32;
340 opts->dmask = 1;
341 break;
342 case Opt_fmask:
343 if (result.uint_32 & ~07777)
344 return invalf(fc, "ntfs3: Invalid value for fmask.");
345 opts->fs_fmask_inv = ~result.uint_32;
346 opts->fmask = 1;
347 break;
348 case Opt_immutable:
349 opts->sys_immutable = result.negated ? 0 : 1;
350 break;
351 case Opt_discard:
352 opts->discard = result.negated ? 0 : 1;
353 break;
354 case Opt_force:
355 opts->force = result.negated ? 0 : 1;
356 break;
357 case Opt_sparse:
358 opts->sparse = result.negated ? 0 : 1;
359 break;
360 case Opt_nohidden:
Kari Argillander9d1939f2021-09-07 18:35:54 +0300361 opts->nohidden = result.negated ? 1 : 0;
Kari Argillander610f8f52021-09-07 18:35:52 +0300362 break;
Konstantin Komarov098250d2022-09-12 18:28:51 +0300363 case Opt_hide_dot_files:
Daniel Pinto4c9ba192022-10-10 12:30:15 +0100364 opts->hide_dot_files = result.negated ? 0 : 1;
Konstantin Komarov098250d2022-09-12 18:28:51 +0300365 break;
Daniel Pinto1d07a9d2022-10-10 12:14:31 +0100366 case Opt_windows_names:
367 opts->windows_names = result.negated ? 0 : 1;
368 break;
Kari Argillander610f8f52021-09-07 18:35:52 +0300369 case Opt_acl:
370 if (!result.negated)
Konstantin Komarov82cae262021-08-13 17:21:29 +0300371#ifdef CONFIG_NTFS3_FS_POSIX_ACL
Kari Argillander610f8f52021-09-07 18:35:52 +0300372 fc->sb_flags |= SB_POSIXACL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300373#else
Kari Argillander610f8f52021-09-07 18:35:52 +0300374 return invalf(fc, "ntfs3: Support for ACL not compiled in!");
Konstantin Komarov82cae262021-08-13 17:21:29 +0300375#endif
Kari Argillander610f8f52021-09-07 18:35:52 +0300376 else
377 fc->sb_flags &= ~SB_POSIXACL;
378 break;
379 case Opt_showmeta:
380 opts->showmeta = result.negated ? 0 : 1;
381 break;
Kari Argillandere274cde2021-09-07 18:35:55 +0300382 case Opt_iocharset:
Kari Argillander610f8f52021-09-07 18:35:52 +0300383 kfree(opts->nls_name);
384 opts->nls_name = param->string;
385 param->string = NULL;
386 break;
387 case Opt_prealloc:
388 opts->prealloc = result.negated ? 0 : 1;
389 break;
Kari Argillander28a941f2021-09-07 18:35:56 +0300390 case Opt_noacsrules:
391 opts->noacsrules = result.negated ? 1 : 0;
Kari Argillander610f8f52021-09-07 18:35:52 +0300392 break;
Konstantin Komarova3a956c2022-09-23 12:42:18 +0300393 case Opt_nocase:
394 opts->nocase = result.negated ? 1 : 0;
395 break;
Kari Argillander610f8f52021-09-07 18:35:52 +0300396 default:
397 /* Should not be here unless we forget add case. */
398 return -EINVAL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300399 }
Konstantin Komarov82cae262021-08-13 17:21:29 +0300400 return 0;
401}
402
Kari Argillander610f8f52021-09-07 18:35:52 +0300403static int ntfs_fs_reconfigure(struct fs_context *fc)
Konstantin Komarov82cae262021-08-13 17:21:29 +0300404{
Kari Argillander610f8f52021-09-07 18:35:52 +0300405 struct super_block *sb = fc->root->d_sb;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300406 struct ntfs_sb_info *sbi = sb->s_fs_info;
Kari Argillander610f8f52021-09-07 18:35:52 +0300407 struct ntfs_mount_options *new_opts = fc->fs_private;
408 int ro_rw;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300409
Kari Argillander610f8f52021-09-07 18:35:52 +0300410 ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300411 if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) {
Kari Argillander610f8f52021-09-07 18:35:52 +0300412 errorf(fc, "ntfs3: Couldn't remount rw because journal is not replayed. Please umount/remount instead\n");
413 return -EINVAL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300414 }
415
Kari Argillander610f8f52021-09-07 18:35:52 +0300416 new_opts->nls = ntfs_load_nls(new_opts->nls_name);
417 if (IS_ERR(new_opts->nls)) {
418 new_opts->nls = NULL;
Kari Argillandere274cde2021-09-07 18:35:55 +0300419 errorf(fc, "ntfs3: Cannot load iocharset %s", new_opts->nls_name);
Kari Argillander610f8f52021-09-07 18:35:52 +0300420 return -EINVAL;
421 }
422 if (new_opts->nls != sbi->options->nls)
Kari Argillandere274cde2021-09-07 18:35:55 +0300423 return invalf(fc, "ntfs3: Cannot use different iocharset when remounting!");
Kari Argillander610f8f52021-09-07 18:35:52 +0300424
Konstantin Komarov82cae262021-08-13 17:21:29 +0300425 sync_filesystem(sb);
426
427 if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) &&
Kari Argillander610f8f52021-09-07 18:35:52 +0300428 !new_opts->force) {
429 errorf(fc, "ntfs3: Volume is dirty and \"force\" flag is not set!");
430 return -EINVAL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300431 }
432
Konstantin Komarovcd399812022-05-11 19:58:36 +0300433 swap(sbi->options, fc->fs_private);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300434
Kari Argillander610f8f52021-09-07 18:35:52 +0300435 return 0;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300436}
437
438static struct kmem_cache *ntfs_inode_cachep;
439
440static struct inode *ntfs_alloc_inode(struct super_block *sb)
441{
Muchun Songfd60b282022-03-22 14:41:03 -0700442 struct ntfs_inode *ni = alloc_inode_sb(sb, ntfs_inode_cachep, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300443
444 if (!ni)
445 return NULL;
446
447 memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode));
Konstantin Komarov82cae262021-08-13 17:21:29 +0300448 mutex_init(&ni->ni_lock);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300449 return &ni->vfs_inode;
450}
451
Konstantin Komarovae6b47b2022-09-12 18:54:06 +0300452static void ntfs_free_inode(struct inode *inode)
Konstantin Komarov82cae262021-08-13 17:21:29 +0300453{
Konstantin Komarov82cae262021-08-13 17:21:29 +0300454 struct ntfs_inode *ni = ntfs_i(inode);
455
456 mutex_destroy(&ni->ni_lock);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300457 kmem_cache_free(ntfs_inode_cachep, ni);
458}
459
Konstantin Komarov82cae262021-08-13 17:21:29 +0300460static void init_once(void *foo)
461{
462 struct ntfs_inode *ni = foo;
463
464 inode_init_once(&ni->vfs_inode);
465}
466
Kari Argillandere8b8e972021-08-03 14:57:09 +0300467/*
468 * put_ntfs - Noinline to reduce binary size.
469 */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300470static noinline void put_ntfs(struct ntfs_sb_info *sbi)
471{
Kari Argillander195c52b2021-08-24 21:37:07 +0300472 kfree(sbi->new_rec);
473 kvfree(ntfs_put_shared(sbi->upcase));
474 kfree(sbi->def_table);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300475
476 wnd_close(&sbi->mft.bitmap);
477 wnd_close(&sbi->used.bitmap);
478
479 if (sbi->mft.ni)
480 iput(&sbi->mft.ni->vfs_inode);
481
482 if (sbi->security.ni)
483 iput(&sbi->security.ni->vfs_inode);
484
485 if (sbi->reparse.ni)
486 iput(&sbi->reparse.ni->vfs_inode);
487
488 if (sbi->objid.ni)
489 iput(&sbi->objid.ni->vfs_inode);
490
491 if (sbi->volume.ni)
492 iput(&sbi->volume.ni->vfs_inode);
493
494 ntfs_update_mftmirr(sbi, 0);
495
496 indx_clear(&sbi->security.index_sii);
497 indx_clear(&sbi->security.index_sdh);
498 indx_clear(&sbi->reparse.index_r);
499 indx_clear(&sbi->objid.index_o);
Kari Argillander195c52b2021-08-24 21:37:07 +0300500 kfree(sbi->compress.lznt);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300501#ifdef CONFIG_NTFS3_LZX_XPRESS
502 xpress_free_decompressor(sbi->compress.xpress);
503 lzx_free_decompressor(sbi->compress.lzx);
504#endif
Kari Argillander195c52b2021-08-24 21:37:07 +0300505 kfree(sbi);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300506}
507
508static void ntfs_put_super(struct super_block *sb)
509{
510 struct ntfs_sb_info *sbi = sb->s_fs_info;
511
Kari Argillandere8b8e972021-08-03 14:57:09 +0300512 /* Mark rw ntfs as clear, if possible. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300513 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
514
Kari Argillander610f8f52021-09-07 18:35:52 +0300515 put_mount_options(sbi->options);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300516 put_ntfs(sbi);
Kari Argillander610f8f52021-09-07 18:35:52 +0300517 sb->s_fs_info = NULL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300518
519 sync_blockdev(sb->s_bdev);
520}
521
522static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf)
523{
524 struct super_block *sb = dentry->d_sb;
525 struct ntfs_sb_info *sbi = sb->s_fs_info;
526 struct wnd_bitmap *wnd = &sbi->used.bitmap;
527
528 buf->f_type = sb->s_magic;
529 buf->f_bsize = sbi->cluster_size;
530 buf->f_blocks = wnd->nbits;
531
532 buf->f_bfree = buf->f_bavail = wnd_zeroes(wnd);
533 buf->f_fsid.val[0] = sbi->volume.ser_num;
534 buf->f_fsid.val[1] = (sbi->volume.ser_num >> 32);
535 buf->f_namelen = NTFS_NAME_LEN;
536
537 return 0;
538}
539
540static int ntfs_show_options(struct seq_file *m, struct dentry *root)
541{
542 struct super_block *sb = root->d_sb;
543 struct ntfs_sb_info *sbi = sb->s_fs_info;
Kari Argillander564c97b2021-09-07 18:35:51 +0300544 struct ntfs_mount_options *opts = sbi->options;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300545 struct user_namespace *user_ns = seq_user_ns(m);
546
Kari Argillander15b2ae72021-09-07 18:35:57 +0300547 seq_printf(m, ",uid=%u",
548 from_kuid_munged(user_ns, opts->fs_uid));
549 seq_printf(m, ",gid=%u",
550 from_kgid_munged(user_ns, opts->fs_gid));
Konstantin Komarov82cae262021-08-13 17:21:29 +0300551 if (opts->fmask)
Marc Aurèle La Francef27b92e2022-08-10 14:28:04 -0600552 seq_printf(m, ",fmask=%04o", opts->fs_fmask_inv ^ 0xffff);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300553 if (opts->dmask)
Marc Aurèle La Francef27b92e2022-08-10 14:28:04 -0600554 seq_printf(m, ",dmask=%04o", opts->fs_dmask_inv ^ 0xffff);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300555 if (opts->nls)
Kari Argillandere274cde2021-09-07 18:35:55 +0300556 seq_printf(m, ",iocharset=%s", opts->nls->charset);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300557 else
Kari Argillandere274cde2021-09-07 18:35:55 +0300558 seq_puts(m, ",iocharset=utf8");
Konstantin Komarov82cae262021-08-13 17:21:29 +0300559 if (opts->sys_immutable)
560 seq_puts(m, ",sys_immutable");
561 if (opts->discard)
562 seq_puts(m, ",discard");
563 if (opts->sparse)
564 seq_puts(m, ",sparse");
565 if (opts->showmeta)
566 seq_puts(m, ",showmeta");
567 if (opts->nohidden)
568 seq_puts(m, ",nohidden");
Daniel Pinto1d07a9d2022-10-10 12:14:31 +0100569 if (opts->windows_names)
570 seq_puts(m, ",windows_names");
Daniel Pinto19b42452022-10-10 12:34:06 +0100571 if (opts->hide_dot_files)
Daniel Pintodc0fcc92022-10-10 12:37:13 +0100572 seq_puts(m, ",hide_dot_files");
Konstantin Komarov82cae262021-08-13 17:21:29 +0300573 if (opts->force)
574 seq_puts(m, ",force");
Kari Argillander28a941f2021-09-07 18:35:56 +0300575 if (opts->noacsrules)
576 seq_puts(m, ",noacsrules");
Konstantin Komarov82cae262021-08-13 17:21:29 +0300577 if (opts->prealloc)
578 seq_puts(m, ",prealloc");
579 if (sb->s_flags & SB_POSIXACL)
580 seq_puts(m, ",acl");
Konstantin Komarov82cae262021-08-13 17:21:29 +0300581
582 return 0;
583}
584
Kari Argillandere8b8e972021-08-03 14:57:09 +0300585/*
586 * ntfs_sync_fs - super_operations::sync_fs
587 */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300588static int ntfs_sync_fs(struct super_block *sb, int wait)
589{
590 int err = 0, err2;
591 struct ntfs_sb_info *sbi = sb->s_fs_info;
592 struct ntfs_inode *ni;
593 struct inode *inode;
594
595 ni = sbi->security.ni;
596 if (ni) {
597 inode = &ni->vfs_inode;
598 err2 = _ni_write_inode(inode, wait);
599 if (err2 && !err)
600 err = err2;
601 }
602
603 ni = sbi->objid.ni;
604 if (ni) {
605 inode = &ni->vfs_inode;
606 err2 = _ni_write_inode(inode, wait);
607 if (err2 && !err)
608 err = err2;
609 }
610
611 ni = sbi->reparse.ni;
612 if (ni) {
613 inode = &ni->vfs_inode;
614 err2 = _ni_write_inode(inode, wait);
615 if (err2 && !err)
616 err = err2;
617 }
618
619 if (!err)
620 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
621
622 ntfs_update_mftmirr(sbi, wait);
623
624 return err;
625}
626
627static const struct super_operations ntfs_sops = {
628 .alloc_inode = ntfs_alloc_inode,
Konstantin Komarovae6b47b2022-09-12 18:54:06 +0300629 .free_inode = ntfs_free_inode,
Konstantin Komarov82cae262021-08-13 17:21:29 +0300630 .evict_inode = ntfs_evict_inode,
631 .put_super = ntfs_put_super,
632 .statfs = ntfs_statfs,
633 .show_options = ntfs_show_options,
634 .sync_fs = ntfs_sync_fs,
Konstantin Komarov82cae262021-08-13 17:21:29 +0300635 .write_inode = ntfs3_write_inode,
636};
637
638static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino,
639 u32 generation)
640{
641 struct MFT_REF ref;
642 struct inode *inode;
643
644 ref.low = cpu_to_le32(ino);
645#ifdef CONFIG_NTFS3_64BIT_CLUSTER
646 ref.high = cpu_to_le16(ino >> 32);
647#else
648 ref.high = 0;
649#endif
650 ref.seq = cpu_to_le16(generation);
651
652 inode = ntfs_iget5(sb, &ref, NULL);
653 if (!IS_ERR(inode) && is_bad_inode(inode)) {
654 iput(inode);
655 inode = ERR_PTR(-ESTALE);
656 }
657
658 return inode;
659}
660
661static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
662 int fh_len, int fh_type)
663{
664 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
665 ntfs_export_get_inode);
666}
667
668static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
669 int fh_len, int fh_type)
670{
671 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
672 ntfs_export_get_inode);
673}
674
675/* TODO: == ntfs_sync_inode */
676static int ntfs_nfs_commit_metadata(struct inode *inode)
677{
678 return _ni_write_inode(inode, 1);
679}
680
681static const struct export_operations ntfs_export_ops = {
682 .fh_to_dentry = ntfs_fh_to_dentry,
683 .fh_to_parent = ntfs_fh_to_parent,
684 .get_parent = ntfs3_get_parent,
685 .commit_metadata = ntfs_nfs_commit_metadata,
686};
687
Kari Argillandere8b8e972021-08-03 14:57:09 +0300688/*
689 * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb".
690 */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300691static u32 format_size_gb(const u64 bytes, u32 *mb)
692{
Kari Argillandere8b8e972021-08-03 14:57:09 +0300693 /* Do simple right 30 bit shift of 64 bit value. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300694 u64 kbytes = bytes >> 10;
695 u32 kbytes32 = kbytes;
696
697 *mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20;
698 if (*mb >= 100)
699 *mb = 99;
700
701 return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12);
702}
703
704static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot)
705{
Randy Dunlapa3b77432022-05-12 20:38:37 -0700706 if (boot->sectors_per_clusters <= 0x80)
707 return boot->sectors_per_clusters;
708 if (boot->sectors_per_clusters >= 0xf4) /* limit shift to 2MB max */
Shigeru Yoshidacaad9dd2022-08-23 23:46:25 +0900709 return 1U << -(s8)boot->sectors_per_clusters;
Randy Dunlapa3b77432022-05-12 20:38:37 -0700710 return -EINVAL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300711}
712
Kari Argillandere8b8e972021-08-03 14:57:09 +0300713/*
714 * ntfs_init_from_boot - Init internal info from on-disk boot sector.
715 */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300716static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
717 u64 dev_size)
718{
719 struct ntfs_sb_info *sbi = sb->s_fs_info;
720 int err;
721 u32 mb, gb, boot_sector_size, sct_per_clst, record_size;
Konstantin Komarovdbf59e22021-09-28 19:19:49 +0300722 u64 sectors, clusters, mlcn, mlcn2;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300723 struct NTFS_BOOT *boot;
724 struct buffer_head *bh;
725 struct MFT_REC *rec;
726 u16 fn, ao;
727
728 sbi->volume.blocks = dev_size >> PAGE_SHIFT;
729
730 bh = ntfs_bread(sb, 0);
731 if (!bh)
732 return -EIO;
733
734 err = -EINVAL;
735 boot = (struct NTFS_BOOT *)bh->b_data;
736
737 if (memcmp(boot->system_id, "NTFS ", sizeof("NTFS ") - 1))
738 goto out;
739
740 /* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/
741 /*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1])
742 * goto out;
743 */
744
745 boot_sector_size = (u32)boot->bytes_per_sector[1] << 8;
746 if (boot->bytes_per_sector[0] || boot_sector_size < SECTOR_SIZE ||
Kari Argillander528c9b32021-08-16 13:37:32 +0300747 !is_power_of_2(boot_sector_size)) {
Konstantin Komarov82cae262021-08-13 17:21:29 +0300748 goto out;
749 }
750
751 /* cluster size: 512, 1K, 2K, 4K, ... 2M */
752 sct_per_clst = true_sectors_per_clst(boot);
Randy Dunlapa3b77432022-05-12 20:38:37 -0700753 if ((int)sct_per_clst < 0)
754 goto out;
Kari Argillander528c9b32021-08-16 13:37:32 +0300755 if (!is_power_of_2(sct_per_clst))
Konstantin Komarov82cae262021-08-13 17:21:29 +0300756 goto out;
757
758 mlcn = le64_to_cpu(boot->mft_clst);
759 mlcn2 = le64_to_cpu(boot->mft2_clst);
760 sectors = le64_to_cpu(boot->sectors_per_volume);
761
762 if (mlcn * sct_per_clst >= sectors)
763 goto out;
764
765 if (mlcn2 * sct_per_clst >= sectors)
766 goto out;
767
Kari Argillandere8b8e972021-08-03 14:57:09 +0300768 /* Check MFT record size. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300769 if ((boot->record_size < 0 &&
770 SECTOR_SIZE > (2U << (-boot->record_size))) ||
Kari Argillander528c9b32021-08-16 13:37:32 +0300771 (boot->record_size >= 0 && !is_power_of_2(boot->record_size))) {
Konstantin Komarov82cae262021-08-13 17:21:29 +0300772 goto out;
773 }
774
Kari Argillandere8b8e972021-08-03 14:57:09 +0300775 /* Check index record size. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300776 if ((boot->index_size < 0 &&
777 SECTOR_SIZE > (2U << (-boot->index_size))) ||
Kari Argillander528c9b32021-08-16 13:37:32 +0300778 (boot->index_size >= 0 && !is_power_of_2(boot->index_size))) {
Konstantin Komarov82cae262021-08-13 17:21:29 +0300779 goto out;
780 }
781
Konstantin Komarovdbf59e22021-09-28 19:19:49 +0300782 sbi->volume.size = sectors * boot_sector_size;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300783
Konstantin Komarovdbf59e22021-09-28 19:19:49 +0300784 gb = format_size_gb(sbi->volume.size + boot_sector_size, &mb);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300785
786 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300787 * - Volume formatted and mounted with the same sector size.
788 * - Volume formatted 4K and mounted as 512.
789 * - Volume formatted 512 and mounted as 4K.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300790 */
Konstantin Komarovdbf59e22021-09-28 19:19:49 +0300791 if (boot_sector_size != sector_size) {
792 ntfs_warn(
793 sb,
794 "Different NTFS' sector size (%u) and media sector size (%u)",
795 boot_sector_size, sector_size);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300796 dev_size += sector_size - 1;
797 }
798
799 sbi->cluster_size = boot_sector_size * sct_per_clst;
800 sbi->cluster_bits = blksize_bits(sbi->cluster_size);
801
802 sbi->mft.lbo = mlcn << sbi->cluster_bits;
803 sbi->mft.lbo2 = mlcn2 << sbi->cluster_bits;
804
Konstantin Komarov09f7c332021-09-28 19:19:17 +0300805 /* Compare boot's cluster and sector. */
806 if (sbi->cluster_size < boot_sector_size)
Konstantin Komarov82cae262021-08-13 17:21:29 +0300807 goto out;
808
Konstantin Komarov09f7c332021-09-28 19:19:17 +0300809 /* Compare boot's cluster and media sector. */
810 if (sbi->cluster_size < sector_size) {
811 /* No way to use ntfs_get_block in this case. */
812 ntfs_err(
813 sb,
814 "Failed to mount 'cause NTFS's cluster size (%u) is less than media sector size (%u)",
815 sbi->cluster_size, sector_size);
816 goto out;
817 }
818
Konstantin Komarov82cae262021-08-13 17:21:29 +0300819 sbi->cluster_mask = sbi->cluster_size - 1;
820 sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask;
821 sbi->record_size = record_size = boot->record_size < 0
822 ? 1 << (-boot->record_size)
823 : (u32)boot->record_size
824 << sbi->cluster_bits;
825
edward lo0b660462022-08-01 15:37:31 +0800826 if (record_size > MAXIMUM_BYTES_PER_MFT || record_size < SECTOR_SIZE)
Konstantin Komarov82cae262021-08-13 17:21:29 +0300827 goto out;
828
829 sbi->record_bits = blksize_bits(record_size);
830 sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes
831
832 sbi->max_bytes_per_attr =
Kari Argillanderfa3cacf2021-08-26 11:56:29 +0300833 record_size - ALIGN(MFTRECORD_FIXUP_OFFSET_1, 8) -
834 ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) -
835 ALIGN(sizeof(enum ATTR_TYPE), 8);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300836
837 sbi->index_size = boot->index_size < 0
838 ? 1u << (-boot->index_size)
839 : (u32)boot->index_size << sbi->cluster_bits;
840
841 sbi->volume.ser_num = le64_to_cpu(boot->serial_num);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300842
Kari Argillandere8b8e972021-08-03 14:57:09 +0300843 /* Warning if RAW volume. */
Konstantin Komarovdbf59e22021-09-28 19:19:49 +0300844 if (dev_size < sbi->volume.size + boot_sector_size) {
Konstantin Komarov82cae262021-08-13 17:21:29 +0300845 u32 mb0, gb0;
846
847 gb0 = format_size_gb(dev_size, &mb0);
848 ntfs_warn(
849 sb,
850 "RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only",
851 gb, mb, gb0, mb0);
852 sb->s_flags |= SB_RDONLY;
853 }
854
855 clusters = sbi->volume.size >> sbi->cluster_bits;
856#ifndef CONFIG_NTFS3_64BIT_CLUSTER
Kari Argillandere8b8e972021-08-03 14:57:09 +0300857 /* 32 bits per cluster. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300858 if (clusters >> 32) {
859 ntfs_notice(
860 sb,
861 "NTFS %u.%02u Gb is too big to use 32 bits per cluster",
862 gb, mb);
863 goto out;
864 }
865#elif BITS_PER_LONG < 64
866#error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS"
867#endif
868
869 sbi->used.bitmap.nbits = clusters;
870
Kari Argillander195c52b2021-08-24 21:37:07 +0300871 rec = kzalloc(record_size, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300872 if (!rec) {
873 err = -ENOMEM;
874 goto out;
875 }
876
877 sbi->new_rec = rec;
878 rec->rhdr.sign = NTFS_FILE_SIGNATURE;
879 rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET_1);
880 fn = (sbi->record_size >> SECTOR_SHIFT) + 1;
881 rec->rhdr.fix_num = cpu_to_le16(fn);
Kari Argillanderfa3cacf2021-08-26 11:56:29 +0300882 ao = ALIGN(MFTRECORD_FIXUP_OFFSET_1 + sizeof(short) * fn, 8);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300883 rec->attr_off = cpu_to_le16(ao);
Kari Argillanderfa3cacf2021-08-26 11:56:29 +0300884 rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8));
Konstantin Komarov82cae262021-08-13 17:21:29 +0300885 rec->total = cpu_to_le32(sbi->record_size);
886 ((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END;
887
Kari Argillander28861e32021-09-09 21:09:42 +0300888 sb_set_blocksize(sb, min_t(u32, sbi->cluster_size, PAGE_SIZE));
Konstantin Komarov82cae262021-08-13 17:21:29 +0300889
890 sbi->block_mask = sb->s_blocksize - 1;
891 sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits;
892 sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits;
893
Kari Argillandere8b8e972021-08-03 14:57:09 +0300894 /* Maximum size for normal files. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300895 sbi->maxbytes = (clusters << sbi->cluster_bits) - 1;
896
897#ifdef CONFIG_NTFS3_64BIT_CLUSTER
898 if (clusters >= (1ull << (64 - sbi->cluster_bits)))
899 sbi->maxbytes = -1;
900 sbi->maxbytes_sparse = -1;
Kari Argillander28861e32021-09-09 21:09:42 +0300901 sb->s_maxbytes = MAX_LFS_FILESIZE;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300902#else
Kari Argillandere8b8e972021-08-03 14:57:09 +0300903 /* Maximum size for sparse file. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300904 sbi->maxbytes_sparse = (1ull << (sbi->cluster_bits + 32)) - 1;
Kari Argillander28861e32021-09-09 21:09:42 +0300905 sb->s_maxbytes = 0xFFFFFFFFull << sbi->cluster_bits;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300906#endif
907
Konstantin Komarov8335ebe2022-07-07 19:25:43 +0300908 /*
909 * Compute the MFT zone at two steps.
910 * It would be nice if we are able to allocate 1/8 of
911 * total clusters for MFT but not more then 512 MB.
912 */
913 sbi->zone_max = min_t(CLST, 0x20000000 >> sbi->cluster_bits, clusters >> 3);
914
Konstantin Komarov82cae262021-08-13 17:21:29 +0300915 err = 0;
916
917out:
918 brelse(bh);
919
920 return err;
921}
922
Kari Argillandere8b8e972021-08-03 14:57:09 +0300923/*
924 * ntfs_fill_super - Try to mount.
925 */
Kari Argillander610f8f52021-09-07 18:35:52 +0300926static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
Konstantin Komarov82cae262021-08-13 17:21:29 +0300927{
928 int err;
Kari Argillander610f8f52021-09-07 18:35:52 +0300929 struct ntfs_sb_info *sbi = sb->s_fs_info;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300930 struct block_device *bdev = sb->s_bdev;
Kari Argillander10b4f122021-09-09 21:09:36 +0300931 struct inode *inode;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300932 struct ntfs_inode *ni;
Konstantin Komarovec5fc722022-10-11 20:19:36 +0300933 size_t i, tt, bad_len, bad_frags;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300934 CLST vcn, lcn, len;
935 struct ATTRIB *attr;
936 const struct VOLUME_INFO *info;
937 u32 idx, done, bytes;
938 struct ATTR_DEF_ENTRY *t;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300939 u16 *shared;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300940 struct MFT_REF ref;
941
942 ref.high = 0;
943
Konstantin Komarov82cae262021-08-13 17:21:29 +0300944 sbi->sb = sb;
Konstantin Komarovcd399812022-05-11 19:58:36 +0300945 sbi->options = fc->fs_private;
946 fc->fs_private = NULL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300947 sb->s_flags |= SB_NODIRATIME;
948 sb->s_magic = 0x7366746e; // "ntfs"
949 sb->s_op = &ntfs_sops;
950 sb->s_export_op = &ntfs_export_ops;
951 sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec
952 sb->s_xattr = ntfs_xattr_handlers;
Konstantin Komarova3a956c2022-09-23 12:42:18 +0300953 sb->s_d_op = sbi->options->nocase ? &ntfs_dentry_ops : NULL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300954
Kari Argillander610f8f52021-09-07 18:35:52 +0300955 sbi->options->nls = ntfs_load_nls(sbi->options->nls_name);
956 if (IS_ERR(sbi->options->nls)) {
957 sbi->options->nls = NULL;
958 errorf(fc, "Cannot load nls %s", sbi->options->nls_name);
Konstantin Komarov9b754502021-09-28 19:00:30 +0300959 err = -EINVAL;
960 goto out;
Kari Argillander610f8f52021-09-07 18:35:52 +0300961 }
Konstantin Komarov82cae262021-08-13 17:21:29 +0300962
Christoph Hellwig7b47ef52022-04-15 06:52:56 +0200963 if (bdev_max_discard_sectors(bdev) && bdev_discard_granularity(bdev)) {
964 sbi->discard_granularity = bdev_discard_granularity(bdev);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300965 sbi->discard_granularity_mask_inv =
966 ~(u64)(sbi->discard_granularity - 1);
967 }
968
Kari Argillandere8b8e972021-08-03 14:57:09 +0300969 /* Parse boot. */
Christoph Hellwigf09dac92022-04-15 06:52:40 +0200970 err = ntfs_init_from_boot(sb, bdev_logical_block_size(bdev),
Christoph Hellwigd54f13a2021-10-18 11:37:15 -0600971 bdev_nr_bytes(bdev));
Konstantin Komarov82cae262021-08-13 17:21:29 +0300972 if (err)
Konstantin Komarov9b754502021-09-28 19:00:30 +0300973 goto out;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300974
Konstantin Komarov82cae262021-08-13 17:21:29 +0300975 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300976 * Load $Volume. This should be done before $LogFile
977 * 'cause 'sbi->volume.ni' is used 'ntfs_set_state'.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300978 */
979 ref.low = cpu_to_le32(MFT_REC_VOL);
980 ref.seq = cpu_to_le16(MFT_REC_VOL);
981 inode = ntfs_iget5(sb, &ref, &NAME_VOLUME);
982 if (IS_ERR(inode)) {
Konstantin Komarov82cae262021-08-13 17:21:29 +0300983 ntfs_err(sb, "Failed to load $Volume.");
Konstantin Komarov9b754502021-09-28 19:00:30 +0300984 err = PTR_ERR(inode);
985 goto out;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300986 }
987
988 ni = ntfs_i(inode);
989
Kari Argillandere8b8e972021-08-03 14:57:09 +0300990 /* Load and save label (not necessary). */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300991 attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL);
992
993 if (!attr) {
994 /* It is ok if no ATTR_LABEL */
995 } else if (!attr->non_res && !is_attr_ext(attr)) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300996 /* $AttrDef allows labels to be up to 128 symbols. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300997 err = utf16s_to_utf8s(resident_data(attr),
998 le32_to_cpu(attr->res.data_size) >> 1,
999 UTF16_LITTLE_ENDIAN, sbi->volume.label,
1000 sizeof(sbi->volume.label));
1001 if (err < 0)
1002 sbi->volume.label[0] = 0;
1003 } else {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001004 /* Should we break mounting here? */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001005 //err = -EINVAL;
Konstantin Komarov9b754502021-09-28 19:00:30 +03001006 //goto put_inode_out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001007 }
1008
1009 attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL);
1010 if (!attr || is_attr_ext(attr)) {
1011 err = -EINVAL;
Konstantin Komarov9b754502021-09-28 19:00:30 +03001012 goto put_inode_out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001013 }
1014
1015 info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO);
1016 if (!info) {
1017 err = -EINVAL;
Konstantin Komarov9b754502021-09-28 19:00:30 +03001018 goto put_inode_out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001019 }
1020
1021 sbi->volume.major_ver = info->major_ver;
1022 sbi->volume.minor_ver = info->minor_ver;
1023 sbi->volume.flags = info->flags;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001024 sbi->volume.ni = ni;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001025
Kari Argillandere8b8e972021-08-03 14:57:09 +03001026 /* Load $MFTMirr to estimate recs_mirr. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001027 ref.low = cpu_to_le32(MFT_REC_MIRR);
1028 ref.seq = cpu_to_le16(MFT_REC_MIRR);
1029 inode = ntfs_iget5(sb, &ref, &NAME_MIRROR);
1030 if (IS_ERR(inode)) {
Konstantin Komarov82cae262021-08-13 17:21:29 +03001031 ntfs_err(sb, "Failed to load $MFTMirr.");
Konstantin Komarov9b754502021-09-28 19:00:30 +03001032 err = PTR_ERR(inode);
1033 goto out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001034 }
1035
1036 sbi->mft.recs_mirr =
1037 ntfs_up_cluster(sbi, inode->i_size) >> sbi->record_bits;
1038
1039 iput(inode);
1040
Konstantin Komarovd3624462021-08-31 16:57:40 +03001041 /* Load LogFile to replay. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001042 ref.low = cpu_to_le32(MFT_REC_LOG);
1043 ref.seq = cpu_to_le16(MFT_REC_LOG);
1044 inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE);
1045 if (IS_ERR(inode)) {
Konstantin Komarov82cae262021-08-13 17:21:29 +03001046 ntfs_err(sb, "Failed to load \x24LogFile.");
Konstantin Komarov9b754502021-09-28 19:00:30 +03001047 err = PTR_ERR(inode);
1048 goto out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001049 }
1050
1051 ni = ntfs_i(inode);
1052
1053 err = ntfs_loadlog_and_replay(ni, sbi);
1054 if (err)
Konstantin Komarov9b754502021-09-28 19:00:30 +03001055 goto put_inode_out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001056
1057 iput(inode);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001058
Konstantin Komarov82cae262021-08-13 17:21:29 +03001059 if (sbi->flags & NTFS_FLAGS_NEED_REPLAY) {
Kari Argillander0cde7e812021-09-09 21:09:38 +03001060 if (!sb_rdonly(sb)) {
Konstantin Komarov82cae262021-08-13 17:21:29 +03001061 ntfs_warn(sb,
1062 "failed to replay log file. Can't mount rw!");
Konstantin Komarov9b754502021-09-28 19:00:30 +03001063 err = -EINVAL;
1064 goto out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001065 }
1066 } else if (sbi->volume.flags & VOLUME_FLAG_DIRTY) {
Kari Argillander0cde7e812021-09-09 21:09:38 +03001067 if (!sb_rdonly(sb) && !sbi->options->force) {
Konstantin Komarov82cae262021-08-13 17:21:29 +03001068 ntfs_warn(
1069 sb,
1070 "volume is dirty and \"force\" flag is not set!");
Konstantin Komarov9b754502021-09-28 19:00:30 +03001071 err = -EINVAL;
1072 goto out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001073 }
1074 }
1075
Kari Argillandere8b8e972021-08-03 14:57:09 +03001076 /* Load $MFT. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001077 ref.low = cpu_to_le32(MFT_REC_MFT);
1078 ref.seq = cpu_to_le16(1);
1079
1080 inode = ntfs_iget5(sb, &ref, &NAME_MFT);
1081 if (IS_ERR(inode)) {
Konstantin Komarov82cae262021-08-13 17:21:29 +03001082 ntfs_err(sb, "Failed to load $MFT.");
Konstantin Komarov9b754502021-09-28 19:00:30 +03001083 err = PTR_ERR(inode);
1084 goto out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001085 }
1086
1087 ni = ntfs_i(inode);
1088
1089 sbi->mft.used = ni->i_valid >> sbi->record_bits;
1090 tt = inode->i_size >> sbi->record_bits;
1091 sbi->mft.next_free = MFT_REC_USER;
1092
1093 err = wnd_init(&sbi->mft.bitmap, sb, tt);
1094 if (err)
Konstantin Komarov9b754502021-09-28 19:00:30 +03001095 goto put_inode_out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001096
1097 err = ni_load_all_mi(ni);
1098 if (err)
Konstantin Komarov9b754502021-09-28 19:00:30 +03001099 goto put_inode_out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001100
1101 sbi->mft.ni = ni;
1102
Kari Argillandere8b8e972021-08-03 14:57:09 +03001103 /* Load $Bitmap. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001104 ref.low = cpu_to_le32(MFT_REC_BITMAP);
1105 ref.seq = cpu_to_le16(MFT_REC_BITMAP);
1106 inode = ntfs_iget5(sb, &ref, &NAME_BITMAP);
1107 if (IS_ERR(inode)) {
Konstantin Komarov82cae262021-08-13 17:21:29 +03001108 ntfs_err(sb, "Failed to load $Bitmap.");
Konstantin Komarov9b754502021-09-28 19:00:30 +03001109 err = PTR_ERR(inode);
1110 goto out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001111 }
1112
Konstantin Komarov82cae262021-08-13 17:21:29 +03001113#ifndef CONFIG_NTFS3_64BIT_CLUSTER
1114 if (inode->i_size >> 32) {
1115 err = -EINVAL;
Konstantin Komarov9b754502021-09-28 19:00:30 +03001116 goto put_inode_out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001117 }
1118#endif
1119
Kari Argillandere8b8e972021-08-03 14:57:09 +03001120 /* Check bitmap boundary. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001121 tt = sbi->used.bitmap.nbits;
1122 if (inode->i_size < bitmap_size(tt)) {
1123 err = -EINVAL;
Konstantin Komarov9b754502021-09-28 19:00:30 +03001124 goto put_inode_out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001125 }
1126
Kari Argillandere8b8e972021-08-03 14:57:09 +03001127 /* Not necessary. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001128 sbi->used.bitmap.set_tail = true;
Kari Argillanderb4f110d2021-09-09 21:09:37 +03001129 err = wnd_init(&sbi->used.bitmap, sb, tt);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001130 if (err)
Konstantin Komarov9b754502021-09-28 19:00:30 +03001131 goto put_inode_out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001132
1133 iput(inode);
1134
Kari Argillandere8b8e972021-08-03 14:57:09 +03001135 /* Compute the MFT zone. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001136 err = ntfs_refresh_zone(sbi);
1137 if (err)
Konstantin Komarov9b754502021-09-28 19:00:30 +03001138 goto out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001139
Konstantin Komarovec5fc722022-10-11 20:19:36 +03001140 /* Load $BadClus. */
1141 ref.low = cpu_to_le32(MFT_REC_BADCLUST);
1142 ref.seq = cpu_to_le16(MFT_REC_BADCLUST);
1143 inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS);
1144 if (IS_ERR(inode)) {
1145 err = PTR_ERR(inode);
1146 ntfs_err(sb, "Failed to load $BadClus (%d).", err);
1147 goto out;
1148 }
1149
1150 ni = ntfs_i(inode);
1151 bad_len = bad_frags = 0;
1152 for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) {
1153 if (lcn == SPARSE_LCN)
1154 continue;
1155
1156 bad_len += len;
1157 bad_frags += 1;
1158 if (sb_rdonly(sb))
1159 continue;
1160
1161 if (wnd_set_used_safe(&sbi->used.bitmap, lcn, len, &tt) || tt) {
1162 /* Bad blocks marked as free in bitmap. */
1163 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1164 }
1165 }
1166 if (bad_len) {
1167 /*
1168 * Notice about bad blocks.
1169 * In normal cases these blocks are marked as used in bitmap.
1170 * And we never allocate space in it.
1171 */
1172 ntfs_notice(sb,
1173 "Volume contains %zu bad blocks in %zu fragments.",
1174 bad_len, bad_frags);
1175 }
1176 iput(inode);
1177
Kari Argillandere8b8e972021-08-03 14:57:09 +03001178 /* Load $AttrDef. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001179 ref.low = cpu_to_le32(MFT_REC_ATTR);
1180 ref.seq = cpu_to_le16(MFT_REC_ATTR);
Kari Argillanderb4f110d2021-09-09 21:09:37 +03001181 inode = ntfs_iget5(sb, &ref, &NAME_ATTRDEF);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001182 if (IS_ERR(inode)) {
Konstantin Komarov82cae262021-08-13 17:21:29 +03001183 ntfs_err(sb, "Failed to load $AttrDef -> %d", err);
Konstantin Komarov9b754502021-09-28 19:00:30 +03001184 err = PTR_ERR(inode);
1185 goto out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001186 }
1187
1188 if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY)) {
1189 err = -EINVAL;
Konstantin Komarov9b754502021-09-28 19:00:30 +03001190 goto put_inode_out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001191 }
1192 bytes = inode->i_size;
Tetsuo Handa59bfd7a2022-10-02 23:54:11 +09001193 sbi->def_table = t = kmalloc(bytes, GFP_NOFS | __GFP_NOWARN);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001194 if (!t) {
1195 err = -ENOMEM;
Konstantin Komarov9b754502021-09-28 19:00:30 +03001196 goto put_inode_out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001197 }
1198
1199 for (done = idx = 0; done < bytes; done += PAGE_SIZE, idx++) {
1200 unsigned long tail = bytes - done;
1201 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1202
1203 if (IS_ERR(page)) {
1204 err = PTR_ERR(page);
Konstantin Komarov9b754502021-09-28 19:00:30 +03001205 goto put_inode_out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001206 }
1207 memcpy(Add2Ptr(t, done), page_address(page),
1208 min(PAGE_SIZE, tail));
1209 ntfs_unmap_page(page);
1210
1211 if (!idx && ATTR_STD != t->type) {
1212 err = -EINVAL;
Konstantin Komarov9b754502021-09-28 19:00:30 +03001213 goto put_inode_out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001214 }
1215 }
1216
1217 t += 1;
1218 sbi->def_entries = 1;
1219 done = sizeof(struct ATTR_DEF_ENTRY);
1220 sbi->reparse.max_size = MAXIMUM_REPARSE_DATA_BUFFER_SIZE;
Colin Ian Kingf8d87ed2021-08-16 11:13:08 +01001221 sbi->ea_max_size = 0x10000; /* default formatter value */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001222
1223 while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) {
1224 u32 t32 = le32_to_cpu(t->type);
1225 u64 sz = le64_to_cpu(t->max_sz);
1226
1227 if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32)
1228 break;
1229
1230 if (t->type == ATTR_REPARSE)
1231 sbi->reparse.max_size = sz;
1232 else if (t->type == ATTR_EA)
1233 sbi->ea_max_size = sz;
1234
1235 done += sizeof(struct ATTR_DEF_ENTRY);
1236 t += 1;
1237 sbi->def_entries += 1;
1238 }
1239 iput(inode);
1240
Kari Argillandere8b8e972021-08-03 14:57:09 +03001241 /* Load $UpCase. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001242 ref.low = cpu_to_le32(MFT_REC_UPCASE);
1243 ref.seq = cpu_to_le16(MFT_REC_UPCASE);
1244 inode = ntfs_iget5(sb, &ref, &NAME_UPCASE);
1245 if (IS_ERR(inode)) {
Kari Argillander04120162021-09-09 21:09:32 +03001246 ntfs_err(sb, "Failed to load $UpCase.");
Konstantin Komarov9b754502021-09-28 19:00:30 +03001247 err = PTR_ERR(inode);
1248 goto out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001249 }
1250
Konstantin Komarov82cae262021-08-13 17:21:29 +03001251 if (inode->i_size != 0x10000 * sizeof(short)) {
1252 err = -EINVAL;
Konstantin Komarov9b754502021-09-28 19:00:30 +03001253 goto put_inode_out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001254 }
1255
Konstantin Komarov82cae262021-08-13 17:21:29 +03001256 for (idx = 0; idx < (0x10000 * sizeof(short) >> PAGE_SHIFT); idx++) {
1257 const __le16 *src;
Kari Argillander0056b272021-09-09 21:09:40 +03001258 u16 *dst = Add2Ptr(sbi->upcase, idx << PAGE_SHIFT);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001259 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1260
1261 if (IS_ERR(page)) {
1262 err = PTR_ERR(page);
Konstantin Komarov9b754502021-09-28 19:00:30 +03001263 goto put_inode_out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001264 }
1265
1266 src = page_address(page);
1267
1268#ifdef __BIG_ENDIAN
1269 for (i = 0; i < PAGE_SIZE / sizeof(u16); i++)
1270 *dst++ = le16_to_cpu(*src++);
1271#else
1272 memcpy(dst, src, PAGE_SIZE);
1273#endif
1274 ntfs_unmap_page(page);
1275 }
1276
Kari Argillander0056b272021-09-09 21:09:40 +03001277 shared = ntfs_set_shared(sbi->upcase, 0x10000 * sizeof(short));
1278 if (shared && sbi->upcase != shared) {
1279 kvfree(sbi->upcase);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001280 sbi->upcase = shared;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001281 }
1282
1283 iput(inode);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001284
1285 if (is_ntfs3(sbi)) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001286 /* Load $Secure. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001287 err = ntfs_security_init(sbi);
1288 if (err)
Konstantin Komarov9b754502021-09-28 19:00:30 +03001289 goto out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001290
Kari Argillandere8b8e972021-08-03 14:57:09 +03001291 /* Load $Extend. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001292 err = ntfs_extend_init(sbi);
1293 if (err)
1294 goto load_root;
1295
Kari Argillandere8b8e972021-08-03 14:57:09 +03001296 /* Load $Extend\$Reparse. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001297 err = ntfs_reparse_init(sbi);
1298 if (err)
1299 goto load_root;
1300
Kari Argillandere8b8e972021-08-03 14:57:09 +03001301 /* Load $Extend\$ObjId. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001302 err = ntfs_objid_init(sbi);
1303 if (err)
1304 goto load_root;
1305 }
1306
1307load_root:
Kari Argillandere8b8e972021-08-03 14:57:09 +03001308 /* Load root. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001309 ref.low = cpu_to_le32(MFT_REC_ROOT);
1310 ref.seq = cpu_to_le16(MFT_REC_ROOT);
1311 inode = ntfs_iget5(sb, &ref, &NAME_ROOT);
Edward Loc1ca8ef2022-09-09 09:03:10 +08001312 if (IS_ERR(inode) || !inode->i_op) {
Konstantin Komarov82cae262021-08-13 17:21:29 +03001313 ntfs_err(sb, "Failed to load root.");
Edward Loc1ca8ef2022-09-09 09:03:10 +08001314 err = IS_ERR(inode) ? PTR_ERR(inode) : -EINVAL;
Konstantin Komarov9b754502021-09-28 19:00:30 +03001315 goto out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001316 }
1317
Konstantin Komarov82cae262021-08-13 17:21:29 +03001318 sb->s_root = d_make_root(inode);
Konstantin Komarov9b754502021-09-28 19:00:30 +03001319 if (!sb->s_root) {
1320 err = -ENOMEM;
1321 goto put_inode_out;
1322 }
Konstantin Komarov82cae262021-08-13 17:21:29 +03001323
1324 return 0;
Konstantin Komarov9b754502021-09-28 19:00:30 +03001325
1326put_inode_out:
Konstantin Komarov82cae262021-08-13 17:21:29 +03001327 iput(inode);
Konstantin Komarov9b754502021-09-28 19:00:30 +03001328out:
1329 /*
1330 * Free resources here.
1331 * ntfs_fs_free will be called with fc->s_fs_info = NULL
1332 */
Shigeru Yoshida51e76a22022-08-23 19:32:05 +09001333 put_mount_options(sbi->options);
Konstantin Komarov9b754502021-09-28 19:00:30 +03001334 put_ntfs(sbi);
1335 sb->s_fs_info = NULL;
1336
Konstantin Komarov82cae262021-08-13 17:21:29 +03001337 return err;
1338}
1339
1340void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len)
1341{
1342 struct ntfs_sb_info *sbi = sb->s_fs_info;
1343 struct block_device *bdev = sb->s_bdev;
1344 sector_t devblock = (u64)lcn * sbi->blocks_per_cluster;
1345 unsigned long blocks = (u64)len * sbi->blocks_per_cluster;
1346 unsigned long cnt = 0;
1347 unsigned long limit = global_zone_page_state(NR_FREE_PAGES)
1348 << (PAGE_SHIFT - sb->s_blocksize_bits);
1349
1350 if (limit >= 0x2000)
1351 limit -= 0x1000;
1352 else if (limit < 32)
1353 limit = 32;
1354 else
1355 limit >>= 1;
1356
1357 while (blocks--) {
1358 clean_bdev_aliases(bdev, devblock++, 1);
1359 if (cnt++ >= limit) {
1360 sync_blockdev(bdev);
1361 cnt = 0;
1362 }
1363 }
1364}
1365
1366/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001367 * ntfs_discard - Issue a discard request (trim for SSD).
Konstantin Komarov82cae262021-08-13 17:21:29 +03001368 */
1369int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len)
1370{
1371 int err;
1372 u64 lbo, bytes, start, end;
1373 struct super_block *sb;
1374
1375 if (sbi->used.next_free_lcn == lcn + len)
1376 sbi->used.next_free_lcn = lcn;
1377
1378 if (sbi->flags & NTFS_FLAGS_NODISCARD)
1379 return -EOPNOTSUPP;
1380
Kari Argillander564c97b2021-09-07 18:35:51 +03001381 if (!sbi->options->discard)
Konstantin Komarov82cae262021-08-13 17:21:29 +03001382 return -EOPNOTSUPP;
1383
1384 lbo = (u64)lcn << sbi->cluster_bits;
1385 bytes = (u64)len << sbi->cluster_bits;
1386
Kari Argillandere8b8e972021-08-03 14:57:09 +03001387 /* Align up 'start' on discard_granularity. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001388 start = (lbo + sbi->discard_granularity - 1) &
1389 sbi->discard_granularity_mask_inv;
Kari Argillandere8b8e972021-08-03 14:57:09 +03001390 /* Align down 'end' on discard_granularity. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001391 end = (lbo + bytes) & sbi->discard_granularity_mask_inv;
1392
1393 sb = sbi->sb;
1394 if (start >= end)
1395 return 0;
1396
1397 err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9,
Christoph Hellwig44abff2c2022-04-15 06:52:57 +02001398 GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001399
1400 if (err == -EOPNOTSUPP)
1401 sbi->flags |= NTFS_FLAGS_NODISCARD;
1402
1403 return err;
1404}
1405
Kari Argillander610f8f52021-09-07 18:35:52 +03001406static int ntfs_fs_get_tree(struct fs_context *fc)
Konstantin Komarov82cae262021-08-13 17:21:29 +03001407{
Kari Argillander610f8f52021-09-07 18:35:52 +03001408 return get_tree_bdev(fc, ntfs_fill_super);
1409}
1410
1411/*
1412 * ntfs_fs_free - Free fs_context.
1413 *
1414 * Note that this will be called after fill_super and reconfigure
1415 * even when they pass. So they have to take pointers if they pass.
1416 */
1417static void ntfs_fs_free(struct fs_context *fc)
1418{
1419 struct ntfs_mount_options *opts = fc->fs_private;
1420 struct ntfs_sb_info *sbi = fc->s_fs_info;
1421
1422 if (sbi)
1423 put_ntfs(sbi);
1424
1425 if (opts)
1426 put_mount_options(opts);
1427}
1428
1429static const struct fs_context_operations ntfs_context_ops = {
1430 .parse_param = ntfs_fs_parse_param,
1431 .get_tree = ntfs_fs_get_tree,
1432 .reconfigure = ntfs_fs_reconfigure,
1433 .free = ntfs_fs_free,
1434};
1435
1436/*
1437 * ntfs_init_fs_context - Initialize spi and opts
1438 *
Konstantin Komarov6700eab2022-06-30 18:49:24 +03001439 * This will called when mount/remount. We will first initialize
Kari Argillander610f8f52021-09-07 18:35:52 +03001440 * options so that if remount we can use just that.
1441 */
1442static int ntfs_init_fs_context(struct fs_context *fc)
1443{
1444 struct ntfs_mount_options *opts;
1445 struct ntfs_sb_info *sbi;
1446
1447 opts = kzalloc(sizeof(struct ntfs_mount_options), GFP_NOFS);
1448 if (!opts)
1449 return -ENOMEM;
1450
1451 /* Default options. */
1452 opts->fs_uid = current_uid();
1453 opts->fs_gid = current_gid();
1454 opts->fs_fmask_inv = ~current_umask();
1455 opts->fs_dmask_inv = ~current_umask();
1456
1457 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
1458 goto ok;
1459
1460 sbi = kzalloc(sizeof(struct ntfs_sb_info), GFP_NOFS);
Kari Argillander27fac772021-09-07 18:35:53 +03001461 if (!sbi)
1462 goto free_opts;
1463
1464 sbi->upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL);
1465 if (!sbi->upcase)
1466 goto free_sbi;
1467
1468 ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1469 DEFAULT_RATELIMIT_BURST);
1470
1471 mutex_init(&sbi->compress.mtx_lznt);
1472#ifdef CONFIG_NTFS3_LZX_XPRESS
1473 mutex_init(&sbi->compress.mtx_xpress);
1474 mutex_init(&sbi->compress.mtx_lzx);
1475#endif
Kari Argillander610f8f52021-09-07 18:35:52 +03001476
Kari Argillander610f8f52021-09-07 18:35:52 +03001477 fc->s_fs_info = sbi;
1478ok:
1479 fc->fs_private = opts;
1480 fc->ops = &ntfs_context_ops;
1481
1482 return 0;
Kari Argillander27fac772021-09-07 18:35:53 +03001483free_sbi:
1484 kfree(sbi);
Colin Ian King880301b2021-09-10 11:02:02 +01001485free_opts:
1486 kfree(opts);
Kari Argillander27fac772021-09-07 18:35:53 +03001487 return -ENOMEM;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001488}
1489
1490// clang-format off
1491static struct file_system_type ntfs_fs_type = {
Kari Argillander610f8f52021-09-07 18:35:52 +03001492 .owner = THIS_MODULE,
1493 .name = "ntfs3",
1494 .init_fs_context = ntfs_init_fs_context,
1495 .parameters = ntfs_fs_parameters,
1496 .kill_sb = kill_block_super,
1497 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
Konstantin Komarov82cae262021-08-13 17:21:29 +03001498};
1499// clang-format on
1500
1501static int __init init_ntfs_fs(void)
1502{
1503 int err;
1504
Kari Argillander2e3a51b2021-08-29 17:42:39 +03001505 pr_info("ntfs3: Max link count %u\n", NTFS_LINK_MAX);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001506
Kari Argillander2e3a51b2021-08-29 17:42:39 +03001507 if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL))
1508 pr_info("ntfs3: Enabled Linux POSIX ACLs support\n");
1509 if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER))
1510 pr_notice("ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n");
1511 if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS))
1512 pr_info("ntfs3: Read-only LZX/Xpress compression included\n");
Konstantin Komarov82cae262021-08-13 17:21:29 +03001513
1514 err = ntfs3_init_bitmap();
1515 if (err)
1516 return err;
1517
1518 ntfs_inode_cachep = kmem_cache_create(
1519 "ntfs_inode_cache", sizeof(struct ntfs_inode), 0,
1520 (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1521 init_once);
1522 if (!ntfs_inode_cachep) {
1523 err = -ENOMEM;
1524 goto out1;
1525 }
1526
1527 err = register_filesystem(&ntfs_fs_type);
1528 if (err)
1529 goto out;
1530
1531 return 0;
1532out:
1533 kmem_cache_destroy(ntfs_inode_cachep);
1534out1:
1535 ntfs3_exit_bitmap();
1536 return err;
1537}
1538
1539static void __exit exit_ntfs_fs(void)
1540{
Konstantin Komarovae6b47b2022-09-12 18:54:06 +03001541 rcu_barrier();
1542 kmem_cache_destroy(ntfs_inode_cachep);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001543 unregister_filesystem(&ntfs_fs_type);
1544 ntfs3_exit_bitmap();
1545}
1546
1547MODULE_LICENSE("GPL");
1548MODULE_DESCRIPTION("ntfs3 read/write filesystem");
Konstantin Komarov82cae262021-08-13 17:21:29 +03001549#ifdef CONFIG_NTFS3_FS_POSIX_ACL
1550MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support");
1551#endif
1552#ifdef CONFIG_NTFS3_64BIT_CLUSTER
Kari Argillander2e3a51b2021-08-29 17:42:39 +03001553MODULE_INFO(cluster, "Warning: Activated 64 bits per cluster. Windows does not support this");
Konstantin Komarov82cae262021-08-13 17:21:29 +03001554#endif
1555#ifdef CONFIG_NTFS3_LZX_XPRESS
1556MODULE_INFO(compression, "Read-only lzx/xpress compression included");
1557#endif
1558
1559MODULE_AUTHOR("Konstantin Komarov");
1560MODULE_ALIAS_FS("ntfs3");
1561
1562module_init(init_ntfs_fs);
1563module_exit(exit_ntfs_fs);