Hopefully finished device io abstraction layer in the library.

(Logical change 1.138)
edge.strict_endians
cantab.net!aia21 2003-06-02 08:53:04 +00:00
parent d1e19dcd82
commit f0628dd514
9 changed files with 369 additions and 124 deletions

View File

@ -70,18 +70,20 @@ struct ntfs_device {
* the low level device described by a ntfs device structure.
*/
struct ntfs_device_operations {
struct ntfs_device *(*open)(const char *pathname, int flags);
int (*open)(struct ntfs_device *dev, int flags);
int (*close)(struct ntfs_device *dev);
int (*sync)(struct ntfs_device *dev);
s64 (*seek)(struct ntfs_device *dev, s64 offset, int whence);
s64 (*read)(struct ntfs_device *dev, void *buf, s64 count);
s64 (*write)(struct ntfs_device *dev, const void *buf, s64 count);
s64 (*pread)(struct ntfs_device *dev, void *buf, s64 count, s64 offset);
s64 (*pwrite)(struct ntfs_device *dev, const void *buf, s64 count,
s64 offset);
int (*sync)(struct ntfs_device *dev);
int (*ioctl)(struct ntfs_device *dev, int request, void *argp);
};
struct ntfs_device *ntfs_device_alloc(void);
struct ntfs_device *ntfs_device_alloc(const char *name, const long state,
struct ntfs_device_operations *dops, void *private);
int ntfs_device_free(struct ntfs_device *dev);
#endif /* defined _NTFS_DEVICE_H */

View File

@ -24,20 +24,22 @@
#include "volume.h"
extern s64 ntfs_pread(const int fd, const s64 pos, s64 count, const void *b);
extern s64 ntfs_pwrite(const int fd, const s64 pos, s64 count, const void *b);
extern s64 ntfs_pread(struct ntfs_device *dev, const s64 pos, s64 count,
void *b);
extern s64 ntfs_pwrite(struct ntfs_device *dev, const s64 pos, s64 count,
const void *b);
extern s64 ntfs_mst_pread(const int fd, const s64 pos, s64 count,
const u32 bksize, const void *b);
extern s64 ntfs_mst_pwrite(const int fd, const s64 pos, s64 count,
extern s64 ntfs_mst_pread(struct ntfs_device *dev, const s64 pos, s64 count,
const u32 bksize, void *b);
extern s64 ntfs_mst_pwrite(struct ntfs_device *dev, const s64 pos, s64 count,
const u32 bksize, const void *b);
extern s64 ntfs_cluster_read(const ntfs_volume *vol, const s64 lcn,
const s64 count, const void *b);
const s64 count, void *b);
extern s64 ntfs_cluster_write(const ntfs_volume *vol, const s64 lcn,
const s64 count, const void *b);
extern s64 ntfs_device_size_get(int f, int block_size);
extern s64 ntfs_device_size_get(struct ntfs_device *dev, int block_size);
#endif /* defined _NTFS_DISK_IO_H */

View File

@ -1,7 +1,7 @@
/*
* volume.h - Exports for NTFS volume handling. Part of the Linux-NTFS project.
*
* Copyright (c) 2000-2002 Anton Altaparmakov
* Copyright (c) 2000-2003 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
@ -87,8 +87,6 @@ typedef enum {
* ntfs_volume - structure describing an open volume in memory
*/
struct _ntfs_volume {
int fd; //FIXME: Remove this. -> Use *dev instead.
char *dev_name; //FIXME: Remove this. -> Use *dev instead.
struct ntfs_device *dev;/* NTFS device associated with the volume. */
char *vol_name; /* Name of the volume. */
unsigned long state; /* NTFS specific flags describing this volume.
@ -156,9 +154,12 @@ struct _ntfs_volume {
bytes. */
};
extern ntfs_volume *ntfs_volume_startup(const char *name, unsigned long rwflag);
extern ntfs_volume *ntfs_mount(const char *name, unsigned long rwflag);
extern ntfs_volume *ntfs_volume_startup(struct ntfs_device *dev,
unsigned long rwflag);
extern ntfs_volume *ntfs_device_mount(struct ntfs_device *dev,
unsigned long rwflag);
extern ntfs_volume *ntfs_mount(const char *name, unsigned long rwflag);
extern int ntfs_umount(ntfs_volume *vol, const BOOL force);
extern int ntfs_version_is_supported(ntfs_volume *vol);

View File

@ -151,7 +151,7 @@ s64 ntfs_get_attribute_value(const ntfs_volume *vol, const MFT_RECORD *m,
* or can we have sparse runs in uncompressed
* files as well?
*/
r = ntfs_pread(vol->fd, rl[i].lcn <<
r = ntfs_pread(vol->dev, rl[i].lcn <<
vol->cluster_size_bits,
rl[i].length <<
vol->cluster_size_bits, intbuf);
@ -189,7 +189,7 @@ s64 ntfs_get_attribute_value(const ntfs_volume *vol, const MFT_RECORD *m,
* to 0 for the length of the run, which should
* be 16 (= compression unit size).
*/
r = ntfs_pread(vol->fd, rl[i].lcn <<
r = ntfs_pread(vol->dev, rl[i].lcn <<
vol->cluster_size_bits,
rl[i].length <<
vol->cluster_size_bits,
@ -580,7 +580,6 @@ s64 ntfs_attr_pread(ntfs_attr *na, const s64 pos, s64 count, void *b)
s64 br, to_read, ofs, total, total2;
ntfs_volume *vol;
runlist_element *rl;
int f;
Dprintf("%s(): Entering for inode 0x%Lx, attr 0x%x, pos 0x%Lx, "
"count 0x%Lx.\n", __FUNCTION__,
@ -591,11 +590,6 @@ s64 ntfs_attr_pread(ntfs_attr *na, const s64 pos, s64 count, void *b)
return -1;
}
vol = na->ni->vol;
f = vol->fd;
if (!f) {
errno = EBADF;
return -1;
}
/*
* Encrypted attributes are not supported. We return access denied,
* which is what Windows NT4 does, too.
@ -700,8 +694,8 @@ retry:
Dprintf("%s(): Reading 0x%Lx bytes from vcn 0x%Lx, lcn 0x%Lx, "
"ofs 0x%Lx.\n", __FUNCTION__, to_read,
rl->vcn, rl->lcn, ofs);
br = ntfs_pread(f, (rl->lcn << vol->cluster_size_bits) + ofs,
to_read, b);
br = ntfs_pread(vol->dev, (rl->lcn << vol->cluster_size_bits) +
ofs, to_read, b);
/* If everything ok, update progress counters and continue. */
if (br > 0) {
total += br;
@ -755,7 +749,7 @@ s64 ntfs_attr_pwrite(ntfs_attr *na, const s64 pos, s64 count, void *b)
ntfs_volume *vol;
ntfs_attr_search_ctx *ctx = NULL;
runlist_element *rl;
int f, eo;
int eo;
struct {
unsigned int initialized_size : 1;
} need_to_undo = { 0 };
@ -768,11 +762,6 @@ s64 ntfs_attr_pwrite(ntfs_attr *na, const s64 pos, s64 count, void *b)
return -1;
}
vol = na->ni->vol;
f = vol->fd;
if (!f) {
errno = EBADF;
return -1;
}
/*
* Encrypted attributes are not supported. We return access denied,
* which is what Windows NT4 does, too.
@ -963,7 +952,7 @@ retry:
"ofs 0x%Lx.\n", __FUNCTION__, to_write,
rl->vcn, rl->lcn, ofs);
if (!NVolReadOnly(vol))
written = ntfs_pwrite(f, (rl->lcn <<
written = ntfs_pwrite(vol->dev, (rl->lcn <<
vol->cluster_size_bits) + ofs,
to_write, b);
else

View File

@ -200,7 +200,7 @@ int ntfs_boot_sector_parse(ntfs_volume *vol, const NTFS_BOOT_SECTOR *bs)
if (sectors_per_cluster & (sectors_per_cluster - 1)) {
Dprintf("Error: %s is not a valid NTFS partition! "
"sectors_per_cluster is not a power of 2.\n",
vol->dev_name);
vol->dev->d_name);
return -1;
}
vol->nr_clusters = sle64_to_cpu(bs->number_of_sectors) >>
@ -214,14 +214,14 @@ int ntfs_boot_sector_parse(ntfs_volume *vol, const NTFS_BOOT_SECTOR *bs)
vol->mftmirr_lcn > vol->nr_clusters) {
Dprintf("Error: %s is not a valid NTFS partition! ($Mft LCN "
"or\n$MftMirr LCN is greater than the number "
"of clusters!\n", vol->dev_name);
"of clusters!\n", vol->dev->d_name);
return -1;
}
vol->cluster_size = sectors_per_cluster * vol->sector_size;
if (vol->cluster_size & (vol->cluster_size - 1)) {
Dprintf("Error: %s is not a valid NTFS partition! "
"cluster_size is not a power of 2.\n",
vol->dev_name);
vol->dev->d_name);
return -1;
}
vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
@ -247,7 +247,7 @@ int ntfs_boot_sector_parse(ntfs_volume *vol, const NTFS_BOOT_SECTOR *bs)
if (vol->mft_record_size & (vol->mft_record_size - 1)) {
Dprintf("Error: %s is not a valid NTFS partition! "
"mft_record_size is not a power of 2.\n",
vol->dev_name);
vol->dev->d_name);
return -1;
}
vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;

View File

@ -0,0 +1,97 @@
/*
* device.c - Low level device io functions. Part of the Linux-NTFS project.
*
* Copyright (c) 2003 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 "config.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "device.h"
/**
* ntfs_device_alloc - allocate an ntfs device structure and pre-initialize it
* name: name of the device (must be present)
* state: initial device state (usually zero)
* dops: ntfs device operations to use with the device (must be present)
* private: pointer to private data (optional)
*
* Allocate an ntfs device structure and pre-initialize it with the user-
* specified device operations @dops, device state @state, device name @name,
* and optional private data @private.
*
* Note, @name is copied and can hence be freed after this functions returns.
*
* On success return a pointer to the allocated ntfs device structure and on
* error return NULL with errno set to the error code returned by malloc().
*/
struct ntfs_device *ntfs_device_alloc(const char *name, const long state,
struct ntfs_device_operations *dops, void *private)
{
struct ntfs_device *dev;
if (!name) {
errno = EINVAL;
return NULL;
}
dev = (struct ntfs_device *)malloc(sizeof(struct ntfs_device));
if (dev) {
if (!(dev->d_name = strdup(name))) {
int eo = errno;
free(dev);
errno = eo;
return NULL;
}
dev->d_ops = dops;
dev->d_state = state;
dev->d_private = private;
}
return dev;
}
/**
* ntfs_device_free - free an ntfs device structure
* @dev: ntfs device structure to free
*
* Free the ntfs device structure @dev.
*
* Return 0 on success or -1 on error with errno set to the error code. The
* following error codes are defined:
* EINVAL Invalid pointer @dev.
* EBUSY Device is still open. Close it before freeing it!
*/
int ntfs_device_free(struct ntfs_device *dev)
{
if (!dev) {
errno = EINVAL;
return -1;
}
if (NDevOpen(dev)) {
errno = EBUSY;
return -1;
}
if (dev->d_name)
free(dev->d_name);
free(dev);
return 0;
}

View File

@ -26,6 +26,9 @@
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef HAVE_LINUX_FD_H
# include <sys/ioctl.h>
# include <linux/fd.h>
@ -35,6 +38,7 @@
#include "disk_io.h"
#include "mst.h"
#include "debug.h"
#include "device.h"
#if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
# define BLKGETSIZE _IO(0x12,96) /* Get device size in 512byte blocks. */
@ -42,13 +46,13 @@
/**
* ntfs_pread - positioned read from disk
* @fd: file descriptor to read from
* @pos: position in file descriptor to read from
* @dev: device to read from
* @pos: position in device 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.
* This function will read @count bytes from device @dev 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
@ -56,12 +60,13 @@
* 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
* to the return code of either seek, 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 ntfs_pread(struct ntfs_device *dev, const s64 pos, s64 count, void *b)
{
s64 br, total;
struct ntfs_device_operations *dops;
Dprintf("%s(): Entering for pos 0x%Lx, count 0x%Lx.\n", __FUNCTION__,
pos, count);
@ -71,15 +76,16 @@ s64 ntfs_pread(const int fd, const s64 pos, s64 count, const void *b)
}
if (!count)
return 0;
dops = dev->d_ops;
/* 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));
if (dops->seek(dev, pos, SEEK_SET) == (off_t)-1) {
Dprintf("ntfs_pread: device seek 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);
br = dops->read(dev, (char*)b + total, count);
/* If everything ok, continue. */
if (br > 0)
continue;
@ -95,13 +101,13 @@ s64 ntfs_pread(const int fd, const s64 pos, s64 count, const void *b)
/**
* ntfs_pwrite - positioned write to disk
* @fd: file descriptor to write to
* @dev: device 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.
* This function will write @count bytes from data buffer @b to the device @dev
* 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
@ -109,12 +115,14 @@ s64 ntfs_pread(const int fd, const s64 pos, s64 count, const void *b)
* 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, or set
* appropriately to the return code of either seek, write, or set
* to EINVAL in case of invalid arguments.
*/
s64 ntfs_pwrite(const int fd, const s64 pos, s64 count, const void *b)
s64 ntfs_pwrite(struct ntfs_device *dev, const s64 pos, s64 count,
const void *b)
{
s64 written, total;
struct ntfs_device_operations *dops;
Dprintf("%s(): Entering for pos 0x%Lx, count 0x%Lx.\n", __FUNCTION__,
pos, count);
@ -124,15 +132,20 @@ s64 ntfs_pwrite(const int fd, const s64 pos, s64 count, const void *b)
}
if (!count)
return 0;
if (NDevReadOnly(dev)) {
errno = EROFS;
return -1;
}
dops = dev->d_ops;
/* Locate to position. */
if (lseek(fd, pos, SEEK_SET) == (off_t)-1) {
Dprintf("ntfs_pwrite: lseek to 0x%Lx returned error: %s\n",
if (dops->seek(dev, pos, SEEK_SET) == (off_t)-1) {
Dprintf("ntfs_pwrite: seek 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);
written = dops->write(dev, (char*)b + total, count);
/* If everything ok, continue. */
if (written > 0)
continue;
@ -148,17 +161,118 @@ s64 ntfs_pwrite(const int fd, const s64 pos, s64 count, const void *b)
return total;
}
static int ntfs_device_disk_io_open(struct ntfs_device *dev, int flags)
{
if (NDevOpen(dev)) {
errno = EBUSY;
return -1;
}
/* Open the device/file obtaining the file descriptor. */
if (((int)dev->d_private = open(dev->d_name, flags)) == -1)
return -1;
/* Setup our read-only open flags appropriately. */
if ((flags & O_RDONLY) == O_RDONLY)
NDevSetReadOnly(dev);
NDevSetOpen(dev);
return 0;
}
static int ntfs_device_disk_io_close(struct ntfs_device *dev)
{
if (!NDevOpen(dev)) {
errno = EBADF;
return -1;
}
/* Close the file descriptor and clear our open flag. */
if (close((int)dev->d_private) == -1)
return -1;
NDevClearOpen(dev);
/* Destroy the device as we are done with it. */
ntfs_device_free(dev);
return 0;
}
static s64 ntfs_device_disk_io_seek(struct ntfs_device *dev, s64 offset,
int whence)
{
return lseek((int)dev->d_private, offset, whence);
}
static s64 ntfs_device_disk_io_read(struct ntfs_device *dev, void *buf,
s64 count)
{
return read((int)dev->d_private, buf, count);
}
static s64 ntfs_device_disk_io_write(struct ntfs_device *dev, const void *buf,
s64 count)
{
if (NDevReadOnly(dev)) {
errno = EROFS;
return -1;
}
return write((int)dev->d_private, buf, count);
}
static s64 ntfs_device_disk_io_pread(struct ntfs_device *dev, void *buf,
s64 count, s64 offset)
{
return ntfs_pread(dev, offset, count, buf);
}
static s64 ntfs_device_disk_io_pwrite(struct ntfs_device *dev, const void *buf,
s64 count, s64 offset)
{
if (NDevReadOnly(dev)) {
errno = EROFS;
return -1;
}
return ntfs_pwrite(dev, offset, count, buf);
}
static int ntfs_device_disk_io_sync(struct ntfs_device *dev)
{
if (!NDevReadOnly(dev))
return fsync((int)dev->d_private);
return 0;
}
static int ntfs_device_disk_io_ioctl(struct ntfs_device *dev, int request,
void *argp)
{
return ioctl((int)dev->d_private, request, argp);
}
/**
* Default device operations for working with unix style devices and files.
*/
struct ntfs_device_operations ntfs_device_disk_io_ops = {
.open = ntfs_device_disk_io_open,
.close = ntfs_device_disk_io_close,
.seek = ntfs_device_disk_io_seek,
.read = ntfs_device_disk_io_read,
.write = ntfs_device_disk_io_write,
.pread = ntfs_device_disk_io_pread,
.pwrite = ntfs_device_disk_io_pwrite,
.sync = ntfs_device_disk_io_sync,
.ioctl = ntfs_device_disk_io_ioctl,
};
/**
* ntfs_mst_pread - multi sector transfer (mst) positioned read
* @fd: file descriptor to read from
* @dev: device 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.
* blocks of size @bksize bytes each from device @dev at position @pos into the
* 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
@ -167,7 +281,7 @@ s64 ntfs_pwrite(const int fd, const s64 pos, s64 count, const void *b)
* 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
* return code of either seek, read, or set to EINVAL in case of invalid
* arguments.
*
* NOTE: If an incomplete multi sector transfer has been detected the magic
@ -177,8 +291,8 @@ s64 ntfs_pwrite(const int fd, const s64 pos, s64 count, const void *b)
* 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 ntfs_mst_pread(struct ntfs_device *dev, const s64 pos, s64 count,
const u32 bksize, void *b)
{
s64 br, i;
@ -187,7 +301,7 @@ s64 ntfs_mst_pread(const int fd, const s64 pos, s64 count,
return -1;
}
/* Do the read. */
br = ntfs_pread(fd, pos, count * bksize, b);
br = ntfs_pread(dev, pos, count * bksize, b);
if (br < 0)
return br;
/*
@ -206,15 +320,15 @@ s64 ntfs_mst_pread(const int fd, const s64 pos, s64 count,
/**
* ntfs_mst_pwrite - multi sector transfer (mst) positioned write
* @fd: file descriptor to write to
* @dev: device 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.
* @count blocks of size @bksize bytes each from data buffer @b to the device
* @dev 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
@ -222,7 +336,7 @@ s64 ntfs_mst_pread(const int fd, const s64 pos, s64 count,
* 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, or set
* appropriately to the return code of either seek, write, or set
* to EINVAL in case of invalid arguments.
*
* NOTE: We mst protect the data, write it, then mst deprotect it using a quick
@ -234,7 +348,7 @@ s64 ntfs_mst_pread(const int fd, const s64 pos, s64 count,
* 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,
s64 ntfs_mst_pwrite(struct ntfs_device *dev, const s64 pos, s64 count,
const u32 bksize, const void *b)
{
s64 written, i;
@ -260,7 +374,7 @@ s64 ntfs_mst_pwrite(const int fd, const s64 pos, s64 count,
}
}
/* Write the prepared data. */
written = ntfs_pwrite(fd, pos, count * bksize, b);
written = ntfs_pwrite(dev, pos, count * bksize, b);
/* Quickly deprotect the data again. */
for (i = 0; i < count; ++i)
ntfs_mst_post_write_fixup((NTFS_RECORD*)((u8*)b + i * bksize));
@ -281,8 +395,8 @@ s64 ntfs_mst_pwrite(const int fd, const s64 pos, s64 count,
* volume @vol into buffer @b. Return number of clusters read or -1 on error,
* with errno set to the error code.
*/
s64 ntfs_cluster_read(const ntfs_volume *vol, const s64 lcn,
const s64 count, const void *b)
s64 ntfs_cluster_read(const ntfs_volume *vol, const s64 lcn, const s64 count,
void *b)
{
s64 br;
@ -294,7 +408,7 @@ s64 ntfs_cluster_read(const ntfs_volume *vol, const s64 lcn,
errno = ESPIPE;
return -1;
}
br = ntfs_pread(vol->fd, lcn << vol->cluster_size_bits,
br = ntfs_pread(vol->dev, lcn << vol->cluster_size_bits,
count << vol->cluster_size_bits, b);
if (br < 0) {
Dperror("Error reading cluster(s)");
@ -328,7 +442,7 @@ s64 ntfs_cluster_write(const ntfs_volume *vol, const s64 lcn,
return -1;
}
if (!NVolReadOnly(vol))
bw = ntfs_pwrite(vol->fd, lcn << vol->cluster_size_bits,
bw = ntfs_pwrite(vol->dev, lcn << vol->cluster_size_bits,
count << vol->cluster_size_bits, b);
else
bw = count << vol->cluster_size_bits;
@ -341,40 +455,41 @@ s64 ntfs_cluster_write(const ntfs_volume *vol, const s64 lcn,
/**
* ntfs_device_offset_valid - test if a device offset is valid
* @f: open file descriptor of device
* @dev: open device
* @ofs: offset to test for validity
*
* Test if the offset @ofs is an existing location on the device described
* by the open file descriptor @f.
* by the open device structure @dev.
*
* Return 0 if it is valid and -1 if it is not valid.
*/
static inline int ntfs_device_offset_valid(int f, s64 ofs)
static inline int ntfs_device_offset_valid(struct ntfs_device *dev, s64 ofs)
{
char ch;
if (lseek(f, ofs, SEEK_SET) >= 0 && read(f, &ch, 1) == 1)
if (dev->d_ops->seek(dev, ofs, SEEK_SET) >= 0 &&
dev->d_ops->read(dev, &ch, 1) == 1)
return 0;
return -1;
}
/**
* ntfs_device_size_get - return the size of a device in blocks
* @f: open file descriptor of device
* @dev: open device
* @block_size: block size in bytes in which to return the result
*
* Return the number of @block_size sized blocks in the device described by the
* open file descriptor @f.
* open device @dev.
*
* Adapted from e2fsutils-1.19, Copyright (C) 1995 Theodore Ts'o.
*/
s64 ntfs_device_size_get(int f, int block_size)
s64 ntfs_device_size_get(struct ntfs_device *dev, int block_size)
{
s64 high, low;
#ifdef BLKGETSIZE
long size;
if (ioctl(f, BLKGETSIZE, &size) >= 0) {
if (dev->d_ops->ioctl(dev, BLKGETSIZE, &size) >= 0) {
Dprintf("BLKGETSIZE nr 512 byte blocks = %ld (0x%ld)\n", size,
size);
return (s64)size * 512 / block_size;
@ -383,7 +498,7 @@ s64 ntfs_device_size_get(int f, int block_size)
#ifdef FDGETPRM
{ struct floppy_struct this_floppy;
if (ioctl(f, FDGETPRM, &this_floppy) >= 0) {
if (dev->d_ops->ioctl(dev, FDGETPRM, &this_floppy) >= 0) {
Dprintf("FDGETPRM nr 512 byte blocks = %ld (0x%ld)\n",
this_floppy.size, this_floppy.size);
return (s64)this_floppy.size * 512 / block_size;
@ -395,17 +510,17 @@ s64 ntfs_device_size_get(int f, int block_size)
* so do binary search to find the size of the device.
*/
low = 0LL;
for (high = 1024LL; !ntfs_device_offset_valid(f, high); high <<= 1)
for (high = 1024LL; !ntfs_device_offset_valid(dev, high); high <<= 1)
low = high;
while (low < high - 1LL) {
const s64 mid = (low + high) / 2;
if (!ntfs_device_offset_valid(f, mid))
if (!ntfs_device_offset_valid(dev, mid))
low = mid;
else
high = mid;
}
lseek(f, 0LL, SEEK_SET);
dev->d_ops->seek(dev, 0LL, SEEK_SET);
return (low + 1LL) / block_size;
}

View File

@ -1011,17 +1011,12 @@ s64 ntfs_rl_pwrite(const ntfs_volume *vol, const runlist_element *rl,
const s64 pos, s64 count, void *b)
{
s64 written, to_write, ofs, total;
int f, err = EIO;
int err = EIO;
if (!vol || !rl || pos < 0 || count < 0) {
errno = EINVAL;
return -1;
}
f = vol->fd;
if (!f) {
errno = EBADF;
return -1;
}
if (!count)
return count;
/* Seek in @rl to the run containing @pos. */
@ -1075,7 +1070,7 @@ s64 ntfs_rl_pwrite(const ntfs_volume *vol, const runlist_element *rl,
ofs);
retry:
if (!NVolReadOnly(vol))
written = ntfs_pwrite(f, (rl->lcn <<
written = ntfs_pwrite(vol->dev, (rl->lcn <<
vol->cluster_size_bits) + ofs,
to_write, b);
else

View File

@ -20,6 +20,7 @@
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
@ -69,12 +70,16 @@ static void __ntfs_volume_release(ntfs_volume *v)
ntfs_attr_close(v->mftmirr_na);
if (v->mftmirr_ni)
ntfs_inode_close(v->mftmirr_ni);
if (v->fd) {
fdatasync(v->fd);
close(v->fd);
if (v->dev) {
struct ntfs_device *dev = v->dev;
if (NDevDirty(dev))
dev->d_ops->sync(dev);
if (dev->d_ops->close(dev))
fprintf(stderr, "%s(): Eeek! Failed to close the "
"device. Error: %s\n", __FUNCTION__,
strerror(errno));
}
if (v->dev_name)
free(v->dev_name);
if (v->vol_name)
free(v->vol_name);
if (v->upcase)
@ -118,7 +123,7 @@ static int ntfs_mft_load(ntfs_volume *vol)
vol->mft_ni->mft_no = 0;
vol->mft_ni->mrec = mb;
/* Can't use any of the higher level functions yet! */
l = ntfs_mst_pread(vol->fd, vol->mft_lcn << vol->cluster_size_bits, 1,
l = ntfs_mst_pread(vol->dev, vol->mft_lcn << vol->cluster_size_bits, 1,
vol->mft_record_size, mb);
if (l != 1) {
if (l != -1)
@ -363,7 +368,7 @@ error_exit:
/**
* ntfs_volume_startup - allocate and setup an ntfs volume
* @name: name of device/file to open
* @dev: device to open
* @rwflag: optional mount flags
*
* Load, verify, and parse bootsector; load and setup $MFT and $MFTMirr. After
@ -373,7 +378,7 @@ error_exit:
* Return the allocated volume structure on success and NULL on error with
* errno set to the error code.
*/
ntfs_volume *ntfs_volume_startup(const char *name, unsigned long rwflag)
ntfs_volume *ntfs_volume_startup(struct ntfs_device *dev, unsigned long rwflag)
{
LCN mft_zone_size, mft_lcn;
s64 br;
@ -388,26 +393,30 @@ ntfs_volume *ntfs_volume_startup(const char *name, unsigned long rwflag)
BOOL debug = 0;
#endif
if (!dev || !dev->d_ops || !dev->d_name) {
errno = EINVAL;
return NULL;
}
/* Allocate the volume structure. */
vol = __ntfs_volume_allocate();
if (!vol)
return NULL;
/* Make a copy of the partition name. */
if (!(vol->dev_name = strdup(name)))
goto error_exit;
/* Attach the device to the volume. */
vol->dev = dev;
if (rwflag & MS_RDONLY)
NVolSetReadOnly(vol);
/* Allocate the boot sector structure. */
if (!(bs = (NTFS_BOOT_SECTOR *)malloc(sizeof(NTFS_BOOT_SECTOR))))
goto error_exit;
if (rwflag & MS_RDONLY)
NVolSetReadOnly(vol);
Dprintf("Reading bootsector... ");
if ((vol->fd = open(name, NVolReadOnly(vol) ? O_RDONLY: O_RDWR)) < 0) {
if (dev->d_ops->open(dev, NVolReadOnly(vol) ? O_RDONLY: O_RDWR)) {
Dputs(FAILED);
Dperror("Error opening partition file");
Dperror("Error opening partition device");
goto error_exit;
}
/* Now read the bootsector. */
br = ntfs_pread(vol->fd, 0, sizeof(NTFS_BOOT_SECTOR), bs);
br = ntfs_pread(dev, 0, sizeof(NTFS_BOOT_SECTOR), bs);
if (br != sizeof(NTFS_BOOT_SECTOR)) {
Dputs(FAILED);
if (br != -1)
@ -421,7 +430,8 @@ ntfs_volume *ntfs_volume_startup(const char *name, unsigned long rwflag)
}
Dputs(OK);
if (!ntfs_boot_sector_is_ntfs(bs, !debug)) {
Dprintf("Error: %s is not a valid NTFS partition!\n", name);
Dprintf("Error: %s is not a valid NTFS partition!\n",
dev->d_name);
errno = EINVAL;
goto error_exit;
}
@ -537,31 +547,28 @@ error_exit:
}
/**
* ntfs_mount - open ntfs volume
* @name: name of device/file to open
* ntfs_device_mount - open ntfs volume
* @dev: device to open
* @rwflag: optional mount flags
*
* This function mounts an ntfs volume. @name should contain the name of the
* device/file to mount as the ntfs volume.
* This function mounts an ntfs volume. @dev should describe the device which
* to mount as the ntfs volume.
*
* @rwflags is an optional second parameter. The same flags are used as for
* the mount system call (man 2 mount). Currently only the following flag
* is implemented:
* MS_RDONLY - mount volume read-only
*
* The function opens the device or file @name and verifies that it contains a
* valid bootsector. Then, it allocates an ntfs_volume structure and initializes
* The function opens the device @dev and verifies that it contains a valid
* bootsector. Then, it allocates an ntfs_volume structure and initializes
* some of the values inside the structure from the information stored in the
* bootsector. It proceeds to load the necessary system files and completes
* setting up the structure.
*
* Return the allocated volume structure on success and NULL on error with
* errno set to the error code.
*
* Note, that a copy is made of @name, and hence it can be discarded as
* soon as the function returns.
*/
ntfs_volume *ntfs_mount(const char *name, unsigned long rwflag)
ntfs_volume *ntfs_device_mount(struct ntfs_device *dev, unsigned long rwflag)
{
s64 l;
const char *OK = "OK";
@ -577,12 +584,7 @@ ntfs_volume *ntfs_mount(const char *name, unsigned long rwflag)
int i, j, eo;
u32 u;
if (!name) {
errno = EINVAL;
return NULL;
}
vol = ntfs_volume_startup(name, rwflag);
vol = ntfs_volume_startup(dev, rwflag);
if (!vol) {
Dperror("Failed to startup volume");
return NULL;
@ -900,13 +902,55 @@ error_exit:
return NULL;
}
/* External declaration for internal structure. */
extern struct ntfs_device_operations ntfs_device_disk_io_ops;
/**
* ntfs_mount - open ntfs volume
* @name: name of device/file to open
* @rwflag: optional mount flags
*
* This function mounts an ntfs volume. @name should contain the name of the
* device/file to mount as the ntfs volume.
*
* @rwflags is an optional second parameter. The same flags are used as for
* the mount system call (man 2 mount). Currently only the following flag
* is implemented:
* MS_RDONLY - mount volume read-only
*
* The function opens the device or file @name and verifies that it contains a
* valid bootsector. Then, it allocates an ntfs_volume structure and initializes
* some of the values inside the structure from the information stored in the
* bootsector. It proceeds to load the necessary system files and completes
* setting up the structure.
*
* Return the allocated volume structure on success and NULL on error with
* errno set to the error code.
*
* Note, that a copy is made of @name, and hence it can be discarded as
* soon as the function returns.
*/
ntfs_volume *ntfs_mount(const char *name, unsigned long rwflag)
{
struct ntfs_device *dev;
/* Allocate an ntfs_device structure. */
dev = ntfs_device_alloc(name, 0, &ntfs_device_disk_io_ops, NULL);
if (!dev)
return NULL;
/* Call ntfs_device_mount() to do the actual mount. */
return ntfs_device_mount(dev, rwflag);
}
/**
* ntfs_umount - close ntfs volume
* @vol: address of ntfs_volume structure of volume to close
* @force: if true force close the volume even if it is busy
*
* Deallocate all structures (including @vol itself) associated with the ntfs
* volume @vol.
* volume @vol. Can be used on volumes mounted with ntfs_mount() as well as
* with volumes mounted with ntfs_device_mount().
*
* Return 0 on success. On error return -1 with errno set appropriately
* (most likely to one of EAGAIN, EBUSY or EINVAL). The EAGAIN error means that