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