1N/A/**
1N/A * mkntfs - Part of the Linux-NTFS project.
1N/A *
1N/A * Copyright (c) 2000-2007 Anton Altaparmakov
1N/A * Copyright (c) 2001-2005 Richard Russon
1N/A * Copyright (c) 2002-2006 Szabolcs Szakacsits
1N/A * Copyright (c) 2005 Erik Sornes
1N/A * Copyright (c) 2007 Yura Pakhuchiy
1N/A *
1N/A * This utility will create an NTFS 1.2 or 3.1 volume on a user
1N/A * specified (block) device.
1N/A *
1N/A * Some things (option handling and determination of mount status) have been
1N/A * adapted from e2fsprogs-1.19 and lib/ext2fs/ismounted.c and misc/mke2fs.c in
1N/A * particular.
1N/A *
1N/A * This program is free software; you can redistribute it and/or modify
1N/A * it under the terms of the GNU General Public License as published by
1N/A * the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program is distributed in the hope that it will be useful,
1N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program (in the main directory of the Linux-NTFS source
1N/A * in the file COPYING); if not, write to the Free Software Foundation,
1N/A * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1N/A */
1N/A
1N/A#ifdef HAVE_CONFIG_H
1N/A#include "config.h"
1N/A#endif
1N/A
1N/A#ifdef HAVE_UNISTD_H
1N/A#include <unistd.h>
1N/A#endif
1N/A#ifdef HAVE_STDLIB_H
1N/A#include <stdlib.h>
1N/A#endif
1N/A#ifdef HAVE_STDIO_H
1N/A#include <stdio.h>
1N/A#endif
1N/A#ifdef HAVE_STDARG_H
1N/A#include <stdarg.h>
1N/A#endif
1N/A#ifdef HAVE_STRING_H
1N/A#include <string.h>
1N/A#endif
1N/A#ifdef HAVE_ERRNO_H
1N/A#include <errno.h>
1N/A#endif
1N/A#ifdef HAVE_TIME_H
1N/A#include <time.h>
1N/A#endif
1N/A#ifdef HAVE_SYS_STAT_H
1N/A#include <sys/stat.h>
1N/A#endif
1N/A#ifdef HAVE_FCNTL_H
1N/A#include <fcntl.h>
1N/A#endif
1N/A#ifdef HAVE_LIMITS_H
1N/A#include <limits.h>
1N/A#endif
1N/A#ifdef HAVE_LIBGEN_H
1N/A#include <libgen.h>
1N/A#endif
1N/A#ifdef ENABLE_UUID
1N/A#include <uuid/uuid.h>
1N/A#endif
1N/A
1N/A
1N/A#ifdef HAVE_GETOPT_H
1N/A#include <getopt.h>
1N/A#else
1N/A extern char *optarg;
1N/A extern int optind;
1N/A#endif
1N/A
1N/A#ifdef HAVE_LINUX_MAJOR_H
1N/A# include <linux/major.h>
1N/A# ifndef MAJOR
1N/A# define MAJOR(dev) ((dev) >> 8)
1N/A# define MINOR(dev) ((dev) & 0xff)
1N/A# endif
1N/A# ifndef IDE_DISK_MAJOR
1N/A# ifndef IDE0_MAJOR
1N/A# define IDE0_MAJOR 3
1N/A# define IDE1_MAJOR 22
1N/A# define IDE2_MAJOR 33
1N/A# define IDE3_MAJOR 34
1N/A# define IDE4_MAJOR 56
1N/A# define IDE5_MAJOR 57
1N/A# define IDE6_MAJOR 88
1N/A# define IDE7_MAJOR 89
1N/A# define IDE8_MAJOR 90
1N/A# define IDE9_MAJOR 91
1N/A# endif
1N/A# define IDE_DISK_MAJOR(M) \
1N/A ((M) == IDE0_MAJOR || (M) == IDE1_MAJOR || \
1N/A (M) == IDE2_MAJOR || (M) == IDE3_MAJOR || \
1N/A (M) == IDE4_MAJOR || (M) == IDE5_MAJOR || \
1N/A (M) == IDE6_MAJOR || (M) == IDE7_MAJOR || \
1N/A (M) == IDE8_MAJOR || (M) == IDE9_MAJOR)
1N/A# endif
1N/A# ifndef SCSI_DISK_MAJOR
1N/A# ifndef SCSI_DISK0_MAJOR
1N/A# define SCSI_DISK0_MAJOR 8
1N/A# define SCSI_DISK1_MAJOR 65
1N/A# define SCSI_DISK7_MAJOR 71
1N/A# endif
1N/A# define SCSI_DISK_MAJOR(M) \
1N/A ((M) == SCSI_DISK0_MAJOR || \
1N/A ((M) >= SCSI_DISK1_MAJOR && \
1N/A (M) <= SCSI_DISK7_MAJOR))
1N/A# endif
1N/A#endif
1N/A
1N/A#include "compat.h"
1N/A#include "security.h"
1N/A#include "types.h"
1N/A#include "attrib.h"
1N/A#include "bitmap.h"
1N/A#include "bootsect.h"
1N/A#include "device.h"
1N/A#include "dir.h"
1N/A#include "mft.h"
1N/A#include "mst.h"
1N/A#include "runlist.h"
1N/A#include "utils.h"
1N/A#include "ntfstime.h"
1N/A#include "sd.h"
1N/A#include "boot.h"
1N/A#include "attrdef.h"
1N/A#include "version.h"
1N/A#include "logging.h"
1N/A#include "support.h"
1N/A#include "unistr.h"
1N/A
1N/A#ifdef NO_NTFS_DEVICE_DEFAULT_IO_OPS
1N/A#error "No default device io operations! Cannot build mkntfs. \
1N/AYou need to run ./configure without the --disable-default-device-io-ops \
1N/Aswitch if you want to be able to build the NTFS utilities."
1N/A#endif
1N/A
1N/A/* Page size on ia32. Can change to 8192 on Alpha. */
1N/A#define NTFS_PAGE_SIZE 4096
1N/A
1N/Astatic char EXEC_NAME[] = "mkntfs";
1N/A
1N/A/**
1N/A * global variables
1N/A */
1N/Astatic u8 *g_buf = NULL;
1N/Astatic int g_mft_bitmap_byte_size = 0;
1N/Astatic u8 *g_mft_bitmap = NULL;
1N/Astatic int g_lcn_bitmap_byte_size = 0;
1N/Astatic u8 *g_lcn_bitmap = NULL;
1N/Astatic runlist *g_rl_mft = NULL;
1N/Astatic runlist *g_rl_mft_bmp = NULL;
1N/Astatic runlist *g_rl_mftmirr = NULL;
1N/Astatic runlist *g_rl_logfile = NULL;
1N/Astatic runlist *g_rl_boot = NULL;
1N/Astatic runlist *g_rl_bad = NULL;
1N/Astatic INDEX_ALLOCATION *g_index_block = NULL;
1N/Astatic ntfs_volume *g_vol = NULL;
1N/Astatic int g_mft_size = 0;
1N/Astatic long long g_mft_lcn = 0; /* lcn of $MFT, $DATA attribute */
1N/Astatic long long g_mftmirr_lcn = 0; /* lcn of $MFTMirr, $DATA */
1N/Astatic long long g_logfile_lcn = 0; /* lcn of $LogFile, $DATA */
1N/Astatic int g_logfile_size = 0; /* in bytes, determined from volume_size */
1N/Astatic long long g_mft_zone_end = 0; /* Determined from volume_size and mft_zone_multiplier, in clusters */
1N/Astatic long long g_num_bad_blocks = 0; /* Number of bad clusters */
1N/Astatic long long *g_bad_blocks = NULL; /* Array of bad clusters */
1N/A
1N/A/**
1N/A * struct mkntfs_options
1N/A */
1N/Astatic struct mkntfs_options {
1N/A char *dev_name; /* Name of the device, or file, to use */
1N/A BOOL enable_compression; /* -C, enables compression of all files on the volume by default. */
1N/A BOOL quick_format; /* -f or -Q, fast format, don't zero the volume first. */
1N/A BOOL force; /* -F, force fs creation. */
1N/A long heads; /* -H, number of heads on device */
1N/A BOOL disable_indexing; /* -I, disables indexing of file contents on the volume by default. */
1N/A BOOL no_action; /* -n, do not write to device, only display what would be done. */
1N/A long long part_start_sect; /* -p, start sector of partition on parent device */
1N/A long sector_size; /* -s, in bytes, power of 2, default is 512 bytes. */
1N/A long sectors_per_track; /* -S, number of sectors per track on device */
1N/A BOOL use_epoch_time; /* -T, fake the time to be 00:00:00 UTC, Jan 1, 1970. */
1N/A long mft_zone_multiplier; /* -z, value from 1 to 4. Default is 1. */
1N/A long long num_sectors; /* size of device in sectors */
1N/A long cluster_size; /* -c, format with this cluster-size */
1N/A char *label; /* -L, volume label */
1N/A} opts;
1N/A
1N/A
1N/A/**
1N/A * mkntfs_license
1N/A */
1N/Astatic void mkntfs_license(void)
1N/A{
1N/A ntfs_log_info("%s", ntfs_gpl);
1N/A}
1N/A
1N/A/**
1N/A * mkntfs_usage
1N/A */
1N/Astatic void mkntfs_usage(void)
1N/A{
1N/A ntfs_log_info("\nUsage: %s [options] device [number-of-sectors]\n"
1N/A"\n"
1N/A"Basic options:\n"
1N/A" -f, --fast Perform a quick format\n"
1N/A" -Q, --quick Perform a quick format\n"
1N/A" -L, --label STRING Set the volume label\n"
1N/A" -C, --enable-compression Enable compression on the volume\n"
1N/A" -I, --no-indexing Disable indexing on the volume\n"
1N/A" -n, --no-action Do not write to disk\n"
1N/A"\n"
1N/A"Advanced options:\n"
1N/A" -c, --cluster-size BYTES Specify the cluster size for the volume\n"
1N/A" -s, --sector-size BYTES Specify the sector size for the device\n"
1N/A" -p, --partition-start SECTOR Specify the partition start sector\n"
1N/A" -H, --heads NUM Specify the number of heads\n"
1N/A" -S, --sectors-per-track NUM Specify the number of sectors per track\n"
1N/A" -z, --mft-zone-multiplier NUM Set the MFT zone multiplier\n"
1N/A" -T, --zero-time Fake the time to be 00:00 UTC, Jan 1, 1970\n"
1N/A" -F, --force Force execution despite errors\n"
1N/A"\n"
1N/A"Output options:\n"
1N/A" -q, --quiet Quiet execution\n"
1N/A" -v, --verbose Verbose execution\n"
1N/A" --debug Very verbose execution\n"
1N/A"\n"
1N/A"Help options:\n"
1N/A" -V, --version Display version\n"
1N/A" -l, --license Display licensing information\n"
1N/A" -h, --help Display this help\n"
1N/A"\n", basename(EXEC_NAME));
1N/A ntfs_log_info("%s%s\n", ntfs_bugs, ntfs_home);
1N/A}
1N/A
1N/A/**
1N/A * mkntfs_version
1N/A */
1N/Astatic void mkntfs_version(void)
1N/A{
1N/A ntfs_log_info("\n%s v%s (libntfs %s)\n\n", EXEC_NAME, VERSION,
1N/A ntfs_libntfs_version());
1N/A ntfs_log_info("Create an NTFS volume on a user specified (block) "
1N/A "device.\n\n");
1N/A ntfs_log_info("Copyright (c) 2000-2007 Anton Altaparmakov\n");
1N/A ntfs_log_info("Copyright (c) 2001-2005 Richard Russon\n");
1N/A ntfs_log_info("Copyright (c) 2002-2006 Szabolcs Szakacsits\n");
1N/A ntfs_log_info("Copyright (c) 2005 Erik Sornes\n");
1N/A ntfs_log_info("Copyright (c) 2007 Yura Pakhuchiy\n");
1N/A ntfs_log_info("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home);
1N/A}
1N/A
1N/A
1N/A/**
1N/A * mkntfs_parse_long
1N/A */
1N/Astatic BOOL mkntfs_parse_long(const char *string, const char *name, long *num)
1N/A{
1N/A char *end = NULL;
1N/A long tmp;
1N/A
1N/A if (!string || !name || !num)
1N/A return FALSE;
1N/A
1N/A if (*num >= 0) {
1N/A ntfs_log_error("You may only specify the %s once.\n", name);
1N/A return FALSE;
1N/A }
1N/A
1N/A tmp = strtol(string, &end, 0);
1N/A if (end && *end) {
1N/A ntfs_log_error("Cannot understand the %s '%s'.\n", name, string);
1N/A return FALSE;
1N/A } else {
1N/A *num = tmp;
1N/A return TRUE;
1N/A }
1N/A}
1N/A
1N/A/**
1N/A * mkntfs_parse_llong
1N/A */
1N/Astatic BOOL mkntfs_parse_llong(const char *string, const char *name,
1N/A long long *num)
1N/A{
1N/A char *end = NULL;
1N/A long long tmp;
1N/A
1N/A if (!string || !name || !num)
1N/A return FALSE;
1N/A
1N/A if (*num >= 0) {
1N/A ntfs_log_error("You may only specify the %s once.\n", name);
1N/A return FALSE;
1N/A }
1N/A
1N/A tmp = strtoll(string, &end, 0);
1N/A if (end && *end) {
1N/A ntfs_log_error("Cannot understand the %s '%s'.\n", name,
1N/A string);
1N/A return FALSE;
1N/A } else {
1N/A *num = tmp;
1N/A return TRUE;
1N/A }
1N/A}
1N/A
1N/A/**
1N/A * mkntfs_init_options
1N/A */
1N/Astatic void mkntfs_init_options(struct mkntfs_options *opts2)
1N/A{
1N/A if (!opts2)
1N/A return;
1N/A
1N/A memset(opts2, 0, sizeof(*opts2));
1N/A
1N/A /* Mark all the numeric options as "unset". */
1N/A opts2->cluster_size = -1;
1N/A opts2->heads = -1;
1N/A opts2->mft_zone_multiplier = -1;
1N/A opts2->num_sectors = -1;
1N/A opts2->part_start_sect = -1;
1N/A opts2->sector_size = -1;
1N/A opts2->sectors_per_track = -1;
1N/A}
1N/A
1N/A/**
1N/A * mkntfs_parse_options
1N/A */
1N/Astatic BOOL mkntfs_parse_options(int argc, char *argv[], struct mkntfs_options *opts2)
1N/A{
1N/A static const char *sopt = "-c:CfFhH:IlL:np:qQs:S:TvVz:";
1N/A static const struct option lopt[] = {
1N/A { "cluster-size", required_argument, NULL, 'c' },
1N/A { "debug", no_argument, NULL, 'Z' },
1N/A { "enable-compression", no_argument, NULL, 'C' },
1N/A { "fast", no_argument, NULL, 'f' },
1N/A { "force", no_argument, NULL, 'F' },
1N/A { "heads", required_argument, NULL, 'H' },
1N/A { "help", no_argument, NULL, 'h' },
1N/A { "label", required_argument, NULL, 'L' },
1N/A { "license", no_argument, NULL, 'l' },
1N/A { "mft-zone-multiplier",required_argument, NULL, 'z' },
1N/A { "no-action", no_argument, NULL, 'n' },
1N/A { "no-indexing", no_argument, NULL, 'I' },
1N/A { "partition-start", required_argument, NULL, 'p' },
1N/A { "quick", no_argument, NULL, 'Q' },
1N/A { "quiet", no_argument, NULL, 'q' },
1N/A { "sector-size", required_argument, NULL, 's' },
1N/A { "sectors-per-track", required_argument, NULL, 'S' },
1N/A { "verbose", no_argument, NULL, 'v' },
1N/A { "version", no_argument, NULL, 'V' },
1N/A { "zero-time", no_argument, NULL, 'T' },
1N/A { NULL, 0, NULL, 0 }
1N/A };
1N/A
1N/A int c = -1;
1N/A int lic = 0;
1N/A int err = 0;
1N/A int ver = 0;
1N/A
1N/A if (!argv || !opts2) {
1N/A ntfs_log_error("Internal error: invalid parameters to "
1N/A "mkntfs_options.\n");
1N/A return FALSE;
1N/A }
1N/A
1N/A opterr = 0; /* We'll handle the errors, thank you. */
1N/A
1N/A while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) {
1N/A switch (c) {
1N/A case 1: /* A device, or a number of sectors */
1N/A if (!opts2->dev_name)
1N/A opts2->dev_name = argv[optind - 1];
1N/A else if (!mkntfs_parse_llong(optarg,
1N/A "number of sectors",
1N/A &opts2->num_sectors))
1N/A err++;
1N/A break;
1N/A case 'C':
1N/A opts2->enable_compression = TRUE;
1N/A break;
1N/A case 'c':
1N/A if (!mkntfs_parse_long(optarg, "cluster size",
1N/A &opts2->cluster_size))
1N/A err++;
1N/A break;
1N/A case 'F':
1N/A opts2->force = TRUE;
1N/A break;
1N/A case 'f': /* fast */
1N/A case 'Q': /* quick */
1N/A opts2->quick_format = TRUE;
1N/A break;
1N/A case 'H':
1N/A if (!mkntfs_parse_long(optarg, "heads", &opts2->heads))
1N/A err++;
1N/A break;
1N/A case 'h':
1N/A err++; /* display help */
1N/A break;
1N/A case 'I':
1N/A opts2->disable_indexing = TRUE;
1N/A break;
1N/A case 'L':
1N/A if (!opts2->label) {
1N/A opts2->label = argv[optind-1];
1N/A } else {
1N/A ntfs_log_error("You may only specify the label "
1N/A "once.\n");
1N/A err++;
1N/A }
1N/A break;
1N/A case 'l':
1N/A lic++; /* display the license */
1N/A break;
1N/A case 'n':
1N/A opts2->no_action = TRUE;
1N/A break;
1N/A case 'p':
1N/A if (!mkntfs_parse_llong(optarg, "partition start",
1N/A &opts2->part_start_sect))
1N/A err++;
1N/A break;
1N/A case 'q':
1N/A ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET |
1N/A NTFS_LOG_LEVEL_VERBOSE |
1N/A NTFS_LOG_LEVEL_PROGRESS);
1N/A break;
1N/A case 's':
1N/A if (!mkntfs_parse_long(optarg, "sector size",
1N/A &opts2->sector_size))
1N/A err++;
1N/A break;
1N/A case 'S':
1N/A if (!mkntfs_parse_long(optarg, "sectors per track",
1N/A &opts2->sectors_per_track))
1N/A err++;
1N/A break;
1N/A case 'T':
1N/A opts2->use_epoch_time = TRUE;
1N/A break;
1N/A case 'v':
1N/A ntfs_log_set_levels(NTFS_LOG_LEVEL_QUIET |
1N/A NTFS_LOG_LEVEL_VERBOSE |
1N/A NTFS_LOG_LEVEL_PROGRESS);
1N/A break;
1N/A case 'V':
1N/A ver++; /* display version info */
1N/A break;
1N/A case 'Z': /* debug - turn on everything */
1N/A ntfs_log_set_levels(NTFS_LOG_LEVEL_DEBUG |
1N/A NTFS_LOG_LEVEL_TRACE |
1N/A NTFS_LOG_LEVEL_VERBOSE |
1N/A NTFS_LOG_LEVEL_QUIET);
1N/A break;
1N/A case 'z':
1N/A if (!mkntfs_parse_long(optarg, "mft zone multiplier",
1N/A &opts2->mft_zone_multiplier))
1N/A err++;
1N/A break;
1N/A default:
1N/A if (ntfs_log_parse_option (argv[optind-1]))
1N/A break;
1N/A if (((optopt == 'c') || (optopt == 'H') ||
1N/A (optopt == 'L') || (optopt == 'p') ||
1N/A (optopt == 's') || (optopt == 'S') ||
1N/A (optopt == 'N') || (optopt == 'z')) &&
1N/A (!optarg)) {
1N/A ntfs_log_error("Option '%s' requires an "
1N/A "argument.\n", argv[optind-1]);
1N/A } else if (optopt != '?') {
1N/A ntfs_log_error("Unknown option '%s'.\n",
1N/A argv[optind - 1]);
1N/A }
1N/A err++;
1N/A break;
1N/A }
1N/A }
1N/A
1N/A if (!err && !ver && !lic) {
1N/A if (opts2->dev_name == NULL) {
1N/A if (argc > 1)
1N/A ntfs_log_error("You must specify a device.\n");
1N/A err++;
1N/A }
1N/A }
1N/A
1N/A if (ver)
1N/A mkntfs_version();
1N/A if (lic)
1N/A mkntfs_license();
1N/A if (err)
1N/A mkntfs_usage();
1N/A
1N/A return (!err && !ver && !lic);
1N/A}
1N/A
1N/A
1N/A/**
1N/A * mkntfs_time
1N/A */
1N/Astatic time_t mkntfs_time(void)
1N/A{
1N/A if (!opts.use_epoch_time)
1N/A return time(NULL);
1N/A return 0;
1N/A}
1N/A
1N/A/**
1N/A * append_to_bad_blocks
1N/A */
1N/Astatic BOOL append_to_bad_blocks(unsigned long long block)
1N/A{
1N/A long long *new_buf;
1N/A
1N/A if (!(g_num_bad_blocks & 15)) {
1N/A new_buf = realloc(g_bad_blocks, (g_num_bad_blocks + 16) *
1N/A sizeof(long long));
1N/A if (!new_buf) {
1N/A ntfs_log_perror("Reallocating memory for bad blocks "
1N/A "list failed");
1N/A return FALSE;
1N/A }
1N/A g_bad_blocks = new_buf;
1N/A }
1N/A g_bad_blocks[g_num_bad_blocks++] = block;
1N/A return TRUE;
1N/A}
1N/A
1N/A/**
1N/A * mkntfs_write
1N/A */
1N/Astatic long long mkntfs_write(struct ntfs_device *dev,
1N/A const void *b, long long count)
1N/A{
1N/A long long bytes_written, total;
1N/A int retry;
1N/A
1N/A if (opts.no_action)
1N/A return count;
1N/A total = 0LL;
1N/A retry = 0;
1N/A do {
1N/A bytes_written = dev->d_ops->write(dev, b, count);
1N/A if (bytes_written == -1LL) {
1N/A retry = errno;
1N/A ntfs_log_perror("Error writing to %s", dev->d_name);
1N/A errno = retry;
1N/A return bytes_written;
1N/A } else if (!bytes_written) {
1N/A retry++;
1N/A } else {
1N/A count -= bytes_written;
1N/A total += bytes_written;
1N/A }
1N/A } while (count && retry < 3);
1N/A if (count)
1N/A ntfs_log_error("Failed to complete writing to %s after three retries."
1N/A "\n", dev->d_name);
1N/A return total;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_rlwrite - Write data to disk on clusters found in a runlist.
1N/A *
1N/A * Write to disk the clusters contained in the runlist @rl taking the data
1N/A * from @val. Take @val_len bytes from @val and pad the rest with zeroes.
1N/A *
1N/A * If the @rl specifies a completely sparse file, @val is allowed to be NULL.
1N/A *
1N/A * @inited_size if not NULL points to an output variable which will contain
1N/A * the actual number of bytes written to disk. I.e. this will not include
1N/A * sparse bytes for example.
1N/A *
1N/A * Return the number of bytes written (minus padding) or -1 on error. Errno
1N/A * will be set to the error code.
1N/A */
1N/Astatic s64 ntfs_rlwrite(struct ntfs_device *dev, const runlist *rl,
1N/A const u8 *val, const s64 val_len, s64 *inited_size)
1N/A{
1N/A s64 bytes_written, total, length, delta;
1N/A int retry, i;
1N/A
1N/A if (inited_size)
1N/A *inited_size = 0LL;
1N/A if (opts.no_action)
1N/A return val_len;
1N/A total = 0LL;
1N/A delta = 0LL;
1N/A for (i = 0; rl[i].length; i++) {
1N/A length = rl[i].length * g_vol->cluster_size;
1N/A /* Don't write sparse runs. */
1N/A if (rl[i].lcn == -1) {
1N/A total += length;
1N/A if (!val)
1N/A continue;
1N/A /* TODO: Check that *val is really zero at pos and len. */
1N/A continue;
1N/A }
1N/A /*
1N/A * Break up the write into the real data write and then a write
1N/A * of zeroes between the end of the real data and the end of
1N/A * the (last) run.
1N/A */
1N/A if (total + length > val_len) {
1N/A delta = length;
1N/A length = val_len - total;
1N/A delta -= length;
1N/A }
1N/A if (dev->d_ops->seek(dev, rl[i].lcn * g_vol->cluster_size,
1N/A SEEK_SET) == (off_t)-1)
1N/A return -1LL;
1N/A retry = 0;
1N/A do {
1N/A bytes_written = dev->d_ops->write(dev, val + total,
1N/A length);
1N/A if (bytes_written == -1LL) {
1N/A retry = errno;
1N/A ntfs_log_perror("Error writing to %s",
1N/A dev->d_name);
1N/A errno = retry;
1N/A return bytes_written;
1N/A }
1N/A if (bytes_written) {
1N/A length -= bytes_written;
1N/A total += bytes_written;
1N/A if (inited_size)
1N/A *inited_size += bytes_written;
1N/A } else {
1N/A retry++;
1N/A }
1N/A } while (length && retry < 3);
1N/A if (length) {
1N/A ntfs_log_error("Failed to complete writing to %s after three "
1N/A "retries.\n", dev->d_name);
1N/A return total;
1N/A }
1N/A }
1N/A if (delta) {
1N/A int eo;
1N/A char *b = ntfs_calloc(delta);
1N/A if (!b)
1N/A return -1;
1N/A bytes_written = mkntfs_write(dev, b, delta);
1N/A eo = errno;
1N/A free(b);
1N/A errno = eo;
1N/A if (bytes_written == -1LL)
1N/A return bytes_written;
1N/A }
1N/A return total;
1N/A}
1N/A
1N/A/**
1N/A * make_room_for_attribute - make room for an attribute inside an mft record
1N/A * @m: mft record
1N/A * @pos: position at which to make space
1N/A * @size: byte size to make available at this position
1N/A *
1N/A * @pos points to the attribute in front of which we want to make space.
1N/A *
1N/A * Return 0 on success or -errno on error. Possible error codes are:
1N/A *
1N/A * -ENOSPC There is not enough space available to complete
1N/A * operation. The caller has to make space before calling
1N/A * this.
1N/A * -EINVAL Can only occur if mkntfs was compiled with -DDEBUG. Means
1N/A * the input parameters were faulty.
1N/A */
1N/Astatic int make_room_for_attribute(MFT_RECORD *m, char *pos, const u32 size)
1N/A{
1N/A u32 biu;
1N/A
1N/A if (!size)
1N/A return 0;
1N/A#ifdef DEBUG
1N/A /*
1N/A * Rigorous consistency checks. Always return -EINVAL even if more
1N/A * appropriate codes exist for simplicity of parsing the return value.
1N/A */
1N/A if (size != ((size + 7) & ~7)) {
1N/A ntfs_log_error("make_room_for_attribute() received non 8-byte aligned "
1N/A "size.\n");
1N/A return -EINVAL;
1N/A }
1N/A if (!m || !pos)
1N/A return -EINVAL;
1N/A if (pos < (char*)m || pos + size < (char*)m ||
1N/A pos > (char*)m + le32_to_cpu(m->bytes_allocated) ||
1N/A pos + size > (char*)m + le32_to_cpu(m->bytes_allocated))
1N/A return -EINVAL;
1N/A /* The -8 is for the attribute terminator. */
1N/A if (pos - (char*)m > (int)le32_to_cpu(m->bytes_in_use) - 8)
1N/A return -EINVAL;
1N/A#endif
1N/A biu = le32_to_cpu(m->bytes_in_use);
1N/A /* Do we have enough space? */
1N/A if (biu + size > le32_to_cpu(m->bytes_allocated))
1N/A return -ENOSPC;
1N/A /* Move everything after pos to pos + size. */
1N/A memmove(pos + size, pos, biu - (pos - (char*)m));
1N/A /* Update mft record. */
1N/A m->bytes_in_use = cpu_to_le32(biu + size);
1N/A return 0;
1N/A}
1N/A
1N/A/**
1N/A * deallocate_scattered_clusters
1N/A */
1N/Astatic void deallocate_scattered_clusters(const runlist *rl)
1N/A{
1N/A LCN j;
1N/A int i;
1N/A
1N/A if (!rl)
1N/A return;
1N/A /* Iterate over all runs in the runlist @rl. */
1N/A for (i = 0; rl[i].length; i++) {
1N/A /* Skip sparse runs. */
1N/A if (rl[i].lcn == -1LL)
1N/A continue;
1N/A /* Deallocate the current run. */
1N/A for (j = rl[i].lcn; j < rl[i].lcn + rl[i].length; j++)
1N/A ntfs_bit_set(g_lcn_bitmap, j, 0);
1N/A }
1N/A}
1N/A
1N/A/**
1N/A * allocate_scattered_clusters
1N/A * @clusters: Amount of clusters to allocate.
1N/A *
1N/A * Allocate @clusters and create a runlist of the allocated clusters.
1N/A *
1N/A * Return the allocated runlist. Caller has to free the runlist when finished
1N/A * with it.
1N/A *
1N/A * On error return NULL and errno is set to the error code.
1N/A *
1N/A * TODO: We should be returning the size as well, but for mkntfs this is not
1N/A * necessary.
1N/A */
1N/Astatic runlist * allocate_scattered_clusters(s64 clusters)
1N/A{
1N/A runlist *rl = NULL, *rlt;
1N/A VCN vcn = 0LL;
1N/A LCN lcn, end, prev_lcn = 0LL;
1N/A int rlpos = 0;
1N/A int rlsize = 0;
1N/A s64 prev_run_len = 0LL;
1N/A char bit;
1N/A
1N/A end = g_vol->nr_clusters;
1N/A /* Loop until all clusters are allocated. */
1N/A while (clusters) {
1N/A /* Loop in current zone until we run out of free clusters. */
1N/A for (lcn = g_mft_zone_end; lcn < end; lcn++) {
1N/A bit = ntfs_bit_get_and_set(g_lcn_bitmap, lcn, 1);
1N/A if (bit)
1N/A continue;
1N/A /*
1N/A * Reallocate memory if necessary. Make sure we have
1N/A * enough for the terminator entry as well.
1N/A */
1N/A if ((rlpos + 2) * (int)sizeof(runlist) >= rlsize) {
1N/A rlsize += 4096; /* PAGE_SIZE */
1N/A rlt = realloc(rl, rlsize);
1N/A if (!rlt)
1N/A goto err_end;
1N/A rl = rlt;
1N/A }
1N/A /* Coalesce with previous run if adjacent LCNs. */
1N/A if (prev_lcn == lcn - prev_run_len) {
1N/A rl[rlpos - 1].length = ++prev_run_len;
1N/A vcn++;
1N/A } else {
1N/A rl[rlpos].vcn = vcn++;
1N/A rl[rlpos].lcn = lcn;
1N/A prev_lcn = lcn;
1N/A rl[rlpos].length = 1LL;
1N/A prev_run_len = 1LL;
1N/A rlpos++;
1N/A }
1N/A /* Done? */
1N/A if (!--clusters) {
1N/A /* Add terminator element and return. */
1N/A rl[rlpos].vcn = vcn;
1N/A rl[rlpos].lcn = 0LL;
1N/A rl[rlpos].length = 0LL;
1N/A return rl;
1N/A }
1N/A
1N/A }
1N/A /* Switch to next zone, decreasing mft zone by factor 2. */
1N/A end = g_mft_zone_end;
1N/A g_mft_zone_end >>= 1;
1N/A /* Have we run out of space on the volume? */
1N/A if (g_mft_zone_end <= 0)
1N/A goto err_end;
1N/A }
1N/A return rl;
1N/Aerr_end:
1N/A if (rl) {
1N/A /* Add terminator element. */
1N/A rl[rlpos].vcn = vcn;
1N/A rl[rlpos].lcn = -1LL;
1N/A rl[rlpos].length = 0LL;
1N/A /* Deallocate all allocated clusters. */
1N/A deallocate_scattered_clusters(rl);
1N/A /* Free the runlist. */
1N/A free(rl);
1N/A }
1N/A return NULL;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_attr_find - find (next) attribute in mft record
1N/A * @type: attribute type to find
1N/A * @name: attribute name to find (optional, i.e. NULL means don't care)
1N/A * @name_len: attribute name length (only needed if @name present)
1N/A * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
1N/A * @val: attribute value to find (optional, resident attributes only)
1N/A * @val_len: attribute value length
1N/A * @ctx: search context with mft record and attribute to search from
1N/A *
1N/A * You shouldn't need to call this function directly. Use lookup_attr() instead.
1N/A *
1N/A * ntfs_attr_find() takes a search context @ctx as parameter and searches the
1N/A * mft record specified by @ctx->mrec, beginning at @ctx->attr, for an
1N/A * attribute of @type, optionally @name and @val. If found, ntfs_attr_find()
1N/A * returns 0 and @ctx->attr will point to the found attribute.
1N/A *
1N/A * If not found, ntfs_attr_find() returns -1, with errno set to ENOENT and
1N/A * @ctx->attr will point to the attribute before which the attribute being
1N/A * searched for would need to be inserted if such an action were to be desired.
1N/A *
1N/A * On actual error, ntfs_attr_find() returns -1 with errno set to the error
1N/A * code but not to ENOENT. In this case @ctx->attr is undefined and in
1N/A * particular do not rely on it not changing.
1N/A *
1N/A * If @ctx->is_first is TRUE, the search begins with @ctx->attr itself. If it
1N/A * is FALSE, the search begins after @ctx->attr.
1N/A *
1N/A * If @type is AT_UNUSED, return the first found attribute, i.e. one can
1N/A * enumerate all attributes by setting @type to AT_UNUSED and then calling
1N/A * ntfs_attr_find() repeatedly until it returns -1 with errno set to ENOENT to
1N/A * indicate that there are no more entries. During the enumeration, each
1N/A * successful call of ntfs_attr_find() will return the next attribute in the
1N/A * mft record @ctx->mrec.
1N/A *
1N/A * If @type is AT_END, seek to the end and return -1 with errno set to ENOENT.
1N/A * AT_END is not a valid attribute, its length is zero for example, thus it is
1N/A * safer to return error instead of success in this case. This also allows us
1N/A * to interoperate cleanly with ntfs_external_attr_find().
1N/A *
1N/A * If @name is AT_UNNAMED search for an unnamed attribute. If @name is present
1N/A * but not AT_UNNAMED search for a named attribute matching @name. Otherwise,
1N/A * match both named and unnamed attributes.
1N/A *
1N/A * If @ic is IGNORE_CASE, the @name comparison is not case sensitive and
1N/A * @ctx->ntfs_ino must be set to the ntfs inode to which the mft record
1N/A * @ctx->mrec belongs. This is so we can get at the ntfs volume and hence at
1N/A * the upcase table. If @ic is CASE_SENSITIVE, the comparison is case
1N/A * sensitive. When @name is present, @name_len is the @name length in Unicode
1N/A * characters.
1N/A *
1N/A * If @name is not present (NULL), we assume that the unnamed attribute is
1N/A * being searched for.
1N/A *
1N/A * Finally, the resident attribute value @val is looked for, if present.
1N/A * If @val is not present (NULL), @val_len is ignored.
1N/A *
1N/A * ntfs_attr_find() only searches the specified mft record and it ignores the
1N/A * presence of an attribute list attribute (unless it is the one being searched
1N/A * for, obviously). If you need to take attribute lists into consideration, use
1N/A * ntfs_attr_lookup() instead (see below). This also means that you cannot use
1N/A * ntfs_attr_find() to search for extent records of non-resident attributes, as
1N/A * extents with lowest_vcn != 0 are usually described by the attribute list
1N/A * attribute only. - Note that it is possible that the first extent is only in
1N/A * the attribute list while the last extent is in the base mft record, so don't
1N/A * rely on being able to find the first extent in the base mft record.
1N/A *
1N/A * Warning: Never use @val when looking for attribute types which can be
1N/A * non-resident as this most likely will result in a crash!
1N/A */
1N/Astatic int mkntfs_attr_find(const ATTR_TYPES type, const ntfschar *name,
1N/A const u32 name_len, const IGNORE_CASE_BOOL ic,
1N/A const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
1N/A{
1N/A ATTR_RECORD *a;
1N/A ntfschar *upcase = g_vol->upcase;
1N/A u32 upcase_len = g_vol->upcase_len;
1N/A
1N/A /*
1N/A * Iterate over attributes in mft record starting at @ctx->attr, or the
1N/A * attribute following that, if @ctx->is_first is TRUE.
1N/A */
1N/A if (ctx->is_first) {
1N/A a = ctx->attr;
1N/A ctx->is_first = FALSE;
1N/A } else {
1N/A a = (ATTR_RECORD*)((char*)ctx->attr +
1N/A le32_to_cpu(ctx->attr->length));
1N/A }
1N/A for (;; a = (ATTR_RECORD*)((char*)a + le32_to_cpu(a->length))) {
1N/A if (p2n(a) < p2n(ctx->mrec) || (char*)a > (char*)ctx->mrec +
1N/A le32_to_cpu(ctx->mrec->bytes_allocated))
1N/A break;
1N/A ctx->attr = a;
1N/A if (((type != AT_UNUSED) && (le32_to_cpu(a->type) >
1N/A le32_to_cpu(type))) ||
1N/A (a->type == AT_END)) {
1N/A errno = ENOENT;
1N/A return -1;
1N/A }
1N/A if (!a->length)
1N/A break;
1N/A /* If this is an enumeration return this attribute. */
1N/A if (type == AT_UNUSED)
1N/A return 0;
1N/A if (a->type != type)
1N/A continue;
1N/A /*
1N/A * If @name is AT_UNNAMED we want an unnamed attribute.
1N/A * If @name is present, compare the two names.
1N/A * Otherwise, match any attribute.
1N/A */
1N/A if (name == AT_UNNAMED) {
1N/A /* The search failed if the found attribute is named. */
1N/A if (a->name_length) {
1N/A errno = ENOENT;
1N/A return -1;
1N/A }
1N/A } else if (name && !ntfs_names_are_equal(name, name_len,
1N/A (ntfschar*)((char*)a + le16_to_cpu(a->name_offset)),
1N/A a->name_length, ic, upcase, upcase_len)) {
1N/A int rc;
1N/A
1N/A rc = ntfs_names_collate(name, name_len,
1N/A (ntfschar*)((char*)a +
1N/A le16_to_cpu(a->name_offset)),
1N/A a->name_length, 1, IGNORE_CASE,
1N/A upcase, upcase_len);
1N/A /*
1N/A * If @name collates before a->name, there is no
1N/A * matching attribute.
1N/A */
1N/A if (rc == -1) {
1N/A errno = ENOENT;
1N/A return -1;
1N/A }
1N/A /* If the strings are not equal, continue search. */
1N/A if (rc)
1N/A continue;
1N/A rc = ntfs_names_collate(name, name_len,
1N/A (ntfschar*)((char*)a +
1N/A le16_to_cpu(a->name_offset)),
1N/A a->name_length, 1, CASE_SENSITIVE,
1N/A upcase, upcase_len);
1N/A if (rc == -1) {
1N/A errno = ENOENT;
1N/A return -1;
1N/A }
1N/A if (rc)
1N/A continue;
1N/A }
1N/A /*
1N/A * The names match or @name not present and attribute is
1N/A * unnamed. If no @val specified, we have found the attribute
1N/A * and are done.
1N/A */
1N/A if (!val) {
1N/A return 0;
1N/A /* @val is present; compare values. */
1N/A } else {
1N/A int rc;
1N/A
1N/A rc = memcmp(val, (char*)a +le16_to_cpu(a->u.res.value_offset),
1N/A min(val_len,
1N/A le32_to_cpu(a->u.res.value_length)));
1N/A /*
1N/A * If @val collates before the current attribute's
1N/A * value, there is no matching attribute.
1N/A */
1N/A if (!rc) {
1N/A u32 avl;
1N/A avl = le32_to_cpu(a->u.res.value_length);
1N/A if (val_len == avl)
1N/A return 0;
1N/A if (val_len < avl) {
1N/A errno = ENOENT;
1N/A return -1;
1N/A }
1N/A } else if (rc < 0) {
1N/A errno = ENOENT;
1N/A return -1;
1N/A }
1N/A }
1N/A }
1N/A ntfs_log_trace("File is corrupt. Run chkdsk.\n");
1N/A errno = EIO;
1N/A return -1;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_attr_lookup - find an attribute in an ntfs inode
1N/A * @type: attribute type to find
1N/A * @name: attribute name to find (optional, i.e. NULL means don't care)
1N/A * @name_len: attribute name length (only needed if @name present)
1N/A * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
1N/A * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
1N/A * @val: attribute value to find (optional, resident attributes only)
1N/A * @val_len: attribute value length
1N/A * @ctx: search context with mft record and attribute to search from
1N/A *
1N/A * Find an attribute in an ntfs inode. On first search @ctx->ntfs_ino must
1N/A * be the base mft record and @ctx must have been obtained from a call to
1N/A * ntfs_attr_get_search_ctx().
1N/A *
1N/A * This function transparently handles attribute lists and @ctx is used to
1N/A * continue searches where they were left off at.
1N/A *
1N/A * If @type is AT_UNUSED, return the first found attribute, i.e. one can
1N/A * enumerate all attributes by setting @type to AT_UNUSED and then calling
1N/A * ntfs_attr_lookup() repeatedly until it returns -1 with errno set to ENOENT
1N/A * to indicate that there are no more entries. During the enumeration, each
1N/A * successful call of ntfs_attr_lookup() will return the next attribute, with
1N/A * the current attribute being described by the search context @ctx.
1N/A *
1N/A * If @type is AT_END, seek to the end of the base mft record ignoring the
1N/A * attribute list completely and return -1 with errno set to ENOENT. AT_END is
1N/A * not a valid attribute, its length is zero for example, thus it is safer to
1N/A * return error instead of success in this case. It should never be needed to
1N/A * do this, but we implement the functionality because it allows for simpler
1N/A * code inside ntfs_external_attr_find().
1N/A *
1N/A * If @name is AT_UNNAMED search for an unnamed attribute. If @name is present
1N/A * but not AT_UNNAMED search for a named attribute matching @name. Otherwise,
1N/A * match both named and unnamed attributes.
1N/A *
1N/A * After finishing with the attribute/mft record you need to call
1N/A * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
1N/A * mapped extent inodes, etc).
1N/A *
1N/A * Return 0 if the search was successful and -1 if not, with errno set to the
1N/A * error code.
1N/A *
1N/A * On success, @ctx->attr is the found attribute, it is in mft record
1N/A * @ctx->mrec, and @ctx->al_entry is the attribute list entry for this
1N/A * attribute with @ctx->base_* being the base mft record to which @ctx->attr
1N/A * belongs. If no attribute list attribute is present @ctx->al_entry and
1N/A * @ctx->base_* are NULL.
1N/A *
1N/A * On error ENOENT, i.e. attribute not found, @ctx->attr is set to the
1N/A * attribute which collates just after the attribute being searched for in the
1N/A * base ntfs inode, i.e. if one wants to add the attribute to the mft record
1N/A * this is the correct place to insert it into, and if there is not enough
1N/A * space, the attribute should be placed in an extent mft record.
1N/A * @ctx->al_entry points to the position within @ctx->base_ntfs_ino->attr_list
1N/A * at which the new attribute's attribute list entry should be inserted. The
1N/A * other @ctx fields, base_ntfs_ino, base_mrec, and base_attr are set to NULL.
1N/A * The only exception to this is when @type is AT_END, in which case
1N/A * @ctx->al_entry is set to NULL also (see above).
1N/A *
1N/A * The following error codes are defined:
1N/A * ENOENT Attribute not found, not an error as such.
1N/A * EINVAL Invalid arguments.
1N/A * EIO I/O error or corrupt data structures found.
1N/A * ENOMEM Not enough memory to allocate necessary buffers.
1N/A */
1N/Astatic int mkntfs_attr_lookup(const ATTR_TYPES type, const ntfschar *name,
1N/A const u32 name_len, const IGNORE_CASE_BOOL ic,
1N/A const VCN lowest_vcn __attribute__((unused)), const u8 *val,
1N/A const u32 val_len, ntfs_attr_search_ctx *ctx)
1N/A{
1N/A ntfs_inode *base_ni;
1N/A
1N/A if (!ctx || !ctx->mrec || !ctx->attr) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A if (ctx->base_ntfs_ino)
1N/A base_ni = ctx->base_ntfs_ino;
1N/A else
1N/A base_ni = ctx->ntfs_ino;
1N/A if (!base_ni || !NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST)
1N/A return mkntfs_attr_find(type, name, name_len, ic, val, val_len,
1N/A ctx);
1N/A errno = EOPNOTSUPP;
1N/A return -1;
1N/A}
1N/A
1N/A/**
1N/A * insert_positioned_attr_in_mft_record
1N/A *
1N/A * Create a non-resident attribute with a predefined on disk location
1N/A * specified by the runlist @rl. The clusters specified by @rl are assumed to
1N/A * be allocated already.
1N/A *
1N/A * Return 0 on success and -errno on error.
1N/A */
1N/Astatic int insert_positioned_attr_in_mft_record(MFT_RECORD *m,
1N/A const ATTR_TYPES type, const char *name, u32 name_len,
1N/A const IGNORE_CASE_BOOL ic, const ATTR_FLAGS flags,
1N/A const runlist *rl, const u8 *val, const s64 val_len)
1N/A{
1N/A ntfs_attr_search_ctx *ctx;
1N/A ATTR_RECORD *a;
1N/A u16 hdr_size;
1N/A int asize, mpa_size, err, i;
1N/A s64 bw = 0, inited_size;
1N/A VCN highest_vcn;
1N/A ntfschar *uname = NULL;
1N/A int uname_len = 0;
1N/A /*
1N/A if (base record)
1N/A attr_lookup();
1N/A else
1N/A */
1N/A
1N/A uname = ntfs_str2ucs(name, &uname_len);
1N/A if (!uname)
1N/A return -errno;
1N/A
1N/A /* Check if the attribute is already there. */
1N/A ctx = ntfs_attr_get_search_ctx(NULL, m);
1N/A if (!ctx) {
1N/A ntfs_log_error("Failed to allocate attribute search context.\n");
1N/A err = -ENOMEM;
1N/A goto err_out;
1N/A }
1N/A if (ic == IGNORE_CASE) {
1N/A ntfs_log_error("FIXME: Hit unimplemented code path #1.\n");
1N/A err = -EOPNOTSUPP;
1N/A goto err_out;
1N/A }
1N/A if (!mkntfs_attr_lookup(type, uname, uname_len, ic, 0, NULL, 0, ctx)) {
1N/A err = -EEXIST;
1N/A goto err_out;
1N/A }
1N/A if (errno != ENOENT) {
1N/A ntfs_log_error("Corrupt inode.\n");
1N/A err = -errno;
1N/A goto err_out;
1N/A }
1N/A a = ctx->attr;
1N/A if (flags & ATTR_COMPRESSION_MASK) {
1N/A ntfs_log_error("Compressed attributes not supported yet.\n");
1N/A /* FIXME: Compress attribute into a temporary buffer, set */
1N/A /* val accordingly and save the compressed size. */
1N/A err = -EOPNOTSUPP;
1N/A goto err_out;
1N/A }
1N/A if (flags & (ATTR_IS_ENCRYPTED | ATTR_IS_SPARSE)) {
1N/A ntfs_log_error("Encrypted/sparse attributes not supported.\n");
1N/A err = -EOPNOTSUPP;
1N/A goto err_out;
1N/A }
1N/A if (flags & ATTR_COMPRESSION_MASK) {
1N/A hdr_size = 72;
1N/A /* FIXME: This compression stuff is all wrong. Never mind for */
1N/A /* now. (AIA) */
1N/A if (val_len)
1N/A mpa_size = 0; /* get_size_for_compressed_mapping_pairs(rl); */
1N/A else
1N/A mpa_size = 0;
1N/A } else {
1N/A hdr_size = 64;
1N/A if (val_len) {
1N/A mpa_size = ntfs_get_size_for_mapping_pairs(g_vol, rl, 0);
1N/A if (mpa_size < 0) {
1N/A err = -errno;
1N/A ntfs_log_error("Failed to get size for mapping "
1N/A "pairs.\n");
1N/A goto err_out;
1N/A }
1N/A } else {
1N/A mpa_size = 0;
1N/A }
1N/A }
1N/A /* Mapping pairs array and next attribute must be 8-byte aligned. */
1N/A asize = (((int)hdr_size + ((name_len + 7) & ~7) + mpa_size) + 7) & ~7;
1N/A /* Get the highest vcn. */
1N/A for (i = 0, highest_vcn = 0LL; rl[i].length; i++)
1N/A highest_vcn += rl[i].length;
1N/A /* Does the value fit inside the allocated size? */
1N/A if (highest_vcn * g_vol->cluster_size < val_len) {
1N/A ntfs_log_error("BUG: Allocated size is smaller than data size!\n");
1N/A err = -EINVAL;
1N/A goto err_out;
1N/A }
1N/A err = make_room_for_attribute(m, (char*)a, asize);
1N/A if (err == -ENOSPC) {
1N/A /*
1N/A * FIXME: Make space! (AIA)
1N/A * can we make it non-resident? if yes, do that.
1N/A * does it fit now? yes -> do it.
1N/A * m's $DATA or $BITMAP+$INDEX_ALLOCATION resident?
1N/A * yes -> make non-resident
1N/A * does it fit now? yes -> do it.
1N/A * make all attributes non-resident
1N/A * does it fit now? yes -> do it.
1N/A * m is a base record? yes -> allocate extension record
1N/A * does the new attribute fit in there? yes -> do it.
1N/A * split up runlist into extents and place each in an extension
1N/A * record.
1N/A * FIXME: the check for needing extension records should be
1N/A * earlier on as it is very quick: asize > m->bytes_allocated?
1N/A */
1N/A err = -EOPNOTSUPP;
1N/A goto err_out;
1N/A#ifdef DEBUG
1N/A } else if (err == -EINVAL) {
1N/A ntfs_log_error("BUG(): in insert_positioned_attribute_in_mft_"
1N/A "record(): make_room_for_attribute() returned "
1N/A "error: EINVAL!\n");
1N/A goto err_out;
1N/A#endif
1N/A }
1N/A a->type = type;
1N/A a->length = cpu_to_le32(asize);
1N/A a->non_resident = 1;
1N/A a->name_length = name_len;
1N/A a->name_offset = cpu_to_le16(hdr_size);
1N/A a->flags = flags;
1N/A a->instance = m->next_attr_instance;
1N/A m->next_attr_instance = cpu_to_le16((le16_to_cpu(m->next_attr_instance)
1N/A + 1) & 0xffff);
1N/A a->u.nonres.lowest_vcn = 0;
1N/A a->u.nonres.highest_vcn = cpu_to_sle64(highest_vcn - 1LL);
1N/A a->u.nonres.mapping_pairs_offset = cpu_to_le16(hdr_size + ((name_len + 7) & ~7));
1N/A memset(a->u.nonres.reserved1, 0, sizeof(a->u.nonres.reserved1));
1N/A /* FIXME: Allocated size depends on compression. */
1N/A a->u.nonres.allocated_size = cpu_to_sle64(highest_vcn * g_vol->cluster_size);
1N/A a->u.nonres.data_size = cpu_to_sle64(val_len);
1N/A if (name_len)
1N/A memcpy((char*)a + hdr_size, uname, name_len << 1);
1N/A if (flags & ATTR_COMPRESSION_MASK) {
1N/A if (flags & ATTR_COMPRESSION_MASK & ~ATTR_IS_COMPRESSED) {
1N/A ntfs_log_error("Unknown compression format. Reverting "
1N/A "to standard compression.\n");
1N/A a->flags &= ~ATTR_COMPRESSION_MASK;
1N/A a->flags |= ATTR_IS_COMPRESSED;
1N/A }
1N/A a->u.nonres.compression_unit = 4;
1N/A inited_size = val_len;
1N/A /* FIXME: Set the compressed size. */
1N/A a->u.nonres.compressed_size = 0;
1N/A /* FIXME: Write out the compressed data. */
1N/A /* FIXME: err = build_mapping_pairs_compressed(); */
1N/A err = -EOPNOTSUPP;
1N/A } else {
1N/A a->u.nonres.compression_unit = 0;
1N/A bw = ntfs_rlwrite(g_vol->u.dev, rl, val, val_len, &inited_size);
1N/A if (bw != val_len) {
1N/A ntfs_log_error("Error writing non-resident attribute "
1N/A "value.\n");
1N/A return -errno;
1N/A }
1N/A err = ntfs_mapping_pairs_build(g_vol, (u8*)a + hdr_size +
1N/A ((name_len + 7) & ~7), mpa_size, rl, 0, NULL);
1N/A }
1N/A a->u.nonres.initialized_size = cpu_to_sle64(inited_size);
1N/A if (err < 0 || bw != val_len) {
1N/A /* FIXME: Handle error. */
1N/A /* deallocate clusters */
1N/A /* remove attribute */
1N/A if (err >= 0)
1N/A err = -EIO;
1N/A ntfs_log_error("insert_positioned_attr_in_mft_record failed "
1N/A "with error %i.\n", err < 0 ? err : (int)bw);
1N/A }
1N/Aerr_out:
1N/A if (ctx)
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A ntfs_ucsfree(uname);
1N/A return err;
1N/A}
1N/A
1N/A/**
1N/A * insert_non_resident_attr_in_mft_record
1N/A *
1N/A * Return 0 on success and -errno on error.
1N/A */
1N/Astatic int insert_non_resident_attr_in_mft_record(MFT_RECORD *m,
1N/A const ATTR_TYPES type, const char *name, u32 name_len,
1N/A const IGNORE_CASE_BOOL ic, const ATTR_FLAGS flags,
1N/A const u8 *val, const s64 val_len)
1N/A{
1N/A ntfs_attr_search_ctx *ctx;
1N/A ATTR_RECORD *a;
1N/A u16 hdr_size;
1N/A int asize, mpa_size, err, i;
1N/A runlist *rl = NULL;
1N/A s64 bw = 0;
1N/A ntfschar *uname = NULL;
1N/A int uname_len = 0;
1N/A /*
1N/A if (base record)
1N/A attr_lookup();
1N/A else
1N/A */
1N/A
1N/A uname = ntfs_str2ucs(name, &uname_len);
1N/A if (!uname)
1N/A return -errno;
1N/A
1N/A /* Check if the attribute is already there. */
1N/A ctx = ntfs_attr_get_search_ctx(NULL, m);
1N/A if (!ctx) {
1N/A ntfs_log_error("Failed to allocate attribute search context.\n");
1N/A err = -ENOMEM;
1N/A goto err_out;
1N/A }
1N/A if (ic == IGNORE_CASE) {
1N/A ntfs_log_error("FIXME: Hit unimplemented code path #2.\n");
1N/A err = -EOPNOTSUPP;
1N/A goto err_out;
1N/A }
1N/A if (!mkntfs_attr_lookup(type, uname, uname_len, ic, 0, NULL, 0, ctx)) {
1N/A err = -EEXIST;
1N/A goto err_out;
1N/A }
1N/A if (errno != ENOENT) {
1N/A ntfs_log_error("Corrupt inode.\n");
1N/A err = -errno;
1N/A goto err_out;
1N/A }
1N/A a = ctx->attr;
1N/A if (flags & ATTR_COMPRESSION_MASK) {
1N/A ntfs_log_error("Compressed attributes not supported yet.\n");
1N/A /* FIXME: Compress attribute into a temporary buffer, set */
1N/A /* val accordingly and save the compressed size. */
1N/A err = -EOPNOTSUPP;
1N/A goto err_out;
1N/A }
1N/A if (flags & (ATTR_IS_ENCRYPTED | ATTR_IS_SPARSE)) {
1N/A ntfs_log_error("Encrypted/sparse attributes not supported.\n");
1N/A err = -EOPNOTSUPP;
1N/A goto err_out;
1N/A }
1N/A if (val_len) {
1N/A rl = allocate_scattered_clusters((val_len +
1N/A g_vol->cluster_size - 1) / g_vol->cluster_size);
1N/A if (!rl) {
1N/A err = -errno;
1N/A ntfs_log_perror("Failed to allocate scattered clusters");
1N/A goto err_out;
1N/A }
1N/A } else {
1N/A rl = NULL;
1N/A }
1N/A if (flags & ATTR_COMPRESSION_MASK) {
1N/A hdr_size = 72;
1N/A /* FIXME: This compression stuff is all wrong. Never mind for */
1N/A /* now. (AIA) */
1N/A if (val_len)
1N/A mpa_size = 0; /* get_size_for_compressed_mapping_pairs(rl); */
1N/A else
1N/A mpa_size = 0;
1N/A } else {
1N/A hdr_size = 64;
1N/A if (val_len) {
1N/A mpa_size = ntfs_get_size_for_mapping_pairs(g_vol, rl, 0);
1N/A if (mpa_size < 0) {
1N/A err = -errno;
1N/A ntfs_log_error("Failed to get size for mapping "
1N/A "pairs.\n");
1N/A goto err_out;
1N/A }
1N/A } else {
1N/A mpa_size = 0;
1N/A }
1N/A }
1N/A /* Mapping pairs array and next attribute must be 8-byte aligned. */
1N/A asize = (((int)hdr_size + ((name_len + 7) & ~7) + mpa_size) + 7) & ~7;
1N/A err = make_room_for_attribute(m, (char*)a, asize);
1N/A if (err == -ENOSPC) {
1N/A /*
1N/A * FIXME: Make space! (AIA)
1N/A * can we make it non-resident? if yes, do that.
1N/A * does it fit now? yes -> do it.
1N/A * m's $DATA or $BITMAP+$INDEX_ALLOCATION resident?
1N/A * yes -> make non-resident
1N/A * does it fit now? yes -> do it.
1N/A * make all attributes non-resident
1N/A * does it fit now? yes -> do it.
1N/A * m is a base record? yes -> allocate extension record
1N/A * does the new attribute fit in there? yes -> do it.
1N/A * split up runlist into extents and place each in an extension
1N/A * record.
1N/A * FIXME: the check for needing extension records should be
1N/A * earlier on as it is very quick: asize > m->bytes_allocated?
1N/A */
1N/A err = -EOPNOTSUPP;
1N/A goto err_out;
1N/A#ifdef DEBUG
1N/A } else if (err == -EINVAL) {
1N/A ntfs_log_error("BUG(): in insert_non_resident_attribute_in_"
1N/A "mft_record(): make_room_for_attribute() "
1N/A "returned error: EINVAL!\n");
1N/A goto err_out;
1N/A#endif
1N/A }
1N/A a->type = type;
1N/A a->length = cpu_to_le32(asize);
1N/A a->non_resident = 1;
1N/A a->name_length = name_len;
1N/A a->name_offset = cpu_to_le16(hdr_size);
1N/A a->flags = flags;
1N/A a->instance = m->next_attr_instance;
1N/A m->next_attr_instance = cpu_to_le16((le16_to_cpu(m->next_attr_instance)
1N/A + 1) & 0xffff);
1N/A a->u.nonres.lowest_vcn = 0;
1N/A for (i = 0; rl[i].length; i++)
1N/A ;
1N/A a->u.nonres.highest_vcn = cpu_to_sle64(rl[i].vcn - 1);
1N/A a->u.nonres.mapping_pairs_offset = cpu_to_le16(hdr_size + ((name_len + 7) & ~7));
1N/A memset(a->u.nonres.reserved1, 0, sizeof(a->u.nonres.reserved1));
1N/A /* FIXME: Allocated size depends on compression. */
1N/A a->u.nonres.allocated_size = cpu_to_sle64((val_len + (g_vol->cluster_size - 1)) &
1N/A ~(g_vol->cluster_size - 1));
1N/A a->u.nonres.data_size = cpu_to_sle64(val_len);
1N/A a->u.nonres.initialized_size = cpu_to_sle64(val_len);
1N/A if (name_len)
1N/A memcpy((char*)a + hdr_size, uname, name_len << 1);
1N/A if (flags & ATTR_COMPRESSION_MASK) {
1N/A if (flags & ATTR_COMPRESSION_MASK & ~ATTR_IS_COMPRESSED) {
1N/A ntfs_log_error("Unknown compression format. Reverting "
1N/A "to standard compression.\n");
1N/A a->flags &= ~ATTR_COMPRESSION_MASK;
1N/A a->flags |= ATTR_IS_COMPRESSED;
1N/A }
1N/A a->u.nonres.compression_unit = 4;
1N/A /* FIXME: Set the compressed size. */
1N/A a->u.nonres.compressed_size = 0;
1N/A /* FIXME: Write out the compressed data. */
1N/A /* FIXME: err = build_mapping_pairs_compressed(); */
1N/A err = -EOPNOTSUPP;
1N/A } else {
1N/A a->u.nonres.compression_unit = 0;
1N/A bw = ntfs_rlwrite(g_vol->u.dev, rl, val, val_len, NULL);
1N/A if (bw != val_len) {
1N/A ntfs_log_error("Error writing non-resident attribute "
1N/A "value.\n");
1N/A return -errno;
1N/A }
1N/A err = ntfs_mapping_pairs_build(g_vol, (u8*)a + hdr_size +
1N/A ((name_len + 7) & ~7), mpa_size, rl, 0, NULL);
1N/A }
1N/A if (err < 0 || bw != val_len) {
1N/A /* FIXME: Handle error. */
1N/A /* deallocate clusters */
1N/A /* remove attribute */
1N/A if (err >= 0)
1N/A err = -EIO;
1N/A ntfs_log_error("insert_non_resident_attr_in_mft_record failed with "
1N/A "error %lld.\n", (long long) (err < 0 ? err : bw));
1N/A }
1N/Aerr_out:
1N/A if (ctx)
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A ntfs_ucsfree(uname);
1N/A free(rl);
1N/A return err;
1N/A}
1N/A
1N/A/**
1N/A * insert_resident_attr_in_mft_record
1N/A *
1N/A * Return 0 on success and -errno on error.
1N/A */
1N/Astatic int insert_resident_attr_in_mft_record(MFT_RECORD *m,
1N/A const ATTR_TYPES type, const char *name, u32 name_len,
1N/A const IGNORE_CASE_BOOL ic, const ATTR_FLAGS flags,
1N/A const RESIDENT_ATTR_FLAGS res_flags,
1N/A const u8 *val, const u32 val_len)
1N/A{
1N/A ntfs_attr_search_ctx *ctx;
1N/A ATTR_RECORD *a;
1N/A int asize, err;
1N/A ntfschar *uname = NULL;
1N/A int uname_len = 0;
1N/A /*
1N/A if (base record)
1N/A mkntfs_attr_lookup();
1N/A else
1N/A */
1N/A
1N/A uname = ntfs_str2ucs(name, &uname_len);
1N/A if (!uname)
1N/A return -errno;
1N/A
1N/A /* Check if the attribute is already there. */
1N/A ctx = ntfs_attr_get_search_ctx(NULL, m);
1N/A if (!ctx) {
1N/A ntfs_log_error("Failed to allocate attribute search context.\n");
1N/A err = -ENOMEM;
1N/A goto err_out;
1N/A }
1N/A if (ic == IGNORE_CASE) {
1N/A ntfs_log_error("FIXME: Hit unimplemented code path #3.\n");
1N/A err = -EOPNOTSUPP;
1N/A goto err_out;
1N/A }
1N/A if (!mkntfs_attr_lookup(type, uname, uname_len, ic, 0, val, val_len,
1N/A ctx)) {
1N/A err = -EEXIST;
1N/A goto err_out;
1N/A }
1N/A if (errno != ENOENT) {
1N/A ntfs_log_error("Corrupt inode.\n");
1N/A err = -errno;
1N/A goto err_out;
1N/A }
1N/A a = ctx->attr;
1N/A /* sizeof(resident attribute record header) == 24 */
1N/A asize = ((24 + ((name_len + 7) & ~7) + val_len) + 7) & ~7;
1N/A err = make_room_for_attribute(m, (char*)a, asize);
1N/A if (err == -ENOSPC) {
1N/A /*
1N/A * FIXME: Make space! (AIA)
1N/A * can we make it non-resident? if yes, do that.
1N/A * does it fit now? yes -> do it.
1N/A * m's $DATA or $BITMAP+$INDEX_ALLOCATION resident?
1N/A * yes -> make non-resident
1N/A * does it fit now? yes -> do it.
1N/A * make all attributes non-resident
1N/A * does it fit now? yes -> do it.
1N/A * m is a base record? yes -> allocate extension record
1N/A * does the new attribute fit in there? yes -> do it.
1N/A * split up runlist into extents and place each in an extension
1N/A * record.
1N/A * FIXME: the check for needing extension records should be
1N/A * earlier on as it is very quick: asize > m->bytes_allocated?
1N/A */
1N/A err = -EOPNOTSUPP;
1N/A goto err_out;
1N/A }
1N/A#ifdef DEBUG
1N/A if (err == -EINVAL) {
1N/A ntfs_log_error("BUG(): in insert_resident_attribute_in_mft_"
1N/A "record(): make_room_for_attribute() returned "
1N/A "error: EINVAL!\n");
1N/A goto err_out;
1N/A }
1N/A#endif
1N/A a->type = type;
1N/A a->length = cpu_to_le32(asize);
1N/A a->non_resident = 0;
1N/A a->name_length = name_len;
1N/A if (type == AT_OBJECT_ID)
1N/A a->name_offset = const_cpu_to_le16(0);
1N/A else
1N/A a->name_offset = const_cpu_to_le16(24);
1N/A a->flags = flags;
1N/A a->instance = m->next_attr_instance;
1N/A m->next_attr_instance = cpu_to_le16((le16_to_cpu(m->next_attr_instance)
1N/A + 1) & 0xffff);
1N/A a->u.res.value_length = cpu_to_le32(val_len);
1N/A a->u.res.value_offset = cpu_to_le16(24 + ((name_len + 7) & ~7));
1N/A a->u.res.resident_flags = res_flags;
1N/A a->u.res.reservedR = 0;
1N/A if (name_len)
1N/A memcpy((char*)a + 24, uname, name_len << 1);
1N/A if (val_len)
1N/A memcpy((char*)a + le16_to_cpu(a->u.res.value_offset), val, val_len);
1N/Aerr_out:
1N/A if (ctx)
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A ntfs_ucsfree(uname);
1N/A return err;
1N/A}
1N/A
1N/A
1N/A/**
1N/A * add_attr_std_info
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int add_attr_std_info(MFT_RECORD *m, const FILE_ATTR_FLAGS flags,
1N/A le32 security_id)
1N/A{
1N/A STANDARD_INFORMATION si;
1N/A int err, sd_size;
1N/A
1N/A sd_size = 48;
1N/A
1N/A si.creation_time = utc2ntfs(mkntfs_time());
1N/A si.last_data_change_time = si.creation_time;
1N/A si.last_mft_change_time = si.creation_time;
1N/A si.last_access_time = si.creation_time;
1N/A si.file_attributes = flags; /* already LE */
1N/A si.u.v30.maximum_versions = cpu_to_le32(0);
1N/A si.u.v30.version_number = cpu_to_le32(0);
1N/A si.u.v30.class_id = cpu_to_le32(0);
1N/A si.u.v30.security_id = security_id;
1N/A if (si.u.v30.security_id != 0)
1N/A sd_size = 72;
1N/A /* FIXME: $Quota support... */
1N/A si.u.v30.owner_id = cpu_to_le32(0);
1N/A si.u.v30.quota_charged = cpu_to_le64(0ULL);
1N/A /* FIXME: $UsnJrnl support... Not needed on fresh w2k3-volume */
1N/A si.u.v30.usn = cpu_to_le64(0ULL);
1N/A /* NTFS 1.2: size of si = 48, NTFS 3.[01]: size of si = 72 */
1N/A err = insert_resident_attr_in_mft_record(m, AT_STANDARD_INFORMATION,
1N/A NULL, 0, 0, 0, 0, (u8*)&si, sd_size);
1N/A if (err < 0)
1N/A ntfs_log_perror("add_attr_std_info failed");
1N/A return err;
1N/A}
1N/A
1N/A/**
1N/A * add_attr_file_name
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int add_attr_file_name(MFT_RECORD *m, const leMFT_REF parent_dir,
1N/A const s64 allocated_size, const s64 data_size,
1N/A const FILE_ATTR_FLAGS flags, const u16 packed_ea_size,
1N/A const u32 reparse_point_tag, const char *file_name,
1N/A const FILE_NAME_TYPE_FLAGS file_name_type)
1N/A{
1N/A ntfs_attr_search_ctx *ctx;
1N/A STANDARD_INFORMATION *si;
1N/A FILE_NAME_ATTR *fn;
1N/A int i, fn_size;
1N/A ntfschar *uname;
1N/A
1N/A /* Check if the attribute is already there. */
1N/A ctx = ntfs_attr_get_search_ctx(NULL, m);
1N/A if (!ctx) {
1N/A ntfs_log_error("Failed to get attribute search context.\n");
1N/A return -ENOMEM;
1N/A }
1N/A if (mkntfs_attr_lookup(AT_STANDARD_INFORMATION, AT_UNNAMED, 0, 0, 0,
1N/A NULL, 0, ctx)) {
1N/A int eo = errno;
1N/A ntfs_log_error("BUG: Standard information attribute not "
1N/A "present in file record.\n");
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A return -eo;
1N/A }
1N/A si = (STANDARD_INFORMATION*)((char*)ctx->attr +
1N/A le16_to_cpu(ctx->attr->u.res.value_offset));
1N/A i = (strlen(file_name) + 1) * sizeof(ntfschar);
1N/A fn_size = sizeof(FILE_NAME_ATTR) + i;
1N/A fn = ntfs_malloc(fn_size);
1N/A if (!fn) {
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A return -errno;
1N/A }
1N/A fn->parent_directory = parent_dir;
1N/A
1N/A fn->creation_time = si->creation_time;
1N/A fn->last_data_change_time = si->last_data_change_time;
1N/A fn->last_mft_change_time = si->last_mft_change_time;
1N/A fn->last_access_time = si->last_access_time;
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A
1N/A fn->allocated_size = cpu_to_sle64(allocated_size);
1N/A fn->data_size = cpu_to_sle64(data_size);
1N/A fn->file_attributes = flags;
1N/A /* These are in a union so can't have both. */
1N/A if (packed_ea_size && reparse_point_tag) {
1N/A free(fn);
1N/A return -EINVAL;
1N/A }
1N/A if (packed_ea_size) {
1N/A fn->u.s.packed_ea_size = cpu_to_le16(packed_ea_size);
1N/A fn->u.s.reserved = cpu_to_le16(0);
1N/A } else {
1N/A fn->u.reparse_point_tag = cpu_to_le32(reparse_point_tag);
1N/A }
1N/A fn->file_name_type = file_name_type;
1N/A uname = fn->file_name;
1N/A i = ntfs_mbstoucs(file_name, &uname, i);
1N/A if (i < 1) {
1N/A free(fn);
1N/A return -EINVAL;
1N/A }
1N/A if (i > 0xff) {
1N/A free(fn);
1N/A return -ENAMETOOLONG;
1N/A }
1N/A /* No terminating null in file names. */
1N/A fn->file_name_length = i;
1N/A fn_size = sizeof(FILE_NAME_ATTR) + i * sizeof(ntfschar);
1N/A i = insert_resident_attr_in_mft_record(m, AT_FILE_NAME, NULL, 0, 0,
1N/A 0, RESIDENT_ATTR_IS_INDEXED, (u8*)fn, fn_size);
1N/A free(fn);
1N/A if (i < 0)
1N/A ntfs_log_error("add_attr_file_name failed: %s\n", strerror(-i));
1N/A return i;
1N/A}
1N/A
1N/A#ifdef ENABLE_UUID
1N/A
1N/A/**
1N/A * add_attr_object_id -
1N/A *
1N/A * Note we insert only a basic object id which only has the GUID and none of
1N/A * the extended fields. This is because we currently only use this function
1N/A * when creating the object id for the volume.
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int add_attr_object_id(MFT_RECORD *m, const GUID *object_id)
1N/A{
1N/A OBJECT_ID_ATTR oi;
1N/A int err;
1N/A
1N/A oi = (OBJECT_ID_ATTR) {
1N/A .object_id = *object_id,
1N/A };
1N/A err = insert_resident_attr_in_mft_record(m, AT_OBJECT_ID, NULL,
1N/A 0, 0, 0, 0, (u8*)&oi, sizeof(oi.object_id));
1N/A if (err < 0)
1N/A ntfs_log_error("add_attr_vol_info failed: %s\n", strerror(-err));
1N/A return err;
1N/A}
1N/A
1N/A#endif
1N/A
1N/A/**
1N/A * add_attr_sd
1N/A *
1N/A * Create the security descriptor attribute adding the security descriptor @sd
1N/A * of length @sd_len to the mft record @m.
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int add_attr_sd(MFT_RECORD *m, const u8 *sd, const s64 sd_len)
1N/A{
1N/A int err;
1N/A
1N/A /* Does it fit? NO: create non-resident. YES: create resident. */
1N/A if (le32_to_cpu(m->bytes_in_use) + 24 + sd_len >
1N/A le32_to_cpu(m->bytes_allocated))
1N/A err = insert_non_resident_attr_in_mft_record(m,
1N/A AT_SECURITY_DESCRIPTOR, NULL, 0, 0, 0, sd,
1N/A sd_len);
1N/A else
1N/A err = insert_resident_attr_in_mft_record(m,
1N/A AT_SECURITY_DESCRIPTOR, NULL, 0, 0, 0, 0, sd,
1N/A sd_len);
1N/A if (err < 0)
1N/A ntfs_log_error("add_attr_sd failed: %s\n", strerror(-err));
1N/A return err;
1N/A}
1N/A
1N/A/**
1N/A * add_attr_data
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int add_attr_data(MFT_RECORD *m, const char *name, const u32 name_len,
1N/A const IGNORE_CASE_BOOL ic, const ATTR_FLAGS flags,
1N/A const u8 *val, const s64 val_len)
1N/A{
1N/A int err;
1N/A
1N/A /*
1N/A * Does it fit? NO: create non-resident. YES: create resident.
1N/A *
1N/A * FIXME: Introduced arbitrary limit of mft record allocated size - 512.
1N/A * This is to get around the problem that if $Bitmap/$DATA becomes too
1N/A * big, but is just small enough to be resident, we would make it
1N/A * resident, and later run out of space when creating the other
1N/A * attributes and this would cause us to abort as making resident
1N/A * attributes non-resident is not supported yet.
1N/A * The proper fix is to support making resident attribute non-resident.
1N/A */
1N/A if (le32_to_cpu(m->bytes_in_use) + 24 + val_len >
1N/A min(le32_to_cpu(m->bytes_allocated),
1N/A le32_to_cpu(m->bytes_allocated) - 512))
1N/A err = insert_non_resident_attr_in_mft_record(m, AT_DATA, name,
1N/A name_len, ic, flags, val, val_len);
1N/A else
1N/A err = insert_resident_attr_in_mft_record(m, AT_DATA, name,
1N/A name_len, ic, flags, 0, val, val_len);
1N/A
1N/A if (err < 0)
1N/A ntfs_log_error("add_attr_data failed: %s\n", strerror(-err));
1N/A return err;
1N/A}
1N/A
1N/A/**
1N/A * add_attr_data_positioned
1N/A *
1N/A * Create a non-resident data attribute with a predefined on disk location
1N/A * specified by the runlist @rl. The clusters specified by @rl are assumed to
1N/A * be allocated already.
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int add_attr_data_positioned(MFT_RECORD *m, const char *name,
1N/A const u32 name_len, const IGNORE_CASE_BOOL ic,
1N/A const ATTR_FLAGS flags, const runlist *rl,
1N/A const u8 *val, const s64 val_len)
1N/A{
1N/A int err;
1N/A
1N/A err = insert_positioned_attr_in_mft_record(m, AT_DATA, name, name_len,
1N/A ic, flags, rl, val, val_len);
1N/A if (err < 0)
1N/A ntfs_log_error("add_attr_data_positioned failed: %s\n",
1N/A strerror(-err));
1N/A return err;
1N/A}
1N/A
1N/A/**
1N/A * add_attr_vol_name
1N/A *
1N/A * Create volume name attribute specifying the volume name @vol_name as a null
1N/A * terminated char string of length @vol_name_len (number of characters not
1N/A * including the terminating null), which is converted internally to a little
1N/A * endian ntfschar string. The name is at least 1 character long and at most
1N/A * 0xff characters long (not counting the terminating null).
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int add_attr_vol_name(MFT_RECORD *m, const char *vol_name,
1N/A const int vol_name_len __attribute__((unused)))
1N/A{
1N/A ntfschar *uname = NULL;
1N/A int uname_len = 0;
1N/A int i;
1N/A
1N/A if (vol_name) {
1N/A uname_len = ntfs_mbstoucs(vol_name, &uname, 0);
1N/A if (uname_len < 0)
1N/A return -errno;
1N/A if (uname_len > 0xff) {
1N/A free(uname);
1N/A return -ENAMETOOLONG;
1N/A }
1N/A }
1N/A i = insert_resident_attr_in_mft_record(m, AT_VOLUME_NAME, NULL, 0, 0,
1N/A 0, 0, (u8*)uname, uname_len*sizeof(ntfschar));
1N/A free(uname);
1N/A if (i < 0)
1N/A ntfs_log_error("add_attr_vol_name failed: %s\n", strerror(-i));
1N/A return i;
1N/A}
1N/A
1N/A/**
1N/A * add_attr_vol_info
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int add_attr_vol_info(MFT_RECORD *m, const VOLUME_FLAGS flags,
1N/A const u8 major_ver, const u8 minor_ver)
1N/A{
1N/A VOLUME_INFORMATION vi;
1N/A int err;
1N/A
1N/A memset(&vi, 0, sizeof(vi));
1N/A vi.major_ver = major_ver;
1N/A vi.minor_ver = minor_ver;
1N/A vi.flags = flags & VOLUME_FLAGS_MASK;
1N/A err = insert_resident_attr_in_mft_record(m, AT_VOLUME_INFORMATION, NULL,
1N/A 0, 0, 0, 0, (u8*)&vi, sizeof(vi));
1N/A if (err < 0)
1N/A ntfs_log_error("add_attr_vol_info failed: %s\n", strerror(-err));
1N/A return err;
1N/A}
1N/A
1N/A/**
1N/A * add_attr_index_root
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int add_attr_index_root(MFT_RECORD *m, const char *name,
1N/A const u32 name_len, const IGNORE_CASE_BOOL ic,
1N/A const ATTR_TYPES indexed_attr_type,
1N/A const COLLATION_RULES collation_rule,
1N/A const u32 index_block_size)
1N/A{
1N/A INDEX_ROOT *r;
1N/A INDEX_ENTRY_HEADER *e;
1N/A int err, val_len;
1N/A
1N/A val_len = sizeof(INDEX_ROOT) + sizeof(INDEX_ENTRY_HEADER);
1N/A r = ntfs_malloc(val_len);
1N/A if (!r)
1N/A return -errno;
1N/A r->type = (indexed_attr_type == AT_FILE_NAME) ? AT_FILE_NAME : 0;
1N/A if (indexed_attr_type == AT_FILE_NAME &&
1N/A collation_rule != COLLATION_FILE_NAME) {
1N/A free(r);
1N/A ntfs_log_error("add_attr_index_root: indexed attribute is $FILE_NAME "
1N/A "but collation rule is not COLLATION_FILE_NAME.\n");
1N/A return -EINVAL;
1N/A }
1N/A r->collation_rule = collation_rule;
1N/A r->index_block_size = cpu_to_le32(index_block_size);
1N/A if (index_block_size >= g_vol->cluster_size) {
1N/A if (index_block_size % g_vol->cluster_size) {
1N/A ntfs_log_error("add_attr_index_root: index block size is not "
1N/A "a multiple of the cluster size.\n");
1N/A free(r);
1N/A return -EINVAL;
1N/A }
1N/A r->clusters_per_index_block = index_block_size /
1N/A g_vol->cluster_size;
1N/A } else { /* if (g_vol->cluster_size > index_block_size) */
1N/A if (index_block_size & (index_block_size - 1)) {
1N/A ntfs_log_error("add_attr_index_root: index block size is not "
1N/A "a power of 2.\n");
1N/A free(r);
1N/A return -EINVAL;
1N/A }
1N/A if (index_block_size < (u32)opts.sector_size) {
1N/A ntfs_log_error("add_attr_index_root: index block size "
1N/A "is smaller than the sector size.\n");
1N/A free(r);
1N/A return -EINVAL;
1N/A }
1N/A r->clusters_per_index_block = index_block_size /
1N/A opts.sector_size;
1N/A }
1N/A memset(&r->reserved, 0, sizeof(r->reserved));
1N/A r->index.entries_offset = const_cpu_to_le32(sizeof(INDEX_HEADER));
1N/A r->index.index_length = const_cpu_to_le32(sizeof(INDEX_HEADER) +
1N/A sizeof(INDEX_ENTRY_HEADER));
1N/A r->index.allocated_size = r->index.index_length;
1N/A r->index.flags = SMALL_INDEX;
1N/A memset(&r->index.reserved, 0, sizeof(r->index.reserved));
1N/A e = (INDEX_ENTRY_HEADER*)((u8*)&r->index +
1N/A le32_to_cpu(r->index.entries_offset));
1N/A /*
1N/A * No matter whether this is a file index or a view as this is a
1N/A * termination entry, hence no key value / data is associated with it
1N/A * at all. Thus, we just need the union to be all zero.
1N/A */
1N/A e->u.indexed_file = const_cpu_to_le64(0LL);
1N/A e->length = const_cpu_to_le16(sizeof(INDEX_ENTRY_HEADER));
1N/A e->key_length = const_cpu_to_le16(0);
1N/A e->flags = INDEX_ENTRY_END;
1N/A e->reserved = const_cpu_to_le16(0);
1N/A err = insert_resident_attr_in_mft_record(m, AT_INDEX_ROOT, name,
1N/A name_len, ic, 0, 0, (u8*)r, val_len);
1N/A free(r);
1N/A if (err < 0)
1N/A ntfs_log_error("add_attr_index_root failed: %s\n", strerror(-err));
1N/A return err;
1N/A}
1N/A
1N/A/**
1N/A * add_attr_index_alloc
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int add_attr_index_alloc(MFT_RECORD *m, const char *name,
1N/A const u32 name_len, const IGNORE_CASE_BOOL ic,
1N/A const u8 *index_alloc_val, const u32 index_alloc_val_len)
1N/A{
1N/A int err;
1N/A
1N/A err = insert_non_resident_attr_in_mft_record(m, AT_INDEX_ALLOCATION,
1N/A name, name_len, ic, 0, index_alloc_val,
1N/A index_alloc_val_len);
1N/A if (err < 0)
1N/A ntfs_log_error("add_attr_index_alloc failed: %s\n", strerror(-err));
1N/A return err;
1N/A}
1N/A
1N/A/**
1N/A * add_attr_bitmap
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int add_attr_bitmap(MFT_RECORD *m, const char *name, const u32 name_len,
1N/A const IGNORE_CASE_BOOL ic, const u8 *bitmap,
1N/A const u32 bitmap_len)
1N/A{
1N/A int err;
1N/A
1N/A /* Does it fit? NO: create non-resident. YES: create resident. */
1N/A if (le32_to_cpu(m->bytes_in_use) + 24 + bitmap_len >
1N/A le32_to_cpu(m->bytes_allocated))
1N/A err = insert_non_resident_attr_in_mft_record(m, AT_BITMAP, name,
1N/A name_len, ic, 0, bitmap, bitmap_len);
1N/A else
1N/A err = insert_resident_attr_in_mft_record(m, AT_BITMAP, name,
1N/A name_len, ic, 0, 0, bitmap, bitmap_len);
1N/A
1N/A if (err < 0)
1N/A ntfs_log_error("add_attr_bitmap failed: %s\n", strerror(-err));
1N/A return err;
1N/A}
1N/A
1N/A/**
1N/A * add_attr_bitmap_positioned
1N/A *
1N/A * Create a non-resident bitmap attribute with a predefined on disk location
1N/A * specified by the runlist @rl. The clusters specified by @rl are assumed to
1N/A * be allocated already.
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int add_attr_bitmap_positioned(MFT_RECORD *m, const char *name,
1N/A const u32 name_len, const IGNORE_CASE_BOOL ic,
1N/A const runlist *rl, const u8 *bitmap, const u32 bitmap_len)
1N/A{
1N/A int err;
1N/A
1N/A err = insert_positioned_attr_in_mft_record(m, AT_BITMAP, name, name_len,
1N/A ic, 0, rl, bitmap, bitmap_len);
1N/A if (err < 0)
1N/A ntfs_log_error("add_attr_bitmap_positioned failed: %s\n",
1N/A strerror(-err));
1N/A return err;
1N/A}
1N/A
1N/A
1N/A/**
1N/A * upgrade_to_large_index
1N/A *
1N/A * Create bitmap and index allocation attributes, modify index root
1N/A * attribute accordingly and move all of the index entries from the index root
1N/A * into the index allocation.
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int upgrade_to_large_index(MFT_RECORD *m, const char *name,
1N/A u32 name_len, const IGNORE_CASE_BOOL ic,
1N/A INDEX_ALLOCATION **idx)
1N/A{
1N/A ntfs_attr_search_ctx *ctx;
1N/A ATTR_RECORD *a;
1N/A INDEX_ROOT *r;
1N/A INDEX_ENTRY *re;
1N/A INDEX_ALLOCATION *ia_val = NULL;
1N/A ntfschar *uname = NULL;
1N/A int uname_len = 0;
1N/A u8 bmp[8];
1N/A char *re_start, *re_end;
1N/A int i, err, index_block_size;
1N/A
1N/A uname = ntfs_str2ucs(name, &uname_len);
1N/A if (!uname)
1N/A return -errno;
1N/A
1N/A /* Find the index root attribute. */
1N/A ctx = ntfs_attr_get_search_ctx(NULL, m);
1N/A if (!ctx) {
1N/A ntfs_log_error("Failed to allocate attribute search context.\n");
1N/A ntfs_ucsfree(uname);
1N/A return -ENOMEM;
1N/A }
1N/A if (ic == IGNORE_CASE) {
1N/A ntfs_log_error("FIXME: Hit unimplemented code path #4.\n");
1N/A err = -EOPNOTSUPP;
1N/A ntfs_ucsfree(uname);
1N/A goto err_out;
1N/A }
1N/A err = mkntfs_attr_lookup(AT_INDEX_ROOT, uname, uname_len, ic, 0, NULL, 0,
1N/A ctx);
1N/A ntfs_ucsfree(uname);
1N/A if (err) {
1N/A err = -ENOTDIR;
1N/A goto err_out;
1N/A }
1N/A a = ctx->attr;
1N/A if (a->non_resident || a->flags) {
1N/A err = -EINVAL;
1N/A goto err_out;
1N/A }
1N/A r = (INDEX_ROOT*)((char*)a + le16_to_cpu(a->u.res.value_offset));
1N/A re_end = (char*)r + le32_to_cpu(a->u.res.value_length);
1N/A re_start = (char*)&r->index + le32_to_cpu(r->index.entries_offset);
1N/A re = (INDEX_ENTRY*)re_start;
1N/A index_block_size = le32_to_cpu(r->index_block_size);
1N/A memset(bmp, 0, sizeof(bmp));
1N/A ntfs_bit_set(bmp, 0ULL, 1);
1N/A /* Bitmap has to be at least 8 bytes in size. */
1N/A err = add_attr_bitmap(m, name, name_len, ic, bmp, sizeof(bmp));
1N/A if (err)
1N/A goto err_out;
1N/A ia_val = ntfs_calloc(index_block_size);
1N/A if (!ia_val) {
1N/A err = -errno;
1N/A goto err_out;
1N/A }
1N/A /* Setup header. */
1N/A ia_val->magic = magic_INDX;
1N/A ia_val->usa_ofs = cpu_to_le16(sizeof(INDEX_ALLOCATION));
1N/A if (index_block_size >= NTFS_BLOCK_SIZE) {
1N/A ia_val->usa_count = cpu_to_le16(index_block_size /
1N/A NTFS_BLOCK_SIZE + 1);
1N/A } else {
1N/A ia_val->usa_count = cpu_to_le16(1);
1N/A ntfs_log_error("Sector size is bigger than index block size. "
1N/A "Setting usa_count to 1. If Windows chkdsk "
1N/A "reports this as corruption, please email %s "
1N/A "stating that you saw this message and that "
1N/A "the filesystem created was corrupt. "
1N/A "Thank you.", NTFS_DEV_LIST);
1N/A }
1N/A /* Set USN to 1. */
1N/A *(le16*)((char*)ia_val + le16_to_cpu(ia_val->usa_ofs)) =
1N/A cpu_to_le16(1);
1N/A ia_val->lsn = 0;
1N/A ia_val->index_block_vcn = 0;
1N/A ia_val->index.flags = LEAF_NODE;
1N/A /* Align to 8-byte boundary. */
1N/A ia_val->index.entries_offset = cpu_to_le32((sizeof(INDEX_HEADER) +
1N/A le16_to_cpu(ia_val->usa_count) * 2 + 7) & ~7);
1N/A ia_val->index.allocated_size = cpu_to_le32(index_block_size -
1N/A (sizeof(INDEX_ALLOCATION) - sizeof(INDEX_HEADER)));
1N/A /* Find the last entry in the index root and save it in re. */
1N/A while ((char*)re < re_end && !(re->flags & INDEX_ENTRY_END)) {
1N/A /* Next entry in index root. */
1N/A re = (INDEX_ENTRY*)((char*)re + le16_to_cpu(re->length));
1N/A }
1N/A /* Copy all the entries including the termination entry. */
1N/A i = (char*)re - re_start + le16_to_cpu(re->length);
1N/A memcpy((char*)&ia_val->index +
1N/A le32_to_cpu(ia_val->index.entries_offset), re_start, i);
1N/A /* Finish setting up index allocation. */
1N/A ia_val->index.index_length = cpu_to_le32(i +
1N/A le32_to_cpu(ia_val->index.entries_offset));
1N/A /* Move the termination entry forward to the beginning if necessary. */
1N/A if ((char*)re > re_start) {
1N/A memmove(re_start, (char*)re, le16_to_cpu(re->length));
1N/A re = (INDEX_ENTRY*)re_start;
1N/A }
1N/A /* Now fixup empty index root with pointer to index allocation VCN 0. */
1N/A r->index.flags = LARGE_INDEX;
1N/A re->flags |= INDEX_ENTRY_NODE;
1N/A if (le16_to_cpu(re->length) < sizeof(INDEX_ENTRY_HEADER) + sizeof(VCN))
1N/A re->length = cpu_to_le16(le16_to_cpu(re->length) + sizeof(VCN));
1N/A r->index.index_length = cpu_to_le32(le32_to_cpu(r->index.entries_offset)
1N/A + le16_to_cpu(re->length));
1N/A r->index.allocated_size = r->index.index_length;
1N/A /* Resize index root attribute. */
1N/A if (ntfs_resident_attr_value_resize(m, a, sizeof(INDEX_ROOT) -
1N/A sizeof(INDEX_HEADER) +
1N/A le32_to_cpu(r->index.allocated_size))) {
1N/A /* TODO: Remove the added bitmap! */
1N/A /* Revert index root from index allocation. */
1N/A err = -errno;
1N/A goto err_out;
1N/A }
1N/A /* Set VCN pointer to 0LL. */
1N/A *(leVCN*)((char*)re + le16_to_cpu(re->length) - sizeof(VCN)) = 0;
1N/A err = ntfs_mst_pre_write_fixup((NTFS_RECORD*)ia_val, index_block_size);
1N/A if (err) {
1N/A err = -errno;
1N/A ntfs_log_error("ntfs_mst_pre_write_fixup() failed in "
1N/A "upgrade_to_large_index.\n");
1N/A goto err_out;
1N/A }
1N/A err = add_attr_index_alloc(m, name, name_len, ic, (u8*)ia_val,
1N/A index_block_size);
1N/A ntfs_mst_post_write_fixup((NTFS_RECORD*)ia_val);
1N/A if (err) {
1N/A /* TODO: Remove the added bitmap! */
1N/A /* Revert index root from index allocation. */
1N/A goto err_out;
1N/A }
1N/A *idx = ia_val;
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A return 0;
1N/Aerr_out:
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A free(ia_val);
1N/A return err;
1N/A}
1N/A
1N/A/**
1N/A * make_room_for_index_entry_in_index_block
1N/A *
1N/A * Create space of @size bytes at position @pos inside the index block @idx.
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int make_room_for_index_entry_in_index_block(INDEX_BLOCK *idx,
1N/A INDEX_ENTRY *pos, u32 size)
1N/A{
1N/A u32 biu;
1N/A
1N/A if (!size)
1N/A return 0;
1N/A#ifdef DEBUG
1N/A /*
1N/A * Rigorous consistency checks. Always return -EINVAL even if more
1N/A * appropriate codes exist for simplicity of parsing the return value.
1N/A */
1N/A if (size != ((size + 7) & ~7)) {
1N/A ntfs_log_error("make_room_for_index_entry_in_index_block() received "
1N/A "non 8-byte aligned size.\n");
1N/A return -EINVAL;
1N/A }
1N/A if (!idx || !pos)
1N/A return -EINVAL;
1N/A if ((char*)pos < (char*)idx || (char*)pos + size < (char*)idx ||
1N/A (char*)pos > (char*)idx + sizeof(INDEX_BLOCK) -
1N/A sizeof(INDEX_HEADER) +
1N/A le32_to_cpu(idx->index.allocated_size) ||
1N/A (char*)pos + size > (char*)idx + sizeof(INDEX_BLOCK) -
1N/A sizeof(INDEX_HEADER) +
1N/A le32_to_cpu(idx->index.allocated_size))
1N/A return -EINVAL;
1N/A /* The - sizeof(INDEX_ENTRY_HEADER) is for the index terminator. */
1N/A if ((char*)pos - (char*)&idx->index >
1N/A (int)le32_to_cpu(idx->index.index_length)
1N/A - (int)sizeof(INDEX_ENTRY_HEADER))
1N/A return -EINVAL;
1N/A#endif
1N/A biu = le32_to_cpu(idx->index.index_length);
1N/A /* Do we have enough space? */
1N/A if (biu + size > le32_to_cpu(idx->index.allocated_size))
1N/A return -ENOSPC;
1N/A /* Move everything after pos to pos + size. */
1N/A memmove((char*)pos + size, (char*)pos, biu - ((char*)pos -
1N/A (char*)&idx->index));
1N/A /* Update index block. */
1N/A idx->index.index_length = cpu_to_le32(biu + size);
1N/A return 0;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_index_keys_compare
1N/A *
1N/A * not all types of COLLATION_RULES supported yet...
1N/A * added as needed.. (remove this comment when all are added)
1N/A */
1N/Astatic int ntfs_index_keys_compare(u8 *key1, u8 *key2, int key1_length,
1N/A int key2_length, COLLATION_RULES collation_rule)
1N/A{
1N/A u32 u1, u2;
1N/A int i;
1N/A
1N/A if (collation_rule == COLLATION_NTOFS_ULONG) {
1N/A /* i.e. $SII or $QUOTA-$Q */
1N/A u1 = le32_to_cpup(key1);
1N/A u2 = le32_to_cpup(key2);
1N/A if (u1 < u2)
1N/A return -1;
1N/A if (u1 > u2)
1N/A return 1;
1N/A /* u1 == u2 */
1N/A return 0;
1N/A }
1N/A if (collation_rule == COLLATION_NTOFS_ULONGS) {
1N/A /* i.e $OBJID-$O */
1N/A i = 0;
1N/A while (i < min(key1_length, key2_length)) {
1N/A u1 = le32_to_cpup(key1 + i);
1N/A u2 = le32_to_cpup(key2 + i);
1N/A if (u1 < u2)
1N/A return -1;
1N/A if (u1 > u2)
1N/A return 1;
1N/A /* u1 == u2 */
1N/A i += sizeof(u32);
1N/A }
1N/A if (key1_length < key2_length)
1N/A return -1;
1N/A if (key1_length > key2_length)
1N/A return 1;
1N/A return 0;
1N/A }
1N/A if (collation_rule == COLLATION_NTOFS_SECURITY_HASH) {
1N/A /* i.e. $SDH */
1N/A u1 = le32_to_cpu(((SDH_INDEX_KEY*)key1)->hash);
1N/A u2 = le32_to_cpu(((SDH_INDEX_KEY*)key2)->hash);
1N/A if (u1 < u2)
1N/A return -1;
1N/A if (u1 > u2)
1N/A return 1;
1N/A /* u1 == u2 */
1N/A u1 = le32_to_cpu(((SDH_INDEX_KEY*)key1)->security_id);
1N/A u2 = le32_to_cpu(((SDH_INDEX_KEY*)key2)->security_id);
1N/A if (u1 < u2)
1N/A return -1;
1N/A if (u1 > u2)
1N/A return 1;
1N/A return 0;
1N/A }
1N/A if (collation_rule == COLLATION_NTOFS_SID) {
1N/A /* i.e. $QUOTA-O */
1N/A i = memcmp(key1, key2, min(key1_length, key2_length));
1N/A if (!i) {
1N/A if (key1_length < key2_length)
1N/A return -1;
1N/A if (key1_length > key2_length)
1N/A return 1;
1N/A }
1N/A return i;
1N/A }
1N/A ntfs_log_critical("ntfs_index_keys_compare called without supported "
1N/A "collation rule.\n");
1N/A return 0; /* Claim they're equal. What else can we do? */
1N/A}
1N/A
1N/A/**
1N/A * insert_index_entry_in_res_dir_index
1N/A *
1N/A * i.e. insert an index_entry in some named index_root
1N/A * simplified search method, works for mkntfs
1N/A */
1N/Astatic int insert_index_entry_in_res_dir_index(INDEX_ENTRY *idx, u32 idx_size,
1N/A MFT_RECORD *m, ntfschar *name, u32 name_size, ATTR_TYPES type)
1N/A{
1N/A ntfs_attr_search_ctx *ctx;
1N/A INDEX_HEADER *idx_header;
1N/A INDEX_ENTRY *idx_entry, *idx_end;
1N/A ATTR_RECORD *a;
1N/A COLLATION_RULES collation_rule;
1N/A int err, i;
1N/A
1N/A err = 0;
1N/A /* does it fit ?*/
1N/A if (g_vol->mft_record_size > idx_size + le32_to_cpu(m->bytes_allocated))
1N/A return -ENOSPC;
1N/A /* find the INDEX_ROOT attribute:*/
1N/A ctx = ntfs_attr_get_search_ctx(NULL, m);
1N/A if (!ctx) {
1N/A ntfs_log_error("Failed to allocate attribute search "
1N/A "context.\n");
1N/A err = -ENOMEM;
1N/A goto err_out;
1N/A }
1N/A if (mkntfs_attr_lookup(AT_INDEX_ROOT, name, name_size, 0, 0, NULL, 0,
1N/A ctx)) {
1N/A err = -EEXIST;
1N/A goto err_out;
1N/A }
1N/A /* found attribute */
1N/A a = (ATTR_RECORD*)ctx->attr;
1N/A collation_rule = ((INDEX_ROOT*)((u8*)a +
1N/A le16_to_cpu(a->u.res.value_offset)))->collation_rule;
1N/A idx_header = (INDEX_HEADER*)((u8*)a + le16_to_cpu(a->u.res.value_offset)
1N/A + 0x10);
1N/A idx_entry = (INDEX_ENTRY*)((u8*)idx_header +
1N/A le32_to_cpu(idx_header->entries_offset));
1N/A idx_end = (INDEX_ENTRY*)((u8*)idx_entry +
1N/A le32_to_cpu(idx_header->index_length));
1N/A /*
1N/A * Loop until we exceed valid memory (corruption case) or until we
1N/A * reach the last entry.
1N/A */
1N/A if (type == AT_FILE_NAME) {
1N/A while (((u8*)idx_entry < (u8*)idx_end) &&
1N/A !(idx_entry->flags & INDEX_ENTRY_END)) {
1N/A i = ntfs_file_values_compare(&idx->key.file_name,
1N/A &idx_entry->key.file_name, 1,
1N/A IGNORE_CASE, g_vol->upcase,
1N/A g_vol->upcase_len);
1N/A /*
1N/A * If @file_name collates before ie->key.file_name,
1N/A * there is no matching index entry.
1N/A */
1N/A if (i == -1)
1N/A break;
1N/A /* If file names are not equal, continue search. */
1N/A if (i)
1N/A goto do_next;
1N/A if (idx->key.file_name.file_name_type !=
1N/A FILE_NAME_POSIX ||
1N/A idx_entry->key.file_name.file_name_type
1N/A != FILE_NAME_POSIX)
1N/A return -EEXIST;
1N/A i = ntfs_file_values_compare(&idx->key.file_name,
1N/A &idx_entry->key.file_name, 1,
1N/A CASE_SENSITIVE, g_vol->upcase,
1N/A g_vol->upcase_len);
1N/A if (!i)
1N/A return -EEXIST;
1N/A if (i == -1)
1N/A break;
1N/Ado_next:
1N/A idx_entry = (INDEX_ENTRY*)((u8*)idx_entry +
1N/A le16_to_cpu(idx_entry->length));
1N/A }
1N/A } else if (type == AT_UNUSED) { /* case view */
1N/A while (((u8*)idx_entry < (u8*)idx_end) &&
1N/A !(idx_entry->flags & INDEX_ENTRY_END)) {
1N/A i = ntfs_index_keys_compare((u8*)idx + 0x10,
1N/A (u8*)idx_entry + 0x10,
1N/A le16_to_cpu(idx->key_length),
1N/A le16_to_cpu(idx_entry->key_length),
1N/A collation_rule);
1N/A if (!i)
1N/A return -EEXIST;
1N/A if (i == -1)
1N/A break;
1N/A idx_entry = (INDEX_ENTRY*)((u8*)idx_entry +
1N/A le16_to_cpu(idx_entry->length));
1N/A }
1N/A } else
1N/A return -EINVAL;
1N/A memmove((u8*)idx_entry + idx_size, (u8*)idx_entry,
1N/A le32_to_cpu(m->bytes_in_use) -
1N/A ((u8*)idx_entry - (u8*)m));
1N/A memcpy((u8*)idx_entry, (u8*)idx, idx_size);
1N/A /* Adjust various offsets, etc... */
1N/A m->bytes_in_use = cpu_to_le32(le32_to_cpu(m->bytes_in_use) + idx_size);
1N/A a->length = cpu_to_le32(le32_to_cpu(a->length) + idx_size);
1N/A a->u.res.value_length = cpu_to_le32(le32_to_cpu(a->u.res.value_length) + idx_size);
1N/A idx_header->index_length = cpu_to_le32(
1N/A le32_to_cpu(idx_header->index_length) + idx_size);
1N/A idx_header->allocated_size = cpu_to_le32(
1N/A le32_to_cpu(idx_header->allocated_size) + idx_size);
1N/Aerr_out:
1N/A if (ctx)
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A return err;
1N/A}
1N/A
1N/A/**
1N/A * initialize_secure
1N/A *
1N/A * initializes $Secure's $SDH and $SII indexes from $SDS datastream
1N/A */
1N/Astatic int initialize_secure(char *sds, u32 sds_size, MFT_RECORD *m)
1N/A{
1N/A int err, sdh_size, sii_size;
1N/A SECURITY_DESCRIPTOR_HEADER *sds_header;
1N/A INDEX_ENTRY *idx_entry_sdh, *idx_entry_sii;
1N/A SDH_INDEX_DATA *sdh_data;
1N/A SII_INDEX_DATA *sii_data;
1N/A
1N/A sds_header = (SECURITY_DESCRIPTOR_HEADER*)sds;
1N/A sdh_size = sizeof(INDEX_ENTRY_HEADER);
1N/A sdh_size += sizeof(SDH_INDEX_KEY) + sizeof(SDH_INDEX_DATA);
1N/A sii_size = sizeof(INDEX_ENTRY_HEADER);
1N/A sii_size += sizeof(SII_INDEX_KEY) + sizeof(SII_INDEX_DATA);
1N/A idx_entry_sdh = ntfs_calloc(sizeof(INDEX_ENTRY));
1N/A if (!idx_entry_sdh)
1N/A return -errno;
1N/A idx_entry_sii = ntfs_calloc(sizeof(INDEX_ENTRY));
1N/A if (!idx_entry_sii) {
1N/A free(idx_entry_sdh);
1N/A return -errno;
1N/A }
1N/A err = 0;
1N/A
1N/A while ((char*)sds_header < (char*)sds + sds_size) {
1N/A if (!sds_header->length)
1N/A break;
1N/A /* SDH index entry */
1N/A idx_entry_sdh->u.s.data_offset = const_cpu_to_le16(0x18);
1N/A idx_entry_sdh->u.s.data_length = const_cpu_to_le16(0x14);
1N/A idx_entry_sdh->u.s.reservedV = const_cpu_to_le32(0x00);
1N/A idx_entry_sdh->length = const_cpu_to_le16(0x30);
1N/A idx_entry_sdh->key_length = const_cpu_to_le16(0x08);
1N/A idx_entry_sdh->flags = const_cpu_to_le16(0x00);
1N/A idx_entry_sdh->reserved = const_cpu_to_le16(0x00);
1N/A idx_entry_sdh->key.sdh.hash = sds_header->hash;
1N/A idx_entry_sdh->key.sdh.security_id = sds_header->security_id;
1N/A sdh_data = (SDH_INDEX_DATA*)((u8*)idx_entry_sdh +
1N/A le16_to_cpu(idx_entry_sdh->u.s.data_offset));
1N/A sdh_data->hash = sds_header->hash;
1N/A sdh_data->security_id = sds_header->security_id;
1N/A sdh_data->offset = sds_header->offset;
1N/A sdh_data->length = sds_header->length;
1N/A sdh_data->reserved_II = const_cpu_to_le32(0x00490049);
1N/A
1N/A /* SII index entry */
1N/A idx_entry_sii->u.s.data_offset = const_cpu_to_le16(0x14);
1N/A idx_entry_sii->u.s.data_length = const_cpu_to_le16(0x14);
1N/A idx_entry_sii->u.s.reservedV = const_cpu_to_le32(0x00);
1N/A idx_entry_sii->length = const_cpu_to_le16(0x28);
1N/A idx_entry_sii->key_length = const_cpu_to_le16(0x04);
1N/A idx_entry_sii->flags = const_cpu_to_le16(0x00);
1N/A idx_entry_sii->reserved = const_cpu_to_le16(0x00);
1N/A idx_entry_sii->key.sii.security_id = sds_header->security_id;
1N/A sii_data = (SII_INDEX_DATA*)((u8*)idx_entry_sii +
1N/A le16_to_cpu(idx_entry_sii->u.s.data_offset));
1N/A sii_data->hash = sds_header->hash;
1N/A sii_data->security_id = sds_header->security_id;
1N/A sii_data->offset = sds_header->offset;
1N/A sii_data->length = sds_header->length;
1N/A if ((err = insert_index_entry_in_res_dir_index(idx_entry_sdh,
1N/A sdh_size, m, NTFS_INDEX_SDH, 4, AT_UNUSED)))
1N/A break;
1N/A if ((err = insert_index_entry_in_res_dir_index(idx_entry_sii,
1N/A sii_size, m, NTFS_INDEX_SII, 4, AT_UNUSED)))
1N/A break;
1N/A sds_header = (SECURITY_DESCRIPTOR_HEADER*)((u8*)sds_header +
1N/A ((le32_to_cpu(sds_header->length) + 15) & ~15));
1N/A }
1N/A free(idx_entry_sdh);
1N/A free(idx_entry_sii);
1N/A return err;
1N/A}
1N/A
1N/A/**
1N/A * initialize_quota
1N/A *
1N/A * initialize $Quota with the default quota index-entries.
1N/A */
1N/Astatic int initialize_quota(MFT_RECORD *m)
1N/A{
1N/A int o_size, q1_size, q2_size, err, i;
1N/A INDEX_ENTRY *idx_entry_o, *idx_entry_q1, *idx_entry_q2;
1N/A QUOTA_O_INDEX_DATA *idx_entry_o_data;
1N/A QUOTA_CONTROL_ENTRY *idx_entry_q1_data, *idx_entry_q2_data;
1N/A
1N/A err = 0;
1N/A /* q index entry num 1 */
1N/A q1_size = 0x48;
1N/A idx_entry_q1 = ntfs_calloc(q1_size);
1N/A if (!idx_entry_q1)
1N/A return errno;
1N/A idx_entry_q1->u.s.data_offset = const_cpu_to_le16(0x14);
1N/A idx_entry_q1->u.s.data_length = const_cpu_to_le16(0x30);
1N/A idx_entry_q1->u.s.reservedV = const_cpu_to_le32(0x00);
1N/A idx_entry_q1->length = const_cpu_to_le16(0x48);
1N/A idx_entry_q1->key_length = const_cpu_to_le16(0x04);
1N/A idx_entry_q1->flags = const_cpu_to_le16(0x00);
1N/A idx_entry_q1->reserved = const_cpu_to_le16(0x00);
1N/A idx_entry_q1->key.owner_id = const_cpu_to_le32(0x01);
1N/A idx_entry_q1_data = (QUOTA_CONTROL_ENTRY*)((char*)idx_entry_q1
1N/A + le16_to_cpu(idx_entry_q1->u.s.data_offset));
1N/A idx_entry_q1_data->version = const_cpu_to_le32(0x02);
1N/A idx_entry_q1_data->flags = QUOTA_FLAG_DEFAULT_LIMITS;
1N/A idx_entry_q1_data->bytes_used = const_cpu_to_le64(0x00);
1N/A idx_entry_q1_data->change_time = utc2ntfs(mkntfs_time());
1N/A idx_entry_q1_data->threshold = cpu_to_sle64(-1);
1N/A idx_entry_q1_data->limit = cpu_to_sle64(-1);
1N/A idx_entry_q1_data->exceeded_time = 0;
1N/A err = insert_index_entry_in_res_dir_index(idx_entry_q1, q1_size, m,
1N/A NTFS_INDEX_Q, 2, AT_UNUSED);
1N/A free(idx_entry_q1);
1N/A if (err)
1N/A return err;
1N/A /* q index entry num 2 */
1N/A q2_size = 0x58;
1N/A idx_entry_q2 = ntfs_calloc(q2_size);
1N/A if (!idx_entry_q2)
1N/A return errno;
1N/A idx_entry_q2->u.s.data_offset = const_cpu_to_le16(0x14);
1N/A idx_entry_q2->u.s.data_length = const_cpu_to_le16(0x40);
1N/A idx_entry_q2->u.s.reservedV = const_cpu_to_le32(0x00);
1N/A idx_entry_q2->length = const_cpu_to_le16(0x58);
1N/A idx_entry_q2->key_length = const_cpu_to_le16(0x04);
1N/A idx_entry_q2->flags = const_cpu_to_le16(0x00);
1N/A idx_entry_q2->reserved = const_cpu_to_le16(0x00);
1N/A idx_entry_q2->key.owner_id = QUOTA_FIRST_USER_ID;
1N/A idx_entry_q2_data = (QUOTA_CONTROL_ENTRY*)((char*)idx_entry_q2
1N/A + le16_to_cpu(idx_entry_q2->u.s.data_offset));
1N/A idx_entry_q2_data->version = const_cpu_to_le32(0x02);
1N/A idx_entry_q2_data->flags = QUOTA_FLAG_DEFAULT_LIMITS;
1N/A idx_entry_q2_data->bytes_used = const_cpu_to_le64(0x00);
1N/A idx_entry_q2_data->change_time = utc2ntfs(mkntfs_time());;
1N/A idx_entry_q2_data->threshold = cpu_to_sle64(-1);
1N/A idx_entry_q2_data->limit = cpu_to_sle64(-1);
1N/A idx_entry_q2_data->exceeded_time = 0;
1N/A idx_entry_q2_data->sid.revision = 1;
1N/A idx_entry_q2_data->sid.sub_authority_count = 2;
1N/A for (i = 0; i < 5; i++)
1N/A idx_entry_q2_data->sid.identifier_authority.value[i] = 0;
1N/A idx_entry_q2_data->sid.identifier_authority.value[5] = 0x05;
1N/A idx_entry_q2_data->sid.sub_authority[0] =
1N/A const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID);
1N/A idx_entry_q2_data->sid.sub_authority[1] =
1N/A const_cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS);
1N/A err = insert_index_entry_in_res_dir_index(idx_entry_q2, q2_size, m,
1N/A NTFS_INDEX_Q, 2, AT_UNUSED);
1N/A free(idx_entry_q2);
1N/A if (err)
1N/A return err;
1N/A o_size = 0x28;
1N/A idx_entry_o = ntfs_calloc(o_size);
1N/A if (!idx_entry_o)
1N/A return errno;
1N/A idx_entry_o->u.s.data_offset = const_cpu_to_le16(0x20);
1N/A idx_entry_o->u.s.data_length = const_cpu_to_le16(0x04);
1N/A idx_entry_o->u.s.reservedV = const_cpu_to_le32(0x00);
1N/A idx_entry_o->length = const_cpu_to_le16(0x28);
1N/A idx_entry_o->key_length = const_cpu_to_le16(0x10);
1N/A idx_entry_o->flags = const_cpu_to_le16(0x00);
1N/A idx_entry_o->reserved = const_cpu_to_le16(0x00);
1N/A idx_entry_o->key.sid.revision = 0x01;
1N/A idx_entry_o->key.sid.sub_authority_count = 0x02;
1N/A for (i = 0; i < 5; i++)
1N/A idx_entry_o->key.sid.identifier_authority.value[i] = 0;
1N/A idx_entry_o->key.sid.identifier_authority.value[5] = 0x05;
1N/A idx_entry_o->key.sid.sub_authority[0] =
1N/A const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID);
1N/A idx_entry_o->key.sid.sub_authority[1] =
1N/A const_cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS);
1N/A idx_entry_o_data = (QUOTA_O_INDEX_DATA*)((char*)idx_entry_o
1N/A + le16_to_cpu(idx_entry_o->u.s.data_offset));
1N/A idx_entry_o_data->owner_id = QUOTA_FIRST_USER_ID;
1N/A /* 20 00 00 00 padding after here on ntfs 3.1. 3.0 is unchecked. */
1N/A idx_entry_o_data->unknown = const_cpu_to_le32(32);
1N/A err = insert_index_entry_in_res_dir_index(idx_entry_o, o_size, m,
1N/A NTFS_INDEX_O, 2, AT_UNUSED);
1N/A free(idx_entry_o);
1N/A
1N/A return err;
1N/A}
1N/A
1N/A/**
1N/A * insert_file_link_in_dir_index
1N/A *
1N/A * Insert the fully completed FILE_NAME_ATTR @file_name which is inside
1N/A * the file with mft reference @file_ref into the index (allocation) block
1N/A * @idx (which belongs to @file_ref's parent directory).
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int insert_file_link_in_dir_index(INDEX_BLOCK *idx, leMFT_REF file_ref,
1N/A FILE_NAME_ATTR *file_name, u32 file_name_size)
1N/A{
1N/A int err, i;
1N/A INDEX_ENTRY *ie;
1N/A char *index_end;
1N/A
1N/A /*
1N/A * Lookup dir entry @file_name in dir @idx to determine correct
1N/A * insertion location. FIXME: Using a very oversimplified lookup
1N/A * method which is sufficient for mkntfs but no good whatsoever in
1N/A * real world scenario. (AIA)
1N/A */
1N/A
1N/A index_end = (char*)&idx->index + le32_to_cpu(idx->index.index_length);
1N/A ie = (INDEX_ENTRY*)((char*)&idx->index +
1N/A le32_to_cpu(idx->index.entries_offset));
1N/A /*
1N/A * Loop until we exceed valid memory (corruption case) or until we
1N/A * reach the last entry.
1N/A */
1N/A while ((char*)ie < index_end && !(ie->flags & INDEX_ENTRY_END)) {
1N/A#if 0
1N/A#ifdef DEBUG
1N/A ntfs_log_debug("file_name_attr1->file_name_length = %i\n",
1N/A file_name->file_name_length);
1N/A if (file_name->file_name_length) {
1N/A char *__buf = NULL;
1N/A i = ntfs_ucstombs((ntfschar*)&file_name->file_name,
1N/A file_name->file_name_length, &__buf, 0);
1N/A if (i < 0)
1N/A ntfs_log_debug("Name contains non-displayable "
1N/A "Unicode characters.\n");
1N/A ntfs_log_debug("file_name_attr1->file_name = %s\n",
1N/A __buf);
1N/A free(__buf);
1N/A }
1N/A ntfs_log_debug("file_name_attr2->file_name_length = %i\n",
1N/A ie->key.file_name.file_name_length);
1N/A if (ie->key.file_name.file_name_length) {
1N/A char *__buf = NULL;
1N/A i = ntfs_ucstombs(ie->key.file_name.file_name,
1N/A ie->key.file_name.file_name_length + 1, &__buf,
1N/A 0);
1N/A if (i < 0)
1N/A ntfs_log_debug("Name contains non-displayable "
1N/A "Unicode characters.\n");
1N/A ntfs_log_debug("file_name_attr2->file_name = %s\n",
1N/A __buf);
1N/A free(__buf);
1N/A }
1N/A#endif
1N/A#endif
1N/A i = ntfs_file_values_compare(file_name,
1N/A (FILE_NAME_ATTR*)&ie->key.file_name, 1,
1N/A IGNORE_CASE, g_vol->upcase, g_vol->upcase_len);
1N/A /*
1N/A * If @file_name collates before ie->key.file_name, there is no
1N/A * matching index entry.
1N/A */
1N/A if (i == -1)
1N/A break;
1N/A /* If file names are not equal, continue search. */
1N/A if (i)
1N/A goto do_next;
1N/A /* File names are equal when compared ignoring case. */
1N/A /*
1N/A * If BOTH file names are in the POSIX namespace, do a case
1N/A * sensitive comparison as well. Otherwise the names match so
1N/A * we return -EEXIST. FIXME: There are problems with this in a
1N/A * real world scenario, when one is POSIX and one isn't, but
1N/A * fine for mkntfs where we don't use POSIX namespace at all
1N/A * and hence this following code is luxury. (AIA)
1N/A */
1N/A if (file_name->file_name_type != FILE_NAME_POSIX ||
1N/A ie->key.file_name.file_name_type != FILE_NAME_POSIX)
1N/A return -EEXIST;
1N/A i = ntfs_file_values_compare(file_name,
1N/A (FILE_NAME_ATTR*)&ie->key.file_name, 1,
1N/A CASE_SENSITIVE, g_vol->upcase,
1N/A g_vol->upcase_len);
1N/A if (i == -1)
1N/A break;
1N/A /* Complete match. Bugger. Can't insert. */
1N/A if (!i)
1N/A return -EEXIST;
1N/Ado_next:
1N/A#ifdef DEBUG
1N/A /* Next entry. */
1N/A if (!ie->length) {
1N/A ntfs_log_debug("BUG: ie->length is zero, breaking out "
1N/A "of loop.\n");
1N/A break;
1N/A }
1N/A#endif
1N/A ie = (INDEX_ENTRY*)((char*)ie + le16_to_cpu(ie->length));
1N/A };
1N/A i = (sizeof(INDEX_ENTRY_HEADER) + file_name_size + 7) & ~7;
1N/A err = make_room_for_index_entry_in_index_block(idx, ie, i);
1N/A if (err) {
1N/A ntfs_log_error("make_room_for_index_entry_in_index_block "
1N/A "failed: %s\n", strerror(-err));
1N/A return err;
1N/A }
1N/A /* Create entry in place and copy file name attribute value. */
1N/A ie->u.indexed_file = file_ref;
1N/A ie->length = cpu_to_le16(i);
1N/A ie->key_length = cpu_to_le16(file_name_size);
1N/A ie->flags = cpu_to_le16(0);
1N/A ie->reserved = cpu_to_le16(0);
1N/A memcpy((char*)&ie->key.file_name, (char*)file_name, file_name_size);
1N/A return 0;
1N/A}
1N/A
1N/A/**
1N/A * create_hardlink_res
1N/A *
1N/A * Create a file_name_attribute in the mft record @m_file which points to the
1N/A * parent directory with mft reference @ref_parent.
1N/A *
1N/A * Then, insert an index entry with this file_name_attribute in the index
1N/A * root @idx of the index_root attribute of the parent directory.
1N/A *
1N/A * @ref_file is the mft reference of @m_file.
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int create_hardlink_res(MFT_RECORD *m_parent, const leMFT_REF ref_parent,
1N/A MFT_RECORD *m_file, const leMFT_REF ref_file,
1N/A const s64 allocated_size, const s64 data_size,
1N/A const FILE_ATTR_FLAGS flags, const u16 packed_ea_size,
1N/A const u32 reparse_point_tag, const char *file_name,
1N/A const FILE_NAME_TYPE_FLAGS file_name_type)
1N/A{
1N/A FILE_NAME_ATTR *fn;
1N/A int i, fn_size, idx_size;
1N/A INDEX_ENTRY *idx_entry_new;
1N/A ntfschar *uname;
1N/A
1N/A /* Create the file_name attribute. */
1N/A i = (strlen(file_name) + 1) * sizeof(ntfschar);
1N/A fn_size = sizeof(FILE_NAME_ATTR) + i;
1N/A fn = ntfs_malloc(fn_size);
1N/A if (!fn)
1N/A return -errno;
1N/A fn->parent_directory = ref_parent;
1N/A /* FIXME: copy the creation_time from the std info */
1N/A fn->creation_time = utc2ntfs(mkntfs_time());
1N/A fn->last_data_change_time = fn->creation_time;
1N/A fn->last_mft_change_time = fn->creation_time;
1N/A fn->last_access_time = fn->creation_time;
1N/A fn->allocated_size = cpu_to_sle64(allocated_size);
1N/A fn->data_size = cpu_to_sle64(data_size);
1N/A fn->file_attributes = flags;
1N/A /* These are in a union so can't have both. */
1N/A if (packed_ea_size && reparse_point_tag) {
1N/A free(fn);
1N/A return -EINVAL;
1N/A }
1N/A if (packed_ea_size) {
1N/A free(fn);
1N/A return -EINVAL;
1N/A }
1N/A if (packed_ea_size) {
1N/A fn->u.s.packed_ea_size = cpu_to_le16(packed_ea_size);
1N/A fn->u.s.reserved = cpu_to_le16(0);
1N/A } else {
1N/A fn->u.reparse_point_tag = cpu_to_le32(reparse_point_tag);
1N/A }
1N/A fn->file_name_type = file_name_type;
1N/A uname = fn->file_name;
1N/A i = ntfs_mbstoucs(file_name, &uname, i);
1N/A if (i < 1) {
1N/A free(fn);
1N/A return -EINVAL;
1N/A }
1N/A if (i > 0xff) {
1N/A free(fn);
1N/A return -ENAMETOOLONG;
1N/A }
1N/A /* No terminating null in file names. */
1N/A fn->file_name_length = i;
1N/A fn_size = sizeof(FILE_NAME_ATTR) + i * sizeof(ntfschar);
1N/A /* Increment the link count of @m_file. */
1N/A i = le16_to_cpu(m_file->link_count);
1N/A if (i == 0xffff) {
1N/A ntfs_log_error("Too many hardlinks present already.\n");
1N/A free(fn);
1N/A return -EINVAL;
1N/A }
1N/A m_file->link_count = cpu_to_le16(i + 1);
1N/A /* Add the file_name to @m_file. */
1N/A i = insert_resident_attr_in_mft_record(m_file, AT_FILE_NAME, NULL, 0, 0,
1N/A 0, RESIDENT_ATTR_IS_INDEXED, (u8*)fn, fn_size);
1N/A if (i < 0) {
1N/A ntfs_log_error("create_hardlink failed adding file name "
1N/A "attribute: %s\n", strerror(-i));
1N/A free(fn);
1N/A /* Undo link count increment. */
1N/A m_file->link_count = cpu_to_le16(
1N/A le16_to_cpu(m_file->link_count) - 1);
1N/A return i;
1N/A }
1N/A /* Insert the index entry for file_name in @idx. */
1N/A idx_size = (fn_size + 7) & ~7;
1N/A idx_entry_new = ntfs_calloc(idx_size + 0x10);
1N/A if (!idx_entry_new)
1N/A return -errno;
1N/A idx_entry_new->u.indexed_file = ref_file;
1N/A idx_entry_new->length = cpu_to_le16(idx_size + 0x10);
1N/A idx_entry_new->key_length = cpu_to_le16(fn_size);
1N/A memcpy((u8*)idx_entry_new + 0x10, (u8*)fn, fn_size);
1N/A i = insert_index_entry_in_res_dir_index(idx_entry_new, idx_size + 0x10,
1N/A m_parent, NTFS_INDEX_I30, 4, AT_FILE_NAME);
1N/A if (i < 0) {
1N/A ntfs_log_error("create_hardlink failed inserting index entry: "
1N/A "%s\n", strerror(-i));
1N/A /* FIXME: Remove the file name attribute from @m_file. */
1N/A free(idx_entry_new);
1N/A free(fn);
1N/A /* Undo link count increment. */
1N/A m_file->link_count = cpu_to_le16(
1N/A le16_to_cpu(m_file->link_count) - 1);
1N/A return i;
1N/A }
1N/A free(idx_entry_new);
1N/A free(fn);
1N/A return 0;
1N/A}
1N/A
1N/A/**
1N/A * create_hardlink
1N/A *
1N/A * Create a file_name_attribute in the mft record @m_file which points to the
1N/A * parent directory with mft reference @ref_parent.
1N/A *
1N/A * Then, insert an index entry with this file_name_attribute in the index
1N/A * block @idx of the index allocation attribute of the parent directory.
1N/A *
1N/A * @ref_file is the mft reference of @m_file.
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int create_hardlink(INDEX_BLOCK *idx, const leMFT_REF ref_parent,
1N/A MFT_RECORD *m_file, const leMFT_REF ref_file,
1N/A const s64 allocated_size, const s64 data_size,
1N/A const FILE_ATTR_FLAGS flags, const u16 packed_ea_size,
1N/A const u32 reparse_point_tag, const char *file_name,
1N/A const FILE_NAME_TYPE_FLAGS file_name_type)
1N/A{
1N/A FILE_NAME_ATTR *fn;
1N/A int i, fn_size;
1N/A ntfschar *uname;
1N/A
1N/A /* Create the file_name attribute. */
1N/A i = (strlen(file_name) + 1) * sizeof(ntfschar);
1N/A fn_size = sizeof(FILE_NAME_ATTR) + i;
1N/A fn = ntfs_malloc(fn_size);
1N/A if (!fn)
1N/A return -errno;
1N/A fn->parent_directory = ref_parent;
1N/A /* FIXME: Is this correct? Or do we have to copy the creation_time */
1N/A /* from the std info? */
1N/A fn->creation_time = utc2ntfs(mkntfs_time());
1N/A fn->last_data_change_time = fn->creation_time;
1N/A fn->last_mft_change_time = fn->creation_time;
1N/A fn->last_access_time = fn->creation_time;
1N/A fn->allocated_size = cpu_to_sle64(allocated_size);
1N/A fn->data_size = cpu_to_sle64(data_size);
1N/A fn->file_attributes = flags;
1N/A /* These are in a union so can't have both. */
1N/A if (packed_ea_size && reparse_point_tag) {
1N/A free(fn);
1N/A return -EINVAL;
1N/A }
1N/A if (packed_ea_size) {
1N/A fn->u.s.packed_ea_size = cpu_to_le16(packed_ea_size);
1N/A fn->u.s.reserved = cpu_to_le16(0);
1N/A } else {
1N/A fn->u.reparse_point_tag = cpu_to_le32(reparse_point_tag);
1N/A }
1N/A fn->file_name_type = file_name_type;
1N/A uname = fn->file_name;
1N/A i = ntfs_mbstoucs(file_name, &uname, i);
1N/A if (i < 1) {
1N/A free(fn);
1N/A return -EINVAL;
1N/A }
1N/A if (i > 0xff) {
1N/A free(fn);
1N/A return -ENAMETOOLONG;
1N/A }
1N/A /* No terminating null in file names. */
1N/A fn->file_name_length = i;
1N/A fn_size = sizeof(FILE_NAME_ATTR) + i * sizeof(ntfschar);
1N/A /* Increment the link count of @m_file. */
1N/A i = le16_to_cpu(m_file->link_count);
1N/A if (i == 0xffff) {
1N/A ntfs_log_error("Too many hardlinks present already.\n");
1N/A free(fn);
1N/A return -EINVAL;
1N/A }
1N/A m_file->link_count = cpu_to_le16(i + 1);
1N/A /* Add the file_name to @m_file. */
1N/A i = insert_resident_attr_in_mft_record(m_file, AT_FILE_NAME, NULL, 0, 0,
1N/A 0, RESIDENT_ATTR_IS_INDEXED, (u8*)fn, fn_size);
1N/A if (i < 0) {
1N/A ntfs_log_error("create_hardlink failed adding file name attribute: "
1N/A "%s\n", strerror(-i));
1N/A free(fn);
1N/A /* Undo link count increment. */
1N/A m_file->link_count = cpu_to_le16(
1N/A le16_to_cpu(m_file->link_count) - 1);
1N/A return i;
1N/A }
1N/A /* Insert the index entry for file_name in @idx. */
1N/A i = insert_file_link_in_dir_index(idx, ref_file, fn, fn_size);
1N/A if (i < 0) {
1N/A ntfs_log_error("create_hardlink failed inserting index entry: %s\n",
1N/A strerror(-i));
1N/A /* FIXME: Remove the file name attribute from @m_file. */
1N/A free(fn);
1N/A /* Undo link count increment. */
1N/A m_file->link_count = cpu_to_le16(
1N/A le16_to_cpu(m_file->link_count) - 1);
1N/A return i;
1N/A }
1N/A free(fn);
1N/A return 0;
1N/A}
1N/A
1N/A#ifdef ENABLE_UUID
1N/A
1N/A/**
1N/A * index_obj_id_insert
1N/A *
1N/A * Insert an index entry with the key @guid and data pointing to the mft record
1N/A * @ref in the $O index root of the mft record @m (which must be the mft record
1N/A * for $ObjId).
1N/A *
1N/A * Return 0 on success or -errno on error.
1N/A */
1N/Astatic int index_obj_id_insert(MFT_RECORD *m, const GUID *guid,
1N/A const leMFT_REF ref)
1N/A{
1N/A INDEX_ENTRY *idx_entry_new;
1N/A int data_ofs, idx_size, err;
1N/A OBJ_ID_INDEX_DATA *oi;
1N/A
1N/A /*
1N/A * Insert the index entry for the object id in the index.
1N/A *
1N/A * First determine the size of the index entry to be inserted. This
1N/A * consists of the index entry header, followed by the index key, i.e.
1N/A * the GUID, followed by the index data, i.e. OBJ_ID_INDEX_DATA.
1N/A */
1N/A data_ofs = (sizeof(INDEX_ENTRY_HEADER) + sizeof(GUID) + 7) & ~7;
1N/A idx_size = (data_ofs + sizeof(OBJ_ID_INDEX_DATA) + 7) & ~7;
1N/A idx_entry_new = ntfs_calloc(idx_size);
1N/A if (!idx_entry_new)
1N/A return -errno;
1N/A idx_entry_new->u.s.data_offset = cpu_to_le16(data_ofs);
1N/A idx_entry_new->u.s.data_length = cpu_to_le16(sizeof(OBJ_ID_INDEX_DATA));
1N/A idx_entry_new->length = cpu_to_le16(idx_size);
1N/A idx_entry_new->key_length = cpu_to_le16(sizeof(GUID));
1N/A idx_entry_new->key.object_id = *guid;
1N/A oi = (OBJ_ID_INDEX_DATA*)((u8*)idx_entry_new + data_ofs);
1N/A oi->mft_reference = ref;
1N/A err = insert_index_entry_in_res_dir_index(idx_entry_new, idx_size, m,
1N/A NTFS_INDEX_O, 2, AT_UNUSED);
1N/A free(idx_entry_new);
1N/A if (err < 0) {
1N/A ntfs_log_error("index_obj_id_insert failed inserting index "
1N/A "entry: %s\n", strerror(-err));
1N/A return err;
1N/A }
1N/A return 0;
1N/A}
1N/A
1N/A#endif
1N/A
1N/A/**
1N/A * mkntfs_cleanup
1N/A */
1N/Astatic void mkntfs_cleanup(void)
1N/A{
1N/A /* Close the volume */
1N/A if (g_vol) {
1N/A if (g_vol->u.dev) {
1N/A if (NDevOpen(g_vol->u.dev) && g_vol->u.dev->d_ops->close(g_vol->u.dev))
1N/A ntfs_log_perror("Warning: Could not close %s", g_vol->u.dev->d_name);
1N/A ntfs_device_free(g_vol->u.dev);
1N/A }
1N/A free(g_vol->vol_name);
1N/A free(g_vol->attrdef);
1N/A free(g_vol->upcase);
1N/A free(g_vol);
1N/A g_vol = NULL;
1N/A }
1N/A
1N/A /* Free any memory we've used */
1N/A free(g_bad_blocks); g_bad_blocks = NULL;
1N/A free(g_buf); g_buf = NULL;
1N/A free(g_index_block); g_index_block = NULL;
1N/A free(g_lcn_bitmap); g_lcn_bitmap = NULL;
1N/A free(g_mft_bitmap); g_mft_bitmap = NULL;
1N/A free(g_rl_bad); g_rl_bad = NULL;
1N/A free(g_rl_boot); g_rl_boot = NULL;
1N/A free(g_rl_logfile); g_rl_logfile = NULL;
1N/A free(g_rl_mft); g_rl_mft = NULL;
1N/A free(g_rl_mft_bmp); g_rl_mft_bmp = NULL;
1N/A free(g_rl_mftmirr); g_rl_mftmirr = NULL;
1N/A}
1N/A
1N/A
1N/A/**
1N/A * mkntfs_open_partition -
1N/A */
1N/Astatic BOOL mkntfs_open_partition(ntfs_volume *vol)
1N/A{
1N/A BOOL result = FALSE;
1N/A int i;
1N/A struct stat sbuf;
1N/A unsigned long mnt_flags;
1N/A
1N/A /*
1N/A * Allocate and initialize an ntfs device structure and attach it to
1N/A * the volume.
1N/A */
1N/A vol->u.dev = ntfs_device_alloc(opts.dev_name, 0, &ntfs_device_default_io_ops, NULL);
1N/A if (!vol->u.dev) {
1N/A ntfs_log_perror("Could not create device");
1N/A goto done;
1N/A }
1N/A
1N/A /* Open the device for reading or reading and writing. */
1N/A if (opts.no_action) {
1N/A ntfs_log_quiet("Running in READ-ONLY mode!\n");
1N/A i = O_RDONLY;
1N/A } else {
1N/A i = O_RDWR;
1N/A }
1N/A if (vol->u.dev->d_ops->open(vol->u.dev, i)) {
1N/A if (errno == ENOENT)
1N/A ntfs_log_error("The device doesn't exist; did you specify it correctly?\n");
1N/A else
1N/A ntfs_log_perror("Could not open %s", vol->u.dev->d_name);
1N/A goto done;
1N/A }
1N/A /* Verify we are dealing with a block device. */
1N/A if (vol->u.dev->d_ops->stat(vol->u.dev, &sbuf)) {
1N/A ntfs_log_perror("Error getting information about %s", vol->u.dev->d_name);
1N/A goto done;
1N/A }
1N/A
1N/A if (!S_ISBLK(sbuf.st_mode)) {
1N/A ntfs_log_error("%s is not a block device.\n", vol->u.dev->d_name);
1N/A if (!opts.force) {
1N/A ntfs_log_error("Refusing to make a filesystem here!\n");
1N/A goto done;
1N/A }
1N/A if (!opts.num_sectors) {
1N/A if (!sbuf.st_size && !sbuf.st_blocks) {
1N/A ntfs_log_error("You must specify the number of sectors.\n");
1N/A goto done;
1N/A }
1N/A if (opts.sector_size) {
1N/A if (sbuf.st_size)
1N/A opts.num_sectors = sbuf.st_size / opts.sector_size;
1N/A else
1N/A opts.num_sectors = ((s64)sbuf.st_blocks << 9) / opts.sector_size;
1N/A } else {
1N/A if (sbuf.st_size)
1N/A opts.num_sectors = sbuf.st_size / 512;
1N/A else
1N/A opts.num_sectors = sbuf.st_blocks;
1N/A opts.sector_size = 512;
1N/A }
1N/A }
1N/A ntfs_log_warning("mkntfs forced anyway.\n");
1N/A#ifdef HAVE_LINUX_MAJOR_H
1N/A } else if ((IDE_DISK_MAJOR(MAJOR(sbuf.st_rdev)) &&
1N/A MINOR(sbuf.st_rdev) % 64 == 0) ||
1N/A (SCSI_DISK_MAJOR(MAJOR(sbuf.st_rdev)) &&
1N/A MINOR(sbuf.st_rdev) % 16 == 0)) {
1N/A ntfs_log_error("%s is entire device, not just one partition.\n", vol->u.dev->d_name);
1N/A if (!opts.force) {
1N/A ntfs_log_error("Refusing to make a filesystem here!\n");
1N/A goto done;
1N/A }
1N/A ntfs_log_warning("mkntfs forced anyway.\n");
1N/A#endif
1N/A }
1N/A /* Make sure the file system is not mounted. */
1N/A if (ntfs_check_if_mounted(vol->u.dev->d_name, &mnt_flags)) {
1N/A ntfs_log_perror("Failed to determine whether %s is mounted", vol->u.dev->d_name);
1N/A } else if (mnt_flags & NTFS_MF_MOUNTED) {
1N/A ntfs_log_error("%s is mounted.\n", vol->u.dev->d_name);
1N/A if (!opts.force) {
1N/A ntfs_log_error("Refusing to make a filesystem here!\n");
1N/A goto done;
1N/A }
1N/A ntfs_log_warning("mkntfs forced anyway. Hope /etc/mtab is incorrect.\n");
1N/A }
1N/A result = TRUE;
1N/Adone:
1N/A return result;
1N/A}
1N/A
1N/A/**
1N/A * mkntfs_get_page_size - detect the system's memory page size.
1N/A */
1N/Astatic long mkntfs_get_page_size(void)
1N/A{
1N/A long page_size;
1N/A#ifdef _SC_PAGESIZE
1N/A page_size = sysconf(_SC_PAGESIZE);
1N/A if (page_size < 0)
1N/A#endif
1N/A#ifdef _SC_PAGE_SIZE
1N/A page_size = sysconf(_SC_PAGE_SIZE);
1N/A if (page_size < 0)
1N/A#endif
1N/A {
1N/A ntfs_log_warning("Failed to determine system page size. "
1N/A "Assuming safe default of 4096 bytes.\n");
1N/A return 4096;
1N/A }
1N/A ntfs_log_debug("System page size is %li bytes.\n", page_size);
1N/A return page_size;
1N/A}
1N/A
1N/A/**
1N/A * mkntfs_override_vol_params -
1N/A */
1N/Astatic BOOL mkntfs_override_vol_params(ntfs_volume *vol)
1N/A{
1N/A s64 volume_size;
1N/A long page_size;
1N/A int i;
1N/A BOOL winboot = TRUE;
1N/A
1N/A /* If user didn't specify the sector size, determine it now. */
1N/A if (opts.sector_size < 0) {
1N/A opts.sector_size = ntfs_device_sector_size_get(vol->u.dev);
1N/A if (opts.sector_size < 0) {
1N/A ntfs_log_warning("The sector size was not specified "
1N/A "for %s and it could not be obtained "
1N/A "automatically. It has been set to 512 "
1N/A "bytes.\n", vol->u.dev->d_name);
1N/A opts.sector_size = 512;
1N/A }
1N/A }
1N/A /* Validate sector size. */
1N/A if ((opts.sector_size - 1) & opts.sector_size) {
1N/A ntfs_log_error("The sector size is invalid. It must be a "
1N/A "power of two, e.g. 512, 1024.\n");
1N/A return FALSE;
1N/A }
1N/A if (opts.sector_size < 256 || opts.sector_size > 4096) {
1N/A ntfs_log_error("The sector size is invalid. The minimum size "
1N/A "is 256 bytes and the maximum is 4096 bytes.\n");
1N/A return FALSE;
1N/A }
1N/A ntfs_log_debug("sector size = %ld bytes\n", opts.sector_size);
1N/A /* Now set the device block size to the sector size. */
1N/A if (ntfs_device_block_size_set(vol->u.dev, opts.sector_size))
1N/A ntfs_log_debug("Failed to set the device block size to the "
1N/A "sector size. This may cause problems when "
1N/A "creating the backup boot sector and also may "
1N/A "affect performance but should be harmless "
1N/A "otherwise. Error: %s\n", strerror(errno));
1N/A /* If user didn't specify the number of sectors, determine it now. */
1N/A if (opts.num_sectors < 0) {
1N/A opts.num_sectors = ntfs_device_size_get(vol->u.dev,
1N/A opts.sector_size);
1N/A if (opts.num_sectors <= 0) {
1N/A ntfs_log_error("Couldn't determine the size of %s. "
1N/A "Please specify the number of sectors "
1N/A "manually.\n", vol->u.dev->d_name);
1N/A return FALSE;
1N/A }
1N/A }
1N/A ntfs_log_debug("number of sectors = %lld (0x%llx)\n", opts.num_sectors,
1N/A opts.num_sectors);
1N/A /*
1N/A * Reserve the last sector for the backup boot sector unless the
1N/A * sector size is less than 512 bytes in which case reserve 512 bytes
1N/A * worth of sectors.
1N/A */
1N/A i = 1;
1N/A if (opts.sector_size < 512)
1N/A i = 512 / opts.sector_size;
1N/A opts.num_sectors -= i;
1N/A /* If user didn't specify the partition start sector, determine it. */
1N/A if (opts.part_start_sect < 0) {
1N/A opts.part_start_sect = ntfs_device_partition_start_sector_get(
1N/A vol->u.dev);
1N/A if (opts.part_start_sect < 0) {
1N/A ntfs_log_warning("The partition start sector was not "
1N/A "specified for %s and it could not be obtained "
1N/A "automatically. It has been set to 0.\n",
1N/A vol->u.dev->d_name);
1N/A opts.part_start_sect = 0;
1N/A winboot = FALSE;
1N/A } else if (opts.part_start_sect >> 32) {
1N/A ntfs_log_warning("The partition start sector specified "
1N/A "for %s and the automatically determined value "
1N/A "is too large. It has been set to 0.\n",
1N/A vol->u.dev->d_name);
1N/A opts.part_start_sect = 0;
1N/A winboot = FALSE;
1N/A }
1N/A } else if (opts.part_start_sect >> 32) {
1N/A ntfs_log_error("Invalid partition start sector. Maximum is "
1N/A "4294967295 (2^32-1).\n");
1N/A return FALSE;
1N/A }
1N/A /* If user didn't specify the sectors per track, determine it now. */
1N/A if (opts.sectors_per_track < 0) {
1N/A opts.sectors_per_track = ntfs_device_sectors_per_track_get(
1N/A vol->u.dev);
1N/A if (opts.sectors_per_track < 0) {
1N/A ntfs_log_warning("The number of sectors per track was "
1N/A "not specified for %s and it could not be "
1N/A "obtained automatically. It has been set to "
1N/A "0.\n", vol->u.dev->d_name);
1N/A opts.sectors_per_track = 0;
1N/A winboot = FALSE;
1N/A } else if (opts.sectors_per_track > 65535) {
1N/A ntfs_log_warning("The number of sectors per track was "
1N/A "not specified for %s and the automatically "
1N/A "determined value is too large. It has been "
1N/A "set to 0.\n", vol->u.dev->d_name);
1N/A opts.sectors_per_track = 0;
1N/A winboot = FALSE;
1N/A }
1N/A } else if (opts.sectors_per_track > 65535) {
1N/A ntfs_log_error("Invalid number of sectors per track. Maximum "
1N/A "is 65535.\n");
1N/A return FALSE;
1N/A }
1N/A /* If user didn't specify the number of heads, determine it now. */
1N/A if (opts.heads < 0) {
1N/A opts.heads = ntfs_device_heads_get(vol->u.dev);
1N/A if (opts.heads < 0) {
1N/A ntfs_log_warning("The number of heads was not "
1N/A "specified for %s and it could not be obtained "
1N/A "automatically. It has been set to 0.\n",
1N/A vol->u.dev->d_name);
1N/A opts.heads = 0;
1N/A winboot = FALSE;
1N/A } else if (opts.heads > 65535) {
1N/A ntfs_log_warning("The number of heads was not "
1N/A "specified for %s and the automatically "
1N/A "determined value is too large. It has been "
1N/A "set to 0.\n", vol->u.dev->d_name);
1N/A opts.heads = 0;
1N/A winboot = FALSE;
1N/A }
1N/A } else if (opts.heads > 65535) {
1N/A ntfs_log_error("Invalid number of heads. Maximum is 65535.\n");
1N/A return FALSE;
1N/A }
1N/A volume_size = opts.num_sectors * opts.sector_size;
1N/A /* Validate volume size. */
1N/A if (volume_size < (1 << 20)) { /* 1MiB */
1N/A ntfs_log_error("Device is too small (%llikiB). Minimum NTFS "
1N/A "volume size is 1MiB.\n", volume_size / 1024);
1N/A return FALSE;
1N/A }
1N/A ntfs_log_debug("volume size = %llikiB\n", volume_size / 1024);
1N/A /* If user didn't specify the cluster size, determine it now. */
1N/A if (!vol->cluster_size) {
1N/A /*
1N/A * Windows Vista always uses 4096 bytes as the default cluster
1N/A * size regardless of the volume size so we do it, too.
1N/A */
1N/A vol->cluster_size = 4096;
1N/A /* For small volumes on devices with large sector sizes. */
1N/A if (vol->cluster_size < (u32)opts.sector_size)
1N/A vol->cluster_size = opts.sector_size;
1N/A /*
1N/A * For huge volumes, grow the cluster size until the number of
1N/A * clusters fits into 32 bits or the cluster size exceeds the
1N/A * maximum limit of 64kiB.
1N/A */
1N/A while (volume_size >> (ffs(vol->cluster_size) - 1 + 32)) {
1N/A vol->cluster_size <<= 1;
1N/A if (vol->cluster_size > 65535) {
1N/A ntfs_log_error("Device is too large to hold an "
1N/A "NTFS volume (maximum size is "
1N/A "256TiB).\n");
1N/A return FALSE;
1N/A }
1N/A }
1N/A ntfs_log_quiet("Cluster size has been automatically set to %u "
1N/A "bytes.\n", (unsigned)vol->cluster_size);
1N/A }
1N/A /* Validate cluster size. */
1N/A if (vol->cluster_size & (vol->cluster_size - 1)) {
1N/A ntfs_log_error("The cluster size is invalid. It must be a "
1N/A "power of two, e.g. 1024, 4096.\n");
1N/A return FALSE;
1N/A }
1N/A if (vol->cluster_size < (u32)opts.sector_size) {
1N/A ntfs_log_error("The cluster size is invalid. It must be equal "
1N/A "to, or larger than, the sector size.\n");
1N/A return FALSE;
1N/A }
1N/A if (vol->cluster_size > 128 * (u32)opts.sector_size) {
1N/A ntfs_log_error("The cluster size is invalid. It cannot be "
1N/A "more that 128 times the size of the sector "
1N/A "size.\n");
1N/A return FALSE;
1N/A }
1N/A if (vol->cluster_size > 65536) {
1N/A ntfs_log_error("The cluster size is invalid. The maximum "
1N/A "cluster size is 65536 bytes (64kiB).\n");
1N/A return FALSE;
1N/A }
1N/A vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
1N/A ntfs_log_debug("cluster size = %u bytes\n",
1N/A (unsigned int)vol->cluster_size);
1N/A if (vol->cluster_size > 4096) {
1N/A if (opts.enable_compression) {
1N/A if (!opts.force) {
1N/A ntfs_log_error("Windows cannot use compression "
1N/A "when the cluster size is "
1N/A "larger than 4096 bytes.\n");
1N/A return FALSE;
1N/A }
1N/A opts.enable_compression = 0;
1N/A }
1N/A ntfs_log_warning("Windows cannot use compression when the "
1N/A "cluster size is larger than 4096 bytes. "
1N/A "Compression has been disabled for this "
1N/A "volume.\n");
1N/A }
1N/A vol->nr_clusters = volume_size / vol->cluster_size;
1N/A /*
1N/A * Check the cluster_size and num_sectors for consistency with
1N/A * sector_size and num_sectors. And check both of these for consistency
1N/A * with volume_size.
1N/A */
1N/A if ((vol->nr_clusters != ((opts.num_sectors * opts.sector_size) /
1N/A vol->cluster_size) ||
1N/A (volume_size / opts.sector_size) != opts.num_sectors ||
1N/A (volume_size / vol->cluster_size) !=
1N/A vol->nr_clusters)) {
1N/A /* XXX is this code reachable? */
1N/A ntfs_log_error("Illegal combination of volume/cluster/sector "
1N/A "size and/or cluster/sector number.\n");
1N/A return FALSE;
1N/A }
1N/A ntfs_log_debug("number of clusters = %llu (0x%llx)\n",
1N/A vol->nr_clusters, vol->nr_clusters);
1N/A /* Number of clusters must fit within 32 bits (Win2k limitation). */
1N/A if (vol->nr_clusters >> 32) {
1N/A if (vol->cluster_size >= 65536) {
1N/A ntfs_log_error("Device is too large to hold an NTFS "
1N/A "volume (maximum size is 256TiB).\n");
1N/A return FALSE;
1N/A }
1N/A ntfs_log_error("Number of clusters exceeds 32 bits. Please "
1N/A "try again with a larger\ncluster size or "
1N/A "leave the cluster size unspecified and the "
1N/A "smallest possible cluster size for the size "
1N/A "of the device will be used.\n");
1N/A return FALSE;
1N/A }
1N/A page_size = mkntfs_get_page_size();
1N/A /*
1N/A * Set the mft record size. By default this is 1024 but it has to be
1N/A * at least as big as a sector and not bigger than a page on the system
1N/A * or the NTFS kernel driver will not be able to mount the volume.
1N/A * TODO: The mft record size should be user specifiable just like the
1N/A * "inode size" can be specified on other Linux/Unix file systems.
1N/A */
1N/A vol->mft_record_size = 1024;
1N/A if (vol->mft_record_size < (u32)opts.sector_size)
1N/A vol->mft_record_size = opts.sector_size;
1N/A if (vol->mft_record_size > (unsigned long)page_size)
1N/A ntfs_log_warning("Mft record size (%u bytes) exceeds system "
1N/A "page size (%li bytes). You will not be able "
1N/A "to mount this volume using the NTFS kernel "
1N/A "driver.\n", (unsigned)vol->mft_record_size,
1N/A page_size);
1N/A vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
1N/A ntfs_log_debug("mft record size = %u bytes\n",
1N/A (unsigned)vol->mft_record_size);
1N/A /*
1N/A * Set the index record size. By default this is 4096 but it has to be
1N/A * at least as big as a sector and not bigger than a page on the system
1N/A * or the NTFS kernel driver will not be able to mount the volume.
1N/A * FIXME: Should we make the index record size to be user specifiable?
1N/A */
1N/A vol->indx_record_size = 4096;
1N/A if (vol->indx_record_size < (u32)opts.sector_size)
1N/A vol->indx_record_size = opts.sector_size;
1N/A if (vol->indx_record_size > (unsigned long)page_size)
1N/A ntfs_log_warning("Index record size (%u bytes) exceeds system "
1N/A "page size (%li bytes). You will not be able "
1N/A "to mount this volume using the NTFS kernel "
1N/A "driver.\n", (unsigned)vol->indx_record_size,
1N/A page_size);
1N/A vol->indx_record_size_bits = ffs(vol->indx_record_size) - 1;
1N/A ntfs_log_debug("index record size = %u bytes\n",
1N/A (unsigned)vol->indx_record_size);
1N/A if (!winboot) {
1N/A ntfs_log_warning("To boot from a device, Windows needs the "
1N/A "'partition start sector', the 'sectors per "
1N/A "track' and the 'number of heads' to be "
1N/A "set.\n");
1N/A ntfs_log_warning("Windows will not be able to boot from this "
1N/A "device.\n");
1N/A }
1N/A return TRUE;
1N/A}
1N/A
1N/A/**
1N/A * mkntfs_initialize_bitmaps -
1N/A */
1N/Astatic BOOL mkntfs_initialize_bitmaps(void)
1N/A{
1N/A u64 i;
1N/A int mft_bitmap_size;
1N/A
1N/A /* Determine lcn bitmap byte size and allocate it. */
1N/A g_lcn_bitmap_byte_size = (g_vol->nr_clusters + 7) >> 3;
1N/A /* Needs to be multiple of 8 bytes. */
1N/A g_lcn_bitmap_byte_size = (g_lcn_bitmap_byte_size + 7) & ~7;
1N/A i = (g_lcn_bitmap_byte_size + g_vol->cluster_size - 1) &
1N/A ~(g_vol->cluster_size - 1);
1N/A ntfs_log_debug("g_lcn_bitmap_byte_size = %i, allocated = %llu\n",
1N/A g_lcn_bitmap_byte_size, i);
1N/A g_lcn_bitmap = ntfs_calloc(g_lcn_bitmap_byte_size);
1N/A if (!g_lcn_bitmap)
1N/A return FALSE;
1N/A /*
1N/A * $Bitmap can overlap the end of the volume. Any bits in this region
1N/A * must be set. This region also encompasses the backup boot sector.
1N/A */
1N/A for (i = g_vol->nr_clusters; i < (u64)g_lcn_bitmap_byte_size << 3; i++)
1N/A ntfs_bit_set(g_lcn_bitmap, i, 1);
1N/A /*
1N/A * Mft size is 27 (NTFS 3.0+) mft records or one cluster, whichever is
1N/A * bigger.
1N/A */
1N/A g_mft_size = 27;
1N/A g_mft_size *= g_vol->mft_record_size;
1N/A if (g_mft_size < (s32)g_vol->cluster_size)
1N/A g_mft_size = g_vol->cluster_size;
1N/A ntfs_log_debug("MFT size = %i (0x%x) bytes\n", g_mft_size, g_mft_size);
1N/A /* Determine mft bitmap size and allocate it. */
1N/A mft_bitmap_size = g_mft_size / g_vol->mft_record_size;
1N/A /* Convert to bytes, at least one. */
1N/A g_mft_bitmap_byte_size = (mft_bitmap_size + 7) >> 3;
1N/A /* Mft bitmap is allocated in multiples of 8 bytes. */
1N/A g_mft_bitmap_byte_size = (g_mft_bitmap_byte_size + 7) & ~7;
1N/A ntfs_log_debug("mft_bitmap_size = %i, g_mft_bitmap_byte_size = %i\n",
1N/A mft_bitmap_size, g_mft_bitmap_byte_size);
1N/A g_mft_bitmap = ntfs_calloc(g_mft_bitmap_byte_size);
1N/A if (!g_mft_bitmap)
1N/A return FALSE;
1N/A /* Create runlist for mft bitmap. */
1N/A g_rl_mft_bmp = ntfs_malloc(2 * sizeof(runlist));
1N/A if (!g_rl_mft_bmp)
1N/A return FALSE;
1N/A
1N/A g_rl_mft_bmp[0].vcn = 0LL;
1N/A /* Mft bitmap is right after $Boot's data. */
1N/A i = (8192 + g_vol->cluster_size - 1) / g_vol->cluster_size;
1N/A g_rl_mft_bmp[0].lcn = i;
1N/A /*
1N/A * Size is always one cluster, even though valid data size and
1N/A * initialized data size are only 8 bytes.
1N/A */
1N/A g_rl_mft_bmp[1].vcn = 1LL;
1N/A g_rl_mft_bmp[0].length = 1LL;
1N/A g_rl_mft_bmp[1].lcn = -1LL;
1N/A g_rl_mft_bmp[1].length = 0LL;
1N/A /* Allocate cluster for mft bitmap. */
1N/A ntfs_bit_set(g_lcn_bitmap, i, 1);
1N/A return TRUE;
1N/A}
1N/A
1N/A/**
1N/A * mkntfs_initialize_rl_mft -
1N/A */
1N/Astatic BOOL mkntfs_initialize_rl_mft(void)
1N/A{
1N/A int i, j;
1N/A
1N/A /* If user didn't specify the mft lcn, determine it now. */
1N/A if (!g_mft_lcn) {
1N/A /*
1N/A * We start at the higher value out of 16kiB and just after the
1N/A * mft bitmap.
1N/A */
1N/A g_mft_lcn = g_rl_mft_bmp[0].lcn + g_rl_mft_bmp[0].length;
1N/A if (g_mft_lcn * g_vol->cluster_size < 16 * 1024)
1N/A g_mft_lcn = (16 * 1024 + g_vol->cluster_size - 1) /
1N/A g_vol->cluster_size;
1N/A }
1N/A ntfs_log_debug("$MFT logical cluster number = 0x%llx\n", g_mft_lcn);
1N/A /* Determine MFT zone size. */
1N/A g_mft_zone_end = g_vol->nr_clusters;
1N/A switch (opts.mft_zone_multiplier) { /* % of volume size in clusters */
1N/A case 4:
1N/A g_mft_zone_end = g_mft_zone_end >> 1; /* 50% */
1N/A break;
1N/A case 3:
1N/A g_mft_zone_end = g_mft_zone_end * 3 >> 3;/* 37.5% */
1N/A break;
1N/A case 2:
1N/A g_mft_zone_end = g_mft_zone_end >> 2; /* 25% */
1N/A break;
1N/A case 1:
1N/A default:
1N/A g_mft_zone_end = g_mft_zone_end >> 3; /* 12.5% */
1N/A break;
1N/A }
1N/A ntfs_log_debug("MFT zone size = %lldkiB\n", g_mft_zone_end <<
1N/A g_vol->cluster_size_bits >> 10 /* >> 10 == / 1024 */);
1N/A /*
1N/A * The mft zone begins with the mft data attribute, not at the beginning
1N/A * of the device.
1N/A */
1N/A g_mft_zone_end += g_mft_lcn;
1N/A /* Create runlist for mft. */
1N/A g_rl_mft = ntfs_malloc(2 * sizeof(runlist));
1N/A if (!g_rl_mft)
1N/A return FALSE;
1N/A
1N/A g_rl_mft[0].vcn = 0LL;
1N/A g_rl_mft[0].lcn = g_mft_lcn;
1N/A /* rounded up division by cluster size */
1N/A j = (g_mft_size + g_vol->cluster_size - 1) / g_vol->cluster_size;
1N/A g_rl_mft[1].vcn = j;
1N/A g_rl_mft[0].length = j;
1N/A g_rl_mft[1].lcn = -1LL;
1N/A g_rl_mft[1].length = 0LL;
1N/A /* Allocate clusters for mft. */
1N/A for (i = 0; i < j; i++)
1N/A ntfs_bit_set(g_lcn_bitmap, g_mft_lcn + i, 1);
1N/A /* Determine mftmirr_lcn (middle of volume). */
1N/A g_mftmirr_lcn = (opts.num_sectors * opts.sector_size >> 1)
1N/A / g_vol->cluster_size;
1N/A ntfs_log_debug("$MFTMirr logical cluster number = 0x%llx\n",
1N/A g_mftmirr_lcn);
1N/A /* Create runlist for mft mirror. */
1N/A g_rl_mftmirr = ntfs_malloc(2 * sizeof(runlist));
1N/A if (!g_rl_mftmirr)
1N/A return FALSE;
1N/A
1N/A g_rl_mftmirr[0].vcn = 0LL;
1N/A g_rl_mftmirr[0].lcn = g_mftmirr_lcn;
1N/A /*
1N/A * The mft mirror is either 4kb (the first four records) or one cluster
1N/A * in size, which ever is bigger. In either case, it contains a
1N/A * byte-for-byte identical copy of the beginning of the mft (i.e. either
1N/A * the first four records (4kb) or the first cluster worth of records,
1N/A * whichever is bigger).
1N/A */
1N/A j = (4 * g_vol->mft_record_size + g_vol->cluster_size - 1) / g_vol->cluster_size;
1N/A g_rl_mftmirr[1].vcn = j;
1N/A g_rl_mftmirr[0].length = j;
1N/A g_rl_mftmirr[1].lcn = -1LL;
1N/A g_rl_mftmirr[1].length = 0LL;
1N/A /* Allocate clusters for mft mirror. */
1N/A for (i = 0; i < j; i++)
1N/A ntfs_bit_set(g_lcn_bitmap, g_mftmirr_lcn + i, 1);
1N/A g_logfile_lcn = g_mftmirr_lcn + j;
1N/A ntfs_log_debug("$LogFile logical cluster number = 0x%llx\n",
1N/A g_logfile_lcn);
1N/A return TRUE;
1N/A}
1N/A
1N/A/**
1N/A * mkntfs_initialize_rl_logfile -
1N/A */
1N/Astatic BOOL mkntfs_initialize_rl_logfile(void)
1N/A{
1N/A int i, j;
1N/A u64 volume_size;
1N/A
1N/A /* Create runlist for log file. */
1N/A g_rl_logfile = ntfs_malloc(2 * sizeof(runlist));
1N/A if (!g_rl_logfile)
1N/A return FALSE;
1N/A
1N/A
1N/A volume_size = g_vol->nr_clusters << g_vol->cluster_size_bits;
1N/A
1N/A g_rl_logfile[0].vcn = 0LL;
1N/A g_rl_logfile[0].lcn = g_logfile_lcn;
1N/A /*
1N/A * Determine logfile_size from volume_size (rounded up to a cluster),
1N/A * making sure it does not overflow the end of the volume.
1N/A */
1N/A if (volume_size < 2048LL * 1024) /* < 2MiB */
1N/A g_logfile_size = 256LL * 1024; /* -> 256kiB */
1N/A else if (volume_size < 4000000LL) /* < 4MB */
1N/A g_logfile_size = 512LL * 1024; /* -> 512kiB */
1N/A else if (volume_size <= 200LL * 1024 * 1024) /* < 200MiB */
1N/A g_logfile_size = 2048LL * 1024; /* -> 2MiB */
1N/A else {
1N/A /*
1N/A * FIXME: The $LogFile size is 64 MiB upwards from 12GiB but
1N/A * the "200" divider below apparently approximates "100" or
1N/A * some other value as the volume size decreases. For example:
1N/A * Volume size LogFile size Ratio
1N/A * 8799808 46048 191.100
1N/A * 8603248 45072 190.877
1N/A * 7341704 38768 189.375
1N/A * 6144828 32784 187.433
1N/A * 4192932 23024 182.111
1N/A */
1N/A if (volume_size >= 12LL << 30) /* > 12GiB */
1N/A g_logfile_size = 64 << 20; /* -> 64MiB */
1N/A else
1N/A g_logfile_size = (volume_size / 200) &
1N/A ~(g_vol->cluster_size - 1);
1N/A }
1N/A j = g_logfile_size / g_vol->cluster_size;
1N/A while (g_rl_logfile[0].lcn + j >= g_vol->nr_clusters) {
1N/A /*
1N/A * $Logfile would overflow volume. Need to make it smaller than
1N/A * the standard size. It's ok as we are creating a non-standard
1N/A * volume anyway if it is that small.
1N/A */
1N/A g_logfile_size >>= 1;
1N/A j = g_logfile_size / g_vol->cluster_size;
1N/A }
1N/A g_logfile_size = (g_logfile_size + g_vol->cluster_size - 1) &
1N/A ~(g_vol->cluster_size - 1);
1N/A ntfs_log_debug("$LogFile (journal) size = %ikiB\n",
1N/A g_logfile_size / 1024);
1N/A /*
1N/A * FIXME: The 256kiB limit is arbitrary. Should find out what the real
1N/A * minimum requirement for Windows is so it doesn't blue screen.
1N/A */
1N/A if (g_logfile_size < 256 << 10) {
1N/A ntfs_log_error("$LogFile would be created with invalid size. "
1N/A "This is not allowed as it would cause Windows "
1N/A "to blue screen and during boot.\n");
1N/A return FALSE;
1N/A }
1N/A g_rl_logfile[1].vcn = j;
1N/A g_rl_logfile[0].length = j;
1N/A g_rl_logfile[1].lcn = -1LL;
1N/A g_rl_logfile[1].length = 0LL;
1N/A /* Allocate clusters for log file. */
1N/A for (i = 0; i < j; i++)
1N/A ntfs_bit_set(g_lcn_bitmap, g_logfile_lcn + i, 1);
1N/A return TRUE;
1N/A}
1N/A
1N/A/**
1N/A * mkntfs_initialize_rl_boot -
1N/A */
1N/Astatic BOOL mkntfs_initialize_rl_boot(void)
1N/A{
1N/A int i, j;
1N/A /* Create runlist for $Boot. */
1N/A g_rl_boot = ntfs_malloc(2 * sizeof(runlist));
1N/A if (!g_rl_boot)
1N/A return FALSE;
1N/A
1N/A g_rl_boot[0].vcn = 0LL;
1N/A g_rl_boot[0].lcn = 0LL;
1N/A /*
1N/A * $Boot is always 8192 (0x2000) bytes or 1 cluster, whichever is
1N/A * bigger.
1N/A */
1N/A j = (8192 + g_vol->cluster_size - 1) / g_vol->cluster_size;
1N/A g_rl_boot[1].vcn = j;
1N/A g_rl_boot[0].length = j;
1N/A g_rl_boot[1].lcn = -1LL;
1N/A g_rl_boot[1].length = 0LL;
1N/A /* Allocate clusters for $Boot. */
1N/A for (i = 0; i < j; i++)
1N/A ntfs_bit_set(g_lcn_bitmap, 0LL + i, 1);
1N/A return TRUE;
1N/A}
1N/A
1N/A/**
1N/A * mkntfs_initialize_rl_bad -
1N/A */
1N/Astatic BOOL mkntfs_initialize_rl_bad(void)
1N/A{
1N/A /* Create runlist for $BadClus, $DATA named stream $Bad. */
1N/A g_rl_bad = ntfs_malloc(2 * sizeof(runlist));
1N/A if (!g_rl_bad)
1N/A return FALSE;
1N/A
1N/A g_rl_bad[0].vcn = 0LL;
1N/A g_rl_bad[0].lcn = -1LL;
1N/A /*
1N/A * $BadClus named stream $Bad contains the whole volume as a single
1N/A * sparse runlist entry.
1N/A */
1N/A g_rl_bad[1].vcn = g_vol->nr_clusters;
1N/A g_rl_bad[0].length = g_vol->nr_clusters;
1N/A g_rl_bad[1].lcn = -1LL;
1N/A g_rl_bad[1].length = 0LL;
1N/A
1N/A /* TODO: Mark bad blocks as such. */
1N/A return TRUE;
1N/A}
1N/A
1N/A/**
1N/A * mkntfs_fill_device_with_zeroes -
1N/A */
1N/Astatic BOOL mkntfs_fill_device_with_zeroes(void)
1N/A{
1N/A /*
1N/A * If not quick format, fill the device with 0s.
1N/A * FIXME: Except bad blocks! (AIA)
1N/A */
1N/A int i;
1N/A ssize_t bw;
1N/A unsigned long long position;
1N/A float progress_inc = (float)g_vol->nr_clusters / 100;
1N/A u64 volume_size;
1N/A
1N/A volume_size = g_vol->nr_clusters << g_vol->cluster_size_bits;
1N/A
1N/A ntfs_log_progress("Initializing device with zeroes: 0%%");
1N/A for (position = 0; position < (unsigned long long)g_vol->nr_clusters;
1N/A position++) {
1N/A if (!(position % (int)(progress_inc+1))) {
1N/A ntfs_log_progress("\b\b\b\b%3.0f%%", position /
1N/A progress_inc);
1N/A }
1N/A bw = mkntfs_write(g_vol->u.dev, g_buf, g_vol->cluster_size);
1N/A if (bw != (ssize_t)g_vol->cluster_size) {
1N/A if (bw != -1 || errno != EIO) {
1N/A ntfs_log_error("This should not happen.\n");
1N/A return FALSE;
1N/A }
1N/A if (!position) {
1N/A ntfs_log_error("Error: Cluster zero is bad. "
1N/A "Cannot create NTFS file "
1N/A "system.\n");
1N/A return FALSE;
1N/A }
1N/A /* Add the baddie to our bad blocks list. */
1N/A if (!append_to_bad_blocks(position))
1N/A return FALSE;
1N/A ntfs_log_quiet("\nFound bad cluster (%lld). Adding to "
1N/A "list of bad blocks.\nInitializing "
1N/A "device with zeroes: %3.0f%%", position,
1N/A position / progress_inc);
1N/A /* Seek to next cluster. */
1N/A g_vol->u.dev->d_ops->seek(g_vol->u.dev,
1N/A ((off_t)position + 1) *
1N/A g_vol->cluster_size, SEEK_SET);
1N/A }
1N/A }
1N/A ntfs_log_progress("\b\b\b\b100%%");
1N/A position = (volume_size & (g_vol->cluster_size - 1)) /
1N/A opts.sector_size;
1N/A for (i = 0; (unsigned long)i < position; i++) {
1N/A bw = mkntfs_write(g_vol->u.dev, g_buf, opts.sector_size);
1N/A if (bw != opts.sector_size) {
1N/A if (bw != -1 || errno != EIO) {
1N/A ntfs_log_error("This should not happen.\n");
1N/A return FALSE;
1N/A } else if (i + 1ull == position) {
1N/A ntfs_log_error("Error: Bad cluster found in "
1N/A "location reserved for system "
1N/A "file $Boot.\n");
1N/A return FALSE;
1N/A }
1N/A /* Seek to next sector. */
1N/A g_vol->u.dev->d_ops->seek(g_vol->u.dev,
1N/A opts.sector_size, SEEK_CUR);
1N/A }
1N/A }
1N/A ntfs_log_progress(" - Done.\n");
1N/A return TRUE;
1N/A}
1N/A
1N/A/**
1N/A * mkntfs_sync_index_record
1N/A *
1N/A * (ERSO) made a function out of this, but the reason for doing that
1N/A * disappeared during coding....
1N/A */
1N/Astatic BOOL mkntfs_sync_index_record(INDEX_ALLOCATION* idx, MFT_RECORD* m,
1N/A ntfschar* name, u32 name_len)
1N/A{
1N/A int i, err;
1N/A ntfs_attr_search_ctx *ctx;
1N/A ATTR_RECORD *a;
1N/A long long lw;
1N/A runlist *rl_index = NULL;
1N/A
1N/A i = 5 * sizeof(ntfschar);
1N/A ctx = ntfs_attr_get_search_ctx(NULL, m);
1N/A if (!ctx) {
1N/A ntfs_log_perror("Failed to allocate attribute search context");
1N/A return FALSE;
1N/A }
1N/A /* FIXME: This should be IGNORE_CASE! */
1N/A if (mkntfs_attr_lookup(AT_INDEX_ALLOCATION, name, name_len, 0, 0,
1N/A NULL, 0, ctx)) {
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A ntfs_log_error("BUG: $INDEX_ALLOCATION attribute not found.\n");
1N/A return FALSE;
1N/A }
1N/A a = ctx->attr;
1N/A rl_index = ntfs_mapping_pairs_decompress(g_vol, a, NULL);
1N/A if (!rl_index) {
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A ntfs_log_error("Failed to decompress runlist of $INDEX_ALLOCATION "
1N/A "attribute.\n");
1N/A return FALSE;
1N/A }
1N/A if (sle64_to_cpu(a->u.nonres.initialized_size) < i) {
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A free(rl_index);
1N/A ntfs_log_error("BUG: $INDEX_ALLOCATION attribute too short.\n");
1N/A return FALSE;
1N/A }
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A i = sizeof(INDEX_BLOCK) - sizeof(INDEX_HEADER) +
1N/A le32_to_cpu(idx->index.allocated_size);
1N/A err = ntfs_mst_pre_write_fixup((NTFS_RECORD*)idx, i);
1N/A if (err) {
1N/A free(rl_index);
1N/A ntfs_log_error("ntfs_mst_pre_write_fixup() failed while "
1N/A "syncing index block.\n");
1N/A return FALSE;
1N/A }
1N/A lw = ntfs_rlwrite(g_vol->u.dev, rl_index, (u8*)idx, i, NULL);
1N/A free(rl_index);
1N/A if (lw != i) {
1N/A ntfs_log_error("Error writing $INDEX_ALLOCATION.\n");
1N/A return FALSE;
1N/A }
1N/A /* No more changes to @idx below here so no need for fixup: */
1N/A /* ntfs_mst_post_write_fixup((NTFS_RECORD*)idx); */
1N/A return TRUE;
1N/A}
1N/A
1N/A/**
1N/A * create_file_volume -
1N/A */
1N/Astatic BOOL create_file_volume(MFT_RECORD *m, leMFT_REF root_ref,
1N/A VOLUME_FLAGS fl, const GUID *volume_guid
1N/A#ifndef ENABLE_UUID
1N/A __attribute__((unused))
1N/A#endif
1N/A )
1N/A{
1N/A int i, err;
1N/A u8 *sd;
1N/A
1N/A ntfs_log_verbose("Creating $Volume (mft record 3)\n");
1N/A m = (MFT_RECORD*)(g_buf + 3 * g_vol->mft_record_size);
1N/A err = create_hardlink(g_index_block, root_ref, m,
1N/A MK_LE_MREF(FILE_Volume, FILE_Volume), 0LL, 0LL,
1N/A FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM, 0, 0,
1N/A "$Volume", FILE_NAME_WIN32_AND_DOS);
1N/A if (!err) {
1N/A init_system_file_sd(FILE_Volume, &sd, &i);
1N/A err = add_attr_sd(m, sd, i);
1N/A }
1N/A if (!err)
1N/A err = add_attr_data(m, NULL, 0, 0, 0, NULL, 0);
1N/A if (!err)
1N/A err = add_attr_vol_name(m, g_vol->vol_name, g_vol->vol_name ?
1N/A strlen(g_vol->vol_name) : 0);
1N/A if (!err) {
1N/A if (fl & VOLUME_IS_DIRTY)
1N/A ntfs_log_quiet("Setting the volume dirty so check "
1N/A "disk runs on next reboot into "
1N/A "Windows.\n");
1N/A err = add_attr_vol_info(m, fl, g_vol->major_ver,
1N/A g_vol->minor_ver);
1N/A }
1N/A#ifdef ENABLE_UUID
1N/A if (!err)
1N/A err = add_attr_object_id(m, volume_guid);
1N/A#endif
1N/A if (err < 0) {
1N/A ntfs_log_error("Couldn't create $Volume: %s\n",
1N/A strerror(-err));
1N/A return FALSE;
1N/A }
1N/A return TRUE;
1N/A}
1N/A
1N/A/**
1N/A * create_backup_boot_sector
1N/A *
1N/A * Return 0 on success or -1 if it couldn't be created.
1N/A */
1N/Astatic int create_backup_boot_sector(u8 *buff)
1N/A{
1N/A const char *s;
1N/A ssize_t bw;
1N/A int size, e;
1N/A
1N/A ntfs_log_verbose("Creating backup boot sector.\n");
1N/A /*
1N/A * Write the first max(512, opts.sector_size) bytes from buf to the
1N/A * last sector, but limit that to 8192 bytes of written data since that
1N/A * is how big $Boot is (and how big our buffer is)..
1N/A */
1N/A size = 512;
1N/A if (size < opts.sector_size)
1N/A size = opts.sector_size;
1N/A if (size < opts.cluster_size)
1N/A size = opts.cluster_size;
1N/A if (g_vol->u.dev->d_ops->seek(g_vol->u.dev, (opts.num_sectors + 1) *
1N/A opts.sector_size - size, SEEK_SET) == (off_t)-1) {
1N/A ntfs_log_perror("Seek failed");
1N/A goto bb_err;
1N/A }
1N/A if (size > 8192)
1N/A size = 8192;
1N/A bw = mkntfs_write(g_vol->u.dev, buff, size);
1N/A if (bw == size)
1N/A return 0;
1N/A e = errno;
1N/A if (bw == -1LL)
1N/A s = strerror(e);
1N/A else
1N/A s = "unknown error";
1N/A /* At least some 2.4 kernels return EIO instead of ENOSPC. */
1N/A if (bw != -1LL || (bw == -1LL && e != ENOSPC && e != EIO)) {
1N/A ntfs_log_critical("Couldn't write backup boot sector: %s\n", s);
1N/A return -1;
1N/A }
1N/Abb_err:
1N/A ntfs_log_error("Couldn't write backup boot sector. This is due to a "
1N/A "limitation in the\nLinux kernel. This is not a major "
1N/A "problem as Windows check disk will create the\n"
1N/A "backup boot sector when it is run on your next boot "
1N/A "into Windows.\n");
1N/A return -1;
1N/A}
1N/A
1N/A/**
1N/A * mkntfs_create_root_structures -
1N/A */
1N/Astatic BOOL mkntfs_create_root_structures(void)
1N/A{
1N/A NTFS_BOOT_SECTOR *bs;
1N/A MFT_RECORD *m;
1N/A leMFT_REF root_ref;
1N/A leMFT_REF extend_ref;
1N/A int i;
1N/A int j;
1N/A int err;
1N/A u8 *sd;
1N/A FILE_ATTR_FLAGS extend_flags;
1N/A VOLUME_FLAGS volume_flags = 0;
1N/A int nr_sysfiles;
1N/A u8 *buf_log = NULL;
1N/A int buf_sds_first_size;
1N/A char *buf_sds;
1N/A
1N/A ntfs_log_quiet("Creating NTFS volume structures.\n");
1N/A nr_sysfiles = 27;
1N/A /*
1N/A * Setup an empty mft record. Note, we can just give 0 as the mft
1N/A * reference as we are creating an NTFS 1.2 volume for which the mft
1N/A * reference is ignored by ntfs_mft_record_layout().
1N/A *
1N/A * Copy the mft record onto all 16 records in the buffer and setup the
1N/A * sequence numbers of each system file to equal the mft record number
1N/A * of that file (only for $MFT is the sequence number 1 rather than 0).
1N/A */
1N/A for (i = 0; i < nr_sysfiles; i++) {
1N/A if (ntfs_mft_record_layout(g_vol, 0, m = (MFT_RECORD *)(g_buf +
1N/A i * g_vol->mft_record_size))) {
1N/A ntfs_log_error("Failed to layout system mft records."
1N/A "\n");
1N/A return FALSE;
1N/A }
1N/A if (i == 0 || i > 23)
1N/A m->sequence_number = cpu_to_le16(1);
1N/A else
1N/A m->sequence_number = cpu_to_le16(i);
1N/A }
1N/A /*
1N/A * If only one cluster contains all system files then
1N/A * fill the rest of it with empty, formatted records.
1N/A */
1N/A if (nr_sysfiles * (s32)g_vol->mft_record_size < g_mft_size) {
1N/A for (i = nr_sysfiles;
1N/A i * (s32)g_vol->mft_record_size < g_mft_size; i++) {
1N/A m = (MFT_RECORD *)(g_buf + i * g_vol->mft_record_size);
1N/A if (ntfs_mft_record_layout(g_vol, 0, m)) {
1N/A ntfs_log_error("Failed to layout mft record."
1N/A "\n");
1N/A return FALSE;
1N/A }
1N/A m->flags = cpu_to_le16(0);
1N/A m->sequence_number = cpu_to_le16(i);
1N/A }
1N/A }
1N/A /*
1N/A * Create the 16 system files, adding the system information attribute
1N/A * to each as well as marking them in use in the mft bitmap.
1N/A */
1N/A for (i = 0; i < nr_sysfiles; i++) {
1N/A le32 file_attrs;
1N/A
1N/A m = (MFT_RECORD*)(g_buf + i * g_vol->mft_record_size);
1N/A if (i < 16 || i > 23) {
1N/A m->mft_record_number = cpu_to_le32(i);
1N/A m->flags |= MFT_RECORD_IN_USE;
1N/A ntfs_bit_set(g_mft_bitmap, 0LL + i, 1);
1N/A }
1N/A file_attrs = FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM;
1N/A if (i == FILE_root) {
1N/A file_attrs |= FILE_ATTR_ARCHIVE;
1N/A if (opts.disable_indexing)
1N/A file_attrs |= FILE_ATTR_NOT_CONTENT_INDEXED;
1N/A if (opts.enable_compression)
1N/A file_attrs |= FILE_ATTR_COMPRESSED;
1N/A }
1N/A /* setting specific security_id flag and */
1N/A /* file permissions for ntfs 3.x */
1N/A if (i == 0 || i == 1 || i == 2 || i == 6 || i == 8 ||
1N/A i == 10) {
1N/A add_attr_std_info(m, file_attrs,
1N/A cpu_to_le32(0x0100));
1N/A } else if (i == 9) {
1N/A file_attrs |= FILE_ATTR_VIEW_INDEX_PRESENT;
1N/A add_attr_std_info(m, file_attrs,
1N/A cpu_to_le32(0x0101));
1N/A } else if (i == 11) {
1N/A add_attr_std_info(m, file_attrs,
1N/A cpu_to_le32(0x0101));
1N/A } else if (i == 24 || i == 25 || i == 26) {
1N/A file_attrs |= FILE_ATTR_ARCHIVE;
1N/A file_attrs |= FILE_ATTR_VIEW_INDEX_PRESENT;
1N/A add_attr_std_info(m, file_attrs,
1N/A cpu_to_le32(0x0101));
1N/A } else {
1N/A add_attr_std_info(m, file_attrs,
1N/A cpu_to_le32(0x00));
1N/A }
1N/A }
1N/A /* The root directory mft reference. */
1N/A root_ref = MK_LE_MREF(FILE_root, FILE_root);
1N/A extend_ref = MK_LE_MREF(11,11);
1N/A ntfs_log_verbose("Creating root directory (mft record 5)\n");
1N/A m = (MFT_RECORD*)(g_buf + 5 * g_vol->mft_record_size);
1N/A m->flags |= MFT_RECORD_IS_DIRECTORY;
1N/A m->link_count = cpu_to_le16(le16_to_cpu(m->link_count) + 1);
1N/A err = add_attr_file_name(m, root_ref, 0LL, 0LL,
1N/A FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM |
1N/A FILE_ATTR_I30_INDEX_PRESENT, 0, 0, ".",
1N/A FILE_NAME_WIN32_AND_DOS);
1N/A if (!err) {
1N/A init_root_sd(&sd, &i);
1N/A err = add_attr_sd(m, sd, i);
1N/A }
1N/A /* FIXME: This should be IGNORE_CASE */
1N/A if (!err)
1N/A err = add_attr_index_root(m, "$I30", 4, 0, AT_FILE_NAME,
1N/A COLLATION_FILE_NAME, g_vol->indx_record_size);
1N/A /* FIXME: This should be IGNORE_CASE */
1N/A if (!err)
1N/A err = upgrade_to_large_index(m, "$I30", 4, 0, &g_index_block);
1N/A if (!err) {
1N/A ntfs_attr_search_ctx *ctx;
1N/A ATTR_RECORD *a;
1N/A ctx = ntfs_attr_get_search_ctx(NULL, m);
1N/A if (!ctx) {
1N/A ntfs_log_perror("Failed to allocate attribute search "
1N/A "context");
1N/A return FALSE;
1N/A }
1N/A /* There is exactly one file name so this is ok. */
1N/A if (mkntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, 0, 0, NULL,
1N/A 0, ctx)) {
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A ntfs_log_error("BUG: $FILE_NAME attribute not found."
1N/A "\n");
1N/A return FALSE;
1N/A }
1N/A a = ctx->attr;
1N/A err = insert_file_link_in_dir_index(g_index_block, root_ref,
1N/A (FILE_NAME_ATTR*)((char*)a +
1N/A le16_to_cpu(a->u.res.value_offset)),
1N/A le32_to_cpu(a->u.res.value_length));
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A }
1N/A if (err) {
1N/A ntfs_log_error("Couldn't create root directory: %s\n",
1N/A strerror(-err));
1N/A return FALSE;
1N/A }
1N/A /* Add all other attributes, on a per-file basis for clarity. */
1N/A ntfs_log_verbose("Creating $MFT (mft record 0)\n");
1N/A m = (MFT_RECORD*)g_buf;
1N/A err = add_attr_data_positioned(m, NULL, 0, 0, 0, g_rl_mft, g_buf,
1N/A g_mft_size);
1N/A if (!err)
1N/A err = create_hardlink(g_index_block, root_ref, m,
1N/A MK_LE_MREF(FILE_MFT, 1), g_mft_size,
1N/A g_mft_size, FILE_ATTR_HIDDEN |
1N/A FILE_ATTR_SYSTEM, 0, 0, "$MFT",
1N/A FILE_NAME_WIN32_AND_DOS);
1N/A /* mft_bitmap is not modified in mkntfs; no need to sync it later. */
1N/A if (!err)
1N/A err = add_attr_bitmap_positioned(m, NULL, 0, 0, g_rl_mft_bmp,
1N/A g_mft_bitmap, g_mft_bitmap_byte_size);
1N/A if (err < 0) {
1N/A ntfs_log_error("Couldn't create $MFT: %s\n", strerror(-err));
1N/A return FALSE;
1N/A }
1N/A ntfs_log_verbose("Creating $MFTMirr (mft record 1)\n");
1N/A m = (MFT_RECORD*)(g_buf + 1 * g_vol->mft_record_size);
1N/A err = add_attr_data_positioned(m, NULL, 0, 0, 0, g_rl_mftmirr, g_buf,
1N/A g_rl_mftmirr[0].length * g_vol->cluster_size);
1N/A if (!err)
1N/A err = create_hardlink(g_index_block, root_ref, m,
1N/A MK_LE_MREF(FILE_MFTMirr, FILE_MFTMirr),
1N/A g_rl_mftmirr[0].length * g_vol->cluster_size,
1N/A g_rl_mftmirr[0].length * g_vol->cluster_size,
1N/A FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM, 0, 0,
1N/A "$MFTMirr", FILE_NAME_WIN32_AND_DOS);
1N/A if (err < 0) {
1N/A ntfs_log_error("Couldn't create $MFTMirr: %s\n",
1N/A strerror(-err));
1N/A return FALSE;
1N/A }
1N/A ntfs_log_verbose("Creating $LogFile (mft record 2)\n");
1N/A m = (MFT_RECORD*)(g_buf + 2 * g_vol->mft_record_size);
1N/A buf_log = ntfs_malloc(g_logfile_size);
1N/A if (!buf_log)
1N/A return FALSE;
1N/A memset(buf_log, -1, g_logfile_size);
1N/A err = add_attr_data_positioned(m, NULL, 0, 0, 0, g_rl_logfile, buf_log,
1N/A g_logfile_size);
1N/A free(buf_log);
1N/A buf_log = NULL;
1N/A if (!err)
1N/A err = create_hardlink(g_index_block, root_ref, m,
1N/A MK_LE_MREF(FILE_LogFile, FILE_LogFile),
1N/A g_logfile_size, g_logfile_size,
1N/A FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM, 0, 0,
1N/A "$LogFile", FILE_NAME_WIN32_AND_DOS);
1N/A if (err < 0) {
1N/A ntfs_log_error("Couldn't create $LogFile: %s\n",
1N/A strerror(-err));
1N/A return FALSE;
1N/A }
1N/A ntfs_log_verbose("Creating $AttrDef (mft record 4)\n");
1N/A m = (MFT_RECORD*)(g_buf + 4 * g_vol->mft_record_size);
1N/A err = add_attr_data(m, NULL, 0, 0, 0, (u8*)g_vol->attrdef,
1N/A g_vol->attrdef_len);
1N/A if (!err)
1N/A err = create_hardlink(g_index_block, root_ref, m,
1N/A MK_LE_MREF(FILE_AttrDef, FILE_AttrDef),
1N/A (g_vol->attrdef_len + g_vol->cluster_size - 1) &
1N/A ~(g_vol->cluster_size - 1), g_vol->attrdef_len,
1N/A FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM, 0, 0,
1N/A "$AttrDef", FILE_NAME_WIN32_AND_DOS);
1N/A if (!err) {
1N/A init_system_file_sd(FILE_AttrDef, &sd, &i);
1N/A err = add_attr_sd(m, sd, i);
1N/A }
1N/A if (err < 0) {
1N/A ntfs_log_error("Couldn't create $AttrDef: %s\n",
1N/A strerror(-err));
1N/A return FALSE;
1N/A }
1N/A ntfs_log_verbose("Creating $Bitmap (mft record 6)\n");
1N/A m = (MFT_RECORD*)(g_buf + 6 * g_vol->mft_record_size);
1N/A /* the data attribute of $Bitmap must be non-resident or otherwise */
1N/A /* windows 2003 will regard the volume as corrupt (ERSO) */
1N/A if (!err)
1N/A err = insert_non_resident_attr_in_mft_record(m,
1N/A AT_DATA, NULL, 0, 0, 0,
1N/A g_lcn_bitmap, g_lcn_bitmap_byte_size);
1N/A
1N/A
1N/A if (!err)
1N/A err = create_hardlink(g_index_block, root_ref, m,
1N/A MK_LE_MREF(FILE_Bitmap, FILE_Bitmap),
1N/A (g_lcn_bitmap_byte_size + g_vol->cluster_size -
1N/A 1) & ~(g_vol->cluster_size - 1),
1N/A g_lcn_bitmap_byte_size,
1N/A FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM, 0, 0,
1N/A "$Bitmap", FILE_NAME_WIN32_AND_DOS);
1N/A if (err < 0) {
1N/A ntfs_log_error("Couldn't create $Bitmap: %s\n", strerror(-err));
1N/A return FALSE;
1N/A }
1N/A ntfs_log_verbose("Creating $Boot (mft record 7)\n");
1N/A m = (MFT_RECORD*)(g_buf + 7 * g_vol->mft_record_size);
1N/A bs = ntfs_calloc(8192);
1N/A if (!bs)
1N/A return FALSE;
1N/A memcpy(bs, boot_array, sizeof(boot_array));
1N/A /*
1N/A * Create the boot sector in bs. Note, that bs is already zeroed
1N/A * in the boot sector section and that it has the NTFS OEM id/magic
1N/A * already inserted, so no need to worry about these things.
1N/A */
1N/A bs->bpb.bytes_per_sector = cpu_to_le16(opts.sector_size);
1N/A bs->bpb.sectors_per_cluster = (u8)(g_vol->cluster_size /
1N/A opts.sector_size);
1N/A bs->bpb.media_type = 0xf8; /* hard disk */
1N/A bs->bpb.sectors_per_track = cpu_to_le16(opts.sectors_per_track);
1N/A ntfs_log_debug("sectors per track = %ld (0x%lx)\n",
1N/A opts.sectors_per_track, opts.sectors_per_track);
1N/A bs->bpb.heads = cpu_to_le16(opts.heads);
1N/A ntfs_log_debug("heads = %ld (0x%lx)\n", opts.heads, opts.heads);
1N/A bs->bpb.hidden_sectors = cpu_to_le32(opts.part_start_sect);
1N/A ntfs_log_debug("hidden sectors = %llu (0x%llx)\n", opts.part_start_sect,
1N/A opts.part_start_sect);
1N/A bs->physical_drive = 0x80; /* boot from hard disk */
1N/A bs->extended_boot_signature = 0x80; /* everybody sets this, so we do */
1N/A bs->number_of_sectors = cpu_to_sle64(opts.num_sectors);
1N/A bs->mft_lcn = cpu_to_sle64(g_mft_lcn);
1N/A bs->mftmirr_lcn = cpu_to_sle64(g_mftmirr_lcn);
1N/A if (g_vol->mft_record_size >= g_vol->cluster_size) {
1N/A bs->clusters_per_mft_record = g_vol->mft_record_size /
1N/A g_vol->cluster_size;
1N/A } else {
1N/A bs->clusters_per_mft_record = -(ffs(g_vol->mft_record_size) -
1N/A 1);
1N/A if ((u32)(1 << -bs->clusters_per_mft_record) !=
1N/A g_vol->mft_record_size) {
1N/A free(bs);
1N/A ntfs_log_error("BUG: calculated clusters_per_mft_record"
1N/A " is wrong (= 0x%x)\n",
1N/A bs->clusters_per_mft_record);
1N/A return FALSE;
1N/A }
1N/A }
1N/A ntfs_log_debug("clusters per mft record = %i (0x%x)\n",
1N/A bs->clusters_per_mft_record,
1N/A bs->clusters_per_mft_record);
1N/A if (g_vol->indx_record_size >= g_vol->cluster_size) {
1N/A bs->clusters_per_index_record = g_vol->indx_record_size /
1N/A g_vol->cluster_size;
1N/A } else {
1N/A bs->clusters_per_index_record = -g_vol->indx_record_size_bits;
1N/A if ((1 << -bs->clusters_per_index_record) !=
1N/A (s32)g_vol->indx_record_size) {
1N/A free(bs);
1N/A ntfs_log_error("BUG: calculated "
1N/A "clusters_per_index_record is wrong "
1N/A "(= 0x%x)\n",
1N/A bs->clusters_per_index_record);
1N/A return FALSE;
1N/A }
1N/A }
1N/A ntfs_log_debug("clusters per index block = %i (0x%x)\n",
1N/A bs->clusters_per_index_record,
1N/A bs->clusters_per_index_record);
1N/A /* Generate a 64-bit random number for the serial number. */
1N/A bs->volume_serial_number = cpu_to_le64(((u64)random() << 32) |
1N/A ((u64)random() & 0xffffffff));
1N/A /*
1N/A * Leave zero for now as NT4 leaves it zero, too. If want it later, see
1N/A * ../libntfs/bootsect.c for how to calculate it.
1N/A */
1N/A bs->checksum = cpu_to_le32(0);
1N/A /* Make sure the bootsector is ok. */
1N/A if (!ntfs_boot_sector_is_ntfs(bs, TRUE)) {
1N/A free(bs);
1N/A ntfs_log_error("FATAL: Generated boot sector is invalid!\n");
1N/A return FALSE;
1N/A }
1N/A err = add_attr_data_positioned(m, NULL, 0, 0, 0, g_rl_boot, (u8*)bs,
1N/A 8192);
1N/A if (!err)
1N/A err = create_hardlink(g_index_block, root_ref, m,
1N/A MK_LE_MREF(FILE_Boot, FILE_Boot),
1N/A (8192 + g_vol->cluster_size - 1) &
1N/A ~(g_vol->cluster_size - 1), 8192,
1N/A FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM, 0, 0,
1N/A "$Boot", FILE_NAME_WIN32_AND_DOS);
1N/A if (!err) {
1N/A init_system_file_sd(FILE_Boot, &sd, &i);
1N/A err = add_attr_sd(m, sd, i);
1N/A }
1N/A if (err < 0) {
1N/A free(bs);
1N/A ntfs_log_error("Couldn't create $Boot: %s\n", strerror(-err));
1N/A return FALSE;
1N/A }
1N/A if (create_backup_boot_sector((u8*)bs)) {
1N/A /*
1N/A * Pre-2.6 kernels couldn't access the last sector if it was
1N/A * odd and we failed to set the device block size to the sector
1N/A * size, hence we schedule chkdsk to create it.
1N/A */
1N/A volume_flags |= VOLUME_IS_DIRTY;
1N/A }
1N/A free(bs);
1N/A#ifdef ENABLE_UUID
1N/A /*
1N/A * We cheat a little here and if the user has requested all times to be
1N/A * set to zero then we set the GUID to zero as well. This options is
1N/A * only used for development purposes so that should be fine.
1N/A */
1N/A if (!opts.use_epoch_time) {
1N/A /* Generate a GUID for the volume. */
1N/A uuid_generate((void*)&g_vol->guid);
1N/A } else
1N/A memset(&g_vol->guid, 0, sizeof(g_vol->guid));
1N/A#endif
1N/A if (!create_file_volume(m, root_ref, volume_flags, &g_vol->guid))
1N/A return FALSE;
1N/A ntfs_log_verbose("Creating $BadClus (mft record 8)\n");
1N/A m = (MFT_RECORD*)(g_buf + 8 * g_vol->mft_record_size);
1N/A /* FIXME: This should be IGNORE_CASE */
1N/A /* Create a sparse named stream of size equal to the volume size. */
1N/A err = add_attr_data_positioned(m, "$Bad", 4, 0, 0, g_rl_bad, NULL,
1N/A g_vol->nr_clusters * g_vol->cluster_size);
1N/A if (!err) {
1N/A err = add_attr_data(m, NULL, 0, 0, 0, NULL, 0);
1N/A }
1N/A if (!err) {
1N/A err = create_hardlink(g_index_block, root_ref, m,
1N/A MK_LE_MREF(FILE_BadClus, FILE_BadClus),
1N/A 0LL, 0LL, FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM,
1N/A 0, 0, "$BadClus", FILE_NAME_WIN32_AND_DOS);
1N/A }
1N/A if (err < 0) {
1N/A ntfs_log_error("Couldn't create $BadClus: %s\n",
1N/A strerror(-err));
1N/A return FALSE;
1N/A }
1N/A /* create $Secure (NTFS 3.0+) */
1N/A ntfs_log_verbose("Creating $Secure (mft record 9)\n");
1N/A m = (MFT_RECORD*)(g_buf + 9 * g_vol->mft_record_size);
1N/A m->flags |= MFT_RECORD_IS_VIEW_INDEX;
1N/A if (!err)
1N/A err = create_hardlink(g_index_block, root_ref, m,
1N/A MK_LE_MREF(9, 9), 0LL, 0LL,
1N/A FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM |
1N/A FILE_ATTR_VIEW_INDEX_PRESENT, 0, 0,
1N/A "$Secure", FILE_NAME_WIN32_AND_DOS);
1N/A buf_sds = NULL;
1N/A buf_sds_first_size = 0;
1N/A if (!err) {
1N/A int buf_sds_size;
1N/A
1N/A buf_sds_first_size = 0xfc;
1N/A buf_sds_size = 0x40000 + buf_sds_first_size;
1N/A buf_sds = ntfs_calloc(buf_sds_size);
1N/A if (!buf_sds)
1N/A return FALSE;
1N/A init_secure_sds(buf_sds);
1N/A memcpy(buf_sds + 0x40000, buf_sds, buf_sds_first_size);
1N/A err = add_attr_data(m, "$SDS", 4, 0, 0, (u8*)buf_sds,
1N/A buf_sds_size);
1N/A }
1N/A /* FIXME: This should be IGNORE_CASE */
1N/A if (!err)
1N/A err = add_attr_index_root(m, "$SDH", 4, 0, AT_UNUSED,
1N/A COLLATION_NTOFS_SECURITY_HASH,
1N/A g_vol->indx_record_size);
1N/A /* FIXME: This should be IGNORE_CASE */
1N/A if (!err)
1N/A err = add_attr_index_root(m, "$SII", 4, 0, AT_UNUSED,
1N/A COLLATION_NTOFS_ULONG, g_vol->indx_record_size);
1N/A if (!err)
1N/A err = initialize_secure(buf_sds, buf_sds_first_size, m);
1N/A free(buf_sds);
1N/A if (err < 0) {
1N/A ntfs_log_error("Couldn't create $Secure: %s\n",
1N/A strerror(-err));
1N/A return FALSE;
1N/A }
1N/A ntfs_log_verbose("Creating $UpCase (mft record 0xa)\n");
1N/A m = (MFT_RECORD*)(g_buf + 0xa * g_vol->mft_record_size);
1N/A err = add_attr_data(m, NULL, 0, 0, 0, (u8*)g_vol->upcase,
1N/A g_vol->upcase_len << 1);
1N/A if (!err)
1N/A err = create_hardlink(g_index_block, root_ref, m,
1N/A MK_LE_MREF(FILE_UpCase, FILE_UpCase),
1N/A ((g_vol->upcase_len << 1) +
1N/A g_vol->cluster_size - 1) &
1N/A ~(g_vol->cluster_size - 1),
1N/A g_vol->upcase_len << 1,
1N/A FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM, 0, 0,
1N/A "$UpCase", FILE_NAME_WIN32_AND_DOS);
1N/A if (err < 0) {
1N/A ntfs_log_error("Couldn't create $UpCase: %s\n", strerror(-err));
1N/A return FALSE;
1N/A }
1N/A ntfs_log_verbose("Creating $Extend (mft record 11)\n");
1N/A /*
1N/A * $Extend index must be resident. Otherwise, w2k3 will regard the
1N/A * volume as corrupt. (ERSO)
1N/A */
1N/A m = (MFT_RECORD*)(g_buf + 11 * g_vol->mft_record_size);
1N/A m->flags |= MFT_RECORD_IS_DIRECTORY;
1N/A if (!err)
1N/A err = create_hardlink(g_index_block, root_ref, m,
1N/A MK_LE_MREF(11, 11), 0LL, 0LL,
1N/A FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM |
1N/A FILE_ATTR_I30_INDEX_PRESENT, 0, 0,
1N/A "$Extend", FILE_NAME_WIN32_AND_DOS);
1N/A /* FIXME: This should be IGNORE_CASE */
1N/A if (!err)
1N/A err = add_attr_index_root(m, "$I30", 4, 0, AT_FILE_NAME,
1N/A COLLATION_FILE_NAME, g_vol->indx_record_size);
1N/A if (err < 0) {
1N/A ntfs_log_error("Couldn't create $Extend: %s\n",
1N/A strerror(-err));
1N/A return FALSE;
1N/A }
1N/A /* NTFS reserved system files (mft records 0xc-0xf) */
1N/A for (i = 0xc; i < 0x10; i++) {
1N/A ntfs_log_verbose("Creating system file (mft record 0x%x)\n", i);
1N/A m = (MFT_RECORD*)(g_buf + i * g_vol->mft_record_size);
1N/A err = add_attr_data(m, NULL, 0, 0, 0, NULL, 0);
1N/A if (!err) {
1N/A init_system_file_sd(i, &sd, &j);
1N/A err = add_attr_sd(m, sd, j);
1N/A }
1N/A if (err < 0) {
1N/A ntfs_log_error("Couldn't create system file %i (0x%x): "
1N/A "%s\n", i, i, strerror(-err));
1N/A return FALSE;
1N/A }
1N/A }
1N/A /* create systemfiles for ntfs volumes (3.1) */
1N/A /* starting with file 24 (ignoring file 16-23) */
1N/A extend_flags = FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM |
1N/A FILE_ATTR_ARCHIVE | FILE_ATTR_VIEW_INDEX_PRESENT;
1N/A ntfs_log_verbose("Creating $Quota (mft record 24)\n");
1N/A m = (MFT_RECORD*)(g_buf + 24 * g_vol->mft_record_size);
1N/A m->flags |= MFT_RECORD_IS_4;
1N/A m->flags |= MFT_RECORD_IS_VIEW_INDEX;
1N/A if (!err)
1N/A err = create_hardlink_res((MFT_RECORD*)(g_buf +
1N/A 11 * g_vol->mft_record_size), extend_ref, m,
1N/A MK_LE_MREF(24, 1), 0LL, 0LL, extend_flags,
1N/A 0, 0, "$Quota", FILE_NAME_WIN32_AND_DOS);
1N/A /* FIXME: This should be IGNORE_CASE */
1N/A if (!err)
1N/A err = add_attr_index_root(m, "$Q", 2, 0, AT_UNUSED,
1N/A COLLATION_NTOFS_ULONG, g_vol->indx_record_size);
1N/A /* FIXME: This should be IGNORE_CASE */
1N/A if (!err)
1N/A err = add_attr_index_root(m, "$O", 2, 0, AT_UNUSED,
1N/A COLLATION_NTOFS_SID, g_vol->indx_record_size);
1N/A if (!err)
1N/A err = initialize_quota(m);
1N/A if (err < 0) {
1N/A ntfs_log_error("Couldn't create $Quota: %s\n", strerror(-err));
1N/A return FALSE;
1N/A }
1N/A ntfs_log_verbose("Creating $ObjId (mft record 25)\n");
1N/A m = (MFT_RECORD*)(g_buf + 25 * g_vol->mft_record_size);
1N/A m->flags |= MFT_RECORD_IS_4;
1N/A m->flags |= MFT_RECORD_IS_VIEW_INDEX;
1N/A if (!err)
1N/A err = create_hardlink_res((MFT_RECORD*)(g_buf +
1N/A 11 * g_vol->mft_record_size), extend_ref,
1N/A m, MK_LE_MREF(25, 1), 0LL, 0LL,
1N/A extend_flags, 0, 0, "$ObjId",
1N/A FILE_NAME_WIN32_AND_DOS);
1N/A
1N/A /* FIXME: This should be IGNORE_CASE */
1N/A if (!err)
1N/A err = add_attr_index_root(m, "$O", 2, 0, AT_UNUSED,
1N/A COLLATION_NTOFS_ULONGS,
1N/A g_vol->indx_record_size);
1N/A#ifdef ENABLE_UUID
1N/A if (!err)
1N/A err = index_obj_id_insert(m, &g_vol->guid,
1N/A MK_LE_MREF(FILE_Volume, FILE_Volume));
1N/A#endif
1N/A if (err < 0) {
1N/A ntfs_log_error("Couldn't create $ObjId: %s\n",
1N/A strerror(-err));
1N/A return FALSE;
1N/A }
1N/A ntfs_log_verbose("Creating $Reparse (mft record 26)\n");
1N/A m = (MFT_RECORD*)(g_buf + 26 * g_vol->mft_record_size);
1N/A m->flags |= MFT_RECORD_IS_4;
1N/A m->flags |= MFT_RECORD_IS_VIEW_INDEX;
1N/A if (!err)
1N/A err = create_hardlink_res((MFT_RECORD*)(g_buf +
1N/A 11 * g_vol->mft_record_size),
1N/A extend_ref, m, MK_LE_MREF(26, 1),
1N/A 0LL, 0LL, extend_flags, 0, 0,
1N/A "$Reparse", FILE_NAME_WIN32_AND_DOS);
1N/A /* FIXME: This should be IGNORE_CASE */
1N/A if (!err)
1N/A err = add_attr_index_root(m, "$R", 2, 0, AT_UNUSED,
1N/A COLLATION_NTOFS_ULONGS, g_vol->indx_record_size);
1N/A if (err < 0) {
1N/A ntfs_log_error("Couldn't create $Reparse: %s\n",
1N/A strerror(-err));
1N/A return FALSE;
1N/A }
1N/A return TRUE;
1N/A}
1N/A
1N/A/**
1N/A * mkntfs_redirect
1N/A */
1N/Astatic int mkntfs_redirect(struct mkntfs_options *opts2)
1N/A{
1N/A int result = 1;
1N/A ntfs_attr_search_ctx *ctx = NULL;
1N/A long long lw, pos;
1N/A ATTR_RECORD *a;
1N/A MFT_RECORD *m;
1N/A int i, err;
1N/A
1N/A if (!opts2) {
1N/A ntfs_log_error("Internal error: invalid parameters to mkntfs_options.\n");
1N/A goto done;
1N/A }
1N/A /* Initialize the random number generator with the current time. */
1N/A srandom(mkntfs_time());
1N/A /* Allocate and initialize ntfs_volume structure g_vol. */
1N/A g_vol = ntfs_volume_alloc();
1N/A if (!g_vol) {
1N/A ntfs_log_perror("Could not create volume");
1N/A goto done;
1N/A }
1N/A /* Create NTFS 3.1 (Windows XP/Vista) volumes. */
1N/A g_vol->major_ver = 3;
1N/A g_vol->minor_ver = 1;
1N/A /* Transfer some options to the volume. */
1N/A if (opts.label) {
1N/A g_vol->vol_name = strdup(opts.label);
1N/A if (!g_vol->vol_name) {
1N/A ntfs_log_perror("Could not copy volume name");
1N/A goto done;
1N/A }
1N/A }
1N/A if (opts.cluster_size >= 0)
1N/A g_vol->cluster_size = opts.cluster_size;
1N/A /* Length is in unicode characters. */
1N/A g_vol->upcase_len = 65536;
1N/A g_vol->upcase = ntfs_malloc(g_vol->upcase_len * sizeof(ntfschar));
1N/A if (!g_vol->upcase)
1N/A goto done;
1N/A ntfs_upcase_table_build(g_vol->upcase,
1N/A g_vol->upcase_len * sizeof(ntfschar));
1N/A g_vol->attrdef = ntfs_malloc(sizeof(attrdef_ntfs3x_array));
1N/A if (!g_vol->attrdef) {
1N/A ntfs_log_perror("Could not create attrdef structure");
1N/A goto done;
1N/A }
1N/A memcpy(g_vol->attrdef, attrdef_ntfs3x_array,
1N/A sizeof(attrdef_ntfs3x_array));
1N/A g_vol->attrdef_len = sizeof(attrdef_ntfs3x_array);
1N/A /* Open the partition. */
1N/A if (!mkntfs_open_partition(g_vol))
1N/A goto done;
1N/A /*
1N/A * Decide on the sector size, cluster size, mft record and index record
1N/A * sizes as well as the number of sectors/tracks/heads/size, etc.
1N/A */
1N/A if (!mkntfs_override_vol_params(g_vol))
1N/A goto done;
1N/A /* Initialize $Bitmap and $MFT/$BITMAP related stuff. */
1N/A if (!mkntfs_initialize_bitmaps())
1N/A goto done;
1N/A /* Initialize MFT & set g_logfile_lcn. */
1N/A if (!mkntfs_initialize_rl_mft())
1N/A goto done;
1N/A /* Initialize $LogFile. */
1N/A if (!mkntfs_initialize_rl_logfile())
1N/A goto done;
1N/A /* Initialize $Boot. */
1N/A if (!mkntfs_initialize_rl_boot())
1N/A goto done;
1N/A /* Allocate a buffer large enough to hold the mft. */
1N/A g_buf = ntfs_calloc(g_mft_size);
1N/A if (!g_buf)
1N/A goto done;
1N/A /* Create runlist for $BadClus, $DATA named stream $Bad. */
1N/A if (!mkntfs_initialize_rl_bad())
1N/A goto done;
1N/A /* If not quick format, fill the device with 0s. */
1N/A if (!opts.quick_format) {
1N/A if (!mkntfs_fill_device_with_zeroes())
1N/A goto done;
1N/A }
1N/A /* Create NTFS volume structures. */
1N/A if (!mkntfs_create_root_structures())
1N/A goto done;
1N/A /*
1N/A * - Do not step onto bad blocks!!!
1N/A * - If any bad blocks were specified or found, modify $BadClus,
1N/A * allocating the bad clusters in $Bitmap.
1N/A * - C&w bootsector backup bootsector (backup in last sector of the
1N/A * partition).
1N/A * - If NTFS 3.0+, c&w $Secure file and $Extend directory with the
1N/A * corresponding special files in it, i.e. $ObjId, $Quota, $Reparse,
1N/A * and $UsnJrnl. And others? Or not all necessary?
1N/A * - RE: Populate $root with the system files (and $Extend directory if
1N/A * applicable). Possibly should move this as far to the top as
1N/A * possible and update during each subsequent c&w of each system file.
1N/A */
1N/A ntfs_log_verbose("Syncing root directory index record.\n");
1N/A if (!mkntfs_sync_index_record(g_index_block, (MFT_RECORD*)(g_buf + 5 *
1N/A g_vol->mft_record_size), NTFS_INDEX_I30, 4))
1N/A goto done;
1N/A
1N/A ntfs_log_verbose("Syncing $Bitmap.\n");
1N/A m = (MFT_RECORD*)(g_buf + 6 * g_vol->mft_record_size);
1N/A
1N/A ctx = ntfs_attr_get_search_ctx(NULL, m);
1N/A if (!ctx) {
1N/A ntfs_log_perror("Could not create an attribute search context");
1N/A goto done;
1N/A }
1N/A
1N/A if (mkntfs_attr_lookup(AT_DATA, AT_UNNAMED, 0, 0, 0, NULL, 0, ctx)) {
1N/A ntfs_log_error("BUG: $DATA attribute not found.\n");
1N/A goto done;
1N/A }
1N/A
1N/A a = ctx->attr;
1N/A if (a->non_resident) {
1N/A runlist *rl = ntfs_mapping_pairs_decompress(g_vol, a, NULL);
1N/A if (!rl) {
1N/A ntfs_log_error("ntfs_mapping_pairs_decompress() failed\n");
1N/A goto done;
1N/A }
1N/A lw = ntfs_rlwrite(g_vol->u.dev, rl, g_lcn_bitmap, g_lcn_bitmap_byte_size, NULL);
1N/A err = errno;
1N/A free(rl);
1N/A if (lw != g_lcn_bitmap_byte_size) {
1N/A ntfs_log_error("ntfs_rlwrite: %s\n", lw == -1 ?
1N/A strerror(err) : "unknown error");
1N/A goto done;
1N/A }
1N/A } else {
1N/A memcpy((char*)a + le16_to_cpu(a->u.res.value_offset), g_lcn_bitmap, le32_to_cpu(a->u.res.value_length));
1N/A }
1N/A
1N/A /*
1N/A * No need to sync $MFT/$BITMAP as that has never been modified since
1N/A * its creation.
1N/A */
1N/A ntfs_log_verbose("Syncing $MFT.\n");
1N/A pos = g_mft_lcn * g_vol->cluster_size;
1N/A lw = 1;
1N/A for (i = 0; i < g_mft_size / (s32)g_vol->mft_record_size; i++) {
1N/A if (!opts.no_action)
1N/A lw = ntfs_mst_pwrite(g_vol->u.dev, pos, 1, g_vol->mft_record_size, g_buf + i * g_vol->mft_record_size);
1N/A if (lw != 1) {
1N/A ntfs_log_error("ntfs_mst_pwrite: %s\n", lw == -1 ?
1N/A strerror(errno) : "unknown error");
1N/A goto done;
1N/A }
1N/A pos += g_vol->mft_record_size;
1N/A }
1N/A ntfs_log_verbose("Updating $MFTMirr.\n");
1N/A pos = g_mftmirr_lcn * g_vol->cluster_size;
1N/A lw = 1;
1N/A for (i = 0; i < g_rl_mftmirr[0].length * g_vol->cluster_size / g_vol->mft_record_size; i++) {
1N/A m = (MFT_RECORD*)(g_buf + i * g_vol->mft_record_size);
1N/A /*
1N/A * Decrement the usn by one, so it becomes the same as the one
1N/A * in $MFT once it is mst protected. - This is as we need the
1N/A * $MFTMirr to have the exact same byte by byte content as
1N/A * $MFT, rather than just equivalent meaning content.
1N/A */
1N/A if (ntfs_mft_usn_dec(m)) {
1N/A ntfs_log_error("ntfs_mft_usn_dec");
1N/A goto done;
1N/A }
1N/A if (!opts.no_action)
1N/A lw = ntfs_mst_pwrite(g_vol->u.dev, pos, 1, g_vol->mft_record_size, g_buf + i * g_vol->mft_record_size);
1N/A if (lw != 1) {
1N/A ntfs_log_error("ntfs_mst_pwrite: %s\n", lw == -1 ?
1N/A strerror(errno) : "unknown error");
1N/A goto done;
1N/A }
1N/A pos += g_vol->mft_record_size;
1N/A }
1N/A ntfs_log_verbose("Syncing device.\n");
1N/A if (g_vol->u.dev->d_ops->sync(g_vol->u.dev)) {
1N/A ntfs_log_error("Syncing device. FAILED");
1N/A goto done;
1N/A }
1N/A ntfs_log_quiet("mkntfs completed successfully. Have a nice day.\n");
1N/A result = 0;
1N/Adone:
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A mkntfs_cleanup(); /* Device is unlocked and closed here */
1N/A return result;
1N/A}
1N/A
1N/A
1N/A/**
1N/A * main - Begin here
1N/A *
1N/A * Start from here.
1N/A *
1N/A * Return: 0 Success, the program worked
1N/A * 1 Error, something went wrong
1N/A */
1N/Aint main(int argc, char *argv[])
1N/A{
1N/A int result = 1;
1N/A
1N/A ntfs_log_set_handler(ntfs_log_handler_outerr);
1N/A utils_set_locale();
1N/A
1N/A mkntfs_init_options(&opts); /* Set up the options */
1N/A
1N/A if (!mkntfs_parse_options(argc, argv, &opts)) /* Read the command line options */
1N/A goto done;
1N/A
1N/A result = mkntfs_redirect(&opts);
1N/Adone:
1N/A return result;
1N/A}