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