2001-10-22 18:14:00 +04:00
/*
2008-01-30 17:00:02 +03:00
* Copyright ( C ) 2001 - 2004 Sistina Software , Inc . All rights reserved .
2007-08-21 00:55:30 +04:00
* Copyright ( C ) 2004 - 2007 Red Hat , Inc . All rights reserved .
2001-10-22 18:14:00 +04:00
*
2004-03-30 23:35:44 +04:00
* 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
2007-08-21 00:55:30 +04:00
* of the GNU Lesser General Public License v .2 .1 .
2004-03-30 23:35:44 +04:00
*
2007-08-21 00:55:30 +04:00
* You should have received a copy of the GNU Lesser General Public License
2004-03-30 23:35:44 +04:00
* along with this program ; if not , write to the Free Software Foundation ,
* Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
2001-10-22 18:14:00 +04:00
*/
2002-11-18 17:01:16 +03:00
# include "lib.h"
2001-10-22 18:14:00 +04:00
# include "config.h"
# include "dev-cache.h"
# include "filter-persistent.h"
2006-11-04 06:34:10 +03:00
# include "lvm-file.h"
2008-03-12 19:03:22 +03:00
# include "lvm-string.h"
2001-10-22 18:14:00 +04:00
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
struct pfilter {
char * file ;
2005-10-17 03:03:59 +04:00
struct dm_hash_table * devices ;
2001-10-23 16:24:55 +04:00
struct dev_filter * real ;
2006-11-04 06:34:10 +03:00
time_t ctime ;
2001-10-22 18:14:00 +04:00
} ;
/*
2006-11-04 06:34:10 +03:00
* The hash table holds one of these two states
* against each entry .
2001-10-22 18:14:00 +04:00
*/
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 )
2005-10-17 03:03:59 +04:00
dm_hash_destroy ( pf - > devices ) ;
2001-10-22 18:14:00 +04:00
2008-01-30 16:19:47 +03:00
if ( ! ( pf - > devices = dm_hash_create ( 128 ) ) )
return_0 ;
2003-01-08 19:41:22 +03:00
return 1 ;
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
2005-03-22 01:40:35 +03:00
log_verbose ( " Wiping cache of LVM-capable devices " ) ;
2005-10-17 03:03:59 +04:00
dm_hash_wipe ( pf - > devices ) ;
2005-03-22 01:40:35 +03:00
2002-11-18 17:01:16 +03:00
/* Trigger complete device scan */
dev_cache_scan ( 1 ) ;
2001-10-24 21:53:50 +04:00
return 1 ;
2001-10-23 16:24:55 +04:00
}
2001-10-22 18:14:00 +04:00
2004-03-08 21:28:45 +03:00
static int _read_array ( struct pfilter * pf , struct config_tree * cft ,
2001-10-23 16:24:55 +04:00
const char * path , void * data )
{
2004-05-04 22:28:15 +04:00
const struct config_node * cn ;
2001-10-23 16:24:55 +04:00
struct config_value * cv ;
2001-10-22 18:14:00 +04:00
2004-03-08 21:28:45 +03:00
if ( ! ( cn = find_config_node ( cft - > root , path ) ) ) {
2001-10-24 21:53:50 +04:00
log_very_verbose ( " Couldn't find %s array in '%s' " ,
2002-04-24 22:20:51 +04:00
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 "
2002-04-24 22:20:51 +04:00
" which is not a string ... ignoring " ) ;
2001-10-22 18:14:00 +04:00
continue ;
}
2005-10-17 03:03:59 +04:00
if ( ! dm_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 " ,
2002-04-24 22:20:51 +04:00
cv - > v . str ) ;
2002-11-18 17:01:16 +03:00
/* Populate dev_cache ourselves */
dev_cache_get ( cv - > v . str , NULL ) ;
2001-10-22 18:14:00 +04:00
}
2001-10-23 16:24:55 +04:00
return 1 ;
}
2006-11-04 06:34:10 +03:00
int persistent_filter_load ( struct dev_filter * f , struct config_tree * * cft_out )
2001-10-23 16:24:55 +04:00
{
struct pfilter * pf = ( struct pfilter * ) f - > private ;
2004-03-08 21:28:45 +03:00
struct config_tree * cft ;
2008-01-30 17:00:02 +03:00
struct stat info ;
2006-11-04 06:34:10 +03:00
int r = 0 ;
2001-10-23 16:24:55 +04:00
2008-01-30 17:00:02 +03:00
if ( ! stat ( pf - > file , & info ) )
2006-11-04 06:34:10 +03:00
pf - > ctime = info . st_ctime ;
else {
2008-01-30 17:00:02 +03:00
log_very_verbose ( " %s: stat failed: %s " , pf - > file ,
2006-11-04 06:34:10 +03:00
strerror ( errno ) ) ;
return_0 ;
2001-10-23 16:24:55 +04:00
}
2006-11-04 06:34:10 +03:00
if ( ! ( cft = create_config_tree ( pf - > file , 1 ) ) )
return_0 ;
if ( ! read_config_file ( cft ) )
goto_out ;
2001-10-23 16:24:55 +04:00
2004-03-08 21:28:45 +03:00
_read_array ( pf , cft , " persistent_filter_cache/valid_devices " ,
2001-10-25 18:41:28 +04:00
PF_GOOD_DEVICE ) ;
2002-11-18 17:01:16 +03:00
/* We don't gain anything by holding invalid devices */
2004-03-08 21:28:45 +03:00
/* _read_array(pf, cft, "persistent_filter_cache/invalid_devices",
2002-11-18 17:01:16 +03:00
PF_BAD_DEVICE ) ; */
/* Did we find anything? */
2005-10-17 03:03:59 +04:00
if ( dm_hash_get_num_entries ( pf - > devices ) ) {
2002-11-18 17:01:16 +03:00
/* We populated dev_cache ourselves */
dev_cache_scan ( 0 ) ;
2001-10-23 16:24:55 +04:00
r = 1 ;
2002-11-18 17:01:16 +03:00
}
2001-10-22 18:14:00 +04:00
2003-01-07 00:09:04 +03:00
log_very_verbose ( " Loaded persistent filter cache from %s " , pf - > file ) ;
2002-04-24 22:20:51 +04:00
out :
2006-11-04 06:34:10 +03:00
if ( r & & cft_out )
* cft_out = cft ;
else
destroy_config_tree ( cft ) ;
2001-10-22 18:14:00 +04:00
return r ;
}
2002-11-18 17:01:16 +03:00
static void _write_array ( struct pfilter * pf , FILE * fp , const char * path ,
2001-10-23 16:24:55 +04:00
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 ;
2008-03-12 19:03:22 +03:00
char * buf , * str ;
2005-10-17 03:03:59 +04:00
struct dm_hash_node * n ;
2001-10-22 18:14:00 +04:00
2005-10-17 03:03:59 +04:00
for ( n = dm_hash_get_first ( pf - > devices ) ; n ;
n = dm_hash_get_next ( pf - > devices , n ) ) {
d = dm_hash_get_data ( pf - > devices , n ) ;
2001-10-23 16:24:55 +04:00
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
2008-03-12 19:03:22 +03:00
str = dm_hash_get_key ( pf - > devices , n ) ;
if ( ! ( buf = alloca ( escaped_len ( str ) ) ) ) {
log_error ( " persistent filter device path stack "
" allocation failed " ) ;
return ;
}
fprintf ( fp , " \t \t \" %s \" " , escape_double_quotes ( buf , str ) ) ;
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 ;
2006-11-04 06:34:10 +03:00
char * tmp_file ;
struct stat info , info2 ;
struct config_tree * cft = NULL ;
2001-10-25 19:07:26 +04:00
FILE * fp ;
2006-11-04 06:34:10 +03:00
int lockfd ;
int r = 0 ;
2002-01-25 02:17:16 +03:00
2005-10-17 03:03:59 +04:00
if ( ! dm_hash_get_num_entries ( pf - > devices ) ) {
2002-01-25 02:17:16 +03:00
log_very_verbose ( " Internal persistent device cache empty "
" - not writing to %s " , pf - > file ) ;
return 0 ;
}
2002-11-18 17:01:16 +03:00
if ( ! dev_cache_has_scanned ( ) ) {
log_very_verbose ( " Device cache incomplete - 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
2006-11-04 06:34:10 +03:00
while ( 1 ) {
if ( ( lockfd = fcntl_lock_file ( pf - > file , F_WRLCK , 0 ) ) < 0 )
return_0 ;
/*
* Ensure we locked the file we expected
*/
if ( fstat ( lockfd , & info ) ) {
log_sys_error ( " fstat " , pf - > file ) ;
goto out ;
}
if ( stat ( pf - > file , & info2 ) ) {
log_sys_error ( " stat " , pf - > file ) ;
goto out ;
}
2007-07-20 19:22:46 +04:00
if ( is_same_inode ( info , info2 ) )
2006-11-04 06:34:10 +03:00
break ;
fcntl_unlock_file ( lockfd ) ;
}
/*
* If file contents changed since we loaded it , merge new contents
*/
if ( info . st_ctime ! = pf - > ctime )
/* Keep cft open to avoid losing lock */
persistent_filter_load ( f , & cft ) ;
tmp_file = alloca ( strlen ( pf - > file ) + 5 ) ;
sprintf ( tmp_file , " %s.tmp " , pf - > file ) ;
if ( ! ( fp = fopen ( tmp_file , " w " ) ) ) {
/* EACCES has been reported over NFS */
if ( errno ! = EROFS & & errno ! = EACCES )
log_sys_error ( " fopen " , tmp_file ) ;
goto out ;
2001-10-23 16:24:55 +04:00
}
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 ) ;
2002-11-18 17:01:16 +03:00
/* We don't gain anything by remembering invalid devices */
/* _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 " ) ;
2007-07-24 21:48:08 +04:00
if ( lvm_fclose ( fp , tmp_file ) )
goto_out ;
2006-11-04 06:34:10 +03:00
if ( rename ( tmp_file , pf - > file ) )
log_error ( " %s: rename to %s failed: %s " , tmp_file , pf - > file ,
strerror ( errno ) ) ;
r = 1 ;
out :
fcntl_unlock_file ( lockfd ) ;
if ( cft )
destroy_config_tree ( cft ) ;
return r ;
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 ;
2005-10-17 03:03:59 +04:00
void * l = dm_hash_lookup ( pf - > devices , dev_name ( dev ) ) ;
2001-10-25 18:51:51 +04:00
struct str_list * sl ;
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 ) ?
2002-04-24 22:20:51 +04:00
PF_GOOD_DEVICE : PF_BAD_DEVICE ;
2001-10-22 18:14:00 +04:00
2005-06-01 20:51:55 +04:00
list_iterate_items ( sl , & dev - > aliases )
2005-10-17 03:03:59 +04:00
dm_hash_insert ( pf - > devices , sl - > str , l ) ;
2005-06-01 20:51:55 +04:00
2004-11-24 23:36:52 +03:00
} else if ( l = = PF_BAD_DEVICE )
log_debug ( " %s: Skipping (cached) " , dev_name ( dev ) ) ;
2001-10-22 18:14:00 +04:00
2004-11-24 23:36:52 +03:00
return ( l = = PF_BAD_DEVICE ) ? 0 : 1 ;
2001-10-22 18:14:00 +04:00
}
2006-04-19 19:33:07 +04:00
static void _persistent_destroy ( struct dev_filter * f )
2001-10-22 18:14:00 +04:00
{
struct pfilter * pf = ( struct pfilter * ) f - > private ;
2005-10-17 03:03:59 +04:00
dm_hash_destroy ( pf - > devices ) ;
dm_free ( pf - > file ) ;
2001-10-23 16:24:55 +04:00
pf - > real - > destroy ( pf - > real ) ;
2005-10-17 03:03:59 +04:00
dm_free ( pf ) ;
dm_free ( f ) ;
2001-10-22 18:14:00 +04:00
}
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 ;
2008-01-30 16:19:47 +03:00
if ( ! ( pf = dm_malloc ( sizeof ( * pf ) ) ) )
return_NULL ;
2001-10-22 18:14:00 +04:00
memset ( pf , 0 , sizeof ( * pf ) ) ;
2007-04-26 20:44:59 +04:00
if ( ! ( pf - > file = dm_malloc ( strlen ( file ) + 1 ) ) )
goto_bad ;
2001-10-22 18:14:00 +04:00
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 ;
}
2007-04-26 20:44:59 +04:00
if ( ! ( f = dm_malloc ( sizeof ( * f ) ) ) )
goto_bad ;
2001-10-22 18:14:00 +04:00
2001-10-23 16:24:55 +04:00
f - > passes_filter = _lookup_p ;
2006-04-19 19:33:07 +04:00
f - > destroy = _persistent_destroy ;
2001-10-22 18:14:00 +04:00
f - > private = pf ;
return f ;
2002-04-24 22:20:51 +04:00
bad :
2005-10-17 03:03:59 +04:00
dm_free ( pf - > file ) ;
2001-10-22 18:14:00 +04:00
if ( pf - > devices )
2005-10-17 03:03:59 +04:00
dm_hash_destroy ( pf - > devices ) ;
dm_free ( pf ) ;
dm_free ( f ) ;
2001-10-22 18:14:00 +04:00
return NULL ;
}