/**
* inode.c - Inode handling code. Part of the Linux-NTFS project.
*
* Copyright (c) 2002-2005 Anton Altaparmakov
* Copyright (c) 2004-2007 Yura Pakhuchiy
* Copyright (c) 2004-2005 Richard Russon
*
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (in the main directory of the Linux-NTFS
* distribution in the file COPYING); if not, write to the Free Software
* Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include "compat.h"
#include "types.h"
#include "attrib.h"
#include "inode.h"
#include "debug.h"
#include "mft.h"
#include "attrlist.h"
#include "runlist.h"
#include "lcnalloc.h"
#include "index.h"
#include "dir.h"
#include "ntfstime.h"
#include "logging.h"
/**
* __ntfs_inode_allocate - Create and initialise an NTFS inode object
* @vol:
*
* Description...
*
* Returns:
*/
{
if (ni) {
}
return ni;
}
/**
* ntfs_inode_allocate - Create an NTFS inode object
* @vol:
*
* Description...
*
* Returns:
*/
{
return __ntfs_inode_allocate(vol);
}
/**
* __ntfs_inode_release - Destroy an NTFS inode object
* @ni:
*
* Description...
*
* Returns:
*/
{
ntfs_log_debug("Eeek. Discarding dirty inode!\n");
return 0;
}
/**
* __ntfs_inode_add_to_cache - do not use me! Only for internal library use.
*/
{
}
/**
* ntfs_inode_open - open an inode ready for access
* @vol: volume to get the inode from
* @mref: inode number / mft record number to open
*
* Allocate an ntfs_inode structure and initialize it for the given inode
* specified by @mref. @mref specifies the inode number / mft record to read,
* including the sequence number, which can be 0 if no sequence number checking
* is to be performed.
*
* Then, allocate a buffer for the mft record, read the mft record from the
* volume @vol, and attach it to the ntfs_inode structure (->mrec). The
* mft record is mst deprotected and sanity checked for validity and we abort
* if deprotection or checks fail.
*
* Finally, search for an attribute list attribute in the mft record and if one
* is found, load the attribute list attribute value and attach it to the
* ntfs_inode structure (->attr_list). Also set the NI_AttrList bit to indicate
* this.
*
* Return a pointer to the ntfs_inode structure on success or NULL on error,
* with errno set to the error code.
*/
{
s64 l;
int err = 0;
if (!vol) {
return NULL;
}
/* Check cache, maybe this inode already opened? */
ntfs_log_trace("Found this inode in cache, increment "
"reference count and return it.\n");
tmp_ni->nr_references++;
return tmp_ni;
}
}
/* Search failed. Properly open inode. */
if (!ni)
return NULL;
goto err_out;
goto err_out;
}
if (!ctx)
goto err_out;
/* Receive some basic information about inode. */
ntfs_log_trace("Failed to receive STANDARD_INFORMATION "
"attribute.\n");
goto put_err_out;
}
/* Set attribute list information. */
ctx)) {
goto put_err_out;
/* Attribute list attribute does not present. */
goto get_size;
}
if (!l)
goto put_err_out;
if (l > 0x40000) {
goto put_err_out;
}
ni->attr_list_size = l;
goto put_err_out;
if (!l)
goto put_err_out;
if (l != ni->attr_list_size) {
goto put_err_out;
}
goto put_err_out;
/* Directory or special file. */
} else {
else
} else {
}
}
return ni;
if (!err)
if (!err)
return NULL;
}
/**
* ntfs_inode_close - close an ntfs inode and free all associated memory
* @ni: ntfs inode to close
*
* Make sure the ntfs inode @ni is clean.
*
* If the ntfs inode @ni is a base inode, close all associated extent inodes,
* then deallocate all memory attached to it, and finally free the ntfs inode
* structure itself.
*
* If it is an extent inode, we disconnect it from its base inode before we
* destroy it.
*
* It is OK to pass NULL to this function, it is just noop in this case.
*
* Return 0 on success or -1 on error with errno set to the error code. On
* error, @ni has not been freed. The user should attempt to handle the error
* and call ntfs_inode_close() again. The following error codes are defined:
*
* EINVAL @ni is invalid (probably it is an extent inode).
* EIO I/O error while trying to write inode to disk.
*/
{
if (!ni)
return 0;
/* Decrement number of users. If there are left then just return. */
ni->nr_references--;
if (ni->nr_references) {
ntfs_log_trace("There are %d more references left to "
"this inode.\n",
ni->nr_references);
return 0;
} else
ntfs_log_trace("There are no more references left to "
"this inode.\n");
}
/* Check whether all attributes of this inode are closed. */
ntfs_log_error("%s(): Not all attributes are closed. "
"We definitely have memory leak. "
"Continue anyway.\n", "ntfs_inode_close");
/* If we have dirty metadata, write it out. */
if (ntfs_inode_sync(ni)) {
return -1;
}
}
/* Is this a base inode with mapped extent inodes? */
if (ni->nr_extents > 0) {
while (ni->nr_extents > 0) {
return -1;
}
}
s32 i;
/*
* If the inode is an extent inode, disconnect it from the
* base inode before destroying it.
*/
for (i = 0; i < base_ni->nr_extents; ++i) {
continue;
/* Found it. Disconnect. */
sizeof(ntfs_inode *));
/* Buffer should be for multiple of four extents. */
i = -1;
break;
}
/*
* ElectricFence is unhappy with realloc(x,0) as free(x)
* thus we explicitly separate these two cases.
*/
if (base_ni->nr_extents) {
/* Resize the memory buffer. */
sizeof(ntfs_inode *));
/* Ignore errors, they don't really matter. */
if (tmp_nis)
} else if (tmp_nis)
/* Allow for error checking. */
i = -1;
break;
}
if (i != -1)
ntfs_log_debug("Extent inode was not attached to base "
"inode! Continuing regardless.\n");
}
/* Remove inode from the list of opened inodes. */
return __ntfs_inode_release(ni);
}
/**
* ntfs_extent_inode_open - load an extent inode and attach it to its base
* @base_ni: base ntfs inode
* @mref: mft reference of the extent inode to load (in little endian)
*
* First check if the extent inode @mref is already attached to the base ntfs
* inode @base_ni, and if so, return a pointer to the attached extent inode.
*
* If the extent inode is not already attached to the base inode, allocate an
* ntfs_inode structure and initialize it for the given inode @mref. @mref
* specifies the inode number / mft record to read, including the sequence
* number, which can be 0 if no sequence number checking is to be performed.
*
* Then, allocate a buffer for the mft record, read the mft record from the
* volume @base_ni->vol, and attach it to the ntfs_inode structure (->mrec).
* The mft record is mst deprotected and sanity checked for validity and we
* abort if deprotection or checks fail.
*
* Finally attach the ntfs inode to its base inode @base_ni and return a
* pointer to the ntfs_inode structure on success or NULL on error, with errno
* set to the error code.
*
* Note, extent inodes are never closed directly. They are automatically
* disposed off by the closing of the base inode.
*/
{
int i;
if (!base_ni) {
return NULL;
}
ntfs_log_trace("Opening extent inode 0x%llx "
"(base MFT record 0x%llx).\n",
(unsigned long long)mft_no,
/* Is the extent inode already open and attached to the base inode? */
if (base_ni->nr_extents > 0) {
for (i = 0; i < base_ni->nr_extents; i++) {
ni = extent_nis[i];
continue;
/* Verify the sequence number if given. */
ntfs_log_debug("Found stale extent mft "
"reference! Corrupt file "
"system. Run chkdsk.\n");
return NULL;
}
/* We are done, return the extent inode. */
return ni;
}
}
/* Wasn't there, we need to load the extent inode. */
if (!ni)
return NULL;
NULL))
goto err_out;
/* Attach extent inode to base inode, reallocating memory if needed. */
if (!extent_nis)
goto err_out;
if (base_ni->nr_extents) {
i - 4 * sizeof(ntfs_inode *));
}
}
return ni;
i = errno;
errno = i;
ntfs_log_perror("Failed to open extent inode");
return NULL;
}
/**
* ntfs_inode_attach_all_extents - attach all extents for target inode
* @ni: opened ntfs inode for which perform attach
*
* Return 0 on success and -1 on error with errno set to the error code.
*/
{
if (!ni) {
ntfs_log_trace("Invalid arguments.\n");
return -1;
}
/* Inode haven't got attribute list, thus nothing to attach. */
if (!NInoAttrList(ni))
return 0;
ntfs_log_trace("Corrupted in-memory structure.\n");
return -1;
}
/* Walk through attribute list and attach all extents. */
errno = 0;
ntfs_log_trace("Couldn't attach extent "
"inode (attr type 0x%x "
"references to it).\n",
return -1;
}
}
}
return 0;
}
/**
* ntfs_inode_sync_standard_information - update standard information attribute
* @ni: ntfs inode to update standard information
*
* Return 0 on success or -1 on error with errno set to the error code.
*/
{
int err;
if (!ctx)
return -1;
ntfs_log_trace("Failed to receive STANDARD_INFORMATION "
"attribute.\n");
return -1;
}
return 0;
}
/**
* ntfs_inode_sync_file_name - update FILE_NAME attributes
* @ni: ntfs inode to update FILE_NAME attributes
*
* Update all FILE_NAME attributes for inode @ni in the index.
*
* Return 0 on success or -1 on error with errno set to the error code.
*/
{
int err = 0;
if (!ctx) {
ntfs_log_trace("Failed to get attribute search context.\n");
goto err_out;
}
/* Walk through all FILE_NAME attributes and update them. */
/*
* WARNING: We cheater here and obtain 2 attribute
* search contexts for one inode (first we obtained
* above, second will be obtained inside
* ntfs_index_lookup), it's acceptable for library,
* but will lock kernel.
*/
} else
if (!index_ni) {
if (!err)
ntfs_log_trace("Failed to open inode with index.\n");
continue;
}
if (!ictx) {
if (!err)
ntfs_log_trace("Failed to get index context.\n");
continue;
}
if (!err) {
else
}
ntfs_log_trace("Index lookup failed.\n");
continue;
}
/* Update flags and file size. */
}
/* Check for real error occurred. */
ntfs_log_trace("Attribute lookup failed.\n");
goto err_out;
}
if (err) {
return -1;
}
return 0;
if (ctx)
return -1;
}
/**
* ntfs_inode_sync - write the inode (and its dirty extents) to disk
* @ni: ntfs inode to write
*
* Write the inode @ni to disk as well as its dirty extent inodes if such
* exist and @ni is a base inode. If @ni is an extent inode, only @ni is
* written completely disregarding its base inode and any other extent inodes.
*
* For a base inode with dirty extent inodes if any writes fail for whatever
* reason, the failing inode is skipped and the sync process is continued. At
* the end the error condition that brought about the failure is returned. Thus
* the smallest amount of data loss possible occurs.
*
* Return 0 on success or -1 on error with errno set to the error code.
* The following error codes are defined:
* EINVAL - Invalid arguments were passed to the function.
* EIO - I/O error while writing the inode (or one of its extents).
*/
{
int err = 0;
if (!ni) {
return -1;
}
/* Update FILE_NAME's in the index. */
}
ntfs_log_trace("Failed to sync FILE_NAME attributes.\n");
}
/* Write out attribute list from cache to disk. */
if (!na) {
ntfs_log_trace("Attribute list sync failed "
"(open failed).\n");
}
} else {
ni->attr_list_size) {
ntfs_log_trace("Attribute list "
"sync failed "
"(write).\n");
}
}
} else {
ntfs_log_trace("Attribute list sync failed "
"(invalid size).\n");
}
}
}
/* Write this inode out to the $MFT (and $MFTMirr if applicable). */
if (NInoTestAndClearDirty(ni)) {
/* Update STANDARD_INFORMATION. */
}
ntfs_log_trace("Failed to sync standard "
"information.\n");
}
/* Write MFT record. */
}
ntfs_log_trace("Base MFT record sync failed.\n");
}
}
/* If this is a base inode with extents write all dirty extents, too. */
if (ni->nr_extents > 0) {
s32 i;
for (i = 0; i < ni->nr_extents; ++i) {
if (NInoTestAndClearDirty(eni)) {
}
ntfs_log_trace("Extent MFT record sync "
"failed.\n");
}
}
}
}
if (!err)
return 0;
return -1;
}
/**
* ntfs_inode_add_attrlist - add attribute list to inode and fill it
* @ni: opened ntfs inode to which add attribute list
*
* Return 0 on success or -1 on error with errno set to the error code.
* The following error codes are defined:
* EINVAL - Invalid arguments were passed to the function.
* EEXIST - Attribute list already exist.
* ENOMEM - Not enough memory to perform add.
*/
{
int err;
if (!ni) {
ntfs_log_trace("Invalid arguments.\n");
return -1;
}
ntfs_log_trace("Inode already has got attribute list.\n");
return -1;
}
al_allocated = 0x40;
al_len = 0;
if (!al) {
ntfs_log_trace("Not enough memory.\n");
return -1;
}
/* Form attribute list. */
if (!ctx) {
ntfs_log_trace("Couldn't get search context.\n");
goto err_out;
}
/* Walk through all attributes. */
ntfs_log_trace("Attribute list already present.\n");
goto put_err_out;
}
/* Calculate new length of attribute list. */
/* Allocate more memory if needed. */
while (al_len > al_allocated) {
al_allocated += 0x40;
0x40)); /* Valgrind. */
if (!aln) {
ntfs_log_trace("Not enough memory.\n");
goto put_err_out;
}
}
/* Add attribute to attribute list. */
else
ale->lowest_vcn = 0;
}
/* Check for real error occurred. */
ntfs_log_trace("Attribute lookup failed.\n");
goto put_err_out;
}
/* Deallocate trailing memory. */
if (!aln) {
ntfs_log_trace("realloc() failed.\n");
goto put_err_out;
}
/* Set in-memory attribute list. */
/* Free space if there is not enough it for $ATTRIBUTE_LIST. */
if (ntfs_inode_free_space(ni,
/* Failed to free space. */
ntfs_log_trace("Failed to free space for "
"$ATTRIBUTE_LIST.\n");
goto rollback;
}
}
/* Add $ATTRIBUTE_LIST to mft record. */
ntfs_log_trace("Couldn't add $ATTRIBUTE_LIST to MFT record.\n");
goto rollback;
}
/* Resize it. */
if (!na) {
ntfs_log_trace("Failed to open just added $ATTRIBUTE_LIST.\n");
goto remove_attrlist_record;
}
ntfs_log_trace("Failed to resize just added $ATTRIBUTE_LIST.\n");
goto remove_attrlist_record;;
}
/* Done! */
return 0;
/* Prevent ntfs_attr_recorm_rm from freeing attribute list. */
/* Remove $ATTRIBUTE_LIST record. */
if (ntfs_attr_record_rm(ctx))
ntfs_log_trace("Rollback failed. Failed to remove attribute "
"list record.\n");
} else
ntfs_log_trace("Rollback failed. Couldn't find attribute list "
"record.\n");
/* Setup back in-memory runlist. */
/*
* Scan attribute list for attributes that placed not in the base MFT
* record and move them to it.
*/
ntfs_log_trace("Rollback failed. Couldn't "
"back attribute to base MFT record.\n");
} else
ntfs_log_trace("Rollback failed. ntfs_attr_lookup "
"failed.\n");
}
}
/* Remove in-memory attribute list. */
ni->attr_list_size = 0;
return -1;
}
/**
* ntfs_inode_free_space - free space in the MFT record of inode
* @ni: ntfs inode in which MFT record free space
* @size: amount of space needed to free
*
* Return 0 on success or -1 on error with errno set to the error code.
*/
{
ntfs_log_trace("Invalid arguments.\n");
return -1;
}
ntfs_log_trace("Entering for inode 0x%llx, size %d.\n",
return 0;
if (!ctx) {
ntfs_log_trace("Failed to get attribute search context.\n");
return -1;
}
/*
* Chkdsk complain if $STANDARD_INFORMATION is not in the base MFT
* record. FIXME: I'm not sure in this, need to recheck. For now simply
* do not move $STANDARD_INFORMATION at all.
*
* Also we can't move $ATTRIBUTE_LIST from base MFT_RECORD, so position
* search context on first attribute after $STANDARD_INFORMATION and
* $ATTRIBUTE_LIST.
*
* Why we reposition instead of simply skip this attributes during
* enumeration? Because in case we have got only in-memory attribute
* list ntfs_attr_lookup will fail when it will try to find
* $ATTRIBUTE_LIST.
*/
0, ctx)) {
ntfs_log_trace("Attribute lookup failed.\n");
goto put_err_out;
}
goto put_err_out;
}
}
while (1) {
int record_size;
/*
* Check whether attribute is from different MFT record. If so,
* find next, because we don't need such.
*/
ntfs_log_trace("Attribute lookup failed.\n");
} else
goto put_err_out;
}
}
/* Move away attribute. */
if (ntfs_attr_record_move_away(ctx, 0)) {
ntfs_log_trace("Failed to move out attribute.\n");
break;
}
freed += record_size;
/* Check whether we done. */
return 0;
}
/*
* Reposition to first attribute after $STANDARD_INFORMATION and
* $ATTRIBUTE_LIST (see comments upwards).
*/
ntfs_log_trace("Attribute lookup failed.\n");
break;
}
break;
}
}
}
ntfs_log_trace("No attributes left that can be moved out.\n");
return -1;
}
/**
* ntfs_inode_update_times - update selected time fields for ntfs inode
* @ni: ntfs inode for which update time fields
* @mask: select which time fields should be updated
*
* This function updates time fields to current time. Fields to update are
* selected using @mask (see enum @ntfs_time_update_flags for posssible values).
*/
{
if (!ni) {
return;
}
return;
if (mask & NTFS_UPDATE_ATIME)
if (mask & NTFS_UPDATE_MTIME)
if (mask & NTFS_UPDATE_CTIME)
}
/**
* ntfs_inode_badclus_bad - check for $Badclus:$Bad data attribute
* @mft_no: mft record number where @attr is present
* @attr: attribute record used to check for the $Bad attribute
*
* Check if the mft record given by @mft_no and @attr contains the bad sector
* list. Please note that mft record numbers describing $Badclus extent inodes
* will not match the current $Badclus:$Bad check.
*
* On success return 1 if the file is $Badclus:$Bad, otherwise return 0.
* On error return -1 with errno set to the error code.
*/
{
if (!attr) {
ntfs_log_error("Invalid argument.\n");
return -1;
}
if (mft_no != FILE_BadClus)
return 0;
return 0;
ntfs_log_perror("Couldn't convert '$Bad' to Unicode");
return -1;
}
ret = 1;
return ret;
}