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