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