2004-09-16 17:32:23 +08:00
|
|
|
/**
|
|
|
|
* ntfsmftalloc - Part of the Linux-NTFS project.
|
|
|
|
*
|
2005-10-07 22:10:56 +08:00
|
|
|
* Copyright (c) 2002-2005 Anton Altaparmakov
|
2004-09-16 17:32:23 +08:00
|
|
|
*
|
|
|
|
* This utility will allocate and initialize an mft record.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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_STDARG_H
|
|
|
|
# include <stdarg.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ERRNO_H
|
|
|
|
# include <errno.h>
|
|
|
|
#endif
|
2005-09-28 21:47:47 +08:00
|
|
|
#ifdef HAVE_TIME_H
|
2004-09-16 17:32:23 +08:00
|
|
|
#include <time.h>
|
2005-09-28 21:47:47 +08:00
|
|
|
#endif
|
2004-09-16 17:32:23 +08:00
|
|
|
#ifdef HAVE_GETOPT_H
|
|
|
|
# include <getopt.h>
|
|
|
|
#else
|
|
|
|
extern int optind;
|
|
|
|
#endif
|
2005-09-28 21:47:47 +08:00
|
|
|
#ifdef HAVE_LIMITS_H
|
2004-09-16 17:32:23 +08:00
|
|
|
#include <limits.h>
|
2005-09-28 21:47:47 +08:00
|
|
|
#endif
|
2004-09-16 17:32:23 +08:00
|
|
|
#ifndef LLONG_MAX
|
|
|
|
# define LLONG_MAX 9223372036854775807LL
|
|
|
|
#endif
|
|
|
|
|
2010-12-15 19:03:51 +08:00
|
|
|
#include "types.h"
|
|
|
|
#include "attrib.h"
|
|
|
|
#include "inode.h"
|
|
|
|
#include "layout.h"
|
|
|
|
#include "volume.h"
|
|
|
|
#include "mft.h"
|
2004-09-16 17:32:23 +08:00
|
|
|
#include "utils.h"
|
2010-11-22 22:26:27 +08:00
|
|
|
/* #include "version.h" */
|
2010-12-15 19:03:51 +08:00
|
|
|
#include "logging.h"
|
2004-09-16 17:32:23 +08:00
|
|
|
|
|
|
|
static const char *EXEC_NAME = "ntfsmftalloc";
|
|
|
|
|
|
|
|
/* Need these global so ntfsmftalloc_exit can access them. */
|
|
|
|
static BOOL success = FALSE;
|
|
|
|
|
|
|
|
static char *dev_name;
|
|
|
|
|
|
|
|
static ntfs_volume *vol;
|
|
|
|
static ntfs_inode *ni = NULL;
|
|
|
|
static ntfs_inode *base_ni = NULL;
|
|
|
|
static s64 base_mft_no = -1;
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
/* -h, print usage and exit. */
|
|
|
|
int no_action; /* -n, do not write to device, only display
|
|
|
|
what would be done. */
|
|
|
|
int quiet; /* -q, quiet execution. */
|
|
|
|
int verbose; /* -v, verbose execution, given twice, really
|
|
|
|
verbose execution (debug mode). */
|
|
|
|
int force; /* -f, force allocation. */
|
|
|
|
/* -V, print version and exit. */
|
|
|
|
} opts;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* err_exit - error output and terminate; ignores quiet (-q)
|
|
|
|
*/
|
2005-10-12 19:52:40 +08:00
|
|
|
__attribute__((noreturn))
|
|
|
|
__attribute__((format(printf, 1, 2)))
|
2004-09-16 17:32:23 +08:00
|
|
|
static void err_exit(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
fprintf(stderr, "ERROR: ");
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fprintf(stderr, "Aborting...\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* copyright - print copyright statements
|
|
|
|
*/
|
|
|
|
static void copyright(void)
|
|
|
|
{
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("Copyright (c) 2004-2005 Anton Altaparmakov\n"
|
2004-09-16 17:32:23 +08:00
|
|
|
"Allocate and initialize a base or an extent mft "
|
2004-09-16 18:10:20 +08:00
|
|
|
"record. If a base mft record\nis not specified, a "
|
|
|
|
"base mft record is allocated and initialized. "
|
|
|
|
"Otherwise,\nan extent mft record is allocated and "
|
|
|
|
"initialized to point to the specified\nbase mft "
|
2004-09-16 17:32:23 +08:00
|
|
|
"record.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-07-07 06:47:01 +08:00
|
|
|
* license - print license statement
|
2004-09-16 17:32:23 +08:00
|
|
|
*/
|
|
|
|
static void license(void)
|
|
|
|
{
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("%s", ntfs_gpl);
|
2004-09-16 17:32:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* usage - print a list of the parameters to the program
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
__attribute__((noreturn))
|
|
|
|
static void usage(void)
|
2004-09-16 17:32:23 +08:00
|
|
|
{
|
|
|
|
copyright();
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("Usage: %s [options] device [base-mft-record]\n"
|
2004-09-16 17:32:23 +08:00
|
|
|
" -n Do not write to disk\n"
|
|
|
|
" -f Force execution despite errors\n"
|
|
|
|
" -q Quiet execution\n"
|
|
|
|
" -v Verbose execution\n"
|
|
|
|
" -vv Very verbose execution\n"
|
|
|
|
" -V Display version information\n"
|
|
|
|
" -l Display licensing information\n"
|
|
|
|
" -h Display this help\n", EXEC_NAME);
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("%s%s", ntfs_bugs, ntfs_home);
|
2004-09-16 17:32:23 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* parse_options
|
|
|
|
*/
|
|
|
|
static void parse_options(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
long long ll;
|
|
|
|
char *s;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (argc && *argv)
|
|
|
|
EXEC_NAME = *argv;
|
2010-11-22 21:00:04 +08:00
|
|
|
ntfs_log_info("%s v%s (libntfs-3g)\n", EXEC_NAME, VERSION);
|
2005-11-20 22:15:33 +08:00
|
|
|
while ((c = getopt(argc, argv, "fh?nqvVl")) != EOF) {
|
2004-09-16 17:32:23 +08:00
|
|
|
switch (c) {
|
|
|
|
case 'f':
|
|
|
|
opts.force = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
opts.no_action = 1;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
opts.quiet = 1;
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET);
|
2004-09-16 17:32:23 +08:00
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
opts.verbose++;
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE);
|
2004-09-16 17:32:23 +08:00
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
/* Version number already printed, so just exit. */
|
|
|
|
exit(0);
|
|
|
|
case 'l':
|
|
|
|
copyright();
|
|
|
|
license();
|
|
|
|
exit(0);
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
2005-11-20 22:15:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.verbose > 1)
|
|
|
|
ntfs_log_set_levels(NTFS_LOG_LEVEL_DEBUG | NTFS_LOG_LEVEL_TRACE |
|
|
|
|
NTFS_LOG_LEVEL_VERBOSE | NTFS_LOG_LEVEL_QUIET);
|
|
|
|
|
2004-09-16 17:32:23 +08:00
|
|
|
if (optind == argc)
|
|
|
|
usage();
|
|
|
|
/* Get the device. */
|
|
|
|
dev_name = argv[optind++];
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_verbose("device name = %s\n", dev_name);
|
2004-09-16 17:32:23 +08:00
|
|
|
if (optind != argc) {
|
|
|
|
/* Get the base mft record number. */
|
|
|
|
ll = strtoll(argv[optind++], &s, 0);
|
|
|
|
if (*s || !ll || (ll >= LLONG_MAX && errno == ERANGE))
|
|
|
|
err_exit("Invalid base mft record number: %s\n",
|
|
|
|
argv[optind - 1]);
|
|
|
|
base_mft_no = ll;
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_verbose("base mft record number = 0x%llx\n", (long long)ll);
|
2004-09-16 17:32:23 +08:00
|
|
|
}
|
|
|
|
if (optind != argc)
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dump_mft_record
|
|
|
|
*/
|
|
|
|
static void dump_mft_record(MFT_RECORD *m)
|
|
|
|
{
|
|
|
|
ATTR_RECORD *a;
|
|
|
|
unsigned int u;
|
|
|
|
MFT_REF r;
|
|
|
|
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("-- Beginning dump of mft record. --\n");
|
2004-09-16 17:32:23 +08:00
|
|
|
u = le32_to_cpu(m->magic);
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("Mft record signature (magic) = %c%c%c%c\n", u & 0xff,
|
2004-09-16 17:32:23 +08:00
|
|
|
u >> 8 & 0xff, u >> 16 & 0xff, u >> 24 & 0xff);
|
|
|
|
u = le16_to_cpu(m->usa_ofs);
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("Update sequence array offset = %u (0x%x)\n", u, u);
|
|
|
|
ntfs_log_info("Update sequence array size = %u\n", le16_to_cpu(m->usa_count));
|
|
|
|
ntfs_log_info("$LogFile sequence number (lsn) = %llu\n",
|
2015-12-22 06:55:31 +08:00
|
|
|
(unsigned long long)sle64_to_cpu(m->lsn));
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("Sequence number = %u\n", le16_to_cpu(m->sequence_number));
|
|
|
|
ntfs_log_info("Reference (hard link) count = %u\n",
|
2004-09-16 17:32:23 +08:00
|
|
|
le16_to_cpu(m->link_count));
|
|
|
|
u = le16_to_cpu(m->attrs_offset);
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("First attribute offset = %u (0x%x)\n", u, u);
|
|
|
|
ntfs_log_info("Flags = %u: ", le16_to_cpu(m->flags));
|
2004-09-16 17:32:23 +08:00
|
|
|
if (m->flags & MFT_RECORD_IN_USE)
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("MFT_RECORD_IN_USE");
|
2004-09-16 17:32:23 +08:00
|
|
|
else
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("MFT_RECORD_NOT_IN_USE");
|
2004-09-16 17:32:23 +08:00
|
|
|
if (m->flags & MFT_RECORD_IS_DIRECTORY)
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info(" | MFT_RECORD_IS_DIRECTORY");
|
|
|
|
ntfs_log_info("\n");
|
2004-09-16 17:32:23 +08:00
|
|
|
u = le32_to_cpu(m->bytes_in_use);
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("Bytes in use = %u (0x%x)\n", u, u);
|
2004-09-16 17:32:23 +08:00
|
|
|
u = le32_to_cpu(m->bytes_allocated);
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("Bytes allocated = %u (0x%x)\n", u, u);
|
2004-09-16 17:32:23 +08:00
|
|
|
r = le64_to_cpu(m->base_mft_record);
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("Base mft record reference:\n\tMft record number = %llu\n\t"
|
2004-09-16 17:32:23 +08:00
|
|
|
"Sequence number = %u\n",
|
|
|
|
(unsigned long long)MREF(r), MSEQNO(r));
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("Next attribute instance = %u\n",
|
2004-09-16 17:32:23 +08:00
|
|
|
le16_to_cpu(m->next_attr_instance));
|
|
|
|
a = (ATTR_RECORD*)((char*)m + le16_to_cpu(m->attrs_offset));
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("-- Beginning dump of attributes within mft record. --\n");
|
2004-09-16 17:32:23 +08:00
|
|
|
while ((char*)a < (char*)m + le32_to_cpu(m->bytes_in_use)) {
|
|
|
|
if (a->type == AT_END)
|
|
|
|
break;
|
|
|
|
a = (ATTR_RECORD*)((char*)a + le32_to_cpu(a->length));
|
|
|
|
};
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("-- End of attributes. --\n");
|
2004-09-16 17:32:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ntfsmftalloc_exit
|
|
|
|
*/
|
|
|
|
static void ntfsmftalloc_exit(void)
|
|
|
|
{
|
|
|
|
if (success)
|
|
|
|
return;
|
|
|
|
/* If there is a base inode, close that instead of the extent inode. */
|
|
|
|
if (base_ni)
|
|
|
|
ni = base_ni;
|
|
|
|
/* Close the inode. */
|
|
|
|
if (ni && ntfs_inode_close(ni)) {
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_perror("Warning: Failed to close inode 0x%llx",
|
|
|
|
(long long)ni->mft_no);
|
2004-09-16 17:32:23 +08:00
|
|
|
}
|
|
|
|
/* Unmount the volume. */
|
|
|
|
if (ntfs_umount(vol, 0) == -1)
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_perror("Warning: Could not umount %s", dev_name);
|
2004-09-16 17:32:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* main
|
|
|
|
*/
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
unsigned long mnt_flags, ul;
|
|
|
|
int err;
|
|
|
|
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_set_handler(ntfs_log_handler_outerr);
|
|
|
|
|
2004-09-16 17:32:23 +08:00
|
|
|
/* Initialize opts to zero / required values. */
|
|
|
|
memset(&opts, 0, sizeof(opts));
|
|
|
|
/* Parse command line options. */
|
|
|
|
parse_options(argc, argv);
|
|
|
|
utils_set_locale();
|
|
|
|
/* Make sure the file system is not mounted. */
|
|
|
|
if (ntfs_check_if_mounted(dev_name, &mnt_flags))
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_error("Failed to determine whether %s is mounted: %s\n",
|
2004-09-16 17:32:23 +08:00
|
|
|
dev_name, strerror(errno));
|
|
|
|
else if (mnt_flags & NTFS_MF_MOUNTED) {
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_error("%s is mounted.\n", dev_name);
|
2004-09-16 17:32:23 +08:00
|
|
|
if (!opts.force)
|
|
|
|
err_exit("Refusing to run!\n");
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_error("ntfsmftalloc forced anyway. Hope /etc/mtab "
|
2004-09-16 17:32:23 +08:00
|
|
|
"is incorrect.\n");
|
|
|
|
}
|
|
|
|
/* Mount the device. */
|
|
|
|
if (opts.no_action) {
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_quiet("Running in READ-ONLY mode!\n");
|
2012-11-07 23:29:48 +08:00
|
|
|
ul = NTFS_MNT_RDONLY;
|
2004-09-16 17:32:23 +08:00
|
|
|
} else
|
|
|
|
ul = 0;
|
|
|
|
vol = ntfs_mount(dev_name, ul);
|
|
|
|
if (!vol)
|
|
|
|
err_exit("Failed to mount %s: %s\n", dev_name, strerror(errno));
|
|
|
|
/* Register our exit function which will unlock and close the device. */
|
|
|
|
err = atexit(&ntfsmftalloc_exit);
|
|
|
|
if (err == -1) {
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_error("Could not set up exit() function because atexit() "
|
2004-09-16 17:32:23 +08:00
|
|
|
"failed: %s Aborting...\n", strerror(errno));
|
|
|
|
ntfsmftalloc_exit();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (base_mft_no != -1) {
|
|
|
|
base_ni = ntfs_inode_open(vol, base_mft_no);
|
|
|
|
if (!base_ni)
|
|
|
|
err_exit("Failed to open base inode 0x%llx: %s\n",
|
|
|
|
(long long)base_mft_no,
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
/* Open the specified inode. */
|
|
|
|
ni = ntfs_mft_record_alloc(vol, base_ni);
|
|
|
|
if (!ni)
|
|
|
|
err_exit("Failed to allocate mft record: %s\n",
|
|
|
|
strerror(errno));
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info("Allocated %s mft record 0x%llx", base_ni ? "extent" : "base",
|
2004-09-16 17:32:23 +08:00
|
|
|
(long long)ni->mft_no);
|
|
|
|
if (base_ni)
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info(" with base mft record 0x%llx",
|
2004-09-16 17:32:23 +08:00
|
|
|
(long long)base_mft_no);
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_info(".\n");
|
2004-09-16 17:32:23 +08:00
|
|
|
if (!opts.quiet && opts.verbose > 1) {
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_verbose("Dumping allocated mft record 0x%llx:\n",
|
2004-09-16 17:32:23 +08:00
|
|
|
(long long)ni->mft_no);
|
|
|
|
dump_mft_record(ni->mrec);
|
|
|
|
}
|
|
|
|
/* Close the (base) inode. */
|
|
|
|
if (base_ni)
|
|
|
|
ni = base_ni;
|
|
|
|
err = ntfs_inode_close(ni);
|
|
|
|
if (err)
|
|
|
|
err_exit("Failed to close inode 0x%llx: %s\n",
|
|
|
|
(long long)ni->mft_no, strerror(errno));
|
|
|
|
/* Unmount the volume. */
|
|
|
|
err = ntfs_umount(vol, 0);
|
|
|
|
/* Disable our ntfsmftalloc_exit() handler. */
|
|
|
|
success = TRUE;
|
|
|
|
if (err == -1)
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_perror("Warning: Failed to umount %s", dev_name);
|
2004-09-16 17:32:23 +08:00
|
|
|
else
|
2005-11-20 22:15:33 +08:00
|
|
|
ntfs_log_quiet("ntfsmftalloc completed successfully.\n");
|
2004-09-16 17:32:23 +08:00
|
|
|
return 0;
|
|
|
|
}
|