whitespace and include guards
2002/07/08 06:23:22-00:00 !antona Don't use string concatenation with __FUNCTION__ as gcc-3.x don't like it. 2002/07/02 23:47:10-00:00 !antona Global replacement of __[su]{8,16,32,64} with [su]{8,16,32,64} and layout.h define it. 2002/06/05 20:32:53-00:00 !antona Mft mirror now updated from ntfs_write_mft_record, yey! Fixup ntfstools accordingly. 2002/06/05 00:29:19-00:00 !antona Make ntfs_attr_pread work with resident attributes and start on ntfs_attr_pwrite (incomplete!). 2002/06/04 12:12:42-00:00 !antona Final POSIXification of disk_io functions. (famous last words) 2002/06/01 00:41:45-00:00 !antona huge update! 2002/04/29 01:53:55-00:00 !antona Loads of stuff. Improvements, start on attr pread and attr mst_pread. Write to follow. ntfslabel cleanup and extensions. libntfs cleanups, fixes, etc. 2002/04/18 18:15:46-00:00 !antona Define API for bootsect.[ch]: is_boot_sector_ntfs(). 2002/04/16 15:34:32-00:00 !antona Fix the library... 2002/04/15 20:04:27-00:00 !antona Fix all compiler warnings that came up with -Wall. Enabled -Wall for ./configure --enable-debug everywhere. Fix a few bugs in mkntfs that came up in the warnings (just error code paths, nothing major). 2002/04/15 18:39:37-00:00 !antona Cleanup some mistakes. 2002/04/15 17:51:26-00:00 !antona read/write_mft_record(s) are here 2002/04/15 00:42:07-00:00 !antona Big rewrite of disk_io.c. Now should have stable API for low level disk access. Move all mft record related stuff from disk_io.c to mft.c. 2002/04/14 15:26:23-00:00 !antona Remove find_first_attr and make all users use get_attr_search_ctx + find_attr instead. 2002/04/14 14:08:30-00:00 !antona Cleanup library code. Throw away unused stuff. 2001/06/01 02:07:26-00:00 !antona It has been a long time since last commit. At moment have done a lot of work on mkntfs but also at the moment ntfsfix and ntfsdump_logfile and libntfs are broken. Basically only mkntfs works and that is not complete either. 2001/04/11 15:29:39-00:00 !flatcap minor build fixes 2001/04/11 14:29:12-00:00 !antona Almost fixed compilaton. Remember to declare a struct type with struct NAME { declaration }; rather than the bogus struct { declaration NAME }; then can use typedef NAME othername; on a forward declaration of NAME (struct NAME;) and all is fine... 2001/04/11 11:49:16-00:00 !antona Header file reorganisation so that it compiles. 2001/04/08 03:02:55-00:00 !antona Added cvs Id header. 2001/04/05 20:14:45-00:00 !antona Commit of current state of development including locking a la kernel. This doesn't work on user space (semaphores don't work). Just want to have it committed. Will take out locking / modify it where necessary to use pthreads ASAP. 2001/04/03 22:42:42-00:00 !antona See last commit message. 2001/04/02 02:04:37-00:00 !antona Everything compiles again! Yey! (Don't know about working though, haven't tried it... So be careful...) The definitely final find_{first_}attr() functions are in place. Currently still no support for attribute lists. The two new _RE files contain the C-fied and more or less (more less than more actually) cleaned up functions from the ntfs driver. Once they are cleaned up (find_attr() is already completed but I left it in the _RE files for future reference/educational value) and modified to suit my ideas of how they should work, which are not quite the same as the driver way, they will make it into attrib.[ch]. If anyone gives the new code a try, I would be interested in whether it worked or not... (-; 2001/03/26 04:34:20-00:00 !antona typo fix 2001/03/26 04:28:07-00:00 !antona Cleanup and fixes. 2001/03/26 03:39:59-00:00 !antona Updates 2001/03/05 03:04:40-00:00 !antona Corresponding changes to the library. 2001/03/02 15:05:53-00:00 !antona Commit latest library state. !!!NOTE!!! This breaks everything!!! I'm in the middle of rewritting the find_attribute stuff in attrib.c at the moment. 2001/01/26 00:34:10-00:00 !antona Added disk io functions file. (Logical change 1.5)edge.strict_endians
parent
24f3018dfc
commit
4593a8d3ca
|
@ -0,0 +1,333 @@
|
|||
/*
|
||||
* $Id$
|
||||
*
|
||||
* disk_io.c - Disk io functions. Part of the Linux-NTFS project.
|
||||
*
|
||||
* Copyright (c) 2000-2002 Anton Altaparmakov.
|
||||
*
|
||||
* This program/include file is free software; you can redistribute it and/or
|
||||
* 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.
|
||||
*
|
||||
* This program/include file is distributed in the hope that it will be
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "types.h"
|
||||
#include "disk_io.h"
|
||||
#include "mst.h"
|
||||
#include "debug.h"
|
||||
|
||||
/**
|
||||
* ntfs_pread - positioned read from disk
|
||||
* @fd: file descriptor to read from
|
||||
* @pos: position in file descriptor to read from
|
||||
* @count: number of bytes to read
|
||||
* @b: output data buffer
|
||||
*
|
||||
* This function will read @count bytes from file descriptor @fd at position
|
||||
* @pos into the data buffer @b.
|
||||
*
|
||||
* On success, return the number of successfully read bytes. If this number is
|
||||
* lower than @count this means that we have either reached end of file or
|
||||
* encountered an error during the read so that the read is partial. 0 means
|
||||
* end of file or nothing to read (@count is 0).
|
||||
*
|
||||
* On error and nothing has been read, return -1 with errno set appropriately
|
||||
* to the return code of either lseek, read, or set to EINVAL in case of
|
||||
* invalid arguments.
|
||||
*/
|
||||
s64 ntfs_pread(const int fd, const s64 pos, s64 count, const void *b)
|
||||
{
|
||||
s64 br, total;
|
||||
|
||||
Dprintf("%s(): Entering for pos 0x%Lx, count 0x%Lx.\n", __FUNCTION__,
|
||||
pos, count);
|
||||
if (!b || count < 0 || pos < 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (!count)
|
||||
return 0;
|
||||
/* Locate to position. */
|
||||
if (lseek(fd, pos, SEEK_SET) == (off_t)-1) {
|
||||
Dprintf("ntfs_pread: lseek to 0x%Lx returned error: %s\n", pos,
|
||||
strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
/* Read the data. */
|
||||
for (total = 0; count; count -= br, total += br) {
|
||||
br = read(fd, (char*)b + total, count);
|
||||
/* If everything ok, continue. */
|
||||
if (br > 0)
|
||||
continue;
|
||||
/* If EOF or error return number of bytes read. */
|
||||
if (!br || total)
|
||||
return total;
|
||||
/* Nothing read and error, return error status. */
|
||||
return br;
|
||||
}
|
||||
/* Finally, return the number of bytes read. */
|
||||
return total;
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_pwrite - positioned write to disk
|
||||
* @fd: file descriptor to write to
|
||||
* @pos: position in file descriptor to write to
|
||||
* @count: number of bytes to write
|
||||
* @b: data buffer to write to disk
|
||||
*
|
||||
* This function will write @count bytes from data buffer @b to file descriptor
|
||||
* @fd at position @pos.
|
||||
*
|
||||
* 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 either lseek, write, fdatasync, or set
|
||||
* to EINVAL in case of invalid arguments.
|
||||
*/
|
||||
s64 ntfs_pwrite(const int fd, const s64 pos, s64 count, const void *b)
|
||||
{
|
||||
s64 written, total;
|
||||
|
||||
Dprintf("%s(): Entering for pos 0x%Lx, count 0x%Lx.\n", __FUNCTION__,
|
||||
pos, count);
|
||||
if (!b || count < 0 || pos < 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (!count)
|
||||
return 0;
|
||||
/* Locate to position. */
|
||||
if (lseek(fd, pos, SEEK_SET) == (off_t)-1) {
|
||||
Dprintf("ntfs_pwrite: lseek to 0x%Lx returned error: %s\n",
|
||||
pos, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
/* Write the data. */
|
||||
for (total = 0; count; count -= written, total += written) {
|
||||
written = write(fd, (char*)b + total, count);
|
||||
/* If everything ok, continue. */
|
||||
if (written > 0)
|
||||
continue;
|
||||
/*
|
||||
* If nothing written or error return number of bytes written.
|
||||
*/
|
||||
if (!written || total)
|
||||
break;
|
||||
/* Nothing written and error, return error status. */
|
||||
return written;
|
||||
}
|
||||
/* Sync write to disk. */
|
||||
if (fdatasync(fd) == -1)
|
||||
return -1;
|
||||
/* Finally, return the number of bytes written. */
|
||||
return total;
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_mst_pread - multi sector transfer (mst) positioned read
|
||||
* @fd: file descriptor to read from
|
||||
* @pos: position in file descriptor to read from
|
||||
* @count: number of blocks to read
|
||||
* @bksize: size of each block that needs mst deprotecting
|
||||
* @b: output data buffer
|
||||
*
|
||||
* Multi sector transfer (mst) positioned read. This function will read @count
|
||||
* blocks of size @bksize bytes each from file descriptor @fd at position @pos
|
||||
* into the data buffer @b.
|
||||
*
|
||||
* On success, return the number of successfully read blocks. If this number is
|
||||
* lower than @count this means that we have reached end of file, that the read
|
||||
* was interrupted, or that an error was encountered during the read so that
|
||||
* the read is partial. 0 means end of file or nothing was read (also return 0
|
||||
* when @count or @bksize are 0).
|
||||
*
|
||||
* On error and nothing was read, return -1 with errno set appropriately to the
|
||||
* return code of either lseek, read, or set to EINVAL in case of invalid
|
||||
* arguments.
|
||||
*
|
||||
* NOTE: If an incomplete multi sector transfer has been detected the magic
|
||||
* will have been changed to magic_BAAD but no error will be returned. Thus it
|
||||
* is possible that we return count blocks as being read but that any number
|
||||
* (between zero and count!) of these blocks is actually subject to a multi
|
||||
* sector transfer error. This should be detected by the caller by checking for
|
||||
* the magic being "BAAD".
|
||||
*/
|
||||
s64 ntfs_mst_pread(const int fd, const s64 pos, s64 count,
|
||||
const u32 bksize, const void *b)
|
||||
{
|
||||
s64 br, i;
|
||||
|
||||
if (bksize & (bksize - 1) || bksize % NTFS_SECTOR_SIZE) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
/* Do the read. */
|
||||
br = ntfs_pread(fd, pos, count * bksize, b);
|
||||
if (br < 0)
|
||||
return br;
|
||||
/*
|
||||
* Apply fixups to successfully read data, disregarding any errors
|
||||
* returned from the MST fixup function. This is because we want to
|
||||
* fixup everything possible and we rely on the fact that the "BAAD"
|
||||
* magic will be detected later on.
|
||||
*/
|
||||
count = br / bksize;
|
||||
for (i = 0; i < count; ++i)
|
||||
ntfs_post_read_mst_fixup((NTFS_RECORD*)(b + i * bksize),
|
||||
bksize);
|
||||
/* Finally, return the number of complete blocks read. */
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_mst_pwrite - multi sector transfer (mst) positioned write
|
||||
* @fd: file descriptor to write to
|
||||
* @pos: position in file descriptor to write to
|
||||
* @count: number of blocks to write
|
||||
* @bksize: size of each block that needs mst protecting
|
||||
* @b: data buffer to write to disk
|
||||
*
|
||||
* Multi sector transfer (mst) positioned write. This function will write
|
||||
* @count blocks of size @bksize bytes each from data buffer @b to file
|
||||
* descriptor @fd at position @pos.
|
||||
*
|
||||
* On success, return the number of successfully written blocks. If this number
|
||||
* is lower than @count this means that the write has been interrutped 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 or @bksize are 0).
|
||||
*
|
||||
* On error and nothing has been written, return -1 with errno set
|
||||
* appropriately to the return code of either lseek, write, fdatasync, or set
|
||||
* to EINVAL in case of invalid arguments.
|
||||
*
|
||||
* NOTE: We mst protect the data, write it, then mst deprotect it using a quick
|
||||
* deprotect algorithm (no checking). This saves us from making a copy before
|
||||
* the write and at the same time causes the usn to be incremented in the
|
||||
* buffer. This conceptually fits in better with the idea that cached data is
|
||||
* always deprotected and protection is performed when the data is actually
|
||||
* going to hit the disk and the cache is immediately deprotected again
|
||||
* simulating an mst read on the written data. This way cache coherency is
|
||||
* achieved.
|
||||
*/
|
||||
s64 ntfs_mst_pwrite(const int fd, const s64 pos, s64 count,
|
||||
const u32 bksize, const void *b)
|
||||
{
|
||||
s64 written, i;
|
||||
|
||||
if (count < 0 || bksize % NTFS_SECTOR_SIZE) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (!count)
|
||||
return 0;
|
||||
/* Prepare data for writing. */
|
||||
for (i = 0; i < count; ++i) {
|
||||
int err;
|
||||
|
||||
err = ntfs_pre_write_mst_fixup((NTFS_RECORD*)(b + i * bksize),
|
||||
bksize);
|
||||
if (err < 0) {
|
||||
/* Abort write at this position. */
|
||||
if (!i)
|
||||
return err;
|
||||
count = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Write the prepared data. */
|
||||
written = ntfs_pwrite(fd, pos, count * bksize, b);
|
||||
/* Quickly deprotect the data again. */
|
||||
for (i = 0; i < count; ++i)
|
||||
ntfs_post_write_mst_fixup((NTFS_RECORD*)(b + i * bksize));
|
||||
if (written <= 0)
|
||||
return written;
|
||||
/* Finally, return the number of complete blocks written. */
|
||||
return written / bksize;
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_read_clusters - read ntfs clusters
|
||||
* @vol: volume to read from
|
||||
* @lcn: starting logical cluster number
|
||||
* @count: number of clusters to read
|
||||
* @b: output data buffer
|
||||
*
|
||||
* Read @count ntfs clusters starting at logical cluster number @lcn from
|
||||
* volume @vol into buffer @b. Return number of clusters read or -1 on error,
|
||||
* with errno set to the error code.
|
||||
*/
|
||||
s64 ntfs_read_clusters(const ntfs_volume *vol, const s64 lcn,
|
||||
const s64 count, const void *b)
|
||||
{
|
||||
s64 br;
|
||||
|
||||
if (!vol || lcn < 0 || count < 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (vol->nr_clusters <= lcn + count) {
|
||||
errno = ESPIPE;
|
||||
return -1;
|
||||
}
|
||||
br = ntfs_pread(vol->fd, lcn << vol->cluster_size_bits,
|
||||
count << vol->cluster_size_bits, b);
|
||||
if (br < 0) {
|
||||
Dperror("Error reading cluster(s)");
|
||||
return br;
|
||||
}
|
||||
return br >> vol->cluster_size_bits;
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_write_clusters - write ntfs clusters
|
||||
* @vol: volume to write to
|
||||
* @lcn: starting logical cluster number
|
||||
* @count: number of clusters to write
|
||||
* @b: data buffer to write to disk
|
||||
*
|
||||
* Write @count ntfs clusters starting at logical cluster number @lcn from
|
||||
* buffer @b to volume @vol. Return the number of clusters written or -1 on
|
||||
* error, with errno set to the error code.
|
||||
*/
|
||||
s64 ntfs_write_clusters(const ntfs_volume *vol, const s64 lcn,
|
||||
const s64 count, const void *b)
|
||||
{
|
||||
s64 bw;
|
||||
|
||||
if (!vol || lcn < 0 || count < 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (vol->nr_clusters <= lcn + count) {
|
||||
errno = ESPIPE;
|
||||
return -1;
|
||||
}
|
||||
bw = ntfs_pwrite(vol->fd, lcn << vol->cluster_size_bits,
|
||||
count << vol->cluster_size_bits, b);
|
||||
if (bw < 0) {
|
||||
Dperror("Error writing cluster(s)");
|
||||
return bw;
|
||||
}
|
||||
return bw >> vol->cluster_size_bits;
|
||||
}
|
||||
|
Loading…
Reference in New Issue