1 /* 2 * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project. 3 * 4 * Copyright (c) 2001-2006 Anton Altaparmakov 5 * Copyright (c) 2001,2002 Richard Russon 6 * 7 * This program/include file is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License as published 9 * by the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program/include file is distributed in the hope that it will be 13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program (in the main directory of the Linux-NTFS 19 * distribution in the file COPYING); if not, write to the Free Software 20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 */ 22 23 #include <linux/stddef.h> 24 #include <linux/init.h> 25 #include <linux/slab.h> 26 #include <linux/string.h> 27 #include <linux/spinlock.h> 28 #include <linux/blkdev.h> /* For bdev_hardsect_size(). */ 29 #include <linux/backing-dev.h> 30 #include <linux/buffer_head.h> 31 #include <linux/vfs.h> 32 #include <linux/moduleparam.h> 33 #include <linux/smp_lock.h> 34 35 #include "sysctl.h" 36 #include "logfile.h" 37 #include "quota.h" 38 #include "usnjrnl.h" 39 #include "dir.h" 40 #include "debug.h" 41 #include "index.h" 42 #include "aops.h" 43 #include "layout.h" 44 #include "malloc.h" 45 #include "ntfs.h" 46 47 /* Number of mounted filesystems which have compression enabled. */ 48 static unsigned long ntfs_nr_compression_users; 49 50 /* A global default upcase table and a corresponding reference count. */ 51 static ntfschar *default_upcase = NULL; 52 static unsigned long ntfs_nr_upcase_users = 0; 53 54 /* Error constants/strings used in inode.c::ntfs_show_options(). */ 55 typedef enum { 56 /* One of these must be present, default is ON_ERRORS_CONTINUE. */ 57 ON_ERRORS_PANIC = 0x01, 58 ON_ERRORS_REMOUNT_RO = 0x02, 59 ON_ERRORS_CONTINUE = 0x04, 60 /* Optional, can be combined with any of the above. */ 61 ON_ERRORS_RECOVER = 0x10, 62 } ON_ERRORS_ACTIONS; 63 64 const option_t on_errors_arr[] = { 65 { ON_ERRORS_PANIC, "panic" }, 66 { ON_ERRORS_REMOUNT_RO, "remount-ro", }, 67 { ON_ERRORS_CONTINUE, "continue", }, 68 { ON_ERRORS_RECOVER, "recover" }, 69 { 0, NULL } 70 }; 71 72 /** 73 * simple_getbool - 74 * 75 * Copied from old ntfs driver (which copied from vfat driver). 76 */ 77 static int simple_getbool(char *s, BOOL *setval) 78 { 79 if (s) { 80 if (!strcmp(s, "1") || !strcmp(s, "yes") || !strcmp(s, "true")) 81 *setval = TRUE; 82 else if (!strcmp(s, "0") || !strcmp(s, "no") || 83 !strcmp(s, "false")) 84 *setval = FALSE; 85 else 86 return 0; 87 } else 88 *setval = TRUE; 89 return 1; 90 } 91 92 /** 93 * parse_options - parse the (re)mount options 94 * @vol: ntfs volume 95 * @opt: string containing the (re)mount options 96 * 97 * Parse the recognized options in @opt for the ntfs volume described by @vol. 98 */ 99 static BOOL parse_options(ntfs_volume *vol, char *opt) 100 { 101 char *p, *v, *ov; 102 static char *utf8 = "utf8"; 103 int errors = 0, sloppy = 0; 104 uid_t uid = (uid_t)-1; 105 gid_t gid = (gid_t)-1; 106 mode_t fmask = (mode_t)-1, dmask = (mode_t)-1; 107 int mft_zone_multiplier = -1, on_errors = -1; 108 int show_sys_files = -1, case_sensitive = -1, disable_sparse = -1; 109 struct nls_table *nls_map = NULL, *old_nls; 110 111 /* I am lazy... (-8 */ 112 #define NTFS_GETOPT_WITH_DEFAULT(option, variable, default_value) \ 113 if (!strcmp(p, option)) { \ 114 if (!v || !*v) \ 115 variable = default_value; \ 116 else { \ 117 variable = simple_strtoul(ov = v, &v, 0); \ 118 if (*v) \ 119 goto needs_val; \ 120 } \ 121 } 122 #define NTFS_GETOPT(option, variable) \ 123 if (!strcmp(p, option)) { \ 124 if (!v || !*v) \ 125 goto needs_arg; \ 126 variable = simple_strtoul(ov = v, &v, 0); \ 127 if (*v) \ 128 goto needs_val; \ 129 } 130 #define NTFS_GETOPT_OCTAL(option, variable) \ 131 if (!strcmp(p, option)) { \ 132 if (!v || !*v) \ 133 goto needs_arg; \ 134 variable = simple_strtoul(ov = v, &v, 8); \ 135 if (*v) \ 136 goto needs_val; \ 137 } 138 #define NTFS_GETOPT_BOOL(option, variable) \ 139 if (!strcmp(p, option)) { \ 140 BOOL val; \ 141 if (!simple_getbool(v, &val)) \ 142 goto needs_bool; \ 143 variable = val; \ 144 } 145 #define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array) \ 146 if (!strcmp(p, option)) { \ 147 int _i; \ 148 if (!v || !*v) \ 149 goto needs_arg; \ 150 ov = v; \ 151 if (variable == -1) \ 152 variable = 0; \ 153 for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \ 154 if (!strcmp(opt_array[_i].str, v)) { \ 155 variable |= opt_array[_i].val; \ 156 break; \ 157 } \ 158 if (!opt_array[_i].str || !*opt_array[_i].str) \ 159 goto needs_val; \ 160 } 161 if (!opt || !*opt) 162 goto no_mount_options; 163 ntfs_debug("Entering with mount options string: %s", opt); 164 while ((p = strsep(&opt, ","))) { 165 if ((v = strchr(p, '='))) 166 *v++ = 0; 167 NTFS_GETOPT("uid", uid) 168 else NTFS_GETOPT("gid", gid) 169 else NTFS_GETOPT_OCTAL("umask", fmask = dmask) 170 else NTFS_GETOPT_OCTAL("fmask", fmask) 171 else NTFS_GETOPT_OCTAL("dmask", dmask) 172 else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier) 173 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, TRUE) 174 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files) 175 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive) 176 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse) 177 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors, 178 on_errors_arr) 179 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes")) 180 ntfs_warning(vol->sb, "Ignoring obsolete option %s.", 181 p); 182 else if (!strcmp(p, "nls") || !strcmp(p, "iocharset")) { 183 if (!strcmp(p, "iocharset")) 184 ntfs_warning(vol->sb, "Option iocharset is " 185 "deprecated. Please use " 186 "option nls=<charsetname> in " 187 "the future."); 188 if (!v || !*v) 189 goto needs_arg; 190 use_utf8: 191 old_nls = nls_map; 192 nls_map = load_nls(v); 193 if (!nls_map) { 194 if (!old_nls) { 195 ntfs_error(vol->sb, "NLS character set " 196 "%s not found.", v); 197 return FALSE; 198 } 199 ntfs_error(vol->sb, "NLS character set %s not " 200 "found. Using previous one %s.", 201 v, old_nls->charset); 202 nls_map = old_nls; 203 } else /* nls_map */ { 204 if (old_nls) 205 unload_nls(old_nls); 206 } 207 } else if (!strcmp(p, "utf8")) { 208 BOOL val = FALSE; 209 ntfs_warning(vol->sb, "Option utf8 is no longer " 210 "supported, using option nls=utf8. Please " 211 "use option nls=utf8 in the future and " 212 "make sure utf8 is compiled either as a " 213 "module or into the kernel."); 214 if (!v || !*v) 215 val = TRUE; 216 else if (!simple_getbool(v, &val)) 217 goto needs_bool; 218 if (val) { 219 v = utf8; 220 goto use_utf8; 221 } 222 } else { 223 ntfs_error(vol->sb, "Unrecognized mount option %s.", p); 224 if (errors < INT_MAX) 225 errors++; 226 } 227 #undef NTFS_GETOPT_OPTIONS_ARRAY 228 #undef NTFS_GETOPT_BOOL 229 #undef NTFS_GETOPT 230 #undef NTFS_GETOPT_WITH_DEFAULT 231 } 232 no_mount_options: 233 if (errors && !sloppy) 234 return FALSE; 235 if (sloppy) 236 ntfs_warning(vol->sb, "Sloppy option given. Ignoring " 237 "unrecognized mount option(s) and continuing."); 238 /* Keep this first! */ 239 if (on_errors != -1) { 240 if (!on_errors) { 241 ntfs_error(vol->sb, "Invalid errors option argument " 242 "or bug in options parser."); 243 return FALSE; 244 } 245 } 246 if (nls_map) { 247 if (vol->nls_map && vol->nls_map != nls_map) { 248 ntfs_error(vol->sb, "Cannot change NLS character set " 249 "on remount."); 250 return FALSE; 251 } /* else (!vol->nls_map) */ 252 ntfs_debug("Using NLS character set %s.", nls_map->charset); 253 vol->nls_map = nls_map; 254 } else /* (!nls_map) */ { 255 if (!vol->nls_map) { 256 vol->nls_map = load_nls_default(); 257 if (!vol->nls_map) { 258 ntfs_error(vol->sb, "Failed to load default " 259 "NLS character set."); 260 return FALSE; 261 } 262 ntfs_debug("Using default NLS character set (%s).", 263 vol->nls_map->charset); 264 } 265 } 266 if (mft_zone_multiplier != -1) { 267 if (vol->mft_zone_multiplier && vol->mft_zone_multiplier != 268 mft_zone_multiplier) { 269 ntfs_error(vol->sb, "Cannot change mft_zone_multiplier " 270 "on remount."); 271 return FALSE; 272 } 273 if (mft_zone_multiplier < 1 || mft_zone_multiplier > 4) { 274 ntfs_error(vol->sb, "Invalid mft_zone_multiplier. " 275 "Using default value, i.e. 1."); 276 mft_zone_multiplier = 1; 277 } 278 vol->mft_zone_multiplier = mft_zone_multiplier; 279 } 280 if (!vol->mft_zone_multiplier) 281 vol->mft_zone_multiplier = 1; 282 if (on_errors != -1) 283 vol->on_errors = on_errors; 284 if (!vol->on_errors || vol->on_errors == ON_ERRORS_RECOVER) 285 vol->on_errors |= ON_ERRORS_CONTINUE; 286 if (uid != (uid_t)-1) 287 vol->uid = uid; 288 if (gid != (gid_t)-1) 289 vol->gid = gid; 290 if (fmask != (mode_t)-1) 291 vol->fmask = fmask; 292 if (dmask != (mode_t)-1) 293 vol->dmask = dmask; 294 if (show_sys_files != -1) { 295 if (show_sys_files) 296 NVolSetShowSystemFiles(vol); 297 else 298 NVolClearShowSystemFiles(vol); 299 } 300 if (case_sensitive != -1) { 301 if (case_sensitive) 302 NVolSetCaseSensitive(vol); 303 else 304 NVolClearCaseSensitive(vol); 305 } 306 if (disable_sparse != -1) { 307 if (disable_sparse) 308 NVolClearSparseEnabled(vol); 309 else { 310 if (!NVolSparseEnabled(vol) && 311 vol->major_ver && vol->major_ver < 3) 312 ntfs_warning(vol->sb, "Not enabling sparse " 313 "support due to NTFS volume " 314 "version %i.%i (need at least " 315 "version 3.0).", vol->major_ver, 316 vol->minor_ver); 317 else 318 NVolSetSparseEnabled(vol); 319 } 320 } 321 return TRUE; 322 needs_arg: 323 ntfs_error(vol->sb, "The %s option requires an argument.", p); 324 return FALSE; 325 needs_bool: 326 ntfs_error(vol->sb, "The %s option requires a boolean argument.", p); 327 return FALSE; 328 needs_val: 329 ntfs_error(vol->sb, "Invalid %s option argument: %s", p, ov); 330 return FALSE; 331 } 332 333 #ifdef NTFS_RW 334 335 /** 336 * ntfs_write_volume_flags - write new flags to the volume information flags 337 * @vol: ntfs volume on which to modify the flags 338 * @flags: new flags value for the volume information flags 339 * 340 * Internal function. You probably want to use ntfs_{set,clear}_volume_flags() 341 * instead (see below). 342 * 343 * Replace the volume information flags on the volume @vol with the value 344 * supplied in @flags. Note, this overwrites the volume information flags, so 345 * make sure to combine the flags you want to modify with the old flags and use 346 * the result when calling ntfs_write_volume_flags(). 347 * 348 * Return 0 on success and -errno on error. 349 */ 350 static int ntfs_write_volume_flags(ntfs_volume *vol, const VOLUME_FLAGS flags) 351 { 352 ntfs_inode *ni = NTFS_I(vol->vol_ino); 353 MFT_RECORD *m; 354 VOLUME_INFORMATION *vi; 355 ntfs_attr_search_ctx *ctx; 356 int err; 357 358 ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.", 359 le16_to_cpu(vol->vol_flags), le16_to_cpu(flags)); 360 if (vol->vol_flags == flags) 361 goto done; 362 BUG_ON(!ni); 363 m = map_mft_record(ni); 364 if (IS_ERR(m)) { 365 err = PTR_ERR(m); 366 goto err_out; 367 } 368 ctx = ntfs_attr_get_search_ctx(ni, m); 369 if (!ctx) { 370 err = -ENOMEM; 371 goto put_unm_err_out; 372 } 373 err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0, 374 ctx); 375 if (err) 376 goto put_unm_err_out; 377 vi = (VOLUME_INFORMATION*)((u8*)ctx->attr + 378 le16_to_cpu(ctx->attr->data.resident.value_offset)); 379 vol->vol_flags = vi->flags = flags; 380 flush_dcache_mft_record_page(ctx->ntfs_ino); 381 mark_mft_record_dirty(ctx->ntfs_ino); 382 ntfs_attr_put_search_ctx(ctx); 383 unmap_mft_record(ni); 384 done: 385 ntfs_debug("Done."); 386 return 0; 387 put_unm_err_out: 388 if (ctx) 389 ntfs_attr_put_search_ctx(ctx); 390 unmap_mft_record(ni); 391 err_out: 392 ntfs_error(vol->sb, "Failed with error code %i.", -err); 393 return err; 394 } 395 396 /** 397 * ntfs_set_volume_flags - set bits in the volume information flags 398 * @vol: ntfs volume on which to modify the flags 399 * @flags: flags to set on the volume 400 * 401 * Set 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 static inline int ntfs_set_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags) 406 { 407 flags &= VOLUME_FLAGS_MASK; 408 return ntfs_write_volume_flags(vol, vol->vol_flags | flags); 409 } 410 411 /** 412 * ntfs_clear_volume_flags - clear bits in the volume information flags 413 * @vol: ntfs volume on which to modify the flags 414 * @flags: flags to clear on the volume 415 * 416 * Clear the bits in @flags in the volume information flags on the volume @vol. 417 * 418 * Return 0 on success and -errno on error. 419 */ 420 static inline int ntfs_clear_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags) 421 { 422 flags &= VOLUME_FLAGS_MASK; 423 flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags)); 424 return ntfs_write_volume_flags(vol, flags); 425 } 426 427 #endif /* NTFS_RW */ 428 429 /** 430 * ntfs_remount - change the mount options of a mounted ntfs filesystem 431 * @sb: superblock of mounted ntfs filesystem 432 * @flags: remount flags 433 * @opt: remount options string 434 * 435 * Change the mount options of an already mounted ntfs filesystem. 436 * 437 * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after 438 * ntfs_remount() returns successfully (i.e. returns 0). Otherwise, 439 * @sb->s_flags are not changed. 440 */ 441 static int ntfs_remount(struct super_block *sb, int *flags, char *opt) 442 { 443 ntfs_volume *vol = NTFS_SB(sb); 444 445 ntfs_debug("Entering with remount options string: %s", opt); 446 #ifndef NTFS_RW 447 /* For read-only compiled driver, enforce read-only flag. */ 448 *flags |= MS_RDONLY; 449 #else /* NTFS_RW */ 450 /* 451 * For the read-write compiled driver, if we are remounting read-write, 452 * make sure there are no volume errors and that no unsupported volume 453 * flags are set. Also, empty the logfile journal as it would become 454 * stale as soon as something is written to the volume and mark the 455 * volume dirty so that chkdsk is run if the volume is not umounted 456 * cleanly. Finally, mark the quotas out of date so Windows rescans 457 * the volume on boot and updates them. 458 * 459 * When remounting read-only, mark the volume clean if no volume errors 460 * have occured. 461 */ 462 if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) { 463 static const char *es = ". Cannot remount read-write."; 464 465 /* Remounting read-write. */ 466 if (NVolErrors(vol)) { 467 ntfs_error(sb, "Volume has errors and is read-only%s", 468 es); 469 return -EROFS; 470 } 471 if (vol->vol_flags & VOLUME_IS_DIRTY) { 472 ntfs_error(sb, "Volume is dirty and read-only%s", es); 473 return -EROFS; 474 } 475 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) { 476 ntfs_error(sb, "Volume has unsupported flags set and " 477 "is read-only%s", es); 478 return -EROFS; 479 } 480 if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) { 481 ntfs_error(sb, "Failed to set dirty bit in volume " 482 "information flags%s", es); 483 return -EROFS; 484 } 485 #if 0 486 // TODO: Enable this code once we start modifying anything that 487 // is different between NTFS 1.2 and 3.x... 488 /* Set NT4 compatibility flag on newer NTFS version volumes. */ 489 if ((vol->major_ver > 1)) { 490 if (ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) { 491 ntfs_error(sb, "Failed to set NT4 " 492 "compatibility flag%s", es); 493 NVolSetErrors(vol); 494 return -EROFS; 495 } 496 } 497 #endif 498 if (!ntfs_empty_logfile(vol->logfile_ino)) { 499 ntfs_error(sb, "Failed to empty journal $LogFile%s", 500 es); 501 NVolSetErrors(vol); 502 return -EROFS; 503 } 504 if (!ntfs_mark_quotas_out_of_date(vol)) { 505 ntfs_error(sb, "Failed to mark quotas out of date%s", 506 es); 507 NVolSetErrors(vol); 508 return -EROFS; 509 } 510 if (!ntfs_stamp_usnjrnl(vol)) { 511 ntfs_error(sb, "Failed to stamp transation log " 512 "($UsnJrnl)%s", es); 513 NVolSetErrors(vol); 514 return -EROFS; 515 } 516 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) { 517 /* Remounting read-only. */ 518 if (!NVolErrors(vol)) { 519 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY)) 520 ntfs_warning(sb, "Failed to clear dirty bit " 521 "in volume information " 522 "flags. Run chkdsk."); 523 } 524 } 525 #endif /* NTFS_RW */ 526 527 // TODO: Deal with *flags. 528 529 if (!parse_options(vol, opt)) 530 return -EINVAL; 531 ntfs_debug("Done."); 532 return 0; 533 } 534 535 /** 536 * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector 537 * @sb: Super block of the device to which @b belongs. 538 * @b: Boot sector of device @sb to check. 539 * @silent: If TRUE, all output will be silenced. 540 * 541 * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot 542 * sector. Returns TRUE if it is valid and FALSE if not. 543 * 544 * @sb is only needed for warning/error output, i.e. it can be NULL when silent 545 * is TRUE. 546 */ 547 static BOOL is_boot_sector_ntfs(const struct super_block *sb, 548 const NTFS_BOOT_SECTOR *b, const BOOL silent) 549 { 550 /* 551 * Check that checksum == sum of u32 values from b to the checksum 552 * field. If checksum is zero, no checking is done. We will work when 553 * the checksum test fails, since some utilities update the boot sector 554 * ignoring the checksum which leaves the checksum out-of-date. We 555 * report a warning if this is the case. 556 */ 557 if ((void*)b < (void*)&b->checksum && b->checksum && !silent) { 558 le32 *u; 559 u32 i; 560 561 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u) 562 i += le32_to_cpup(u); 563 if (le32_to_cpu(b->checksum) != i) 564 ntfs_warning(sb, "Invalid boot sector checksum."); 565 } 566 /* Check OEMidentifier is "NTFS " */ 567 if (b->oem_id != magicNTFS) 568 goto not_ntfs; 569 /* Check bytes per sector value is between 256 and 4096. */ 570 if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 || 571 le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000) 572 goto not_ntfs; 573 /* Check sectors per cluster value is valid. */ 574 switch (b->bpb.sectors_per_cluster) { 575 case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128: 576 break; 577 default: 578 goto not_ntfs; 579 } 580 /* Check the cluster size is not above the maximum (64kiB). */ 581 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) * 582 b->bpb.sectors_per_cluster > NTFS_MAX_CLUSTER_SIZE) 583 goto not_ntfs; 584 /* Check reserved/unused fields are really zero. */ 585 if (le16_to_cpu(b->bpb.reserved_sectors) || 586 le16_to_cpu(b->bpb.root_entries) || 587 le16_to_cpu(b->bpb.sectors) || 588 le16_to_cpu(b->bpb.sectors_per_fat) || 589 le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats) 590 goto not_ntfs; 591 /* Check clusters per file mft record value is valid. */ 592 if ((u8)b->clusters_per_mft_record < 0xe1 || 593 (u8)b->clusters_per_mft_record > 0xf7) 594 switch (b->clusters_per_mft_record) { 595 case 1: case 2: case 4: case 8: case 16: case 32: case 64: 596 break; 597 default: 598 goto not_ntfs; 599 } 600 /* Check clusters per index block value is valid. */ 601 if ((u8)b->clusters_per_index_record < 0xe1 || 602 (u8)b->clusters_per_index_record > 0xf7) 603 switch (b->clusters_per_index_record) { 604 case 1: case 2: case 4: case 8: case 16: case 32: case 64: 605 break; 606 default: 607 goto not_ntfs; 608 } 609 /* 610 * Check for valid end of sector marker. We will work without it, but 611 * many BIOSes will refuse to boot from a bootsector if the magic is 612 * incorrect, so we emit a warning. 613 */ 614 if (!silent && b->end_of_sector_marker != const_cpu_to_le16(0xaa55)) 615 ntfs_warning(sb, "Invalid end of sector marker."); 616 return TRUE; 617 not_ntfs: 618 return FALSE; 619 } 620 621 /** 622 * read_ntfs_boot_sector - read the NTFS boot sector of a device 623 * @sb: super block of device to read the boot sector from 624 * @silent: if true, suppress all output 625 * 626 * Reads the boot sector from the device and validates it. If that fails, tries 627 * to read the backup boot sector, first from the end of the device a-la NT4 and 628 * later and then from the middle of the device a-la NT3.51 and before. 629 * 630 * If a valid boot sector is found but it is not the primary boot sector, we 631 * repair the primary boot sector silently (unless the device is read-only or 632 * the primary boot sector is not accessible). 633 * 634 * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super 635 * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized 636 * to their respective values. 637 * 638 * Return the unlocked buffer head containing the boot sector or NULL on error. 639 */ 640 static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb, 641 const int silent) 642 { 643 const char *read_err_str = "Unable to read %s boot sector."; 644 struct buffer_head *bh_primary, *bh_backup; 645 sector_t nr_blocks = NTFS_SB(sb)->nr_blocks; 646 647 /* Try to read primary boot sector. */ 648 if ((bh_primary = sb_bread(sb, 0))) { 649 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*) 650 bh_primary->b_data, silent)) 651 return bh_primary; 652 if (!silent) 653 ntfs_error(sb, "Primary boot sector is invalid."); 654 } else if (!silent) 655 ntfs_error(sb, read_err_str, "primary"); 656 if (!(NTFS_SB(sb)->on_errors & ON_ERRORS_RECOVER)) { 657 if (bh_primary) 658 brelse(bh_primary); 659 if (!silent) 660 ntfs_error(sb, "Mount option errors=recover not used. " 661 "Aborting without trying to recover."); 662 return NULL; 663 } 664 /* Try to read NT4+ backup boot sector. */ 665 if ((bh_backup = sb_bread(sb, nr_blocks - 1))) { 666 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*) 667 bh_backup->b_data, silent)) 668 goto hotfix_primary_boot_sector; 669 brelse(bh_backup); 670 } else if (!silent) 671 ntfs_error(sb, read_err_str, "backup"); 672 /* Try to read NT3.51- backup boot sector. */ 673 if ((bh_backup = sb_bread(sb, nr_blocks >> 1))) { 674 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*) 675 bh_backup->b_data, silent)) 676 goto hotfix_primary_boot_sector; 677 if (!silent) 678 ntfs_error(sb, "Could not find a valid backup boot " 679 "sector."); 680 brelse(bh_backup); 681 } else if (!silent) 682 ntfs_error(sb, read_err_str, "backup"); 683 /* We failed. Cleanup and return. */ 684 if (bh_primary) 685 brelse(bh_primary); 686 return NULL; 687 hotfix_primary_boot_sector: 688 if (bh_primary) { 689 /* 690 * If we managed to read sector zero and the volume is not 691 * read-only, copy the found, valid backup boot sector to the 692 * primary boot sector. Note we only copy the actual boot 693 * sector structure, not the actual whole device sector as that 694 * may be bigger and would potentially damage the $Boot system 695 * file (FIXME: Would be nice to know if the backup boot sector 696 * on a large sector device contains the whole boot loader or 697 * just the first 512 bytes). 698 */ 699 if (!(sb->s_flags & MS_RDONLY)) { 700 ntfs_warning(sb, "Hot-fix: Recovering invalid primary " 701 "boot sector from backup copy."); 702 memcpy(bh_primary->b_data, bh_backup->b_data, 703 NTFS_BLOCK_SIZE); 704 mark_buffer_dirty(bh_primary); 705 sync_dirty_buffer(bh_primary); 706 if (buffer_uptodate(bh_primary)) { 707 brelse(bh_backup); 708 return bh_primary; 709 } 710 ntfs_error(sb, "Hot-fix: Device write error while " 711 "recovering primary boot sector."); 712 } else { 713 ntfs_warning(sb, "Hot-fix: Recovery of primary boot " 714 "sector failed: Read-only mount."); 715 } 716 brelse(bh_primary); 717 } 718 ntfs_warning(sb, "Using backup boot sector."); 719 return bh_backup; 720 } 721 722 /** 723 * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol 724 * @vol: volume structure to initialise with data from boot sector 725 * @b: boot sector to parse 726 * 727 * Parse the ntfs boot sector @b and store all imporant information therein in 728 * the ntfs super block @vol. Return TRUE on success and FALSE on error. 729 */ 730 static BOOL parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b) 731 { 732 unsigned int sectors_per_cluster_bits, nr_hidden_sects; 733 int clusters_per_mft_record, clusters_per_index_record; 734 s64 ll; 735 736 vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector); 737 vol->sector_size_bits = ffs(vol->sector_size) - 1; 738 ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size, 739 vol->sector_size); 740 ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits, 741 vol->sector_size_bits); 742 if (vol->sector_size < vol->sb->s_blocksize) { 743 ntfs_error(vol->sb, "Sector size (%i) is smaller than the " 744 "device block size (%lu). This is not " 745 "supported. Sorry.", vol->sector_size, 746 vol->sb->s_blocksize); 747 return FALSE; 748 } 749 ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster); 750 sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1; 751 ntfs_debug("sectors_per_cluster_bits = 0x%x", 752 sectors_per_cluster_bits); 753 nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors); 754 ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects); 755 vol->cluster_size = vol->sector_size << sectors_per_cluster_bits; 756 vol->cluster_size_mask = vol->cluster_size - 1; 757 vol->cluster_size_bits = ffs(vol->cluster_size) - 1; 758 ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size, 759 vol->cluster_size); 760 ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask); 761 ntfs_debug("vol->cluster_size_bits = %i", vol->cluster_size_bits); 762 if (vol->cluster_size < vol->sector_size) { 763 ntfs_error(vol->sb, "Cluster size (%i) is smaller than the " 764 "sector size (%i). This is not supported. " 765 "Sorry.", vol->cluster_size, vol->sector_size); 766 return FALSE; 767 } 768 clusters_per_mft_record = b->clusters_per_mft_record; 769 ntfs_debug("clusters_per_mft_record = %i (0x%x)", 770 clusters_per_mft_record, clusters_per_mft_record); 771 if (clusters_per_mft_record > 0) 772 vol->mft_record_size = vol->cluster_size << 773 (ffs(clusters_per_mft_record) - 1); 774 else 775 /* 776 * When mft_record_size < cluster_size, clusters_per_mft_record 777 * = -log2(mft_record_size) bytes. mft_record_size normaly is 778 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal). 779 */ 780 vol->mft_record_size = 1 << -clusters_per_mft_record; 781 vol->mft_record_size_mask = vol->mft_record_size - 1; 782 vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1; 783 ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size, 784 vol->mft_record_size); 785 ntfs_debug("vol->mft_record_size_mask = 0x%x", 786 vol->mft_record_size_mask); 787 ntfs_debug("vol->mft_record_size_bits = %i (0x%x)", 788 vol->mft_record_size_bits, vol->mft_record_size_bits); 789 /* 790 * We cannot support mft record sizes above the PAGE_CACHE_SIZE since 791 * we store $MFT/$DATA, the table of mft records in the page cache. 792 */ 793 if (vol->mft_record_size > PAGE_CACHE_SIZE) { 794 ntfs_error(vol->sb, "Mft record size (%i) exceeds the " 795 "PAGE_CACHE_SIZE on your system (%lu). " 796 "This is not supported. Sorry.", 797 vol->mft_record_size, PAGE_CACHE_SIZE); 798 return FALSE; 799 } 800 /* We cannot support mft record sizes below the sector size. */ 801 if (vol->mft_record_size < vol->sector_size) { 802 ntfs_error(vol->sb, "Mft record size (%i) is smaller than the " 803 "sector size (%i). This is not supported. " 804 "Sorry.", vol->mft_record_size, 805 vol->sector_size); 806 return FALSE; 807 } 808 clusters_per_index_record = b->clusters_per_index_record; 809 ntfs_debug("clusters_per_index_record = %i (0x%x)", 810 clusters_per_index_record, clusters_per_index_record); 811 if (clusters_per_index_record > 0) 812 vol->index_record_size = vol->cluster_size << 813 (ffs(clusters_per_index_record) - 1); 814 else 815 /* 816 * When index_record_size < cluster_size, 817 * clusters_per_index_record = -log2(index_record_size) bytes. 818 * index_record_size normaly equals 4096 bytes, which is 819 * encoded as 0xF4 (-12 in decimal). 820 */ 821 vol->index_record_size = 1 << -clusters_per_index_record; 822 vol->index_record_size_mask = vol->index_record_size - 1; 823 vol->index_record_size_bits = ffs(vol->index_record_size) - 1; 824 ntfs_debug("vol->index_record_size = %i (0x%x)", 825 vol->index_record_size, vol->index_record_size); 826 ntfs_debug("vol->index_record_size_mask = 0x%x", 827 vol->index_record_size_mask); 828 ntfs_debug("vol->index_record_size_bits = %i (0x%x)", 829 vol->index_record_size_bits, 830 vol->index_record_size_bits); 831 /* We cannot support index record sizes below the sector size. */ 832 if (vol->index_record_size < vol->sector_size) { 833 ntfs_error(vol->sb, "Index record size (%i) is smaller than " 834 "the sector size (%i). This is not " 835 "supported. Sorry.", vol->index_record_size, 836 vol->sector_size); 837 return FALSE; 838 } 839 /* 840 * Get the size of the volume in clusters and check for 64-bit-ness. 841 * Windows currently only uses 32 bits to save the clusters so we do 842 * the same as it is much faster on 32-bit CPUs. 843 */ 844 ll = sle64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits; 845 if ((u64)ll >= 1ULL << 32) { 846 ntfs_error(vol->sb, "Cannot handle 64-bit clusters. Sorry."); 847 return FALSE; 848 } 849 vol->nr_clusters = ll; 850 ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol->nr_clusters); 851 /* 852 * On an architecture where unsigned long is 32-bits, we restrict the 853 * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler 854 * will hopefully optimize the whole check away. 855 */ 856 if (sizeof(unsigned long) < 8) { 857 if ((ll << vol->cluster_size_bits) >= (1ULL << 41)) { 858 ntfs_error(vol->sb, "Volume size (%lluTiB) is too " 859 "large for this architecture. " 860 "Maximum supported is 2TiB. Sorry.", 861 (unsigned long long)ll >> (40 - 862 vol->cluster_size_bits)); 863 return FALSE; 864 } 865 } 866 ll = sle64_to_cpu(b->mft_lcn); 867 if (ll >= vol->nr_clusters) { 868 ntfs_error(vol->sb, "MFT LCN (%lli, 0x%llx) is beyond end of " 869 "volume. Weird.", (unsigned long long)ll, 870 (unsigned long long)ll); 871 return FALSE; 872 } 873 vol->mft_lcn = ll; 874 ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn); 875 ll = sle64_to_cpu(b->mftmirr_lcn); 876 if (ll >= vol->nr_clusters) { 877 ntfs_error(vol->sb, "MFTMirr LCN (%lli, 0x%llx) is beyond end " 878 "of volume. Weird.", (unsigned long long)ll, 879 (unsigned long long)ll); 880 return FALSE; 881 } 882 vol->mftmirr_lcn = ll; 883 ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol->mftmirr_lcn); 884 #ifdef NTFS_RW 885 /* 886 * Work out the size of the mft mirror in number of mft records. If the 887 * cluster size is less than or equal to the size taken by four mft 888 * records, the mft mirror stores the first four mft records. If the 889 * cluster size is bigger than the size taken by four mft records, the 890 * mft mirror contains as many mft records as will fit into one 891 * cluster. 892 */ 893 if (vol->cluster_size <= (4 << vol->mft_record_size_bits)) 894 vol->mftmirr_size = 4; 895 else 896 vol->mftmirr_size = vol->cluster_size >> 897 vol->mft_record_size_bits; 898 ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size); 899 #endif /* NTFS_RW */ 900 vol->serial_no = le64_to_cpu(b->volume_serial_number); 901 ntfs_debug("vol->serial_no = 0x%llx", 902 (unsigned long long)vol->serial_no); 903 return TRUE; 904 } 905 906 /** 907 * ntfs_setup_allocators - initialize the cluster and mft allocators 908 * @vol: volume structure for which to setup the allocators 909 * 910 * Setup the cluster (lcn) and mft allocators to the starting values. 911 */ 912 static void ntfs_setup_allocators(ntfs_volume *vol) 913 { 914 #ifdef NTFS_RW 915 LCN mft_zone_size, mft_lcn; 916 #endif /* NTFS_RW */ 917 918 ntfs_debug("vol->mft_zone_multiplier = 0x%x", 919 vol->mft_zone_multiplier); 920 #ifdef NTFS_RW 921 /* Determine the size of the MFT zone. */ 922 mft_zone_size = vol->nr_clusters; 923 switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */ 924 case 4: 925 mft_zone_size >>= 1; /* 50% */ 926 break; 927 case 3: 928 mft_zone_size = (mft_zone_size + 929 (mft_zone_size >> 1)) >> 2; /* 37.5% */ 930 break; 931 case 2: 932 mft_zone_size >>= 2; /* 25% */ 933 break; 934 /* case 1: */ 935 default: 936 mft_zone_size >>= 3; /* 12.5% */ 937 break; 938 } 939 /* Setup the mft zone. */ 940 vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn; 941 ntfs_debug("vol->mft_zone_pos = 0x%llx", 942 (unsigned long long)vol->mft_zone_pos); 943 /* 944 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs 945 * source) and if the actual mft_lcn is in the expected place or even 946 * further to the front of the volume, extend the mft_zone to cover the 947 * beginning of the volume as well. This is in order to protect the 948 * area reserved for the mft bitmap as well within the mft_zone itself. 949 * On non-standard volumes we do not protect it as the overhead would 950 * be higher than the speed increase we would get by doing it. 951 */ 952 mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size; 953 if (mft_lcn * vol->cluster_size < 16 * 1024) 954 mft_lcn = (16 * 1024 + vol->cluster_size - 1) / 955 vol->cluster_size; 956 if (vol->mft_zone_start <= mft_lcn) 957 vol->mft_zone_start = 0; 958 ntfs_debug("vol->mft_zone_start = 0x%llx", 959 (unsigned long long)vol->mft_zone_start); 960 /* 961 * Need to cap the mft zone on non-standard volumes so that it does 962 * not point outside the boundaries of the volume. We do this by 963 * halving the zone size until we are inside the volume. 964 */ 965 vol->mft_zone_end = vol->mft_lcn + mft_zone_size; 966 while (vol->mft_zone_end >= vol->nr_clusters) { 967 mft_zone_size >>= 1; 968 vol->mft_zone_end = vol->mft_lcn + mft_zone_size; 969 } 970 ntfs_debug("vol->mft_zone_end = 0x%llx", 971 (unsigned long long)vol->mft_zone_end); 972 /* 973 * Set the current position within each data zone to the start of the 974 * respective zone. 975 */ 976 vol->data1_zone_pos = vol->mft_zone_end; 977 ntfs_debug("vol->data1_zone_pos = 0x%llx", 978 (unsigned long long)vol->data1_zone_pos); 979 vol->data2_zone_pos = 0; 980 ntfs_debug("vol->data2_zone_pos = 0x%llx", 981 (unsigned long long)vol->data2_zone_pos); 982 983 /* Set the mft data allocation position to mft record 24. */ 984 vol->mft_data_pos = 24; 985 ntfs_debug("vol->mft_data_pos = 0x%llx", 986 (unsigned long long)vol->mft_data_pos); 987 #endif /* NTFS_RW */ 988 } 989 990 #ifdef NTFS_RW 991 992 /** 993 * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume 994 * @vol: ntfs super block describing device whose mft mirror to load 995 * 996 * Return TRUE on success or FALSE on error. 997 */ 998 static BOOL load_and_init_mft_mirror(ntfs_volume *vol) 999 { 1000 struct inode *tmp_ino; 1001 ntfs_inode *tmp_ni; 1002 1003 ntfs_debug("Entering."); 1004 /* Get mft mirror inode. */ 1005 tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr); 1006 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) { 1007 if (!IS_ERR(tmp_ino)) 1008 iput(tmp_ino); 1009 /* Caller will display error message. */ 1010 return FALSE; 1011 } 1012 /* 1013 * Re-initialize some specifics about $MFTMirr's inode as 1014 * ntfs_read_inode() will have set up the default ones. 1015 */ 1016 /* Set uid and gid to root. */ 1017 tmp_ino->i_uid = tmp_ino->i_gid = 0; 1018 /* Regular file. No access for anyone. */ 1019 tmp_ino->i_mode = S_IFREG; 1020 /* No VFS initiated operations allowed for $MFTMirr. */ 1021 tmp_ino->i_op = &ntfs_empty_inode_ops; 1022 tmp_ino->i_fop = &ntfs_empty_file_ops; 1023 /* Put in our special address space operations. */ 1024 tmp_ino->i_mapping->a_ops = &ntfs_mst_aops; 1025 tmp_ni = NTFS_I(tmp_ino); 1026 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */ 1027 NInoSetMstProtected(tmp_ni); 1028 NInoSetSparseDisabled(tmp_ni); 1029 /* 1030 * Set up our little cheat allowing us to reuse the async read io 1031 * completion handler for directories. 1032 */ 1033 tmp_ni->itype.index.block_size = vol->mft_record_size; 1034 tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits; 1035 vol->mftmirr_ino = tmp_ino; 1036 ntfs_debug("Done."); 1037 return TRUE; 1038 } 1039 1040 /** 1041 * check_mft_mirror - compare contents of the mft mirror with the mft 1042 * @vol: ntfs super block describing device whose mft mirror to check 1043 * 1044 * Return TRUE on success or FALSE on error. 1045 * 1046 * Note, this function also results in the mft mirror runlist being completely 1047 * mapped into memory. The mft mirror write code requires this and will BUG() 1048 * should it find an unmapped runlist element. 1049 */ 1050 static BOOL check_mft_mirror(ntfs_volume *vol) 1051 { 1052 struct super_block *sb = vol->sb; 1053 ntfs_inode *mirr_ni; 1054 struct page *mft_page, *mirr_page; 1055 u8 *kmft, *kmirr; 1056 runlist_element *rl, rl2[2]; 1057 pgoff_t index; 1058 int mrecs_per_page, i; 1059 1060 ntfs_debug("Entering."); 1061 /* Compare contents of $MFT and $MFTMirr. */ 1062 mrecs_per_page = PAGE_CACHE_SIZE / vol->mft_record_size; 1063 BUG_ON(!mrecs_per_page); 1064 BUG_ON(!vol->mftmirr_size); 1065 mft_page = mirr_page = NULL; 1066 kmft = kmirr = NULL; 1067 index = i = 0; 1068 do { 1069 u32 bytes; 1070 1071 /* Switch pages if necessary. */ 1072 if (!(i % mrecs_per_page)) { 1073 if (index) { 1074 ntfs_unmap_page(mft_page); 1075 ntfs_unmap_page(mirr_page); 1076 } 1077 /* Get the $MFT page. */ 1078 mft_page = ntfs_map_page(vol->mft_ino->i_mapping, 1079 index); 1080 if (IS_ERR(mft_page)) { 1081 ntfs_error(sb, "Failed to read $MFT."); 1082 return FALSE; 1083 } 1084 kmft = page_address(mft_page); 1085 /* Get the $MFTMirr page. */ 1086 mirr_page = ntfs_map_page(vol->mftmirr_ino->i_mapping, 1087 index); 1088 if (IS_ERR(mirr_page)) { 1089 ntfs_error(sb, "Failed to read $MFTMirr."); 1090 goto mft_unmap_out; 1091 } 1092 kmirr = page_address(mirr_page); 1093 ++index; 1094 } 1095 /* Make sure the record is ok. */ 1096 if (ntfs_is_baad_recordp((le32*)kmft)) { 1097 ntfs_error(sb, "Incomplete multi sector transfer " 1098 "detected in mft record %i.", i); 1099 mm_unmap_out: 1100 ntfs_unmap_page(mirr_page); 1101 mft_unmap_out: 1102 ntfs_unmap_page(mft_page); 1103 return FALSE; 1104 } 1105 if (ntfs_is_baad_recordp((le32*)kmirr)) { 1106 ntfs_error(sb, "Incomplete multi sector transfer " 1107 "detected in mft mirror record %i.", i); 1108 goto mm_unmap_out; 1109 } 1110 /* Get the amount of data in the current record. */ 1111 bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use); 1112 if (!bytes || bytes > vol->mft_record_size) { 1113 bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use); 1114 if (!bytes || bytes > vol->mft_record_size) 1115 bytes = vol->mft_record_size; 1116 } 1117 /* Compare the two records. */ 1118 if (memcmp(kmft, kmirr, bytes)) { 1119 ntfs_error(sb, "$MFT and $MFTMirr (record %i) do not " 1120 "match. Run ntfsfix or chkdsk.", i); 1121 goto mm_unmap_out; 1122 } 1123 kmft += vol->mft_record_size; 1124 kmirr += vol->mft_record_size; 1125 } while (++i < vol->mftmirr_size); 1126 /* Release the last pages. */ 1127 ntfs_unmap_page(mft_page); 1128 ntfs_unmap_page(mirr_page); 1129 1130 /* Construct the mft mirror runlist by hand. */ 1131 rl2[0].vcn = 0; 1132 rl2[0].lcn = vol->mftmirr_lcn; 1133 rl2[0].length = (vol->mftmirr_size * vol->mft_record_size + 1134 vol->cluster_size - 1) / vol->cluster_size; 1135 rl2[1].vcn = rl2[0].length; 1136 rl2[1].lcn = LCN_ENOENT; 1137 rl2[1].length = 0; 1138 /* 1139 * Because we have just read all of the mft mirror, we know we have 1140 * mapped the full runlist for it. 1141 */ 1142 mirr_ni = NTFS_I(vol->mftmirr_ino); 1143 down_read(&mirr_ni->runlist.lock); 1144 rl = mirr_ni->runlist.rl; 1145 /* Compare the two runlists. They must be identical. */ 1146 i = 0; 1147 do { 1148 if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn || 1149 rl2[i].length != rl[i].length) { 1150 ntfs_error(sb, "$MFTMirr location mismatch. " 1151 "Run chkdsk."); 1152 up_read(&mirr_ni->runlist.lock); 1153 return FALSE; 1154 } 1155 } while (rl2[i++].length); 1156 up_read(&mirr_ni->runlist.lock); 1157 ntfs_debug("Done."); 1158 return TRUE; 1159 } 1160 1161 /** 1162 * load_and_check_logfile - load and check the logfile inode for a volume 1163 * @vol: ntfs super block describing device whose logfile to load 1164 * 1165 * Return TRUE on success or FALSE on error. 1166 */ 1167 static BOOL load_and_check_logfile(ntfs_volume *vol, 1168 RESTART_PAGE_HEADER **rp) 1169 { 1170 struct inode *tmp_ino; 1171 1172 ntfs_debug("Entering."); 1173 tmp_ino = ntfs_iget(vol->sb, FILE_LogFile); 1174 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) { 1175 if (!IS_ERR(tmp_ino)) 1176 iput(tmp_ino); 1177 /* Caller will display error message. */ 1178 return FALSE; 1179 } 1180 if (!ntfs_check_logfile(tmp_ino, rp)) { 1181 iput(tmp_ino); 1182 /* ntfs_check_logfile() will have displayed error output. */ 1183 return FALSE; 1184 } 1185 NInoSetSparseDisabled(NTFS_I(tmp_ino)); 1186 vol->logfile_ino = tmp_ino; 1187 ntfs_debug("Done."); 1188 return TRUE; 1189 } 1190 1191 #define NTFS_HIBERFIL_HEADER_SIZE 4096 1192 1193 /** 1194 * check_windows_hibernation_status - check if Windows is suspended on a volume 1195 * @vol: ntfs super block of device to check 1196 * 1197 * Check if Windows is hibernated on the ntfs volume @vol. This is done by 1198 * looking for the file hiberfil.sys in the root directory of the volume. If 1199 * the file is not present Windows is definitely not suspended. 1200 * 1201 * If hiberfil.sys exists and is less than 4kiB in size it means Windows is 1202 * definitely suspended (this volume is not the system volume). Caveat: on a 1203 * system with many volumes it is possible that the < 4kiB check is bogus but 1204 * for now this should do fine. 1205 * 1206 * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the 1207 * hiberfil header (which is the first 4kiB). If this begins with "hibr", 1208 * Windows is definitely suspended. If it is completely full of zeroes, 1209 * Windows is definitely not hibernated. Any other case is treated as if 1210 * Windows is suspended. This caters for the above mentioned caveat of a 1211 * system with many volumes where no "hibr" magic would be present and there is 1212 * no zero header. 1213 * 1214 * Return 0 if Windows is not hibernated on the volume, >0 if Windows is 1215 * hibernated on the volume, and -errno on error. 1216 */ 1217 static int check_windows_hibernation_status(ntfs_volume *vol) 1218 { 1219 MFT_REF mref; 1220 struct inode *vi; 1221 ntfs_inode *ni; 1222 struct page *page; 1223 u32 *kaddr, *kend; 1224 ntfs_name *name = NULL; 1225 int ret = 1; 1226 static const ntfschar hiberfil[13] = { const_cpu_to_le16('h'), 1227 const_cpu_to_le16('i'), const_cpu_to_le16('b'), 1228 const_cpu_to_le16('e'), const_cpu_to_le16('r'), 1229 const_cpu_to_le16('f'), const_cpu_to_le16('i'), 1230 const_cpu_to_le16('l'), const_cpu_to_le16('.'), 1231 const_cpu_to_le16('s'), const_cpu_to_le16('y'), 1232 const_cpu_to_le16('s'), 0 }; 1233 1234 ntfs_debug("Entering."); 1235 /* 1236 * Find the inode number for the hibernation file by looking up the 1237 * filename hiberfil.sys in the root directory. 1238 */ 1239 mutex_lock(&vol->root_ino->i_mutex); 1240 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12, 1241 &name); 1242 mutex_unlock(&vol->root_ino->i_mutex); 1243 if (IS_ERR_MREF(mref)) { 1244 ret = MREF_ERR(mref); 1245 /* If the file does not exist, Windows is not hibernated. */ 1246 if (ret == -ENOENT) { 1247 ntfs_debug("hiberfil.sys not present. Windows is not " 1248 "hibernated on the volume."); 1249 return 0; 1250 } 1251 /* A real error occured. */ 1252 ntfs_error(vol->sb, "Failed to find inode number for " 1253 "hiberfil.sys."); 1254 return ret; 1255 } 1256 /* We do not care for the type of match that was found. */ 1257 kfree(name); 1258 /* Get the inode. */ 1259 vi = ntfs_iget(vol->sb, MREF(mref)); 1260 if (IS_ERR(vi) || is_bad_inode(vi)) { 1261 if (!IS_ERR(vi)) 1262 iput(vi); 1263 ntfs_error(vol->sb, "Failed to load hiberfil.sys."); 1264 return IS_ERR(vi) ? PTR_ERR(vi) : -EIO; 1265 } 1266 if (unlikely(i_size_read(vi) < NTFS_HIBERFIL_HEADER_SIZE)) { 1267 ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). " 1268 "Windows is hibernated on the volume. This " 1269 "is not the system volume.", i_size_read(vi)); 1270 goto iput_out; 1271 } 1272 ni = NTFS_I(vi); 1273 page = ntfs_map_page(vi->i_mapping, 0); 1274 if (IS_ERR(page)) { 1275 ntfs_error(vol->sb, "Failed to read from hiberfil.sys."); 1276 ret = PTR_ERR(page); 1277 goto iput_out; 1278 } 1279 kaddr = (u32*)page_address(page); 1280 if (*(le32*)kaddr == const_cpu_to_le32(0x72626968)/*'hibr'*/) { 1281 ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is " 1282 "hibernated on the volume. This is the " 1283 "system volume."); 1284 goto unm_iput_out; 1285 } 1286 kend = kaddr + NTFS_HIBERFIL_HEADER_SIZE/sizeof(*kaddr); 1287 do { 1288 if (unlikely(*kaddr)) { 1289 ntfs_debug("hiberfil.sys is larger than 4kiB " 1290 "(0x%llx), does not contain the " 1291 "\"hibr\" magic, and does not have a " 1292 "zero header. Windows is hibernated " 1293 "on the volume. This is not the " 1294 "system volume.", i_size_read(vi)); 1295 goto unm_iput_out; 1296 } 1297 } while (++kaddr < kend); 1298 ntfs_debug("hiberfil.sys contains a zero header. Windows is not " 1299 "hibernated on the volume. This is the system " 1300 "volume."); 1301 ret = 0; 1302 unm_iput_out: 1303 ntfs_unmap_page(page); 1304 iput_out: 1305 iput(vi); 1306 return ret; 1307 } 1308 1309 /** 1310 * load_and_init_quota - load and setup the quota file for a volume if present 1311 * @vol: ntfs super block describing device whose quota file to load 1312 * 1313 * Return TRUE on success or FALSE on error. If $Quota is not present, we 1314 * leave vol->quota_ino as NULL and return success. 1315 */ 1316 static BOOL load_and_init_quota(ntfs_volume *vol) 1317 { 1318 MFT_REF mref; 1319 struct inode *tmp_ino; 1320 ntfs_name *name = NULL; 1321 static const ntfschar Quota[7] = { const_cpu_to_le16('$'), 1322 const_cpu_to_le16('Q'), const_cpu_to_le16('u'), 1323 const_cpu_to_le16('o'), const_cpu_to_le16('t'), 1324 const_cpu_to_le16('a'), 0 }; 1325 static ntfschar Q[3] = { const_cpu_to_le16('$'), 1326 const_cpu_to_le16('Q'), 0 }; 1327 1328 ntfs_debug("Entering."); 1329 /* 1330 * Find the inode number for the quota file by looking up the filename 1331 * $Quota in the extended system files directory $Extend. 1332 */ 1333 mutex_lock(&vol->extend_ino->i_mutex); 1334 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6, 1335 &name); 1336 mutex_unlock(&vol->extend_ino->i_mutex); 1337 if (IS_ERR_MREF(mref)) { 1338 /* 1339 * If the file does not exist, quotas are disabled and have 1340 * never been enabled on this volume, just return success. 1341 */ 1342 if (MREF_ERR(mref) == -ENOENT) { 1343 ntfs_debug("$Quota not present. Volume does not have " 1344 "quotas enabled."); 1345 /* 1346 * No need to try to set quotas out of date if they are 1347 * not enabled. 1348 */ 1349 NVolSetQuotaOutOfDate(vol); 1350 return TRUE; 1351 } 1352 /* A real error occured. */ 1353 ntfs_error(vol->sb, "Failed to find inode number for $Quota."); 1354 return FALSE; 1355 } 1356 /* We do not care for the type of match that was found. */ 1357 kfree(name); 1358 /* Get the inode. */ 1359 tmp_ino = ntfs_iget(vol->sb, MREF(mref)); 1360 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) { 1361 if (!IS_ERR(tmp_ino)) 1362 iput(tmp_ino); 1363 ntfs_error(vol->sb, "Failed to load $Quota."); 1364 return FALSE; 1365 } 1366 vol->quota_ino = tmp_ino; 1367 /* Get the $Q index allocation attribute. */ 1368 tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2); 1369 if (IS_ERR(tmp_ino)) { 1370 ntfs_error(vol->sb, "Failed to load $Quota/$Q index."); 1371 return FALSE; 1372 } 1373 vol->quota_q_ino = tmp_ino; 1374 ntfs_debug("Done."); 1375 return TRUE; 1376 } 1377 1378 /** 1379 * load_and_init_usnjrnl - load and setup the transaction log if present 1380 * @vol: ntfs super block describing device whose usnjrnl file to load 1381 * 1382 * Return TRUE on success or FALSE on error. 1383 * 1384 * If $UsnJrnl is not present or in the process of being disabled, we set 1385 * NVolUsnJrnlStamped() and return success. 1386 * 1387 * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn, 1388 * i.e. transaction logging has only just been enabled or the journal has been 1389 * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped() 1390 * and return success. 1391 */ 1392 static BOOL load_and_init_usnjrnl(ntfs_volume *vol) 1393 { 1394 MFT_REF mref; 1395 struct inode *tmp_ino; 1396 ntfs_inode *tmp_ni; 1397 struct page *page; 1398 ntfs_name *name = NULL; 1399 USN_HEADER *uh; 1400 static const ntfschar UsnJrnl[9] = { const_cpu_to_le16('$'), 1401 const_cpu_to_le16('U'), const_cpu_to_le16('s'), 1402 const_cpu_to_le16('n'), const_cpu_to_le16('J'), 1403 const_cpu_to_le16('r'), const_cpu_to_le16('n'), 1404 const_cpu_to_le16('l'), 0 }; 1405 static ntfschar Max[5] = { const_cpu_to_le16('$'), 1406 const_cpu_to_le16('M'), const_cpu_to_le16('a'), 1407 const_cpu_to_le16('x'), 0 }; 1408 static ntfschar J[3] = { const_cpu_to_le16('$'), 1409 const_cpu_to_le16('J'), 0 }; 1410 1411 ntfs_debug("Entering."); 1412 /* 1413 * Find the inode number for the transaction log file by looking up the 1414 * filename $UsnJrnl in the extended system files directory $Extend. 1415 */ 1416 mutex_lock(&vol->extend_ino->i_mutex); 1417 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), UsnJrnl, 8, 1418 &name); 1419 mutex_unlock(&vol->extend_ino->i_mutex); 1420 if (IS_ERR_MREF(mref)) { 1421 /* 1422 * If the file does not exist, transaction logging is disabled, 1423 * just return success. 1424 */ 1425 if (MREF_ERR(mref) == -ENOENT) { 1426 ntfs_debug("$UsnJrnl not present. Volume does not " 1427 "have transaction logging enabled."); 1428 not_enabled: 1429 /* 1430 * No need to try to stamp the transaction log if 1431 * transaction logging is not enabled. 1432 */ 1433 NVolSetUsnJrnlStamped(vol); 1434 return TRUE; 1435 } 1436 /* A real error occured. */ 1437 ntfs_error(vol->sb, "Failed to find inode number for " 1438 "$UsnJrnl."); 1439 return FALSE; 1440 } 1441 /* We do not care for the type of match that was found. */ 1442 kfree(name); 1443 /* Get the inode. */ 1444 tmp_ino = ntfs_iget(vol->sb, MREF(mref)); 1445 if (unlikely(IS_ERR(tmp_ino) || is_bad_inode(tmp_ino))) { 1446 if (!IS_ERR(tmp_ino)) 1447 iput(tmp_ino); 1448 ntfs_error(vol->sb, "Failed to load $UsnJrnl."); 1449 return FALSE; 1450 } 1451 vol->usnjrnl_ino = tmp_ino; 1452 /* 1453 * If the transaction log is in the process of being deleted, we can 1454 * ignore it. 1455 */ 1456 if (unlikely(vol->vol_flags & VOLUME_DELETE_USN_UNDERWAY)) { 1457 ntfs_debug("$UsnJrnl in the process of being disabled. " 1458 "Volume does not have transaction logging " 1459 "enabled."); 1460 goto not_enabled; 1461 } 1462 /* Get the $DATA/$Max attribute. */ 1463 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, Max, 4); 1464 if (IS_ERR(tmp_ino)) { 1465 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$Max " 1466 "attribute."); 1467 return FALSE; 1468 } 1469 vol->usnjrnl_max_ino = tmp_ino; 1470 if (unlikely(i_size_read(tmp_ino) < sizeof(USN_HEADER))) { 1471 ntfs_error(vol->sb, "Found corrupt $UsnJrnl/$DATA/$Max " 1472 "attribute (size is 0x%llx but should be at " 1473 "least 0x%zx bytes).", i_size_read(tmp_ino), 1474 sizeof(USN_HEADER)); 1475 return FALSE; 1476 } 1477 /* Get the $DATA/$J attribute. */ 1478 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, J, 2); 1479 if (IS_ERR(tmp_ino)) { 1480 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$J " 1481 "attribute."); 1482 return FALSE; 1483 } 1484 vol->usnjrnl_j_ino = tmp_ino; 1485 /* Verify $J is non-resident and sparse. */ 1486 tmp_ni = NTFS_I(vol->usnjrnl_j_ino); 1487 if (unlikely(!NInoNonResident(tmp_ni) || !NInoSparse(tmp_ni))) { 1488 ntfs_error(vol->sb, "$UsnJrnl/$DATA/$J attribute is resident " 1489 "and/or not sparse."); 1490 return FALSE; 1491 } 1492 /* Read the USN_HEADER from $DATA/$Max. */ 1493 page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0); 1494 if (IS_ERR(page)) { 1495 ntfs_error(vol->sb, "Failed to read from $UsnJrnl/$DATA/$Max " 1496 "attribute."); 1497 return FALSE; 1498 } 1499 uh = (USN_HEADER*)page_address(page); 1500 /* Sanity check the $Max. */ 1501 if (unlikely(sle64_to_cpu(uh->allocation_delta) > 1502 sle64_to_cpu(uh->maximum_size))) { 1503 ntfs_error(vol->sb, "Allocation delta (0x%llx) exceeds " 1504 "maximum size (0x%llx). $UsnJrnl is corrupt.", 1505 (long long)sle64_to_cpu(uh->allocation_delta), 1506 (long long)sle64_to_cpu(uh->maximum_size)); 1507 ntfs_unmap_page(page); 1508 return FALSE; 1509 } 1510 /* 1511 * If the transaction log has been stamped and nothing has been written 1512 * to it since, we do not need to stamp it. 1513 */ 1514 if (unlikely(sle64_to_cpu(uh->lowest_valid_usn) >= 1515 i_size_read(vol->usnjrnl_j_ino))) { 1516 if (likely(sle64_to_cpu(uh->lowest_valid_usn) == 1517 i_size_read(vol->usnjrnl_j_ino))) { 1518 ntfs_unmap_page(page); 1519 ntfs_debug("$UsnJrnl is enabled but nothing has been " 1520 "logged since it was last stamped. " 1521 "Treating this as if the volume does " 1522 "not have transaction logging " 1523 "enabled."); 1524 goto not_enabled; 1525 } 1526 ntfs_error(vol->sb, "$UsnJrnl has lowest valid usn (0x%llx) " 1527 "which is out of bounds (0x%llx). $UsnJrnl " 1528 "is corrupt.", 1529 (long long)sle64_to_cpu(uh->lowest_valid_usn), 1530 i_size_read(vol->usnjrnl_j_ino)); 1531 ntfs_unmap_page(page); 1532 return FALSE; 1533 } 1534 ntfs_unmap_page(page); 1535 ntfs_debug("Done."); 1536 return TRUE; 1537 } 1538 1539 /** 1540 * load_and_init_attrdef - load the attribute definitions table for a volume 1541 * @vol: ntfs super block describing device whose attrdef to load 1542 * 1543 * Return TRUE on success or FALSE on error. 1544 */ 1545 static BOOL load_and_init_attrdef(ntfs_volume *vol) 1546 { 1547 loff_t i_size; 1548 struct super_block *sb = vol->sb; 1549 struct inode *ino; 1550 struct page *page; 1551 pgoff_t index, max_index; 1552 unsigned int size; 1553 1554 ntfs_debug("Entering."); 1555 /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */ 1556 ino = ntfs_iget(sb, FILE_AttrDef); 1557 if (IS_ERR(ino) || is_bad_inode(ino)) { 1558 if (!IS_ERR(ino)) 1559 iput(ino); 1560 goto failed; 1561 } 1562 NInoSetSparseDisabled(NTFS_I(ino)); 1563 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */ 1564 i_size = i_size_read(ino); 1565 if (i_size <= 0 || i_size > 0x7fffffff) 1566 goto iput_failed; 1567 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size); 1568 if (!vol->attrdef) 1569 goto iput_failed; 1570 index = 0; 1571 max_index = i_size >> PAGE_CACHE_SHIFT; 1572 size = PAGE_CACHE_SIZE; 1573 while (index < max_index) { 1574 /* Read the attrdef table and copy it into the linear buffer. */ 1575 read_partial_attrdef_page: 1576 page = ntfs_map_page(ino->i_mapping, index); 1577 if (IS_ERR(page)) 1578 goto free_iput_failed; 1579 memcpy((u8*)vol->attrdef + (index++ << PAGE_CACHE_SHIFT), 1580 page_address(page), size); 1581 ntfs_unmap_page(page); 1582 }; 1583 if (size == PAGE_CACHE_SIZE) { 1584 size = i_size & ~PAGE_CACHE_MASK; 1585 if (size) 1586 goto read_partial_attrdef_page; 1587 } 1588 vol->attrdef_size = i_size; 1589 ntfs_debug("Read %llu bytes from $AttrDef.", i_size); 1590 iput(ino); 1591 return TRUE; 1592 free_iput_failed: 1593 ntfs_free(vol->attrdef); 1594 vol->attrdef = NULL; 1595 iput_failed: 1596 iput(ino); 1597 failed: 1598 ntfs_error(sb, "Failed to initialize attribute definition table."); 1599 return FALSE; 1600 } 1601 1602 #endif /* NTFS_RW */ 1603 1604 /** 1605 * load_and_init_upcase - load the upcase table for an ntfs volume 1606 * @vol: ntfs super block describing device whose upcase to load 1607 * 1608 * Return TRUE on success or FALSE on error. 1609 */ 1610 static BOOL load_and_init_upcase(ntfs_volume *vol) 1611 { 1612 loff_t i_size; 1613 struct super_block *sb = vol->sb; 1614 struct inode *ino; 1615 struct page *page; 1616 pgoff_t index, max_index; 1617 unsigned int size; 1618 int i, max; 1619 1620 ntfs_debug("Entering."); 1621 /* Read upcase table and setup vol->upcase and vol->upcase_len. */ 1622 ino = ntfs_iget(sb, FILE_UpCase); 1623 if (IS_ERR(ino) || is_bad_inode(ino)) { 1624 if (!IS_ERR(ino)) 1625 iput(ino); 1626 goto upcase_failed; 1627 } 1628 /* 1629 * The upcase size must not be above 64k Unicode characters, must not 1630 * be zero and must be a multiple of sizeof(ntfschar). 1631 */ 1632 i_size = i_size_read(ino); 1633 if (!i_size || i_size & (sizeof(ntfschar) - 1) || 1634 i_size > 64ULL * 1024 * sizeof(ntfschar)) 1635 goto iput_upcase_failed; 1636 vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size); 1637 if (!vol->upcase) 1638 goto iput_upcase_failed; 1639 index = 0; 1640 max_index = i_size >> PAGE_CACHE_SHIFT; 1641 size = PAGE_CACHE_SIZE; 1642 while (index < max_index) { 1643 /* Read the upcase table and copy it into the linear buffer. */ 1644 read_partial_upcase_page: 1645 page = ntfs_map_page(ino->i_mapping, index); 1646 if (IS_ERR(page)) 1647 goto iput_upcase_failed; 1648 memcpy((char*)vol->upcase + (index++ << PAGE_CACHE_SHIFT), 1649 page_address(page), size); 1650 ntfs_unmap_page(page); 1651 }; 1652 if (size == PAGE_CACHE_SIZE) { 1653 size = i_size & ~PAGE_CACHE_MASK; 1654 if (size) 1655 goto read_partial_upcase_page; 1656 } 1657 vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS; 1658 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).", 1659 i_size, 64 * 1024 * sizeof(ntfschar)); 1660 iput(ino); 1661 down(&ntfs_lock); 1662 if (!default_upcase) { 1663 ntfs_debug("Using volume specified $UpCase since default is " 1664 "not present."); 1665 up(&ntfs_lock); 1666 return TRUE; 1667 } 1668 max = default_upcase_len; 1669 if (max > vol->upcase_len) 1670 max = vol->upcase_len; 1671 for (i = 0; i < max; i++) 1672 if (vol->upcase[i] != default_upcase[i]) 1673 break; 1674 if (i == max) { 1675 ntfs_free(vol->upcase); 1676 vol->upcase = default_upcase; 1677 vol->upcase_len = max; 1678 ntfs_nr_upcase_users++; 1679 up(&ntfs_lock); 1680 ntfs_debug("Volume specified $UpCase matches default. Using " 1681 "default."); 1682 return TRUE; 1683 } 1684 up(&ntfs_lock); 1685 ntfs_debug("Using volume specified $UpCase since it does not match " 1686 "the default."); 1687 return TRUE; 1688 iput_upcase_failed: 1689 iput(ino); 1690 ntfs_free(vol->upcase); 1691 vol->upcase = NULL; 1692 upcase_failed: 1693 down(&ntfs_lock); 1694 if (default_upcase) { 1695 vol->upcase = default_upcase; 1696 vol->upcase_len = default_upcase_len; 1697 ntfs_nr_upcase_users++; 1698 up(&ntfs_lock); 1699 ntfs_error(sb, "Failed to load $UpCase from the volume. Using " 1700 "default."); 1701 return TRUE; 1702 } 1703 up(&ntfs_lock); 1704 ntfs_error(sb, "Failed to initialize upcase table."); 1705 return FALSE; 1706 } 1707 1708 /** 1709 * load_system_files - open the system files using normal functions 1710 * @vol: ntfs super block describing device whose system files to load 1711 * 1712 * Open the system files with normal access functions and complete setting up 1713 * the ntfs super block @vol. 1714 * 1715 * Return TRUE on success or FALSE on error. 1716 */ 1717 static BOOL load_system_files(ntfs_volume *vol) 1718 { 1719 struct super_block *sb = vol->sb; 1720 MFT_RECORD *m; 1721 VOLUME_INFORMATION *vi; 1722 ntfs_attr_search_ctx *ctx; 1723 #ifdef NTFS_RW 1724 RESTART_PAGE_HEADER *rp; 1725 int err; 1726 #endif /* NTFS_RW */ 1727 1728 ntfs_debug("Entering."); 1729 #ifdef NTFS_RW 1730 /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */ 1731 if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) { 1732 static const char *es1 = "Failed to load $MFTMirr"; 1733 static const char *es2 = "$MFTMirr does not match $MFT"; 1734 static const char *es3 = ". Run ntfsfix and/or chkdsk."; 1735 1736 /* If a read-write mount, convert it to a read-only mount. */ 1737 if (!(sb->s_flags & MS_RDONLY)) { 1738 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO | 1739 ON_ERRORS_CONTINUE))) { 1740 ntfs_error(sb, "%s and neither on_errors=" 1741 "continue nor on_errors=" 1742 "remount-ro was specified%s", 1743 !vol->mftmirr_ino ? es1 : es2, 1744 es3); 1745 goto iput_mirr_err_out; 1746 } 1747 sb->s_flags |= MS_RDONLY; 1748 ntfs_error(sb, "%s. Mounting read-only%s", 1749 !vol->mftmirr_ino ? es1 : es2, es3); 1750 } else 1751 ntfs_warning(sb, "%s. Will not be able to remount " 1752 "read-write%s", 1753 !vol->mftmirr_ino ? es1 : es2, es3); 1754 /* This will prevent a read-write remount. */ 1755 NVolSetErrors(vol); 1756 } 1757 #endif /* NTFS_RW */ 1758 /* Get mft bitmap attribute inode. */ 1759 vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0); 1760 if (IS_ERR(vol->mftbmp_ino)) { 1761 ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute."); 1762 goto iput_mirr_err_out; 1763 } 1764 /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */ 1765 if (!load_and_init_upcase(vol)) 1766 goto iput_mftbmp_err_out; 1767 #ifdef NTFS_RW 1768 /* 1769 * Read attribute definitions table and setup @vol->attrdef and 1770 * @vol->attrdef_size. 1771 */ 1772 if (!load_and_init_attrdef(vol)) 1773 goto iput_upcase_err_out; 1774 #endif /* NTFS_RW */ 1775 /* 1776 * Get the cluster allocation bitmap inode and verify the size, no 1777 * need for any locking at this stage as we are already running 1778 * exclusively as we are mount in progress task. 1779 */ 1780 vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap); 1781 if (IS_ERR(vol->lcnbmp_ino) || is_bad_inode(vol->lcnbmp_ino)) { 1782 if (!IS_ERR(vol->lcnbmp_ino)) 1783 iput(vol->lcnbmp_ino); 1784 goto bitmap_failed; 1785 } 1786 NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino)); 1787 if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) { 1788 iput(vol->lcnbmp_ino); 1789 bitmap_failed: 1790 ntfs_error(sb, "Failed to load $Bitmap."); 1791 goto iput_attrdef_err_out; 1792 } 1793 /* 1794 * Get the volume inode and setup our cache of the volume flags and 1795 * version. 1796 */ 1797 vol->vol_ino = ntfs_iget(sb, FILE_Volume); 1798 if (IS_ERR(vol->vol_ino) || is_bad_inode(vol->vol_ino)) { 1799 if (!IS_ERR(vol->vol_ino)) 1800 iput(vol->vol_ino); 1801 volume_failed: 1802 ntfs_error(sb, "Failed to load $Volume."); 1803 goto iput_lcnbmp_err_out; 1804 } 1805 m = map_mft_record(NTFS_I(vol->vol_ino)); 1806 if (IS_ERR(m)) { 1807 iput_volume_failed: 1808 iput(vol->vol_ino); 1809 goto volume_failed; 1810 } 1811 if (!(ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m))) { 1812 ntfs_error(sb, "Failed to get attribute search context."); 1813 goto get_ctx_vol_failed; 1814 } 1815 if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0, 1816 ctx) || ctx->attr->non_resident || ctx->attr->flags) { 1817 err_put_vol: 1818 ntfs_attr_put_search_ctx(ctx); 1819 get_ctx_vol_failed: 1820 unmap_mft_record(NTFS_I(vol->vol_ino)); 1821 goto iput_volume_failed; 1822 } 1823 vi = (VOLUME_INFORMATION*)((char*)ctx->attr + 1824 le16_to_cpu(ctx->attr->data.resident.value_offset)); 1825 /* Some bounds checks. */ 1826 if ((u8*)vi < (u8*)ctx->attr || (u8*)vi + 1827 le32_to_cpu(ctx->attr->data.resident.value_length) > 1828 (u8*)ctx->attr + le32_to_cpu(ctx->attr->length)) 1829 goto err_put_vol; 1830 /* Copy the volume flags and version to the ntfs_volume structure. */ 1831 vol->vol_flags = vi->flags; 1832 vol->major_ver = vi->major_ver; 1833 vol->minor_ver = vi->minor_ver; 1834 ntfs_attr_put_search_ctx(ctx); 1835 unmap_mft_record(NTFS_I(vol->vol_ino)); 1836 printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver, 1837 vol->minor_ver); 1838 if (vol->major_ver < 3 && NVolSparseEnabled(vol)) { 1839 ntfs_warning(vol->sb, "Disabling sparse support due to NTFS " 1840 "volume version %i.%i (need at least version " 1841 "3.0).", vol->major_ver, vol->minor_ver); 1842 NVolClearSparseEnabled(vol); 1843 } 1844 #ifdef NTFS_RW 1845 /* Make sure that no unsupported volume flags are set. */ 1846 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) { 1847 static const char *es1a = "Volume is dirty"; 1848 static const char *es1b = "Volume has unsupported flags set"; 1849 static const char *es2 = ". Run chkdsk and mount in Windows."; 1850 const char *es1; 1851 1852 es1 = vol->vol_flags & VOLUME_IS_DIRTY ? es1a : es1b; 1853 /* If a read-write mount, convert it to a read-only mount. */ 1854 if (!(sb->s_flags & MS_RDONLY)) { 1855 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO | 1856 ON_ERRORS_CONTINUE))) { 1857 ntfs_error(sb, "%s and neither on_errors=" 1858 "continue nor on_errors=" 1859 "remount-ro was specified%s", 1860 es1, es2); 1861 goto iput_vol_err_out; 1862 } 1863 sb->s_flags |= MS_RDONLY; 1864 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 1865 } else 1866 ntfs_warning(sb, "%s. Will not be able to remount " 1867 "read-write%s", es1, es2); 1868 /* 1869 * Do not set NVolErrors() because ntfs_remount() re-checks the 1870 * flags which we need to do in case any flags have changed. 1871 */ 1872 } 1873 /* 1874 * Get the inode for the logfile, check it and determine if the volume 1875 * was shutdown cleanly. 1876 */ 1877 rp = NULL; 1878 if (!load_and_check_logfile(vol, &rp) || 1879 !ntfs_is_logfile_clean(vol->logfile_ino, rp)) { 1880 static const char *es1a = "Failed to load $LogFile"; 1881 static const char *es1b = "$LogFile is not clean"; 1882 static const char *es2 = ". Mount in Windows."; 1883 const char *es1; 1884 1885 es1 = !vol->logfile_ino ? es1a : es1b; 1886 /* If a read-write mount, convert it to a read-only mount. */ 1887 if (!(sb->s_flags & MS_RDONLY)) { 1888 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO | 1889 ON_ERRORS_CONTINUE))) { 1890 ntfs_error(sb, "%s and neither on_errors=" 1891 "continue nor on_errors=" 1892 "remount-ro was specified%s", 1893 es1, es2); 1894 if (vol->logfile_ino) { 1895 BUG_ON(!rp); 1896 ntfs_free(rp); 1897 } 1898 goto iput_logfile_err_out; 1899 } 1900 sb->s_flags |= MS_RDONLY; 1901 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 1902 } else 1903 ntfs_warning(sb, "%s. Will not be able to remount " 1904 "read-write%s", es1, es2); 1905 /* This will prevent a read-write remount. */ 1906 NVolSetErrors(vol); 1907 } 1908 ntfs_free(rp); 1909 #endif /* NTFS_RW */ 1910 /* Get the root directory inode so we can do path lookups. */ 1911 vol->root_ino = ntfs_iget(sb, FILE_root); 1912 if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) { 1913 if (!IS_ERR(vol->root_ino)) 1914 iput(vol->root_ino); 1915 ntfs_error(sb, "Failed to load root directory."); 1916 goto iput_logfile_err_out; 1917 } 1918 #ifdef NTFS_RW 1919 /* 1920 * Check if Windows is suspended to disk on the target volume. If it 1921 * is hibernated, we must not write *anything* to the disk so set 1922 * NVolErrors() without setting the dirty volume flag and mount 1923 * read-only. This will prevent read-write remounting and it will also 1924 * prevent all writes. 1925 */ 1926 err = check_windows_hibernation_status(vol); 1927 if (unlikely(err)) { 1928 static const char *es1a = "Failed to determine if Windows is " 1929 "hibernated"; 1930 static const char *es1b = "Windows is hibernated"; 1931 static const char *es2 = ". Run chkdsk."; 1932 const char *es1; 1933 1934 es1 = err < 0 ? es1a : es1b; 1935 /* If a read-write mount, convert it to a read-only mount. */ 1936 if (!(sb->s_flags & MS_RDONLY)) { 1937 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO | 1938 ON_ERRORS_CONTINUE))) { 1939 ntfs_error(sb, "%s and neither on_errors=" 1940 "continue nor on_errors=" 1941 "remount-ro was specified%s", 1942 es1, es2); 1943 goto iput_root_err_out; 1944 } 1945 sb->s_flags |= MS_RDONLY; 1946 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 1947 } else 1948 ntfs_warning(sb, "%s. Will not be able to remount " 1949 "read-write%s", es1, es2); 1950 /* This will prevent a read-write remount. */ 1951 NVolSetErrors(vol); 1952 } 1953 /* If (still) a read-write mount, mark the volume dirty. */ 1954 if (!(sb->s_flags & MS_RDONLY) && 1955 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) { 1956 static const char *es1 = "Failed to set dirty bit in volume " 1957 "information flags"; 1958 static const char *es2 = ". Run chkdsk."; 1959 1960 /* Convert to a read-only mount. */ 1961 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO | 1962 ON_ERRORS_CONTINUE))) { 1963 ntfs_error(sb, "%s and neither on_errors=continue nor " 1964 "on_errors=remount-ro was specified%s", 1965 es1, es2); 1966 goto iput_root_err_out; 1967 } 1968 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 1969 sb->s_flags |= MS_RDONLY; 1970 /* 1971 * Do not set NVolErrors() because ntfs_remount() might manage 1972 * to set the dirty flag in which case all would be well. 1973 */ 1974 } 1975 #if 0 1976 // TODO: Enable this code once we start modifying anything that is 1977 // different between NTFS 1.2 and 3.x... 1978 /* 1979 * If (still) a read-write mount, set the NT4 compatibility flag on 1980 * newer NTFS version volumes. 1981 */ 1982 if (!(sb->s_flags & MS_RDONLY) && (vol->major_ver > 1) && 1983 ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) { 1984 static const char *es1 = "Failed to set NT4 compatibility flag"; 1985 static const char *es2 = ". Run chkdsk."; 1986 1987 /* Convert to a read-only mount. */ 1988 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO | 1989 ON_ERRORS_CONTINUE))) { 1990 ntfs_error(sb, "%s and neither on_errors=continue nor " 1991 "on_errors=remount-ro was specified%s", 1992 es1, es2); 1993 goto iput_root_err_out; 1994 } 1995 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 1996 sb->s_flags |= MS_RDONLY; 1997 NVolSetErrors(vol); 1998 } 1999 #endif 2000 /* If (still) a read-write mount, empty the logfile. */ 2001 if (!(sb->s_flags & MS_RDONLY) && 2002 !ntfs_empty_logfile(vol->logfile_ino)) { 2003 static const char *es1 = "Failed to empty $LogFile"; 2004 static const char *es2 = ". Mount in Windows."; 2005 2006 /* Convert to a read-only mount. */ 2007 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO | 2008 ON_ERRORS_CONTINUE))) { 2009 ntfs_error(sb, "%s and neither on_errors=continue nor " 2010 "on_errors=remount-ro was specified%s", 2011 es1, es2); 2012 goto iput_root_err_out; 2013 } 2014 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 2015 sb->s_flags |= MS_RDONLY; 2016 NVolSetErrors(vol); 2017 } 2018 #endif /* NTFS_RW */ 2019 /* If on NTFS versions before 3.0, we are done. */ 2020 if (unlikely(vol->major_ver < 3)) 2021 return TRUE; 2022 /* NTFS 3.0+ specific initialization. */ 2023 /* Get the security descriptors inode. */ 2024 vol->secure_ino = ntfs_iget(sb, FILE_Secure); 2025 if (IS_ERR(vol->secure_ino) || is_bad_inode(vol->secure_ino)) { 2026 if (!IS_ERR(vol->secure_ino)) 2027 iput(vol->secure_ino); 2028 ntfs_error(sb, "Failed to load $Secure."); 2029 goto iput_root_err_out; 2030 } 2031 // TODO: Initialize security. 2032 /* Get the extended system files' directory inode. */ 2033 vol->extend_ino = ntfs_iget(sb, FILE_Extend); 2034 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) { 2035 if (!IS_ERR(vol->extend_ino)) 2036 iput(vol->extend_ino); 2037 ntfs_error(sb, "Failed to load $Extend."); 2038 goto iput_sec_err_out; 2039 } 2040 #ifdef NTFS_RW 2041 /* Find the quota file, load it if present, and set it up. */ 2042 if (!load_and_init_quota(vol)) { 2043 static const char *es1 = "Failed to load $Quota"; 2044 static const char *es2 = ". Run chkdsk."; 2045 2046 /* If a read-write mount, convert it to a read-only mount. */ 2047 if (!(sb->s_flags & MS_RDONLY)) { 2048 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO | 2049 ON_ERRORS_CONTINUE))) { 2050 ntfs_error(sb, "%s and neither on_errors=" 2051 "continue nor on_errors=" 2052 "remount-ro was specified%s", 2053 es1, es2); 2054 goto iput_quota_err_out; 2055 } 2056 sb->s_flags |= MS_RDONLY; 2057 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 2058 } else 2059 ntfs_warning(sb, "%s. Will not be able to remount " 2060 "read-write%s", es1, es2); 2061 /* This will prevent a read-write remount. */ 2062 NVolSetErrors(vol); 2063 } 2064 /* If (still) a read-write mount, mark the quotas out of date. */ 2065 if (!(sb->s_flags & MS_RDONLY) && 2066 !ntfs_mark_quotas_out_of_date(vol)) { 2067 static const char *es1 = "Failed to mark quotas out of date"; 2068 static const char *es2 = ". Run chkdsk."; 2069 2070 /* Convert to a read-only mount. */ 2071 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO | 2072 ON_ERRORS_CONTINUE))) { 2073 ntfs_error(sb, "%s and neither on_errors=continue nor " 2074 "on_errors=remount-ro was specified%s", 2075 es1, es2); 2076 goto iput_quota_err_out; 2077 } 2078 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 2079 sb->s_flags |= MS_RDONLY; 2080 NVolSetErrors(vol); 2081 } 2082 /* 2083 * Find the transaction log file ($UsnJrnl), load it if present, check 2084 * it, and set it up. 2085 */ 2086 if (!load_and_init_usnjrnl(vol)) { 2087 static const char *es1 = "Failed to load $UsnJrnl"; 2088 static const char *es2 = ". Run chkdsk."; 2089 2090 /* If a read-write mount, convert it to a read-only mount. */ 2091 if (!(sb->s_flags & MS_RDONLY)) { 2092 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO | 2093 ON_ERRORS_CONTINUE))) { 2094 ntfs_error(sb, "%s and neither on_errors=" 2095 "continue nor on_errors=" 2096 "remount-ro was specified%s", 2097 es1, es2); 2098 goto iput_usnjrnl_err_out; 2099 } 2100 sb->s_flags |= MS_RDONLY; 2101 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 2102 } else 2103 ntfs_warning(sb, "%s. Will not be able to remount " 2104 "read-write%s", es1, es2); 2105 /* This will prevent a read-write remount. */ 2106 NVolSetErrors(vol); 2107 } 2108 /* If (still) a read-write mount, stamp the transaction log. */ 2109 if (!(sb->s_flags & MS_RDONLY) && !ntfs_stamp_usnjrnl(vol)) { 2110 static const char *es1 = "Failed to stamp transaction log " 2111 "($UsnJrnl)"; 2112 static const char *es2 = ". Run chkdsk."; 2113 2114 /* Convert to a read-only mount. */ 2115 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO | 2116 ON_ERRORS_CONTINUE))) { 2117 ntfs_error(sb, "%s and neither on_errors=continue nor " 2118 "on_errors=remount-ro was specified%s", 2119 es1, es2); 2120 goto iput_usnjrnl_err_out; 2121 } 2122 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 2123 sb->s_flags |= MS_RDONLY; 2124 NVolSetErrors(vol); 2125 } 2126 #endif /* NTFS_RW */ 2127 return TRUE; 2128 #ifdef NTFS_RW 2129 iput_usnjrnl_err_out: 2130 if (vol->usnjrnl_j_ino) 2131 iput(vol->usnjrnl_j_ino); 2132 if (vol->usnjrnl_max_ino) 2133 iput(vol->usnjrnl_max_ino); 2134 if (vol->usnjrnl_ino) 2135 iput(vol->usnjrnl_ino); 2136 iput_quota_err_out: 2137 if (vol->quota_q_ino) 2138 iput(vol->quota_q_ino); 2139 if (vol->quota_ino) 2140 iput(vol->quota_ino); 2141 iput(vol->extend_ino); 2142 #endif /* NTFS_RW */ 2143 iput_sec_err_out: 2144 iput(vol->secure_ino); 2145 iput_root_err_out: 2146 iput(vol->root_ino); 2147 iput_logfile_err_out: 2148 #ifdef NTFS_RW 2149 if (vol->logfile_ino) 2150 iput(vol->logfile_ino); 2151 iput_vol_err_out: 2152 #endif /* NTFS_RW */ 2153 iput(vol->vol_ino); 2154 iput_lcnbmp_err_out: 2155 iput(vol->lcnbmp_ino); 2156 iput_attrdef_err_out: 2157 vol->attrdef_size = 0; 2158 if (vol->attrdef) { 2159 ntfs_free(vol->attrdef); 2160 vol->attrdef = NULL; 2161 } 2162 #ifdef NTFS_RW 2163 iput_upcase_err_out: 2164 #endif /* NTFS_RW */ 2165 vol->upcase_len = 0; 2166 down(&ntfs_lock); 2167 if (vol->upcase == default_upcase) { 2168 ntfs_nr_upcase_users--; 2169 vol->upcase = NULL; 2170 } 2171 up(&ntfs_lock); 2172 if (vol->upcase) { 2173 ntfs_free(vol->upcase); 2174 vol->upcase = NULL; 2175 } 2176 iput_mftbmp_err_out: 2177 iput(vol->mftbmp_ino); 2178 iput_mirr_err_out: 2179 #ifdef NTFS_RW 2180 if (vol->mftmirr_ino) 2181 iput(vol->mftmirr_ino); 2182 #endif /* NTFS_RW */ 2183 return FALSE; 2184 } 2185 2186 /** 2187 * ntfs_put_super - called by the vfs to unmount a volume 2188 * @sb: vfs superblock of volume to unmount 2189 * 2190 * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when 2191 * the volume is being unmounted (umount system call has been invoked) and it 2192 * releases all inodes and memory belonging to the NTFS specific part of the 2193 * super block. 2194 */ 2195 static void ntfs_put_super(struct super_block *sb) 2196 { 2197 ntfs_volume *vol = NTFS_SB(sb); 2198 2199 ntfs_debug("Entering."); 2200 #ifdef NTFS_RW 2201 /* 2202 * Commit all inodes while they are still open in case some of them 2203 * cause others to be dirtied. 2204 */ 2205 ntfs_commit_inode(vol->vol_ino); 2206 2207 /* NTFS 3.0+ specific. */ 2208 if (vol->major_ver >= 3) { 2209 if (vol->usnjrnl_j_ino) 2210 ntfs_commit_inode(vol->usnjrnl_j_ino); 2211 if (vol->usnjrnl_max_ino) 2212 ntfs_commit_inode(vol->usnjrnl_max_ino); 2213 if (vol->usnjrnl_ino) 2214 ntfs_commit_inode(vol->usnjrnl_ino); 2215 if (vol->quota_q_ino) 2216 ntfs_commit_inode(vol->quota_q_ino); 2217 if (vol->quota_ino) 2218 ntfs_commit_inode(vol->quota_ino); 2219 if (vol->extend_ino) 2220 ntfs_commit_inode(vol->extend_ino); 2221 if (vol->secure_ino) 2222 ntfs_commit_inode(vol->secure_ino); 2223 } 2224 2225 ntfs_commit_inode(vol->root_ino); 2226 2227 down_write(&vol->lcnbmp_lock); 2228 ntfs_commit_inode(vol->lcnbmp_ino); 2229 up_write(&vol->lcnbmp_lock); 2230 2231 down_write(&vol->mftbmp_lock); 2232 ntfs_commit_inode(vol->mftbmp_ino); 2233 up_write(&vol->mftbmp_lock); 2234 2235 if (vol->logfile_ino) 2236 ntfs_commit_inode(vol->logfile_ino); 2237 2238 if (vol->mftmirr_ino) 2239 ntfs_commit_inode(vol->mftmirr_ino); 2240 ntfs_commit_inode(vol->mft_ino); 2241 2242 /* 2243 * If a read-write mount and no volume errors have occured, mark the 2244 * volume clean. Also, re-commit all affected inodes. 2245 */ 2246 if (!(sb->s_flags & MS_RDONLY)) { 2247 if (!NVolErrors(vol)) { 2248 if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY)) 2249 ntfs_warning(sb, "Failed to clear dirty bit " 2250 "in volume information " 2251 "flags. Run chkdsk."); 2252 ntfs_commit_inode(vol->vol_ino); 2253 ntfs_commit_inode(vol->root_ino); 2254 if (vol->mftmirr_ino) 2255 ntfs_commit_inode(vol->mftmirr_ino); 2256 ntfs_commit_inode(vol->mft_ino); 2257 } else { 2258 ntfs_warning(sb, "Volume has errors. Leaving volume " 2259 "marked dirty. Run chkdsk."); 2260 } 2261 } 2262 #endif /* NTFS_RW */ 2263 2264 iput(vol->vol_ino); 2265 vol->vol_ino = NULL; 2266 2267 /* NTFS 3.0+ specific clean up. */ 2268 if (vol->major_ver >= 3) { 2269 #ifdef NTFS_RW 2270 if (vol->usnjrnl_j_ino) { 2271 iput(vol->usnjrnl_j_ino); 2272 vol->usnjrnl_j_ino = NULL; 2273 } 2274 if (vol->usnjrnl_max_ino) { 2275 iput(vol->usnjrnl_max_ino); 2276 vol->usnjrnl_max_ino = NULL; 2277 } 2278 if (vol->usnjrnl_ino) { 2279 iput(vol->usnjrnl_ino); 2280 vol->usnjrnl_ino = NULL; 2281 } 2282 if (vol->quota_q_ino) { 2283 iput(vol->quota_q_ino); 2284 vol->quota_q_ino = NULL; 2285 } 2286 if (vol->quota_ino) { 2287 iput(vol->quota_ino); 2288 vol->quota_ino = NULL; 2289 } 2290 #endif /* NTFS_RW */ 2291 if (vol->extend_ino) { 2292 iput(vol->extend_ino); 2293 vol->extend_ino = NULL; 2294 } 2295 if (vol->secure_ino) { 2296 iput(vol->secure_ino); 2297 vol->secure_ino = NULL; 2298 } 2299 } 2300 2301 iput(vol->root_ino); 2302 vol->root_ino = NULL; 2303 2304 down_write(&vol->lcnbmp_lock); 2305 iput(vol->lcnbmp_ino); 2306 vol->lcnbmp_ino = NULL; 2307 up_write(&vol->lcnbmp_lock); 2308 2309 down_write(&vol->mftbmp_lock); 2310 iput(vol->mftbmp_ino); 2311 vol->mftbmp_ino = NULL; 2312 up_write(&vol->mftbmp_lock); 2313 2314 #ifdef NTFS_RW 2315 if (vol->logfile_ino) { 2316 iput(vol->logfile_ino); 2317 vol->logfile_ino = NULL; 2318 } 2319 if (vol->mftmirr_ino) { 2320 /* Re-commit the mft mirror and mft just in case. */ 2321 ntfs_commit_inode(vol->mftmirr_ino); 2322 ntfs_commit_inode(vol->mft_ino); 2323 iput(vol->mftmirr_ino); 2324 vol->mftmirr_ino = NULL; 2325 } 2326 /* 2327 * If any dirty inodes are left, throw away all mft data page cache 2328 * pages to allow a clean umount. This should never happen any more 2329 * due to mft.c::ntfs_mft_writepage() cleaning all the dirty pages as 2330 * the underlying mft records are written out and cleaned. If it does, 2331 * happen anyway, we want to know... 2332 */ 2333 ntfs_commit_inode(vol->mft_ino); 2334 write_inode_now(vol->mft_ino, 1); 2335 if (!list_empty(&sb->s_dirty)) { 2336 const char *s1, *s2; 2337 2338 mutex_lock(&vol->mft_ino->i_mutex); 2339 truncate_inode_pages(vol->mft_ino->i_mapping, 0); 2340 mutex_unlock(&vol->mft_ino->i_mutex); 2341 write_inode_now(vol->mft_ino, 1); 2342 if (!list_empty(&sb->s_dirty)) { 2343 static const char *_s1 = "inodes"; 2344 static const char *_s2 = ""; 2345 s1 = _s1; 2346 s2 = _s2; 2347 } else { 2348 static const char *_s1 = "mft pages"; 2349 static const char *_s2 = "They have been thrown " 2350 "away. "; 2351 s1 = _s1; 2352 s2 = _s2; 2353 } 2354 ntfs_error(sb, "Dirty %s found at umount time. %sYou should " 2355 "run chkdsk. Please email " 2356 "linux-ntfs-dev@lists.sourceforge.net and say " 2357 "that you saw this message. Thank you.", s1, 2358 s2); 2359 } 2360 #endif /* NTFS_RW */ 2361 2362 iput(vol->mft_ino); 2363 vol->mft_ino = NULL; 2364 2365 /* Throw away the table of attribute definitions. */ 2366 vol->attrdef_size = 0; 2367 if (vol->attrdef) { 2368 ntfs_free(vol->attrdef); 2369 vol->attrdef = NULL; 2370 } 2371 vol->upcase_len = 0; 2372 /* 2373 * Destroy the global default upcase table if necessary. Also decrease 2374 * the number of upcase users if we are a user. 2375 */ 2376 down(&ntfs_lock); 2377 if (vol->upcase == default_upcase) { 2378 ntfs_nr_upcase_users--; 2379 vol->upcase = NULL; 2380 } 2381 if (!ntfs_nr_upcase_users && default_upcase) { 2382 ntfs_free(default_upcase); 2383 default_upcase = NULL; 2384 } 2385 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users) 2386 free_compression_buffers(); 2387 up(&ntfs_lock); 2388 if (vol->upcase) { 2389 ntfs_free(vol->upcase); 2390 vol->upcase = NULL; 2391 } 2392 if (vol->nls_map) { 2393 unload_nls(vol->nls_map); 2394 vol->nls_map = NULL; 2395 } 2396 sb->s_fs_info = NULL; 2397 kfree(vol); 2398 return; 2399 } 2400 2401 /** 2402 * get_nr_free_clusters - return the number of free clusters on a volume 2403 * @vol: ntfs volume for which to obtain free cluster count 2404 * 2405 * Calculate the number of free clusters on the mounted NTFS volume @vol. We 2406 * actually calculate the number of clusters in use instead because this 2407 * allows us to not care about partial pages as these will be just zero filled 2408 * and hence not be counted as allocated clusters. 2409 * 2410 * The only particularity is that clusters beyond the end of the logical ntfs 2411 * volume will be marked as allocated to prevent errors which means we have to 2412 * discount those at the end. This is important as the cluster bitmap always 2413 * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside 2414 * the logical volume and marked in use when they are not as they do not exist. 2415 * 2416 * If any pages cannot be read we assume all clusters in the erroring pages are 2417 * in use. This means we return an underestimate on errors which is better than 2418 * an overestimate. 2419 */ 2420 static s64 get_nr_free_clusters(ntfs_volume *vol) 2421 { 2422 s64 nr_free = vol->nr_clusters; 2423 u32 *kaddr; 2424 struct address_space *mapping = vol->lcnbmp_ino->i_mapping; 2425 filler_t *readpage = (filler_t*)mapping->a_ops->readpage; 2426 struct page *page; 2427 pgoff_t index, max_index; 2428 2429 ntfs_debug("Entering."); 2430 /* Serialize accesses to the cluster bitmap. */ 2431 down_read(&vol->lcnbmp_lock); 2432 /* 2433 * Convert the number of bits into bytes rounded up, then convert into 2434 * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one 2435 * full and one partial page max_index = 2. 2436 */ 2437 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> 2438 PAGE_CACHE_SHIFT; 2439 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */ 2440 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.", 2441 max_index, PAGE_CACHE_SIZE / 4); 2442 for (index = 0; index < max_index; index++) { 2443 unsigned int i; 2444 /* 2445 * Read the page from page cache, getting it from backing store 2446 * if necessary, and increment the use count. 2447 */ 2448 page = read_cache_page(mapping, index, (filler_t*)readpage, 2449 NULL); 2450 /* Ignore pages which errored synchronously. */ 2451 if (IS_ERR(page)) { 2452 ntfs_debug("Sync read_cache_page() error. Skipping " 2453 "page (index 0x%lx).", index); 2454 nr_free -= PAGE_CACHE_SIZE * 8; 2455 continue; 2456 } 2457 wait_on_page_locked(page); 2458 /* Ignore pages which errored asynchronously. */ 2459 if (!PageUptodate(page)) { 2460 ntfs_debug("Async read_cache_page() error. Skipping " 2461 "page (index 0x%lx).", index); 2462 page_cache_release(page); 2463 nr_free -= PAGE_CACHE_SIZE * 8; 2464 continue; 2465 } 2466 kaddr = (u32*)kmap_atomic(page, KM_USER0); 2467 /* 2468 * For each 4 bytes, subtract the number of set bits. If this 2469 * is the last page and it is partial we don't really care as 2470 * it just means we do a little extra work but it won't affect 2471 * the result as all out of range bytes are set to zero by 2472 * ntfs_readpage(). 2473 */ 2474 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++) 2475 nr_free -= (s64)hweight32(kaddr[i]); 2476 kunmap_atomic(kaddr, KM_USER0); 2477 page_cache_release(page); 2478 } 2479 ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1); 2480 /* 2481 * Fixup for eventual bits outside logical ntfs volume (see function 2482 * description above). 2483 */ 2484 if (vol->nr_clusters & 63) 2485 nr_free += 64 - (vol->nr_clusters & 63); 2486 up_read(&vol->lcnbmp_lock); 2487 /* If errors occured we may well have gone below zero, fix this. */ 2488 if (nr_free < 0) 2489 nr_free = 0; 2490 ntfs_debug("Exiting."); 2491 return nr_free; 2492 } 2493 2494 /** 2495 * __get_nr_free_mft_records - return the number of free inodes on a volume 2496 * @vol: ntfs volume for which to obtain free inode count 2497 * @nr_free: number of mft records in filesystem 2498 * @max_index: maximum number of pages containing set bits 2499 * 2500 * Calculate the number of free mft records (inodes) on the mounted NTFS 2501 * volume @vol. We actually calculate the number of mft records in use instead 2502 * because this allows us to not care about partial pages as these will be just 2503 * zero filled and hence not be counted as allocated mft record. 2504 * 2505 * If any pages cannot be read we assume all mft records in the erroring pages 2506 * are in use. This means we return an underestimate on errors which is better 2507 * than an overestimate. 2508 * 2509 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing. 2510 */ 2511 static unsigned long __get_nr_free_mft_records(ntfs_volume *vol, 2512 s64 nr_free, const pgoff_t max_index) 2513 { 2514 u32 *kaddr; 2515 struct address_space *mapping = vol->mftbmp_ino->i_mapping; 2516 filler_t *readpage = (filler_t*)mapping->a_ops->readpage; 2517 struct page *page; 2518 pgoff_t index; 2519 2520 ntfs_debug("Entering."); 2521 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */ 2522 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = " 2523 "0x%lx.", max_index, PAGE_CACHE_SIZE / 4); 2524 for (index = 0; index < max_index; index++) { 2525 unsigned int i; 2526 /* 2527 * Read the page from page cache, getting it from backing store 2528 * if necessary, and increment the use count. 2529 */ 2530 page = read_cache_page(mapping, index, (filler_t*)readpage, 2531 NULL); 2532 /* Ignore pages which errored synchronously. */ 2533 if (IS_ERR(page)) { 2534 ntfs_debug("Sync read_cache_page() error. Skipping " 2535 "page (index 0x%lx).", index); 2536 nr_free -= PAGE_CACHE_SIZE * 8; 2537 continue; 2538 } 2539 wait_on_page_locked(page); 2540 /* Ignore pages which errored asynchronously. */ 2541 if (!PageUptodate(page)) { 2542 ntfs_debug("Async read_cache_page() error. Skipping " 2543 "page (index 0x%lx).", index); 2544 page_cache_release(page); 2545 nr_free -= PAGE_CACHE_SIZE * 8; 2546 continue; 2547 } 2548 kaddr = (u32*)kmap_atomic(page, KM_USER0); 2549 /* 2550 * For each 4 bytes, subtract the number of set bits. If this 2551 * is the last page and it is partial we don't really care as 2552 * it just means we do a little extra work but it won't affect 2553 * the result as all out of range bytes are set to zero by 2554 * ntfs_readpage(). 2555 */ 2556 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++) 2557 nr_free -= (s64)hweight32(kaddr[i]); 2558 kunmap_atomic(kaddr, KM_USER0); 2559 page_cache_release(page); 2560 } 2561 ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.", 2562 index - 1); 2563 /* If errors occured we may well have gone below zero, fix this. */ 2564 if (nr_free < 0) 2565 nr_free = 0; 2566 ntfs_debug("Exiting."); 2567 return nr_free; 2568 } 2569 2570 /** 2571 * ntfs_statfs - return information about mounted NTFS volume 2572 * @sb: super block of mounted volume 2573 * @sfs: statfs structure in which to return the information 2574 * 2575 * Return information about the mounted NTFS volume @sb in the statfs structure 2576 * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is 2577 * called). We interpret the values to be correct of the moment in time at 2578 * which we are called. Most values are variable otherwise and this isn't just 2579 * the free values but the totals as well. For example we can increase the 2580 * total number of file nodes if we run out and we can keep doing this until 2581 * there is no more space on the volume left at all. 2582 * 2583 * Called from vfs_statfs which is used to handle the statfs, fstatfs, and 2584 * ustat system calls. 2585 * 2586 * Return 0 on success or -errno on error. 2587 */ 2588 static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs) 2589 { 2590 s64 size; 2591 ntfs_volume *vol = NTFS_SB(sb); 2592 ntfs_inode *mft_ni = NTFS_I(vol->mft_ino); 2593 pgoff_t max_index; 2594 unsigned long flags; 2595 2596 ntfs_debug("Entering."); 2597 /* Type of filesystem. */ 2598 sfs->f_type = NTFS_SB_MAGIC; 2599 /* Optimal transfer block size. */ 2600 sfs->f_bsize = PAGE_CACHE_SIZE; 2601 /* 2602 * Total data blocks in filesystem in units of f_bsize and since 2603 * inodes are also stored in data blocs ($MFT is a file) this is just 2604 * the total clusters. 2605 */ 2606 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >> 2607 PAGE_CACHE_SHIFT; 2608 /* Free data blocks in filesystem in units of f_bsize. */ 2609 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >> 2610 PAGE_CACHE_SHIFT; 2611 if (size < 0LL) 2612 size = 0LL; 2613 /* Free blocks avail to non-superuser, same as above on NTFS. */ 2614 sfs->f_bavail = sfs->f_bfree = size; 2615 /* Serialize accesses to the inode bitmap. */ 2616 down_read(&vol->mftbmp_lock); 2617 read_lock_irqsave(&mft_ni->size_lock, flags); 2618 size = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits; 2619 /* 2620 * Convert the maximum number of set bits into bytes rounded up, then 2621 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we 2622 * have one full and one partial page max_index = 2. 2623 */ 2624 max_index = ((((mft_ni->initialized_size >> vol->mft_record_size_bits) 2625 + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 2626 read_unlock_irqrestore(&mft_ni->size_lock, flags); 2627 /* Number of inodes in filesystem (at this point in time). */ 2628 sfs->f_files = size; 2629 /* Free inodes in fs (based on current total count). */ 2630 sfs->f_ffree = __get_nr_free_mft_records(vol, size, max_index); 2631 up_read(&vol->mftbmp_lock); 2632 /* 2633 * File system id. This is extremely *nix flavour dependent and even 2634 * within Linux itself all fs do their own thing. I interpret this to 2635 * mean a unique id associated with the mounted fs and not the id 2636 * associated with the filesystem driver, the latter is already given 2637 * by the filesystem type in sfs->f_type. Thus we use the 64-bit 2638 * volume serial number splitting it into two 32-bit parts. We enter 2639 * the least significant 32-bits in f_fsid[0] and the most significant 2640 * 32-bits in f_fsid[1]. 2641 */ 2642 sfs->f_fsid.val[0] = vol->serial_no & 0xffffffff; 2643 sfs->f_fsid.val[1] = (vol->serial_no >> 32) & 0xffffffff; 2644 /* Maximum length of filenames. */ 2645 sfs->f_namelen = NTFS_MAX_NAME_LEN; 2646 return 0; 2647 } 2648 2649 /** 2650 * The complete super operations. 2651 */ 2652 static struct super_operations ntfs_sops = { 2653 .alloc_inode = ntfs_alloc_big_inode, /* VFS: Allocate new inode. */ 2654 .destroy_inode = ntfs_destroy_big_inode, /* VFS: Deallocate inode. */ 2655 .put_inode = ntfs_put_inode, /* VFS: Called just before 2656 the inode reference count 2657 is decreased. */ 2658 #ifdef NTFS_RW 2659 //.dirty_inode = NULL, /* VFS: Called from 2660 // __mark_inode_dirty(). */ 2661 .write_inode = ntfs_write_inode, /* VFS: Write dirty inode to 2662 disk. */ 2663 //.drop_inode = NULL, /* VFS: Called just after the 2664 // inode reference count has 2665 // been decreased to zero. 2666 // NOTE: The inode lock is 2667 // held. See fs/inode.c:: 2668 // generic_drop_inode(). */ 2669 //.delete_inode = NULL, /* VFS: Delete inode from disk. 2670 // Called when i_count becomes 2671 // 0 and i_nlink is also 0. */ 2672 //.write_super = NULL, /* Flush dirty super block to 2673 // disk. */ 2674 //.sync_fs = NULL, /* ? */ 2675 //.write_super_lockfs = NULL, /* ? */ 2676 //.unlockfs = NULL, /* ? */ 2677 #endif /* NTFS_RW */ 2678 .put_super = ntfs_put_super, /* Syscall: umount. */ 2679 .statfs = ntfs_statfs, /* Syscall: statfs */ 2680 .remount_fs = ntfs_remount, /* Syscall: mount -o remount. */ 2681 .clear_inode = ntfs_clear_big_inode, /* VFS: Called when an inode is 2682 removed from memory. */ 2683 //.umount_begin = NULL, /* Forced umount. */ 2684 .show_options = ntfs_show_options, /* Show mount options in 2685 proc. */ 2686 }; 2687 2688 /** 2689 * ntfs_fill_super - mount an ntfs filesystem 2690 * @sb: super block of ntfs filesystem to mount 2691 * @opt: string containing the mount options 2692 * @silent: silence error output 2693 * 2694 * ntfs_fill_super() is called by the VFS to mount the device described by @sb 2695 * with the mount otions in @data with the NTFS filesystem. 2696 * 2697 * If @silent is true, remain silent even if errors are detected. This is used 2698 * during bootup, when the kernel tries to mount the root filesystem with all 2699 * registered filesystems one after the other until one succeeds. This implies 2700 * that all filesystems except the correct one will quite correctly and 2701 * expectedly return an error, but nobody wants to see error messages when in 2702 * fact this is what is supposed to happen. 2703 * 2704 * NOTE: @sb->s_flags contains the mount options flags. 2705 */ 2706 static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent) 2707 { 2708 ntfs_volume *vol; 2709 struct buffer_head *bh; 2710 struct inode *tmp_ino; 2711 int blocksize, result; 2712 2713 ntfs_debug("Entering."); 2714 #ifndef NTFS_RW 2715 sb->s_flags |= MS_RDONLY; 2716 #endif /* ! NTFS_RW */ 2717 /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */ 2718 sb->s_fs_info = kmalloc(sizeof(ntfs_volume), GFP_NOFS); 2719 vol = NTFS_SB(sb); 2720 if (!vol) { 2721 if (!silent) 2722 ntfs_error(sb, "Allocation of NTFS volume structure " 2723 "failed. Aborting mount..."); 2724 return -ENOMEM; 2725 } 2726 /* Initialize ntfs_volume structure. */ 2727 *vol = (ntfs_volume) { 2728 .sb = sb, 2729 /* 2730 * Default is group and other don't have any access to files or 2731 * directories while owner has full access. Further, files by 2732 * default are not executable but directories are of course 2733 * browseable. 2734 */ 2735 .fmask = 0177, 2736 .dmask = 0077, 2737 }; 2738 init_rwsem(&vol->mftbmp_lock); 2739 init_rwsem(&vol->lcnbmp_lock); 2740 2741 unlock_kernel(); 2742 2743 /* By default, enable sparse support. */ 2744 NVolSetSparseEnabled(vol); 2745 2746 /* Important to get the mount options dealt with now. */ 2747 if (!parse_options(vol, (char*)opt)) 2748 goto err_out_now; 2749 2750 /* We support sector sizes up to the PAGE_CACHE_SIZE. */ 2751 if (bdev_hardsect_size(sb->s_bdev) > PAGE_CACHE_SIZE) { 2752 if (!silent) 2753 ntfs_error(sb, "Device has unsupported sector size " 2754 "(%i). The maximum supported sector " 2755 "size on this architecture is %lu " 2756 "bytes.", 2757 bdev_hardsect_size(sb->s_bdev), 2758 PAGE_CACHE_SIZE); 2759 goto err_out_now; 2760 } 2761 /* 2762 * Setup the device access block size to NTFS_BLOCK_SIZE or the hard 2763 * sector size, whichever is bigger. 2764 */ 2765 blocksize = sb_min_blocksize(sb, NTFS_BLOCK_SIZE); 2766 if (blocksize < NTFS_BLOCK_SIZE) { 2767 if (!silent) 2768 ntfs_error(sb, "Unable to set device block size."); 2769 goto err_out_now; 2770 } 2771 BUG_ON(blocksize != sb->s_blocksize); 2772 ntfs_debug("Set device block size to %i bytes (block size bits %i).", 2773 blocksize, sb->s_blocksize_bits); 2774 /* Determine the size of the device in units of block_size bytes. */ 2775 if (!i_size_read(sb->s_bdev->bd_inode)) { 2776 if (!silent) 2777 ntfs_error(sb, "Unable to determine device size."); 2778 goto err_out_now; 2779 } 2780 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >> 2781 sb->s_blocksize_bits; 2782 /* Read the boot sector and return unlocked buffer head to it. */ 2783 if (!(bh = read_ntfs_boot_sector(sb, silent))) { 2784 if (!silent) 2785 ntfs_error(sb, "Not an NTFS volume."); 2786 goto err_out_now; 2787 } 2788 /* 2789 * Extract the data from the boot sector and setup the ntfs volume 2790 * using it. 2791 */ 2792 result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data); 2793 brelse(bh); 2794 if (!result) { 2795 if (!silent) 2796 ntfs_error(sb, "Unsupported NTFS filesystem."); 2797 goto err_out_now; 2798 } 2799 /* 2800 * If the boot sector indicates a sector size bigger than the current 2801 * device block size, switch the device block size to the sector size. 2802 * TODO: It may be possible to support this case even when the set 2803 * below fails, we would just be breaking up the i/o for each sector 2804 * into multiple blocks for i/o purposes but otherwise it should just 2805 * work. However it is safer to leave disabled until someone hits this 2806 * error message and then we can get them to try it without the setting 2807 * so we know for sure that it works. 2808 */ 2809 if (vol->sector_size > blocksize) { 2810 blocksize = sb_set_blocksize(sb, vol->sector_size); 2811 if (blocksize != vol->sector_size) { 2812 if (!silent) 2813 ntfs_error(sb, "Unable to set device block " 2814 "size to sector size (%i).", 2815 vol->sector_size); 2816 goto err_out_now; 2817 } 2818 BUG_ON(blocksize != sb->s_blocksize); 2819 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >> 2820 sb->s_blocksize_bits; 2821 ntfs_debug("Changed device block size to %i bytes (block size " 2822 "bits %i) to match volume sector size.", 2823 blocksize, sb->s_blocksize_bits); 2824 } 2825 /* Initialize the cluster and mft allocators. */ 2826 ntfs_setup_allocators(vol); 2827 /* Setup remaining fields in the super block. */ 2828 sb->s_magic = NTFS_SB_MAGIC; 2829 /* 2830 * Ntfs allows 63 bits for the file size, i.e. correct would be: 2831 * sb->s_maxbytes = ~0ULL >> 1; 2832 * But the kernel uses a long as the page cache page index which on 2833 * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel 2834 * defined to the maximum the page cache page index can cope with 2835 * without overflowing the index or to 2^63 - 1, whichever is smaller. 2836 */ 2837 sb->s_maxbytes = MAX_LFS_FILESIZE; 2838 /* Ntfs measures time in 100ns intervals. */ 2839 sb->s_time_gran = 100; 2840 /* 2841 * Now load the metadata required for the page cache and our address 2842 * space operations to function. We do this by setting up a specialised 2843 * read_inode method and then just calling the normal iget() to obtain 2844 * the inode for $MFT which is sufficient to allow our normal inode 2845 * operations and associated address space operations to function. 2846 */ 2847 sb->s_op = &ntfs_sops; 2848 tmp_ino = new_inode(sb); 2849 if (!tmp_ino) { 2850 if (!silent) 2851 ntfs_error(sb, "Failed to load essential metadata."); 2852 goto err_out_now; 2853 } 2854 tmp_ino->i_ino = FILE_MFT; 2855 insert_inode_hash(tmp_ino); 2856 if (ntfs_read_inode_mount(tmp_ino) < 0) { 2857 if (!silent) 2858 ntfs_error(sb, "Failed to load essential metadata."); 2859 goto iput_tmp_ino_err_out_now; 2860 } 2861 down(&ntfs_lock); 2862 /* 2863 * The current mount is a compression user if the cluster size is 2864 * less than or equal 4kiB. 2865 */ 2866 if (vol->cluster_size <= 4096 && !ntfs_nr_compression_users++) { 2867 result = allocate_compression_buffers(); 2868 if (result) { 2869 ntfs_error(NULL, "Failed to allocate buffers " 2870 "for compression engine."); 2871 ntfs_nr_compression_users--; 2872 up(&ntfs_lock); 2873 goto iput_tmp_ino_err_out_now; 2874 } 2875 } 2876 /* 2877 * Generate the global default upcase table if necessary. Also 2878 * temporarily increment the number of upcase users to avoid race 2879 * conditions with concurrent (u)mounts. 2880 */ 2881 if (!default_upcase) 2882 default_upcase = generate_default_upcase(); 2883 ntfs_nr_upcase_users++; 2884 up(&ntfs_lock); 2885 /* 2886 * From now on, ignore @silent parameter. If we fail below this line, 2887 * it will be due to a corrupt fs or a system error, so we report it. 2888 */ 2889 /* 2890 * Open the system files with normal access functions and complete 2891 * setting up the ntfs super block. 2892 */ 2893 if (!load_system_files(vol)) { 2894 ntfs_error(sb, "Failed to load system files."); 2895 goto unl_upcase_iput_tmp_ino_err_out_now; 2896 } 2897 if ((sb->s_root = d_alloc_root(vol->root_ino))) { 2898 /* We increment i_count simulating an ntfs_iget(). */ 2899 atomic_inc(&vol->root_ino->i_count); 2900 ntfs_debug("Exiting, status successful."); 2901 /* Release the default upcase if it has no users. */ 2902 down(&ntfs_lock); 2903 if (!--ntfs_nr_upcase_users && default_upcase) { 2904 ntfs_free(default_upcase); 2905 default_upcase = NULL; 2906 } 2907 up(&ntfs_lock); 2908 sb->s_export_op = &ntfs_export_ops; 2909 lock_kernel(); 2910 return 0; 2911 } 2912 ntfs_error(sb, "Failed to allocate root directory."); 2913 /* Clean up after the successful load_system_files() call from above. */ 2914 // TODO: Use ntfs_put_super() instead of repeating all this code... 2915 // FIXME: Should mark the volume clean as the error is most likely 2916 // -ENOMEM. 2917 iput(vol->vol_ino); 2918 vol->vol_ino = NULL; 2919 /* NTFS 3.0+ specific clean up. */ 2920 if (vol->major_ver >= 3) { 2921 #ifdef NTFS_RW 2922 if (vol->usnjrnl_j_ino) { 2923 iput(vol->usnjrnl_j_ino); 2924 vol->usnjrnl_j_ino = NULL; 2925 } 2926 if (vol->usnjrnl_max_ino) { 2927 iput(vol->usnjrnl_max_ino); 2928 vol->usnjrnl_max_ino = NULL; 2929 } 2930 if (vol->usnjrnl_ino) { 2931 iput(vol->usnjrnl_ino); 2932 vol->usnjrnl_ino = NULL; 2933 } 2934 if (vol->quota_q_ino) { 2935 iput(vol->quota_q_ino); 2936 vol->quota_q_ino = NULL; 2937 } 2938 if (vol->quota_ino) { 2939 iput(vol->quota_ino); 2940 vol->quota_ino = NULL; 2941 } 2942 #endif /* NTFS_RW */ 2943 if (vol->extend_ino) { 2944 iput(vol->extend_ino); 2945 vol->extend_ino = NULL; 2946 } 2947 if (vol->secure_ino) { 2948 iput(vol->secure_ino); 2949 vol->secure_ino = NULL; 2950 } 2951 } 2952 iput(vol->root_ino); 2953 vol->root_ino = NULL; 2954 iput(vol->lcnbmp_ino); 2955 vol->lcnbmp_ino = NULL; 2956 iput(vol->mftbmp_ino); 2957 vol->mftbmp_ino = NULL; 2958 #ifdef NTFS_RW 2959 if (vol->logfile_ino) { 2960 iput(vol->logfile_ino); 2961 vol->logfile_ino = NULL; 2962 } 2963 if (vol->mftmirr_ino) { 2964 iput(vol->mftmirr_ino); 2965 vol->mftmirr_ino = NULL; 2966 } 2967 #endif /* NTFS_RW */ 2968 /* Throw away the table of attribute definitions. */ 2969 vol->attrdef_size = 0; 2970 if (vol->attrdef) { 2971 ntfs_free(vol->attrdef); 2972 vol->attrdef = NULL; 2973 } 2974 vol->upcase_len = 0; 2975 down(&ntfs_lock); 2976 if (vol->upcase == default_upcase) { 2977 ntfs_nr_upcase_users--; 2978 vol->upcase = NULL; 2979 } 2980 up(&ntfs_lock); 2981 if (vol->upcase) { 2982 ntfs_free(vol->upcase); 2983 vol->upcase = NULL; 2984 } 2985 if (vol->nls_map) { 2986 unload_nls(vol->nls_map); 2987 vol->nls_map = NULL; 2988 } 2989 /* Error exit code path. */ 2990 unl_upcase_iput_tmp_ino_err_out_now: 2991 /* 2992 * Decrease the number of upcase users and destroy the global default 2993 * upcase table if necessary. 2994 */ 2995 down(&ntfs_lock); 2996 if (!--ntfs_nr_upcase_users && default_upcase) { 2997 ntfs_free(default_upcase); 2998 default_upcase = NULL; 2999 } 3000 if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users) 3001 free_compression_buffers(); 3002 up(&ntfs_lock); 3003 iput_tmp_ino_err_out_now: 3004 iput(tmp_ino); 3005 if (vol->mft_ino && vol->mft_ino != tmp_ino) 3006 iput(vol->mft_ino); 3007 vol->mft_ino = NULL; 3008 /* 3009 * This is needed to get ntfs_clear_extent_inode() called for each 3010 * inode we have ever called ntfs_iget()/iput() on, otherwise we A) 3011 * leak resources and B) a subsequent mount fails automatically due to 3012 * ntfs_iget() never calling down into our ntfs_read_locked_inode() 3013 * method again... FIXME: Do we need to do this twice now because of 3014 * attribute inodes? I think not, so leave as is for now... (AIA) 3015 */ 3016 if (invalidate_inodes(sb)) { 3017 ntfs_error(sb, "Busy inodes left. This is most likely a NTFS " 3018 "driver bug."); 3019 /* Copied from fs/super.c. I just love this message. (-; */ 3020 printk("NTFS: Busy inodes after umount. Self-destruct in 5 " 3021 "seconds. Have a nice day...\n"); 3022 } 3023 /* Errors at this stage are irrelevant. */ 3024 err_out_now: 3025 lock_kernel(); 3026 sb->s_fs_info = NULL; 3027 kfree(vol); 3028 ntfs_debug("Failed, returning -EINVAL."); 3029 return -EINVAL; 3030 } 3031 3032 /* 3033 * This is a slab cache to optimize allocations and deallocations of Unicode 3034 * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN 3035 * (255) Unicode characters + a terminating NULL Unicode character. 3036 */ 3037 struct kmem_cache *ntfs_name_cache; 3038 3039 /* Slab caches for efficient allocation/deallocation of inodes. */ 3040 struct kmem_cache *ntfs_inode_cache; 3041 struct kmem_cache *ntfs_big_inode_cache; 3042 3043 /* Init once constructor for the inode slab cache. */ 3044 static void ntfs_big_inode_init_once(void *foo, struct kmem_cache *cachep, 3045 unsigned long flags) 3046 { 3047 ntfs_inode *ni = (ntfs_inode *)foo; 3048 3049 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == 3050 SLAB_CTOR_CONSTRUCTOR) 3051 inode_init_once(VFS_I(ni)); 3052 } 3053 3054 /* 3055 * Slab caches to optimize allocations and deallocations of attribute search 3056 * contexts and index contexts, respectively. 3057 */ 3058 struct kmem_cache *ntfs_attr_ctx_cache; 3059 struct kmem_cache *ntfs_index_ctx_cache; 3060 3061 /* Driver wide semaphore. */ 3062 DECLARE_MUTEX(ntfs_lock); 3063 3064 static struct super_block *ntfs_get_sb(struct file_system_type *fs_type, 3065 int flags, const char *dev_name, void *data) 3066 { 3067 return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super); 3068 } 3069 3070 static struct file_system_type ntfs_fs_type = { 3071 .owner = THIS_MODULE, 3072 .name = "ntfs", 3073 .get_sb = ntfs_get_sb, 3074 .kill_sb = kill_block_super, 3075 .fs_flags = FS_REQUIRES_DEV, 3076 }; 3077 3078 /* Stable names for the slab caches. */ 3079 static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache"; 3080 static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache"; 3081 static const char ntfs_name_cache_name[] = "ntfs_name_cache"; 3082 static const char ntfs_inode_cache_name[] = "ntfs_inode_cache"; 3083 static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache"; 3084 3085 static int __init init_ntfs_fs(void) 3086 { 3087 int err = 0; 3088 3089 /* This may be ugly but it results in pretty output so who cares. (-8 */ 3090 printk(KERN_INFO "NTFS driver " NTFS_VERSION " [Flags: R/" 3091 #ifdef NTFS_RW 3092 "W" 3093 #else 3094 "O" 3095 #endif 3096 #ifdef DEBUG 3097 " DEBUG" 3098 #endif 3099 #ifdef MODULE 3100 " MODULE" 3101 #endif 3102 "].\n"); 3103 3104 ntfs_debug("Debug messages are enabled."); 3105 3106 ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name, 3107 sizeof(ntfs_index_context), 0 /* offset */, 3108 SLAB_HWCACHE_ALIGN, NULL /* ctor */, NULL /* dtor */); 3109 if (!ntfs_index_ctx_cache) { 3110 printk(KERN_CRIT "NTFS: Failed to create %s!\n", 3111 ntfs_index_ctx_cache_name); 3112 goto ictx_err_out; 3113 } 3114 ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name, 3115 sizeof(ntfs_attr_search_ctx), 0 /* offset */, 3116 SLAB_HWCACHE_ALIGN, NULL /* ctor */, NULL /* dtor */); 3117 if (!ntfs_attr_ctx_cache) { 3118 printk(KERN_CRIT "NTFS: Failed to create %s!\n", 3119 ntfs_attr_ctx_cache_name); 3120 goto actx_err_out; 3121 } 3122 3123 ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name, 3124 (NTFS_MAX_NAME_LEN+1) * sizeof(ntfschar), 0, 3125 SLAB_HWCACHE_ALIGN, NULL, NULL); 3126 if (!ntfs_name_cache) { 3127 printk(KERN_CRIT "NTFS: Failed to create %s!\n", 3128 ntfs_name_cache_name); 3129 goto name_err_out; 3130 } 3131 3132 ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name, 3133 sizeof(ntfs_inode), 0, 3134 SLAB_RECLAIM_ACCOUNT, NULL, NULL); 3135 if (!ntfs_inode_cache) { 3136 printk(KERN_CRIT "NTFS: Failed to create %s!\n", 3137 ntfs_inode_cache_name); 3138 goto inode_err_out; 3139 } 3140 3141 ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name, 3142 sizeof(big_ntfs_inode), 0, 3143 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT, 3144 ntfs_big_inode_init_once, NULL); 3145 if (!ntfs_big_inode_cache) { 3146 printk(KERN_CRIT "NTFS: Failed to create %s!\n", 3147 ntfs_big_inode_cache_name); 3148 goto big_inode_err_out; 3149 } 3150 3151 /* Register the ntfs sysctls. */ 3152 err = ntfs_sysctl(1); 3153 if (err) { 3154 printk(KERN_CRIT "NTFS: Failed to register NTFS sysctls!\n"); 3155 goto sysctl_err_out; 3156 } 3157 3158 err = register_filesystem(&ntfs_fs_type); 3159 if (!err) { 3160 ntfs_debug("NTFS driver registered successfully."); 3161 return 0; /* Success! */ 3162 } 3163 printk(KERN_CRIT "NTFS: Failed to register NTFS filesystem driver!\n"); 3164 3165 sysctl_err_out: 3166 kmem_cache_destroy(ntfs_big_inode_cache); 3167 big_inode_err_out: 3168 kmem_cache_destroy(ntfs_inode_cache); 3169 inode_err_out: 3170 kmem_cache_destroy(ntfs_name_cache); 3171 name_err_out: 3172 kmem_cache_destroy(ntfs_attr_ctx_cache); 3173 actx_err_out: 3174 kmem_cache_destroy(ntfs_index_ctx_cache); 3175 ictx_err_out: 3176 if (!err) { 3177 printk(KERN_CRIT "NTFS: Aborting NTFS filesystem driver " 3178 "registration...\n"); 3179 err = -ENOMEM; 3180 } 3181 return err; 3182 } 3183 3184 static void __exit exit_ntfs_fs(void) 3185 { 3186 int err = 0; 3187 3188 ntfs_debug("Unregistering NTFS driver."); 3189 3190 unregister_filesystem(&ntfs_fs_type); 3191 3192 if (kmem_cache_destroy(ntfs_big_inode_cache) && (err = 1)) 3193 printk(KERN_CRIT "NTFS: Failed to destory %s.\n", 3194 ntfs_big_inode_cache_name); 3195 if (kmem_cache_destroy(ntfs_inode_cache) && (err = 1)) 3196 printk(KERN_CRIT "NTFS: Failed to destory %s.\n", 3197 ntfs_inode_cache_name); 3198 if (kmem_cache_destroy(ntfs_name_cache) && (err = 1)) 3199 printk(KERN_CRIT "NTFS: Failed to destory %s.\n", 3200 ntfs_name_cache_name); 3201 if (kmem_cache_destroy(ntfs_attr_ctx_cache) && (err = 1)) 3202 printk(KERN_CRIT "NTFS: Failed to destory %s.\n", 3203 ntfs_attr_ctx_cache_name); 3204 if (kmem_cache_destroy(ntfs_index_ctx_cache) && (err = 1)) 3205 printk(KERN_CRIT "NTFS: Failed to destory %s.\n", 3206 ntfs_index_ctx_cache_name); 3207 if (err) 3208 printk(KERN_CRIT "NTFS: This causes memory to leak! There is " 3209 "probably a BUG in the driver! Please report " 3210 "you saw this message to " 3211 "linux-ntfs-dev@lists.sourceforge.net\n"); 3212 /* Unregister the ntfs sysctls. */ 3213 ntfs_sysctl(0); 3214 } 3215 3216 MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>"); 3217 MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2005 Anton Altaparmakov"); 3218 MODULE_VERSION(NTFS_VERSION); 3219 MODULE_LICENSE("GPL"); 3220 #ifdef DEBUG 3221 module_param(debug_msgs, bool, 0); 3222 MODULE_PARM_DESC(debug_msgs, "Enable debug messages."); 3223 #endif 3224 3225 module_init(init_ntfs_fs) 3226 module_exit(exit_ntfs_fs) 3227