1N/A * volume.c - NTFS volume handling code. Part of the Linux-NTFS project. 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 * 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 * 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 * 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 * ntfs_volume_alloc - Create an NTFS volume object and initialise it 1N/A * __ntfs_volume_release - Destroy an NTFS volume object 1N/A /* Sync and print error about not detached inodes. */ 1N/A "references.\n",
"__ntfs_volume_release",
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 * ntfs_mft_load - load the $MFT and setup the ntfs volume with it 1N/A * @vol: ntfs volume whose $MFT to load 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 * Return 0 on success and -1 on error with errno set to the error code. 1N/A /* Manually setup an ntfs_inode. */ 1N/A /* Can't use any of the higher level functions yet! */ 1N/A /* Find the $ATTRIBUTE_LIST attribute in $MFT if present. */ 1N/A if (l <= 0 || l >
0x40000) {
1N/A "$MFT/$ATTRIBUTE_LIST.\n");
1N/A "reading $MFT/$ATTRIBUTE_LIST.\n");
1N/A /* Receive attributes from STANDARD_INFORMATION. */ 1N/A /* We now have a fully setup ntfs inode for $MFT in vol->mft_ni. */ 1N/A /* Get an ntfs attribute for $MFT/$DATA and set it up, too. */ 1N/A /* Read all extents from the $DATA attribute in $MFT. */ 1N/A /* $MFT must be non-resident. */ 1N/A "resident extent was found. $MFT is " 1N/A "corrupt. Run chkdsk.\n");
1N/A /* $MFT must be uncompressed and unencrypted. */ 1N/A " extent was found. $MFT is corrupt. " 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 /* Get the lowest vcn for the next extent. */ 1N/A /* Only one extent or error, which we catch below. */ 1N/A /* Avoid endless loops due to corruption. */ 1N/A "attribute. Run chkdsk.\n");
1N/A "$MFT is corrupt. Run chkdsk.\n");
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 /* Done with the $Mft mft record. */ 1N/A * The volume is now setup so we can use all read access functions. 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 * 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 * Return 0 on success and -1 on error with errno set to the error code. 1N/A /* Get an ntfs attribute for $MFTMirr/$DATA, too. */ 1N/A /* Check $MFTMirr runlist. */ 1N/A "are fragmented. Run chkdsk.\n");
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 * 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 * Return the allocated volume structure on success and NULL on error with 1N/A * errno set to the error code. 1N/A /* Allocate the volume structure. */ 1N/A /* Create the default upcase table. */ 1N/A /* Attach the device to the volume. */ 1N/A /* Now read the bootsector. */ 1N/A "bootsector size. Weird!\n");
1N/A /* Now set the device block size to the sector size. */ 1N/A "sector size. This may affect performance " 1N/A "but should be harmless otherwise. Error: " 1N/A * We now initialize the cluster allocator. 1N/A * FIXME: Move this to its own function? (AIA) 1N/A // TODO: Make this tunable at mount time. (AIA) 1N/A /* Determine the size of the MFT zone. */ 1N/A /* Setup the mft zone. */ 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 * 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 * Set the current position within each data zone to the start of the 1N/A /* Set the mft data allocation position to mft record 24. */ 1N/A * The cluster allocator is now fully operational. 1N/A /* Need to setup $MFT so we can use the library read functions. */ 1N/A /* Need to setup $MFTMirr so we can use the write functions, too. */ 1N/A * ntfs_volume_check_logfile - check logfile on target volume 1N/A * @vol: volume on which to check logfile 1N/A * Return 0 on success and -1 on error with errno set error code. 1N/A * @vol: An ntfs volume obtained from ntfs_mount 1N/A * ntfs_volume_check_hiberfile - check hiberfil.sys whether Windows is 1N/A * hibernated on the target volume 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 * ntfs_volume_get_nr_free_mft_records - calculate number of free MFT records 1N/A * vol: ntfs volume for which perform calculations. 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 * 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 for (j = 0; j <
8; j++)
1N/A * ntfs_volume_get_nr_free_clusters - calculate number of free clusters 1N/A * vol: ntfs volume for which perform calculations. 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 * 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 for (j = 0; j <
8; j++)
1N/A * ntfs_device_mount - open ntfs volume 1N/A * @dev: device to open 1N/A * @flags: optional mount flags 1N/A * This function mounts an ntfs volume. @dev should describe the device which 1N/A * to mount as the ntfs volume. 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 * 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 * 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 * Return the allocated volume structure on success and NULL on error with 1N/A * errno set to the error code. 1N/A /* Record whether this is a forensic mount. */ 1N/A /* Load data from $MFT and $MFTMirr and compare the contents. */ 1N/A "length (%d != %lld).\n",
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 "sector transfer detected in " 1N/A "record for %s.\n", s);
1N/A "multi sector transfer " 1N/A "detected in %s.\n", s);
1N/A "record for %s.\n", s);
1N/A /* Now load the bitmap from $Bitmap. */ 1N/A /* Get an ntfs attribute for $Bitmap/$DATA. */ 1N/A /* Done with the $Bitmap mft record. */ 1N/A /* Now load the upcase table from $UpCase. */ 1N/A /* Get an ntfs attribute for $UpCase/$DATA. */ 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 /* Throw away default table. */ 1N/A /* Read in the $DATA attribute value into the buffer. */ 1N/A "expected length!\n");
1N/A /* Done with the $UpCase mft record. */ 1N/A * Now load $Volume and set the version information and flags in the 1N/A * vol structure accordingly. 1N/A /* Get a search context for the $Volume/$VOLUME_INFORMATION lookup. */ 1N/A /* Find the $VOLUME_INFORMATION attribute. */ 1N/A /* Has to be resident. */ 1N/A "resident (and it isn't)!\n");
1N/A /* Get a pointer to the value of the attribute. */ 1N/A /* Sanity checks. */ 1N/A "$Volume is corrupt!\n");
1N/A /* Setup vol from the volume information attribute value. */ 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 /* Record whether the volume was dirty or not. */ 1N/A * Reinitialize the search context for the $Volume/$VOLUME_NAME lookup. 1N/A "attribute in $Volume failed. " 1N/A "This probably means something is " 1N/A "corrupt. Run chkdsk.\n");
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 /* Has to be resident. */ 1N/A /* Get a pointer to the value of the attribute. */ 1N/A * Convert Unicode volume name to current locale multibyte 1N/A "converted to current locale");
1N/A "non-ASCII characters with underscores.\n");
1N/A for (j = 0; j < (
s32)u; j++) {
1N/A /* Now load the attribute definitions from $AttrDef. */ 1N/A /* Get an ntfs attribute for $AttrDef/$DATA. */ 1N/A /* Check we don't overflow 32-bits. */ 1N/A "(max 32-bit allowed).\n");
1N/A /* Read in the $DATA attribute value into the buffer. */ 1N/A "expected length!\n");
1N/A /* Done with the $AttrDef mft record. */ 1N/A /* Initialize number of free clusters and MFT records. */ 1N/A * Check for dirty logfile and hibernated Windows. 1N/A * We care only about read-write mounts. 1N/A * If all is ok, reset the logfile and set the dirty bit on the volume. 1N/A * But do not do that if this is a FORENSIC mount. 1N/A "forced to continue.\n");
1N/A * ntfs_mount - open ntfs volume 1N/A * @flags: optional mount flags 1N/A * This function mounts an ntfs volume. @name should contain the name of the 1N/A * @flags is an optional second parameter. See ntfs_device_mount comment for 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 * Return the allocated volume structure on success and NULL on error with 1N/A * errno set to the error code. 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 /* Allocate an ntfs_device structure. */ 1N/A /* Call ntfs_device_mount() to do the actual mount. */ 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 * 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 * Deallocate all structures (including @vol itself) associated with the ntfs 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 * 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 * 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 * @vol must have previously been returned by a call to ntfs_device_mount(). 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 * 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 * Deallocate all structures (including @vol itself) associated with the ntfs 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 * 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 * @vol must have previously been returned by a call to ntfs_mount(). 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 * realpath - If there is no realpath on the system 1N/A * ntfs_mntent_check - desc 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 * See description of ntfs_check_if_mounted(), below. 1N/A#
endif /* HAVE_MNTENT_H */ 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 * 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 * 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 * If the device @file is found, set the NTFS_MF_MOUNTED flags in *@mnt_flags. 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 * 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 * On success return 0 with *@mnt_flags set to the ntfs mount flags. 1N/A * On error return -1 with errno set to the error code. 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 * 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 * Return 0 if NTFS version is supported otherwise -1 with errno set. 1N/A * The following error codes are defined: 1N/A * EOPNOTSUPP - Unknown NTFS version 1N/A * EINVAL - Invalid argument 1N/A * ntfs_logfile_reset - "empty" $LogFile data attribute value 1N/A * @vol: ntfs volume whose $LogFile we intend to reset. 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 * 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 * On success return 0. 1N/A * On error return -1 with errno set to the error code. 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 * Set the on-disk volume flags in the mft record of $Volume and 1N/A * on volume @vol to @flags. 1N/A * Return 0 if successful and -1 if not with errno set to the error code. 1N/A /* Get a pointer to the volume information attribute. */ 1N/A "resident (and it isn't)!\n");
1N/A /* Get a pointer to the value of the attribute. */ 1N/A /* Sanity checks. */ 1N/A /* Set the volume flags. */ 1N/A /* Write them to disk. */