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