2001-10-22 18:14:00 +04:00
/*
* Copyright ( C ) 2001 Sistina Software ( UK ) Limited .
*
2001-10-31 15:47:01 +03:00
* This file is released under the LGPL .
2001-10-22 18:14:00 +04:00
*/
# include "config.h"
# include "dev-cache.h"
# include "hash.h"
# include "dbg_malloc.h"
# include "log.h"
# include "filter-persistent.h"
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
struct pfilter {
char * file ;
struct hash_table * devices ;
2001-10-23 16:24:55 +04:00
struct dev_filter * real ;
2001-10-22 18:14:00 +04:00
} ;
/*
* entries in the table can be in one of these
* states .
*/
2001-10-23 16:24:55 +04:00
# define PF_BAD_DEVICE ((void *) 1)
# define PF_GOOD_DEVICE ((void *) 2)
2001-10-22 18:14:00 +04:00
2001-10-22 18:39:12 +04:00
static int _init_hash ( struct pfilter * pf )
2001-10-22 18:14:00 +04:00
{
if ( pf - > devices )
hash_destroy ( pf - > devices ) ;
pf - > devices = hash_create ( 128 ) ;
2001-10-23 16:24:55 +04:00
return pf - > devices ? 1 : 0 ;
2001-10-22 18:14:00 +04:00
}
2001-10-23 16:24:55 +04:00
int persistent_filter_wipe ( struct dev_filter * f )
2001-10-22 18:14:00 +04:00
{
2001-10-23 16:24:55 +04:00
struct pfilter * pf = ( struct pfilter * ) f - > private ;
2001-10-22 18:14:00 +04:00
2001-10-24 21:53:50 +04:00
hash_wipe ( pf - > devices ) ;
return 1 ;
2001-10-23 16:24:55 +04:00
}
2001-10-22 18:14:00 +04:00
2001-10-23 16:24:55 +04:00
static int _read_array ( struct pfilter * pf , struct config_file * cf ,
const char * path , void * data )
{
struct config_node * cn ;
struct config_value * cv ;
2001-10-22 18:14:00 +04:00
2001-10-23 17:12:05 +04:00
if ( ! ( cn = find_config_node ( cf - > root , path , ' / ' ) ) ) {
2001-10-24 21:53:50 +04:00
log_very_verbose ( " Couldn't find %s array in '%s' " ,
path , pf - > file ) ;
2001-10-23 16:24:55 +04:00
return 0 ;
2001-10-22 18:14:00 +04:00
}
/*
* iterate through the array , adding
* devices as we go .
*/
for ( cv = cn - > v ; cv ; cv = cv - > next ) {
if ( cv - > type ! = CFG_STRING ) {
2001-10-24 21:53:50 +04:00
log_verbose ( " Devices array contains a value "
2001-10-22 18:14:00 +04:00
" which is not a string ... ignoring " ) ;
continue ;
}
2001-10-23 16:24:55 +04:00
if ( ! hash_insert ( pf - > devices , cv - > v . str , data ) )
2001-10-23 22:20:27 +04:00
log_verbose ( " Couldn't add '%s' to filter ... ignoring " ,
2001-10-22 18:14:00 +04:00
cv - > v . str ) ;
}
2001-10-23 16:24:55 +04:00
return 1 ;
}
int persistent_filter_load ( struct dev_filter * f )
{
struct pfilter * pf = ( struct pfilter * ) f - > private ;
int r = 0 ;
struct config_file * cf ;
if ( ! ( cf = create_config_file ( ) ) ) {
stack ;
return 0 ;
}
if ( ! read_config ( cf , pf - > file ) ) {
stack ;
goto out ;
}
2001-10-25 18:41:28 +04:00
_read_array ( pf , cf , " persistent_filter_cache/valid_devices " ,
PF_GOOD_DEVICE ) ;
_read_array ( pf , cf , " persistent_filter_cache/invalid_devices " ,
PF_BAD_DEVICE ) ;
2001-10-24 21:53:50 +04:00
if ( hash_get_num_entries ( pf - > devices ) )
2001-10-23 16:24:55 +04:00
r = 1 ;
2001-10-22 18:14:00 +04:00
out :
destroy_config_file ( cf ) ;
return r ;
}
2001-10-23 16:24:55 +04:00
static void _write_array ( struct pfilter * pf , FILE * fp , const char * path ,
void * data )
2001-10-22 18:14:00 +04:00
{
2001-10-23 16:24:55 +04:00
void * d ;
2001-10-22 18:14:00 +04:00
int first = 1 ;
struct hash_node * n ;
2001-10-25 18:41:28 +04:00
for ( n = hash_get_first ( pf - > devices ) ; n ;
2001-10-22 18:14:00 +04:00
n = hash_get_next ( pf - > devices , n ) ) {
2001-10-23 16:24:55 +04:00
d = hash_get_data ( pf - > devices , n ) ;
if ( d ! = data )
continue ;
2001-10-22 18:14:00 +04:00
if ( ! first )
fprintf ( fp , " , \n " ) ;
2001-10-24 21:53:50 +04:00
else {
fprintf ( fp , " \t %s=[ \n " , path ) ;
2001-10-22 18:14:00 +04:00
first = 0 ;
2001-10-24 21:53:50 +04:00
}
2001-10-22 18:14:00 +04:00
2001-10-24 21:53:50 +04:00
fprintf ( fp , " \t \t \" %s \" " , hash_get_key ( pf - > devices , n ) ) ;
2001-10-22 18:14:00 +04:00
}
2001-10-24 21:53:50 +04:00
if ( ! first )
fprintf ( fp , " \n \t ] \n " ) ;
return ;
2001-10-22 18:14:00 +04:00
}
2001-10-23 16:24:55 +04:00
int persistent_filter_dump ( struct dev_filter * f )
2001-10-22 18:14:00 +04:00
{
2001-10-23 16:24:55 +04:00
struct pfilter * pf = ( struct pfilter * ) f - > private ;
2001-10-22 18:14:00 +04:00
2001-10-25 19:07:26 +04:00
FILE * fp ;
2002-01-25 02:17:16 +03:00
if ( ! hash_get_num_entries ( pf - > devices ) ) {
log_very_verbose ( " Internal persistent device cache empty "
" - not writing to %s " , pf - > file ) ;
return 0 ;
}
2001-10-23 16:24:55 +04:00
log_very_verbose ( " Dumping persistent device cache to %s " , pf - > file ) ;
2001-10-22 18:14:00 +04:00
2001-10-25 19:07:26 +04:00
fp = fopen ( pf - > file , " w " ) ;
2001-10-23 16:24:55 +04:00
if ( ! fp ) {
2001-10-25 19:07:26 +04:00
log_sys_error ( " fopen " , pf - > file ) ;
2001-10-23 16:24:55 +04:00
return 0 ;
}
2001-10-22 18:14:00 +04:00
2001-10-23 16:24:55 +04:00
fprintf ( fp , " # This file is automatically maintained by lvm. \n \n " ) ;
2001-10-24 21:53:50 +04:00
fprintf ( fp , " persistent_filter_cache { \n " ) ;
2001-10-22 18:14:00 +04:00
2001-10-23 16:24:55 +04:00
_write_array ( pf , fp , " valid_devices " , PF_GOOD_DEVICE ) ;
_write_array ( pf , fp , " invalid_devices " , PF_BAD_DEVICE ) ;
2001-10-22 18:14:00 +04:00
2001-10-24 21:53:50 +04:00
fprintf ( fp , " } \n " ) ;
2001-10-23 16:24:55 +04:00
fclose ( fp ) ;
return 1 ;
2001-10-22 18:14:00 +04:00
}
2001-10-23 16:24:55 +04:00
static int _lookup_p ( struct dev_filter * f , struct device * dev )
2001-10-22 18:14:00 +04:00
{
struct pfilter * pf = ( struct pfilter * ) f - > private ;
2001-10-25 18:04:18 +04:00
void * l = hash_lookup ( pf - > devices , dev_name ( dev ) ) ;
2001-10-25 18:51:51 +04:00
struct str_list * sl ;
2001-10-31 15:47:01 +03:00
struct list * ah ;
2001-10-22 18:14:00 +04:00
2001-10-23 16:24:55 +04:00
if ( ! l ) {
l = pf - > real - > passes_filter ( pf - > real , dev ) ?
PF_GOOD_DEVICE : PF_BAD_DEVICE ;
2001-10-22 18:14:00 +04:00
2001-10-31 15:47:01 +03:00
list_iterate ( ah , & dev - > aliases ) {
sl = list_item ( ah , struct str_list ) ;
2001-10-25 18:51:51 +04:00
hash_insert ( pf - > devices , sl - > str , l ) ;
}
2001-10-22 18:14:00 +04:00
}
2001-10-23 16:24:55 +04:00
return l = = PF_GOOD_DEVICE ;
2001-10-22 18:14:00 +04:00
}
2001-10-22 18:39:12 +04:00
static void _destroy ( struct dev_filter * f )
2001-10-22 18:14:00 +04:00
{
struct pfilter * pf = ( struct pfilter * ) f - > private ;
hash_destroy ( pf - > devices ) ;
dbg_free ( pf - > file ) ;
2001-10-23 16:24:55 +04:00
pf - > real - > destroy ( pf - > real ) ;
2001-10-22 18:14:00 +04:00
dbg_free ( pf ) ;
dbg_free ( f ) ;
}
2001-10-23 16:24:55 +04:00
struct dev_filter * persistent_filter_create ( struct dev_filter * real ,
const char * file )
2001-10-22 18:14:00 +04:00
{
struct pfilter * pf ;
struct dev_filter * f = NULL ;
if ( ! ( pf = dbg_malloc ( sizeof ( * pf ) ) ) ) {
stack ;
return NULL ;
}
memset ( pf , 0 , sizeof ( * pf ) ) ;
if ( ! ( pf - > file = dbg_malloc ( strlen ( file ) + 1 ) ) ) {
stack ;
goto bad ;
}
strcpy ( pf - > file , file ) ;
2001-10-23 16:24:55 +04:00
pf - > real = real ;
2001-10-22 18:14:00 +04:00
if ( ! ( _init_hash ( pf ) ) ) {
2001-10-23 22:20:27 +04:00
log_error ( " Couldn't create hash table for persistent filter. " ) ;
2001-10-22 18:14:00 +04:00
goto bad ;
}
if ( ! ( f = dbg_malloc ( sizeof ( * f ) ) ) ) {
stack ;
goto bad ;
}
2001-10-23 16:24:55 +04:00
f - > passes_filter = _lookup_p ;
2001-10-22 18:14:00 +04:00
f - > destroy = _destroy ;
f - > private = pf ;
return f ;
bad :
dbg_free ( pf - > file ) ;
if ( pf - > devices )
hash_destroy ( pf - > devices ) ;
dbg_free ( pf ) ;
dbg_free ( f ) ;
return NULL ;
}