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