ntfsfix.c revision 7e7bd3dccbfe8f79e25e5c1554b5bc3a9aaca321
/**
* ntfsfix - Part of the Linux-NTFS project.
*
* Copyright (c) 2000-2006 Anton Altaparmakov
* Copyright (c) 2002-2006 Szabolcs Szakacsits
* Copyright (c) 2007 Yura Pakhuchiy
*
* This utility fixes some common NTFS problems, resets the NTFS journal file
* and schedules an NTFS consistency check for the first boot into Windows.
*
* Anton Altaparmakov <aia21@cantab.net>
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (in the main directory of the Linux-NTFS source
* in the file COPYING); if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* WARNING: This program might not work on architectures which do not allow
* unaligned access. For those, the program would need to start using
* get/put_unaligned macros (#include <asm/unaligned.h>), but not doing it yet,
* since NTFS really mostly applies to ia32 only, which does allow unaligned
* accesses. We might not actually have a problem though, since the structs are
* defined as being packed so that might be enough for gcc to insert the
* correct code.
*
* this program please let me know whether it works or not!
*
* Anton Altaparmakov <aia21@cantab.net>
*/
#include "config.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include "types.h"
#include "attrib.h"
#include "mft.h"
#include "device.h"
#include "logfile.h"
#include "utils.h"
#include "version.h"
#include "logging.h"
# error "No default device io operations! Cannot build ntfsfix. \
You need to run ./configure without the --disable-default-device-io-ops \
switch if you want to be able to build the NTFS utilities."
#endif
static const char *EXEC_NAME = "ntfsfix";
static const char *OK = "OK\n";
static const char *FAILED = "FAILED\n";
static struct {
char *volume;
} opt;
/**
* usage
*/
static int usage(void)
{
ntfs_log_info("%s v%s (libntfs %s)\n"
"\n"
"Usage: %s [options] device\n"
" Attempt to fix an NTFS partition.\n"
"\n"
" -h, --help Display this help\n"
" -V, --version Display version information\n"
"\n"
exit(1);
}
/**
* version
*/
static void version(void)
{
ntfs_log_info("%s v%s\n\n"
"Attempt to fix an NTFS partition.\n\n"
"Copyright (c) 2000-2006 Anton Altaparmakov\n"
"Copyright (c) 2002-2006 Szabolcs Szakacsits\n"
"Copyright (c) 2007 Yura Pakhuchiy\n\n",
exit(1);
}
/**
* parse_options
*/
{
int c;
static const char *sopt = "-hV";
};
switch (c) {
case 1: /* A non-option argument */
else {
ntfs_log_info("ERROR: Too many arguments.\n");
usage();
}
break;
case 'h':
case '?':
usage();
case 'V':
version();
default:
usage();
}
}
ntfs_log_info("ERROR: You must specify a device.\n");
usage();
}
}
/**
* OLD_ntfs_volume_set_flags
*/
{
MFT_RECORD *m = NULL;
ATTR_RECORD *a;
if (!vol) {
return -1;
}
ntfs_log_perror("Failed to read $Volume");
return -1;
}
/* Sanity check */
if (!(m->flags & MFT_RECORD_IN_USE)) {
ntfs_log_error("$Volume has been deleted. Cannot handle this "
"yet. Run chkdsk to fix this.\n");
goto err_exit;
}
/* Get a pointer to the volume information attribute. */
if (!ctx) {
ntfs_log_debug("Failed to allocate attribute search "
"context.\n");
goto err_exit;
}
0, ctx)) {
ntfs_log_error("Attribute $VOLUME_INFORMATION was not found in "
"$Volume!\n");
goto err_out;
}
/* Sanity check. */
if (a->non_resident) {
ntfs_log_error("Attribute $VOLUME_INFORMATION must be resident "
"(and it isn't)!\n");
goto err_out;
}
/* Get a pointer to the value of the attribute. */
/* Sanity checks. */
(char*)m + le32_to_cpu(m->bytes_in_use) ||
ntfs_log_error("Attribute $VOLUME_INFORMATION in $Volume is "
"corrupt!\n");
goto err_out;
}
/* Set the volume flags. */
ntfs_log_perror("Error writing $Volume");
goto err_out;
}
ret = 0; /* success */
free(m);
return ret;
}
/**
* set_dirty_flag
*/
{
if (NVolWasDirty(vol))
return 0;
ntfs_log_info("Setting required flags on partition... ");
/*
* Set chkdsk flag, i.e. mark the partition dirty so chkdsk will run
* and fix it for us.
*/
ntfs_log_error("Error setting volume flags.\n");
return -1;
}
return 0;
}
/**
* empty_journal
*/
{
if (NVolLogFileEmpty(vol))
return 0;
ntfs_log_info("Going to empty the journal ($LogFile)... ");
if (ntfs_logfile_reset(vol)) {
ntfs_log_perror("Failed to reset $LogFile");
return -1;
}
return 0;
}
/**
* fix_mftmirr
*/
{
unsigned char *m, *m2;
ntfs_log_info("\nProcessing $MFT and $MFTMirr...\n");
/* Load data from $MFT and $MFTMirr and compare the contents. */
if (!m) {
ntfs_log_perror("Failed to allocate memory");
return -1;
}
if (!m2) {
ntfs_log_perror("Failed to allocate memory");
free(m);
return -1;
}
ntfs_log_info("Reading $MFT... ");
vol->mft_record_size, m);
if (l != vol->mftmirr_size) {
if (l != -1)
ntfs_log_perror("Failed to read $MFT");
goto error_exit;
}
ntfs_log_info("Reading $MFTMirr... ");
if (l != vol->mftmirr_size) {
if (l != -1)
ntfs_log_perror("Failed to read $MFTMirr");
goto error_exit;
}
/*
* FIXME: Need to actually check the $MFTMirr for being real. Otherwise
* we might corrupt the partition if someone is experimenting with
* software RAID and the $MFTMirr is not actually in the position we
* expect it to be... )-:
* FIXME: We should emit a warning it $MFTMirr is damaged and ask
* user whether to recreate it from $MFT or whether to abort. - The
* warning needs to include the danger of software RAID arrays.
* Maybe we should go as far as to detect whether we are running on a
* MD disk and if yes then bomb out right at the start of the program?
*/
ntfs_log_info("Comparing $MFTMirr to $MFT... ");
for (i = 0; i < vol->mftmirr_size; ++i) {
"$Volume", "$AttrDef", "root directory", "$Bitmap",
"$Boot", "$BadClus", "$Secure", "$UpCase", "$Extend" };
const char *s;
if (i < 12)
s = ESTR[i];
else if (i < 16)
s = "system file";
else
s = "mft record";
ntfs_log_error("$MFT error: Incomplete multi "
"sector transfer detected in "
"%s.\nCannot handle this yet. "
")-:\n", s);
goto error_exit;
}
ntfs_log_error("$MFT error: Invalid mft "
"record for %s.\nCannot "
"handle this yet. )-:\n", s);
goto error_exit;
}
}
ntfs_log_error("$MFTMirr error: Incomplete "
"multi sector transfer "
"detected in %s.\n", s);
goto error_exit;
}
ntfs_log_error("$MFTMirr error: Invalid mft "
"record for %s.\n", s);
goto error_exit;
}
/* $MFT is corrupt but $MFTMirr is ok, use $MFTMirr. */
}
if (!done) {
}
ntfs_log_info("Correcting differences in $MFT%s "
i);
if (br) {
ntfs_log_perror("Error correcting $MFT%s",
goto error_exit;
}
}
}
if (!done)
ntfs_log_info("Processing of $MFT and $MFTMirr completed "
"successfully.\n");
ret = 0;
free(m);
return ret;
}
/**
* fix_mount
*/
static int fix_mount(void)
{
struct ntfs_device *dev;
ntfs_log_info("Attempting to correct errors... ");
NULL);
if (!dev) {
ntfs_log_perror("Failed to allocate device");
return -1;
}
if (!vol) {
ntfs_log_perror("Failed to startup volume");
ntfs_log_error("Volume is corrupt. You should run chkdsk.\n");
return -1;
}
if (fix_mftmirr(vol) < 0)
goto error_exit;
if (set_dirty_flag(vol) < 0)
goto error_exit;
if (empty_journal(vol) < 0)
goto error_exit;
ret = 0;
/* ntfs_umount() will invoke ntfs_device_free() for us. */
if (ntfs_umount(vol, 0))
return ret;
}
/**
* main
*/
{
unsigned long mnt_flags;
if ((mnt_flags & NTFS_MF_MOUNTED) &&
ntfs_log_error("Refusing to operate on read-write "
exit(1);
}
} else
ntfs_log_perror("Failed to determine whether %s is mounted",
/* Attempt a full mount first. */
ntfs_log_info("Mounting volume... ");
if (vol) {
ntfs_log_info("Processing of $MFT and $MFTMirr completed "
"successfully.\n");
} else {
if (fix_mount() < 0)
exit(1);
if (!vol) {
ntfs_log_perror("Remount failed");
exit(1);
}
}
/* So the unmount does not clear it again. */
/* Check NTFS version is ok for us (in $Volume) */
if (ntfs_version_is_supported(vol)) {
ntfs_log_error("Error: Unknown NTFS version.\n");
goto error_exit;
}
/*
* FIXME: If on NTFS 3.0+, check for presence of the usn
* journal and stamp it if present.
*/
}
/* FIXME: We should be marking the quota out of date, too. */
/* That's all for now! */
ntfs_log_info("NTFS partition %s was processed successfully.\n",
/* Set return code to 0. */
ret = 0;
if (ntfs_umount(vol, 0))
return ret;
}