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