runlist.c revision 7e7bd3dccbfe8f79e25e5c1554b5bc3a9aaca321
/**
* runlist.c - Run list handling code. Part of the Linux-NTFS project.
*
* Copyright (c) 2002-2005 Anton Altaparmakov
* Copyright (c) 2002-2005 Richard Russon
* Copyright (c) 2002-2006 Szabolcs Szakacsits
* Copyright (c) 2004-2007 Yura Pakhuchiy
*
* 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_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include "compat.h"
#include "types.h"
#include "volume.h"
#include "layout.h"
#include "debug.h"
#include "device.h"
#include "logging.h"
/**
* ntfs_rl_mm - runlist memmove
* @base:
* @dst:
* @src:
* @size:
*
* Description...
*
* Returns:
*/
int size)
{
}
/**
* ntfs_rl_mc - runlist memory copy
* @dstbase:
* @dst:
* @srcbase:
* @src:
* @size:
*
* Description...
*
* Returns:
*/
{
if (size > 0)
}
/**
* ntfs_rl_realloc - Reallocate memory for runlists
* @rl: original runlist
* @old_size: number of runlist elements in the original runlist @rl
* @new_size: number of runlist elements we need space for
*
* As the runlists grow, more memory will be required. To prevent large
* numbers of small reallocations of memory, this function returns a 4kiB block
* of memory.
*
* N.B. If the new allocation doesn't require a different number of 4kiB
* blocks in memory, the function will return the original pointer.
*
* On success, return a pointer to the newly allocated, or recycled, memory.
* On error, return NULL with errno set to the error code.
*/
{
return rl;
}
/**
* ntfs_rl_are_mergeable - test if two runlists can be joined together
* @dst: original runlist
* @src: new runlist to test for mergeability with @dst
*
* Test if two runlists can be joined together. For this, their VCNs and LCNs
* must be adjacent.
*
* Return: TRUE Success, the runlists can be merged.
* FALSE Failure, the runlists cannot be merged.
*/
{
ntfs_log_debug("Eeek. ntfs_rl_are_mergeable() invoked with NULL "
"pointer!\n");
return FALSE;
}
/* We can merge unmapped regions even if they are misaligned. */
return TRUE;
/* If the runs are misaligned, we cannot merge them. */
return FALSE;
/* If both runs are non-sparse and contiguous, we can merge them. */
return TRUE;
/* If we are merging two holes, we can merge them. */
return TRUE;
/* Cannot merge. */
return FALSE;
}
/**
* __ntfs_rl_merge - merge two runlists without testing if they can be merged
* @dst: original, destination runlist
* @src: new runlist to merge with @dst
*
* Merge the two runlists, writing into the destination runlist @dst. The
* caller must make sure the runlists can be merged or this will corrupt the
* destination runlist.
*/
{
}
/**
* ntfs_rl_append - append a runlist after a given element
* @dst: original runlist to be worked on
* @dsize: number of elements in @dst (including end marker)
* @src: runlist to be inserted into @dst
* @ssize: number of elements in @src (excluding end marker)
* @loc: append the new runlist @src after this element in @dst
*
* Append the runlist @src after element @loc in @dst. Merge the right end of
* the new runlist, if necessary. Adjust the size of the hole before the
* appended runlist.
*
* On success, return a pointer to the new, combined, runlist. Note, both
* runlists @dst and @src are deallocated before returning so you cannot use
* the pointers for anything any more. (Strictly speaking the returned runlist
* may be the same as @dst but this is irrelevant.)
*
* On error, return NULL, with errno set to the error code. Both runlists are
* left unmodified.
*/
{
int marker; /* End of the inserted runs */
ntfs_log_debug("Eeek. ntfs_rl_append() invoked with NULL "
"pointer!\n");
return NULL;
}
/* First, check if the right hand end needs merging. */
/* Space required: @dst size + @src size, less one if we merged. */
if (!dst)
return NULL;
/*
* We are guaranteed to succeed from here so can start modifying the
* original runlists.
*/
/* First, merge the right hand end, if necessary. */
if (right)
/* marker - First run after the @src runs that have been inserted */
/* Move the tail of @dst out of the way, then copy in @src. */
/* Adjust the size of the preceding hole. */
/* We may have changed the length of the file, so fix the end marker */
return dst;
}
/**
* ntfs_rl_insert - insert a runlist into another
* @dst: original runlist to be worked on
* @dsize: number of elements in @dst (including end marker)
* @src: new runlist to be inserted
* @ssize: number of elements in @src (excluding end marker)
* @loc: insert the new runlist @src before this element in @dst
*
* Insert the runlist @src before element @loc in the runlist @dst. Merge the
* left end of the new runlist, if necessary. Adjust the size of the hole
* after the inserted runlist.
*
* On success, return a pointer to the new, combined, runlist. Note, both
* runlists @dst and @src are deallocated before returning so you cannot use
* the pointers for anything any more. (Strictly speaking the returned runlist
* may be the same as @dst but this is irrelevant.)
*
* On error, return NULL, with errno set to the error code. Both runlists are
* left unmodified.
*/
{
int marker; /* End of the inserted runs */
ntfs_log_debug("Eeek. ntfs_rl_insert() invoked with NULL "
"pointer!\n");
return NULL;
}
/* disc => Discontinuity between the end of @dst and the start of @src.
* This means we might need to insert a "notmapped" run.
*/
if (loc == 0)
else {
if (left)
}
/* Space required: @dst size + @src size, less one if we merged, plus
* one if there was a discontinuity.
*/
if (!dst)
return NULL;
/*
* We are guaranteed to succeed from here so can start modifying the
* original runlist.
*/
if (left)
/*
* marker - First run after the @src runs that have been inserted
* Nominally: marker = @loc + @ssize (location + number of runs in @src)
* If "left", then the first run in @src has been merged with one in @dst.
* If "disc", then @dst and @src don't meet and we need an extra run to fill the gap.
*/
/* Move the tail of @dst out of the way, then copy in @src. */
/* Adjust the VCN of the first run after the insertion ... */
/* ... and the length. */
/* Writing beyond the end of the file and there's a discontinuity. */
if (disc) {
if (loc > 0) {
} else {
}
}
return dst;
}
/**
* ntfs_rl_replace - overwrite a runlist element with another runlist
* @dst: original runlist to be worked on
* @dsize: number of elements in @dst (including end marker)
* @src: new runlist to be inserted
* @ssize: number of elements in @src (excluding end marker)
* @loc: index in runlist @dst to overwrite with @src
*
* Replace the runlist element @dst at @loc with @src. Merge the left and
* right ends of the inserted runlist, if necessary.
*
* On success, return a pointer to the new, combined, runlist. Note, both
* runlists @dst and @src are deallocated before returning so you cannot use
* the pointers for anything any more. (Strictly speaking the returned runlist
* may be the same as @dst but this is irrelevant.)
*
* On error, return NULL, with errno set to the error code. Both runlists are
* left unmodified.
*/
{
signed delta;
int tail; /* Start of tail of @dst */
int marker; /* End of the inserted runs */
ntfs_log_debug("Eeek. ntfs_rl_replace() invoked with NULL "
"pointer!\n");
return NULL;
}
/* First, see if the left and right ends need merging. */
if (loc > 0)
/* Allocate some space. We'll need less if the left, right, or both
* ends get merged. The -1 accounts for the run being replaced.
*/
if (delta > 0) {
if (!dst)
return NULL;
}
/*
* We are guaranteed to succeed from here so can start modifying the
* original runlists.
*/
/* First, merge the left and right ends, if necessary. */
if (right)
if (left)
/*
* tail - Offset of the tail of @dst
* Nominally: @tail = @loc + 1 (location, skipping the replaced run)
* If "right", then one of @dst's runs is already merged into @src.
*/
/*
* marker - First run after the @src runs that have been inserted
* Nominally: @marker = @loc + @ssize (location + number of runs in @src)
* If "left", then the first run in @src has been merged with one in @dst.
*/
/* Move the tail of @dst out of the way, then copy in @src. */
/* We may have changed the length of the file, so fix the end marker */
return dst;
}
/**
* ntfs_rl_split - insert a runlist into the centre of a hole
* @dst: original runlist to be worked on
* @dsize: number of elements in @dst (including end marker)
* @src: new runlist to be inserted
* @ssize: number of elements in @src (excluding end marker)
* @loc: index in runlist @dst at which to split and insert @src
*
* Split the runlist @dst at @loc into two and insert @new in between the two
* fragments. No merging of runlists is necessary. Adjust the size of the
* holes either side.
*
* On success, return a pointer to the new, combined, runlist. Note, both
* runlists @dst and @src are deallocated before returning so you cannot use
* the pointers for anything any more. (Strictly speaking the returned runlist
* may be the same as @dst but this is irrelevant.)
*
* On error, return NULL, with errno set to the error code. Both runlists are
* left unmodified.
*/
{
ntfs_log_trace("Invoked with NULL pointer!\n");
return NULL;
}
/* Space required: @dst size + @src size + one new hole. */
if (!dst)
return dst;
/*
* We are guaranteed to succeed from here so can start modifying the
* original runlists.
*/
/* Move the tail of @dst out of the way, then copy in @src. */
/* Adjust the size of the holes either size of @src. */
return dst;
}
/**
* ntfs_runlists_merge - merge two runlists into one
* @drl: original runlist to be worked on
* @srl: new runlist to be merged into @drl
*
* First we sanity check the two runlists @srl and @drl to make sure that they
* are sensible and can be merged. The runlist @srl must be either after the
* runlist @drl or completely within a hole (or unmapped region) in @drl.
*
* Merging of runlists is necessary in two cases:
* 1. When attribute lists are used and a further extent is being mapped.
* 2. When new clusters are allocated to fill a hole or extend a file.
*
* There are four possible ways @srl can be merged. It can:
* - be inserted at the beginning of a hole,
* - split the hole in two and be inserted between the two fragments,
* - be appended at the end of a hole, or it can
* - replace the whole hole.
* It can also be appended to the end of the runlist, which is just a variant
* of the insert case.
*
* On success, return a pointer to the new, combined, runlist. Note, both
* runlists @drl and @srl are deallocated before returning so you cannot use
* the pointers for anything any more. (Strictly speaking the returned runlist
* may be the same as @dst but this is irrelevant.)
*
* On error, return NULL, with errno set to the error code. Both runlists are
* left unmodified. The following error codes are defined:
* ENOMEM Not enough memory to allocate runlist array.
* EINVAL Invalid parameters were passed in.
* ERANGE The runlists overlap and cannot be merged.
*/
{
int sstart; /* First index with lcn > LCN_RL_NOT_MAPPED. */
int dins; /* Index into @drl at which to insert @srl. */
lcn >= LCN_HOLE. */
int marker = 0;
VCN marker_vcn = 0;
ntfs_log_debug("dst:\n");
ntfs_log_debug("src:\n");
/* Check for silly calling... */
if (!srl)
return drl;
/* Check for the case where the first mapping is being done now. */
if (!drl) {
/* Complete the source runlist if necessary. */
/* Scan to the end of the source runlist. */
;
dend++;
if (!drl)
return drl;
/* Insert start element at the front of the runlist. */
}
goto finished;
}
/* Skip any unmapped start element(s) in the source runlist. */
si++;
/* Can't have an entirely unmapped source runlist. */
ntfs_log_debug("Eeek! ntfs_runlists_merge() received entirely "
"unmapped source runlist.\n");
return NULL;
}
/* Record the starting points. */
/*
* Skip forward in @drl until we reach the position where @srl needs to
* be inserted. If we reach the end of @drl, @srl just needs to be
* appended to @drl.
*/
break;
}
/* Sanity check for illegal overlaps. */
ntfs_log_debug("Run lists overlap. Cannot merge!\n");
return NULL;
}
/* Scan to the end of both runlists in order to know their sizes. */
;
;
/* Scan to the last element with lcn >= LCN_HOLE. */
;
;
{
/* Or we'll lose an end marker */
ss++;
if (start) {
if (finish)
else
} else {
if (finish)
else
}
if (!drl) {
ntfs_log_perror("Merge failed");
return drl;
}
if (marker) {
ntfs_log_debug("Triggering marker code.\n");
;
/* We only need to care if @srl ended after @drl. */
int slots = 0;
ntfs_log_debug("Old marker = %lli, replacing with "
"LCN_ENOENT.\n",
goto finished;
}
/*
* We need to create an unmapped runlist element in
* @drl or extend an existing one before adding the
* ENOENT terminator.
*/
ds--;
slots = 1;
}
/* Add an unmapped runlist element. */
if (!slots) {
* extra memory already! (AIA)
*/
if (!drl)
goto critical_error;
slots = 2;
}
ds++;
/* Need to set vcn if it isn't set already. */
if (slots != 1)
/* We now used up a slot. */
slots--;
}
/* Finally add the ENOENT terminator. */
ds++;
if (!slots) {
* memory already! (AIA)
*/
if (!drl)
goto critical_error;
}
}
}
}
/* The merge was completed successfully. */
ntfs_log_debug("Merged runlist:\n");
return drl;
/* Critical error! We cannot afford to fail here. */
ntfs_log_perror("libntfs: Critical error");
ntfs_log_debug("Forcing segmentation fault!\n");
return drl;
}
/**
* ntfs_mapping_pairs_decompress - convert mapping pairs array to runlist
* @vol: ntfs volume on which the attribute resides
* @attr: attribute record whose mapping pairs array to decompress
* @old_rl: optional runlist in which to insert @attr's runlist
*
* Decompress the attribute @attr's mapping pairs array into a runlist. On
* success, return the decompressed runlist.
*
* If @old_rl is not NULL, decompressed runlist is inserted into the
* appropriate place in @old_rl and the resultant, combined runlist is
* returned. The original @old_rl is deallocated.
*
* On error, return NULL with errno set to the error code. @old_rl is left
* unmodified in that case.
*
* The following error codes are defined:
* ENOMEM Not enough memory to allocate runlist array.
* EIO Corrupt runlist.
* EINVAL Invalid parameters were passed in.
* ERANGE The two runlists overlap.
*
* FIXME: For now we take the conceptionally simplest approach of creating the
* new runlist disregarding the already existing one and then splicing the
* two into one, if that is possible (we check for overlap and discard the new
* runlist if overlap present before returning NULL, with errno = ERANGE).
*/
{
runlist_elements. */
u8 b; /* Current byte offset in buf. */
ntfs_log_trace("Entering for attr 0x%x.\n",
/* Make sure attr exists and is non-resident. */
return NULL;
}
/* Start at vcn = lowest_vcn and lcn 0. */
lcn = 0;
/* Get start of the mapping pairs array. */
ntfs_log_debug("Corrupt attribute.\n");
return NULL;
}
/* Current position in runlist array. */
rlpos = 0;
/* Allocate first 4kiB block and set current runlist size to 4kiB. */
rlsize = 0x1000;
if (!rl)
return NULL;
/* Insert unmapped starting element if necessary. */
if (vcn) {
rlpos++;
}
/*
* Allocate more memory if needed, including space for the
* not-mapped and terminator elements.
*/
rlsize += 0x1000;
if (!rl2) {
return NULL;
}
}
/* Enter the current vcn into the current runlist element. */
/*
* Get the change in vcn, i.e. the run length in clusters.
* Doing it this way ensures that we signextend negative values.
* A negative run length doesn't make any sense, but hey, I
* didn't make up the NTFS specs and Windows NT4 treats the run
* length as a signed value so that's how it is...
*/
b = *buf & 0xf;
if (b) {
goto io_error;
} else { /* The length entry is compulsory. */
ntfs_log_debug("Missing length entry in mapping pairs "
"array.\n");
}
/*
* Assume a negative length to indicate data corruption and
* hence clean-up and return NULL.
*/
if (deltaxcn < 0) {
ntfs_log_debug("Invalid length in mapping pairs array.\n");
goto err_out;
}
/*
* Enter the current run length into the current runlist
* element.
*/
/* Increment the current vcn by the current run length. */
/*
* There might be no lcn change at all, as is the case for
* sparse clusters on NTFS 3.0+, in which case we set the lcn
* to LCN_HOLE.
*/
if (!(*buf & 0xf0))
else {
/* Get the lcn change which really can be negative. */
goto io_error;
/* Change the current lcn to it's new value. */
#ifdef DEBUG
/*
* On NTFS 1.2-, apparently can have lcn == -1 to
* indicate a hole. But we haven't verified ourselves
* whether it is really the lcn or the deltaxcn that is
* -1. So if either is found give us a message so we
* can investigate it further!
*/
ntfs_log_debug("lcn delta == -1\n");
ntfs_log_debug("lcn == -1\n");
}
#endif
/* Check lcn is not below -1. */
ntfs_log_debug("Invalid LCN < -1 in mapping pairs "
"array.\n");
goto err_out;
}
/* Enter the current lcn into the runlist element. */
}
/* Get to the next runlist element. */
rlpos++;
/* Increment the buffer position to the next mapping pair. */
}
goto io_error;
/*
* If there is a highest_vcn specified, it must be equal to the final
* vcn in the runlist - 1, or something has gone badly wrong.
*/
ntfs_log_debug("Corrupt mapping pairs array in non-resident "
"attribute.\n");
goto err_out;
}
/* Setup not mapped runlist element if this is the base extent. */
/*
* A highest_vcn of zero means this is a single extent
* attribute so simply terminate the runlist with LCN_ENOENT).
*/
if (deltaxcn) {
/*
* If there is a difference between the highest_vcn and
* the highest cluster, the runlist is either corrupt
* or, more likely, there are more extents following
* this one.
*/
if (deltaxcn < max_cluster) {
ntfs_log_debug("More extents to follow; deltaxcn = "
"0x%llx, max_cluster = 0x%llx\n",
(long long)deltaxcn,
(long long)max_cluster);
rlpos++;
} else if (deltaxcn > max_cluster) {
ntfs_log_debug("Corrupt attribute. deltaxcn = "
"0x%llx, max_cluster = 0x%llx\n",
(long long)deltaxcn,
(long long)max_cluster);
goto mpa_err;
}
}
} else /* Not the base extent. There may be more extents to follow. */
/* Setup terminating runlist element. */
/* If no existing runlist was specified, we are done. */
if (!old_rl) {
ntfs_log_debug("Mapping pairs array successfully decompressed:\n");
return rl;
}
/* Now combine the new and old runlists checking for overlaps. */
if (old_rl)
return old_rl;
ntfs_log_debug("Failed to merge runlists.\n");
return NULL;
ntfs_log_debug("Corrupt attribute.\n");
return NULL;
}
/**
* ntfs_rl_vcn_to_lcn - convert a vcn into a lcn given a runlist
* @rl: runlist to use for conversion
* @vcn: vcn to convert
*
* Convert the virtual cluster number @vcn of an attribute into a logical
* cluster number (lcn) of a device using the runlist @rl to map vcns to their
* corresponding lcns.
*
* Since lcns must be >= 0, we use negative return values with special meaning:
*
* Return value Meaning / Description
* ==================================================
* -1 = LCN_HOLE Hole / not allocated on disk.
* -2 = LCN_RL_NOT_MAPPED This is part of the runlist which has not been
* inserted into the runlist yet.
* -3 = LCN_ENOENT There is no such vcn in the attribute.
* -4 = LCN_EINVAL Input parameter error.
*/
{
int i;
return (LCN)LCN_EINVAL;
/*
* If rl is NULL, assume that we have found an unmapped runlist. The
* caller can then attempt to map it and fail appropriately if
* necessary.
*/
if (!rl)
return (LCN)LCN_RL_NOT_MAPPED;
/* Catch out of lower bounds vcn. */
return (LCN)LCN_ENOENT;
}
}
/*
* The terminator element is setup to the correct value, i.e. one of
* LCN_HOLE, LCN_RL_NOT_MAPPED, or LCN_ENOENT.
*/
/* Just in case... We could replace this with BUG() some day. */
return (LCN)LCN_ENOENT;
}
/**
* ntfs_rl_pread - gather read from disk
* @vol: ntfs volume to read from
* @rl: runlist specifying where to read the data from
* @pos: byte position within runlist @rl at which to begin the read
* @count: number of bytes to read
* @b: data buffer into which to read from disk
*
* This function will read @count bytes from the volume @vol to the data buffer
* @b gathering the data as specified by the runlist @rl. The read begins at
* offset @pos into the runlist @rl.
*
* On success, return the number of successfully read bytes. If this number is
* lower than @count this means that the read reached end of file or that an
* error was encountered during the read so that the read is partial. 0 means
* nothing was read (also return 0 when @count is 0).
*
* On error and nothing has been read, return -1 with errno set appropriately
* to the return code of ntfs_pread(), or to EINVAL in case of invalid
* arguments.
*
* NOTE: If we encounter EOF while reading we return EIO because we assume that
* the run list must point to valid locations within the ntfs volume.
*/
{
return -1;
}
if (!count)
return count;
/* Seek in @rl to the run containing @pos. */
/* Offset in the run at which to begin reading. */
goto rl_err_out;
goto rl_err_out;
/* It is a hole. Just fill buffer @b with zeroes. */
/* Update counters and proceed with next run. */
continue;
}
/* It is a real lcn, read it from the volume. */
ofs);
/* If everything ok, update progress counters and continue. */
if (bytes_read > 0) {
total += bytes_read;
count -= bytes_read;
b = (u8*)b + bytes_read;
continue;
}
/* If the syscall was interrupted, try again. */
goto retry;
goto rl_err_out;
}
/* Finally, return the number of bytes read. */
return total;
if (total)
return total;
return -1;
}
/**
* ntfs_rl_pwrite - scatter write to disk
* @vol: ntfs volume to write to
* @rl: runlist specifying where to write the data to
* @pos: byte position within runlist @rl at which to begin the write
* @count: number of bytes to write
* @b: data buffer to write to disk
*
* This function will write @count bytes from data buffer @b to the volume @vol
* scattering the data as specified by the runlist @rl. The write begins at
* offset @pos into the runlist @rl.
*
* On success, return the number of successfully written bytes. If this number
* is lower than @count this means that the write has been interrupted in
* flight or that an error was encountered during the write so that the write
* is partial. 0 means nothing was written (also return 0 when @count is 0).
*
* On error and nothing has been written, return -1 with errno set
* appropriately to the return code of ntfs_pwrite(), or to to EINVAL in case
* of invalid arguments.
*/
{
return -1;
}
if (!count)
return count;
/* Seek in @rl to the run containing @pos. */
/* Offset in the run at which to begin writing. */
goto rl_err_out;
s64 t;
int cnt;
goto rl_err_out;
/*
* It is a hole. Check if the buffer is zero in this
* region and if not abort with error.
*/
for (t = 0; t < written; t++) {
if (((unsigned long*)b)[t])
goto rl_err_out;
}
if (cnt) {
int i;
~(sizeof(unsigned long) - 1));
for (i = 0; i < cnt; i++) {
if (b2[i])
goto rl_err_out;
}
}
/*
* The buffer region is zero, update progress counters
* and proceed with next run.
*/
continue;
}
/* It is a real lcn, write it to the volume. */
ofs);
if (!NVolReadOnly(vol))
to_write, b);
else
/* If everything ok, update progress counters and continue. */
if (written > 0) {
continue;
}
/* If the syscall was interrupted, try again. */
goto retry;
goto rl_err_out;
}
/* Finally, return the number of bytes written. */
return total;
if (total)
return total;
return -1;
}
/**
* ntfs_rl_fill_zero - fill given region with zeroes
* @vol: ntfs volume to write to
* @rl: runlist specifying where to write zeroes to
* @pos: byte position within runlist @rl at which to begin the zeroing
* @count: number of bytes to fill with zeros
*
* Return 0 on success and -1 on error with errno set to the error code.
*/
{
char *buf;
int ret = 0;
(long long)count);
return -1;
}
if (!buf)
return -1;
if (written <= 0) {
ntfs_log_perror("Failed to zero space");
ret = -1;
break;
}
}
return ret;
}
/**
* ntfs_get_nr_significant_bytes - get number of bytes needed to store a number
* @n: number for which to get the number of bytes for
*
* Return the number of bytes required to store @n unambiguously as
* a signed number.
*
* This is used in the context of the mapping pairs array to determine how
* many bytes will be needed in the array to store a given logical cluster
* number (lcn) or a specific run length.
*
* Return the number of bytes written. This function cannot fail.
*/
int ntfs_get_nr_significant_bytes(const s64 n)
{
s64 l = n;
int i;
s8 j;
i = 0;
do {
l >>= 8;
i++;
} while (l != 0LL && l != -1LL);
j = (n >> 8 * (i - 1)) & 0xff;
/* If the sign bit is wrong, we need an extra byte. */
i++;
return i;
}
/**
* ntfs_get_size_for_mapping_pairs - get bytes needed for mapping pairs array
* @vol: ntfs volume (needed for the ntfs version)
* @rl: runlist for which to determine the size of the mapping pairs
* @start_vcn: vcn at which to start the mapping pairs array
*
* Walk the runlist @rl and calculate the size in bytes of the mapping pairs
* array corresponding to the runlist @rl, starting at vcn @start_vcn. This
* for example allows us to allocate a buffer of the right size when building
* the mapping pairs array.
*
* If @rl is NULL, just return 1 (for the single terminator byte).
*
* Return the calculated size in bytes on success. On error, return -1 with
* errno set to the error code. The following error codes are defined:
* EINVAL - Run list contains unmapped elements. Make sure to only pass
* fully mapped runlists to this function.
* - @start_vcn is invalid.
* EIO - The runlist is corrupt.
*/
{
int rls;
if (start_vcn < 0) {
ntfs_log_trace("start_vcn %lld (should be >= 0)\n",
(long long) start_vcn);
return -1;
}
if (!rl) {
if (start_vcn) {
ntfs_log_trace("rl NULL, start_vcn %lld (should be > 0)\n",
(long long) start_vcn);
return -1;
}
return 1;
}
/* Skip to runlist element containing @start_vcn. */
rl++;
return -1;
}
prev_lcn = 0;
/* Always need the terminating zero byte. */
rls = 1;
/* Do the first partial run if present. */
/* We know rl->length != 0 already. */
goto err_out;
/* Header byte + length. */
/*
* If the logical cluster number (lcn) denotes a hole and we
* are on NTFS 3.0+, we don't store it at all, i.e. we need
* zero space. On earlier NTFS versions we just store the lcn.
* Note: this assumes that on NTFS 1.2-, holes are stored with
* an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
*/
/* Change in lcn. */
}
/* Go to next runlist element. */
rl++;
}
/* Do the full runs. */
goto err_out;
/* Header byte + length. */
/*
* If the logical cluster number (lcn) denotes a hole and we
* are on NTFS 3.0+, we don't store it at all, i.e. we need
* zero space. On earlier NTFS versions we just store the lcn.
* Note: this assumes that on NTFS 1.2-, holes are stored with
* an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
*/
/* Change in lcn. */
prev_lcn);
}
}
return rls;
else
return -1;
}
/**
* ntfs_write_significant_bytes - write the significant bytes of a number
* @dst: destination buffer to write to
* @dst_max: pointer to last byte of destination buffer for bounds checking
* @n: number whose significant bytes to write
*
* Store in @dst, the minimum bytes of the number @n which are required to
* identify @n unambiguously as a signed number, taking care not to exceed
* @dest_max, the maximum position within @dst to which we are allowed to
* write.
*
* This is used when building the mapping pairs array of a runlist to compress
* a given logical cluster number (lcn) or a specific run length to the minimum
* size possible.
*
* Return the number of bytes written on success. On error, i.e. the
* destination buffer @dst is too small, return -1 with errno set ENOSPC.
*/
{
s64 l = n;
int i;
s8 j;
i = 0;
do {
goto err_out;
*dst++ = l & 0xffLL;
l >>= 8;
i++;
} while (l != 0LL && l != -1LL);
j = (n >> 8 * (i - 1)) & 0xff;
/* If the sign bit is wrong, we need an extra byte. */
if (n < 0LL && j >= 0) {
goto err_out;
i++;
} else if (n > 0LL && j < 0) {
goto err_out;
i++;
*dst = 0;
}
return i;
return -1;
}
/**
* ntfs_mapping_pairs_build - build the mapping pairs array from a runlist
* @vol: ntfs volume (needed for the ntfs version)
* @dst: destination buffer to which to write the mapping pairs array
* @dst_len: size of destination buffer @dst in bytes
* @rl: runlist for which to build the mapping pairs array
* @start_vcn: vcn at which to start the mapping pairs array
* @stop_vcn: first vcn outside destination buffer on success or ENOSPC error
*
* Create the mapping pairs array from the runlist @rl, starting at vcn
* @start_vcn and save the array in @dst. @dst_len is the size of @dst in
* bytes and it should be at least equal to the value obtained by calling
* ntfs_get_size_for_mapping_pairs().
*
* If @rl is NULL, just write a single terminator byte to @dst.
*
* On success or ENOSPC error, if @stop_vcn is not NULL, *@stop_vcn is set to
* the first vcn outside the destination buffer. Note that on error @dst has
* been filled with all the mapping pairs that will fit, thus it can be treated
* as partial success, in that a new attribute extent needs to be created or the
* next extent has to be used and the mapping pairs build has to be continued
* with @start_vcn set to *@stop_vcn.
*
* Return 0 on success. On error, return -1 with errno set to the error code.
* The following error codes are defined:
* EINVAL - Run list contains unmapped elements. Make sure to only pass
* fully mapped runlists to this function.
* - @start_vcn is invalid.
* EIO - The runlist is corrupt.
* ENOSPC - The destination buffer is too small.
*/
{
if (start_vcn < 0)
goto val_err;
if (!rl) {
if (start_vcn)
goto val_err;
if (stop_vcn)
*stop_vcn = 0;
if (dst_len < 1) {
return -1;
}
/* Terminator byte. */
*dst = 0;
return 0;
}
/* Skip to runlist element containing @start_vcn. */
rl++;
goto val_err;
/*
* @dst_max is used for bounds checking in
* ntfs_write_significant_bytes().
*/
prev_lcn = 0;
/* Do the first partial run if present. */
/* We know rl->length != 0 already. */
goto err_out;
/* Write length. */
if (len_len < 0)
goto size_err;
/*
* If the logical cluster number (lcn) denotes a hole and we
* are on NTFS 3.0+, we don't store it at all, i.e. we need
* zero space. On earlier NTFS versions we just write the lcn
* change. FIXME: Do we need to write the lcn change or just
* the lcn in that case? Not sure as I have never seen this
* case on NT4. - We assume that we just need to write the lcn
* change until someone tells us otherwise... (AIA)
*/
/* Write change in lcn. */
if (lcn_len < 0)
goto size_err;
} else
lcn_len = 0;
goto size_err;
/* Update header byte. */
/* Position at next mapping pairs array element. */
/* Go to next runlist element. */
rl++;
}
/* Do the full runs. */
goto err_out;
/* Write length. */
if (len_len < 0)
goto size_err;
/*
* If the logical cluster number (lcn) denotes a hole and we
* are on NTFS 3.0+, we don't store it at all, i.e. we need
* zero space. On earlier NTFS versions we just write the lcn
* change. FIXME: Do we need to write the lcn change or just
* the lcn in that case? Not sure as I have never seen this
* case on NT4. - We assume that we just need to write the lcn
* change until someone tells us otherwise... (AIA)
*/
/* Write change in lcn. */
if (lcn_len < 0)
goto size_err;
} else
lcn_len = 0;
goto size_err;
/* Update header byte. */
/* Position at next mapping pairs array element. */
}
/* Set stop vcn. */
if (stop_vcn)
/* Add terminator byte. */
*dst = 0;
return 0;
/* Set stop vcn. */
if (stop_vcn)
/* Add terminator byte. */
*dst = 0;
return -1;
return -1;
else
return -1;
}
/**
* ntfs_rl_truncate - truncate a runlist starting at a specified vcn
* @arl: address of runlist to truncate
* @start_vcn: first vcn which should be cut off
*
* Truncate the runlist *@arl starting at vcn @start_vcn as well as the memory
* buffer holding the runlist.
*
* Return 0 on success and -1 on error with errno set to the error code.
*
* NOTE: @arl is the address of the runlist. We need the address so we can
* modify the pointer to the runlist with the new, reallocated memory buffer.
*/
{
return -1;
}
ntfs_log_perror("Start_vcn lies outside front of runlist");
return -1;
}
/* Find the starting vcn in the run list. */
break;
rl++;
}
ntfs_log_trace("Truncating already truncated runlist?\n");
return -1;
}
/* Truncate the run. */
/*
* If a run was partially truncated, make the following runlist
* element a terminator instead of the truncated runlist
* element itself.
*/
++rl;
}
return 0;
}
/**
* ntfs_rl_sparse - check whether runlist have sparse regions or not.
* @rl: runlist to check
*
* This function just skips not mapped regions assuming they are not sparse,
* so you need to ensure that runlist is fully mapped if you want perform full
* check.
*
* Return 1 if have, 0 if not, -1 on error with errno set to the error code.
*/
{
if (!rl) {
ntfs_log_trace("Invalid argument passed.\n");
return -1;
}
continue;
ntfs_log_trace("Bad runlist.\n");
return -1;
}
return 1;
}
}
return 0;
}
/**
* ntfs_rl_get_compressed_size - calculate length of non sparse regions
* @vol: ntfs volume (need for cluster size)
* @rl: runlist to calculate for
*
* Return compressed size or -1 on error with errno set to the error code.
*/
{
if (!rl) {
ntfs_log_trace("Invalid argument passed.\n");
return -1;
}
ntfs_log_trace("Received unmapped runlist.\n");
return -1;
}
} else
}
}
#ifdef NTFS_TEST
/**
* test_rl_helper
*/
#define MKRL(R,V,L,S) \
(R)->vcn = V; \
(R)->lcn = L; \
(R)->length = S;
/*
}
*/
/**
* test_rl_dump_runlist - Runlist test: Display the contents of a runlist
* @rl:
*
* Description...
*
* Returns:
*/
{
int abbr = 0; /* abbreviate long lists */
int len = 0;
int i;
if (!rl) {
printf(" Run list not present.\n");
return;
}
if (abbr)
printf(" VCN LCN len\n");
for (i = 0; ; i++, rl++) {
if (i == 4)
printf(" ...\n");
continue;
}
ind = 3;
printf("%8lld %8s %8lld\n",
} else
printf("%8lld %8lld %8lld\n",
break;
}
printf("\n");
}
/**
* test_rl_runlists_merge - Runlist test: Merge two runlists
* @drl:
* @srl:
*
* Description...
*
* Returns:
*/
{
printf("dst:\n");
printf("src:\n");
printf("res:\n");
return res;
}
/**
* test_rl_read_buffer - Runlist test: Read a file containing a runlist
* @file:
* @buf:
* @bufsize:
*
* Description...
*
* Returns:
*/
{
if (!fptr) {
return 0;
}
return 0;
}
return 1;
}
/**
* test_rl_pure_src - Runlist test: Complicate the simple tests a little
* @contig:
* @multi:
* @vcn:
* @len:
*
* Description...
*
* Returns:
*/
{
int fudge;
if (contig)
fudge = 0;
else
fudge = 999;
if (!result)
return NULL;
if (multi) {
} else {
}
return result;
}
/**
* test_rl_pure_test - Runlist test: Perform tests using simple runlists
* @test:
* @contig:
* @multi:
* @vcn:
* @len:
* @file:
* @size:
*
* Description...
*
* Returns:
*/
static void test_rl_pure_test(int test, BOOL contig, BOOL multi, int vcn, int len, runlist_element *file, int size)
{
}
/**
* test_rl_pure - Runlist test: Create tests using simple runlists
* @contig:
* @multi:
*
* Description...
*
* Returns:
*/
{
/* VCN, LCN, len */
static runlist_element file1[] = {
{ 0, -1, 100 }, /* HOLE */
{ 100, 1100, 100 }, /* DATA */
{ 200, -1, 100 }, /* HOLE */
{ 300, 1300, 100 }, /* DATA */
{ 400, -1, 100 }, /* HOLE */
{ 500, -3, 0 } /* NOENT */
};
static runlist_element file2[] = {
{ 0, 1000, 100 }, /* DATA */
{ 100, -1, 100 }, /* HOLE */
{ 200, -3, 0 } /* NOENT */
};
static runlist_element file3[] = {
{ 0, 1000, 100 }, /* DATA */
{ 100, -3, 0 } /* NOENT */
};
static runlist_element file4[] = {
{ 0, -3, 0 } /* NOENT */
};
static runlist_element file5[] = {
{ 0, -2, 100 }, /* NOTMAP */
{ 100, 1100, 100 }, /* DATA */
{ 200, -2, 100 }, /* NOTMAP */
{ 300, 1300, 100 }, /* DATA */
{ 400, -2, 100 }, /* NOTMAP */
{ 500, -3, 0 } /* NOENT */
};
static runlist_element file6[] = {
{ 0, 1000, 100 }, /* DATA */
{ 100, -2, 100 }, /* NOTMAP */
{ 200, -3, 0 } /* NOENT */
};
BOOL c, m;
c = TRUE;
c = FALSE;
else {
printf("rl pure [contig|noncontig] [single|multi]\n");
return;
}
m = TRUE;
m = FALSE;
else {
printf("rl pure [contig|noncontig] [single|multi]\n");
return;
}
}
/**
* test_rl_zero - Runlist test: Merge a zero-length runlist
*
* Description...
*
* Returns:
*/
static void test_rl_zero(void)
{
if (!bob)
return;
if (!jim)
return;
}
/**
* test_rl_frag_combine - Runlist test: Perform tests using fragmented files
* @vol:
* @attr1:
* @attr2:
* @attr3:
*
* Description...
*
* Returns:
*/
static void test_rl_frag_combine(ntfs_volume *vol, ATTR_RECORD *attr1, ATTR_RECORD *attr2, ATTR_RECORD *attr3)
{
if (!run1)
return;
if (!run2)
return;
if (!run3)
return;
}
/**
* test_rl_frag - Runlist test: Create tests using very fragmented files
* @test:
*
* Description...
*
* Returns:
*/
static void test_rl_frag(char *test)
{
goto out;
goto out;
goto out;
goto out;
else
out:
}
/**
* test_rl_main - Runlist test: Program start (main)
* @argc:
* @argv:
*
* Description...
*
* Returns:
*/
{
else
printf("rl [zero|frag|pure] {args}\n");
return 0;
}
#endif