blob: 503e2e23f711e9c66ede31dde62ac64ca3c44f76 [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,
231 Opt_no_acs_rules,
232 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),
249 fsparam_flag("no_acs_rules", Opt_no_acs_rules),
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.");
297 opts->uid = 1;
298 break;
299 case Opt_gid:
300 opts->fs_gid = make_kgid(current_user_ns(), result.uint_32);
301 if (!gid_valid(opts->fs_gid))
302 return invalf(fc, "ntfs3: Invalid value for gid.");
303 opts->gid = 1;
304 break;
305 case Opt_umask:
306 if (result.uint_32 & ~07777)
307 return invalf(fc, "ntfs3: Invalid value for umask.");
308 opts->fs_fmask_inv = ~result.uint_32;
309 opts->fs_dmask_inv = ~result.uint_32;
310 opts->fmask = 1;
311 opts->dmask = 1;
312 break;
313 case Opt_dmask:
314 if (result.uint_32 & ~07777)
315 return invalf(fc, "ntfs3: Invalid value for dmask.");
316 opts->fs_dmask_inv = ~result.uint_32;
317 opts->dmask = 1;
318 break;
319 case Opt_fmask:
320 if (result.uint_32 & ~07777)
321 return invalf(fc, "ntfs3: Invalid value for fmask.");
322 opts->fs_fmask_inv = ~result.uint_32;
323 opts->fmask = 1;
324 break;
325 case Opt_immutable:
326 opts->sys_immutable = result.negated ? 0 : 1;
327 break;
328 case Opt_discard:
329 opts->discard = result.negated ? 0 : 1;
330 break;
331 case Opt_force:
332 opts->force = result.negated ? 0 : 1;
333 break;
334 case Opt_sparse:
335 opts->sparse = result.negated ? 0 : 1;
336 break;
337 case Opt_nohidden:
Kari Argillander9d1939f2021-09-07 18:35:54 +0300338 opts->nohidden = result.negated ? 1 : 0;
Kari Argillander610f8f52021-09-07 18:35:52 +0300339 break;
340 case Opt_acl:
341 if (!result.negated)
Konstantin Komarov82cae262021-08-13 17:21:29 +0300342#ifdef CONFIG_NTFS3_FS_POSIX_ACL
Kari Argillander610f8f52021-09-07 18:35:52 +0300343 fc->sb_flags |= SB_POSIXACL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300344#else
Kari Argillander610f8f52021-09-07 18:35:52 +0300345 return invalf(fc, "ntfs3: Support for ACL not compiled in!");
Konstantin Komarov82cae262021-08-13 17:21:29 +0300346#endif
Kari Argillander610f8f52021-09-07 18:35:52 +0300347 else
348 fc->sb_flags &= ~SB_POSIXACL;
349 break;
350 case Opt_showmeta:
351 opts->showmeta = result.negated ? 0 : 1;
352 break;
Kari Argillandere274cde2021-09-07 18:35:55 +0300353 case Opt_iocharset:
Kari Argillander610f8f52021-09-07 18:35:52 +0300354 kfree(opts->nls_name);
355 opts->nls_name = param->string;
356 param->string = NULL;
357 break;
358 case Opt_prealloc:
359 opts->prealloc = result.negated ? 0 : 1;
360 break;
361 case Opt_no_acs_rules:
362 opts->no_acs_rules = 1;
363 break;
364 default:
365 /* Should not be here unless we forget add case. */
366 return -EINVAL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300367 }
Konstantin Komarov82cae262021-08-13 17:21:29 +0300368 return 0;
369}
370
Kari Argillander610f8f52021-09-07 18:35:52 +0300371static int ntfs_fs_reconfigure(struct fs_context *fc)
Konstantin Komarov82cae262021-08-13 17:21:29 +0300372{
Kari Argillander610f8f52021-09-07 18:35:52 +0300373 struct super_block *sb = fc->root->d_sb;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300374 struct ntfs_sb_info *sbi = sb->s_fs_info;
Kari Argillander610f8f52021-09-07 18:35:52 +0300375 struct ntfs_mount_options *new_opts = fc->fs_private;
376 int ro_rw;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300377
Kari Argillander610f8f52021-09-07 18:35:52 +0300378 ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300379 if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) {
Kari Argillander610f8f52021-09-07 18:35:52 +0300380 errorf(fc, "ntfs3: Couldn't remount rw because journal is not replayed. Please umount/remount instead\n");
381 return -EINVAL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300382 }
383
Kari Argillander610f8f52021-09-07 18:35:52 +0300384 new_opts->nls = ntfs_load_nls(new_opts->nls_name);
385 if (IS_ERR(new_opts->nls)) {
386 new_opts->nls = NULL;
Kari Argillandere274cde2021-09-07 18:35:55 +0300387 errorf(fc, "ntfs3: Cannot load iocharset %s", new_opts->nls_name);
Kari Argillander610f8f52021-09-07 18:35:52 +0300388 return -EINVAL;
389 }
390 if (new_opts->nls != sbi->options->nls)
Kari Argillandere274cde2021-09-07 18:35:55 +0300391 return invalf(fc, "ntfs3: Cannot use different iocharset when remounting!");
Kari Argillander610f8f52021-09-07 18:35:52 +0300392
Konstantin Komarov82cae262021-08-13 17:21:29 +0300393 sync_filesystem(sb);
394
395 if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) &&
Kari Argillander610f8f52021-09-07 18:35:52 +0300396 !new_opts->force) {
397 errorf(fc, "ntfs3: Volume is dirty and \"force\" flag is not set!");
398 return -EINVAL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300399 }
400
Kari Argillander610f8f52021-09-07 18:35:52 +0300401 memcpy(sbi->options, new_opts, sizeof(*new_opts));
Konstantin Komarov82cae262021-08-13 17:21:29 +0300402
Kari Argillander610f8f52021-09-07 18:35:52 +0300403 return 0;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300404}
405
406static struct kmem_cache *ntfs_inode_cachep;
407
408static struct inode *ntfs_alloc_inode(struct super_block *sb)
409{
410 struct ntfs_inode *ni = kmem_cache_alloc(ntfs_inode_cachep, GFP_NOFS);
411
412 if (!ni)
413 return NULL;
414
415 memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode));
416
417 mutex_init(&ni->ni_lock);
418
419 return &ni->vfs_inode;
420}
421
422static void ntfs_i_callback(struct rcu_head *head)
423{
424 struct inode *inode = container_of(head, struct inode, i_rcu);
425 struct ntfs_inode *ni = ntfs_i(inode);
426
427 mutex_destroy(&ni->ni_lock);
428
429 kmem_cache_free(ntfs_inode_cachep, ni);
430}
431
432static void ntfs_destroy_inode(struct inode *inode)
433{
434 call_rcu(&inode->i_rcu, ntfs_i_callback);
435}
436
437static void init_once(void *foo)
438{
439 struct ntfs_inode *ni = foo;
440
441 inode_init_once(&ni->vfs_inode);
442}
443
Kari Argillandere8b8e972021-08-03 14:57:09 +0300444/*
445 * put_ntfs - Noinline to reduce binary size.
446 */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300447static noinline void put_ntfs(struct ntfs_sb_info *sbi)
448{
Kari Argillander195c52b2021-08-24 21:37:07 +0300449 kfree(sbi->new_rec);
450 kvfree(ntfs_put_shared(sbi->upcase));
451 kfree(sbi->def_table);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300452
453 wnd_close(&sbi->mft.bitmap);
454 wnd_close(&sbi->used.bitmap);
455
456 if (sbi->mft.ni)
457 iput(&sbi->mft.ni->vfs_inode);
458
459 if (sbi->security.ni)
460 iput(&sbi->security.ni->vfs_inode);
461
462 if (sbi->reparse.ni)
463 iput(&sbi->reparse.ni->vfs_inode);
464
465 if (sbi->objid.ni)
466 iput(&sbi->objid.ni->vfs_inode);
467
468 if (sbi->volume.ni)
469 iput(&sbi->volume.ni->vfs_inode);
470
471 ntfs_update_mftmirr(sbi, 0);
472
473 indx_clear(&sbi->security.index_sii);
474 indx_clear(&sbi->security.index_sdh);
475 indx_clear(&sbi->reparse.index_r);
476 indx_clear(&sbi->objid.index_o);
Kari Argillander195c52b2021-08-24 21:37:07 +0300477 kfree(sbi->compress.lznt);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300478#ifdef CONFIG_NTFS3_LZX_XPRESS
479 xpress_free_decompressor(sbi->compress.xpress);
480 lzx_free_decompressor(sbi->compress.lzx);
481#endif
Kari Argillander195c52b2021-08-24 21:37:07 +0300482 kfree(sbi);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300483}
484
485static void ntfs_put_super(struct super_block *sb)
486{
487 struct ntfs_sb_info *sbi = sb->s_fs_info;
488
Kari Argillandere8b8e972021-08-03 14:57:09 +0300489 /* Mark rw ntfs as clear, if possible. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300490 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
491
Kari Argillander610f8f52021-09-07 18:35:52 +0300492 put_mount_options(sbi->options);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300493 put_ntfs(sbi);
Kari Argillander610f8f52021-09-07 18:35:52 +0300494 sb->s_fs_info = NULL;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300495
496 sync_blockdev(sb->s_bdev);
497}
498
499static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf)
500{
501 struct super_block *sb = dentry->d_sb;
502 struct ntfs_sb_info *sbi = sb->s_fs_info;
503 struct wnd_bitmap *wnd = &sbi->used.bitmap;
504
505 buf->f_type = sb->s_magic;
506 buf->f_bsize = sbi->cluster_size;
507 buf->f_blocks = wnd->nbits;
508
509 buf->f_bfree = buf->f_bavail = wnd_zeroes(wnd);
510 buf->f_fsid.val[0] = sbi->volume.ser_num;
511 buf->f_fsid.val[1] = (sbi->volume.ser_num >> 32);
512 buf->f_namelen = NTFS_NAME_LEN;
513
514 return 0;
515}
516
517static int ntfs_show_options(struct seq_file *m, struct dentry *root)
518{
519 struct super_block *sb = root->d_sb;
520 struct ntfs_sb_info *sbi = sb->s_fs_info;
Kari Argillander564c97b2021-09-07 18:35:51 +0300521 struct ntfs_mount_options *opts = sbi->options;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300522 struct user_namespace *user_ns = seq_user_ns(m);
523
524 if (opts->uid)
525 seq_printf(m, ",uid=%u",
526 from_kuid_munged(user_ns, opts->fs_uid));
527 if (opts->gid)
528 seq_printf(m, ",gid=%u",
529 from_kgid_munged(user_ns, opts->fs_gid));
530 if (opts->fmask)
531 seq_printf(m, ",fmask=%04o", ~opts->fs_fmask_inv);
532 if (opts->dmask)
533 seq_printf(m, ",dmask=%04o", ~opts->fs_dmask_inv);
534 if (opts->nls)
Kari Argillandere274cde2021-09-07 18:35:55 +0300535 seq_printf(m, ",iocharset=%s", opts->nls->charset);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300536 else
Kari Argillandere274cde2021-09-07 18:35:55 +0300537 seq_puts(m, ",iocharset=utf8");
Konstantin Komarov82cae262021-08-13 17:21:29 +0300538 if (opts->sys_immutable)
539 seq_puts(m, ",sys_immutable");
540 if (opts->discard)
541 seq_puts(m, ",discard");
542 if (opts->sparse)
543 seq_puts(m, ",sparse");
544 if (opts->showmeta)
545 seq_puts(m, ",showmeta");
546 if (opts->nohidden)
547 seq_puts(m, ",nohidden");
548 if (opts->force)
549 seq_puts(m, ",force");
550 if (opts->no_acs_rules)
551 seq_puts(m, ",no_acs_rules");
552 if (opts->prealloc)
553 seq_puts(m, ",prealloc");
554 if (sb->s_flags & SB_POSIXACL)
555 seq_puts(m, ",acl");
Konstantin Komarov82cae262021-08-13 17:21:29 +0300556
557 return 0;
558}
559
Kari Argillandere8b8e972021-08-03 14:57:09 +0300560/*
561 * ntfs_sync_fs - super_operations::sync_fs
562 */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300563static int ntfs_sync_fs(struct super_block *sb, int wait)
564{
565 int err = 0, err2;
566 struct ntfs_sb_info *sbi = sb->s_fs_info;
567 struct ntfs_inode *ni;
568 struct inode *inode;
569
570 ni = sbi->security.ni;
571 if (ni) {
572 inode = &ni->vfs_inode;
573 err2 = _ni_write_inode(inode, wait);
574 if (err2 && !err)
575 err = err2;
576 }
577
578 ni = sbi->objid.ni;
579 if (ni) {
580 inode = &ni->vfs_inode;
581 err2 = _ni_write_inode(inode, wait);
582 if (err2 && !err)
583 err = err2;
584 }
585
586 ni = sbi->reparse.ni;
587 if (ni) {
588 inode = &ni->vfs_inode;
589 err2 = _ni_write_inode(inode, wait);
590 if (err2 && !err)
591 err = err2;
592 }
593
594 if (!err)
595 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
596
597 ntfs_update_mftmirr(sbi, wait);
598
599 return err;
600}
601
602static const struct super_operations ntfs_sops = {
603 .alloc_inode = ntfs_alloc_inode,
604 .destroy_inode = ntfs_destroy_inode,
605 .evict_inode = ntfs_evict_inode,
606 .put_super = ntfs_put_super,
607 .statfs = ntfs_statfs,
608 .show_options = ntfs_show_options,
609 .sync_fs = ntfs_sync_fs,
Konstantin Komarov82cae262021-08-13 17:21:29 +0300610 .write_inode = ntfs3_write_inode,
611};
612
613static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino,
614 u32 generation)
615{
616 struct MFT_REF ref;
617 struct inode *inode;
618
619 ref.low = cpu_to_le32(ino);
620#ifdef CONFIG_NTFS3_64BIT_CLUSTER
621 ref.high = cpu_to_le16(ino >> 32);
622#else
623 ref.high = 0;
624#endif
625 ref.seq = cpu_to_le16(generation);
626
627 inode = ntfs_iget5(sb, &ref, NULL);
628 if (!IS_ERR(inode) && is_bad_inode(inode)) {
629 iput(inode);
630 inode = ERR_PTR(-ESTALE);
631 }
632
633 return inode;
634}
635
636static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
637 int fh_len, int fh_type)
638{
639 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
640 ntfs_export_get_inode);
641}
642
643static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
644 int fh_len, int fh_type)
645{
646 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
647 ntfs_export_get_inode);
648}
649
650/* TODO: == ntfs_sync_inode */
651static int ntfs_nfs_commit_metadata(struct inode *inode)
652{
653 return _ni_write_inode(inode, 1);
654}
655
656static const struct export_operations ntfs_export_ops = {
657 .fh_to_dentry = ntfs_fh_to_dentry,
658 .fh_to_parent = ntfs_fh_to_parent,
659 .get_parent = ntfs3_get_parent,
660 .commit_metadata = ntfs_nfs_commit_metadata,
661};
662
Kari Argillandere8b8e972021-08-03 14:57:09 +0300663/*
664 * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb".
665 */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300666static u32 format_size_gb(const u64 bytes, u32 *mb)
667{
Kari Argillandere8b8e972021-08-03 14:57:09 +0300668 /* Do simple right 30 bit shift of 64 bit value. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300669 u64 kbytes = bytes >> 10;
670 u32 kbytes32 = kbytes;
671
672 *mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20;
673 if (*mb >= 100)
674 *mb = 99;
675
676 return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12);
677}
678
679static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot)
680{
681 return boot->sectors_per_clusters <= 0x80
682 ? boot->sectors_per_clusters
683 : (1u << (0 - boot->sectors_per_clusters));
684}
685
Kari Argillandere8b8e972021-08-03 14:57:09 +0300686/*
687 * ntfs_init_from_boot - Init internal info from on-disk boot sector.
688 */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300689static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
690 u64 dev_size)
691{
692 struct ntfs_sb_info *sbi = sb->s_fs_info;
693 int err;
694 u32 mb, gb, boot_sector_size, sct_per_clst, record_size;
695 u64 sectors, clusters, fs_size, mlcn, mlcn2;
696 struct NTFS_BOOT *boot;
697 struct buffer_head *bh;
698 struct MFT_REC *rec;
699 u16 fn, ao;
700
701 sbi->volume.blocks = dev_size >> PAGE_SHIFT;
702
703 bh = ntfs_bread(sb, 0);
704 if (!bh)
705 return -EIO;
706
707 err = -EINVAL;
708 boot = (struct NTFS_BOOT *)bh->b_data;
709
710 if (memcmp(boot->system_id, "NTFS ", sizeof("NTFS ") - 1))
711 goto out;
712
713 /* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/
714 /*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1])
715 * goto out;
716 */
717
718 boot_sector_size = (u32)boot->bytes_per_sector[1] << 8;
719 if (boot->bytes_per_sector[0] || boot_sector_size < SECTOR_SIZE ||
Kari Argillander528c9b32021-08-16 13:37:32 +0300720 !is_power_of_2(boot_sector_size)) {
Konstantin Komarov82cae262021-08-13 17:21:29 +0300721 goto out;
722 }
723
724 /* cluster size: 512, 1K, 2K, 4K, ... 2M */
725 sct_per_clst = true_sectors_per_clst(boot);
Kari Argillander528c9b32021-08-16 13:37:32 +0300726 if (!is_power_of_2(sct_per_clst))
Konstantin Komarov82cae262021-08-13 17:21:29 +0300727 goto out;
728
729 mlcn = le64_to_cpu(boot->mft_clst);
730 mlcn2 = le64_to_cpu(boot->mft2_clst);
731 sectors = le64_to_cpu(boot->sectors_per_volume);
732
733 if (mlcn * sct_per_clst >= sectors)
734 goto out;
735
736 if (mlcn2 * sct_per_clst >= sectors)
737 goto out;
738
Kari Argillandere8b8e972021-08-03 14:57:09 +0300739 /* Check MFT record size. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300740 if ((boot->record_size < 0 &&
741 SECTOR_SIZE > (2U << (-boot->record_size))) ||
Kari Argillander528c9b32021-08-16 13:37:32 +0300742 (boot->record_size >= 0 && !is_power_of_2(boot->record_size))) {
Konstantin Komarov82cae262021-08-13 17:21:29 +0300743 goto out;
744 }
745
Kari Argillandere8b8e972021-08-03 14:57:09 +0300746 /* Check index record size. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300747 if ((boot->index_size < 0 &&
748 SECTOR_SIZE > (2U << (-boot->index_size))) ||
Kari Argillander528c9b32021-08-16 13:37:32 +0300749 (boot->index_size >= 0 && !is_power_of_2(boot->index_size))) {
Konstantin Komarov82cae262021-08-13 17:21:29 +0300750 goto out;
751 }
752
753 sbi->sector_size = boot_sector_size;
754 sbi->sector_bits = blksize_bits(boot_sector_size);
755 fs_size = (sectors + 1) << sbi->sector_bits;
756
757 gb = format_size_gb(fs_size, &mb);
758
759 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300760 * - Volume formatted and mounted with the same sector size.
761 * - Volume formatted 4K and mounted as 512.
762 * - Volume formatted 512 and mounted as 4K.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300763 */
764 if (sbi->sector_size != sector_size) {
765 ntfs_warn(sb,
766 "Different NTFS' sector size and media sector size");
767 dev_size += sector_size - 1;
768 }
769
770 sbi->cluster_size = boot_sector_size * sct_per_clst;
771 sbi->cluster_bits = blksize_bits(sbi->cluster_size);
772
773 sbi->mft.lbo = mlcn << sbi->cluster_bits;
774 sbi->mft.lbo2 = mlcn2 << sbi->cluster_bits;
775
776 if (sbi->cluster_size < sbi->sector_size)
777 goto out;
778
779 sbi->cluster_mask = sbi->cluster_size - 1;
780 sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask;
781 sbi->record_size = record_size = boot->record_size < 0
782 ? 1 << (-boot->record_size)
783 : (u32)boot->record_size
784 << sbi->cluster_bits;
785
786 if (record_size > MAXIMUM_BYTES_PER_MFT)
787 goto out;
788
789 sbi->record_bits = blksize_bits(record_size);
790 sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes
791
792 sbi->max_bytes_per_attr =
Kari Argillanderfa3cacf2021-08-26 11:56:29 +0300793 record_size - ALIGN(MFTRECORD_FIXUP_OFFSET_1, 8) -
794 ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) -
795 ALIGN(sizeof(enum ATTR_TYPE), 8);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300796
797 sbi->index_size = boot->index_size < 0
798 ? 1u << (-boot->index_size)
799 : (u32)boot->index_size << sbi->cluster_bits;
800
801 sbi->volume.ser_num = le64_to_cpu(boot->serial_num);
802 sbi->volume.size = sectors << sbi->sector_bits;
803
Kari Argillandere8b8e972021-08-03 14:57:09 +0300804 /* Warning if RAW volume. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300805 if (dev_size < fs_size) {
806 u32 mb0, gb0;
807
808 gb0 = format_size_gb(dev_size, &mb0);
809 ntfs_warn(
810 sb,
811 "RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only",
812 gb, mb, gb0, mb0);
813 sb->s_flags |= SB_RDONLY;
814 }
815
816 clusters = sbi->volume.size >> sbi->cluster_bits;
817#ifndef CONFIG_NTFS3_64BIT_CLUSTER
Kari Argillandere8b8e972021-08-03 14:57:09 +0300818 /* 32 bits per cluster. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300819 if (clusters >> 32) {
820 ntfs_notice(
821 sb,
822 "NTFS %u.%02u Gb is too big to use 32 bits per cluster",
823 gb, mb);
824 goto out;
825 }
826#elif BITS_PER_LONG < 64
827#error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS"
828#endif
829
830 sbi->used.bitmap.nbits = clusters;
831
Kari Argillander195c52b2021-08-24 21:37:07 +0300832 rec = kzalloc(record_size, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300833 if (!rec) {
834 err = -ENOMEM;
835 goto out;
836 }
837
838 sbi->new_rec = rec;
839 rec->rhdr.sign = NTFS_FILE_SIGNATURE;
840 rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET_1);
841 fn = (sbi->record_size >> SECTOR_SHIFT) + 1;
842 rec->rhdr.fix_num = cpu_to_le16(fn);
Kari Argillanderfa3cacf2021-08-26 11:56:29 +0300843 ao = ALIGN(MFTRECORD_FIXUP_OFFSET_1 + sizeof(short) * fn, 8);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300844 rec->attr_off = cpu_to_le16(ao);
Kari Argillanderfa3cacf2021-08-26 11:56:29 +0300845 rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8));
Konstantin Komarov82cae262021-08-13 17:21:29 +0300846 rec->total = cpu_to_le32(sbi->record_size);
847 ((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END;
848
849 if (sbi->cluster_size < PAGE_SIZE)
850 sb_set_blocksize(sb, sbi->cluster_size);
851
852 sbi->block_mask = sb->s_blocksize - 1;
853 sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits;
854 sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits;
855
Kari Argillandere8b8e972021-08-03 14:57:09 +0300856 /* Maximum size for normal files. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300857 sbi->maxbytes = (clusters << sbi->cluster_bits) - 1;
858
859#ifdef CONFIG_NTFS3_64BIT_CLUSTER
860 if (clusters >= (1ull << (64 - sbi->cluster_bits)))
861 sbi->maxbytes = -1;
862 sbi->maxbytes_sparse = -1;
863#else
Kari Argillandere8b8e972021-08-03 14:57:09 +0300864 /* Maximum size for sparse file. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300865 sbi->maxbytes_sparse = (1ull << (sbi->cluster_bits + 32)) - 1;
866#endif
867
868 err = 0;
869
870out:
871 brelse(bh);
872
873 return err;
874}
875
Kari Argillandere8b8e972021-08-03 14:57:09 +0300876/*
877 * ntfs_fill_super - Try to mount.
878 */
Kari Argillander610f8f52021-09-07 18:35:52 +0300879static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
Konstantin Komarov82cae262021-08-13 17:21:29 +0300880{
881 int err;
Kari Argillander610f8f52021-09-07 18:35:52 +0300882 struct ntfs_sb_info *sbi = sb->s_fs_info;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300883 struct block_device *bdev = sb->s_bdev;
884 struct inode *bd_inode = bdev->bd_inode;
885 struct request_queue *rq = bdev_get_queue(bdev);
886 struct inode *inode = NULL;
887 struct ntfs_inode *ni;
888 size_t i, tt;
889 CLST vcn, lcn, len;
890 struct ATTRIB *attr;
891 const struct VOLUME_INFO *info;
892 u32 idx, done, bytes;
893 struct ATTR_DEF_ENTRY *t;
Kari Argillander27fac772021-09-07 18:35:53 +0300894 u16 *upcase;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300895 u16 *shared;
896 bool is_ro;
897 struct MFT_REF ref;
898
899 ref.high = 0;
900
Konstantin Komarov82cae262021-08-13 17:21:29 +0300901 sbi->sb = sb;
902 sb->s_flags |= SB_NODIRATIME;
903 sb->s_magic = 0x7366746e; // "ntfs"
904 sb->s_op = &ntfs_sops;
905 sb->s_export_op = &ntfs_export_ops;
906 sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec
907 sb->s_xattr = ntfs_xattr_handlers;
908
Kari Argillander610f8f52021-09-07 18:35:52 +0300909 sbi->options->nls = ntfs_load_nls(sbi->options->nls_name);
910 if (IS_ERR(sbi->options->nls)) {
911 sbi->options->nls = NULL;
912 errorf(fc, "Cannot load nls %s", sbi->options->nls_name);
913 return -EINVAL;
914 }
Konstantin Komarov82cae262021-08-13 17:21:29 +0300915
916 if (!rq || !blk_queue_discard(rq) || !rq->limits.discard_granularity) {
917 ;
918 } else {
919 sbi->discard_granularity = rq->limits.discard_granularity;
920 sbi->discard_granularity_mask_inv =
921 ~(u64)(sbi->discard_granularity - 1);
922 }
923
924 sb_set_blocksize(sb, PAGE_SIZE);
925
Kari Argillandere8b8e972021-08-03 14:57:09 +0300926 /* Parse boot. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300927 err = ntfs_init_from_boot(sb, rq ? queue_logical_block_size(rq) : 512,
928 bd_inode->i_size);
929 if (err)
930 goto out;
931
932#ifdef CONFIG_NTFS3_64BIT_CLUSTER
933 sb->s_maxbytes = MAX_LFS_FILESIZE;
934#else
935 sb->s_maxbytes = 0xFFFFFFFFull << sbi->cluster_bits;
936#endif
937
Konstantin Komarov82cae262021-08-13 17:21:29 +0300938 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300939 * Load $Volume. This should be done before $LogFile
940 * 'cause 'sbi->volume.ni' is used 'ntfs_set_state'.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300941 */
942 ref.low = cpu_to_le32(MFT_REC_VOL);
943 ref.seq = cpu_to_le16(MFT_REC_VOL);
944 inode = ntfs_iget5(sb, &ref, &NAME_VOLUME);
945 if (IS_ERR(inode)) {
946 err = PTR_ERR(inode);
947 ntfs_err(sb, "Failed to load $Volume.");
948 inode = NULL;
949 goto out;
950 }
951
952 ni = ntfs_i(inode);
953
Kari Argillandere8b8e972021-08-03 14:57:09 +0300954 /* Load and save label (not necessary). */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300955 attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL);
956
957 if (!attr) {
958 /* It is ok if no ATTR_LABEL */
959 } else if (!attr->non_res && !is_attr_ext(attr)) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300960 /* $AttrDef allows labels to be up to 128 symbols. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300961 err = utf16s_to_utf8s(resident_data(attr),
962 le32_to_cpu(attr->res.data_size) >> 1,
963 UTF16_LITTLE_ENDIAN, sbi->volume.label,
964 sizeof(sbi->volume.label));
965 if (err < 0)
966 sbi->volume.label[0] = 0;
967 } else {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300968 /* Should we break mounting here? */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300969 //err = -EINVAL;
970 //goto out;
971 }
972
973 attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL);
974 if (!attr || is_attr_ext(attr)) {
975 err = -EINVAL;
976 goto out;
977 }
978
979 info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO);
980 if (!info) {
981 err = -EINVAL;
982 goto out;
983 }
984
985 sbi->volume.major_ver = info->major_ver;
986 sbi->volume.minor_ver = info->minor_ver;
987 sbi->volume.flags = info->flags;
988
989 sbi->volume.ni = ni;
990 inode = NULL;
991
Kari Argillandere8b8e972021-08-03 14:57:09 +0300992 /* Load $MFTMirr to estimate recs_mirr. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300993 ref.low = cpu_to_le32(MFT_REC_MIRR);
994 ref.seq = cpu_to_le16(MFT_REC_MIRR);
995 inode = ntfs_iget5(sb, &ref, &NAME_MIRROR);
996 if (IS_ERR(inode)) {
997 err = PTR_ERR(inode);
998 ntfs_err(sb, "Failed to load $MFTMirr.");
999 inode = NULL;
1000 goto out;
1001 }
1002
1003 sbi->mft.recs_mirr =
1004 ntfs_up_cluster(sbi, inode->i_size) >> sbi->record_bits;
1005
1006 iput(inode);
1007
Konstantin Komarovd3624462021-08-31 16:57:40 +03001008 /* Load LogFile to replay. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001009 ref.low = cpu_to_le32(MFT_REC_LOG);
1010 ref.seq = cpu_to_le16(MFT_REC_LOG);
1011 inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE);
1012 if (IS_ERR(inode)) {
1013 err = PTR_ERR(inode);
1014 ntfs_err(sb, "Failed to load \x24LogFile.");
1015 inode = NULL;
1016 goto out;
1017 }
1018
1019 ni = ntfs_i(inode);
1020
1021 err = ntfs_loadlog_and_replay(ni, sbi);
1022 if (err)
1023 goto out;
1024
1025 iput(inode);
1026 inode = NULL;
1027
1028 is_ro = sb_rdonly(sbi->sb);
1029
1030 if (sbi->flags & NTFS_FLAGS_NEED_REPLAY) {
1031 if (!is_ro) {
1032 ntfs_warn(sb,
1033 "failed to replay log file. Can't mount rw!");
1034 err = -EINVAL;
1035 goto out;
1036 }
1037 } else if (sbi->volume.flags & VOLUME_FLAG_DIRTY) {
Kari Argillander564c97b2021-09-07 18:35:51 +03001038 if (!is_ro && !sbi->options->force) {
Konstantin Komarov82cae262021-08-13 17:21:29 +03001039 ntfs_warn(
1040 sb,
1041 "volume is dirty and \"force\" flag is not set!");
1042 err = -EINVAL;
1043 goto out;
1044 }
1045 }
1046
Kari Argillandere8b8e972021-08-03 14:57:09 +03001047 /* Load $MFT. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001048 ref.low = cpu_to_le32(MFT_REC_MFT);
1049 ref.seq = cpu_to_le16(1);
1050
1051 inode = ntfs_iget5(sb, &ref, &NAME_MFT);
1052 if (IS_ERR(inode)) {
1053 err = PTR_ERR(inode);
1054 ntfs_err(sb, "Failed to load $MFT.");
1055 inode = NULL;
1056 goto out;
1057 }
1058
1059 ni = ntfs_i(inode);
1060
1061 sbi->mft.used = ni->i_valid >> sbi->record_bits;
1062 tt = inode->i_size >> sbi->record_bits;
1063 sbi->mft.next_free = MFT_REC_USER;
1064
1065 err = wnd_init(&sbi->mft.bitmap, sb, tt);
1066 if (err)
1067 goto out;
1068
1069 err = ni_load_all_mi(ni);
1070 if (err)
1071 goto out;
1072
1073 sbi->mft.ni = ni;
1074
Kari Argillandere8b8e972021-08-03 14:57:09 +03001075 /* Load $BadClus. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001076 ref.low = cpu_to_le32(MFT_REC_BADCLUST);
1077 ref.seq = cpu_to_le16(MFT_REC_BADCLUST);
1078 inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS);
1079 if (IS_ERR(inode)) {
1080 err = PTR_ERR(inode);
1081 ntfs_err(sb, "Failed to load $BadClus.");
1082 inode = NULL;
1083 goto out;
1084 }
1085
1086 ni = ntfs_i(inode);
1087
1088 for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) {
1089 if (lcn == SPARSE_LCN)
1090 continue;
1091
1092 if (!sbi->bad_clusters)
1093 ntfs_notice(sb, "Volume contains bad blocks");
1094
1095 sbi->bad_clusters += len;
1096 }
1097
1098 iput(inode);
1099
Kari Argillandere8b8e972021-08-03 14:57:09 +03001100 /* Load $Bitmap. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001101 ref.low = cpu_to_le32(MFT_REC_BITMAP);
1102 ref.seq = cpu_to_le16(MFT_REC_BITMAP);
1103 inode = ntfs_iget5(sb, &ref, &NAME_BITMAP);
1104 if (IS_ERR(inode)) {
1105 err = PTR_ERR(inode);
1106 ntfs_err(sb, "Failed to load $Bitmap.");
1107 inode = NULL;
1108 goto out;
1109 }
1110
1111 ni = ntfs_i(inode);
1112
1113#ifndef CONFIG_NTFS3_64BIT_CLUSTER
1114 if (inode->i_size >> 32) {
1115 err = -EINVAL;
1116 goto out;
1117 }
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;
1124 goto out;
1125 }
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;
1129 err = wnd_init(&sbi->used.bitmap, sbi->sb, tt);
1130 if (err)
1131 goto out;
1132
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)
1138 goto out;
1139
Kari Argillandere8b8e972021-08-03 14:57:09 +03001140 /* Load $AttrDef. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001141 ref.low = cpu_to_le32(MFT_REC_ATTR);
1142 ref.seq = cpu_to_le16(MFT_REC_ATTR);
1143 inode = ntfs_iget5(sbi->sb, &ref, &NAME_ATTRDEF);
1144 if (IS_ERR(inode)) {
1145 err = PTR_ERR(inode);
1146 ntfs_err(sb, "Failed to load $AttrDef -> %d", err);
1147 inode = NULL;
1148 goto out;
1149 }
1150
1151 if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY)) {
1152 err = -EINVAL;
1153 goto out;
1154 }
1155 bytes = inode->i_size;
Kari Argillander195c52b2021-08-24 21:37:07 +03001156 sbi->def_table = t = kmalloc(bytes, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001157 if (!t) {
1158 err = -ENOMEM;
1159 goto out;
1160 }
1161
1162 for (done = idx = 0; done < bytes; done += PAGE_SIZE, idx++) {
1163 unsigned long tail = bytes - done;
1164 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1165
1166 if (IS_ERR(page)) {
1167 err = PTR_ERR(page);
1168 goto out;
1169 }
1170 memcpy(Add2Ptr(t, done), page_address(page),
1171 min(PAGE_SIZE, tail));
1172 ntfs_unmap_page(page);
1173
1174 if (!idx && ATTR_STD != t->type) {
1175 err = -EINVAL;
1176 goto out;
1177 }
1178 }
1179
1180 t += 1;
1181 sbi->def_entries = 1;
1182 done = sizeof(struct ATTR_DEF_ENTRY);
1183 sbi->reparse.max_size = MAXIMUM_REPARSE_DATA_BUFFER_SIZE;
Colin Ian Kingf8d87ed2021-08-16 11:13:08 +01001184 sbi->ea_max_size = 0x10000; /* default formatter value */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001185
1186 while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) {
1187 u32 t32 = le32_to_cpu(t->type);
1188 u64 sz = le64_to_cpu(t->max_sz);
1189
1190 if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32)
1191 break;
1192
1193 if (t->type == ATTR_REPARSE)
1194 sbi->reparse.max_size = sz;
1195 else if (t->type == ATTR_EA)
1196 sbi->ea_max_size = sz;
1197
1198 done += sizeof(struct ATTR_DEF_ENTRY);
1199 t += 1;
1200 sbi->def_entries += 1;
1201 }
1202 iput(inode);
1203
Kari Argillandere8b8e972021-08-03 14:57:09 +03001204 /* Load $UpCase. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001205 ref.low = cpu_to_le32(MFT_REC_UPCASE);
1206 ref.seq = cpu_to_le16(MFT_REC_UPCASE);
1207 inode = ntfs_iget5(sb, &ref, &NAME_UPCASE);
1208 if (IS_ERR(inode)) {
1209 err = PTR_ERR(inode);
1210 ntfs_err(sb, "Failed to load \x24LogFile.");
1211 inode = NULL;
1212 goto out;
1213 }
1214
1215 ni = ntfs_i(inode);
1216
1217 if (inode->i_size != 0x10000 * sizeof(short)) {
1218 err = -EINVAL;
1219 goto out;
1220 }
1221
Kari Argillander27fac772021-09-07 18:35:53 +03001222 upcase = sbi->upcase;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001223
1224 for (idx = 0; idx < (0x10000 * sizeof(short) >> PAGE_SHIFT); idx++) {
1225 const __le16 *src;
1226 u16 *dst = Add2Ptr(upcase, idx << PAGE_SHIFT);
1227 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1228
1229 if (IS_ERR(page)) {
1230 err = PTR_ERR(page);
1231 goto out;
1232 }
1233
1234 src = page_address(page);
1235
1236#ifdef __BIG_ENDIAN
1237 for (i = 0; i < PAGE_SIZE / sizeof(u16); i++)
1238 *dst++ = le16_to_cpu(*src++);
1239#else
1240 memcpy(dst, src, PAGE_SIZE);
1241#endif
1242 ntfs_unmap_page(page);
1243 }
1244
1245 shared = ntfs_set_shared(upcase, 0x10000 * sizeof(short));
1246 if (shared && upcase != shared) {
1247 sbi->upcase = shared;
Kari Argillander195c52b2021-08-24 21:37:07 +03001248 kvfree(upcase);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001249 }
1250
1251 iput(inode);
1252 inode = NULL;
1253
1254 if (is_ntfs3(sbi)) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001255 /* Load $Secure. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001256 err = ntfs_security_init(sbi);
1257 if (err)
1258 goto out;
1259
Kari Argillandere8b8e972021-08-03 14:57:09 +03001260 /* Load $Extend. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001261 err = ntfs_extend_init(sbi);
1262 if (err)
1263 goto load_root;
1264
Kari Argillandere8b8e972021-08-03 14:57:09 +03001265 /* Load $Extend\$Reparse. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001266 err = ntfs_reparse_init(sbi);
1267 if (err)
1268 goto load_root;
1269
Kari Argillandere8b8e972021-08-03 14:57:09 +03001270 /* Load $Extend\$ObjId. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001271 err = ntfs_objid_init(sbi);
1272 if (err)
1273 goto load_root;
1274 }
1275
1276load_root:
Kari Argillandere8b8e972021-08-03 14:57:09 +03001277 /* Load root. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001278 ref.low = cpu_to_le32(MFT_REC_ROOT);
1279 ref.seq = cpu_to_le16(MFT_REC_ROOT);
1280 inode = ntfs_iget5(sb, &ref, &NAME_ROOT);
1281 if (IS_ERR(inode)) {
1282 err = PTR_ERR(inode);
1283 ntfs_err(sb, "Failed to load root.");
1284 inode = NULL;
1285 goto out;
1286 }
1287
1288 ni = ntfs_i(inode);
1289
1290 sb->s_root = d_make_root(inode);
1291
1292 if (!sb->s_root) {
1293 err = -EINVAL;
1294 goto out;
1295 }
1296
Kari Argillander610f8f52021-09-07 18:35:52 +03001297 fc->fs_private = NULL;
1298 fc->s_fs_info = NULL;
1299
Konstantin Komarov82cae262021-08-13 17:21:29 +03001300 return 0;
1301
1302out:
1303 iput(inode);
1304
1305 if (sb->s_root) {
1306 d_drop(sb->s_root);
1307 sb->s_root = NULL;
1308 }
1309
Konstantin Komarov82cae262021-08-13 17:21:29 +03001310 return err;
1311}
1312
1313void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len)
1314{
1315 struct ntfs_sb_info *sbi = sb->s_fs_info;
1316 struct block_device *bdev = sb->s_bdev;
1317 sector_t devblock = (u64)lcn * sbi->blocks_per_cluster;
1318 unsigned long blocks = (u64)len * sbi->blocks_per_cluster;
1319 unsigned long cnt = 0;
1320 unsigned long limit = global_zone_page_state(NR_FREE_PAGES)
1321 << (PAGE_SHIFT - sb->s_blocksize_bits);
1322
1323 if (limit >= 0x2000)
1324 limit -= 0x1000;
1325 else if (limit < 32)
1326 limit = 32;
1327 else
1328 limit >>= 1;
1329
1330 while (blocks--) {
1331 clean_bdev_aliases(bdev, devblock++, 1);
1332 if (cnt++ >= limit) {
1333 sync_blockdev(bdev);
1334 cnt = 0;
1335 }
1336 }
1337}
1338
1339/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001340 * ntfs_discard - Issue a discard request (trim for SSD).
Konstantin Komarov82cae262021-08-13 17:21:29 +03001341 */
1342int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len)
1343{
1344 int err;
1345 u64 lbo, bytes, start, end;
1346 struct super_block *sb;
1347
1348 if (sbi->used.next_free_lcn == lcn + len)
1349 sbi->used.next_free_lcn = lcn;
1350
1351 if (sbi->flags & NTFS_FLAGS_NODISCARD)
1352 return -EOPNOTSUPP;
1353
Kari Argillander564c97b2021-09-07 18:35:51 +03001354 if (!sbi->options->discard)
Konstantin Komarov82cae262021-08-13 17:21:29 +03001355 return -EOPNOTSUPP;
1356
1357 lbo = (u64)lcn << sbi->cluster_bits;
1358 bytes = (u64)len << sbi->cluster_bits;
1359
Kari Argillandere8b8e972021-08-03 14:57:09 +03001360 /* Align up 'start' on discard_granularity. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001361 start = (lbo + sbi->discard_granularity - 1) &
1362 sbi->discard_granularity_mask_inv;
Kari Argillandere8b8e972021-08-03 14:57:09 +03001363 /* Align down 'end' on discard_granularity. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001364 end = (lbo + bytes) & sbi->discard_granularity_mask_inv;
1365
1366 sb = sbi->sb;
1367 if (start >= end)
1368 return 0;
1369
1370 err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9,
1371 GFP_NOFS, 0);
1372
1373 if (err == -EOPNOTSUPP)
1374 sbi->flags |= NTFS_FLAGS_NODISCARD;
1375
1376 return err;
1377}
1378
Kari Argillander610f8f52021-09-07 18:35:52 +03001379static int ntfs_fs_get_tree(struct fs_context *fc)
Konstantin Komarov82cae262021-08-13 17:21:29 +03001380{
Kari Argillander610f8f52021-09-07 18:35:52 +03001381 return get_tree_bdev(fc, ntfs_fill_super);
1382}
1383
1384/*
1385 * ntfs_fs_free - Free fs_context.
1386 *
1387 * Note that this will be called after fill_super and reconfigure
1388 * even when they pass. So they have to take pointers if they pass.
1389 */
1390static void ntfs_fs_free(struct fs_context *fc)
1391{
1392 struct ntfs_mount_options *opts = fc->fs_private;
1393 struct ntfs_sb_info *sbi = fc->s_fs_info;
1394
1395 if (sbi)
1396 put_ntfs(sbi);
1397
1398 if (opts)
1399 put_mount_options(opts);
1400}
1401
1402static const struct fs_context_operations ntfs_context_ops = {
1403 .parse_param = ntfs_fs_parse_param,
1404 .get_tree = ntfs_fs_get_tree,
1405 .reconfigure = ntfs_fs_reconfigure,
1406 .free = ntfs_fs_free,
1407};
1408
1409/*
1410 * ntfs_init_fs_context - Initialize spi and opts
1411 *
1412 * This will called when mount/remount. We will first initiliaze
1413 * options so that if remount we can use just that.
1414 */
1415static int ntfs_init_fs_context(struct fs_context *fc)
1416{
1417 struct ntfs_mount_options *opts;
1418 struct ntfs_sb_info *sbi;
1419
1420 opts = kzalloc(sizeof(struct ntfs_mount_options), GFP_NOFS);
1421 if (!opts)
1422 return -ENOMEM;
1423
1424 /* Default options. */
1425 opts->fs_uid = current_uid();
1426 opts->fs_gid = current_gid();
1427 opts->fs_fmask_inv = ~current_umask();
1428 opts->fs_dmask_inv = ~current_umask();
1429
1430 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
1431 goto ok;
1432
1433 sbi = kzalloc(sizeof(struct ntfs_sb_info), GFP_NOFS);
Kari Argillander27fac772021-09-07 18:35:53 +03001434 if (!sbi)
1435 goto free_opts;
1436
1437 sbi->upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL);
1438 if (!sbi->upcase)
1439 goto free_sbi;
1440
1441 ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL,
1442 DEFAULT_RATELIMIT_BURST);
1443
1444 mutex_init(&sbi->compress.mtx_lznt);
1445#ifdef CONFIG_NTFS3_LZX_XPRESS
1446 mutex_init(&sbi->compress.mtx_xpress);
1447 mutex_init(&sbi->compress.mtx_lzx);
1448#endif
Kari Argillander610f8f52021-09-07 18:35:52 +03001449
1450 sbi->options = opts;
1451 fc->s_fs_info = sbi;
1452ok:
1453 fc->fs_private = opts;
1454 fc->ops = &ntfs_context_ops;
1455
1456 return 0;
Kari Argillander27fac772021-09-07 18:35:53 +03001457free_opts:
1458 kfree(opts);
1459free_sbi:
1460 kfree(sbi);
1461 return -ENOMEM;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001462}
1463
1464// clang-format off
1465static struct file_system_type ntfs_fs_type = {
Kari Argillander610f8f52021-09-07 18:35:52 +03001466 .owner = THIS_MODULE,
1467 .name = "ntfs3",
1468 .init_fs_context = ntfs_init_fs_context,
1469 .parameters = ntfs_fs_parameters,
1470 .kill_sb = kill_block_super,
1471 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
Konstantin Komarov82cae262021-08-13 17:21:29 +03001472};
1473// clang-format on
1474
1475static int __init init_ntfs_fs(void)
1476{
1477 int err;
1478
Kari Argillander2e3a51b2021-08-29 17:42:39 +03001479 pr_info("ntfs3: Max link count %u\n", NTFS_LINK_MAX);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001480
Kari Argillander2e3a51b2021-08-29 17:42:39 +03001481 if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL))
1482 pr_info("ntfs3: Enabled Linux POSIX ACLs support\n");
1483 if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER))
1484 pr_notice("ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n");
1485 if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS))
1486 pr_info("ntfs3: Read-only LZX/Xpress compression included\n");
Konstantin Komarov82cae262021-08-13 17:21:29 +03001487
1488 err = ntfs3_init_bitmap();
1489 if (err)
1490 return err;
1491
1492 ntfs_inode_cachep = kmem_cache_create(
1493 "ntfs_inode_cache", sizeof(struct ntfs_inode), 0,
1494 (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1495 init_once);
1496 if (!ntfs_inode_cachep) {
1497 err = -ENOMEM;
1498 goto out1;
1499 }
1500
1501 err = register_filesystem(&ntfs_fs_type);
1502 if (err)
1503 goto out;
1504
1505 return 0;
1506out:
1507 kmem_cache_destroy(ntfs_inode_cachep);
1508out1:
1509 ntfs3_exit_bitmap();
1510 return err;
1511}
1512
1513static void __exit exit_ntfs_fs(void)
1514{
1515 if (ntfs_inode_cachep) {
1516 rcu_barrier();
1517 kmem_cache_destroy(ntfs_inode_cachep);
1518 }
1519
1520 unregister_filesystem(&ntfs_fs_type);
1521 ntfs3_exit_bitmap();
1522}
1523
1524MODULE_LICENSE("GPL");
1525MODULE_DESCRIPTION("ntfs3 read/write filesystem");
Konstantin Komarov82cae262021-08-13 17:21:29 +03001526#ifdef CONFIG_NTFS3_FS_POSIX_ACL
1527MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support");
1528#endif
1529#ifdef CONFIG_NTFS3_64BIT_CLUSTER
Kari Argillander2e3a51b2021-08-29 17:42:39 +03001530MODULE_INFO(cluster, "Warning: Activated 64 bits per cluster. Windows does not support this");
Konstantin Komarov82cae262021-08-13 17:21:29 +03001531#endif
1532#ifdef CONFIG_NTFS3_LZX_XPRESS
1533MODULE_INFO(compression, "Read-only lzx/xpress compression included");
1534#endif
1535
1536MODULE_AUTHOR("Konstantin Komarov");
1537MODULE_ALIAS_FS("ntfs3");
1538
1539module_init(init_ntfs_fs);
1540module_exit(exit_ntfs_fs);