2018-04-27 16:24:05 +03:00
/*
* Copyright ( C ) 2018 Red Hat , Inc . All rights reserved .
*
* This file is part of LVM2 .
*
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
* of the GNU General Public License v .2 .
*
* 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 . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
*/
2018-06-19 21:43:31 +03:00
# include "units.h"
# include "lib/device/bcache.h"
2018-04-27 16:24:05 +03:00
# include <errno.h>
# include <stdio.h>
# include <stdlib.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
//----------------------------------------------------------------
2021-02-23 12:39:29 +03:00
# define SECTOR_SHIFT 9
2018-04-27 16:24:05 +03:00
# define SECTOR_SIZE 512
2018-05-01 12:47:40 +03:00
# define BLOCK_SIZE_SECTORS 8
2021-02-23 12:39:29 +03:00
# define PAGE_SIZE sysconf(_SC_PAGESIZE)
# define PAGE_SIZE_SECTORS (PAGE_SIZE >> SECTOR_SHIFT)
2018-05-01 12:47:40 +03:00
# define NR_BLOCKS 64
2018-04-27 16:24:05 +03:00
struct fixture {
struct io_engine * e ;
2018-04-30 19:09:24 +03:00
uint8_t * data ;
2018-04-27 16:24:05 +03:00
char fname [ 64 ] ;
int fd ;
bcache: use indirection table for fd
Add a "device index" (di) for each device, and use this
in the bcache api to the rest of lvm. This replaces the
file descriptor (fd) in the api. The rest of lvm uses
new functions bcache_set_fd(), bcache_clear_fd(), and
bcache_change_fd() to control which fd bcache uses for
io to a particular device.
. lvm opens a dev and gets and fd.
fd = open(dev);
. lvm passes fd to the bcache layer and gets a di
to use in the bcache api for the dev.
di = bcache_set_fd(fd);
. lvm uses bcache functions, passing di for the dev.
bcache_write_bytes(di, ...), etc.
. bcache translates di to fd to do io.
. lvm closes the device and clears the di/fd bcache state.
close(fd);
bcache_clear_fd(di);
In the bcache layer, a di-to-fd translation table
(int *_fd_table) is added. When bcache needs to
perform io on a di, it uses _fd_table[di].
In the following commit, lvm will make use of the new
bcache_change_fd() function to change the fd that
bcache uses for the dev, without dropping cached blocks.
2020-09-17 17:40:18 +03:00
int di ;
2018-04-27 16:24:05 +03:00
} ;
2018-05-01 12:47:40 +03:00
static void _fill_buffer ( uint8_t * buffer , uint8_t seed , size_t count )
2018-04-27 16:24:05 +03:00
{
2018-04-30 19:09:24 +03:00
unsigned i ;
2018-05-01 12:47:40 +03:00
uint8_t b = seed ;
for ( i = 0 ; i < count ; i + + ) {
buffer [ i ] = b ;
b = ( ( b < < 5 ) + b ) + i ;
}
}
static void _check_buffer ( uint8_t * buffer , uint8_t seed , size_t count )
{
unsigned i ;
uint8_t b = seed ;
for ( i = 0 ; i < count ; i + + ) {
T_ASSERT_EQUAL ( buffer [ i ] , b ) ;
b = ( ( b < < 5 ) + b ) + i ;
}
}
static void _print_buffer ( const char * name , uint8_t * buffer , size_t count )
{
unsigned col ;
fprintf ( stderr , " %s: \n " , name ) ;
while ( count ) {
for ( col = 0 ; count & & col < 20 ; col + + ) {
fprintf ( stderr , " %x, " , ( unsigned ) * buffer ) ;
col + + ;
buffer + + ;
count - - ;
}
fprintf ( stderr , " \n " ) ;
}
}
static void * _fix_init ( void )
{
2018-04-27 16:24:05 +03:00
struct fixture * f = malloc ( sizeof ( * f ) ) ;
T_ASSERT ( f ) ;
f - > e = create_async_io_engine ( ) ;
T_ASSERT ( f - > e ) ;
2021-02-23 12:39:29 +03:00
if ( posix_memalign ( ( void * * ) & f - > data , PAGE_SIZE , SECTOR_SIZE * BLOCK_SIZE_SECTORS ) )
2018-04-30 18:55:19 +03:00
test_fail ( " posix_memalign failed " ) ;
2018-04-27 16:24:05 +03:00
snprintf ( f - > fname , sizeof ( f - > fname ) , " unit-test-XXXXXX " ) ;
2018-05-13 00:23:54 +03:00
f - > fd = mkstemp ( f - > fname ) ;
2018-04-27 16:24:05 +03:00
T_ASSERT ( f - > fd > = 0 ) ;
2018-05-01 14:07:33 +03:00
_fill_buffer ( f - > data , 123 , SECTOR_SIZE * BLOCK_SIZE_SECTORS ) ;
2018-04-30 19:09:24 +03:00
2018-05-13 00:23:54 +03:00
T_ASSERT ( write ( f - > fd , f - > data , SECTOR_SIZE * BLOCK_SIZE_SECTORS ) > 0 ) ;
T_ASSERT ( lseek ( f - > fd , 0 , SEEK_SET ) ! = - 1 ) ;
2018-04-27 16:24:05 +03:00
return f ;
}
static void _fix_exit ( void * fixture )
{
struct fixture * f = fixture ;
close ( f - > fd ) ;
bcache: use indirection table for fd
Add a "device index" (di) for each device, and use this
in the bcache api to the rest of lvm. This replaces the
file descriptor (fd) in the api. The rest of lvm uses
new functions bcache_set_fd(), bcache_clear_fd(), and
bcache_change_fd() to control which fd bcache uses for
io to a particular device.
. lvm opens a dev and gets and fd.
fd = open(dev);
. lvm passes fd to the bcache layer and gets a di
to use in the bcache api for the dev.
di = bcache_set_fd(fd);
. lvm uses bcache functions, passing di for the dev.
bcache_write_bytes(di, ...), etc.
. bcache translates di to fd to do io.
. lvm closes the device and clears the di/fd bcache state.
close(fd);
bcache_clear_fd(di);
In the bcache layer, a di-to-fd translation table
(int *_fd_table) is added. When bcache needs to
perform io on a di, it uses _fd_table[di].
In the following commit, lvm will make use of the new
bcache_change_fd() function to change the fd that
bcache uses for the dev, without dropping cached blocks.
2020-09-17 17:40:18 +03:00
bcache_clear_fd ( f - > di ) ;
2018-04-27 16:24:05 +03:00
unlink ( f - > fname ) ;
free ( f - > data ) ;
2018-05-01 12:47:40 +03:00
if ( f - > e )
f - > e - > destroy ( f - > e ) ;
2018-04-27 16:24:05 +03:00
free ( f ) ;
}
static void _test_create ( void * fixture )
{
// empty
}
struct io {
bool completed ;
int error ;
} ;
static void _io_init ( struct io * io )
{
io - > completed = false ;
io - > error = 0 ;
}
static void _complete_io ( void * context , int io_error )
{
struct io * io = context ;
io - > completed = true ;
io - > error = io_error ;
}
static void _test_read ( void * fixture )
{
struct fixture * f = fixture ;
struct io io ;
2021-02-23 12:39:29 +03:00
struct bcache * cache = bcache_create ( PAGE_SIZE_SECTORS , BLOCK_SIZE_SECTORS , f - > e ) ;
bcache: use indirection table for fd
Add a "device index" (di) for each device, and use this
in the bcache api to the rest of lvm. This replaces the
file descriptor (fd) in the api. The rest of lvm uses
new functions bcache_set_fd(), bcache_clear_fd(), and
bcache_change_fd() to control which fd bcache uses for
io to a particular device.
. lvm opens a dev and gets and fd.
fd = open(dev);
. lvm passes fd to the bcache layer and gets a di
to use in the bcache api for the dev.
di = bcache_set_fd(fd);
. lvm uses bcache functions, passing di for the dev.
bcache_write_bytes(di, ...), etc.
. bcache translates di to fd to do io.
. lvm closes the device and clears the di/fd bcache state.
close(fd);
bcache_clear_fd(di);
In the bcache layer, a di-to-fd translation table
(int *_fd_table) is added. When bcache needs to
perform io on a di, it uses _fd_table[di].
In the following commit, lvm will make use of the new
bcache_change_fd() function to change the fd that
bcache uses for the dev, without dropping cached blocks.
2020-09-17 17:40:18 +03:00
T_ASSERT ( cache ) ;
f - > di = bcache_set_fd ( f - > fd ) ;
2018-04-27 16:24:05 +03:00
_io_init ( & io ) ;
bcache: use indirection table for fd
Add a "device index" (di) for each device, and use this
in the bcache api to the rest of lvm. This replaces the
file descriptor (fd) in the api. The rest of lvm uses
new functions bcache_set_fd(), bcache_clear_fd(), and
bcache_change_fd() to control which fd bcache uses for
io to a particular device.
. lvm opens a dev and gets and fd.
fd = open(dev);
. lvm passes fd to the bcache layer and gets a di
to use in the bcache api for the dev.
di = bcache_set_fd(fd);
. lvm uses bcache functions, passing di for the dev.
bcache_write_bytes(di, ...), etc.
. bcache translates di to fd to do io.
. lvm closes the device and clears the di/fd bcache state.
close(fd);
bcache_clear_fd(di);
In the bcache layer, a di-to-fd translation table
(int *_fd_table) is added. When bcache needs to
perform io on a di, it uses _fd_table[di].
In the following commit, lvm will make use of the new
bcache_change_fd() function to change the fd that
bcache uses for the dev, without dropping cached blocks.
2020-09-17 17:40:18 +03:00
T_ASSERT ( f - > e - > issue ( f - > e , DIR_READ , f - > di , 0 , BLOCK_SIZE_SECTORS , f - > data , & io ) ) ;
2018-04-27 16:24:05 +03:00
T_ASSERT ( f - > e - > wait ( f - > e , _complete_io ) ) ;
T_ASSERT ( io . completed ) ;
T_ASSERT ( ! io . error ) ;
2018-05-01 12:47:40 +03:00
_check_buffer ( f - > data , 123 , sizeof ( f - > data ) ) ;
2018-04-27 16:24:05 +03:00
}
static void _test_write ( void * fixture )
{
struct fixture * f = fixture ;
struct io io ;
2021-02-23 12:39:29 +03:00
struct bcache * cache = bcache_create ( PAGE_SIZE_SECTORS , BLOCK_SIZE_SECTORS , f - > e ) ;
bcache: use indirection table for fd
Add a "device index" (di) for each device, and use this
in the bcache api to the rest of lvm. This replaces the
file descriptor (fd) in the api. The rest of lvm uses
new functions bcache_set_fd(), bcache_clear_fd(), and
bcache_change_fd() to control which fd bcache uses for
io to a particular device.
. lvm opens a dev and gets and fd.
fd = open(dev);
. lvm passes fd to the bcache layer and gets a di
to use in the bcache api for the dev.
di = bcache_set_fd(fd);
. lvm uses bcache functions, passing di for the dev.
bcache_write_bytes(di, ...), etc.
. bcache translates di to fd to do io.
. lvm closes the device and clears the di/fd bcache state.
close(fd);
bcache_clear_fd(di);
In the bcache layer, a di-to-fd translation table
(int *_fd_table) is added. When bcache needs to
perform io on a di, it uses _fd_table[di].
In the following commit, lvm will make use of the new
bcache_change_fd() function to change the fd that
bcache uses for the dev, without dropping cached blocks.
2020-09-17 17:40:18 +03:00
T_ASSERT ( cache ) ;
f - > di = bcache_set_fd ( f - > fd ) ;
2018-04-27 16:24:05 +03:00
_io_init ( & io ) ;
bcache: use indirection table for fd
Add a "device index" (di) for each device, and use this
in the bcache api to the rest of lvm. This replaces the
file descriptor (fd) in the api. The rest of lvm uses
new functions bcache_set_fd(), bcache_clear_fd(), and
bcache_change_fd() to control which fd bcache uses for
io to a particular device.
. lvm opens a dev and gets and fd.
fd = open(dev);
. lvm passes fd to the bcache layer and gets a di
to use in the bcache api for the dev.
di = bcache_set_fd(fd);
. lvm uses bcache functions, passing di for the dev.
bcache_write_bytes(di, ...), etc.
. bcache translates di to fd to do io.
. lvm closes the device and clears the di/fd bcache state.
close(fd);
bcache_clear_fd(di);
In the bcache layer, a di-to-fd translation table
(int *_fd_table) is added. When bcache needs to
perform io on a di, it uses _fd_table[di].
In the following commit, lvm will make use of the new
bcache_change_fd() function to change the fd that
bcache uses for the dev, without dropping cached blocks.
2020-09-17 17:40:18 +03:00
T_ASSERT ( f - > e - > issue ( f - > e , DIR_WRITE , f - > di , 0 , BLOCK_SIZE_SECTORS , f - > data , & io ) ) ;
2018-04-27 16:24:05 +03:00
T_ASSERT ( f - > e - > wait ( f - > e , _complete_io ) ) ;
T_ASSERT ( io . completed ) ;
T_ASSERT ( ! io . error ) ;
}
2018-05-01 12:47:40 +03:00
static void _test_write_bytes ( void * fixture )
{
struct fixture * f = fixture ;
unsigned offset = 345 ;
char buf_out [ 32 ] ;
char buf_in [ 32 ] ;
2021-02-23 12:39:29 +03:00
struct bcache * cache = bcache_create ( PAGE_SIZE_SECTORS , BLOCK_SIZE_SECTORS , f - > e ) ;
2018-05-01 12:47:40 +03:00
T_ASSERT ( cache ) ;
bcache: use indirection table for fd
Add a "device index" (di) for each device, and use this
in the bcache api to the rest of lvm. This replaces the
file descriptor (fd) in the api. The rest of lvm uses
new functions bcache_set_fd(), bcache_clear_fd(), and
bcache_change_fd() to control which fd bcache uses for
io to a particular device.
. lvm opens a dev and gets and fd.
fd = open(dev);
. lvm passes fd to the bcache layer and gets a di
to use in the bcache api for the dev.
di = bcache_set_fd(fd);
. lvm uses bcache functions, passing di for the dev.
bcache_write_bytes(di, ...), etc.
. bcache translates di to fd to do io.
. lvm closes the device and clears the di/fd bcache state.
close(fd);
bcache_clear_fd(di);
In the bcache layer, a di-to-fd translation table
(int *_fd_table) is added. When bcache needs to
perform io on a di, it uses _fd_table[di].
In the following commit, lvm will make use of the new
bcache_change_fd() function to change the fd that
bcache uses for the dev, without dropping cached blocks.
2020-09-17 17:40:18 +03:00
f - > di = bcache_set_fd ( f - > fd ) ;
// T_ASSERT(bcache_read_bytes(cache, f->di, offset, sizeof(buf_in), buf_in));
2018-05-01 12:47:40 +03:00
_fill_buffer ( ( uint8_t * ) buf_out , 234 , sizeof ( buf_out ) ) ;
bcache: use indirection table for fd
Add a "device index" (di) for each device, and use this
in the bcache api to the rest of lvm. This replaces the
file descriptor (fd) in the api. The rest of lvm uses
new functions bcache_set_fd(), bcache_clear_fd(), and
bcache_change_fd() to control which fd bcache uses for
io to a particular device.
. lvm opens a dev and gets and fd.
fd = open(dev);
. lvm passes fd to the bcache layer and gets a di
to use in the bcache api for the dev.
di = bcache_set_fd(fd);
. lvm uses bcache functions, passing di for the dev.
bcache_write_bytes(di, ...), etc.
. bcache translates di to fd to do io.
. lvm closes the device and clears the di/fd bcache state.
close(fd);
bcache_clear_fd(di);
In the bcache layer, a di-to-fd translation table
(int *_fd_table) is added. When bcache needs to
perform io on a di, it uses _fd_table[di].
In the following commit, lvm will make use of the new
bcache_change_fd() function to change the fd that
bcache uses for the dev, without dropping cached blocks.
2020-09-17 17:40:18 +03:00
T_ASSERT ( bcache_write_bytes ( cache , f - > di , offset , sizeof ( buf_out ) , buf_out ) ) ;
T_ASSERT ( bcache_read_bytes ( cache , f - > di , offset , sizeof ( buf_in ) , buf_in ) ) ;
2018-05-01 12:47:40 +03:00
_print_buffer ( " buf_out " , ( uint8_t * ) buf_out , sizeof ( buf_out ) ) ;
_print_buffer ( " buf_in " , ( uint8_t * ) buf_in , sizeof ( buf_in ) ) ;
T_ASSERT ( ! memcmp ( buf_out , buf_in , sizeof ( buf_out ) ) ) ;
bcache_destroy ( cache ) ;
f - > e = NULL ; // already destroyed
}
2018-04-27 16:24:05 +03:00
//----------------------------------------------------------------
# define T(path, desc, fn) register_test(ts, " / base / device / bcache / io-engine / " path, desc, fn)
static struct test_suite * _tests ( void )
{
struct test_suite * ts = test_suite_create ( _fix_init , _fix_exit ) ;
if ( ! ts ) {
fprintf ( stderr , " out of memory \n " ) ;
exit ( 1 ) ;
}
T ( " create-destroy " , " simple create/destroy " , _test_create ) ;
2018-05-01 14:07:33 +03:00
T ( " read " , " read sanity check " , _test_read ) ;
T ( " write " , " write sanity check " , _test_write ) ;
2018-05-01 12:47:40 +03:00
T ( " bcache-write-bytes " , " test the utility fns " , _test_write_bytes ) ;
2018-04-27 16:24:05 +03:00
return ts ;
}
void io_engine_tests ( struct dm_list * all_tests )
{
dm_list_add ( all_tests , & _tests ( ) - > list ) ;
}