2001-10-03 16:41:29 +04:00
/*
* Copyright ( C ) 2001 Sistina Software
*
* This file is released under the GPL .
*/
2001-10-04 14:13:07 +04:00
# include "device.h"
# include "lvm-types.h"
# include "log.h"
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
# include <sys/ioctl.h>
# include <sys/mount.h>
2001-10-03 16:41:29 +04:00
int dev_get_size ( struct device * dev , uint64_t * size )
{
int fd ;
long s ;
2001-10-10 20:36:32 +04:00
log_very_verbose ( " Getting size of %s " , dev - > name ) ;
2001-10-04 14:13:07 +04:00
if ( ( fd = open ( dev - > name , O_RDONLY ) ) < 0 ) {
2001-10-10 20:36:32 +04:00
log_sys_error ( " open " , dev - > name ) ;
2001-10-03 16:41:29 +04:00
return 0 ;
}
/* FIXME: add 64 bit ioctl */
if ( ioctl ( fd , BLKGETSIZE , & s ) < 0 ) {
2001-10-10 20:36:32 +04:00
log_sys_error ( " ioctl BLKGETSIZE " , dev - > name ) ;
2001-10-03 16:41:29 +04:00
close ( fd ) ;
return 0 ;
}
close ( fd ) ;
* size = ( uint64_t ) s ;
return 1 ;
}
2001-10-10 17:03:10 +04:00
/*
* FIXME : factor common code out .
*/
2001-10-08 20:08:16 +04:00
int _read ( int fd , void * buf , size_t count )
{
size_t n = 0 ;
int tot = 0 ;
while ( tot < count ) {
2001-10-23 15:16:30 +04:00
do
n = read ( fd , buf , count - tot ) ;
while ( ( n < 0 ) & & ( ( errno = = EINTR ) | | ( errno = = EAGAIN ) ) ) ;
2001-10-08 20:08:16 +04:00
if ( n < = 0 )
return tot ? tot : n ;
tot + = n ;
buf + = n ;
}
return tot ;
}
2001-10-03 16:41:29 +04:00
int64_t dev_read ( struct device * dev , uint64_t offset ,
int64_t len , void * buffer )
{
2001-10-08 20:08:16 +04:00
int64_t r ;
int fd = open ( dev - > name , O_RDONLY ) ;
if ( fd < 0 ) {
2001-10-10 20:36:32 +04:00
log_sys_very_verbose ( " open " , dev - > name ) ;
2001-10-08 20:08:16 +04:00
return 0 ;
}
if ( lseek ( fd , offset , SEEK_SET ) < 0 ) {
2001-10-10 20:36:32 +04:00
log_sys_error ( " lseek " , dev - > name ) ;
2001-10-08 20:08:16 +04:00
return 0 ;
}
r = _read ( fd , buffer , len ) ;
close ( fd ) ;
return r ;
2001-10-03 16:41:29 +04:00
}
2001-10-10 17:03:10 +04:00
int _write ( int fd , const void * buf , size_t count )
{
size_t n = 0 ;
int tot = 0 ;
while ( tot < count ) {
2001-10-23 15:16:30 +04:00
do
n = write ( fd , buf , count - tot ) ;
while ( ( n < 0 ) & & ( ( errno = = EINTR ) | | ( errno = = EAGAIN ) ) ) ;
2001-10-10 17:03:10 +04:00
if ( n < = 0 )
return tot ? tot : n ;
tot + = n ;
buf + = n ;
}
return tot ;
}
2001-10-03 16:41:29 +04:00
int64_t dev_write ( struct device * dev , uint64_t offset ,
int64_t len , void * buffer )
{
2001-10-10 17:03:10 +04:00
int64_t r ;
int fd = open ( dev - > name , O_WRONLY ) ;
if ( fd < 0 ) {
2001-10-10 20:36:32 +04:00
log_sys_error ( " open " , dev - > name ) ;
2001-10-10 17:03:10 +04:00
return 0 ;
}
if ( lseek ( fd , offset , SEEK_SET ) < 0 ) {
2001-10-10 20:36:32 +04:00
log_sys_error ( " lseek " , dev - > name ) ;
2001-10-10 17:03:10 +04:00
return 0 ;
}
r = _write ( fd , buffer , len ) ;
close ( fd ) ;
return r ;
2001-10-03 16:41:29 +04:00
}