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