1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * NTFS kernel super block handling. 4 * 5 * Copyright (c) 2001-2012 Anton Altaparmakov and Tuxera Inc. 6 * Copyright (c) 2001,2002 Richard Russon 7 * Copyright (c) 2025 LG Electronics Co., Ltd. 8 */ 9 10 #include <linux/blkdev.h> /* For bdev_logical_block_size(). */ 11 #include <linux/backing-dev.h> 12 #include <linux/vfs.h> 13 #include <linux/fs_struct.h> 14 #include <linux/sched/mm.h> 15 #include <linux/fs_context.h> 16 #include <linux/fs_parser.h> 17 18 #include "sysctl.h" 19 #include "logfile.h" 20 #include "quota.h" 21 #include "index.h" 22 #include "ntfs.h" 23 #include "ea.h" 24 #include "volume.h" 25 26 /* A global default upcase table and a corresponding reference count. */ 27 static __le16 *default_upcase; 28 static unsigned long ntfs_nr_upcase_users; 29 30 static struct workqueue_struct *ntfs_wq; 31 32 /* Error constants/strings used in inode.c::ntfs_show_options(). */ 33 enum { 34 /* One of these must be present, default is ON_ERRORS_CONTINUE. */ 35 ON_ERRORS_PANIC = 0x01, 36 ON_ERRORS_REMOUNT_RO = 0x02, 37 ON_ERRORS_CONTINUE = 0x04, 38 }; 39 40 static const struct constant_table ntfs_param_enums[] = { 41 { "panic", ON_ERRORS_PANIC }, 42 { "remount-ro", ON_ERRORS_REMOUNT_RO }, 43 { "continue", ON_ERRORS_CONTINUE }, 44 {} 45 }; 46 47 enum { 48 Opt_uid, 49 Opt_gid, 50 Opt_umask, 51 Opt_dmask, 52 Opt_fmask, 53 Opt_errors, 54 Opt_nls, 55 Opt_charset, 56 Opt_show_sys_files, 57 Opt_show_meta, 58 Opt_case_sensitive, 59 Opt_disable_sparse, 60 Opt_sparse, 61 Opt_mft_zone_multiplier, 62 Opt_preallocated_size, 63 Opt_sys_immutable, 64 Opt_nohidden, 65 Opt_hide_dot_files, 66 Opt_check_windows_names, 67 Opt_acl, 68 Opt_discard, 69 Opt_nocase, 70 }; 71 72 static const struct fs_parameter_spec ntfs_parameters[] = { 73 fsparam_u32("uid", Opt_uid), 74 fsparam_u32("gid", Opt_gid), 75 fsparam_u32oct("umask", Opt_umask), 76 fsparam_u32oct("dmask", Opt_dmask), 77 fsparam_u32oct("fmask", Opt_fmask), 78 fsparam_string("nls", Opt_nls), 79 fsparam_string("iocharset", Opt_charset), 80 fsparam_enum("errors", Opt_errors, ntfs_param_enums), 81 fsparam_flag("show_sys_files", Opt_show_sys_files), 82 fsparam_flag("showmeta", Opt_show_meta), 83 fsparam_flag("case_sensitive", Opt_case_sensitive), 84 fsparam_flag("disable_sparse", Opt_disable_sparse), 85 fsparam_s32("mft_zone_multiplier", Opt_mft_zone_multiplier), 86 fsparam_u64("preallocated_size", Opt_preallocated_size), 87 fsparam_flag("sys_immutable", Opt_sys_immutable), 88 fsparam_flag("nohidden", Opt_nohidden), 89 fsparam_flag("hide_dot_files", Opt_hide_dot_files), 90 fsparam_flag("windows_names", Opt_check_windows_names), 91 fsparam_flag("acl", Opt_acl), 92 fsparam_flag("discard", Opt_discard), 93 fsparam_flag("sparse", Opt_sparse), 94 fsparam_flag("nocase", Opt_nocase), 95 {} 96 }; 97 98 static int ntfs_parse_param(struct fs_context *fc, struct fs_parameter *param) 99 { 100 struct ntfs_volume *vol = fc->s_fs_info; 101 struct fs_parse_result result; 102 int opt; 103 104 opt = fs_parse(fc, ntfs_parameters, param, &result); 105 if (opt < 0) 106 return opt; 107 108 switch (opt) { 109 case Opt_uid: 110 vol->uid = make_kuid(current_user_ns(), result.uint_32); 111 break; 112 case Opt_gid: 113 vol->gid = make_kgid(current_user_ns(), result.uint_32); 114 break; 115 case Opt_umask: 116 vol->fmask = vol->dmask = result.uint_32; 117 break; 118 case Opt_dmask: 119 vol->dmask = result.uint_32; 120 break; 121 case Opt_fmask: 122 vol->fmask = result.uint_32; 123 break; 124 case Opt_errors: 125 vol->on_errors = result.uint_32; 126 break; 127 case Opt_nls: 128 case Opt_charset: 129 if (vol->nls_map) 130 unload_nls(vol->nls_map); 131 vol->nls_map = load_nls(param->string); 132 if (!vol->nls_map) { 133 ntfs_error(vol->sb, "Failed to load NLS table '%s'.", 134 param->string); 135 return -EINVAL; 136 } 137 break; 138 case Opt_mft_zone_multiplier: 139 if (vol->mft_zone_multiplier && vol->mft_zone_multiplier != 140 result.int_32) { 141 ntfs_error(vol->sb, "Cannot change mft_zone_multiplier on remount."); 142 return -EINVAL; 143 } 144 if (result.int_32 < 1 || result.int_32 > 4) { 145 ntfs_error(vol->sb, 146 "Invalid mft_zone_multiplier. Using default value, i.e. 1."); 147 vol->mft_zone_multiplier = 1; 148 } else 149 vol->mft_zone_multiplier = result.int_32; 150 break; 151 case Opt_show_sys_files: 152 case Opt_show_meta: 153 if (result.boolean) 154 NVolSetShowSystemFiles(vol); 155 else 156 NVolClearShowSystemFiles(vol); 157 break; 158 case Opt_case_sensitive: 159 if (result.boolean) 160 NVolSetCaseSensitive(vol); 161 else 162 NVolClearCaseSensitive(vol); 163 break; 164 case Opt_nocase: 165 if (result.boolean) 166 NVolClearCaseSensitive(vol); 167 else 168 NVolSetCaseSensitive(vol); 169 break; 170 case Opt_preallocated_size: 171 vol->preallocated_size = (loff_t)result.uint_64; 172 break; 173 case Opt_sys_immutable: 174 if (result.boolean) 175 NVolSetSysImmutable(vol); 176 else 177 NVolClearSysImmutable(vol); 178 break; 179 case Opt_nohidden: 180 if (result.boolean) 181 NVolClearShowHiddenFiles(vol); 182 else 183 NVolSetShowHiddenFiles(vol); 184 break; 185 case Opt_hide_dot_files: 186 if (result.boolean) 187 NVolSetHideDotFiles(vol); 188 else 189 NVolClearHideDotFiles(vol); 190 break; 191 case Opt_check_windows_names: 192 if (result.boolean) 193 NVolSetCheckWindowsNames(vol); 194 else 195 NVolClearCheckWindowsNames(vol); 196 break; 197 case Opt_acl: 198 #ifdef CONFIG_NTFS_FS_POSIX_ACL 199 if (result.boolean) 200 fc->sb_flags |= SB_POSIXACL; 201 else 202 fc->sb_flags &= ~SB_POSIXACL; 203 break; 204 #else 205 return -EINVAL; 206 #endif 207 case Opt_discard: 208 if (result.boolean) 209 NVolSetDiscard(vol); 210 else 211 NVolClearDiscard(vol); 212 break; 213 case Opt_disable_sparse: 214 if (result.boolean) 215 NVolSetDisableSparse(vol); 216 else 217 NVolClearDisableSparse(vol); 218 break; 219 case Opt_sparse: 220 break; 221 default: 222 return -EINVAL; 223 } 224 225 return 0; 226 } 227 228 static int ntfs_reconfigure(struct fs_context *fc) 229 { 230 struct super_block *sb = fc->root->d_sb; 231 struct ntfs_volume *vol = NTFS_SB(sb); 232 233 ntfs_debug("Entering with remount"); 234 235 sync_filesystem(sb); 236 237 /* 238 * For the read-write compiled driver, if we are remounting read-write, 239 * make sure there are no volume errors and that no unsupported volume 240 * flags are set. Also, empty the logfile journal as it would become 241 * stale as soon as something is written to the volume and mark the 242 * volume dirty so that chkdsk is run if the volume is not umounted 243 * cleanly. Finally, mark the quotas out of date so Windows rescans 244 * the volume on boot and updates them. 245 * 246 * When remounting read-only, mark the volume clean if no volume errors 247 * have occurred. 248 */ 249 if (sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY)) { 250 static const char *es = ". Cannot remount read-write."; 251 252 /* Remounting read-write. */ 253 if (NVolErrors(vol)) { 254 ntfs_error(sb, "Volume has errors and is read-only%s", 255 es); 256 return -EROFS; 257 } 258 if (vol->vol_flags & VOLUME_IS_DIRTY) { 259 ntfs_error(sb, "Volume is dirty and read-only%s", es); 260 return -EROFS; 261 } 262 if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) { 263 ntfs_error(sb, "Volume has been modified by chkdsk and is read-only%s", es); 264 return -EROFS; 265 } 266 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) { 267 ntfs_error(sb, "Volume has unsupported flags set (0x%x) and is read-only%s", 268 le16_to_cpu(vol->vol_flags), es); 269 return -EROFS; 270 } 271 if (vol->logfile_ino && !ntfs_empty_logfile(vol->logfile_ino)) { 272 ntfs_error(sb, "Failed to empty journal LogFile%s", 273 es); 274 NVolSetErrors(vol); 275 return -EROFS; 276 } 277 if (!ntfs_mark_quotas_out_of_date(vol)) { 278 ntfs_error(sb, "Failed to mark quotas out of date%s", 279 es); 280 NVolSetErrors(vol); 281 return -EROFS; 282 } 283 } else if (!sb_rdonly(sb) && (fc->sb_flags & SB_RDONLY)) { 284 /* Remounting read-only. */ 285 if (!NVolErrors(vol)) { 286 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY)) 287 ntfs_warning(sb, 288 "Failed to clear dirty bit in volume information flags. Run chkdsk."); 289 } 290 } 291 292 ntfs_debug("Done."); 293 return 0; 294 } 295 296 const struct option_t on_errors_arr[] = { 297 { ON_ERRORS_PANIC, "panic" }, 298 { ON_ERRORS_REMOUNT_RO, "remount-ro", }, 299 { ON_ERRORS_CONTINUE, "continue", }, 300 { 0, NULL } 301 }; 302 303 void ntfs_handle_error(struct super_block *sb) 304 { 305 struct ntfs_volume *vol = NTFS_SB(sb); 306 307 if (sb_rdonly(sb)) 308 return; 309 310 if (vol->on_errors == ON_ERRORS_REMOUNT_RO) { 311 sb->s_flags |= SB_RDONLY; 312 pr_crit("(device %s): Filesystem has been set read-only\n", 313 sb->s_id); 314 } else if (vol->on_errors == ON_ERRORS_PANIC) { 315 panic("ntfs: (device %s): panic from previous error\n", 316 sb->s_id); 317 } else if (vol->on_errors == ON_ERRORS_CONTINUE) { 318 if (errseq_check(&sb->s_wb_err, vol->wb_err) == -ENODEV) { 319 NVolSetShutdown(vol); 320 vol->wb_err = sb->s_wb_err; 321 } 322 } 323 } 324 325 /* 326 * ntfs_write_volume_flags - write new flags to the volume information flags 327 * @vol: ntfs volume on which to modify the flags 328 * @flags: new flags value for the volume information flags 329 * 330 * Internal function. You probably want to use ntfs_{set,clear}_volume_flags() 331 * instead (see below). 332 * 333 * Replace the volume information flags on the volume @vol with the value 334 * supplied in @flags. Note, this overwrites the volume information flags, so 335 * make sure to combine the flags you want to modify with the old flags and use 336 * the result when calling ntfs_write_volume_flags(). 337 * 338 * Return 0 on success and -errno on error. 339 */ 340 static int ntfs_write_volume_flags(struct ntfs_volume *vol, const __le16 flags) 341 { 342 struct ntfs_inode *ni = NTFS_I(vol->vol_ino); 343 struct volume_information *vi; 344 struct ntfs_attr_search_ctx *ctx; 345 int err; 346 347 ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.", 348 le16_to_cpu(vol->vol_flags), le16_to_cpu(flags)); 349 mutex_lock(&ni->mrec_lock); 350 if (vol->vol_flags == flags) 351 goto done; 352 353 ctx = ntfs_attr_get_search_ctx(ni, NULL); 354 if (!ctx) { 355 err = -ENOMEM; 356 goto put_unm_err_out; 357 } 358 359 err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0, 360 ctx); 361 if (err) 362 goto put_unm_err_out; 363 364 vi = (struct volume_information *)((u8 *)ctx->attr + 365 le16_to_cpu(ctx->attr->data.resident.value_offset)); 366 vol->vol_flags = vi->flags = flags; 367 mark_mft_record_dirty(ctx->ntfs_ino); 368 ntfs_attr_put_search_ctx(ctx); 369 done: 370 mutex_unlock(&ni->mrec_lock); 371 ntfs_debug("Done."); 372 return 0; 373 put_unm_err_out: 374 if (ctx) 375 ntfs_attr_put_search_ctx(ctx); 376 mutex_unlock(&ni->mrec_lock); 377 ntfs_error(vol->sb, "Failed with error code %i.", -err); 378 return err; 379 } 380 381 /* 382 * ntfs_set_volume_flags - set bits in the volume information flags 383 * @vol: ntfs volume on which to modify the flags 384 * @flags: flags to set on the volume 385 * 386 * Set the bits in @flags in the volume information flags on the volume @vol. 387 * 388 * Return 0 on success and -errno on error. 389 */ 390 int ntfs_set_volume_flags(struct ntfs_volume *vol, __le16 flags) 391 { 392 flags &= VOLUME_FLAGS_MASK; 393 return ntfs_write_volume_flags(vol, vol->vol_flags | flags); 394 } 395 396 /* 397 * ntfs_clear_volume_flags - clear bits in the volume information flags 398 * @vol: ntfs volume on which to modify the flags 399 * @flags: flags to clear on the volume 400 * 401 * Clear the bits in @flags in the volume information flags on the volume @vol. 402 * 403 * Return 0 on success and -errno on error. 404 */ 405 int ntfs_clear_volume_flags(struct ntfs_volume *vol, __le16 flags) 406 { 407 flags &= VOLUME_FLAGS_MASK; 408 flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags)); 409 return ntfs_write_volume_flags(vol, flags); 410 } 411 412 int ntfs_write_volume_label(struct ntfs_volume *vol, char *label) 413 { 414 struct ntfs_inode *vol_ni = NTFS_I(vol->vol_ino); 415 struct ntfs_attr_search_ctx *ctx; 416 char *new_label; 417 __le16 *uname; 418 int uname_len, ret; 419 420 uname_len = ntfs_nlstoucs(vol, label, strlen(label), 421 &uname, FSLABEL_MAX); 422 if (uname_len < 0) { 423 ntfs_error(vol->sb, 424 "Failed to convert volume label '%s' to Unicode.", 425 label); 426 return uname_len; 427 } 428 429 if (uname_len > NTFS_MAX_LABEL_LEN) { 430 ntfs_error(vol->sb, 431 "Volume label is too long (max %d characters).", 432 NTFS_MAX_LABEL_LEN); 433 kvfree(uname); 434 return -EINVAL; 435 } 436 437 /* 438 * Allocate the in-memory label copy up front. If kstrdup() fails we 439 * bail out before touching on-disk metadata, so the in-memory label 440 * and the on-disk label stay in sync. 441 */ 442 new_label = kstrdup(label, GFP_KERNEL); 443 if (!new_label) { 444 kvfree(uname); 445 return -ENOMEM; 446 } 447 448 mutex_lock(&vol_ni->mrec_lock); 449 ctx = ntfs_attr_get_search_ctx(vol_ni, NULL); 450 if (!ctx) { 451 ret = -ENOMEM; 452 goto out; 453 } 454 455 if (!ntfs_attr_lookup(AT_VOLUME_NAME, NULL, 0, 0, 0, NULL, 0, 456 ctx)) 457 ntfs_attr_record_rm(ctx); 458 ntfs_attr_put_search_ctx(ctx); 459 460 ret = ntfs_resident_attr_record_add(vol_ni, AT_VOLUME_NAME, AT_UNNAMED, 0, 461 (u8 *)uname, uname_len * sizeof(__le16), 0); 462 out: 463 mutex_unlock(&vol_ni->mrec_lock); 464 kvfree(uname); 465 466 if (ret >= 0) { 467 kfree(vol->volume_label); 468 vol->volume_label = new_label; 469 mark_inode_dirty_sync(vol->vol_ino); 470 ret = 0; 471 } else { 472 kfree(new_label); 473 } 474 return ret; 475 } 476 477 /* 478 * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector 479 * @sb: Super block of the device to which @b belongs. 480 * @b: Boot sector of device @sb to check. 481 * @silent: If 'true', all output will be silenced. 482 * 483 * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot 484 * sector. Returns 'true' if it is valid and 'false' if not. 485 * 486 * @sb is only needed for warning/error output, i.e. it can be NULL when silent 487 * is 'true'. 488 */ 489 static bool is_boot_sector_ntfs(const struct super_block *sb, 490 const struct ntfs_boot_sector *b, const bool silent) 491 { 492 /* 493 * Check that checksum == sum of u32 values from b to the checksum 494 * field. If checksum is zero, no checking is done. We will work when 495 * the checksum test fails, since some utilities update the boot sector 496 * ignoring the checksum which leaves the checksum out-of-date. We 497 * report a warning if this is the case. 498 */ 499 if ((void *)b < (void *)&b->checksum && b->checksum && !silent) { 500 __le32 *u; 501 u32 i; 502 503 for (i = 0, u = (__le32 *)b; u < (__le32 *)(&b->checksum); ++u) 504 i += le32_to_cpup(u); 505 if (le32_to_cpu(b->checksum) != i) 506 ntfs_warning(sb, "Invalid boot sector checksum."); 507 } 508 /* Check OEMidentifier is "NTFS " */ 509 if (b->oem_id != magicNTFS) 510 goto not_ntfs; 511 /* Check bytes per sector value is between 256 and 4096. */ 512 if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 || 513 le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000) 514 goto not_ntfs; 515 /* 516 * Check sectors per cluster value is valid and the cluster size 517 * is not above the maximum (2MB). 518 */ 519 if (b->bpb.sectors_per_cluster > 0x80 && 520 b->bpb.sectors_per_cluster < 0xf4) 521 goto not_ntfs; 522 523 /* Check reserved/unused fields are really zero. */ 524 if (le16_to_cpu(b->bpb.reserved_sectors) || 525 le16_to_cpu(b->bpb.root_entries) || 526 le16_to_cpu(b->bpb.sectors) || 527 le16_to_cpu(b->bpb.sectors_per_fat) || 528 le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats) 529 goto not_ntfs; 530 /* Check clusters per file mft record value is valid. */ 531 if ((u8)b->clusters_per_mft_record < 0xe1 || 532 (u8)b->clusters_per_mft_record > 0xf7) 533 switch (b->clusters_per_mft_record) { 534 case 1: case 2: case 4: case 8: case 16: case 32: case 64: 535 break; 536 default: 537 goto not_ntfs; 538 } 539 /* Check clusters per index block value is valid. */ 540 if ((u8)b->clusters_per_index_record < 0xe1 || 541 (u8)b->clusters_per_index_record > 0xf7) 542 switch (b->clusters_per_index_record) { 543 case 1: case 2: case 4: case 8: case 16: case 32: case 64: 544 break; 545 default: 546 goto not_ntfs; 547 } 548 /* 549 * Check for valid end of sector marker. We will work without it, but 550 * many BIOSes will refuse to boot from a bootsector if the magic is 551 * incorrect, so we emit a warning. 552 */ 553 if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55)) 554 ntfs_warning(sb, "Invalid end of sector marker."); 555 return true; 556 not_ntfs: 557 return false; 558 } 559 560 /* 561 * read_ntfs_boot_sector - read the NTFS boot sector of a device 562 * @sb: super block of device to read the boot sector from 563 * @silent: if true, suppress all output 564 * 565 * Reads the boot sector from the device and validates it. 566 */ 567 static char *read_ntfs_boot_sector(struct super_block *sb, 568 const int silent) 569 { 570 char *boot_sector; 571 572 boot_sector = kzalloc(PAGE_SIZE, GFP_NOFS); 573 if (!boot_sector) 574 return NULL; 575 576 if (ntfs_bdev_read(sb->s_bdev, boot_sector, 0, PAGE_SIZE)) { 577 if (!silent) 578 ntfs_error(sb, "Unable to read primary boot sector."); 579 kfree(boot_sector); 580 return NULL; 581 } 582 583 if (!is_boot_sector_ntfs(sb, (struct ntfs_boot_sector *)boot_sector, 584 silent)) { 585 if (!silent) 586 ntfs_error(sb, "Primary boot sector is invalid."); 587 kfree(boot_sector); 588 return NULL; 589 } 590 591 return boot_sector; 592 } 593 594 /* 595 * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol 596 * @vol: volume structure to initialise with data from boot sector 597 * @b: boot sector to parse 598 * 599 * Parse the ntfs boot sector @b and store all imporant information therein in 600 * the ntfs super block @vol. Return 'true' on success and 'false' on error. 601 */ 602 static bool parse_ntfs_boot_sector(struct ntfs_volume *vol, 603 const struct ntfs_boot_sector *b) 604 { 605 unsigned int sectors_per_cluster, sectors_per_cluster_bits, nr_hidden_sects; 606 int clusters_per_mft_record, clusters_per_index_record; 607 s64 ll; 608 609 vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector); 610 vol->sector_size_bits = ffs(vol->sector_size) - 1; 611 ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size, 612 vol->sector_size); 613 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits, 614 vol->sector_size_bits); 615 if (vol->sector_size < vol->sb->s_blocksize) { 616 ntfs_error(vol->sb, 617 "Sector size (%i) is smaller than the device block size (%lu). This is not supported.", 618 vol->sector_size, vol->sb->s_blocksize); 619 return false; 620 } 621 622 if (b->bpb.sectors_per_cluster >= 0xf4) 623 sectors_per_cluster = 1U << -(s8)b->bpb.sectors_per_cluster; 624 else 625 sectors_per_cluster = b->bpb.sectors_per_cluster; 626 ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster); 627 sectors_per_cluster_bits = ffs(sectors_per_cluster) - 1; 628 ntfs_debug("sectors_per_cluster_bits = 0x%x", 629 sectors_per_cluster_bits); 630 nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors); 631 ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects); 632 vol->cluster_size = vol->sector_size << sectors_per_cluster_bits; 633 vol->cluster_size_mask = vol->cluster_size - 1; 634 vol->cluster_size_bits = ffs(vol->cluster_size) - 1; 635 ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size, 636 vol->cluster_size); 637 ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask); 638 ntfs_debug("vol->cluster_size_bits = %i", vol->cluster_size_bits); 639 if (vol->cluster_size < vol->sector_size) { 640 ntfs_error(vol->sb, 641 "Cluster size (%i) is smaller than the sector size (%i). This is not supported.", 642 vol->cluster_size, vol->sector_size); 643 return false; 644 } 645 clusters_per_mft_record = b->clusters_per_mft_record; 646 ntfs_debug("clusters_per_mft_record = %i (0x%x)", 647 clusters_per_mft_record, clusters_per_mft_record); 648 if (clusters_per_mft_record > 0) 649 vol->mft_record_size = vol->cluster_size << 650 (ffs(clusters_per_mft_record) - 1); 651 else 652 /* 653 * When mft_record_size < cluster_size, clusters_per_mft_record 654 * = -log2(mft_record_size) bytes. mft_record_size normaly is 655 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal). 656 */ 657 vol->mft_record_size = 1 << -clusters_per_mft_record; 658 vol->mft_record_size_mask = vol->mft_record_size - 1; 659 vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1; 660 ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size, 661 vol->mft_record_size); 662 ntfs_debug("vol->mft_record_size_mask = 0x%x", 663 vol->mft_record_size_mask); 664 ntfs_debug("vol->mft_record_size_bits = %i (0x%x)", 665 vol->mft_record_size_bits, vol->mft_record_size_bits); 666 /* 667 * We cannot support mft record sizes above the PAGE_SIZE since 668 * we store $MFT/$DATA, the table of mft records in the page cache. 669 */ 670 if (vol->mft_record_size > PAGE_SIZE) { 671 ntfs_error(vol->sb, 672 "Mft record size (%i) exceeds the PAGE_SIZE on your system (%lu). This is not supported.", 673 vol->mft_record_size, PAGE_SIZE); 674 return false; 675 } 676 /* We cannot support mft record sizes below the sector size. */ 677 if (vol->mft_record_size < vol->sector_size) { 678 ntfs_warning(vol->sb, "Mft record size (%i) is smaller than the sector size (%i).", 679 vol->mft_record_size, vol->sector_size); 680 } 681 clusters_per_index_record = b->clusters_per_index_record; 682 ntfs_debug("clusters_per_index_record = %i (0x%x)", 683 clusters_per_index_record, clusters_per_index_record); 684 if (clusters_per_index_record > 0) 685 vol->index_record_size = vol->cluster_size << 686 (ffs(clusters_per_index_record) - 1); 687 else 688 /* 689 * When index_record_size < cluster_size, 690 * clusters_per_index_record = -log2(index_record_size) bytes. 691 * index_record_size normaly equals 4096 bytes, which is 692 * encoded as 0xF4 (-12 in decimal). 693 */ 694 vol->index_record_size = 1 << -clusters_per_index_record; 695 vol->index_record_size_mask = vol->index_record_size - 1; 696 vol->index_record_size_bits = ffs(vol->index_record_size) - 1; 697 ntfs_debug("vol->index_record_size = %i (0x%x)", 698 vol->index_record_size, vol->index_record_size); 699 ntfs_debug("vol->index_record_size_mask = 0x%x", 700 vol->index_record_size_mask); 701 ntfs_debug("vol->index_record_size_bits = %i (0x%x)", 702 vol->index_record_size_bits, 703 vol->index_record_size_bits); 704 /* We cannot support index record sizes below the sector size. */ 705 if (vol->index_record_size < vol->sector_size) { 706 ntfs_error(vol->sb, 707 "Index record size (%i) is smaller than the sector size (%i). This is not supported.", 708 vol->index_record_size, vol->sector_size); 709 return false; 710 } 711 /* 712 * Get the size of the volume in clusters and check for 64-bit-ness. 713 * Windows currently only uses 32 bits to save the clusters so we do 714 * the same as it is much faster on 32-bit CPUs. 715 */ 716 ll = le64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits; 717 if ((u64)ll >= 1ULL << 32) { 718 ntfs_error(vol->sb, "Cannot handle 64-bit clusters."); 719 return false; 720 } 721 vol->nr_clusters = ll; 722 ntfs_debug("vol->nr_clusters = 0x%llx", vol->nr_clusters); 723 ll = le64_to_cpu(b->mft_lcn); 724 if (ll >= vol->nr_clusters) { 725 ntfs_error(vol->sb, "MFT LCN (%lli, 0x%llx) is beyond end of volume. Weird.", 726 ll, ll); 727 return false; 728 } 729 vol->mft_lcn = ll; 730 ntfs_debug("vol->mft_lcn = 0x%llx", vol->mft_lcn); 731 ll = le64_to_cpu(b->mftmirr_lcn); 732 if (ll >= vol->nr_clusters) { 733 ntfs_error(vol->sb, "MFTMirr LCN (%lli, 0x%llx) is beyond end of volume. Weird.", 734 ll, ll); 735 return false; 736 } 737 vol->mftmirr_lcn = ll; 738 ntfs_debug("vol->mftmirr_lcn = 0x%llx", vol->mftmirr_lcn); 739 /* 740 * Work out the size of the mft mirror in number of mft records. If the 741 * cluster size is less than or equal to the size taken by four mft 742 * records, the mft mirror stores the first four mft records. If the 743 * cluster size is bigger than the size taken by four mft records, the 744 * mft mirror contains as many mft records as will fit into one 745 * cluster. 746 */ 747 if (vol->cluster_size <= (4 << vol->mft_record_size_bits)) 748 vol->mftmirr_size = 4; 749 else 750 vol->mftmirr_size = vol->cluster_size >> 751 vol->mft_record_size_bits; 752 ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size); 753 vol->serial_no = le64_to_cpu(b->volume_serial_number); 754 ntfs_debug("vol->serial_no = 0x%llx", vol->serial_no); 755 756 vol->sparse_compression_unit = 4; 757 if (vol->cluster_size > 4096) { 758 switch (vol->cluster_size) { 759 case 65536: 760 vol->sparse_compression_unit = 0; 761 break; 762 case 32768: 763 vol->sparse_compression_unit = 1; 764 break; 765 case 16384: 766 vol->sparse_compression_unit = 2; 767 break; 768 case 8192: 769 vol->sparse_compression_unit = 3; 770 break; 771 } 772 } 773 774 return true; 775 } 776 777 /* 778 * ntfs_setup_allocators - initialize the cluster and mft allocators 779 * @vol: volume structure for which to setup the allocators 780 * 781 * Setup the cluster (lcn) and mft allocators to the starting values. 782 */ 783 static void ntfs_setup_allocators(struct ntfs_volume *vol) 784 { 785 s64 mft_zone_size, mft_lcn; 786 787 ntfs_debug("vol->mft_zone_multiplier = 0x%x", 788 vol->mft_zone_multiplier); 789 /* Determine the size of the MFT zone. */ 790 mft_zone_size = vol->nr_clusters; 791 switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */ 792 case 4: 793 mft_zone_size >>= 1; /* 50% */ 794 break; 795 case 3: 796 mft_zone_size = (mft_zone_size + 797 (mft_zone_size >> 1)) >> 2; /* 37.5% */ 798 break; 799 case 2: 800 mft_zone_size >>= 2; /* 25% */ 801 break; 802 /* case 1: */ 803 default: 804 mft_zone_size >>= 3; /* 12.5% */ 805 break; 806 } 807 /* Setup the mft zone. */ 808 vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn; 809 ntfs_debug("vol->mft_zone_pos = 0x%llx", vol->mft_zone_pos); 810 /* 811 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs 812 * source) and if the actual mft_lcn is in the expected place or even 813 * further to the front of the volume, extend the mft_zone to cover the 814 * beginning of the volume as well. This is in order to protect the 815 * area reserved for the mft bitmap as well within the mft_zone itself. 816 * On non-standard volumes we do not protect it as the overhead would 817 * be higher than the speed increase we would get by doing it. 818 */ 819 mft_lcn = NTFS_B_TO_CLU(vol, 8192 + 2 * vol->cluster_size - 1); 820 if (mft_lcn * vol->cluster_size < 16 * 1024) 821 mft_lcn = (16 * 1024 + vol->cluster_size - 1) >> 822 vol->cluster_size_bits; 823 if (vol->mft_zone_start <= mft_lcn) 824 vol->mft_zone_start = 0; 825 ntfs_debug("vol->mft_zone_start = 0x%llx", vol->mft_zone_start); 826 /* 827 * Need to cap the mft zone on non-standard volumes so that it does 828 * not point outside the boundaries of the volume. We do this by 829 * halving the zone size until we are inside the volume. 830 */ 831 vol->mft_zone_end = vol->mft_lcn + mft_zone_size; 832 while (vol->mft_zone_end >= vol->nr_clusters) { 833 mft_zone_size >>= 1; 834 vol->mft_zone_end = vol->mft_lcn + mft_zone_size; 835 } 836 ntfs_debug("vol->mft_zone_end = 0x%llx", vol->mft_zone_end); 837 /* 838 * Set the current position within each data zone to the start of the 839 * respective zone. 840 */ 841 vol->data1_zone_pos = vol->mft_zone_end; 842 ntfs_debug("vol->data1_zone_pos = 0x%llx", vol->data1_zone_pos); 843 vol->data2_zone_pos = 0; 844 ntfs_debug("vol->data2_zone_pos = 0x%llx", vol->data2_zone_pos); 845 846 /* Set the mft data allocation position to mft record 24. */ 847 vol->mft_data_pos = 24; 848 ntfs_debug("vol->mft_data_pos = 0x%llx", vol->mft_data_pos); 849 } 850 851 static struct lock_class_key mftmirr_runlist_lock_key, 852 mftmirr_mrec_lock_key; 853 /* 854 * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume 855 * @vol: ntfs super block describing device whose mft mirror to load 856 * 857 * Return 'true' on success or 'false' on error. 858 */ 859 static bool load_and_init_mft_mirror(struct ntfs_volume *vol) 860 { 861 struct inode *tmp_ino; 862 struct ntfs_inode *tmp_ni; 863 864 ntfs_debug("Entering."); 865 /* Get mft mirror inode. */ 866 tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr); 867 if (IS_ERR(tmp_ino)) { 868 if (!IS_ERR(tmp_ino)) 869 iput(tmp_ino); 870 /* Caller will display error message. */ 871 return false; 872 } 873 lockdep_set_class(&NTFS_I(tmp_ino)->runlist.lock, 874 &mftmirr_runlist_lock_key); 875 lockdep_set_class(&NTFS_I(tmp_ino)->mrec_lock, 876 &mftmirr_mrec_lock_key); 877 /* 878 * Re-initialize some specifics about $MFTMirr's inode as 879 * ntfs_read_inode() will have set up the default ones. 880 */ 881 /* Set uid and gid to root. */ 882 tmp_ino->i_uid = GLOBAL_ROOT_UID; 883 tmp_ino->i_gid = GLOBAL_ROOT_GID; 884 /* Regular file. No access for anyone. */ 885 tmp_ino->i_mode = S_IFREG; 886 /* No VFS initiated operations allowed for $MFTMirr. */ 887 tmp_ino->i_op = &ntfs_empty_inode_ops; 888 tmp_ino->i_fop = &ntfs_empty_file_ops; 889 /* Put in our special address space operations. */ 890 tmp_ino->i_mapping->a_ops = &ntfs_aops; 891 tmp_ni = NTFS_I(tmp_ino); 892 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */ 893 NInoSetMstProtected(tmp_ni); 894 NInoSetSparseDisabled(tmp_ni); 895 /* 896 * Set up our little cheat allowing us to reuse the async read io 897 * completion handler for directories. 898 */ 899 tmp_ni->itype.index.block_size = vol->mft_record_size; 900 tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits; 901 vol->mftmirr_ino = tmp_ino; 902 ntfs_debug("Done."); 903 return true; 904 } 905 906 /* 907 * check_mft_mirror - compare contents of the mft mirror with the mft 908 * @vol: ntfs super block describing device whose mft mirror to check 909 * 910 * Return 'true' on success or 'false' on error. 911 * 912 * Note, this function also results in the mft mirror runlist being completely 913 * mapped into memory. The mft mirror write code requires this and will BUG() 914 * should it find an unmapped runlist element. 915 */ 916 static bool check_mft_mirror(struct ntfs_volume *vol) 917 { 918 struct super_block *sb = vol->sb; 919 struct ntfs_inode *mirr_ni; 920 struct folio *mft_folio = NULL, *mirr_folio = NULL; 921 u8 *kmft = NULL, *kmirr = NULL; 922 struct runlist_element *rl, rl2[2]; 923 pgoff_t index; 924 int mrecs_per_page, i; 925 926 ntfs_debug("Entering."); 927 /* Compare contents of $MFT and $MFTMirr. */ 928 mrecs_per_page = PAGE_SIZE / vol->mft_record_size; 929 index = i = 0; 930 do { 931 u32 bytes; 932 933 /* Switch pages if necessary. */ 934 if (!(i % mrecs_per_page)) { 935 if (index) { 936 kunmap_local(kmirr); 937 folio_put(mirr_folio); 938 kunmap_local(kmft); 939 folio_put(mft_folio); 940 } 941 /* Get the $MFT page. */ 942 mft_folio = read_mapping_folio(vol->mft_ino->i_mapping, 943 index, NULL); 944 if (IS_ERR(mft_folio)) { 945 ntfs_error(sb, "Failed to read $MFT."); 946 return false; 947 } 948 kmft = kmap_local_folio(mft_folio, 0); 949 /* Get the $MFTMirr page. */ 950 mirr_folio = read_mapping_folio(vol->mftmirr_ino->i_mapping, 951 index, NULL); 952 if (IS_ERR(mirr_folio)) { 953 ntfs_error(sb, "Failed to read $MFTMirr."); 954 goto mft_unmap_out; 955 } 956 kmirr = kmap_local_folio(mirr_folio, 0); 957 ++index; 958 } 959 960 /* Do not check the record if it is not in use. */ 961 if (((struct mft_record *)kmft)->flags & MFT_RECORD_IN_USE) { 962 /* Make sure the record is ok. */ 963 if (ntfs_is_baad_recordp((__le32 *)kmft)) { 964 ntfs_error(sb, 965 "Incomplete multi sector transfer detected in mft record %i.", 966 i); 967 mm_unmap_out: 968 kunmap_local(kmirr); 969 folio_put(mirr_folio); 970 mft_unmap_out: 971 kunmap_local(kmft); 972 folio_put(mft_folio); 973 return false; 974 } 975 } 976 /* Do not check the mirror record if it is not in use. */ 977 if (((struct mft_record *)kmirr)->flags & MFT_RECORD_IN_USE) { 978 if (ntfs_is_baad_recordp((__le32 *)kmirr)) { 979 ntfs_error(sb, 980 "Incomplete multi sector transfer detected in mft mirror record %i.", 981 i); 982 goto mm_unmap_out; 983 } 984 } 985 /* Get the amount of data in the current record. */ 986 bytes = le32_to_cpu(((struct mft_record *)kmft)->bytes_in_use); 987 if (bytes < sizeof(struct mft_record_old) || 988 bytes > vol->mft_record_size || 989 ntfs_is_baad_recordp((__le32 *)kmft)) { 990 bytes = le32_to_cpu(((struct mft_record *)kmirr)->bytes_in_use); 991 if (bytes < sizeof(struct mft_record_old) || 992 bytes > vol->mft_record_size || 993 ntfs_is_baad_recordp((__le32 *)kmirr)) 994 bytes = vol->mft_record_size; 995 } 996 /* Compare the two records. */ 997 if (memcmp(kmft, kmirr, bytes)) { 998 ntfs_error(sb, 999 "$MFT and $MFTMirr record %i do not match. Run chkdsk.", 1000 i); 1001 goto mm_unmap_out; 1002 } 1003 kmft += vol->mft_record_size; 1004 kmirr += vol->mft_record_size; 1005 } while (++i < vol->mftmirr_size); 1006 /* Release the last folios. */ 1007 kunmap_local(kmirr); 1008 folio_put(mirr_folio); 1009 kunmap_local(kmft); 1010 folio_put(mft_folio); 1011 1012 /* Construct the mft mirror runlist by hand. */ 1013 rl2[0].vcn = 0; 1014 rl2[0].lcn = vol->mftmirr_lcn; 1015 rl2[0].length = NTFS_B_TO_CLU(vol, vol->mftmirr_size * vol->mft_record_size + 1016 vol->cluster_size - 1); 1017 rl2[1].vcn = rl2[0].length; 1018 rl2[1].lcn = LCN_ENOENT; 1019 rl2[1].length = 0; 1020 /* 1021 * Because we have just read all of the mft mirror, we know we have 1022 * mapped the full runlist for it. 1023 */ 1024 mirr_ni = NTFS_I(vol->mftmirr_ino); 1025 down_read(&mirr_ni->runlist.lock); 1026 rl = mirr_ni->runlist.rl; 1027 /* Compare the two runlists. They must be identical. */ 1028 i = 0; 1029 do { 1030 if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn || 1031 rl2[i].length != rl[i].length) { 1032 ntfs_error(sb, "$MFTMirr location mismatch. Run chkdsk."); 1033 up_read(&mirr_ni->runlist.lock); 1034 return false; 1035 } 1036 } while (rl2[i++].length); 1037 up_read(&mirr_ni->runlist.lock); 1038 ntfs_debug("Done."); 1039 return true; 1040 } 1041 1042 /* 1043 * load_and_check_logfile - load and check the logfile inode for a volume 1044 * @vol: ntfs volume to load the logfile for 1045 * @rp: on success, set to the restart page header 1046 * 1047 * Return 0 on success or errno on error. 1048 */ 1049 static int load_and_check_logfile(struct ntfs_volume *vol, 1050 struct restart_page_header **rp) 1051 { 1052 struct inode *tmp_ino; 1053 int err = 0; 1054 1055 ntfs_debug("Entering."); 1056 tmp_ino = ntfs_iget(vol->sb, FILE_LogFile); 1057 if (IS_ERR(tmp_ino)) { 1058 if (!IS_ERR(tmp_ino)) 1059 iput(tmp_ino); 1060 /* Caller will display error message. */ 1061 return -ENOENT; 1062 } 1063 if (!ntfs_check_logfile(tmp_ino, rp)) 1064 err = -EINVAL; 1065 NInoSetSparseDisabled(NTFS_I(tmp_ino)); 1066 vol->logfile_ino = tmp_ino; 1067 ntfs_debug("Done."); 1068 return err; 1069 } 1070 1071 #define NTFS_HIBERFIL_HEADER_SIZE 4096 1072 1073 /* 1074 * check_windows_hibernation_status - check if Windows is suspended on a volume 1075 * @vol: ntfs super block of device to check 1076 * 1077 * Check if Windows is hibernated on the ntfs volume @vol. This is done by 1078 * looking for the file hiberfil.sys in the root directory of the volume. If 1079 * the file is not present Windows is definitely not suspended. 1080 * 1081 * If hiberfil.sys exists and is less than 4kiB in size it means Windows is 1082 * definitely suspended (this volume is not the system volume). Caveat: on a 1083 * system with many volumes it is possible that the < 4kiB check is bogus but 1084 * for now this should do fine. 1085 * 1086 * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the 1087 * hiberfil header (which is the first 4kiB). If this begins with "hibr", 1088 * Windows is definitely suspended. If it is completely full of zeroes, 1089 * Windows is definitely not hibernated. Any other case is treated as if 1090 * Windows is suspended. This caters for the above mentioned caveat of a 1091 * system with many volumes where no "hibr" magic would be present and there is 1092 * no zero header. 1093 * 1094 * Return 0 if Windows is not hibernated on the volume, >0 if Windows is 1095 * hibernated on the volume, and -errno on error. 1096 */ 1097 static int check_windows_hibernation_status(struct ntfs_volume *vol) 1098 { 1099 static const __le16 hiberfil[13] = { cpu_to_le16('h'), 1100 cpu_to_le16('i'), cpu_to_le16('b'), 1101 cpu_to_le16('e'), cpu_to_le16('r'), 1102 cpu_to_le16('f'), cpu_to_le16('i'), 1103 cpu_to_le16('l'), cpu_to_le16('.'), 1104 cpu_to_le16('s'), cpu_to_le16('y'), 1105 cpu_to_le16('s'), 0 }; 1106 u64 mref; 1107 struct inode *vi; 1108 struct folio *folio; 1109 u32 *kaddr, *kend, *start_addr = NULL; 1110 struct ntfs_name *name = NULL; 1111 int ret = 1; 1112 1113 ntfs_debug("Entering."); 1114 /* 1115 * Find the inode number for the hibernation file by looking up the 1116 * filename hiberfil.sys in the root directory. 1117 */ 1118 inode_lock(vol->root_ino); 1119 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12, 1120 &name); 1121 inode_unlock(vol->root_ino); 1122 kfree(name); 1123 if (IS_ERR_MREF(mref)) { 1124 ret = MREF_ERR(mref); 1125 /* If the file does not exist, Windows is not hibernated. */ 1126 if (ret == -ENOENT) { 1127 ntfs_debug("hiberfil.sys not present. Windows is not hibernated on the volume."); 1128 return 0; 1129 } 1130 /* A real error occurred. */ 1131 ntfs_error(vol->sb, "Failed to find inode number for hiberfil.sys."); 1132 return ret; 1133 } 1134 /* Get the inode. */ 1135 vi = ntfs_iget(vol->sb, MREF(mref)); 1136 if (IS_ERR(vi)) { 1137 if (!IS_ERR(vi)) 1138 iput(vi); 1139 ntfs_error(vol->sb, "Failed to load hiberfil.sys."); 1140 return IS_ERR(vi) ? PTR_ERR(vi) : -EIO; 1141 } 1142 if (unlikely(i_size_read(vi) < NTFS_HIBERFIL_HEADER_SIZE)) { 1143 ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). Windows is hibernated on the volume. This is not the system volume.", 1144 i_size_read(vi)); 1145 goto iput_out; 1146 } 1147 1148 folio = read_mapping_folio(vi->i_mapping, 0, NULL); 1149 if (IS_ERR(folio)) { 1150 ntfs_error(vol->sb, "Failed to read from hiberfil.sys."); 1151 ret = PTR_ERR(folio); 1152 goto iput_out; 1153 } 1154 start_addr = (u32 *)kmap_local_folio(folio, 0); 1155 kaddr = start_addr; 1156 if (*(__le32 *)kaddr == cpu_to_le32(0x72626968)/*'hibr'*/) { 1157 ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is hibernated on the volume. This is the system volume."); 1158 goto unm_iput_out; 1159 } 1160 kend = kaddr + NTFS_HIBERFIL_HEADER_SIZE/sizeof(*kaddr); 1161 do { 1162 if (unlikely(*kaddr)) { 1163 ntfs_debug("hiberfil.sys is larger than 4kiB (0x%llx), does not contain the \"hibr\" magic, and does not have a zero header. Windows is hibernated on the volume. This is not the system volume.", 1164 i_size_read(vi)); 1165 goto unm_iput_out; 1166 } 1167 } while (++kaddr < kend); 1168 ntfs_debug("hiberfil.sys contains a zero header. Windows is not hibernated on the volume. This is the system volume."); 1169 ret = 0; 1170 unm_iput_out: 1171 kunmap_local(start_addr); 1172 folio_put(folio); 1173 iput_out: 1174 iput(vi); 1175 return ret; 1176 } 1177 1178 /* 1179 * load_and_init_quota - load and setup the quota file for a volume if present 1180 * @vol: ntfs super block describing device whose quota file to load 1181 * 1182 * Return 'true' on success or 'false' on error. If $Quota is not present, we 1183 * leave vol->quota_ino as NULL and return success. 1184 */ 1185 static bool load_and_init_quota(struct ntfs_volume *vol) 1186 { 1187 static const __le16 Quota[7] = { cpu_to_le16('$'), 1188 cpu_to_le16('Q'), cpu_to_le16('u'), 1189 cpu_to_le16('o'), cpu_to_le16('t'), 1190 cpu_to_le16('a'), 0 }; 1191 static __le16 Q[3] = { cpu_to_le16('$'), 1192 cpu_to_le16('Q'), 0 }; 1193 struct ntfs_name *name = NULL; 1194 u64 mref; 1195 struct inode *tmp_ino; 1196 1197 ntfs_debug("Entering."); 1198 /* 1199 * Find the inode number for the quota file by looking up the filename 1200 * $Quota in the extended system files directory $Extend. 1201 */ 1202 inode_lock(vol->extend_ino); 1203 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6, 1204 &name); 1205 inode_unlock(vol->extend_ino); 1206 kfree(name); 1207 if (IS_ERR_MREF(mref)) { 1208 /* 1209 * If the file does not exist, quotas are disabled and have 1210 * never been enabled on this volume, just return success. 1211 */ 1212 if (MREF_ERR(mref) == -ENOENT) { 1213 ntfs_debug("$Quota not present. Volume does not have quotas enabled."); 1214 /* 1215 * No need to try to set quotas out of date if they are 1216 * not enabled. 1217 */ 1218 NVolSetQuotaOutOfDate(vol); 1219 return true; 1220 } 1221 /* A real error occurred. */ 1222 ntfs_error(vol->sb, "Failed to find inode number for $Quota."); 1223 return false; 1224 } 1225 /* Get the inode. */ 1226 tmp_ino = ntfs_iget(vol->sb, MREF(mref)); 1227 if (IS_ERR(tmp_ino)) { 1228 if (!IS_ERR(tmp_ino)) 1229 iput(tmp_ino); 1230 ntfs_error(vol->sb, "Failed to load $Quota."); 1231 return false; 1232 } 1233 vol->quota_ino = tmp_ino; 1234 /* Get the $Q index allocation attribute. */ 1235 tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2); 1236 if (IS_ERR(tmp_ino)) { 1237 ntfs_error(vol->sb, "Failed to load $Quota/$Q index."); 1238 return false; 1239 } 1240 vol->quota_q_ino = tmp_ino; 1241 ntfs_debug("Done."); 1242 return true; 1243 } 1244 1245 /* 1246 * load_and_init_attrdef - load the attribute definitions table for a volume 1247 * @vol: ntfs super block describing device whose attrdef to load 1248 * 1249 * Return 'true' on success or 'false' on error. 1250 */ 1251 static bool load_and_init_attrdef(struct ntfs_volume *vol) 1252 { 1253 loff_t i_size; 1254 struct super_block *sb = vol->sb; 1255 struct inode *ino; 1256 struct folio *folio; 1257 u8 *addr; 1258 pgoff_t index, max_index; 1259 unsigned int size; 1260 1261 ntfs_debug("Entering."); 1262 /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */ 1263 ino = ntfs_iget(sb, FILE_AttrDef); 1264 if (IS_ERR(ino)) { 1265 if (!IS_ERR(ino)) 1266 iput(ino); 1267 goto failed; 1268 } 1269 NInoSetSparseDisabled(NTFS_I(ino)); 1270 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */ 1271 i_size = i_size_read(ino); 1272 if (i_size <= 0 || i_size > 0x7fffffff) 1273 goto iput_failed; 1274 vol->attrdef = kvzalloc(i_size, GFP_NOFS); 1275 if (!vol->attrdef) 1276 goto iput_failed; 1277 index = 0; 1278 max_index = i_size >> PAGE_SHIFT; 1279 size = PAGE_SIZE; 1280 while (index < max_index) { 1281 /* Read the attrdef table and copy it into the linear buffer. */ 1282 read_partial_attrdef_page: 1283 folio = read_mapping_folio(ino->i_mapping, index, NULL); 1284 if (IS_ERR(folio)) 1285 goto free_iput_failed; 1286 addr = kmap_local_folio(folio, 0); 1287 memcpy((u8 *)vol->attrdef + (index++ << PAGE_SHIFT), 1288 addr, size); 1289 kunmap_local(addr); 1290 folio_put(folio); 1291 } 1292 if (size == PAGE_SIZE) { 1293 size = i_size & ~PAGE_MASK; 1294 if (size) 1295 goto read_partial_attrdef_page; 1296 } 1297 vol->attrdef_size = i_size; 1298 ntfs_debug("Read %llu bytes from $AttrDef.", i_size); 1299 iput(ino); 1300 return true; 1301 free_iput_failed: 1302 kvfree(vol->attrdef); 1303 vol->attrdef = NULL; 1304 iput_failed: 1305 iput(ino); 1306 failed: 1307 ntfs_error(sb, "Failed to initialize attribute definition table."); 1308 return false; 1309 } 1310 1311 /* 1312 * load_and_init_upcase - load the upcase table for an ntfs volume 1313 * @vol: ntfs super block describing device whose upcase to load 1314 * 1315 * Return 'true' on success or 'false' on error. 1316 */ 1317 static bool load_and_init_upcase(struct ntfs_volume *vol) 1318 { 1319 loff_t i_size; 1320 struct super_block *sb = vol->sb; 1321 struct inode *ino; 1322 struct folio *folio; 1323 u8 *addr; 1324 pgoff_t index, max_index; 1325 unsigned int size; 1326 int i, max; 1327 1328 ntfs_debug("Entering."); 1329 /* Read upcase table and setup vol->upcase and vol->upcase_len. */ 1330 ino = ntfs_iget(sb, FILE_UpCase); 1331 if (IS_ERR(ino)) { 1332 if (!IS_ERR(ino)) 1333 iput(ino); 1334 goto upcase_failed; 1335 } 1336 /* 1337 * The upcase size must not be above 64k Unicode characters, must not 1338 * be zero and must be a multiple of sizeof(__le16). 1339 */ 1340 i_size = i_size_read(ino); 1341 if (!i_size || i_size & (sizeof(__le16) - 1) || 1342 i_size > 64ULL * 1024 * sizeof(__le16)) 1343 goto iput_upcase_failed; 1344 vol->upcase = kvzalloc(i_size, GFP_NOFS); 1345 if (!vol->upcase) 1346 goto iput_upcase_failed; 1347 index = 0; 1348 max_index = i_size >> PAGE_SHIFT; 1349 size = PAGE_SIZE; 1350 while (index < max_index) { 1351 /* Read the upcase table and copy it into the linear buffer. */ 1352 read_partial_upcase_page: 1353 folio = read_mapping_folio(ino->i_mapping, index, NULL); 1354 if (IS_ERR(folio)) 1355 goto iput_upcase_failed; 1356 addr = kmap_local_folio(folio, 0); 1357 memcpy((char *)vol->upcase + (index++ << PAGE_SHIFT), 1358 addr, size); 1359 kunmap_local(addr); 1360 folio_put(folio); 1361 } 1362 if (size == PAGE_SIZE) { 1363 size = i_size & ~PAGE_MASK; 1364 if (size) 1365 goto read_partial_upcase_page; 1366 } 1367 vol->upcase_len = i_size >> sizeof(unsigned char); 1368 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).", 1369 i_size, 64 * 1024 * sizeof(__le16)); 1370 iput(ino); 1371 mutex_lock(&ntfs_lock); 1372 if (!default_upcase) { 1373 ntfs_debug("Using volume specified $UpCase since default is not present."); 1374 mutex_unlock(&ntfs_lock); 1375 return true; 1376 } 1377 max = default_upcase_len; 1378 if (max > vol->upcase_len) 1379 max = vol->upcase_len; 1380 for (i = 0; i < max; i++) 1381 if (vol->upcase[i] != default_upcase[i]) 1382 break; 1383 if (i == max) { 1384 kvfree(vol->upcase); 1385 vol->upcase = default_upcase; 1386 vol->upcase_len = max; 1387 ntfs_nr_upcase_users++; 1388 mutex_unlock(&ntfs_lock); 1389 ntfs_debug("Volume specified $UpCase matches default. Using default."); 1390 return true; 1391 } 1392 mutex_unlock(&ntfs_lock); 1393 ntfs_debug("Using volume specified $UpCase since it does not match the default."); 1394 return true; 1395 iput_upcase_failed: 1396 iput(ino); 1397 kvfree(vol->upcase); 1398 vol->upcase = NULL; 1399 upcase_failed: 1400 mutex_lock(&ntfs_lock); 1401 if (default_upcase) { 1402 vol->upcase = default_upcase; 1403 vol->upcase_len = default_upcase_len; 1404 ntfs_nr_upcase_users++; 1405 mutex_unlock(&ntfs_lock); 1406 ntfs_error(sb, "Failed to load $UpCase from the volume. Using default."); 1407 return true; 1408 } 1409 mutex_unlock(&ntfs_lock); 1410 ntfs_error(sb, "Failed to initialize upcase table."); 1411 return false; 1412 } 1413 1414 /* 1415 * The lcn and mft bitmap inodes are NTFS-internal inodes with 1416 * their own special locking rules: 1417 */ 1418 static struct lock_class_key 1419 lcnbmp_runlist_lock_key, lcnbmp_mrec_lock_key, 1420 mftbmp_runlist_lock_key, mftbmp_mrec_lock_key; 1421 1422 /* 1423 * load_system_files - open the system files using normal functions 1424 * @vol: ntfs super block describing device whose system files to load 1425 * 1426 * Open the system files with normal access functions and complete setting up 1427 * the ntfs super block @vol. 1428 * 1429 * Return 'true' on success or 'false' on error. 1430 */ 1431 static bool load_system_files(struct ntfs_volume *vol) 1432 { 1433 struct super_block *sb = vol->sb; 1434 struct mft_record *m; 1435 struct volume_information *vi; 1436 struct ntfs_attr_search_ctx *ctx; 1437 struct restart_page_header *rp; 1438 int err; 1439 1440 ntfs_debug("Entering."); 1441 /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */ 1442 if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) { 1443 /* If a read-write mount, convert it to a read-only mount. */ 1444 if (!sb_rdonly(sb) && vol->on_errors == ON_ERRORS_REMOUNT_RO) { 1445 static const char *es1 = "Failed to load $MFTMirr"; 1446 static const char *es2 = "$MFTMirr does not match $MFT"; 1447 static const char *es3 = ". Run ntfsck and/or chkdsk."; 1448 1449 sb->s_flags |= SB_RDONLY; 1450 ntfs_error(sb, "%s. Mounting read-only%s", 1451 !vol->mftmirr_ino ? es1 : es2, es3); 1452 } 1453 NVolSetErrors(vol); 1454 } 1455 /* Get mft bitmap attribute inode. */ 1456 vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0); 1457 if (IS_ERR(vol->mftbmp_ino)) { 1458 ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute."); 1459 goto iput_mirr_err_out; 1460 } 1461 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->runlist.lock, 1462 &mftbmp_runlist_lock_key); 1463 lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->mrec_lock, 1464 &mftbmp_mrec_lock_key); 1465 /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */ 1466 if (!load_and_init_upcase(vol)) 1467 goto iput_mftbmp_err_out; 1468 /* 1469 * Read attribute definitions table and setup @vol->attrdef and 1470 * @vol->attrdef_size. 1471 */ 1472 if (!load_and_init_attrdef(vol)) 1473 goto iput_upcase_err_out; 1474 /* 1475 * Get the cluster allocation bitmap inode and verify the size, no 1476 * need for any locking at this stage as we are already running 1477 * exclusively as we are mount in progress task. 1478 */ 1479 vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap); 1480 if (IS_ERR(vol->lcnbmp_ino)) { 1481 if (!IS_ERR(vol->lcnbmp_ino)) 1482 iput(vol->lcnbmp_ino); 1483 goto bitmap_failed; 1484 } 1485 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->runlist.lock, 1486 &lcnbmp_runlist_lock_key); 1487 lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->mrec_lock, 1488 &lcnbmp_mrec_lock_key); 1489 1490 NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino)); 1491 if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) { 1492 iput(vol->lcnbmp_ino); 1493 bitmap_failed: 1494 ntfs_error(sb, "Failed to load $Bitmap."); 1495 goto iput_attrdef_err_out; 1496 } 1497 /* 1498 * Get the volume inode and setup our cache of the volume flags and 1499 * version. 1500 */ 1501 vol->vol_ino = ntfs_iget(sb, FILE_Volume); 1502 if (IS_ERR(vol->vol_ino)) { 1503 if (!IS_ERR(vol->vol_ino)) 1504 iput(vol->vol_ino); 1505 volume_failed: 1506 ntfs_error(sb, "Failed to load $Volume."); 1507 goto iput_lcnbmp_err_out; 1508 } 1509 m = map_mft_record(NTFS_I(vol->vol_ino)); 1510 if (IS_ERR(m)) { 1511 iput_volume_failed: 1512 iput(vol->vol_ino); 1513 goto volume_failed; 1514 } 1515 1516 ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m); 1517 if (!ctx) { 1518 ntfs_error(sb, "Failed to get attribute search context."); 1519 goto get_ctx_vol_failed; 1520 } 1521 1522 if (!ntfs_attr_lookup(AT_VOLUME_NAME, NULL, 0, 0, 0, NULL, 0, ctx) && 1523 !ctx->attr->non_resident && 1524 !(ctx->attr->flags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) && 1525 le32_to_cpu(ctx->attr->data.resident.value_length) > 0) { 1526 err = ntfs_ucstonls(vol, (__le16 *)((u8 *)ctx->attr + 1527 le16_to_cpu(ctx->attr->data.resident.value_offset)), 1528 le32_to_cpu(ctx->attr->data.resident.value_length) / 2, 1529 &vol->volume_label, NTFS_MAX_LABEL_LEN); 1530 if (err < 0) 1531 vol->volume_label = NULL; 1532 } 1533 1534 if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0, 1535 ctx) || ctx->attr->non_resident || ctx->attr->flags) { 1536 ntfs_attr_put_search_ctx(ctx); 1537 get_ctx_vol_failed: 1538 unmap_mft_record(NTFS_I(vol->vol_ino)); 1539 goto iput_volume_failed; 1540 } 1541 vi = (struct volume_information *)((char *)ctx->attr + 1542 le16_to_cpu(ctx->attr->data.resident.value_offset)); 1543 /* Copy the volume flags and version to the struct ntfs_volume structure. */ 1544 vol->vol_flags = vi->flags; 1545 vol->major_ver = vi->major_ver; 1546 vol->minor_ver = vi->minor_ver; 1547 ntfs_attr_put_search_ctx(ctx); 1548 unmap_mft_record(NTFS_I(vol->vol_ino)); 1549 pr_info("volume version %i.%i, dev %s, cluster size %d\n", 1550 vol->major_ver, vol->minor_ver, sb->s_id, vol->cluster_size); 1551 1552 /* Make sure that no unsupported volume flags are set. */ 1553 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) { 1554 static const char *es1a = "Volume is dirty"; 1555 static const char *es1b = "Volume has been modified by chkdsk"; 1556 static const char *es1c = "Volume has unsupported flags set"; 1557 static const char *es2a = ". Run chkdsk and mount in Windows."; 1558 static const char *es2b = ". Mount in Windows."; 1559 const char *es1, *es2; 1560 1561 es2 = es2a; 1562 if (vol->vol_flags & VOLUME_IS_DIRTY) 1563 es1 = es1a; 1564 else if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) { 1565 es1 = es1b; 1566 es2 = es2b; 1567 } else { 1568 es1 = es1c; 1569 ntfs_warning(sb, "Unsupported volume flags 0x%x encountered.", 1570 (unsigned int)le16_to_cpu(vol->vol_flags)); 1571 } 1572 /* If a read-write mount, convert it to a read-only mount. */ 1573 if (!sb_rdonly(sb) && vol->on_errors == ON_ERRORS_REMOUNT_RO) { 1574 sb->s_flags |= SB_RDONLY; 1575 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 1576 } 1577 /* 1578 * Do not set NVolErrors() because ntfs_remount() re-checks the 1579 * flags which we need to do in case any flags have changed. 1580 */ 1581 } 1582 /* 1583 * Get the inode for the logfile, check it and determine if the volume 1584 * was shutdown cleanly. 1585 */ 1586 rp = NULL; 1587 err = load_and_check_logfile(vol, &rp); 1588 if (err) { 1589 /* If a read-write mount, convert it to a read-only mount. */ 1590 if (!sb_rdonly(sb) && vol->on_errors == ON_ERRORS_REMOUNT_RO) { 1591 sb->s_flags |= SB_RDONLY; 1592 ntfs_error(sb, "Failed to load LogFile. Mounting read-only."); 1593 } 1594 NVolSetErrors(vol); 1595 } 1596 1597 kvfree(rp); 1598 /* Get the root directory inode so we can do path lookups. */ 1599 vol->root_ino = ntfs_iget(sb, FILE_root); 1600 if (IS_ERR(vol->root_ino)) { 1601 if (!IS_ERR(vol->root_ino)) 1602 iput(vol->root_ino); 1603 ntfs_error(sb, "Failed to load root directory."); 1604 goto iput_logfile_err_out; 1605 } 1606 /* 1607 * Check if Windows is suspended to disk on the target volume. If it 1608 * is hibernated, we must not write *anything* to the disk so set 1609 * NVolErrors() without setting the dirty volume flag and mount 1610 * read-only. This will prevent read-write remounting and it will also 1611 * prevent all writes. 1612 */ 1613 err = check_windows_hibernation_status(vol); 1614 if (unlikely(err)) { 1615 static const char *es1a = "Failed to determine if Windows is hibernated"; 1616 static const char *es1b = "Windows is hibernated"; 1617 static const char *es2 = ". Run chkdsk."; 1618 const char *es1; 1619 1620 es1 = err < 0 ? es1a : es1b; 1621 /* If a read-write mount, convert it to a read-only mount. */ 1622 if (!sb_rdonly(sb) && vol->on_errors == ON_ERRORS_REMOUNT_RO) { 1623 sb->s_flags |= SB_RDONLY; 1624 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 1625 } 1626 NVolSetErrors(vol); 1627 } 1628 1629 /* If (still) a read-write mount, empty the logfile. */ 1630 if (!sb_rdonly(sb) && 1631 vol->logfile_ino && !ntfs_empty_logfile(vol->logfile_ino) && 1632 vol->on_errors == ON_ERRORS_REMOUNT_RO) { 1633 static const char *es1 = "Failed to empty LogFile"; 1634 static const char *es2 = ". Mount in Windows."; 1635 1636 /* Convert to a read-only mount. */ 1637 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 1638 sb->s_flags |= SB_RDONLY; 1639 NVolSetErrors(vol); 1640 } 1641 /* If on NTFS versions before 3.0, we are done. */ 1642 if (unlikely(vol->major_ver < 3)) 1643 return true; 1644 /* NTFS 3.0+ specific initialization. */ 1645 /* Get the security descriptors inode. */ 1646 vol->secure_ino = ntfs_iget(sb, FILE_Secure); 1647 if (IS_ERR(vol->secure_ino)) { 1648 if (!IS_ERR(vol->secure_ino)) 1649 iput(vol->secure_ino); 1650 ntfs_error(sb, "Failed to load $Secure."); 1651 goto iput_root_err_out; 1652 } 1653 /* Get the extended system files' directory inode. */ 1654 vol->extend_ino = ntfs_iget(sb, FILE_Extend); 1655 if (IS_ERR(vol->extend_ino) || 1656 !S_ISDIR(vol->extend_ino->i_mode)) { 1657 if (!IS_ERR(vol->extend_ino)) 1658 iput(vol->extend_ino); 1659 ntfs_error(sb, "Failed to load $Extend."); 1660 goto iput_sec_err_out; 1661 } 1662 /* Find the quota file, load it if present, and set it up. */ 1663 if (!load_and_init_quota(vol) && 1664 vol->on_errors == ON_ERRORS_REMOUNT_RO) { 1665 static const char *es1 = "Failed to load $Quota"; 1666 static const char *es2 = ". Run chkdsk."; 1667 1668 sb->s_flags |= SB_RDONLY; 1669 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 1670 /* This will prevent a read-write remount. */ 1671 NVolSetErrors(vol); 1672 } 1673 1674 return true; 1675 1676 iput_sec_err_out: 1677 iput(vol->secure_ino); 1678 iput_root_err_out: 1679 iput(vol->root_ino); 1680 iput_logfile_err_out: 1681 if (vol->logfile_ino) 1682 iput(vol->logfile_ino); 1683 iput(vol->vol_ino); 1684 iput_lcnbmp_err_out: 1685 iput(vol->lcnbmp_ino); 1686 iput_attrdef_err_out: 1687 vol->attrdef_size = 0; 1688 if (vol->attrdef) { 1689 kvfree(vol->attrdef); 1690 vol->attrdef = NULL; 1691 } 1692 iput_upcase_err_out: 1693 vol->upcase_len = 0; 1694 mutex_lock(&ntfs_lock); 1695 if (vol->upcase && vol->upcase == default_upcase) { 1696 ntfs_nr_upcase_users--; 1697 vol->upcase = NULL; 1698 } 1699 mutex_unlock(&ntfs_lock); 1700 if (vol->upcase) { 1701 kvfree(vol->upcase); 1702 vol->upcase = NULL; 1703 } 1704 iput_mftbmp_err_out: 1705 iput(vol->mftbmp_ino); 1706 iput_mirr_err_out: 1707 iput(vol->mftmirr_ino); 1708 return false; 1709 } 1710 1711 static void ntfs_volume_free(struct ntfs_volume *vol) 1712 { 1713 /* Throw away the table of attribute definitions. */ 1714 vol->attrdef_size = 0; 1715 if (vol->attrdef) { 1716 kvfree(vol->attrdef); 1717 vol->attrdef = NULL; 1718 } 1719 vol->upcase_len = 0; 1720 /* 1721 * Destroy the global default upcase table if necessary. Also decrease 1722 * the number of upcase users if we are a user. 1723 */ 1724 mutex_lock(&ntfs_lock); 1725 if (vol->upcase && vol->upcase == default_upcase) { 1726 ntfs_nr_upcase_users--; 1727 vol->upcase = NULL; 1728 } 1729 1730 if (!ntfs_nr_upcase_users && default_upcase) { 1731 kvfree(default_upcase); 1732 default_upcase = NULL; 1733 } 1734 1735 free_compression_buffers(); 1736 1737 mutex_unlock(&ntfs_lock); 1738 if (vol->upcase) { 1739 kvfree(vol->upcase); 1740 vol->upcase = NULL; 1741 } 1742 1743 unload_nls(vol->nls_map); 1744 1745 if (vol->lcn_empty_bits_per_page) 1746 kvfree(vol->lcn_empty_bits_per_page); 1747 kfree(vol->volume_label); 1748 kfree(vol); 1749 } 1750 1751 /* 1752 * ntfs_put_super - called by the vfs to unmount a volume 1753 * @sb: vfs superblock of volume to unmount 1754 */ 1755 static void ntfs_put_super(struct super_block *sb) 1756 { 1757 struct ntfs_volume *vol = NTFS_SB(sb); 1758 1759 pr_info("Entering %s, dev %s\n", __func__, sb->s_id); 1760 1761 cancel_work_sync(&vol->precalc_work); 1762 1763 /* 1764 * Commit all inodes while they are still open in case some of them 1765 * cause others to be dirtied. 1766 */ 1767 ntfs_commit_inode(vol->vol_ino); 1768 1769 /* NTFS 3.0+ specific. */ 1770 if (vol->major_ver >= 3) { 1771 if (vol->quota_q_ino) 1772 ntfs_commit_inode(vol->quota_q_ino); 1773 if (vol->quota_ino) 1774 ntfs_commit_inode(vol->quota_ino); 1775 if (vol->extend_ino) 1776 ntfs_commit_inode(vol->extend_ino); 1777 if (vol->secure_ino) 1778 ntfs_commit_inode(vol->secure_ino); 1779 } 1780 1781 ntfs_commit_inode(vol->root_ino); 1782 1783 ntfs_commit_inode(vol->lcnbmp_ino); 1784 1785 /* 1786 * the GFP_NOFS scope is not needed because ntfs_commit_inode 1787 * does nothing 1788 */ 1789 ntfs_commit_inode(vol->mftbmp_ino); 1790 1791 if (vol->logfile_ino) 1792 ntfs_commit_inode(vol->logfile_ino); 1793 1794 if (vol->mftmirr_ino) 1795 ntfs_commit_inode(vol->mftmirr_ino); 1796 ntfs_commit_inode(vol->mft_ino); 1797 1798 /* 1799 * If a read-write mount and no volume errors have occurred, mark the 1800 * volume clean. Also, re-commit all affected inodes. 1801 */ 1802 if (!sb_rdonly(sb)) { 1803 if (!NVolErrors(vol)) { 1804 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY)) 1805 ntfs_warning(sb, 1806 "Failed to clear dirty bit in volume information flags. Run chkdsk."); 1807 ntfs_commit_inode(vol->vol_ino); 1808 ntfs_commit_inode(vol->root_ino); 1809 if (vol->mftmirr_ino) 1810 ntfs_commit_inode(vol->mftmirr_ino); 1811 ntfs_commit_inode(vol->mft_ino); 1812 } else { 1813 ntfs_warning(sb, 1814 "Volume has errors. Leaving volume marked dirty. Run chkdsk."); 1815 } 1816 } 1817 1818 iput(vol->vol_ino); 1819 vol->vol_ino = NULL; 1820 1821 /* NTFS 3.0+ specific clean up. */ 1822 if (vol->major_ver >= 3) { 1823 if (vol->quota_q_ino) { 1824 iput(vol->quota_q_ino); 1825 vol->quota_q_ino = NULL; 1826 } 1827 if (vol->quota_ino) { 1828 iput(vol->quota_ino); 1829 vol->quota_ino = NULL; 1830 } 1831 if (vol->extend_ino) { 1832 iput(vol->extend_ino); 1833 vol->extend_ino = NULL; 1834 } 1835 if (vol->secure_ino) { 1836 iput(vol->secure_ino); 1837 vol->secure_ino = NULL; 1838 } 1839 } 1840 1841 iput(vol->root_ino); 1842 vol->root_ino = NULL; 1843 1844 iput(vol->lcnbmp_ino); 1845 vol->lcnbmp_ino = NULL; 1846 1847 iput(vol->mftbmp_ino); 1848 vol->mftbmp_ino = NULL; 1849 1850 if (vol->logfile_ino) { 1851 iput(vol->logfile_ino); 1852 vol->logfile_ino = NULL; 1853 } 1854 if (vol->mftmirr_ino) { 1855 /* Re-commit the mft mirror and mft just in case. */ 1856 ntfs_commit_inode(vol->mftmirr_ino); 1857 ntfs_commit_inode(vol->mft_ino); 1858 iput(vol->mftmirr_ino); 1859 vol->mftmirr_ino = NULL; 1860 } 1861 /* 1862 * We should have no dirty inodes left, due to 1863 * mft.c::ntfs_mft_writepage() cleaning all the dirty pages as 1864 * the underlying mft records are written out and cleaned. 1865 */ 1866 ntfs_commit_inode(vol->mft_ino); 1867 write_inode_now(vol->mft_ino, 1); 1868 1869 iput(vol->mft_ino); 1870 vol->mft_ino = NULL; 1871 blkdev_issue_flush(sb->s_bdev); 1872 1873 ntfs_volume_free(vol); 1874 } 1875 1876 int ntfs_force_shutdown(struct super_block *sb, u32 flags) 1877 { 1878 struct ntfs_volume *vol = NTFS_SB(sb); 1879 int ret; 1880 1881 if (NVolShutdown(vol)) 1882 return 0; 1883 1884 switch (flags) { 1885 case FS_SHUTDOWN_FLAGS_DEFAULT: 1886 case FS_SHUTDOWN_FLAGS_LOGFLUSH: 1887 ret = bdev_freeze(sb->s_bdev); 1888 if (ret) 1889 return ret; 1890 bdev_thaw(sb->s_bdev); 1891 NVolSetShutdown(vol); 1892 break; 1893 case FS_SHUTDOWN_FLAGS_NOLOGFLUSH: 1894 NVolSetShutdown(vol); 1895 break; 1896 default: 1897 return -EINVAL; 1898 } 1899 1900 return 0; 1901 } 1902 1903 static void ntfs_shutdown(struct super_block *sb) 1904 { 1905 ntfs_force_shutdown(sb, FS_SHUTDOWN_FLAGS_NOLOGFLUSH); 1906 1907 } 1908 1909 static int ntfs_sync_fs(struct super_block *sb, int wait) 1910 { 1911 struct ntfs_volume *vol = NTFS_SB(sb); 1912 int err = 0; 1913 1914 if (NVolShutdown(vol)) 1915 return -EIO; 1916 1917 if (!wait) 1918 return 0; 1919 1920 /* If there are some dirty buffers in the bdev inode */ 1921 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY)) { 1922 ntfs_warning(sb, "Failed to clear dirty bit in volume information flags. Run chkdsk."); 1923 err = -EIO; 1924 } 1925 sync_inodes_sb(sb); 1926 sync_blockdev(sb->s_bdev); 1927 blkdev_issue_flush(sb->s_bdev); 1928 return err; 1929 } 1930 1931 /* 1932 * get_nr_free_clusters - return the number of free clusters on a volume 1933 * @vol: ntfs volume for which to obtain free cluster count 1934 * 1935 * Calculate the number of free clusters on the mounted NTFS volume @vol. We 1936 * actually calculate the number of clusters in use instead because this 1937 * allows us to not care about partial pages as these will be just zero filled 1938 * and hence not be counted as allocated clusters. 1939 * 1940 * The only particularity is that clusters beyond the end of the logical ntfs 1941 * volume will be marked as allocated to prevent errors which means we have to 1942 * discount those at the end. This is important as the cluster bitmap always 1943 * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside 1944 * the logical volume and marked in use when they are not as they do not exist. 1945 * 1946 * If any pages cannot be read we assume all clusters in the erroring pages are 1947 * in use. This means we return an underestimate on errors which is better than 1948 * an overestimate. 1949 */ 1950 s64 get_nr_free_clusters(struct ntfs_volume *vol) 1951 { 1952 s64 nr_free = vol->nr_clusters; 1953 u32 nr_used; 1954 struct address_space *mapping = vol->lcnbmp_ino->i_mapping; 1955 struct folio *folio; 1956 pgoff_t index, max_index; 1957 struct file_ra_state *ra; 1958 1959 ntfs_debug("Entering."); 1960 /* Serialize accesses to the cluster bitmap. */ 1961 1962 if (NVolFreeClusterKnown(vol)) 1963 return atomic64_read(&vol->free_clusters); 1964 1965 ra = kzalloc(sizeof(*ra), GFP_NOFS); 1966 if (!ra) 1967 return 0; 1968 1969 file_ra_state_init(ra, mapping); 1970 1971 /* 1972 * Convert the number of bits into bytes rounded up, then convert into 1973 * multiples of PAGE_SIZE, rounding up so that if we have one 1974 * full and one partial page max_index = 2. 1975 */ 1976 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_SIZE - 1) >> 1977 PAGE_SHIFT; 1978 /* Use multiples of 4 bytes, thus max_size is PAGE_SIZE / 4. */ 1979 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.", 1980 max_index, PAGE_SIZE / 4); 1981 for (index = 0; index < max_index; index++) { 1982 unsigned long *kaddr; 1983 1984 /* 1985 * Get folio from page cache, getting it from backing store 1986 * if necessary, and increment the use count. 1987 */ 1988 folio = ntfs_get_locked_folio(mapping, index, max_index, ra); 1989 1990 /* Ignore pages which errored synchronously. */ 1991 if (IS_ERR(folio)) { 1992 ntfs_debug("Skipping page (index 0x%lx).", index); 1993 nr_free -= PAGE_SIZE * 8; 1994 vol->lcn_empty_bits_per_page[index] = 0; 1995 continue; 1996 } 1997 1998 kaddr = kmap_local_folio(folio, 0); 1999 /* 2000 * Subtract the number of set bits. If this 2001 * is the last page and it is partial we don't really care as 2002 * it just means we do a little extra work but it won't affect 2003 * the result as all out of range bytes are set to zero by 2004 * ntfs_readpage(). 2005 */ 2006 nr_used = bitmap_weight(kaddr, PAGE_SIZE * BITS_PER_BYTE); 2007 nr_free -= nr_used; 2008 vol->lcn_empty_bits_per_page[index] = PAGE_SIZE * BITS_PER_BYTE - nr_used; 2009 kunmap_local(kaddr); 2010 folio_unlock(folio); 2011 folio_put(folio); 2012 } 2013 ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1); 2014 /* 2015 * Fixup for eventual bits outside logical ntfs volume (see function 2016 * description above). 2017 */ 2018 if (vol->nr_clusters & 63) 2019 nr_free += 64 - (vol->nr_clusters & 63); 2020 2021 /* If errors occurred we may well have gone below zero, fix this. */ 2022 if (nr_free < 0) 2023 nr_free = 0; 2024 else 2025 atomic64_set(&vol->free_clusters, nr_free); 2026 2027 kfree(ra); 2028 NVolSetFreeClusterKnown(vol); 2029 wake_up_all(&vol->free_waitq); 2030 ntfs_debug("Exiting."); 2031 return nr_free; 2032 } 2033 2034 /* 2035 * @nr_clusters is the number of clusters requested for allocation. 2036 * 2037 * Return the number of clusters available for allocation within 2038 * the range of @nr_clusters, which is counts that considered 2039 * for delayed allocation. 2040 */ 2041 s64 ntfs_available_clusters_count(struct ntfs_volume *vol, s64 nr_clusters) 2042 { 2043 s64 free_clusters; 2044 2045 /* wait event */ 2046 if (!NVolFreeClusterKnown(vol)) 2047 wait_event(vol->free_waitq, NVolFreeClusterKnown(vol)); 2048 2049 free_clusters = atomic64_read(&vol->free_clusters) - 2050 atomic64_read(&vol->dirty_clusters); 2051 if (free_clusters <= 0) 2052 return -ENOSPC; 2053 else if (free_clusters < nr_clusters) 2054 nr_clusters = free_clusters; 2055 2056 return nr_clusters; 2057 } 2058 2059 /* 2060 * __get_nr_free_mft_records - return the number of free inodes on a volume 2061 * @vol: ntfs volume for which to obtain free inode count 2062 * @nr_free: number of mft records in filesystem 2063 * @max_index: maximum number of pages containing set bits 2064 * 2065 * Calculate the number of free mft records (inodes) on the mounted NTFS 2066 * volume @vol. We actually calculate the number of mft records in use instead 2067 * because this allows us to not care about partial pages as these will be just 2068 * zero filled and hence not be counted as allocated mft record. 2069 * 2070 * If any pages cannot be read we assume all mft records in the erroring pages 2071 * are in use. This means we return an underestimate on errors which is better 2072 * than an overestimate. 2073 * 2074 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing. 2075 */ 2076 static unsigned long __get_nr_free_mft_records(struct ntfs_volume *vol, 2077 s64 nr_free, const pgoff_t max_index) 2078 { 2079 struct address_space *mapping = vol->mftbmp_ino->i_mapping; 2080 struct folio *folio; 2081 pgoff_t index; 2082 struct file_ra_state *ra; 2083 2084 ntfs_debug("Entering."); 2085 2086 ra = kzalloc(sizeof(*ra), GFP_NOFS); 2087 if (!ra) 2088 return 0; 2089 2090 file_ra_state_init(ra, mapping); 2091 2092 /* Use multiples of 4 bytes, thus max_size is PAGE_SIZE / 4. */ 2093 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = 0x%lx.", 2094 max_index, PAGE_SIZE / 4); 2095 for (index = 0; index < max_index; index++) { 2096 unsigned long *kaddr; 2097 2098 /* 2099 * Get folio from page cache, getting it from backing store 2100 * if necessary, and increment the use count. 2101 */ 2102 folio = ntfs_get_locked_folio(mapping, index, max_index, ra); 2103 2104 /* Ignore pages which errored synchronously. */ 2105 if (IS_ERR(folio)) { 2106 ntfs_debug("read_mapping_page() error. Skipping page (index 0x%lx).", 2107 index); 2108 nr_free -= PAGE_SIZE * 8; 2109 continue; 2110 } 2111 2112 kaddr = kmap_local_folio(folio, 0); 2113 /* 2114 * Subtract the number of set bits. If this 2115 * is the last page and it is partial we don't really care as 2116 * it just means we do a little extra work but it won't affect 2117 * the result as all out of range bytes are set to zero by 2118 * ntfs_readpage(). 2119 */ 2120 nr_free -= bitmap_weight(kaddr, 2121 PAGE_SIZE * BITS_PER_BYTE); 2122 kunmap_local(kaddr); 2123 folio_unlock(folio); 2124 folio_put(folio); 2125 } 2126 ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.", 2127 index - 1); 2128 /* If errors occurred we may well have gone below zero, fix this. */ 2129 if (nr_free < 0) 2130 nr_free = 0; 2131 else 2132 atomic64_set(&vol->free_mft_records, nr_free); 2133 2134 kfree(ra); 2135 ntfs_debug("Exiting."); 2136 return nr_free; 2137 } 2138 2139 /* 2140 * ntfs_statfs - return information about mounted NTFS volume 2141 * @dentry: dentry from mounted volume 2142 * @sfs: statfs structure in which to return the information 2143 * 2144 * Return information about the mounted NTFS volume @dentry in the statfs structure 2145 * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is 2146 * called). We interpret the values to be correct of the moment in time at 2147 * which we are called. Most values are variable otherwise and this isn't just 2148 * the free values but the totals as well. For example we can increase the 2149 * total number of file nodes if we run out and we can keep doing this until 2150 * there is no more space on the volume left at all. 2151 * 2152 * Called from vfs_statfs which is used to handle the statfs, fstatfs, and 2153 * ustat system calls. 2154 * 2155 * Return 0 on success or -errno on error. 2156 */ 2157 static int ntfs_statfs(struct dentry *dentry, struct kstatfs *sfs) 2158 { 2159 struct super_block *sb = dentry->d_sb; 2160 s64 size; 2161 struct ntfs_volume *vol = NTFS_SB(sb); 2162 struct ntfs_inode *mft_ni = NTFS_I(vol->mft_ino); 2163 unsigned long flags; 2164 2165 ntfs_debug("Entering."); 2166 /* Type of filesystem. */ 2167 sfs->f_type = NTFS_SB_MAGIC; 2168 /* Optimal transfer block size. */ 2169 sfs->f_bsize = vol->cluster_size; 2170 /* Fundamental file system block size, used as the unit. */ 2171 sfs->f_frsize = vol->cluster_size; 2172 2173 /* 2174 * Total data blocks in filesystem in units of f_bsize and since 2175 * inodes are also stored in data blocs ($MFT is a file) this is just 2176 * the total clusters. 2177 */ 2178 sfs->f_blocks = vol->nr_clusters; 2179 2180 /* wait event */ 2181 if (!NVolFreeClusterKnown(vol)) 2182 wait_event(vol->free_waitq, NVolFreeClusterKnown(vol)); 2183 2184 /* Free data blocks in filesystem in units of f_bsize. */ 2185 size = atomic64_read(&vol->free_clusters) - 2186 atomic64_read(&vol->dirty_clusters); 2187 if (size < 0LL) 2188 size = 0LL; 2189 2190 /* Free blocks avail to non-superuser, same as above on NTFS. */ 2191 sfs->f_bavail = sfs->f_bfree = size; 2192 2193 /* Number of inodes in filesystem (at this point in time). */ 2194 read_lock_irqsave(&mft_ni->size_lock, flags); 2195 sfs->f_files = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits; 2196 read_unlock_irqrestore(&mft_ni->size_lock, flags); 2197 2198 /* Free inodes in fs (based on current total count). */ 2199 sfs->f_ffree = atomic64_read(&vol->free_mft_records); 2200 2201 /* 2202 * File system id. This is extremely *nix flavour dependent and even 2203 * within Linux itself all fs do their own thing. I interpret this to 2204 * mean a unique id associated with the mounted fs and not the id 2205 * associated with the filesystem driver, the latter is already given 2206 * by the filesystem type in sfs->f_type. Thus we use the 64-bit 2207 * volume serial number splitting it into two 32-bit parts. We enter 2208 * the least significant 32-bits in f_fsid[0] and the most significant 2209 * 32-bits in f_fsid[1]. 2210 */ 2211 sfs->f_fsid = u64_to_fsid(vol->serial_no); 2212 /* Maximum length of filenames. */ 2213 sfs->f_namelen = NTFS_MAX_NAME_LEN; 2214 2215 return 0; 2216 } 2217 2218 static int ntfs_write_inode(struct inode *vi, struct writeback_control *wbc) 2219 { 2220 return __ntfs_write_inode(vi, wbc->sync_mode == WB_SYNC_ALL); 2221 } 2222 2223 /* 2224 * The complete super operations. 2225 */ 2226 static const struct super_operations ntfs_sops = { 2227 .alloc_inode = ntfs_alloc_big_inode, /* VFS: Allocate new inode. */ 2228 .free_inode = ntfs_free_big_inode, /* VFS: Deallocate inode. */ 2229 .drop_inode = ntfs_drop_big_inode, 2230 .write_inode = ntfs_write_inode, /* VFS: Write dirty inode to disk. */ 2231 .put_super = ntfs_put_super, /* Syscall: umount. */ 2232 .shutdown = ntfs_shutdown, 2233 .sync_fs = ntfs_sync_fs, /* Syscall: sync. */ 2234 .statfs = ntfs_statfs, /* Syscall: statfs */ 2235 .evict_inode = ntfs_evict_big_inode, 2236 .show_options = ntfs_show_options, /* Show mount options in proc. */ 2237 }; 2238 2239 static void precalc_free_clusters(struct work_struct *work) 2240 { 2241 struct ntfs_volume *vol = container_of(work, struct ntfs_volume, precalc_work); 2242 s64 nr_free; 2243 2244 nr_free = get_nr_free_clusters(vol); 2245 2246 ntfs_debug("pre-calculate free clusters(%lld) using workqueue", 2247 nr_free); 2248 } 2249 2250 static struct lock_class_key ntfs_mft_inval_lock_key; 2251 2252 /* 2253 * ntfs_fill_super - mount an ntfs filesystem 2254 * @sb: super block of the device to mount 2255 * @fc: filesystem context containing mount options 2256 * 2257 * ntfs_fill_super() is called by the VFS to mount the device described by @sb 2258 * with the mount otions in @data with the NTFS filesystem. 2259 * 2260 * If @silent is true, remain silent even if errors are detected. This is used 2261 * during bootup, when the kernel tries to mount the root filesystem with all 2262 * registered filesystems one after the other until one succeeds. This implies 2263 * that all filesystems except the correct one will quite correctly and 2264 * expectedly return an error, but nobody wants to see error messages when in 2265 * fact this is what is supposed to happen. 2266 */ 2267 static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc) 2268 { 2269 char *boot; 2270 struct inode *tmp_ino; 2271 int blocksize, result; 2272 pgoff_t lcn_bit_pages; 2273 struct ntfs_volume *vol = NTFS_SB(sb); 2274 int silent = fc->sb_flags & SB_SILENT; 2275 2276 vol->sb = sb; 2277 2278 /* 2279 * We do a pretty difficult piece of bootstrap by reading the 2280 * MFT (and other metadata) from disk into memory. We'll only 2281 * release this metadata during umount, so the locking patterns 2282 * observed during bootstrap do not count. So turn off the 2283 * observation of locking patterns (strictly for this context 2284 * only) while mounting NTFS. [The validator is still active 2285 * otherwise, even for this context: it will for example record 2286 * lock class registrations.] 2287 */ 2288 lockdep_off(); 2289 ntfs_debug("Entering."); 2290 2291 if (vol->nls_map && !strcmp(vol->nls_map->charset, "utf8")) 2292 vol->nls_utf8 = true; 2293 if (NVolDisableSparse(vol)) 2294 vol->preallocated_size = 0; 2295 2296 if (NVolDiscard(vol) && !bdev_max_discard_sectors(sb->s_bdev)) { 2297 ntfs_warning( 2298 sb, 2299 "Discard requested but device does not support discard. Discard disabled."); 2300 NVolClearDiscard(vol); 2301 } 2302 2303 /* We support sector sizes up to the PAGE_SIZE. */ 2304 if (bdev_logical_block_size(sb->s_bdev) > PAGE_SIZE) { 2305 if (!silent) 2306 ntfs_error(sb, 2307 "Device has unsupported sector size (%i). The maximum supported sector size on this architecture is %lu bytes.", 2308 bdev_logical_block_size(sb->s_bdev), 2309 PAGE_SIZE); 2310 goto err_out_now; 2311 } 2312 2313 /* 2314 * Setup the device access block size to NTFS_BLOCK_SIZE or the hard 2315 * sector size, whichever is bigger. 2316 */ 2317 blocksize = sb_min_blocksize(sb, NTFS_BLOCK_SIZE); 2318 if (blocksize < NTFS_BLOCK_SIZE) { 2319 if (!silent) 2320 ntfs_error(sb, "Unable to set device block size."); 2321 goto err_out_now; 2322 } 2323 2324 ntfs_debug("Set device block size to %i bytes (block size bits %i).", 2325 blocksize, sb->s_blocksize_bits); 2326 /* Determine the size of the device in units of block_size bytes. */ 2327 if (!bdev_nr_bytes(sb->s_bdev)) { 2328 if (!silent) 2329 ntfs_error(sb, "Unable to determine device size."); 2330 goto err_out_now; 2331 } 2332 vol->nr_blocks = bdev_nr_bytes(sb->s_bdev) >> 2333 sb->s_blocksize_bits; 2334 /* Read the boot sector and return unlocked buffer head to it. */ 2335 boot = read_ntfs_boot_sector(sb, silent); 2336 if (!boot) { 2337 if (!silent) 2338 ntfs_error(sb, "Not an NTFS volume."); 2339 goto err_out_now; 2340 } 2341 /* 2342 * Extract the data from the boot sector and setup the ntfs volume 2343 * using it. 2344 */ 2345 result = parse_ntfs_boot_sector(vol, (struct ntfs_boot_sector *)boot); 2346 kfree(boot); 2347 if (!result) { 2348 if (!silent) 2349 ntfs_error(sb, "Unsupported NTFS filesystem."); 2350 goto err_out_now; 2351 } 2352 2353 if (vol->sector_size > blocksize) { 2354 blocksize = sb_set_blocksize(sb, vol->sector_size); 2355 if (blocksize != vol->sector_size) { 2356 if (!silent) 2357 ntfs_error(sb, 2358 "Unable to set device block size to sector size (%i).", 2359 vol->sector_size); 2360 goto err_out_now; 2361 } 2362 vol->nr_blocks = bdev_nr_bytes(sb->s_bdev) >> 2363 sb->s_blocksize_bits; 2364 ntfs_debug("Changed device block size to %i bytes (block size bits %i) to match volume sector size.", 2365 blocksize, sb->s_blocksize_bits); 2366 } 2367 /* Initialize the cluster and mft allocators. */ 2368 ntfs_setup_allocators(vol); 2369 /* Setup remaining fields in the super block. */ 2370 sb->s_magic = NTFS_SB_MAGIC; 2371 /* 2372 * Ntfs allows 63 bits for the file size, i.e. correct would be: 2373 * sb->s_maxbytes = ~0ULL >> 1; 2374 * But the kernel uses a long as the page cache page index which on 2375 * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel 2376 * defined to the maximum the page cache page index can cope with 2377 * without overflowing the index or to 2^63 - 1, whichever is smaller. 2378 */ 2379 sb->s_maxbytes = MAX_LFS_FILESIZE; 2380 /* Ntfs measures time in 100ns intervals. */ 2381 sb->s_time_gran = 100; 2382 2383 sb->s_xattr = ntfs_xattr_handlers; 2384 /* 2385 * Now load the metadata required for the page cache and our address 2386 * space operations to function. We do this by setting up a specialised 2387 * read_inode method and then just calling the normal iget() to obtain 2388 * the inode for $MFT which is sufficient to allow our normal inode 2389 * operations and associated address space operations to function. 2390 */ 2391 sb->s_op = &ntfs_sops; 2392 tmp_ino = new_inode(sb); 2393 if (!tmp_ino) { 2394 if (!silent) 2395 ntfs_error(sb, "Failed to load essential metadata."); 2396 goto err_out_now; 2397 } 2398 2399 tmp_ino->i_ino = FILE_MFT; 2400 insert_inode_hash(tmp_ino); 2401 if (ntfs_read_inode_mount(tmp_ino) < 0) { 2402 if (!silent) 2403 ntfs_error(sb, "Failed to load essential metadata."); 2404 goto iput_tmp_ino_err_out_now; 2405 } 2406 lockdep_set_class(&tmp_ino->i_mapping->invalidate_lock, 2407 &ntfs_mft_inval_lock_key); 2408 2409 mutex_lock(&ntfs_lock); 2410 2411 /* 2412 * Generate the global default upcase table if necessary. Also 2413 * temporarily increment the number of upcase users to avoid race 2414 * conditions with concurrent (u)mounts. 2415 */ 2416 if (!default_upcase) 2417 default_upcase = generate_default_upcase(); 2418 ntfs_nr_upcase_users++; 2419 mutex_unlock(&ntfs_lock); 2420 2421 lcn_bit_pages = (((vol->nr_clusters + 7) >> 3) + PAGE_SIZE - 1) >> PAGE_SHIFT; 2422 vol->lcn_empty_bits_per_page = kvmalloc_array(lcn_bit_pages, sizeof(unsigned int), 2423 GFP_KERNEL); 2424 if (!vol->lcn_empty_bits_per_page) { 2425 ntfs_error(sb, 2426 "Unable to allocate pages for storing LCN empty bit counts\n"); 2427 goto unl_upcase_iput_tmp_ino_err_out_now; 2428 } 2429 2430 /* 2431 * From now on, ignore @silent parameter. If we fail below this line, 2432 * it will be due to a corrupt fs or a system error, so we report it. 2433 */ 2434 /* 2435 * Open the system files with normal access functions and complete 2436 * setting up the ntfs super block. 2437 */ 2438 if (!load_system_files(vol)) { 2439 ntfs_error(sb, "Failed to load system files."); 2440 goto unl_upcase_iput_tmp_ino_err_out_now; 2441 } 2442 2443 /* We grab a reference, simulating an ntfs_iget(). */ 2444 ihold(vol->root_ino); 2445 sb->s_root = d_make_root(vol->root_ino); 2446 if (sb->s_root) { 2447 s64 nr_records; 2448 2449 ntfs_debug("Exiting, status successful."); 2450 2451 /* Release the default upcase if it has no users. */ 2452 mutex_lock(&ntfs_lock); 2453 if (!--ntfs_nr_upcase_users && default_upcase) { 2454 kvfree(default_upcase); 2455 default_upcase = NULL; 2456 } 2457 mutex_unlock(&ntfs_lock); 2458 sb->s_export_op = &ntfs_export_ops; 2459 lockdep_on(); 2460 2461 nr_records = __get_nr_free_mft_records(vol, 2462 i_size_read(vol->mft_ino) >> vol->mft_record_size_bits, 2463 ((((NTFS_I(vol->mft_ino)->initialized_size >> 2464 vol->mft_record_size_bits) + 2465 7) >> 3) + PAGE_SIZE - 1) >> PAGE_SHIFT); 2466 ntfs_debug("Free mft records(%lld)", nr_records); 2467 2468 init_waitqueue_head(&vol->free_waitq); 2469 INIT_WORK(&vol->precalc_work, precalc_free_clusters); 2470 queue_work(ntfs_wq, &vol->precalc_work); 2471 return 0; 2472 } 2473 ntfs_error(sb, "Failed to allocate root directory."); 2474 /* Clean up after the successful load_system_files() call from above. */ 2475 iput(vol->vol_ino); 2476 vol->vol_ino = NULL; 2477 /* NTFS 3.0+ specific clean up. */ 2478 if (vol->major_ver >= 3) { 2479 if (vol->quota_q_ino) { 2480 iput(vol->quota_q_ino); 2481 vol->quota_q_ino = NULL; 2482 } 2483 if (vol->quota_ino) { 2484 iput(vol->quota_ino); 2485 vol->quota_ino = NULL; 2486 } 2487 if (vol->extend_ino) { 2488 iput(vol->extend_ino); 2489 vol->extend_ino = NULL; 2490 } 2491 if (vol->secure_ino) { 2492 iput(vol->secure_ino); 2493 vol->secure_ino = NULL; 2494 } 2495 } 2496 iput(vol->root_ino); 2497 vol->root_ino = NULL; 2498 iput(vol->lcnbmp_ino); 2499 vol->lcnbmp_ino = NULL; 2500 iput(vol->mftbmp_ino); 2501 vol->mftbmp_ino = NULL; 2502 if (vol->logfile_ino) { 2503 iput(vol->logfile_ino); 2504 vol->logfile_ino = NULL; 2505 } 2506 if (vol->mftmirr_ino) { 2507 iput(vol->mftmirr_ino); 2508 vol->mftmirr_ino = NULL; 2509 } 2510 /* Throw away the table of attribute definitions. */ 2511 vol->attrdef_size = 0; 2512 if (vol->attrdef) { 2513 kvfree(vol->attrdef); 2514 vol->attrdef = NULL; 2515 } 2516 vol->upcase_len = 0; 2517 mutex_lock(&ntfs_lock); 2518 if (vol->upcase && vol->upcase == default_upcase) { 2519 ntfs_nr_upcase_users--; 2520 vol->upcase = NULL; 2521 } 2522 mutex_unlock(&ntfs_lock); 2523 if (vol->upcase) { 2524 kvfree(vol->upcase); 2525 vol->upcase = NULL; 2526 } 2527 if (vol->nls_map) { 2528 unload_nls(vol->nls_map); 2529 vol->nls_map = NULL; 2530 } 2531 /* Error exit code path. */ 2532 unl_upcase_iput_tmp_ino_err_out_now: 2533 if (vol->lcn_empty_bits_per_page) 2534 kvfree(vol->lcn_empty_bits_per_page); 2535 /* 2536 * Decrease the number of upcase users and destroy the global default 2537 * upcase table if necessary. 2538 */ 2539 mutex_lock(&ntfs_lock); 2540 if (!--ntfs_nr_upcase_users && default_upcase) { 2541 kvfree(default_upcase); 2542 default_upcase = NULL; 2543 } 2544 2545 mutex_unlock(&ntfs_lock); 2546 iput_tmp_ino_err_out_now: 2547 iput(tmp_ino); 2548 if (vol->mft_ino && vol->mft_ino != tmp_ino) 2549 iput(vol->mft_ino); 2550 vol->mft_ino = NULL; 2551 /* Errors at this stage are irrelevant. */ 2552 err_out_now: 2553 sb->s_fs_info = NULL; 2554 kfree(vol); 2555 ntfs_debug("Failed, returning -EINVAL."); 2556 lockdep_on(); 2557 return -EINVAL; 2558 } 2559 2560 /* 2561 * This is a slab cache to optimize allocations and deallocations of Unicode 2562 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN 2563 * (255) Unicode characters + a terminating NULL Unicode character. 2564 */ 2565 struct kmem_cache *ntfs_name_cache; 2566 2567 /* Slab caches for efficient allocation/deallocation of inodes. */ 2568 struct kmem_cache *ntfs_inode_cache; 2569 struct kmem_cache *ntfs_big_inode_cache; 2570 2571 /* Init once constructor for the inode slab cache. */ 2572 static void ntfs_big_inode_init_once(void *foo) 2573 { 2574 struct ntfs_inode *ni = foo; 2575 2576 inode_init_once(VFS_I(ni)); 2577 } 2578 2579 /* 2580 * Slab caches to optimize allocations and deallocations of attribute search 2581 * contexts and index contexts, respectively. 2582 */ 2583 struct kmem_cache *ntfs_attr_ctx_cache; 2584 struct kmem_cache *ntfs_index_ctx_cache; 2585 2586 /* Driver wide mutex. */ 2587 DEFINE_MUTEX(ntfs_lock); 2588 2589 static int ntfs_get_tree(struct fs_context *fc) 2590 { 2591 return get_tree_bdev(fc, ntfs_fill_super); 2592 } 2593 2594 static void ntfs_free_fs_context(struct fs_context *fc) 2595 { 2596 struct ntfs_volume *vol = fc->s_fs_info; 2597 2598 if (vol) 2599 ntfs_volume_free(vol); 2600 } 2601 2602 static const struct fs_context_operations ntfs_context_ops = { 2603 .parse_param = ntfs_parse_param, 2604 .get_tree = ntfs_get_tree, 2605 .free = ntfs_free_fs_context, 2606 .reconfigure = ntfs_reconfigure, 2607 }; 2608 2609 static int ntfs_init_fs_context(struct fs_context *fc) 2610 { 2611 struct ntfs_volume *vol; 2612 2613 /* Allocate a new struct ntfs_volume and place it in sb->s_fs_info. */ 2614 vol = kmalloc(sizeof(struct ntfs_volume), GFP_NOFS); 2615 if (!vol) 2616 return -ENOMEM; 2617 2618 /* Initialize struct ntfs_volume structure. */ 2619 *vol = (struct ntfs_volume) { 2620 .uid = INVALID_UID, 2621 .gid = INVALID_GID, 2622 .fmask = 0, 2623 .dmask = 0, 2624 .mft_zone_multiplier = 1, 2625 .on_errors = ON_ERRORS_CONTINUE, 2626 .nls_map = load_nls_default(), 2627 .preallocated_size = NTFS_DEF_PREALLOC_SIZE, 2628 }; 2629 2630 NVolSetShowHiddenFiles(vol); 2631 NVolSetCaseSensitive(vol); 2632 init_rwsem(&vol->mftbmp_lock); 2633 init_rwsem(&vol->lcnbmp_lock); 2634 2635 fc->s_fs_info = vol; 2636 fc->ops = &ntfs_context_ops; 2637 return 0; 2638 } 2639 2640 static struct file_system_type ntfs_fs_type = { 2641 .owner = THIS_MODULE, 2642 .name = "ntfs", 2643 .init_fs_context = ntfs_init_fs_context, 2644 .parameters = ntfs_parameters, 2645 .kill_sb = kill_block_super, 2646 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP, 2647 }; 2648 MODULE_ALIAS_FS("ntfs"); 2649 2650 static int ntfs_workqueue_init(void) 2651 { 2652 ntfs_wq = alloc_workqueue("ntfs-bg-io", 0, 0); 2653 if (!ntfs_wq) 2654 return -ENOMEM; 2655 return 0; 2656 } 2657 2658 static void ntfs_workqueue_destroy(void) 2659 { 2660 destroy_workqueue(ntfs_wq); 2661 ntfs_wq = NULL; 2662 } 2663 2664 /* Stable names for the slab caches. */ 2665 static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache"; 2666 static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache"; 2667 static const char ntfs_name_cache_name[] = "ntfs_name_cache"; 2668 static const char ntfs_inode_cache_name[] = "ntfs_inode_cache"; 2669 static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache"; 2670 2671 static int __init init_ntfs_fs(void) 2672 { 2673 int err = 0; 2674 2675 err = ntfs_workqueue_init(); 2676 if (err) { 2677 pr_crit("Failed to register workqueue!\n"); 2678 return err; 2679 } 2680 2681 ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name, 2682 sizeof(struct ntfs_index_context), 0 /* offset */, 2683 SLAB_HWCACHE_ALIGN, NULL /* ctor */); 2684 if (!ntfs_index_ctx_cache) { 2685 pr_crit("Failed to create %s!\n", ntfs_index_ctx_cache_name); 2686 goto ictx_err_out; 2687 } 2688 ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name, 2689 sizeof(struct ntfs_attr_search_ctx), 0 /* offset */, 2690 SLAB_HWCACHE_ALIGN, NULL /* ctor */); 2691 if (!ntfs_attr_ctx_cache) { 2692 pr_crit("NTFS: Failed to create %s!\n", 2693 ntfs_attr_ctx_cache_name); 2694 goto actx_err_out; 2695 } 2696 2697 ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name, 2698 (NTFS_MAX_NAME_LEN+2) * sizeof(__le16), 0, 2699 SLAB_HWCACHE_ALIGN, NULL); 2700 if (!ntfs_name_cache) { 2701 pr_crit("Failed to create %s!\n", ntfs_name_cache_name); 2702 goto name_err_out; 2703 } 2704 2705 ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name, 2706 sizeof(struct ntfs_inode), 0, SLAB_RECLAIM_ACCOUNT, NULL); 2707 if (!ntfs_inode_cache) { 2708 pr_crit("Failed to create %s!\n", ntfs_inode_cache_name); 2709 goto inode_err_out; 2710 } 2711 2712 ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name, 2713 sizeof(struct big_ntfs_inode), 0, SLAB_HWCACHE_ALIGN | 2714 SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT, 2715 ntfs_big_inode_init_once); 2716 if (!ntfs_big_inode_cache) { 2717 pr_crit("Failed to create %s!\n", ntfs_big_inode_cache_name); 2718 goto big_inode_err_out; 2719 } 2720 2721 /* Register the ntfs sysctls. */ 2722 err = ntfs_sysctl(1); 2723 if (err) { 2724 pr_crit("Failed to register NTFS sysctls!\n"); 2725 goto sysctl_err_out; 2726 } 2727 2728 err = register_filesystem(&ntfs_fs_type); 2729 if (!err) { 2730 ntfs_debug("NTFS driver registered successfully."); 2731 return 0; /* Success! */ 2732 } 2733 pr_crit("Failed to register NTFS filesystem driver!\n"); 2734 2735 /* Unregister the ntfs sysctls. */ 2736 ntfs_sysctl(0); 2737 sysctl_err_out: 2738 kmem_cache_destroy(ntfs_big_inode_cache); 2739 big_inode_err_out: 2740 kmem_cache_destroy(ntfs_inode_cache); 2741 inode_err_out: 2742 kmem_cache_destroy(ntfs_name_cache); 2743 name_err_out: 2744 kmem_cache_destroy(ntfs_attr_ctx_cache); 2745 actx_err_out: 2746 kmem_cache_destroy(ntfs_index_ctx_cache); 2747 ictx_err_out: 2748 if (!err) { 2749 pr_crit("Aborting NTFS filesystem driver registration...\n"); 2750 err = -ENOMEM; 2751 } 2752 return err; 2753 } 2754 2755 static void __exit exit_ntfs_fs(void) 2756 { 2757 ntfs_debug("Unregistering NTFS driver."); 2758 2759 unregister_filesystem(&ntfs_fs_type); 2760 2761 /* 2762 * Make sure all delayed rcu free inodes are flushed before we 2763 * destroy cache. 2764 */ 2765 rcu_barrier(); 2766 kmem_cache_destroy(ntfs_big_inode_cache); 2767 kmem_cache_destroy(ntfs_inode_cache); 2768 kmem_cache_destroy(ntfs_name_cache); 2769 kmem_cache_destroy(ntfs_attr_ctx_cache); 2770 kmem_cache_destroy(ntfs_index_ctx_cache); 2771 ntfs_workqueue_destroy(); 2772 /* Unregister the ntfs sysctls. */ 2773 ntfs_sysctl(0); 2774 } 2775 2776 module_init(init_ntfs_fs); 2777 module_exit(exit_ntfs_fs); 2778 2779 MODULE_AUTHOR("Anton Altaparmakov <anton@tuxera.com>"); /* Original read-only NTFS driver */ 2780 MODULE_AUTHOR("Namjae Jeon <linkinjeon@kernel.org>"); /* Add write, iomap and various features */ 2781 MODULE_DESCRIPTION("NTFS read-write filesystem driver"); 2782 MODULE_LICENSE("GPL"); 2783 #ifdef DEBUG 2784 module_param(debug_msgs, uint, 0); 2785 MODULE_PARM_DESC(debug_msgs, "Enable debug messages."); 2786 #endif 2787