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