blob: eaf474c5f05ad0f7e110e06d8aae9f79e204e984
1 | /** |
2 | * ntfsfix - Part of the Linux-NTFS project. |
3 | * |
4 | * Copyright (c) 2000-2006 Anton Altaparmakov |
5 | * Copyright (c) 2002-2006 Szabolcs Szakacsits |
6 | * Copyright (c) 2007 Yura Pakhuchiy |
7 | * Copyright (c) 2011-2012 Jean-Pierre Andre |
8 | * |
9 | * This utility fixes some common NTFS problems, resets the NTFS journal file |
10 | * and schedules an NTFS consistency check for the first boot into Windows. |
11 | * |
12 | * Anton Altaparmakov <aia21@cantab.net> |
13 | * |
14 | * This program is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU General Public License as published by |
16 | * the Free Software Foundation; either version 2 of the License, or |
17 | * (at your option) any later version. |
18 | * |
19 | * This program is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU General Public License |
25 | * along with this program (in the main directory of the Linux-NTFS source |
26 | * in the file COPYING); if not, write to the Free Software Foundation, |
27 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
28 | */ |
29 | |
30 | /* |
31 | * WARNING: This program might not work on architectures which do not allow |
32 | * unaligned access. For those, the program would need to start using |
33 | * get/put_unaligned macros (#include <asm/unaligned.h>), but not doing it yet, |
34 | * since NTFS really mostly applies to ia32 only, which does allow unaligned |
35 | * accesses. We might not actually have a problem though, since the structs are |
36 | * defined as being packed so that might be enough for gcc to insert the |
37 | * correct code. |
38 | * |
39 | * If anyone using a non-little endian and/or an aligned access only CPU tries |
40 | * this program please let me know whether it works or not! |
41 | * |
42 | * Anton Altaparmakov <aia21@cantab.net> |
43 | */ |
44 | |
45 | #include "config.h" |
46 | |
47 | #ifdef HAVE_UNISTD_H |
48 | #include <unistd.h> |
49 | #endif |
50 | #ifdef HAVE_STDLIB_H |
51 | #include <stdlib.h> |
52 | #endif |
53 | #ifdef HAVE_STDIO_H |
54 | #include <stdio.h> |
55 | #endif |
56 | #ifdef HAVE_FCNTL_H |
57 | #include <fcntl.h> |
58 | #endif |
59 | #ifdef HAVE_ERRNO_H |
60 | #include <errno.h> |
61 | #endif |
62 | #ifdef HAVE_STRING_H |
63 | #include <string.h> |
64 | #endif |
65 | #ifdef HAVE_GETOPT_H |
66 | #include <getopt.h> |
67 | #endif |
68 | |
69 | #include "types.h" |
70 | #include "attrib.h" |
71 | #include "volume.h" |
72 | #include "bootsect.h" |
73 | #include "mft.h" |
74 | #include "device.h" |
75 | #include "logfile.h" |
76 | #include "runlist.h" |
77 | #include "mst.h" |
78 | #include "utils.h" |
79 | /* #include "version.h" */ |
80 | #include "logging.h" |
81 | #include "misc.h" |
82 | |
83 | #ifdef NO_NTFS_DEVICE_DEFAULT_IO_OPS |
84 | # error "No default device io operations! Cannot build ntfsfix. \ |
85 | You need to run ./configure without the --disable-default-device-io-ops \ |
86 | switch if you want to be able to build the NTFS utilities." |
87 | #endif |
88 | |
89 | static const char *EXEC_NAME = "ntfsfix"; |
90 | static const char OK[] = "OK\n"; |
91 | static const char FAILED[] = "FAILED\n"; |
92 | static const char FOUND[] = "FOUND\n"; |
93 | |
94 | #define DEFAULT_SECTOR_SIZE 512 |
95 | |
96 | static struct { |
97 | char *volume; |
98 | BOOL no_action; |
99 | BOOL clear_bad_sectors; |
100 | BOOL clear_dirty; |
101 | } opt; |
102 | |
103 | /* |
104 | * Definitions for fixing the self-located MFT bug |
105 | */ |
106 | |
107 | #define SELFLOC_LIMIT 16 |
108 | |
109 | struct MFT_SELF_LOCATED { |
110 | ntfs_volume *vol; |
111 | MFT_RECORD *mft0; |
112 | MFT_RECORD *mft1; |
113 | MFT_RECORD *mft2; |
114 | ATTR_LIST_ENTRY *attrlist; |
115 | ATTR_LIST_ENTRY *attrlist_to_ref1; |
116 | MFT_REF mft_ref0; |
117 | MFT_REF mft_ref1; |
118 | LCN attrlist_lcn; |
119 | BOOL attrlist_resident; |
120 | } ; |
121 | |
122 | /** |
123 | * usage |
124 | */ |
125 | __attribute__((noreturn)) |
126 | static void usage(void) |
127 | { |
128 | ntfs_log_info("%s v%s (libntfs-3g)\n" |
129 | "\n" |
130 | "Usage: %s [options] device\n" |
131 | " Attempt to fix an NTFS partition.\n" |
132 | "\n" |
133 | " -b, --clear-bad-sectors Clear the bad sector list\n" |
134 | " -d, --clear-dirty Clear the volume dirty flag\n" |
135 | " -h, --help Display this help\n" |
136 | " -n, --no-action Do not write anything\n" |
137 | " -V, --version Display version information\n" |
138 | "\n" |
139 | "For example: %s /dev/hda6\n\n", |
140 | EXEC_NAME, VERSION, EXEC_NAME, |
141 | EXEC_NAME); |
142 | ntfs_log_info("%s%s", ntfs_bugs, ntfs_home); |
143 | exit(1); |
144 | } |
145 | |
146 | /** |
147 | * version |
148 | */ |
149 | __attribute__((noreturn)) |
150 | static void version(void) |
151 | { |
152 | ntfs_log_info("%s v%s\n\n" |
153 | "Attempt to fix an NTFS partition.\n\n" |
154 | "Copyright (c) 2000-2006 Anton Altaparmakov\n" |
155 | "Copyright (c) 2002-2006 Szabolcs Szakacsits\n" |
156 | "Copyright (c) 2007 Yura Pakhuchiy\n\n" |
157 | "Copyright (c) 2011 Jean-Pierre Andre\n\n", |
158 | EXEC_NAME, VERSION); |
159 | ntfs_log_info("%s\n%s%s", ntfs_gpl, ntfs_bugs, ntfs_home); |
160 | exit(1); |
161 | } |
162 | |
163 | /** |
164 | * parse_options |
165 | */ |
166 | static void parse_options(int argc, char **argv) |
167 | { |
168 | int c; |
169 | static const char *sopt = "-bdhnV"; |
170 | static const struct option lopt[] = { |
171 | { "help", no_argument, NULL, 'h' }, |
172 | { "no-action", no_argument, NULL, 'n' }, |
173 | { "clear-bad-sectors", no_argument, NULL, 'b' }, |
174 | { "clear-dirty", no_argument, NULL, 'd' }, |
175 | { "version", no_argument, NULL, 'V' }, |
176 | { NULL, 0, NULL, 0 } |
177 | }; |
178 | |
179 | memset(&opt, 0, sizeof(opt)); |
180 | |
181 | while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { |
182 | switch (c) { |
183 | case 1: /* A non-option argument */ |
184 | if (!opt.volume) |
185 | opt.volume = argv[optind - 1]; |
186 | else { |
187 | ntfs_log_info("ERROR: Too many arguments.\n"); |
188 | usage(); |
189 | } |
190 | break; |
191 | case 'b': |
192 | opt.clear_bad_sectors = TRUE; |
193 | break; |
194 | case 'd': |
195 | opt.clear_dirty = TRUE; |
196 | break; |
197 | case 'n': |
198 | opt.no_action = TRUE; |
199 | break; |
200 | case 'h': |
201 | case '?': |
202 | usage(); |
203 | /* fall through */ |
204 | case 'V': |
205 | version(); |
206 | default: |
207 | ntfs_log_info("ERROR: Unknown option '%s'.\n", argv[optind - 1]); |
208 | usage(); |
209 | } |
210 | } |
211 | |
212 | if (opt.volume == NULL) { |
213 | ntfs_log_info("ERROR: You must specify a device.\n"); |
214 | usage(); |
215 | } |
216 | } |
217 | |
218 | /** |
219 | * OLD_ntfs_volume_set_flags |
220 | */ |
221 | static int OLD_ntfs_volume_set_flags(ntfs_volume *vol, const le16 flags) |
222 | { |
223 | MFT_RECORD *m = NULL; |
224 | ATTR_RECORD *a; |
225 | VOLUME_INFORMATION *c; |
226 | ntfs_attr_search_ctx *ctx; |
227 | int ret = -1; /* failure */ |
228 | |
229 | if (!vol) { |
230 | errno = EINVAL; |
231 | return -1; |
232 | } |
233 | if (ntfs_file_record_read(vol, FILE_Volume, &m, NULL)) { |
234 | ntfs_log_perror("Failed to read $Volume"); |
235 | return -1; |
236 | } |
237 | /* Sanity check */ |
238 | if (!(m->flags & MFT_RECORD_IN_USE)) { |
239 | ntfs_log_error("$Volume has been deleted. Cannot handle this " |
240 | "yet. Run chkdsk to fix this.\n"); |
241 | errno = EIO; |
242 | goto err_exit; |
243 | } |
244 | /* Get a pointer to the volume information attribute. */ |
245 | ctx = ntfs_attr_get_search_ctx(NULL, m); |
246 | if (!ctx) { |
247 | ntfs_log_debug("Failed to allocate attribute search " |
248 | "context.\n"); |
249 | goto err_exit; |
250 | } |
251 | if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, AT_UNNAMED, 0, |
252 | CASE_SENSITIVE, 0, NULL, 0, ctx)) { |
253 | ntfs_log_error("Attribute $VOLUME_INFORMATION was not found in " |
254 | "$Volume!\n"); |
255 | goto err_out; |
256 | } |
257 | a = ctx->attr; |
258 | /* Sanity check. */ |
259 | if (a->non_resident) { |
260 | ntfs_log_error("Attribute $VOLUME_INFORMATION must be resident " |
261 | "(and it isn't)!\n"); |
262 | errno = EIO; |
263 | goto err_out; |
264 | } |
265 | /* Get a pointer to the value of the attribute. */ |
266 | c = (VOLUME_INFORMATION*)(le16_to_cpu(a->value_offset) + (char*)a); |
267 | /* Sanity checks. */ |
268 | if ((char*)c + le32_to_cpu(a->value_length) > |
269 | (char*)m + le32_to_cpu(m->bytes_in_use) || |
270 | le16_to_cpu(a->value_offset) + |
271 | le32_to_cpu(a->value_length) > le32_to_cpu(a->length)) { |
272 | ntfs_log_error("Attribute $VOLUME_INFORMATION in $Volume is " |
273 | "corrupt!\n"); |
274 | errno = EIO; |
275 | goto err_out; |
276 | } |
277 | /* Set the volume flags. */ |
278 | vol->flags = c->flags = flags; |
279 | if (ntfs_mft_record_write(vol, FILE_Volume, m)) { |
280 | ntfs_log_perror("Error writing $Volume"); |
281 | goto err_out; |
282 | } |
283 | ret = 0; /* success */ |
284 | err_out: |
285 | ntfs_attr_put_search_ctx(ctx); |
286 | err_exit: |
287 | free(m); |
288 | return ret; |
289 | } |
290 | |
291 | /** |
292 | * set_dirty_flag |
293 | */ |
294 | static int set_dirty_flag(ntfs_volume *vol) |
295 | { |
296 | le16 flags; |
297 | |
298 | /* Porting note: We test for the current state of VOLUME_IS_DIRTY. This |
299 | * should actually be more appropriate than testing for NVolWasDirty. */ |
300 | if (vol->flags & VOLUME_IS_DIRTY) |
301 | return 0; |
302 | ntfs_log_info("Setting required flags on partition... "); |
303 | /* |
304 | * Set chkdsk flag, i.e. mark the partition dirty so chkdsk will run |
305 | * and fix it for us. |
306 | */ |
307 | flags = vol->flags | VOLUME_IS_DIRTY; |
308 | if (!opt.no_action && OLD_ntfs_volume_set_flags(vol, flags)) { |
309 | ntfs_log_info(FAILED); |
310 | ntfs_log_error("Error setting volume flags.\n"); |
311 | return -1; |
312 | } |
313 | vol->flags = flags; |
314 | |
315 | /* Porting note: libntfs-3g does not have the 'WasDirty' flag/property, |
316 | * and never touches the 'dirty' bit except when explicitly told to do |
317 | * so. Since we just wrote the VOLUME_IS_DIRTY bit to disk, and |
318 | * vol->flags is up-to-date, we can just ignore the NVolSetWasDirty |
319 | * statement. */ |
320 | /* NVolSetWasDirty(vol); */ |
321 | |
322 | ntfs_log_info(OK); |
323 | return 0; |
324 | } |
325 | |
326 | /** |
327 | * empty_journal |
328 | */ |
329 | static int empty_journal(ntfs_volume *vol) |
330 | { |
331 | if (NVolLogFileEmpty(vol)) |
332 | return 0; |
333 | ntfs_log_info("Going to empty the journal ($LogFile)... "); |
334 | if (ntfs_logfile_reset(vol)) { |
335 | ntfs_log_info(FAILED); |
336 | ntfs_log_perror("Failed to reset $LogFile"); |
337 | return -1; |
338 | } |
339 | ntfs_log_info(OK); |
340 | return 0; |
341 | } |
342 | |
343 | /** |
344 | * Clear the bad cluster marks (option) |
345 | */ |
346 | static int clear_badclus(ntfs_volume *vol) |
347 | { |
348 | static ntfschar badstream[] = { |
349 | const_cpu_to_le16('$'), const_cpu_to_le16('B'), |
350 | const_cpu_to_le16('a'), const_cpu_to_le16('d') |
351 | } ; |
352 | ntfs_inode *ni; |
353 | ntfs_attr *na; |
354 | BOOL ok; |
355 | |
356 | ok = FALSE; |
357 | ntfs_log_info("Going to un-mark the bad clusters ($BadClus)... "); |
358 | ni = ntfs_inode_open(vol, FILE_BadClus); |
359 | if (ni) { |
360 | na = ntfs_attr_open(ni, AT_DATA, badstream, 4); |
361 | /* |
362 | * chkdsk does not adjust the data size when |
363 | * moving clusters to $BadClus, so we have to |
364 | * check the runlist. |
365 | */ |
366 | if (na && !ntfs_attr_map_whole_runlist(na)) { |
367 | if (na->rl |
368 | && na->rl[0].length && na->rl[1].length) { |
369 | /* |
370 | * Truncate the stream to free all its clusters, |
371 | * (which requires setting the data size according |
372 | * to allocation), then reallocate a sparse stream |
373 | * to full size of volume and reset the data size. |
374 | */ |
375 | na->data_size = na->allocated_size; |
376 | na->initialized_size = na->allocated_size; |
377 | if (!ntfs_attr_truncate(na,0) |
378 | && !ntfs_attr_truncate(na,vol->nr_clusters |
379 | << vol->cluster_size_bits)) { |
380 | na->data_size = 0; |
381 | na->initialized_size = 0; |
382 | ni->flags |= FILE_ATTR_SPARSE_FILE; |
383 | NInoFileNameSetDirty(ni); |
384 | ok = TRUE; |
385 | } else { |
386 | ntfs_log_perror("Failed to un-mark the bad clusters"); |
387 | } |
388 | } else { |
389 | ntfs_log_info("No bad clusters..."); |
390 | ok = TRUE; |
391 | } |
392 | ntfs_attr_close(na); |
393 | } else { |
394 | ntfs_log_perror("Failed to open $BadClus::$Bad"); |
395 | } |
396 | ntfs_inode_close(ni); |
397 | } else { |
398 | ntfs_log_perror("Failed to open inode FILE_BadClus"); |
399 | } |
400 | if (ok) |
401 | ntfs_log_info(OK); |
402 | return (ok ? 0 : -1); |
403 | } |
404 | |
405 | /** |
406 | * fix_mftmirr |
407 | */ |
408 | static int fix_mftmirr(ntfs_volume *vol) |
409 | { |
410 | s64 l, br; |
411 | unsigned char *m, *m2; |
412 | int i, ret = -1; /* failure */ |
413 | BOOL done; |
414 | |
415 | ntfs_log_info("\nProcessing $MFT and $MFTMirr...\n"); |
416 | |
417 | /* Load data from $MFT and $MFTMirr and compare the contents. */ |
418 | m = (u8*)malloc(vol->mftmirr_size << vol->mft_record_size_bits); |
419 | if (!m) { |
420 | ntfs_log_perror("Failed to allocate memory"); |
421 | return -1; |
422 | } |
423 | m2 = (u8*)malloc(vol->mftmirr_size << vol->mft_record_size_bits); |
424 | if (!m2) { |
425 | ntfs_log_perror("Failed to allocate memory"); |
426 | free(m); |
427 | return -1; |
428 | } |
429 | |
430 | ntfs_log_info("Reading $MFT... "); |
431 | l = ntfs_attr_mst_pread(vol->mft_na, 0, vol->mftmirr_size, |
432 | vol->mft_record_size, m); |
433 | if (l != vol->mftmirr_size) { |
434 | ntfs_log_info(FAILED); |
435 | if (l != -1) |
436 | errno = EIO; |
437 | ntfs_log_perror("Failed to read $MFT"); |
438 | goto error_exit; |
439 | } |
440 | ntfs_log_info(OK); |
441 | |
442 | ntfs_log_info("Reading $MFTMirr... "); |
443 | l = ntfs_attr_mst_pread(vol->mftmirr_na, 0, vol->mftmirr_size, |
444 | vol->mft_record_size, m2); |
445 | if (l != vol->mftmirr_size) { |
446 | ntfs_log_info(FAILED); |
447 | if (l != -1) |
448 | errno = EIO; |
449 | ntfs_log_perror("Failed to read $MFTMirr"); |
450 | goto error_exit; |
451 | } |
452 | ntfs_log_info(OK); |
453 | |
454 | /* |
455 | * FIXME: Need to actually check the $MFTMirr for being real. Otherwise |
456 | * we might corrupt the partition if someone is experimenting with |
457 | * software RAID and the $MFTMirr is not actually in the position we |
458 | * expect it to be... )-: |
459 | * FIXME: We should emit a warning it $MFTMirr is damaged and ask |
460 | * user whether to recreate it from $MFT or whether to abort. - The |
461 | * warning needs to include the danger of software RAID arrays. |
462 | * Maybe we should go as far as to detect whether we are running on a |
463 | * MD disk and if yes then bomb out right at the start of the program? |
464 | */ |
465 | |
466 | ntfs_log_info("Comparing $MFTMirr to $MFT... "); |
467 | done = FALSE; |
468 | for (i = 0; i < vol->mftmirr_size; ++i) { |
469 | MFT_RECORD *mrec, *mrec2; |
470 | const char *ESTR[12] = { "$MFT", "$MFTMirr", "$LogFile", |
471 | "$Volume", "$AttrDef", "root directory", "$Bitmap", |
472 | "$Boot", "$BadClus", "$Secure", "$UpCase", "$Extend" }; |
473 | const char *s; |
474 | BOOL use_mirr; |
475 | |
476 | if (i < 12) |
477 | s = ESTR[i]; |
478 | else if (i < 16) |
479 | s = "system file"; |
480 | else |
481 | s = "mft record"; |
482 | |
483 | use_mirr = FALSE; |
484 | mrec = (MFT_RECORD*)(m + i * vol->mft_record_size); |
485 | if (mrec->flags & MFT_RECORD_IN_USE) { |
486 | if (ntfs_is_baad_record(mrec->magic)) { |
487 | ntfs_log_info(FAILED); |
488 | ntfs_log_error("$MFT error: Incomplete multi " |
489 | "sector transfer detected in " |
490 | "%s.\nCannot handle this yet. " |
491 | ")-:\n", s); |
492 | goto error_exit; |
493 | } |
494 | if (!ntfs_is_mft_record(mrec->magic)) { |
495 | ntfs_log_info(FAILED); |
496 | ntfs_log_error("$MFT error: Invalid mft " |
497 | "record for %s.\nCannot " |
498 | "handle this yet. )-:\n", s); |
499 | goto error_exit; |
500 | } |
501 | } |
502 | mrec2 = (MFT_RECORD*)(m2 + i * vol->mft_record_size); |
503 | if (mrec2->flags & MFT_RECORD_IN_USE) { |
504 | if (ntfs_is_baad_record(mrec2->magic)) { |
505 | ntfs_log_info(FAILED); |
506 | ntfs_log_error("$MFTMirr error: Incomplete " |
507 | "multi sector transfer " |
508 | "detected in %s.\n", s); |
509 | goto error_exit; |
510 | } |
511 | if (!ntfs_is_mft_record(mrec2->magic)) { |
512 | ntfs_log_info(FAILED); |
513 | ntfs_log_error("$MFTMirr error: Invalid mft " |
514 | "record for %s.\n", s); |
515 | goto error_exit; |
516 | } |
517 | /* $MFT is corrupt but $MFTMirr is ok, use $MFTMirr. */ |
518 | if (!(mrec->flags & MFT_RECORD_IN_USE) && |
519 | !ntfs_is_mft_record(mrec->magic)) |
520 | use_mirr = TRUE; |
521 | } |
522 | if (memcmp(mrec, mrec2, ntfs_mft_record_get_data_size(mrec))) { |
523 | if (!done) { |
524 | done = TRUE; |
525 | ntfs_log_info(FAILED); |
526 | } |
527 | ntfs_log_info("Correcting differences in $MFT%s " |
528 | "record %d...", use_mirr ? "" : "Mirr", |
529 | i); |
530 | br = ntfs_mft_record_write(vol, i, |
531 | use_mirr ? mrec2 : mrec); |
532 | if (br) { |
533 | ntfs_log_info(FAILED); |
534 | ntfs_log_perror("Error correcting $MFT%s", |
535 | use_mirr ? "" : "Mirr"); |
536 | goto error_exit; |
537 | } |
538 | ntfs_log_info(OK); |
539 | } |
540 | } |
541 | if (!done) |
542 | ntfs_log_info(OK); |
543 | ntfs_log_info("Processing of $MFT and $MFTMirr completed " |
544 | "successfully.\n"); |
545 | ret = 0; |
546 | error_exit: |
547 | free(m); |
548 | free(m2); |
549 | return ret; |
550 | } |
551 | |
552 | /* |
553 | * Rewrite the $UpCase file as default |
554 | * |
555 | * Returns 0 if could be written |
556 | */ |
557 | |
558 | static int rewrite_upcase(ntfs_volume *vol, ntfs_attr *na) |
559 | { |
560 | s64 l; |
561 | int res; |
562 | |
563 | /* writing the $UpCase may require bitmap updates */ |
564 | res = -1; |
565 | vol->lcnbmp_ni = ntfs_inode_open(vol, FILE_Bitmap); |
566 | if (!vol->lcnbmp_ni) { |
567 | ntfs_log_perror("Failed to open bitmap inode"); |
568 | } else { |
569 | vol->lcnbmp_na = ntfs_attr_open(vol->lcnbmp_ni, AT_DATA, |
570 | AT_UNNAMED, 0); |
571 | if (!vol->lcnbmp_na) { |
572 | ntfs_log_perror("Failed to open bitmap data attribute"); |
573 | } else { |
574 | /* minimal consistency check on the bitmap */ |
575 | if (((vol->lcnbmp_na->data_size << 3) |
576 | < vol->nr_clusters) |
577 | || ((vol->lcnbmp_na->data_size << 3) |
578 | >= (vol->nr_clusters << 1)) |
579 | || (vol->lcnbmp_na->data_size |
580 | > vol->lcnbmp_na->allocated_size)) { |
581 | ntfs_log_error("Corrupt cluster map size %lld" |
582 | " (allocated %lld minimum %lld)\n", |
583 | (long long)vol->lcnbmp_na->data_size, |
584 | (long long)vol->lcnbmp_na->allocated_size, |
585 | (long long)(vol->nr_clusters + 7) >> 3); |
586 | } else { |
587 | ntfs_log_info("Rewriting $UpCase file\n"); |
588 | l = ntfs_attr_pwrite(na, 0, vol->upcase_len*2, |
589 | vol->upcase); |
590 | if (l != vol->upcase_len*2) { |
591 | ntfs_log_error("Failed to rewrite $UpCase\n"); |
592 | } else { |
593 | ntfs_log_info("$UpCase has been set to default\n"); |
594 | res = 0; |
595 | } |
596 | } |
597 | ntfs_attr_close(vol->lcnbmp_na); |
598 | vol->lcnbmp_na = (ntfs_attr*)NULL; |
599 | } |
600 | ntfs_inode_close(vol->lcnbmp_ni); |
601 | vol->lcnbmp_ni = (ntfs_inode*)NULL; |
602 | } |
603 | return (res); |
604 | } |
605 | |
606 | /* |
607 | * Fix the $UpCase file |
608 | * |
609 | * Returns 0 if the table is valid or has been fixed |
610 | */ |
611 | |
612 | static int fix_upcase(ntfs_volume *vol) |
613 | { |
614 | ntfs_inode *ni; |
615 | ntfs_attr *na; |
616 | ntfschar *upcase; |
617 | s64 l; |
618 | u32 upcase_len; |
619 | u32 k; |
620 | int res; |
621 | |
622 | res = -1; |
623 | ni = (ntfs_inode*)NULL; |
624 | na = (ntfs_attr*)NULL; |
625 | /* Now load the upcase table from $UpCase. */ |
626 | ntfs_log_debug("Loading $UpCase...\n"); |
627 | ni = ntfs_inode_open(vol, FILE_UpCase); |
628 | if (!ni) { |
629 | ntfs_log_perror("Failed to open inode FILE_UpCase"); |
630 | goto error_exit; |
631 | } |
632 | /* Get an ntfs attribute for $UpCase/$DATA. */ |
633 | na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); |
634 | if (!na) { |
635 | ntfs_log_perror("Failed to open ntfs attribute"); |
636 | goto error_exit; |
637 | } |
638 | /* |
639 | * Note: Normally, the upcase table has a length equal to 65536 |
640 | * 2-byte Unicode characters but allow for different cases, so no |
641 | * checks done. Just check we don't overflow 32-bits worth of Unicode |
642 | * characters. |
643 | */ |
644 | if (na->data_size & ~0x1ffffffffULL) { |
645 | ntfs_log_error("Error: Upcase table is too big (max 32-bit " |
646 | "allowed).\n"); |
647 | errno = EINVAL; |
648 | goto error_exit; |
649 | } |
650 | upcase_len = na->data_size >> 1; |
651 | upcase = (ntfschar*)ntfs_malloc(na->data_size); |
652 | if (!upcase) |
653 | goto error_exit; |
654 | /* Read in the $DATA attribute value into the buffer. */ |
655 | l = ntfs_attr_pread(na, 0, na->data_size, upcase); |
656 | if (l != na->data_size) { |
657 | ntfs_log_error("Failed to read $UpCase, unexpected length " |
658 | "(%lld != %lld).\n", (long long)l, |
659 | (long long)na->data_size); |
660 | errno = EIO; |
661 | goto error_exit; |
662 | } |
663 | /* Consistency check of $UpCase, restricted to plain ASCII chars */ |
664 | k = 0x20; |
665 | while ((k < upcase_len) |
666 | && (k < 0x7f) |
667 | && (le16_to_cpu(upcase[k]) |
668 | == ((k < 'a') || (k > 'z') ? k : k + 'A' - 'a'))) |
669 | k++; |
670 | if (k < 0x7f) { |
671 | ntfs_log_error("Corrupted file $UpCase\n"); |
672 | if (!opt.no_action) { |
673 | /* rewrite the $UpCase file from default */ |
674 | res = rewrite_upcase(vol, na); |
675 | /* free the bad upcase record */ |
676 | if (!res) |
677 | free(upcase); |
678 | } else { |
679 | /* keep the default upcase but return an error */ |
680 | free(upcase); |
681 | } |
682 | } else { |
683 | /* accept the upcase table read from $UpCase */ |
684 | free(vol->upcase); |
685 | vol->upcase = upcase; |
686 | vol->upcase_len = upcase_len; |
687 | res = 0; |
688 | } |
689 | error_exit : |
690 | /* Done with the $UpCase mft record. */ |
691 | if (na) |
692 | ntfs_attr_close(na); |
693 | if (ni && ntfs_inode_close(ni)) { |
694 | ntfs_log_perror("Failed to close $UpCase"); |
695 | } |
696 | return (res); |
697 | } |
698 | |
699 | /* |
700 | * Rewrite the boot sector |
701 | * |
702 | * Returns 0 if successful |
703 | */ |
704 | |
705 | static int rewrite_boot(struct ntfs_device *dev, char *full_bs, |
706 | s32 sector_size) |
707 | { |
708 | s64 bw; |
709 | int res; |
710 | |
711 | res = -1; |
712 | ntfs_log_info("Rewriting the bootsector\n"); |
713 | bw = ntfs_pwrite(dev, 0, sector_size, full_bs); |
714 | if (bw == sector_size) |
715 | res = 0; |
716 | else { |
717 | if (bw != -1) |
718 | errno = EINVAL; |
719 | if (!bw) |
720 | ntfs_log_error("Failed to rewrite the bootsector (size=0)\n"); |
721 | else |
722 | ntfs_log_perror("Error rewriting the bootsector"); |
723 | } |
724 | return (res); |
725 | } |
726 | |
727 | /* |
728 | * Locate an unnamed attribute in an MFT record |
729 | * |
730 | * Returns NULL if not found (with no error message) |
731 | */ |
732 | |
733 | static ATTR_RECORD *find_unnamed_attr(MFT_RECORD *mrec, ATTR_TYPES type) |
734 | { |
735 | ATTR_RECORD *a; |
736 | u32 offset; |
737 | |
738 | /* fetch the requested attribute */ |
739 | offset = le16_to_cpu(mrec->attrs_offset); |
740 | a = (ATTR_RECORD*)((char*)mrec + offset); |
741 | while ((a->type != AT_END) |
742 | && ((a->type != type) || a->name_length) |
743 | && (offset < le32_to_cpu(mrec->bytes_in_use))) { |
744 | offset += le32_to_cpu(a->length); |
745 | a = (ATTR_RECORD*)((char*)mrec + offset); |
746 | } |
747 | if ((a->type != type) |
748 | || a->name_length) |
749 | a = (ATTR_RECORD*)NULL; |
750 | return (a); |
751 | } |
752 | |
753 | /* |
754 | * First condition for having a self-located MFT : |
755 | * only 16 MFT records are defined in MFT record 0 |
756 | * |
757 | * Only low-level library functions can be used. |
758 | * |
759 | * Returns TRUE if the condition is met. |
760 | */ |
761 | |
762 | static BOOL short_mft_selfloc_condition(struct MFT_SELF_LOCATED *selfloc) |
763 | { |
764 | BOOL ok; |
765 | ntfs_volume *vol; |
766 | MFT_RECORD *mft0; |
767 | ATTR_RECORD *a; |
768 | runlist_element *rl; |
769 | u16 seqn; |
770 | |
771 | ok = FALSE; |
772 | vol = selfloc->vol; |
773 | mft0 = selfloc->mft0; |
774 | if ((ntfs_pread(vol->dev, |
775 | vol->mft_lcn << vol->cluster_size_bits, |
776 | vol->mft_record_size, mft0) |
777 | == vol->mft_record_size) |
778 | && !ntfs_mst_post_read_fixup((NTFS_RECORD*)mft0, |
779 | vol->mft_record_size)) { |
780 | a = find_unnamed_attr(mft0,AT_DATA); |
781 | if (a |
782 | && a->non_resident |
783 | && (((le64_to_cpu(a->highest_vcn) + 1) |
784 | << vol->cluster_size_bits) |
785 | == (SELFLOC_LIMIT*vol->mft_record_size))) { |
786 | rl = ntfs_mapping_pairs_decompress(vol, a, NULL); |
787 | if (rl) { |
788 | /* |
789 | * The first error condition is having only |
790 | * 16 entries mapped in the first MFT record. |
791 | */ |
792 | if ((rl[0].lcn >= 0) |
793 | && ((rl[0].length << vol->cluster_size_bits) |
794 | == SELFLOC_LIMIT*vol->mft_record_size) |
795 | && (rl[1].vcn == rl[0].length) |
796 | && (rl[1].lcn == LCN_RL_NOT_MAPPED)) { |
797 | ok = TRUE; |
798 | seqn = le16_to_cpu( |
799 | mft0->sequence_number); |
800 | selfloc->mft_ref0 |
801 | = ((MFT_REF)seqn) << 48; |
802 | } |
803 | free(rl); |
804 | } |
805 | } |
806 | } |
807 | return (ok); |
808 | } |
809 | |
810 | /* |
811 | * Second condition for having a self-located MFT : |
812 | * The 16th MFT record is defined in MFT record >= 16 |
813 | * |
814 | * Only low-level library functions can be used. |
815 | * |
816 | * Returns TRUE if the condition is met. |
817 | */ |
818 | |
819 | static BOOL attrlist_selfloc_condition(struct MFT_SELF_LOCATED *selfloc) |
820 | { |
821 | ntfs_volume *vol; |
822 | ATTR_RECORD *a; |
823 | ATTR_LIST_ENTRY *attrlist; |
824 | ATTR_LIST_ENTRY *al; |
825 | runlist_element *rl; |
826 | VCN vcn; |
827 | leVCN levcn; |
828 | u32 length; |
829 | int ok; |
830 | |
831 | ok = FALSE; |
832 | length = 0; |
833 | vol = selfloc->vol; |
834 | a = find_unnamed_attr(selfloc->mft0,AT_ATTRIBUTE_LIST); |
835 | if (a) { |
836 | selfloc->attrlist_resident = !a->non_resident; |
837 | selfloc->attrlist_lcn = 0; |
838 | if (a->non_resident) { |
839 | attrlist = selfloc->attrlist; |
840 | rl = ntfs_mapping_pairs_decompress(vol, a, NULL); |
841 | if (rl |
842 | && (rl->lcn >= 0) |
843 | && (le64_to_cpu(a->data_size) < vol->cluster_size) |
844 | && (ntfs_pread(vol->dev, |
845 | rl->lcn << vol->cluster_size_bits, |
846 | vol->cluster_size, attrlist) == vol->cluster_size)) { |
847 | selfloc->attrlist_lcn = rl->lcn; |
848 | al = attrlist; |
849 | length = le64_to_cpu(a->data_size); |
850 | } |
851 | } else { |
852 | al = (ATTR_LIST_ENTRY*) |
853 | ((char*)a + le16_to_cpu(a->value_offset)); |
854 | length = le32_to_cpu(a->value_length); |
855 | } |
856 | if (length) { |
857 | /* search for a data attribute defining entry 16 */ |
858 | vcn = (SELFLOC_LIMIT*vol->mft_record_size) |
859 | >> vol->cluster_size_bits; |
860 | levcn = cpu_to_le64(vcn); |
861 | while ((length > 0) |
862 | && al->length |
863 | && ((al->type != AT_DATA) |
864 | || ((leVCN)al->lowest_vcn != levcn))) { |
865 | length -= le16_to_cpu(al->length); |
866 | al = (ATTR_LIST_ENTRY*) |
867 | ((char*)al + le16_to_cpu(al->length)); |
868 | } |
869 | if ((length > 0) |
870 | && al->length |
871 | && (al->type == AT_DATA) |
872 | && !al->name_length |
873 | && ((leVCN)al->lowest_vcn == levcn) |
874 | && (MREF_LE(al->mft_reference) >= SELFLOC_LIMIT)) { |
875 | selfloc->mft_ref1 |
876 | = le64_to_cpu(al->mft_reference); |
877 | selfloc->attrlist_to_ref1 = al; |
878 | ok = TRUE; |
879 | } |
880 | } |
881 | } |
882 | return (ok); |
883 | } |
884 | |
885 | /* |
886 | * Third condition for having a self-located MFT : |
887 | * The location of the second part of the MFT is defined in itself |
888 | * |
889 | * To locate the second part, we have to assume the first and the |
890 | * second part of the MFT data are contiguous. |
891 | * |
892 | * Only low-level library functions can be used. |
893 | * |
894 | * Returns TRUE if the condition is met. |
895 | */ |
896 | |
897 | static BOOL self_mapped_selfloc_condition(struct MFT_SELF_LOCATED *selfloc) |
898 | { |
899 | BOOL ok; |
900 | s64 inum; |
901 | u64 offs; |
902 | VCN lowest_vcn; |
903 | MFT_RECORD *mft1; |
904 | ATTR_RECORD *a; |
905 | ntfs_volume *vol; |
906 | runlist_element *rl; |
907 | |
908 | ok = FALSE; |
909 | vol = selfloc->vol; |
910 | mft1 = selfloc->mft1; |
911 | inum = MREF(selfloc->mft_ref1); |
912 | offs = (vol->mft_lcn << vol->cluster_size_bits) |
913 | + (inum << vol->mft_record_size_bits); |
914 | if ((ntfs_pread(vol->dev, offs, vol->mft_record_size, |
915 | mft1) == vol->mft_record_size) |
916 | && !ntfs_mst_post_read_fixup((NTFS_RECORD*)mft1, |
917 | vol->mft_record_size)) { |
918 | lowest_vcn = (SELFLOC_LIMIT*vol->mft_record_size) |
919 | >> vol->cluster_size_bits; |
920 | a = find_unnamed_attr(mft1,AT_DATA); |
921 | if (a |
922 | && (mft1->flags & MFT_RECORD_IN_USE) |
923 | && ((VCN)le64_to_cpu(a->lowest_vcn) == lowest_vcn) |
924 | && (le64_to_cpu(mft1->base_mft_record) |
925 | == selfloc->mft_ref0) |
926 | && ((u16)MSEQNO(selfloc->mft_ref1) |
927 | == le16_to_cpu(mft1->sequence_number))) { |
928 | rl = ntfs_mapping_pairs_decompress(vol, a, NULL); |
929 | if ((rl[0].lcn == LCN_RL_NOT_MAPPED) |
930 | && !rl[0].vcn |
931 | && (rl[0].length == lowest_vcn) |
932 | && (rl[1].vcn == lowest_vcn) |
933 | && ((u64)(rl[1].lcn << vol->cluster_size_bits) |
934 | <= offs) |
935 | && ((u64)((rl[1].lcn + rl[1].length) |
936 | << vol->cluster_size_bits) > offs)) { |
937 | ok = TRUE; |
938 | } |
939 | } |
940 | } |
941 | return (ok); |
942 | } |
943 | |
944 | /* |
945 | * Fourth condition, to be able to fix a self-located MFT : |
946 | * The MFT record 15 must be available. |
947 | * |
948 | * The MFT record 15 is expected to be marked in use, we assume |
949 | * it is available if it has no parent, no name and no attr list. |
950 | * |
951 | * Only low-level library functions can be used. |
952 | * |
953 | * Returns TRUE if the condition is met. |
954 | */ |
955 | |
956 | static BOOL spare_record_selfloc_condition(struct MFT_SELF_LOCATED *selfloc) |
957 | { |
958 | BOOL ok; |
959 | s64 inum; |
960 | u64 offs; |
961 | MFT_RECORD *mft2; |
962 | ntfs_volume *vol; |
963 | |
964 | ok = FALSE; |
965 | vol = selfloc->vol; |
966 | mft2 = selfloc->mft2; |
967 | inum = SELFLOC_LIMIT - 1; |
968 | offs = (vol->mft_lcn << vol->cluster_size_bits) |
969 | + (inum << vol->mft_record_size_bits); |
970 | if ((ntfs_pread(vol->dev, offs, vol->mft_record_size, |
971 | mft2) == vol->mft_record_size) |
972 | && !ntfs_mst_post_read_fixup((NTFS_RECORD*)mft2, |
973 | vol->mft_record_size)) { |
974 | if (!mft2->base_mft_record |
975 | && (mft2->flags & MFT_RECORD_IN_USE) |
976 | && !find_unnamed_attr(mft2,AT_ATTRIBUTE_LIST) |
977 | && !find_unnamed_attr(mft2,AT_FILE_NAME)) { |
978 | ok = TRUE; |
979 | } |
980 | } |
981 | return (ok); |
982 | } |
983 | |
984 | /* |
985 | * Fix a self-located MFT by swapping two MFT records |
986 | * |
987 | * Only low-level library functions can be used. |
988 | * |
989 | * Returns 0 if the MFT corruption could be fixed. |
990 | */ |
991 | static int fix_selfloc_conditions(struct MFT_SELF_LOCATED *selfloc) |
992 | { |
993 | MFT_RECORD *mft1; |
994 | MFT_RECORD *mft2; |
995 | ATTR_RECORD *a; |
996 | ATTR_LIST_ENTRY *al; |
997 | ntfs_volume *vol; |
998 | s64 offs; |
999 | s64 offsm; |
1000 | s64 offs1; |
1001 | s64 offs2; |
1002 | s64 inum; |
1003 | u16 usa_ofs; |
1004 | int res; |
1005 | |
1006 | res = 0; |
1007 | /* |
1008 | * In MFT1, we must fix : |
1009 | * - the self-reference, if present, |
1010 | * - its own sequence number, must be 15 |
1011 | * - the sizes of the data attribute. |
1012 | */ |
1013 | vol = selfloc->vol; |
1014 | mft1 = selfloc->mft1; |
1015 | mft2 = selfloc->mft2; |
1016 | usa_ofs = le16_to_cpu(mft1->usa_ofs); |
1017 | if (usa_ofs >= 48) |
1018 | mft1->mft_record_number = const_cpu_to_le32(SELFLOC_LIMIT - 1); |
1019 | mft1->sequence_number = const_cpu_to_le16(SELFLOC_LIMIT - 1); |
1020 | a = find_unnamed_attr(mft1,AT_DATA); |
1021 | if (a) { |
1022 | a->allocated_size = const_cpu_to_le64(0); |
1023 | a->data_size = const_cpu_to_le64(0); |
1024 | a->initialized_size = const_cpu_to_le64(0); |
1025 | } else |
1026 | res = -1; /* bug : it has been found earlier */ |
1027 | |
1028 | /* |
1029 | * In MFT2, we must fix : |
1030 | * - the self-reference, if present |
1031 | */ |
1032 | usa_ofs = le16_to_cpu(mft2->usa_ofs); |
1033 | if (usa_ofs >= 48) |
1034 | mft2->mft_record_number = cpu_to_le32(MREF(selfloc->mft_ref1)); |
1035 | |
1036 | /* |
1037 | * In the attribute list, we must fix : |
1038 | * - the reference to MFT1 |
1039 | */ |
1040 | al = selfloc->attrlist_to_ref1; |
1041 | al->mft_reference = MK_LE_MREF(SELFLOC_LIMIT - 1, SELFLOC_LIMIT - 1); |
1042 | |
1043 | /* |
1044 | * All fixes done, we can write all if allowed |
1045 | */ |
1046 | if (!res && !opt.no_action) { |
1047 | inum = SELFLOC_LIMIT - 1; |
1048 | offs2 = (vol->mft_lcn << vol->cluster_size_bits) |
1049 | + (inum << vol->mft_record_size_bits); |
1050 | inum = MREF(selfloc->mft_ref1); |
1051 | offs1 = (vol->mft_lcn << vol->cluster_size_bits) |
1052 | + (inum << vol->mft_record_size_bits); |
1053 | |
1054 | /* rewrite the attribute list */ |
1055 | if (selfloc->attrlist_resident) { |
1056 | /* write mft0 and mftmirr if it is resident */ |
1057 | offs = vol->mft_lcn << vol->cluster_size_bits; |
1058 | offsm = vol->mftmirr_lcn << vol->cluster_size_bits; |
1059 | if (ntfs_mst_pre_write_fixup( |
1060 | (NTFS_RECORD*)selfloc->mft0, |
1061 | vol->mft_record_size) |
1062 | || (ntfs_pwrite(vol->dev, offs, vol->mft_record_size, |
1063 | selfloc->mft0) != vol->mft_record_size) |
1064 | || (ntfs_pwrite(vol->dev, offsm, vol->mft_record_size, |
1065 | selfloc->mft0) != vol->mft_record_size)) |
1066 | res = -1; |
1067 | } else { |
1068 | /* write a full cluster if non resident */ |
1069 | offs = selfloc->attrlist_lcn << vol->cluster_size_bits; |
1070 | if (ntfs_pwrite(vol->dev, offs, vol->cluster_size, |
1071 | selfloc->attrlist) != vol->cluster_size) |
1072 | res = -1; |
1073 | } |
1074 | /* replace MFT2 by MFT1 and replace MFT1 by MFT2 */ |
1075 | if (!res |
1076 | && (ntfs_mst_pre_write_fixup((NTFS_RECORD*)selfloc->mft1, |
1077 | vol->mft_record_size) |
1078 | || ntfs_mst_pre_write_fixup((NTFS_RECORD*)selfloc->mft2, |
1079 | vol->mft_record_size) |
1080 | || (ntfs_pwrite(vol->dev, offs2, vol->mft_record_size, |
1081 | mft1) != vol->mft_record_size) |
1082 | || (ntfs_pwrite(vol->dev, offs1, vol->mft_record_size, |
1083 | mft2) != vol->mft_record_size))) |
1084 | res = -1; |
1085 | } |
1086 | return (res); |
1087 | } |
1088 | |
1089 | /* |
1090 | * Detect and fix a Windows XP bug, leading to a corrupt MFT |
1091 | * |
1092 | * Windows cannot boot anymore, so chkdsk cannot be started, which |
1093 | * is a good point, because chkdsk would have deleted all the files. |
1094 | * Older ntfs-3g fell into an endless recursion (recent versions |
1095 | * refuse to mount). |
1096 | * |
1097 | * This situation is very rare, but it was fun to fix it. |
1098 | * |
1099 | * The corrupted condition is : |
1100 | * - MFT entry 0 has only the runlist for MFT entries 0-15 |
1101 | * - The attribute list for MFT shows the second part |
1102 | * in an MFT record beyond 15 |
1103 | * Of course, this record has to be read in order to know where it is. |
1104 | * |
1105 | * Sample case, met in 2011 (Windows XP) : |
1106 | * MFT record 0 has : stdinfo, nonres attrlist, the first |
1107 | * part of MFT data (entries 0-15), and bitmap |
1108 | * MFT record 16 has the name |
1109 | * MFT record 17 has the third part of MFT data (16-117731) |
1110 | * MFT record 18 has the second part of MFT data (117732-170908) |
1111 | * |
1112 | * Assuming the second part of the MFT is contiguous to the first |
1113 | * part, we can find it, and fix the condition by relocating it |
1114 | * and swapping it with MFT record 15. |
1115 | * This record number 15 appears to be hardcoded into Windows NTFS. |
1116 | * |
1117 | * Only low-level library functions can be used. |
1118 | * |
1119 | * Returns 0 if the conditions for the error were not met or |
1120 | * the error could be fixed, |
1121 | * -1 if some error was encountered |
1122 | */ |
1123 | |
1124 | static int fix_self_located_mft(ntfs_volume *vol) |
1125 | { |
1126 | struct MFT_SELF_LOCATED selfloc; |
1127 | BOOL res; |
1128 | |
1129 | ntfs_log_info("Checking for self-located MFT segment... "); |
1130 | res = -1; |
1131 | selfloc.vol = vol; |
1132 | selfloc.mft0 = (MFT_RECORD*)malloc(vol->mft_record_size); |
1133 | selfloc.mft1 = (MFT_RECORD*)malloc(vol->mft_record_size); |
1134 | selfloc.mft2 = (MFT_RECORD*)malloc(vol->mft_record_size); |
1135 | selfloc.attrlist = (ATTR_LIST_ENTRY*)malloc(vol->cluster_size); |
1136 | if (selfloc.mft0 && selfloc.mft1 && selfloc.mft2 |
1137 | && selfloc.attrlist) { |
1138 | if (short_mft_selfloc_condition(&selfloc) |
1139 | && attrlist_selfloc_condition(&selfloc) |
1140 | && self_mapped_selfloc_condition(&selfloc) |
1141 | && spare_record_selfloc_condition(&selfloc)) { |
1142 | ntfs_log_info(FOUND); |
1143 | ntfs_log_info("Fixing the self-located MFT segment... "); |
1144 | res = fix_selfloc_conditions(&selfloc); |
1145 | ntfs_log_info(res ? FAILED : OK); |
1146 | } else { |
1147 | ntfs_log_info(OK); |
1148 | res = 0; |
1149 | } |
1150 | free(selfloc.mft0); |
1151 | free(selfloc.mft1); |
1152 | free(selfloc.mft2); |
1153 | free(selfloc.attrlist); |
1154 | } |
1155 | return (res); |
1156 | } |
1157 | |
1158 | /* |
1159 | * Try an alternate boot sector and fix the real one |
1160 | * |
1161 | * Only after successful checks is the boot sector rewritten. |
1162 | * |
1163 | * The alternate boot sector is not rewritten, either because it |
1164 | * was found correct, or because we truncated the file system |
1165 | * and the last actual sector might be part of some file. |
1166 | * |
1167 | * Returns 0 if successful |
1168 | */ |
1169 | |
1170 | static int try_fix_boot(ntfs_volume *vol, char *full_bs, |
1171 | s64 read_sector, s64 fix_sectors, s32 sector_size) |
1172 | { |
1173 | s64 br; |
1174 | int res; |
1175 | s64 got_sectors; |
1176 | le16 sector_size_le; |
1177 | NTFS_BOOT_SECTOR *bs; |
1178 | |
1179 | res = -1; |
1180 | br = ntfs_pread(vol->dev, read_sector*sector_size, |
1181 | sector_size, full_bs); |
1182 | if (br != sector_size) { |
1183 | if (br != -1) |
1184 | errno = EINVAL; |
1185 | if (!br) |
1186 | ntfs_log_error("Failed to read alternate bootsector (size=0)\n"); |
1187 | else |
1188 | ntfs_log_perror("Error reading alternate bootsector"); |
1189 | } else { |
1190 | bs = (NTFS_BOOT_SECTOR*)full_bs; |
1191 | got_sectors = le64_to_cpu(bs->number_of_sectors); |
1192 | bs->number_of_sectors = cpu_to_le64(fix_sectors); |
1193 | /* alignment problem on Sparc, even doing memcpy() */ |
1194 | sector_size_le = cpu_to_le16(sector_size); |
1195 | if (!memcmp(§or_size_le, &bs->bpb.bytes_per_sector,2) |
1196 | && ntfs_boot_sector_is_ntfs(bs) |
1197 | && !ntfs_boot_sector_parse(vol, bs)) { |
1198 | ntfs_log_info("The alternate bootsector is usable\n"); |
1199 | if (fix_sectors != got_sectors) |
1200 | ntfs_log_info("Set sector count to %lld instead of %lld\n", |
1201 | (long long)fix_sectors, |
1202 | (long long)got_sectors); |
1203 | /* fix the normal boot sector */ |
1204 | if (!opt.no_action) { |
1205 | res = rewrite_boot(vol->dev, full_bs, |
1206 | sector_size); |
1207 | } else |
1208 | res = 0; |
1209 | } |
1210 | if (!res && !opt.no_action) |
1211 | ntfs_log_info("The boot sector has been rewritten\n"); |
1212 | } |
1213 | return (res); |
1214 | } |
1215 | |
1216 | /* |
1217 | * Try the alternate boot sector if the normal one is bad |
1218 | * |
1219 | * Actually : |
1220 | * - first try the last sector of the partition (expected location) |
1221 | * - then try the last sector as shown in the main boot sector, |
1222 | * (could be meaningful for an undersized partition) |
1223 | * - finally try truncating the file system actual size of partition |
1224 | * (could be meaningful for an oversized partition) |
1225 | * |
1226 | * if successful, rewrite the normal boot sector accordingly |
1227 | * |
1228 | * Returns 0 if successful |
1229 | */ |
1230 | |
1231 | static int try_alternate_boot(ntfs_volume *vol, char *full_bs, |
1232 | s32 sector_size, s64 shown_sectors) |
1233 | { |
1234 | s64 actual_sectors; |
1235 | int res; |
1236 | |
1237 | res = -1; |
1238 | ntfs_log_info("Trying the alternate boot sector\n"); |
1239 | |
1240 | /* |
1241 | * We do not rely on the sector size defined in the |
1242 | * boot sector, supposed to be corrupt, so we try to get |
1243 | * the actual sector size and defaulting to 512 if failed |
1244 | * to get. This value is only used to guess the alternate |
1245 | * boot sector location and it is checked against the |
1246 | * value found in the sector itself. It should not damage |
1247 | * anything if wrong. |
1248 | * |
1249 | * Note : the real last sector is not accounted for here. |
1250 | */ |
1251 | actual_sectors = ntfs_device_size_get(vol->dev,sector_size) - 1; |
1252 | |
1253 | /* first try the actual last sector */ |
1254 | if ((actual_sectors > 0) |
1255 | && !try_fix_boot(vol, full_bs, actual_sectors, |
1256 | actual_sectors, sector_size)) |
1257 | res = 0; |
1258 | |
1259 | /* then try the shown last sector, if less than actual */ |
1260 | if (res |
1261 | && (shown_sectors > 0) |
1262 | && (shown_sectors < actual_sectors) |
1263 | && !try_fix_boot(vol, full_bs, shown_sectors, |
1264 | shown_sectors, sector_size)) |
1265 | res = 0; |
1266 | |
1267 | /* then try reducing the number of sectors to actual value */ |
1268 | if (res |
1269 | && (shown_sectors > actual_sectors) |
1270 | && !try_fix_boot(vol, full_bs, 0, actual_sectors, sector_size)) |
1271 | res = 0; |
1272 | |
1273 | return (res); |
1274 | } |
1275 | |
1276 | /* |
1277 | * Check and fix the alternate boot sector |
1278 | * |
1279 | * The alternate boot sector is usually in the last sector of a |
1280 | * partition, which should not be used by the file system |
1281 | * (the sector count in the boot sector should be less than |
1282 | * the total sector count in the partition). |
1283 | * |
1284 | * chkdsk never changes the count in the boot sector. |
1285 | * - If this is less than the total count, chkdsk place the |
1286 | * alternate boot sector into the sector, |
1287 | * - if the count is the same as the total count, chkdsk place |
1288 | * the alternate boot sector into the middle sector (half |
1289 | * the total count rounded upwards) |
1290 | * - if the count is greater than the total count, chkdsk |
1291 | * declares the file system as raw, and refuses to fix anything. |
1292 | * |
1293 | * Here, we check and fix the alternate boot sector, only in the |
1294 | * first situation where the file system does not overflow on the |
1295 | * last sector. |
1296 | * |
1297 | * Note : when shrinking a partition, ntfsresize cannot determine |
1298 | * the future size of the partition. As a consequence the number of |
1299 | * sectors in the boot sectors may be less than the possible size. |
1300 | * |
1301 | * Returns 0 if successful |
1302 | */ |
1303 | |
1304 | static int check_alternate_boot(ntfs_volume *vol) |
1305 | { |
1306 | #if 0 |
1307 | s64 got_sectors; |
1308 | s64 actual_sectors; |
1309 | s64 last_sector_off; |
1310 | char *full_bs; |
1311 | char *alt_bs; |
1312 | NTFS_BOOT_SECTOR *bs; |
1313 | s64 br; |
1314 | s64 bw; |
1315 | int res; |
1316 | |
1317 | res = -1; |
1318 | full_bs = (char*)malloc(vol->sector_size); |
1319 | alt_bs = (char*)malloc(vol->sector_size); |
1320 | if (!full_bs || !alt_bs) { |
1321 | ntfs_log_info("Error : failed to allocate memory\n"); |
1322 | goto error_exit; |
1323 | } |
1324 | /* Now read both bootsectors. */ |
1325 | br = ntfs_pread(vol->dev, 0, vol->sector_size, full_bs); |
1326 | if (br == vol->sector_size) { |
1327 | bs = (NTFS_BOOT_SECTOR*)full_bs; |
1328 | got_sectors = le64_to_cpu(bs->number_of_sectors); |
1329 | actual_sectors = ntfs_device_size_get(vol->dev, |
1330 | vol->sector_size); |
1331 | if (actual_sectors > got_sectors) { |
1332 | last_sector_off = (actual_sectors - 1) |
1333 | << vol->sector_size_bits; |
1334 | ntfs_log_info("Checking the alternate boot sector... "); |
1335 | br = ntfs_pread(vol->dev, last_sector_off, |
1336 | vol->sector_size, alt_bs); |
1337 | } else { |
1338 | ntfs_log_info("Checking file system overflow... "); |
1339 | br = -1; |
1340 | } |
1341 | /* accept getting no byte, needed for short image files */ |
1342 | if (br >= 0) { |
1343 | if ((br != vol->sector_size) |
1344 | || memcmp(full_bs, alt_bs, vol->sector_size)) { |
1345 | if (opt.no_action) { |
1346 | ntfs_log_info("BAD\n"); |
1347 | } else { |
1348 | bw = ntfs_pwrite(vol->dev, |
1349 | last_sector_off, |
1350 | vol->sector_size, full_bs); |
1351 | if (bw == vol->sector_size) { |
1352 | ntfs_log_info("FIXED\n"); |
1353 | res = 0; |
1354 | } else { |
1355 | ntfs_log_info(FAILED); |
1356 | } |
1357 | } |
1358 | } else { |
1359 | ntfs_log_info(OK); |
1360 | res = 0; |
1361 | } |
1362 | } else { |
1363 | ntfs_log_info(FAILED); |
1364 | } |
1365 | } else { |
1366 | ntfs_log_info("Error : could not read the boot sector again\n"); |
1367 | } |
1368 | free(full_bs); |
1369 | free(alt_bs); |
1370 | |
1371 | error_exit : |
1372 | return (res); |
1373 | #endif |
1374 | return (0); |
1375 | } |
1376 | |
1377 | /* |
1378 | * Try to fix problems which may arise in the start up sequence |
1379 | * |
1380 | * This is a replay of the normal start up sequence with fixes when |
1381 | * some problem arise. |
1382 | */ |
1383 | |
1384 | static int fix_startup(struct ntfs_device *dev, unsigned long flags) |
1385 | { |
1386 | s64 br; |
1387 | ntfs_volume *vol; |
1388 | BOOL dev_open; |
1389 | s64 shown_sectors; |
1390 | char *full_bs; |
1391 | NTFS_BOOT_SECTOR *bs; |
1392 | s32 sector_size; |
1393 | int res; |
1394 | int eo; |
1395 | |
1396 | errno = 0; |
1397 | res = -1; |
1398 | dev_open = FALSE; |
1399 | full_bs = (char*)NULL; |
1400 | if (!dev || !dev->d_ops || !dev->d_name) { |
1401 | errno = EINVAL; |
1402 | ntfs_log_perror("%s: dev = %p", __FUNCTION__, dev); |
1403 | vol = (ntfs_volume*)NULL; |
1404 | goto error_exit; |
1405 | } |
1406 | |
1407 | /* Allocate the volume structure. */ |
1408 | vol = ntfs_volume_alloc(); |
1409 | if (!vol) |
1410 | goto error_exit; |
1411 | |
1412 | /* Create the default upcase table. */ |
1413 | vol->upcase_len = ntfs_upcase_build_default(&vol->upcase); |
1414 | if (!vol->upcase_len || !vol->upcase) |
1415 | goto error_exit; |
1416 | |
1417 | /* Default with no locase table and case sensitive file names */ |
1418 | vol->locase = (ntfschar*)NULL; |
1419 | NVolSetCaseSensitive(vol); |
1420 | |
1421 | /* by default, all files are shown and not marked hidden */ |
1422 | NVolSetShowSysFiles(vol); |
1423 | NVolSetShowHidFiles(vol); |
1424 | NVolClearHideDotFiles(vol); |
1425 | if (flags & NTFS_MNT_RDONLY) |
1426 | NVolSetReadOnly(vol); |
1427 | |
1428 | /* ...->open needs bracketing to compile with glibc 2.7 */ |
1429 | if ((dev->d_ops->open)(dev, NVolReadOnly(vol) ? O_RDONLY: O_RDWR)) { |
1430 | ntfs_log_perror("Error opening '%s'", dev->d_name); |
1431 | goto error_exit; |
1432 | } |
1433 | dev_open = TRUE; |
1434 | /* Attach the device to the volume. */ |
1435 | vol->dev = dev; |
1436 | |
1437 | sector_size = ntfs_device_sector_size_get(dev); |
1438 | if (sector_size <= 0) |
1439 | sector_size = DEFAULT_SECTOR_SIZE; |
1440 | full_bs = (char*)malloc(sector_size); |
1441 | if (!full_bs) |
1442 | goto error_exit; |
1443 | /* Now read the bootsector. */ |
1444 | br = ntfs_pread(dev, 0, sector_size, full_bs); |
1445 | if (br != sector_size) { |
1446 | if (br != -1) |
1447 | errno = EINVAL; |
1448 | if (!br) |
1449 | ntfs_log_error("Failed to read bootsector (size=0)\n"); |
1450 | else |
1451 | ntfs_log_perror("Error reading bootsector"); |
1452 | goto error_exit; |
1453 | } |
1454 | bs = (NTFS_BOOT_SECTOR*)full_bs; |
1455 | if (!ntfs_boot_sector_is_ntfs(bs) |
1456 | /* get the bootsector data, only fails when inconsistent */ |
1457 | || (ntfs_boot_sector_parse(vol, bs) < 0)) { |
1458 | shown_sectors = le64_to_cpu(bs->number_of_sectors); |
1459 | /* boot sector is wrong, try the alternate boot sector */ |
1460 | if (try_alternate_boot(vol, full_bs, sector_size, |
1461 | shown_sectors)) { |
1462 | errno = EINVAL; |
1463 | goto error_exit; |
1464 | } |
1465 | res = 0; |
1466 | } else { |
1467 | res = fix_self_located_mft(vol); |
1468 | } |
1469 | error_exit: |
1470 | if (res) { |
1471 | switch (errno) { |
1472 | case ENOMEM : |
1473 | ntfs_log_error("Failed to allocate memory\n"); |
1474 | break; |
1475 | case EINVAL : |
1476 | ntfs_log_error("Unrecoverable error\n"); |
1477 | break; |
1478 | default : |
1479 | break; |
1480 | } |
1481 | } |
1482 | eo = errno; |
1483 | free(full_bs); |
1484 | if (vol) { |
1485 | free(vol->upcase); |
1486 | free(vol); |
1487 | } |
1488 | if (dev_open) { |
1489 | (dev->d_ops->close)(dev); |
1490 | } |
1491 | errno = eo; |
1492 | return (res); |
1493 | } |
1494 | |
1495 | /** |
1496 | * fix_mount |
1497 | */ |
1498 | static int fix_mount(void) |
1499 | { |
1500 | int ret = 0; /* default success */ |
1501 | ntfs_volume *vol; |
1502 | struct ntfs_device *dev; |
1503 | unsigned long flags; |
1504 | |
1505 | ntfs_log_info("Attempting to correct errors... "); |
1506 | |
1507 | dev = ntfs_device_alloc(opt.volume, 0, &ntfs_device_default_io_ops, |
1508 | NULL); |
1509 | if (!dev) { |
1510 | ntfs_log_info(FAILED); |
1511 | ntfs_log_perror("Failed to allocate device"); |
1512 | return -1; |
1513 | } |
1514 | flags = (opt.no_action ? NTFS_MNT_RDONLY : 0); |
1515 | vol = ntfs_volume_startup(dev, flags); |
1516 | if (!vol) { |
1517 | ntfs_log_info(FAILED); |
1518 | ntfs_log_perror("Failed to startup volume"); |
1519 | |
1520 | /* Try fixing the bootsector and MFT, then redo the startup */ |
1521 | if (!fix_startup(dev, flags)) { |
1522 | if (opt.no_action) |
1523 | ntfs_log_info("The startup data can be fixed, " |
1524 | "but no change was requested\n"); |
1525 | else |
1526 | vol = ntfs_volume_startup(dev, flags); |
1527 | } |
1528 | if (!vol) { |
1529 | ntfs_log_error("Volume is corrupt. You should run chkdsk.\n"); |
1530 | ntfs_device_free(dev); |
1531 | return -1; |
1532 | } |
1533 | if (opt.no_action) |
1534 | ret = -1; /* error present and not fixed */ |
1535 | } |
1536 | /* if option -n proceed despite errors, to display them all */ |
1537 | if ((!ret || opt.no_action) && (fix_mftmirr(vol) < 0)) |
1538 | ret = -1; |
1539 | if ((!ret || opt.no_action) && (fix_upcase(vol) < 0)) |
1540 | ret = -1; |
1541 | if ((!ret || opt.no_action) && (set_dirty_flag(vol) < 0)) |
1542 | ret = -1; |
1543 | if ((!ret || opt.no_action) && (empty_journal(vol) < 0)) |
1544 | ret = -1; |
1545 | /* |
1546 | * ntfs_umount() will invoke ntfs_device_free() for us. |
1547 | * Ignore the returned error resulting from partial mounting. |
1548 | */ |
1549 | ntfs_umount(vol, 1); |
1550 | return ret; |
1551 | } |
1552 | |
1553 | /** |
1554 | * main |
1555 | */ |
1556 | int main(int argc, char **argv) |
1557 | { |
1558 | ntfs_volume *vol; |
1559 | unsigned long mnt_flags; |
1560 | unsigned long flags; |
1561 | int ret = 1; /* failure */ |
1562 | BOOL force = FALSE; |
1563 | |
1564 | ntfs_log_set_handler(ntfs_log_handler_outerr); |
1565 | |
1566 | parse_options(argc, argv); |
1567 | |
1568 | if (!ntfs_check_if_mounted(opt.volume, &mnt_flags)) { |
1569 | if ((mnt_flags & NTFS_MF_MOUNTED) && |
1570 | !(mnt_flags & NTFS_MF_READONLY) && !force) { |
1571 | ntfs_log_error("Refusing to operate on read-write " |
1572 | "mounted device %s.\n", opt.volume); |
1573 | exit(1); |
1574 | } |
1575 | } else |
1576 | ntfs_log_perror("Failed to determine whether %s is mounted", |
1577 | opt.volume); |
1578 | /* Attempt a full mount first. */ |
1579 | flags = (opt.no_action ? NTFS_MNT_RDONLY : 0); |
1580 | ntfs_log_info("Mounting volume... "); |
1581 | vol = ntfs_mount(opt.volume, flags); |
1582 | if (vol) { |
1583 | ntfs_log_info(OK); |
1584 | ntfs_log_info("Processing of $MFT and $MFTMirr completed " |
1585 | "successfully.\n"); |
1586 | } else { |
1587 | ntfs_log_info(FAILED); |
1588 | if (fix_mount() < 0) { |
1589 | if (opt.no_action) |
1590 | ntfs_log_info("No change made\n"); |
1591 | exit(1); |
1592 | } |
1593 | vol = ntfs_mount(opt.volume, 0); |
1594 | if (!vol) { |
1595 | ntfs_log_perror("Remount failed"); |
1596 | exit(1); |
1597 | } |
1598 | } |
1599 | if (check_alternate_boot(vol)) { |
1600 | ntfs_log_error("Error: Failed to fix the alternate boot sector\n"); |
1601 | exit(1); |
1602 | } |
1603 | /* So the unmount does not clear it again. */ |
1604 | |
1605 | /* Porting note: The WasDirty flag was set here to prevent ntfs_unmount |
1606 | * from clearing the dirty bit (which might have been set in |
1607 | * fix_mount()). So the intention is to leave the dirty bit set. |
1608 | * |
1609 | * libntfs-3g does not automatically set or clear dirty flags on |
1610 | * mount/unmount, this means that the assumption that the dirty flag is |
1611 | * now set does not hold. So we need to set it if not already set. |
1612 | * |
1613 | * However clear the flag if requested to do so, at this stage |
1614 | * mounting was successful. |
1615 | */ |
1616 | if (opt.clear_dirty) |
1617 | vol->flags &= ~VOLUME_IS_DIRTY; |
1618 | else |
1619 | vol->flags |= VOLUME_IS_DIRTY; |
1620 | if (!opt.no_action && ntfs_volume_write_flags(vol, vol->flags)) { |
1621 | ntfs_log_error("Error: Failed to set volume dirty flag (%d " |
1622 | "(%s))!\n", errno, strerror(errno)); |
1623 | } |
1624 | |
1625 | /* Check NTFS version is ok for us (in $Volume) */ |
1626 | ntfs_log_info("NTFS volume version is %i.%i.\n", vol->major_ver, |
1627 | vol->minor_ver); |
1628 | if (ntfs_version_is_supported(vol)) { |
1629 | ntfs_log_error("Error: Unknown NTFS version.\n"); |
1630 | goto error_exit; |
1631 | } |
1632 | if (opt.clear_bad_sectors && !opt.no_action) { |
1633 | if (clear_badclus(vol)) { |
1634 | ntfs_log_error("Error: Failed to un-mark bad sectors.\n"); |
1635 | goto error_exit; |
1636 | } |
1637 | } |
1638 | if (vol->major_ver >= 3) { |
1639 | /* |
1640 | * FIXME: If on NTFS 3.0+, check for presence of the usn |
1641 | * journal and stamp it if present. |
1642 | */ |
1643 | } |
1644 | /* FIXME: We should be marking the quota out of date, too. */ |
1645 | /* That's all for now! */ |
1646 | ntfs_log_info("NTFS partition %s was processed successfully.\n", |
1647 | vol->dev->d_name); |
1648 | /* Set return code to 0. */ |
1649 | ret = 0; |
1650 | error_exit: |
1651 | if (ntfs_umount(vol, 0)) |
1652 | ntfs_umount(vol, 1); |
1653 | if (ret) |
1654 | exit(ret); |
1655 | return ret; |
1656 | } |
1657 | |
1658 |