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 * resident attribute - Attribute with content stored directly in the MFT record 20 * non-resident attribute - Attribute with content stored in clusters 21 * data_size - Size of attribute content in bytes. Equal to inode->i_size 22 * valid_size - Number of bytes written to the non-resident attribute 23 * allocated_size - Total size of clusters allocated for non-resident content 24 * total_size - Actual size of allocated clusters for sparse or compressed attributes 25 * - Constraint: valid_size <= data_size <= allocated_size 26 * ADS - Alternative data stream: Named data attribute (0x80) 27 * 28 * WSL - Windows Subsystem for Linux 29 * https://docs.microsoft.com/en-us/windows/wsl/file-permissions 30 * It stores uid/gid/mode/dev in xattr 31 * 32 * ntfs allows up to 2^64 clusters per volume. 33 * It means you should use 64 bits lcn to operate with ntfs. 34 * Implementation of ntfs.sys uses only 32 bits lcn. 35 * Default ntfs3 uses 32 bits lcn too. 36 * ntfs3 built with CONFIG_NTFS3_64BIT_CLUSTER (ntfs3_64) uses 64 bits per lcn. 37 * 38 * 39 * ntfs limits, cluster size is 4K (2^12) 40 * ----------------------------------------------------------------------------- 41 * | Volume size | Clusters | ntfs.sys | ntfs3 | ntfs3_64 | mkntfs | chkdsk | 42 * ----------------------------------------------------------------------------- 43 * | < 16T, 2^44 | < 2^32 | yes | yes | yes | yes | yes | 44 * | > 16T, 2^44 | > 2^32 | no | no | yes | yes | yes | 45 * ----------------------------------------------------------|------------------ 46 * 47 * To mount large volumes as ntfs one should use large cluster size (up to 2M) 48 * The maximum volume size in this case is 2^32 * 2^21 = 2^53 = 8P 49 * 50 * ntfs limits, cluster size is 2M (2^21) 51 * ----------------------------------------------------------------------------- 52 * | < 8P, 2^53 | < 2^32 | yes | yes | yes | yes | yes | 53 * | > 8P, 2^53 | > 2^32 | no | no | yes | yes | yes | 54 * ----------------------------------------------------------|------------------ 55 * 56 */ 57 58 #include <linux/blkdev.h> 59 #include <linux/buffer_head.h> 60 #include <linux/exportfs.h> 61 #include <linux/fs.h> 62 #include <linux/fs_context.h> 63 #include <linux/fs_parser.h> 64 #include <linux/fs_struct.h> 65 #include <linux/log2.h> 66 #include <linux/minmax.h> 67 #include <linux/module.h> 68 #include <linux/nls.h> 69 #include <linux/overflow.h> 70 #include <linux/proc_fs.h> 71 #include <linux/seq_file.h> 72 #include <linux/statfs.h> 73 74 #include "debug.h" 75 #include "ntfs.h" 76 #include "ntfs_fs.h" 77 #ifdef CONFIG_NTFS3_LZX_XPRESS 78 #include "lib/lib.h" 79 #endif 80 81 #define CREATE_TRACE_POINTS 82 #include <trace/events/ntfs3.h> 83 84 #ifdef CONFIG_PRINTK 85 /* 86 * ntfs_printk - Trace warnings/notices/errors. 87 * 88 * Thanks Joe Perches <joe@perches.com> for implementation 89 */ 90 void ntfs_printk(const struct super_block *sb, const char *fmt, ...) 91 { 92 struct va_format vaf; 93 va_list args; 94 int level; 95 struct ntfs_sb_info *sbi = sb->s_fs_info; 96 97 /* Should we use different ratelimits for warnings/notices/errors? */ 98 if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3")) 99 return; 100 101 va_start(args, fmt); 102 103 level = printk_get_level(fmt); 104 vaf.fmt = printk_skip_level(fmt); 105 vaf.va = &args; 106 printk("%c%cntfs3(%s): %pV\n", KERN_SOH_ASCII, level, sb->s_id, &vaf); 107 108 va_end(args); 109 } 110 111 static char s_name_buf[512]; 112 static atomic_t s_name_buf_cnt = ATOMIC_INIT(1); // 1 means 'free s_name_buf'. 113 114 /* 115 * ntfs_inode_printk 116 * 117 * Print warnings/notices/errors about inode using name or inode number. 118 */ 119 void ntfs_inode_printk(struct inode *inode, const char *fmt, ...) 120 { 121 struct super_block *sb = inode->i_sb; 122 struct ntfs_sb_info *sbi = sb->s_fs_info; 123 char *name; 124 va_list args; 125 struct va_format vaf; 126 int level; 127 128 if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3")) 129 return; 130 131 /* Use static allocated buffer, if possible. */ 132 name = atomic_dec_and_test(&s_name_buf_cnt) ? 133 s_name_buf : 134 kmalloc(sizeof(s_name_buf), GFP_NOFS); 135 136 if (name) { 137 struct dentry *de = d_find_alias(inode); 138 139 if (de) { 140 int len; 141 spin_lock(&de->d_lock); 142 len = snprintf(name, sizeof(s_name_buf), " \"%s\"", 143 de->d_name.name); 144 spin_unlock(&de->d_lock); 145 if (len <= 0) 146 name[0] = 0; 147 else if (len >= sizeof(s_name_buf)) 148 name[sizeof(s_name_buf) - 1] = 0; 149 } else { 150 name[0] = 0; 151 } 152 dput(de); /* Cocci warns if placed in branch "if (de)" */ 153 } 154 155 va_start(args, fmt); 156 157 level = printk_get_level(fmt); 158 vaf.fmt = printk_skip_level(fmt); 159 vaf.va = &args; 160 161 printk("%c%cntfs3(%s): ino=%llx,%s %pV\n", KERN_SOH_ASCII, level, 162 sb->s_id, inode->i_ino, name ? name : "", &vaf); 163 164 va_end(args); 165 166 atomic_inc(&s_name_buf_cnt); 167 if (name != s_name_buf) 168 kfree(name); 169 } 170 #endif 171 172 /* 173 * Shared memory struct. 174 * 175 * On-disk ntfs's upcase table is created by ntfs formatter. 176 * 'upcase' table is 128K bytes of memory. 177 * We should read it into memory when mounting. 178 * Several ntfs volumes likely use the same 'upcase' table. 179 * It is good idea to share in-memory 'upcase' table between different volumes. 180 * Unfortunately winxp/vista/win7 use different upcase tables. 181 */ 182 static DEFINE_SPINLOCK(s_shared_lock); 183 184 static struct { 185 void *ptr; 186 u32 len; 187 int cnt; 188 } s_shared[8]; 189 190 /* 191 * ntfs_set_shared 192 * 193 * Return: 194 * * @ptr - If pointer was saved in shared memory. 195 * * NULL - If pointer was not shared. 196 */ 197 void *ntfs_set_shared(void *ptr, u32 bytes) 198 { 199 void *ret = NULL; 200 int i, j = -1; 201 202 spin_lock_bh(&s_shared_lock); 203 for (i = 0; i < ARRAY_SIZE(s_shared); i++) { 204 if (!s_shared[i].cnt) { 205 j = i; 206 } else if (bytes == s_shared[i].len && 207 !memcmp(s_shared[i].ptr, ptr, bytes)) { 208 s_shared[i].cnt += 1; 209 ret = s_shared[i].ptr; 210 break; 211 } 212 } 213 214 if (!ret && j != -1) { 215 s_shared[j].ptr = ptr; 216 s_shared[j].len = bytes; 217 s_shared[j].cnt = 1; 218 ret = ptr; 219 } 220 spin_unlock_bh(&s_shared_lock); 221 222 return ret; 223 } 224 225 /* 226 * ntfs_put_shared 227 * 228 * Return: 229 * * @ptr - If pointer is not shared anymore. 230 * * NULL - If pointer is still shared. 231 */ 232 void *ntfs_put_shared(void *ptr) 233 { 234 void *ret = ptr; 235 int i; 236 237 spin_lock_bh(&s_shared_lock); 238 for (i = 0; i < ARRAY_SIZE(s_shared); i++) { 239 if (s_shared[i].cnt && s_shared[i].ptr == ptr) { 240 if (--s_shared[i].cnt) 241 ret = NULL; 242 break; 243 } 244 } 245 spin_unlock_bh(&s_shared_lock); 246 247 return ret; 248 } 249 250 static inline void put_mount_options(struct ntfs_mount_options *options) 251 { 252 kfree(options->nls_name); 253 unload_nls(options->nls); 254 kfree(options); 255 } 256 257 static void put_mount_options_rcu(struct rcu_head *head) 258 { 259 put_mount_options(container_of(head, struct ntfs_mount_options, rcu)); 260 } 261 262 enum Opt { 263 Opt_uid, 264 Opt_gid, 265 Opt_umask, 266 Opt_dmask, 267 Opt_fmask, 268 Opt_immutable, 269 Opt_discard, 270 Opt_force, 271 Opt_sparse, 272 Opt_nohidden, 273 Opt_hide_dot_files, 274 Opt_windows_names, 275 Opt_showmeta, 276 Opt_acl, 277 Opt_acl_bool, 278 Opt_iocharset, 279 Opt_prealloc, 280 Opt_prealloc_bool, 281 Opt_nocase, 282 Opt_delalloc, 283 Opt_delalloc_bool, 284 Opt_ads, 285 Opt_ads_bool, 286 Opt_ugm, 287 Opt_ugm_bool, 288 Opt_err, 289 }; 290 291 // clang-format off 292 static const struct fs_parameter_spec ntfs_fs_parameters[] = { 293 fsparam_uid("uid", Opt_uid), 294 fsparam_gid("gid", Opt_gid), 295 fsparam_u32oct("umask", Opt_umask), 296 fsparam_u32oct("dmask", Opt_dmask), 297 fsparam_u32oct("fmask", Opt_fmask), 298 fsparam_flag("sys_immutable", Opt_immutable), 299 fsparam_flag("discard", Opt_discard), 300 fsparam_flag("force", Opt_force), 301 fsparam_flag("sparse", Opt_sparse), 302 fsparam_flag("nohidden", Opt_nohidden), 303 fsparam_flag("hide_dot_files", Opt_hide_dot_files), 304 fsparam_flag("windows_names", Opt_windows_names), 305 fsparam_flag("showmeta", Opt_showmeta), 306 fsparam_flag("acl", Opt_acl), 307 fsparam_bool("acl", Opt_acl_bool), 308 fsparam_string("iocharset", Opt_iocharset), 309 fsparam_flag("prealloc", Opt_prealloc), 310 fsparam_bool("prealloc", Opt_prealloc_bool), 311 fsparam_flag("nocase", Opt_nocase), 312 fsparam_flag("delalloc", Opt_delalloc), 313 fsparam_bool("delalloc", Opt_delalloc_bool), 314 fsparam_flag("ads", Opt_ads), 315 fsparam_bool("ads", Opt_ads_bool), 316 fsparam_flag("ugm", Opt_ugm), 317 fsparam_bool("ugm", Opt_ugm_bool), 318 {} 319 }; 320 // clang-format on 321 322 /* 323 * Load nls table or if @nls is utf8 then return NULL. 324 * 325 */ 326 static struct nls_table *ntfs_load_nls(const char *nls) 327 { 328 struct nls_table *ret; 329 330 if (!nls) 331 nls = CONFIG_NLS_DEFAULT; 332 333 if (strcmp(nls, "utf8") == 0) 334 return NULL; 335 336 if (strcmp(nls, CONFIG_NLS_DEFAULT) == 0) 337 return load_nls_default(); 338 339 ret = load_nls(nls); 340 if (ret) 341 return ret; 342 343 return ERR_PTR(-EINVAL); 344 } 345 346 static int ntfs_fs_parse_param(struct fs_context *fc, 347 struct fs_parameter *param) 348 { 349 struct ntfs_mount_options *opts = fc->fs_private; 350 struct fs_parse_result result; 351 int opt; 352 353 opt = fs_parse(fc, ntfs_fs_parameters, param, &result); 354 355 if (opt == -ENOPARAM && strcmp(param->key, "source")) { 356 /* Show mini help. */ 357 pr_err("ntfs3: unknown option \"%s\". Use uid,gid,umask," 358 "dmask,fmask,sys_immutable,discard,force,sparse," 359 "nohidden,hide_dot_files,windows_names,showmeta,acl," 360 "iocharset,prealloc,nocase,delalloc,ads,ugm", 361 param->key); 362 return -ENOPARAM; 363 } 364 365 if (opt < 0) 366 return opt; 367 368 switch (opt) { 369 case Opt_uid: 370 opts->fs_uid = result.uid; 371 break; 372 case Opt_gid: 373 opts->fs_gid = result.gid; 374 break; 375 case Opt_umask: 376 if (result.uint_32 & ~07777) 377 return invalf(fc, "ntfs3: Invalid value for umask."); 378 opts->fs_fmask_inv = ~result.uint_32; 379 opts->fs_dmask_inv = ~result.uint_32; 380 opts->fmask = 1; 381 opts->dmask = 1; 382 break; 383 case Opt_dmask: 384 if (result.uint_32 & ~07777) 385 return invalf(fc, "ntfs3: Invalid value for dmask."); 386 opts->fs_dmask_inv = ~result.uint_32; 387 opts->dmask = 1; 388 break; 389 case Opt_fmask: 390 if (result.uint_32 & ~07777) 391 return invalf(fc, "ntfs3: Invalid value for fmask."); 392 opts->fs_fmask_inv = ~result.uint_32; 393 opts->fmask = 1; 394 break; 395 case Opt_immutable: 396 opts->sys_immutable = 1; 397 break; 398 case Opt_discard: 399 opts->discard = 1; 400 break; 401 case Opt_force: 402 opts->force = 1; 403 break; 404 case Opt_sparse: 405 opts->sparse = 1; 406 break; 407 case Opt_nohidden: 408 opts->nohidden = 1; 409 break; 410 case Opt_hide_dot_files: 411 opts->hide_dot_files = 1; 412 break; 413 case Opt_windows_names: 414 opts->windows_names = 1; 415 break; 416 case Opt_showmeta: 417 opts->showmeta = 1; 418 break; 419 case Opt_acl_bool: 420 if (result.boolean) { 421 fallthrough; 422 case Opt_acl: 423 #ifdef CONFIG_NTFS3_FS_POSIX_ACL 424 fc->sb_flags |= SB_POSIXACL; 425 #else 426 return invalf( 427 fc, "ntfs3: Support for ACL not compiled in!"); 428 #endif 429 } else 430 fc->sb_flags &= ~SB_POSIXACL; 431 break; 432 case Opt_iocharset: 433 kfree(opts->nls_name); 434 opts->nls_name = param->string; 435 param->string = NULL; 436 break; 437 case Opt_prealloc: 438 opts->prealloc = 1; 439 break; 440 case Opt_prealloc_bool: 441 opts->prealloc = result.boolean; 442 break; 443 case Opt_nocase: 444 opts->nocase = 1; 445 break; 446 case Opt_delalloc: 447 opts->delalloc = 1; 448 break; 449 case Opt_delalloc_bool: 450 opts->delalloc = result.boolean; 451 break; 452 case Opt_ads: 453 opts->ads = 1; 454 break; 455 case Opt_ads_bool: 456 opts->ads = result.boolean; 457 break; 458 case Opt_ugm: 459 opts->ugm = 1; 460 break; 461 case Opt_ugm_bool: 462 opts->ugm = result.boolean; 463 break; 464 default: 465 /* Should not be here unless we forget add case. */ 466 return -EINVAL; 467 } 468 return 0; 469 } 470 471 static int ntfs_fs_reconfigure(struct fs_context *fc) 472 { 473 struct super_block *sb = fc->root->d_sb; 474 struct ntfs_sb_info *sbi = sb->s_fs_info; 475 struct ntfs_mount_options *new_opts = fc->fs_private; 476 struct ntfs_mount_options *old_opts; 477 int ro_rw; 478 479 ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY); 480 if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) { 481 errorf(fc, 482 "ntfs3: Couldn't remount rw because journal is not replayed. Please umount/remount instead\n"); 483 return -EINVAL; 484 } 485 486 new_opts->nls = ntfs_load_nls(new_opts->nls_name); 487 if (IS_ERR(new_opts->nls)) { 488 new_opts->nls = NULL; 489 errorf(fc, "ntfs3: Cannot load iocharset %s", 490 new_opts->nls_name); 491 return -EINVAL; 492 } 493 if (new_opts->nls != sbi->options->nls) 494 return invalf( 495 fc, 496 "ntfs3: Cannot use different iocharset when remounting!"); 497 498 if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) && 499 !new_opts->force) { 500 errorf(fc, 501 "ntfs3: Volume is dirty and \"force\" flag is not set!"); 502 return -EINVAL; 503 } 504 505 sync_filesystem(sb); 506 old_opts = sbi->options; 507 /* pairs with the READ_ONCE() in ntfs_nls_to_utf16() */ 508 smp_store_release(&sbi->options, new_opts); 509 fc->fs_private = NULL; 510 /* 511 * ntfs_d_hash() and ntfs_d_compare() of a pathwalk in rcu mode may 512 * still read the old options through the sbi. 513 */ 514 call_rcu(&old_opts->rcu, put_mount_options_rcu); 515 516 return 0; 517 } 518 519 #ifdef CONFIG_PROC_FS 520 static struct proc_dir_entry *proc_info_root; 521 522 /* 523 * ntfs3_volinfo: 524 * 525 * The content of /proc/fs/ntfs3/<dev>/volinfo 526 * 527 * ntfs3.1 528 * cluster size 529 * number of clusters 530 * total number of mft records 531 * number of used mft records ~= number of files + folders 532 * real state of ntfs "dirty"/"clean" 533 * current state of ntfs "dirty"/"clean" 534 */ 535 static int ntfs3_volinfo(struct seq_file *m, void *o) 536 { 537 struct super_block *sb = m->private; 538 struct ntfs_sb_info *sbi = sb->s_fs_info; 539 540 seq_printf(m, "ntfs%d.%d\n%u\n%zu\n%zu\n%zu\n%s\n%s\n", 541 sbi->volume.major_ver, sbi->volume.minor_ver, 542 sbi->cluster_size, sbi->used.bitmap.nbits, 543 sbi->mft.bitmap.nbits, 544 sbi->mft.bitmap.nbits - wnd_zeroes(&sbi->mft.bitmap), 545 sbi->volume.real_dirty ? "dirty" : "clean", 546 (sbi->volume.flags & VOLUME_FLAG_DIRTY) ? "dirty" : "clean"); 547 548 return 0; 549 } 550 551 static int ntfs3_volinfo_open(struct inode *inode, struct file *file) 552 { 553 return single_open(file, ntfs3_volinfo, pde_data(inode)); 554 } 555 556 /* read /proc/fs/ntfs3/<dev>/label */ 557 static int ntfs3_label_show(struct seq_file *m, void *o) 558 { 559 struct super_block *sb = m->private; 560 struct ntfs_sb_info *sbi = sb->s_fs_info; 561 562 seq_printf(m, "%s\n", sbi->volume.label); 563 564 return 0; 565 } 566 567 /* write /proc/fs/ntfs3/<dev>/label */ 568 static ssize_t ntfs3_label_write(struct file *file, const char __user *buffer, 569 size_t count, loff_t *ppos) 570 { 571 int err; 572 struct super_block *sb = pde_data(file_inode(file)); 573 ssize_t ret = count; 574 u8 *label; 575 576 if (sb_rdonly(sb)) 577 return -EROFS; 578 579 label = kmalloc(count, GFP_NOFS); 580 581 if (!label) 582 return -ENOMEM; 583 584 if (copy_from_user(label, buffer, ret)) { 585 ret = -EFAULT; 586 goto out; 587 } 588 while (ret > 0 && label[ret - 1] == '\n') 589 ret -= 1; 590 591 err = ntfs_set_label(sb->s_fs_info, label, ret); 592 593 if (err < 0) { 594 ntfs_err(sb, "failed (%d) to write label", err); 595 ret = err; 596 goto out; 597 } 598 599 *ppos += count; 600 ret = count; 601 out: 602 kfree(label); 603 return ret; 604 } 605 606 static int ntfs3_label_open(struct inode *inode, struct file *file) 607 { 608 return single_open(file, ntfs3_label_show, pde_data(inode)); 609 } 610 611 static const struct proc_ops ntfs3_volinfo_fops = { 612 .proc_read = seq_read, 613 .proc_lseek = seq_lseek, 614 .proc_release = single_release, 615 .proc_open = ntfs3_volinfo_open, 616 }; 617 618 static const struct proc_ops ntfs3_label_fops = { 619 .proc_read = seq_read, 620 .proc_lseek = seq_lseek, 621 .proc_release = single_release, 622 .proc_open = ntfs3_label_open, 623 .proc_write = ntfs3_label_write, 624 }; 625 626 static void ntfs_create_procdir(struct super_block *sb) 627 { 628 struct proc_dir_entry *e; 629 630 if (!proc_info_root) 631 return; 632 633 e = proc_mkdir(sb->s_id, proc_info_root); 634 if (e) { 635 struct ntfs_sb_info *sbi = sb->s_fs_info; 636 637 proc_create_data("volinfo", 0444, e, &ntfs3_volinfo_fops, sb); 638 proc_create_data("label", 0644, e, &ntfs3_label_fops, sb); 639 sbi->procdir = e; 640 } 641 } 642 643 static void ntfs_remove_procdir(struct super_block *sb) 644 { 645 struct ntfs_sb_info *sbi = sb->s_fs_info; 646 647 if (!sbi->procdir) 648 return; 649 650 remove_proc_entry("label", sbi->procdir); 651 remove_proc_entry("volinfo", sbi->procdir); 652 remove_proc_entry(sb->s_id, proc_info_root); 653 sbi->procdir = NULL; 654 } 655 656 static void ntfs_create_proc_root(void) 657 { 658 proc_info_root = proc_mkdir("fs/ntfs3", NULL); 659 } 660 661 static void ntfs_remove_proc_root(void) 662 { 663 if (proc_info_root) { 664 remove_proc_entry("fs/ntfs3", NULL); 665 proc_info_root = NULL; 666 } 667 } 668 #else 669 // clang-format off 670 static void ntfs_create_procdir(struct super_block *sb){} 671 static void ntfs_remove_procdir(struct super_block *sb){} 672 static void ntfs_create_proc_root(void){} 673 static void ntfs_remove_proc_root(void){} 674 // clang-format on 675 #endif 676 677 static struct kmem_cache *ntfs_inode_cachep; 678 679 static struct inode *ntfs_alloc_inode(struct super_block *sb) 680 { 681 struct ntfs_inode *ni = alloc_inode_sb(sb, ntfs_inode_cachep, GFP_NOFS); 682 683 if (!ni) 684 return NULL; 685 686 memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode)); 687 mutex_init(&ni->ni_lock); 688 return &ni->vfs_inode; 689 } 690 691 static void ntfs_free_inode(struct inode *inode) 692 { 693 struct ntfs_inode *ni = ntfs_i(inode); 694 695 mutex_destroy(&ni->ni_lock); 696 kmem_cache_free(ntfs_inode_cachep, ni); 697 } 698 699 static void init_once(void *foo) 700 { 701 struct ntfs_inode *ni = foo; 702 703 inode_init_once(&ni->vfs_inode); 704 } 705 706 /* 707 * Noinline to reduce binary size. 708 */ 709 static noinline void ntfs3_put_sbi(struct ntfs_sb_info *sbi) 710 { 711 wnd_close(&sbi->mft.bitmap); 712 wnd_close(&sbi->used.bitmap); 713 714 if (sbi->mft.ni) { 715 iput(&sbi->mft.ni->vfs_inode); 716 sbi->mft.ni = NULL; 717 } 718 719 if (sbi->security.ni) { 720 iput(&sbi->security.ni->vfs_inode); 721 sbi->security.ni = NULL; 722 } 723 724 if (sbi->reparse.ni) { 725 iput(&sbi->reparse.ni->vfs_inode); 726 sbi->reparse.ni = NULL; 727 } 728 729 if (sbi->objid.ni) { 730 iput(&sbi->objid.ni->vfs_inode); 731 sbi->objid.ni = NULL; 732 } 733 734 if (sbi->volume.ni) { 735 iput(&sbi->volume.ni->vfs_inode); 736 sbi->volume.ni = NULL; 737 } 738 739 ntfs_update_mftmirr(sbi); 740 741 indx_clear(&sbi->security.index_sii); 742 indx_clear(&sbi->security.index_sdh); 743 indx_clear(&sbi->reparse.index_r); 744 indx_clear(&sbi->objid.index_o); 745 } 746 747 static void ntfs3_free_sbi(struct ntfs_sb_info *sbi) 748 { 749 if (sbi->options) 750 put_mount_options(sbi->options); 751 kfree(sbi->new_rec); 752 kvfree(ntfs_put_shared(sbi->upcase)); 753 kvfree(sbi->def_table); 754 kfree(sbi->compress.lznt); 755 #ifdef CONFIG_NTFS3_LZX_XPRESS 756 xpress_free_decompressor(sbi->compress.xpress); 757 lzx_free_decompressor(sbi->compress.lzx); 758 #endif 759 kfree(sbi); 760 } 761 762 static void ntfs3_free_sbi_rcu(struct rcu_head *head) 763 { 764 ntfs3_free_sbi(container_of(head, struct ntfs_sb_info, rcu)); 765 } 766 767 static void ntfs_put_super(struct super_block *sb) 768 { 769 struct ntfs_sb_info *sbi = sb->s_fs_info; 770 771 ntfs_remove_procdir(sb); 772 773 /* Mark rw ntfs as clear, if possible. */ 774 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR); 775 776 /* 777 * The mount options stay until the sbi is freed: ->d_hash() and 778 * ->d_compare() of a pathwalk in rcu mode read the nls table through 779 * them. 780 */ 781 ntfs3_put_sbi(sbi); 782 } 783 784 static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf) 785 { 786 struct super_block *sb = dentry->d_sb; 787 struct ntfs_sb_info *sbi = sb->s_fs_info; 788 struct wnd_bitmap *wnd = &sbi->used.bitmap; 789 CLST da_clusters = ntfs_get_da(sbi); 790 791 buf->f_type = sb->s_magic; 792 buf->f_bsize = buf->f_frsize = sbi->cluster_size; 793 buf->f_blocks = wnd->nbits; 794 795 buf->f_bfree = wnd_zeroes(wnd); 796 if (buf->f_bfree > da_clusters) { 797 buf->f_bfree -= da_clusters; 798 } else { 799 buf->f_bfree = 0; 800 } 801 buf->f_bavail = buf->f_bfree; 802 803 buf->f_fsid.val[0] = sbi->volume.ser_num; 804 buf->f_fsid.val[1] = sbi->volume.ser_num >> 32; 805 buf->f_namelen = NTFS_NAME_LEN; 806 807 return 0; 808 } 809 810 static int ntfs_show_options(struct seq_file *m, struct dentry *root) 811 { 812 struct super_block *sb = root->d_sb; 813 struct ntfs_sb_info *sbi = sb->s_fs_info; 814 struct ntfs_mount_options *opts = sbi->options; 815 struct user_namespace *user_ns = seq_user_ns(m); 816 817 seq_printf(m, ",uid=%u", from_kuid_munged(user_ns, opts->fs_uid)); 818 seq_printf(m, ",gid=%u", from_kgid_munged(user_ns, opts->fs_gid)); 819 if (opts->dmask) 820 seq_printf(m, ",dmask=%04o", opts->fs_dmask_inv ^ 0xffff); 821 if (opts->fmask) 822 seq_printf(m, ",fmask=%04o", opts->fs_fmask_inv ^ 0xffff); 823 if (opts->sys_immutable) 824 seq_puts(m, ",sys_immutable"); 825 if (opts->discard) 826 seq_puts(m, ",discard"); 827 if (opts->force) 828 seq_puts(m, ",force"); 829 if (opts->sparse) 830 seq_puts(m, ",sparse"); 831 if (opts->nohidden) 832 seq_puts(m, ",nohidden"); 833 if (opts->hide_dot_files) 834 seq_puts(m, ",hide_dot_files"); 835 if (opts->windows_names) 836 seq_puts(m, ",windows_names"); 837 if (opts->showmeta) 838 seq_puts(m, ",showmeta"); 839 if (sb->s_flags & SB_POSIXACL) 840 seq_puts(m, ",acl"); 841 if (opts->nls) 842 seq_printf(m, ",iocharset=%s", opts->nls->charset); 843 else 844 seq_puts(m, ",iocharset=utf8"); 845 if (opts->prealloc) 846 seq_puts(m, ",prealloc"); 847 if (opts->nocase) 848 seq_puts(m, ",nocase"); 849 if (opts->delalloc) 850 seq_puts(m, ",delalloc"); 851 if (opts->ads) 852 seq_puts(m, ",ads"); 853 854 return 0; 855 } 856 857 /* 858 * ntfs_shutdown - super_operations::shutdown 859 */ 860 static void ntfs_shutdown(struct super_block *sb) 861 { 862 set_bit(NTFS_FLAGS_SHUTDOWN_BIT, &ntfs_sb(sb)->flags); 863 } 864 865 /* 866 * ntfs_sync_fs - super_operations::sync_fs 867 */ 868 static int ntfs_sync_fs(struct super_block *sb, int wait) 869 { 870 int err = 0, err2; 871 struct ntfs_sb_info *sbi = sb->s_fs_info; 872 struct ntfs_inode *ni; 873 struct inode *inode; 874 875 if (unlikely(ntfs3_forced_shutdown(sb))) 876 return -EIO; 877 878 ni = sbi->security.ni; 879 if (ni) { 880 inode = &ni->vfs_inode; 881 err2 = _ni_write_inode(inode, wait); 882 if (err2 && !err) 883 err = err2; 884 } 885 886 ni = sbi->objid.ni; 887 if (ni) { 888 inode = &ni->vfs_inode; 889 err2 = _ni_write_inode(inode, wait); 890 if (err2 && !err) 891 err = err2; 892 } 893 894 ni = sbi->reparse.ni; 895 if (ni) { 896 inode = &ni->vfs_inode; 897 err2 = _ni_write_inode(inode, wait); 898 if (err2 && !err) 899 err = err2; 900 } 901 902 if (!err) 903 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR); 904 905 ntfs_update_mftmirr(sbi); 906 907 if (wait) { 908 sync_blockdev(sb->s_bdev); 909 blkdev_issue_flush(sb->s_bdev); 910 } 911 912 return err; 913 } 914 915 static const struct super_operations ntfs_sops = { 916 .alloc_inode = ntfs_alloc_inode, 917 .free_inode = ntfs_free_inode, 918 .evict_inode = ntfs_evict_inode, 919 .put_super = ntfs_put_super, 920 .statfs = ntfs_statfs, 921 .show_options = ntfs_show_options, 922 .shutdown = ntfs_shutdown, 923 .sync_fs = ntfs_sync_fs, 924 .write_inode = ntfs3_write_inode, 925 }; 926 927 static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino, 928 u32 generation) 929 { 930 struct MFT_REF ref; 931 struct inode *inode; 932 933 ref.low = cpu_to_le32(ino); 934 #ifdef CONFIG_NTFS3_64BIT_CLUSTER 935 ref.high = cpu_to_le16(ino >> 32); 936 #else 937 ref.high = 0; 938 #endif 939 ref.seq = cpu_to_le16(generation); 940 941 inode = ntfs_iget5(sb, &ref, NULL); 942 if (!IS_ERR(inode) && is_bad_inode(inode)) { 943 iput(inode); 944 inode = ERR_PTR(-ESTALE); 945 } 946 947 return inode; 948 } 949 950 static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid, 951 int fh_len, int fh_type) 952 { 953 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, 954 ntfs_export_get_inode); 955 } 956 957 static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid, 958 int fh_len, int fh_type) 959 { 960 return generic_fh_to_parent(sb, fid, fh_len, fh_type, 961 ntfs_export_get_inode); 962 } 963 964 /* TODO: == ntfs_sync_inode */ 965 static int ntfs_nfs_commit_metadata(struct inode *inode) 966 { 967 return _ni_write_inode(inode, 1); 968 } 969 970 static const struct export_operations ntfs_export_ops = { 971 .encode_fh = generic_encode_ino32_fh, 972 .fh_to_dentry = ntfs_fh_to_dentry, 973 .fh_to_parent = ntfs_fh_to_parent, 974 .get_parent = ntfs3_get_parent, 975 .commit_metadata = ntfs_nfs_commit_metadata, 976 }; 977 978 /* 979 * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb". 980 */ 981 static u32 format_size_gb(const u64 bytes, u32 *mb) 982 { 983 /* Do simple right 30 bit shift of 64 bit value. */ 984 u64 kbytes = bytes >> 10; 985 u32 kbytes32 = kbytes; 986 987 *mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20; 988 if (*mb >= 100) 989 *mb = 99; 990 991 return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12); 992 } 993 994 static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot) 995 { 996 if (boot->sectors_per_clusters <= 0x80) 997 return boot->sectors_per_clusters; 998 if (boot->sectors_per_clusters >= 0xf4) /* limit shift to 2MB max */ 999 return 1U << (-(s8)boot->sectors_per_clusters); 1000 return -EINVAL; 1001 } 1002 1003 /* 1004 * ntfs_init_from_boot - Init internal info from on-disk boot sector. 1005 * 1006 * NTFS mount begins from boot - special formatted 512 bytes. 1007 * There are two boots: the first and the last 512 bytes of volume. 1008 * The content of boot is not changed during ntfs life. 1009 * 1010 * NOTE: ntfs.sys checks only first (primary) boot. 1011 * chkdsk checks both boots. 1012 */ 1013 static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size, 1014 u64 dev_size, struct NTFS_BOOT **boot2) 1015 { 1016 struct ntfs_sb_info *sbi = sb->s_fs_info; 1017 int err; 1018 u32 mb, gb, boot_sector_size = 0, sct_per_clst, record_size; 1019 u64 sectors, clusters, mlcn, mlcn2, mft_pos, mft2_pos, dev_size0; 1020 struct NTFS_BOOT *boot; 1021 struct buffer_head *bh; 1022 struct MFT_REC *rec; 1023 u16 fn, ao; 1024 u8 cluster_bits; 1025 u32 boot_off = 0; 1026 sector_t boot_block = 0; 1027 const char *hint = "Primary boot"; 1028 1029 /* Save original dev_size. Used with alternative boot. */ 1030 dev_size0 = dev_size; 1031 1032 sbi->volume.blocks = dev_size >> PAGE_SHIFT; 1033 1034 /* Set dummy blocksize to read boot_block. */ 1035 if (!sb_min_blocksize(sb, PAGE_SIZE)) { 1036 return -EINVAL; 1037 } 1038 1039 read_boot: 1040 bh = ntfs_bread(sb, boot_block); 1041 if (!bh) 1042 return boot_block ? -EINVAL : -EIO; 1043 1044 err = -EINVAL; 1045 1046 /* Corrupted image; do not read OOB */ 1047 if (bh->b_size - sizeof(*boot) < boot_off) 1048 goto out; 1049 1050 boot = (struct NTFS_BOOT *)Add2Ptr(bh->b_data, boot_off); 1051 1052 if (memcmp(boot->system_id, "NTFS ", sizeof("NTFS ") - 1)) { 1053 ntfs_err(sb, "%s signature is not NTFS.", hint); 1054 goto out; 1055 } 1056 1057 /* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/ 1058 /*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1]) 1059 * goto out; 1060 */ 1061 1062 boot_sector_size = ((u32)boot->bytes_per_sector[1] << 8) | 1063 boot->bytes_per_sector[0]; 1064 if (boot_sector_size < SECTOR_SIZE || 1065 !is_power_of_2(boot_sector_size)) { 1066 ntfs_err(sb, "%s: invalid bytes per sector %u.", hint, 1067 boot_sector_size); 1068 goto out; 1069 } 1070 1071 /* cluster size: 512, 1K, 2K, 4K, ... 2M */ 1072 sct_per_clst = true_sectors_per_clst(boot); 1073 if ((int)sct_per_clst < 0 || !is_power_of_2(sct_per_clst)) { 1074 ntfs_err(sb, "%s: invalid sectors per cluster %u.", hint, 1075 sct_per_clst); 1076 goto out; 1077 } 1078 1079 sbi->cluster_size = boot_sector_size * sct_per_clst; 1080 sbi->cluster_bits = cluster_bits = blksize_bits(sbi->cluster_size); 1081 sbi->cluster_mask = sbi->cluster_size - 1; 1082 sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask; 1083 1084 mlcn = le64_to_cpu(boot->mft_clst); 1085 mlcn2 = le64_to_cpu(boot->mft2_clst); 1086 sectors = le64_to_cpu(boot->sectors_per_volume); 1087 1088 /* 1089 * Convert mlcn/mlcn2 to sector positions before comparing with 1090 * 'sectors'. All three are u64 values that come from the boot 1091 * sector, so use check_mul_overflow() to keep a wraparound from 1092 * silently bypassing the comparison. 1093 */ 1094 if (check_mul_overflow(mlcn, (u64)sct_per_clst, &mft_pos) || 1095 check_mul_overflow(mlcn2, (u64)sct_per_clst, &mft2_pos) || 1096 mft_pos >= sectors || mft2_pos >= sectors) { 1097 ntfs_err( 1098 sb, 1099 "%s: start of MFT 0x%llx (0x%llx) is out of volume 0x%llx.", 1100 hint, mlcn, mlcn2, sectors); 1101 goto out; 1102 } 1103 1104 if (boot->record_size >= 0) { 1105 record_size = (u32)boot->record_size << cluster_bits; 1106 } else if (-boot->record_size <= MAXIMUM_SHIFT_BYTES_PER_MFT) { 1107 record_size = 1u << (-boot->record_size); 1108 } else { 1109 ntfs_err(sb, "%s: invalid record size %d.", hint, 1110 boot->record_size); 1111 goto out; 1112 } 1113 1114 sbi->record_size = record_size; 1115 sbi->record_bits = blksize_bits(record_size); 1116 sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes 1117 1118 /* Check MFT record size. */ 1119 if (record_size < SECTOR_SIZE || !is_power_of_2(record_size)) { 1120 ntfs_err(sb, "%s: invalid bytes per MFT record %u (%d).", hint, 1121 record_size, boot->record_size); 1122 goto out; 1123 } 1124 1125 if (record_size > MAXIMUM_BYTES_PER_MFT) { 1126 ntfs_err(sb, "Unsupported bytes per MFT record %u.", 1127 record_size); 1128 goto out; 1129 } 1130 1131 if (boot->index_size >= 0) { 1132 sbi->index_size = (u32)boot->index_size << cluster_bits; 1133 } else if (-boot->index_size <= MAXIMUM_SHIFT_BYTES_PER_INDEX) { 1134 sbi->index_size = 1u << (-boot->index_size); 1135 } else { 1136 ntfs_err(sb, "%s: invalid index size %d.", hint, 1137 boot->index_size); 1138 goto out; 1139 } 1140 1141 /* Check index record size. */ 1142 if (sbi->index_size < SECTOR_SIZE || !is_power_of_2(sbi->index_size)) { 1143 ntfs_err(sb, "%s: invalid bytes per index %u(%d).", hint, 1144 sbi->index_size, boot->index_size); 1145 goto out; 1146 } 1147 1148 if (sbi->index_size > MAXIMUM_BYTES_PER_INDEX) { 1149 ntfs_err(sb, "%s: unsupported bytes per index %u.", hint, 1150 sbi->index_size); 1151 goto out; 1152 } 1153 1154 sbi->volume.size = sectors * boot_sector_size; 1155 1156 gb = format_size_gb(sbi->volume.size + boot_sector_size, &mb); 1157 1158 /* 1159 * - Volume formatted and mounted with the same sector size. 1160 * - Volume formatted 4K and mounted as 512. 1161 * - Volume formatted 512 and mounted as 4K. 1162 */ 1163 if (boot_sector_size != sector_size) { 1164 ntfs_warn( 1165 sb, 1166 "Different NTFS sector size (%u) and media sector size (%u).", 1167 boot_sector_size, sector_size); 1168 dev_size += sector_size - 1; 1169 } 1170 1171 sbi->bdev_blocksize = max(boot_sector_size, sector_size); 1172 sbi->mft.lbo = mlcn << cluster_bits; 1173 sbi->mft.lbo2 = mlcn2 << cluster_bits; 1174 1175 /* Compare boot's cluster and sector. */ 1176 if (sbi->cluster_size < boot_sector_size) { 1177 ntfs_err(sb, "%s: invalid bytes per cluster (%u).", hint, 1178 sbi->cluster_size); 1179 goto out; 1180 } 1181 1182 /* Compare boot's cluster and media sector. */ 1183 if (sbi->cluster_size < sector_size) { 1184 /* No way to use ntfs_get_block in this case. */ 1185 ntfs_err( 1186 sb, 1187 "Failed to mount 'cause NTFS's cluster size (%u) is less than media sector size (%u).", 1188 sbi->cluster_size, sector_size); 1189 goto out; 1190 } 1191 1192 sbi->max_bytes_per_attr = 1193 record_size - ALIGN(MFTRECORD_FIXUP_OFFSET, 8) - 1194 ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) - 1195 ALIGN(sizeof(enum ATTR_TYPE), 8); 1196 1197 sbi->volume.ser_num = le64_to_cpu(boot->serial_num); 1198 1199 /* Warning if RAW volume. */ 1200 if (dev_size < sbi->volume.size + boot_sector_size) { 1201 u32 mb0, gb0; 1202 1203 gb0 = format_size_gb(dev_size, &mb0); 1204 ntfs_warn( 1205 sb, 1206 "RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only.", 1207 gb, mb, gb0, mb0); 1208 sb->s_flags |= SB_RDONLY; 1209 } 1210 1211 clusters = sbi->volume.size >> cluster_bits; 1212 #ifndef CONFIG_NTFS3_64BIT_CLUSTER 1213 /* 32 bits per cluster. */ 1214 if (clusters >> 32) { 1215 ntfs_notice( 1216 sb, 1217 "NTFS %u.%02u Gb is too big to use 32 bits per cluster.", 1218 gb, mb); 1219 goto out; 1220 } 1221 #elif BITS_PER_LONG < 64 1222 #error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS" 1223 #endif 1224 1225 sbi->used.bitmap.nbits = clusters; 1226 1227 rec = kzalloc(record_size, GFP_NOFS); 1228 if (!rec) { 1229 err = -ENOMEM; 1230 goto out; 1231 } 1232 1233 sbi->new_rec = rec; 1234 rec->rhdr.sign = NTFS_FILE_SIGNATURE; 1235 rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET); 1236 fn = (sbi->record_size >> SECTOR_SHIFT) + 1; 1237 rec->rhdr.fix_num = cpu_to_le16(fn); 1238 ao = ALIGN(MFTRECORD_FIXUP_OFFSET + sizeof(short) * fn, 8); 1239 rec->attr_off = cpu_to_le16(ao); 1240 rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8)); 1241 rec->total = cpu_to_le32(sbi->record_size); 1242 ((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END; 1243 1244 if (!sb_set_blocksize(sb, min_t(u32, sbi->cluster_size, PAGE_SIZE))) { 1245 err = -EINVAL; 1246 goto out; 1247 } 1248 1249 sbi->block_mask = sb->s_blocksize - 1; 1250 sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits; 1251 sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits; 1252 1253 /* Maximum size for normal files. */ 1254 sbi->maxbytes = (clusters << cluster_bits) - 1; 1255 1256 #ifdef CONFIG_NTFS3_64BIT_CLUSTER 1257 if (clusters >= (1ull << (64 - cluster_bits))) 1258 sbi->maxbytes = MAX_LFS_FILESIZE; 1259 sbi->maxbytes_sparse = MAX_LFS_FILESIZE; 1260 sb->s_maxbytes = MAX_LFS_FILESIZE; 1261 #else 1262 /* 1263 * Maximum size for sparse file: 1264 * 16 aligned up clusters should fit in 32 bits. 1265 * xfstest 308 on sparse file. 1266 */ 1267 sbi->maxbytes_sparse = (1ull << (cluster_bits + 32)) - 1 - 1268 (sbi->cluster_size << NTFS_LZNT_CUNIT); 1269 sb->s_maxbytes = 0xFFFFFFFFull << cluster_bits; 1270 #endif 1271 1272 /* 1273 * Compute the MFT zone at two steps. 1274 * It would be nice if we are able to allocate 1/8 of 1275 * total clusters for MFT but not more then 512 MB. 1276 */ 1277 sbi->zone_max = min_t(CLST, 0x20000000 >> cluster_bits, clusters >> 3); 1278 1279 err = 0; 1280 1281 if (bh->b_blocknr && !sb_rdonly(sb)) { 1282 /* 1283 * Alternative boot is ok but primary is not ok. 1284 * Do not update primary boot here 'cause it may be faked boot. 1285 * Let ntfs to be mounted and update boot later. 1286 */ 1287 *boot2 = kmemdup(boot, sizeof(*boot), GFP_NOFS | __GFP_NOWARN); 1288 } 1289 1290 out: 1291 trace_ntfs3_init_from_boot(sb, sector_size, boot_sector_size, 1292 !!boot_block, err); 1293 brelse(bh); 1294 1295 if (err == -EINVAL && !boot_block && dev_size0 > PAGE_SHIFT) { 1296 u32 block_size = min_t(u32, sector_size, PAGE_SIZE); 1297 u64 lbo = dev_size0 - sizeof(*boot); 1298 1299 boot_block = lbo >> blksize_bits(block_size); 1300 boot_off = lbo & (block_size - 1); 1301 if (boot_block && block_size >= boot_off + sizeof(*boot)) { 1302 /* 1303 * Try alternative boot (last sector) 1304 */ 1305 if (!sb_set_blocksize(sb, block_size)) 1306 return -EINVAL; 1307 hint = "Alternative boot"; 1308 dev_size = dev_size0; /* restore original size. */ 1309 goto read_boot; 1310 } 1311 } 1312 1313 return err; 1314 } 1315 1316 /* 1317 * ntfs_fill_super - Try to mount. 1318 */ 1319 static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc) 1320 { 1321 int err; 1322 struct ntfs_sb_info *sbi = sb->s_fs_info; 1323 struct block_device *bdev = sb->s_bdev; 1324 struct ntfs_mount_options *fc_opts; 1325 struct ntfs_mount_options *options = NULL; 1326 struct inode *inode; 1327 struct ntfs_inode *ni; 1328 size_t i, tt, bad_len, bad_frags; 1329 CLST vcn, lcn, len; 1330 struct ATTRIB *attr; 1331 const struct VOLUME_INFO *info; 1332 u32 done, bytes; 1333 struct ATTR_DEF_ENTRY *t; 1334 u16 *shared; 1335 struct MFT_REF ref; 1336 bool ro = sb_rdonly(sb); 1337 struct NTFS_BOOT *boot2 = NULL; 1338 1339 ref.high = 0; 1340 1341 sbi->sb = sb; 1342 fc_opts = fc->fs_private; 1343 if (!fc_opts) { 1344 errorf(fc, "missing mount options"); 1345 return -EINVAL; 1346 } 1347 options = kmemdup(fc_opts, sizeof(*fc_opts), GFP_KERNEL); 1348 if (!options) 1349 return -ENOMEM; 1350 1351 if (fc_opts->nls_name) { 1352 options->nls_name = kstrdup(fc_opts->nls_name, GFP_KERNEL); 1353 if (!options->nls_name) { 1354 kfree(options); 1355 return -ENOMEM; 1356 } 1357 } 1358 sbi->options = options; 1359 sb->s_flags |= SB_NODIRATIME; 1360 sb->s_magic = 0x7366746e; // "ntfs" 1361 sb->s_op = &ntfs_sops; 1362 sb->s_export_op = &ntfs_export_ops; 1363 sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec 1364 sb->s_xattr = ntfs_xattr_handlers; 1365 set_default_d_op(sb, options->nocase ? &ntfs_dentry_ops : NULL); 1366 1367 options->nls = ntfs_load_nls(options->nls_name); 1368 if (IS_ERR(options->nls)) { 1369 options->nls = NULL; 1370 errorf(fc, "Cannot load nls %s", options->nls_name); 1371 err = -EINVAL; 1372 goto out; 1373 } 1374 1375 if (bdev_max_discard_sectors(bdev) && bdev_discard_granularity(bdev)) { 1376 sbi->discard_granularity = bdev_discard_granularity(bdev); 1377 sbi->discard_granularity_mask_inv = 1378 ~(u64)(sbi->discard_granularity - 1); 1379 } 1380 1381 /* Parse boot. */ 1382 err = ntfs_init_from_boot(sb, bdev_logical_block_size(bdev), 1383 bdev_nr_bytes(bdev), &boot2); 1384 if (err) 1385 goto out; 1386 1387 /* 1388 * Load $Volume. This should be done before $LogFile 1389 * 'cause 'sbi->volume.ni' is used in 'ntfs_set_state'. 1390 */ 1391 ref.low = cpu_to_le32(MFT_REC_VOL); 1392 ref.seq = cpu_to_le16(MFT_REC_VOL); 1393 inode = ntfs_iget5(sb, &ref, &NAME_VOLUME); 1394 if (IS_ERR(inode)) { 1395 err = PTR_ERR(inode); 1396 ntfs_err(sb, "Failed to load $Volume (%d).", err); 1397 goto out; 1398 } 1399 1400 ni = ntfs_i(inode); 1401 1402 /* Load and save label (not necessary). */ 1403 attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL); 1404 1405 if (!attr) { 1406 /* It is ok if no ATTR_LABEL */ 1407 } else if (!attr->non_res && !is_attr_ext(attr)) { 1408 /* $AttrDef allows labels to be up to 128 symbols. */ 1409 err = utf16s_to_utf8s(resident_data(attr), 1410 le32_to_cpu(attr->res.data_size) >> 1, 1411 UTF16_LITTLE_ENDIAN, sbi->volume.label, 1412 sizeof(sbi->volume.label)); 1413 if (err < 0) { 1414 sbi->volume.label[0] = 0; 1415 } else if (err >= sizeof(sbi->volume.label)) { 1416 sbi->volume.label[sizeof(sbi->volume.label) - 1] = 0; 1417 } else { 1418 sbi->volume.label[err] = 0; 1419 } 1420 } else { 1421 /* Should we break mounting here? */ 1422 //err = -EINVAL; 1423 //goto put_inode_out; 1424 } 1425 1426 attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL); 1427 if (!attr || is_attr_ext(attr) || 1428 !(info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO))) { 1429 ntfs_err(sb, "$Volume is corrupted."); 1430 err = -EINVAL; 1431 goto put_inode_out; 1432 } 1433 1434 sbi->volume.major_ver = info->major_ver; 1435 sbi->volume.minor_ver = info->minor_ver; 1436 sbi->volume.flags = info->flags; 1437 sbi->volume.ni = ni; 1438 if (info->flags & VOLUME_FLAG_DIRTY) { 1439 sbi->volume.real_dirty = true; 1440 ntfs_info(sb, "It is recommended to use chkdsk."); 1441 } 1442 1443 /* Load $MFTMirr to estimate recs_mirr. */ 1444 ref.low = cpu_to_le32(MFT_REC_MIRR); 1445 ref.seq = cpu_to_le16(MFT_REC_MIRR); 1446 inode = ntfs_iget5(sb, &ref, &NAME_MIRROR); 1447 if (IS_ERR(inode)) { 1448 err = PTR_ERR(inode); 1449 ntfs_err(sb, "Failed to load $MFTMirr (%d).", err); 1450 goto out; 1451 } 1452 1453 sbi->mft.recs_mirr = ntfs_up_cluster(sbi, inode->i_size) >> 1454 sbi->record_bits; 1455 1456 iput(inode); 1457 1458 /* Load LogFile to replay. */ 1459 ref.low = cpu_to_le32(MFT_REC_LOG); 1460 ref.seq = cpu_to_le16(MFT_REC_LOG); 1461 inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE); 1462 if (IS_ERR(inode)) { 1463 err = PTR_ERR(inode); 1464 ntfs_err(sb, "Failed to load \x24LogFile (%d).", err); 1465 goto out; 1466 } 1467 1468 ni = ntfs_i(inode); 1469 1470 err = ntfs_loadlog_and_replay(ni, sbi); 1471 if (err) 1472 goto put_inode_out; 1473 1474 iput(inode); 1475 1476 if ((sbi->flags & NTFS_FLAGS_NEED_REPLAY) && !ro) { 1477 ntfs_warn(sb, "failed to replay log file. Can't mount rw!"); 1478 err = -EINVAL; 1479 goto out; 1480 } 1481 1482 if ((sbi->volume.flags & VOLUME_FLAG_DIRTY) && !ro && !options->force) { 1483 ntfs_warn(sb, "volume is dirty and \"force\" flag is not set!"); 1484 err = -EINVAL; 1485 goto out; 1486 } 1487 1488 /* Load $MFT. */ 1489 ref.low = cpu_to_le32(MFT_REC_MFT); 1490 ref.seq = cpu_to_le16(1); 1491 1492 inode = ntfs_iget5(sb, &ref, &NAME_MFT); 1493 if (IS_ERR(inode)) { 1494 err = PTR_ERR(inode); 1495 ntfs_err(sb, "Failed to load $MFT (%d).", err); 1496 goto out; 1497 } 1498 1499 ni = ntfs_i(inode); 1500 1501 sbi->mft.used = ni->i_valid >> sbi->record_bits; 1502 tt = inode->i_size >> sbi->record_bits; 1503 sbi->mft.next_free = MFT_REC_USER; 1504 1505 err = ni_load_all_mi(ni); 1506 if (err) { 1507 ntfs_err(sb, "Failed to load $MFT's subrecords (%d).", err); 1508 goto put_inode_out; 1509 } 1510 1511 /* Merge MFT bitmap runs from extent records loaded by ni_load_all_mi. */ 1512 { 1513 struct ATTRIB *a = NULL; 1514 struct ATTR_LIST_ENTRY *le = NULL; 1515 1516 while ((a = ni_enum_attr_ex(ni, a, &le, NULL))) { 1517 CLST svcn, evcn; 1518 u16 roff; 1519 1520 if (a->type != ATTR_BITMAP || !a->non_res) 1521 continue; 1522 1523 svcn = le64_to_cpu(a->nres.svcn); 1524 if (!svcn) 1525 continue; /* Base record runs already loaded. */ 1526 1527 evcn = le64_to_cpu(a->nres.evcn); 1528 roff = le16_to_cpu(a->nres.run_off); 1529 1530 err = run_unpack_ex(&sbi->mft.bitmap.run, sbi, 1531 MFT_REC_MFT, svcn, evcn, svcn, 1532 Add2Ptr(a, roff), 1533 le32_to_cpu(a->size) - roff); 1534 if (err < 0) { 1535 ntfs_err( 1536 sb, 1537 "Failed to unpack $MFT bitmap extent (%d).", 1538 err); 1539 goto put_inode_out; 1540 } 1541 err = 0; 1542 } 1543 } 1544 1545 err = wnd_init(&sbi->mft.bitmap, sb, tt); 1546 if (err) 1547 goto put_inode_out; 1548 1549 sbi->mft.ni = ni; 1550 1551 /* Load $Bitmap. */ 1552 ref.low = cpu_to_le32(MFT_REC_BITMAP); 1553 ref.seq = cpu_to_le16(MFT_REC_BITMAP); 1554 inode = ntfs_iget5(sb, &ref, &NAME_BITMAP); 1555 if (IS_ERR(inode)) { 1556 err = PTR_ERR(inode); 1557 ntfs_err(sb, "Failed to load $Bitmap (%d).", err); 1558 goto out; 1559 } 1560 1561 #ifndef CONFIG_NTFS3_64BIT_CLUSTER 1562 if (inode->i_size >> 32) { 1563 err = -EINVAL; 1564 goto put_inode_out; 1565 } 1566 #endif 1567 1568 /* Check bitmap boundary. */ 1569 tt = sbi->used.bitmap.nbits; 1570 if (inode->i_size < ntfs3_bitmap_size(tt)) { 1571 ntfs_err(sb, "$Bitmap is corrupted."); 1572 err = -EINVAL; 1573 goto put_inode_out; 1574 } 1575 1576 err = wnd_init(&sbi->used.bitmap, sb, tt); 1577 if (err) { 1578 ntfs_err(sb, "Failed to initialize $Bitmap (%d).", err); 1579 goto put_inode_out; 1580 } 1581 1582 iput(inode); 1583 1584 /* Compute the MFT zone. */ 1585 err = ntfs_refresh_zone(sbi); 1586 if (err) { 1587 ntfs_err(sb, "Failed to initialize MFT zone (%d).", err); 1588 goto out; 1589 } 1590 1591 /* Load $BadClus. */ 1592 ref.low = cpu_to_le32(MFT_REC_BADCLUST); 1593 ref.seq = cpu_to_le16(MFT_REC_BADCLUST); 1594 inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS); 1595 if (IS_ERR(inode)) { 1596 err = PTR_ERR(inode); 1597 ntfs_err(sb, "Failed to load $BadClus (%d).", err); 1598 goto out; 1599 } 1600 1601 ni = ntfs_i(inode); 1602 bad_len = bad_frags = 0; 1603 for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) { 1604 if (lcn == SPARSE_LCN) 1605 continue; 1606 1607 bad_len += len; 1608 bad_frags += 1; 1609 if (ro) 1610 continue; 1611 1612 if (wnd_set_used_safe(&sbi->used.bitmap, lcn, len, &tt) || tt) { 1613 /* Bad blocks marked as free in bitmap. */ 1614 ntfs_set_state(sbi, NTFS_DIRTY_ERROR); 1615 } 1616 } 1617 if (bad_len) { 1618 /* 1619 * Notice about bad blocks. 1620 * In normal cases these blocks are marked as used in bitmap. 1621 * And we never allocate space in it. 1622 */ 1623 ntfs_notice(sb, 1624 "Volume contains %zu bad blocks in %zu fragments.", 1625 bad_len, bad_frags); 1626 } 1627 iput(inode); 1628 1629 /* Load $AttrDef. */ 1630 ref.low = cpu_to_le32(MFT_REC_ATTR); 1631 ref.seq = cpu_to_le16(MFT_REC_ATTR); 1632 inode = ntfs_iget5(sb, &ref, &NAME_ATTRDEF); 1633 if (IS_ERR(inode)) { 1634 err = PTR_ERR(inode); 1635 ntfs_err(sb, "Failed to load $AttrDef (%d)", err); 1636 goto out; 1637 } 1638 1639 /* 1640 * Typical $AttrDef contains up to 20 entries. 1641 * Check for extremely large/small size. 1642 */ 1643 if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY) || 1644 inode->i_size > 100 * sizeof(struct ATTR_DEF_ENTRY)) { 1645 ntfs_err(sb, "Looks like $AttrDef is corrupted (size=%llu).", 1646 inode->i_size); 1647 err = -EINVAL; 1648 goto put_inode_out; 1649 } 1650 1651 bytes = inode->i_size; 1652 sbi->def_table = t = kvmalloc(bytes, GFP_KERNEL); 1653 if (!t) { 1654 err = -ENOMEM; 1655 goto put_inode_out; 1656 } 1657 1658 /* Read the entire file. */ 1659 err = inode_read_data(inode, sbi->def_table, bytes); 1660 if (err) { 1661 ntfs_err(sb, "Failed to read $AttrDef (%d).", err); 1662 goto put_inode_out; 1663 } 1664 1665 if (ATTR_STD != t->type) { 1666 ntfs_err(sb, "$AttrDef is corrupted."); 1667 err = -EINVAL; 1668 goto put_inode_out; 1669 } 1670 1671 t += 1; 1672 sbi->def_entries = 1; 1673 done = sizeof(struct ATTR_DEF_ENTRY); 1674 1675 while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) { 1676 u32 t32 = le32_to_cpu(t->type); 1677 u64 sz = le64_to_cpu(t->max_sz); 1678 1679 if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32) 1680 break; 1681 1682 if (t->type == ATTR_REPARSE) 1683 sbi->reparse.max_size = sz; 1684 else if (t->type == ATTR_EA) 1685 sbi->ea_max_size = sz; 1686 1687 done += sizeof(struct ATTR_DEF_ENTRY); 1688 t += 1; 1689 sbi->def_entries += 1; 1690 } 1691 iput(inode); 1692 1693 /* Load $UpCase. */ 1694 ref.low = cpu_to_le32(MFT_REC_UPCASE); 1695 ref.seq = cpu_to_le16(MFT_REC_UPCASE); 1696 inode = ntfs_iget5(sb, &ref, &NAME_UPCASE); 1697 if (IS_ERR(inode)) { 1698 err = PTR_ERR(inode); 1699 ntfs_err(sb, "Failed to load $UpCase (%d).", err); 1700 goto out; 1701 } 1702 1703 if (inode->i_size != 0x10000 * sizeof(short)) { 1704 err = -EINVAL; 1705 ntfs_err(sb, "$UpCase is corrupted."); 1706 goto put_inode_out; 1707 } 1708 1709 /* Read the entire file. */ 1710 err = inode_read_data(inode, sbi->upcase, 0x10000 * sizeof(short)); 1711 if (err) { 1712 ntfs_err(sb, "Failed to read $UpCase (%d).", err); 1713 goto put_inode_out; 1714 } 1715 1716 #ifdef __BIG_ENDIAN 1717 { 1718 u16 *dst = sbi->upcase; 1719 1720 for (i = 0; i < 0x10000; i++) 1721 __swab16s(dst++); 1722 } 1723 #endif 1724 1725 shared = ntfs_set_shared(sbi->upcase, 0x10000 * sizeof(short)); 1726 if (shared && sbi->upcase != shared) { 1727 kvfree(sbi->upcase); 1728 sbi->upcase = shared; 1729 } 1730 1731 iput(inode); 1732 1733 if (is_ntfs3(sbi)) { 1734 /* Load $Secure. */ 1735 err = ntfs_security_init(sbi); 1736 if (err) { 1737 ntfs_err(sb, "Failed to initialize $Secure (%d).", err); 1738 goto out; 1739 } 1740 1741 /* Load $Extend. */ 1742 err = ntfs_extend_init(sbi); 1743 if (err) { 1744 ntfs_warn(sb, "Failed to initialize $Extend."); 1745 goto load_root; 1746 } 1747 1748 /* Load $Extend/$Reparse. */ 1749 err = ntfs_reparse_init(sbi); 1750 if (err) { 1751 ntfs_warn(sb, "Failed to initialize $Extend/$Reparse."); 1752 goto load_root; 1753 } 1754 1755 /* Load $Extend/$ObjId. */ 1756 err = ntfs_objid_init(sbi); 1757 if (err) { 1758 ntfs_warn(sb, "Failed to initialize $Extend/$ObjId."); 1759 goto load_root; 1760 } 1761 } 1762 1763 load_root: 1764 /* Load root. */ 1765 ref.low = cpu_to_le32(MFT_REC_ROOT); 1766 ref.seq = cpu_to_le16(MFT_REC_ROOT); 1767 inode = ntfs_iget5(sb, &ref, &NAME_ROOT); 1768 if (IS_ERR(inode)) { 1769 err = PTR_ERR(inode); 1770 ntfs_err(sb, "Failed to load root (%d).", err); 1771 goto out; 1772 } 1773 1774 /* 1775 * Final check. Looks like this case should never occurs. 1776 */ 1777 if (!inode->i_op) { 1778 err = -EINVAL; 1779 ntfs_err(sb, "Failed to load root (%d).", err); 1780 goto put_inode_out; 1781 } 1782 1783 sb->s_root = d_make_root(inode); 1784 if (!sb->s_root) { 1785 err = -ENOMEM; 1786 goto out; 1787 } 1788 1789 if (boot2) { 1790 /* 1791 * Alternative boot is ok but primary is not ok. 1792 * Volume is recognized as NTFS. Update primary boot. 1793 */ 1794 struct buffer_head *bh0 = sb_getblk(sb, 0); 1795 if (bh0) { 1796 wait_on_buffer(bh0); 1797 lock_buffer(bh0); 1798 memcpy(bh0->b_data, boot2, sizeof(*boot2)); 1799 set_buffer_uptodate(bh0); 1800 mark_buffer_dirty(bh0); 1801 unlock_buffer(bh0); 1802 if (!sync_dirty_buffer(bh0)) 1803 ntfs_warn(sb, "primary boot is updated"); 1804 put_bh(bh0); 1805 } 1806 1807 kfree(boot2); 1808 } 1809 1810 ntfs_create_procdir(sb); 1811 trace_ntfs3_fill_super(sb, ro, sbi->cluster_size, sbi->record_size, 1812 sbi->index_size, 0); 1813 1814 return 0; 1815 1816 put_inode_out: 1817 iput(inode); 1818 out: 1819 trace_ntfs3_fill_super(sb, ro, sbi->cluster_size, sbi->record_size, 1820 sbi->index_size, err); 1821 /* sbi->options == options */ 1822 if (options) { 1823 put_mount_options(sbi->options); 1824 sbi->options = NULL; 1825 } 1826 1827 ntfs3_put_sbi(sbi); 1828 kfree(boot2); 1829 return err; 1830 } 1831 1832 void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len) 1833 { 1834 struct ntfs_sb_info *sbi = sb->s_fs_info; 1835 struct block_device *bdev = sb->s_bdev; 1836 sector_t devblock = (u64)lcn * sbi->blocks_per_cluster; 1837 unsigned long blocks = (u64)len * sbi->blocks_per_cluster; 1838 unsigned long cnt = 0; 1839 unsigned long limit = global_zone_page_state(NR_FREE_PAGES) 1840 << (PAGE_SHIFT - sb->s_blocksize_bits); 1841 1842 if (limit >= 0x2000) 1843 limit -= 0x1000; 1844 else if (limit < 32) 1845 limit = 32; 1846 else 1847 limit >>= 1; 1848 1849 while (blocks--) { 1850 clean_bdev_aliases(bdev, devblock++, 1); 1851 if (cnt++ >= limit) { 1852 sync_blockdev(bdev); 1853 cnt = 0; 1854 } 1855 } 1856 } 1857 1858 /* 1859 * ntfs_discard - Issue a discard request (trim for SSD). 1860 */ 1861 int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len) 1862 { 1863 int err; 1864 u64 lbo, bytes, start, end; 1865 struct super_block *sb; 1866 1867 if (sbi->used.next_free_lcn == lcn + len) 1868 sbi->used.next_free_lcn = lcn; 1869 1870 if (sbi->flags & NTFS_FLAGS_NODISCARD) 1871 return -EOPNOTSUPP; 1872 1873 if (!sbi->options->discard) 1874 return -EOPNOTSUPP; 1875 1876 lbo = (u64)lcn << sbi->cluster_bits; 1877 bytes = (u64)len << sbi->cluster_bits; 1878 1879 /* Align up 'start' on discard_granularity. */ 1880 start = (lbo + sbi->discard_granularity - 1) & 1881 sbi->discard_granularity_mask_inv; 1882 /* Align down 'end' on discard_granularity. */ 1883 end = (lbo + bytes) & sbi->discard_granularity_mask_inv; 1884 1885 sb = sbi->sb; 1886 if (start >= end) 1887 return 0; 1888 1889 err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9, 1890 GFP_NOFS); 1891 1892 if (err == -EOPNOTSUPP) 1893 sbi->flags |= NTFS_FLAGS_NODISCARD; 1894 1895 return err; 1896 } 1897 1898 static int ntfs_fs_get_tree(struct fs_context *fc) 1899 { 1900 return get_tree_bdev(fc, ntfs_fill_super); 1901 } 1902 1903 /* 1904 * ntfs_fs_free - Free fs_context. 1905 * 1906 * Note that this will be called after fill_super and reconfigure 1907 * even when they pass. So they have to take pointers if they pass. 1908 */ 1909 static void ntfs_fs_free(struct fs_context *fc) 1910 { 1911 struct ntfs_mount_options *opts = fc->fs_private; 1912 struct ntfs_sb_info *sbi = fc->s_fs_info; 1913 1914 if (sbi) { 1915 ntfs3_put_sbi(sbi); 1916 ntfs3_free_sbi(sbi); 1917 } 1918 1919 if (opts) 1920 put_mount_options(opts); 1921 } 1922 1923 // clang-format off 1924 static const struct fs_context_operations ntfs_context_ops = { 1925 .parse_param = ntfs_fs_parse_param, 1926 .get_tree = ntfs_fs_get_tree, 1927 .reconfigure = ntfs_fs_reconfigure, 1928 .free = ntfs_fs_free, 1929 }; 1930 // clang-format on 1931 1932 /* 1933 * ntfs_init_fs_context - Initialize sbi and opts 1934 * 1935 * This will called when mount/remount. We will first initialize 1936 * options so that if remount we can use just that. 1937 */ 1938 static int ntfs_init_fs_context(struct fs_context *fc) 1939 { 1940 struct ntfs_mount_options *opts; 1941 struct ntfs_sb_info *sbi; 1942 1943 opts = kzalloc_obj(struct ntfs_mount_options, GFP_NOFS); 1944 if (!opts) 1945 return -ENOMEM; 1946 1947 /* Default options. */ 1948 opts->fs_uid = current_uid(); 1949 opts->fs_gid = current_gid(); 1950 opts->fs_fmask_inv = opts->fs_dmask_inv = ~current_umask(); 1951 opts->prealloc = 1; 1952 opts->ads = 1; 1953 opts->ugm = 1; 1954 1955 #ifdef CONFIG_NTFS3_FS_POSIX_ACL 1956 /* Set the default value 'acl' */ 1957 fc->sb_flags |= SB_POSIXACL; 1958 #endif 1959 1960 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) 1961 goto ok; 1962 1963 sbi = kzalloc_obj(struct ntfs_sb_info, GFP_NOFS); 1964 if (!sbi) 1965 goto free_opts; 1966 1967 sbi->upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL); 1968 if (!sbi->upcase) 1969 goto free_sbi; 1970 1971 ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL, 1972 DEFAULT_RATELIMIT_BURST); 1973 1974 mutex_init(&sbi->compress.mtx_lznt); 1975 #ifdef CONFIG_NTFS3_LZX_XPRESS 1976 mutex_init(&sbi->compress.mtx_xpress); 1977 mutex_init(&sbi->compress.mtx_lzx); 1978 #endif 1979 1980 fc->s_fs_info = sbi; 1981 ok: 1982 fc->fs_private = opts; 1983 fc->ops = &ntfs_context_ops; 1984 1985 return 0; 1986 free_sbi: 1987 kfree(sbi); 1988 free_opts: 1989 kfree(opts); 1990 return -ENOMEM; 1991 } 1992 1993 static void ntfs3_kill_sb(struct super_block *sb) 1994 { 1995 struct ntfs_sb_info *sbi = sb->s_fs_info; 1996 1997 kill_block_super(sb); 1998 1999 /* 2000 * A pathwalk in rcu mode may still be in ->d_hash() or ->d_compare() 2001 * and read the upcase table and the nls table through the sbi. 2002 */ 2003 call_rcu(&sbi->rcu, ntfs3_free_sbi_rcu); 2004 } 2005 2006 // clang-format off 2007 static struct file_system_type ntfs_fs_type = { 2008 .owner = THIS_MODULE, 2009 .name = "ntfs3", 2010 .init_fs_context = ntfs_init_fs_context, 2011 .parameters = ntfs_fs_parameters, 2012 .kill_sb = ntfs3_kill_sb, 2013 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP, 2014 }; 2015 // clang-format on 2016 2017 static int __init init_ntfs_fs(void) 2018 { 2019 int err; 2020 2021 if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL)) 2022 pr_info("ntfs3: Enabled Linux POSIX ACLs support\n"); 2023 if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER)) 2024 pr_notice( 2025 "ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n"); 2026 if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS)) 2027 pr_info("ntfs3: Read-only LZX/Xpress compression included\n"); 2028 2029 ntfs_create_proc_root(); 2030 2031 err = ntfs3_init_bitmap(); 2032 if (err) 2033 goto out2; 2034 2035 ntfs_inode_cachep = kmem_cache_create( 2036 "ntfs_inode_cache", sizeof(struct ntfs_inode), 0, 2037 (SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT), init_once); 2038 if (!ntfs_inode_cachep) { 2039 err = -ENOMEM; 2040 goto out1; 2041 } 2042 2043 err = register_filesystem(&ntfs_fs_type); 2044 if (err) 2045 goto out; 2046 2047 return 0; 2048 out: 2049 kmem_cache_destroy(ntfs_inode_cachep); 2050 out1: 2051 ntfs3_exit_bitmap(); 2052 out2: 2053 ntfs_remove_proc_root(); 2054 return err; 2055 } 2056 2057 static void __exit exit_ntfs_fs(void) 2058 { 2059 rcu_barrier(); 2060 kmem_cache_destroy(ntfs_inode_cachep); 2061 unregister_filesystem(&ntfs_fs_type); 2062 ntfs3_exit_bitmap(); 2063 ntfs_remove_proc_root(); 2064 } 2065 2066 MODULE_LICENSE("GPL"); 2067 MODULE_DESCRIPTION("ntfs3 read/write filesystem"); 2068 #ifdef CONFIG_NTFS3_FS_POSIX_ACL 2069 MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support"); 2070 #endif 2071 #ifdef CONFIG_NTFS3_64BIT_CLUSTER 2072 MODULE_INFO( 2073 cluster, 2074 "Warning: Activated 64 bits per cluster. Windows does not support this"); 2075 #endif 2076 #ifdef CONFIG_NTFS3_LZX_XPRESS 2077 MODULE_INFO(compression, "Read-only lzx/xpress compression included"); 2078 #endif 2079 2080 MODULE_AUTHOR("Konstantin Komarov"); 2081 MODULE_ALIAS_FS("ntfs3"); 2082 2083 module_init(init_ntfs_fs); 2084 module_exit(exit_ntfs_fs); 2085