2001-10-03 21:03:25 +04:00
/*
2008-01-30 17:00:02 +03:00
* Copyright ( C ) 2001 - 2004 Sistina Software , Inc . All rights reserved .
2009-09-03 01:29:23 +04:00
* Copyright ( C ) 2004 - 2009 Red Hat , Inc . All rights reserved .
2001-10-03 21:03:25 +04:00
*
2004-03-30 23:35:44 +04:00
* This file is part of LVM2 .
2001-10-03 21:03:25 +04:00
*
2004-03-30 23:35:44 +04:00
* 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 .
2001-10-03 21:03:25 +04:00
*
2007-08-21 00:55:30 +04:00
* You should have received a copy of the GNU Lesser General Public License
2004-03-30 23:35:44 +04:00
* along with this program ; if not , write to the Free Software Foundation ,
2016-01-21 13:49:46 +03:00
* Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
2001-10-03 21:03:25 +04:00
*/
# include "tools.h"
2015-12-01 23:09:01 +03:00
struct vgrename_params {
const char * vg_name_old ;
const char * vg_name_new ;
unsigned int old_name_is_uuid : 1 ;
unsigned int lock_vg_old_first : 1 ;
unsigned int unlock_new_name : 1 ;
} ;
2009-09-03 01:29:23 +04:00
2010-04-14 17:03:06 +04:00
static int _lock_new_vg_for_rename ( struct cmd_context * cmd ,
const char * vg_name_new )
2009-09-03 01:29:23 +04:00
{
2015-12-01 23:09:01 +03:00
if ( ! lock_vol ( cmd , vg_name_new , LCK_VG_WRITE , NULL ) ) {
2009-09-03 01:29:23 +04:00
log_error ( " Can't get lock for %s " , vg_name_new ) ;
return 0 ;
}
return 1 ;
}
2015-12-01 23:09:01 +03:00
static int _vgrename_single ( struct cmd_context * cmd , const char * vg_name ,
struct volume_group * vg , struct processing_handle * handle )
2001-10-03 21:03:25 +04:00
{
2015-12-01 23:09:01 +03:00
struct vgrename_params * vp = ( struct vgrename_params * ) handle - > custom_handle ;
2017-03-30 01:17:37 +03:00
char old_path [ PATH_MAX ] ;
char new_path [ PATH_MAX ] ;
2021-08-03 23:32:33 +03:00
char vgid [ ID_LEN + 1 ] __attribute__ ( ( aligned ( 8 ) ) ) = { 0 } ;
2006-04-14 01:08:29 +04:00
struct id id ;
2015-12-01 23:09:01 +03:00
const char * name ;
char * dev_dir ;
2013-10-08 13:15:05 +04:00
2015-12-01 23:09:01 +03:00
/*
* vg_name_old may be a UUID which process_each_vg
* replaced with the real VG name . In that case ,
* vp - > vg_name_old will be the UUID and vg_name will be
* the actual VG name . Check again if the old and new
* names match , using the real names .
*/
if ( vp - > old_name_is_uuid & & ! strcmp ( vp - > vg_name_new , vg_name ) ) {
log_error ( " New VG name must differ from the old VG name. " ) ;
return ECMD_FAILED ;
2002-02-12 00:00:35 +03:00
}
2002-02-11 18:42:34 +03:00
2015-12-01 23:09:01 +03:00
/*
* Check if a VG already exists with the new VG name .
*
* ( FIXME : We could look for the new name in the list of all
* VGs that process_each_vg created , but we don ' t have access
2018-07-10 21:39:29 +03:00
* to that list here , so we have to look in lvmcache . )
2015-12-01 23:09:01 +03:00
*/
2016-02-23 21:57:56 +03:00
if ( lvmcache_vginfo_from_vgname ( vp - > vg_name_new , NULL ) ) {
2015-12-01 23:09:01 +03:00
log_error ( " New VG name \" %s \" already exists " , vp - > vg_name_new ) ;
return ECMD_FAILED ;
2006-04-14 01:08:29 +04:00
}
2021-08-03 23:32:33 +03:00
if ( id_read_format_try ( & id , vp - > vg_name_new ) ) {
memcpy ( vgid , & id , ID_LEN ) ;
if ( ( name = lvmcache_vgname_from_vgid ( cmd - > mem , vgid ) ) ) {
log_error ( " New VG name \" %s \" matches the UUID of existing VG %s " , vp - > vg_name_new , name ) ;
return ECMD_FAILED ;
}
2015-12-01 23:09:01 +03:00
}
2009-09-03 01:29:40 +04:00
2015-12-01 23:09:01 +03:00
/*
* Lock the old VG name first :
* . The old VG name has already been locked by process_each_vg .
* . Now lock the new VG name here , second .
*
* Lock the new VG name first :
* . The new VG name has already been pre - locked below ,
* before process_each_vg was called .
* . process_each_vg then locked the old VG name second .
* . Nothing to do here .
*
* Special case when the old VG name is a uuid :
* . The old VG ' s real name wasn ' t known before process_each_vg ,
* so the correct lock ordering wasn ' t known beforehand ,
* so no pre - locking was done .
* . The old VG ' s real name has been locked by process_each_vg .
* . Now lock the new VG name here , second .
* . Suppress lock ordering checks because the lock order may
* have wanted the new name first , which wasn ' t possible in
* this uuid - for - name case .
*/
if ( vp - > lock_vg_old_first | | vp - > old_name_is_uuid ) {
if ( ! _lock_new_vg_for_rename ( cmd , vp - > vg_name_new ) )
return ECMD_FAILED ;
2001-10-03 21:03:25 +04:00
}
2015-12-01 23:09:01 +03:00
dev_dir = cmd - > dev_dir ;
2015-09-03 18:38:16 +03:00
if ( ! lockd_rename_vg_before ( cmd , vg ) ) {
stack ;
2015-09-03 18:21:44 +03:00
goto error ;
2015-09-03 18:38:16 +03:00
}
2015-03-05 23:00:44 +03:00
2001-10-08 22:44:22 +04:00
/* Change the volume group name */
2015-12-01 23:09:01 +03:00
vg_rename ( cmd , vg , vp - > vg_name_new ) ;
2001-10-03 21:03:25 +04:00
2008-12-19 17:22:48 +03:00
/* store it on disks */
log_verbose ( " Writing out updated volume group " ) ;
if ( ! vg_write ( vg ) | | ! vg_commit ( vg ) ) {
goto error ;
}
2017-03-30 01:17:37 +03:00
if ( ( dm_snprintf ( old_path , sizeof ( old_path ) , " %s%s " , dev_dir , vg_name ) < 0 ) | |
( dm_snprintf ( new_path , sizeof ( new_path ) , " %s%s " , dev_dir , vp - > vg_name_new ) < 0 ) ) {
log_error ( " Renaming path is too long %s/%s %s/%s " ,
dev_dir , vg_name , dev_dir , vp - > vg_name_new ) ;
goto error ;
}
2001-10-03 21:03:25 +04:00
2008-04-30 18:34:02 +04:00
if ( activation ( ) & & dir_exists ( old_path ) ) {
2002-02-11 18:42:34 +03:00
log_verbose ( " Renaming \" %s \" to \" %s \" " , old_path , new_path ) ;
2008-12-19 17:22:48 +03:00
2002-11-18 17:04:08 +03:00
if ( test_mode ( ) )
log_verbose ( " Test mode: Skipping rename. " ) ;
2001-10-03 21:03:25 +04:00
2010-02-24 23:01:40 +03:00
else if ( lvs_in_vg_activated ( vg ) ) {
2008-12-22 12:00:51 +03:00
if ( ! vg_refresh_visible ( cmd , vg ) ) {
2008-12-19 17:22:48 +03:00
log_error ( " Renaming \" %s \" to \" %s \" failed " ,
old_path , new_path ) ;
goto error ;
}
}
2001-10-03 21:03:25 +04:00
}
2015-03-05 23:00:44 +03:00
lockd_rename_vg_final ( cmd , vg , 1 ) ;
2015-12-01 23:09:01 +03:00
if ( ! backup_remove ( cmd , vg_name ) )
2010-12-22 18:36:41 +03:00
stack ;
2001-10-03 21:03:25 +04: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 , vg , vp - > vg_name_new ) ;
2015-12-01 23:09:01 +03:00
vp - > unlock_new_name = 0 ;
2002-02-11 18:42:34 +03:00
config: add silent mode
Accept -q as the short form of --quiet.
Suppress non-essential standard output if -q is given twice.
Treat log/silent in lvm.conf as equivalent to -qq.
Review all log_print messages and change some to
log_print_unless_silent.
When silent, the following commands still produce output:
dumpconfig, lvdisplay, lvmdiskscan, lvs, pvck, pvdisplay,
pvs, version, vgcfgrestore -l, vgdisplay, vgs.
[Needs checking.]
Non-essential messages are shifted from log level 4 to log level 5
for syslog and lvm2_log_fn purposes.
2012-08-25 23:35:48 +04:00
log_print_unless_silent ( " Volume group \" %s \" successfully renamed to \" %s \" " ,
2015-12-01 23:09:01 +03:00
vp - > vg_name_old , vp - > vg_name_new ) ;
2007-08-31 23:09:49 +04:00
return 1 ;
2002-02-11 18:42:34 +03:00
2015-12-01 23:09:01 +03:00
error :
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 , vg , vp - > vg_name_new ) ;
2015-12-01 23:09:01 +03:00
vp - > unlock_new_name = 0 ;
2015-03-05 23:00:44 +03:00
lockd_rename_vg_final ( cmd , vg , 0 ) ;
2007-08-31 23:09:49 +04:00
return 0 ;
}
int vgrename ( struct cmd_context * cmd , int argc , char * * argv )
{
2015-12-01 23:09:01 +03:00
struct vgrename_params vp = { 0 } ;
struct processing_handle * handle ;
const char * vg_name_new ;
const char * vg_name_old ;
struct id id ;
int ret ;
2007-08-31 23:09:49 +04:00
if ( argc ! = 2 ) {
log_error ( " Old and new volume group names need specifying " ) ;
return EINVALID_CMD_LINE ;
}
2015-12-01 23:09:01 +03:00
vg_name_old = skip_dev_dir ( cmd , argv [ 0 ] , NULL ) ;
vg_name_new = skip_dev_dir ( cmd , argv [ 1 ] , NULL ) ;
if ( ! validate_vg_rename_params ( cmd , vg_name_old , vg_name_new ) )
2020-04-21 17:33:56 +03:00
return_ECMD_FAILED ;
2015-12-01 23:09:01 +03:00
if ( ! ( vp . vg_name_old = dm_pool_strdup ( cmd - > mem , vg_name_old ) ) )
return_ECMD_FAILED ;
if ( ! ( vp . vg_name_new = dm_pool_strdup ( cmd - > mem , vg_name_new ) ) )
return_ECMD_FAILED ;
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 " ) )
2015-03-05 23:00:44 +03:00
return_ECMD_FAILED ;
2018-12-07 23:35:22 +03:00
clear_hint_file ( cmd ) ;
2015-12-01 23:09:01 +03:00
/*
* Special case where vg_name_old may be a UUID :
* If vg_name_old is a UUID , then process_each may
* translate it to an actual VG name that we don ' t
* yet know . The lock ordering , and pre - locking ,
* needs to be done based on VG names . When
* vg_name_old is a UUID , do not do any pre - locking
* based on it , since it ' s likely to be wrong , and
* defer all the locking to the _single function .
*
* When it ' s not a UUID , we know the two VG names ,
* and we can pre - lock the new VG name if the lock
* ordering wants it locked before the old VG name
* which will be locked by process_each . If lock
* ordering wants the old name locked first , then
* the _single function will lock the new VG name .
*/
if ( ! ( vp . old_name_is_uuid = id_read_format_try ( & id , vg_name_old ) ) ) {
if ( strcmp ( vg_name_new , vg_name_old ) < 0 ) {
vp . lock_vg_old_first = 0 ;
vp . unlock_new_name = 1 ;
if ( ! _lock_new_vg_for_rename ( cmd , vg_name_new ) )
return ECMD_FAILED ;
} else {
/* The old VG is locked by process_each_vg. */
vp . lock_vg_old_first = 1 ;
}
}
2016-05-31 13:24:05 +03:00
if ( ! ( handle = init_processing_handle ( cmd , NULL ) ) ) {
2015-12-01 23:09:01 +03:00
log_error ( " Failed to initialize processing handle. " ) ;
return ECMD_FAILED ;
}
handle - > custom_handle = & vp ;
2019-06-21 21:37:11 +03:00
ret = process_each_vg ( cmd , 0 , NULL , vg_name_old , NULL , READ_FOR_UPDATE ,
2016-05-03 12:46:28 +03:00
0 , handle , _vgrename_single ) ;
2015-12-01 23:09:01 +03:00
/* Needed if process_each_vg returns error before calling _single. */
if ( vp . unlock_new_name )
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 , vg_name_new ) ;
2009-09-03 01:29:23 +04:00
2015-12-01 23:09:01 +03:00
destroy_processing_handle ( cmd , handle ) ;
return ret ;
2001-10-03 21:03:25 +04:00
}