2001-12-11 14:42:30 +03:00
/*
2004-03-30 23:35:44 +04:00
* Copyright ( C ) 2002 - 2004 Sistina Software , Inc . All rights reserved .
2018-04-20 18:43:50 +03:00
* Copyright ( C ) 2004 - 2007 Red Hat , Inc . All rights reserved .
2001-12-11 14:42:30 +03: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-12-11 14:42:30 +03:00
*/
2018-05-14 12:30:20 +03:00
# include "lib/misc/lib.h"
2020-01-30 13:03:27 +03:00
# include "base/memory/zalloc.h"
2018-05-14 12:30:20 +03:00
# include "lib/label/label.h"
# include "lib/misc/crc.h"
# include "lib/mm/xlate.h"
# include "lib/cache/lvmcache.h"
# include "lib/device/bcache.h"
# include "lib/commands/toolcontext.h"
# include "lib/activate/activate.h"
2018-12-07 23:35:22 +03:00
# include "lib/label/hints.h"
2019-03-04 21:13:09 +03:00
# include "lib/metadata/metadata.h"
2019-10-30 23:11:57 +03:00
# include "lib/format_text/layout.h"
device usage based on devices file
The LVM devices file lists devices that lvm can use. The default
file is /etc/lvm/devices/system.devices, and the lvmdevices(8)
command is used to add or remove device entries. If the file
does not exist, or if lvm.conf includes use_devicesfile=0, then
lvm will not use a devices file. When the devices file is in use,
the regex filter is not used, and the filter settings in lvm.conf
or on the command line are ignored.
LVM records devices in the devices file using hardware-specific
IDs, such as the WWID, and attempts to use subsystem-specific
IDs for virtual device types. These device IDs are also written
in the VG metadata. When no hardware or virtual ID is available,
lvm falls back using the unstable device name as the device ID.
When devnames are used, lvm performs extra scanning to find
devices if their devname changes, e.g. after reboot.
When proper device IDs are used, an lvm command will not look
at devices outside the devices file, but when devnames are used
as a fallback, lvm will scan devices outside the devices file
to locate PVs on renamed devices. A config setting
search_for_devnames can be used to control the scanning for
renamed devname entries.
Related to the devices file, the new command option
--devices <devnames> allows a list of devices to be specified for
the command to use, overriding the devices file. The listed
devices act as a sort of devices file in terms of limiting which
devices lvm will see and use. Devices that are not listed will
appear to be missing to the lvm command.
Multiple devices files can be kept in /etc/lvm/devices, which
allows lvm to be used with different sets of devices, e.g.
system devices do not need to be exposed to a specific application,
and the application can use lvm on its own set of devices that are
not exposed to the system. The option --devicesfile <filename> is
used to select the devices file to use with the command. Without
the option set, the default system devices file is used.
Setting --devicesfile "" causes lvm to not use a devices file.
An existing, empty devices file means lvm will see no devices.
The new command vgimportdevices adds PVs from a VG to the devices
file and updates the VG metadata to include the device IDs.
vgimportdevices -a will import all VGs into the system devices file.
LVM commands run by dmeventd not use a devices file by default,
and will look at all devices on the system. A devices file can
be created for dmeventd (/etc/lvm/devices/dmeventd.devices) If
this file exists, lvm commands run by dmeventd will use it.
Internal implementaion:
- device_ids_read - read the devices file
. add struct dev_use (du) to cmd->use_devices for each devices file entry
- dev_cache_scan - get /dev entries
. add struct device (dev) to dev_cache for each device on the system
- device_ids_match - match devices file entries to /dev entries
. match each du on cmd->use_devices to a dev in dev_cache, using device ID
. on match, set du->dev, dev->id, dev->flags MATCHED_USE_ID
- label_scan - read lvm headers and metadata from devices
. filters are applied, those that do not need data from the device
. filter-deviceid skips devs without MATCHED_USE_ID, i.e.
skips /dev entries that are not listed in the devices file
. read lvm label from dev
. filters are applied, those that use data from the device
. read lvm metadata from dev
. add info/vginfo structs for PVs/VGs (info is "lvmcache")
- device_ids_find_renamed_devs - handle devices with unstable devname ID
where devname changed
. this step only needed when devs do not have proper device IDs,
and their dev names change, e.g. after reboot sdb becomes sdc.
. detect incorrect match because PVID in the devices file entry
does not match the PVID found when the device was read above
. undo incorrect match between du and dev above
. search system devices for new location of PVID
. update devices file with new devnames for PVIDs on renamed devices
. label_scan the renamed devs
- continue with command processing
2020-06-23 21:25:41 +03:00
# include "lib/device/device_id.h"
2021-11-05 20:19:35 +03:00
# include "lib/device/online.h"
2002-11-18 17:04:08 +03:00
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
2018-06-08 15:40:53 +03:00
# include <sys/types.h>
2019-08-08 21:11:05 +03:00
# include <sys/resource.h>
2002-11-18 17:04:08 +03:00
/* FIXME Allow for larger labels? Restricted to single sector currently */
2001-12-11 14:42:30 +03:00
2019-03-04 21:13:09 +03:00
static uint64_t _current_bcache_size_bytes ;
2002-01-10 18:01:58 +03:00
/*
* Internal labeller struct .
*/
struct labeller_i {
2008-11-04 01:14:30 +03:00
struct dm_list list ;
2001-12-11 14:42:30 +03:00
2002-01-10 18:01:58 +03:00
struct labeller * l ;
2021-09-22 18:05:09 +03:00
char name [ 0 ] ;
2002-01-10 18:01:58 +03:00
} ;
2001-12-13 11:40:47 +03:00
2008-11-04 01:14:30 +03:00
static struct dm_list _labellers ;
2001-12-11 14:42:30 +03:00
2002-01-10 18:01:58 +03:00
static struct labeller_i * _alloc_li ( const char * name , struct labeller * l )
2001-12-11 14:42:30 +03:00
{
2002-01-10 18:01:58 +03:00
struct labeller_i * li ;
size_t len ;
2001-12-11 14:42:30 +03:00
2024-04-09 12:40:20 +03:00
len = strlen ( name ) ;
2001-12-11 14:42:30 +03:00
2024-04-09 12:40:20 +03:00
if ( ! ( li = malloc ( sizeof ( * li ) + len + 1 ) ) ) {
2002-01-16 03:01:36 +03:00
log_error ( " Couldn't allocate memory for labeller list object. " ) ;
2002-01-10 18:01:58 +03:00
return NULL ;
}
li - > l = l ;
2024-04-09 12:40:20 +03:00
memcpy ( li - > name , name , len + 1 ) ;
2002-01-10 18:01:58 +03:00
return li ;
2001-12-12 12:05:44 +03:00
}
2001-12-11 14:42:30 +03:00
2002-01-10 18:01:58 +03:00
int label_init ( void )
{
2008-11-04 01:14:30 +03:00
dm_list_init ( & _labellers ) ;
2002-01-10 19:48:28 +03:00
return 1 ;
2001-12-11 14:42:30 +03:00
}
2002-01-10 18:01:58 +03:00
void label_exit ( void )
2001-12-14 16:15:15 +03:00
{
2012-02-08 15:12:18 +04:00
struct labeller_i * li , * tli ;
2002-01-10 18:01:58 +03:00
2012-02-08 15:12:18 +04:00
dm_list_iterate_items_safe ( li , tli , & _labellers ) {
dm_list_del ( & li - > list ) ;
2002-11-18 17:04:08 +03:00
li - > l - > ops - > destroy ( li - > l ) ;
2018-06-08 15:40:53 +03:00
free ( li ) ;
2002-01-10 18:01:58 +03:00
}
2004-05-05 01:25:57 +04:00
2008-11-04 01:14:30 +03:00
dm_list_init ( & _labellers ) ;
2001-12-14 16:15:15 +03:00
}
2013-07-29 17:58:18 +04:00
int label_register_handler ( struct labeller * handler )
2001-12-11 14:42:30 +03:00
{
2002-01-10 18:01:58 +03:00
struct labeller_i * li ;
2013-07-29 17:58:18 +04:00
if ( ! ( li = _alloc_li ( handler - > fmt - > name , handler ) ) )
2008-01-30 16:19:47 +03:00
return_0 ;
2001-12-11 14:42:30 +03:00
2008-11-04 01:14:30 +03:00
dm_list_add ( & _labellers , & li - > list ) ;
2002-01-10 18:01:58 +03:00
return 1 ;
2001-12-11 14:42:30 +03:00
}
2002-01-10 18:01:58 +03:00
struct labeller * label_get_handler ( const char * name )
2001-12-11 14:42:30 +03:00
{
2002-01-10 18:01:58 +03:00
struct labeller_i * li ;
2001-12-11 14:42:30 +03:00
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( li , & _labellers )
2002-01-10 18:01:58 +03:00
if ( ! strcmp ( li - > name , name ) )
return li - > l ;
2001-12-11 14:42:30 +03:00
2002-01-10 18:01:58 +03:00
return NULL ;
}
2001-12-11 14:42:30 +03:00
2002-11-18 17:04:08 +03:00
/* FIXME Also wipe associated metadata area headers? */
2002-01-11 13:43:32 +03:00
int label_remove ( struct device * dev )
2001-12-11 14:42:30 +03:00
{
2018-02-20 00:40:44 +03:00
char readbuf [ LABEL_SIZE ] __attribute__ ( ( aligned ( 8 ) ) ) ;
2002-11-18 17:04:08 +03:00
int r = 1 ;
uint64_t sector ;
int wipe ;
struct labeller_i * li ;
struct label_header * lh ;
2017-08-11 22:32:04 +03:00
struct lvmcache_info * info ;
2002-11-18 17:04:08 +03:00
log_very_verbose ( " Scanning for labels to wipe from %s " , dev_name ( dev ) ) ;
2018-05-16 21:24:00 +03:00
if ( ! label_scan_open_excl ( dev ) ) {
2018-02-20 00:40:44 +03:00
log_error ( " Failed to open device %s " , dev_name ( dev ) ) ;
return 0 ;
2002-11-18 17:04:08 +03:00
}
/* Scan first few sectors for anything looking like a label */
for ( sector = 0 ; sector < LABEL_SCAN_SECTORS ;
sector + = LABEL_SIZE > > SECTOR_SHIFT ) {
2018-02-20 00:40:44 +03:00
memset ( readbuf , 0 , sizeof ( readbuf ) ) ;
2018-02-27 20:26:04 +03:00
if ( ! dev_read_bytes ( dev , sector < < SECTOR_SHIFT , LABEL_SIZE , readbuf ) ) {
2020-09-16 21:47:06 +03:00
log_error ( " Failed to read label from %s sector %llu " ,
dev_name ( dev ) , ( unsigned long long ) sector ) ;
2018-02-20 00:40:44 +03:00
continue ;
}
lh = ( struct label_header * ) readbuf ;
2002-11-18 17:04:08 +03:00
wipe = 0 ;
2019-05-02 00:50:48 +03:00
if ( ! memcmp ( lh - > id , LABEL_ID , sizeof ( lh - > id ) ) ) {
2002-11-18 17:04:08 +03:00
if ( xlate64 ( lh - > sector_xl ) = = sector )
wipe = 1 ;
} else {
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( li , & _labellers ) {
2018-02-20 00:40:44 +03:00
if ( li - > l - > ops - > can_handle ( li - > l , ( char * ) lh , sector ) ) {
2002-11-18 17:04:08 +03:00
wipe = 1 ;
break ;
}
}
}
if ( wipe ) {
2020-09-16 21:47:06 +03:00
log_very_verbose ( " %s: Wiping label at sector %llu " ,
dev_name ( dev ) , ( unsigned long long ) sector ) ;
2018-02-20 00:40:44 +03:00
2018-02-27 20:26:04 +03:00
if ( ! dev_write_zeros ( dev , sector < < SECTOR_SHIFT , LABEL_SIZE ) ) {
2020-09-16 21:47:06 +03:00
log_error ( " Failed to remove label from %s at sector %llu " ,
dev_name ( dev ) , ( unsigned long long ) sector ) ;
2018-02-20 00:40:44 +03:00
r = 0 ;
} else {
2017-08-11 22:32:04 +03:00
/* Also remove the PV record from cache. */
info = lvmcache_info_from_pvid ( dev - > pvid , dev , 0 ) ;
if ( info )
lvmcache_del ( info ) ;
2002-11-18 17:04:08 +03:00
}
}
}
return r ;
2001-12-11 14:42:30 +03:00
}
2002-11-18 17:04:08 +03:00
/* Caller may need to use label_get_handler to create label struct! */
int label_write ( struct device * dev , struct label * label )
{
2010-07-09 19:34:40 +04:00
char buf [ LABEL_SIZE ] __attribute__ ( ( aligned ( 8 ) ) ) ;
2002-11-18 17:04:08 +03:00
struct label_header * lh = ( struct label_header * ) buf ;
2018-10-30 00:53:17 +03:00
uint64_t offset ;
2002-11-18 17:04:08 +03:00
int r = 1 ;
2003-08-27 01:12:06 +04:00
if ( ! label - > labeller - > ops - > write ) {
2009-07-16 00:02:46 +04:00
log_error ( " Label handler does not support label writes " ) ;
2003-08-27 01:12:06 +04:00
return 0 ;
}
2002-11-18 17:04:08 +03:00
if ( ( LABEL_SIZE + ( label - > sector < < SECTOR_SHIFT ) ) > LABEL_SCAN_SIZE ) {
2020-09-16 21:47:06 +03:00
log_error ( " Label sector % " PRIu64 " beyond range (%ld) " ,
2002-11-18 17:04:08 +03:00
label - > sector , LABEL_SCAN_SECTORS ) ;
return 0 ;
}
memset ( buf , 0 , LABEL_SIZE ) ;
2019-05-02 00:50:48 +03:00
memcpy ( lh - > id , LABEL_ID , sizeof ( lh - > id ) ) ;
2002-11-18 17:04:08 +03:00
lh - > sector_xl = xlate64 ( label - > sector ) ;
lh - > offset_xl = xlate32 ( sizeof ( * lh ) ) ;
2008-01-30 16:19:47 +03:00
if ( ! ( label - > labeller - > ops - > write ) ( label , buf ) )
return_0 ;
2002-11-18 17:04:08 +03:00
2010-09-27 23:09:34 +04:00
lh - > crc_xl = xlate32 ( calc_crc ( INITIAL_CRC , ( uint8_t * ) & lh - > offset_xl , LABEL_SIZE -
( ( uint8_t * ) & lh - > offset_xl - ( uint8_t * ) lh ) ) ) ;
2002-11-18 17:04:08 +03:00
2020-09-16 21:47:06 +03:00
log_very_verbose ( " %s: Writing label to sector % " PRIu64 " with stored offset % "
PRIu32 " . " , dev_name ( dev ) , label - > sector ,
2016-11-01 13:02:01 +03:00
xlate32 ( lh - > offset_xl ) ) ;
2018-02-20 00:40:44 +03:00
if ( ! label_scan_open ( dev ) ) {
log_error ( " Failed to open device %s " , dev_name ( dev ) ) ;
return 0 ;
}
2018-10-30 00:53:17 +03:00
offset = label - > sector < < SECTOR_SHIFT ;
dev_set_last_byte ( dev , offset + LABEL_SIZE ) ;
if ( ! dev_write_bytes ( dev , offset , LABEL_SIZE , buf ) ) {
2013-01-08 02:30:29 +04:00
log_debug_devs ( " Failed to write label to %s " , dev_name ( dev ) ) ;
2019-11-13 18:15:07 +03:00
return 0 ;
2002-11-18 17:04:08 +03:00
}
2018-10-30 00:53:17 +03:00
dev_unset_last_byte ( dev ) ;
2002-11-18 17:04:08 +03:00
return r ;
2001-12-11 14:42:30 +03:00
}
2002-11-18 17:04:08 +03:00
void label_destroy ( struct label * label )
2001-12-11 19:49:40 +03:00
{
2002-11-18 17:04:08 +03:00
label - > labeller - > ops - > destroy_label ( label - > labeller , label ) ;
2018-06-08 15:40:53 +03:00
free ( label ) ;
2002-11-18 17:04:08 +03:00
}
struct label * label_create ( struct labeller * labeller )
{
struct label * label ;
2018-06-08 15:40:53 +03:00
if ( ! ( label = zalloc ( sizeof ( * label ) ) ) ) {
2024-08-30 00:06:04 +03:00
log_error ( " label allocation failed " ) ;
2002-11-18 17:04:08 +03:00
return NULL ;
}
label - > labeller = labeller ;
labeller - > ops - > initialise_label ( labeller , label ) ;
return label ;
2001-12-11 19:49:40 +03:00
}
2018-02-07 00:18:11 +03:00
2024-07-08 23:32:35 +03:00
/* global variable for accessing the bcache populated by label scan */
/* FIXME/TODO convert to cmd_context */
static struct bcache * scan_bcache ;
2018-02-15 19:27:43 +03:00
# define BCACHE_BLOCK_SIZE_IN_SECTORS 256 /* 256*512 = 128K */
2018-02-07 00:18:11 +03:00
static bool _in_bcache ( struct device * dev )
{
2018-02-14 01:17:02 +03:00
if ( ! dev )
return NULL ;
2018-02-07 00:18:11 +03:00
return ( dev - > flags & DEV_IN_BCACHE ) ? true : false ;
}
static struct labeller * _find_lvm_header ( struct device * dev ,
2021-06-29 01:09:09 +03:00
char * headers_buf ,
2023-07-13 13:32:44 +03:00
size_t headers_buf_size ,
2018-02-07 00:18:11 +03:00
uint64_t * label_sector ,
2018-05-11 00:27:34 +03:00
uint64_t block_sector ,
uint64_t start_sector )
2018-02-07 00:18:11 +03:00
{
struct labeller_i * li ;
struct labeller * labeller_ret = NULL ;
struct label_header * lh ;
uint64_t sector ;
int found = 0 ;
2018-05-11 00:27:34 +03:00
for ( sector = start_sector ; sector < start_sector + LABEL_SCAN_SECTORS ;
2018-02-07 00:18:11 +03:00
sector + = LABEL_SIZE > > SECTOR_SHIFT ) {
2018-05-11 00:27:34 +03:00
2021-06-29 01:09:09 +03:00
if ( ( sector * 512 ) > = headers_buf_size )
2018-05-11 00:27:34 +03:00
break ;
2021-06-29 01:09:09 +03:00
lh = ( struct label_header * ) ( headers_buf + ( sector < < SECTOR_SHIFT ) ) ;
2018-02-07 00:18:11 +03:00
2019-05-02 00:50:48 +03:00
if ( ! memcmp ( lh - > id , LABEL_ID , sizeof ( lh - > id ) ) ) {
2018-02-07 00:18:11 +03:00
if ( found ) {
2020-09-16 21:47:06 +03:00
log_error ( " Ignoring additional label on %s at sector %llu " ,
dev_name ( dev ) , ( unsigned long long ) ( block_sector + sector ) ) ;
2018-02-07 00:18:11 +03:00
}
2018-05-11 00:27:34 +03:00
if ( xlate64 ( lh - > sector_xl ) ! = sector ) {
2020-09-16 21:47:06 +03:00
log_warn ( " %s: Label for sector %llu found at sector %llu - ignoring. " ,
dev_name ( dev ) ,
( unsigned long long ) xlate64 ( lh - > sector_xl ) ,
( unsigned long long ) ( block_sector + sector ) ) ;
2018-02-07 00:18:11 +03:00
continue ;
}
2018-05-11 00:27:34 +03:00
if ( calc_crc ( INITIAL_CRC , ( uint8_t * ) & lh - > offset_xl ,
LABEL_SIZE - ( ( uint8_t * ) & lh - > offset_xl - ( uint8_t * ) lh ) ) ! = xlate32 ( lh - > crc_xl ) ) {
2018-02-07 00:18:11 +03:00
log_very_verbose ( " Label checksum incorrect on %s - ignoring " , dev_name ( dev ) ) ;
continue ;
}
if ( found )
continue ;
}
dm_list_iterate_items ( li , & _labellers ) {
2018-05-11 00:27:34 +03:00
if ( li - > l - > ops - > can_handle ( li - > l , ( char * ) lh , block_sector + sector ) ) {
2021-08-03 23:32:33 +03:00
log_debug ( " Found label at sector %llu on %s " ,
( unsigned long long ) ( block_sector + sector ) , dev_name ( dev ) ) ;
2018-02-07 00:18:11 +03:00
if ( found ) {
2020-09-16 21:47:06 +03:00
log_error ( " Ignoring additional label on %s at sector %llu " ,
2018-02-07 00:18:11 +03:00
dev_name ( dev ) ,
2020-09-16 21:47:06 +03:00
( unsigned long long ) ( block_sector + sector ) ) ;
2018-02-07 00:18:11 +03:00
continue ;
}
labeller_ret = li - > l ;
found = 1 ;
if ( label_sector )
2018-05-11 00:27:34 +03:00
* label_sector = block_sector + sector ;
2018-02-07 00:18:11 +03:00
break ;
}
}
}
return labeller_ret ;
}
/*
* Process / parse the headers from the data read from a device .
* Populates lvmcache with device / mda locations / vgname
* so that vg_read ( vgname ) will know which devices / locations
* to read metadata from .
*
* If during processing , headers / metadata are found to be needed
* beyond the range of the scanned block , then additional reads
* are performed in the processing functions to get that data .
*/
2018-05-04 01:12:07 +03:00
static int _process_block ( struct cmd_context * cmd , struct dev_filter * f ,
2023-07-13 13:32:44 +03:00
struct device * dev , char * headers_buf , size_t headers_buf_size ,
2018-05-11 00:27:34 +03:00
uint64_t block_sector , uint64_t start_sector ,
int * is_lvm_device )
2018-02-07 00:18:11 +03:00
{
2021-06-29 01:09:09 +03:00
char * label_buf ;
2018-02-07 00:18:11 +03:00
struct labeller * labeller ;
2021-06-29 01:09:09 +03:00
uint64_t label_sector = 0 ;
2019-02-05 22:40:34 +03:00
int is_duplicate = 0 ;
2018-02-07 00:18:11 +03:00
int ret = 0 ;
2018-05-04 01:12:07 +03:00
2018-12-07 23:35:22 +03:00
dev - > flags & = ~ DEV_SCAN_FOUND_LABEL ;
2018-05-04 01:12:07 +03:00
/*
2020-10-20 23:10:08 +03:00
* The device may have signatures that exclude it from being processed ,
* even if it might look like a PV . Now that the device has been read
* and data is available in bcache for it , recheck filters , including
* those that use data . The device needs to be excluded before it
* begins to be processed as a PV .
2018-05-04 01:12:07 +03:00
*/
2020-10-20 23:10:08 +03:00
if ( f ) {
if ( ! f - > passes_filter ( cmd , f , dev , NULL ) ) {
/*
* If this device was previously scanned ( not common )
* and if it passed filters at that point , lvmcache
* info may have been saved for it . Now the same
* device is being scanned again , and it may fail
* filters this time . If the caller did not clear
* lvmcache info for this dev before rescanning , do
* that now . It ' s unlikely this is actually needed .
*/
if ( dev - > pvid [ 0 ] ) {
2023-04-26 15:40:11 +03:00
log_print_unless_silent ( " Clear pvid and info for filtered dev %s. " , dev_name ( dev ) ) ;
2020-10-20 23:10:08 +03:00
lvmcache_del_dev ( dev ) ;
memset ( dev - > pvid , 0 , sizeof ( dev - > pvid ) ) ;
}
2018-05-04 01:12:07 +03:00
* is_lvm_device = 0 ;
goto_out ;
}
}
2018-02-07 00:18:11 +03:00
/*
2021-06-29 01:09:09 +03:00
* Finds the data sector containing the label .
2018-02-07 00:18:11 +03:00
*/
2021-06-29 01:09:09 +03:00
if ( ! ( labeller = _find_lvm_header ( dev , headers_buf , headers_buf_size , & label_sector , block_sector , start_sector ) ) ) {
2018-02-07 00:18:11 +03:00
/*
* Non - PVs exit here
*
* FIXME : check for PVs with errors that also exit here !
* i . e . this code cannot distinguish between a non - lvm
* device an an lvm device with errors .
*/
log_very_verbose ( " %s: No lvm label detected " , dev_name ( dev ) ) ;
2020-10-20 23:10:08 +03:00
/* See comment above */
if ( dev - > pvid [ 0 ] ) {
2023-04-26 15:40:11 +03:00
log_print_unless_silent ( " Clear pvid and info for no lvm header %s " , dev_name ( dev ) ) ;
2020-10-20 23:10:08 +03:00
lvmcache_del_dev ( dev ) ;
memset ( dev - > pvid , 0 , sizeof ( dev - > pvid ) ) ;
}
2018-02-07 00:18:11 +03:00
2021-10-13 22:13:54 +03:00
dev - > flags | = DEV_SCAN_FOUND_NOLABEL ;
2018-02-07 00:18:11 +03:00
* is_lvm_device = 0 ;
2020-10-02 12:14:18 +03:00
goto out ;
2018-02-07 00:18:11 +03:00
}
2018-12-07 23:35:22 +03:00
dev - > flags | = DEV_SCAN_FOUND_LABEL ;
2018-02-07 00:18:11 +03:00
* is_lvm_device = 1 ;
2021-06-29 01:09:09 +03:00
label_buf = headers_buf + ( label_sector * 512 ) ;
2018-02-07 00:18:11 +03:00
/*
* This is the point where the scanning code dives into the rest of
2019-02-05 22:40:34 +03:00
* lvm . ops - > read ( ) is _text_read ( ) which reads the pv_header , mda
* locations , and metadata text . All of the info it finds about the PV
* and VG is stashed in lvmcache which saves it in the form of
* info / vginfo structs . That lvmcache info is used later when the
* command wants to read the VG to do something to it .
2018-02-07 00:18:11 +03:00
*/
2021-06-29 01:09:09 +03:00
ret = labeller - > ops - > read ( cmd , labeller , dev , label_buf , label_sector , & is_duplicate ) ;
2018-02-07 00:18:11 +03:00
2019-02-05 22:40:34 +03:00
if ( ! ret ) {
if ( is_duplicate ) {
/*
* _text_read ( ) called lvmcache_add ( ) which found an
* existing info struct for this PVID but for a
* different dev . lvmcache_add ( ) did not add an info
* struct for this dev , but added this dev to the list
* of duplicate devs .
*/
2019-08-01 23:04:10 +03:00
log_debug ( " label scan found duplicate PVID %s on %s " , dev - > pvid , dev_name ( dev ) ) ;
2019-02-05 22:40:34 +03:00
} else {
/*
* Leave the info in lvmcache because the device is
* present and can still be used even if it has
* metadata that we can ' t process ( we can get metadata
* from another PV / mda . ) _text_read only saves mdas
* with good metadata in lvmcache ( this includes old
* metadata ) , and if a PV has no mdas with good
* metadata , then the info for the PV will be in
* lvmcache with empty info - > mdas , and it will behave
* like a PV with no mdas ( a common configuration . )
*/
log_warn ( " WARNING: scan failed to get metadata summary from %s PVID %s " , dev_name ( dev ) , dev - > pvid ) ;
}
2018-02-07 00:18:11 +03:00
}
out :
return ret ;
}
2018-02-13 17:58:35 +03:00
static int _scan_dev_open ( struct device * dev )
{
2018-05-25 19:14:12 +03:00
struct dm_list * name_list ;
struct dm_str_list * name_sl ;
2018-02-13 17:58:35 +03:00
const char * name ;
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
const char * modestr ;
2018-05-25 19:14:12 +03:00
struct stat sbuf ;
2018-02-13 17:58:35 +03:00
int flags = 0 ;
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 fd , di ;
2018-02-13 17:58:35 +03:00
2018-02-14 01:17:02 +03:00
if ( ! dev )
return 0 ;
2018-02-13 17:58:35 +03:00
if ( dev - > flags & DEV_IN_BCACHE ) {
2018-05-25 19:14:12 +03:00
/* Shouldn't happen */
log_error ( " Device open %s has DEV_IN_BCACHE already set " , dev_name ( dev ) ) ;
2018-02-13 17:58:35 +03:00
dev - > flags & = ~ DEV_IN_BCACHE ;
}
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
if ( dev - > bcache_di ! = - 1 ) {
2018-05-25 19:14:12 +03:00
/* Shouldn't happen */
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
log_error ( " Device open %s already open with di %d fd %d " ,
dev_name ( dev ) , dev - > bcache_di , dev - > bcache_fd ) ;
2018-02-13 17:58:35 +03:00
return 0 ;
}
2022-03-01 02:37:12 +03:00
next_name :
2018-05-25 19:14:12 +03:00
/*
* All the names for this device ( major : minor ) are kept on
* dev - > aliases , the first one is the primary / preferred name .
2022-03-01 02:37:12 +03:00
*
* The default name preferences in dev - cache mean that the first
* name in dev - > aliases is not a symlink for scsi devices , but is
* the / dev / mapper / symlink for mpath devices .
*
* If preferred names are set to symlinks , should this
* first attempt to open using a non - symlink ?
*
* dm_list_first ( ) returns NULL if the list is empty .
2018-05-25 19:14:12 +03:00
*/
if ( ! ( name_list = dm_list_first ( & dev - > aliases ) ) ) {
2024-05-28 17:17:53 +03:00
log_error ( " Device open %u:%u has no path names. " ,
MAJOR ( dev - > dev ) , MINOR ( dev - > dev ) ) ;
2018-02-13 17:58:35 +03:00
return 0 ;
}
2018-05-25 19:14:12 +03:00
name_sl = dm_list_item ( name_list , struct dm_str_list ) ;
name = name_sl - > str ;
2018-02-13 17:58:35 +03:00
flags | = O_DIRECT ;
flags | = O_NOATIME ;
2018-06-20 19:32:45 +03:00
/*
* FIXME : udev is a train wreck when we open RDWR and close , so we
* need to only use RDWR when we actually need to write , and use
* RDONLY otherwise . Fix , disable or scrap udev nonsense so we can
* just open with RDWR by default .
*/
if ( dev - > flags & DEV_BCACHE_EXCL ) {
2018-02-13 17:58:35 +03:00
flags | = O_EXCL ;
2018-06-20 19:32:45 +03:00
flags | = O_RDWR ;
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
modestr = " rwex " ;
2018-06-20 19:32:45 +03:00
} else if ( dev - > flags & DEV_BCACHE_WRITE ) {
flags | = O_RDWR ;
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
modestr = " rw " ;
2018-06-20 19:32:45 +03:00
} else {
flags | = O_RDONLY ;
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
modestr = " ro " ;
2018-06-20 19:32:45 +03:00
}
2018-02-13 17:58:35 +03:00
fd = open ( name , flags , 0777 ) ;
if ( fd < 0 ) {
if ( ( errno = = EBUSY ) & & ( flags & O_EXCL ) ) {
log_error ( " Can't open %s exclusively. Mounted filesystem? " ,
dev_name ( dev ) ) ;
2022-03-01 02:37:12 +03:00
return 0 ;
2018-02-13 17:58:35 +03:00
} else {
2018-05-25 19:14:12 +03:00
/*
2022-03-01 02:37:12 +03:00
* drop name from dev - > aliases and use verify_aliases to
* drop any other invalid aliases before retrying open with
* any remaining valid paths .
2018-05-25 19:14:12 +03:00
*/
2024-05-28 17:17:53 +03:00
log_debug ( " Drop alias for %u:%u failed open %s (%d). " ,
MAJOR ( dev - > dev ) , MINOR ( dev - > dev ) , name , errno ) ;
2024-07-08 23:32:41 +03:00
dev_cache_failed_path ( dev , name ) ;
dev_cache_verify_aliases ( dev ) ;
2022-03-01 02:37:12 +03:00
goto next_name ;
2018-02-13 17:58:35 +03:00
}
2022-03-01 02:37:12 +03:00
}
/* Verify that major:minor from the path still match dev. */
if ( ( fstat ( fd , & sbuf ) < 0 ) | | ( sbuf . st_rdev ! = dev - > dev ) ) {
2024-05-28 17:17:53 +03:00
log_warn ( " Invalid path %s for device %u:%u, trying different path. " ,
name , MAJOR ( dev - > dev ) , MINOR ( dev - > dev ) ) ;
2022-03-01 02:37:12 +03:00
( void ) close ( fd ) ;
2024-07-08 23:32:41 +03:00
dev_cache_failed_path ( dev , name ) ;
dev_cache_verify_aliases ( dev ) ;
2022-03-01 02:37:12 +03:00
goto next_name ;
2018-02-13 17:58:35 +03:00
}
dev - > flags | = DEV_IN_BCACHE ;
dev - > bcache_fd = 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
di = bcache_set_fd ( fd ) ;
if ( di = = - 1 ) {
log_error ( " Failed to set bcache fd. " ) ;
2021-03-10 03:26:30 +03:00
if ( close ( fd ) )
log_sys_debug ( " close " , name ) ;
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
dev - > bcache_fd = - 1 ;
return 0 ;
}
log_debug ( " open %s %s di %d fd %d " , dev_name ( dev ) , modestr , di , fd ) ;
dev - > bcache_di = di ;
2018-02-13 17:58:35 +03:00
return 1 ;
}
static int _scan_dev_close ( struct device * dev )
{
if ( ! ( dev - > flags & DEV_IN_BCACHE ) )
log_error ( " scan_dev_close %s no DEV_IN_BCACHE set " , dev_name ( dev ) ) ;
dev - > flags & = ~ DEV_IN_BCACHE ;
dev - > flags & = ~ DEV_BCACHE_EXCL ;
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
dev - > flags & = ~ DEV_BCACHE_WRITE ;
2018-02-13 17:58:35 +03:00
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
if ( dev - > bcache_di = = - 1 ) {
2018-02-13 17:58:35 +03:00
log_error ( " scan_dev_close %s already closed " , dev_name ( dev ) ) ;
return 0 ;
}
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 ( dev - > bcache_di ) ;
2018-02-13 17:58:35 +03:00
if ( close ( dev - > bcache_fd ) )
log_warn ( " close %s errno %d " , dev_name ( dev ) , errno ) ;
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
2018-02-13 17:58:35 +03:00
dev - > bcache_fd = - 1 ;
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
dev - > bcache_di = - 1 ;
2018-02-13 17:58:35 +03:00
return 1 ;
}
2019-10-28 18:01:47 +03:00
// Like bcache_invalidate, only it throws any dirty data away if the
// write fails.
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
static void _invalidate_di ( struct bcache * cache , int di )
2019-10-28 18:01:47 +03:00
{
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
if ( ! bcache_invalidate_di ( cache , di ) )
bcache_abort_di ( cache , di ) ;
2019-10-28 18:01:47 +03:00
}
2018-02-07 00:18:11 +03:00
/*
* Read or reread label / metadata from selected devs .
*
* Reads and looks at label_header , pv_header , pv_header_extension ,
* mda_header , raw_locns , vg metadata from each device .
*
* Effect is populating lvmcache with latest info / vginfo ( PV / VG ) data
* from the devs . If a scanned device does not have a label_header ,
* its info is removed from lvmcache .
*/
2021-06-29 01:09:09 +03:00
# define HEADERS_BUF_SIZE 4096
2018-05-04 01:12:07 +03:00
static int _scan_list ( struct cmd_context * cmd , struct dev_filter * f ,
2020-10-23 21:53:52 +03:00
struct dm_list * devs , int want_other_devs , int * failed )
2018-02-07 00:18:11 +03:00
{
2021-06-29 01:09:09 +03:00
char headers_buf [ HEADERS_BUF_SIZE ] ;
2018-02-07 00:18:11 +03:00
struct dm_list wait_devs ;
struct dm_list done_devs ;
struct device_list * devl , * devl2 ;
struct block * bb ;
2018-04-06 21:12:26 +03:00
int scan_read_errors = 0 ;
int scan_process_errors = 0 ;
2018-02-07 00:18:11 +03:00
int scan_failed_count = 0 ;
int rem_prefetches ;
2018-05-04 01:12:07 +03:00
int submit_count ;
2018-02-07 00:18:11 +03:00
int is_lvm_device ;
2018-03-01 21:40:37 +03:00
int ret ;
2018-02-07 00:18:11 +03:00
dm_list_init ( & wait_devs ) ;
dm_list_init ( & done_devs ) ;
2018-04-06 21:12:26 +03:00
log_debug_devs ( " Scanning %d devices for VG info " , dm_list_size ( devs ) ) ;
2018-02-07 00:18:11 +03:00
scan_more :
2024-07-08 23:32:35 +03:00
rem_prefetches = bcache_max_prefetches ( scan_bcache ) ;
2018-05-04 01:12:07 +03:00
submit_count = 0 ;
2018-02-07 00:18:11 +03:00
dm_list_iterate_items_safe ( devl , devl2 , devs ) {
2022-02-10 23:00:25 +03:00
devl - > dev - > flags & = ~ DEV_SCAN_NOT_READ ;
2018-02-07 00:18:11 +03:00
/*
* If we prefetch more devs than blocks in the cache , then the
* cache will wait for earlier reads to complete , toss the
* results , and reuse those blocks before we ' ve had a chance to
* use them . So , prefetch as many as are available , wait for
* and process them , then repeat .
*/
if ( ! rem_prefetches )
break ;
if ( ! _in_bcache ( devl - > dev ) ) {
2018-02-13 17:58:35 +03:00
if ( ! _scan_dev_open ( devl - > dev ) ) {
2024-05-28 17:17:53 +03:00
log_debug_devs ( " Scan failed to open %u:%u %s. " ,
MAJOR ( devl - > dev - > dev ) , MINOR ( devl - > dev - > dev ) , dev_name ( devl - > dev ) ) ;
2018-02-13 17:58:35 +03:00
dm_list_del ( & devl - > list ) ;
2022-02-10 23:00:25 +03:00
devl - > dev - > flags | = DEV_SCAN_NOT_READ ;
2018-02-07 00:18:11 +03:00
continue ;
}
}
2024-07-08 23:32:35 +03:00
bcache_prefetch ( scan_bcache , devl - > dev - > bcache_di , 0 ) ;
2018-02-07 00:18:11 +03:00
rem_prefetches - - ;
2018-05-04 01:12:07 +03:00
submit_count + + ;
2018-02-07 00:18:11 +03:00
dm_list_del ( & devl - > list ) ;
dm_list_add ( & wait_devs , & devl - > list ) ;
}
2018-05-04 01:12:07 +03:00
log_debug_devs ( " Scanning submitted %d reads " , submit_count ) ;
2018-02-07 00:18:11 +03:00
dm_list_iterate_items_safe ( devl , devl2 , & wait_devs ) {
bb = NULL ;
2018-04-06 21:12:26 +03:00
is_lvm_device = 0 ;
2018-02-07 00:18:11 +03:00
2024-07-08 23:32:35 +03:00
if ( ! bcache_get ( scan_bcache , devl - > dev - > bcache_di , 0 , 0 , & bb ) ) {
2019-11-13 18:15:07 +03:00
log_debug_devs ( " Scan failed to read %s. " , dev_name ( devl - > dev ) ) ;
2018-04-06 21:12:26 +03:00
scan_read_errors + + ;
scan_failed_count + + ;
2022-02-10 23:00:25 +03:00
devl - > dev - > flags | = DEV_SCAN_NOT_READ ;
2018-03-01 21:40:37 +03:00
lvmcache_del_dev ( devl - > dev ) ;
2021-06-29 01:09:09 +03:00
if ( bb )
bcache_put ( bb ) ;
2018-02-07 00:18:11 +03:00
} else {
2021-06-29 01:09:09 +03:00
/* copy the first 4k from bb that will contain label_header */
memcpy ( headers_buf , bb - > data , HEADERS_BUF_SIZE ) ;
/*
* " put " the bcache block before process_block because
* processing metadata may need to invalidate and reread
* metadata that ' s covered by bb . invalidate / reread is
* not allowed while bb is held . The functions for
* filtering and scanning metadata for this device use
* dev_read_bytes ( ) , which will generally grab the
* bcache block / data that we ' re putting here . Since
* we ' re doing put , it ' s possible but not likely that
* bcache could drop the block before dev_read_bytes ( )
* uses it again , in which case bcache will reread it
* from disk for dev_read_bytes ( ) .
*/
bcache_put ( bb ) ;
2024-05-28 17:17:53 +03:00
log_debug_devs ( " Processing data from device %s %u:%u di %d. " ,
2018-05-30 01:02:27 +03:00
dev_name ( devl - > dev ) ,
2024-05-28 17:17:53 +03:00
MAJOR ( devl - > dev - > dev ) , MINOR ( devl - > dev - > dev ) ,
2021-06-29 01:09:09 +03:00
devl - > dev - > bcache_di ) ;
2018-03-01 21:40:37 +03:00
2021-06-29 01:09:09 +03:00
ret = _process_block ( cmd , f , devl - > dev , headers_buf , sizeof ( headers_buf ) , 0 , 0 , & is_lvm_device ) ;
2018-03-01 21:40:37 +03:00
if ( ! ret & & is_lvm_device ) {
2018-03-09 20:39:12 +03:00
log_debug_devs ( " Scan failed to process %s " , dev_name ( devl - > dev ) ) ;
2018-04-06 21:12:26 +03:00
scan_process_errors + + ;
scan_failed_count + + ;
2018-03-01 21:40:37 +03:00
}
2018-02-07 00:18:11 +03:00
}
/*
* Keep the bcache block of lvm devices we have processed so
* that the vg_read phase can reuse it . If bcache failed to
* read the block , or the device does not belong to lvm , then
2020-10-23 21:53:52 +03:00
* drop it from bcache . When " want_other_devs " is set , it
* means the caller wants to scan and keep open non - lvm devs ,
* e . g . to pvcreate them .
2018-02-07 00:18:11 +03:00
*/
2020-10-23 21:53:52 +03:00
if ( ! is_lvm_device & & ! want_other_devs ) {
2024-07-08 23:32:35 +03:00
_invalidate_di ( scan_bcache , devl - > dev - > bcache_di ) ;
2018-02-13 17:58:35 +03:00
_scan_dev_close ( devl - > dev ) ;
2018-02-07 00:18:11 +03:00
}
dm_list_del ( & devl - > list ) ;
dm_list_add ( & done_devs , & devl - > list ) ;
}
if ( ! dm_list_empty ( devs ) )
goto scan_more ;
2018-05-25 19:14:12 +03:00
log_debug_devs ( " Scanned devices: read errors %d process errors %d failed %d " ,
scan_read_errors , scan_process_errors , scan_failed_count ) ;
2018-02-07 00:18:11 +03:00
2018-02-13 17:58:35 +03:00
if ( failed )
* failed = scan_failed_count ;
2018-02-15 01:15:30 +03:00
dm_list_splice ( devs , & done_devs ) ;
2018-02-13 17:58:35 +03:00
return 1 ;
2018-02-07 00:18:11 +03:00
}
2018-05-15 20:16:58 +03:00
/*
2019-03-01 22:55:59 +03:00
* We don ' t know ahead of time if we will find some VG metadata
* that is larger than the total size of the bcache , which would
* prevent us from reading / writing the VG since we do not dynamically
* increase the bcache size when we find it ' s too small . In these
* cases the user would need to set io_memory_size to be larger
* than the max VG metadata size ( lvm does not impose any limit on
* the metadata size . )
2018-05-15 20:16:58 +03:00
*/
2019-08-16 21:35:09 +03:00
# define MIN_BCACHE_BLOCKS 32 /* 4MB (32 * 128KB) */
# define MAX_BCACHE_BLOCKS 4096 /* 512MB (4096 * 128KB) */
2018-02-27 21:35:45 +03:00
2024-07-08 23:32:35 +03:00
int label_scan_setup_bcache ( void )
2018-02-15 00:45:31 +03:00
{
2018-11-16 21:21:20 +03:00
struct io_engine * ioe = NULL ;
2019-03-01 22:55:59 +03:00
int iomem_kb = io_memory_size ( ) ;
int block_size_kb = ( BCACHE_BLOCK_SIZE_IN_SECTORS * 512 ) / 1024 ;
int cache_blocks ;
2024-07-08 23:32:35 +03:00
if ( scan_bcache )
2024-01-26 18:08:54 +03:00
return 1 ;
2019-03-01 22:55:59 +03:00
cache_blocks = iomem_kb / block_size_kb ;
2018-02-15 00:45:31 +03:00
2018-02-27 21:35:45 +03:00
if ( cache_blocks < MIN_BCACHE_BLOCKS )
cache_blocks = MIN_BCACHE_BLOCKS ;
2018-02-15 00:45:31 +03:00
2018-05-15 20:16:58 +03:00
if ( cache_blocks > MAX_BCACHE_BLOCKS )
cache_blocks = MAX_BCACHE_BLOCKS ;
2019-03-04 21:13:09 +03:00
_current_bcache_size_bytes = cache_blocks * BCACHE_BLOCK_SIZE_IN_SECTORS * 512 ;
2018-11-16 21:21:20 +03:00
if ( use_aio ( ) ) {
if ( ! ( ioe = create_async_io_engine ( ) ) ) {
log_warn ( " Failed to set up async io, using sync io. " ) ;
init_use_aio ( 0 ) ;
}
}
if ( ! ioe ) {
if ( ! ( ioe = create_sync_io_engine ( ) ) ) {
log_error ( " Failed to set up sync io. " ) ;
return 0 ;
}
2018-02-15 00:45:31 +03:00
}
2024-07-08 23:32:35 +03:00
if ( ! ( scan_bcache = bcache_create ( BCACHE_BLOCK_SIZE_IN_SECTORS , cache_blocks , ioe ) ) ) {
2022-07-06 01:08:00 +03:00
log_error ( " Failed to set up io layer with %d blocks. " , cache_blocks ) ;
2018-02-15 00:45:31 +03:00
return 0 ;
}
return 1 ;
}
2019-08-08 21:11:05 +03:00
/*
* We don ' t know how many of num_devs will be PVs that we need to
* keep open , but if it ' s greater than the soft limit , then we ' ll
* need the soft limit raised , so do that before starting .
*
* If opens approach the raised soft / hard limit while scanning , then
* we could also attempt to raise the soft / hard limits during the scan .
*/
# define BASE_FD_COUNT 32 /* Number of open files we want apart from devs */
2021-10-29 22:49:36 +03:00
void prepare_open_file_limit ( struct cmd_context * cmd , unsigned int num_devs )
2019-08-08 21:11:05 +03:00
{
2019-08-26 18:19:16 +03:00
# ifdef HAVE_PRLIMIT
2021-09-10 23:32:44 +03:00
struct rlimit old = { 0 } , new ;
2019-08-08 21:11:05 +03:00
unsigned int want = num_devs + BASE_FD_COUNT ;
int rv ;
rv = prlimit ( 0 , RLIMIT_NOFILE , NULL , & old ) ;
if ( rv < 0 ) {
log_debug ( " Checking fd limit for num_devs %u failed %d " , num_devs , errno ) ;
return ;
}
log_debug ( " Checking fd limit for num_devs %u want %u soft %lld hard %lld " ,
num_devs , want , ( long long ) old . rlim_cur , ( long long ) old . rlim_max ) ;
/* Current soft limit is enough */
if ( old . rlim_cur > want )
return ;
/* Soft limit already raised to max */
if ( old . rlim_cur = = old . rlim_max )
return ;
/* Raise soft limit up to hard/max limit */
new . rlim_cur = old . rlim_max ;
new . rlim_max = old . rlim_max ;
log_debug ( " Setting fd limit for num_devs %u soft %lld hard %lld " ,
num_devs , ( long long ) new . rlim_cur , ( long long ) new . rlim_max ) ;
rv = prlimit ( 0 , RLIMIT_NOFILE , & new , & old ) ;
if ( rv < 0 ) {
if ( errno = = EPERM )
log_warn ( " WARNING: permission error setting open file limit for scanning %u devices. " , num_devs ) ;
else
log_warn ( " WARNING: cannot set open file limit for scanning %u devices. " , num_devs ) ;
return ;
}
2019-08-26 18:19:16 +03:00
# endif
2019-08-08 21:11:05 +03:00
}
device usage based on devices file
The LVM devices file lists devices that lvm can use. The default
file is /etc/lvm/devices/system.devices, and the lvmdevices(8)
command is used to add or remove device entries. If the file
does not exist, or if lvm.conf includes use_devicesfile=0, then
lvm will not use a devices file. When the devices file is in use,
the regex filter is not used, and the filter settings in lvm.conf
or on the command line are ignored.
LVM records devices in the devices file using hardware-specific
IDs, such as the WWID, and attempts to use subsystem-specific
IDs for virtual device types. These device IDs are also written
in the VG metadata. When no hardware or virtual ID is available,
lvm falls back using the unstable device name as the device ID.
When devnames are used, lvm performs extra scanning to find
devices if their devname changes, e.g. after reboot.
When proper device IDs are used, an lvm command will not look
at devices outside the devices file, but when devnames are used
as a fallback, lvm will scan devices outside the devices file
to locate PVs on renamed devices. A config setting
search_for_devnames can be used to control the scanning for
renamed devname entries.
Related to the devices file, the new command option
--devices <devnames> allows a list of devices to be specified for
the command to use, overriding the devices file. The listed
devices act as a sort of devices file in terms of limiting which
devices lvm will see and use. Devices that are not listed will
appear to be missing to the lvm command.
Multiple devices files can be kept in /etc/lvm/devices, which
allows lvm to be used with different sets of devices, e.g.
system devices do not need to be exposed to a specific application,
and the application can use lvm on its own set of devices that are
not exposed to the system. The option --devicesfile <filename> is
used to select the devices file to use with the command. Without
the option set, the default system devices file is used.
Setting --devicesfile "" causes lvm to not use a devices file.
An existing, empty devices file means lvm will see no devices.
The new command vgimportdevices adds PVs from a VG to the devices
file and updates the VG metadata to include the device IDs.
vgimportdevices -a will import all VGs into the system devices file.
LVM commands run by dmeventd not use a devices file by default,
and will look at all devices on the system. A devices file can
be created for dmeventd (/etc/lvm/devices/dmeventd.devices) If
this file exists, lvm commands run by dmeventd will use it.
Internal implementaion:
- device_ids_read - read the devices file
. add struct dev_use (du) to cmd->use_devices for each devices file entry
- dev_cache_scan - get /dev entries
. add struct device (dev) to dev_cache for each device on the system
- device_ids_match - match devices file entries to /dev entries
. match each du on cmd->use_devices to a dev in dev_cache, using device ID
. on match, set du->dev, dev->id, dev->flags MATCHED_USE_ID
- label_scan - read lvm headers and metadata from devices
. filters are applied, those that do not need data from the device
. filter-deviceid skips devs without MATCHED_USE_ID, i.e.
skips /dev entries that are not listed in the devices file
. read lvm label from dev
. filters are applied, those that use data from the device
. read lvm metadata from dev
. add info/vginfo structs for PVs/VGs (info is "lvmcache")
- device_ids_find_renamed_devs - handle devices with unstable devname ID
where devname changed
. this step only needed when devs do not have proper device IDs,
and their dev names change, e.g. after reboot sdb becomes sdc.
. detect incorrect match because PVID in the devices file entry
does not match the PVID found when the device was read above
. undo incorrect match between du and dev above
. search system devices for new location of PVID
. update devices file with new devnames for PVIDs on renamed devices
. label_scan the renamed devs
- continue with command processing
2020-06-23 21:25:41 +03:00
/*
* Currently the only caller is pvck which probably doesn ' t need
* deferred filters checked after the read . . . it wants to know if
* anything has the pvid , even a dev that might be filtered .
*/
2019-10-30 23:11:57 +03:00
int label_scan_for_pvid ( struct cmd_context * cmd , char * pvid , struct device * * dev_out )
{
char buf [ LABEL_SIZE ] __attribute__ ( ( aligned ( 8 ) ) ) ;
struct dm_list devs ;
struct dev_iter * iter ;
struct device * dev ;
struct pv_header * pvh ;
int ret = 0 ;
dm_list_init ( & devs ) ;
device usage based on devices file
The LVM devices file lists devices that lvm can use. The default
file is /etc/lvm/devices/system.devices, and the lvmdevices(8)
command is used to add or remove device entries. If the file
does not exist, or if lvm.conf includes use_devicesfile=0, then
lvm will not use a devices file. When the devices file is in use,
the regex filter is not used, and the filter settings in lvm.conf
or on the command line are ignored.
LVM records devices in the devices file using hardware-specific
IDs, such as the WWID, and attempts to use subsystem-specific
IDs for virtual device types. These device IDs are also written
in the VG metadata. When no hardware or virtual ID is available,
lvm falls back using the unstable device name as the device ID.
When devnames are used, lvm performs extra scanning to find
devices if their devname changes, e.g. after reboot.
When proper device IDs are used, an lvm command will not look
at devices outside the devices file, but when devnames are used
as a fallback, lvm will scan devices outside the devices file
to locate PVs on renamed devices. A config setting
search_for_devnames can be used to control the scanning for
renamed devname entries.
Related to the devices file, the new command option
--devices <devnames> allows a list of devices to be specified for
the command to use, overriding the devices file. The listed
devices act as a sort of devices file in terms of limiting which
devices lvm will see and use. Devices that are not listed will
appear to be missing to the lvm command.
Multiple devices files can be kept in /etc/lvm/devices, which
allows lvm to be used with different sets of devices, e.g.
system devices do not need to be exposed to a specific application,
and the application can use lvm on its own set of devices that are
not exposed to the system. The option --devicesfile <filename> is
used to select the devices file to use with the command. Without
the option set, the default system devices file is used.
Setting --devicesfile "" causes lvm to not use a devices file.
An existing, empty devices file means lvm will see no devices.
The new command vgimportdevices adds PVs from a VG to the devices
file and updates the VG metadata to include the device IDs.
vgimportdevices -a will import all VGs into the system devices file.
LVM commands run by dmeventd not use a devices file by default,
and will look at all devices on the system. A devices file can
be created for dmeventd (/etc/lvm/devices/dmeventd.devices) If
this file exists, lvm commands run by dmeventd will use it.
Internal implementaion:
- device_ids_read - read the devices file
. add struct dev_use (du) to cmd->use_devices for each devices file entry
- dev_cache_scan - get /dev entries
. add struct device (dev) to dev_cache for each device on the system
- device_ids_match - match devices file entries to /dev entries
. match each du on cmd->use_devices to a dev in dev_cache, using device ID
. on match, set du->dev, dev->id, dev->flags MATCHED_USE_ID
- label_scan - read lvm headers and metadata from devices
. filters are applied, those that do not need data from the device
. filter-deviceid skips devs without MATCHED_USE_ID, i.e.
skips /dev entries that are not listed in the devices file
. read lvm label from dev
. filters are applied, those that use data from the device
. read lvm metadata from dev
. add info/vginfo structs for PVs/VGs (info is "lvmcache")
- device_ids_find_renamed_devs - handle devices with unstable devname ID
where devname changed
. this step only needed when devs do not have proper device IDs,
and their dev names change, e.g. after reboot sdb becomes sdc.
. detect incorrect match because PVID in the devices file entry
does not match the PVID found when the device was read above
. undo incorrect match between du and dev above
. search system devices for new location of PVID
. update devices file with new devnames for PVIDs on renamed devices
. label_scan the renamed devs
- continue with command processing
2020-06-23 21:25:41 +03:00
/*
* Creates a list of available devices , does not open or read any ,
* and does not filter them .
*/
if ( ! setup_devices ( cmd ) ) {
log_error ( " Failed to set up devices. " ) ;
return 0 ;
}
2024-07-08 23:32:35 +03:00
if ( ! label_scan_setup_bcache ( ) )
2024-06-19 15:19:52 +03:00
return_0 ;
device usage based on devices file
The LVM devices file lists devices that lvm can use. The default
file is /etc/lvm/devices/system.devices, and the lvmdevices(8)
command is used to add or remove device entries. If the file
does not exist, or if lvm.conf includes use_devicesfile=0, then
lvm will not use a devices file. When the devices file is in use,
the regex filter is not used, and the filter settings in lvm.conf
or on the command line are ignored.
LVM records devices in the devices file using hardware-specific
IDs, such as the WWID, and attempts to use subsystem-specific
IDs for virtual device types. These device IDs are also written
in the VG metadata. When no hardware or virtual ID is available,
lvm falls back using the unstable device name as the device ID.
When devnames are used, lvm performs extra scanning to find
devices if their devname changes, e.g. after reboot.
When proper device IDs are used, an lvm command will not look
at devices outside the devices file, but when devnames are used
as a fallback, lvm will scan devices outside the devices file
to locate PVs on renamed devices. A config setting
search_for_devnames can be used to control the scanning for
renamed devname entries.
Related to the devices file, the new command option
--devices <devnames> allows a list of devices to be specified for
the command to use, overriding the devices file. The listed
devices act as a sort of devices file in terms of limiting which
devices lvm will see and use. Devices that are not listed will
appear to be missing to the lvm command.
Multiple devices files can be kept in /etc/lvm/devices, which
allows lvm to be used with different sets of devices, e.g.
system devices do not need to be exposed to a specific application,
and the application can use lvm on its own set of devices that are
not exposed to the system. The option --devicesfile <filename> is
used to select the devices file to use with the command. Without
the option set, the default system devices file is used.
Setting --devicesfile "" causes lvm to not use a devices file.
An existing, empty devices file means lvm will see no devices.
The new command vgimportdevices adds PVs from a VG to the devices
file and updates the VG metadata to include the device IDs.
vgimportdevices -a will import all VGs into the system devices file.
LVM commands run by dmeventd not use a devices file by default,
and will look at all devices on the system. A devices file can
be created for dmeventd (/etc/lvm/devices/dmeventd.devices) If
this file exists, lvm commands run by dmeventd will use it.
Internal implementaion:
- device_ids_read - read the devices file
. add struct dev_use (du) to cmd->use_devices for each devices file entry
- dev_cache_scan - get /dev entries
. add struct device (dev) to dev_cache for each device on the system
- device_ids_match - match devices file entries to /dev entries
. match each du on cmd->use_devices to a dev in dev_cache, using device ID
. on match, set du->dev, dev->id, dev->flags MATCHED_USE_ID
- label_scan - read lvm headers and metadata from devices
. filters are applied, those that do not need data from the device
. filter-deviceid skips devs without MATCHED_USE_ID, i.e.
skips /dev entries that are not listed in the devices file
. read lvm label from dev
. filters are applied, those that use data from the device
. read lvm metadata from dev
. add info/vginfo structs for PVs/VGs (info is "lvmcache")
- device_ids_find_renamed_devs - handle devices with unstable devname ID
where devname changed
. this step only needed when devs do not have proper device IDs,
and their dev names change, e.g. after reboot sdb becomes sdc.
. detect incorrect match because PVID in the devices file entry
does not match the PVID found when the device was read above
. undo incorrect match between du and dev above
. search system devices for new location of PVID
. update devices file with new devnames for PVIDs on renamed devices
. label_scan the renamed devs
- continue with command processing
2020-06-23 21:25:41 +03:00
/*
* Iterating over all available devices with cmd - > filter filters
* devices ; those returned from dev_iter_get are the devs that
* pass filters , and are those we can use .
*/
2019-10-30 23:11:57 +03:00
2024-07-08 23:32:41 +03:00
if ( ! ( iter = dev_iter_create ( cmd - > filter , 0 ) ) ) {
2019-10-30 23:11:57 +03:00
log_error ( " Scanning failed to get devices. " ) ;
return 0 ;
}
log_debug_devs ( " Reading labels for pvid " ) ;
2024-06-19 15:19:52 +03:00
while ( ( dev = dev_iter_get ( cmd , iter ) ) ) {
2019-10-30 23:11:57 +03:00
memset ( buf , 0 , sizeof ( buf ) ) ;
if ( ! label_scan_open ( dev ) )
continue ;
if ( ! dev_read_bytes ( dev , 512 , LABEL_SIZE , buf ) ) {
_scan_dev_close ( dev ) ;
goto out ;
}
pvh = ( struct pv_header * ) ( buf + 32 ) ;
if ( ! memcmp ( pvh - > pv_uuid , pvid , ID_LEN ) ) {
2024-06-19 15:19:52 +03:00
* dev_out = dev ;
2019-10-30 23:11:57 +03:00
_scan_dev_close ( dev ) ;
break ;
}
_scan_dev_close ( dev ) ;
}
ret = 1 ;
out :
2024-06-19 15:19:52 +03:00
dev_iter_destroy ( iter ) ;
2019-10-30 23:11:57 +03:00
return ret ;
}
2021-11-13 01:46:39 +03:00
/*
* Clear state that label_scan_vg_online ( ) created so it will not
* confuse the standard label_scan ( ) that the caller falls back to .
* the results of filtering ( call filter - > wipe )
* the results of matching device_id ( reset dev and du )
* the results of scanning in lvmcache
*/
static void _clear_scan_state ( struct cmd_context * cmd , struct dm_list * devs )
{
struct device_list * devl ;
struct device * dev ;
struct dev_use * du ;
dm_list_iterate_items ( devl , devs ) {
dev = devl - > dev ;
cmd - > filter - > wipe ( cmd , cmd - > filter , dev , NULL ) ;
dev - > flags & = ~ DEV_MATCHED_USE_ID ;
dev - > id = NULL ;
if ( ( du = get_du_for_dev ( cmd , dev ) ) )
du - > dev = NULL ;
lvmcache_del_dev ( dev ) ;
memset ( dev - > pvid , 0 , ID_LEN ) ;
}
}
2021-11-05 20:19:35 +03:00
/*
* Use files under / run / lvm / , created by pvscan - - cache autoactivation ,
2021-11-08 20:30:25 +03:00
* to optimize device setup / scanning . autoactivation happens during
* system startup when the hints file is not useful , but he pvs_online
* files can provide a similar optimization to the hints file .
*/
int label_scan_vg_online ( struct cmd_context * cmd , const char * vgname ,
int * found_none , int * found_all , int * found_incomplete )
2021-11-05 20:19:35 +03:00
{
struct dm_list pvs_online ;
struct dm_list devs ;
2021-11-13 01:46:39 +03:00
struct dm_list devs_drop ;
2021-11-05 20:19:35 +03:00
struct pv_online * po ;
struct device_list * devl , * devl2 ;
int relax_deviceid_filter = 0 ;
2023-07-13 13:32:44 +03:00
unsigned metadata_pv_count ;
2021-11-06 01:01:34 +03:00
int try_dev_scan = 0 ;
2021-11-05 20:19:35 +03:00
dm_list_init ( & pvs_online ) ;
dm_list_init ( & devs ) ;
2021-11-13 01:46:39 +03:00
dm_list_init ( & devs_drop ) ;
2021-11-05 20:19:35 +03:00
2021-11-08 20:30:25 +03:00
log_debug_devs ( " Finding online devices to scan " ) ;
2021-11-05 20:19:35 +03:00
/*
* First attempt to use / run / lvm / pvs_lookup / vgname which should be
* used in cases where all PVs in a VG do not contain metadata .
* When the pvs_lookup file does not exist , then simply use all
* / run / lvm / pvs_online / pvid files that contain a matching vgname .
* The list of po structs represents the PVs in the VG , and the
* info from the online files tell us which devices those PVs are
* located on .
*/
2021-11-08 20:30:25 +03:00
if ( vgname ) {
if ( ! get_pvs_lookup ( & pvs_online , vgname ) ) {
if ( ! get_pvs_online ( & pvs_online , vgname ) )
goto bad ;
}
} else {
if ( ! get_pvs_online ( & pvs_online , NULL ) )
2021-11-05 20:19:35 +03:00
goto bad ;
}
2021-11-08 20:30:25 +03:00
if ( dm_list_empty ( & pvs_online ) ) {
* found_none = 1 ;
return 1 ;
}
2021-11-06 01:01:34 +03:00
/*
2021-11-09 01:02:48 +03:00
* For each po add a struct dev to dev - cache . This is a faster
2021-11-06 01:01:34 +03:00
* alternative to the usual dev_cache_scan ( ) which looks at all
* devices . If this optimization fails , then fall back to the usual
* dev_cache_scan ( ) .
*/
2021-11-05 20:19:35 +03:00
dm_list_iterate_items ( po , & pvs_online ) {
2021-11-09 01:02:48 +03:00
if ( ! ( po - > dev = setup_dev_in_dev_cache ( cmd , po - > devno , po - > devname [ 0 ] ? po - > devname : NULL ) ) ) {
2024-05-28 17:17:53 +03:00
log_debug ( " No device found for quick mapping of online PV %u:%u %s PVID %s. " ,
MAJOR ( po - > devno ) , MINOR ( po - > devno ) , po - > devname , po - > pvid ) ;
2021-11-06 01:01:34 +03:00
try_dev_scan = 1 ;
2021-11-09 01:02:48 +03:00
continue ;
2021-11-05 20:19:35 +03:00
}
if ( ! ( devl = dm_pool_zalloc ( cmd - > mem , sizeof ( * devl ) ) ) )
goto_bad ;
devl - > dev = po - > dev ;
dm_list_add ( & devs , & devl - > list ) ;
}
2021-11-06 01:01:34 +03:00
/*
* Translating a devno ( major : minor ) into a device name can be
* problematic for some devices that have unusual sysfs layouts , so if
* this happens , do a full dev_cache_scan , which is slower , but is
* sure to find the device .
*/
if ( try_dev_scan ) {
2022-02-23 00:03:11 +03:00
log_debug ( " Repeat dev cache scan to translate devnos. " ) ;
2021-11-06 01:01:34 +03:00
dev_cache_scan ( cmd ) ;
dm_list_iterate_items ( po , & pvs_online ) {
if ( po - > dev )
continue ;
2024-07-08 23:32:41 +03:00
if ( ! ( po - > dev = dev_cache_get_by_devt ( cmd , po - > devno ) ) ) {
2024-05-28 17:17:53 +03:00
log_error ( " No device found for %u:%u PVID %s. " ,
MAJOR ( po - > devno ) , MINOR ( po - > devno ) , po - > pvid ) ;
2021-11-06 01:01:34 +03:00
goto bad ;
}
if ( ! ( devl = dm_pool_zalloc ( cmd - > mem , sizeof ( * devl ) ) ) )
goto_bad ;
devl - > dev = po - > dev ;
dm_list_add ( & devs , & devl - > list ) ;
}
}
2021-11-05 20:19:35 +03:00
/*
* factor code common to pvscan_cache_args
*/
2021-11-13 01:46:39 +03:00
/*
* Match devs with the devices file because special / optimized
* device setup was used which does not check the devices file .
* If a match fails here do not exclude it , that will be done below by
* passes_filter ( ) which runs filter - deviceid . The
* relax_deviceid_filter case needs to be able to work around
* unmatching devs .
*/
2021-11-05 20:19:35 +03:00
if ( cmd - > enable_devices_file ) {
dm_list_iterate_items ( devl , & devs )
device_ids_match_dev ( cmd , devl - > dev ) ;
}
if ( cmd - > enable_devices_list )
device_ids_match_device_list ( cmd ) ;
2023-09-13 20:28:47 +03:00
if ( cmd - > enable_devices_file & &
( device_ids_use_devname ( cmd ) | | cmd - > device_ids_refresh_trigger ) ) {
2021-11-05 20:19:35 +03:00
relax_deviceid_filter = 1 ;
cmd - > filter_deviceid_skip = 1 ;
2021-11-13 01:46:39 +03:00
/* PVIDs read from devs matched to devices file below instead. */
2023-09-13 20:28:47 +03:00
log_debug ( " Skipping device_id filtering " ) ;
2021-11-05 20:19:35 +03:00
}
2023-01-28 00:56:06 +03:00
/*
* See corresponding code in pvscan . This function is used during
* startup autoactivation when udev has not created all symlinks , so
* regex filter containing symlinks doesn ' t work . pvscan has code
* to properly check devs against the filter using DEVLINKS . The
* pvscan will only create pvs_online files for devs that pass the
* filter . We get devs from the pvs_online files , so we inherit the
* regex filtering from pvscan and don ' t have to do it ourself .
*/
2023-02-15 19:08:25 +03:00
cmd - > filter_regex_skip = 1 ;
2023-01-28 00:56:06 +03:00
2021-11-05 20:19:35 +03:00
cmd - > filter_nodata_only = 1 ;
dm_list_iterate_items_safe ( devl , devl2 , & devs ) {
if ( ! cmd - > filter - > passes_filter ( cmd , cmd - > filter , devl - > dev , NULL ) ) {
2023-04-26 15:40:11 +03:00
log_print_unless_silent ( " %s excluded: %s. " ,
dev_name ( devl - > dev ) , dev_filtered_reason ( devl - > dev ) ) ;
2021-11-05 20:19:35 +03:00
dm_list_del ( & devl - > list ) ;
2021-11-13 01:46:39 +03:00
dm_list_add ( & devs_drop , & devl - > list ) ;
2021-11-05 20:19:35 +03:00
}
}
cmd - > filter_nodata_only = 0 ;
/*
* Clear the results of nodata filters that were saved by the
* persistent filter so that the complete set of filters will
* be checked by passes_filter below .
*/
dm_list_iterate_items ( devl , & devs )
cmd - > filter - > wipe ( cmd , cmd - > filter , devl - > dev , NULL ) ;
/*
* Read header from each dev .
* Eliminate non - lvm devs .
* Apply all filters .
*/
log_debug ( " label_scan_vg_online: read and filter devs " ) ;
2024-07-08 23:32:35 +03:00
label_scan_setup_bcache ( ) ;
2021-11-05 20:19:35 +03:00
dm_list_iterate_items_safe ( devl , devl2 , & devs ) {
2021-11-13 01:46:39 +03:00
struct dev_use * du ;
2021-11-05 20:19:35 +03:00
int has_pvid ;
if ( ! label_read_pvid ( devl - > dev , & has_pvid ) ) {
2023-04-26 15:40:11 +03:00
log_print_unless_silent ( " %s cannot read label. " , dev_name ( devl - > dev ) ) ;
2021-11-05 20:19:35 +03:00
dm_list_del ( & devl - > list ) ;
2021-11-13 01:46:39 +03:00
dm_list_add ( & devs_drop , & devl - > list ) ;
2021-11-05 20:19:35 +03:00
continue ;
}
if ( ! has_pvid ) {
/* Not an lvm device */
2023-04-26 15:40:11 +03:00
log_print_unless_silent ( " %s not an lvm device. " , dev_name ( devl - > dev ) ) ;
2021-11-05 20:19:35 +03:00
dm_list_del ( & devl - > list ) ;
2021-11-13 01:46:39 +03:00
dm_list_add ( & devs_drop , & devl - > list ) ;
2021-11-05 20:19:35 +03:00
continue ;
}
/*
* filter - deviceid is not being used because of unstable devnames ,
* so in place of that check if the pvid is in the devices file .
*/
if ( relax_deviceid_filter ) {
2021-11-13 01:46:39 +03:00
if ( ! ( du = get_du_for_pvid ( cmd , devl - > dev - > pvid ) ) ) {
2023-04-26 15:40:11 +03:00
log_print_unless_silent ( " %s excluded by devices file (checking PVID). " ,
dev_name ( devl - > dev ) ) ;
2021-11-05 20:19:35 +03:00
dm_list_del ( & devl - > list ) ;
2021-11-13 01:46:39 +03:00
dm_list_add ( & devs_drop , & devl - > list ) ;
2021-11-05 20:19:35 +03:00
continue ;
2021-11-13 01:46:39 +03:00
} else {
/* Special case matching for devname entries based on pvid. */
log_debug ( " Match device_id %s %s to %s: matching PVID " ,
idtype_to_str ( du - > idtype ) , du - > idname , dev_name ( devl - > dev ) ) ;
2021-11-05 20:19:35 +03:00
}
}
/* Applies all filters, including those that need data from dev. */
if ( ! cmd - > filter - > passes_filter ( cmd , cmd - > filter , devl - > dev , NULL ) ) {
2023-04-26 15:40:11 +03:00
log_print_unless_silent ( " %s excluded: %s. " ,
dev_name ( devl - > dev ) , dev_filtered_reason ( devl - > dev ) ) ;
2021-11-05 20:19:35 +03:00
dm_list_del ( & devl - > list ) ;
2021-11-13 01:46:39 +03:00
dm_list_add ( & devs_drop , & devl - > list ) ;
2021-11-05 20:19:35 +03:00
}
}
if ( relax_deviceid_filter )
cmd - > filter_deviceid_skip = 0 ;
2023-01-28 00:56:06 +03:00
cmd - > filter_regex_skip = 0 ;
2021-11-05 20:19:35 +03:00
free_po_list ( & pvs_online ) ;
2021-11-08 20:30:25 +03:00
if ( dm_list_empty ( & devs ) ) {
2021-11-13 01:46:39 +03:00
_clear_scan_state ( cmd , & devs_drop ) ;
2021-11-08 20:30:25 +03:00
* found_none = 1 ;
2021-11-05 20:19:35 +03:00
return 1 ;
2021-11-08 20:30:25 +03:00
}
2021-11-05 20:19:35 +03:00
/*
* Scan devs to populate lvmcache info , which includes the mda info that ' s
* needed to read vg metadata .
* bcache data from label_read_pvid above is not invalidated so it can
* be reused ( more data may need to be read depending on how much of the
* metadata was covered when reading the pvid . )
*/
_scan_list ( cmd , NULL , & devs , 0 , NULL ) ;
/*
* Check if all PVs from the VG were found after scanning the devs
* produced from the online files . The online files are effectively
* hints that usually work , but are not definitive , so we need to
* be able to fall back to a standard label scan if the online hints
* gave fewer PVs than listed in VG metadata .
*/
2021-11-08 20:30:25 +03:00
if ( vgname ) {
metadata_pv_count = lvmcache_pvsummary_count ( vgname ) ;
if ( metadata_pv_count > dm_list_size ( & devs ) ) {
log_debug ( " Incomplete PV list from online files %d metadata %d. " ,
dm_list_size ( & devs ) , metadata_pv_count ) ;
2021-11-13 01:46:39 +03:00
_clear_scan_state ( cmd , & devs_drop ) ;
_clear_scan_state ( cmd , & devs ) ;
2021-11-08 20:30:25 +03:00
* found_incomplete = 1 ;
return 1 ;
}
2021-11-05 20:19:35 +03:00
}
2021-11-08 20:30:25 +03:00
* found_all = 1 ;
2021-11-05 20:19:35 +03:00
return 1 ;
bad :
2021-11-13 01:46:39 +03:00
_clear_scan_state ( cmd , & devs_drop ) ;
_clear_scan_state ( cmd , & devs ) ;
2021-11-05 20:19:35 +03:00
free_po_list ( & pvs_online ) ;
return 0 ;
}
2018-02-07 00:18:11 +03:00
/*
2019-05-21 20:02:40 +03:00
* Scan devices on the system to discover which are LVM devices .
* Info about the LVM devices ( PVs ) is saved in lvmcache in a
* basic / summary form ( info / vginfo structs ) . The vg_read phase
* uses this summary info to know which PVs to look at for
* processing a given VG .
2018-02-07 00:18:11 +03:00
*/
int label_scan ( struct cmd_context * cmd )
{
struct dm_list all_devs ;
2020-10-20 23:10:08 +03:00
struct dm_list filtered_devs ;
2018-12-07 23:35:22 +03:00
struct dm_list scan_devs ;
2019-04-11 19:49:18 +03:00
struct dm_list hints_list ;
2018-02-07 00:18:11 +03:00
struct dev_iter * iter ;
2018-05-14 21:38:16 +03:00
struct device_list * devl , * devl2 ;
2018-02-07 00:18:11 +03:00
struct device * dev ;
2019-03-04 21:13:09 +03:00
uint64_t max_metadata_size_bytes ;
2019-05-21 20:02:40 +03:00
int using_hints ;
int create_hints = 0 ; /* NEWHINTS_NONE */
2018-02-07 00:18:11 +03:00
log_debug_devs ( " Finding devices to scan " ) ;
dm_list_init ( & all_devs ) ;
2020-10-20 23:10:08 +03:00
dm_list_init ( & filtered_devs ) ;
2018-12-07 23:35:22 +03:00
dm_list_init ( & scan_devs ) ;
2019-04-11 19:49:18 +03:00
dm_list_init ( & hints_list ) ;
2018-02-07 00:18:11 +03:00
2024-07-08 23:32:35 +03:00
if ( ! label_scan_setup_bcache ( ) )
2024-01-26 18:08:54 +03:00
return_0 ;
2020-10-20 23:10:08 +03:00
2024-06-28 21:35:12 +03:00
/*
* Initialize cache of dm device uuids , which uses a special dm kernel
* feature for efficiently querying many dm devs together . ( It ' s done
* here , before processing the hints file , so that the dm uuid checks
* in hint processing can benefit from the dm uuid cache . )
*/
2024-07-08 23:32:41 +03:00
if ( ! dm_devs_cache_update ( ) )
2024-06-03 00:10:05 +03:00
return_0 ;
2024-05-24 21:50:46 +03:00
2018-02-07 00:18:11 +03:00
/*
device usage based on devices file
The LVM devices file lists devices that lvm can use. The default
file is /etc/lvm/devices/system.devices, and the lvmdevices(8)
command is used to add or remove device entries. If the file
does not exist, or if lvm.conf includes use_devicesfile=0, then
lvm will not use a devices file. When the devices file is in use,
the regex filter is not used, and the filter settings in lvm.conf
or on the command line are ignored.
LVM records devices in the devices file using hardware-specific
IDs, such as the WWID, and attempts to use subsystem-specific
IDs for virtual device types. These device IDs are also written
in the VG metadata. When no hardware or virtual ID is available,
lvm falls back using the unstable device name as the device ID.
When devnames are used, lvm performs extra scanning to find
devices if their devname changes, e.g. after reboot.
When proper device IDs are used, an lvm command will not look
at devices outside the devices file, but when devnames are used
as a fallback, lvm will scan devices outside the devices file
to locate PVs on renamed devices. A config setting
search_for_devnames can be used to control the scanning for
renamed devname entries.
Related to the devices file, the new command option
--devices <devnames> allows a list of devices to be specified for
the command to use, overriding the devices file. The listed
devices act as a sort of devices file in terms of limiting which
devices lvm will see and use. Devices that are not listed will
appear to be missing to the lvm command.
Multiple devices files can be kept in /etc/lvm/devices, which
allows lvm to be used with different sets of devices, e.g.
system devices do not need to be exposed to a specific application,
and the application can use lvm on its own set of devices that are
not exposed to the system. The option --devicesfile <filename> is
used to select the devices file to use with the command. Without
the option set, the default system devices file is used.
Setting --devicesfile "" causes lvm to not use a devices file.
An existing, empty devices file means lvm will see no devices.
The new command vgimportdevices adds PVs from a VG to the devices
file and updates the VG metadata to include the device IDs.
vgimportdevices -a will import all VGs into the system devices file.
LVM commands run by dmeventd not use a devices file by default,
and will look at all devices on the system. A devices file can
be created for dmeventd (/etc/lvm/devices/dmeventd.devices) If
this file exists, lvm commands run by dmeventd will use it.
Internal implementaion:
- device_ids_read - read the devices file
. add struct dev_use (du) to cmd->use_devices for each devices file entry
- dev_cache_scan - get /dev entries
. add struct device (dev) to dev_cache for each device on the system
- device_ids_match - match devices file entries to /dev entries
. match each du on cmd->use_devices to a dev in dev_cache, using device ID
. on match, set du->dev, dev->id, dev->flags MATCHED_USE_ID
- label_scan - read lvm headers and metadata from devices
. filters are applied, those that do not need data from the device
. filter-deviceid skips devs without MATCHED_USE_ID, i.e.
skips /dev entries that are not listed in the devices file
. read lvm label from dev
. filters are applied, those that use data from the device
. read lvm metadata from dev
. add info/vginfo structs for PVs/VGs (info is "lvmcache")
- device_ids_find_renamed_devs - handle devices with unstable devname ID
where devname changed
. this step only needed when devs do not have proper device IDs,
and their dev names change, e.g. after reboot sdb becomes sdc.
. detect incorrect match because PVID in the devices file entry
does not match the PVID found when the device was read above
. undo incorrect match between du and dev above
. search system devices for new location of PVID
. update devices file with new devnames for PVIDs on renamed devices
. label_scan the renamed devs
- continue with command processing
2020-06-23 21:25:41 +03:00
* Creates a list of available devices , does not open or read any ,
* and does not filter them . The list of all available devices
* is kept in " dev-cache " , and comes from / dev entries or libudev .
* The list of devs found here needs to be filtered to get the
* list of devs we can use . The dev_iter calls using cmd - > filter
* are what filters the devs .
2018-02-07 00:18:11 +03:00
*/
device usage based on devices file
The LVM devices file lists devices that lvm can use. The default
file is /etc/lvm/devices/system.devices, and the lvmdevices(8)
command is used to add or remove device entries. If the file
does not exist, or if lvm.conf includes use_devicesfile=0, then
lvm will not use a devices file. When the devices file is in use,
the regex filter is not used, and the filter settings in lvm.conf
or on the command line are ignored.
LVM records devices in the devices file using hardware-specific
IDs, such as the WWID, and attempts to use subsystem-specific
IDs for virtual device types. These device IDs are also written
in the VG metadata. When no hardware or virtual ID is available,
lvm falls back using the unstable device name as the device ID.
When devnames are used, lvm performs extra scanning to find
devices if their devname changes, e.g. after reboot.
When proper device IDs are used, an lvm command will not look
at devices outside the devices file, but when devnames are used
as a fallback, lvm will scan devices outside the devices file
to locate PVs on renamed devices. A config setting
search_for_devnames can be used to control the scanning for
renamed devname entries.
Related to the devices file, the new command option
--devices <devnames> allows a list of devices to be specified for
the command to use, overriding the devices file. The listed
devices act as a sort of devices file in terms of limiting which
devices lvm will see and use. Devices that are not listed will
appear to be missing to the lvm command.
Multiple devices files can be kept in /etc/lvm/devices, which
allows lvm to be used with different sets of devices, e.g.
system devices do not need to be exposed to a specific application,
and the application can use lvm on its own set of devices that are
not exposed to the system. The option --devicesfile <filename> is
used to select the devices file to use with the command. Without
the option set, the default system devices file is used.
Setting --devicesfile "" causes lvm to not use a devices file.
An existing, empty devices file means lvm will see no devices.
The new command vgimportdevices adds PVs from a VG to the devices
file and updates the VG metadata to include the device IDs.
vgimportdevices -a will import all VGs into the system devices file.
LVM commands run by dmeventd not use a devices file by default,
and will look at all devices on the system. A devices file can
be created for dmeventd (/etc/lvm/devices/dmeventd.devices) If
this file exists, lvm commands run by dmeventd will use it.
Internal implementaion:
- device_ids_read - read the devices file
. add struct dev_use (du) to cmd->use_devices for each devices file entry
- dev_cache_scan - get /dev entries
. add struct device (dev) to dev_cache for each device on the system
- device_ids_match - match devices file entries to /dev entries
. match each du on cmd->use_devices to a dev in dev_cache, using device ID
. on match, set du->dev, dev->id, dev->flags MATCHED_USE_ID
- label_scan - read lvm headers and metadata from devices
. filters are applied, those that do not need data from the device
. filter-deviceid skips devs without MATCHED_USE_ID, i.e.
skips /dev entries that are not listed in the devices file
. read lvm label from dev
. filters are applied, those that use data from the device
. read lvm metadata from dev
. add info/vginfo structs for PVs/VGs (info is "lvmcache")
- device_ids_find_renamed_devs - handle devices with unstable devname ID
where devname changed
. this step only needed when devs do not have proper device IDs,
and their dev names change, e.g. after reboot sdb becomes sdc.
. detect incorrect match because PVID in the devices file entry
does not match the PVID found when the device was read above
. undo incorrect match between du and dev above
. search system devices for new location of PVID
. update devices file with new devnames for PVIDs on renamed devices
. label_scan the renamed devs
- continue with command processing
2020-06-23 21:25:41 +03:00
if ( ! setup_devices ( cmd ) ) {
log_error ( " Failed to set up devices. " ) ;
return 0 ;
}
2018-02-07 00:18:11 +03:00
2019-07-09 22:48:31 +03:00
/*
* If we know that there will be md components with an end
* superblock , then enable the full md filter before label
* scan begins . FIXME : we could skip the full md check on
* devs that are not identified as PVs , but then we ' d need
* to do something other than using the standard md filter .
*/
if ( cmd - > md_component_detection & & ! cmd - > use_full_md_check & &
! strcmp ( cmd - > md_component_checks , " auto " ) & &
2024-07-08 23:32:41 +03:00
dev_cache_has_md_with_end_superblock ( cmd - > dev_types ) ) {
2019-07-09 22:48:31 +03:00
log_debug ( " Enable full md component check. " ) ;
cmd - > use_full_md_check = 1 ;
}
2019-05-21 20:02:40 +03:00
/*
2020-10-20 23:10:08 +03:00
* Create a list of all devices in dev - cache ( all found on the system . )
* Do not apply filters and do not read any ( the filter arg is NULL ) .
* Invalidate bcache data for all devs ( there will usually be no bcache
* data to invalidate . )
2019-05-21 20:02:40 +03:00
*/
2024-07-08 23:32:41 +03:00
if ( ! ( iter = dev_iter_create ( NULL , 0 ) ) ) {
2022-07-06 01:08:00 +03:00
log_error ( " Failed to get device list. " ) ;
2018-02-07 00:18:11 +03:00
return 0 ;
}
2018-06-15 19:03:55 +03:00
while ( ( dev = dev_iter_get ( cmd , iter ) ) ) {
2018-06-08 15:40:53 +03:00
if ( ! ( devl = zalloc ( sizeof ( * devl ) ) ) )
2018-06-01 18:40:28 +03:00
continue ;
2018-02-07 00:18:11 +03:00
devl - > dev = dev ;
dm_list_add ( & all_devs , & devl - > list ) ;
/*
* label_scan should not generally be called a second time ,
2020-10-20 23:10:08 +03:00
* so this will usually do nothing .
2018-02-07 00:18:11 +03:00
*/
2020-09-15 17:20:16 +03:00
label_scan_invalidate ( dev ) ;
2021-04-23 20:45:34 +03:00
}
2018-02-07 00:18:11 +03:00
dev_iter_destroy ( iter ) ;
2020-10-20 23:10:08 +03:00
/*
* Exclude devices that fail nodata filters . ( Those filters that can be
* checked without reading data from the device . )
*
* The result of checking nodata filters is saved by the " persistent
* filter " , and this result needs to be cleared (wiped) so that the
* complete set of filters ( including those that require data ) can be
* checked in _process_block , where headers have been read .
2022-02-10 23:00:25 +03:00
*
* FIXME : devs that are filtered with data in _process_block
* are not moved to the filtered_devs list like devs filtered
* here without data . Does that have any effect ?
2020-10-20 23:10:08 +03:00
*/
log_debug_devs ( " Filtering devices to scan (nodata) " ) ;
cmd - > filter_nodata_only = 1 ;
dm_list_iterate_items_safe ( devl , devl2 , & all_devs ) {
dev = devl - > dev ;
if ( ! cmd - > filter - > passes_filter ( cmd , cmd - > filter , dev , NULL ) ) {
dm_list_del ( & devl - > list ) ;
dm_list_add ( & filtered_devs , & devl - > list ) ;
if ( dev - > pvid [ 0 ] ) {
2023-04-26 15:40:11 +03:00
log_print_unless_silent ( " Clear pvid and info for filtered dev %s. " ,
dev_name ( dev ) ) ;
2020-10-20 23:10:08 +03:00
lvmcache_del_dev ( dev ) ;
memset ( dev - > pvid , 0 , sizeof ( dev - > pvid ) ) ;
}
}
2018-02-07 00:18:11 +03:00
}
2021-07-16 00:33:50 +03:00
log_debug_devs ( " Filtering devices to scan done (nodata) " ) ;
2020-10-20 23:10:08 +03:00
cmd - > filter_nodata_only = 0 ;
dm_list_iterate_items ( devl , & all_devs )
cmd - > filter - > wipe ( cmd , cmd - > filter , devl - > dev , NULL ) ;
dm_list_iterate_items ( devl , & filtered_devs )
cmd - > filter - > wipe ( cmd , cmd - > filter , devl - > dev , NULL ) ;
2018-02-07 00:18:11 +03:00
2018-12-07 23:35:22 +03:00
/*
2019-05-21 20:02:40 +03:00
* In some common cases we can avoid scanning all devices
* by using hints which tell us which devices are PVs , which
* are the only devices we actually need to scan . Without
* hints we need to scan all devs to find which are PVs .
2018-12-07 23:35:22 +03:00
*
* TODO : if the command is using hints and a single vgname
* arg , we can also take the vg lock here , prior to scanning .
* This means we would not need to rescan the PVs in the VG
* in vg_read ( skip lvmcache_label_rescan_vg ) after the
* vg lock is usually taken . ( Some commands are already
* able to avoid rescan in vg_read , but locking early would
* apply to more cases . )
*/
2019-05-21 20:02:40 +03:00
if ( ! get_hints ( cmd , & hints_list , & create_hints , & all_devs , & scan_devs ) ) {
2018-12-07 23:35:22 +03:00
dm_list_splice ( & scan_devs , & all_devs ) ;
2019-04-11 19:49:18 +03:00
dm_list_init ( & hints_list ) ;
2019-05-21 20:02:40 +03:00
using_hints = 0 ;
} else
using_hints = 1 ;
2018-12-07 23:35:22 +03:00
2019-08-08 21:11:05 +03:00
/*
* If the total number of devices exceeds the soft open file
* limit , then increase the soft limit to the hard / max limit
* in case the number of PVs in scan_devs ( it ' s only the PVs
* which we want to keep open ) is higher than the current
* soft limit .
*/
2021-10-29 22:49:36 +03:00
prepare_open_file_limit ( cmd , dm_list_size ( & scan_devs ) ) ;
2019-08-08 21:11:05 +03:00
2018-12-07 23:35:22 +03:00
/*
* Do the main scan .
*/
2020-10-23 21:53:52 +03:00
_scan_list ( cmd , cmd - > filter , & scan_devs , 0 , NULL ) ;
2018-12-07 23:35:22 +03:00
2019-03-04 21:13:09 +03:00
/*
* Metadata could be larger than total size of bcache , and bcache
* cannot currently be resized during the command . If this is the
* case ( or within reach ) , warn that io_memory_size needs to be
* set larger .
*
* Even if bcache out of space did not cause a failure during scan , it
* may cause a failure during the next vg_read phase or during vg_write .
*
* If there was an error during scan , we could recreate bcache here
* with a larger size and then restart label_scan . But , this does not
2024-08-30 00:05:41 +03:00
* address the problem of writing new metadata that exceeds the bcache
2019-03-04 21:13:09 +03:00
* size and failing , which would often be hit first , i . e . we ' ll fail
2024-08-30 00:05:41 +03:00
* to write new metadata exceeding the max size before we have a chance
2019-03-04 21:13:09 +03:00
* to read any metadata with that size , unless we find an existing vg
* that has been previously created with the larger size .
*
* If the largest metadata is within 1 MB of the bcache size , then start
* warning .
*/
max_metadata_size_bytes = lvmcache_max_metadata_size ( ) ;
if ( max_metadata_size_bytes + ( 1024 * 1024 ) > _current_bcache_size_bytes ) {
/* we want bcache to be 1MB larger than the max metadata seen */
uint64_t want_size_kb = ( max_metadata_size_bytes / 1024 ) + 1024 ;
uint64_t remainder ;
if ( ( remainder = ( want_size_kb % 1024 ) ) )
want_size_kb = want_size_kb + 1024 - remainder ;
log_warn ( " WARNING: metadata may not be usable with current io_memory_size %d KiB " ,
io_memory_size ( ) ) ;
2020-09-16 21:47:06 +03:00
log_warn ( " WARNING: increase lvm.conf io_memory_size to at least %llu KiB " ,
( unsigned long long ) want_size_kb ) ;
2019-03-04 21:13:09 +03:00
}
2019-05-21 20:02:40 +03:00
/*
* If we ' re using hints to limit which devs we scanned , verify
* that those hints were valid , and if not we need to scan the
* rest of the devs .
*/
if ( using_hints ) {
2019-04-11 19:49:18 +03:00
if ( ! validate_hints ( cmd , & hints_list ) ) {
2018-12-07 23:35:22 +03:00
log_debug ( " Will scan %d remaining devices " , dm_list_size ( & all_devs ) ) ;
2020-10-23 21:53:52 +03:00
_scan_list ( cmd , cmd - > filter , & all_devs , 0 , NULL ) ;
device usage based on devices file
The LVM devices file lists devices that lvm can use. The default
file is /etc/lvm/devices/system.devices, and the lvmdevices(8)
command is used to add or remove device entries. If the file
does not exist, or if lvm.conf includes use_devicesfile=0, then
lvm will not use a devices file. When the devices file is in use,
the regex filter is not used, and the filter settings in lvm.conf
or on the command line are ignored.
LVM records devices in the devices file using hardware-specific
IDs, such as the WWID, and attempts to use subsystem-specific
IDs for virtual device types. These device IDs are also written
in the VG metadata. When no hardware or virtual ID is available,
lvm falls back using the unstable device name as the device ID.
When devnames are used, lvm performs extra scanning to find
devices if their devname changes, e.g. after reboot.
When proper device IDs are used, an lvm command will not look
at devices outside the devices file, but when devnames are used
as a fallback, lvm will scan devices outside the devices file
to locate PVs on renamed devices. A config setting
search_for_devnames can be used to control the scanning for
renamed devname entries.
Related to the devices file, the new command option
--devices <devnames> allows a list of devices to be specified for
the command to use, overriding the devices file. The listed
devices act as a sort of devices file in terms of limiting which
devices lvm will see and use. Devices that are not listed will
appear to be missing to the lvm command.
Multiple devices files can be kept in /etc/lvm/devices, which
allows lvm to be used with different sets of devices, e.g.
system devices do not need to be exposed to a specific application,
and the application can use lvm on its own set of devices that are
not exposed to the system. The option --devicesfile <filename> is
used to select the devices file to use with the command. Without
the option set, the default system devices file is used.
Setting --devicesfile "" causes lvm to not use a devices file.
An existing, empty devices file means lvm will see no devices.
The new command vgimportdevices adds PVs from a VG to the devices
file and updates the VG metadata to include the device IDs.
vgimportdevices -a will import all VGs into the system devices file.
LVM commands run by dmeventd not use a devices file by default,
and will look at all devices on the system. A devices file can
be created for dmeventd (/etc/lvm/devices/dmeventd.devices) If
this file exists, lvm commands run by dmeventd will use it.
Internal implementaion:
- device_ids_read - read the devices file
. add struct dev_use (du) to cmd->use_devices for each devices file entry
- dev_cache_scan - get /dev entries
. add struct device (dev) to dev_cache for each device on the system
- device_ids_match - match devices file entries to /dev entries
. match each du on cmd->use_devices to a dev in dev_cache, using device ID
. on match, set du->dev, dev->id, dev->flags MATCHED_USE_ID
- label_scan - read lvm headers and metadata from devices
. filters are applied, those that do not need data from the device
. filter-deviceid skips devs without MATCHED_USE_ID, i.e.
skips /dev entries that are not listed in the devices file
. read lvm label from dev
. filters are applied, those that use data from the device
. read lvm metadata from dev
. add info/vginfo structs for PVs/VGs (info is "lvmcache")
- device_ids_find_renamed_devs - handle devices with unstable devname ID
where devname changed
. this step only needed when devs do not have proper device IDs,
and their dev names change, e.g. after reboot sdb becomes sdc.
. detect incorrect match because PVID in the devices file entry
does not match the PVID found when the device was read above
. undo incorrect match between du and dev above
. search system devices for new location of PVID
. update devices file with new devnames for PVIDs on renamed devices
. label_scan the renamed devs
- continue with command processing
2020-06-23 21:25:41 +03:00
/* scan_devs are the devs that have been scanned */
dm_list_splice ( & scan_devs , & all_devs ) ;
2019-05-21 20:02:40 +03:00
using_hints = 0 ;
create_hints = 0 ;
device usage based on devices file
The LVM devices file lists devices that lvm can use. The default
file is /etc/lvm/devices/system.devices, and the lvmdevices(8)
command is used to add or remove device entries. If the file
does not exist, or if lvm.conf includes use_devicesfile=0, then
lvm will not use a devices file. When the devices file is in use,
the regex filter is not used, and the filter settings in lvm.conf
or on the command line are ignored.
LVM records devices in the devices file using hardware-specific
IDs, such as the WWID, and attempts to use subsystem-specific
IDs for virtual device types. These device IDs are also written
in the VG metadata. When no hardware or virtual ID is available,
lvm falls back using the unstable device name as the device ID.
When devnames are used, lvm performs extra scanning to find
devices if their devname changes, e.g. after reboot.
When proper device IDs are used, an lvm command will not look
at devices outside the devices file, but when devnames are used
as a fallback, lvm will scan devices outside the devices file
to locate PVs on renamed devices. A config setting
search_for_devnames can be used to control the scanning for
renamed devname entries.
Related to the devices file, the new command option
--devices <devnames> allows a list of devices to be specified for
the command to use, overriding the devices file. The listed
devices act as a sort of devices file in terms of limiting which
devices lvm will see and use. Devices that are not listed will
appear to be missing to the lvm command.
Multiple devices files can be kept in /etc/lvm/devices, which
allows lvm to be used with different sets of devices, e.g.
system devices do not need to be exposed to a specific application,
and the application can use lvm on its own set of devices that are
not exposed to the system. The option --devicesfile <filename> is
used to select the devices file to use with the command. Without
the option set, the default system devices file is used.
Setting --devicesfile "" causes lvm to not use a devices file.
An existing, empty devices file means lvm will see no devices.
The new command vgimportdevices adds PVs from a VG to the devices
file and updates the VG metadata to include the device IDs.
vgimportdevices -a will import all VGs into the system devices file.
LVM commands run by dmeventd not use a devices file by default,
and will look at all devices on the system. A devices file can
be created for dmeventd (/etc/lvm/devices/dmeventd.devices) If
this file exists, lvm commands run by dmeventd will use it.
Internal implementaion:
- device_ids_read - read the devices file
. add struct dev_use (du) to cmd->use_devices for each devices file entry
- dev_cache_scan - get /dev entries
. add struct device (dev) to dev_cache for each device on the system
- device_ids_match - match devices file entries to /dev entries
. match each du on cmd->use_devices to a dev in dev_cache, using device ID
. on match, set du->dev, dev->id, dev->flags MATCHED_USE_ID
- label_scan - read lvm headers and metadata from devices
. filters are applied, those that do not need data from the device
. filter-deviceid skips devs without MATCHED_USE_ID, i.e.
skips /dev entries that are not listed in the devices file
. read lvm label from dev
. filters are applied, those that use data from the device
. read lvm metadata from dev
. add info/vginfo structs for PVs/VGs (info is "lvmcache")
- device_ids_find_renamed_devs - handle devices with unstable devname ID
where devname changed
. this step only needed when devs do not have proper device IDs,
and their dev names change, e.g. after reboot sdb becomes sdc.
. detect incorrect match because PVID in the devices file entry
does not match the PVID found when the device was read above
. undo incorrect match between du and dev above
. search system devices for new location of PVID
. update devices file with new devnames for PVIDs on renamed devices
. label_scan the renamed devs
- continue with command processing
2020-06-23 21:25:41 +03:00
/* invalid hints means a new dev probably appeared and
we should search for any missing pvids again . */
unlink_searched_devnames ( cmd ) ;
2018-12-07 23:35:22 +03:00
}
}
2018-02-13 17:58:35 +03:00
2021-11-02 00:01:09 +03:00
free_hints ( & hints_list ) ;
device usage based on devices file
The LVM devices file lists devices that lvm can use. The default
file is /etc/lvm/devices/system.devices, and the lvmdevices(8)
command is used to add or remove device entries. If the file
does not exist, or if lvm.conf includes use_devicesfile=0, then
lvm will not use a devices file. When the devices file is in use,
the regex filter is not used, and the filter settings in lvm.conf
or on the command line are ignored.
LVM records devices in the devices file using hardware-specific
IDs, such as the WWID, and attempts to use subsystem-specific
IDs for virtual device types. These device IDs are also written
in the VG metadata. When no hardware or virtual ID is available,
lvm falls back using the unstable device name as the device ID.
When devnames are used, lvm performs extra scanning to find
devices if their devname changes, e.g. after reboot.
When proper device IDs are used, an lvm command will not look
at devices outside the devices file, but when devnames are used
as a fallback, lvm will scan devices outside the devices file
to locate PVs on renamed devices. A config setting
search_for_devnames can be used to control the scanning for
renamed devname entries.
Related to the devices file, the new command option
--devices <devnames> allows a list of devices to be specified for
the command to use, overriding the devices file. The listed
devices act as a sort of devices file in terms of limiting which
devices lvm will see and use. Devices that are not listed will
appear to be missing to the lvm command.
Multiple devices files can be kept in /etc/lvm/devices, which
allows lvm to be used with different sets of devices, e.g.
system devices do not need to be exposed to a specific application,
and the application can use lvm on its own set of devices that are
not exposed to the system. The option --devicesfile <filename> is
used to select the devices file to use with the command. Without
the option set, the default system devices file is used.
Setting --devicesfile "" causes lvm to not use a devices file.
An existing, empty devices file means lvm will see no devices.
The new command vgimportdevices adds PVs from a VG to the devices
file and updates the VG metadata to include the device IDs.
vgimportdevices -a will import all VGs into the system devices file.
LVM commands run by dmeventd not use a devices file by default,
and will look at all devices on the system. A devices file can
be created for dmeventd (/etc/lvm/devices/dmeventd.devices) If
this file exists, lvm commands run by dmeventd will use it.
Internal implementaion:
- device_ids_read - read the devices file
. add struct dev_use (du) to cmd->use_devices for each devices file entry
- dev_cache_scan - get /dev entries
. add struct device (dev) to dev_cache for each device on the system
- device_ids_match - match devices file entries to /dev entries
. match each du on cmd->use_devices to a dev in dev_cache, using device ID
. on match, set du->dev, dev->id, dev->flags MATCHED_USE_ID
- label_scan - read lvm headers and metadata from devices
. filters are applied, those that do not need data from the device
. filter-deviceid skips devs without MATCHED_USE_ID, i.e.
skips /dev entries that are not listed in the devices file
. read lvm label from dev
. filters are applied, those that use data from the device
. read lvm metadata from dev
. add info/vginfo structs for PVs/VGs (info is "lvmcache")
- device_ids_find_renamed_devs - handle devices with unstable devname ID
where devname changed
. this step only needed when devs do not have proper device IDs,
and their dev names change, e.g. after reboot sdb becomes sdc.
. detect incorrect match because PVID in the devices file entry
does not match the PVID found when the device was read above
. undo incorrect match between du and dev above
. search system devices for new location of PVID
. update devices file with new devnames for PVIDs on renamed devices
. label_scan the renamed devs
- continue with command processing
2020-06-23 21:25:41 +03:00
/*
* Check if the devices_file content is up to date and
* if not update it .
*/
2023-10-28 01:13:25 +03:00
device_ids_validate ( cmd , & scan_devs , using_hints , 0 , NULL ) ;
device usage based on devices file
The LVM devices file lists devices that lvm can use. The default
file is /etc/lvm/devices/system.devices, and the lvmdevices(8)
command is used to add or remove device entries. If the file
does not exist, or if lvm.conf includes use_devicesfile=0, then
lvm will not use a devices file. When the devices file is in use,
the regex filter is not used, and the filter settings in lvm.conf
or on the command line are ignored.
LVM records devices in the devices file using hardware-specific
IDs, such as the WWID, and attempts to use subsystem-specific
IDs for virtual device types. These device IDs are also written
in the VG metadata. When no hardware or virtual ID is available,
lvm falls back using the unstable device name as the device ID.
When devnames are used, lvm performs extra scanning to find
devices if their devname changes, e.g. after reboot.
When proper device IDs are used, an lvm command will not look
at devices outside the devices file, but when devnames are used
as a fallback, lvm will scan devices outside the devices file
to locate PVs on renamed devices. A config setting
search_for_devnames can be used to control the scanning for
renamed devname entries.
Related to the devices file, the new command option
--devices <devnames> allows a list of devices to be specified for
the command to use, overriding the devices file. The listed
devices act as a sort of devices file in terms of limiting which
devices lvm will see and use. Devices that are not listed will
appear to be missing to the lvm command.
Multiple devices files can be kept in /etc/lvm/devices, which
allows lvm to be used with different sets of devices, e.g.
system devices do not need to be exposed to a specific application,
and the application can use lvm on its own set of devices that are
not exposed to the system. The option --devicesfile <filename> is
used to select the devices file to use with the command. Without
the option set, the default system devices file is used.
Setting --devicesfile "" causes lvm to not use a devices file.
An existing, empty devices file means lvm will see no devices.
The new command vgimportdevices adds PVs from a VG to the devices
file and updates the VG metadata to include the device IDs.
vgimportdevices -a will import all VGs into the system devices file.
LVM commands run by dmeventd not use a devices file by default,
and will look at all devices on the system. A devices file can
be created for dmeventd (/etc/lvm/devices/dmeventd.devices) If
this file exists, lvm commands run by dmeventd will use it.
Internal implementaion:
- device_ids_read - read the devices file
. add struct dev_use (du) to cmd->use_devices for each devices file entry
- dev_cache_scan - get /dev entries
. add struct device (dev) to dev_cache for each device on the system
- device_ids_match - match devices file entries to /dev entries
. match each du on cmd->use_devices to a dev in dev_cache, using device ID
. on match, set du->dev, dev->id, dev->flags MATCHED_USE_ID
- label_scan - read lvm headers and metadata from devices
. filters are applied, those that do not need data from the device
. filter-deviceid skips devs without MATCHED_USE_ID, i.e.
skips /dev entries that are not listed in the devices file
. read lvm label from dev
. filters are applied, those that use data from the device
. read lvm metadata from dev
. add info/vginfo structs for PVs/VGs (info is "lvmcache")
- device_ids_find_renamed_devs - handle devices with unstable devname ID
where devname changed
. this step only needed when devs do not have proper device IDs,
and their dev names change, e.g. after reboot sdb becomes sdc.
. detect incorrect match because PVID in the devices file entry
does not match the PVID found when the device was read above
. undo incorrect match between du and dev above
. search system devices for new location of PVID
. update devices file with new devnames for PVIDs on renamed devices
. label_scan the renamed devs
- continue with command processing
2020-06-23 21:25:41 +03:00
2018-05-14 21:38:16 +03:00
dm_list_iterate_items_safe ( devl , devl2 , & all_devs ) {
dm_list_del ( & devl - > list ) ;
2018-06-08 15:40:53 +03:00
free ( devl ) ;
2018-05-14 21:38:16 +03:00
}
2018-12-07 23:35:22 +03:00
dm_list_iterate_items_safe ( devl , devl2 , & scan_devs ) {
dm_list_del ( & devl - > list ) ;
free ( devl ) ;
}
2021-01-19 01:26:02 +03:00
dm_list_iterate_items_safe ( devl , devl2 , & filtered_devs ) {
dm_list_del ( & devl - > list ) ;
free ( devl ) ;
}
2021-02-06 01:16:03 +03:00
/*
* Look for md components that might have been missed by filter - md
* during the scan . With the label scanning complete we have metadata
* available that can sometimes offer a clue that a dev is actually an
* md component ( device name hint , pv size vs dev size ) . In some of
* those cases we may want to do a full md check on a dev that has been
* scanned . This is done before hints are written so that any devs
* dropped due to being md components will not be included in a new
* hint file .
*/
lvmcache_extra_md_component_checks ( cmd ) ;
2019-05-21 20:02:40 +03:00
/*
* If hints were not available / usable , then we scanned all devs ,
* and we now know which are PVs . Save this list of PVs we ' ve
* identified as hints for the next command to use .
* ( create_hints variable has NEWHINTS_X value which indicates
* the reason for creating the new hints . )
*/
2023-10-05 23:22:32 +03:00
if ( create_hints & & ! cmd - > device_ids_invalid )
2019-05-21 20:02:40 +03:00
write_hint_file ( cmd , create_hints ) ;
2018-12-07 23:35:22 +03:00
2018-02-13 17:58:35 +03:00
return 1 ;
2018-02-07 00:18:11 +03:00
}
2020-10-15 22:05:45 +03:00
/*
* Read the header of the disk and if it ' s a PV
* save the pvid in dev - > pvid .
*/
2021-04-24 01:32:37 +03:00
int label_read_pvid ( struct device * dev , int * has_pvid )
2020-10-15 22:05:45 +03:00
{
2024-01-26 18:08:54 +03:00
char buf [ 4096 ] __attribute__ ( ( aligned ( 8 ) ) ) = { 0 } ;
2020-10-15 22:05:45 +03:00
struct label_header * lh ;
struct pv_header * pvh ;
if ( ! label_scan_open ( dev ) )
return_0 ;
/*
* We could do :
* dev_read_bytes ( dev , 512 , LABEL_SIZE , buf ) ;
* which works , but there ' s a bcache issue that
* prevents proper invalidation after that .
*/
if ( ! dev_read_bytes ( dev , 0 , 4096 , buf ) ) {
label_scan_invalidate ( dev ) ;
2021-04-24 01:32:37 +03:00
return_0 ;
2020-10-15 22:05:45 +03:00
}
2021-04-24 01:32:37 +03:00
if ( has_pvid )
* has_pvid = 0 ;
2020-10-15 22:05:45 +03:00
lh = ( struct label_header * ) ( buf + 512 ) ;
if ( memcmp ( lh - > id , LABEL_ID , sizeof ( lh - > id ) ) ) {
2021-11-13 01:46:39 +03:00
/* Not an lvm device */
2020-10-15 22:05:45 +03:00
label_scan_invalidate ( dev ) ;
2021-04-24 01:32:37 +03:00
return 1 ;
2020-10-15 22:05:45 +03:00
}
/*
* wipefs - a just clears the type field , leaving the
* rest of the label_header intact .
*/
if ( memcmp ( lh - > type , LVM2_LABEL , sizeof ( lh - > type ) ) ) {
2021-11-13 01:46:39 +03:00
/* Not an lvm device */
2020-10-15 22:05:45 +03:00
label_scan_invalidate ( dev ) ;
2021-04-24 01:32:37 +03:00
return 1 ;
2020-10-15 22:05:45 +03:00
}
2021-04-24 01:32:37 +03:00
if ( has_pvid )
* has_pvid = 1 ;
2020-10-15 22:05:45 +03:00
pvh = ( struct pv_header * ) ( buf + 512 + 32 ) ;
memcpy ( dev - > pvid , pvh - > pv_uuid , ID_LEN ) ;
return 1 ;
}
2020-10-21 20:20:17 +03:00
/*
* label_scan_devs without invalidating data for the devs first ,
* when the caller wants to make use of any bcache data that
* they may have already read .
*/
int label_scan_devs_cached ( struct cmd_context * cmd , struct dev_filter * f , struct dm_list * devs )
{
2024-07-08 23:32:35 +03:00
if ( ! scan_bcache )
2020-10-21 20:20:17 +03:00
return 0 ;
2020-10-23 21:53:52 +03:00
_scan_list ( cmd , f , devs , 0 , NULL ) ;
2020-10-21 20:20:17 +03:00
return 1 ;
}
2018-02-07 00:18:11 +03:00
/*
* Scan and cache lvm data from the listed devices . If a device is already
* scanned and cached , this replaces the previously cached lvm data for the
* device . This is called when vg_read ( ) wants to guarantee that it is using
* the latest data from the devices in the VG ( since the scan populated bcache
* without a lock . )
*/
2018-05-04 01:12:07 +03:00
int label_scan_devs ( struct cmd_context * cmd , struct dev_filter * f , struct dm_list * devs )
2018-02-07 00:18:11 +03:00
{
struct device_list * devl ;
2024-07-08 23:32:35 +03:00
if ( ! label_scan_setup_bcache ( ) )
2024-01-26 18:08:54 +03:00
return_0 ;
2018-03-07 00:05:27 +03:00
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
dm_list_iterate_items ( devl , devs ) {
if ( _in_bcache ( devl - > dev ) )
2024-07-08 23:32:35 +03:00
_invalidate_di ( scan_bcache , devl - > dev - > bcache_di ) ;
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
}
2018-02-13 17:58:35 +03:00
2020-10-23 21:53:52 +03:00
_scan_list ( cmd , f , devs , 0 , NULL ) ;
2018-02-13 17:58:35 +03:00
2019-06-12 00:17:24 +03:00
return 1 ;
}
int label_scan_devs_rw ( struct cmd_context * cmd , struct dev_filter * f , struct dm_list * devs )
{
struct device_list * devl ;
2024-07-08 23:32:35 +03:00
if ( ! label_scan_setup_bcache ( ) )
2024-01-26 18:08:54 +03:00
return_0 ;
2019-06-12 00:17:24 +03:00
dm_list_iterate_items ( devl , devs ) {
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
if ( _in_bcache ( devl - > dev ) )
2024-07-08 23:32:35 +03:00
_invalidate_di ( scan_bcache , devl - > dev - > bcache_di ) ;
2019-06-12 00:17:24 +03:00
devl - > dev - > flags | = DEV_BCACHE_WRITE ;
}
2020-10-23 21:53:52 +03:00
_scan_list ( cmd , f , devs , 0 , NULL ) ;
2018-02-13 17:58:35 +03:00
return 1 ;
}
2020-10-23 21:53:52 +03:00
int label_scan_devs_excl ( struct cmd_context * cmd , struct dev_filter * f , struct dm_list * devs )
2018-02-13 17:58:35 +03:00
{
struct device_list * devl ;
int failed = 0 ;
dm_list_iterate_items ( devl , devs ) {
2020-09-15 17:20:16 +03:00
label_scan_invalidate ( devl - > dev ) ;
2018-02-13 17:58:35 +03:00
/*
* With this flag set , _scan_dev_open ( ) done by
* _scan_list ( ) will do open EXCL
*/
devl - > dev - > flags | = DEV_BCACHE_EXCL ;
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
devl - > dev - > flags | = DEV_BCACHE_WRITE ;
2018-02-07 00:18:11 +03:00
}
2020-10-23 21:53:52 +03:00
_scan_list ( cmd , f , devs , 1 , & failed ) ;
2018-02-13 17:58:35 +03:00
if ( failed )
return 0 ;
return 1 ;
2018-02-07 00:18:11 +03:00
}
void label_scan_invalidate ( struct device * dev )
{
if ( _in_bcache ( dev ) ) {
2024-07-08 23:32:35 +03:00
_invalidate_di ( scan_bcache , dev - > bcache_di ) ;
2018-02-13 17:58:35 +03:00
_scan_dev_close ( dev ) ;
2018-02-07 00:18:11 +03:00
}
}
2018-02-16 23:18:55 +03:00
/*
* If a PV is stacked on an LV , then the LV is kept open
* in bcache , and needs to be closed so the open fd doesn ' t
* interfere with processing the LV .
*/
void label_scan_invalidate_lv ( struct cmd_context * cmd , struct logical_volume * lv )
{
2024-07-01 18:58:23 +03:00
struct lvinfo lvinfo ;
2018-02-16 23:18:55 +03:00
struct device * dev ;
2024-07-01 18:58:23 +03:00
dev_t devt ;
2018-02-16 23:18:55 +03:00
2024-07-01 18:58:23 +03:00
/* FIXME: use dev_cache_get_existing() with the lv name,
which allow us to skip the getting devno from lv_info . */
if ( lv_info ( cmd , lv , 0 , & lvinfo , 0 , 0 ) & & lvinfo . exists ) {
/* FIXME: Still unclear what is it supposed to find */
devt = MKDEV ( lvinfo . major , lvinfo . minor ) ;
2024-07-08 23:32:41 +03:00
if ( ( dev = dev_cache_get_by_devt ( cmd , devt ) ) )
2024-07-01 18:58:23 +03:00
label_scan_invalidate ( dev ) ;
}
2018-02-16 23:18:55 +03:00
}
2021-12-15 13:45:22 +03:00
void label_scan_invalidate_lvs ( struct cmd_context * cmd , struct dm_list * lvs )
{
struct lv_list * lvl ;
2022-02-23 00:03:11 +03:00
/*
2022-03-01 21:22:46 +03:00
* This is only needed when the command sees PVs stacked on LVs which
* will only happen with scan_lvs = 1.
2022-02-23 00:03:11 +03:00
*/
2022-03-01 21:22:46 +03:00
if ( ! cmd - > scan_lvs )
return ;
2021-12-15 13:45:22 +03:00
2024-05-24 21:50:46 +03:00
log_debug ( " Invalidating devs for any PVs on LVs. " ) ;
2024-07-08 23:32:41 +03:00
if ( dm_devs_cache_use ( ) )
2024-07-01 19:50:59 +03:00
dm_devs_cache_label_invalidate ( cmd ) ;
2024-07-01 19:30:51 +03:00
else {
dm_list_iterate_items ( lvl , lvs )
label_scan_invalidate_lv ( cmd , lvl - > lv ) ;
}
2021-12-15 13:45:22 +03:00
}
2018-02-07 00:18:11 +03:00
/*
2018-02-28 01:35:47 +03:00
* Empty the bcache of all blocks and close all open fds ,
* but keep the bcache set up .
2018-02-07 00:18:11 +03:00
*/
2018-02-28 01:35:47 +03:00
void label_scan_drop ( struct cmd_context * cmd )
2018-02-07 00:18:11 +03:00
{
struct dev_iter * iter ;
struct device * dev ;
2024-07-08 23:32:41 +03:00
if ( ! ( iter = dev_iter_create ( NULL , 0 ) ) )
2018-02-07 00:18:11 +03:00
return ;
2018-06-15 19:03:55 +03:00
while ( ( dev = dev_iter_get ( cmd , iter ) ) ) {
scan: wipe filters when dropping scanned data
Fix clearing persistent filter state when clearing all
the state from a label_scan.
label_scan reads devs and saves info in bcache, lvmcache,
and in the persistent filter. In some uncommon cases, an
lvm command wants to clear all info from a prior label_scan,
and repeat label_scan from scratch. In these cases, info
in lvmcache, bcache and the persistent filter all need to
be cleared before repeating label_scan.
By missing the persistent filter wiping, outdated persistent
filter info, from a prior label_scan, could cause lvm to
incorrectly filter devices that change between polling intervals.
(i.e. if the device changes in such a way that the filtering
results change.)
A case where lvm wants to do multiple label_scans is a
polling command (like lvconvert --merge), when lvmpolld
has been disabled, so that the command itself needs to
to do repeated polling checks.
2021-02-10 23:06:58 +03:00
cmd - > filter - > wipe ( cmd , cmd - > filter , dev , NULL ) ;
2018-02-20 00:40:44 +03:00
if ( _in_bcache ( dev ) )
_scan_dev_close ( dev ) ;
}
2018-02-07 00:18:11 +03:00
dev_iter_destroy ( iter ) ;
2018-02-28 01:35:47 +03:00
}
/*
* Close devices that are open because bcache is holding blocks for them .
* Destroy the bcache .
*/
void label_scan_destroy ( struct cmd_context * cmd )
{
2024-07-08 23:32:35 +03:00
if ( ! scan_bcache )
2018-02-28 01:35:47 +03:00
return ;
label_scan_drop ( cmd ) ;
2018-02-07 00:18:11 +03:00
2024-07-08 23:32:35 +03:00
bcache_destroy ( scan_bcache ) ;
scan_bcache = NULL ;
2018-02-07 00:18:11 +03:00
}
/*
* Read ( or re - read ) and process ( or re - process ) the data for a device . This
* will reset ( clear and repopulate ) the bcache and lvmcache info for this
* device . There are only a couple odd places that want to reread a specific
* device , this is not a commonly used function .
*/
2021-10-14 22:02:59 +03:00
int label_scan_dev ( struct cmd_context * cmd , struct device * dev )
2018-02-07 00:18:11 +03:00
{
struct dm_list one_dev ;
struct device_list * devl ;
2018-02-13 17:58:35 +03:00
int failed = 0 ;
2018-02-07 00:18:11 +03:00
/* scanning is done by list, so make a single item list for this dev */
2018-06-08 15:40:53 +03:00
if ( ! ( devl = zalloc ( sizeof ( * devl ) ) ) )
2018-02-07 00:18:11 +03:00
return 0 ;
devl - > dev = dev ;
dm_list_init ( & one_dev ) ;
dm_list_add ( & one_dev , & devl - > list ) ;
2020-09-15 17:20:16 +03:00
label_scan_invalidate ( dev ) ;
2018-02-07 00:18:11 +03:00
2021-10-14 22:02:59 +03:00
_scan_list ( cmd , NULL , & one_dev , 0 , & failed ) ;
2018-02-07 00:18:11 +03:00
2018-06-08 15:40:53 +03:00
free ( devl ) ;
2018-02-07 00:18:11 +03:00
2018-02-13 17:58:35 +03:00
if ( failed )
return 0 ;
return 1 ;
2018-02-07 00:18:11 +03:00
}
2018-02-20 00:40:44 +03:00
/*
* This is needed to write to a new non - lvm device .
* Scanning that dev would not keep it open or in
* bcache , but to use bcache_write we need the dev
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
* to be open so we can use dev - > bcache_di to write .
2018-02-20 00:40:44 +03:00
*/
int label_scan_open ( struct device * dev )
{
if ( ! _in_bcache ( dev ) )
return _scan_dev_open ( dev ) ;
return 1 ;
}
2018-05-16 21:24:00 +03:00
int label_scan_open_excl ( struct device * dev )
{
2018-06-20 19:32:45 +03:00
if ( _in_bcache ( dev ) & & ! ( dev - > flags & DEV_BCACHE_EXCL ) ) {
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
log_debug ( " close and reopen excl %s " , dev_name ( dev ) ) ;
2024-07-08 23:32:35 +03:00
_invalidate_di ( scan_bcache , dev - > bcache_di ) ;
2018-06-20 19:32:45 +03:00
_scan_dev_close ( dev ) ;
}
2018-05-16 21:24:00 +03:00
dev - > flags | = DEV_BCACHE_EXCL ;
2018-06-20 19:32:45 +03:00
dev - > flags | = DEV_BCACHE_WRITE ;
2018-05-16 21:24:00 +03:00
return label_scan_open ( dev ) ;
}
2019-04-26 22:49:27 +03:00
int label_scan_open_rw ( struct device * dev )
{
if ( _in_bcache ( dev ) & & ! ( dev - > flags & DEV_BCACHE_WRITE ) ) {
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
log_debug ( " close and reopen rw %s " , dev_name ( dev ) ) ;
2024-07-08 23:32:35 +03:00
_invalidate_di ( scan_bcache , dev - > bcache_di ) ;
2019-04-26 22:49:27 +03:00
_scan_dev_close ( dev ) ;
}
dev - > flags | = DEV_BCACHE_WRITE ;
return label_scan_open ( dev ) ;
}
2020-09-18 22:42:23 +03:00
int label_scan_reopen_rw ( struct device * dev )
{
2022-02-23 00:03:11 +03:00
const char * name ;
2020-09-18 22:42:23 +03:00
int flags = 0 ;
int prev_fd = dev - > bcache_fd ;
int fd ;
2022-02-23 00:03:11 +03:00
if ( dm_list_empty ( & dev - > aliases ) ) {
2024-05-28 17:17:53 +03:00
log_error ( " Cannot reopen rw device %u:%u with no valid paths di %d fd %d. " ,
MAJOR ( dev - > dev ) , MINOR ( dev - > dev ) , dev - > bcache_di , dev - > bcache_fd ) ;
2022-02-23 00:03:11 +03:00
return 0 ;
}
name = dev_name ( dev ) ;
if ( ! name | | name [ 0 ] ! = ' / ' ) {
2024-05-28 17:17:53 +03:00
log_error ( " Cannot reopen rw device %u:%u with no valid name di %d fd %d. " ,
MAJOR ( dev - > dev ) , MINOR ( dev - > dev ) , dev - > bcache_di , dev - > bcache_fd ) ;
2022-02-23 00:03:11 +03:00
return 0 ;
}
2020-09-18 22:42:23 +03:00
if ( ! ( dev - > flags & DEV_IN_BCACHE ) ) {
if ( ( dev - > bcache_fd ! = - 1 ) | | ( dev - > bcache_di ! = - 1 ) ) {
/* shouldn't happen */
log_debug ( " Reopen writeable %s uncached fd %d di %d " ,
dev_name ( dev ) , dev - > bcache_fd , dev - > bcache_di ) ;
return 0 ;
}
2020-09-25 19:59:49 +03:00
dev - > flags | = DEV_BCACHE_WRITE ;
return _scan_dev_open ( dev ) ;
2020-09-18 22:42:23 +03:00
}
if ( ( dev - > flags & DEV_BCACHE_WRITE ) )
return 1 ;
if ( dev - > bcache_fd = = - 1 ) {
log_error ( " Failed to open writable %s index %d fd none " ,
dev_name ( dev ) , dev - > bcache_di ) ;
return 0 ;
}
if ( dev - > bcache_di = = - 1 ) {
log_error ( " Failed to open writeable %s index none fd %d " ,
dev_name ( dev ) , dev - > bcache_fd ) ;
return 0 ;
}
flags | = O_DIRECT ;
flags | = O_NOATIME ;
flags | = O_RDWR ;
2022-02-23 00:03:11 +03:00
fd = open ( name , flags , 0777 ) ;
2020-09-18 22:42:23 +03:00
if ( fd < 0 ) {
log_error ( " Failed to open rw %s errno %d di %d fd %d. " ,
dev_name ( dev ) , errno , dev - > bcache_di , dev - > bcache_fd ) ;
return 0 ;
}
if ( ! bcache_change_fd ( dev - > bcache_di , fd ) ) {
log_error ( " Failed to change to rw fd %s di %d fd %d. " ,
dev_name ( dev ) , dev - > bcache_di , fd ) ;
2021-03-10 03:26:30 +03:00
if ( close ( fd ) )
log_sys_debug ( " close " , dev_name ( dev ) ) ;
2020-09-18 22:42:23 +03:00
return 0 ;
}
if ( close ( dev - > bcache_fd ) )
log_debug ( " reopen writeable %s close prev errno %d di %d fd %d. " ,
dev_name ( dev ) , errno , dev - > bcache_di , dev - > bcache_fd ) ;
dev - > flags | = DEV_IN_BCACHE ;
dev - > flags | = DEV_BCACHE_WRITE ;
dev - > bcache_fd = fd ;
log_debug ( " reopen writable %s di %d prev %d fd %d " ,
dev_name ( dev ) , dev - > bcache_di , prev_fd , fd ) ;
return 1 ;
}
2018-05-09 18:34:28 +03:00
bool dev_read_bytes ( struct device * dev , uint64_t start , size_t len , void * data )
2018-02-27 20:26:04 +03:00
{
2024-07-08 23:32:35 +03:00
if ( ! scan_bcache ) {
2018-05-11 17:50:25 +03:00
/* Should not happen */
log_error ( " dev_read bcache not set up %s " , dev_name ( dev ) ) ;
return false ;
2018-02-27 20:26:04 +03:00
}
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
if ( dev - > bcache_di < 0 ) {
2019-05-02 21:32:30 +03:00
/* This is not often needed. */
2018-03-09 20:39:12 +03:00
if ( ! label_scan_open ( dev ) ) {
2020-09-16 21:47:06 +03:00
log_error ( " Error opening device %s for reading at %llu length %u. " ,
dev_name ( dev ) , ( unsigned long long ) start , ( uint32_t ) len ) ;
2018-03-09 20:39:12 +03:00
return false ;
}
2018-02-27 20:26:04 +03:00
}
2024-07-08 23:32:35 +03:00
if ( ! bcache_read_bytes ( scan_bcache , dev - > bcache_di , start , len , data ) ) {
2020-09-16 21:47:06 +03:00
log_error ( " Error reading device %s at %llu length %u. " ,
dev_name ( dev ) , ( unsigned long long ) start , ( uint32_t ) len ) ;
2018-02-28 00:03:56 +03:00
label_scan_invalidate ( dev ) ;
return false ;
}
return true ;
2018-02-27 20:26:04 +03:00
}
2018-05-09 18:34:28 +03:00
bool dev_write_bytes ( struct device * dev , uint64_t start , size_t len , void * data )
2018-02-27 20:26:04 +03:00
{
2018-04-09 21:57:44 +03:00
if ( test_mode ( ) )
return true ;
2024-07-08 23:32:35 +03:00
if ( ! scan_bcache ) {
2018-05-11 17:50:25 +03:00
/* Should not happen */
log_error ( " dev_write bcache not set up %s " , dev_name ( dev ) ) ;
return false ;
2018-02-27 20:26:04 +03:00
}
2018-08-01 18:26:28 +03:00
if ( _in_bcache ( dev ) & & ! ( dev - > flags & DEV_BCACHE_WRITE ) ) {
2018-06-20 19:32:45 +03:00
/* FIXME: avoid tossing out bcache blocks just to replace 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
log_debug ( " close and reopen to write %s " , dev_name ( dev ) ) ;
2024-07-08 23:32:35 +03:00
_invalidate_di ( scan_bcache , dev - > bcache_di ) ;
2018-06-20 19:32:45 +03:00
_scan_dev_close ( dev ) ;
dev - > flags | = DEV_BCACHE_WRITE ;
2021-04-22 17:49:34 +03:00
( void ) label_scan_open ( dev ) ; /* checked later */
2018-06-20 19:32:45 +03:00
}
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
if ( dev - > bcache_di < 0 ) {
2019-05-02 21:32:30 +03:00
/* This is not often needed. */
2018-06-20 19:32:45 +03:00
dev - > flags | = DEV_BCACHE_WRITE ;
2018-03-09 20:39:12 +03:00
if ( ! label_scan_open ( dev ) ) {
2020-09-16 21:47:06 +03:00
log_error ( " Error opening device %s for writing at %llu length %u. " ,
dev_name ( dev ) , ( unsigned long long ) start , ( uint32_t ) len ) ;
2018-03-09 20:39:12 +03:00
return false ;
}
2018-02-27 20:26:04 +03:00
}
2024-07-08 23:32:35 +03:00
if ( ! bcache_write_bytes ( scan_bcache , dev - > bcache_di , start , len , data ) ) {
2020-09-16 21:47:06 +03:00
log_error ( " Error writing device %s at %llu length %u. " ,
dev_name ( dev ) , ( unsigned long long ) start , ( uint32_t ) len ) ;
2019-11-13 18:15:07 +03:00
dev_unset_last_byte ( dev ) ;
2018-05-01 17:33:55 +03:00
label_scan_invalidate ( dev ) ;
return false ;
}
2024-07-08 23:32:35 +03:00
if ( ! bcache_flush ( scan_bcache ) ) {
2020-09-16 21:47:06 +03:00
log_error ( " Error writing device %s at %llu length %u. " ,
dev_name ( dev ) , ( unsigned long long ) start , ( uint32_t ) len ) ;
2019-11-13 18:15:07 +03:00
dev_unset_last_byte ( dev ) ;
2018-02-28 00:03:56 +03:00
label_scan_invalidate ( dev ) ;
return false ;
}
return true ;
2018-02-27 20:26:04 +03:00
}
2019-11-27 00:46:49 +03:00
bool dev_invalidate_bytes ( struct device * dev , uint64_t start , size_t len )
{
2024-07-08 23:32:35 +03:00
return bcache_invalidate_bytes ( scan_bcache , dev - > bcache_di , start , len ) ;
2019-11-27 00:46:49 +03:00
}
2021-06-29 02:10:47 +03:00
void dev_invalidate ( struct device * dev )
{
2024-07-08 23:32:35 +03:00
bcache_invalidate_di ( scan_bcache , dev - > bcache_di ) ;
2021-06-29 02:10:47 +03:00
}
2018-05-09 18:34:28 +03:00
bool dev_write_zeros ( struct device * dev , uint64_t start , size_t len )
2018-02-27 20:26:04 +03:00
{
2020-09-15 17:29:12 +03:00
return dev_set_bytes ( dev , start , len , 0 ) ;
2018-02-27 20:26:04 +03:00
}
2018-05-09 18:34:28 +03:00
bool dev_set_bytes ( struct device * dev , uint64_t start , size_t len , uint8_t val )
{
2020-09-16 21:54:16 +03:00
bool rv ;
2018-05-09 18:34:28 +03:00
if ( test_mode ( ) )
return true ;
2024-07-08 23:32:35 +03:00
if ( ! scan_bcache ) {
2018-05-11 17:50:25 +03:00
log_error ( " dev_set_bytes bcache not set up %s " , dev_name ( dev ) ) ;
2018-05-09 18:34:28 +03:00
return false ;
}
2018-08-01 18:26:28 +03:00
if ( _in_bcache ( dev ) & & ! ( dev - > flags & DEV_BCACHE_WRITE ) ) {
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
log_debug ( " close and reopen to write %s " , dev_name ( dev ) ) ;
2024-07-08 23:32:35 +03:00
_invalidate_di ( scan_bcache , dev - > bcache_di ) ;
2018-06-20 19:32:45 +03:00
_scan_dev_close ( dev ) ;
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
/* goes to label_scan_open() since bcache_di < 0 */
2018-06-20 19:32:45 +03:00
}
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
if ( dev - > bcache_di = = - 1 ) {
2019-05-02 21:32:30 +03:00
/* This is not often needed. */
2018-06-20 19:32:45 +03:00
dev - > flags | = DEV_BCACHE_WRITE ;
2018-05-09 18:34:28 +03:00
if ( ! label_scan_open ( dev ) ) {
2020-09-16 21:47:06 +03:00
log_error ( " Error opening device %s for writing at %llu length %u. " ,
dev_name ( dev ) , ( unsigned long long ) start , ( uint32_t ) len ) ;
2018-05-09 18:34:28 +03:00
return false ;
}
}
2018-10-30 00:53:17 +03:00
dev_set_last_byte ( dev , start + len ) ;
2020-09-16 21:54:16 +03:00
if ( ! val )
2024-07-08 23:32:35 +03:00
rv = bcache_zero_bytes ( scan_bcache , dev - > bcache_di , start , len ) ;
2020-09-16 21:54:16 +03:00
else
2024-07-08 23:32:35 +03:00
rv = bcache_set_bytes ( scan_bcache , dev - > bcache_di , start , len , val ) ;
2020-09-16 21:54:16 +03:00
if ( ! rv ) {
log_error ( " Error writing device value %s at %llu length %u. " ,
2020-09-16 21:47:06 +03:00
dev_name ( dev ) , ( unsigned long long ) start , ( uint32_t ) len ) ;
2020-09-16 21:54:16 +03:00
goto fail ;
2018-05-09 18:34:28 +03:00
}
2024-07-08 23:32:35 +03:00
if ( ! bcache_flush ( scan_bcache ) ) {
2020-09-16 21:47:06 +03:00
log_error ( " Error writing device %s at %llu length %u. " ,
dev_name ( dev ) , ( unsigned long long ) start , ( uint32_t ) len ) ;
2020-09-16 21:54:16 +03:00
goto fail ;
2018-05-09 18:34:28 +03:00
}
2018-10-30 00:53:17 +03:00
dev_unset_last_byte ( dev ) ;
2018-05-09 18:34:28 +03:00
return true ;
2020-09-16 21:54:16 +03:00
fail :
dev_unset_last_byte ( dev ) ;
label_scan_invalidate ( dev ) ;
return false ;
2018-05-09 18:34:28 +03:00
}
2018-10-30 00:53:17 +03:00
void dev_set_last_byte ( struct device * dev , uint64_t offset )
{
2019-07-26 22:21:08 +03:00
unsigned int physical_block_size = 0 ;
unsigned int logical_block_size = 0 ;
unsigned int bs ;
2018-10-30 00:53:17 +03:00
2019-07-26 22:21:08 +03:00
if ( ! dev_get_direct_block_sizes ( dev , & physical_block_size , & logical_block_size ) ) {
2018-11-03 00:19:47 +03:00
stack ;
2019-07-26 22:21:08 +03:00
return ; /* FIXME: error path ? */
}
if ( ( physical_block_size = = 512 ) & & ( logical_block_size = = 512 ) )
bs = 512 ;
else if ( ( physical_block_size = = 4096 ) & & ( logical_block_size = = 4096 ) )
bs = 4096 ;
else if ( ( physical_block_size = = 512 ) | | ( logical_block_size = = 512 ) ) {
log_debug ( " Set last byte mixed block sizes physical %u logical %u using 512 " ,
physical_block_size , logical_block_size ) ;
bs = 512 ;
} else if ( ( physical_block_size = = 4096 ) | | ( logical_block_size = = 4096 ) ) {
log_debug ( " Set last byte mixed block sizes physical %u logical %u using 4096 " ,
physical_block_size , logical_block_size ) ;
bs = 4096 ;
} else {
log_debug ( " Set last byte mixed block sizes physical %u logical %u using 512 " ,
physical_block_size , logical_block_size ) ;
bs = 512 ;
2018-11-03 00:19:47 +03:00
}
2018-10-30 00:53:17 +03:00
2024-07-08 23:32:35 +03:00
bcache_set_last_byte ( scan_bcache , dev - > bcache_di , offset , bs ) ;
2018-10-30 00:53:17 +03:00
}
void dev_unset_last_byte ( struct device * dev )
{
2024-07-08 23:32:35 +03:00
bcache_unset_last_byte ( scan_bcache , dev - > bcache_di ) ;
2018-10-30 00:53:17 +03:00
}