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 devices file /etc/lvm/devices/system.devices is a list of
devices that lvm can use. This is the default system devices
file, which is specified in lvm.conf devices/devicesfile.
The command option --devicesfile <filename> allows lvm to be
used with a different set of devices. This allows different
applications to use lvm on different sets of devices, e.g.
system devices do not need to be exposed to an application
using lvm on its own devices, and application devices do not
need to be exposed to the system.
In most cases (with limited exceptions), lvm will not read or
use a device not listed in the devices file. When the devices
file is used, the regex filter is not used, and the filter
settings in lvm.conf are ignored. filter-deviceid is used
when the devices file is enabled, and rejects any device that
does not match an entry in the devices file.
Set use_devicesfile=0 in lvm.conf or set --devicesfile ""
on the command line to disable the use of a devices file.
When disabled, lvm will see and use any device on the system
that passes the regex filter (and other standard filters.)
A device ID, e.g. wwid or serial number from sysfs, is a
unique ID that identifies a device. The device ID is
generally independent of the device content, and lvm can
get the device ID without reading the device.
The device ID is used in the devices file as the primary
method of identifying device entries, and is also included
in VG metadata for PVs.
Each device_id has a device_id_type which indicates where
the device_id comes from, e.g. "sys_wwid" means the device_id
comes from the sysfs wwid file. Others are sys_serial,
mpath_uuid, loop_file, md_uuid, devname. (devname is the
device path, which is a fallback when no other proper
device_id_type is available.)
filter-deviceid permits lvm to use only devices on the system
that have a device_id matching a devices file entry. Using
the device_id, lvm can determine the set of devices to use
without reading any devices, so the devices file will constrain
lvm in two ways:
1. it limits the devices that lvm will read.
2. it limits the devices that lvm will use.
In some uncommon cases, e.g. when devices have no unique ID
and device_id has to fall back to using the devname, lvm may
need to read all devices on the system to determine which
ones correspond to the devices file entries. In this case,
the devices file does not limit the devices that lvm reads,
but it does limit the devices that lvm uses.
pvcreate/vgcreate/vgextend are not constrained by the devices
file, and will look outside it to find the new PV. They assign
the new PV a device_id and add it to the devices file. It is
also possible to explicitly add new PVs to the devices file before
using them in pvcreate/etc, in which case these commands would not
need to look outside the devices file for the new device.
vgimportdevices VG looks at all devices on the system to find an
existing VG and add its devices to the devices file. The command
is not limited by an existing devices file. The command will also
add device_ids to the VG metadata if the VG does not yet include
device_ids. vgimportdevices -a imports devices for all accessible
VGs. Since vgimportdevices does not limit itself to devices in
an existing devices file, the lvm.conf regex filter applies.
Adding --foreign will import devices for foreign VGs, but device_ids
are not added to foreign VGs. Incomplete VGs are not imported.
The lvmdevices command manages the devices file. The primary
purpose is to edit the devices file, but it will read PV headers
to find/check PVIDs. (It does not read, process or modify VG
metadata.)
lvmdevices
. Displays devices file entries.
lvmdevices --check
. Checks devices file entries.
lvmdevices --update
. Updates devices file entries.
lvmdevices --adddev <devname>
. Adds devices_file entry (reads pv header).
lvmdevices --deldev <devname>
. Removes devices file entry.
lvmdevices --addpvid <pvid>
. Reads pv header of all devices to find <pvid>,
and if found adds devices file entry.
lvmdevices --delpvid <pvid>
. Removes devices file entry.
The vgimportclone command has a new option --importdevices
that does the equivalent of vgimportdevices with the cloned
devices that are being imported. The devices are "uncloned"
(new vgname and pvids) while at the same time adding the
devices to the devices file. This allows cloned PVs to be
imported without duplicate PVs ever appearing on the system.
The command option --devices <devnames> allows a specific
list of devices to be exposed to the lvm command, overriding
the devices file.
2020-06-23 21:25:41 +03:00
# include "lib/device/device_id.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 ;
2020-08-28 20:15:01 +03:00
char name [ ] ;
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
2002-01-10 18:01:58 +03:00
len = sizeof ( * li ) + strlen ( name ) + 1 ;
2001-12-11 14:42:30 +03:00
2018-06-08 15:40:53 +03:00
if ( ! ( li = malloc ( len ) ) ) {
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 ;
strcpy ( li - > name , name ) ;
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 ) ) ) ) {
2002-11-18 17:04:08 +03:00
log_error ( " label allocaction failed " ) ;
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
/* global variable for accessing the bcache populated by label scan */
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 ,
char * scan_buf ,
2018-05-11 00:27:34 +03:00
uint32_t scan_buf_sectors ,
2018-02-07 00:18:11 +03:00
char * label_buf ,
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 ;
/*
* Find which sector in scan_buf starts with a valid label ,
* and copy it into label_buf .
*/
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
/*
* The scan_buf passed in is a bcache block , which is
* BCACHE_BLOCK_SIZE_IN_SECTORS large . So if start_sector is
* one of the last couple sectors in that buffer , we need to
* break early .
*/
if ( sector > = scan_buf_sectors )
break ;
2018-02-07 00:18:11 +03:00
lh = ( struct label_header * ) ( scan_buf + ( sector < < SECTOR_SHIFT ) ) ;
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 ) ) {
2020-09-16 21:47:06 +03:00
log_very_verbose ( " %s: %s label detected at sector %llu " ,
2018-02-07 00:18:11 +03:00
dev_name ( dev ) , li - > name ,
2020-09-16 21:47:06 +03:00
( unsigned long long ) ( block_sector + sector ) ) ;
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 ;
memcpy ( label_buf , lh , LABEL_SIZE ) ;
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 ,
2018-05-11 00:27:34 +03:00
struct device * dev , struct block * bb ,
uint64_t block_sector , uint64_t start_sector ,
int * is_lvm_device )
2018-02-07 00:18:11 +03:00
{
char label_buf [ LABEL_SIZE ] __attribute__ ( ( aligned ( 8 ) ) ) ;
struct labeller * labeller ;
2018-12-01 00:26:26 +03:00
uint64_t 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 ] ) {
log_print ( " Clear pvid and info for filtered dev %s " , dev_name ( dev ) ) ;
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
/*
* Finds the data sector containing the label and copies into label_buf .
* label_buf : struct label_header + struct pv_header + struct pv_header_extension
*
* FIXME : we don ' t need to copy one sector from bb - > data into label_buf ,
* we can just point label_buf at one sector in ld - > buf .
*/
2018-05-11 00:27:34 +03:00
if ( ! ( labeller = _find_lvm_header ( dev , bb - > data , BCACHE_BLOCK_SIZE_IN_SECTORS , label_buf , & 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 ] ) {
log_print ( " Clear pvid and info for no lvm header %s " , dev_name ( dev ) ) ;
lvmcache_del_dev ( dev ) ;
memset ( dev - > pvid , 0 , sizeof ( dev - > pvid ) ) ;
}
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 ;
/*
* 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
*/
2020-01-28 19:33:15 +03:00
ret = labeller - > ops - > read ( cmd , labeller , dev , label_buf , 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 ;
int retried = 0 ;
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 ;
}
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 .
*/
if ( ! ( name_list = dm_list_first ( & dev - > aliases ) ) ) {
/* Shouldn't happen */
log_error ( " Device open %s %d:%d has no path names. " ,
dev_name ( dev ) , ( int ) MAJOR ( dev - > dev ) , ( int ) 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
2018-05-25 19:14:12 +03:00
retry_open :
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 ) ) ;
} else {
2018-05-25 19:14:12 +03:00
int major , minor ;
/*
* Shouldn ' t happen , if it does , print stat info to help figure
* out what ' s wrong .
*/
major = ( int ) MAJOR ( dev - > dev ) ;
minor = ( int ) MINOR ( dev - > dev ) ;
log_error ( " Device open %s %d:%d failed errno %d " , name , major , minor , errno ) ;
if ( stat ( name , & sbuf ) ) {
log_debug_devs ( " Device open %s %d:%d stat failed errno %d " ,
name , major , minor , errno ) ;
} else if ( sbuf . st_rdev ! = dev - > dev ) {
log_debug_devs ( " Device open %s %d:%d stat %d:%d does not match. " ,
name , major , minor ,
( int ) MAJOR ( sbuf . st_rdev ) , ( int ) MINOR ( sbuf . st_rdev ) ) ;
}
if ( ! retried ) {
/*
* FIXME : remove this , the theory for this retry is that
* there may be a udev race that we can sometimes mask by
* retrying . This is here until we can figure out if it ' s
* needed and if so fix the real problem .
*/
usleep ( 5000 ) ;
log_debug_devs ( " Device open %s retry " , dev_name ( dev ) ) ;
retried = 1 ;
goto retry_open ;
}
2018-02-13 17:58:35 +03:00
}
return 0 ;
}
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. " ) ;
close ( fd ) ;
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 ;
}
2018-05-30 01:02:27 +03:00
static void _drop_bad_aliases ( struct device * dev )
{
struct dm_str_list * strl , * strl2 ;
const char * name ;
struct stat sbuf ;
int major = ( int ) MAJOR ( dev - > dev ) ;
int minor = ( int ) MINOR ( dev - > dev ) ;
int bad ;
dm_list_iterate_items_safe ( strl , strl2 , & dev - > aliases ) {
name = strl - > str ;
bad = 0 ;
if ( stat ( name , & sbuf ) ) {
bad = 1 ;
log_debug_devs ( " Device path check %d:%d %s stat failed errno %d " ,
major , minor , name , errno ) ;
} else if ( sbuf . st_rdev ! = dev - > dev ) {
bad = 1 ;
log_debug_devs ( " Device path check %d:%d %s stat %d:%d does not match. " ,
major , minor , name ,
( int ) MAJOR ( sbuf . st_rdev ) , ( int ) MINOR ( sbuf . st_rdev ) ) ;
}
if ( bad ) {
log_debug_devs ( " Device path check %d:%d dropping path %s. " , major , minor , name ) ;
dev_cache_failed_path ( dev , name ) ;
}
}
}
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 .
*/
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
{
struct dm_list wait_devs ;
struct dm_list done_devs ;
2018-05-25 19:14:12 +03:00
struct dm_list reopen_devs ;
2018-02-07 00:18:11 +03:00
struct device_list * devl , * devl2 ;
struct block * bb ;
2018-05-25 19:14:12 +03:00
int retried_open = 0 ;
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-05-25 19:14:12 +03:00
dm_list_init ( & reopen_devs ) ;
2018-02-07 00:18:11 +03:00
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 :
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 ) {
/*
* 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 ) ) {
2018-03-01 21:40:37 +03:00
log_debug_devs ( " Scan failed to open %s. " , dev_name ( devl - > dev ) ) ;
2018-02-13 17:58:35 +03:00
dm_list_del ( & devl - > list ) ;
2018-05-25 19:14:12 +03:00
dm_list_add ( & reopen_devs , & devl - > list ) ;
2018-02-07 00:18:11 +03:00
continue ;
}
}
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_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
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_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 + + ;
2018-03-01 21:40:37 +03:00
lvmcache_del_dev ( devl - > dev ) ;
2018-02-07 00:18:11 +03:00
} else {
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_devs ( " Processing data from device %s %d:%d di %d block %p " ,
2018-05-30 01:02:27 +03:00
dev_name ( devl - > dev ) ,
( int ) MAJOR ( devl - > dev - > dev ) ,
( int ) MINOR ( devl - > dev - > 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
devl - > dev - > bcache_di , ( void * ) bb ) ;
2018-03-01 21:40:37 +03:00
2018-05-11 00:27:34 +03:00
ret = _process_block ( cmd , f , devl - > dev , bb , 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
}
if ( bb )
bcache_put ( bb ) ;
/*
* 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 ) {
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
_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
/*
* We ' re done scanning all the devs . If we failed to open any of them
* the first time through , refresh device paths and retry . We failed
* to open the devs on the reopen_devs list .
*
* FIXME : it ' s not clear if or why this helps .
*/
if ( ! dm_list_empty ( & reopen_devs ) ) {
if ( retried_open ) {
/* Don't try again. */
scan_failed_count + = dm_list_size ( & reopen_devs ) ;
dm_list_splice ( & done_devs , & reopen_devs ) ;
goto out ;
}
retried_open = 1 ;
2018-05-30 01:02:27 +03:00
dm_list_iterate_items_safe ( devl , devl2 , & reopen_devs ) {
_drop_bad_aliases ( devl - > dev ) ;
if ( dm_list_empty ( & devl - > dev - > aliases ) ) {
log_warn ( " WARNING: Scan ignoring device %d:%d with no paths. " ,
( int ) MAJOR ( devl - > dev - > dev ) ,
( int ) MINOR ( devl - > dev - > dev ) ) ;
dm_list_del ( & devl - > list ) ;
lvmcache_del_dev ( devl - > dev ) ;
scan_failed_count + + ;
}
}
2018-05-25 19:14:12 +03:00
/* Put devs that failed to open back on the original list to retry. */
dm_list_splice ( devs , & reopen_devs ) ;
goto scan_more ;
}
out :
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
2019-07-09 21:16:26 +03:00
static int _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 ;
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
}
if ( ! ( scan_bcache = bcache_create ( BCACHE_BLOCK_SIZE_IN_SECTORS , cache_blocks , ioe ) ) ) {
log_error ( " Failed to create bcache with %d cache blocks. " , cache_blocks ) ;
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 */
static void _prepare_open_file_limit ( struct cmd_context * cmd , unsigned int num_devs )
{
2019-08-26 18:19:16 +03:00
# ifdef HAVE_PRLIMIT
2019-08-08 21:11:05 +03:00
struct rlimit old , new ;
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 devices file /etc/lvm/devices/system.devices is a list of
devices that lvm can use. This is the default system devices
file, which is specified in lvm.conf devices/devicesfile.
The command option --devicesfile <filename> allows lvm to be
used with a different set of devices. This allows different
applications to use lvm on different sets of devices, e.g.
system devices do not need to be exposed to an application
using lvm on its own devices, and application devices do not
need to be exposed to the system.
In most cases (with limited exceptions), lvm will not read or
use a device not listed in the devices file. When the devices
file is used, the regex filter is not used, and the filter
settings in lvm.conf are ignored. filter-deviceid is used
when the devices file is enabled, and rejects any device that
does not match an entry in the devices file.
Set use_devicesfile=0 in lvm.conf or set --devicesfile ""
on the command line to disable the use of a devices file.
When disabled, lvm will see and use any device on the system
that passes the regex filter (and other standard filters.)
A device ID, e.g. wwid or serial number from sysfs, is a
unique ID that identifies a device. The device ID is
generally independent of the device content, and lvm can
get the device ID without reading the device.
The device ID is used in the devices file as the primary
method of identifying device entries, and is also included
in VG metadata for PVs.
Each device_id has a device_id_type which indicates where
the device_id comes from, e.g. "sys_wwid" means the device_id
comes from the sysfs wwid file. Others are sys_serial,
mpath_uuid, loop_file, md_uuid, devname. (devname is the
device path, which is a fallback when no other proper
device_id_type is available.)
filter-deviceid permits lvm to use only devices on the system
that have a device_id matching a devices file entry. Using
the device_id, lvm can determine the set of devices to use
without reading any devices, so the devices file will constrain
lvm in two ways:
1. it limits the devices that lvm will read.
2. it limits the devices that lvm will use.
In some uncommon cases, e.g. when devices have no unique ID
and device_id has to fall back to using the devname, lvm may
need to read all devices on the system to determine which
ones correspond to the devices file entries. In this case,
the devices file does not limit the devices that lvm reads,
but it does limit the devices that lvm uses.
pvcreate/vgcreate/vgextend are not constrained by the devices
file, and will look outside it to find the new PV. They assign
the new PV a device_id and add it to the devices file. It is
also possible to explicitly add new PVs to the devices file before
using them in pvcreate/etc, in which case these commands would not
need to look outside the devices file for the new device.
vgimportdevices VG looks at all devices on the system to find an
existing VG and add its devices to the devices file. The command
is not limited by an existing devices file. The command will also
add device_ids to the VG metadata if the VG does not yet include
device_ids. vgimportdevices -a imports devices for all accessible
VGs. Since vgimportdevices does not limit itself to devices in
an existing devices file, the lvm.conf regex filter applies.
Adding --foreign will import devices for foreign VGs, but device_ids
are not added to foreign VGs. Incomplete VGs are not imported.
The lvmdevices command manages the devices file. The primary
purpose is to edit the devices file, but it will read PV headers
to find/check PVIDs. (It does not read, process or modify VG
metadata.)
lvmdevices
. Displays devices file entries.
lvmdevices --check
. Checks devices file entries.
lvmdevices --update
. Updates devices file entries.
lvmdevices --adddev <devname>
. Adds devices_file entry (reads pv header).
lvmdevices --deldev <devname>
. Removes devices file entry.
lvmdevices --addpvid <pvid>
. Reads pv header of all devices to find <pvid>,
and if found adds devices file entry.
lvmdevices --delpvid <pvid>
. Removes devices file entry.
The vgimportclone command has a new option --importdevices
that does the equivalent of vgimportdevices with the cloned
devices that are being imported. The devices are "uncloned"
(new vgname and pvids) while at the same time adding the
devices to the devices file. This allows cloned PVs to be
imported without duplicate PVs ever appearing on the system.
The command option --devices <devnames> allows a specific
list of devices to be exposed to the lvm command, overriding
the devices file.
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_list * devl , * devl2 ;
struct device * dev ;
struct pv_header * pvh ;
int ret = 0 ;
dm_list_init ( & devs ) ;
device usage based on devices file
The devices file /etc/lvm/devices/system.devices is a list of
devices that lvm can use. This is the default system devices
file, which is specified in lvm.conf devices/devicesfile.
The command option --devicesfile <filename> allows lvm to be
used with a different set of devices. This allows different
applications to use lvm on different sets of devices, e.g.
system devices do not need to be exposed to an application
using lvm on its own devices, and application devices do not
need to be exposed to the system.
In most cases (with limited exceptions), lvm will not read or
use a device not listed in the devices file. When the devices
file is used, the regex filter is not used, and the filter
settings in lvm.conf are ignored. filter-deviceid is used
when the devices file is enabled, and rejects any device that
does not match an entry in the devices file.
Set use_devicesfile=0 in lvm.conf or set --devicesfile ""
on the command line to disable the use of a devices file.
When disabled, lvm will see and use any device on the system
that passes the regex filter (and other standard filters.)
A device ID, e.g. wwid or serial number from sysfs, is a
unique ID that identifies a device. The device ID is
generally independent of the device content, and lvm can
get the device ID without reading the device.
The device ID is used in the devices file as the primary
method of identifying device entries, and is also included
in VG metadata for PVs.
Each device_id has a device_id_type which indicates where
the device_id comes from, e.g. "sys_wwid" means the device_id
comes from the sysfs wwid file. Others are sys_serial,
mpath_uuid, loop_file, md_uuid, devname. (devname is the
device path, which is a fallback when no other proper
device_id_type is available.)
filter-deviceid permits lvm to use only devices on the system
that have a device_id matching a devices file entry. Using
the device_id, lvm can determine the set of devices to use
without reading any devices, so the devices file will constrain
lvm in two ways:
1. it limits the devices that lvm will read.
2. it limits the devices that lvm will use.
In some uncommon cases, e.g. when devices have no unique ID
and device_id has to fall back to using the devname, lvm may
need to read all devices on the system to determine which
ones correspond to the devices file entries. In this case,
the devices file does not limit the devices that lvm reads,
but it does limit the devices that lvm uses.
pvcreate/vgcreate/vgextend are not constrained by the devices
file, and will look outside it to find the new PV. They assign
the new PV a device_id and add it to the devices file. It is
also possible to explicitly add new PVs to the devices file before
using them in pvcreate/etc, in which case these commands would not
need to look outside the devices file for the new device.
vgimportdevices VG looks at all devices on the system to find an
existing VG and add its devices to the devices file. The command
is not limited by an existing devices file. The command will also
add device_ids to the VG metadata if the VG does not yet include
device_ids. vgimportdevices -a imports devices for all accessible
VGs. Since vgimportdevices does not limit itself to devices in
an existing devices file, the lvm.conf regex filter applies.
Adding --foreign will import devices for foreign VGs, but device_ids
are not added to foreign VGs. Incomplete VGs are not imported.
The lvmdevices command manages the devices file. The primary
purpose is to edit the devices file, but it will read PV headers
to find/check PVIDs. (It does not read, process or modify VG
metadata.)
lvmdevices
. Displays devices file entries.
lvmdevices --check
. Checks devices file entries.
lvmdevices --update
. Updates devices file entries.
lvmdevices --adddev <devname>
. Adds devices_file entry (reads pv header).
lvmdevices --deldev <devname>
. Removes devices file entry.
lvmdevices --addpvid <pvid>
. Reads pv header of all devices to find <pvid>,
and if found adds devices file entry.
lvmdevices --delpvid <pvid>
. Removes devices file entry.
The vgimportclone command has a new option --importdevices
that does the equivalent of vgimportdevices with the cloned
devices that are being imported. The devices are "uncloned"
(new vgname and pvids) while at the same time adding the
devices to the devices file. This allows cloned PVs to be
imported without duplicate PVs ever appearing on the system.
The command option --devices <devnames> allows a specific
list of devices to be exposed to the lvm command, overriding
the devices file.
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 ;
}
/*
* 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
if ( ! ( iter = dev_iter_create ( cmd - > filter , 0 ) ) ) {
log_error ( " Scanning failed to get devices. " ) ;
return 0 ;
}
log_debug_devs ( " Filtering devices to scan " ) ;
while ( ( dev = dev_iter_get ( cmd , iter ) ) ) {
if ( ! ( devl = zalloc ( sizeof ( * devl ) ) ) )
continue ;
devl - > dev = dev ;
dm_list_add ( & devs , & devl - > list ) ;
} ;
dev_iter_destroy ( iter ) ;
if ( ! scan_bcache ) {
if ( ! _setup_bcache ( ) )
goto_out ;
}
log_debug_devs ( " Reading labels for pvid " ) ;
dm_list_iterate_items ( devl , & devs ) {
dev = devl - > dev ;
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 ) ) {
* dev_out = devl - > dev ;
_scan_dev_close ( dev ) ;
break ;
}
_scan_dev_close ( dev ) ;
}
ret = 1 ;
out :
dm_list_iterate_items_safe ( devl , devl2 , & devs ) {
dm_list_del ( & devl - > list ) ;
free ( devl ) ;
}
return ret ;
}
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 ;
device usage based on devices file
The devices file /etc/lvm/devices/system.devices is a list of
devices that lvm can use. This is the default system devices
file, which is specified in lvm.conf devices/devicesfile.
The command option --devicesfile <filename> allows lvm to be
used with a different set of devices. This allows different
applications to use lvm on different sets of devices, e.g.
system devices do not need to be exposed to an application
using lvm on its own devices, and application devices do not
need to be exposed to the system.
In most cases (with limited exceptions), lvm will not read or
use a device not listed in the devices file. When the devices
file is used, the regex filter is not used, and the filter
settings in lvm.conf are ignored. filter-deviceid is used
when the devices file is enabled, and rejects any device that
does not match an entry in the devices file.
Set use_devicesfile=0 in lvm.conf or set --devicesfile ""
on the command line to disable the use of a devices file.
When disabled, lvm will see and use any device on the system
that passes the regex filter (and other standard filters.)
A device ID, e.g. wwid or serial number from sysfs, is a
unique ID that identifies a device. The device ID is
generally independent of the device content, and lvm can
get the device ID without reading the device.
The device ID is used in the devices file as the primary
method of identifying device entries, and is also included
in VG metadata for PVs.
Each device_id has a device_id_type which indicates where
the device_id comes from, e.g. "sys_wwid" means the device_id
comes from the sysfs wwid file. Others are sys_serial,
mpath_uuid, loop_file, md_uuid, devname. (devname is the
device path, which is a fallback when no other proper
device_id_type is available.)
filter-deviceid permits lvm to use only devices on the system
that have a device_id matching a devices file entry. Using
the device_id, lvm can determine the set of devices to use
without reading any devices, so the devices file will constrain
lvm in two ways:
1. it limits the devices that lvm will read.
2. it limits the devices that lvm will use.
In some uncommon cases, e.g. when devices have no unique ID
and device_id has to fall back to using the devname, lvm may
need to read all devices on the system to determine which
ones correspond to the devices file entries. In this case,
the devices file does not limit the devices that lvm reads,
but it does limit the devices that lvm uses.
pvcreate/vgcreate/vgextend are not constrained by the devices
file, and will look outside it to find the new PV. They assign
the new PV a device_id and add it to the devices file. It is
also possible to explicitly add new PVs to the devices file before
using them in pvcreate/etc, in which case these commands would not
need to look outside the devices file for the new device.
vgimportdevices VG looks at all devices on the system to find an
existing VG and add its devices to the devices file. The command
is not limited by an existing devices file. The command will also
add device_ids to the VG metadata if the VG does not yet include
device_ids. vgimportdevices -a imports devices for all accessible
VGs. Since vgimportdevices does not limit itself to devices in
an existing devices file, the lvm.conf regex filter applies.
Adding --foreign will import devices for foreign VGs, but device_ids
are not added to foreign VGs. Incomplete VGs are not imported.
The lvmdevices command manages the devices file. The primary
purpose is to edit the devices file, but it will read PV headers
to find/check PVIDs. (It does not read, process or modify VG
metadata.)
lvmdevices
. Displays devices file entries.
lvmdevices --check
. Checks devices file entries.
lvmdevices --update
. Updates devices file entries.
lvmdevices --adddev <devname>
. Adds devices_file entry (reads pv header).
lvmdevices --deldev <devname>
. Removes devices file entry.
lvmdevices --addpvid <pvid>
. Reads pv header of all devices to find <pvid>,
and if found adds devices file entry.
lvmdevices --delpvid <pvid>
. Removes devices file entry.
The vgimportclone command has a new option --importdevices
that does the equivalent of vgimportdevices with the cloned
devices that are being imported. The devices are "uncloned"
(new vgname and pvids) while at the same time adding the
devices to the devices file. This allows cloned PVs to be
imported without duplicate PVs ever appearing on the system.
The command option --devices <devnames> allows a specific
list of devices to be exposed to the lvm command, overriding
the devices file.
2020-06-23 21:25:41 +03:00
int device_ids_invalid = 0 ;
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
2020-10-20 23:10:08 +03:00
if ( ! scan_bcache ) {
if ( ! _setup_bcache ( ) )
return_0 ;
}
2018-02-07 00:18:11 +03:00
/*
device usage based on devices file
The devices file /etc/lvm/devices/system.devices is a list of
devices that lvm can use. This is the default system devices
file, which is specified in lvm.conf devices/devicesfile.
The command option --devicesfile <filename> allows lvm to be
used with a different set of devices. This allows different
applications to use lvm on different sets of devices, e.g.
system devices do not need to be exposed to an application
using lvm on its own devices, and application devices do not
need to be exposed to the system.
In most cases (with limited exceptions), lvm will not read or
use a device not listed in the devices file. When the devices
file is used, the regex filter is not used, and the filter
settings in lvm.conf are ignored. filter-deviceid is used
when the devices file is enabled, and rejects any device that
does not match an entry in the devices file.
Set use_devicesfile=0 in lvm.conf or set --devicesfile ""
on the command line to disable the use of a devices file.
When disabled, lvm will see and use any device on the system
that passes the regex filter (and other standard filters.)
A device ID, e.g. wwid or serial number from sysfs, is a
unique ID that identifies a device. The device ID is
generally independent of the device content, and lvm can
get the device ID without reading the device.
The device ID is used in the devices file as the primary
method of identifying device entries, and is also included
in VG metadata for PVs.
Each device_id has a device_id_type which indicates where
the device_id comes from, e.g. "sys_wwid" means the device_id
comes from the sysfs wwid file. Others are sys_serial,
mpath_uuid, loop_file, md_uuid, devname. (devname is the
device path, which is a fallback when no other proper
device_id_type is available.)
filter-deviceid permits lvm to use only devices on the system
that have a device_id matching a devices file entry. Using
the device_id, lvm can determine the set of devices to use
without reading any devices, so the devices file will constrain
lvm in two ways:
1. it limits the devices that lvm will read.
2. it limits the devices that lvm will use.
In some uncommon cases, e.g. when devices have no unique ID
and device_id has to fall back to using the devname, lvm may
need to read all devices on the system to determine which
ones correspond to the devices file entries. In this case,
the devices file does not limit the devices that lvm reads,
but it does limit the devices that lvm uses.
pvcreate/vgcreate/vgextend are not constrained by the devices
file, and will look outside it to find the new PV. They assign
the new PV a device_id and add it to the devices file. It is
also possible to explicitly add new PVs to the devices file before
using them in pvcreate/etc, in which case these commands would not
need to look outside the devices file for the new device.
vgimportdevices VG looks at all devices on the system to find an
existing VG and add its devices to the devices file. The command
is not limited by an existing devices file. The command will also
add device_ids to the VG metadata if the VG does not yet include
device_ids. vgimportdevices -a imports devices for all accessible
VGs. Since vgimportdevices does not limit itself to devices in
an existing devices file, the lvm.conf regex filter applies.
Adding --foreign will import devices for foreign VGs, but device_ids
are not added to foreign VGs. Incomplete VGs are not imported.
The lvmdevices command manages the devices file. The primary
purpose is to edit the devices file, but it will read PV headers
to find/check PVIDs. (It does not read, process or modify VG
metadata.)
lvmdevices
. Displays devices file entries.
lvmdevices --check
. Checks devices file entries.
lvmdevices --update
. Updates devices file entries.
lvmdevices --adddev <devname>
. Adds devices_file entry (reads pv header).
lvmdevices --deldev <devname>
. Removes devices file entry.
lvmdevices --addpvid <pvid>
. Reads pv header of all devices to find <pvid>,
and if found adds devices file entry.
lvmdevices --delpvid <pvid>
. Removes devices file entry.
The vgimportclone command has a new option --importdevices
that does the equivalent of vgimportdevices with the cloned
devices that are being imported. The devices are "uncloned"
(new vgname and pvids) while at the same time adding the
devices to the devices file. This allows cloned PVs to be
imported without duplicate PVs ever appearing on the system.
The command option --devices <devnames> allows a specific
list of devices to be exposed to the lvm command, overriding
the devices file.
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 devices file /etc/lvm/devices/system.devices is a list of
devices that lvm can use. This is the default system devices
file, which is specified in lvm.conf devices/devicesfile.
The command option --devicesfile <filename> allows lvm to be
used with a different set of devices. This allows different
applications to use lvm on different sets of devices, e.g.
system devices do not need to be exposed to an application
using lvm on its own devices, and application devices do not
need to be exposed to the system.
In most cases (with limited exceptions), lvm will not read or
use a device not listed in the devices file. When the devices
file is used, the regex filter is not used, and the filter
settings in lvm.conf are ignored. filter-deviceid is used
when the devices file is enabled, and rejects any device that
does not match an entry in the devices file.
Set use_devicesfile=0 in lvm.conf or set --devicesfile ""
on the command line to disable the use of a devices file.
When disabled, lvm will see and use any device on the system
that passes the regex filter (and other standard filters.)
A device ID, e.g. wwid or serial number from sysfs, is a
unique ID that identifies a device. The device ID is
generally independent of the device content, and lvm can
get the device ID without reading the device.
The device ID is used in the devices file as the primary
method of identifying device entries, and is also included
in VG metadata for PVs.
Each device_id has a device_id_type which indicates where
the device_id comes from, e.g. "sys_wwid" means the device_id
comes from the sysfs wwid file. Others are sys_serial,
mpath_uuid, loop_file, md_uuid, devname. (devname is the
device path, which is a fallback when no other proper
device_id_type is available.)
filter-deviceid permits lvm to use only devices on the system
that have a device_id matching a devices file entry. Using
the device_id, lvm can determine the set of devices to use
without reading any devices, so the devices file will constrain
lvm in two ways:
1. it limits the devices that lvm will read.
2. it limits the devices that lvm will use.
In some uncommon cases, e.g. when devices have no unique ID
and device_id has to fall back to using the devname, lvm may
need to read all devices on the system to determine which
ones correspond to the devices file entries. In this case,
the devices file does not limit the devices that lvm reads,
but it does limit the devices that lvm uses.
pvcreate/vgcreate/vgextend are not constrained by the devices
file, and will look outside it to find the new PV. They assign
the new PV a device_id and add it to the devices file. It is
also possible to explicitly add new PVs to the devices file before
using them in pvcreate/etc, in which case these commands would not
need to look outside the devices file for the new device.
vgimportdevices VG looks at all devices on the system to find an
existing VG and add its devices to the devices file. The command
is not limited by an existing devices file. The command will also
add device_ids to the VG metadata if the VG does not yet include
device_ids. vgimportdevices -a imports devices for all accessible
VGs. Since vgimportdevices does not limit itself to devices in
an existing devices file, the lvm.conf regex filter applies.
Adding --foreign will import devices for foreign VGs, but device_ids
are not added to foreign VGs. Incomplete VGs are not imported.
The lvmdevices command manages the devices file. The primary
purpose is to edit the devices file, but it will read PV headers
to find/check PVIDs. (It does not read, process or modify VG
metadata.)
lvmdevices
. Displays devices file entries.
lvmdevices --check
. Checks devices file entries.
lvmdevices --update
. Updates devices file entries.
lvmdevices --adddev <devname>
. Adds devices_file entry (reads pv header).
lvmdevices --deldev <devname>
. Removes devices file entry.
lvmdevices --addpvid <pvid>
. Reads pv header of all devices to find <pvid>,
and if found adds devices file entry.
lvmdevices --delpvid <pvid>
. Removes devices file entry.
The vgimportclone command has a new option --importdevices
that does the equivalent of vgimportdevices with the cloned
devices that are being imported. The devices are "uncloned"
(new vgname and pvids) while at the same time adding the
devices to the devices file. This allows cloned PVs to be
imported without duplicate PVs ever appearing on the system.
The command option --devices <devnames> allows a specific
list of devices to be exposed to the lvm command, overriding
the devices file.
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 " ) & &
dev_cache_has_md_with_end_superblock ( cmd - > dev_types ) ) {
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
*/
2020-10-20 23:10:08 +03:00
if ( ! ( iter = dev_iter_create ( NULL , 0 ) ) ) {
2018-02-07 00:18:11 +03:00
log_error ( " Scanning failed to get devices. " ) ;
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 ) ;
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 .
*/
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 ] ) {
log_print ( " Clear pvid and info for filtered dev %s " , dev_name ( dev ) ) ;
lvmcache_del_dev ( dev ) ;
memset ( dev - > pvid , 0 , sizeof ( dev - > pvid ) ) ;
}
}
2018-02-07 00:18:11 +03:00
}
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 .
*/
_prepare_open_file_limit ( cmd , dm_list_size ( & scan_devs ) ) ;
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
* address the problem of writing new metadata that excedes the bcache
* size and failing , which would often be hit first , i . e . we ' ll fail
* to write new metadata exceding the max size before we have a chance
* 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
}
2018-12-07 23:35:22 +03:00
dm_list_init ( & cmd - > hints ) ;
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 devices file /etc/lvm/devices/system.devices is a list of
devices that lvm can use. This is the default system devices
file, which is specified in lvm.conf devices/devicesfile.
The command option --devicesfile <filename> allows lvm to be
used with a different set of devices. This allows different
applications to use lvm on different sets of devices, e.g.
system devices do not need to be exposed to an application
using lvm on its own devices, and application devices do not
need to be exposed to the system.
In most cases (with limited exceptions), lvm will not read or
use a device not listed in the devices file. When the devices
file is used, the regex filter is not used, and the filter
settings in lvm.conf are ignored. filter-deviceid is used
when the devices file is enabled, and rejects any device that
does not match an entry in the devices file.
Set use_devicesfile=0 in lvm.conf or set --devicesfile ""
on the command line to disable the use of a devices file.
When disabled, lvm will see and use any device on the system
that passes the regex filter (and other standard filters.)
A device ID, e.g. wwid or serial number from sysfs, is a
unique ID that identifies a device. The device ID is
generally independent of the device content, and lvm can
get the device ID without reading the device.
The device ID is used in the devices file as the primary
method of identifying device entries, and is also included
in VG metadata for PVs.
Each device_id has a device_id_type which indicates where
the device_id comes from, e.g. "sys_wwid" means the device_id
comes from the sysfs wwid file. Others are sys_serial,
mpath_uuid, loop_file, md_uuid, devname. (devname is the
device path, which is a fallback when no other proper
device_id_type is available.)
filter-deviceid permits lvm to use only devices on the system
that have a device_id matching a devices file entry. Using
the device_id, lvm can determine the set of devices to use
without reading any devices, so the devices file will constrain
lvm in two ways:
1. it limits the devices that lvm will read.
2. it limits the devices that lvm will use.
In some uncommon cases, e.g. when devices have no unique ID
and device_id has to fall back to using the devname, lvm may
need to read all devices on the system to determine which
ones correspond to the devices file entries. In this case,
the devices file does not limit the devices that lvm reads,
but it does limit the devices that lvm uses.
pvcreate/vgcreate/vgextend are not constrained by the devices
file, and will look outside it to find the new PV. They assign
the new PV a device_id and add it to the devices file. It is
also possible to explicitly add new PVs to the devices file before
using them in pvcreate/etc, in which case these commands would not
need to look outside the devices file for the new device.
vgimportdevices VG looks at all devices on the system to find an
existing VG and add its devices to the devices file. The command
is not limited by an existing devices file. The command will also
add device_ids to the VG metadata if the VG does not yet include
device_ids. vgimportdevices -a imports devices for all accessible
VGs. Since vgimportdevices does not limit itself to devices in
an existing devices file, the lvm.conf regex filter applies.
Adding --foreign will import devices for foreign VGs, but device_ids
are not added to foreign VGs. Incomplete VGs are not imported.
The lvmdevices command manages the devices file. The primary
purpose is to edit the devices file, but it will read PV headers
to find/check PVIDs. (It does not read, process or modify VG
metadata.)
lvmdevices
. Displays devices file entries.
lvmdevices --check
. Checks devices file entries.
lvmdevices --update
. Updates devices file entries.
lvmdevices --adddev <devname>
. Adds devices_file entry (reads pv header).
lvmdevices --deldev <devname>
. Removes devices file entry.
lvmdevices --addpvid <pvid>
. Reads pv header of all devices to find <pvid>,
and if found adds devices file entry.
lvmdevices --delpvid <pvid>
. Removes devices file entry.
The vgimportclone command has a new option --importdevices
that does the equivalent of vgimportdevices with the cloned
devices that are being imported. The devices are "uncloned"
(new vgname and pvids) while at the same time adding the
devices to the devices file. This allows cloned PVs to be
imported without duplicate PVs ever appearing on the system.
The command option --devices <devnames> allows a specific
list of devices to be exposed to the lvm command, overriding
the devices file.
2020-06-23 21:25:41 +03:00
/* scan_devs are the devs that have been scanned */
dm_list_splice ( & scan_devs , & all_devs ) ;
2020-03-03 21:25:34 +03:00
free_hints ( & hints_list ) ;
2019-05-21 20:02:40 +03:00
using_hints = 0 ;
create_hints = 0 ;
device usage based on devices file
The devices file /etc/lvm/devices/system.devices is a list of
devices that lvm can use. This is the default system devices
file, which is specified in lvm.conf devices/devicesfile.
The command option --devicesfile <filename> allows lvm to be
used with a different set of devices. This allows different
applications to use lvm on different sets of devices, e.g.
system devices do not need to be exposed to an application
using lvm on its own devices, and application devices do not
need to be exposed to the system.
In most cases (with limited exceptions), lvm will not read or
use a device not listed in the devices file. When the devices
file is used, the regex filter is not used, and the filter
settings in lvm.conf are ignored. filter-deviceid is used
when the devices file is enabled, and rejects any device that
does not match an entry in the devices file.
Set use_devicesfile=0 in lvm.conf or set --devicesfile ""
on the command line to disable the use of a devices file.
When disabled, lvm will see and use any device on the system
that passes the regex filter (and other standard filters.)
A device ID, e.g. wwid or serial number from sysfs, is a
unique ID that identifies a device. The device ID is
generally independent of the device content, and lvm can
get the device ID without reading the device.
The device ID is used in the devices file as the primary
method of identifying device entries, and is also included
in VG metadata for PVs.
Each device_id has a device_id_type which indicates where
the device_id comes from, e.g. "sys_wwid" means the device_id
comes from the sysfs wwid file. Others are sys_serial,
mpath_uuid, loop_file, md_uuid, devname. (devname is the
device path, which is a fallback when no other proper
device_id_type is available.)
filter-deviceid permits lvm to use only devices on the system
that have a device_id matching a devices file entry. Using
the device_id, lvm can determine the set of devices to use
without reading any devices, so the devices file will constrain
lvm in two ways:
1. it limits the devices that lvm will read.
2. it limits the devices that lvm will use.
In some uncommon cases, e.g. when devices have no unique ID
and device_id has to fall back to using the devname, lvm may
need to read all devices on the system to determine which
ones correspond to the devices file entries. In this case,
the devices file does not limit the devices that lvm reads,
but it does limit the devices that lvm uses.
pvcreate/vgcreate/vgextend are not constrained by the devices
file, and will look outside it to find the new PV. They assign
the new PV a device_id and add it to the devices file. It is
also possible to explicitly add new PVs to the devices file before
using them in pvcreate/etc, in which case these commands would not
need to look outside the devices file for the new device.
vgimportdevices VG looks at all devices on the system to find an
existing VG and add its devices to the devices file. The command
is not limited by an existing devices file. The command will also
add device_ids to the VG metadata if the VG does not yet include
device_ids. vgimportdevices -a imports devices for all accessible
VGs. Since vgimportdevices does not limit itself to devices in
an existing devices file, the lvm.conf regex filter applies.
Adding --foreign will import devices for foreign VGs, but device_ids
are not added to foreign VGs. Incomplete VGs are not imported.
The lvmdevices command manages the devices file. The primary
purpose is to edit the devices file, but it will read PV headers
to find/check PVIDs. (It does not read, process or modify VG
metadata.)
lvmdevices
. Displays devices file entries.
lvmdevices --check
. Checks devices file entries.
lvmdevices --update
. Updates devices file entries.
lvmdevices --adddev <devname>
. Adds devices_file entry (reads pv header).
lvmdevices --deldev <devname>
. Removes devices file entry.
lvmdevices --addpvid <pvid>
. Reads pv header of all devices to find <pvid>,
and if found adds devices file entry.
lvmdevices --delpvid <pvid>
. Removes devices file entry.
The vgimportclone command has a new option --importdevices
that does the equivalent of vgimportdevices with the cloned
devices that are being imported. The devices are "uncloned"
(new vgname and pvids) while at the same time adding the
devices to the devices file. This allows cloned PVs to be
imported without duplicate PVs ever appearing on the system.
The command option --devices <devnames> allows a specific
list of devices to be exposed to the lvm command, overriding
the devices file.
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
} else {
/* The hints may be used by another device iteration. */
2019-04-11 19:49:18 +03:00
dm_list_splice ( & cmd - > hints , & hints_list ) ;
2018-12-07 23:35:22 +03:00
}
}
2018-02-13 17:58:35 +03:00
device usage based on devices file
The devices file /etc/lvm/devices/system.devices is a list of
devices that lvm can use. This is the default system devices
file, which is specified in lvm.conf devices/devicesfile.
The command option --devicesfile <filename> allows lvm to be
used with a different set of devices. This allows different
applications to use lvm on different sets of devices, e.g.
system devices do not need to be exposed to an application
using lvm on its own devices, and application devices do not
need to be exposed to the system.
In most cases (with limited exceptions), lvm will not read or
use a device not listed in the devices file. When the devices
file is used, the regex filter is not used, and the filter
settings in lvm.conf are ignored. filter-deviceid is used
when the devices file is enabled, and rejects any device that
does not match an entry in the devices file.
Set use_devicesfile=0 in lvm.conf or set --devicesfile ""
on the command line to disable the use of a devices file.
When disabled, lvm will see and use any device on the system
that passes the regex filter (and other standard filters.)
A device ID, e.g. wwid or serial number from sysfs, is a
unique ID that identifies a device. The device ID is
generally independent of the device content, and lvm can
get the device ID without reading the device.
The device ID is used in the devices file as the primary
method of identifying device entries, and is also included
in VG metadata for PVs.
Each device_id has a device_id_type which indicates where
the device_id comes from, e.g. "sys_wwid" means the device_id
comes from the sysfs wwid file. Others are sys_serial,
mpath_uuid, loop_file, md_uuid, devname. (devname is the
device path, which is a fallback when no other proper
device_id_type is available.)
filter-deviceid permits lvm to use only devices on the system
that have a device_id matching a devices file entry. Using
the device_id, lvm can determine the set of devices to use
without reading any devices, so the devices file will constrain
lvm in two ways:
1. it limits the devices that lvm will read.
2. it limits the devices that lvm will use.
In some uncommon cases, e.g. when devices have no unique ID
and device_id has to fall back to using the devname, lvm may
need to read all devices on the system to determine which
ones correspond to the devices file entries. In this case,
the devices file does not limit the devices that lvm reads,
but it does limit the devices that lvm uses.
pvcreate/vgcreate/vgextend are not constrained by the devices
file, and will look outside it to find the new PV. They assign
the new PV a device_id and add it to the devices file. It is
also possible to explicitly add new PVs to the devices file before
using them in pvcreate/etc, in which case these commands would not
need to look outside the devices file for the new device.
vgimportdevices VG looks at all devices on the system to find an
existing VG and add its devices to the devices file. The command
is not limited by an existing devices file. The command will also
add device_ids to the VG metadata if the VG does not yet include
device_ids. vgimportdevices -a imports devices for all accessible
VGs. Since vgimportdevices does not limit itself to devices in
an existing devices file, the lvm.conf regex filter applies.
Adding --foreign will import devices for foreign VGs, but device_ids
are not added to foreign VGs. Incomplete VGs are not imported.
The lvmdevices command manages the devices file. The primary
purpose is to edit the devices file, but it will read PV headers
to find/check PVIDs. (It does not read, process or modify VG
metadata.)
lvmdevices
. Displays devices file entries.
lvmdevices --check
. Checks devices file entries.
lvmdevices --update
. Updates devices file entries.
lvmdevices --adddev <devname>
. Adds devices_file entry (reads pv header).
lvmdevices --deldev <devname>
. Removes devices file entry.
lvmdevices --addpvid <pvid>
. Reads pv header of all devices to find <pvid>,
and if found adds devices file entry.
lvmdevices --delpvid <pvid>
. Removes devices file entry.
The vgimportclone command has a new option --importdevices
that does the equivalent of vgimportdevices with the cloned
devices that are being imported. The devices are "uncloned"
(new vgname and pvids) while at the same time adding the
devices to the devices file. This allows cloned PVs to be
imported without duplicate PVs ever appearing on the system.
The command option --devices <devnames> allows a specific
list of devices to be exposed to the lvm command, overriding
the devices file.
2020-06-23 21:25:41 +03:00
/*
* Check if the devices_file content is up to date and
* if not update it .
*/
device_ids_validate ( cmd , & scan_devs , & device_ids_invalid , 0 ) ;
2019-05-21 20:06:34 +03:00
/*
* Stronger exclusion of md components that might have been
* misidentified as PVs due to having an end - of - device md superblock .
* If we ' re not using hints , and are not already doing a full md check
* on devs being scanned , then if udev info is missing for a PV , scan
* the end of the PV to verify it ' s not an md component . The full
* dev_is_md_component call will do new reads at the end of the dev .
*/
if ( cmd - > md_component_detection & & ! cmd - > use_full_md_check & & ! using_hints & &
! strcmp ( cmd - > md_component_checks , " auto " ) ) {
int once = 0 ;
dm_list_iterate_items ( devl , & scan_devs ) {
if ( ! ( devl - > dev - > flags & DEV_SCAN_FOUND_LABEL ) )
continue ;
if ( ! ( devl - > dev - > flags & DEV_UDEV_INFO_MISSING ) )
continue ;
if ( ! once + + )
log_debug_devs ( " Scanning end of PVs with no udev info for MD components " ) ;
if ( dev_is_md_component ( devl - > dev , NULL , 1 ) ) {
2019-08-01 23:04:10 +03:00
log_debug_devs ( " Scan dropping PV from MD component %s " , dev_name ( devl - > dev ) ) ;
2019-05-21 20:06:34 +03:00
devl - > dev - > flags & = ~ DEV_SCAN_FOUND_LABEL ;
lvmcache_del_dev ( devl - > dev ) ;
2019-08-01 23:04:10 +03:00
lvmcache_del_dev_from_duplicates ( devl - > dev ) ;
2019-05-21 20:06:34 +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 ) ;
}
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 . )
*/
device usage based on devices file
The devices file /etc/lvm/devices/system.devices is a list of
devices that lvm can use. This is the default system devices
file, which is specified in lvm.conf devices/devicesfile.
The command option --devicesfile <filename> allows lvm to be
used with a different set of devices. This allows different
applications to use lvm on different sets of devices, e.g.
system devices do not need to be exposed to an application
using lvm on its own devices, and application devices do not
need to be exposed to the system.
In most cases (with limited exceptions), lvm will not read or
use a device not listed in the devices file. When the devices
file is used, the regex filter is not used, and the filter
settings in lvm.conf are ignored. filter-deviceid is used
when the devices file is enabled, and rejects any device that
does not match an entry in the devices file.
Set use_devicesfile=0 in lvm.conf or set --devicesfile ""
on the command line to disable the use of a devices file.
When disabled, lvm will see and use any device on the system
that passes the regex filter (and other standard filters.)
A device ID, e.g. wwid or serial number from sysfs, is a
unique ID that identifies a device. The device ID is
generally independent of the device content, and lvm can
get the device ID without reading the device.
The device ID is used in the devices file as the primary
method of identifying device entries, and is also included
in VG metadata for PVs.
Each device_id has a device_id_type which indicates where
the device_id comes from, e.g. "sys_wwid" means the device_id
comes from the sysfs wwid file. Others are sys_serial,
mpath_uuid, loop_file, md_uuid, devname. (devname is the
device path, which is a fallback when no other proper
device_id_type is available.)
filter-deviceid permits lvm to use only devices on the system
that have a device_id matching a devices file entry. Using
the device_id, lvm can determine the set of devices to use
without reading any devices, so the devices file will constrain
lvm in two ways:
1. it limits the devices that lvm will read.
2. it limits the devices that lvm will use.
In some uncommon cases, e.g. when devices have no unique ID
and device_id has to fall back to using the devname, lvm may
need to read all devices on the system to determine which
ones correspond to the devices file entries. In this case,
the devices file does not limit the devices that lvm reads,
but it does limit the devices that lvm uses.
pvcreate/vgcreate/vgextend are not constrained by the devices
file, and will look outside it to find the new PV. They assign
the new PV a device_id and add it to the devices file. It is
also possible to explicitly add new PVs to the devices file before
using them in pvcreate/etc, in which case these commands would not
need to look outside the devices file for the new device.
vgimportdevices VG looks at all devices on the system to find an
existing VG and add its devices to the devices file. The command
is not limited by an existing devices file. The command will also
add device_ids to the VG metadata if the VG does not yet include
device_ids. vgimportdevices -a imports devices for all accessible
VGs. Since vgimportdevices does not limit itself to devices in
an existing devices file, the lvm.conf regex filter applies.
Adding --foreign will import devices for foreign VGs, but device_ids
are not added to foreign VGs. Incomplete VGs are not imported.
The lvmdevices command manages the devices file. The primary
purpose is to edit the devices file, but it will read PV headers
to find/check PVIDs. (It does not read, process or modify VG
metadata.)
lvmdevices
. Displays devices file entries.
lvmdevices --check
. Checks devices file entries.
lvmdevices --update
. Updates devices file entries.
lvmdevices --adddev <devname>
. Adds devices_file entry (reads pv header).
lvmdevices --deldev <devname>
. Removes devices file entry.
lvmdevices --addpvid <pvid>
. Reads pv header of all devices to find <pvid>,
and if found adds devices file entry.
lvmdevices --delpvid <pvid>
. Removes devices file entry.
The vgimportclone command has a new option --importdevices
that does the equivalent of vgimportdevices with the cloned
devices that are being imported. The devices are "uncloned"
(new vgname and pvids) while at the same time adding the
devices to the devices file. This allows cloned PVs to be
imported without duplicate PVs ever appearing on the system.
The command option --devices <devnames> allows a specific
list of devices to be exposed to the lvm command, overriding
the devices file.
2020-06-23 21:25:41 +03:00
if ( create_hints & & ! 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 .
*/
int label_read_pvid ( struct device * dev )
{
char buf [ 4096 ] __attribute__ ( ( aligned ( 8 ) ) ) ;
struct label_header * lh ;
struct pv_header * pvh ;
memset ( buf , 0 , sizeof ( buf ) ) ;
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 ) ;
return 0 ;
}
lh = ( struct label_header * ) ( buf + 512 ) ;
if ( memcmp ( lh - > id , LABEL_ID , sizeof ( lh - > id ) ) ) {
/* Not an lvm deice */
label_scan_invalidate ( dev ) ;
return 0 ;
}
/*
* wipefs - a just clears the type field , leaving the
* rest of the label_header intact .
*/
if ( memcmp ( lh - > type , LVM2_LABEL , sizeof ( lh - > type ) ) ) {
/* Not an lvm deice */
label_scan_invalidate ( dev ) ;
return 0 ;
}
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 )
{
if ( ! scan_bcache )
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 ;
2018-03-07 00:05:27 +03:00
if ( ! scan_bcache ) {
2019-07-09 21:16:26 +03:00
if ( ! _setup_bcache ( ) )
2018-03-07 00:05:27 +03:00
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
dm_list_iterate_items ( devl , devs ) {
if ( _in_bcache ( devl - > dev ) )
_invalidate_di ( scan_bcache , devl - > dev - > bcache_di ) ;
}
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 ;
if ( ! scan_bcache ) {
2019-07-09 21:16:26 +03:00
if ( ! _setup_bcache ( ) )
2019-06-12 00:17:24 +03:00
return 0 ;
}
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 ) )
_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 ) ) {
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
_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 )
{
struct lvinfo lvinfo ;
struct device * dev ;
dev_t devt ;
2018-10-15 17:29:09 +03:00
if ( ! lv_info ( cmd , lv , 0 , & lvinfo , 0 , 0 ) )
return ;
2018-02-16 23:18:55 +03:00
devt = MKDEV ( lvinfo . major , lvinfo . minor ) ;
2019-04-05 22:03:38 +03:00
if ( ( dev = dev_cache_get_by_devt ( cmd , devt , NULL , NULL ) ) )
2018-02-16 23:18:55 +03:00
label_scan_invalidate ( dev ) ;
}
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 ;
2018-05-04 01:12:07 +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 ) ) ) {
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 )
{
if ( ! scan_bcache )
return ;
label_scan_drop ( cmd ) ;
2018-02-07 00:18:11 +03:00
bcache_destroy ( scan_bcache ) ;
scan_bcache = NULL ;
}
/*
* 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 .
*/
2020-10-15 20:56:36 +03:00
int label_scan_dev ( 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
2020-10-23 21:53:52 +03:00
_scan_list ( NULL , 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-15 01:21:27 +03:00
int label_scan_setup_bcache ( void )
{
if ( ! scan_bcache ) {
2019-07-09 21:16:26 +03:00
if ( ! _setup_bcache ( ) )
2018-02-15 01:21:27 +03:00
return 0 ;
}
return 1 ;
}
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 ) ) ;
_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 ) ) ;
_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 )
{
int flags = 0 ;
int prev_fd = dev - > bcache_fd ;
int fd ;
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 ;
fd = open ( dev_name ( dev ) , flags , 0777 ) ;
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 ) ;
close ( fd ) ;
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
{
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
}
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_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 ;
2018-02-27 20:26:04 +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 ) ) ;
_invalidate_di ( scan_bcache , dev - > bcache_di ) ;
2018-06-20 19:32:45 +03:00
_scan_dev_close ( dev ) ;
dev - > flags | = DEV_BCACHE_WRITE ;
label_scan_open ( 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
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
}
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_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 ;
}
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 )
{
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
return bcache_invalidate_bytes ( scan_bcache , dev - > bcache_di , start , len ) ;
2019-11-27 00:46:49 +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 ;
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 ) ) ;
_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 )
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
rv = bcache_zero_bytes ( scan_bcache , dev - > bcache_di , start , len ) ;
2020-09-16 21:54:16 +03:00
else
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
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
}
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
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_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 )
{
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_unset_last_byte ( scan_bcache , dev - > bcache_di ) ;
2018-10-30 00:53:17 +03:00
}