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