2001-10-03 16:41:29 +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-03 16:41:29 +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-03 16:41:29 +04:00
*/
2002-11-18 17:01:16 +03:00
# include "lib.h"
2001-10-03 16:41:29 +04:00
# include "dev-cache.h"
2001-10-25 15:34:55 +04:00
# include "lvm-types.h"
# include "btree.h"
2003-03-24 21:08:53 +03:00
# include "filter.h"
2005-03-22 01:40:35 +03:00
# include "filter-persistent.h"
2007-04-26 20:44:59 +04:00
# include "toolcontext.h"
2001-10-03 16:41:29 +04:00
# include <unistd.h>
# include <sys/param.h>
# include <dirent.h>
struct dev_iter {
2001-10-25 15:34:55 +04:00
struct btree_iter * current ;
2001-10-03 16:41:29 +04:00
struct dev_filter * filter ;
} ;
struct dir_list {
2008-11-04 01:14:30 +03:00
struct dm_list list ;
2001-10-03 16:41:29 +04:00
char dir [ 0 ] ;
} ;
static struct {
2005-10-17 03:03:59 +04:00
struct dm_pool * mem ;
struct dm_hash_table * names ;
2001-10-25 15:34:55 +04:00
struct btree * devices ;
2007-04-27 22:52:05 +04:00
struct dm_regex * preferred_names_matcher ;
2010-05-25 02:53:48 +04:00
const char * dev_dir ;
2001-10-03 16:41:29 +04:00
int has_scanned ;
2008-11-04 01:14:30 +03:00
struct dm_list dirs ;
struct dm_list files ;
2001-10-03 16:41:29 +04:00
} _cache ;
2005-10-17 03:03:59 +04:00
# define _alloc(x) dm_pool_zalloc(_cache.mem, (x))
# define _free(x) dm_pool_free(_cache.mem, (x))
# define _strdup(x) dm_pool_strdup(_cache.mem, (x))
2001-10-03 16:41:29 +04:00
2011-04-22 16:05:32 +04:00
static int _insert ( const char * path , int rec , int check_with_udev_db ) ;
2001-10-25 15:34:55 +04:00
2003-07-05 02:34:56 +04:00
struct device * dev_create_file ( const char * filename , struct device * dev ,
2005-05-03 21:28:23 +04:00
struct str_list * alias , int use_malloc )
2003-07-05 02:34:56 +04:00
{
int allocate = ! dev ;
2005-05-03 21:28:23 +04:00
if ( allocate ) {
if ( use_malloc ) {
2005-10-17 03:03:59 +04:00
if ( ! ( dev = dm_malloc ( sizeof ( * dev ) ) ) ) {
2005-05-03 21:28:23 +04:00
log_error ( " struct device allocation failed " ) ;
return NULL ;
}
2005-10-17 03:03:59 +04:00
if ( ! ( alias = dm_malloc ( sizeof ( * alias ) ) ) ) {
2005-05-03 21:28:23 +04:00
log_error ( " struct str_list allocation failed " ) ;
2005-10-17 03:03:59 +04:00
dm_free ( dev ) ;
2005-05-03 21:28:23 +04:00
return NULL ;
}
2005-10-17 03:03:59 +04:00
if ( ! ( alias - > str = dm_strdup ( filename ) ) ) {
2005-05-09 21:02:52 +04:00
log_error ( " filename strdup failed " ) ;
2005-10-17 03:03:59 +04:00
dm_free ( dev ) ;
dm_free ( alias ) ;
2005-05-09 21:02:52 +04:00
return NULL ;
}
2005-05-03 21:28:23 +04:00
dev - > flags = DEV_ALLOCED ;
} else {
if ( ! ( dev = _alloc ( sizeof ( * dev ) ) ) ) {
log_error ( " struct device allocation failed " ) ;
return NULL ;
}
if ( ! ( alias = _alloc ( sizeof ( * alias ) ) ) ) {
log_error ( " struct str_list allocation failed " ) ;
2005-05-03 21:31:56 +04:00
_free ( dev ) ;
2005-05-03 21:28:23 +04:00
return NULL ;
}
2005-05-09 21:02:52 +04:00
if ( ! ( alias - > str = _strdup ( filename ) ) ) {
log_error ( " filename strdup failed " ) ;
return NULL ;
}
2003-07-05 02:34:56 +04:00
}
2005-10-17 03:03:59 +04:00
} else if ( ! ( alias - > str = dm_strdup ( filename ) ) ) {
2005-05-03 21:31:56 +04:00
log_error ( " filename strdup failed " ) ;
return NULL ;
}
2005-05-03 21:28:23 +04:00
dev - > flags | = DEV_REGULAR ;
2008-11-04 01:14:30 +03:00
dm_list_init ( & dev - > aliases ) ;
dm_list_add ( & dev - > aliases , & alias - > list ) ;
2003-07-05 02:34:56 +04:00
dev - > end = UINT64_C ( 0 ) ;
dev - > dev = 0 ;
dev - > fd = - 1 ;
dev - > open_count = 0 ;
2010-10-13 19:40:38 +04:00
dev - > error_count = 0 ;
dev - > max_error_count = NO_DEV_ERROR_COUNT_LIMIT ;
2004-12-10 19:01:35 +03:00
dev - > block_size = - 1 ;
2009-05-20 15:09:49 +04:00
dev - > read_ahead = - 1 ;
2003-07-05 02:34:56 +04:00
memset ( dev - > pvid , 0 , sizeof ( dev - > pvid ) ) ;
2008-11-04 01:14:30 +03:00
dm_list_init ( & dev - > open_list ) ;
2003-07-05 02:34:56 +04:00
return dev ;
}
static struct device * _dev_create ( dev_t d )
2001-10-25 15:34:55 +04:00
{
struct device * dev ;
if ( ! ( dev = _alloc ( sizeof ( * dev ) ) ) ) {
2003-01-08 19:41:22 +03:00
log_error ( " struct device allocation failed " ) ;
2001-10-25 18:04:18 +04:00
return NULL ;
2001-10-25 15:34:55 +04:00
}
2003-07-05 02:34:56 +04:00
dev - > flags = 0 ;
2008-11-04 01:14:30 +03:00
dm_list_init ( & dev - > aliases ) ;
2001-10-25 15:34:55 +04:00
dev - > dev = d ;
2001-11-14 13:01:52 +03:00
dev - > fd = - 1 ;
2003-07-05 02:34:56 +04:00
dev - > open_count = 0 ;
2010-10-13 19:40:38 +04:00
dev - > max_error_count = dev_disable_after_error_count ( ) ;
2004-12-10 19:01:35 +03:00
dev - > block_size = - 1 ;
2009-05-20 15:09:49 +04:00
dev - > read_ahead = - 1 ;
2003-07-05 02:34:56 +04:00
dev - > end = UINT64_C ( 0 ) ;
2002-11-18 17:01:16 +03:00
memset ( dev - > pvid , 0 , sizeof ( dev - > pvid ) ) ;
2008-11-04 01:14:30 +03:00
dm_list_init ( & dev - > open_list ) ;
2003-01-08 19:41:22 +03:00
2001-10-25 15:34:55 +04:00
return dev ;
}
2007-04-26 20:44:59 +04:00
void dev_set_preferred_name ( struct str_list * sl , struct device * dev )
{
/*
* Don ' t interfere with ordering specified in config file .
*/
if ( _cache . preferred_names_matcher )
return ;
log_debug ( " %s: New preferred name " , sl - > str ) ;
2008-11-04 01:14:30 +03:00
dm_list_del ( & sl - > list ) ;
dm_list_add_h ( & dev - > aliases , & sl - > list ) ;
2007-04-26 20:44:59 +04:00
}
2010-08-03 17:39:27 +04:00
/*
* Check whether path0 or path1 contains the subpath . The path that
* * does not * contain the subpath wins ( return 0 or 1 ) . If both paths
* contain the subpath , return - 1. If none of them contains the subpath ,
* return - 2.
*/
static int _builtin_preference ( const char * path0 , const char * path1 ,
size_t skip_prefix_count , const char * subpath )
{
size_t subpath_len ;
int r0 , r1 ;
subpath_len = strlen ( subpath ) ;
r0 = ! strncmp ( path0 + skip_prefix_count , subpath , subpath_len ) ;
r1 = ! strncmp ( path1 + skip_prefix_count , subpath , subpath_len ) ;
if ( ! r0 & & r1 )
/* path0 does not have the subpath - it wins */
return 0 ;
else if ( r0 & & ! r1 )
/* path1 does not have the subpath - it wins */
return 1 ;
else if ( r0 & & r1 )
/* both of them have the subpath */
return - 1 ;
/* no path has the subpath */
return - 2 ;
}
static int _apply_builtin_path_preference_rules ( const char * path0 , const char * path1 )
{
size_t devdir_len ;
int r ;
devdir_len = strlen ( _cache . dev_dir ) ;
if ( ! strncmp ( path0 , _cache . dev_dir , devdir_len ) & &
! strncmp ( path1 , _cache . dev_dir , devdir_len ) ) {
/*
* We ' re trying to achieve the ordering :
* / dev / block / < / dev / dm - * < / dev / disk / < / dev / mapper / < anything else
*/
/* Prefer any other path over /dev/block/ path. */
if ( ( r = _builtin_preference ( path0 , path1 , devdir_len , " block/ " ) ) > = - 1 )
return r ;
/* Prefer any other path over /dev/dm-* path. */
if ( ( r = _builtin_preference ( path0 , path1 , devdir_len , " dm- " ) ) > = - 1 )
return r ;
/* Prefer any other path over /dev/disk/ path. */
if ( ( r = _builtin_preference ( path0 , path1 , devdir_len , " disk/ " ) ) > = - 1 )
return r ;
/* Prefer any other path over /dev/mapper/ path. */
if ( ( r = _builtin_preference ( path0 , path1 , 0 , dm_dir ( ) ) ) > = - 1 )
return r ;
}
return - 1 ;
}
2003-01-04 00:11:23 +03:00
/* Return 1 if we prefer path1 else return 0 */
static int _compare_paths ( const char * path0 , const char * path1 )
{
int slash0 = 0 , slash1 = 0 ;
2007-04-26 20:44:59 +04:00
int m0 , m1 ;
2003-01-04 00:11:23 +03:00
const char * p ;
char p0 [ PATH_MAX ] , p1 [ PATH_MAX ] ;
char * s0 , * s1 ;
struct stat stat0 , stat1 ;
2010-08-03 17:39:27 +04:00
int r ;
2003-01-04 00:11:23 +03:00
2007-04-26 21:14:57 +04:00
/*
* FIXME Better to compare patterns one - at - a - time against all names .
*/
2007-04-26 20:44:59 +04:00
if ( _cache . preferred_names_matcher ) {
2007-04-27 22:52:05 +04:00
m0 = dm_regex_match ( _cache . preferred_names_matcher , path0 ) ;
m1 = dm_regex_match ( _cache . preferred_names_matcher , path1 ) ;
2007-04-26 20:44:59 +04:00
if ( m0 ! = m1 ) {
if ( m0 < 0 )
return 1 ;
if ( m1 < 0 )
return 0 ;
if ( m0 < m1 )
return 1 ;
if ( m1 < m0 )
return 0 ;
}
}
2010-08-03 17:39:27 +04:00
/* Apply built-in preference rules first. */
if ( ( r = _apply_builtin_path_preference_rules ( path0 , path1 ) ) > = 0 )
return r ;
2010-05-25 02:53:48 +04:00
2003-01-04 00:11:23 +03:00
/* Return the path with fewer slashes */
for ( p = path0 ; p + + ; p = ( const char * ) strchr ( p , ' / ' ) )
slash0 + + ;
for ( p = path1 ; p + + ; p = ( const char * ) strchr ( p , ' / ' ) )
slash1 + + ;
if ( slash0 < slash1 )
return 0 ;
if ( slash1 < slash0 )
return 1 ;
strncpy ( p0 , path0 , PATH_MAX ) ;
strncpy ( p1 , path1 , PATH_MAX ) ;
s0 = & p0 [ 0 ] + 1 ;
s1 = & p1 [ 0 ] + 1 ;
/* We prefer symlinks - they exist for a reason!
* So we prefer a shorter path before the first symlink in the name .
* FIXME Configuration option to invert this ? */
while ( s0 ) {
s0 = strchr ( s0 , ' / ' ) ;
s1 = strchr ( s1 , ' / ' ) ;
if ( s0 ) {
* s0 = ' \0 ' ;
* s1 = ' \0 ' ;
}
if ( lstat ( p0 , & stat0 ) ) {
2007-07-23 14:45:49 +04:00
log_sys_very_verbose ( " lstat " , p0 ) ;
2003-01-04 00:11:23 +03:00
return 1 ;
}
if ( lstat ( p1 , & stat1 ) ) {
2007-07-23 14:45:49 +04:00
log_sys_very_verbose ( " lstat " , p1 ) ;
2003-01-04 00:11:23 +03:00
return 0 ;
}
if ( S_ISLNK ( stat0 . st_mode ) & & ! S_ISLNK ( stat1 . st_mode ) )
return 0 ;
if ( ! S_ISLNK ( stat0 . st_mode ) & & S_ISLNK ( stat1 . st_mode ) )
return 1 ;
if ( s0 ) {
* s0 + + = ' / ' ;
* s1 + + = ' / ' ;
}
}
/* ASCII comparison */
if ( strcmp ( path0 , path1 ) < 0 )
return 0 ;
else
return 1 ;
}
2001-10-25 15:34:55 +04:00
static int _add_alias ( struct device * dev , const char * path )
{
struct str_list * sl = _alloc ( sizeof ( * sl ) ) ;
2005-06-01 20:51:55 +04:00
struct str_list * strl ;
2003-01-04 00:11:23 +03:00
const char * oldpath ;
int prefer_old = 1 ;
2001-10-25 15:34:55 +04:00
2008-01-30 16:19:47 +03:00
if ( ! sl )
return_0 ;
2001-10-25 15:34:55 +04:00
2003-01-08 19:41:22 +03:00
/* Is name already there? */
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( strl , & dev - > aliases ) {
2005-06-01 20:51:55 +04:00
if ( ! strcmp ( strl - > str , path ) ) {
2005-03-21 17:51:49 +03:00
log_debug ( " %s: Already in device cache " , path ) ;
2003-01-08 19:41:22 +03:00
return 1 ;
}
}
2011-04-22 16:05:32 +04:00
sl - > str = path ;
2001-10-25 15:34:55 +04:00
2008-11-04 01:14:30 +03:00
if ( ! dm_list_empty ( & dev - > aliases ) ) {
oldpath = dm_list_item ( dev - > aliases . n , struct str_list ) - > str ;
2003-01-04 00:11:23 +03:00
prefer_old = _compare_paths ( path , oldpath ) ;
2003-01-08 19:41:22 +03:00
log_debug ( " %s: Aliased to %s in device cache%s " ,
path , oldpath , prefer_old ? " " : " (preferred name) " ) ;
} else
log_debug ( " %s: Added to device cache " , path ) ;
2003-01-04 00:11:23 +03:00
if ( prefer_old )
2008-11-04 01:14:30 +03:00
dm_list_add ( & dev - > aliases , & sl - > list ) ;
2003-01-04 00:11:23 +03:00
else
2008-11-04 01:14:30 +03:00
dm_list_add_h ( & dev - > aliases , & sl - > list ) ;
2003-01-04 00:11:23 +03:00
2001-10-25 15:34:55 +04:00
return 1 ;
}
/*
* Either creates a new dev , or adds an alias to
* an existing dev .
*/
static int _insert_dev ( const char * path , dev_t d )
{
struct device * dev ;
2005-05-03 21:28:23 +04:00
static dev_t loopfile_count = 0 ;
int loopfile = 0 ;
2011-04-22 16:05:32 +04:00
char * path_copy ;
2005-05-03 21:28:23 +04:00
/* Generate pretend device numbers for loopfiles */
if ( ! d ) {
2005-10-17 03:03:59 +04:00
if ( dm_hash_lookup ( _cache . names , path ) )
2005-06-14 21:54:48 +04:00
return 1 ;
2005-05-03 21:28:23 +04:00
d = + + loopfile_count ;
loopfile = 1 ;
}
2001-10-25 15:34:55 +04:00
/* is this device already registered ? */
2002-12-20 02:25:55 +03:00
if ( ! ( dev = ( struct device * ) btree_lookup ( _cache . devices ,
( uint32_t ) d ) ) ) {
2001-10-25 15:34:55 +04:00
/* create new device */
2005-05-03 21:28:23 +04:00
if ( loopfile ) {
2008-01-30 16:19:47 +03:00
if ( ! ( dev = dev_create_file ( path , NULL , NULL , 0 ) ) )
return_0 ;
} else if ( ! ( dev = _dev_create ( d ) ) )
return_0 ;
2001-10-25 15:34:55 +04:00
2002-12-20 02:25:55 +03:00
if ( ! ( btree_insert ( _cache . devices , ( uint32_t ) d , dev ) ) ) {
2009-07-16 00:02:46 +04:00
log_error ( " Couldn't insert device into binary tree. " ) ;
2001-10-25 15:34:55 +04:00
_free ( dev ) ;
return 0 ;
}
}
2011-04-22 16:05:32 +04:00
if ( ! ( path_copy = dm_pool_strdup ( _cache . mem , path ) ) ) {
log_error ( " Failed to duplicate path string. " ) ;
return 0 ;
}
if ( ! loopfile & & ! _add_alias ( dev , path_copy ) ) {
2009-07-16 00:02:46 +04:00
log_error ( " Couldn't add alias to dev cache. " ) ;
2001-10-25 15:34:55 +04:00
return 0 ;
}
2011-04-22 16:05:32 +04:00
if ( ! dm_hash_insert ( _cache . names , path_copy , dev ) ) {
2009-07-16 00:02:46 +04:00
log_error ( " Couldn't add name to hash in dev cache. " ) ;
2001-10-25 18:19:39 +04:00
return 0 ;
}
2001-10-25 15:34:55 +04:00
return 1 ;
}
2001-10-08 17:58:52 +04:00
2001-10-25 15:34:55 +04:00
static char * _join ( const char * dir , const char * name )
{
2002-12-20 02:25:55 +03:00
size_t len = strlen ( dir ) + strlen ( name ) + 2 ;
2005-10-17 03:03:59 +04:00
char * r = dm_malloc ( len ) ;
2001-10-25 15:34:55 +04:00
if ( r )
snprintf ( r , len , " %s/%s " , dir , name ) ;
return r ;
}
2001-10-03 16:41:29 +04:00
/*
* Get rid of extra slashes in the path string .
*/
static void _collapse_slashes ( char * str )
{
char * ptr ;
int was_slash = 0 ;
for ( ptr = str ; * ptr ; ptr + + ) {
if ( * ptr = = ' / ' ) {
if ( was_slash )
continue ;
was_slash = 1 ;
} else
was_slash = 0 ;
* str + + = * ptr ;
}
* str = * ptr ;
}
2001-10-25 15:34:55 +04:00
static int _insert_dir ( const char * dir )
2001-10-03 16:41:29 +04:00
{
2001-10-25 15:34:55 +04:00
int n , dirent_count , r = 1 ;
struct dirent * * dirent ;
char * path ;
2001-10-03 16:41:29 +04:00
2001-10-25 15:34:55 +04:00
dirent_count = scandir ( dir , & dirent , NULL , alphasort ) ;
if ( dirent_count > 0 ) {
for ( n = 0 ; n < dirent_count ; n + + ) {
if ( dirent [ n ] - > d_name [ 0 ] = = ' . ' ) {
free ( dirent [ n ] ) ;
continue ;
}
2001-10-03 16:41:29 +04:00
2008-01-30 16:19:47 +03:00
if ( ! ( path = _join ( dir , dirent [ n ] - > d_name ) ) )
return_0 ;
2001-10-03 16:41:29 +04:00
2001-10-25 15:34:55 +04:00
_collapse_slashes ( path ) ;
2011-04-22 16:05:32 +04:00
r & = _insert ( path , 1 , 0 ) ;
2005-10-17 03:03:59 +04:00
dm_free ( path ) ;
2001-10-03 16:41:29 +04:00
2001-10-25 15:34:55 +04:00
free ( dirent [ n ] ) ;
}
free ( dirent ) ;
}
2001-10-08 17:58:52 +04:00
2001-10-25 15:34:55 +04:00
return r ;
2001-10-08 17:58:52 +04:00
}
2005-05-03 21:28:23 +04:00
static int _insert_file ( const char * path )
{
struct stat info ;
if ( stat ( path , & info ) < 0 ) {
log_sys_very_verbose ( " stat " , path ) ;
return 0 ;
}
if ( ! S_ISREG ( info . st_mode ) ) {
log_debug ( " %s: Not a regular file " , path ) ;
return 0 ;
}
2008-01-30 16:19:47 +03:00
if ( ! _insert_dev ( path , 0 ) )
return_0 ;
2005-05-03 21:28:23 +04:00
return 1 ;
}
2011-04-22 16:05:32 +04:00
# ifdef UDEV_SYNC_SUPPORT
static int _device_in_udev_db ( const dev_t d )
{
struct udev * udev ;
struct udev_device * udev_device ;
if ( ! ( udev = udev_get_library_context ( ) ) )
return_0 ;
if ( ( udev_device = udev_device_new_from_devnum ( udev , ' b ' , d ) ) ) {
udev_device_unref ( udev_device ) ;
return 1 ;
}
return 0 ;
}
static int _insert_udev_dir ( struct udev * udev , const char * dir )
{
struct udev_enumerate * udev_enum = NULL ;
struct udev_list_entry * device_entry , * symlink_entry ;
const char * node_name , * symlink_name ;
struct udev_device * device ;
int r = 1 ;
if ( ! ( udev_enum = udev_enumerate_new ( udev ) ) )
goto bad ;
if ( udev_enumerate_add_match_subsystem ( udev_enum , " block " ) | |
udev_enumerate_scan_devices ( udev_enum ) )
goto bad ;
udev_list_entry_foreach ( device_entry , udev_enumerate_get_list_entry ( udev_enum ) ) {
device = udev_device_new_from_syspath ( udev , udev_list_entry_get_name ( device_entry ) ) ;
node_name = udev_device_get_devnode ( device ) ;
r & = _insert ( node_name , 0 , 0 ) ;
udev_list_entry_foreach ( symlink_entry , udev_device_get_devlinks_list_entry ( device ) ) {
symlink_name = udev_list_entry_get_name ( symlink_entry ) ;
r & = _insert ( symlink_name , 0 , 0 ) ;
}
udev_device_unref ( device ) ;
}
udev_enumerate_unref ( udev_enum ) ;
return r ;
bad :
log_error ( " Failed to enumerate udev device list. " ) ;
udev_enumerate_unref ( udev_enum ) ;
return 0 ;
}
static void _insert_dirs ( struct dm_list * dirs )
{
struct dir_list * dl ;
struct udev * udev ;
int with_udev ;
with_udev = obtain_device_list_from_udev ( ) & &
( udev = udev_get_library_context ( ) ) ;
dm_list_iterate_items ( dl , & _cache . dirs ) {
if ( with_udev ) {
if ( ! _insert_udev_dir ( udev , dl - > dir ) )
log_debug ( " %s: Failed to insert devices from "
" udev-managed directory to device "
" cache fully " , dl - > dir ) ;
}
else if ( ! _insert_dir ( dl - > dir ) )
2011-04-28 23:59:17 +04:00
log_debug ( " %s: Failed to insert devices to "
2011-04-22 16:05:32 +04:00
" device cache fully " , dl - > dir ) ;
}
}
# else /* UDEV_SYNC_SUPPORT */
static int _device_in_udev_db ( const dev_t d )
{
return 0 ;
}
static void _insert_dirs ( struct dm_list * dirs )
{
struct dir_list * dl ;
dm_list_iterate_items ( dl , & _cache . dirs )
_insert_dir ( dl - > dir ) ;
}
# endif /* UDEV_SYNC_SUPPORT */
static int _insert ( const char * path , int rec , int check_with_udev_db )
2001-10-08 17:58:52 +04:00
{
struct stat info ;
2001-10-25 15:34:55 +04:00
int r = 0 ;
2001-10-08 17:58:52 +04:00
if ( stat ( path , & info ) < 0 ) {
2001-10-10 20:36:32 +04:00
log_sys_very_verbose ( " stat " , path ) ;
2001-10-08 17:58:52 +04:00
return 0 ;
}
2011-04-22 16:05:32 +04:00
if ( check_with_udev_db & & ! _device_in_udev_db ( info . st_rdev ) ) {
log_very_verbose ( " %s: Not in udev db " , path ) ;
return 0 ;
}
2002-04-24 22:20:51 +04:00
if ( S_ISDIR ( info . st_mode ) ) { /* add a directory */
2004-02-13 21:55:43 +03:00
/* check it's not a symbolic link */
if ( lstat ( path , & info ) < 0 ) {
log_sys_very_verbose ( " lstat " , path ) ;
return 0 ;
}
if ( S_ISLNK ( info . st_mode ) ) {
log_debug ( " %s: Symbolic link to directory " , path ) ;
return 0 ;
}
2001-10-25 15:34:55 +04:00
if ( rec )
r = _insert_dir ( path ) ;
2001-10-08 17:58:52 +04:00
2001-10-25 15:34:55 +04:00
} else { /* add a device */
if ( ! S_ISBLK ( info . st_mode ) ) {
2002-01-17 19:39:24 +03:00
log_debug ( " %s: Not a block device " , path ) ;
2001-10-25 15:34:55 +04:00
return 0 ;
}
2001-10-08 17:58:52 +04:00
2008-01-30 16:19:47 +03:00
if ( ! _insert_dev ( path , info . st_rdev ) )
return_0 ;
2001-10-25 15:34:55 +04:00
r = 1 ;
2001-10-03 16:41:29 +04:00
}
2001-10-25 15:34:55 +04:00
return r ;
2001-10-03 16:41:29 +04:00
}
2005-03-08 16:46:17 +03:00
static void _full_scan ( int dev_scan )
2001-10-03 16:41:29 +04:00
{
2005-06-01 20:51:55 +04:00
struct dir_list * dl ;
2001-10-03 16:41:29 +04:00
2005-03-08 16:46:17 +03:00
if ( _cache . has_scanned & & ! dev_scan )
2001-10-03 16:41:29 +04:00
return ;
2011-04-22 16:05:32 +04:00
_insert_dirs ( & _cache . dirs ) ;
2001-10-03 16:41:29 +04:00
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( dl , & _cache . files )
2005-05-03 21:28:23 +04:00
_insert_file ( dl - > dir ) ;
2001-10-03 16:41:29 +04:00
_cache . has_scanned = 1 ;
2005-03-22 01:40:35 +03:00
init_full_scan_done ( 1 ) ;
2001-10-03 16:41:29 +04:00
}
2002-11-18 17:01:16 +03:00
int dev_cache_has_scanned ( void )
{
return _cache . has_scanned ;
}
void dev_cache_scan ( int do_scan )
{
if ( ! do_scan )
_cache . has_scanned = 1 ;
2005-03-22 01:40:35 +03:00
else
2005-03-08 16:46:17 +03:00
_full_scan ( 1 ) ;
2002-11-18 17:01:16 +03:00
}
2007-04-26 20:44:59 +04:00
static int _init_preferred_names ( struct cmd_context * cmd )
{
const struct config_node * cn ;
2010-12-20 16:12:55 +03:00
const struct config_value * v ;
2007-04-26 20:44:59 +04:00
struct dm_pool * scratch = NULL ;
2010-12-20 16:23:11 +03:00
const char * * regex ;
2007-04-26 20:44:59 +04:00
unsigned count = 0 ;
int i , r = 0 ;
_cache . preferred_names_matcher = NULL ;
if ( ! ( cn = find_config_tree_node ( cmd , " devices/preferred_names " ) ) | |
cn - > v - > type = = CFG_EMPTY_ARRAY ) {
log_very_verbose ( " devices/preferred_names not found in config file: "
" using built-in preferences " ) ;
return 1 ;
}
for ( v = cn - > v ; v ; v = v - > next ) {
if ( v - > type ! = CFG_STRING ) {
log_error ( " preferred_names patterns must be enclosed in quotes " ) ;
return 0 ;
2008-01-30 17:00:02 +03:00
}
2007-04-26 20:44:59 +04:00
count + + ;
}
if ( ! ( scratch = dm_pool_create ( " preferred device name matcher " , 1024 ) ) )
return_0 ;
if ( ! ( regex = dm_pool_alloc ( scratch , sizeof ( * regex ) * count ) ) ) {
log_error ( " Failed to allocate preferred device name "
" pattern list. " ) ;
goto out ;
}
for ( v = cn - > v , i = count - 1 ; v ; v = v - > next , i - - ) {
if ( ! ( regex [ i ] = dm_pool_strdup ( scratch , v - > v . str ) ) ) {
log_error ( " Failed to allocate a preferred device name "
" pattern. " ) ;
goto out ;
}
}
if ( ! ( _cache . preferred_names_matcher =
2010-12-20 16:23:11 +03:00
dm_regex_create ( _cache . mem , regex , count ) ) ) {
2007-04-26 20:44:59 +04:00
log_error ( " Preferred device name pattern matcher creation failed. " ) ;
goto out ;
}
r = 1 ;
out :
dm_pool_destroy ( scratch ) ;
return r ;
}
int dev_cache_init ( struct cmd_context * cmd )
2001-10-03 16:41:29 +04:00
{
2001-10-25 15:34:55 +04:00
_cache . names = NULL ;
2005-03-22 01:40:35 +03:00
_cache . has_scanned = 0 ;
2001-10-25 15:34:55 +04:00
2008-01-30 16:19:47 +03:00
if ( ! ( _cache . mem = dm_pool_create ( " dev_cache " , 10 * 1024 ) ) )
return_0 ;
2001-10-03 16:41:29 +04:00
2005-10-17 03:03:59 +04:00
if ( ! ( _cache . names = dm_hash_create ( 128 ) ) ) {
dm_pool_destroy ( _cache . mem ) ;
2001-10-03 16:41:29 +04:00
_cache . mem = 0 ;
2008-01-30 16:19:47 +03:00
return_0 ;
2001-10-03 16:41:29 +04:00
}
2001-10-25 15:34:55 +04:00
if ( ! ( _cache . devices = btree_create ( _cache . mem ) ) ) {
2009-07-16 00:02:46 +04:00
log_error ( " Couldn't create binary tree for dev-cache. " ) ;
2001-10-25 15:34:55 +04:00
goto bad ;
}
2010-05-25 02:53:48 +04:00
if ( ! ( _cache . dev_dir = _strdup ( cmd - > dev_dir ) ) ) {
log_error ( " strdup dev_dir failed. " ) ;
goto bad ;
}
2008-11-04 01:14:30 +03:00
dm_list_init ( & _cache . dirs ) ;
dm_list_init ( & _cache . files ) ;
2001-10-08 16:11:33 +04:00
2007-04-26 20:44:59 +04:00
if ( ! _init_preferred_names ( cmd ) )
goto_bad ;
2001-10-03 16:41:29 +04:00
return 1 ;
2001-10-25 15:34:55 +04:00
2002-04-24 22:20:51 +04:00
bad :
2001-10-25 15:34:55 +04:00
dev_cache_exit ( ) ;
return 0 ;
2001-10-03 16:41:29 +04:00
}
2002-12-20 02:25:55 +03:00
static void _check_closed ( struct device * dev )
2001-11-14 13:01:52 +03:00
{
2002-01-16 00:28:04 +03:00
if ( dev - > fd > = 0 )
2009-07-16 00:02:46 +04:00
log_error ( " Device '%s' has been left open. " , dev_name ( dev ) ) ;
2002-01-16 00:28:04 +03:00
}
2001-11-14 13:01:52 +03:00
2006-04-19 19:33:07 +04:00
static void _check_for_open_devices ( void )
2002-01-16 00:28:04 +03:00
{
2005-10-17 03:03:59 +04:00
dm_hash_iter ( _cache . names , ( dm_hash_iterate_fn ) _check_closed ) ;
2001-11-14 13:01:52 +03:00
}
2001-10-03 16:41:29 +04:00
void dev_cache_exit ( void )
{
2001-10-25 15:34:55 +04:00
if ( _cache . names )
2003-11-14 20:55:39 +03:00
_check_for_open_devices ( ) ;
2007-04-26 20:44:59 +04:00
if ( _cache . preferred_names_matcher )
_cache . preferred_names_matcher = NULL ;
2003-11-14 20:55:39 +03:00
if ( _cache . mem ) {
2005-10-17 03:03:59 +04:00
dm_pool_destroy ( _cache . mem ) ;
2003-11-14 20:55:39 +03:00
_cache . mem = NULL ;
}
if ( _cache . names ) {
2005-10-17 03:03:59 +04:00
dm_hash_destroy ( _cache . names ) ;
2003-11-14 20:55:39 +03:00
_cache . names = NULL ;
}
_cache . devices = NULL ;
_cache . has_scanned = 0 ;
2008-11-04 01:14:30 +03:00
dm_list_init ( & _cache . dirs ) ;
dm_list_init ( & _cache . files ) ;
2001-10-03 16:41:29 +04:00
}
int dev_cache_add_dir ( const char * path )
{
struct dir_list * dl ;
2001-10-23 15:50:49 +04:00
struct stat st ;
if ( stat ( path , & st ) ) {
log_error ( " Ignoring %s: %s " , path , strerror ( errno ) ) ;
/* But don't fail */
return 1 ;
}
if ( ! S_ISDIR ( st . st_mode ) ) {
log_error ( " Ignoring %s: Not a directory " , path ) ;
return 1 ;
}
2001-10-03 16:41:29 +04:00
2003-01-08 19:41:22 +03:00
if ( ! ( dl = _alloc ( sizeof ( * dl ) + strlen ( path ) + 1 ) ) ) {
log_error ( " dir_list allocation failed " ) ;
2001-10-03 16:41:29 +04:00
return 0 ;
2003-01-08 19:41:22 +03:00
}
2001-10-03 16:41:29 +04:00
strcpy ( dl - > dir , path ) ;
2008-11-04 01:14:30 +03:00
dm_list_add ( & _cache . dirs , & dl - > list ) ;
2001-10-03 16:41:29 +04:00
return 1 ;
}
2005-05-03 21:28:23 +04:00
int dev_cache_add_loopfile ( const char * path )
{
struct dir_list * dl ;
struct stat st ;
if ( stat ( path , & st ) ) {
log_error ( " Ignoring %s: %s " , path , strerror ( errno ) ) ;
/* But don't fail */
return 1 ;
}
if ( ! S_ISREG ( st . st_mode ) ) {
log_error ( " Ignoring %s: Not a regular file " , path ) ;
return 1 ;
}
if ( ! ( dl = _alloc ( sizeof ( * dl ) + strlen ( path ) + 1 ) ) ) {
log_error ( " dir_list allocation failed for file " ) ;
return 0 ;
}
strcpy ( dl - > dir , path ) ;
2008-11-04 01:14:30 +03:00
dm_list_add ( & _cache . files , & dl - > list ) ;
2005-05-03 21:28:23 +04:00
return 1 ;
}
2002-01-25 02:16:19 +03:00
/* Check cached device name is still valid before returning it */
/* This should be a rare occurrence */
2003-07-05 02:34:56 +04:00
/* set quiet if the cache is expected to be out-of-date */
2002-01-25 02:16:19 +03:00
/* FIXME Make rest of code pass/cache struct device instead of dev_name */
2003-07-05 02:34:56 +04:00
const char * dev_name_confirmed ( struct device * dev , int quiet )
2002-01-25 02:16:19 +03:00
{
struct stat buf ;
2003-07-05 02:34:56 +04:00
const char * name ;
2002-01-25 02:16:19 +03:00
int r ;
2005-05-03 21:28:23 +04:00
if ( ( dev - > flags & DEV_REGULAR ) )
return dev_name ( dev ) ;
2008-11-04 01:14:30 +03:00
while ( ( r = stat ( name = dm_list_item ( dev - > aliases . n ,
2002-04-24 22:20:51 +04:00
struct str_list ) - > str , & buf ) ) | |
2002-01-25 02:16:19 +03:00
( buf . st_rdev ! = dev - > dev ) ) {
2003-07-05 02:34:56 +04:00
if ( r < 0 ) {
if ( quiet )
log_sys_debug ( " stat " , name ) ;
else
log_sys_error ( " stat " , name ) ;
}
if ( quiet )
log_debug ( " Path %s no longer valid for device(%d,%d) " ,
2004-02-13 21:55:43 +03:00
name , ( int ) MAJOR ( dev - > dev ) ,
2003-07-05 02:34:56 +04:00
( int ) MINOR ( dev - > dev ) ) ;
else
log_error ( " Path %s no longer valid for device(%d,%d) " ,
name , ( int ) MAJOR ( dev - > dev ) ,
( int ) MINOR ( dev - > dev ) ) ;
2002-01-25 02:16:19 +03:00
/* Remove the incorrect hash entry */
2005-10-17 03:03:59 +04:00
dm_hash_remove ( _cache . names , name ) ;
2002-01-25 02:16:19 +03:00
/* Leave list alone if there isn't an alternative name */
/* so dev_name will always find something to return. */
/* Otherwise add the name to the correct device. */
2008-11-04 01:14:30 +03:00
if ( dm_list_size ( & dev - > aliases ) > 1 ) {
dm_list_del ( dev - > aliases . n ) ;
2002-01-25 02:16:19 +03:00
if ( ! r )
2011-04-22 16:05:32 +04:00
_insert ( name , 0 , obtain_device_list_from_udev ( ) ) ;
2002-01-25 02:16:19 +03:00
continue ;
}
2008-05-29 02:27:47 +04:00
/* Scanning issues this inappropriately sometimes. */
log_debug ( " Aborting - please provide new pathname for what "
2002-01-25 02:16:19 +03:00
" used to be %s " , name ) ;
return NULL ;
}
return dev_name ( dev ) ;
}
2001-10-03 16:41:29 +04:00
struct device * dev_cache_get ( const char * name , struct dev_filter * f )
{
2002-01-25 02:16:19 +03:00
struct stat buf ;
2005-10-17 03:03:59 +04:00
struct device * d = ( struct device * ) dm_hash_lookup ( _cache . names , name ) ;
2001-10-08 16:11:33 +04:00
2005-05-03 21:28:23 +04:00
if ( d & & ( d - > flags & DEV_REGULAR ) )
return d ;
2002-01-25 02:16:19 +03:00
/* If the entry's wrong, remove it */
if ( d & & ( stat ( name , & buf ) | | ( buf . st_rdev ! = d - > dev ) ) ) {
2005-10-17 03:03:59 +04:00
dm_hash_remove ( _cache . names , name ) ;
2002-01-25 02:16:19 +03:00
d = NULL ;
}
2001-10-08 17:58:52 +04:00
if ( ! d ) {
2011-04-22 16:05:32 +04:00
_insert ( name , 0 , obtain_device_list_from_udev ( ) ) ;
2005-10-17 03:03:59 +04:00
d = ( struct device * ) dm_hash_lookup ( _cache . names , name ) ;
2005-06-14 21:54:48 +04:00
if ( ! d ) {
_full_scan ( 0 ) ;
2005-10-17 03:03:59 +04:00
d = ( struct device * ) dm_hash_lookup ( _cache . names , name ) ;
2005-06-14 21:54:48 +04:00
}
2001-10-08 17:58:52 +04:00
}
2001-10-08 16:11:33 +04:00
2005-05-03 21:28:23 +04:00
return ( d & & ( ! f | | ( d - > flags & DEV_REGULAR ) | |
f - > passes_filter ( f , d ) ) ) ? d : NULL ;
2001-10-03 16:41:29 +04:00
}
2005-03-08 16:46:17 +03:00
struct dev_iter * dev_iter_create ( struct dev_filter * f , int dev_scan )
2001-10-03 16:41:29 +04:00
{
2005-10-17 03:03:59 +04:00
struct dev_iter * di = dm_malloc ( sizeof ( * di ) ) ;
2001-10-03 16:41:29 +04:00
2003-01-08 19:41:22 +03:00
if ( ! di ) {
log_error ( " dev_iter allocation failed " ) ;
2001-10-03 16:41:29 +04:00
return NULL ;
2003-01-08 19:41:22 +03:00
}
2001-10-03 16:41:29 +04:00
2006-08-01 18:56:33 +04:00
if ( dev_scan & & ! trust_cache ( ) ) {
2005-03-22 01:40:35 +03:00
/* Flag gets reset between each command */
if ( ! full_scan_done ( ) )
persistent_filter_wipe ( f ) ; /* Calls _full_scan(1) */
} else
_full_scan ( 0 ) ;
2001-10-25 15:34:55 +04:00
di - > current = btree_first ( _cache . devices ) ;
2001-10-03 16:41:29 +04:00
di - > filter = f ;
2010-09-22 05:36:13 +04:00
di - > filter - > use_count + + ;
2001-10-03 16:41:29 +04:00
return di ;
}
void dev_iter_destroy ( struct dev_iter * iter )
{
2010-09-22 05:36:13 +04:00
iter - > filter - > use_count - - ;
2005-10-17 03:03:59 +04:00
dm_free ( iter ) ;
2001-10-03 16:41:29 +04:00
}
2006-04-19 19:33:07 +04:00
static struct device * _iter_next ( struct dev_iter * iter )
2001-10-03 16:41:29 +04:00
{
2001-10-25 15:34:55 +04:00
struct device * d = btree_get_data ( iter - > current ) ;
iter - > current = btree_next ( iter - > current ) ;
2001-10-03 16:41:29 +04:00
return d ;
}
struct device * dev_iter_get ( struct dev_iter * iter )
{
while ( iter - > current ) {
struct device * d = _iter_next ( iter ) ;
2005-05-03 21:28:23 +04:00
if ( ! iter - > filter | | ( d - > flags & DEV_REGULAR ) | |
2001-10-03 16:41:29 +04:00
iter - > filter - > passes_filter ( iter - > filter , d ) )
return d ;
}
return NULL ;
}
2006-04-19 19:33:07 +04:00
2010-10-13 19:40:38 +04:00
void dev_reset_error_count ( struct cmd_context * cmd )
{
2011-01-17 18:16:55 +03:00
struct dev_iter iter ;
2010-10-13 19:40:38 +04:00
2011-01-17 18:16:55 +03:00
if ( ! _cache . devices )
2010-10-13 19:40:38 +04:00
return ;
2011-01-17 18:16:55 +03:00
iter . current = btree_first ( _cache . devices ) ;
while ( iter . current )
_iter_next ( & iter ) - > error_count = 0 ;
2010-10-13 19:40:38 +04:00
}
2006-04-19 19:33:07 +04:00
int dev_fd ( struct device * dev )
{
return dev - > fd ;
}
const char * dev_name ( const struct device * dev )
{
2008-11-04 01:14:30 +03:00
return ( dev ) ? dm_list_item ( dev - > aliases . n , struct str_list ) - > str :
2006-04-19 19:33:07 +04:00
" unknown device " ;
}