1N/A/**
1N/A * volume.c - NTFS volume handling code. Part of the Linux-NTFS project.
1N/A *
1N/A * Copyright (c) 2000-2006 Anton Altaparmakov
1N/A * Copyright (c) 2002-2006 Szabolcs Szakacsits
1N/A * Copyright (c) 2004-2005 Richard Russon
1N/A * Copyright (c) 2005-2007 Yura Pakhuchiy
1N/A *
1N/A * This program/include file is free software; you can redistribute it and/or
1N/A * modify it under the terms of the GNU General Public License as published
1N/A * by 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/include file is distributed in the hope that it will be
1N/A * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
1N/A * of 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
1N/A * distribution in the file COPYING); if not, write to the Free Software
1N/A * Foundation,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_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_STRING_H
1N/A#include <string.h>
1N/A#endif
1N/A#ifdef HAVE_FCNTL_H
1N/A#include <fcntl.h>
1N/A#endif
1N/A#ifdef HAVE_UNISTD_H
1N/A#include <unistd.h>
1N/A#endif
1N/A#ifdef HAVE_ERRNO_H
1N/A#include <errno.h>
1N/A#endif
1N/A#ifdef HAVE_SYS_STAT_H
1N/A#include <sys/stat.h>
1N/A#endif
1N/A#ifdef HAVE_LIMITS_H
1N/A#include <limits.h>
1N/A#endif
1N/A
1N/A#include "compat.h"
1N/A#include "volume.h"
1N/A#include "attrib.h"
1N/A#include "mft.h"
1N/A#include "bootsect.h"
1N/A#include "device.h"
1N/A#include "debug.h"
1N/A#include "inode.h"
1N/A#include "runlist.h"
1N/A#include "logfile.h"
1N/A#include "dir.h"
1N/A#include "logging.h"
1N/A
1N/A#ifndef PATH_MAX
1N/A#define PATH_MAX 4096
1N/A#endif
1N/A
1N/A/**
1N/A * ntfs_volume_alloc - Create an NTFS volume object and initialise it
1N/A *
1N/A * Description...
1N/A *
1N/A * Returns:
1N/A */
1N/Antfs_volume *ntfs_volume_alloc(void)
1N/A{
1N/A ntfs_volume *vol;
1N/A int i;
1N/A
1N/A vol = calloc(1, sizeof(ntfs_volume));
1N/A if (vol) {
1N/A for (i = 0; i < NTFS_INODE_CACHE_SIZE; i++)
1N/A INIT_LIST_HEAD(&vol->inode_cache[i]);
1N/A }
1N/A return vol;
1N/A}
1N/A
1N/A/**
1N/A * __ntfs_volume_release - Destroy an NTFS volume object
1N/A * @v:
1N/A *
1N/A * Description...
1N/A *
1N/A * Returns:
1N/A */
1N/Astatic void __ntfs_volume_release(ntfs_volume *v)
1N/A{
1N/A struct list_head *pos, *tmp;
1N/A int i;
1N/A
1N/A /* Sync and print error about not detached inodes. */
1N/A for (i = 0; i < NTFS_INODE_CACHE_SIZE; i++)
1N/A list_for_each_safe(pos, tmp, &v->inode_cache[i]) {
1N/A ntfs_inode *ni =
1N/A list_entry(pos, ntfs_inode, list_entry);
1N/A
1N/A switch (ni->mft_no) {
1N/A case FILE_Volume:
1N/A case FILE_Bitmap:
1N/A case FILE_MFT:
1N/A case FILE_MFTMirr:
1N/A if (ni->nr_references == 1)
1N/A continue;
1N/A break;
1N/A }
1N/A
1N/A ntfs_log_error("%s(): Inode %llu still have %d "
1N/A "references.\n", "__ntfs_volume_release",
1N/A ni->mft_no, ni->nr_references);
1N/A ntfs_inode_sync(ni);
1N/A }
1N/A /*
1N/A * Clear the dirty bit if it was not set before we mounted and this is
1N/A * not a forensic mount.
1N/A */
1N/A if (!NVolReadOnly(v) && !NVolWasDirty(v) && !NVolForensicMount(v)) {
1N/A v->flags &= ~VOLUME_IS_DIRTY;
1N/A (void)ntfs_volume_write_flags(v, v->flags);
1N/A }
1N/A if (v->lcnbmp_ni && NInoDirty(v->lcnbmp_ni))
1N/A ntfs_inode_sync(v->lcnbmp_ni);
1N/A if (v->vol_ni)
1N/A ntfs_inode_close(v->vol_ni);
1N/A if (v->lcnbmp_na)
1N/A ntfs_attr_close(v->lcnbmp_na);
1N/A if (v->lcnbmp_ni)
1N/A ntfs_inode_close(v->lcnbmp_ni);
1N/A if (v->mft_ni && NInoDirty(v->mft_ni))
1N/A ntfs_inode_sync(v->mft_ni);
1N/A if (v->mftbmp_na)
1N/A ntfs_attr_close(v->mftbmp_na);
1N/A if (v->mft_na)
1N/A ntfs_attr_close(v->mft_na);
1N/A if (v->mft_ni)
1N/A ntfs_inode_close(v->mft_ni);
1N/A if (v->mftmirr_ni && NInoDirty(v->mftmirr_ni))
1N/A ntfs_inode_sync(v->mftmirr_ni);
1N/A if (v->mftmirr_na)
1N/A ntfs_attr_close(v->mftmirr_na);
1N/A if (v->mftmirr_ni)
1N/A ntfs_inode_close(v->mftmirr_ni);
1N/A if (v->u.dev) {
1N/A struct ntfs_device *dev = v->u.dev;
1N/A
1N/A if (NDevDirty(dev))
1N/A dev->d_ops->sync(dev);
1N/A if (dev->d_ops->close(dev))
1N/A ntfs_log_perror("Failed to close the device");
1N/A }
1N/A free(v->vol_name);
1N/A free(v->upcase);
1N/A free(v->attrdef);
1N/A free(v);
1N/A}
1N/A
1N/A/**
1N/A * ntfs_mft_load - load the $MFT and setup the ntfs volume with it
1N/A * @vol: ntfs volume whose $MFT to load
1N/A *
1N/A * Load $MFT from @vol and setup @vol with it. After calling this function the
1N/A * volume @vol is ready for use by all read access functions provided by the
1N/A * ntfs library.
1N/A *
1N/A * Return 0 on success and -1 on error with errno set to the error code.
1N/A */
1N/Astatic int ntfs_mft_load(ntfs_volume *vol)
1N/A{
1N/A VCN next_vcn, last_vcn, highest_vcn;
1N/A s64 l;
1N/A MFT_RECORD *mb = NULL;
1N/A ntfs_attr_search_ctx *ctx = NULL;
1N/A ATTR_RECORD *a;
1N/A STANDARD_INFORMATION *std_info;
1N/A int eo;
1N/A
1N/A /* Manually setup an ntfs_inode. */
1N/A vol->mft_ni = ntfs_inode_allocate(vol);
1N/A mb = (MFT_RECORD*)ntfs_malloc(vol->mft_record_size);
1N/A if (!vol->mft_ni || !mb) {
1N/A ntfs_log_perror("Error allocating memory for $MFT");
1N/A goto error_exit;
1N/A }
1N/A vol->mft_ni->mft_no = 0;
1N/A vol->mft_ni->mrec = mb;
1N/A __ntfs_inode_add_to_cache(vol->mft_ni);
1N/A /* Can't use any of the higher level functions yet! */
1N/A l = ntfs_mst_pread(vol->u.dev, vol->mft_lcn << vol->cluster_size_bits, 1,
1N/A vol->mft_record_size, mb);
1N/A if (l != 1) {
1N/A if (l != -1)
1N/A errno = EIO;
1N/A ntfs_log_perror("Error reading $MFT");
1N/A goto error_exit;
1N/A }
1N/A if (ntfs_is_baad_record(mb->magic)) {
1N/A ntfs_log_error("Incomplete multi sector transfer detected in "
1N/A "$MFT.\n");
1N/A goto io_error_exit;
1N/A }
1N/A if (!ntfs_is_mft_record(mb->magic)) {
1N/A ntfs_log_error("$MFT has invalid magic.\n");
1N/A goto io_error_exit;
1N/A }
1N/A ctx = ntfs_attr_get_search_ctx(vol->mft_ni, NULL);
1N/A if (!ctx) {
1N/A ntfs_log_perror("Failed to allocate attribute search context");
1N/A goto error_exit;
1N/A }
1N/A if (p2n(ctx->attr) < p2n(mb) ||
1N/A (char*)ctx->attr > (char*)mb + vol->mft_record_size) {
1N/A ntfs_log_error("$MFT is corrupt.\n");
1N/A goto io_error_exit;
1N/A }
1N/A /* Find the $ATTRIBUTE_LIST attribute in $MFT if present. */
1N/A if (ntfs_attr_lookup(AT_ATTRIBUTE_LIST, AT_UNNAMED, 0, 0, 0, NULL, 0,
1N/A ctx)) {
1N/A if (errno != ENOENT) {
1N/A ntfs_log_error("$MFT has corrupt attribute list.\n");
1N/A goto io_error_exit;
1N/A }
1N/A goto mft_has_no_attr_list;
1N/A }
1N/A NInoSetAttrList(vol->mft_ni);
1N/A l = ntfs_get_attribute_value_length(ctx->attr);
1N/A if (l <= 0 || l > 0x40000) {
1N/A ntfs_log_error("$MFT/$ATTRIBUTE_LIST has invalid length.\n");
1N/A goto io_error_exit;
1N/A }
1N/A vol->mft_ni->attr_list_size = l;
1N/A vol->mft_ni->attr_list = ntfs_malloc(l);
1N/A if (!vol->mft_ni->attr_list)
1N/A goto error_exit;
1N/A
1N/A l = ntfs_get_attribute_value(vol, ctx->attr, vol->mft_ni->attr_list);
1N/A if (!l) {
1N/A ntfs_log_error("Failed to get value of "
1N/A "$MFT/$ATTRIBUTE_LIST.\n");
1N/A goto io_error_exit;
1N/A }
1N/A if (l != vol->mft_ni->attr_list_size) {
1N/A ntfs_log_error("Got unexpected amount of data when "
1N/A "reading $MFT/$ATTRIBUTE_LIST.\n");
1N/A goto io_error_exit;
1N/A }
1N/Amft_has_no_attr_list:
1N/A /* Receive attributes from STANDARD_INFORMATION. */
1N/A std_info = ntfs_attr_readall(vol->mft_ni, AT_STANDARD_INFORMATION,
1N/A AT_UNNAMED, 0, NULL);
1N/A vol->mft_ni->flags = std_info->file_attributes;
1N/A free(std_info);
1N/A
1N/A /* We now have a fully setup ntfs inode for $MFT in vol->mft_ni. */
1N/A
1N/A /* Get an ntfs attribute for $MFT/$DATA and set it up, too. */
1N/A vol->mft_na = ntfs_attr_open(vol->mft_ni, AT_DATA, AT_UNNAMED, 0);
1N/A if (!vol->mft_na) {
1N/A ntfs_log_perror("Failed to open ntfs attribute");
1N/A goto error_exit;
1N/A }
1N/A /* Read all extents from the $DATA attribute in $MFT. */
1N/A ntfs_attr_reinit_search_ctx(ctx);
1N/A last_vcn = vol->mft_na->allocated_size >> vol->cluster_size_bits;
1N/A highest_vcn = next_vcn = 0;
1N/A a = NULL;
1N/A while (!ntfs_attr_lookup(AT_DATA, AT_UNNAMED, 0, 0, next_vcn, NULL, 0,
1N/A ctx)) {
1N/A runlist_element *nrl;
1N/A
1N/A a = ctx->attr;
1N/A /* $MFT must be non-resident. */
1N/A if (!a->non_resident) {
1N/A ntfs_log_error("$MFT must be non-resident but a "
1N/A "resident extent was found. $MFT is "
1N/A "corrupt. Run chkdsk.\n");
1N/A goto io_error_exit;
1N/A }
1N/A /* $MFT must be uncompressed and unencrypted. */
1N/A if (a->flags & ATTR_COMPRESSION_MASK ||
1N/A a->flags & ATTR_IS_ENCRYPTED) {
1N/A ntfs_log_error("$MFT must be uncompressed and "
1N/A "unencrypted but a compressed/encrypted"
1N/A " extent was found. $MFT is corrupt. "
1N/A "Run chkdsk.\n");
1N/A goto io_error_exit;
1N/A }
1N/A /*
1N/A * Decompress the mapping pairs array of this extent and merge
1N/A * the result into the existing runlist. No need for locking
1N/A * as we have exclusive access to the inode at this time and we
1N/A * are a mount in progress task, too.
1N/A */
1N/A nrl = ntfs_mapping_pairs_decompress(vol, a, vol->mft_na->rl);
1N/A if (!nrl) {
1N/A ntfs_log_perror("ntfs_mapping_pairs_decompress() "
1N/A "failed");
1N/A goto error_exit;
1N/A }
1N/A vol->mft_na->rl = nrl;
1N/A
1N/A /* Get the lowest vcn for the next extent. */
1N/A highest_vcn = sle64_to_cpu(a->u.nonres.highest_vcn);
1N/A next_vcn = highest_vcn + 1;
1N/A
1N/A /* Only one extent or error, which we catch below. */
1N/A if (next_vcn <= 0)
1N/A break;
1N/A
1N/A /* Avoid endless loops due to corruption. */
1N/A if (next_vcn < sle64_to_cpu(a->u.nonres.lowest_vcn)) {
1N/A ntfs_log_error("$MFT has corrupt attribute list "
1N/A "attribute. Run chkdsk.\n");
1N/A goto io_error_exit;
1N/A }
1N/A }
1N/A if (!a) {
1N/A ntfs_log_error("$MFT/$DATA attribute not found. "
1N/A "$MFT is corrupt. Run chkdsk.\n");
1N/A goto io_error_exit;
1N/A }
1N/A if (highest_vcn && highest_vcn != last_vcn - 1) {
1N/A ntfs_log_error("Failed to load the complete runlist for "
1N/A "$MFT/$DATA. Bug or corrupt $MFT. "
1N/A "Run chkdsk.\n highest_vcn = 0x%llx, "
1N/A "last_vcn - 1 = 0x%llx\n", (long long)
1N/A highest_vcn, (long long)last_vcn - 1);
1N/A goto io_error_exit;
1N/A }
1N/A /* Done with the $Mft mft record. */
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A ctx = NULL;
1N/A /*
1N/A * The volume is now setup so we can use all read access functions.
1N/A */
1N/A vol->mftbmp_na = ntfs_attr_open(vol->mft_ni, AT_BITMAP, AT_UNNAMED, 0);
1N/A if (!vol->mftbmp_na) {
1N/A ntfs_log_perror("Failed to open $MFT/$BITMAP");
1N/A goto error_exit;
1N/A }
1N/A return 0;
1N/Aio_error_exit:
1N/A errno = EIO;
1N/Aerror_exit:
1N/A eo = errno;
1N/A if (ctx)
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A if (vol->mft_na) {
1N/A ntfs_attr_close(vol->mft_na);
1N/A vol->mft_na = NULL;
1N/A }
1N/A if (vol->mft_ni) {
1N/A ntfs_inode_close(vol->mft_ni);
1N/A vol->mft_ni = NULL;
1N/A }
1N/A ntfs_log_error("%s(): Failed.\n", "ntfs_mft_load");
1N/A errno = eo;
1N/A return -1;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_mftmirr_load - load the $MFTMirr and setup the ntfs volume with it
1N/A * @vol: ntfs volume whose $MFTMirr to load
1N/A *
1N/A * Load $MFTMirr from @vol and setup @vol with it. After calling this function
1N/A * the volume @vol is ready for use by all write access functions provided by
1N/A * the ntfs library (assuming ntfs_mft_load() has been called successfully
1N/A * beforehand).
1N/A *
1N/A * Return 0 on success and -1 on error with errno set to the error code.
1N/A */
1N/Astatic int ntfs_mftmirr_load(ntfs_volume *vol)
1N/A{
1N/A int err;
1N/A
1N/A vol->mftmirr_ni = ntfs_inode_open(vol, FILE_MFTMirr);
1N/A if (!vol->mftmirr_ni) {
1N/A ntfs_log_perror("Failed to open inode $MFTMirr");
1N/A return -1;
1N/A }
1N/A /* Get an ntfs attribute for $MFTMirr/$DATA, too. */
1N/A vol->mftmirr_na = ntfs_attr_open(vol->mftmirr_ni, AT_DATA,
1N/A AT_UNNAMED, 0);
1N/A if (!vol->mftmirr_na) {
1N/A ntfs_log_perror("Failed to open $MFTMirr/$DATA");
1N/A goto error_exit;
1N/A }
1N/A if (ntfs_attr_map_runlist(vol->mftmirr_na, 0) < 0) {
1N/A ntfs_log_perror("Failed to map runlist of $MFTMirr/$DATA");
1N/A goto error_exit;
1N/A }
1N/A /* Check $MFTMirr runlist. */
1N/A if (vol->mftmirr_na->rl[0].lcn != vol->mftmirr_lcn ||
1N/A vol->mftmirr_na->rl[0].length < (vol->mftmirr_size *
1N/A vol->mft_record_size + vol->cluster_size - 1) /
1N/A vol->cluster_size) {
1N/A ntfs_log_error("$MFTMirr location mismatch or first 4 records "
1N/A "are fragmented. Run chkdsk.\n");
1N/A errno = EIO;
1N/A goto error_exit;
1N/A
1N/A }
1N/A return 0;
1N/Aerror_exit:
1N/A err = errno;
1N/A if (vol->mftmirr_na) {
1N/A ntfs_attr_close(vol->mftmirr_na);
1N/A vol->mftmirr_na = NULL;
1N/A }
1N/A ntfs_inode_close(vol->mftmirr_ni);
1N/A vol->mftmirr_ni = NULL;
1N/A errno = err;
1N/A return -1;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_volume_startup - allocate and setup an ntfs volume
1N/A * @dev: device to open
1N/A * @flags: optional mount flags
1N/A *
1N/A * Load, verify, and parse bootsector; load and setup $MFT and $MFTMirr. After
1N/A * calling this function, the volume is setup sufficiently to call all read
1N/A * and write access functions provided by the library.
1N/A *
1N/A * Return the allocated volume structure on success and NULL on error with
1N/A * errno set to the error code.
1N/A */
1N/Antfs_volume *ntfs_volume_startup(struct ntfs_device *dev,
1N/A ntfs_mount_flags flags)
1N/A{
1N/A LCN mft_zone_size, mft_lcn;
1N/A s64 br;
1N/A ntfs_volume *vol;
1N/A NTFS_BOOT_SECTOR *bs;
1N/A int eo;
1N/A#ifdef DEBUG
1N/A const char *OK = "OK\n";
1N/A const char *FAILED = "FAILED\n";
1N/A BOOL debug = 1;
1N/A#else
1N/A BOOL debug = 0;
1N/A#endif
1N/A
1N/A if (!dev || !dev->d_ops || !dev->d_name) {
1N/A errno = EINVAL;
1N/A return NULL;
1N/A }
1N/A
1N/A if (!(bs = (NTFS_BOOT_SECTOR *)ntfs_malloc(sizeof(NTFS_BOOT_SECTOR))))
1N/A return NULL;
1N/A
1N/A /* Allocate the volume structure. */
1N/A vol = ntfs_volume_alloc();
1N/A if (!vol)
1N/A goto error_exit;
1N/A /* Create the default upcase table. */
1N/A vol->upcase_len = 65536;
1N/A vol->upcase = (ntfschar*)ntfs_malloc(vol->upcase_len *
1N/A sizeof(ntfschar));
1N/A if (!vol->upcase)
1N/A goto error_exit;
1N/A ntfs_upcase_table_build(vol->upcase,
1N/A vol->upcase_len * sizeof(ntfschar));
1N/A if (flags & NTFS_MNT_RDONLY)
1N/A NVolSetReadOnly(vol);
1N/A if (flags & NTFS_MNT_CASE_SENSITIVE)
1N/A NVolSetCaseSensitive(vol);
1N/A if (flags & NTFS_MNT_INTERIX)
1N/A NVolSetInterix(vol);
1N/A ntfs_log_debug("Reading bootsector... ");
1N/A if (dev->d_ops->open(dev, NVolReadOnly(vol) ? O_RDONLY :
1N/A ((flags & NTFS_MNT_NOT_EXCLUSIVE) ? O_RDWR :
1N/A (O_RDWR | O_EXCL)))) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_perror("Error opening partition device");
1N/A goto error_exit;
1N/A }
1N/A /* Attach the device to the volume. */
1N/A vol->u.dev = dev;
1N/A /* Now read the bootsector. */
1N/A br = ntfs_pread(dev, 0, sizeof(NTFS_BOOT_SECTOR), bs);
1N/A if (br != sizeof(NTFS_BOOT_SECTOR)) {
1N/A ntfs_log_debug(FAILED);
1N/A if (br != -1)
1N/A errno = EINVAL;
1N/A if (!br)
1N/A ntfs_log_debug("Error: partition is smaller than "
1N/A "bootsector size. Weird!\n");
1N/A else
1N/A ntfs_log_perror("Error reading bootsector");
1N/A goto error_exit;
1N/A }
1N/A ntfs_log_debug(OK);
1N/A if (!ntfs_boot_sector_is_ntfs(bs, !debug)) {
1N/A ntfs_log_debug("Error: %s is not a valid NTFS partition!\n",
1N/A dev->d_name);
1N/A errno = EINVAL;
1N/A goto error_exit;
1N/A }
1N/A if (ntfs_boot_sector_parse(vol, bs) < 0) {
1N/A ntfs_log_perror("Failed to parse ntfs bootsector");
1N/A goto error_exit;
1N/A }
1N/A free(bs);
1N/A bs = NULL;
1N/A /* Now set the device block size to the sector size. */
1N/A if (ntfs_device_block_size_set(vol->u.dev, vol->sector_size))
1N/A ntfs_log_debug("Failed to set the device block size to the "
1N/A "sector size. This may affect performance "
1N/A "but should be harmless otherwise. Error: "
1N/A "%s\n", strerror(errno));
1N/A /*
1N/A * We now initialize the cluster allocator.
1N/A *
1N/A * FIXME: Move this to its own function? (AIA)
1N/A */
1N/A
1N/A // TODO: Make this tunable at mount time. (AIA)
1N/A vol->mft_zone_multiplier = 1;
1N/A
1N/A /* Determine the size of the MFT zone. */
1N/A mft_zone_size = vol->nr_clusters;
1N/A switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */
1N/A case 4:
1N/A mft_zone_size >>= 1; /* 50% */
1N/A break;
1N/A case 3:
1N/A mft_zone_size = mft_zone_size * 3 >> 3; /* 37.5% */
1N/A break;
1N/A case 2:
1N/A mft_zone_size >>= 2; /* 25% */
1N/A break;
1N/A /* case 1: */
1N/A default:
1N/A mft_zone_size >>= 3; /* 12.5% */
1N/A break;
1N/A }
1N/A
1N/A /* Setup the mft zone. */
1N/A vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
1N/A ntfs_log_debug("mft_zone_pos = 0x%llx\n", (long long)vol->mft_zone_pos);
1N/A
1N/A /*
1N/A * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
1N/A * source) and if the actual mft_lcn is in the expected place or even
1N/A * further to the front of the volume, extend the mft_zone to cover the
1N/A * beginning of the volume as well. This is in order to protect the
1N/A * area reserved for the mft bitmap as well within the mft_zone itself.
1N/A * On non-standard volumes we don't protect it as the overhead would be
1N/A * higher than the speed increase we would get by doing it.
1N/A */
1N/A mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
1N/A if (mft_lcn * vol->cluster_size < 16 * 1024)
1N/A mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
1N/A vol->cluster_size;
1N/A if (vol->mft_zone_start <= mft_lcn)
1N/A vol->mft_zone_start = 0;
1N/A ntfs_log_debug("mft_zone_start = 0x%llx\n",
1N/A (long long)vol->mft_zone_start);
1N/A
1N/A /*
1N/A * Need to cap the mft zone on non-standard volumes so that it does
1N/A * not point outside the boundaries of the volume. We do this by
1N/A * halving the zone size until we are inside the volume.
1N/A */
1N/A vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
1N/A while (vol->mft_zone_end >= vol->nr_clusters) {
1N/A mft_zone_size >>= 1;
1N/A vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
1N/A }
1N/A ntfs_log_debug("mft_zone_end = 0x%llx\n", (long long)vol->mft_zone_end);
1N/A
1N/A /*
1N/A * Set the current position within each data zone to the start of the
1N/A * respective zone.
1N/A */
1N/A vol->data1_zone_pos = vol->mft_zone_end;
1N/A ntfs_log_debug("data1_zone_pos = 0x%llx\n", vol->data1_zone_pos);
1N/A vol->data2_zone_pos = 0;
1N/A ntfs_log_debug("data2_zone_pos = 0x%llx\n", vol->data2_zone_pos);
1N/A
1N/A /* Set the mft data allocation position to mft record 24. */
1N/A vol->mft_data_pos = 24;
1N/A
1N/A /*
1N/A * The cluster allocator is now fully operational.
1N/A */
1N/A
1N/A /* Need to setup $MFT so we can use the library read functions. */
1N/A ntfs_log_debug("Loading $MFT... ");
1N/A if (ntfs_mft_load(vol) < 0) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_perror("Failed to load $MFT");
1N/A goto error_exit;
1N/A }
1N/A ntfs_log_debug(OK);
1N/A
1N/A /* Need to setup $MFTMirr so we can use the write functions, too. */
1N/A ntfs_log_debug("Loading $MFTMirr... ");
1N/A if (ntfs_mftmirr_load(vol) < 0) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_perror("Failed to load $MFTMirr");
1N/A goto error_exit;
1N/A }
1N/A ntfs_log_debug(OK);
1N/A return vol;
1N/Aerror_exit:
1N/A eo = errno;
1N/A free(bs);
1N/A if (vol)
1N/A __ntfs_volume_release(vol);
1N/A errno = eo;
1N/A return NULL;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_volume_check_logfile - check logfile on target volume
1N/A * @vol: volume on which to check logfile
1N/A *
1N/A * Return 0 on success and -1 on error with errno set error code.
1N/A */
1N/Astatic int ntfs_volume_check_logfile(ntfs_volume *vol)
1N/A{
1N/A ntfs_inode *ni;
1N/A ntfs_attr *na = NULL;
1N/A RESTART_PAGE_HEADER *rp = NULL;
1N/A int err = 0;
1N/A
1N/A if ((ni = ntfs_inode_open(vol, FILE_LogFile)) == NULL) {
1N/A ntfs_log_debug("Failed to open inode FILE_LogFile.\n");
1N/A errno = EIO;
1N/A return -1;
1N/A }
1N/A if ((na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0)) == NULL) {
1N/A ntfs_log_debug("Failed to open $FILE_LogFile/$DATA\n");
1N/A err = EIO;
1N/A goto exit;
1N/A }
1N/A if (!ntfs_check_logfile(na, &rp) || !ntfs_is_logfile_clean(na, rp))
1N/A err = EOPNOTSUPP;
1N/A free(rp);
1N/Aexit:
1N/A if (na)
1N/A ntfs_attr_close(na);
1N/A ntfs_inode_close(ni);
1N/A if (err) {
1N/A errno = err;
1N/A return -1;
1N/A }
1N/A return 0;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_hiberfile_open - Find and open '/hiberfil.sys'
1N/A * @vol: An ntfs volume obtained from ntfs_mount
1N/A *
1N/A * Return: inode Success, hiberfil.sys is valid
1N/A * NULL hiberfil.sys doesn't exist or some other error occurred
1N/A */
1N/Astatic ntfs_inode *ntfs_hiberfile_open(ntfs_volume *vol)
1N/A{
1N/A u64 inode;
1N/A ntfs_inode *ni_root;
1N/A ntfs_inode *ni_hibr = NULL;
1N/A ntfschar *unicode = NULL;
1N/A int unicode_len;
1N/A const char *hiberfile = "hiberfil.sys";
1N/A
1N/A if (!vol) {
1N/A errno = EINVAL;
1N/A return NULL;
1N/A }
1N/A
1N/A ni_root = ntfs_inode_open(vol, FILE_root);
1N/A if (!ni_root) {
1N/A ntfs_log_debug("Couldn't open the root directory.\n");
1N/A return NULL;
1N/A }
1N/A
1N/A unicode_len = ntfs_mbstoucs(hiberfile, &unicode, 0);
1N/A if (unicode_len < 0) {
1N/A ntfs_log_perror("Couldn't convert 'hiberfil.sys' to Unicode");
1N/A goto out;
1N/A }
1N/A
1N/A inode = ntfs_inode_lookup_by_name(ni_root, unicode, unicode_len);
1N/A if (inode == (u64)-1) {
1N/A ntfs_log_debug("Couldn't find file '%s'.\n", hiberfile);
1N/A goto out;
1N/A }
1N/A
1N/A inode = MREF(inode);
1N/A ni_hibr = ntfs_inode_open(vol, inode);
1N/A if (!ni_hibr) {
1N/A ntfs_log_debug("Couldn't open inode %lld.\n", (long long)inode);
1N/A goto out;
1N/A }
1N/Aout:
1N/A ntfs_inode_close(ni_root);
1N/A free(unicode);
1N/A return ni_hibr;
1N/A}
1N/A
1N/A
1N/A#define NTFS_HIBERFILE_HEADER_SIZE 4096
1N/A
1N/A/**
1N/A * ntfs_volume_check_hiberfile - check hiberfil.sys whether Windows is
1N/A * hibernated on the target volume
1N/A * @vol: volume on which to check hiberfil.sys
1N/A *
1N/A * Return: 0 if Windows isn't hibernated for sure
1N/A * -1 otherwise and errno is set to the appropriate value
1N/A */
1N/Astatic int ntfs_volume_check_hiberfile(ntfs_volume *vol)
1N/A{
1N/A ntfs_inode *ni;
1N/A ntfs_attr *na = NULL;
1N/A int bytes_read, ret = -1;
1N/A char *buf = NULL;
1N/A
1N/A ni = ntfs_hiberfile_open(vol);
1N/A if (!ni) {
1N/A if (errno == ENOENT)
1N/A return 0;
1N/A return -1;
1N/A }
1N/A
1N/A buf = ntfs_malloc(NTFS_HIBERFILE_HEADER_SIZE);
1N/A if (!buf)
1N/A goto out;
1N/A
1N/A na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0);
1N/A if (!na) {
1N/A ntfs_log_perror("Failed to open hiberfil.sys data attribute");
1N/A goto out;
1N/A }
1N/A
1N/A bytes_read = ntfs_attr_pread(na, 0, NTFS_HIBERFILE_HEADER_SIZE, buf);
1N/A if (bytes_read == -1) {
1N/A ntfs_log_perror("Failed to read hiberfil.sys");
1N/A goto out;
1N/A }
1N/A if (bytes_read < NTFS_HIBERFILE_HEADER_SIZE) {
1N/A ntfs_log_debug("Hibernated non-system partition, refused to "
1N/A "mount!\n");
1N/A errno = EPERM;
1N/A goto out;
1N/A }
1N/A if (memcmp(buf, "hibr", 4) == 0) {
1N/A ntfs_log_debug("Windows is hibernated, refused to mount!\n");
1N/A errno = EPERM;
1N/A goto out;
1N/A }
1N/A ret = 0;
1N/Aout:
1N/A if (na)
1N/A ntfs_attr_close(na);
1N/A free(buf);
1N/A ntfs_inode_close(ni);
1N/A return ret;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_volume_get_nr_free_mft_records - calculate number of free MFT records
1N/A * vol: ntfs volume for which perform calculations.
1N/A *
1N/A * This function initializes @vol->nr_free_mft_records. @vol->mftbmp_na should
1N/A * be already opened upon call to this function.
1N/A *
1N/A * Return 0 on success. On error return -1 with errno set appropriately and
1N/A * @vol->nr_free_mft_records is not touched in this case.
1N/A */
1N/Astatic int ntfs_volume_get_nr_free_mft_records(ntfs_volume *vol)
1N/A{
1N/A long nr_free = vol->mft_na->data_size >> vol->mft_record_size_bits;
1N/A s64 br, total = 0;
1N/A u8 *buf;
1N/A
1N/A buf = ntfs_malloc(vol->cluster_size);
1N/A if (!buf)
1N/A return -1;
1N/A while (1) {
1N/A int i, j;
1N/A
1N/A br = ntfs_attr_pread(vol->mftbmp_na, total,
1N/A vol->cluster_size, buf);
1N/A if (br <= 0)
1N/A break;
1N/A total += br;
1N/A for (i = 0; i < br; i++)
1N/A for (j = 0; j < 8; j++)
1N/A if ((buf[i] >> j) & 1)
1N/A nr_free--;
1N/A }
1N/A free(buf);
1N/A if (!total || br < 0) {
1N/A ntfs_log_error("pread: %s\n", strerror(errno));
1N/A return -1;
1N/A }
1N/A vol->nr_free_mft_records = nr_free;
1N/A return 0;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_volume_get_nr_free_clusters - calculate number of free clusters
1N/A * vol: ntfs volume for which perform calculations.
1N/A *
1N/A * This function initializes @vol->nr_free_clusters. @vol->lcnbmp_na should be
1N/A * already opened upon call to this function.
1N/A *
1N/A * Return 0 on success. On error return -1 with errno set appropriately and
1N/A * @vol->nr_free_clusters is not touched in this case.
1N/A */
1N/Astatic long ntfs_volume_get_nr_free_clusters(ntfs_volume *vol)
1N/A{
1N/A long nr_free = vol->nr_clusters;
1N/A s64 br, total = 0;
1N/A u8 *buf;
1N/A
1N/A buf = ntfs_malloc(vol->cluster_size);
1N/A if (!buf)
1N/A return -1;
1N/A while (1) {
1N/A int i, j;
1N/A
1N/A br = ntfs_attr_pread(vol->lcnbmp_na, total,
1N/A vol->cluster_size, buf);
1N/A if (br <= 0)
1N/A break;
1N/A total += br;
1N/A for (i = 0; i < br; i++)
1N/A for (j = 0; j < 8; j++)
1N/A if ((buf[i] >> j) & 1)
1N/A nr_free--;
1N/A }
1N/A free(buf);
1N/A if (!total || br < 0) {
1N/A ntfs_log_error("pread: %s\n", strerror(errno));
1N/A return -1;
1N/A }
1N/A vol->nr_free_clusters = nr_free;
1N/A return 0;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_device_mount - open ntfs volume
1N/A * @dev: device to open
1N/A * @flags: optional mount flags
1N/A *
1N/A * This function mounts an ntfs volume. @dev should describe the device which
1N/A * to mount as the ntfs volume.
1N/A *
1N/A * @flags is an optional second parameter. Some flags are similar to flags used
1N/A * as for the mount system call (man 2 mount). Currently the following flags
1N/A * are implemented:
1N/A * NTFS_MNT_RDONLY - mount volume read-only
1N/A * NTFS_MNT_CASE_SENSITIVE - treat filenames as case sensitive even if
1N/A * they are not in POSIX namespace
1N/A * NTFS_MNT_NOT_EXCLUSIVE - (unix only) do not open volume exclusively
1N/A * NTFS_MNT_FORENSIC - mount for forensic purposes, i.e. do not do
1N/A * any writing at all during the mount, i.e. no
1N/A * journal emptying, no dirty bit setting, etc.
1N/A * NTFS_MNT_INTERIX - make libntfs recognize special Interix files
1N/A *
1N/A * The function opens the device @dev and verifies that it contains a valid
1N/A * bootsector. Then, it allocates an ntfs_volume structure and initializes
1N/A * some of the values inside the structure from the information stored in the
1N/A * bootsector. It proceeds to load the necessary system files and completes
1N/A * setting up the structure.
1N/A *
1N/A * Return the allocated volume structure on success and NULL on error with
1N/A * errno set to the error code.
1N/A */
1N/Antfs_volume *ntfs_device_mount(struct ntfs_device *dev, ntfs_mount_flags flags)
1N/A{
1N/A s64 l;
1N/A#ifdef DEBUG
1N/A const char *OK = "OK\n";
1N/A const char *FAILED = "FAILED\n";
1N/A#endif
1N/A ntfs_volume *vol;
1N/A u8 *m = NULL, *m2 = NULL;
1N/A ntfs_attr_search_ctx *ctx = NULL;
1N/A ntfs_inode *ni;
1N/A ntfs_attr *na;
1N/A ATTR_RECORD *a;
1N/A VOLUME_INFORMATION *vinf;
1N/A ntfschar *vname;
1N/A int i, j, eo;
1N/A u32 u;
1N/A
1N/A vol = ntfs_volume_startup(dev, flags);
1N/A if (!vol) {
1N/A ntfs_log_perror("Failed to startup volume");
1N/A return NULL;
1N/A }
1N/A /* Record whether this is a forensic mount. */
1N/A if (flags & NTFS_MNT_FORENSIC)
1N/A NVolSetForensicMount(vol);
1N/A /* Load data from $MFT and $MFTMirr and compare the contents. */
1N/A m = (u8*)ntfs_malloc(vol->mftmirr_size << vol->mft_record_size_bits);
1N/A m2 = (u8*)ntfs_malloc(vol->mftmirr_size << vol->mft_record_size_bits);
1N/A if (!m || !m2)
1N/A goto error_exit;
1N/A
1N/A l = ntfs_attr_mst_pread(vol->mft_na, 0, vol->mftmirr_size,
1N/A vol->mft_record_size, m);
1N/A if (l != vol->mftmirr_size) {
1N/A if (l == -1)
1N/A ntfs_log_perror("Failed to read $MFT");
1N/A else {
1N/A ntfs_log_debug("Failed to read $MFT, unexpected length "
1N/A "(%d != %lld).\n", vol->mftmirr_size, l);
1N/A errno = EIO;
1N/A }
1N/A goto error_exit;
1N/A }
1N/A l = ntfs_attr_mst_pread(vol->mftmirr_na, 0, vol->mftmirr_size,
1N/A vol->mft_record_size, m2);
1N/A if (l != vol->mftmirr_size) {
1N/A if (l == -1)
1N/A ntfs_log_perror("Failed to read $MFTMirr");
1N/A else {
1N/A ntfs_log_debug("Failed to read $MFTMirr, unexpected "
1N/A "length (%d != %lld).\n",
1N/A vol->mftmirr_size, l);
1N/A errno = EIO;
1N/A }
1N/A goto error_exit;
1N/A }
1N/A ntfs_log_debug("Comparing $MFTMirr to $MFT... ");
1N/A for (i = 0; i < vol->mftmirr_size; ++i) {
1N/A MFT_RECORD *mrec, *mrec2;
1N/A const char *ESTR[12] = { "$MFT", "$MFTMirr", "$LogFile",
1N/A "$Volume", "$AttrDef", "root directory", "$Bitmap",
1N/A "$Boot", "$BadClus", "$Secure", "$UpCase", "$Extend" };
1N/A const char *s;
1N/A
1N/A if (i < 12)
1N/A s = ESTR[i];
1N/A else if (i < 16)
1N/A s = "system file";
1N/A else
1N/A s = "mft record";
1N/A
1N/A mrec = (MFT_RECORD*)(m + i * vol->mft_record_size);
1N/A if (mrec->flags & MFT_RECORD_IN_USE) {
1N/A if (ntfs_is_baad_record(mrec->magic)) {
1N/A ntfs_log_debug("FAILED\n");
1N/A ntfs_log_debug("$MFT error: Incomplete multi "
1N/A "sector transfer detected in "
1N/A "%s.\n", s);
1N/A goto io_error_exit;
1N/A }
1N/A if (!ntfs_is_mft_record(mrec->magic)) {
1N/A ntfs_log_debug("FAILED\n");
1N/A ntfs_log_debug("$MFT error: Invalid mft "
1N/A "record for %s.\n", s);
1N/A goto io_error_exit;
1N/A }
1N/A }
1N/A mrec2 = (MFT_RECORD*)(m2 + i * vol->mft_record_size);
1N/A if (mrec2->flags & MFT_RECORD_IN_USE) {
1N/A if (ntfs_is_baad_record(mrec2->magic)) {
1N/A ntfs_log_debug("FAILED\n");
1N/A ntfs_log_debug("$MFTMirr error: Incomplete "
1N/A "multi sector transfer "
1N/A "detected in %s.\n", s);
1N/A goto io_error_exit;
1N/A }
1N/A if (!ntfs_is_mft_record(mrec2->magic)) {
1N/A ntfs_log_debug("FAILED\n");
1N/A ntfs_log_debug("$MFTMirr error: Invalid mft "
1N/A "record for %s.\n", s);
1N/A goto io_error_exit;
1N/A }
1N/A }
1N/A if (memcmp(mrec, mrec2, ntfs_mft_record_get_data_size(mrec))) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_debug("$MFTMirr does not match $MFT. Run "
1N/A "chkdsk.\n");
1N/A goto io_error_exit;
1N/A }
1N/A }
1N/A ntfs_log_debug(OK);
1N/A
1N/A free(m2);
1N/A free(m);
1N/A m = m2 = NULL;
1N/A
1N/A /* Now load the bitmap from $Bitmap. */
1N/A ntfs_log_debug("Loading $Bitmap... ");
1N/A vol->lcnbmp_ni = ntfs_inode_open(vol, FILE_Bitmap);
1N/A if (!vol->lcnbmp_ni) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_perror("Failed to open inode");
1N/A goto error_exit;
1N/A }
1N/A /* Get an ntfs attribute for $Bitmap/$DATA. */
1N/A vol->lcnbmp_na = ntfs_attr_open(vol->lcnbmp_ni, AT_DATA, AT_UNNAMED, 0);
1N/A if (!vol->lcnbmp_na) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_perror("Failed to open ntfs attribute");
1N/A goto error_exit;
1N/A }
1N/A /* Done with the $Bitmap mft record. */
1N/A ntfs_log_debug(OK);
1N/A
1N/A /* Now load the upcase table from $UpCase. */
1N/A ntfs_log_debug("Loading $UpCase... ");
1N/A ni = ntfs_inode_open(vol, FILE_UpCase);
1N/A if (!ni) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_perror("Failed to open inode");
1N/A goto error_exit;
1N/A }
1N/A /* Get an ntfs attribute for $UpCase/$DATA. */
1N/A na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0);
1N/A if (!na) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_perror("Failed to open ntfs attribute");
1N/A goto error_exit;
1N/A }
1N/A /*
1N/A * Note: Normally, the upcase table has a length equal to 65536
1N/A * 2-byte Unicode characters but allow for different cases, so no
1N/A * checks done. Just check we don't overflow 32-bits worth of Unicode
1N/A * characters.
1N/A */
1N/A if (na->data_size & ~0x1ffffffffULL) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_debug("Error: Upcase table is too big (max 32-bit "
1N/A "allowed).\n");
1N/A errno = EINVAL;
1N/A goto error_exit;
1N/A }
1N/A if (vol->upcase_len != na->data_size >> 1) {
1N/A vol->upcase_len = na->data_size >> 1;
1N/A /* Throw away default table. */
1N/A free(vol->upcase);
1N/A vol->upcase = (ntfschar*)ntfs_malloc(na->data_size);
1N/A if (!vol->upcase) {
1N/A ntfs_log_debug(FAILED);
1N/A goto error_exit;
1N/A }
1N/A }
1N/A /* Read in the $DATA attribute value into the buffer. */
1N/A l = ntfs_attr_pread(na, 0, na->data_size, vol->upcase);
1N/A if (l != na->data_size) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_debug("Amount of data read does not correspond to "
1N/A "expected length!\n");
1N/A errno = EIO;
1N/A goto error_exit;
1N/A }
1N/A /* Done with the $UpCase mft record. */
1N/A ntfs_log_debug(OK);
1N/A ntfs_attr_close(na);
1N/A if (ntfs_inode_close(ni))
1N/A ntfs_log_perror("Failed to close inode, leaking memory");
1N/A
1N/A /*
1N/A * Now load $Volume and set the version information and flags in the
1N/A * vol structure accordingly.
1N/A */
1N/A ntfs_log_debug("Loading $Volume... ");
1N/A vol->vol_ni = ntfs_inode_open(vol, FILE_Volume);
1N/A if (!vol->vol_ni) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_perror("Failed to open inode");
1N/A goto error_exit;
1N/A }
1N/A /* Get a search context for the $Volume/$VOLUME_INFORMATION lookup. */
1N/A ctx = ntfs_attr_get_search_ctx(vol->vol_ni, NULL);
1N/A if (!ctx) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_perror("Failed to allocate attribute search context");
1N/A goto error_exit;
1N/A }
1N/A /* Find the $VOLUME_INFORMATION attribute. */
1N/A if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, AT_UNNAMED, 0, 0, 0, NULL,
1N/A 0, ctx)) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_debug("$VOLUME_INFORMATION attribute not found in "
1N/A "$Volume?!?\n");
1N/A goto error_exit;
1N/A }
1N/A a = ctx->attr;
1N/A /* Has to be resident. */
1N/A if (a->non_resident) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_debug("Error: Attribute $VOLUME_INFORMATION must be "
1N/A "resident (and it isn't)!\n");
1N/A errno = EIO;
1N/A goto error_exit;
1N/A }
1N/A /* Get a pointer to the value of the attribute. */
1N/A vinf = (VOLUME_INFORMATION*)(le16_to_cpu(a->u.res.value_offset) + (char*)a);
1N/A /* Sanity checks. */
1N/A if ((char*)vinf + le32_to_cpu(a->u.res.value_length) > (char*)ctx->mrec +
1N/A le32_to_cpu(ctx->mrec->bytes_in_use) ||
1N/A le16_to_cpu(a->u.res.value_offset) + le32_to_cpu(
1N/A a->u.res.value_length) > le32_to_cpu(a->length)) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_debug("Error: Attribute $VOLUME_INFORMATION in "
1N/A "$Volume is corrupt!\n");
1N/A errno = EIO;
1N/A goto error_exit;
1N/A }
1N/A /* Setup vol from the volume information attribute value. */
1N/A vol->major_ver = vinf->major_ver;
1N/A vol->minor_ver = vinf->minor_ver;
1N/A /*
1N/A * Do not use le16_to_cpu() macro here as our VOLUME_FLAGS are defined
1N/A * using cpu_to_le16() macro and hence are consistent.
1N/A */
1N/A vol->flags = vinf->flags;
1N/A /* Record whether the volume was dirty or not. */
1N/A if (vol->flags & VOLUME_IS_DIRTY)
1N/A NVolSetWasDirty(vol);
1N/A /*
1N/A * Reinitialize the search context for the $Volume/$VOLUME_NAME lookup.
1N/A */
1N/A ntfs_attr_reinit_search_ctx(ctx);
1N/A if (ntfs_attr_lookup(AT_VOLUME_NAME, AT_UNNAMED, 0, 0, 0, NULL, 0,
1N/A ctx)) {
1N/A if (errno != ENOENT) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_debug("Error: Lookup of $VOLUME_NAME "
1N/A "attribute in $Volume failed. "
1N/A "This probably means something is "
1N/A "corrupt. Run chkdsk.\n");
1N/A goto error_exit;
1N/A }
1N/A /*
1N/A * Attribute not present. This has been seen in the field.
1N/A * Treat this the same way as if the attribute was present but
1N/A * had zero length.
1N/A */
1N/A vol->vol_name = ntfs_malloc(1);
1N/A if (!vol->vol_name) {
1N/A ntfs_log_debug(FAILED);
1N/A goto error_exit;
1N/A }
1N/A vol->vol_name[0] = '\0';
1N/A } else {
1N/A a = ctx->attr;
1N/A /* Has to be resident. */
1N/A if (a->non_resident) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_debug("Error: Attribute $VOLUME_NAME must be "
1N/A "resident!\n");
1N/A errno = EIO;
1N/A goto error_exit;
1N/A }
1N/A /* Get a pointer to the value of the attribute. */
1N/A vname = (ntfschar*)(le16_to_cpu(a->u.res.value_offset) + (char*)a);
1N/A u = le32_to_cpu(a->u.res.value_length) / 2;
1N/A /*
1N/A * Convert Unicode volume name to current locale multibyte
1N/A * format.
1N/A */
1N/A vol->vol_name = NULL;
1N/A if (ntfs_ucstombs(vname, u, &vol->vol_name, 0) == -1) {
1N/A ntfs_log_perror("Error: Volume name could not be "
1N/A "converted to current locale");
1N/A ntfs_log_debug("Forcing name into ASCII by replacing "
1N/A "non-ASCII characters with underscores.\n");
1N/A vol->vol_name = ntfs_malloc(u + 1);
1N/A if (!vol->vol_name) {
1N/A ntfs_log_debug(FAILED);
1N/A goto error_exit;
1N/A }
1N/A for (j = 0; j < (s32)u; j++) {
1N/A u16 uc = le16_to_cpu(vname[j]);
1N/A if (uc > 0xff)
1N/A uc = (u16)'_';
1N/A vol->vol_name[j] = (char)uc;
1N/A }
1N/A vol->vol_name[u] = 0;
1N/A }
1N/A }
1N/A ntfs_log_debug(OK);
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A ctx = NULL;
1N/A /* Now load the attribute definitions from $AttrDef. */
1N/A ntfs_log_debug("Loading $AttrDef... ");
1N/A ni = ntfs_inode_open(vol, FILE_AttrDef);
1N/A if (!ni) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_perror("Failed to open inode");
1N/A goto error_exit;
1N/A }
1N/A /* Get an ntfs attribute for $AttrDef/$DATA. */
1N/A na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0);
1N/A if (!na) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_perror("Failed to open ntfs attribute");
1N/A goto error_exit;
1N/A }
1N/A /* Check we don't overflow 32-bits. */
1N/A if (na->data_size > 0xffffffffLL) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_debug("Error: Attribute definition table is too big "
1N/A "(max 32-bit allowed).\n");
1N/A errno = EINVAL;
1N/A goto error_exit;
1N/A }
1N/A vol->attrdef_len = na->data_size;
1N/A vol->attrdef = (ATTR_DEF*)ntfs_malloc(na->data_size);
1N/A if (!vol->attrdef) {
1N/A ntfs_log_debug(FAILED);
1N/A goto error_exit;
1N/A }
1N/A /* Read in the $DATA attribute value into the buffer. */
1N/A l = ntfs_attr_pread(na, 0, na->data_size, vol->attrdef);
1N/A if (l != na->data_size) {
1N/A ntfs_log_debug(FAILED);
1N/A ntfs_log_debug("Amount of data read does not correspond to "
1N/A "expected length!\n");
1N/A errno = EIO;
1N/A goto error_exit;
1N/A }
1N/A /* Done with the $AttrDef mft record. */
1N/A ntfs_log_debug(OK);
1N/A ntfs_attr_close(na);
1N/A if (ntfs_inode_close(ni))
1N/A ntfs_log_perror("Failed to close inode, leaking memory");
1N/A /* Initialize number of free clusters and MFT records. */
1N/A if (ntfs_volume_get_nr_free_mft_records(vol)) {
1N/A ntfs_log_perror("Failed to calculate number of free MFTs");
1N/A goto error_exit;
1N/A }
1N/A if (ntfs_volume_get_nr_free_clusters(vol)) {
1N/A ntfs_log_perror("Failed to calculate number of free clusters");
1N/A goto error_exit;
1N/A }
1N/A /*
1N/A * Check for dirty logfile and hibernated Windows.
1N/A * We care only about read-write mounts.
1N/A *
1N/A * If all is ok, reset the logfile and set the dirty bit on the volume.
1N/A *
1N/A * But do not do that if this is a FORENSIC mount.
1N/A */
1N/A if (!(flags & NTFS_MNT_RDONLY)) {
1N/A if (ntfs_volume_check_hiberfile(vol) < 0)
1N/A goto error_exit;
1N/A if (ntfs_volume_check_logfile(vol) < 0) {
1N/A if (errno != EOPNOTSUPP || !(flags & NTFS_MNT_FORCE))
1N/A goto error_exit;
1N/A ntfs_log_warning("WARNING: $LogFile is not clean, "
1N/A "forced to continue.\n");
1N/A NVolSetWasDirty(vol); /* Leave volume dirty since we
1N/A empted logfile. */
1N/A }
1N/A if (!NVolForensicMount(vol)) {
1N/A if (ntfs_logfile_reset(vol) < 0)
1N/A goto error_exit;
1N/A if (!(vol->flags & VOLUME_IS_DIRTY)) {
1N/A vol->flags |= VOLUME_IS_DIRTY;
1N/A if (ntfs_volume_write_flags(vol, vol->flags) <
1N/A 0)
1N/A goto error_exit;
1N/A }
1N/A }
1N/A }
1N/A return vol;
1N/Aio_error_exit:
1N/A errno = EIO;
1N/Aerror_exit:
1N/A eo = errno;
1N/A if (ctx)
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A free(m);
1N/A free(m2);
1N/A __ntfs_volume_release(vol);
1N/A errno = eo;
1N/A return NULL;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_mount - open ntfs volume
1N/A * @name: name of device/file to open
1N/A * @flags: optional mount flags
1N/A *
1N/A * This function mounts an ntfs volume. @name should contain the name of the
1N/A * device/file to mount as the ntfs volume.
1N/A *
1N/A * @flags is an optional second parameter. See ntfs_device_mount comment for
1N/A * description.
1N/A *
1N/A * The function opens the device or file @name and verifies that it contains a
1N/A * valid bootsector. Then, it allocates an ntfs_volume structure and initializes
1N/A * some of the values inside the structure from the information stored in the
1N/A * bootsector. It proceeds to load the necessary system files and completes
1N/A * setting up the structure.
1N/A *
1N/A * Return the allocated volume structure on success and NULL on error with
1N/A * errno set to the error code.
1N/A *
1N/A * Note, that a copy is made of @name, and hence it can be discarded as
1N/A * soon as the function returns.
1N/A */
1N/Antfs_volume *ntfs_mount(const char *name __attribute__((unused)),
1N/A ntfs_mount_flags flags __attribute__((unused)))
1N/A{
1N/A#ifndef NO_NTFS_DEVICE_DEFAULT_IO_OPS
1N/A struct ntfs_device *dev;
1N/A ntfs_volume *vol;
1N/A
1N/A /* Allocate an ntfs_device structure. */
1N/A dev = ntfs_device_alloc(name, 0, &ntfs_device_default_io_ops, NULL);
1N/A if (!dev)
1N/A return NULL;
1N/A /* Call ntfs_device_mount() to do the actual mount. */
1N/A vol = ntfs_device_mount(dev, flags);
1N/A if (!vol) {
1N/A int eo = errno;
1N/A ntfs_device_free(dev);
1N/A errno = eo;
1N/A }
1N/A return vol;
1N/A#else
1N/A /*
1N/A * ntfs_mount() makes no sense if NO_NTFS_DEVICE_DEFAULT_IO_OPS is
1N/A * defined as there are no device operations available in libntfs in
1N/A * this case.
1N/A */
1N/A errno = EOPNOTSUPP;
1N/A return NULL;
1N/A#endif
1N/A}
1N/A
1N/A/**
1N/A * ntfs_device_umount - close ntfs volume
1N/A * @vol: address of ntfs_volume structure of volume to close
1N/A * @force: if true force close the volume even if it is busy
1N/A *
1N/A * Deallocate all structures (including @vol itself) associated with the ntfs
1N/A * volume @vol.
1N/A *
1N/A * Note it is up to the caller to destroy the device associated with the volume
1N/A * being unmounted after this function returns.
1N/A *
1N/A * Return 0 on success. On error return -1 with errno set appropriately
1N/A * (most likely to one of EAGAIN, EBUSY or EINVAL). The EAGAIN error means that
1N/A * an operation is in progress and if you try the close later the operation
1N/A * might be completed and the close succeed.
1N/A *
1N/A * If @force is true (i.e. not zero) this function will close the volume even
1N/A * if this means that data might be lost.
1N/A *
1N/A * @vol must have previously been returned by a call to ntfs_device_mount().
1N/A *
1N/A * @vol itself is deallocated and should no longer be dereferenced after this
1N/A * function returns success. If it returns an error then nothing has been done
1N/A * so it is safe to continue using @vol.
1N/A */
1N/Aint ntfs_device_umount(ntfs_volume *vol,
1N/A const BOOL force __attribute__((unused)))
1N/A{
1N/A if (!vol) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A __ntfs_volume_release(vol);
1N/A return 0;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_umount - close ntfs volume
1N/A * @vol: address of ntfs_volume structure of volume to close
1N/A * @force: if true force close the volume even if it is busy
1N/A *
1N/A * Deallocate all structures (including @vol itself) associated with the ntfs
1N/A * volume @vol.
1N/A *
1N/A * Return 0 on success. On error return -1 with errno set appropriately
1N/A * (most likely to one of EAGAIN, EBUSY or EINVAL). The EAGAIN error means that
1N/A * an operation is in progress and if you try the close later the operation
1N/A * might be completed and the close succeed.
1N/A *
1N/A * If @force is true (i.e. not zero) this function will close the volume even
1N/A * if this means that data might be lost.
1N/A *
1N/A * @vol must have previously been returned by a call to ntfs_mount().
1N/A *
1N/A * @vol itself is deallocated and should no longer be dereferenced after this
1N/A * function returns success. If it returns an error then nothing has been done
1N/A * so it is safe to continue using @vol.
1N/A */
1N/Aint ntfs_umount(ntfs_volume *vol,
1N/A const BOOL force __attribute__((unused)))
1N/A{
1N/A struct ntfs_device *dev;
1N/A
1N/A if (!vol) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A dev = vol->u.dev;
1N/A __ntfs_volume_release(vol);
1N/A ntfs_device_free(dev);
1N/A return 0;
1N/A}
1N/A
1N/A#ifdef HAVE_MNTENT_H
1N/A
1N/A#ifndef HAVE_REALPATH
1N/A/**
1N/A * realpath - If there is no realpath on the system
1N/A */
1N/Astatic char *realpath(const char *path, char *resolved_path)
1N/A{
1N/A strncpy(resolved_path, path, PATH_MAX);
1N/A resolved_path[PATH_MAX] = '\0';
1N/A return resolved_path;
1N/A}
1N/A#endif
1N/A
1N/A/**
1N/A * ntfs_mntent_check - desc
1N/A *
1N/A * If you are wanting to use this, you actually wanted to use
1N/A * ntfs_check_if_mounted(), you just didn't realize. (-:
1N/A *
1N/A * See description of ntfs_check_if_mounted(), below.
1N/A */
1N/Astatic int ntfs_mntent_check(const char *file, unsigned long *mnt_flags)
1N/A{
1N/A struct mntent *mnt;
1N/A char *real_file = NULL, *real_fsname = NULL;
1N/A FILE *f;
1N/A int err = 0;
1N/A
1N/A real_file = ntfs_malloc(PATH_MAX + 1);
1N/A if (!real_file)
1N/A return -1;
1N/A real_fsname = ntfs_malloc(PATH_MAX + 1);
1N/A if (!real_fsname) {
1N/A err = errno;
1N/A goto exit;
1N/A }
1N/A if (!realpath(file, real_file)) {
1N/A err = errno;
1N/A goto exit;
1N/A }
1N/A if (!(f = setmntent(MOUNTED, "r"))) {
1N/A err = errno;
1N/A goto exit;
1N/A }
1N/A while ((mnt = getmntent(f))) {
1N/A if (!realpath(mnt->mnt_fsname, real_fsname))
1N/A continue;
1N/A if (!strcmp(real_file, real_fsname))
1N/A break;
1N/A }
1N/A endmntent(f);
1N/A if (!mnt)
1N/A goto exit;
1N/A *mnt_flags = NTFS_MF_MOUNTED;
1N/A if (!strcmp(mnt->mnt_dir, "/"))
1N/A *mnt_flags |= NTFS_MF_ISROOT;
1N/A#ifdef HAVE_HASMNTOPT
1N/A if (hasmntopt(mnt, "ro") && !hasmntopt(mnt, "rw"))
1N/A *mnt_flags |= NTFS_MF_READONLY;
1N/A#endif
1N/Aexit:
1N/A free(real_file);
1N/A free(real_fsname);
1N/A if (err) {
1N/A errno = err;
1N/A return -1;
1N/A }
1N/A return 0;
1N/A}
1N/A#endif /* HAVE_MNTENT_H */
1N/A
1N/A/**
1N/A * ntfs_check_if_mounted - check if an ntfs volume is currently mounted
1N/A * @file: device file to check
1N/A * @mnt_flags: pointer into which to return the ntfs mount flags (see volume.h)
1N/A *
1N/A * If the running system does not support the {set,get,end}mntent() calls,
1N/A * just return 0 and set *@mnt_flags to zero.
1N/A *
1N/A * When the system does support the calls, ntfs_check_if_mounted() first tries
1N/A * to find the device @file in /etc/mtab (or wherever this is kept on the
1N/A * running system). If it is not found, assume the device is not mounted and
1N/A * return 0 and set *@mnt_flags to zero.
1N/A *
1N/A * If the device @file is found, set the NTFS_MF_MOUNTED flags in *@mnt_flags.
1N/A *
1N/A * Further if @file is mounted as the file system root ("/"), set the flag
1N/A * NTFS_MF_ISROOT in *@mnt_flags.
1N/A *
1N/A * Finally, check if the file system is mounted read-only, and if so set the
1N/A * NTFS_MF_READONLY flag in *@mnt_flags.
1N/A *
1N/A * On success return 0 with *@mnt_flags set to the ntfs mount flags.
1N/A *
1N/A * On error return -1 with errno set to the error code.
1N/A */
1N/Aint ntfs_check_if_mounted(const char *file __attribute__((unused)),
1N/A unsigned long *mnt_flags)
1N/A{
1N/A *mnt_flags = 0;
1N/A#ifdef HAVE_MNTENT_H
1N/A return ntfs_mntent_check(file, mnt_flags);
1N/A#else
1N/A return 0;
1N/A#endif
1N/A}
1N/A
1N/A/**
1N/A * ntfs_version_is_supported - check if NTFS version is supported.
1N/A * @vol: ntfs volume whose version we're interested in.
1N/A *
1N/A * The function checks if the NTFS volume version is known or not.
1N/A * Version 1.1 and 1.2 are used by Windows NT3.x and NT4.
1N/A * Version 2.x is used by Windows 2000 Betas.
1N/A * Version 3.0 is used by Windows 2000.
1N/A * Version 3.1 is used by Windows XP, Windows Server 2003 and Vista.
1N/A *
1N/A * Return 0 if NTFS version is supported otherwise -1 with errno set.
1N/A *
1N/A * The following error codes are defined:
1N/A * EOPNOTSUPP - Unknown NTFS version
1N/A * EINVAL - Invalid argument
1N/A */
1N/Aint ntfs_version_is_supported(ntfs_volume *vol)
1N/A{
1N/A u8 major, minor;
1N/A
1N/A if (!vol) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A
1N/A major = vol->major_ver;
1N/A minor = vol->minor_ver;
1N/A
1N/A if (NTFS_V1_1(major, minor) || NTFS_V1_2(major, minor))
1N/A return 0;
1N/A
1N/A if (NTFS_V2_X(major, minor))
1N/A return 0;
1N/A
1N/A if (NTFS_V3_0(major, minor) || NTFS_V3_1(major, minor))
1N/A return 0;
1N/A
1N/A errno = EOPNOTSUPP;
1N/A return -1;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_logfile_reset - "empty" $LogFile data attribute value
1N/A * @vol: ntfs volume whose $LogFile we intend to reset.
1N/A *
1N/A * Fill the value of the $LogFile data attribute, i.e. the contents of
1N/A * the file, with 0xff's, thus marking the journal as empty.
1N/A *
1N/A * FIXME(?): We might need to zero the LSN field of every single mft
1N/A * record as well. (But, first try without doing that and see what
1N/A * happens, since chkdsk might pickup the pieces and do it for us...)
1N/A *
1N/A * On success return 0.
1N/A *
1N/A * On error return -1 with errno set to the error code.
1N/A */
1N/Aint ntfs_logfile_reset(ntfs_volume *vol)
1N/A{
1N/A ntfs_inode *ni;
1N/A ntfs_attr *na;
1N/A int eo;
1N/A
1N/A if (!vol) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A
1N/A if ((ni = ntfs_inode_open(vol, FILE_LogFile)) == NULL) {
1N/A ntfs_log_perror("Failed to open inode FILE_LogFile.");
1N/A return -1;
1N/A }
1N/A
1N/A if ((na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0)) == NULL) {
1N/A eo = errno;
1N/A ntfs_log_perror("Failed to open $FILE_LogFile/$DATA");
1N/A goto error_exit;
1N/A }
1N/A
1N/A if (ntfs_empty_logfile(na)) {
1N/A eo = errno;
1N/A ntfs_log_perror("Failed to empty $FILE_LogFile/$DATA");
1N/A ntfs_attr_close(na);
1N/A goto error_exit;
1N/A }
1N/A ntfs_attr_close(na);
1N/A return ntfs_inode_close(ni);
1N/A
1N/Aerror_exit:
1N/A ntfs_inode_close(ni);
1N/A errno = eo;
1N/A return -1;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_volume_write_flags - set the flags of an ntfs volume
1N/A * @vol: ntfs volume where we set the volume flags
1N/A * @flags: new flags
1N/A *
1N/A * Set the on-disk volume flags in the mft record of $Volume and
1N/A * on volume @vol to @flags.
1N/A *
1N/A * Return 0 if successful and -1 if not with errno set to the error code.
1N/A */
1N/Aint ntfs_volume_write_flags(ntfs_volume *vol, const le16 flags)
1N/A{
1N/A ATTR_RECORD *a;
1N/A VOLUME_INFORMATION *c;
1N/A ntfs_attr_search_ctx *ctx;
1N/A int ret = -1; /* failure */
1N/A
1N/A if (!vol || !vol->vol_ni) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A /* Get a pointer to the volume information attribute. */
1N/A ctx = ntfs_attr_get_search_ctx(vol->vol_ni, NULL);
1N/A if (!ctx) {
1N/A ntfs_log_perror("Failed to allocate attribute search context");
1N/A return -1;
1N/A }
1N/A if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, AT_UNNAMED, 0, 0, 0, NULL,
1N/A 0, ctx)) {
1N/A ntfs_log_error("Attribute $VOLUME_INFORMATION was not found "
1N/A "in $Volume!\n");
1N/A goto err_out;
1N/A }
1N/A a = ctx->attr;
1N/A /* Sanity check. */
1N/A if (a->non_resident) {
1N/A ntfs_log_error("Attribute $VOLUME_INFORMATION must be "
1N/A "resident (and it isn't)!\n");
1N/A errno = EIO;
1N/A goto err_out;
1N/A }
1N/A /* Get a pointer to the value of the attribute. */
1N/A c = (VOLUME_INFORMATION*)(le16_to_cpu(a->u.res.value_offset) + (char*)a);
1N/A /* Sanity checks. */
1N/A if ((char*)c + le32_to_cpu(a->u.res.value_length) > (char*)ctx->mrec +
1N/A le32_to_cpu(ctx->mrec->bytes_in_use) ||
1N/A le16_to_cpu(a->u.res.value_offset) +
1N/A le32_to_cpu(a->u.res.value_length) > le32_to_cpu(a->length)) {
1N/A ntfs_log_error("Attribute $VOLUME_INFORMATION in $Volume is "
1N/A "corrupt!\n");
1N/A errno = EIO;
1N/A goto err_out;
1N/A }
1N/A /* Set the volume flags. */
1N/A vol->flags = c->flags = flags & VOLUME_FLAGS_MASK;
1N/A /* Write them to disk. */
1N/A ntfs_inode_mark_dirty(vol->vol_ni);
1N/A if (ntfs_inode_sync(vol->vol_ni)) {
1N/A ntfs_log_perror("Error writing $Volume");
1N/A goto err_out;
1N/A }
1N/A ret = 0; /* success */
1N/Aerr_out:
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A if (ret)
1N/A ntfs_log_error("%s(): Failed.\n", "ntfs_volume_write_flags");
1N/A return ret;
1N/A}
1N/A