2009-12-19 15:06:57 +01:00
|
|
|
/**
|
|
|
|
* gekko_io.c - Gekko style disk io functions.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009 Rhys "Shareese" Koedijk
|
2010-02-01 00:14:14 +01:00
|
|
|
* Copyright (c) 2010 Dimok
|
2009-12-19 15:06:57 +01:00
|
|
|
*
|
|
|
|
* 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; 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_STDIO_H
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_MATH_H
|
|
|
|
#include <math.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LOCALE_H
|
|
|
|
#include <locale.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "ntfs.h"
|
|
|
|
#include "types.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "device_io.h"
|
|
|
|
#include "gekko_io.h"
|
|
|
|
#include "cache.h"
|
|
|
|
#include "device.h"
|
|
|
|
#include "bootsect.h"
|
|
|
|
|
|
|
|
#define DEV_FD(dev) ((gekko_fd *)dev->d_private)
|
|
|
|
|
|
|
|
/* Prototypes */
|
2010-09-19 01:16:05 +02:00
|
|
|
static s64 ntfs_device_gekko_io_readbytes( struct ntfs_device *dev, s64 offset, s64 count, void *buf );
|
|
|
|
static bool ntfs_device_gekko_io_readsectors( struct ntfs_device *dev, sec_t sector, sec_t numSectors, void* buffer );
|
|
|
|
static s64 ntfs_device_gekko_io_writebytes( struct ntfs_device *dev, s64 offset, s64 count, const void *buf );
|
|
|
|
static bool ntfs_device_gekko_io_writesectors( struct ntfs_device *dev, sec_t sector, sec_t numSectors, const void* buffer );
|
2009-12-19 15:06:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-09-19 01:16:05 +02:00
|
|
|
static int ntfs_device_gekko_io_open( struct ntfs_device *dev, int flags )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "dev %p, flags %i\n", dev, flags );
|
2009-12-19 15:06:57 +01:00
|
|
|
|
|
|
|
// Get the device driver descriptor
|
2010-09-19 01:16:05 +02:00
|
|
|
gekko_fd *fd = DEV_FD( dev );
|
|
|
|
if ( !fd )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device interface
|
|
|
|
const DISC_INTERFACE* interface = fd->interface;
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !interface )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Start the device interface and ensure that it is inserted
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !interface->startup() )
|
|
|
|
{
|
|
|
|
ntfs_log_perror( "device failed to start\n" );
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !interface->isInserted() )
|
|
|
|
{
|
|
|
|
ntfs_log_perror( "device media is not inserted\n" );
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Check that the device isn't already open (used by another volume?)
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( NDevOpen( dev ) )
|
|
|
|
{
|
|
|
|
ntfs_log_perror( "device is busy (already open)\n" );
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that there is a valid NTFS boot sector at the start of the device
|
|
|
|
NTFS_BOOT_SECTOR boot;
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( interface->readSectors( fd->startSector, 1, &boot ) )
|
|
|
|
{
|
|
|
|
if ( !ntfs_boot_sector_is_ntfs( &boot ) )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EINVALPART;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ntfs_log_perror( "read failure @ sector %d\n", fd->startSector );
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Parse the boot sector
|
2010-09-19 01:16:05 +02:00
|
|
|
fd->hiddenSectors = le32_to_cpu( boot.bpb.hidden_sectors );
|
|
|
|
fd->sectorSize = le16_to_cpu( boot.bpb.bytes_per_sector );
|
|
|
|
fd->sectorCount = sle64_to_cpu( boot.number_of_sectors );
|
2009-12-19 15:06:57 +01:00
|
|
|
fd->pos = 0;
|
2010-09-19 01:16:05 +02:00
|
|
|
fd->len = ( fd->sectorCount * fd->sectorSize );
|
|
|
|
fd->ino = le64_to_cpu( boot.volume_serial_number );
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Mark the device as read-only (if required)
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( flags & O_RDONLY )
|
|
|
|
{
|
|
|
|
NDevSetReadOnly( dev );
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Create the cache
|
2010-09-19 01:16:05 +02:00
|
|
|
fd->cache = _NTFS_cache_constructor( fd->cachePageCount, fd->cachePageSize, interface, fd->startSector + fd->sectorCount, fd->sectorSize );
|
2009-12-19 15:06:57 +01:00
|
|
|
|
|
|
|
// Mark the device as open
|
2010-09-19 01:16:05 +02:00
|
|
|
NDevSetBlock( dev );
|
|
|
|
NDevSetOpen( dev );
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-09-19 01:16:05 +02:00
|
|
|
static int ntfs_device_gekko_io_close( struct ntfs_device *dev )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "dev %p\n", dev );
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device driver descriptor
|
2010-09-19 01:16:05 +02:00
|
|
|
gekko_fd *fd = DEV_FD( dev );
|
|
|
|
if ( !fd )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the device is actually open
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !NDevOpen( dev ) )
|
|
|
|
{
|
|
|
|
ntfs_log_perror( "device is not open\n" );
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Mark the device as closed
|
2010-09-19 01:16:05 +02:00
|
|
|
NDevClearOpen( dev );
|
|
|
|
NDevClearBlock( dev );
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Flush the device (if dirty and not read-only)
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( NDevDirty( dev ) && !NDevReadOnly( dev ) )
|
|
|
|
{
|
|
|
|
ntfs_log_debug( "device is dirty, will now sync\n" );
|
2009-12-19 15:06:57 +01:00
|
|
|
|
|
|
|
// ...?
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Mark the device as clean
|
2010-09-19 01:16:05 +02:00
|
|
|
NDevClearDirty( dev );
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flush and destroy the cache (if required)
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( fd->cache )
|
|
|
|
{
|
|
|
|
_NTFS_cache_flush( fd->cache );
|
|
|
|
_NTFS_cache_destructor( fd->cache );
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Shutdown the device interface
|
|
|
|
/*const DISC_INTERFACE* interface = fd->interface;
|
|
|
|
if (interface) {
|
|
|
|
interface->shutdown();
|
|
|
|
}*/
|
|
|
|
|
|
|
|
// Free the device driver private data
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_free( dev->d_private );
|
2009-12-19 15:06:57 +01:00
|
|
|
dev->d_private = NULL;
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-09-19 01:16:05 +02:00
|
|
|
static s64 ntfs_device_gekko_io_seek( struct ntfs_device *dev, s64 offset, int whence )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "dev %p, offset %Li, whence %i\n", dev, offset, whence );
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device driver descriptor
|
2010-09-19 01:16:05 +02:00
|
|
|
gekko_fd *fd = DEV_FD( dev );
|
|
|
|
if ( !fd )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Set the current position on the device (in bytes)
|
2010-09-19 01:16:05 +02:00
|
|
|
switch ( whence )
|
|
|
|
{
|
|
|
|
case SEEK_SET: fd->pos = MIN( MAX( offset, 0 ), fd->len ); break;
|
|
|
|
case SEEK_CUR: fd->pos = MIN( MAX( fd->pos + offset, 0 ), fd->len ); break;
|
|
|
|
case SEEK_END: fd->pos = MIN( MAX( fd->len + offset, 0 ), fd->len ); break;
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-09-19 01:16:05 +02:00
|
|
|
static s64 ntfs_device_gekko_io_read( struct ntfs_device *dev, void *buf, s64 count )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
return ntfs_device_gekko_io_readbytes( dev, DEV_FD( dev )->pos, count, buf );
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-09-19 01:16:05 +02:00
|
|
|
static s64 ntfs_device_gekko_io_write( struct ntfs_device *dev, const void *buf, s64 count )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
return ntfs_device_gekko_io_writebytes( dev, DEV_FD( dev )->pos, count, buf );
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-09-19 01:16:05 +02:00
|
|
|
static s64 ntfs_device_gekko_io_pread( struct ntfs_device *dev, void *buf, s64 count, s64 offset )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
return ntfs_device_gekko_io_readbytes( dev, offset, count, buf );
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-01 00:14:14 +01:00
|
|
|
*
|
2009-12-19 15:06:57 +01:00
|
|
|
*/
|
2010-09-19 01:16:05 +02:00
|
|
|
static s64 ntfs_device_gekko_io_pwrite( struct ntfs_device *dev, const void *buf, s64 count, s64 offset )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
return ntfs_device_gekko_io_writebytes( dev, offset, count, buf );
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-09-19 01:16:05 +02:00
|
|
|
static s64 ntfs_device_gekko_io_readbytes( struct ntfs_device *dev, s64 offset, s64 count, void *buf )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "dev %p, offset %Li, count %Li\n", dev, offset, count );
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device driver descriptor
|
2010-09-19 01:16:05 +02:00
|
|
|
gekko_fd *fd = DEV_FD( dev );
|
|
|
|
if ( !fd )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device interface
|
|
|
|
const DISC_INTERFACE* interface = fd->interface;
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !interface )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( offset < 0 )
|
2010-09-16 21:59:41 +02:00
|
|
|
{
|
|
|
|
errno = EROFS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !count )
|
2010-02-01 00:14:14 +01:00
|
|
|
return 0;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
sec_t sec_start = ( sec_t ) fd->startSector;
|
2009-12-19 15:06:57 +01:00
|
|
|
sec_t sec_count = 1;
|
2010-09-19 01:16:05 +02:00
|
|
|
u32 buffer_offset = ( u32 ) ( offset % fd->sectorSize );
|
2010-02-01 00:14:14 +01:00
|
|
|
u8 *buffer = NULL;
|
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Determine the range of sectors required for this read
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( offset > 0 )
|
|
|
|
{
|
|
|
|
sec_start += ( sec_t ) floor( ( f64 ) offset / ( f64 ) fd->sectorSize );
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( buffer_offset + count > fd->sectorSize )
|
|
|
|
{
|
|
|
|
sec_count = ( sec_t ) ceil( ( f64 ) ( buffer_offset + count ) / ( f64 ) fd->sectorSize );
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// If this read happens to be on the sector boundaries then do the read straight into the destination buffer
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( ( buffer_offset == 0 ) && ( count % fd->sectorSize == 0 ) )
|
|
|
|
{
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Read from the device
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "direct read from sector %d (%d sector(s) long)\n", sec_start, sec_count );
|
|
|
|
if ( !ntfs_device_gekko_io_readsectors( dev, sec_start, sec_count, buf ) )
|
|
|
|
{
|
|
|
|
ntfs_log_perror( "direct read failure @ sector %d (%d sector(s) long)\n", sec_start, sec_count );
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
// Else read into a buffer and copy over only what was requested
|
2010-02-25 13:08:03 +01:00
|
|
|
}
|
|
|
|
else
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Allocate a buffer to hold the read data
|
2010-09-19 01:16:05 +02:00
|
|
|
buffer = ( u8* )ntfs_alloc( sec_count * fd->sectorSize );
|
|
|
|
if ( !buffer )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Read from the device
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "buffered read from sector %d (%d sector(s) long)\n", sec_start, sec_count );
|
|
|
|
ntfs_log_trace( "count: %d sec_count:%d fd->sectorSize: %d )\n", ( u32 )count, ( u32 )sec_count, ( u32 )fd->sectorSize );
|
|
|
|
if ( !ntfs_device_gekko_io_readsectors( dev, sec_start, sec_count, buffer ) )
|
|
|
|
{
|
|
|
|
ntfs_log_perror( "buffered read failure @ sector %d (%d sector(s) long)\n", sec_start, sec_count );
|
|
|
|
ntfs_free( buffer );
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Copy what was requested to the destination buffer
|
2010-09-19 01:16:05 +02:00
|
|
|
memcpy( buf, buffer + buffer_offset, count );
|
|
|
|
ntfs_free( buffer );
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-01 00:14:14 +01:00
|
|
|
*
|
2009-12-19 15:06:57 +01:00
|
|
|
*/
|
2010-09-19 01:16:05 +02:00
|
|
|
static s64 ntfs_device_gekko_io_writebytes( struct ntfs_device *dev, s64 offset, s64 count, const void *buf )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "dev %p, offset %lli, count %lli\n", dev, offset, count );
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device driver descriptor
|
2010-09-19 01:16:05 +02:00
|
|
|
gekko_fd *fd = DEV_FD( dev );
|
|
|
|
if ( !fd )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device interface
|
|
|
|
const DISC_INTERFACE* interface = fd->interface;
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !interface )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Check that the device can be written to
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( NDevReadOnly( dev ) )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EROFS;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( count < 0 || offset < 0 )
|
|
|
|
{
|
2010-09-16 21:59:41 +02:00
|
|
|
errno = EROFS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( count == 0 )
|
2010-02-01 00:14:14 +01:00
|
|
|
return 0;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
sec_t sec_start = ( sec_t ) fd->startSector;
|
2009-12-19 15:06:57 +01:00
|
|
|
sec_t sec_count = 1;
|
2010-09-19 01:16:05 +02:00
|
|
|
u32 buffer_offset = ( u32 ) ( offset % fd->sectorSize );
|
2010-02-01 00:14:14 +01:00
|
|
|
u8 *buffer = NULL;
|
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Determine the range of sectors required for this write
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( offset > 0 )
|
|
|
|
{
|
|
|
|
sec_start += ( sec_t ) floor( ( f64 ) offset / ( f64 ) fd->sectorSize );
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( ( buffer_offset + count ) > fd->sectorSize )
|
|
|
|
{
|
|
|
|
sec_count = ( sec_t ) ceil( ( f64 ) ( buffer_offset + count ) / ( f64 ) fd->sectorSize );
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// If this write happens to be on the sector boundaries then do the write straight to disc
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( ( buffer_offset == 0 ) && ( count % fd->sectorSize == 0 ) )
|
2010-09-16 21:59:41 +02:00
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
// Write to the device
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "direct write to sector %d (%d sector(s) long)\n", sec_start, sec_count );
|
|
|
|
if ( !ntfs_device_gekko_io_writesectors( dev, sec_start, sec_count, buf ) )
|
|
|
|
{
|
|
|
|
ntfs_log_perror( "direct write failure @ sector %d (%d sector(s) long)\n", sec_start, sec_count );
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
// Else write from a buffer aligned to the sector boundaries
|
2010-09-16 21:59:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
// Allocate a buffer to hold the write data
|
2010-09-19 01:16:05 +02:00
|
|
|
buffer = ( u8 * ) ntfs_alloc( sec_count * fd->sectorSize );
|
|
|
|
if ( !buffer )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Read the first and last sectors of the buffer from disc (if required)
|
2010-02-01 00:14:14 +01:00
|
|
|
// NOTE: This is done because the data does not line up with the sector boundaries,
|
2009-12-19 15:06:57 +01:00
|
|
|
// we just read in the buffer edges where the data overlaps with the rest of the disc
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( buffer_offset != 0 )
|
2010-09-16 21:59:41 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !ntfs_device_gekko_io_readsectors( dev, sec_start, 1, buffer ) )
|
|
|
|
{
|
|
|
|
ntfs_log_perror( "read failure @ sector %d\n", sec_start );
|
|
|
|
ntfs_free( buffer );
|
2010-09-16 21:59:41 +02:00
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( ( buffer_offset + count ) % fd->sectorSize != 0 )
|
2010-09-16 21:59:41 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !ntfs_device_gekko_io_readsectors( dev, sec_start + sec_count - 1, 1, buffer + ( ( sec_count - 1 ) * fd->sectorSize ) ) )
|
|
|
|
{
|
|
|
|
ntfs_log_perror( "read failure @ sector %d\n", sec_start + sec_count - 1 );
|
|
|
|
ntfs_free( buffer );
|
2010-09-16 21:59:41 +02:00
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
}
|
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Copy the data into the write buffer
|
2010-09-19 01:16:05 +02:00
|
|
|
memcpy( buffer + buffer_offset, buf, count );
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Write to the device
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "buffered write to sector %d (%d sector(s) long)\n", sec_start, sec_count );
|
|
|
|
if ( !ntfs_device_gekko_io_writesectors( dev, sec_start, sec_count, buffer ) )
|
|
|
|
{
|
|
|
|
ntfs_log_perror( "buffered write failure @ sector %d\n", sec_start );
|
|
|
|
ntfs_free( buffer );
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EIO;
|
2010-02-01 00:14:14 +01:00
|
|
|
return -1;
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Free the buffer
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_free( buffer );
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Mark the device as dirty (if we actually wrote anything)
|
2010-09-19 01:16:05 +02:00
|
|
|
NDevSetDirty( dev );
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
static bool ntfs_device_gekko_io_readsectors( struct ntfs_device *dev, sec_t sector, sec_t numSectors, void* buffer )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device driver descriptor
|
2010-09-19 01:16:05 +02:00
|
|
|
gekko_fd *fd = DEV_FD( dev );
|
|
|
|
if ( !fd )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EBADF;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Read the sectors from disc (or cache, if enabled)
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( fd->cache )
|
|
|
|
return _NTFS_cache_readSectors( fd->cache, sector, numSectors, buffer );
|
2009-12-19 15:06:57 +01:00
|
|
|
else
|
2010-09-19 01:16:05 +02:00
|
|
|
return fd->interface->readSectors( sector, numSectors, buffer );
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
static bool ntfs_device_gekko_io_writesectors( struct ntfs_device *dev, sec_t sector, sec_t numSectors, const void* buffer )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device driver descriptor
|
2010-09-19 01:16:05 +02:00
|
|
|
gekko_fd *fd = DEV_FD( dev );
|
|
|
|
if ( !fd )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EBADF;
|
2010-02-01 00:14:14 +01:00
|
|
|
return false;
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Write the sectors to disc (or cache, if enabled)
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( fd->cache )
|
|
|
|
return _NTFS_cache_writeSectors( fd->cache, sector, numSectors, buffer );
|
2009-12-19 15:06:57 +01:00
|
|
|
else
|
2010-09-19 01:16:05 +02:00
|
|
|
return fd->interface->writeSectors( sector, numSectors, buffer );
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-09-19 01:16:05 +02:00
|
|
|
static int ntfs_device_gekko_io_sync( struct ntfs_device *dev )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
gekko_fd *fd = DEV_FD( dev );
|
|
|
|
ntfs_log_trace( "dev %p\n", dev );
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Check that the device can be written to
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( NDevReadOnly( dev ) )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EROFS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the device as clean
|
2010-09-19 01:16:05 +02:00
|
|
|
NDevClearDirty( dev );
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Flush any sectors in the disc cache (if required)
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( fd->cache )
|
|
|
|
{
|
|
|
|
if ( !_NTFS_cache_flush( fd->cache ) )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-09-19 01:16:05 +02:00
|
|
|
static int ntfs_device_gekko_io_stat( struct ntfs_device *dev, struct stat *buf )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "dev %p, buf %p\n", dev, buf );
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device driver descriptor
|
2010-09-19 01:16:05 +02:00
|
|
|
gekko_fd *fd = DEV_FD( dev );
|
|
|
|
if ( !fd )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Short circuit cases were we don't actually have to do anything
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !buf )
|
2009-12-19 15:06:57 +01:00
|
|
|
return 0;
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Build the device mode
|
2010-09-19 01:16:05 +02:00
|
|
|
mode_t mode = ( S_IFBLK ) |
|
|
|
|
( S_IRUSR | S_IRGRP | S_IROTH ) |
|
|
|
|
( ( !NDevReadOnly( dev ) ) ? ( S_IWUSR | S_IWGRP | S_IWOTH ) : 0 );
|
2009-12-19 15:06:57 +01:00
|
|
|
|
|
|
|
// Zero out the stat buffer
|
2010-09-19 01:16:05 +02:00
|
|
|
memset( buf, 0, sizeof( struct stat ) );
|
2009-12-19 15:06:57 +01:00
|
|
|
|
|
|
|
// Build the device stats
|
|
|
|
buf->st_dev = fd->interface->ioType;
|
|
|
|
buf->st_ino = fd->ino;
|
|
|
|
buf->st_mode = mode;
|
|
|
|
buf->st_rdev = fd->interface->ioType;
|
|
|
|
buf->st_blksize = fd->sectorSize;
|
|
|
|
buf->st_blocks = fd->sectorCount;
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-09-19 01:16:05 +02:00
|
|
|
static int ntfs_device_gekko_io_ioctl( struct ntfs_device *dev, int request, void *argp )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "dev %p, request %i, argp %p\n", dev, request, argp );
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the device driver descriptor
|
2010-09-19 01:16:05 +02:00
|
|
|
gekko_fd *fd = DEV_FD( dev );
|
|
|
|
if ( !fd )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Figure out which i/o control was requested
|
2010-09-19 01:16:05 +02:00
|
|
|
switch ( request )
|
|
|
|
{
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
// Get block device size (sectors)
|
|
|
|
#if defined(BLKGETSIZE)
|
|
|
|
case BLKGETSIZE:
|
|
|
|
{
|
|
|
|
*( u32* )argp = fd->sectorCount;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
// Get block device size (bytes)
|
|
|
|
#if defined(BLKGETSIZE64)
|
|
|
|
case BLKGETSIZE64:
|
|
|
|
{
|
|
|
|
*( u64* )argp = ( fd->sectorCount * fd->sectorSize );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
// Get hard drive geometry
|
|
|
|
#if defined(HDIO_GETGEO)
|
|
|
|
case HDIO_GETGEO:
|
|
|
|
{
|
|
|
|
struct hd_geometry *geo = ( struct hd_geometry* )argp;
|
|
|
|
geo->sectors = 0;
|
|
|
|
geo->heads = 0;
|
|
|
|
geo->cylinders = 0;
|
|
|
|
geo->start = fd->hiddenSectors;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
// Get block device sector size (bytes)
|
|
|
|
#if defined(BLKSSZGET)
|
|
|
|
case BLKSSZGET:
|
|
|
|
{
|
|
|
|
*( int* )argp = fd->sectorSize;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Set block device block size (bytes)
|
|
|
|
#if defined(BLKBSZSET)
|
|
|
|
case BLKBSZSET:
|
|
|
|
{
|
|
|
|
int sectorSize = *( int* )argp;
|
|
|
|
fd->sectorSize = sectorSize;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Unimplemented ioctrl
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
ntfs_log_perror( "Unimplemented ioctrl %i\n", request );
|
|
|
|
errno = EOPNOTSUPP;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
2010-02-01 00:14:14 +01:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Device operations for working with gekko style devices and files.
|
|
|
|
*/
|
2010-09-19 01:16:05 +02:00
|
|
|
struct ntfs_device_operations ntfs_device_gekko_io_ops =
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
.open = ntfs_device_gekko_io_open,
|
|
|
|
.close = ntfs_device_gekko_io_close,
|
|
|
|
.seek = ntfs_device_gekko_io_seek,
|
|
|
|
.read = ntfs_device_gekko_io_read,
|
|
|
|
.write = ntfs_device_gekko_io_write,
|
|
|
|
.pread = ntfs_device_gekko_io_pread,
|
|
|
|
.pwrite = ntfs_device_gekko_io_pwrite,
|
|
|
|
.sync = ntfs_device_gekko_io_sync,
|
|
|
|
.stat = ntfs_device_gekko_io_stat,
|
|
|
|
.ioctl = ntfs_device_gekko_io_ioctl,
|
|
|
|
};
|