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