2016-05-25 21:57:33 +03:00
/*
* Copyright ( C ) 2016 Red Hat , Inc . All rights reserved .
*
* 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
* of the GNU Lesser General Public License v .2 .1 .
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program ; if not , write to the Free Software Foundation ,
* Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
*/
# include "tools.h"
2018-05-14 12:30:20 +03:00
# include "lib/cache/lvmcache.h"
# include "lib/filters/filter.h"
2016-05-25 21:57:33 +03:00
struct vgimportclone_params {
unsigned done ;
unsigned total ;
int import_vg ;
int found_args ;
struct dm_list arg_import ;
const char * base_vgname ;
const char * old_vgname ;
const char * new_vgname ;
} ;
struct vgimportclone_device {
struct dm_list list ;
struct device * dev ;
unsigned found_in_vg : 1 ;
} ;
static int _vgimportclone_pv_single ( struct cmd_context * cmd , struct volume_group * vg ,
struct physical_volume * pv , struct processing_handle * handle )
{
struct vgimportclone_params * vp = ( struct vgimportclone_params * ) handle - > custom_handle ;
struct vgimportclone_device * vd ;
if ( vg & & is_orphan_vg ( vg - > name ) ) {
log_error ( " Cannot import clone of orphan PV %s. " , dev_name ( pv - > dev ) ) ;
return ECMD_FAILED ;
}
if ( ! ( vd = dm_pool_zalloc ( cmd - > mem , sizeof ( * vd ) ) ) ) {
log_error ( " alloc failed. " ) ;
return ECMD_FAILED ;
}
vd - > dev = pv - > dev ;
dm_list_add ( & vp - > arg_import , & vd - > list ) ;
log_debug ( " vgimportclone dev %s VG %s found to import " ,
dev_name ( vd - > dev ) , vg ? vg - > name : " <none> " ) ;
vp - > found_args + + ;
return ECMD_PROCESSED ;
}
static int _vgimportclone_vg_single ( struct cmd_context * cmd , const char * vg_name ,
struct volume_group * vg , struct processing_handle * handle )
{
char uuid [ 64 ] __attribute__ ( ( aligned ( 8 ) ) ) ;
struct vgimportclone_params * vp = ( struct vgimportclone_params * ) handle - > custom_handle ;
struct vgimportclone_device * vd ;
struct pv_list * pvl , * new_pvl ;
struct lv_list * lvl ;
int devs_used_for_lv = 0 ;
int found ;
if ( vg_is_exported ( vg ) & & ! vp - > import_vg ) {
log_error ( " VG %s is exported, use the --import option. " , vg - > name ) ;
2016-11-25 15:46:06 +03:00
goto bad ;
2016-05-25 21:57:33 +03:00
}
if ( vg_status ( vg ) & PARTIAL_VG ) {
log_error ( " VG %s is partial, it must be complete. " , vg - > name ) ;
2016-11-25 15:46:06 +03:00
goto bad ;
2016-05-25 21:57:33 +03:00
}
/*
* N . B . lvs_in_vg_activated ( ) is not smart enough to distinguish
* between LVs that are active in the original VG vs the cloned VG
* that ' s being imported , so check DEV_USED_FOR_LV .
*/
dm_list_iterate_items ( pvl , & vg - > pvs ) {
if ( pvl - > pv - > dev - > flags & DEV_USED_FOR_LV ) {
log_error ( " Device %s has active LVs, deactivate first. " , dev_name ( pvl - > pv - > dev ) ) ;
devs_used_for_lv + + ;
}
}
if ( devs_used_for_lv )
goto_bad ;
/*
* The arg_import list must match the PVs in VG .
*/
dm_list_iterate_items ( pvl , & vg - > pvs ) {
found = 0 ;
dm_list_iterate_items ( vd , & vp - > arg_import ) {
if ( pvl - > pv - > dev ! = vd - > dev )
continue ;
vd - > found_in_vg = 1 ;
found = 1 ;
break ;
}
if ( ! found ) {
if ( ! id_write_format ( & pvl - > pv - > id , uuid , sizeof ( uuid ) ) )
goto_bad ;
/* all PVs in the VG must be imported together, pvl is missing from args. */
log_error ( " PV with UUID %s is part of VG %s, but is not included in the devices to import. " ,
uuid , vg - > name ) ;
log_error ( " All PVs in the VG must be imported together. " ) ;
2016-11-25 15:46:06 +03:00
goto bad ;
2016-05-25 21:57:33 +03:00
}
}
dm_list_iterate_items ( vd , & vp - > arg_import ) {
if ( ! vd - > found_in_vg ) {
/* device arg is not in the VG. */
log_error ( " Device %s was not found in VG %s. " , dev_name ( vd - > dev ) , vg - > name ) ;
log_error ( " The devices to import must match the devices in the VG. " ) ;
2016-11-25 15:46:06 +03:00
goto bad ;
2016-05-25 21:57:33 +03:00
}
}
/*
* Write changes .
*/
if ( ! archive ( vg ) )
return_ECMD_FAILED ;
if ( vp - > import_vg )
vg - > status & = ~ EXPORTED_VG ;
if ( ! id_create ( & vg - > id ) )
goto_bad ;
/* Low level vg_write code needs old_name to be set! */
vg - > old_name = vg - > name ;
if ( ! ( vg - > name = dm_pool_strdup ( vg - > vgmem , vp - > new_vgname ) ) )
goto_bad ;
2018-05-25 23:26:30 +03:00
/* A duplicate of a shared VG is imported as a new local VG. */
vg - > lock_type = NULL ;
vg - > lock_args = NULL ;
vg - > system_id = cmd - > system_id ? dm_pool_strdup ( vg - > vgmem , cmd - > system_id ) : NULL ;
2016-05-25 21:57:33 +03:00
dm_list_iterate_items ( pvl , & vg - > pvs ) {
if ( ! ( new_pvl = dm_pool_zalloc ( vg - > vgmem , sizeof ( * new_pvl ) ) ) )
goto_bad ;
new_pvl - > pv = pvl - > pv ;
if ( ! ( pvl - > pv - > vg_name = dm_pool_strdup ( vg - > vgmem , vp - > new_vgname ) ) )
goto_bad ;
if ( vp - > import_vg )
new_pvl - > pv - > status & = ~ EXPORTED_VG ;
/* Low level pv_write code needs old_id to be set! */
memcpy ( & new_pvl - > pv - > old_id , & new_pvl - > pv - > id , sizeof ( new_pvl - > pv - > id ) ) ;
if ( ! id_create ( & new_pvl - > pv - > id ) )
goto_bad ;
dm_list_add ( & vg - > pv_write_list , & new_pvl - > list ) ;
}
2018-05-25 23:26:30 +03:00
dm_list_iterate_items ( lvl , & vg - > lvs ) {
2016-05-25 21:57:33 +03:00
memcpy ( & lvl - > lv - > lvid , & vg - > id , sizeof ( vg - > id ) ) ;
2018-05-25 23:26:30 +03:00
lvl - > lv - > lock_args = NULL ;
}
2016-05-25 21:57:33 +03:00
if ( ! vg_write ( vg ) | | ! vg_commit ( vg ) )
goto_bad ;
return ECMD_PROCESSED ;
bad :
return ECMD_FAILED ;
}
int vgimportclone ( struct cmd_context * cmd , int argc , char * * argv )
{
struct vgimportclone_params vp = { 0 } ;
struct processing_handle * handle = NULL ;
struct dm_list vgnameids_on_system ; /* vgnameid_list */
struct vgnameid_list * vgnl ;
struct vgimportclone_device * vd ;
struct lvmcache_info * info ;
const char * vgname ;
char base_vgname [ NAME_LEN ] = { 0 } ;
char tmp_vgname [ NAME_LEN ] = { 0 } ;
unsigned int vgname_count ;
int ret = ECMD_FAILED ;
if ( ! argc ) {
log_error ( " PV names required. " ) ;
return EINVALID_CMD_LINE ;
}
dm_list_init ( & vgnameids_on_system ) ;
dm_list_init ( & vp . arg_import ) ;
set_pv_notify ( cmd ) ;
vp . import_vg = arg_is_set ( cmd , import_ARG ) ;
if ( ! ( handle = init_processing_handle ( cmd , NULL ) ) ) {
log_error ( " Failed to initialize processing handle. " ) ;
return ECMD_FAILED ;
}
handle - > custom_handle = & vp ;
locking: unify global lock for flock and lockd
There have been two file locks used to protect lvm
"global state": "ORPHANS" and "GLOBAL".
Commands that used the ORPHAN flock in exclusive mode:
pvcreate, pvremove, vgcreate, vgextend, vgremove,
vgcfgrestore
Commands that used the ORPHAN flock in shared mode:
vgimportclone, pvs, pvscan, pvresize, pvmove,
pvdisplay, pvchange, fullreport
Commands that used the GLOBAL flock in exclusive mode:
pvchange, pvscan, vgimportclone, vgscan
Commands that used the GLOBAL flock in shared mode:
pvscan --cache, pvs
The ORPHAN lock covers the important cases of serializing
the use of orphan PVs. It also partially covers the
reporting of orphan PVs (although not correctly as
explained below.)
The GLOBAL lock doesn't seem to have a clear purpose
(it may have eroded over time.)
Neither lock correctly protects the VG namespace, or
orphan PV properties.
To simplify and correct these issues, the two separate
flocks are combined into the one GLOBAL flock, and this flock
is used from the locking sites that are in place for the
lvmlockd global lock.
The logic behind the lvmlockd (distributed) global lock is
that any command that changes "global state" needs to take
the global lock in ex mode. Global state in lvm is: the list
of VG names, the set of orphan PVs, and any properties of
orphan PVs. Reading this global state can use the global lock
in sh mode to ensure it doesn't change while being reported.
The locking of global state now looks like:
lockd_global()
previously named lockd_gl(), acquires the distributed
global lock through lvmlockd. This is unchanged.
It serializes distributed lvm commands that are changing
global state. This is a no-op when lvmlockd is not in use.
lockf_global()
acquires an flock on a local file. It serializes local lvm
commands that are changing global state.
lock_global()
first calls lockf_global() to acquire the local flock for
global state, and if this succeeds, it calls lockd_global()
to acquire the distributed lock for global state.
Replace instances of lockd_gl() with lock_global(), so that the
existing sites for lvmlockd global state locking are now also
used for local file locking of global state. Remove the previous
file locking calls lock_vol(GLOBAL) and lock_vol(ORPHAN).
The following commands which change global state are now
serialized with the exclusive global flock:
pvchange (of orphan), pvresize (of orphan), pvcreate, pvremove,
vgcreate, vgextend, vgremove, vgreduce, vgrename,
vgcfgrestore, vgimportclone, vgmerge, vgsplit
Commands that use a shared flock to read global state (and will
be serialized against the prior list) are those that use
process_each functions that are based on processing a list of
all VG names, or all PVs. The list of all VGs or all PVs is
global state and the shared lock prevents those lists from
changing while the command is processing them.
The ORPHAN lock previously attempted to produce an accurate
listing of orphan PVs, but it was only acquired at the end of
the command during the fake vg_read of the fake orphan vg.
This is not when orphan PVs were determined; they were
determined by elimination beforehand by processing all real
VGs, and subtracting the PVs in the real VGs from the list
of all PVs that had been identified during the initial scan.
This is fixed by holding the single global lock in shared mode
while processing all VGs to determine the list of orphan PVs.
2019-04-18 23:01:19 +03:00
if ( ! lock_global ( cmd , " ex " ) ) {
2016-05-25 21:57:33 +03:00
destroy_processing_handle ( cmd , handle ) ;
return ECMD_FAILED ;
}
/*
* Find the devices being imported which are named on the command line .
* They may be in the list of unchosen duplicates .
*/
log_debug ( " Finding devices to import. " ) ;
2017-01-14 00:08:51 +03:00
cmd - > cname - > flags | = ENABLE_DUPLICATE_DEVS ;
2019-06-21 21:37:11 +03:00
process_each_pv ( cmd , argc , argv , NULL , 0 , 0 , handle , _vgimportclone_pv_single ) ;
2016-05-25 21:57:33 +03:00
if ( vp . found_args ! = argc ) {
log_error ( " Failed to find all devices. " ) ;
2016-11-25 15:46:06 +03:00
goto out ;
2016-05-25 21:57:33 +03:00
}
/*
* Find the VG name of the PVs being imported , save as old_vgname .
* N . B . If vd - > dev is a duplicate , then it may not match info - > dev .
*/
dm_list_iterate_items ( vd , & vp . arg_import ) {
if ( ! ( info = lvmcache_info_from_pvid ( vd - > dev - > pvid , NULL , 0 ) ) ) {
log_error ( " Failed to find PVID for device %s in lvmcache. " , dev_name ( vd - > dev ) ) ;
2016-11-25 15:46:06 +03:00
goto out ;
2016-05-25 21:57:33 +03:00
}
if ( ! ( vgname = lvmcache_vgname_from_info ( info ) ) ) {
log_error ( " Failed to find VG name for device %s in lvmcache. " , dev_name ( vd - > dev ) ) ;
2016-11-25 15:46:06 +03:00
goto out ;
2016-05-25 21:57:33 +03:00
}
if ( ! vp . old_vgname ) {
if ( ! ( vp . old_vgname = dm_pool_strdup ( cmd - > mem , vgname ) ) )
goto_out ;
} else {
if ( strcmp ( vp . old_vgname , vgname ) ) {
log_error ( " Devices must be from the same VG. " ) ;
2016-11-25 15:46:06 +03:00
goto out ;
2016-05-25 21:57:33 +03:00
}
}
}
/*
* Pick a new VG name , save as new_vgname . The new name begins with
* the basevgname or old_vgname , plus a $ i suffix , if necessary , to
* make it unique . This requires comparing the old_vgname with all the
* VG names on the system .
*/
if ( arg_is_set ( cmd , basevgname_ARG ) ) {
2018-02-09 19:06:30 +03:00
vgname = arg_str_value ( cmd , basevgname_ARG , " " ) ;
if ( dm_snprintf ( base_vgname , sizeof ( base_vgname ) , " %s " , vgname ) < 0 ) {
log_error ( " Base vg name %s is too long. " , vgname ) ;
goto out ;
}
( void ) dm_strncpy ( tmp_vgname , base_vgname , NAME_LEN ) ;
2016-05-25 21:57:33 +03:00
vgname_count = 0 ;
} else {
2018-02-09 19:06:30 +03:00
if ( dm_snprintf ( base_vgname , sizeof ( base_vgname ) , " %s " , vp . old_vgname ) < 0 ) {
log_error ( INTERNAL_ERROR " Old vg name %s is too long. " , vp . old_vgname ) ;
goto out ;
}
if ( dm_snprintf ( tmp_vgname , sizeof ( tmp_vgname ) , " %s1 " , vp . old_vgname ) < 0 ) {
log_error ( " Temporary vg name %s1 is too long. " , vp . old_vgname ) ;
goto out ;
}
2016-05-25 21:57:33 +03:00
vgname_count = 1 ;
}
2019-06-11 22:09:13 +03:00
if ( ! lvmcache_get_vgnameids ( cmd , & vgnameids_on_system , NULL , 0 ) )
2016-05-25 21:57:33 +03:00
goto_out ;
retry_name :
dm_list_iterate_items ( vgnl , & vgnameids_on_system ) {
if ( ! strcmp ( vgnl - > vg_name , tmp_vgname ) ) {
vgname_count + + ;
2018-02-09 19:06:30 +03:00
if ( dm_snprintf ( tmp_vgname , sizeof ( tmp_vgname ) , " %s%u " , base_vgname , vgname_count ) < 0 ) {
log_error ( " Failed to generated temporary vg name, %s%u is too long. " , base_vgname , vgname_count ) ;
goto out ;
}
2016-05-25 21:57:33 +03:00
goto retry_name ;
}
}
if ( ! ( vp . new_vgname = dm_pool_strdup ( cmd - > mem , tmp_vgname ) ) )
goto_out ;
log_debug ( " Using new VG name %s. " , vp . new_vgname ) ;
2020-04-22 22:08:54 +03:00
lvmcache_destroy ( cmd , 1 , 0 ) ;
2016-05-25 21:57:33 +03:00
/*
* Create a device filter so that we are only working with the devices
* in arg_import . With the original devs hidden ( that arg_import were
* cloned from ) , we can read and write the cloned PVs and VG without
* touching the original PVs / VG .
*/
init_internal_filtering ( 1 ) ;
dm_list_iterate_items ( vd , & vp . arg_import )
internal_filter_allow ( cmd - > mem , vd - > dev ) ;
2020-04-22 22:08:54 +03:00
refresh_filters ( cmd ) ;
2016-05-25 21:57:33 +03:00
log_debug ( " Changing VG %s to %s. " , vp . old_vgname , vp . new_vgname ) ;
if ( ! lock_vol ( cmd , vp . new_vgname , LCK_VG_WRITE , NULL ) ) {
log_error ( " Can't get lock for new VG name %s " , vp . new_vgname ) ;
goto out ;
}
2018-05-25 23:26:30 +03:00
/*
* Trying to lock the duplicated VG would conflict with the original ,
* and it ' s not needed because the new VG will be imported as a local VG .
*/
cmd - > lockd_vg_disable = 1 ;
2018-12-07 23:35:22 +03:00
clear_hint_file ( cmd ) ;
2019-06-21 21:37:11 +03:00
ret = process_each_vg ( cmd , 0 , NULL , vp . old_vgname , NULL , READ_FOR_UPDATE , 0 , handle , _vgimportclone_vg_single ) ;
2016-05-25 21:57:33 +03:00
lvmetad: two phase vg_update
Previously, a command sent lvmetad new VG metadata in vg_commit().
In vg_commit(), devices are suspended, so any memory allocation
done by the command while sending to lvmetad, or by lvmetad while
updating its cache could deadlock if memory reclaim was triggered.
Now lvmetad is updated in unlock_vg(), after devices are resumed.
The new method for updating VG metadata in lvmetad is in two phases:
1. In vg_write(), before devices are suspended, the command sends
lvmetad a short message ("set_vg_info") telling it what the new
VG seqno will be. lvmetad sees that the seqno is newer than
the seqno of its cached VG, so it sets the INVALID flag for the
cached VG. If sending the message to lvmetad fails, the command
fails before the metadata is committed and the change is not made.
If sending the message succeeds, vg_commit() is called.
2. In unlock_vg(), after devices are resumed, the command sends
lvmetad the standard vg_update message with the new metadata.
lvmetad sees that the seqno in the new metadata matches the
seqno it saved from set_vg_info, and knows it has the latest
copy, so it clears the INVALID flag for the cached VG.
If a command fails between 1 and 2 (after committing the VG on disk,
but before sending lvmetad the new metadata), the cached VG retains
the INVALID flag in lvmetad. A subsequent command will read the
cached VG from lvmetad, see the INVALID flag, ignore the cached
copy, read the VG from disk instead, update the lvmetad copy
with the latest copy from disk, (this clears the INVALID flag
in lvmetad), and use the correct VG metadata for the command.
(This INVALID mechanism already existed for use by lvmlockd.)
2016-06-08 22:42:03 +03:00
unlock_vg ( cmd , NULL , vp . new_vgname ) ;
2016-05-25 21:57:33 +03:00
out :
internal_filter_clear ( ) ;
init_internal_filtering ( 0 ) ;
destroy_processing_handle ( cmd , handle ) ;
return ret ;
}