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 ,
2016-01-21 13:49:46 +03:00
* Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
2001-10-22 18:14:00 +04:00
*/
2018-06-08 15:40:53 +03:00
# include "base/memory/zalloc.h"
2018-05-14 12:30:20 +03:00
# include "lib/misc/lib.h"
# include "lib/filters/filter.h"
# include "lib/config/config.h"
2001-10-22 18:14:00 +04:00
struct pfilter {
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 ;
2013-06-12 14:08:56 +04:00
struct dev_types * dt ;
2001-10-22 18:14:00 +04:00
} ;
2018-05-04 01:12:07 +03:00
/*
* The persistent filter is filter layer that sits above the other filters and
* caches the final result of those other filters . When a device is first
* checked against filters , it will not be in this cache , so this filter will
* pass the device down to the other filters to check it . The other filters
* will run and either include the device ( good / pass ) or exclude the device
* ( bad / fail ) . That good or bad result propagates up through this filter which
* saves the result . The next time some code checks the filters against the
* device , this persistent / cache filter is checked first . This filter finds
* the previous result in its cache and returns it without reevaluating the
* other real filters .
*
* FIXME : a cache like this should not be needed . The fact it ' s needed is a
* symptom of code that should be fixed to not reevaluate filters multiple
* times . A device should be checked against the filter once , and then not
* need to be checked again . With scanning now controlled , we could probably
* do this .
*/
2018-06-21 18:52:35 +03:00
static int _good_device ;
static int _bad_device ;
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
*/
2018-06-21 18:52:35 +03:00
# define PF_BAD_DEVICE ((void *) &_good_device)
# define PF_GOOD_DEVICE ((void *) &_bad_device)
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
2024-05-26 01:33:16 +03:00
if ( ! ( pf - > devices = dm_hash_create ( 8191 ) ) )
2008-01-30 16:19:47 +03:00
return_0 ;
2003-01-08 19:41:22 +03:00
return 1 ;
2001-10-22 18:14:00 +04:00
}
2020-10-20 23:17:56 +03:00
static void _persistent_filter_wipe ( struct cmd_context * cmd , struct dev_filter * f , struct device * dev , const char * use_filter_name )
2001-10-22 18:14:00 +04:00
{
2001-10-23 16:24:55 +04:00
struct pfilter * pf = ( struct pfilter * ) f - > private ;
2020-10-20 23:17:56 +03:00
struct dm_str_list * sl ;
2001-10-22 18:14:00 +04:00
2020-10-20 23:17:56 +03:00
if ( ! dev ) {
dm_hash_wipe ( pf - > devices ) ;
} else {
dm_list_iterate_items ( sl , & dev - > aliases )
dm_hash_remove ( pf - > devices , sl - > str ) ;
}
2001-10-23 16:24:55 +04:00
}
2001-10-22 18:14:00 +04:00
2018-12-07 23:35:22 +03:00
static int _lookup_p ( struct cmd_context * cmd , struct dev_filter * f , struct device * dev , const char * use_filter_name )
2001-10-22 18:14:00 +04:00
{
struct pfilter * pf = ( struct pfilter * ) f - > private ;
2018-05-30 01:02:27 +03:00
void * l ;
2014-05-29 11:41:03 +04:00
struct dm_str_list * sl ;
2018-05-04 01:12:07 +03:00
int pass = 1 ;
2001-10-22 18:14:00 +04:00
2018-12-07 23:35:22 +03:00
if ( use_filter_name & & strcmp ( f - > name , use_filter_name ) )
return pf - > real - > passes_filter ( cmd , pf - > real , dev , use_filter_name ) ;
2018-05-30 01:02:27 +03:00
if ( dm_list_empty ( & dev - > aliases ) ) {
2024-05-28 17:17:53 +03:00
log_debug_devs ( " %u:%u: filter cache skipping (no name). " ,
MAJOR ( dev - > dev ) , MINOR ( dev - > dev ) ) ;
2018-05-30 01:02:27 +03:00
return 0 ;
}
l = dm_hash_lookup ( pf - > devices , dev_name ( dev ) ) ;
2018-05-04 01:12:07 +03:00
/* Cached bad, skip dev */
2010-07-02 06:09:57 +04:00
if ( l = = PF_BAD_DEVICE ) {
2018-05-04 01:12:07 +03:00
log_debug_devs ( " %s: filter cache skipping (cached bad) " , dev_name ( dev ) ) ;
2010-07-02 06:09:57 +04:00
return 0 ;
}
2018-05-04 01:12:07 +03:00
/* Cached good, use dev */
if ( l = = PF_GOOD_DEVICE ) {
log_debug_devs ( " %s: filter cache using (cached good) " , dev_name ( dev ) ) ;
return 1 ;
2010-07-02 06:09:57 +04:00
}
2018-05-04 01:12:07 +03:00
/* Uncached, check filters and cache the result */
2001-10-23 16:24:55 +04:00
if ( ! l ) {
2018-12-07 23:35:22 +03:00
pass = pf - > real - > passes_filter ( cmd , pf - > real , dev , use_filter_name ) ;
2018-05-04 01:12:07 +03:00
if ( ! pass ) {
/*
* A device that does not pass one filter is excluded
* even if the result of another filter is deferred ,
* because the deferred result won ' t change the exclude .
*/
l = PF_BAD_DEVICE ;
2022-04-01 23:06:03 +03:00
} else if ( pass = = 1 ) {
l = PF_GOOD_DEVICE ;
} else {
log_error ( " Ignore invalid filter result %d %s " , pass , dev_name ( dev ) ) ;
2018-05-04 01:12:07 +03:00
pass = 1 ;
2022-04-01 23:06:03 +03:00
/* don't cache invalid result */
2018-05-04 01:12:07 +03:00
goto out ;
}
2021-10-14 19:16:49 +03:00
if ( ! dev - > filtered_flags ) /* skipping reason already logged by filter */
log_debug_devs ( " filter caching %s %s " , pass ? " good " : " bad " , dev_name ( dev ) ) ;
2001-10-22 18:14:00 +04:00
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( sl , & dev - > aliases )
2012-02-28 15:12:58 +04:00
if ( ! dm_hash_insert ( pf - > devices , sl - > str , l ) ) {
log_error ( " Failed to hash alias to filter. " ) ;
return 0 ;
}
2010-07-02 06:09:57 +04:00
}
2018-05-04 01:12:07 +03:00
out :
return pass ;
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 ;
2010-09-22 05:36:13 +04:00
if ( f - > use_count )
log_error ( INTERNAL_ERROR " Destroying persistent filter while in use %u times. " , f - > use_count ) ;
2005-10-17 03:03:59 +04:00
dm_hash_destroy ( pf - > devices ) ;
2001-10-23 16:24:55 +04:00
pf - > real - > destroy ( pf - > real ) ;
2018-06-08 15:40:53 +03:00
free ( pf ) ;
free ( f ) ;
2001-10-22 18:14:00 +04:00
}
2018-06-13 22:00:47 +03:00
struct dev_filter * persistent_filter_create ( struct dev_types * dt , struct dev_filter * real )
2001-10-22 18:14:00 +04:00
{
struct pfilter * pf ;
struct dev_filter * f = NULL ;
2018-06-08 15:40:53 +03:00
if ( ! ( pf = zalloc ( sizeof ( * pf ) ) ) ) {
2011-01-06 18:29:24 +03:00
log_error ( " Allocation of persistent filter failed. " ) ;
return NULL ;
}
2001-10-22 18:14:00 +04:00
2013-06-12 14:08:56 +04:00
pf - > dt = dt ;
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 ;
}
2018-06-08 15:40:53 +03:00
if ( ! ( f = zalloc ( sizeof ( * f ) ) ) ) {
2011-01-06 18:29:24 +03:00
log_error ( " Allocation of device filter for persistent filter failed. " ) ;
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 ;
2010-09-22 05:36:13 +04:00
f - > use_count = 0 ;
2001-10-22 18:14:00 +04:00
f - > private = pf ;
2012-08-13 21:44:10 +04:00
f - > wipe = _persistent_filter_wipe ;
2018-12-07 23:35:22 +03:00
f - > name = " persistent " ;
2001-10-22 18:14:00 +04:00
2013-08-14 02:26:58 +04:00
log_debug_devs ( " Persistent filter initialised. " ) ;
2001-10-22 18:14:00 +04:00
return f ;
2002-04-24 22:20:51 +04:00
bad :
2001-10-22 18:14:00 +04:00
if ( pf - > devices )
2005-10-17 03:03:59 +04:00
dm_hash_destroy ( pf - > devices ) ;
2018-06-08 15:40:53 +03:00
free ( pf ) ;
free ( f ) ;
2001-10-22 18:14:00 +04:00
return NULL ;
}