2001-09-25 16:49:28 +04:00
/*
2008-01-30 17:00:02 +03:00
* Copyright ( C ) 2001 - 2004 Sistina Software , Inc . All rights reserved .
2012-02-23 04:11:01 +04:00
* Copyright ( C ) 2004 - 2012 Red Hat , Inc . All rights reserved .
2001-09-25 16:49:28 +04:00
*
2004-03-30 23:35:44 +04:00
* This file is part of LVM2 .
*
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
2007-08-21 00:55:30 +04:00
* of the GNU Lesser General Public License v .2 .1 .
2004-03-30 23:35:44 +04:00
*
2007-08-21 00:55:30 +04:00
* You should have received a copy of the GNU Lesser General Public License
2004-03-30 23:35:44 +04:00
* along with this program ; if not , write to the Free Software Foundation ,
* Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
2001-09-25 16:49:28 +04:00
*/
2002-11-18 17:04:08 +03:00
# include "lib.h"
2001-10-12 14:32:06 +04:00
# include "device.h"
2001-10-01 19:14:39 +04:00
# include "metadata.h"
2002-02-11 23:50:53 +03:00
# include "toolcontext.h"
2002-02-25 15:56:16 +03:00
# include "lvm-string.h"
2008-01-16 01:56:30 +03:00
# include "lvm-file.h"
2014-05-01 23:07:17 +04:00
# include "lvm-signal.h"
2003-07-05 02:34:56 +04:00
# include "lvmcache.h"
2012-02-23 17:11:07 +04:00
# include "lvmetad.h"
2003-07-05 02:34:56 +04:00
# include "memlock.h"
2005-04-20 00:52:35 +04:00
# include "str_list.h"
# include "pv_alloc.h"
2009-10-16 21:41:49 +04:00
# include "segtype.h"
2005-10-25 23:08:21 +04:00
# include "activate.h"
2006-11-10 21:24:11 +03:00
# include "display.h"
2007-06-06 23:40:28 +04:00
# include "locking.h"
2007-08-21 21:38:20 +04:00
# include "archiver.h"
2008-09-19 09:33:37 +04:00
# include "defaults.h"
2015-03-05 23:00:44 +03:00
# include "lvmlockd.h"
2001-09-25 16:49:28 +04:00
2010-07-06 02:23:15 +04:00
# include <math.h>
2006-08-17 23:53:36 +04:00
# include <sys/param.h>
2008-01-30 17:00:02 +03:00
static struct physical_volume * _pv_read ( struct cmd_context * cmd ,
2009-04-22 13:31:30 +04:00
struct dm_pool * pvmem ,
2007-06-11 22:29:30 +04:00
const char * pv_name ,
2011-02-21 15:13:40 +03:00
struct format_instance * fid ,
2014-10-07 04:04:09 +04:00
uint32_t warn_flags , int scan_label_only ) ;
2007-06-11 22:29:30 +04:00
2009-07-14 06:16:05 +04:00
static uint32_t _vg_bad_status_bits ( const struct volume_group * vg ,
2009-11-25 01:55:55 +03:00
uint64_t status ) ;
Change vg_create() to take only minimal parameters and obtain a lock.
vg_t *vg_create(struct cmd_context *cmd, const char *vg_name);
This is the first step towards the API called to create a VG.
Call vg_lock_newname() inside this function. Use _vg_make_handle()
where possible.
Now we have 2 ways to construct a volume group:
1) vg_read: Used when constructing an existing VG from disks
2) vg_create: Used when constructing a new VG
Both of these interfaces obtain a lock, and return a vg_t *.
The usage of _vg_make_handle() inside vg_create() doesn't fit
perfectly but it's ok for now. Needs some cleanup though and I've
noted "FIXME" in the code.
Add the new vg_create() plus vg 'set' functions for non-default
VG parameters in the following tools:
- vgcreate: Fairly straightforward refactoring. We just moved
vg_lock_newname inside vg_create so we check the return via
vg_read_error.
- vgsplit: The refactoring here is a bit more tricky. Originally
we called vg_lock_newname and depending on the error code, we either
read the existing vg or created the new one. Now vg_create()
calls vg_lock_newname, so we first try to create the VG. If this
fails with FAILED_EXIST, we can then do the vg_read. If the
create succeeds, we check the input parameters and set any new
values on the VG.
TODO in future patches:
1. The VG_ORPHAN lock needs some thought. We may want to treat
this as any other VG, and require the application to obtain a handle
and pass it to other API calls (for example, vg_extend). Or,
we may find that hiding the VG_ORPHAN lock inside other APIs is
the way to go. I thought of placing the VG_ORPHAN lock inside
vg_create() and tying it to the vg handle, but was not certain
this was the right approach.
2. Cleanup error paths. Integrate vg_read_error() with vg_create and
vg_read* error codes and/or the new error APIs.
Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
2009-07-09 14:09:33 +04:00
2010-08-21 00:59:05 +04:00
static int _alignment_overrides_default ( unsigned long data_alignment ,
unsigned long default_pe_align )
2010-08-12 08:11:48 +04:00
{
2010-08-21 00:59:05 +04:00
return data_alignment & & ( default_pe_align % data_alignment ) ;
2010-08-12 08:11:48 +04:00
}
2009-02-22 22:00:26 +03:00
unsigned long set_pe_align ( struct physical_volume * pv , unsigned long data_alignment )
2006-08-17 23:30:59 +04:00
{
2010-08-21 00:59:05 +04:00
unsigned long default_pe_align , temp_pe_align ;
2010-08-12 08:11:48 +04:00
2008-09-19 09:19:09 +04:00
if ( pv - > pe_align )
goto out ;
2008-09-19 08:28:58 +04:00
2010-08-21 00:59:05 +04:00
if ( data_alignment ) {
/* Always use specified data_alignment */
2009-02-22 22:00:26 +03:00
pv - > pe_align = data_alignment ;
2010-08-21 00:59:05 +04:00
goto out ;
}
2013-06-25 14:30:34 +04:00
default_pe_align = find_config_tree_int ( pv - > fmt - > cmd , devices_default_data_alignment_CFG , NULL ) ;
2010-08-21 00:59:05 +04:00
if ( default_pe_align )
/* align on 1 MiB multiple */
default_pe_align * = DEFAULT_PE_ALIGN ;
2009-10-06 20:00:38 +04:00
else
2010-08-21 00:59:05 +04:00
/* align on 64 KiB multiple (old default) */
default_pe_align = DEFAULT_PE_ALIGN_OLD ;
pv - > pe_align = MAX ( ( default_pe_align < < SECTOR_SHIFT ) ,
lvm_getpagesize ( ) ) > > SECTOR_SHIFT ;
2008-09-19 09:19:09 +04:00
2008-10-03 18:22:18 +04:00
if ( ! pv - > dev )
goto out ;
2009-02-22 22:00:26 +03:00
/*
2009-07-06 23:04:24 +04:00
* Align to stripe - width of underlying md device if present
2009-02-22 22:00:26 +03:00
*/
2013-06-25 14:31:53 +04:00
if ( find_config_tree_bool ( pv - > fmt - > cmd , devices_md_chunk_alignment_CFG , NULL ) ) {
2013-06-12 14:08:56 +04:00
temp_pe_align = dev_md_stripe_width ( pv - > fmt - > cmd - > dev_types , pv - > dev ) ;
2010-08-21 00:59:05 +04:00
if ( _alignment_overrides_default ( temp_pe_align , default_pe_align ) )
2013-05-13 23:56:47 +04:00
pv - > pe_align = temp_pe_align ;
2010-08-12 08:11:48 +04:00
}
2008-09-19 09:19:09 +04:00
2009-08-01 21:08:43 +04:00
/*
* Align to topology ' s minimum_io_size or optimal_io_size if present
* - minimum_io_size - the smallest request the device can perform
* w / o incurring a read - modify - write penalty ( e . g . MD ' s chunk size )
* - optimal_io_size - the device ' s preferred unit of receiving I / O
* ( e . g . MD ' s stripe width )
*/
2013-06-25 14:31:53 +04:00
if ( find_config_tree_bool ( pv - > fmt - > cmd , devices_data_alignment_detection_CFG , NULL ) ) {
2013-06-12 14:08:56 +04:00
temp_pe_align = dev_minimum_io_size ( pv - > fmt - > cmd - > dev_types , pv - > dev ) ;
2010-08-21 00:59:05 +04:00
if ( _alignment_overrides_default ( temp_pe_align , default_pe_align ) )
2013-05-13 23:56:47 +04:00
pv - > pe_align = temp_pe_align ;
2009-08-01 21:08:43 +04:00
2013-06-12 14:08:56 +04:00
temp_pe_align = dev_optimal_io_size ( pv - > fmt - > cmd - > dev_types , pv - > dev ) ;
2010-08-21 00:59:05 +04:00
if ( _alignment_overrides_default ( temp_pe_align , default_pe_align ) )
2013-05-13 23:56:47 +04:00
pv - > pe_align = temp_pe_align ;
2009-08-01 21:08:43 +04:00
}
2010-08-21 00:59:05 +04:00
out :
2008-10-03 18:22:18 +04:00
log_very_verbose ( " %s: Setting PE alignment to %lu sectors. " ,
dev_name ( pv - > dev ) , pv - > pe_align ) ;
2008-09-19 08:28:58 +04:00
return pv - > pe_align ;
2006-08-17 23:30:59 +04:00
}
2009-07-30 21:45:28 +04:00
unsigned long set_pe_align_offset ( struct physical_volume * pv ,
unsigned long data_alignment_offset )
{
if ( pv - > pe_align_offset )
goto out ;
2010-08-21 00:59:05 +04:00
if ( data_alignment_offset ) {
/* Always use specified data_alignment_offset */
2009-07-30 21:45:28 +04:00
pv - > pe_align_offset = data_alignment_offset ;
2010-08-21 00:59:05 +04:00
goto out ;
}
2009-07-30 21:45:28 +04:00
if ( ! pv - > dev )
goto out ;
2013-06-25 14:31:53 +04:00
if ( find_config_tree_bool ( pv - > fmt - > cmd , devices_data_alignment_offset_detection_CFG , NULL ) ) {
2013-06-12 14:08:56 +04:00
int align_offset = dev_alignment_offset ( pv - > fmt - > cmd - > dev_types , pv - > dev ) ;
2010-03-03 00:56:14 +03:00
/* must handle a -1 alignment_offset; means dev is misaligned */
if ( align_offset < 0 )
align_offset = 0 ;
pv - > pe_align_offset = MAX ( pv - > pe_align_offset , align_offset ) ;
}
2009-08-01 21:07:36 +04:00
2010-08-21 00:59:05 +04:00
out :
2009-07-30 21:45:28 +04:00
log_very_verbose ( " %s: Setting PE alignment offset to %lu sectors. " ,
dev_name ( pv - > dev ) , pv - > pe_align_offset ) ;
return pv - > pe_align_offset ;
}
2010-04-06 18:04:54 +04:00
void add_pvl_to_vgs ( struct volume_group * vg , struct pv_list * pvl )
{
dm_list_add ( & vg - > pvs , & pvl - > list ) ;
vg - > pv_count + + ;
2010-04-13 21:26:36 +04:00
pvl - > pv - > vg = vg ;
2011-03-11 17:50:13 +03:00
pv_set_fid ( pvl - > pv , vg - > fid ) ;
2010-04-06 18:04:54 +04:00
}
2010-04-13 21:25:44 +04:00
void del_pvl_from_vgs ( struct volume_group * vg , struct pv_list * pvl )
{
2012-02-10 06:53:03 +04:00
struct lvmcache_info * info ;
2011-03-11 17:50:13 +03:00
2010-04-13 21:25:44 +04:00
vg - > pv_count - - ;
dm_list_del ( & pvl - > list ) ;
2011-03-11 17:50:13 +03:00
2012-02-10 06:53:03 +04:00
pvl - > pv - > vg = vg - > fid - > fmt - > orphan_vg ; /* orphan */
if ( ( info = lvmcache_info_from_pvid ( ( const char * ) & pvl - > pv - > id , 0 ) ) )
lvmcache_fid_add_mdas ( info , vg - > fid - > fmt - > orphan_vg - > fid ,
( const char * ) & pvl - > pv - > id , ID_LEN ) ;
pv_set_fid ( pvl - > pv , vg - > fid - > fmt - > orphan_vg - > fid ) ;
2010-04-13 21:25:44 +04:00
}
2007-07-12 09:04:42 +04:00
/**
* add_pv_to_vg - Add a physical volume to a volume group
* @ vg - volume group to add to
* @ pv_name - name of the pv ( to be removed )
* @ pv - physical volume to add to volume group
2011-06-01 23:29:31 +04:00
* @ pp - physical volume creation params ( OPTIONAL )
2007-07-12 09:04:42 +04:00
*
* Returns :
* 0 - failure
* 1 - success
* FIXME : remove pv_name - obtain safely from pv
*/
int add_pv_to_vg ( struct volume_group * vg , const char * pv_name ,
2011-06-01 23:29:31 +04:00
struct physical_volume * pv , struct pvcreate_params * pp )
2001-10-12 18:25:53 +04:00
{
2011-06-01 23:29:31 +04:00
struct pv_to_create * pvc ;
2001-10-15 22:39:40 +04:00
struct pv_list * pvl ;
2007-07-03 01:48:30 +04:00
struct format_instance * fid = vg - > fid ;
2009-04-10 14:01:08 +04:00
struct dm_pool * mem = vg - > vgmem ;
2010-07-09 19:34:40 +04:00
char uuid [ 64 ] __attribute__ ( ( aligned ( 8 ) ) ) ;
2001-10-12 18:25:53 +04:00
2001-10-15 22:39:40 +04:00
log_verbose ( " Adding physical volume '%s' to volume group '%s' " ,
2001-11-10 01:01:04 +03:00
pv_name , vg - > name ) ;
2001-10-15 22:39:40 +04:00
2005-10-17 03:03:59 +04:00
if ( ! ( pvl = dm_pool_zalloc ( mem , sizeof ( * pvl ) ) ) ) {
2001-10-15 22:39:40 +04:00
log_error ( " pv_list allocation for '%s' failed " , pv_name ) ;
2001-10-12 18:25:53 +04:00
return 0 ;
}
2007-11-02 16:06:42 +03:00
if ( ! is_orphan_vg ( pv - > vg_name ) ) {
2001-10-15 22:39:40 +04:00
log_error ( " Physical volume '%s' is already in volume group "
" '%s' " , pv_name , pv - > vg_name ) ;
2001-10-15 16:49:58 +04:00
return 0 ;
}
2001-10-12 18:25:53 +04:00
2002-11-18 17:04:08 +03:00
if ( pv - > fmt ! = fid - > fmt ) {
log_error ( " Physical volume %s is of different format type (%s) " ,
pv_name , pv - > fmt - > name ) ;
return 0 ;
}
2005-10-25 23:08:21 +04:00
/* Ensure PV doesn't depend on another PV already in the VG */
2006-05-11 21:58:58 +04:00
if ( pv_uses_vg ( pv , vg ) ) {
2005-10-25 23:08:21 +04:00
log_error ( " Physical volume %s might be constructed from same "
" volume group %s " , pv_name , vg - > name ) ;
return 0 ;
}
2005-10-17 03:03:59 +04:00
if ( ! ( pv - > vg_name = dm_pool_strdup ( mem , vg - > name ) ) ) {
2001-10-15 22:39:40 +04:00
log_error ( " vg->name allocation failed for '%s' " , pv_name ) ;
2001-10-12 18:25:53 +04:00
return 0 ;
}
2006-04-13 01:23:04 +04:00
memcpy ( & pv - > vgid , & vg - > id , sizeof ( vg - > id ) ) ;
2001-10-16 00:29:15 +04:00
/* Units of 512-byte sectors */
2001-10-12 18:25:53 +04:00
pv - > pe_size = vg - > extent_size ;
/*
2006-10-08 03:06:18 +04:00
* pe_count must always be calculated by pv_setup
2001-10-12 18:25:53 +04:00
*/
2002-04-24 22:20:51 +04:00
pv - > pe_alloc_count = 0 ;
2001-10-12 18:25:53 +04:00
2011-02-21 15:24:15 +03:00
if ( ! fid - > fmt - > ops - > pv_setup ( fid - > fmt , pv , vg ) ) {
2002-01-28 00:30:47 +03:00
log_error ( " Format-specific setup of physical volume '%s' "
2001-10-15 22:39:40 +04:00
" failed. " , pv_name ) ;
return 0 ;
}
2013-03-19 16:58:02 +04:00
if ( find_pv_in_vg ( vg , pv_name ) | |
find_pv_in_vg_by_uuid ( vg , & pv - > id ) ) {
2010-04-08 19:18:35 +04:00
if ( ! id_write_format ( & pv - > id , uuid , sizeof ( uuid ) ) ) {
stack ;
uuid [ 0 ] = ' \0 ' ;
}
2012-02-23 17:11:07 +04:00
log_error ( " Physical volume '%s (%s)' already in the VG. " ,
2010-04-08 19:18:35 +04:00
pv_name , uuid ) ;
2001-10-12 18:25:53 +04:00
return 0 ;
}
2003-11-06 23:33:34 +03:00
if ( vg - > pv_count & & ( vg - > pv_count = = vg - > max_pv ) ) {
2001-10-15 22:39:40 +04:00
log_error ( " No space for '%s' - volume group '%s' "
" holds max %d physical volume(s). " , pv_name ,
vg - > name , vg - > max_pv ) ;
return 0 ;
}
2008-01-30 16:19:47 +03:00
if ( ! alloc_pv_segment_whole_pv ( mem , pv ) )
return_0 ;
2001-10-15 22:39:40 +04:00
2011-11-05 02:49:53 +04:00
if ( ( uint64_t ) vg - > extent_count + pv - > pe_count > MAX_EXTENT_COUNT ) {
2006-11-10 21:24:11 +03:00
log_error ( " Unable to add %s to %s: new extent count (% "
PRIu64 " ) exceeds limit (% " PRIu32 " ). " ,
pv_name , vg - > name ,
( uint64_t ) vg - > extent_count + pv - > pe_count ,
2011-11-05 02:49:53 +04:00
MAX_EXTENT_COUNT ) ;
2006-11-10 21:24:11 +03:00
return 0 ;
}
2010-04-06 18:03:43 +04:00
pvl - > pv = pv ;
2010-04-06 18:04:54 +04:00
add_pvl_to_vgs ( vg , pvl ) ;
2001-11-06 22:02:26 +03:00
vg - > extent_count + = pv - > pe_count ;
vg - > free_count + = pv - > pe_count ;
2001-10-12 18:25:53 +04:00
2013-02-19 06:13:59 +04:00
dm_list_iterate_items ( pvl , & fid - > fmt - > orphan_vg - > pvs )
if ( pv = = pvl - > pv ) { /* unlink from orphan */
dm_list_del ( & pvl - > list ) ;
break ;
}
2011-06-01 23:29:31 +04:00
if ( pv - > status & UNLABELLED_PV ) {
if ( ! ( pvc = dm_pool_zalloc ( mem , sizeof ( * pvc ) ) ) ) {
log_error ( " pv_to_create allocation for '%s' failed " , pv_name ) ;
return 0 ;
}
pvc - > pv = pv ;
pvc - > pp = pp ;
dm_list_add ( & vg - > pvs_to_create , & pvc - > list ) ;
}
2001-10-12 18:25:53 +04:00
return 1 ;
}
2009-04-10 13:56:00 +04:00
static int _copy_pv ( struct dm_pool * pvmem ,
struct physical_volume * pv_to ,
2005-04-20 00:44:21 +04:00
struct physical_volume * pv_from )
2005-04-18 03:59:04 +04:00
{
memcpy ( pv_to , pv_from , sizeof ( * pv_to ) ) ;
2005-04-20 00:52:35 +04:00
2011-03-11 17:50:13 +03:00
/* We must use pv_set_fid here to update the reference counter! */
pv_to - > fid = NULL ;
pv_set_fid ( pv_to , pv_from - > fid ) ;
2009-04-02 19:01:11 +04:00
if ( ! ( pv_to - > vg_name = dm_pool_strdup ( pvmem , pv_from - > vg_name ) ) )
return_0 ;
if ( ! str_list_dup ( pvmem , & pv_to - > tags , & pv_from - > tags ) )
return_0 ;
2005-04-20 00:52:35 +04:00
2009-04-02 19:01:11 +04:00
if ( ! peg_dup ( pvmem , & pv_to - > segments , & pv_from - > segments ) )
2008-01-30 16:19:47 +03:00
return_0 ;
2005-04-20 00:52:35 +04:00
return 1 ;
2005-04-18 03:59:04 +04:00
}
2009-04-10 13:56:00 +04:00
static struct pv_list * _copy_pvl ( struct dm_pool * pvmem , struct pv_list * pvl_from )
{
struct pv_list * pvl_to = NULL ;
if ( ! ( pvl_to = dm_pool_zalloc ( pvmem , sizeof ( * pvl_to ) ) ) )
return_NULL ;
if ( ! ( pvl_to - > pv = dm_pool_alloc ( pvmem , sizeof ( * pvl_to - > pv ) ) ) )
goto_bad ;
2015-09-06 01:56:30 +03:00
if ( ! _copy_pv ( pvmem , pvl_to - > pv , pvl_from - > pv ) )
2009-04-10 13:56:00 +04:00
goto_bad ;
return pvl_to ;
2015-09-06 01:56:30 +03:00
2009-04-10 13:56:00 +04:00
bad :
dm_pool_free ( pvmem , pvl_to ) ;
return NULL ;
}
2005-04-18 03:57:44 +04:00
int get_pv_from_vg_by_id ( const struct format_type * fmt , const char * vg_name ,
2006-04-13 01:23:04 +04:00
const char * vgid , const char * pvid ,
struct physical_volume * pv )
2005-04-18 03:57:44 +04:00
{
struct volume_group * vg ;
struct pv_list * pvl ;
2014-10-07 04:04:09 +04:00
uint32_t warn_flags = WARN_PV_READ | WARN_INCONSISTENT ;
2009-04-10 14:01:08 +04:00
int r = 0 , consistent = 0 ;
2005-04-18 03:57:44 +04:00
2014-10-07 04:04:09 +04:00
if ( ! ( vg = vg_read_internal ( fmt - > cmd , vg_name , vgid , warn_flags , & consistent ) ) ) {
2009-07-15 21:26:26 +04:00
log_error ( " get_pv_from_vg_by_id: vg_read_internal failed to read VG %s " ,
2005-04-18 03:57:44 +04:00
vg_name ) ;
return 0 ;
}
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( pvl , & vg - > pvs ) {
2006-04-13 01:23:04 +04:00
if ( id_equal ( & pvl - > pv - > id , ( const struct id * ) pvid ) ) {
2009-04-10 13:56:00 +04:00
if ( ! _copy_pv ( fmt - > cmd - > mem , pv , pvl - > pv ) ) {
2009-04-02 19:01:11 +04:00
log_error ( " internal PV duplication failed " ) ;
2009-04-10 14:01:08 +04:00
r = 0 ;
goto out ;
2009-04-02 19:01:11 +04:00
}
2009-04-10 14:01:08 +04:00
r = 1 ;
goto out ;
2005-04-18 03:57:44 +04:00
}
}
2009-04-10 14:01:08 +04:00
out :
2011-08-11 00:25:29 +04:00
release_vg ( vg ) ;
2009-04-10 14:01:08 +04:00
return r ;
2005-04-18 03:57:44 +04:00
}
2014-04-28 14:11:44 +04:00
static int _move_pv ( struct volume_group * vg_from , struct volume_group * vg_to ,
const char * pv_name , int enforce_pv_from_source )
2009-07-14 06:15:21 +04:00
{
struct physical_volume * pv ;
struct pv_list * pvl ;
/* FIXME: handle tags */
if ( ! ( pvl = find_pv_in_vg ( vg_from , pv_name ) ) ) {
2014-04-25 23:53:34 +04:00
if ( ! enforce_pv_from_source & &
2014-06-24 16:58:53 +04:00
find_pv_in_vg ( vg_to , pv_name ) )
2014-04-25 23:53:34 +04:00
/*
* PV has already been moved . This can happen if an
* LV is being moved that has multiple sub - LVs on the
* same PV .
*/
return 1 ;
2009-07-14 06:15:21 +04:00
log_error ( " Physical volume %s not in volume group %s " ,
pv_name , vg_from - > name ) ;
return 0 ;
}
2009-07-14 06:16:05 +04:00
if ( _vg_bad_status_bits ( vg_from , RESIZEABLE_VG ) | |
_vg_bad_status_bits ( vg_to , RESIZEABLE_VG ) )
return 0 ;
2010-04-13 21:26:03 +04:00
del_pvl_from_vgs ( vg_from , pvl ) ;
add_pvl_to_vgs ( vg_to , pvl ) ;
2009-07-14 06:15:21 +04:00
pv = pvl - > pv ;
vg_from - > extent_count - = pv_pe_count ( pv ) ;
vg_to - > extent_count + = pv_pe_count ( pv ) ;
vg_from - > free_count - = pv_pe_count ( pv ) - pv_pe_alloc_count ( pv ) ;
vg_to - > free_count + = pv_pe_count ( pv ) - pv_pe_alloc_count ( pv ) ;
return 1 ;
}
2014-04-25 23:53:34 +04:00
int move_pv ( struct volume_group * vg_from , struct volume_group * vg_to ,
const char * pv_name )
{
return _move_pv ( vg_from , vg_to , pv_name , 1 ) ;
}
2009-07-14 06:15:21 +04:00
int move_pvs_used_by_lv ( struct volume_group * vg_from ,
struct volume_group * vg_to ,
const char * lv_name )
{
struct lv_segment * lvseg ;
unsigned s ;
struct lv_list * lvl ;
struct logical_volume * lv ;
/* FIXME: handle tags */
if ( ! ( lvl = find_lv_in_vg ( vg_from , lv_name ) ) ) {
log_error ( " Logical volume %s not in volume group %s " ,
lv_name , vg_from - > name ) ;
return 0 ;
}
2009-07-14 06:16:05 +04:00
if ( _vg_bad_status_bits ( vg_from , RESIZEABLE_VG ) | |
_vg_bad_status_bits ( vg_to , RESIZEABLE_VG ) )
return 0 ;
2009-07-14 06:15:21 +04:00
dm_list_iterate_items ( lvseg , & lvl - > lv - > segments ) {
if ( lvseg - > log_lv )
if ( ! move_pvs_used_by_lv ( vg_from , vg_to ,
lvseg - > log_lv - > name ) )
return_0 ;
for ( s = 0 ; s < lvseg - > area_count ; s + + ) {
if ( seg_type ( lvseg , s ) = = AREA_PV ) {
2014-04-25 23:53:34 +04:00
if ( ! _move_pv ( vg_from , vg_to ,
pv_dev_name ( seg_pv ( lvseg , s ) ) , 0 ) )
2009-07-14 06:15:21 +04:00
return_0 ;
} else if ( seg_type ( lvseg , s ) = = AREA_LV ) {
lv = seg_lv ( lvseg , s ) ;
if ( ! move_pvs_used_by_lv ( vg_from , vg_to ,
lv - > name ) )
return_0 ;
}
}
}
return 1 ;
}
2013-09-26 20:37:40 +04:00
int validate_new_vg_name ( struct cmd_context * cmd , const char * vg_name )
2008-01-16 01:56:30 +03:00
{
2011-11-18 23:31:09 +04:00
static char vg_path [ PATH_MAX ] ;
2013-09-26 20:37:40 +04:00
name_error_t name_error ;
2008-01-16 01:56:30 +03:00
2013-09-26 20:37:40 +04:00
name_error = validate_name_detailed ( vg_name ) ;
if ( NAME_VALID ! = name_error ) {
display_name_error ( name_error ) ;
log_error ( " New volume group name \" %s \" is invalid. " , vg_name ) ;
2012-10-16 12:20:02 +04:00
return 0 ;
}
2008-01-16 01:56:30 +03:00
2011-11-18 23:31:09 +04:00
snprintf ( vg_path , sizeof ( vg_path ) , " %s%s " , cmd - > dev_dir , vg_name ) ;
2008-01-16 01:56:30 +03:00
if ( path_exists ( vg_path ) ) {
log_error ( " %s: already exists in filesystem " , vg_path ) ;
return 0 ;
}
return 1 ;
}
int validate_vg_rename_params ( struct cmd_context * cmd ,
const char * vg_name_old ,
const char * vg_name_new )
{
unsigned length ;
char * dev_dir ;
dev_dir = cmd - > dev_dir ;
length = strlen ( dev_dir ) ;
/* Check sanity of new name */
if ( strlen ( vg_name_new ) > NAME_LEN - length - 2 ) {
log_error ( " New volume group path exceeds maximum length "
" of %d! " , NAME_LEN - length - 2 ) ;
return 0 ;
}
2012-10-16 12:20:02 +04:00
if ( ! validate_new_vg_name ( cmd , vg_name_new ) )
return_0 ;
2008-01-16 01:56:30 +03:00
if ( ! strcmp ( vg_name_old , vg_name_new ) ) {
log_error ( " Old and new volume group names must differ " ) ;
return 0 ;
}
return 1 ;
}
2002-12-20 02:25:55 +03:00
int vg_rename ( struct cmd_context * cmd , struct volume_group * vg ,
const char * new_name )
{
2009-04-10 14:01:08 +04:00
struct dm_pool * mem = vg - > vgmem ;
2005-06-01 20:51:55 +04:00
struct pv_list * pvl ;
2002-12-20 02:25:55 +03:00
2010-04-14 17:09:16 +04:00
vg - > old_name = vg - > name ;
2005-10-17 03:03:59 +04:00
if ( ! ( vg - > name = dm_pool_strdup ( mem , new_name ) ) ) {
2002-12-20 02:25:55 +03:00
log_error ( " vg->name allocation failed for '%s' " , new_name ) ;
return 0 ;
}
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( pvl , & vg - > pvs ) {
2005-10-17 03:03:59 +04:00
if ( ! ( pvl - > pv - > vg_name = dm_pool_strdup ( mem , new_name ) ) ) {
2002-12-20 02:25:55 +03:00
log_error ( " pv->vg_name allocation failed for '%s' " ,
2007-10-12 18:29:32 +04:00
pv_dev_name ( pvl - > pv ) ) ;
2002-12-20 02:25:55 +03:00
return 0 ;
}
}
return 1 ;
}
2009-09-03 01:39:29 +04:00
int vg_remove_check ( struct volume_group * vg )
2007-08-21 21:38:20 +04:00
{
2008-12-04 18:54:26 +03:00
unsigned lv_count ;
2007-08-21 21:38:20 +04:00
2009-07-01 21:00:50 +04:00
if ( vg_read_error ( vg ) | | vg_missing_pv_count ( vg ) ) {
2008-09-19 10:42:00 +04:00
log_error ( " Volume group \" %s \" not found, is inconsistent "
2009-07-11 00:05:29 +04:00
" or has PVs missing. " , vg ? vg - > name : " " ) ;
2007-08-21 21:38:20 +04:00
log_error ( " Consider vgreduce --removemissing if metadata "
" is inconsistent. " ) ;
return 0 ;
}
if ( ! vg_check_status ( vg , EXPORTED_VG ) )
return 0 ;
2009-05-14 01:27:43 +04:00
lv_count = vg_visible_lvs ( vg ) ;
2008-12-04 18:54:26 +03:00
if ( lv_count ) {
log_error ( " Volume group \" %s \" still contains %u "
2009-07-11 00:05:29 +04:00
" logical volume(s) " , vg - > name , lv_count ) ;
2007-08-21 21:38:20 +04:00
return 0 ;
}
if ( ! archive ( vg ) )
return 0 ;
2010-06-30 22:03:52 +04:00
return 1 ;
}
void vg_remove_pvs ( struct volume_group * vg )
{
struct pv_list * pvl , * tpvl ;
2009-09-03 01:39:49 +04:00
dm_list_iterate_items_safe ( pvl , tpvl , & vg - > pvs ) {
2010-04-13 21:26:03 +04:00
del_pvl_from_vgs ( vg , pvl ) ;
2009-09-03 01:39:49 +04:00
dm_list_add ( & vg - > removed_pvs , & pvl - > list ) ;
}
2009-09-03 01:39:29 +04:00
}
2015-03-05 23:00:44 +03:00
int vg_remove_direct ( struct volume_group * vg )
2009-09-03 01:39:29 +04:00
{
struct physical_volume * pv ;
struct pv_list * pvl ;
int ret = 1 ;
2009-09-03 01:39:07 +04:00
if ( ! vg_remove_mdas ( vg ) ) {
log_error ( " vg_remove_mdas %s failed " , vg - > name ) ;
2007-08-21 21:38:20 +04:00
return 0 ;
}
/* init physical volumes */
2009-09-03 01:39:49 +04:00
dm_list_iterate_items ( pvl , & vg - > removed_pvs ) {
2007-08-21 21:38:20 +04:00
pv = pvl - > pv ;
2010-06-30 23:55:43 +04:00
if ( is_missing_pv ( pv ) )
continue ;
2007-08-21 21:38:20 +04:00
log_verbose ( " Removing physical volume \" %s \" from "
2009-07-11 00:05:29 +04:00
" volume group \" %s \" " , pv_dev_name ( pv ) , vg - > name ) ;
2008-02-06 18:47:28 +03:00
pv - > vg_name = vg - > fid - > fmt - > orphan_vg_name ;
2014-07-11 14:24:15 +04:00
pv - > status & = ~ ALLOCATABLE_PV ;
2007-08-21 21:38:20 +04:00
if ( ! dev_get_size ( pv_dev ( pv ) , & pv - > size ) ) {
2007-10-12 18:29:32 +04:00
log_error ( " %s: Couldn't get size. " , pv_dev_name ( pv ) ) ;
2007-08-21 21:38:20 +04:00
ret = 0 ;
continue ;
}
/* FIXME Write to same sector label was read from */
2011-02-28 16:19:02 +03:00
if ( ! pv_write ( vg - > cmd , pv , 0 ) ) {
2007-08-21 21:38:20 +04:00
log_error ( " Failed to remove physical volume \" %s \" "
" from volume group \" %s \" " ,
2009-07-11 00:05:29 +04:00
pv_dev_name ( pv ) , vg - > name ) ;
2007-08-21 21:38:20 +04:00
ret = 0 ;
}
}
2012-02-23 17:11:07 +04:00
/* FIXME Handle partial failures from above. */
if ( ! lvmetad_vg_remove ( vg ) )
stack ;
2015-03-05 23:00:44 +03:00
lockd_vg_update ( vg ) ;
2010-12-22 18:36:41 +03:00
if ( ! backup_remove ( vg - > cmd , vg - > name ) )
stack ;
2007-08-21 21:38:20 +04:00
if ( ret )
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 removed " , vg - > name ) ;
2007-08-21 21:38:20 +04:00
else
2009-07-11 00:05:29 +04:00
log_error ( " Volume group \" %s \" not properly removed " , vg - > name ) ;
2007-08-21 21:38:20 +04:00
2015-03-05 23:00:44 +03:00
return ret ;
}
int vg_remove ( struct volume_group * vg )
{
int ret ;
if ( ! lock_vol ( vg - > cmd , VG_ORPHANS , LCK_VG_WRITE , NULL ) ) {
log_error ( " Can't get lock for orphan PVs " ) ;
return 0 ;
}
ret = vg_remove_direct ( vg ) ;
2009-07-11 00:08:37 +04:00
unlock_vg ( vg - > cmd , VG_ORPHANS ) ;
2007-08-21 21:38:20 +04:00
return ret ;
}
2013-12-12 14:26:35 +04:00
int check_dev_block_size_for_vg ( struct device * dev , const struct volume_group * vg ,
unsigned int * max_phys_block_size_found )
{
unsigned int phys_block_size , block_size ;
if ( ! ( dev_get_block_size ( dev , & phys_block_size , & block_size ) ) )
return_0 ;
if ( phys_block_size > * max_phys_block_size_found )
* max_phys_block_size_found = phys_block_size ;
if ( phys_block_size > > SECTOR_SHIFT > vg - > extent_size ) {
log_error ( " Physical extent size used for volume group %s "
" is less than physical block size that %s uses. " ,
vg - > name , dev_name ( dev ) ) ;
return 0 ;
}
return 1 ;
}
int vg_check_pv_dev_block_sizes ( const struct volume_group * vg )
{
struct pv_list * pvl ;
unsigned int max_phys_block_size_found = 0 ;
dm_list_iterate_items ( pvl , & vg - > pvs ) {
if ( ! check_dev_block_size_for_vg ( pvl - > pv - > dev , vg , & max_phys_block_size_found ) )
return 0 ;
}
return 1 ;
}
2009-10-06 00:02:04 +04:00
/*
* Extend a VG by a single PV / device path
*
* Parameters :
* - vg : handle of volume group to extend by ' pv_name '
* - pv_name : device path of PV to add to VG
2009-10-06 00:02:30 +04:00
* - pp : parameters to pass to implicit pvcreate ; if NULL , do not pvcreate
2013-12-12 14:26:35 +04:00
* - max_phys_block_size : largest physical block size found amongst PVs in a VG
2009-10-06 00:02:04 +04:00
*
*/
2009-10-06 00:02:30 +04:00
static int vg_extend_single_pv ( struct volume_group * vg , char * pv_name ,
2013-12-12 14:26:35 +04:00
struct pvcreate_params * pp ,
unsigned int * max_phys_block_size )
2009-10-06 00:02:04 +04:00
{
struct physical_volume * pv ;
2014-07-31 11:30:25 +04:00
pv = find_pv_by_name ( vg - > cmd , pv_name , 1 , 1 ) ;
2009-10-06 00:02:30 +04:00
if ( ! pv & & ! pp ) {
2009-10-06 00:02:04 +04:00
log_error ( " %s not identified as an existing "
" physical volume " , pv_name ) ;
return 0 ;
2009-10-06 00:02:30 +04:00
} else if ( ! pv & & pp ) {
2013-07-09 05:37:56 +04:00
if ( ! ( pv = pvcreate_vol ( vg - > cmd , pv_name , pp , 0 ) ) )
2012-03-14 00:36:02 +04:00
return_0 ;
2009-10-06 00:02:04 +04:00
}
2013-12-12 14:26:35 +04:00
if ( ! ( check_dev_block_size_for_vg ( pv - > dev , ( const struct volume_group * ) vg ,
max_phys_block_size ) ) )
goto_bad ;
if ( ! add_pv_to_vg ( vg , pv_name , pv , pp ) )
goto_bad ;
2009-10-06 00:02:04 +04:00
return 1 ;
2013-12-12 14:26:35 +04:00
bad :
free_pv_fid ( pv ) ;
return 0 ;
2009-10-06 00:02:04 +04:00
}
2009-10-06 00:02:48 +04:00
/*
* Extend a VG by a single PV / device path
*
* Parameters :
* - vg : handle of volume group to extend by ' pv_name '
* - pv_count : count of device paths of PVs
* - pv_names : device paths of PVs to add to VG
* - pp : parameters to pass to implicit pvcreate ; if NULL , do not pvcreate
*
*/
2011-02-18 17:47:28 +03:00
int vg_extend ( struct volume_group * vg , int pv_count , const char * const * pv_names ,
2009-10-06 00:02:48 +04:00
struct pvcreate_params * pp )
2001-10-16 02:04:27 +04:00
{
int i ;
2011-02-18 17:47:28 +03:00
char * pv_name ;
2013-12-12 14:26:35 +04:00
unsigned int max_phys_block_size = 0 ;
2009-07-11 00:09:21 +04:00
2009-07-14 06:14:04 +04:00
if ( _vg_bad_status_bits ( vg , RESIZEABLE_VG ) )
2012-03-14 00:36:02 +04:00
return_0 ;
2009-07-14 06:14:04 +04:00
2001-10-16 02:04:27 +04:00
/* attach each pv */
2007-07-12 09:04:42 +04:00
for ( i = 0 ; i < pv_count ; i + + ) {
2011-02-18 17:47:28 +03:00
if ( ! ( pv_name = dm_strdup ( pv_names [ i ] ) ) ) {
log_error ( " Failed to duplicate pv name %s. " , pv_names [ i ] ) ;
return 0 ;
}
2011-08-30 18:55:15 +04:00
dm_unescape_colons_and_at_signs ( pv_name , NULL , NULL ) ;
2013-12-12 14:26:35 +04:00
if ( ! vg_extend_single_pv ( vg , pv_name , pp , & max_phys_block_size ) ) {
2011-02-18 17:47:28 +03:00
log_error ( " Unable to add physical volume '%s' to "
" volume group '%s'. " , pv_name , vg - > name ) ;
dm_free ( pv_name ) ;
return 0 ;
}
dm_free ( pv_name ) ;
2007-07-12 09:04:42 +04:00
}
2001-10-16 02:04:27 +04:00
2002-11-18 17:04:08 +03:00
/* FIXME Decide whether to initialise and add new mdahs to format instance */
2001-10-16 02:04:27 +04:00
return 1 ;
}
2011-02-18 17:47:28 +03:00
int vg_reduce ( struct volume_group * vg , const char * pv_name )
2009-07-27 21:43:39 +04:00
{
struct physical_volume * pv ;
struct pv_list * pvl ;
if ( ! ( pvl = find_pv_in_vg ( vg , pv_name ) ) ) {
log_error ( " Physical volume %s not in volume group %s. " ,
pv_name , vg - > name ) ;
2013-09-04 03:07:43 +04:00
return 0 ;
2009-07-27 21:43:39 +04:00
}
pv = pvl - > pv ;
2013-09-04 03:07:43 +04:00
if ( vgreduce_single ( vg - > cmd , vg , pv , 0 ) ) {
dm_list_add ( & vg - > removed_pvs , & pvl - > list ) ;
return 1 ;
2009-07-27 21:43:39 +04:00
}
log_error ( " Unable to remove physical volume '%s' from "
2014-05-22 01:10:02 +04:00
" volume group '%s'. " , pv_name , vg - > name ) ;
2013-09-04 03:07:43 +04:00
2009-07-27 21:43:39 +04:00
return 0 ;
}
2010-02-24 21:15:49 +03:00
int lv_change_tag ( struct logical_volume * lv , const char * tag , int add_tag )
{
2010-02-24 21:15:57 +03:00
char * tag_new ;
2010-02-24 21:15:49 +03:00
if ( ! ( lv - > vg - > fid - > fmt - > features & FMT_TAGS ) ) {
log_error ( " Logical volume %s/%s does not support tags " ,
lv - > vg - > name , lv - > name ) ;
return 0 ;
}
if ( add_tag ) {
2010-02-24 21:15:57 +03:00
if ( ! ( tag_new = dm_pool_strdup ( lv - > vg - > vgmem , tag ) ) ) {
2010-07-09 20:57:44 +04:00
log_error ( " Failed to duplicate tag %s from %s/%s " ,
tag , lv - > vg - > name , lv - > name ) ;
return 0 ;
2010-02-24 21:15:57 +03:00
}
if ( ! str_list_add ( lv - > vg - > vgmem , & lv - > tags , tag_new ) ) {
2010-02-24 21:15:49 +03:00
log_error ( " Failed to add tag %s to %s/%s " ,
tag , lv - > vg - > name , lv - > name ) ;
return 0 ;
}
2012-02-08 16:52:58 +04:00
} else
str_list_del ( & lv - > tags , tag ) ;
2010-02-24 21:15:49 +03:00
return 1 ;
}
2010-02-24 21:15:05 +03:00
int vg_change_tag ( struct volume_group * vg , const char * tag , int add_tag )
{
2010-02-24 21:15:57 +03:00
char * tag_new ;
2010-02-24 21:15:05 +03:00
if ( ! ( vg - > fid - > fmt - > features & FMT_TAGS ) ) {
log_error ( " Volume group %s does not support tags " , vg - > name ) ;
return 0 ;
}
if ( add_tag ) {
2010-02-24 21:15:57 +03:00
if ( ! ( tag_new = dm_pool_strdup ( vg - > vgmem , tag ) ) ) {
2010-07-09 20:57:44 +04:00
log_error ( " Failed to duplicate tag %s from %s " ,
tag , vg - > name ) ;
return 0 ;
2010-02-24 21:15:57 +03:00
}
if ( ! str_list_add ( vg - > vgmem , & vg - > tags , tag_new ) ) {
2010-02-24 21:15:05 +03:00
log_error ( " Failed to add tag %s to volume group %s " ,
tag , vg - > name ) ;
return 0 ;
}
2012-02-08 16:52:58 +04:00
} else
str_list_del ( & vg - > tags , tag ) ;
2010-02-24 21:15:05 +03:00
return 1 ;
}
2001-11-12 18:10:01 +03:00
const char * strip_dir ( const char * vg_name , const char * dev_dir )
2001-11-12 15:16:57 +03:00
{
2002-12-20 02:25:55 +03:00
size_t len = strlen ( dev_dir ) ;
2001-11-12 15:16:57 +03:00
if ( ! strncmp ( vg_name , dev_dir , len ) )
vg_name + = len ;
return vg_name ;
}
2014-09-19 16:51:41 +04:00
/*
* Validates major and minor numbers .
* On > 2.4 kernel we only support dynamic major number .
*/
int validate_major_minor ( const struct cmd_context * cmd ,
const struct format_type * fmt ,
int32_t major , int32_t minor )
{
int r = 1 ;
if ( ! strncmp ( cmd - > kernel_vsn , " 2.4. " , 4 ) | |
( fmt - > features & FMT_RESTRICTED_LVIDS ) ) {
if ( major < 0 | | major > 255 ) {
log_error ( " Major number %d outside range 0-255. " , major ) ;
r = 0 ;
}
if ( minor < 0 | | minor > 255 ) {
log_error ( " Minor number %d outside range 0-255. " , minor ) ;
r = 0 ;
}
} else {
/* 12 bits for major number */
if ( ( major ! = - 1 ) & &
( major ! = cmd - > dev_types - > device_mapper_major ) ) {
/* User supplied some major number */
if ( major < 0 | | major > 4095 ) {
log_error ( " Major number %d outside range 0-4095. " , major ) ;
r = 0 ;
} else
log_print_unless_silent ( " Ignoring supplied major %d number - "
" kernel assigns major numbers dynamically. " ,
major ) ;
}
/* 20 bits for minor number */
if ( minor < 0 | | minor > 1048575 ) {
log_error ( " Minor number %d outside range 0-1048575. " , minor ) ;
r = 0 ;
}
}
return r ;
}
2008-01-15 00:07:58 +03:00
/*
* Validate parameters to vg_create ( ) before calling .
2008-01-16 22:54:39 +03:00
* FIXME : Move inside vg_create library function .
* FIXME : Change vgcreate_params struct to individual gets / sets
2008-01-15 00:07:58 +03:00
*/
2009-11-01 23:05:17 +03:00
int vgcreate_params_validate ( struct cmd_context * cmd ,
struct vgcreate_params * vp )
2008-01-15 00:07:58 +03:00
{
2012-10-16 12:20:02 +04:00
if ( ! validate_new_vg_name ( cmd , vp - > vg_name ) )
return_0 ;
2008-01-15 00:07:58 +03:00
if ( vp - > alloc = = ALLOC_INHERIT ) {
log_error ( " Volume Group allocation policy cannot inherit "
" from anything " ) ;
2012-10-16 12:07:27 +04:00
return 0 ;
2008-01-15 00:07:58 +03:00
}
if ( ! vp - > extent_size ) {
log_error ( " Physical extent size may not be zero " ) ;
2012-10-16 12:07:27 +04:00
return 0 ;
2008-01-15 00:07:58 +03:00
}
if ( ! ( cmd - > fmt - > features & FMT_UNLIMITED_VOLS ) ) {
if ( ! vp - > max_lv )
vp - > max_lv = 255 ;
if ( ! vp - > max_pv )
vp - > max_pv = 255 ;
if ( vp - > max_lv > 255 | | vp - > max_pv > 255 ) {
log_error ( " Number of volumes may not exceed 255 " ) ;
2012-10-16 12:07:27 +04:00
return 0 ;
2008-01-15 00:07:58 +03:00
}
}
2012-10-16 12:07:27 +04:00
return 1 ;
2008-01-15 00:07:58 +03:00
}
2014-02-22 04:44:21 +04:00
/*
* Update content of precommitted VG
*
* TODO : Optimize in the future , since lvmetad needs similar
* config tree processing in lvmetad_vg_update ( ) .
*/
static int _vg_update_vg_precommitted ( struct volume_group * vg )
2013-03-17 19:27:44 +04:00
{
2014-02-22 04:44:21 +04:00
release_vg ( vg - > vg_precommitted ) ;
vg - > vg_precommitted = NULL ;
2013-03-17 19:27:44 +04:00
2014-03-01 01:40:00 +04:00
if ( vg - > cft_precommitted ) {
dm_config_destroy ( vg - > cft_precommitted ) ;
vg - > cft_precommitted = NULL ;
}
2013-03-18 18:46:00 +04:00
2014-03-01 01:40:00 +04:00
if ( ! ( vg - > cft_precommitted = export_vg_to_config_tree ( vg ) ) )
return_0 ;
2013-03-17 19:27:44 +04:00
2014-03-01 01:40:00 +04:00
if ( ! ( vg - > vg_precommitted = import_vg_from_config_tree ( vg - > cft_precommitted , vg - > fid ) ) ) {
dm_config_destroy ( vg - > cft_precommitted ) ;
vg - > cft_precommitted = NULL ;
return_0 ;
}
2013-03-18 18:46:00 +04:00
2014-03-01 01:40:00 +04:00
return 1 ;
2014-02-22 04:44:21 +04:00
}
2015-11-25 01:29:18 +03:00
static int _vg_update_vg_committed ( struct volume_group * vg )
2014-02-22 04:44:21 +04:00
{
if ( dm_pool_locked ( vg - > vgmem ) )
return 1 ;
2015-11-25 01:29:18 +03:00
if ( vg - > vg_committed | | is_orphan_vg ( vg - > name ) ) /* we already have it */
2014-02-22 04:44:21 +04:00
return 1 ;
if ( ! _vg_update_vg_precommitted ( vg ) )
2013-03-18 18:46:00 +04:00
return_0 ;
2015-11-25 01:29:18 +03:00
vg - > vg_committed = vg - > vg_precommitted ;
2014-02-22 04:44:21 +04:00
vg - > vg_precommitted = NULL ;
2014-03-01 01:40:00 +04:00
if ( vg - > cft_precommitted ) {
dm_config_destroy ( vg - > cft_precommitted ) ;
vg - > cft_precommitted = NULL ;
}
2014-02-22 04:44:21 +04:00
return 1 ;
2013-03-17 19:27:44 +04:00
}
2009-07-29 17:26:01 +04:00
/*
* Create a ( struct volume_group ) volume group handle from a struct volume_group pointer and a
* possible failure code or zero for success .
*/
static struct volume_group * _vg_make_handle ( struct cmd_context * cmd ,
2011-03-10 15:43:29 +03:00
struct volume_group * vg ,
uint32_t failure )
2009-07-29 17:26:01 +04:00
{
2013-03-17 19:27:44 +04:00
2011-08-11 21:24:23 +04:00
/* Never return a cached VG structure for a failure */
if ( vg & & vg - > vginfo & & failure ! = SUCCESS ) {
release_vg ( vg ) ;
vg = NULL ;
}
2011-03-10 15:43:29 +03:00
if ( ! vg & & ! ( vg = alloc_vg ( " vg_make_handle " , cmd , NULL ) ) )
return_NULL ;
2009-07-29 17:26:01 +04:00
2011-03-10 15:43:29 +03:00
if ( vg - > read_status ! = failure )
vg - > read_status = failure ;
2009-07-29 17:26:01 +04:00
2015-11-25 01:29:18 +03:00
if ( vg - > fid & & ! _vg_update_vg_committed ( vg ) )
2013-07-22 14:04:47 +04:00
vg - > read_status | = FAILED_ALLOCATION ;
2011-03-10 15:43:29 +03:00
return vg ;
2009-07-29 17:26:01 +04:00
}
2009-10-16 21:41:49 +04:00
int lv_has_unknown_segments ( const struct logical_volume * lv )
{
struct lv_segment * seg ;
/* foreach segment */
dm_list_iterate_items ( seg , & lv - > segments )
if ( seg_unknown ( seg ) )
return 1 ;
return 0 ;
}
int vg_has_unknown_segments ( const struct volume_group * vg )
{
struct lv_list * lvl ;
/* foreach LV */
dm_list_iterate_items ( lvl , & vg - > lvs )
if ( lv_has_unknown_segments ( lvl - > lv ) )
return 1 ;
return 0 ;
}
Change vg_create() to take only minimal parameters and obtain a lock.
vg_t *vg_create(struct cmd_context *cmd, const char *vg_name);
This is the first step towards the API called to create a VG.
Call vg_lock_newname() inside this function. Use _vg_make_handle()
where possible.
Now we have 2 ways to construct a volume group:
1) vg_read: Used when constructing an existing VG from disks
2) vg_create: Used when constructing a new VG
Both of these interfaces obtain a lock, and return a vg_t *.
The usage of _vg_make_handle() inside vg_create() doesn't fit
perfectly but it's ok for now. Needs some cleanup though and I've
noted "FIXME" in the code.
Add the new vg_create() plus vg 'set' functions for non-default
VG parameters in the following tools:
- vgcreate: Fairly straightforward refactoring. We just moved
vg_lock_newname inside vg_create so we check the return via
vg_read_error.
- vgsplit: The refactoring here is a bit more tricky. Originally
we called vg_lock_newname and depending on the error code, we either
read the existing vg or created the new one. Now vg_create()
calls vg_lock_newname, so we first try to create the VG. If this
fails with FAILED_EXIST, we can then do the vg_read. If the
create succeeds, we check the input parameters and set any new
values on the VG.
TODO in future patches:
1. The VG_ORPHAN lock needs some thought. We may want to treat
this as any other VG, and require the application to obtain a handle
and pass it to other API calls (for example, vg_extend). Or,
we may find that hiding the VG_ORPHAN lock inside other APIs is
the way to go. I thought of placing the VG_ORPHAN lock inside
vg_create() and tying it to the vg handle, but was not certain
this was the right approach.
2. Cleanup error paths. Integrate vg_read_error() with vg_create and
vg_read* error codes and/or the new error APIs.
Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
2009-07-09 14:09:33 +04:00
/*
* Create a VG with default parameters .
* Returns :
2009-07-29 17:26:01 +04:00
* - struct volume_group * with SUCCESS code : VG structure created
* - NULL or struct volume_group * with FAILED_ * code : error creating VG structure
Change vg_create() to take only minimal parameters and obtain a lock.
vg_t *vg_create(struct cmd_context *cmd, const char *vg_name);
This is the first step towards the API called to create a VG.
Call vg_lock_newname() inside this function. Use _vg_make_handle()
where possible.
Now we have 2 ways to construct a volume group:
1) vg_read: Used when constructing an existing VG from disks
2) vg_create: Used when constructing a new VG
Both of these interfaces obtain a lock, and return a vg_t *.
The usage of _vg_make_handle() inside vg_create() doesn't fit
perfectly but it's ok for now. Needs some cleanup though and I've
noted "FIXME" in the code.
Add the new vg_create() plus vg 'set' functions for non-default
VG parameters in the following tools:
- vgcreate: Fairly straightforward refactoring. We just moved
vg_lock_newname inside vg_create so we check the return via
vg_read_error.
- vgsplit: The refactoring here is a bit more tricky. Originally
we called vg_lock_newname and depending on the error code, we either
read the existing vg or created the new one. Now vg_create()
calls vg_lock_newname, so we first try to create the VG. If this
fails with FAILED_EXIST, we can then do the vg_read. If the
create succeeds, we check the input parameters and set any new
values on the VG.
TODO in future patches:
1. The VG_ORPHAN lock needs some thought. We may want to treat
this as any other VG, and require the application to obtain a handle
and pass it to other API calls (for example, vg_extend). Or,
we may find that hiding the VG_ORPHAN lock inside other APIs is
the way to go. I thought of placing the VG_ORPHAN lock inside
vg_create() and tying it to the vg handle, but was not certain
this was the right approach.
2. Cleanup error paths. Integrate vg_read_error() with vg_create and
vg_read* error codes and/or the new error APIs.
Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
2009-07-09 14:09:33 +04:00
* Use vg_read_error ( ) to determine success or failure .
* FIXME : cleanup usage of _vg_make_handle ( )
*/
2009-07-29 17:26:01 +04:00
struct volume_group * vg_create ( struct cmd_context * cmd , const char * vg_name )
2001-10-12 18:25:53 +04:00
{
2009-07-29 17:26:01 +04:00
struct volume_group * vg ;
2013-06-16 23:07:39 +04:00
struct format_instance_ctx fic = {
. type = FMT_INSTANCE_MDAS | FMT_INSTANCE_AUX_MDAS ,
. context . vg_ref . vg_name = vg_name
} ;
2011-03-11 17:50:13 +03:00
struct format_instance * fid ;
Change vg_create() to take only minimal parameters and obtain a lock.
vg_t *vg_create(struct cmd_context *cmd, const char *vg_name);
This is the first step towards the API called to create a VG.
Call vg_lock_newname() inside this function. Use _vg_make_handle()
where possible.
Now we have 2 ways to construct a volume group:
1) vg_read: Used when constructing an existing VG from disks
2) vg_create: Used when constructing a new VG
Both of these interfaces obtain a lock, and return a vg_t *.
The usage of _vg_make_handle() inside vg_create() doesn't fit
perfectly but it's ok for now. Needs some cleanup though and I've
noted "FIXME" in the code.
Add the new vg_create() plus vg 'set' functions for non-default
VG parameters in the following tools:
- vgcreate: Fairly straightforward refactoring. We just moved
vg_lock_newname inside vg_create so we check the return via
vg_read_error.
- vgsplit: The refactoring here is a bit more tricky. Originally
we called vg_lock_newname and depending on the error code, we either
read the existing vg or created the new one. Now vg_create()
calls vg_lock_newname, so we first try to create the VG. If this
fails with FAILED_EXIST, we can then do the vg_read. If the
create succeeds, we check the input parameters and set any new
values on the VG.
TODO in future patches:
1. The VG_ORPHAN lock needs some thought. We may want to treat
this as any other VG, and require the application to obtain a handle
and pass it to other API calls (for example, vg_extend). Or,
we may find that hiding the VG_ORPHAN lock inside other APIs is
the way to go. I thought of placing the VG_ORPHAN lock inside
vg_create() and tying it to the vg handle, but was not certain
this was the right approach.
2. Cleanup error paths. Integrate vg_read_error() with vg_create and
vg_read* error codes and/or the new error APIs.
Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
2009-07-09 14:09:33 +04:00
uint32_t rc ;
if ( ! validate_name ( vg_name ) ) {
log_error ( " Invalid vg name %s " , vg_name ) ;
/* FIXME: use _vg_make_handle() w/proper error code */
return NULL ;
}
rc = vg_lock_newname ( cmd , vg_name ) ;
if ( rc ! = SUCCESS )
/* NOTE: let caller decide - this may be check for existence */
return _vg_make_handle ( cmd , NULL , rc ) ;
2001-10-12 18:25:53 +04:00
2011-03-10 15:43:29 +03:00
/* Strip dev_dir if present */
vg_name = strip_dir ( vg_name , cmd - > dev_dir ) ;
2009-04-10 13:59:18 +04:00
2011-03-10 15:43:29 +03:00
if ( ! ( vg = alloc_vg ( " vg_create " , cmd , vg_name ) ) )
2009-04-10 13:59:18 +04:00
goto_bad ;
2001-10-12 18:25:53 +04:00
if ( ! id_create ( & vg - > id ) ) {
2009-07-16 00:02:46 +04:00
log_error ( " Couldn't create uuid for volume group '%s'. " ,
vg_name ) ;
2001-10-12 18:25:53 +04:00
goto bad ;
}
2002-01-11 02:21:07 +03:00
vg - > status = ( RESIZEABLE_VG | LVM_READ | LVM_WRITE ) ;
2015-02-24 02:03:52 +03:00
vg - > system_id = NULL ;
if ( ! ( vg - > lvm1_system_id = dm_pool_zalloc ( vg - > vgmem , NAME_LEN + 1 ) ) )
2007-04-26 20:44:59 +04:00
goto_bad ;
Change vg_create() to take only minimal parameters and obtain a lock.
vg_t *vg_create(struct cmd_context *cmd, const char *vg_name);
This is the first step towards the API called to create a VG.
Call vg_lock_newname() inside this function. Use _vg_make_handle()
where possible.
Now we have 2 ways to construct a volume group:
1) vg_read: Used when constructing an existing VG from disks
2) vg_create: Used when constructing a new VG
Both of these interfaces obtain a lock, and return a vg_t *.
The usage of _vg_make_handle() inside vg_create() doesn't fit
perfectly but it's ok for now. Needs some cleanup though and I've
noted "FIXME" in the code.
Add the new vg_create() plus vg 'set' functions for non-default
VG parameters in the following tools:
- vgcreate: Fairly straightforward refactoring. We just moved
vg_lock_newname inside vg_create so we check the return via
vg_read_error.
- vgsplit: The refactoring here is a bit more tricky. Originally
we called vg_lock_newname and depending on the error code, we either
read the existing vg or created the new one. Now vg_create()
calls vg_lock_newname, so we first try to create the VG. If this
fails with FAILED_EXIST, we can then do the vg_read. If the
create succeeds, we check the input parameters and set any new
values on the VG.
TODO in future patches:
1. The VG_ORPHAN lock needs some thought. We may want to treat
this as any other VG, and require the application to obtain a handle
and pass it to other API calls (for example, vg_extend). Or,
we may find that hiding the VG_ORPHAN lock inside other APIs is
the way to go. I thought of placing the VG_ORPHAN lock inside
vg_create() and tying it to the vg handle, but was not certain
this was the right approach.
2. Cleanup error paths. Integrate vg_read_error() with vg_create and
vg_read* error codes and/or the new error APIs.
Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
2009-07-09 14:09:33 +04:00
vg - > extent_size = DEFAULT_EXTENT_SIZE * 2 ;
vg - > max_lv = DEFAULT_MAX_LV ;
vg - > max_pv = DEFAULT_MAX_PV ;
vg - > alloc = DEFAULT_ALLOC_POLICY ;
2010-06-29 00:36:37 +04:00
vg - > mda_copies = DEFAULT_VGMETADATACOPIES ;
2004-05-19 02:12:53 +04:00
2011-03-11 17:50:13 +03:00
if ( ! ( fid = cmd - > fmt - > ops - > create_instance ( cmd - > fmt , & fic ) ) ) {
2002-04-24 22:20:51 +04:00
log_error ( " Failed to create format instance " ) ;
goto bad ;
}
2011-03-11 17:50:13 +03:00
vg_set_fid ( vg , fid ) ;
2002-04-24 22:20:51 +04:00
2003-08-27 01:12:06 +04:00
if ( vg - > fid - > fmt - > ops - > vg_setup & &
! vg - > fid - > fmt - > ops - > vg_setup ( vg - > fid , vg ) ) {
2001-10-15 22:39:40 +04:00
log_error ( " Format specific setup of volume group '%s' failed. " ,
vg_name ) ;
2001-10-12 18:25:53 +04:00
goto bad ;
}
Change vg_create() to take only minimal parameters and obtain a lock.
vg_t *vg_create(struct cmd_context *cmd, const char *vg_name);
This is the first step towards the API called to create a VG.
Call vg_lock_newname() inside this function. Use _vg_make_handle()
where possible.
Now we have 2 ways to construct a volume group:
1) vg_read: Used when constructing an existing VG from disks
2) vg_create: Used when constructing a new VG
Both of these interfaces obtain a lock, and return a vg_t *.
The usage of _vg_make_handle() inside vg_create() doesn't fit
perfectly but it's ok for now. Needs some cleanup though and I've
noted "FIXME" in the code.
Add the new vg_create() plus vg 'set' functions for non-default
VG parameters in the following tools:
- vgcreate: Fairly straightforward refactoring. We just moved
vg_lock_newname inside vg_create so we check the return via
vg_read_error.
- vgsplit: The refactoring here is a bit more tricky. Originally
we called vg_lock_newname and depending on the error code, we either
read the existing vg or created the new one. Now vg_create()
calls vg_lock_newname, so we first try to create the VG. If this
fails with FAILED_EXIST, we can then do the vg_read. If the
create succeeds, we check the input parameters and set any new
values on the VG.
TODO in future patches:
1. The VG_ORPHAN lock needs some thought. We may want to treat
this as any other VG, and require the application to obtain a handle
and pass it to other API calls (for example, vg_extend). Or,
we may find that hiding the VG_ORPHAN lock inside other APIs is
the way to go. I thought of placing the VG_ORPHAN lock inside
vg_create() and tying it to the vg handle, but was not certain
this was the right approach.
2. Cleanup error paths. Integrate vg_read_error() with vg_create and
vg_read* error codes and/or the new error APIs.
Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
2009-07-09 14:09:33 +04:00
return _vg_make_handle ( cmd , vg , SUCCESS ) ;
2001-10-12 18:25:53 +04:00
Change vg_create() to take only minimal parameters and obtain a lock.
vg_t *vg_create(struct cmd_context *cmd, const char *vg_name);
This is the first step towards the API called to create a VG.
Call vg_lock_newname() inside this function. Use _vg_make_handle()
where possible.
Now we have 2 ways to construct a volume group:
1) vg_read: Used when constructing an existing VG from disks
2) vg_create: Used when constructing a new VG
Both of these interfaces obtain a lock, and return a vg_t *.
The usage of _vg_make_handle() inside vg_create() doesn't fit
perfectly but it's ok for now. Needs some cleanup though and I've
noted "FIXME" in the code.
Add the new vg_create() plus vg 'set' functions for non-default
VG parameters in the following tools:
- vgcreate: Fairly straightforward refactoring. We just moved
vg_lock_newname inside vg_create so we check the return via
vg_read_error.
- vgsplit: The refactoring here is a bit more tricky. Originally
we called vg_lock_newname and depending on the error code, we either
read the existing vg or created the new one. Now vg_create()
calls vg_lock_newname, so we first try to create the VG. If this
fails with FAILED_EXIST, we can then do the vg_read. If the
create succeeds, we check the input parameters and set any new
values on the VG.
TODO in future patches:
1. The VG_ORPHAN lock needs some thought. We may want to treat
this as any other VG, and require the application to obtain a handle
and pass it to other API calls (for example, vg_extend). Or,
we may find that hiding the VG_ORPHAN lock inside other APIs is
the way to go. I thought of placing the VG_ORPHAN lock inside
vg_create() and tying it to the vg handle, but was not certain
this was the right approach.
2. Cleanup error paths. Integrate vg_read_error() with vg_create and
vg_read* error codes and/or the new error APIs.
Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
2009-07-09 14:09:33 +04:00
bad :
2011-08-11 00:25:29 +04:00
unlock_and_release_vg ( cmd , vg , vg_name ) ;
Change vg_create() to take only minimal parameters and obtain a lock.
vg_t *vg_create(struct cmd_context *cmd, const char *vg_name);
This is the first step towards the API called to create a VG.
Call vg_lock_newname() inside this function. Use _vg_make_handle()
where possible.
Now we have 2 ways to construct a volume group:
1) vg_read: Used when constructing an existing VG from disks
2) vg_create: Used when constructing a new VG
Both of these interfaces obtain a lock, and return a vg_t *.
The usage of _vg_make_handle() inside vg_create() doesn't fit
perfectly but it's ok for now. Needs some cleanup though and I've
noted "FIXME" in the code.
Add the new vg_create() plus vg 'set' functions for non-default
VG parameters in the following tools:
- vgcreate: Fairly straightforward refactoring. We just moved
vg_lock_newname inside vg_create so we check the return via
vg_read_error.
- vgsplit: The refactoring here is a bit more tricky. Originally
we called vg_lock_newname and depending on the error code, we either
read the existing vg or created the new one. Now vg_create()
calls vg_lock_newname, so we first try to create the VG. If this
fails with FAILED_EXIST, we can then do the vg_read. If the
create succeeds, we check the input parameters and set any new
values on the VG.
TODO in future patches:
1. The VG_ORPHAN lock needs some thought. We may want to treat
this as any other VG, and require the application to obtain a handle
and pass it to other API calls (for example, vg_extend). Or,
we may find that hiding the VG_ORPHAN lock inside other APIs is
the way to go. I thought of placing the VG_ORPHAN lock inside
vg_create() and tying it to the vg handle, but was not certain
this was the right approach.
2. Cleanup error paths. Integrate vg_read_error() with vg_create and
vg_read* error codes and/or the new error APIs.
Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
2009-07-09 14:09:33 +04:00
/* FIXME: use _vg_make_handle() w/proper error code */
2001-10-12 18:25:53 +04:00
return NULL ;
}
2014-10-30 13:38:49 +03:00
/* Rounds up by default */
uint32_t extents_from_size ( struct cmd_context * cmd , uint64_t size ,
2009-07-26 06:34:09 +04:00
uint32_t extent_size )
{
if ( size % extent_size ) {
size + = extent_size - size % extent_size ;
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 ( " Rounding up size to full physical extent %s " ,
display_size ( cmd , size ) ) ;
2009-07-26 06:34:09 +04:00
}
2011-11-05 02:49:53 +04:00
if ( size > ( uint64_t ) MAX_EXTENT_COUNT * extent_size ) {
2009-07-26 06:34:09 +04:00
log_error ( " Volume too large (%s) for extent size %s. "
" Upper limit is %s. " ,
display_size ( cmd , size ) ,
display_size ( cmd , ( uint64_t ) extent_size ) ,
2011-11-05 02:49:53 +04:00
display_size ( cmd , ( uint64_t ) MAX_EXTENT_COUNT *
2009-07-26 06:34:09 +04:00
extent_size ) ) ;
return 0 ;
}
2014-10-30 13:38:49 +03:00
return ( uint32_t ) ( size / extent_size ) ;
2009-07-26 06:34:09 +04:00
}
2014-10-31 01:43:12 +03:00
/*
* Converts size according to percentage with specified rounding to extents
*
* For PERCENT_NONE size is in standard sector units .
* For all other percent type is in DM_PERCENT_1 base unit ( supports decimal point )
*
* Return value of 0 extents is an error .
*/
uint32_t extents_from_percent_size ( struct volume_group * vg , const struct dm_list * pvh ,
uint32_t extents , int roundup ,
percent_type_t percent , uint64_t size )
{
uint32_t count ;
switch ( percent ) {
case PERCENT_NONE :
if ( ! roundup & & ( size % vg - > extent_size ) ) {
if ( ! ( size - = size % vg - > extent_size ) ) {
log_error ( " Specified size is smaller then physical extent boundary. " ) ;
return 0 ;
}
log_print_unless_silent ( " Rounding size to boundary between physical extents: %s. " ,
display_size ( vg - > cmd , size ) ) ;
}
return extents_from_size ( vg - > cmd , size , vg - > extent_size ) ;
case PERCENT_LV :
break ; /* Base extents already passed in. */
case PERCENT_VG :
extents = vg - > extent_count ;
break ;
case PERCENT_PVS :
if ( pvh ! = & vg - > pvs ) {
/* Physical volumes are specified on cmdline */
if ( ! ( extents = pv_list_extents_free ( pvh ) ) ) {
log_error ( " No free extents in the list of physical volumes. " ) ;
return 0 ;
}
break ;
}
/* Fall back to use all PVs in VG like %FREE */
case PERCENT_FREE :
if ( ! ( extents = vg - > free_count ) ) {
log_error ( " No free extents in Volume group %s. " , vg - > name ) ;
return 0 ;
}
break ;
default :
log_error ( INTERNAL_ERROR " Unsupported percent type %u. " , percent ) ;
return 0 ;
}
if ( ! ( count = percent_of_extents ( size , extents , roundup ) ) )
log_error ( " Converted %.2f%%%s into 0 extents. " ,
( double ) size / DM_PERCENT_1 , get_percent_string ( percent ) ) ;
else
log_verbose ( " Converted %.2f%%%s into % " PRIu32 " extents. " ,
( double ) size / DM_PERCENT_1 , get_percent_string ( percent ) , count ) ;
return count ;
}
2010-07-06 02:23:15 +04:00
static dm_bitset_t _bitset_with_random_bits ( struct dm_pool * mem , uint32_t num_bits ,
uint32_t num_set_bits , unsigned * seed )
{
dm_bitset_t bs ;
unsigned bit_selected ;
char buf [ 32 ] ;
uint32_t i = num_bits - num_set_bits ;
if ( ! ( bs = dm_bitset_create ( mem , ( unsigned ) num_bits ) ) ) {
log_error ( " Failed to allocate bitset for setting random bits. " ) ;
return NULL ;
}
if ( ! dm_pool_begin_object ( mem , 512 ) ) {
log_error ( " dm_pool_begin_object failed for random list of bits. " ) ;
dm_pool_free ( mem , bs ) ;
return NULL ;
}
/* Perform loop num_set_bits times, selecting one bit each time */
while ( i + + < num_bits ) {
/* Select a random bit between 0 and (i-1) inclusive. */
2014-04-04 04:26:19 +04:00
bit_selected = lvm_even_rand ( seed , i ) ;
2010-07-06 02:23:15 +04:00
/*
* If the bit was already set , set the new bit that became
* choosable for the first time during this pass .
* This maintains a uniform probability distribution by compensating
* for being unable to select it until this pass .
*/
if ( dm_bit ( bs , bit_selected ) )
bit_selected = i - 1 ;
dm_bit_set ( bs , bit_selected ) ;
if ( dm_snprintf ( buf , sizeof ( buf ) , " %u " , bit_selected ) < 0 ) {
log_error ( " snprintf random bit failed. " ) ;
dm_pool_free ( mem , bs ) ;
return NULL ;
}
if ( ! dm_pool_grow_object ( mem , buf , strlen ( buf ) ) ) {
log_error ( " Failed to generate list of random bits. " ) ;
dm_pool_free ( mem , bs ) ;
return NULL ;
}
}
2011-03-14 20:00:57 +03:00
if ( ! dm_pool_grow_object ( mem , " \0 " , 1 ) ) {
log_error ( " Failed to finish list of random bits. " ) ;
dm_pool_free ( mem , bs ) ;
return NULL ;
}
2013-01-08 02:30:29 +04:00
log_debug_metadata ( " Selected % " PRIu32 " random bits from % " PRIu32 " : %s " , num_set_bits , num_bits , ( char * ) dm_pool_end_object ( mem ) ) ;
2010-07-06 02:23:15 +04:00
return bs ;
}
2010-06-29 00:37:54 +04:00
static int _vg_ignore_mdas ( struct volume_group * vg , uint32_t num_to_ignore )
{
struct metadata_area * mda ;
2010-07-06 02:23:15 +04:00
uint32_t mda_used_count = vg_mda_used_count ( vg ) ;
dm_bitset_t mda_to_ignore_bs ;
int r = 1 ;
2010-06-29 00:37:54 +04:00
2013-01-08 02:30:29 +04:00
log_debug_metadata ( " Adjusting ignored mdas for %s: % " PRIu32 " of % " PRIu32 " mdas in use "
" but % " PRIu32 " required. Changing % " PRIu32 " mda. " ,
vg - > name , mda_used_count , vg_mda_count ( vg ) , vg_mda_copies ( vg ) , num_to_ignore ) ;
2010-06-30 17:51:11 +04:00
2010-06-29 00:37:54 +04:00
if ( ! num_to_ignore )
return 1 ;
2010-06-30 17:51:11 +04:00
2010-07-06 02:23:15 +04:00
if ( ! ( mda_to_ignore_bs = _bitset_with_random_bits ( vg - > vgmem , mda_used_count ,
num_to_ignore , & vg - > cmd - > rand_seed ) ) )
return_0 ;
2010-06-30 23:28:35 +04:00
dm_list_iterate_items ( mda , & vg - > fid - > metadata_areas_in_use )
2010-07-06 02:23:15 +04:00
if ( ! mda_is_ignored ( mda ) & & ( - - mda_used_count ,
dm_bit ( mda_to_ignore_bs , mda_used_count ) ) ) {
2010-06-29 00:37:54 +04:00
mda_set_ignored ( mda , 1 ) ;
2010-06-30 23:28:35 +04:00
if ( ! - - num_to_ignore )
2010-07-06 02:23:15 +04:00
goto out ;
2010-06-29 00:37:54 +04:00
}
2010-06-30 17:51:11 +04:00
log_error ( INTERNAL_ERROR " Unable to find % " PRIu32 " metadata areas to ignore "
2010-06-29 00:37:54 +04:00
" on volume group %s " , num_to_ignore , vg - > name ) ;
2010-06-30 17:51:11 +04:00
2010-07-06 02:23:15 +04:00
r = 0 ;
out :
dm_pool_free ( vg - > vgmem , mda_to_ignore_bs ) ;
return r ;
2010-06-29 00:37:54 +04:00
}
static int _vg_unignore_mdas ( struct volume_group * vg , uint32_t num_to_unignore )
{
struct metadata_area * mda , * tmda ;
2010-07-06 02:23:15 +04:00
uint32_t mda_used_count = vg_mda_used_count ( vg ) ;
uint32_t mda_count = vg_mda_count ( vg ) ;
uint32_t mda_free_count = mda_count - mda_used_count ;
dm_bitset_t mda_to_unignore_bs ;
int r = 1 ;
2010-06-29 00:37:54 +04:00
if ( ! num_to_unignore )
return 1 ;
2010-06-30 17:51:11 +04:00
2013-01-08 02:30:29 +04:00
log_debug_metadata ( " Adjusting ignored mdas for %s: % " PRIu32 " of % " PRIu32 " mdas in use "
" but % " PRIu32 " required. Changing % " PRIu32 " mda. " ,
vg - > name , mda_used_count , mda_count , vg_mda_copies ( vg ) , num_to_unignore ) ;
2010-07-06 02:23:15 +04:00
if ( ! ( mda_to_unignore_bs = _bitset_with_random_bits ( vg - > vgmem , mda_free_count ,
num_to_unignore , & vg - > cmd - > rand_seed ) ) )
return_0 ;
2010-06-30 17:51:11 +04:00
2010-06-30 23:28:35 +04:00
dm_list_iterate_items_safe ( mda , tmda , & vg - > fid - > metadata_areas_ignored )
2010-07-06 02:23:15 +04:00
if ( mda_is_ignored ( mda ) & & ( - - mda_free_count ,
dm_bit ( mda_to_unignore_bs , mda_free_count ) ) ) {
2010-06-29 00:37:54 +04:00
mda_set_ignored ( mda , 0 ) ;
dm_list_move ( & vg - > fid - > metadata_areas_in_use ,
& mda - > list ) ;
2010-06-30 23:28:35 +04:00
if ( ! - - num_to_unignore )
2010-07-06 02:23:15 +04:00
goto out ;
2010-06-29 00:37:54 +04:00
}
2010-06-30 17:51:11 +04:00
2010-06-30 23:28:35 +04:00
dm_list_iterate_items ( mda , & vg - > fid - > metadata_areas_in_use )
2010-07-07 00:09:38 +04:00
if ( mda_is_ignored ( mda ) & & ( - - mda_free_count ,
dm_bit ( mda_to_unignore_bs , mda_free_count ) ) ) {
2010-06-29 00:37:54 +04:00
mda_set_ignored ( mda , 0 ) ;
2010-06-30 23:28:35 +04:00
if ( ! - - num_to_unignore )
2010-07-06 02:23:15 +04:00
goto out ;
2010-06-29 00:37:54 +04:00
}
2010-06-30 17:51:11 +04:00
log_error ( INTERNAL_ERROR " Unable to find % " PRIu32 " metadata areas to unignore "
" on volume group %s " , num_to_unignore , vg - > name ) ;
2010-07-06 02:23:15 +04:00
r = 0 ;
out :
dm_pool_free ( vg - > vgmem , mda_to_unignore_bs ) ;
return r ;
2010-06-29 00:37:54 +04:00
}
static int _vg_adjust_ignored_mdas ( struct volume_group * vg )
{
2010-06-30 23:28:35 +04:00
uint32_t mda_copies_used = vg_mda_used_count ( vg ) ;
2010-06-29 00:37:54 +04:00
2010-06-30 23:28:35 +04:00
if ( vg - > mda_copies = = VGMETADATACOPIES_UNMANAGED ) {
/* Ensure at least one mda is in use. */
if ( ! mda_copies_used & & vg_mda_count ( vg ) & & ! _vg_unignore_mdas ( vg , 1 ) )
return_0 ;
2010-06-29 00:37:54 +04:00
else
2010-06-30 23:28:35 +04:00
return 1 ;
2010-06-29 00:37:54 +04:00
}
2010-06-30 17:51:11 +04:00
2010-06-30 23:28:35 +04:00
/* Not an error to have vg_mda_count larger than total mdas. */
if ( vg - > mda_copies = = VGMETADATACOPIES_ALL | |
vg - > mda_copies > = vg_mda_count ( vg ) ) {
/* Use all */
if ( ! _vg_unignore_mdas ( vg , vg_mda_count ( vg ) - mda_copies_used ) )
return_0 ;
} else if ( mda_copies_used < vg - > mda_copies ) {
if ( ! _vg_unignore_mdas ( vg , vg - > mda_copies - mda_copies_used ) )
return_0 ;
} else if ( mda_copies_used > vg - > mda_copies )
if ( ! _vg_ignore_mdas ( vg , mda_copies_used - vg - > mda_copies ) )
return_0 ;
Allow 'all' and 'unmanaged' values for --vgmetadatacopies.
Allowing an 'all' and 'unmanaged' value is more intuitive, and
provides a simple way for users to get back to original LVM behavior
of metadata written to all PVs in the volume group.
If the user requests "--vgmetadatacopies unmanaged", this instructs
LVM not to manage the ignore bits to achieve a specific number of
metadata copies in the volume group. The user is free to use
"pvchange --metadataignore" to control the mdas on a per-PV basis.
If the user requests "--vgmetadatacopies all", this instructs LVM
to do 2 things: 1) clear all ignore bits, and 2) set the "unmanaged"
policy going forward.
Internally, we use the special MAX_UINT32 value to indicate 'all'.
This 'just' works since it's the largest value possible for the
field and so all 'ignore' bits on all mdas in the VG will get
cleared inside _vg_metadata_balance(). However, after we've
called the _vg_metadata_balance function, we check for the special
'all' value, and if set, we write the "unmanaged" value into the
metadata. As such, the 'all' value is never written to disk.
Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
2010-06-29 00:40:01 +04:00
/*
* The VGMETADATACOPIES_ALL value will never be written disk .
* It is a special cmdline value that means 2 things :
* 1. clear all ignore bits in all mdas in this vg
* 2. set the " unmanaged " policy going forward for metadata balancing
*/
if ( vg - > mda_copies = = VGMETADATACOPIES_ALL )
vg - > mda_copies = VGMETADATACOPIES_UNMANAGED ;
2010-06-30 17:51:11 +04:00
return 1 ;
2010-06-29 00:37:54 +04:00
}
2010-08-20 16:43:49 +04:00
uint64_t find_min_mda_size ( struct dm_list * mdas )
{
uint64_t min_mda_size = UINT64_MAX , mda_size ;
struct metadata_area * mda ;
dm_list_iterate_items ( mda , mdas ) {
if ( ! mda - > ops - > mda_total_sectors )
continue ;
mda_size = mda - > ops - > mda_total_sectors ( mda ) ;
if ( mda_size < min_mda_size )
min_mda_size = mda_size ;
}
if ( min_mda_size = = UINT64_MAX )
min_mda_size = UINT64_C ( 0 ) ;
return min_mda_size ;
}
2010-06-29 00:38:56 +04:00
static int _move_mdas ( struct volume_group * vg_from , struct volume_group * vg_to ,
struct dm_list * mdas_from , struct dm_list * mdas_to )
2007-03-23 15:43:17 +03:00
{
struct metadata_area * mda , * mda2 ;
int common_mda = 0 ;
2008-11-04 01:14:30 +03:00
dm_list_iterate_items_safe ( mda , mda2 , mdas_from ) {
2007-03-23 15:43:17 +03:00
if ( ! mda - > ops - > mda_in_vg ) {
common_mda = 1 ;
continue ;
}
2008-04-08 16:49:21 +04:00
if ( ! mda - > ops - > mda_in_vg ( vg_from - > fid , vg_from , mda ) ) {
if ( is_orphan_vg ( vg_to - > name ) )
2008-11-04 01:14:30 +03:00
dm_list_del ( & mda - > list ) ;
2008-04-08 16:49:21 +04:00
else
2008-11-04 01:14:30 +03:00
dm_list_move ( mdas_to , & mda - > list ) ;
2008-04-08 16:49:21 +04:00
}
2007-03-23 15:43:17 +03:00
}
2010-06-29 00:38:56 +04:00
return common_mda ;
}
/*
* Separate metadata areas after splitting a VG .
* Also accepts orphan VG as destination ( for vgreduce ) .
*/
2010-07-09 19:34:40 +04:00
int vg_split_mdas ( struct cmd_context * cmd __attribute__ ( ( unused ) ) ,
2010-06-29 00:38:56 +04:00
struct volume_group * vg_from , struct volume_group * vg_to )
{
struct dm_list * mdas_from_in_use , * mdas_to_in_use ;
struct dm_list * mdas_from_ignored , * mdas_to_ignored ;
int common_mda = 0 ;
2007-03-23 15:43:17 +03:00
2010-06-29 00:38:56 +04:00
mdas_from_in_use = & vg_from - > fid - > metadata_areas_in_use ;
mdas_from_ignored = & vg_from - > fid - > metadata_areas_ignored ;
mdas_to_in_use = & vg_to - > fid - > metadata_areas_in_use ;
mdas_to_ignored = & vg_to - > fid - > metadata_areas_ignored ;
common_mda = _move_mdas ( vg_from , vg_to ,
mdas_from_in_use , mdas_to_in_use ) ;
common_mda = _move_mdas ( vg_from , vg_to ,
mdas_from_ignored , mdas_to_ignored ) ;
if ( ( dm_list_empty ( mdas_from_in_use ) & &
dm_list_empty ( mdas_from_ignored ) ) | |
( ( ! is_orphan_vg ( vg_to - > name ) & &
dm_list_empty ( mdas_to_in_use ) & &
dm_list_empty ( mdas_to_ignored ) ) ) )
2007-03-23 15:43:17 +03:00
return common_mda ;
return 1 ;
}
2009-07-26 05:53:09 +04:00
/*
* See if we may pvcreate on this device .
* 0 indicates we may not .
*/
2014-08-14 04:30:01 +04:00
static int _pvcreate_check ( struct cmd_context * cmd , const char * name ,
2015-02-19 16:30:20 +03:00
struct pvcreate_params * pp , int * wiped )
2009-07-26 05:53:09 +04:00
{
struct physical_volume * pv ;
struct device * dev ;
2014-08-14 04:30:01 +04:00
int r = 0 ;
int scan_needed = 0 ;
int filter_refresh_needed = 0 ;
2015-02-16 16:43:03 +03:00
2009-07-26 05:53:09 +04:00
/* FIXME Check partition type is LVM unless --force is given */
2015-02-19 16:30:20 +03:00
* wiped = 0 ;
2009-07-26 05:53:09 +04:00
/* Is there a pv here already? */
2014-02-18 12:59:21 +04:00
pv = find_pv_by_name ( cmd , name , 1 , 1 ) ;
2009-07-26 05:53:09 +04:00
/* Allow partial & exported VGs to be destroyed. */
/* We must have -ff to overwrite a non orphan */
if ( pv & & ! is_orphan ( pv ) & & pp - > force ! = DONT_PROMPT_OVERRIDE ) {
log_error ( " Can't initialize physical volume \" %s \" of "
" volume group \" %s \" without -ff " , name , pv_vg_name ( pv ) ) ;
2014-08-14 04:30:01 +04:00
goto out ;
2009-07-26 05:53:09 +04:00
}
/* prompt */
if ( pv & & ! is_orphan ( pv ) & & ! pp - > yes & &
2013-11-24 21:59:46 +04:00
yes_no_prompt ( " Really INITIALIZE physical volume \" %s \" of volume group \" %s \" [y/n]? " ,
name , pv_vg_name ( pv ) ) = = ' n ' ) {
2009-12-03 22:18:33 +03:00
log_error ( " %s: physical volume not initialized " , name ) ;
2014-08-14 04:30:01 +04:00
goto out ;
2009-07-26 05:53:09 +04:00
}
if ( sigint_caught ( ) )
2014-08-14 04:30:01 +04:00
goto_out ;
2009-07-26 05:53:09 +04:00
2014-10-02 14:00:57 +04:00
dev = dev_cache_get ( name , cmd - > full_filter ) ;
2009-07-26 05:53:09 +04:00
2015-02-19 16:30:20 +03:00
/*
* Refresh + rescan at the end is needed if :
* - we don ' t obtain device list from udev ,
* hence persistent cache file is used
* and we need to trash it and reevaluate
* for any changes done outside - adding
* any new foreign signature which may affect
* filtering - before we do pvcreate , we
* need to be sure that we have up - to - date
* view for filters
*
* - we have wiped existing foreign signatures
* from dev as this may affect what ' s filtered
* as well
*
*
* Only rescan at the end is needed if :
* - we ' ve just checked whether dev is fileterd
* by MD filter . We do the refresh in - situ ,
* so no need to require the refresh at the
* end of this fn . This is to allow for
* wiping MD signature during pvcreate for
* the dev - the dev would normally be
* filtered because of MD filter .
* This is an exception .
*/
2009-07-26 05:53:09 +04:00
/* Is there an md superblock here? */
if ( ! dev & & md_filtering ( ) ) {
2011-10-11 13:09:00 +04:00
if ( ! refresh_filters ( cmd ) )
2014-08-14 04:30:01 +04:00
goto_out ;
2011-10-11 13:09:00 +04:00
2009-07-26 05:53:09 +04:00
init_md_filtering ( 0 ) ;
2014-10-02 14:00:57 +04:00
dev = dev_cache_get ( name , cmd - > full_filter ) ;
2009-07-26 05:53:09 +04:00
init_md_filtering ( 1 ) ;
2014-08-14 04:30:01 +04:00
scan_needed = 1 ;
2015-02-19 16:30:20 +03:00
} else if ( ! obtain_device_list_from_udev ( ) )
filter_refresh_needed = scan_needed = 1 ;
2009-07-26 05:53:09 +04:00
if ( ! dev ) {
log_error ( " Device %s not found (or ignored by filtering). " , name ) ;
2014-08-14 04:30:01 +04:00
goto out ;
2009-07-26 05:53:09 +04:00
}
/*
* This test will fail if the device belongs to an MD array .
*/
if ( ! dev_test_excl ( dev ) ) {
/* FIXME Detect whether device-mapper itself is still using it */
log_error ( " Can't open %s exclusively. Mounted filesystem? " ,
name ) ;
2014-08-14 04:30:01 +04:00
goto out ;
2009-07-26 05:53:09 +04:00
}
2014-02-10 16:28:13 +04:00
if ( ! wipe_known_signatures ( cmd , dev , name ,
TYPE_LVM1_MEMBER | TYPE_LVM2_MEMBER ,
2015-02-19 16:30:20 +03:00
0 , pp - > yes , pp - > force , wiped ) ) {
2013-11-06 18:09:29 +04:00
log_error ( " Aborting pvcreate on %s. " , name ) ;
2014-08-14 04:30:01 +04:00
goto out ;
2015-02-17 11:46:34 +03:00
}
2015-05-07 13:10:20 +03:00
if ( * wiped )
2014-08-14 04:30:01 +04:00
filter_refresh_needed = scan_needed = 1 ;
2010-08-20 03:08:18 +04:00
2009-07-26 05:53:09 +04:00
if ( sigint_caught ( ) )
2014-08-14 04:30:01 +04:00
goto_out ;
2009-07-26 05:53:09 +04:00
2014-08-14 04:30:01 +04:00
if ( pv & & ! is_orphan ( pv ) & & pp - > force )
2009-07-26 05:53:09 +04:00
log_warn ( " WARNING: Forcing physical volume creation on "
" %s%s%s%s " , name ,
! is_orphan ( pv ) ? " of volume group \" " : " " ,
2014-03-19 03:01:00 +04:00
pv_vg_name ( pv ) ,
2009-07-26 05:53:09 +04:00
! is_orphan ( pv ) ? " \" " : " " ) ;
2014-08-14 04:30:01 +04:00
r = 1 ;
out :
if ( filter_refresh_needed )
2014-08-19 16:25:02 +04:00
if ( ! refresh_filters ( cmd ) ) {
stack ;
r = 0 ;
}
2014-08-14 04:30:01 +04:00
2015-02-19 16:30:20 +03:00
if ( scan_needed ) {
2015-12-01 23:09:01 +03:00
lvmcache_force_next_label_scan ( ) ;
if ( ! lvmcache_label_scan ( cmd ) ) {
2014-08-19 16:25:02 +04:00
stack ;
r = 0 ;
}
2015-02-19 16:30:20 +03:00
}
2011-03-11 17:56:56 +03:00
free_pv_fid ( pv ) ;
2014-08-14 04:30:01 +04:00
return r ;
2009-07-26 05:53:09 +04:00
}
2009-11-01 22:51:54 +03:00
void pvcreate_params_set_defaults ( struct pvcreate_params * pp )
2009-07-26 05:53:30 +04:00
{
memset ( pp , 0 , sizeof ( * pp ) ) ;
2009-10-06 00:03:25 +04:00
pp - > zero = 1 ;
2009-07-26 05:53:30 +04:00
pp - > size = 0 ;
pp - > data_alignment = UINT64_C ( 0 ) ;
2009-07-30 21:45:28 +04:00
pp - > data_alignment_offset = UINT64_C ( 0 ) ;
2009-07-26 05:53:30 +04:00
pp - > pvmetadatacopies = DEFAULT_PVMETADATACOPIES ;
pp - > pvmetadatasize = DEFAULT_PVMETADATASIZE ;
pp - > labelsector = DEFAULT_LABELSECTOR ;
pp - > force = PROMPT ;
pp - > yes = 0 ;
2010-07-01 00:03:52 +04:00
pp - > metadataignore = DEFAULT_PVMETADATAIGNORE ;
2013-02-18 13:51:32 +04:00
pp - > rp . restorefile = 0 ;
pp - > rp . idp = 0 ;
2013-05-28 14:37:22 +04:00
pp - > rp . ba_start = 0 ;
pp - > rp . ba_size = 0 ;
2013-02-18 15:47:31 +04:00
pp - > rp . pe_start = PV_PE_START_CALC ;
2013-02-18 13:51:32 +04:00
pp - > rp . extent_count = 0 ;
pp - > rp . extent_size = 0 ;
2009-07-26 05:53:30 +04:00
}
2011-06-01 23:29:31 +04:00
static int _pvcreate_write ( struct cmd_context * cmd , struct pv_to_create * pvc )
{
int zero = pvc - > pp - > zero ;
struct physical_volume * pv = pvc - > pv ;
struct device * dev = pv - > dev ;
const char * pv_name = dev_name ( dev ) ;
/* Wipe existing label first */
if ( ! label_remove ( pv_dev ( pv ) ) ) {
log_error ( " Failed to wipe existing label on %s " , pv_name ) ;
return 0 ;
}
if ( zero ) {
log_verbose ( " Zeroing start of device %s " , pv_name ) ;
if ( ! dev_open_quiet ( dev ) ) {
log_error ( " %s not opened: device not zeroed " , pv_name ) ;
return 0 ;
}
if ( ! dev_set ( dev , UINT64_C ( 0 ) , ( size_t ) 2048 , 0 ) ) {
log_error ( " %s not wiped: aborting " , pv_name ) ;
2012-02-28 14:11:35 +04:00
if ( ! dev_close ( dev ) )
stack ;
2011-06-01 23:29:31 +04:00
return 0 ;
}
2012-02-28 14:11:35 +04:00
if ( ! dev_close ( dev ) )
stack ;
2011-06-01 23:29:31 +04:00
}
2012-03-12 18:18:28 +04:00
log_verbose ( " Writing physical volume data to disk \" %s \" " ,
pv_name ) ;
2011-06-01 23:29:31 +04:00
if ( ! ( pv_write ( cmd , pv , 1 ) ) ) {
log_error ( " Failed to write physical volume \" %s \" " , pv_name ) ;
return 0 ;
}
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 ( " Physical volume \" %s \" successfully created " , pv_name ) ;
2011-06-01 23:29:31 +04:00
return 1 ;
}
2013-09-11 03:01:28 +04:00
static int _verify_pv_create_params ( struct pvcreate_params * pp )
{
/*
* FIXME : Some of these checks are duplicates in pvcreate_params_validate .
*/
if ( pp - > pvmetadatacopies > 2 ) {
log_error ( " Metadatacopies may only be 0, 1 or 2 " ) ;
return 0 ;
}
if ( pp - > data_alignment > UINT32_MAX ) {
log_error ( " Physical volume data alignment is too big. " ) ;
return 0 ;
}
if ( pp - > data_alignment_offset > UINT32_MAX ) {
log_error ( " Physical volume data alignment offset is too big. " ) ;
return 0 ;
}
return 1 ;
}
2009-07-26 05:53:57 +04:00
/*
2013-07-09 05:37:56 +04:00
* pvcreate_vol ( ) - initialize a device with PV label and metadata area
2009-07-26 05:53:57 +04:00
*
* Parameters :
* - pv_name : device path to initialize
2009-10-06 20:00:38 +04:00
* - pp : parameters to pass to pv_create ; if NULL , use default values
2009-07-26 05:53:57 +04:00
*
* Returns :
* NULL : error
2009-07-29 17:26:01 +04:00
* struct physical_volume * ( non - NULL ) : handle to physical volume created
2009-07-26 05:53:57 +04:00
*/
2013-07-09 05:37:56 +04:00
struct physical_volume * pvcreate_vol ( struct cmd_context * cmd , const char * pv_name ,
struct pvcreate_params * pp , int write_now )
2009-07-26 05:53:09 +04:00
{
2011-03-11 17:56:56 +03:00
struct physical_volume * pv = NULL ;
2009-07-26 05:53:09 +04:00
struct device * dev ;
2015-02-19 16:30:20 +03:00
int wiped = 0 ;
2009-07-26 05:53:09 +04:00
struct dm_list mdas ;
2009-07-26 05:53:30 +04:00
struct pvcreate_params default_pp ;
2010-07-09 19:34:40 +04:00
char buffer [ 64 ] __attribute__ ( ( aligned ( 8 ) ) ) ;
2015-02-19 16:30:20 +03:00
dev_ext_t dev_ext_src ;
2009-07-26 05:53:30 +04:00
2009-11-01 22:51:54 +03:00
pvcreate_params_set_defaults ( & default_pp ) ;
2009-07-26 06:02:22 +04:00
if ( ! pp )
2009-07-26 05:53:30 +04:00
pp = & default_pp ;
2009-07-26 05:53:09 +04:00
2013-09-11 03:01:28 +04:00
if ( ! _verify_pv_create_params ( pp ) ) {
goto bad ;
}
2013-02-18 13:51:32 +04:00
if ( pp - > rp . idp ) {
if ( ( dev = lvmcache_device_from_pvid ( cmd , pp - > rp . idp , NULL , NULL ) ) & &
2014-10-02 14:00:57 +04:00
( dev ! = dev_cache_get ( pv_name , cmd - > full_filter ) ) ) {
2013-02-18 13:51:32 +04:00
if ( ! id_write_format ( ( const struct id * ) & pp - > rp . idp - > uuid ,
2009-08-20 11:03:02 +04:00
buffer , sizeof ( buffer ) ) )
2011-03-11 17:56:56 +03:00
goto_bad ;
2009-08-20 11:03:02 +04:00
log_error ( " uuid %s already in use on \" %s \" " , buffer ,
dev_name ( dev ) ) ;
2012-03-14 00:36:02 +04:00
goto bad ;
2009-07-26 05:53:09 +04:00
}
}
2015-02-19 16:30:20 +03:00
if ( ! _pvcreate_check ( cmd , pv_name , pp , & wiped ) )
2011-03-11 17:56:56 +03:00
goto_bad ;
2009-07-26 05:53:09 +04:00
if ( sigint_caught ( ) )
2011-03-11 17:56:56 +03:00
goto_bad ;
2009-07-26 05:53:09 +04:00
2015-02-19 16:30:20 +03:00
/*
* wipe_known_signatures called in _pvcreate_check fires
* WATCH event to update udev database . But at the moment ,
* we have no way to synchronize with such event - we may
* end up still seeing the old info in udev db and pvcreate
* can fail to proceed because of the device still being
* filtered ( because of the stale info in udev db ) .
* Disable udev dev - ext source temporarily here for
* this reason and rescan with DEV_EXT_NONE dev - ext
* source ( so filters use DEV_EXT_NONE source ) .
*/
dev_ext_src = external_device_info_source ( ) ;
if ( wiped & & ( dev_ext_src = = DEV_EXT_UDEV ) )
init_external_device_info_source ( DEV_EXT_NONE ) ;
dev = dev_cache_get ( pv_name , cmd - > full_filter ) ;
init_external_device_info_source ( dev_ext_src ) ;
if ( ! dev ) {
2009-07-26 05:53:09 +04:00
log_error ( " %s: Couldn't find device. Check your filters? " ,
pv_name ) ;
2011-03-11 17:56:56 +03:00
goto bad ;
2009-07-26 05:53:09 +04:00
}
dm_list_init ( & mdas ) ;
2011-06-01 23:29:31 +04:00
2013-02-18 15:47:31 +04:00
if ( ! ( pv = pv_create ( cmd , dev , pp - > size , pp - > data_alignment ,
pp - > data_alignment_offset , pp - > labelsector ,
pp - > pvmetadatacopies , pp - > pvmetadatasize ,
pp - > metadataignore , & pp - > rp ) ) ) {
2009-07-26 05:53:09 +04:00
log_error ( " Failed to setup physical volume \" %s \" " , pv_name ) ;
2011-03-11 17:56:56 +03:00
goto bad ;
2009-07-26 05:53:09 +04:00
}
log_verbose ( " Set up physical volume for \" %s \" with % " PRIu64
" available sectors " , pv_name , pv_size ( pv ) ) ;
2013-03-25 19:21:59 +04:00
pv - > status | = UNLABELLED_PV ;
2011-06-01 23:29:31 +04:00
if ( write_now ) {
struct pv_to_create pvc ;
pvc . pp = pp ;
pvc . pv = pv ;
if ( ! _pvcreate_write ( cmd , & pvc ) )
2011-03-11 17:56:56 +03:00
goto bad ;
2009-07-26 05:53:09 +04:00
}
2009-07-26 05:53:57 +04:00
return pv ;
2009-07-26 05:53:09 +04:00
2011-03-11 17:56:56 +03:00
bad :
2009-07-26 05:53:57 +04:00
return NULL ;
2009-07-26 05:53:09 +04:00
}
2008-09-19 08:28:58 +04:00
static struct physical_volume * _alloc_pv ( struct dm_pool * mem , struct device * dev )
2007-10-12 22:37:19 +04:00
{
2012-02-13 14:51:52 +04:00
struct physical_volume * pv ;
2007-10-12 22:37:19 +04:00
2012-02-13 14:51:52 +04:00
if ( ! ( pv = dm_pool_zalloc ( mem , sizeof ( * pv ) ) ) ) {
log_error ( " Failed to allocate pv structure. " ) ;
return NULL ;
}
2007-10-12 22:37:19 +04:00
2008-09-19 08:28:58 +04:00
pv - > dev = dev ;
2007-10-12 22:37:19 +04:00
2008-11-04 01:14:30 +03:00
dm_list_init ( & pv - > tags ) ;
dm_list_init ( & pv - > segments ) ;
2007-10-12 22:37:19 +04:00
return pv ;
}
2009-07-26 05:52:19 +04:00
/**
* pv_create - initialize a physical volume for use with a volume group
2012-02-13 15:03:59 +04:00
* created PV belongs to Orphan VG .
2009-07-26 05:52:19 +04:00
*
* @ fmt : format type
* @ dev : PV device to initialize
* @ size : size of the PV in sectors
* @ data_alignment : requested alignment of data
2009-07-30 21:45:28 +04:00
* @ data_alignment_offset : requested offset to aligned data
2009-07-26 05:52:19 +04:00
* @ pe_start : physical extent start
* @ existing_extent_count
* @ existing_extent_size
* @ pvmetadatacopies
* @ pvmetadatasize
* @ mdas
*
* Returns :
* PV handle - physical volume initialized successfully
* NULL - invalid parameter or problem initializing the physical volume
*
* Note :
* FIXME : shorten argument list and replace with explict ' set ' functions
*/
struct physical_volume * pv_create ( const struct cmd_context * cmd ,
2007-06-11 22:29:30 +04:00
struct device * dev ,
2013-02-18 15:47:31 +04:00
uint64_t size ,
2009-02-22 22:00:26 +03:00
unsigned long data_alignment ,
2009-07-30 21:45:28 +04:00
unsigned long data_alignment_offset ,
2011-02-21 15:24:15 +03:00
uint64_t label_sector ,
2011-09-07 12:34:21 +04:00
unsigned pvmetadatacopies ,
2011-02-21 15:24:15 +03:00
uint64_t pvmetadatasize ,
2013-02-18 15:47:31 +04:00
unsigned metadataignore ,
struct pvcreate_restorable_params * rp )
2001-09-25 16:49:28 +04:00
{
2009-07-26 05:52:19 +04:00
const struct format_type * fmt = cmd - > fmt ;
2012-02-13 15:03:59 +04:00
struct dm_pool * mem = fmt - > orphan_vg - > vgmem ;
2008-09-19 08:28:58 +04:00
struct physical_volume * pv = _alloc_pv ( mem , dev ) ;
2011-02-21 15:24:15 +03:00
unsigned mda_index ;
2012-02-13 15:03:59 +04:00
struct pv_list * pvl ;
2001-10-12 14:32:06 +04:00
2007-10-12 22:37:19 +04:00
if ( ! pv )
2012-02-13 14:51:52 +04:00
return_NULL ;
2001-10-12 14:32:06 +04:00
2013-02-18 15:47:31 +04:00
if ( rp - > idp )
memcpy ( & pv - > id , rp - > idp , sizeof ( * rp - > idp ) ) ;
2005-01-20 21:11:53 +03:00
else if ( ! id_create ( & pv - > id ) ) {
log_error ( " Failed to create random uuid for %s. " ,
dev_name ( dev ) ) ;
2007-10-12 22:37:19 +04:00
goto bad ;
2005-01-20 21:11:53 +03:00
}
2002-01-16 21:10:08 +03:00
2002-02-20 21:29:30 +03:00
if ( ! dev_get_size ( pv - > dev , & pv - > size ) ) {
2007-10-12 18:29:32 +04:00
log_error ( " %s: Couldn't get size. " , pv_dev_name ( pv ) ) ;
2002-02-20 21:29:30 +03:00
goto bad ;
}
if ( size ) {
if ( size > pv - > size )
2007-06-28 21:33:44 +04:00
log_warn ( " WARNING: %s: Overriding real size. "
2007-10-12 18:29:32 +04:00
" You could lose data. " , pv_dev_name ( pv ) ) ;
2002-04-24 22:20:51 +04:00
log_verbose ( " %s: Pretending size is % " PRIu64 " sectors. " ,
2007-10-12 18:29:32 +04:00
pv_dev_name ( pv ) , size ) ;
2002-02-20 21:29:30 +03:00
pv - > size = size ;
}
2002-04-24 22:20:51 +04:00
2011-02-18 17:11:22 +03:00
if ( pv - > size < pv_min_size ( ) ) {
log_error ( " %s: Size must exceed minimum of % " PRIu64 " sectors. " ,
pv_dev_name ( pv ) , pv_min_size ( ) ) ;
2001-10-12 14:32:06 +04:00
goto bad ;
}
2013-02-21 17:47:49 +04:00
if ( pv - > size < data_alignment + data_alignment_offset ) {
2009-02-23 19:53:42 +03:00
log_error ( " %s: Data alignment must not exceed device size. " ,
pv_dev_name ( pv ) ) ;
goto bad ;
}
2012-02-10 06:53:03 +04:00
if ( ! ( pvl = dm_pool_zalloc ( mem , sizeof ( * pvl ) ) ) ) {
log_error ( " pv_list allocation in pv_create failed " ) ;
2011-02-21 15:12:32 +03:00
goto bad ;
}
2012-02-10 06:53:03 +04:00
pvl - > pv = pv ;
add_pvl_to_vgs ( fmt - > orphan_vg , pvl ) ;
fmt - > orphan_vg - > extent_count + = pv - > pe_count ;
fmt - > orphan_vg - > free_count + = pv - > pe_count ;
2011-02-21 15:12:32 +03:00
2002-11-18 17:04:08 +03:00
pv - > fmt = fmt ;
2008-02-06 18:47:28 +03:00
pv - > vg_name = fmt - > orphan_vg_name ;
2002-02-15 17:33:59 +03:00
2013-02-18 15:47:31 +04:00
if ( ! fmt - > ops - > pv_initialise ( fmt , label_sector , data_alignment ,
data_alignment_offset , rp , pv ) ) {
2011-02-21 15:24:15 +03:00
log_error ( " Format-specific initialisation of physical "
" volume %s failed. " , pv_dev_name ( pv ) ) ;
2002-02-15 17:33:59 +03:00
goto bad ;
}
2009-02-22 22:00:26 +03:00
2011-02-21 15:24:15 +03:00
for ( mda_index = 0 ; mda_index < pvmetadatacopies ; mda_index + + ) {
if ( pv - > fmt - > ops - > pv_add_metadata_area & &
! pv - > fmt - > ops - > pv_add_metadata_area ( pv - > fmt , pv ,
2013-02-18 15:47:31 +04:00
rp - > pe_start ! = PV_PE_START_CALC ,
2011-02-21 15:24:15 +03:00
mda_index , pvmetadatasize ,
metadataignore ) ) {
log_error ( " Failed to add metadata area for "
" new physical volume %s " , pv_dev_name ( pv ) ) ;
goto bad ;
}
}
2001-10-12 14:32:06 +04:00
return pv ;
2001-10-15 22:39:40 +04:00
bad :
2012-02-13 15:03:59 +04:00
// FIXME: detach from orphan in error path
//free_pv_fid(pv);
//dm_pool_free(mem, pv);
2001-10-12 14:32:06 +04:00
return NULL ;
2001-09-25 16:49:28 +04:00
}
2007-06-11 22:29:30 +04:00
/* FIXME: liblvm todo - make into function that returns handle */
2008-03-14 01:51:24 +03:00
struct pv_list * find_pv_in_vg ( const struct volume_group * vg ,
2013-03-19 16:58:02 +04:00
const char * pv_name )
2001-10-15 22:39:40 +04:00
{
2002-01-21 17:28:12 +03:00
struct pv_list * pvl ;
2014-10-07 18:06:21 +04:00
struct device * dev = dev_cache_get ( pv_name , vg - > cmd - > filter ) ;
2001-10-25 18:04:18 +04:00
2014-10-07 18:06:21 +04:00
/*
* If the device does not exist or is filtered out , don ' t bother trying
* to find it in the list . This also prevents accidentally finding a
* non - NULL PV which happens to be missing ( i . e . its pv - > dev is NULL )
* for such devices .
*/
if ( ! dev )
return NULL ;
dm_list_iterate_items ( pvl , & vg - > pvs )
if ( pvl - > pv - > dev = = dev )
2002-01-21 17:28:12 +03:00
return pvl ;
2001-09-25 16:49:28 +04:00
2001-10-15 22:39:40 +04:00
return NULL ;
2002-11-18 17:04:08 +03:00
}
2008-11-04 01:14:30 +03:00
struct pv_list * find_pv_in_pv_list ( const struct dm_list * pl ,
2008-03-28 22:08:23 +03:00
const struct physical_volume * pv )
{
struct pv_list * pvl ;
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( pvl , pl )
2008-03-28 22:08:23 +03:00
if ( pvl - > pv = = pv )
return pvl ;
2008-04-10 23:59:43 +04:00
2008-03-28 22:08:23 +03:00
return NULL ;
}
2003-01-18 00:04:26 +03:00
int pv_is_in_vg ( struct volume_group * vg , struct physical_volume * pv )
{
2005-06-01 20:51:55 +04:00
struct pv_list * pvl ;
2003-01-18 00:04:26 +03:00
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( pvl , & vg - > pvs )
2005-06-01 20:51:55 +04:00
if ( pv = = pvl - > pv )
2003-01-18 00:04:26 +03:00
return 1 ;
return 0 ;
}
2007-06-13 02:41:27 +04:00
/**
* find_pv_in_vg_by_uuid - Find PV in VG by PV UUID
* @ vg : volume group to search
* @ id : UUID of the PV to match
*
* Returns :
2010-03-16 18:30:48 +03:00
* struct pv_list within owning struct volume_group - if UUID of PV found in VG
2007-06-13 02:41:27 +04:00
* NULL - invalid parameter or UUID of PV not found in VG
*
* Note
* FIXME - liblvm todo - make into function that takes VG handle
*/
2010-03-16 18:30:48 +03:00
struct pv_list * find_pv_in_vg_by_uuid ( const struct volume_group * vg ,
const struct id * id )
2007-06-11 22:29:30 +04:00
{
2013-03-19 16:58:02 +04:00
struct pv_list * pvl ;
dm_list_iterate_items ( pvl , & vg - > pvs )
if ( id_equal ( & pvl - > pv - > id , id ) )
return pvl ;
return NULL ;
2007-06-11 22:29:30 +04:00
}
2008-03-14 01:51:24 +03:00
struct lv_list * find_lv_in_vg ( const struct volume_group * vg ,
const char * lv_name )
2001-10-29 16:52:23 +03:00
{
2002-01-21 17:28:12 +03:00
struct lv_list * lvl ;
2001-10-29 16:52:23 +03:00
const char * ptr ;
/* Use last component */
if ( ( ptr = strrchr ( lv_name , ' / ' ) ) )
ptr + + ;
else
ptr = lv_name ;
2001-10-31 15:47:01 +03:00
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( lvl , & vg - > lvs )
2002-01-21 19:49:32 +03:00
if ( ! strcmp ( lvl - > lv - > name , ptr ) )
2002-01-21 17:28:12 +03:00
return lvl ;
2001-10-29 16:52:23 +03:00
2001-11-10 01:01:04 +03:00
return NULL ;
2001-10-29 16:52:23 +03:00
}
2008-11-04 01:14:30 +03:00
struct lv_list * find_lv_in_lv_list ( const struct dm_list * ll ,
2008-03-28 22:08:23 +03:00
const struct logical_volume * lv )
{
struct lv_list * lvl ;
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( lvl , ll )
2008-03-28 22:08:23 +03:00
if ( lvl - > lv = = lv )
return lvl ;
2008-04-10 23:59:43 +04:00
2008-03-28 22:08:23 +03:00
return NULL ;
}
2015-11-22 01:31:44 +03:00
struct logical_volume * find_lv_in_vg_by_lvid ( struct volume_group * vg ,
const union lvid * lvid )
2002-02-25 15:56:16 +03:00
{
struct lv_list * lvl ;
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( lvl , & vg - > lvs )
2002-03-05 23:03:09 +03:00
if ( ! strncmp ( lvl - > lv - > lvid . s , lvid - > s , sizeof ( * lvid ) ) )
2015-11-22 01:31:44 +03:00
return lvl - > lv ;
2002-02-25 15:56:16 +03:00
return NULL ;
}
2008-03-14 01:51:24 +03:00
struct logical_volume * find_lv ( const struct volume_group * vg ,
const char * lv_name )
2001-10-29 16:52:23 +03:00
{
2002-01-21 17:28:12 +03:00
struct lv_list * lvl = find_lv_in_vg ( vg , lv_name ) ;
2002-01-21 19:49:32 +03:00
return lvl ? lvl - > lv : NULL ;
2001-10-29 16:52:23 +03:00
}
2001-11-28 16:45:50 +03:00
struct physical_volume * find_pv ( struct volume_group * vg , struct device * dev )
2001-10-29 16:52:23 +03:00
{
2005-06-01 20:51:55 +04:00
struct pv_list * pvl ;
2001-11-10 01:01:04 +03:00
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( pvl , & vg - > pvs )
2005-06-01 20:51:55 +04:00
if ( dev = = pvl - > pv - > dev )
return pvl - > pv ;
2002-01-21 19:05:23 +03:00
2001-11-10 01:01:04 +03:00
return NULL ;
2001-10-29 16:52:23 +03:00
}
2002-02-25 15:56:16 +03:00
2007-06-11 22:29:30 +04:00
/* FIXME: liblvm todo - make into function that returns handle */
2004-05-05 15:04:28 +04:00
struct physical_volume * find_pv_by_name ( struct cmd_context * cmd ,
2013-03-19 17:05:19 +04:00
const char * pv_name ,
2013-11-30 01:45:37 +04:00
int allow_orphan , int allow_unformatted )
2004-05-05 15:04:28 +04:00
{
2013-02-19 05:32:25 +04:00
struct device * dev ;
struct pv_list * pvl ;
struct dm_list * pvslist ;
struct physical_volume * pv = NULL ;
lvmcache_seed_infos_from_lvmetad ( cmd ) ;
2004-05-05 15:04:28 +04:00
2013-02-19 05:32:25 +04:00
if ( ! ( dev = dev_cache_get ( pv_name , cmd - > filter ) ) ) {
2014-07-31 11:30:25 +04:00
if ( ! allow_unformatted )
log_error ( " Physical volume %s not found " , pv_name ) ;
2013-02-19 05:32:25 +04:00
return_NULL ;
2004-05-05 15:04:28 +04:00
}
2013-02-19 05:32:25 +04:00
if ( ! ( pvslist = get_pvs ( cmd ) ) )
return_NULL ;
dm_list_iterate_items ( pvl , pvslist )
if ( pvl - > pv - > dev = = dev )
pv = pvl - > pv ;
else
free_pv_fid ( pvl - > pv ) ;
2013-11-30 01:45:37 +04:00
if ( ! pv & & ! allow_unformatted )
2013-02-19 05:32:25 +04:00
log_error ( " Physical volume %s not found " , pv_name ) ;
2008-01-16 21:15:26 +03:00
2013-03-20 17:34:05 +04:00
if ( pv & & ! allow_orphan & & is_orphan_vg ( pv - > vg_name ) ) {
2004-05-05 15:04:28 +04:00
log_error ( " Physical volume %s not in a volume group " , pv_name ) ;
2011-03-11 17:56:56 +03:00
goto bad ;
2004-05-05 15:04:28 +04:00
}
return pv ;
2011-03-11 17:56:56 +03:00
bad :
free_pv_fid ( pv ) ;
return NULL ;
2004-05-05 15:04:28 +04:00
}
2003-04-25 02:23:24 +04:00
/* Find segment at a given logical extent in an LV */
2007-12-20 21:55:46 +03:00
struct lv_segment * find_seg_by_le ( const struct logical_volume * lv , uint32_t le )
2003-04-25 02:23:24 +04:00
{
struct lv_segment * seg ;
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( seg , & lv - > segments )
2003-04-25 02:23:24 +04:00
if ( le > = seg - > le & & le < seg - > le + seg - > len )
return seg ;
return NULL ;
}
2007-12-20 21:55:46 +03:00
struct lv_segment * first_seg ( const struct logical_volume * lv )
2005-10-28 16:48:50 +04:00
{
2009-05-12 23:09:21 +04:00
struct lv_segment * seg ;
2005-10-28 16:48:50 +04:00
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( seg , & lv - > segments )
2009-05-12 23:09:21 +04:00
return seg ;
2005-10-28 16:48:50 +04:00
2009-05-12 23:09:21 +04:00
return NULL ;
2005-10-28 16:48:50 +04:00
}
2011-10-29 00:12:54 +04:00
struct lv_segment * last_seg ( const struct logical_volume * lv )
{
2012-02-27 13:51:31 +04:00
struct lv_segment * seg ;
2011-10-29 00:12:54 +04:00
2012-02-27 13:51:31 +04:00
dm_list_iterate_back_items ( seg , & lv - > segments )
return seg ;
2011-10-29 00:12:54 +04:00
2012-02-27 13:51:31 +04:00
return NULL ;
2011-10-29 00:12:54 +04:00
}
2009-09-03 01:39:07 +04:00
int vg_remove_mdas ( struct volume_group * vg )
2002-04-24 22:20:51 +04:00
{
2002-11-18 17:04:08 +03:00
struct metadata_area * mda ;
2002-04-24 22:20:51 +04:00
/* FIXME Improve recovery situation? */
/* Remove each copy of the metadata */
2010-06-29 00:32:44 +04:00
dm_list_iterate_items ( mda , & vg - > fid - > metadata_areas_in_use ) {
2002-11-18 17:04:08 +03:00
if ( mda - > ops - > vg_remove & &
2008-01-30 16:19:47 +03:00
! mda - > ops - > vg_remove ( vg - > fid , vg , mda ) )
return_0 ;
2002-04-24 22:20:51 +04:00
}
return 1 ;
}
2008-01-16 22:54:39 +03:00
/*
* Determine whether two vgs are compatible for merging .
*/
2010-07-09 19:34:40 +04:00
int vgs_are_compatible ( struct cmd_context * cmd __attribute__ ( ( unused ) ) ,
2008-01-16 22:54:39 +03:00
struct volume_group * vg_from ,
struct volume_group * vg_to )
{
struct lv_list * lvl1 , * lvl2 ;
struct pv_list * pvl ;
2011-02-18 17:47:28 +03:00
const char * name1 , * name2 ;
2008-01-16 22:54:39 +03:00
if ( lvs_in_vg_activated ( vg_from ) ) {
log_error ( " Logical volumes in \" %s \" must be inactive " ,
vg_from - > name ) ;
2008-01-17 20:17:09 +03:00
return 0 ;
2008-01-16 22:54:39 +03:00
}
/* Check compatibility */
if ( vg_to - > extent_size ! = vg_from - > extent_size ) {
log_error ( " Extent sizes differ: %d (%s) and %d (%s) " ,
vg_to - > extent_size , vg_to - > name ,
vg_from - > extent_size , vg_from - > name ) ;
2008-01-17 20:17:09 +03:00
return 0 ;
2008-01-16 22:54:39 +03:00
}
if ( vg_to - > max_pv & &
( vg_to - > max_pv < vg_to - > pv_count + vg_from - > pv_count ) ) {
log_error ( " Maximum number of physical volumes (%d) exceeded "
" for \" %s \" and \" %s \" " , vg_to - > max_pv , vg_to - > name ,
vg_from - > name ) ;
2008-01-17 20:17:09 +03:00
return 0 ;
2008-01-16 22:54:39 +03:00
}
if ( vg_to - > max_lv & &
2009-05-14 01:27:43 +04:00
( vg_to - > max_lv < vg_visible_lvs ( vg_to ) + vg_visible_lvs ( vg_from ) ) ) {
2008-01-16 22:54:39 +03:00
log_error ( " Maximum number of logical volumes (%d) exceeded "
" for \" %s \" and \" %s \" " , vg_to - > max_lv , vg_to - > name ,
vg_from - > name ) ;
2008-01-17 20:17:09 +03:00
return 0 ;
2008-01-16 22:54:39 +03:00
}
2008-01-22 05:48:53 +03:00
/* Metadata types must be the same */
if ( vg_to - > fid - > fmt ! = vg_from - > fid - > fmt ) {
log_error ( " Metadata types differ for \" %s \" and \" %s \" " ,
vg_to - > name , vg_from - > name ) ;
return 0 ;
}
/* Clustering attribute must be the same */
2008-04-10 21:09:32 +04:00
if ( vg_is_clustered ( vg_to ) ! = vg_is_clustered ( vg_from ) ) {
2008-01-22 05:48:53 +03:00
log_error ( " Clustered attribute differs for \" %s \" and \" %s \" " ,
vg_to - > name , vg_from - > name ) ;
return 0 ;
}
2008-01-16 22:54:39 +03:00
/* Check no conflicts with LV names */
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( lvl1 , & vg_to - > lvs ) {
2008-01-17 20:17:09 +03:00
name1 = lvl1 - > lv - > name ;
2008-01-16 22:54:39 +03:00
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( lvl2 , & vg_from - > lvs ) {
2008-01-17 20:17:09 +03:00
name2 = lvl2 - > lv - > name ;
2008-01-16 22:54:39 +03:00
if ( ! strcmp ( name1 , name2 ) ) {
log_error ( " Duplicate logical volume "
" name \" %s \" "
" in \" %s \" and \" %s \" " ,
name1 , vg_to - > name , vg_from - > name ) ;
2008-01-17 20:17:09 +03:00
return 0 ;
2008-01-16 22:54:39 +03:00
}
}
}
/* Check no PVs are constructed from either VG */
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( pvl , & vg_to - > pvs ) {
2008-01-16 22:54:39 +03:00
if ( pv_uses_vg ( pvl - > pv , vg_from ) ) {
log_error ( " Physical volume %s might be constructed "
" from same volume group %s. " ,
pv_dev_name ( pvl - > pv ) , vg_from - > name ) ;
2008-01-17 20:17:09 +03:00
return 0 ;
2008-01-16 22:54:39 +03:00
}
}
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( pvl , & vg_from - > pvs ) {
2008-01-16 22:54:39 +03:00
if ( pv_uses_vg ( pvl - > pv , vg_to ) ) {
log_error ( " Physical volume %s might be constructed "
" from same volume group %s. " ,
pv_dev_name ( pvl - > pv ) , vg_to - > name ) ;
2008-01-17 20:17:09 +03:00
return 0 ;
2008-01-16 22:54:39 +03:00
}
}
return 1 ;
}
2008-09-19 10:42:00 +04:00
struct _lv_postorder_baton {
int ( * fn ) ( struct logical_volume * lv , void * data ) ;
void * data ;
} ;
static int _lv_postorder_visit ( struct logical_volume * ,
int ( * fn ) ( struct logical_volume * lv , void * data ) ,
void * data ) ;
static int _lv_each_dependency ( struct logical_volume * lv ,
int ( * fn ) ( struct logical_volume * lv , void * data ) ,
void * data )
{
2011-04-08 18:40:18 +04:00
unsigned i , s ;
2008-09-19 10:42:00 +04:00
struct lv_segment * lvseg ;
2013-12-17 16:53:15 +04:00
struct dm_list * snh ;
2008-09-19 10:42:00 +04:00
struct logical_volume * deps [ ] = {
2010-05-21 16:45:18 +04:00
( lv - > rdevice & & lv ! = lv - > rdevice - > lv ) ? lv - > rdevice - > lv : 0 ,
( lv - > rdevice & & lv ! = lv - > rdevice - > slog ) ? lv - > rdevice - > slog : 0 ,
2008-09-19 10:42:00 +04:00
lv - > snapshot ? lv - > snapshot - > origin : 0 ,
lv - > snapshot ? lv - > snapshot - > cow : 0 } ;
2014-04-04 23:10:30 +04:00
for ( i = 0 ; i < DM_ARRAY_SIZE ( deps ) ; + + i ) {
2008-09-19 10:42:00 +04:00
if ( deps [ i ] & & ! fn ( deps [ i ] , data ) )
return_0 ;
}
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( lvseg , & lv - > segments ) {
2013-12-17 16:53:15 +04:00
if ( lvseg - > external_lv & & ! fn ( lvseg - > external_lv , data ) )
return_0 ;
2008-09-19 10:42:00 +04:00
if ( lvseg - > log_lv & & ! fn ( lvseg - > log_lv , data ) )
return_0 ;
2010-05-21 16:45:18 +04:00
if ( lvseg - > rlog_lv & & ! fn ( lvseg - > rlog_lv , data ) )
return_0 ;
2012-01-25 12:50:10 +04:00
if ( lvseg - > pool_lv & & ! fn ( lvseg - > pool_lv , data ) )
return_0 ;
if ( lvseg - > metadata_lv & & ! fn ( lvseg - > metadata_lv , data ) )
return_0 ;
2008-09-19 10:42:00 +04:00
for ( s = 0 ; s < lvseg - > area_count ; + + s ) {
if ( seg_type ( lvseg , s ) = = AREA_LV & & ! fn ( seg_lv ( lvseg , s ) , data ) )
return_0 ;
}
}
2013-12-17 16:53:15 +04:00
if ( lv_is_origin ( lv ) )
dm_list_iterate ( snh , & lv - > snapshot_segs )
if ( ! fn ( dm_list_struct_base ( snh , struct lv_segment , origin_list ) - > cow , data ) )
return_0 ;
2008-09-19 10:42:00 +04:00
return 1 ;
}
static int _lv_postorder_cleanup ( struct logical_volume * lv , void * data )
{
if ( ! ( lv - > status & POSTORDER_FLAG ) )
return 1 ;
lv - > status & = ~ POSTORDER_FLAG ;
if ( ! _lv_each_dependency ( lv , _lv_postorder_cleanup , data ) )
return_0 ;
return 1 ;
}
2011-02-14 22:27:05 +03:00
static int _lv_postorder_level ( struct logical_volume * lv , void * data )
{
struct _lv_postorder_baton * baton = data ;
2015-11-17 15:21:22 +03:00
return ( data ) ? _lv_postorder_visit ( lv , baton - > fn , baton - > data ) : 0 ;
2011-02-14 22:27:05 +03:00
} ;
2008-09-19 10:42:00 +04:00
static int _lv_postorder_visit ( struct logical_volume * lv ,
int ( * fn ) ( struct logical_volume * lv , void * data ) ,
void * data )
{
struct _lv_postorder_baton baton ;
int r ;
if ( lv - > status & POSTORDER_FLAG )
return 1 ;
2011-02-14 22:27:05 +03:00
if ( lv - > status & POSTORDER_OPEN_FLAG )
return 1 ; // a data structure loop has closed...
lv - > status | = POSTORDER_OPEN_FLAG ;
2008-09-19 10:42:00 +04:00
baton . fn = fn ;
baton . data = data ;
r = _lv_each_dependency ( lv , _lv_postorder_level , & baton ) ;
2011-02-14 22:27:05 +03:00
2009-05-30 05:54:29 +04:00
if ( r )
2008-09-19 10:42:00 +04:00
r = fn ( lv , data ) ;
2009-05-30 05:54:29 +04:00
2011-02-14 22:27:05 +03:00
lv - > status & = ~ POSTORDER_OPEN_FLAG ;
lv - > status | = POSTORDER_FLAG ;
2008-09-19 10:42:00 +04:00
return r ;
}
/*
* This will walk the LV dependency graph in depth - first order and in the
* postorder , call a callback function " fn " . The void * data is passed along all
* the calls . The callback may return zero to indicate an error and terminate
* the depth - first walk . The error is propagated to return value of
* _lv_postorder .
*/
static int _lv_postorder ( struct logical_volume * lv ,
int ( * fn ) ( struct logical_volume * lv , void * data ) ,
void * data )
{
int r ;
2011-08-11 21:34:30 +04:00
int pool_locked = dm_pool_locked ( lv - > vg - > vgmem ) ;
if ( pool_locked & & ! dm_pool_unlock ( lv - > vg - > vgmem , 0 ) )
return_0 ;
2008-09-19 10:42:00 +04:00
r = _lv_postorder_visit ( lv , fn , data ) ;
_lv_postorder_cleanup ( lv , 0 ) ;
2011-08-11 21:34:30 +04:00
if ( pool_locked & & ! dm_pool_lock ( lv - > vg - > vgmem , 0 ) )
return_0 ;
2008-09-19 10:42:00 +04:00
return r ;
}
2011-03-10 17:40:32 +03:00
/*
* Calls _lv_postorder ( ) on each LV from VG . Avoids duplicate transitivity visits .
* Clears with _lv_postorder_cleanup ( ) when all LVs were visited by postorder .
*/
static int _lv_postorder_vg ( struct volume_group * vg ,
int ( * fn ) ( struct logical_volume * lv , void * data ) ,
void * data )
{
struct lv_list * lvl ;
int r = 1 ;
2011-08-11 21:34:30 +04:00
int pool_locked = dm_pool_locked ( vg - > vgmem ) ;
if ( pool_locked & & ! dm_pool_unlock ( vg - > vgmem , 0 ) )
return_0 ;
2011-03-10 17:40:32 +03:00
dm_list_iterate_items ( lvl , & vg - > lvs )
if ( ! _lv_postorder_visit ( lvl - > lv , fn , data ) ) {
stack ;
r = 0 ;
}
dm_list_iterate_items ( lvl , & vg - > lvs )
_lv_postorder_cleanup ( lvl - > lv , 0 ) ;
2011-08-11 21:34:30 +04:00
if ( pool_locked & & ! dm_pool_lock ( vg - > vgmem , 0 ) )
return_0 ;
2011-03-10 17:40:32 +03:00
return r ;
}
2008-09-19 10:42:00 +04:00
struct _lv_mark_if_partial_baton {
int partial ;
} ;
static int _lv_mark_if_partial_collect ( struct logical_volume * lv , void * data )
{
struct _lv_mark_if_partial_baton * baton = data ;
2015-11-17 15:21:22 +03:00
if ( baton & & lv - > status & PARTIAL_LV )
2008-09-19 10:42:00 +04:00
baton - > partial = 1 ;
return 1 ;
}
static int _lv_mark_if_partial_single ( struct logical_volume * lv , void * data )
{
2011-04-08 18:40:18 +04:00
unsigned s ;
2008-09-19 10:42:00 +04:00
struct _lv_mark_if_partial_baton baton ;
struct lv_segment * lvseg ;
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( lvseg , & lv - > segments ) {
2008-09-19 10:42:00 +04:00
for ( s = 0 ; s < lvseg - > area_count ; + + s ) {
if ( seg_type ( lvseg , s ) = = AREA_PV ) {
2010-03-16 17:37:38 +03:00
if ( is_missing_pv ( seg_pv ( lvseg , s ) ) )
2008-09-19 10:42:00 +04:00
lv - > status | = PARTIAL_LV ;
}
}
}
baton . partial = 0 ;
2012-02-28 15:10:45 +04:00
if ( ! _lv_each_dependency ( lv , _lv_mark_if_partial_collect , & baton ) )
return_0 ;
2008-09-19 10:42:00 +04:00
if ( baton . partial )
lv - > status | = PARTIAL_LV ;
return 1 ;
}
/*
* Mark LVs with missing PVs using PARTIAL_LV status flag . The flag is
* propagated transitively , so LVs referencing other LVs are marked
* partial as well , if any of their referenced LVs are marked partial .
*/
2011-05-07 17:32:05 +04:00
int vg_mark_partial_lvs ( struct volume_group * vg , int clear )
2008-09-19 10:42:00 +04:00
{
2011-05-07 17:32:05 +04:00
struct lv_list * lvl ;
if ( clear )
dm_list_iterate_items ( lvl , & vg - > lvs )
lvl - > lv - > status & = ~ PARTIAL_LV ;
2011-03-10 17:40:32 +03:00
if ( ! _lv_postorder_vg ( vg , _lv_mark_if_partial_single , NULL ) )
return_0 ;
2008-09-19 10:42:00 +04:00
return 1 ;
}
2009-05-20 15:09:49 +04:00
/*
* Be sure that all PV devices have cached read ahead in dev - cache
* Currently it takes read_ahead from first PV segment only
*/
static int _lv_read_ahead_single ( struct logical_volume * lv , void * data )
{
struct lv_segment * seg = first_seg ( lv ) ;
uint32_t seg_read_ahead = 0 , * read_ahead = data ;
2012-06-21 14:43:31 +04:00
if ( ! read_ahead ) {
log_error ( INTERNAL_ERROR " Read ahead data missing. " ) ;
return 0 ;
}
2009-06-01 16:43:31 +04:00
if ( seg & & seg - > area_count & & seg_type ( seg , 0 ) = = AREA_PV )
2009-05-20 15:09:49 +04:00
dev_get_read_ahead ( seg_pv ( seg , 0 ) - > dev , & seg_read_ahead ) ;
if ( seg_read_ahead > * read_ahead )
* read_ahead = seg_read_ahead ;
return 1 ;
}
2009-06-01 16:43:31 +04:00
/*
* Calculate readahead for logical volume from underlying PV devices .
* If read_ahead is NULL , only ensure that readahead of PVs are preloaded
* into PV struct device in dev cache .
*/
void lv_calculate_readahead ( const struct logical_volume * lv , uint32_t * read_ahead )
2009-05-20 15:09:49 +04:00
{
2009-06-01 16:43:31 +04:00
uint32_t _read_ahead = 0 ;
2009-05-20 15:09:49 +04:00
if ( lv - > read_ahead = = DM_READ_AHEAD_AUTO )
2009-06-01 16:43:31 +04:00
_lv_postorder ( ( struct logical_volume * ) lv , _lv_read_ahead_single , & _read_ahead ) ;
2009-05-20 15:09:49 +04:00
2009-06-01 16:43:31 +04:00
if ( read_ahead ) {
2013-01-08 02:30:29 +04:00
log_debug_metadata ( " Calculated readahead of LV %s is %u " , lv - > name , _read_ahead ) ;
2009-06-01 16:43:31 +04:00
* read_ahead = _read_ahead ;
}
2009-05-20 15:09:49 +04:00
}
2011-03-30 17:35:51 +04:00
struct validate_hash {
struct dm_hash_table * lvname ;
struct dm_hash_table * lvid ;
struct dm_hash_table * pvid ;
2015-03-05 23:00:44 +03:00
struct dm_hash_table * lv_lock_args ;
2011-03-30 17:35:51 +04:00
} ;
2010-12-14 20:51:09 +03:00
/*
* Check that an LV and all its PV references are correctly listed in vg - > lvs
* and vg - > pvs , respectively . This only looks at a single LV , but * not * at the
* LVs it is using . To do the latter , you should use _lv_postorder with this
* function . C . f . vg_validate .
*/
static int _lv_validate_references_single ( struct logical_volume * lv , void * data )
{
struct volume_group * vg = lv - > vg ;
2011-03-30 17:35:51 +04:00
struct validate_hash * vhash = data ;
2010-12-14 20:51:09 +03:00
struct lv_segment * lvseg ;
2011-03-30 17:35:51 +04:00
struct physical_volume * pv ;
2011-04-08 18:40:18 +04:00
unsigned s ;
2010-12-14 20:51:09 +03:00
int r = 1 ;
2011-03-30 17:35:51 +04:00
if ( lv ! = dm_hash_lookup_binary ( vhash - > lvid , & lv - > lvid . id [ 1 ] ,
sizeof ( lv - > lvid . id [ 1 ] ) ) ) {
2010-12-14 20:51:09 +03:00
log_error ( INTERNAL_ERROR
" Referenced LV %s not listed in VG %s. " ,
lv - > name , vg - > name ) ;
r = 0 ;
}
dm_list_iterate_items ( lvseg , & lv - > segments ) {
for ( s = 0 ; s < lvseg - > area_count ; + + s ) {
2011-03-30 17:35:51 +04:00
if ( seg_type ( lvseg , s ) ! = AREA_PV )
continue ;
pv = seg_pv ( lvseg , s ) ;
/* look up the reference in vg->pvs */
if ( pv ! = dm_hash_lookup_binary ( vhash - > pvid , & pv - > id ,
sizeof ( pv - > id ) ) ) {
log_error ( INTERNAL_ERROR
" Referenced PV %s not listed in VG %s. " ,
pv_dev_name ( pv ) , vg - > name ) ;
r = 0 ;
2010-12-14 20:51:09 +03:00
}
}
}
return r ;
}
2015-07-09 21:24:28 +03:00
/*
* Format is < version > : < info >
*/
static int _validate_lock_args_chars ( const char * lock_args )
{
int i ;
char c ;
int found_colon = 0 ;
int r = 1 ;
for ( i = 0 ; i < strlen ( lock_args ) ; i + + ) {
c = lock_args [ i ] ;
if ( ! isalnum ( c ) & & c ! = ' . ' & & c ! = ' _ ' & & c ! = ' - ' & & c ! = ' + ' & & c ! = ' : ' ) {
log_error ( INTERNAL_ERROR " Invalid character at index %d of lock_args \" %s \" " ,
i , lock_args ) ;
r = 0 ;
}
if ( c = = ' : ' & & found_colon ) {
log_error ( INTERNAL_ERROR " Invalid colon at index %d of lock_args \" %s \" " ,
i , lock_args ) ;
r = 0 ;
}
if ( c = = ' : ' )
found_colon = 1 ;
}
return r ;
}
static int _validate_vg_lock_args ( struct volume_group * vg )
{
if ( ! _validate_lock_args_chars ( vg - > lock_args ) ) {
log_error ( INTERNAL_ERROR " VG %s has invalid lock_args chars " , vg - > name ) ;
return 0 ;
}
return 1 ;
}
/*
* For lock_type sanlock , LV lock_args are < version > : < info >
* For lock_type dlm , LV lock_args are not used , and lock_args is
* just set to " dlm " .
*/
static int _validate_lv_lock_args ( struct logical_volume * lv )
{
int r = 1 ;
if ( ! strcmp ( lv - > vg - > lock_type , " sanlock " ) ) {
if ( ! _validate_lock_args_chars ( lv - > lock_args ) ) {
log_error ( INTERNAL_ERROR " LV %s/%s has invalid lock_args chars " ,
lv - > vg - > name , display_lvname ( lv ) ) ;
return 0 ;
}
} else if ( ! strcmp ( lv - > vg - > lock_type , " dlm " ) ) {
if ( strcmp ( lv - > lock_args , " dlm " ) ) {
log_error ( INTERNAL_ERROR " LV %s/%s has invalid lock_args \" %s \" " ,
lv - > vg - > name , display_lvname ( lv ) , lv - > lock_args ) ;
r = 0 ;
}
}
return r ;
}
2005-07-12 23:40:59 +04:00
int vg_validate ( struct volume_group * vg )
2002-04-24 22:20:51 +04:00
{
2011-03-10 16:11:59 +03:00
struct pv_list * pvl ;
struct lv_list * lvl ;
2010-12-14 20:07:35 +03:00
struct lv_segment * seg ;
2014-05-29 11:41:03 +04:00
struct dm_str_list * sl ;
2010-07-09 19:34:40 +04:00
char uuid [ 64 ] __attribute__ ( ( aligned ( 8 ) ) ) ;
2014-07-03 22:06:04 +04:00
char uuid2 [ 64 ] __attribute__ ( ( aligned ( 8 ) ) ) ;
2006-08-09 23:33:25 +04:00
int r = 1 ;
2013-07-05 19:10:11 +04:00
unsigned hidden_lv_count = 0 , lv_count = 0 , lv_visible_count = 0 ;
unsigned pv_count = 0 ;
unsigned num_snapshots = 0 ;
2013-07-05 19:10:47 +04:00
unsigned spare_count = 0 ;
2014-03-31 13:51:50 +04:00
size_t vg_name_len = strlen ( vg - > name ) ;
size_t dev_name_len ;
2011-03-30 17:35:51 +04:00
struct validate_hash vhash = { NULL } ;
2006-08-09 23:33:25 +04:00
2010-11-09 15:34:40 +03:00
if ( vg - > alloc = = ALLOC_CLING_BY_TAGS ) {
log_error ( INTERNAL_ERROR " VG %s allocation policy set to invalid cling_by_tags. " ,
vg - > name ) ;
r = 0 ;
}
2015-03-09 21:56:24 +03:00
if ( vg - > status & LVM_WRITE_LOCKED ) {
log_error ( INTERNAL_ERROR " VG %s has external flag LVM_WRITE_LOCKED set internally. " ,
vg - > name ) ;
r = 0 ;
}
2006-10-06 02:02:52 +04:00
/* FIXME Also check there's no data/metadata overlap */
2011-03-30 17:35:51 +04:00
if ( ! ( vhash . pvid = dm_hash_create ( vg - > pv_count ) ) ) {
2011-03-10 16:11:59 +03:00
log_error ( " Failed to allocate pvid hash. " ) ;
return 0 ;
}
2012-02-23 04:11:01 +04:00
dm_list_iterate_items ( sl , & vg - > tags )
if ( ! validate_tag ( sl - > str ) ) {
log_error ( INTERNAL_ERROR " VG %s tag %s has invalid form. " ,
vg - > name , sl - > str ) ;
r = 0 ;
}
2010-04-01 15:43:24 +04:00
dm_list_iterate_items ( pvl , & vg - > pvs ) {
if ( + + pv_count > vg - > pv_count ) {
log_error ( INTERNAL_ERROR " PV list corruption detected in VG %s. " , vg - > name ) ;
/* FIXME Dump list structure? */
r = 0 ;
}
2011-03-10 16:11:59 +03:00
2010-04-13 21:26:36 +04:00
if ( pvl - > pv - > vg ! = vg ) {
log_error ( INTERNAL_ERROR " VG %s PV list entry points "
2011-03-10 16:11:59 +03:00
" to different VG %s. " , vg - > name ,
2010-04-13 21:26:36 +04:00
pvl - > pv - > vg ? pvl - > pv - > vg - > name : " NULL " ) ;
r = 0 ;
}
2007-03-23 15:43:17 +03:00
if ( strcmp ( pvl - > pv - > vg_name , vg - > name ) ) {
2010-01-07 17:29:53 +03:00
log_error ( INTERNAL_ERROR " VG name for PV %s is corrupted. " ,
2007-10-12 18:29:32 +04:00
pv_dev_name ( pvl - > pv ) ) ;
2007-03-23 15:43:17 +03:00
r = 0 ;
}
2011-03-10 16:11:59 +03:00
2011-03-30 17:35:51 +04:00
if ( dm_hash_lookup_binary ( vhash . pvid , & pvl - > pv - > id ,
2011-03-10 16:11:59 +03:00
sizeof ( pvl - > pv - > id ) ) ) {
if ( ! id_write_format ( & pvl - > pv - > id , uuid ,
sizeof ( uuid ) ) )
stack ;
log_error ( INTERNAL_ERROR " Duplicate PV id "
" %s detected for %s in %s. " ,
uuid , pv_dev_name ( pvl - > pv ) ,
vg - > name ) ;
r = 0 ;
}
2012-02-23 04:11:01 +04:00
dm_list_iterate_items ( sl , & pvl - > pv - > tags )
if ( ! validate_tag ( sl - > str ) ) {
log_error ( INTERNAL_ERROR " PV %s tag %s has invalid form. " ,
pv_dev_name ( pvl - > pv ) , sl - > str ) ;
r = 0 ;
}
2011-03-30 17:35:51 +04:00
if ( ! dm_hash_insert_binary ( vhash . pvid , & pvl - > pv - > id ,
2011-03-10 16:11:59 +03:00
sizeof ( pvl - > pv - > id ) , pvl - > pv ) ) {
log_error ( " Failed to hash pvid. " ) ;
r = 0 ;
break ;
}
2006-08-09 23:33:25 +04:00
}
2002-04-24 22:20:51 +04:00
2011-03-10 16:11:59 +03:00
2005-05-03 21:28:23 +04:00
if ( ! check_pv_segments ( vg ) ) {
2009-12-16 22:22:11 +03:00
log_error ( INTERNAL_ERROR " PV segments corrupted in %s. " ,
2005-05-03 21:28:23 +04:00
vg - > name ) ;
2006-08-09 23:33:25 +04:00
r = 0 ;
}
2015-03-23 15:32:00 +03:00
dm_list_iterate_items ( lvl , & vg - > removed_lvs ) {
if ( ! ( lvl - > lv - > status & LV_REMOVED ) ) {
log_error ( INTERNAL_ERROR " LV %s is not marked as removed while it's part "
" of removed LV list for VG %s " , lvl - > lv - > name , vg - > name ) ;
r = 0 ;
}
}
2009-05-14 01:27:43 +04:00
/*
* Count all non - snapshot invisible LVs
*/
dm_list_iterate_items ( lvl , & vg - > lvs ) {
2010-04-01 15:43:24 +04:00
lv_count + + ;
2015-03-23 15:32:00 +03:00
if ( lvl - > lv - > status & LV_REMOVED ) {
log_error ( INTERNAL_ERROR " LV %s is marked as removed while it's "
" still part of the VG %s " , lvl - > lv - > name , vg - > name ) ;
r = 0 ;
}
2015-03-09 21:56:24 +03:00
if ( lvl - > lv - > status & LVM_WRITE_LOCKED ) {
log_error ( INTERNAL_ERROR " LV %s has external flag LVM_WRITE_LOCKED set internally. " ,
2015-03-17 20:48:56 +03:00
lvl - > lv - > name ) ;
2015-03-09 21:56:24 +03:00
r = 0 ;
}
2014-03-31 13:51:50 +04:00
dev_name_len = strlen ( lvl - > lv - > name ) + vg_name_len + 3 ;
if ( dev_name_len > = NAME_LEN ) {
log_error ( INTERNAL_ERROR " LV name \" %s/%s \" length % "
PRIsize_t " is not supported. " ,
vg - > name , lvl - > lv - > name , dev_name_len ) ;
r = 0 ;
}
2014-07-03 22:06:04 +04:00
if ( ! id_equal ( & lvl - > lv - > lvid . id [ 0 ] , & lvl - > lv - > vg - > id ) ) {
if ( ! id_write_format ( & lvl - > lv - > lvid . id [ 0 ] , uuid ,
sizeof ( uuid ) ) )
stack ;
if ( ! id_write_format ( & lvl - > lv - > vg - > id , uuid2 ,
sizeof ( uuid2 ) ) )
stack ;
log_error ( INTERNAL_ERROR " LV %s has VG UUID %s but its VG %s has UUID %s " ,
lvl - > lv - > name , uuid , lvl - > lv - > vg - > name , uuid2 ) ;
r = 0 ;
}
2014-03-31 13:51:50 +04:00
2014-09-21 13:34:50 +04:00
if ( lv_is_pool_metadata_spare ( lvl - > lv ) ) {
if ( + + spare_count > 1 ) {
2014-11-11 17:13:00 +03:00
log_error ( INTERNAL_ERROR " LV %s is extra pool metadata spare volume. %u found but only 1 allowed. " ,
2014-09-21 13:34:50 +04:00
lvl - > lv - > name , spare_count ) ;
r = 0 ;
}
if ( vg - > pool_metadata_spare_lv ! = lvl - > lv ) {
2014-11-11 17:13:00 +03:00
log_error ( INTERNAL_ERROR " LV %s is not the VG's pool metadata spare volume. " ,
2014-09-21 13:34:50 +04:00
lvl - > lv - > name ) ;
r = 0 ;
}
}
2010-04-01 15:43:24 +04:00
if ( lv_is_cow ( lvl - > lv ) )
num_snapshots + + ;
if ( lv_is_visible ( lvl - > lv ) )
lv_visible_count + + ;
2010-04-01 17:08:06 +04:00
if ( ! check_lv_segments ( lvl - > lv , 0 ) ) {
log_error ( INTERNAL_ERROR " LV segments corrupted in %s. " ,
lvl - > lv - > name ) ;
r = 0 ;
}
2010-11-09 15:34:40 +03:00
if ( lvl - > lv - > alloc = = ALLOC_CLING_BY_TAGS ) {
log_error ( INTERNAL_ERROR " LV %s allocation policy set to invalid cling_by_tags. " ,
lvl - > lv - > name ) ;
r = 0 ;
}
2012-02-23 04:11:01 +04:00
if ( ! validate_name ( lvl - > lv - > name ) ) {
log_error ( INTERNAL_ERROR " LV name %s has invalid form. " , lvl - > lv - > name ) ;
r = 0 ;
}
dm_list_iterate_items ( sl , & lvl - > lv - > tags )
if ( ! validate_tag ( sl - > str ) ) {
log_error ( INTERNAL_ERROR " LV %s tag %s has invalid form. " ,
lvl - > lv - > name , sl - > str ) ;
r = 0 ;
}
2009-05-14 01:27:43 +04:00
if ( lvl - > lv - > status & VISIBLE_LV )
continue ;
/* snapshots */
2009-05-27 17:19:34 +04:00
if ( lv_is_cow ( lvl - > lv ) )
continue ;
/* virtual origins are always hidden */
if ( lv_is_origin ( lvl - > lv ) & & ! lv_is_virtual_origin ( lvl - > lv ) )
2009-05-14 01:27:43 +04:00
continue ;
/* count other non-snapshot invisible volumes */
2009-05-27 17:19:34 +04:00
hidden_lv_count + + ;
2009-05-14 01:27:43 +04:00
/*
* FIXME : add check for unreferenced invisible LVs
* - snapshot cow & origin
* - mirror log & images
* - mirror conversion volumes ( _mimagetmp * )
*/
}
/*
* all volumes = visible LVs + snapshot_cows + invisible LVs
*/
2010-04-01 15:43:24 +04:00
if ( lv_count ! = lv_visible_count + num_snapshots + hidden_lv_count ) {
2013-07-05 19:10:11 +04:00
log_error ( INTERNAL_ERROR " #LVs (%u) != #visible LVs (%u) "
" + #snapshots (%u) + #internal LVs (%u) in VG %s " ,
lv_count , lv_visible_count , num_snapshots ,
hidden_lv_count , vg - > name ) ;
2008-06-06 23:28:35 +04:00
r = 0 ;
2008-04-22 16:54:33 +04:00
}
2010-04-01 17:08:06 +04:00
/* Avoid endless loop if lv->segments list is corrupt */
if ( ! r )
2011-03-30 17:35:51 +04:00
goto out ;
2010-04-01 17:08:06 +04:00
2011-03-30 17:35:51 +04:00
if ( ! ( vhash . lvname = dm_hash_create ( lv_count ) ) ) {
2011-03-10 16:11:59 +03:00
log_error ( " Failed to allocate lv_name hash " ) ;
2011-03-30 17:35:51 +04:00
r = 0 ;
goto out ;
2011-03-10 16:11:59 +03:00
}
2011-03-30 17:35:51 +04:00
if ( ! ( vhash . lvid = dm_hash_create ( lv_count ) ) ) {
2011-03-10 16:11:59 +03:00
log_error ( " Failed to allocate uuid hash " ) ;
2011-03-30 17:35:51 +04:00
r = 0 ;
goto out ;
2011-03-10 16:11:59 +03:00
}
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( lvl , & vg - > lvs ) {
2011-03-30 17:35:51 +04:00
if ( dm_hash_lookup ( vhash . lvname , lvl - > lv - > name ) ) {
2011-03-10 16:11:59 +03:00
log_error ( INTERNAL_ERROR
" Duplicate LV name %s detected in %s. " ,
lvl - > lv - > name , vg - > name ) ;
r = 0 ;
}
2011-03-30 17:35:51 +04:00
if ( dm_hash_lookup_binary ( vhash . lvid , & lvl - > lv - > lvid . id [ 1 ] ,
2011-03-10 16:11:59 +03:00
sizeof ( lvl - > lv - > lvid . id [ 1 ] ) ) ) {
if ( ! id_write_format ( & lvl - > lv - > lvid . id [ 1 ] , uuid ,
sizeof ( uuid ) ) )
stack ;
log_error ( INTERNAL_ERROR " Duplicate LV id "
" %s detected for %s in %s. " ,
uuid , lvl - > lv - > name , vg - > name ) ;
r = 0 ;
2006-08-09 23:33:25 +04:00
}
2005-05-03 21:28:23 +04:00
2005-10-28 01:51:28 +04:00
if ( ! check_lv_segments ( lvl - > lv , 1 ) ) {
2009-12-16 22:22:11 +03:00
log_error ( INTERNAL_ERROR " LV segments corrupted in %s. " ,
2005-06-01 20:51:55 +04:00
lvl - > lv - > name ) ;
2006-08-09 23:33:25 +04:00
r = 0 ;
2005-06-01 20:51:55 +04:00
}
2011-03-10 16:11:59 +03:00
2011-03-30 17:35:51 +04:00
if ( ! dm_hash_insert ( vhash . lvname , lvl - > lv - > name , lvl ) ) {
2011-03-10 16:11:59 +03:00
log_error ( " Failed to hash lvname. " ) ;
r = 0 ;
break ;
}
2011-03-30 17:35:51 +04:00
if ( ! dm_hash_insert_binary ( vhash . lvid , & lvl - > lv - > lvid . id [ 1 ] ,
2011-03-11 01:39:36 +03:00
sizeof ( lvl - > lv - > lvid . id [ 1 ] ) , lvl - > lv ) ) {
2011-03-10 16:11:59 +03:00
log_error ( " Failed to hash lvid. " ) ;
r = 0 ;
break ;
}
2005-06-01 20:51:55 +04:00
}
2011-03-30 17:35:51 +04:00
if ( ! _lv_postorder_vg ( vg , _lv_validate_references_single , & vhash ) ) {
2011-03-10 17:40:32 +03:00
stack ;
r = 0 ;
2010-12-14 20:51:09 +03:00
}
dm_list_iterate_items ( lvl , & vg - > lvs ) {
2014-09-16 00:33:53 +04:00
if ( ! lv_is_pvmove ( lvl - > lv ) )
2010-12-14 20:07:35 +03:00
continue ;
dm_list_iterate_items ( seg , & lvl - > lv - > segments ) {
if ( seg_is_mirrored ( seg ) ) {
if ( seg - > area_count ! = 2 ) {
log_error ( INTERNAL_ERROR
2011-03-10 16:11:59 +03:00
" Segment in %s is not 2-way. " ,
lvl - > lv - > name ) ;
2010-12-14 20:07:35 +03:00
r = 0 ;
}
} else if ( seg - > area_count ! = 1 ) {
log_error ( INTERNAL_ERROR
2011-03-10 16:11:59 +03:00
" Segment in %s has wrong number of areas: %d. " ,
lvl - > lv - > name , seg - > area_count ) ;
2010-12-14 20:07:35 +03:00
r = 0 ;
}
}
}
2008-08-29 17:41:21 +04:00
if ( ! ( vg - > fid - > fmt - > features & FMT_UNLIMITED_VOLS ) & &
( ! vg - > max_lv | | ! vg - > max_pv ) ) {
2009-12-16 22:22:11 +03:00
log_error ( INTERNAL_ERROR " Volume group %s has limited PV/LV count "
2008-08-29 17:41:21 +04:00
" but limit is not set. " , vg - > name ) ;
r = 0 ;
}
2013-07-05 19:10:47 +04:00
if ( vg - > pool_metadata_spare_lv & &
! lv_is_pool_metadata_spare ( vg - > pool_metadata_spare_lv ) ) {
log_error ( INTERNAL_ERROR " VG references non pool metadata spare LV %s. " ,
vg - > pool_metadata_spare_lv - > name ) ;
r = 0 ;
}
2009-05-14 01:29:10 +04:00
if ( vg_max_lv_reached ( vg ) )
stack ;
2015-03-05 23:00:44 +03:00
if ( ! ( vhash . lv_lock_args = dm_hash_create ( lv_count ) ) ) {
log_error ( " Failed to allocate lv_lock_args hash " ) ;
r = 0 ;
goto out ;
}
if ( is_lockd_type ( vg - > lock_type ) ) {
if ( ! vg - > lock_args ) {
log_error ( INTERNAL_ERROR " VG %s with lock_type %s without lock_args " ,
vg - > name , vg - > lock_type ) ;
r = 0 ;
}
if ( vg_is_clustered ( vg ) ) {
log_error ( INTERNAL_ERROR " VG %s with lock_type %s is clustered " ,
vg - > name , vg - > lock_type ) ;
r = 0 ;
}
if ( vg - > system_id & & vg - > system_id [ 0 ] ) {
log_error ( INTERNAL_ERROR " VG %s with lock_type %s has system_id %s " ,
vg - > name , vg - > lock_type , vg - > system_id ) ;
r = 0 ;
}
if ( strcmp ( vg - > lock_type , " sanlock " ) & & strcmp ( vg - > lock_type , " dlm " ) ) {
log_error ( INTERNAL_ERROR " VG %s has unknown lock_type %s " ,
vg - > name , vg - > lock_type ) ;
r = 0 ;
}
2015-07-09 21:24:28 +03:00
2015-07-10 19:41:29 +03:00
if ( ! _validate_vg_lock_args ( vg ) )
2015-07-09 21:24:28 +03:00
r = 0 ;
2015-03-05 23:00:44 +03:00
} else {
if ( vg - > lock_args ) {
log_error ( INTERNAL_ERROR " VG %s has lock_args %s without lock_type " ,
vg - > name , vg - > lock_args ) ;
r = 0 ;
}
}
dm_list_iterate_items ( lvl , & vg - > lvs ) {
if ( is_lockd_type ( vg - > lock_type ) ) {
if ( lockd_lv_uses_lock ( lvl - > lv ) ) {
2015-07-09 21:24:28 +03:00
if ( vg - > skip_validate_lock_args )
2015-03-05 23:00:44 +03:00
continue ;
2015-07-09 21:24:28 +03:00
2015-07-10 19:41:29 +03:00
/*
* FIXME : make missing lock_args an error .
* There are at least two cases where this
* check doesn ' t work correctly :
*
* 1. When creating a cow snapshot ,
* ( lvcreate - s - L1M - n snap1 vg / lv1 ) ,
* lockd_lv_uses_lock ( ) uses lv_is_cow ( )
* which depends on lv - > snapshot being
* set , but it ' s not set at this point ,
* so lockd_lv_uses_lock ( ) cannot identify
* the LV as a cow_lv , and thinks it needs
* a lock when it doesn ' t . To fix this we
* probably need to validate by finding the
* origin LV , then finding all its snapshots
* which will have no lock_args .
*
* 2. When converting an LV to a thin pool
* without using an existing metadata LV ,
* ( lvconvert - - type thin - pool vg / poolX ) ,
* there is an intermediate LV created ,
* probably for the metadata LV , and
* validate is called on the VG in this
* intermediate state , which finds the
* newly created LV which is not yet
* identified as a metadata LV , and
* does not have any lock_args . To fix
* this we might be able to find the place
* where the intermediate LV is created ,
* and set new variable on it like for vgs ,
* lv - > skip_validate_lock_args .
*/
2015-07-09 21:24:28 +03:00
if ( ! lvl - > lv - > lock_args ) {
2015-07-10 19:41:29 +03:00
/*
log_verbose ( " LV %s/%s missing lock_args " ,
vg - > name , lvl - > lv - > name ) ;
2015-03-05 23:00:44 +03:00
r = 0 ;
2015-07-10 19:41:29 +03:00
*/
2015-07-09 21:24:28 +03:00
continue ;
}
if ( ! _validate_lv_lock_args ( lvl - > lv ) ) {
r = 0 ;
continue ;
}
if ( ! strcmp ( vg - > lock_type , " sanlock " ) ) {
2015-03-05 23:00:44 +03:00
if ( dm_hash_lookup ( vhash . lv_lock_args , lvl - > lv - > lock_args ) ) {
log_error ( INTERNAL_ERROR " LV %s/%s has duplicate lock_args %s. " ,
vg - > name , lvl - > lv - > name , lvl - > lv - > lock_args ) ;
r = 0 ;
}
if ( ! dm_hash_insert ( vhash . lv_lock_args , lvl - > lv - > lock_args , lvl ) ) {
log_error ( " Failed to hash lvname. " ) ;
r = 0 ;
}
}
} else {
if ( lvl - > lv - > lock_args ) {
log_error ( INTERNAL_ERROR " LV %s/%s shouldn't have lock_args " ,
vg - > name , lvl - > lv - > name ) ;
r = 0 ;
}
}
} else {
if ( lvl - > lv - > lock_args ) {
log_error ( INTERNAL_ERROR " LV %s/%s with no lock_type has lock_args %s " ,
vg - > name , lvl - > lv - > name , lvl - > lv - > lock_args ) ;
r = 0 ;
}
}
}
2011-03-30 17:35:51 +04:00
out :
if ( vhash . lvid )
dm_hash_destroy ( vhash . lvid ) ;
if ( vhash . lvname )
dm_hash_destroy ( vhash . lvname ) ;
if ( vhash . pvid )
dm_hash_destroy ( vhash . pvid ) ;
2015-03-05 23:00:44 +03:00
if ( vhash . lv_lock_args )
dm_hash_destroy ( vhash . lv_lock_args ) ;
2009-03-16 17:34:57 +03:00
2006-08-09 23:33:25 +04:00
return r ;
2005-07-12 23:40:59 +04:00
}
/*
* After vg_write ( ) returns success ,
* caller MUST call either vg_commit ( ) or vg_revert ( )
*/
int vg_write ( struct volume_group * vg )
{
2008-11-04 01:14:30 +03:00
struct dm_list * mdah ;
2015-09-12 00:12:03 +03:00
struct pv_to_create * pv_to_create , * pv_to_create_safe ;
2005-07-12 23:40:59 +04:00
struct metadata_area * mda ;
2015-03-05 23:00:44 +03:00
struct lv_list * lvl ;
2014-05-26 16:23:33 +04:00
int revert = 0 , wrote = 0 ;
2005-07-12 23:40:59 +04:00
2015-03-05 23:00:44 +03:00
dm_list_iterate_items ( lvl , & vg - > lvs ) {
if ( lvl - > lv - > lock_args & & ! strcmp ( lvl - > lv - > lock_args , " pending " ) ) {
if ( ! lockd_init_lv_args ( vg - > cmd , vg , lvl - > lv , vg - > lock_type , & lvl - > lv - > lock_args ) ) {
log_error ( " Cannot allocate lock for new LV. " ) ;
return 0 ;
}
lvl - > lv - > new_lock_args = 1 ;
}
}
2008-01-30 16:19:47 +03:00
if ( ! vg_validate ( vg ) )
return_0 ;
2005-07-12 23:40:59 +04:00
2002-04-30 21:12:37 +04:00
if ( vg - > status & PARTIAL_VG ) {
2008-09-19 10:42:00 +04:00
log_error ( " Cannot update partial volume group %s. " , vg - > name ) ;
return 0 ;
}
if ( vg_missing_pv_count ( vg ) & & ! vg - > cmd - > handles_missing_pvs ) {
log_error ( " Cannot update volume group %s while physical "
" volumes are missing. " , vg - > name ) ;
2002-04-30 21:12:37 +04:00
return 0 ;
}
2009-10-16 21:41:49 +04:00
if ( vg_has_unknown_segments ( vg ) & & ! vg - > cmd - > handles_unknown_segments ) {
log_error ( " Cannot update volume group %s with unknown segments in it! " ,
vg - > name ) ;
return 0 ;
}
2010-06-30 23:28:35 +04:00
if ( ( vg - > fid - > fmt - > features & FMT_MDAS ) & & ! _vg_adjust_ignored_mdas ( vg ) )
2010-06-30 17:51:11 +04:00
return_0 ;
2009-10-16 21:41:49 +04:00
2010-06-30 23:28:35 +04:00
if ( ! vg_mda_used_count ( vg ) ) {
2002-11-18 17:04:08 +03:00
log_error ( " Aborting vg_write: No metadata areas to write to! " ) ;
return 0 ;
}
2008-05-08 22:06:58 +04:00
if ( ! drop_cached_metadata ( vg ) ) {
log_error ( " Unable to drop cached metadata for VG %s. " , vg - > name ) ;
return 0 ;
}
2011-11-18 23:28:00 +04:00
if ( critical_section ( ) )
log_error ( INTERNAL_ERROR
" Writing metadata in critical section. " ) ;
/* Unlock memory if possible */
memlock_unlock ( vg - > cmd ) ;
2002-04-24 22:20:51 +04:00
vg - > seqno + + ;
2015-09-12 00:12:03 +03:00
dm_list_iterate_items_safe ( pv_to_create , pv_to_create_safe , & vg - > pvs_to_create ) {
2011-06-01 23:29:31 +04:00
if ( ! _pvcreate_write ( vg - > cmd , pv_to_create ) )
return 0 ;
2015-09-12 00:12:03 +03:00
dm_list_del ( & pv_to_create - > list ) ;
}
2011-06-01 23:29:31 +04:00
2002-04-24 22:20:51 +04:00
/* Write to each copy of the metadata area */
2010-06-29 00:32:44 +04:00
dm_list_iterate_items ( mda , & vg - > fid - > metadata_areas_in_use ) {
2004-03-27 00:07:30 +03:00
if ( ! mda - > ops - > vg_write ) {
2003-08-27 01:12:06 +04:00
log_error ( " Format does not support writing volume "
" group metadata areas " ) ;
2014-05-26 16:23:33 +04:00
revert = 1 ;
break ;
2003-08-27 01:12:06 +04:00
}
2002-11-18 17:04:08 +03:00
if ( ! mda - > ops - > vg_write ( vg - > fid , vg , mda ) ) {
2014-05-26 16:23:33 +04:00
if ( vg - > cmd - > handles_missing_pvs ) {
2015-01-09 16:04:44 +03:00
log_warn ( " WARNING: Failed to write an MDA of VG %s. " , vg - > name ) ;
2014-05-26 16:23:33 +04:00
mda - > status | = MDA_FAILED ;
} else {
stack ;
revert = 1 ;
break ;
}
} else
+ + wrote ;
}
2005-06-01 20:51:55 +04:00
2014-05-26 16:23:33 +04:00
if ( revert | | ! wrote ) {
2015-01-09 16:04:44 +03:00
log_error ( " Failed to write VG %s. " , vg - > name ) ;
2014-05-26 16:23:33 +04:00
dm_list_uniterate ( mdah , & vg - > fid - > metadata_areas_in_use , & mda - > list ) {
mda = dm_list_item ( mdah , struct metadata_area ) ;
if ( mda - > ops - > vg_revert & &
! mda - > ops - > vg_revert ( vg - > fid , vg , mda ) ) {
stack ;
2003-07-05 02:34:56 +04:00
}
2002-04-24 22:20:51 +04:00
}
2014-05-26 16:23:33 +04:00
return 0 ;
2002-04-24 22:20:51 +04:00
}
2005-04-06 22:59:55 +04:00
/* Now pre-commit each copy of the new metadata */
2010-06-29 00:32:44 +04:00
dm_list_iterate_items ( mda , & vg - > fid - > metadata_areas_in_use ) {
2014-05-26 16:23:33 +04:00
if ( mda - > status & MDA_FAILED )
continue ;
2005-04-06 22:59:55 +04:00
if ( mda - > ops - > vg_precommit & &
! mda - > ops - > vg_precommit ( vg - > fid , vg , mda ) ) {
stack ;
/* Revert */
2010-06-29 00:32:44 +04:00
dm_list_iterate_items ( mda , & vg - > fid - > metadata_areas_in_use ) {
2014-05-26 16:23:33 +04:00
if ( mda - > status & MDA_FAILED )
continue ;
2005-04-06 22:59:55 +04:00
if ( mda - > ops - > vg_revert & &
! mda - > ops - > vg_revert ( vg - > fid , vg , mda ) ) {
stack ;
}
}
return 0 ;
}
}
2014-03-01 01:40:00 +04:00
if ( ! _vg_update_vg_precommitted ( vg ) ) /* prepare precommited */
return_0 ;
2013-01-05 07:07:43 +04:00
/*
* If precommit is not supported , changes take effect immediately .
* FIXME Replace with a more - accurate FMT_COMMIT flag .
*/
if ( ! ( vg - > fid - > fmt - > features & FMT_PRECOMMIT ) & & ! lvmetad_vg_update ( vg ) )
return_0 ;
2015-12-16 01:14:49 +03:00
lockd_vg_update ( vg ) ;
2003-07-05 02:34:56 +04:00
return 1 ;
}
2010-06-29 00:35:33 +04:00
static int _vg_commit_mdas ( struct volume_group * vg )
2003-07-05 02:34:56 +04:00
{
Before committing each mda, arrange mdas so ignored mdas get committed first.
Arrange mdas so mdas that are to be ignored come first. This is an
optimization that ensures consistency on disk for the longest period of time.
This was noted by agk in review of the v4 patchset of pvchange-based mda
balance.
Note the following example for an explanation of the background:
Assume the initial state on disk is as follows:
PV0 (v1, non-ignored)
PV1 (v1, non-ignored)
PV2 (v1, non-ignored)
PV3 (v1, non-ignored)
If we did not sort the list, we would have a commit sequence something like
this:
PV0 (v2, non-ignored)
PV1 (v2, ignored)
PV2 (v2, ignored)
PV3 (v2, non-ignored)
After the commit of PV0's mdas, we'd have an on-disk state like this:
PV0 (v2, non-ignored)
PV1 (v1, non-ignored)
PV2 (v1, non-ignored)
PV3 (v1, non-ignored)
This is an inconsistent state of the disk. If the machine fails, the next
time it was brought back up, the auto-correct mechanism in vg_read would
update the metadata on PV1-PV3. However, if possible we try to avoid
inconsistent on-disk states. Clearly, because we did not sort, we have
a greater chance of on-disk inconsistency - from the time the commit of
PV0 is complete until the time PV3 is complete.
We could improve the amount of time the on-disk state is consistent by simply
sorting the commit order as follows:
PV1 (v2, ignored)
PV2 (v2, ignored)
PV0 (v2, non-ignored)
PV3 (v2, non-ignored)
Thus, after the first PV is committed (in this case PV1), on-disk we would
have:
PV0 (v1, non-ignored)
PV1 (v2, ignored)
PV2 (v1, non-ignored)
PV3 (v1, non-ignored)
This is clearly a consistent state. PV1 will be read but the mda will be
ignored. All other PVs contain v1 metadata, and no auto-correct will be
required. In fact, if we commit all PVs with ignored mdas first, we'll
only have an inconsistent state when we start writing non-ignored PVs,
and thus the chances we'll get an inconsistent state on disk is much
less with the sorted method.
Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
2010-06-29 00:35:49 +04:00
struct metadata_area * mda , * tmda ;
struct dm_list ignored ;
2003-07-05 02:34:56 +04:00
int failed = 0 ;
2010-06-29 00:35:33 +04:00
int cache_updated = 0 ;
2008-04-04 19:41:20 +04:00
Before committing each mda, arrange mdas so ignored mdas get committed first.
Arrange mdas so mdas that are to be ignored come first. This is an
optimization that ensures consistency on disk for the longest period of time.
This was noted by agk in review of the v4 patchset of pvchange-based mda
balance.
Note the following example for an explanation of the background:
Assume the initial state on disk is as follows:
PV0 (v1, non-ignored)
PV1 (v1, non-ignored)
PV2 (v1, non-ignored)
PV3 (v1, non-ignored)
If we did not sort the list, we would have a commit sequence something like
this:
PV0 (v2, non-ignored)
PV1 (v2, ignored)
PV2 (v2, ignored)
PV3 (v2, non-ignored)
After the commit of PV0's mdas, we'd have an on-disk state like this:
PV0 (v2, non-ignored)
PV1 (v1, non-ignored)
PV2 (v1, non-ignored)
PV3 (v1, non-ignored)
This is an inconsistent state of the disk. If the machine fails, the next
time it was brought back up, the auto-correct mechanism in vg_read would
update the metadata on PV1-PV3. However, if possible we try to avoid
inconsistent on-disk states. Clearly, because we did not sort, we have
a greater chance of on-disk inconsistency - from the time the commit of
PV0 is complete until the time PV3 is complete.
We could improve the amount of time the on-disk state is consistent by simply
sorting the commit order as follows:
PV1 (v2, ignored)
PV2 (v2, ignored)
PV0 (v2, non-ignored)
PV3 (v2, non-ignored)
Thus, after the first PV is committed (in this case PV1), on-disk we would
have:
PV0 (v1, non-ignored)
PV1 (v2, ignored)
PV2 (v1, non-ignored)
PV3 (v1, non-ignored)
This is clearly a consistent state. PV1 will be read but the mda will be
ignored. All other PVs contain v1 metadata, and no auto-correct will be
required. In fact, if we commit all PVs with ignored mdas first, we'll
only have an inconsistent state when we start writing non-ignored PVs,
and thus the chances we'll get an inconsistent state on disk is much
less with the sorted method.
Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
2010-06-29 00:35:49 +04:00
/* Rearrange the metadata_areas_in_use so ignored mdas come first. */
dm_list_init ( & ignored ) ;
2010-06-30 21:13:05 +04:00
dm_list_iterate_items_safe ( mda , tmda , & vg - > fid - > metadata_areas_in_use )
Before committing each mda, arrange mdas so ignored mdas get committed first.
Arrange mdas so mdas that are to be ignored come first. This is an
optimization that ensures consistency on disk for the longest period of time.
This was noted by agk in review of the v4 patchset of pvchange-based mda
balance.
Note the following example for an explanation of the background:
Assume the initial state on disk is as follows:
PV0 (v1, non-ignored)
PV1 (v1, non-ignored)
PV2 (v1, non-ignored)
PV3 (v1, non-ignored)
If we did not sort the list, we would have a commit sequence something like
this:
PV0 (v2, non-ignored)
PV1 (v2, ignored)
PV2 (v2, ignored)
PV3 (v2, non-ignored)
After the commit of PV0's mdas, we'd have an on-disk state like this:
PV0 (v2, non-ignored)
PV1 (v1, non-ignored)
PV2 (v1, non-ignored)
PV3 (v1, non-ignored)
This is an inconsistent state of the disk. If the machine fails, the next
time it was brought back up, the auto-correct mechanism in vg_read would
update the metadata on PV1-PV3. However, if possible we try to avoid
inconsistent on-disk states. Clearly, because we did not sort, we have
a greater chance of on-disk inconsistency - from the time the commit of
PV0 is complete until the time PV3 is complete.
We could improve the amount of time the on-disk state is consistent by simply
sorting the commit order as follows:
PV1 (v2, ignored)
PV2 (v2, ignored)
PV0 (v2, non-ignored)
PV3 (v2, non-ignored)
Thus, after the first PV is committed (in this case PV1), on-disk we would
have:
PV0 (v1, non-ignored)
PV1 (v2, ignored)
PV2 (v1, non-ignored)
PV3 (v1, non-ignored)
This is clearly a consistent state. PV1 will be read but the mda will be
ignored. All other PVs contain v1 metadata, and no auto-correct will be
required. In fact, if we commit all PVs with ignored mdas first, we'll
only have an inconsistent state when we start writing non-ignored PVs,
and thus the chances we'll get an inconsistent state on disk is much
less with the sorted method.
Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
2010-06-29 00:35:49 +04:00
if ( mda_is_ignored ( mda ) )
dm_list_move ( & ignored , & mda - > list ) ;
2010-06-30 21:13:05 +04:00
dm_list_iterate_items_safe ( mda , tmda , & ignored )
Before committing each mda, arrange mdas so ignored mdas get committed first.
Arrange mdas so mdas that are to be ignored come first. This is an
optimization that ensures consistency on disk for the longest period of time.
This was noted by agk in review of the v4 patchset of pvchange-based mda
balance.
Note the following example for an explanation of the background:
Assume the initial state on disk is as follows:
PV0 (v1, non-ignored)
PV1 (v1, non-ignored)
PV2 (v1, non-ignored)
PV3 (v1, non-ignored)
If we did not sort the list, we would have a commit sequence something like
this:
PV0 (v2, non-ignored)
PV1 (v2, ignored)
PV2 (v2, ignored)
PV3 (v2, non-ignored)
After the commit of PV0's mdas, we'd have an on-disk state like this:
PV0 (v2, non-ignored)
PV1 (v1, non-ignored)
PV2 (v1, non-ignored)
PV3 (v1, non-ignored)
This is an inconsistent state of the disk. If the machine fails, the next
time it was brought back up, the auto-correct mechanism in vg_read would
update the metadata on PV1-PV3. However, if possible we try to avoid
inconsistent on-disk states. Clearly, because we did not sort, we have
a greater chance of on-disk inconsistency - from the time the commit of
PV0 is complete until the time PV3 is complete.
We could improve the amount of time the on-disk state is consistent by simply
sorting the commit order as follows:
PV1 (v2, ignored)
PV2 (v2, ignored)
PV0 (v2, non-ignored)
PV3 (v2, non-ignored)
Thus, after the first PV is committed (in this case PV1), on-disk we would
have:
PV0 (v1, non-ignored)
PV1 (v2, ignored)
PV2 (v1, non-ignored)
PV3 (v1, non-ignored)
This is clearly a consistent state. PV1 will be read but the mda will be
ignored. All other PVs contain v1 metadata, and no auto-correct will be
required. In fact, if we commit all PVs with ignored mdas first, we'll
only have an inconsistent state when we start writing non-ignored PVs,
and thus the chances we'll get an inconsistent state on disk is much
less with the sorted method.
Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
2010-06-29 00:35:49 +04:00
dm_list_move ( & vg - > fid - > metadata_areas_in_use , & mda - > list ) ;
2002-04-24 22:20:51 +04:00
/* Commit to each copy of the metadata area */
2010-06-29 00:32:44 +04:00
dm_list_iterate_items ( mda , & vg - > fid - > metadata_areas_in_use ) {
2014-05-26 16:23:33 +04:00
if ( mda - > status & MDA_FAILED )
continue ;
2003-07-05 02:34:56 +04:00
failed = 0 ;
2002-11-18 17:04:08 +03:00
if ( mda - > ops - > vg_commit & &
! mda - > ops - > vg_commit ( vg - > fid , vg , mda ) ) {
2002-04-24 22:20:51 +04:00
stack ;
2003-07-05 02:34:56 +04:00
failed = 1 ;
}
/* Update cache first time we succeed */
if ( ! failed & & ! cache_updated ) {
2008-03-17 19:51:31 +03:00
lvmcache_update_vg ( vg , 0 ) ;
2012-02-23 17:11:07 +04:00
// lvmetad_vg_commit(vg);
2003-07-05 02:34:56 +04:00
cache_updated = 1 ;
}
}
2010-06-29 00:35:33 +04:00
return cache_updated ;
}
/* Commit pending changes */
int vg_commit ( struct volume_group * vg )
{
int cache_updated = 0 ;
2012-02-10 05:28:27 +04:00
if ( ! lvmcache_vgname_is_locked ( vg - > name ) ) {
2010-06-29 00:35:33 +04:00
log_error ( INTERNAL_ERROR " Attempt to write new VG metadata "
" without locking %s " , vg - > name ) ;
return cache_updated ;
}
2013-01-05 07:07:43 +04:00
/* Skip if we already did this in vg_write */
if ( ( vg - > fid - > fmt - > features & FMT_PRECOMMIT ) & & ! lvmetad_vg_update ( vg ) )
return_0 ;
2012-02-23 17:11:07 +04:00
2010-06-29 00:35:33 +04:00
cache_updated = _vg_commit_mdas ( vg ) ;
2003-07-05 02:34:56 +04:00
2010-04-14 17:09:16 +04:00
if ( cache_updated ) {
/* Instruct remote nodes to upgrade cached metadata. */
2012-02-27 15:35:59 +04:00
if ( ! remote_commit_cached_metadata ( vg ) )
stack ; // FIXME: What should we do?
2010-04-14 17:09:16 +04:00
/*
* We need to clear old_name after a successful commit .
* The volume_group structure could be reused later .
*/
vg - > old_name = NULL ;
2013-03-17 19:27:44 +04:00
/* This *is* the original now that it's commited. */
2015-11-25 01:29:18 +03:00
release_vg ( vg - > vg_committed ) ;
vg - > vg_committed = vg - > vg_precommitted ;
2014-02-22 04:44:21 +04:00
vg - > vg_precommitted = NULL ;
2014-03-01 01:40:00 +04:00
if ( vg - > cft_precommitted ) {
dm_config_destroy ( vg - > cft_precommitted ) ;
vg - > cft_precommitted = NULL ;
}
2010-04-14 17:09:16 +04:00
}
2010-01-05 19:09:33 +03:00
2008-06-03 21:56:54 +04:00
/* If update failed, remove any cached precommitted metadata. */
if ( ! cache_updated & & ! drop_cached_metadata ( vg ) )
2008-04-15 18:46:19 +04:00
log_error ( " Attempt to drop cached metadata failed "
" after commit for VG %s. " , vg - > name ) ;
2003-07-05 02:34:56 +04:00
/* If at least one mda commit succeeded, it was committed */
return cache_updated ;
}
/* Don't commit any pending changes */
2011-09-27 21:09:42 +04:00
void vg_revert ( struct volume_group * vg )
2003-07-05 02:34:56 +04:00
{
struct metadata_area * mda ;
2015-03-05 23:00:44 +03:00
struct lv_list * lvl ;
dm_list_iterate_items ( lvl , & vg - > lvs ) {
if ( lvl - > lv - > new_lock_args ) {
lockd_free_lv ( vg - > cmd , vg , lvl - > lv - > name , & lvl - > lv - > lvid . id [ 1 ] , lvl - > lv - > lock_args ) ;
lvl - > lv - > new_lock_args = 0 ;
}
}
2003-07-05 02:34:56 +04:00
2014-02-22 04:44:21 +04:00
release_vg ( vg - > vg_precommitted ) ; /* VG is no longer needed */
vg - > vg_precommitted = NULL ;
2014-03-01 01:40:00 +04:00
if ( vg - > cft_precommitted ) {
dm_config_destroy ( vg - > cft_precommitted ) ;
vg - > cft_precommitted = NULL ;
}
2014-02-22 04:44:21 +04:00
2010-06-29 00:32:44 +04:00
dm_list_iterate_items ( mda , & vg - > fid - > metadata_areas_in_use ) {
2003-07-05 02:34:56 +04:00
if ( mda - > ops - > vg_revert & &
! mda - > ops - > vg_revert ( vg - > fid , vg , mda ) ) {
stack ;
2002-04-24 22:20:51 +04:00
}
}
2008-06-03 21:56:54 +04:00
if ( ! drop_cached_metadata ( vg ) )
log_error ( " Attempt to drop cached metadata failed "
" after reverted update for VG %s. " , vg - > name ) ;
2012-02-27 15:35:59 +04:00
if ( ! remote_revert_cached_metadata ( vg ) )
stack ; // FIXME: What should we do?
2002-04-24 22:20:51 +04:00
}
2012-02-10 05:28:27 +04:00
struct _vg_read_orphan_baton {
struct volume_group * vg ;
2014-10-07 04:04:09 +04:00
uint32_t warn_flags ;
2012-02-10 05:28:27 +04:00
} ;
static int _vg_read_orphan_pv ( struct lvmcache_info * info , void * baton )
{
struct _vg_read_orphan_baton * b = baton ;
struct physical_volume * pv = NULL ;
struct pv_list * pvl ;
if ( ! ( pv = _pv_read ( b - > vg - > cmd , b - > vg - > vgmem , dev_name ( lvmcache_device ( info ) ) ,
2014-10-07 04:04:09 +04:00
b - > vg - > fid , b - > warn_flags , 0 ) ) ) {
2012-03-14 00:36:02 +04:00
stack ;
2012-02-10 05:28:27 +04:00
return 1 ;
}
2012-02-23 17:11:07 +04:00
2012-02-10 05:28:27 +04:00
if ( ! ( pvl = dm_pool_zalloc ( b - > vg - > vgmem , sizeof ( * pvl ) ) ) ) {
log_error ( " pv_list allocation failed " ) ;
free_pv_fid ( pv ) ;
return 0 ;
}
pvl - > pv = pv ;
add_pvl_to_vgs ( b - > vg , pvl ) ;
return 1 ;
}
2012-03-01 13:46:38 +04:00
/* Make orphan PVs look like a VG. */
2008-02-06 18:47:28 +03:00
static struct volume_group * _vg_read_orphans ( struct cmd_context * cmd ,
2014-10-07 04:04:09 +04:00
uint32_t warn_flags ,
2008-02-06 18:47:28 +03:00
const char * orphan_vgname )
2002-11-18 17:04:08 +03:00
{
2012-02-10 05:28:27 +04:00
const struct format_type * fmt ;
2003-07-05 02:34:56 +04:00
struct lvmcache_vginfo * vginfo ;
2011-03-11 18:08:31 +03:00
struct volume_group * vg = NULL ;
2012-02-10 05:28:27 +04:00
struct _vg_read_orphan_baton baton ;
2013-11-22 16:18:02 +04:00
struct pv_list * pvl , * tpvl ;
2013-02-19 06:13:59 +04:00
struct pv_list head ;
2002-11-18 17:04:08 +03:00
2013-02-19 06:13:59 +04:00
dm_list_init ( & head . list ) ;
2015-12-01 23:09:01 +03:00
lvmcache_label_scan ( cmd ) ;
2012-02-23 17:11:07 +04:00
lvmcache_seed_infos_from_lvmetad ( cmd ) ;
2008-04-08 16:49:21 +04:00
2012-02-10 05:28:27 +04:00
if ( ! ( vginfo = lvmcache_vginfo_from_vgname ( orphan_vgname , NULL ) ) )
return_NULL ;
2012-02-23 17:11:07 +04:00
if ( ! ( fmt = lvmcache_fmt_from_vgname ( cmd , orphan_vgname , NULL , 0 ) ) )
2008-01-30 16:19:47 +03:00
return_NULL ;
2002-11-18 17:04:08 +03:00
2012-02-10 06:53:03 +04:00
vg = fmt - > orphan_vg ;
2013-11-22 16:18:02 +04:00
dm_list_iterate_items_safe ( pvl , tpvl , & vg - > pvs )
if ( pvl - > pv - > status & UNLABELLED_PV )
dm_list_move ( & head . list , & pvl - > list ) ;
else
2013-02-19 06:13:59 +04:00
pv_set_fid ( pvl - > pv , NULL ) ;
2013-11-22 16:18:02 +04:00
2012-02-29 04:19:14 +04:00
dm_list_init ( & vg - > pvs ) ;
2012-02-29 04:18:27 +04:00
vg - > pv_count = 0 ;
2013-02-19 06:13:59 +04:00
vg - > extent_count = 0 ;
vg - > free_count = 0 ;
2008-04-08 02:12:37 +04:00
2014-10-07 04:04:09 +04:00
baton . warn_flags = warn_flags ;
2012-02-10 05:28:27 +04:00
baton . vg = vg ;
2012-02-13 14:58:20 +04:00
2013-11-24 22:03:29 +04:00
while ( ( pvl = ( struct pv_list * ) dm_list_first ( & head . list ) ) ) {
2013-02-19 06:13:59 +04:00
dm_list_del ( & pvl - > list ) ;
add_pvl_to_vgs ( vg , pvl ) ;
vg - > extent_count + = pvl - > pv - > pe_count ;
vg - > free_count + = pvl - > pv - > pe_count ;
}
2012-02-13 14:58:20 +04:00
if ( ! lvmcache_foreach_pv ( vginfo , _vg_read_orphan_pv , & baton ) )
2012-03-01 13:46:38 +04:00
return_NULL ;
2002-11-18 17:04:08 +03:00
return vg ;
}
2009-04-10 13:56:00 +04:00
static int _update_pv_list ( struct dm_pool * pvmem , struct dm_list * all_pvs , struct volume_group * vg )
2007-02-07 16:29:52 +03:00
{
struct pv_list * pvl , * pvl2 ;
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( pvl , & vg - > pvs ) {
dm_list_iterate_items ( pvl2 , all_pvs ) {
2007-02-07 16:29:52 +03:00
if ( pvl - > pv - > dev = = pvl2 - > pv - > dev )
goto next_pv ;
}
2009-04-10 13:56:00 +04:00
/*
* PV is not on list so add it .
*/
if ( ! ( pvl2 = _copy_pvl ( pvmem , pvl ) ) ) {
2007-02-07 16:29:52 +03:00
log_error ( " pv_list allocation for '%s' failed " ,
2007-10-12 18:29:32 +04:00
pv_dev_name ( pvl - > pv ) ) ;
2007-02-07 16:29:52 +03:00
return 0 ;
}
2008-11-04 01:14:30 +03:00
dm_list_add ( all_pvs , & pvl2 - > list ) ;
2007-02-07 16:29:52 +03:00
next_pv :
;
}
return 1 ;
}
2011-03-11 17:56:56 +03:00
static void _free_pv_list ( struct dm_list * all_pvs )
{
struct pv_list * pvl ;
dm_list_iterate_items ( pvl , all_pvs )
pvl - > pv - > fid - > fmt - > ops - > destroy_instance ( pvl - > pv - > fid ) ;
}
2011-08-11 20:31:40 +04:00
static void _destroy_fid ( struct format_instance * * fid )
{
if ( * fid ) {
( * fid ) - > fmt - > ops - > destroy_instance ( * fid ) ;
* fid = NULL ;
}
}
2009-07-29 17:26:01 +04:00
int vg_missing_pv_count ( const struct volume_group * vg )
2008-09-19 10:42:00 +04:00
{
int ret = 0 ;
struct pv_list * pvl ;
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( pvl , & vg - > pvs ) {
2010-03-16 17:37:38 +03:00
if ( is_missing_pv ( pvl - > pv ) )
2008-09-19 10:42:00 +04:00
+ + ret ;
}
return ret ;
}
2013-10-09 16:04:47 +04:00
static int _check_reappeared_pv ( struct volume_group * correct_vg ,
struct physical_volume * pv , int act )
2009-12-18 15:44:20 +03:00
{
struct pv_list * pvl ;
2013-10-09 16:04:47 +04:00
int rv = 0 ;
2009-12-18 15:44:20 +03:00
2010-11-30 14:15:54 +03:00
/*
* Skip these checks in case the tool is going to deal with missing
* PVs , especially since the resulting messages can be pretty
* confusing .
*/
if ( correct_vg - > cmd - > handles_missing_pvs )
2013-10-09 16:04:47 +04:00
return rv ;
2010-11-30 14:15:54 +03:00
2009-12-18 15:44:20 +03:00
dm_list_iterate_items ( pvl , & correct_vg - > pvs )
2010-03-16 17:37:38 +03:00
if ( pv - > dev = = pvl - > pv - > dev & & is_missing_pv ( pvl - > pv ) ) {
2013-10-09 16:04:47 +04:00
if ( act )
log_warn ( " Missing device %s reappeared, updating "
" metadata for VG %s to version %u. " ,
pv_dev_name ( pvl - > pv ) , pv_vg_name ( pvl - > pv ) ,
correct_vg - > seqno ) ;
2009-12-18 15:44:20 +03:00
if ( pvl - > pv - > pe_alloc_count = = 0 ) {
2013-10-09 16:04:47 +04:00
if ( act ) {
pv - > status & = ~ MISSING_PV ;
pvl - > pv - > status & = ~ MISSING_PV ;
}
+ + rv ;
} else if ( act )
2010-06-23 01:10:53 +04:00
log_warn ( " Device still marked missing because of allocated data "
2009-12-18 15:44:20 +03:00
" on it, remove volumes and consider vgreduce --removemissing. " ) ;
}
2013-10-09 16:04:47 +04:00
return rv ;
}
static int _repair_inconsistent_vg ( struct volume_group * vg )
{
unsigned saved_handles_missing_pvs = vg - > cmd - > handles_missing_pvs ;
2015-10-29 00:06:15 +03:00
/* Cannot write foreign VGs, the owner will repair it. */
if ( vg - > cmd - > system_id & & strcmp ( vg - > system_id , vg - > cmd - > system_id ) ) {
log_verbose ( " Skip metadata repair for foreign VG. " ) ;
return 0 ;
}
/* FIXME: do this at higher level where lvmlockd lock can be changed. */
if ( is_lockd_type ( vg - > lock_type ) ) {
log_verbose ( " Skip metadata repair for shared VG. " ) ;
return 0 ;
}
2013-10-09 16:04:47 +04:00
vg - > cmd - > handles_missing_pvs = 1 ;
if ( ! vg_write ( vg ) ) {
log_error ( " Automatic metadata correction failed " ) ;
vg - > cmd - > handles_missing_pvs = saved_handles_missing_pvs ;
return 0 ;
}
vg - > cmd - > handles_missing_pvs = saved_handles_missing_pvs ;
if ( ! vg_commit ( vg ) ) {
log_error ( " Automatic metadata correction commit failed " ) ;
return 0 ;
}
return 1 ;
2009-12-18 15:44:20 +03:00
}
2012-02-10 05:28:27 +04:00
static int _check_mda_in_use ( struct metadata_area * mda , void * _in_use )
{
int * in_use = _in_use ;
if ( ! mda_is_ignored ( mda ) )
* in_use = 1 ;
return 1 ;
}
2015-04-15 17:40:26 +03:00
static int _wipe_outdated_pvs ( struct cmd_context * cmd , struct volume_group * vg , struct dm_list * to_check )
{
struct pv_list * pvl , * pvl2 ;
char uuid [ 64 ] __attribute__ ( ( aligned ( 8 ) ) ) ;
2015-10-29 00:06:15 +03:00
/*
* Cannot write foreign VGs , the owner will repair it .
* Also , if another host is updating its VG , we may read
* the PVs while some are written but not others , making
* some PVs look outdated to us just because we ' re reading
* the VG while it ' s only partially written out .
*/
if ( cmd - > system_id & & strcmp ( vg - > system_id , cmd - > system_id ) ) {
2015-12-12 00:28:46 +03:00
log_debug_metadata ( " Skip wiping outdated PVs for foreign VG. " ) ;
2015-10-29 00:06:15 +03:00
return 0 ;
}
/*
* FIXME : do this at higher level where lvmlockd lock can be changed .
* Also if we ' re reading the VG with the - - shared option ( not using
* lvmlockd ) , we can see a VG while it ' s being written by another
* host , same as the foreign VG case .
*/
if ( is_lockd_type ( vg - > lock_type ) ) {
2015-12-12 00:28:46 +03:00
log_debug_metadata ( " Skip wiping outdated PVs for shared VG. " ) ;
2015-10-29 00:06:15 +03:00
return 0 ;
}
2015-04-15 17:40:26 +03:00
dm_list_iterate_items ( pvl , to_check ) {
dm_list_iterate_items ( pvl2 , & vg - > pvs ) {
if ( pvl - > pv - > dev = = pvl2 - > pv - > dev )
goto next_pv ;
}
if ( ! id_write_format ( & pvl - > pv - > id , uuid , sizeof ( uuid ) ) )
return_0 ;
log_warn ( " WARNING: Removing PV %s (%s) that no longer belongs to VG %s " ,
pv_dev_name ( pvl - > pv ) , uuid , vg - > name ) ;
if ( ! pv_write_orphan ( cmd , pvl - > pv ) )
return_0 ;
/* Refresh metadata after orphan write */
2015-06-30 20:54:38 +03:00
if ( ! drop_cached_metadata ( vg ) ) {
log_error ( " Unable to drop cached metadata for VG %s while wiping outdated PVs. " , vg - > name ) ;
return 0 ;
}
2015-04-15 17:40:26 +03:00
next_pv :
;
}
return 1 ;
}
2009-01-26 22:01:32 +03:00
/* Caller sets consistent to 1 if it's safe for vg_read_internal to correct
2002-11-18 17:04:08 +03:00
* inconsistent metadata on disk ( i . e . the VG write lock is held ) .
2008-09-19 10:42:00 +04:00
* This guarantees only consistent metadata is returned .
2002-11-18 17:04:08 +03:00
* If consistent is 0 , caller must check whether consistent = = 1 on return
2008-01-30 17:00:02 +03:00
* and take appropriate action if it isn ' t ( e . g . abort ; get write lock
2009-01-26 22:01:32 +03:00
* and call vg_read_internal again ) .
2005-10-31 23:15:28 +03:00
*
* If precommitted is set , use precommitted metadata if present .
2008-06-06 15:12:50 +04:00
*
* Either of vgname or vgid may be NULL .
2011-04-21 17:13:40 +04:00
*
* Note : vginfo structs must not be held or used as parameters
* across the call to this function .
2002-11-18 17:04:08 +03:00
*/
2005-04-06 22:59:55 +04:00
static struct volume_group * _vg_read ( struct cmd_context * cmd ,
const char * vgname ,
2006-04-13 01:23:04 +04:00
const char * vgid ,
2014-10-07 04:04:09 +04:00
uint32_t warn_flags ,
2008-03-17 19:51:31 +03:00
int * consistent , unsigned precommitted )
2002-04-24 22:20:51 +04:00
{
2011-08-11 20:31:40 +04:00
struct format_instance * fid = NULL ;
2011-02-21 15:07:03 +03:00
struct format_instance_ctx fic ;
2002-12-20 02:25:55 +03:00
const struct format_type * fmt ;
struct volume_group * vg , * correct_vg = NULL ;
2002-11-18 17:04:08 +03:00
struct metadata_area * mda ;
2008-06-27 19:18:31 +04:00
struct lvmcache_info * info ;
2002-12-20 02:25:55 +03:00
int inconsistent = 0 ;
2007-02-07 16:29:52 +03:00
int inconsistent_vgid = 0 ;
2008-06-27 19:18:31 +04:00
int inconsistent_pvs = 0 ;
2010-07-07 06:53:16 +04:00
int inconsistent_mdas = 0 ;
2011-06-15 21:45:02 +04:00
int inconsistent_mda_count = 0 ;
2008-03-17 19:51:31 +03:00
unsigned use_precommitted = precommitted ;
2008-11-04 01:14:30 +03:00
struct dm_list * pvids ;
2015-04-15 17:40:26 +03:00
struct pv_list * pvl ;
2008-11-04 01:14:30 +03:00
struct dm_list all_pvs ;
2012-10-12 12:15:30 +04:00
unsigned seqno = 0 ;
2013-10-09 16:04:47 +04:00
int reappeared = 0 ;
2015-03-19 02:43:02 +03:00
struct cached_vg_fmtdata * vg_fmtdata = NULL ; /* Additional format-specific data about the vg */
unsigned use_previous_vg ;
2002-04-24 22:20:51 +04:00
2007-11-02 16:06:42 +03:00
if ( is_orphan_vg ( vgname ) ) {
2005-10-31 23:15:28 +03:00
if ( use_precommitted ) {
2009-12-16 22:22:11 +03:00
log_error ( INTERNAL_ERROR " vg_read_internal requires vgname "
2005-04-06 22:59:55 +04:00
" with pre-commit. " ) ;
return NULL ;
}
2002-11-18 17:04:08 +03:00
* consistent = 1 ;
2014-10-07 04:04:09 +04:00
return _vg_read_orphans ( cmd , warn_flags , vgname ) ;
2002-11-18 17:04:08 +03:00
}
2012-02-23 17:11:07 +04:00
if ( lvmetad_active ( ) & & ! use_precommitted ) {
2013-10-09 16:04:47 +04:00
if ( ( correct_vg = lvmcache_get_vg ( cmd , vgname , vgid , precommitted ) ) ) {
dm_list_iterate_items ( pvl , & correct_vg - > pvs )
if ( pvl - > pv - > dev )
reappeared + = _check_reappeared_pv ( correct_vg , pvl - > pv , * consistent ) ;
if ( reappeared & & * consistent )
* consistent = _repair_inconsistent_vg ( correct_vg ) ;
else
* consistent = ! reappeared ;
2015-06-10 17:27:12 +03:00
if ( _wipe_outdated_pvs ( cmd , correct_vg , & correct_vg - > pvs_outdated ) ) {
2015-04-15 17:41:09 +03:00
/* clear the list */
dm_list_init ( & correct_vg - > pvs_outdated ) ;
2015-06-10 17:27:12 +03:00
lvmetad_vg_clear_outdated_pvs ( correct_vg ) ;
}
2013-10-09 16:04:47 +04:00
}
return correct_vg ;
2012-02-23 17:11:07 +04:00
}
2010-07-07 06:53:16 +04:00
/*
* If cached metadata was inconsistent and * consistent is set
* then repair it now . Otherwise just return it .
* Also return if use_precommitted is set due to the FIXME in
* the missing PV logic below .
*/
2012-02-23 17:11:07 +04:00
if ( ( correct_vg = lvmcache_get_vg ( cmd , vgname , vgid , precommitted ) ) & &
2011-08-11 00:17:33 +04:00
( use_precommitted | | ! * consistent ) ) {
* consistent = 1 ;
2008-04-02 02:40:13 +04:00
return correct_vg ;
2010-07-07 06:53:16 +04:00
} else {
2012-02-23 17:11:07 +04:00
if ( correct_vg & & correct_vg - > seqno > seqno )
seqno = correct_vg - > seqno ;
2011-08-11 00:25:29 +04:00
release_vg ( correct_vg ) ;
2010-07-07 06:53:16 +04:00
correct_vg = NULL ;
2008-04-02 02:40:13 +04:00
}
2012-02-23 17:11:07 +04:00
2002-11-18 17:04:08 +03:00
/* Find the vgname in the cache */
/* If it's not there we must do full scan to be completely sure */
2012-02-23 17:11:07 +04:00
if ( ! ( fmt = lvmcache_fmt_from_vgname ( cmd , vgname , vgid , 1 ) ) ) {
2015-12-01 23:09:01 +03:00
lvmcache_label_scan ( cmd ) ;
2012-02-23 17:11:07 +04:00
if ( ! ( fmt = lvmcache_fmt_from_vgname ( cmd , vgname , vgid , 1 ) ) ) {
2010-12-11 01:39:52 +03:00
/* Independent MDAs aren't supported under low memory */
2011-02-18 17:16:11 +03:00
if ( ! cmd - > independent_metadata_areas & & critical_section ( ) )
2008-01-30 16:19:47 +03:00
return_NULL ;
2015-12-01 23:09:01 +03:00
lvmcache_force_next_label_scan ( ) ;
lvmcache_label_scan ( cmd ) ;
2012-02-23 17:11:07 +04:00
if ( ! ( fmt = lvmcache_fmt_from_vgname ( cmd , vgname , vgid , 0 ) ) )
2008-01-30 16:19:47 +03:00
return_NULL ;
2002-04-24 22:20:51 +04:00
}
}
2008-06-06 15:12:50 +04:00
/* Now determine the correct vgname if none was supplied */
vg_read: look up vgid from name
After recent changes to process_each, vg_read() is usually
given both the vgname and vgid for the intended VG.
However, in some cases vg_read() is given a vgid with
no vgname, or is given a vgname with no vgid.
When given a vgid with no vgname, vg_read() uses lvmcache
to look up the vgname using the vgid. If the vgname is
not found, vg_read() fails.
When given a vgname with no vgid, vg_read() should also
use lvmcache to look up the vgid using the vgname.
If the vgid is not found, vg_read() fails.
If the lvmcache lookup finds multiple vgids for the
vgname, then the lookup fails, causing vg_read() to fail
because the intended VG is uncertain.
Usually, both vgname and vgid for the intended VG are passed
to vg_read(), which means the lvmcache translations
between vgname and vgid are not done.
2015-12-01 00:12:01 +03:00
if ( ! vgname & & ! ( vgname = lvmcache_vgname_from_vgid ( cmd - > mem , vgid ) ) ) {
log_debug_metadata ( " Cache did not find VG name from vgid %.32s " , vgid ) ;
2008-06-06 15:12:50 +04:00
return_NULL ;
vg_read: look up vgid from name
After recent changes to process_each, vg_read() is usually
given both the vgname and vgid for the intended VG.
However, in some cases vg_read() is given a vgid with
no vgname, or is given a vgname with no vgid.
When given a vgid with no vgname, vg_read() uses lvmcache
to look up the vgname using the vgid. If the vgname is
not found, vg_read() fails.
When given a vgname with no vgid, vg_read() should also
use lvmcache to look up the vgid using the vgname.
If the vgid is not found, vg_read() fails.
If the lvmcache lookup finds multiple vgids for the
vgname, then the lookup fails, causing vg_read() to fail
because the intended VG is uncertain.
Usually, both vgname and vgid for the intended VG are passed
to vg_read(), which means the lvmcache translations
between vgname and vgid are not done.
2015-12-01 00:12:01 +03:00
}
/* Determine the correct vgid if none was supplied */
if ( ! vgid & & ! ( vgid = lvmcache_vgid_from_vgname ( cmd , vgname ) ) ) {
log_debug_metadata ( " Cache did not find VG vgid from name %s " , vgname ) ;
return_NULL ;
}
2008-06-06 15:12:50 +04:00
2005-10-31 23:15:28 +03:00
if ( use_precommitted & & ! ( fmt - > features & FMT_PRECOMMIT ) )
use_precommitted = 0 ;
2005-04-06 22:59:55 +04:00
2002-11-18 17:04:08 +03:00
/* create format instance with appropriate metadata area */
2012-02-13 03:01:19 +04:00
fic . type = FMT_INSTANCE_MDAS | FMT_INSTANCE_AUX_MDAS ;
2011-02-21 15:07:03 +03:00
fic . context . vg_ref . vg_name = vgname ;
fic . context . vg_ref . vg_id = vgid ;
if ( ! ( fid = fmt - > ops - > create_instance ( fmt , & fic ) ) ) {
2002-04-24 22:20:51 +04:00
log_error ( " Failed to create format instance " ) ;
return NULL ;
}
2008-06-08 18:18:44 +04:00
/* Store pvids for later so we can check if any are missing */
2011-08-11 20:31:40 +04:00
if ( ! ( pvids = lvmcache_get_pvids ( cmd , vgname , vgid ) ) ) {
_destroy_fid ( & fid ) ;
2008-06-08 18:18:44 +04:00
return_NULL ;
2011-08-11 20:31:40 +04:00
}
2008-06-08 18:18:44 +04:00
2011-08-11 20:31:40 +04:00
/*
* We use the fid globally here so prevent the release_vg
* call to destroy the fid - we may want to reuse it !
*/
fid - > ref_count + + ;
2002-04-24 22:20:51 +04:00
/* Ensure contents of all metadata areas match - else do recovery */
2011-06-15 21:45:02 +04:00
inconsistent_mda_count = 0 ;
2010-06-29 00:32:44 +04:00
dm_list_iterate_items ( mda , & fid - > metadata_areas_in_use ) {
2015-03-19 02:43:02 +03:00
use_previous_vg = 0 ;
2011-08-11 20:31:40 +04:00
2005-10-31 23:15:28 +03:00
if ( ( use_precommitted & &
2015-03-19 02:43:02 +03:00
! ( vg = mda - > ops - > vg_read_precommit ( fid , vgname , mda , & vg_fmtdata , & use_previous_vg ) ) & & ! use_previous_vg ) | |
2005-10-31 23:15:28 +03:00
( ! use_precommitted & &
2015-03-19 02:43:02 +03:00
! ( vg = mda - > ops - > vg_read ( fid , vgname , mda , & vg_fmtdata , & use_previous_vg , 0 ) ) & & ! use_previous_vg ) ) {
2002-11-18 17:04:08 +03:00
inconsistent = 1 ;
2015-03-19 02:43:02 +03:00
vg_fmtdata = NULL ;
continue ;
}
/* Use previous VG because checksum matches */
if ( ! vg ) {
vg = correct_vg ;
2002-11-18 17:04:08 +03:00
continue ;
2002-04-24 22:20:51 +04:00
}
2011-06-15 21:45:02 +04:00
2002-12-20 02:25:55 +03:00
if ( ! correct_vg ) {
2002-04-24 22:20:51 +04:00
correct_vg = vg ;
continue ;
}
2009-04-10 14:01:08 +04:00
2002-11-18 17:04:08 +03:00
/* FIXME Also ensure contents same - checksum compare? */
2002-04-24 22:20:51 +04:00
if ( correct_vg - > seqno ! = vg - > seqno ) {
2010-10-25 15:20:54 +04:00
if ( cmd - > metadata_read_only )
log_very_verbose ( " Not repairing VG %s metadata seqno (%d != %d) "
" as global/metadata_read_only is set. " ,
vgname , vg - > seqno , correct_vg - > seqno ) ;
2011-08-04 19:18:10 +04:00
else
2010-10-25 15:20:54 +04:00
inconsistent = 1 ;
2011-08-04 19:18:10 +04:00
2009-04-10 14:01:08 +04:00
if ( vg - > seqno > correct_vg - > seqno ) {
2011-08-11 00:25:29 +04:00
release_vg ( correct_vg ) ;
2002-04-24 22:20:51 +04:00
correct_vg = vg ;
2011-06-15 21:45:02 +04:00
} else {
mda - > status | = MDA_INCONSISTENT ;
+ + inconsistent_mda_count ;
2009-04-10 14:01:08 +04:00
}
2002-04-24 22:20:51 +04:00
}
2009-04-10 14:01:08 +04:00
2015-03-19 02:43:02 +03:00
if ( vg ! = correct_vg ) {
2011-08-11 00:25:29 +04:00
release_vg ( vg ) ;
2015-03-19 02:43:02 +03:00
vg_fmtdata = NULL ;
}
2002-04-24 22:20:51 +04:00
}
2011-08-11 20:31:40 +04:00
fid - > ref_count - - ;
2002-04-24 22:20:51 +04:00
2006-04-21 23:12:41 +04:00
/* Ensure every PV in the VG was in the cache */
if ( correct_vg ) {
2012-02-23 17:11:07 +04:00
/*
* Update the seqno from the cache , for the benefit of
* retro - style metadata formats like LVM1 .
*/
// correct_vg->seqno = seqno > correct_vg->seqno ? seqno : correct_vg->seqno;
2008-06-27 19:18:31 +04:00
/*
2010-06-29 00:34:58 +04:00
* If the VG has PVs without mdas , or ignored mdas , they may
2010-06-29 00:35:17 +04:00
* still be orphans in the cache : update the cache state here ,
* and update the metadata lists in the vg .
2008-06-27 19:18:31 +04:00
*/
if ( ! inconsistent & &
2008-11-04 01:14:30 +03:00
dm_list_size ( & correct_vg - > pvs ) > dm_list_size ( pvids ) ) {
dm_list_iterate_items ( pvl , & correct_vg - > pvs ) {
2008-06-27 19:18:31 +04:00
if ( ! pvl - > pv - > dev ) {
inconsistent_pvs = 1 ;
break ;
}
if ( str_list_match_item ( pvids , pvl - > pv - > dev - > pvid ) )
continue ;
/*
* PV not marked as belonging to this VG in cache .
2010-06-29 00:34:58 +04:00
* Check it ' s an orphan without metadata area
* not ignored .
2008-06-27 19:18:31 +04:00
*/
2012-02-10 05:28:27 +04:00
if ( ! ( info = lvmcache_info_from_pvid ( pvl - > pv - > dev - > pvid , 1 ) ) | |
! lvmcache_is_orphan ( info ) ) {
2008-06-27 19:18:31 +04:00
inconsistent_pvs = 1 ;
break ;
}
2012-02-10 05:28:27 +04:00
if ( lvmcache_mda_count ( info ) ) {
if ( ! lvmcache_fid_add_mdas_pv ( info , fid ) ) {
2011-08-11 20:31:40 +04:00
release_vg ( correct_vg ) ;
2010-07-07 06:53:16 +04:00
return_NULL ;
2011-08-11 20:31:40 +04:00
}
2012-02-10 05:28:27 +04:00
2013-01-08 02:30:29 +04:00
log_debug_metadata ( " Empty mda found for VG %s. " , vgname ) ;
2010-07-07 06:53:16 +04:00
if ( inconsistent_mdas )
continue ;
/*
* If any newly - added mdas are in - use then their
* metadata needs updating .
*/
2012-02-10 05:28:27 +04:00
lvmcache_foreach_mda ( info , _check_mda_in_use ,
& inconsistent_mdas ) ;
2010-07-07 06:53:16 +04:00
}
2008-06-27 19:18:31 +04:00
}
/* If the check passed, let's update VG and recalculate pvids */
if ( ! inconsistent_pvs ) {
2013-01-08 02:30:29 +04:00
log_debug_metadata ( " Updating cache for PVs without mdas "
" in VG %s. " , vgname ) ;
2010-01-05 19:01:22 +03:00
/*
* If there is no precommitted metadata , committed metadata
* is read and stored in the cache even if use_precommitted is set
*/
lvmcache_update_vg ( correct_vg , correct_vg - > status & PRECOMMITTED ) ;
2008-06-27 19:18:31 +04:00
2011-08-11 20:31:40 +04:00
if ( ! ( pvids = lvmcache_get_pvids ( cmd , vgname , vgid ) ) ) {
release_vg ( correct_vg ) ;
2008-06-27 19:18:31 +04:00
return_NULL ;
2011-08-11 20:31:40 +04:00
}
2008-06-27 19:18:31 +04:00
}
}
2011-08-11 20:31:40 +04:00
fid - > ref_count + + ;
2010-10-25 15:20:54 +04:00
if ( dm_list_size ( & correct_vg - > pvs ) ! =
dm_list_size ( pvids ) + vg_missing_pv_count ( correct_vg ) ) {
2013-01-08 02:30:29 +04:00
log_debug_metadata ( " Cached VG %s had incorrect PV list " ,
vgname ) ;
2006-07-04 23:36:49 +04:00
2011-02-18 17:16:11 +03:00
if ( critical_section ( ) )
2006-07-04 23:36:49 +04:00
inconsistent = 1 ;
2009-04-10 14:01:08 +04:00
else {
2011-08-11 00:25:29 +04:00
release_vg ( correct_vg ) ;
2006-07-04 23:36:49 +04:00
correct_vg = NULL ;
2009-04-10 14:01:08 +04:00
}
2008-11-04 01:14:30 +03:00
} else dm_list_iterate_items ( pvl , & correct_vg - > pvs ) {
2010-03-16 17:37:38 +03:00
if ( is_missing_pv ( pvl - > pv ) )
2008-09-19 10:42:00 +04:00
continue ;
2006-04-21 23:12:41 +04:00
if ( ! str_list_match_item ( pvids , pvl - > pv - > dev - > pvid ) ) {
2013-01-08 02:30:29 +04:00
log_debug_metadata ( " Cached VG %s had incorrect PV list " ,
vgname ) ;
2011-08-11 00:25:29 +04:00
release_vg ( correct_vg ) ;
2006-04-21 23:12:41 +04:00
correct_vg = NULL ;
break ;
}
}
2010-07-07 06:53:16 +04:00
if ( correct_vg & & inconsistent_mdas ) {
2011-08-11 00:25:29 +04:00
release_vg ( correct_vg ) ;
2010-07-07 06:53:16 +04:00
correct_vg = NULL ;
}
2011-08-11 20:31:40 +04:00
fid - > ref_count - - ;
2006-04-21 23:12:41 +04:00
}
2008-11-04 01:14:30 +03:00
dm_list_init ( & all_pvs ) ;
2007-02-07 16:29:52 +03:00
2005-03-22 01:40:35 +03:00
/* Failed to find VG where we expected it - full scan and retry */
2002-12-20 02:25:55 +03:00
if ( ! correct_vg ) {
2011-08-11 20:31:40 +04:00
/*
* Free outstanding format instance that remained unassigned
* from previous step where we tried to get the " correct_vg " ,
* but we failed to do so ( so there ' s a dangling fid now ) .
*/
_destroy_fid ( & fid ) ;
2015-03-19 02:43:02 +03:00
vg_fmtdata = NULL ;
2011-08-11 20:31:40 +04:00
2005-03-22 01:40:35 +03:00
inconsistent = 0 ;
2010-12-11 01:39:52 +03:00
/* Independent MDAs aren't supported under low memory */
2011-02-18 17:16:11 +03:00
if ( ! cmd - > independent_metadata_areas & & critical_section ( ) )
2008-01-30 16:19:47 +03:00
return_NULL ;
2015-12-01 23:09:01 +03:00
lvmcache_force_next_label_scan ( ) ;
lvmcache_label_scan ( cmd ) ;
2012-02-23 17:11:07 +04:00
if ( ! ( fmt = lvmcache_fmt_from_vgname ( cmd , vgname , vgid , 0 ) ) )
2008-01-30 16:19:47 +03:00
return_NULL ;
2005-03-22 01:40:35 +03:00
2005-10-31 23:15:28 +03:00
if ( precommitted & & ! ( fmt - > features & FMT_PRECOMMIT ) )
use_precommitted = 0 ;
2005-04-06 22:59:55 +04:00
2005-03-22 01:40:35 +03:00
/* create format instance with appropriate metadata area */
2012-02-13 03:01:19 +04:00
fic . type = FMT_INSTANCE_MDAS | FMT_INSTANCE_AUX_MDAS ;
2011-02-21 15:07:03 +03:00
fic . context . vg_ref . vg_name = vgname ;
fic . context . vg_ref . vg_id = vgid ;
if ( ! ( fid = fmt - > ops - > create_instance ( fmt , & fic ) ) ) {
2005-03-22 01:40:35 +03:00
log_error ( " Failed to create format instance " ) ;
return NULL ;
}
2011-08-11 20:31:40 +04:00
/*
* We use the fid globally here so prevent the release_vg
* call to destroy the fid - we may want to reuse it !
*/
fid - > ref_count + + ;
2005-03-22 01:40:35 +03:00
/* Ensure contents of all metadata areas match - else recover */
2011-06-15 21:45:02 +04:00
inconsistent_mda_count = 0 ;
2010-06-29 00:32:44 +04:00
dm_list_iterate_items ( mda , & fid - > metadata_areas_in_use ) {
2015-03-19 02:43:02 +03:00
use_previous_vg = 0 ;
2005-10-31 23:15:28 +03:00
if ( ( use_precommitted & &
2015-03-19 02:43:02 +03:00
! ( vg = mda - > ops - > vg_read_precommit ( fid , vgname , mda , & vg_fmtdata , & use_previous_vg ) ) & & ! use_previous_vg ) | |
2005-10-31 23:15:28 +03:00
( ! use_precommitted & &
2015-03-19 02:43:02 +03:00
! ( vg = mda - > ops - > vg_read ( fid , vgname , mda , & vg_fmtdata , & use_previous_vg , 0 ) ) & & ! use_previous_vg ) ) {
2005-03-22 01:40:35 +03:00
inconsistent = 1 ;
2015-03-19 02:43:02 +03:00
vg_fmtdata = NULL ;
2005-03-22 01:40:35 +03:00
continue ;
}
2015-03-19 02:43:02 +03:00
/* Use previous VG because checksum matches */
if ( ! vg ) {
vg = correct_vg ;
continue ;
}
2005-03-22 01:40:35 +03:00
if ( ! correct_vg ) {
correct_vg = vg ;
2009-04-10 14:01:08 +04:00
if ( ! _update_pv_list ( cmd - > mem , & all_pvs , correct_vg ) ) {
2011-03-11 17:56:56 +03:00
_free_pv_list ( & all_pvs ) ;
2011-08-11 20:31:40 +04:00
fid - > ref_count - - ;
2011-08-11 00:25:29 +04:00
release_vg ( vg ) ;
2007-02-07 16:29:52 +03:00
return_NULL ;
2009-04-10 14:01:08 +04:00
}
2005-03-22 01:40:35 +03:00
continue ;
}
2007-02-07 16:29:52 +03:00
2011-03-30 01:57:56 +04:00
if ( ! id_equal ( & vg - > id , & correct_vg - > id ) ) {
2007-02-07 16:29:52 +03:00
inconsistent = 1 ;
inconsistent_vgid = 1 ;
}
2005-03-22 01:40:35 +03:00
/* FIXME Also ensure contents same - checksums same? */
if ( correct_vg - > seqno ! = vg - > seqno ) {
2010-10-25 15:20:54 +04:00
/* Ignore inconsistent seqno if told to skip repair logic */
if ( cmd - > metadata_read_only )
log_very_verbose ( " Not repairing VG %s metadata seqno (%d != %d) "
" as global/metadata_read_only is set. " ,
vgname , vg - > seqno , correct_vg - > seqno ) ;
2011-08-04 19:18:10 +04:00
else
2010-10-25 15:20:54 +04:00
inconsistent = 1 ;
2011-08-04 19:18:10 +04:00
2009-04-10 14:01:08 +04:00
if ( ! _update_pv_list ( cmd - > mem , & all_pvs , vg ) ) {
2011-03-11 17:56:56 +03:00
_free_pv_list ( & all_pvs ) ;
2011-08-11 20:31:40 +04:00
fid - > ref_count - - ;
2011-08-11 00:25:29 +04:00
release_vg ( vg ) ;
release_vg ( correct_vg ) ;
2008-09-25 19:59:10 +04:00
return_NULL ;
2009-04-10 14:01:08 +04:00
}
if ( vg - > seqno > correct_vg - > seqno ) {
2011-08-11 00:25:29 +04:00
release_vg ( correct_vg ) ;
2005-03-22 01:40:35 +03:00
correct_vg = vg ;
2011-06-15 21:45:02 +04:00
} else {
mda - > status | = MDA_INCONSISTENT ;
+ + inconsistent_mda_count ;
2009-04-10 14:01:08 +04:00
}
2005-03-22 01:40:35 +03:00
}
2009-04-10 14:01:08 +04:00
2015-03-19 02:43:02 +03:00
if ( vg ! = correct_vg ) {
2011-08-11 00:25:29 +04:00
release_vg ( vg ) ;
2015-03-19 02:43:02 +03:00
vg_fmtdata = NULL ;
}
2005-03-22 01:40:35 +03:00
}
2011-08-11 20:31:40 +04:00
fid - > ref_count - - ;
2005-03-22 01:40:35 +03:00
/* Give up looking */
2011-03-11 17:56:56 +03:00
if ( ! correct_vg ) {
_free_pv_list ( & all_pvs ) ;
2011-08-11 20:31:40 +04:00
_destroy_fid ( & fid ) ;
2008-01-30 16:19:47 +03:00
return_NULL ;
2011-03-11 17:56:56 +03:00
}
2002-05-13 16:38:54 +04:00
}
2010-01-05 19:01:22 +03:00
/*
* If there is no precommitted metadata , committed metadata
* is read and stored in the cache even if use_precommitted is set
*/
2011-08-11 00:17:33 +04:00
lvmcache_update_vg ( correct_vg , ( correct_vg - > status & PRECOMMITTED ) ) ;
2002-11-18 17:04:08 +03:00
2002-04-24 22:20:51 +04:00
if ( inconsistent ) {
2005-10-31 23:15:28 +03:00
/* FIXME Test should be if we're *using* precommitted metadata not if we were searching for it */
if ( use_precommitted ) {
2005-04-06 22:59:55 +04:00
log_error ( " Inconsistent pre-commit metadata copies "
" for volume group %s " , vgname ) ;
2011-06-15 21:45:02 +04:00
/*
* Check whether all of the inconsistent MDAs were on
* MISSING PVs - - in that case , we should be safe .
2009-12-09 22:29:04 +03:00
*/
2011-06-15 21:45:02 +04:00
dm_list_iterate_items ( mda , & fid - > metadata_areas_in_use ) {
if ( mda - > status & MDA_INCONSISTENT ) {
2013-01-08 02:30:29 +04:00
log_debug_metadata ( " Checking inconsistent MDA: %s " , dev_name ( mda_get_device ( mda ) ) ) ;
2011-06-15 21:45:02 +04:00
dm_list_iterate_items ( pvl , & correct_vg - > pvs ) {
if ( mda_get_device ( mda ) = = pvl - > pv - > dev & &
( pvl - > pv - > status & MISSING_PV ) )
- - inconsistent_mda_count ;
}
}
}
if ( inconsistent_mda_count < 0 )
log_error ( INTERNAL_ERROR " Too many inconsistent MDAs. " ) ;
if ( ! inconsistent_mda_count ) {
2009-12-09 22:29:04 +03:00
* consistent = 0 ;
2011-03-11 17:56:56 +03:00
_free_pv_list ( & all_pvs ) ;
2009-12-09 22:29:04 +03:00
return correct_vg ;
}
2011-03-11 17:56:56 +03:00
_free_pv_list ( & all_pvs ) ;
2011-08-11 00:25:29 +04:00
release_vg ( correct_vg ) ;
2005-04-06 22:59:55 +04:00
return NULL ;
}
2011-03-11 17:56:56 +03:00
if ( ! * consistent ) {
_free_pv_list ( & all_pvs ) ;
2002-11-18 17:04:08 +03:00
return correct_vg ;
2011-03-11 17:56:56 +03:00
}
2002-11-18 17:04:08 +03:00
2007-02-07 16:29:52 +03:00
/* Don't touch if vgids didn't match */
if ( inconsistent_vgid ) {
2014-11-14 16:58:20 +03:00
log_warn ( " WARNING: Inconsistent metadata UUIDs found for "
" volume group %s. " , vgname ) ;
2007-02-07 16:29:52 +03:00
* consistent = 0 ;
2011-03-11 17:56:56 +03:00
_free_pv_list ( & all_pvs ) ;
2007-02-07 16:29:52 +03:00
return correct_vg ;
}
2007-06-28 21:33:44 +04:00
log_warn ( " WARNING: Inconsistent metadata found for VG %s - updating "
" to use version %u " , vgname , correct_vg - > seqno ) ;
2007-02-07 16:29:52 +03:00
2009-12-18 15:44:20 +03:00
/*
* If PV is marked missing but we found it ,
* update metadata and remove MISSING flag
*/
dm_list_iterate_items ( pvl , & all_pvs )
2013-10-09 16:04:47 +04:00
_check_reappeared_pv ( correct_vg , pvl - > pv , 1 ) ;
2009-12-18 15:44:20 +03:00
2013-10-09 16:04:47 +04:00
if ( ! _repair_inconsistent_vg ( correct_vg ) ) {
2011-03-11 17:56:56 +03:00
_free_pv_list ( & all_pvs ) ;
2011-08-11 00:25:29 +04:00
release_vg ( correct_vg ) ;
2005-01-17 21:24:28 +03:00
return NULL ;
}
2007-02-07 16:29:52 +03:00
2015-04-15 17:40:26 +03:00
if ( ! _wipe_outdated_pvs ( cmd , correct_vg , & all_pvs ) ) {
_free_pv_list ( & all_pvs ) ;
release_vg ( correct_vg ) ;
return_NULL ;
2007-02-07 16:29:52 +03:00
}
2002-04-24 22:20:51 +04:00
}
2011-03-11 17:56:56 +03:00
_free_pv_list ( & all_pvs ) ;
2008-09-19 10:42:00 +04:00
if ( vg_missing_pv_count ( correct_vg ) ) {
log_verbose ( " There are %d physical volumes missing. " ,
vg_missing_pv_count ( correct_vg ) ) ;
2011-05-07 17:32:05 +04:00
vg_mark_partial_lvs ( correct_vg , 1 ) ;
2008-09-19 10:42:00 +04:00
}
2003-05-06 16:06:02 +04:00
if ( ( correct_vg - > status & PVMOVE ) & & ! pvmove_mode ( ) ) {
2014-11-14 16:58:20 +03:00
log_error ( " Interrupted pvmove detected in volume group %s. " ,
correct_vg - > name ) ;
log_print ( " Please restore the metadata by running vgcfgrestore. " ) ;
2011-08-11 00:25:29 +04:00
release_vg ( correct_vg ) ;
2003-04-30 19:23:43 +04:00
return NULL ;
}
2002-04-24 22:20:51 +04:00
2003-04-30 19:23:43 +04:00
* consistent = 1 ;
2002-05-13 16:38:54 +04:00
return correct_vg ;
2002-04-24 22:20:51 +04:00
}
2009-11-23 13:44:50 +03:00
struct volume_group * vg_read_internal ( struct cmd_context * cmd , const char * vgname ,
2014-10-07 04:04:09 +04:00
const char * vgid , uint32_t warn_flags , int * consistent )
2005-04-06 22:59:55 +04:00
{
2005-06-14 21:54:48 +04:00
struct volume_group * vg ;
struct lv_list * lvl ;
2014-10-07 04:04:09 +04:00
if ( ! ( vg = _vg_read ( cmd , vgname , vgid , warn_flags , consistent , 0 ) ) )
goto_out ;
2005-06-14 21:54:48 +04:00
if ( ! check_pv_segments ( vg ) ) {
2009-12-16 22:22:11 +03:00
log_error ( INTERNAL_ERROR " PV segments corrupted in %s. " ,
2005-06-14 21:54:48 +04:00
vg - > name ) ;
2011-08-11 00:25:29 +04:00
release_vg ( vg ) ;
2014-10-07 04:04:09 +04:00
vg = NULL ;
goto out ;
2005-06-14 21:54:48 +04:00
}
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( lvl , & vg - > lvs ) {
2010-04-01 15:43:24 +04:00
if ( ! check_lv_segments ( lvl - > lv , 0 ) ) {
log_error ( INTERNAL_ERROR " LV segments corrupted in %s. " ,
lvl - > lv - > name ) ;
2011-08-11 00:25:29 +04:00
release_vg ( vg ) ;
2014-10-07 04:04:09 +04:00
vg = NULL ;
goto out ;
2010-04-01 15:43:24 +04:00
}
2010-04-01 17:08:06 +04:00
}
dm_list_iterate_items ( lvl , & vg - > lvs ) {
2010-04-01 15:43:24 +04:00
/*
* Checks that cross - reference other LVs .
*/
2005-10-28 01:51:28 +04:00
if ( ! check_lv_segments ( lvl - > lv , 1 ) ) {
2009-12-16 22:22:11 +03:00
log_error ( INTERNAL_ERROR " LV segments corrupted in %s. " ,
2005-06-14 21:54:48 +04:00
lvl - > lv - > name ) ;
2011-08-11 00:25:29 +04:00
release_vg ( vg ) ;
2014-10-07 04:04:09 +04:00
vg = NULL ;
goto out ;
2005-06-14 21:54:48 +04:00
}
}
2014-10-07 04:04:09 +04:00
out :
if ( ! * consistent & & ( warn_flags & WARN_INCONSISTENT ) )
2014-11-14 18:08:27 +03:00
log_warn ( " WARNING: Volume Group %s is not consistent. " , vgname ) ;
2014-10-07 04:04:09 +04:00
2005-06-14 21:54:48 +04:00
return vg ;
2005-04-06 22:59:55 +04:00
}
2011-03-11 17:56:56 +03:00
void free_pv_fid ( struct physical_volume * pv )
{
if ( ! pv )
return ;
2013-05-29 14:42:09 +04:00
pv_set_fid ( pv , NULL ) ;
2011-03-11 17:56:56 +03:00
}
2008-01-30 17:00:02 +03:00
/* This is only called by lv_from_lvid, which is only called from
* activate . c so we know the appropriate VG lock is already held and
2009-07-15 21:26:26 +04:00
* the vg_read_internal is therefore safe .
2002-11-18 17:04:08 +03:00
*/
2005-10-31 23:15:28 +03:00
static struct volume_group * _vg_read_by_vgid ( struct cmd_context * cmd ,
const char * vgid ,
2008-03-17 19:51:31 +03:00
unsigned precommitted )
2002-04-24 22:20:51 +04:00
{
2003-10-16 00:10:11 +04:00
const char * vgname ;
2008-11-04 01:14:30 +03:00
struct dm_list * vgnames ;
2010-11-29 21:35:37 +03:00
struct volume_group * vg ;
2014-05-29 11:41:03 +04:00
struct dm_str_list * strl ;
2014-10-07 04:04:09 +04:00
uint32_t warn_flags = WARN_PV_READ | WARN_INCONSISTENT ;
2002-11-18 17:04:08 +03:00
int consistent = 0 ;
/* Is corresponding vgname already cached? */
2012-02-10 05:28:27 +04:00
if ( lvmcache_vgid_is_cached ( vgid ) ) {
2014-10-07 04:04:09 +04:00
if ( ( vg = _vg_read ( cmd , NULL , vgid , warn_flags , & consistent , precommitted ) ) & &
2011-03-30 01:57:56 +04:00
id_equal ( & vg - > id , ( const struct id * ) vgid ) ) {
2002-11-18 17:04:08 +03:00
return vg ;
}
2011-08-11 00:25:29 +04:00
release_vg ( vg ) ;
2002-11-18 17:04:08 +03:00
}
2002-04-24 22:20:51 +04:00
2015-03-05 23:00:44 +03:00
/*
* When using lvmlockd we should never reach this point .
* The VG is locked , then vg_read ( ) is done , which gets
* the latest VG from lvmetad , or disk if lvmetad has
* been invalidated . When we get here the VG should
* always be cached and returned above .
*/
if ( lvmlockd_use ( ) )
log_error ( INTERNAL_ERROR " vg_read_by_vgid failed with lvmlockd " ) ;
2003-10-16 00:10:11 +04:00
/* Mustn't scan if memory locked: ensure cache gets pre-populated! */
2011-02-18 17:16:11 +03:00
if ( critical_section ( ) )
2010-11-29 21:35:37 +03:00
return_NULL ;
2003-07-05 02:34:56 +04:00
2009-07-15 21:26:26 +04:00
/* FIXME Need a genuine read by ID here - don't vg_read_internal by name! */
2003-07-05 02:34:56 +04:00
/* FIXME Disabled vgrenames while active for now because we aren't
* allowed to do a full scan here any more . */
2008-01-30 17:00:02 +03:00
// The slow way - full scan required to cope with vgrename
2015-12-01 23:09:01 +03:00
lvmcache_force_next_label_scan ( ) ;
lvmcache_label_scan ( cmd ) ;
2010-03-16 19:57:03 +03:00
if ( ! ( vgnames = get_vgnames ( cmd , 0 ) ) ) {
2009-02-03 19:19:25 +03:00
log_error ( " vg_read_by_vgid: get_vgnames failed " ) ;
2010-11-29 21:35:37 +03:00
return NULL ;
2002-04-24 22:20:51 +04:00
}
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( strl , vgnames ) {
2005-06-01 20:51:55 +04:00
vgname = strl - > str ;
2010-02-03 17:08:39 +03:00
if ( ! vgname )
2008-01-30 17:00:02 +03:00
continue ; // FIXME Unnecessary?
2002-11-18 17:04:08 +03:00
consistent = 0 ;
2014-10-07 04:04:09 +04:00
if ( ( vg = _vg_read ( cmd , vgname , vgid , warn_flags , & consistent , precommitted ) ) & &
2011-03-30 01:57:56 +04:00
id_equal ( & vg - > id , ( const struct id * ) vgid ) ) {
2002-11-18 17:04:08 +03:00
if ( ! consistent ) {
2011-08-11 00:25:29 +04:00
release_vg ( vg ) ;
2010-11-29 21:35:37 +03:00
return NULL ;
2002-11-18 17:04:08 +03:00
}
return vg ;
}
2011-08-11 00:25:29 +04:00
release_vg ( vg ) ;
2002-04-24 22:20:51 +04:00
}
return NULL ;
}
2002-11-18 17:04:08 +03:00
/* Only called by activate.c */
2005-10-31 23:15:28 +03:00
struct logical_volume * lv_from_lvid ( struct cmd_context * cmd , const char * lvid_s ,
2008-03-17 19:51:31 +03:00
unsigned precommitted )
2002-11-18 17:04:08 +03:00
{
2015-11-22 01:31:44 +03:00
struct logical_volume * lv ;
2002-11-18 17:04:08 +03:00
struct volume_group * vg ;
2002-12-20 02:25:55 +03:00
const union lvid * lvid ;
2002-11-18 17:04:08 +03:00
2002-12-20 02:25:55 +03:00
lvid = ( const union lvid * ) lvid_s ;
2002-11-18 17:04:08 +03:00
2013-01-05 07:07:43 +04:00
log_very_verbose ( " Finding %svolume group for uuid %s " , precommitted ? " precommitted " : " " , lvid_s ) ;
2010-10-25 17:35:13 +04:00
if ( ! ( vg = _vg_read_by_vgid ( cmd , ( const char * ) lvid - > id [ 0 ] . uuid , precommitted ) ) ) {
2002-11-18 17:04:08 +03:00
log_error ( " Volume group for uuid not found: %s " , lvid_s ) ;
return NULL ;
}
log_verbose ( " Found volume group \" %s \" " , vg - > name ) ;
if ( vg - > status & EXPORTED_VG ) {
log_error ( " Volume group \" %s \" is exported " , vg - > name ) ;
2009-04-10 14:01:08 +04:00
goto out ;
2002-11-18 17:04:08 +03:00
}
2015-11-22 01:31:44 +03:00
if ( ! ( lv = find_lv_in_vg_by_lvid ( vg , lvid ) ) ) {
2002-11-18 17:04:08 +03:00
log_very_verbose ( " Can't find logical volume id %s " , lvid_s ) ;
2009-04-10 14:01:08 +04:00
goto out ;
2002-11-18 17:04:08 +03:00
}
2015-11-22 01:31:44 +03:00
return lv ;
2009-04-10 14:01:08 +04:00
out :
2011-08-11 00:25:29 +04:00
release_vg ( vg ) ;
2009-04-10 14:01:08 +04:00
return NULL ;
2002-11-18 17:04:08 +03:00
}
2010-05-19 15:52:37 +04:00
const char * find_vgname_from_pvid ( struct cmd_context * cmd ,
const char * pvid )
{
char * vgname ;
struct lvmcache_info * info ;
vgname = lvmcache_vgname_from_pvid ( cmd , pvid ) ;
if ( is_orphan_vg ( vgname ) ) {
2012-02-10 05:28:27 +04:00
if ( ! ( info = lvmcache_info_from_pvid ( pvid , 0 ) ) ) {
2010-05-19 15:52:37 +04:00
return_NULL ;
}
/*
2010-06-29 00:34:58 +04:00
* If an orphan PV has no MDAs , or it has MDAs but the
* MDA is ignored , it may appear to be an orphan until
* the metadata is read off another PV in the same VG .
* Detecting this means checking every VG by scanning
* every PV on the system .
2010-05-19 15:52:37 +04:00
*/
2012-02-10 05:28:27 +04:00
if ( lvmcache_uncertain_ownership ( info ) ) {
2014-10-07 04:04:09 +04:00
if ( ! scan_vgs_for_pvs ( cmd , WARN_PV_READ ) ) {
2010-05-19 15:52:37 +04:00
log_error ( " Rescan for PVs without "
" metadata areas failed. " ) ;
return NULL ;
}
2010-06-29 00:34:58 +04:00
/*
* Ask lvmcache again - we may have a non - orphan
* name now
*/
vgname = lvmcache_vgname_from_pvid ( cmd , pvid ) ;
2010-05-19 15:52:37 +04:00
}
}
return vgname ;
}
const char * find_vgname_from_pvname ( struct cmd_context * cmd ,
const char * pvname )
{
const char * pvid ;
2012-02-10 05:28:27 +04:00
pvid = lvmcache_pvid_from_devname ( cmd , pvname ) ;
2010-05-19 15:52:37 +04:00
if ( ! pvid )
/* Not a PV */
return NULL ;
return find_vgname_from_pvid ( cmd , pvid ) ;
}
2007-06-11 22:29:30 +04:00
/* FIXME Use label functions instead of PV functions */
2008-01-30 17:00:02 +03:00
static struct physical_volume * _pv_read ( struct cmd_context * cmd ,
2009-04-22 13:31:30 +04:00
struct dm_pool * pvmem ,
2007-06-11 22:29:30 +04:00
const char * pv_name ,
2011-02-21 15:13:40 +03:00
struct format_instance * fid ,
2014-10-07 04:04:09 +04:00
uint32_t warn_flags , int scan_label_only )
2002-04-24 22:20:51 +04:00
{
struct physical_volume * pv ;
2002-11-18 17:04:08 +03:00
struct label * label ;
2003-07-05 02:34:56 +04:00
struct lvmcache_info * info ;
2002-11-18 17:04:08 +03:00
struct device * dev ;
2012-02-10 06:53:03 +04:00
const struct format_type * fmt ;
2012-03-03 00:46:36 +04:00
int found ;
2002-04-24 22:20:51 +04:00
2008-01-30 16:19:47 +03:00
if ( ! ( dev = dev_cache_get ( pv_name , cmd - > filter ) ) )
return_NULL ;
2002-04-24 22:20:51 +04:00
2012-02-23 17:11:07 +04:00
if ( lvmetad_active ( ) ) {
info = lvmcache_info_from_pvid ( dev - > pvid , 0 ) ;
2012-03-03 00:46:36 +04:00
if ( ! info ) {
if ( ! lvmetad_pv_lookup_by_dev ( cmd , dev , & found ) )
return_NULL ;
if ( ! found ) {
2014-10-07 04:04:09 +04:00
if ( warn_flags & WARN_PV_READ )
2012-03-03 00:46:36 +04:00
log_error ( " No physical volume found in lvmetad cache for %s " ,
pv_name ) ;
return NULL ;
}
2012-12-14 17:06:33 +04:00
if ( ! ( info = lvmcache_info_from_pvid ( dev - > pvid , 0 ) ) ) {
2014-10-07 04:04:09 +04:00
if ( warn_flags & WARN_PV_READ )
2012-12-14 17:06:33 +04:00
log_error ( " No cache info in lvmetad cache for %s. " ,
pv_name ) ;
return NULL ;
}
2012-03-03 00:46:36 +04:00
}
2012-02-23 17:11:07 +04:00
label = lvmcache_get_label ( info ) ;
} else {
if ( ! ( label_read ( dev , & label , UINT64_C ( 0 ) ) ) ) {
2014-10-07 04:04:09 +04:00
if ( warn_flags & WARN_PV_READ )
2012-02-23 17:11:07 +04:00
log_error ( " No physical volume label read from %s " ,
pv_name ) ;
return NULL ;
}
info = ( struct lvmcache_info * ) label - > info ;
2002-04-24 22:20:51 +04:00
}
2012-02-10 06:53:03 +04:00
fmt = lvmcache_fmt ( info ) ;
2002-11-18 17:04:08 +03:00
2010-01-22 00:09:23 +03:00
pv = _alloc_pv ( pvmem , dev ) ;
if ( ! pv ) {
2003-04-25 02:23:24 +04:00
log_error ( " pv allocation for '%s' failed " , pv_name ) ;
2005-04-20 00:52:35 +04:00
return NULL ;
2002-11-18 17:04:08 +03:00
}
2011-06-01 23:29:31 +04:00
pv - > label_sector = label - > sector ;
2002-11-18 17:04:08 +03:00
/* FIXME Move more common code up here */
2012-02-10 05:28:27 +04:00
if ( ! ( lvmcache_fmt ( info ) - > ops - > pv_read ( lvmcache_fmt ( info ) , pv_name , pv , scan_label_only ) ) ) {
2002-04-24 22:20:51 +04:00
log_error ( " Failed to read existing physical volume '%s' " ,
pv_name ) ;
2010-01-22 00:09:23 +03:00
goto bad ;
2002-04-24 22:20:51 +04:00
}
if ( ! pv - > size )
2010-01-22 00:09:23 +03:00
goto bad ;
2009-04-22 13:31:30 +04:00
if ( ! alloc_pv_segment_whole_pv ( pvmem , pv ) )
2010-01-22 00:09:23 +03:00
goto_bad ;
2005-04-20 00:52:35 +04:00
2011-02-21 15:13:40 +03:00
if ( fid )
2012-02-10 05:28:27 +04:00
lvmcache_fid_add_mdas ( info , fid , ( const char * ) & pv - > id , ID_LEN ) ;
2011-02-21 15:13:40 +03:00
else {
2012-02-10 06:53:03 +04:00
lvmcache_fid_add_mdas ( info , fmt - > orphan_vg - > fid , ( const char * ) & pv - > id , ID_LEN ) ;
pv_set_fid ( pv , fmt - > orphan_vg - > fid ) ;
2011-02-21 15:13:40 +03:00
}
2005-04-20 00:52:35 +04:00
return pv ;
2010-01-22 00:09:23 +03:00
bad :
2011-03-11 17:56:56 +03:00
free_pv_fid ( pv ) ;
dm_pool_free ( pvmem , pv ) ;
2010-01-22 00:09:23 +03:00
return NULL ;
2002-04-24 22:20:51 +04:00
}
2002-11-18 17:04:08 +03:00
/* May return empty list */
2010-03-16 19:57:03 +03:00
struct dm_list * get_vgnames ( struct cmd_context * cmd , int include_internal )
2002-04-24 22:20:51 +04:00
{
2010-03-16 19:57:03 +03:00
return lvmcache_get_vgnames ( cmd , include_internal ) ;
2002-04-24 22:20:51 +04:00
}
2010-03-16 19:57:03 +03:00
struct dm_list * get_vgids ( struct cmd_context * cmd , int include_internal )
2006-04-13 01:23:04 +04:00
{
2010-03-16 19:57:03 +03:00
return lvmcache_get_vgids ( cmd , include_internal ) ;
2006-04-13 01:23:04 +04:00
}
2015-05-06 00:24:50 +03:00
int get_vgnameids ( struct cmd_context * cmd , struct dm_list * vgnameids ,
const char * only_this_vgname , int include_internal )
{
struct vgnameid_list * vgnl ;
struct format_type * fmt ;
if ( only_this_vgname ) {
if ( ! ( vgnl = dm_pool_alloc ( cmd - > mem , sizeof ( * vgnl ) ) ) ) {
log_error ( " vgnameid_list allocation failed. " ) ;
return 0 ;
}
vgnl - > vg_name = dm_pool_strdup ( cmd - > mem , only_this_vgname ) ;
vgnl - > vgid = NULL ;
dm_list_add ( vgnameids , & vgnl - > list ) ;
return 1 ;
}
if ( lvmetad_active ( ) ) {
/*
* This just gets the list of names / ids from lvmetad
* and does not populate lvmcache .
*/
lvmetad_get_vgnameids ( cmd , vgnameids ) ;
if ( include_internal ) {
dm_list_iterate_items ( fmt , & cmd - > formats ) {
if ( ! ( vgnl = dm_pool_alloc ( cmd - > mem , sizeof ( * vgnl ) ) ) ) {
log_error ( " vgnameid_list allocation failed. " ) ;
return 0 ;
}
vgnl - > vg_name = dm_pool_strdup ( cmd - > mem , fmt - > orphan_vg_name ) ;
vgnl - > vgid = NULL ;
dm_list_add ( vgnameids , & vgnl - > list ) ;
}
}
} else {
/*
* The non - lvmetad case . This function begins by calling
* lvmcache_label_scan ( ) to populate lvmcache .
*/
lvmcache_get_vgnameids ( cmd , include_internal , vgnameids ) ;
}
return 1 ;
}
2014-10-07 04:04:09 +04:00
static int _get_pvs ( struct cmd_context * cmd , uint32_t warn_flags ,
2013-03-20 01:37:04 +04:00
struct dm_list * pvslist , struct dm_list * vgslist )
2002-04-24 22:20:51 +04:00
{
2014-05-29 11:41:03 +04:00
struct dm_str_list * strl ;
2006-04-13 01:23:04 +04:00
const char * vgname , * vgid ;
2009-04-10 13:56:00 +04:00
struct pv_list * pvl , * pvl_copy ;
2008-11-04 01:14:30 +03:00
struct dm_list * vgids ;
2002-11-18 17:04:08 +03:00
struct volume_group * vg ;
int consistent = 0 ;
2003-04-30 19:23:43 +04:00
int old_pvmove ;
2013-03-20 01:37:04 +04:00
struct vg_list * vgl_item = NULL ;
int have_pv = 0 ;
2002-11-18 17:04:08 +03:00
2015-12-01 23:09:01 +03:00
lvmcache_label_scan ( cmd ) ;
2002-04-24 22:20:51 +04:00
2002-11-18 17:04:08 +03:00
/* Get list of VGs */
2010-03-16 19:57:03 +03:00
if ( ! ( vgids = get_vgids ( cmd , 1 ) ) ) {
2009-02-03 19:19:25 +03:00
log_error ( " get_pvs: get_vgids failed " ) ;
2008-01-16 21:15:26 +03:00
return 0 ;
2002-04-24 22:20:51 +04:00
}
2002-11-18 17:04:08 +03:00
/* Read every VG to ensure cache consistency */
/* Orphan VG is last on list */
2003-04-30 19:23:43 +04:00
old_pvmove = pvmove_mode ( ) ;
init_pvmove ( 1 ) ;
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( strl , vgids ) {
2006-04-13 01:23:04 +04:00
vgid = strl - > str ;
if ( ! vgid )
2002-11-18 17:04:08 +03:00
continue ; /* FIXME Unnecessary? */
consistent = 0 ;
2012-02-10 05:28:27 +04:00
if ( ! ( vgname = lvmcache_vgname_from_vgid ( NULL , vgid ) ) ) {
2006-04-13 01:23:04 +04:00
stack ;
continue ;
}
2013-03-20 01:37:04 +04:00
2013-07-09 15:34:48 +04:00
/*
* When we are retrieving a list to return toliblvm we need
2013-03-20 01:37:04 +04:00
* that list to contain VGs that are modifiable as we are using
* the vgmem pool in the vg to provide allocation for liblvm .
* This is a hack to prevent the vg from getting cached as the
2013-07-09 15:34:48 +04:00
* vgid will be NULL .
* FIXME Remove this hack .
2013-03-20 01:37:04 +04:00
*/
2014-10-07 04:04:09 +04:00
warn_flags | = WARN_INCONSISTENT ;
if ( ! ( vg = vg_read_internal ( cmd , vgname , ( ! vgslist ) ? vgid : NULL , warn_flags , & consistent ) ) ) {
2002-11-18 17:04:08 +03:00
stack ;
continue ;
}
/* Move PVs onto results list */
2008-01-16 21:15:26 +03:00
if ( pvslist )
2009-04-10 13:56:00 +04:00
dm_list_iterate_items ( pvl , & vg - > pvs ) {
if ( ! ( pvl_copy = _copy_pvl ( cmd - > mem , pvl ) ) ) {
log_error ( " PV list allocation failed " ) ;
2011-08-11 00:25:29 +04:00
release_vg ( vg ) ;
2009-04-10 13:56:00 +04:00
return 0 ;
}
2013-07-09 15:34:48 +04:00
/* If we are going to release the VG, don't
* store a pointer to it in the PV structure .
2013-03-20 01:37:04 +04:00
*/
2013-07-09 15:34:48 +04:00
if ( ! vgslist )
2013-03-20 01:37:04 +04:00
pvl_copy - > pv - > vg = NULL ;
2013-07-09 15:34:48 +04:00
else
/*
* Make sure the vg mode indicates
* writeable .
* FIXME Rework function to take a
* parameter to control this
*/
2013-06-29 01:25:28 +04:00
pvl_copy - > pv - > vg - > open_mode = ' w ' ;
2013-03-20 01:37:04 +04:00
have_pv = 1 ;
dm_list_add ( pvslist , & pvl_copy - > list ) ;
2009-04-10 13:56:00 +04:00
}
2013-03-20 01:37:04 +04:00
2013-07-09 15:34:48 +04:00
/*
* In the case of the library we want to preserve the embedded
* volume group as subsequent calls to retrieve data about the
* PV require it .
2013-03-20 01:37:04 +04:00
*/
2013-07-09 15:34:48 +04:00
if ( ! vgslist | | ! have_pv )
2013-03-20 01:37:04 +04:00
release_vg ( vg ) ;
2013-07-09 15:34:48 +04:00
else {
/*
* Add VG to list of VG objects that will be returned
2013-03-20 01:37:04 +04:00
*/
vgl_item = dm_pool_alloc ( cmd - > mem , sizeof ( * vgl_item ) ) ;
if ( ! vgl_item ) {
log_error ( " VG list element allocation failed " ) ;
return 0 ;
}
vgl_item - > vg = vg ;
vg = NULL ;
dm_list_add ( vgslist , & vgl_item - > list ) ;
}
have_pv = 0 ;
2002-11-18 17:04:08 +03:00
}
2003-04-30 19:23:43 +04:00
init_pvmove ( old_pvmove ) ;
2002-11-18 17:04:08 +03:00
2013-07-09 15:34:48 +04:00
if ( ! pvslist )
2008-01-16 21:15:26 +03:00
dm_pool_free ( cmd - > mem , vgids ) ;
2013-07-09 15:34:48 +04:00
2008-01-16 21:15:26 +03:00
return 1 ;
}
2013-07-09 15:34:48 +04:00
/*
* Retrieve a list of all physical volumes .
* @ param cmd Command context
* @ param pvslist Set to NULL if you want memory for list created ,
* else valid memory
* @ param vgslist Set to NULL if you need the pv structures to contain
* valid vg pointer . This is the list of VGs
* @ returns NULL on errors , else pvslist which will equal passed - in value if
* supplied .
2013-03-20 01:37:04 +04:00
*/
struct dm_list * get_pvs_internal ( struct cmd_context * cmd ,
2013-07-09 15:34:48 +04:00
struct dm_list * pvslist ,
struct dm_list * vgslist )
2008-01-16 21:15:26 +03:00
{
2013-03-20 01:37:04 +04:00
struct dm_list * results = pvslist ;
2008-01-16 21:15:26 +03:00
2013-03-20 01:37:04 +04:00
if ( NULL = = results ) {
if ( ! ( results = dm_pool_alloc ( cmd - > mem , sizeof ( * results ) ) ) ) {
log_error ( " PV list allocation failed " ) ;
return 0 ;
}
dm_list_init ( results ) ;
}
2008-01-16 21:15:26 +03:00
2014-10-07 04:04:09 +04:00
if ( ! _get_pvs ( cmd , WARN_PV_READ , results , vgslist ) ) {
2013-07-09 15:34:48 +04:00
if ( ! pvslist )
2013-03-20 01:37:04 +04:00
dm_pool_free ( cmd - > mem , results ) ;
return NULL ;
}
2002-04-24 22:20:51 +04:00
return results ;
}
2014-10-07 04:04:09 +04:00
int scan_vgs_for_pvs ( struct cmd_context * cmd , uint32_t warn_flags )
2008-01-16 21:15:26 +03:00
{
2014-10-07 04:04:09 +04:00
return _get_pvs ( cmd , warn_flags , NULL , NULL ) ;
2008-01-16 21:15:26 +03:00
}
2010-07-09 19:34:40 +04:00
int pv_write ( struct cmd_context * cmd __attribute__ ( ( unused ) ) ,
2011-02-28 16:19:02 +03:00
struct physical_volume * pv , int allow_non_orphan )
2002-04-24 22:20:51 +04:00
{
2003-08-27 01:12:06 +04:00
if ( ! pv - > fmt - > ops - > pv_write ) {
log_error ( " Format does not support writing physical volumes " ) ;
return 0 ;
}
2011-02-25 17:08:54 +03:00
/*
* FIXME : Try to remove this restriction . This requires checking
* that the PV and the VG are in a consistent state . We need
* to provide some revert mechanism since PV label together
* with VG metadata write is not atomic .
*/
2011-02-28 16:19:02 +03:00
if ( ! allow_non_orphan & &
( ! is_orphan_vg ( pv - > vg_name ) | | pv - > pe_alloc_count ) ) {
2002-11-18 17:04:08 +03:00
log_error ( " Assertion failed: can't _pv_write non-orphan PV "
2014-03-19 02:54:46 +04:00
" (in VG %s) " , pv_vg_name ( pv ) ) ;
2002-11-18 17:04:08 +03:00
return 0 ;
2002-04-24 22:20:51 +04:00
}
2011-02-21 15:26:27 +03:00
if ( ! pv - > fmt - > ops - > pv_write ( pv - > fmt , pv ) )
2008-01-30 16:19:47 +03:00
return_0 ;
2002-04-24 22:20:51 +04:00
2013-03-25 19:21:59 +04:00
pv - > status & = ~ UNLABELLED_PV ;
2012-10-13 22:51:07 +04:00
if ( ! lvmetad_pv_found ( & pv - > id , pv - > dev , pv - > fmt , pv - > label_sector ,
2012-06-27 16:59:34 +04:00
NULL , NULL ) )
2012-02-23 17:11:07 +04:00
return_0 ;
2002-04-24 22:20:51 +04:00
return 1 ;
}
2007-02-07 16:29:52 +03:00
int pv_write_orphan ( struct cmd_context * cmd , struct physical_volume * pv )
{
const char * old_vg_name = pv - > vg_name ;
2008-02-06 18:47:28 +03:00
pv - > vg_name = cmd - > fmt - > orphan_vg_name ;
2007-02-07 16:29:52 +03:00
pv - > status = ALLOCATABLE_PV ;
2008-09-25 19:59:10 +04:00
pv - > pe_alloc_count = 0 ;
2007-02-07 16:29:52 +03:00
if ( ! dev_get_size ( pv - > dev , & pv - > size ) ) {
2007-10-12 18:29:32 +04:00
log_error ( " %s: Couldn't get size. " , pv_dev_name ( pv ) ) ;
2007-02-07 16:29:52 +03:00
return 0 ;
}
2011-02-28 16:19:02 +03:00
if ( ! pv_write ( cmd , pv , 0 ) ) {
2007-02-07 16:29:52 +03:00
log_error ( " Failed to clear metadata from physical "
" volume \" %s \" after removal from \" %s \" " ,
2007-10-12 18:29:32 +04:00
pv_dev_name ( pv ) , old_vg_name ) ;
2007-02-07 16:29:52 +03:00
return 0 ;
}
return 1 ;
}
2007-06-14 19:48:05 +04:00
2010-05-19 06:08:50 +04:00
int is_global_vg ( const char * vg_name )
{
return ( vg_name & & ! strcmp ( vg_name , VG_GLOBAL ) ) ? 1 : 0 ;
}
2007-11-02 16:06:42 +03:00
/**
* is_orphan_vg - Determine whether a vg_name is an orphan
* @ vg_name : pointer to the vg_name
*/
int is_orphan_vg ( const char * vg_name )
{
2010-05-19 06:36:33 +04:00
return ( vg_name & & ! strncmp ( vg_name , ORPHAN_PREFIX , sizeof ( ORPHAN_PREFIX ) - 1 ) ) ? 1 : 0 ;
2007-11-02 16:06:42 +03:00
}
2011-01-12 23:42:50 +03:00
/*
* Exclude pseudo VG names used for locking .
*/
int is_real_vg ( const char * vg_name )
{
return ( vg_name & & * vg_name ! = ' # ' ) ;
}
2012-02-10 05:28:27 +04:00
static int _analyze_mda ( struct metadata_area * mda , void * baton )
{
const struct format_type * fmt = baton ;
mda - > ops - > pv_analyze_mda ( fmt , mda ) ;
return 1 ;
}
2007-04-26 00:03:16 +04:00
/*
* Returns :
* 0 - fail
* 1 - success
*/
int pv_analyze ( struct cmd_context * cmd , const char * pv_name ,
2007-08-22 18:38:18 +04:00
uint64_t label_sector )
2007-04-26 00:03:16 +04:00
{
struct label * label ;
struct device * dev ;
2007-04-26 01:10:55 +04:00
struct lvmcache_info * info ;
2007-04-26 00:03:16 +04:00
dev = dev_cache_get ( pv_name , cmd - > filter ) ;
if ( ! dev ) {
log_error ( " Device %s not found (or ignored by filtering). " ,
pv_name ) ;
return 0 ;
}
/*
* First , scan for LVM labels .
*/
if ( ! label_read ( dev , & label , label_sector ) ) {
log_error ( " Could not find LVM label on %s " ,
pv_name ) ;
return 0 ;
}
2011-10-24 14:24:39 +04:00
log_print ( " Found label on %s, sector % " PRIu64 " , type=%.8s " ,
2007-04-26 00:03:16 +04:00
pv_name , label - > sector , label - > type ) ;
2007-04-26 01:10:55 +04:00
/*
* Next , loop through metadata areas
*/
info = label - > info ;
2012-02-10 05:28:27 +04:00
lvmcache_foreach_mda ( info , _analyze_mda , ( void * ) lvmcache_fmt ( info ) ) ;
2007-04-26 01:10:55 +04:00
2007-04-26 00:03:16 +04:00
return 1 ;
}
2007-06-06 23:40:28 +04:00
2009-07-28 19:14:56 +04:00
/* FIXME: remove / combine this with locking? */
2009-07-29 17:26:01 +04:00
int vg_check_write_mode ( struct volume_group * vg )
2009-07-28 19:14:56 +04:00
{
if ( vg - > open_mode ! = ' w ' ) {
2009-07-29 00:41:41 +04:00
log_errno ( EPERM , " Attempt to modify a read-only VG " ) ;
2009-07-28 19:14:56 +04:00
return 0 ;
}
return 1 ;
}
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
/*
* Return 1 if the VG metadata should be written
2015-03-09 21:53:22 +03:00
* * without * the LVM_WRITE flag in the status line , and
* * with * the LVM_WRITE_LOCKED flag in the flags line .
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
*
* If this is done for a VG , it forces previous versions
2015-03-09 21:53:22 +03:00
* of lvm ( before the LVM_WRITE_LOCKED flag was added ) , to view
* the VG and its LVs as read - only ( because the LVM_WRITE flag
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
* is missing ) . Versions of lvm that understand the
2015-03-09 21:53:22 +03:00
* LVM_WRITE_LOCKED flag know to check the other methods of
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
* access control for the VG , specifically system_id and lock_type .
*
* So , if a VG has a system_id or lock_type , then the
* system_id and lock_type control access to the VG in
* addition to its basic writable status . Because previous
* lvm versions do not know about system_id or lock_type ,
2015-03-09 21:53:22 +03:00
* VGs depending on either of these should have LVM_WRITE_LOCKED
* instead of LVM_WRITE to prevent the previous lvm versions from
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
* assuming they can write the VG and its LVs .
*/
int vg_flag_write_locked ( struct volume_group * vg )
{
if ( vg - > system_id & & vg - > system_id [ 0 ] )
return 1 ;
if ( vg - > lock_type & & vg - > lock_type [ 0 ] & & strcmp ( vg - > lock_type , " none " ) )
return 1 ;
return 0 ;
}
2009-01-27 01:42:59 +03:00
/*
* Performs a set of checks against a VG according to bits set in status
* and returns FAILED_ * bits for those that aren ' t acceptable .
*
* FIXME Remove the unnecessary duplicate definitions and return bits directly .
*/
static uint32_t _vg_bad_status_bits ( const struct volume_group * vg ,
2009-11-25 01:55:55 +03:00
uint64_t status )
2009-01-27 01:13:22 +03:00
{
uint32_t failure = 0 ;
2007-06-06 23:40:28 +04:00
2009-01-27 01:13:22 +03:00
if ( ( status & CLUSTERED ) & &
2009-07-15 09:49:47 +04:00
( vg_is_clustered ( vg ) ) & & ! locking_is_clustered ( ) ) {
2013-10-02 00:20:10 +04:00
if ( ! vg - > cmd - > ignore_clustered_vgs )
log_error ( " Skipping clustered volume group %s " , vg - > name ) ;
else
log_verbose ( " Skipping clustered volume group %s " , vg - > name ) ;
2009-01-27 01:13:22 +03:00
/* Return because other flags are considered undefined. */
return FAILED_CLUSTERED ;
}
if ( ( status & EXPORTED_VG ) & &
2009-09-15 22:35:13 +04:00
vg_is_exported ( vg ) ) {
2009-01-27 01:13:22 +03:00
log_error ( " Volume group %s is exported " , vg - > name ) ;
failure | = FAILED_EXPORTED ;
}
if ( ( status & LVM_WRITE ) & &
! ( vg - > status & LVM_WRITE ) ) {
log_error ( " Volume group %s is read-only " , vg - > name ) ;
failure | = FAILED_READ_ONLY ;
}
if ( ( status & RESIZEABLE_VG ) & &
2009-09-15 22:35:13 +04:00
! vg_is_resizeable ( vg ) ) {
2009-01-27 01:13:22 +03:00
log_error ( " Volume group %s is not resizeable. " , vg - > name ) ;
failure | = FAILED_RESIZEABLE ;
}
return failure ;
}
2007-06-06 23:40:28 +04:00
/**
* vg_check_status - check volume group status flags and log error
* @ vg - volume group to check status flags
2007-06-19 08:36:12 +04:00
* @ status - specific status flags to check ( e . g . EXPORTED_VG )
2007-06-06 23:40:28 +04:00
*/
2009-11-25 01:55:55 +03:00
int vg_check_status ( const struct volume_group * vg , uint64_t status )
2007-06-06 23:40:28 +04:00
{
2009-01-27 01:42:59 +03:00
return ! _vg_bad_status_bits ( vg , status ) ;
2007-06-06 23:40:28 +04:00
}
2007-06-13 01:20:20 +04:00
2014-04-18 05:27:16 +04:00
/*
* VG is left unlocked on failure
*/
2010-05-19 05:16:40 +04:00
static struct volume_group * _recover_vg ( struct cmd_context * cmd ,
2010-08-20 03:26:31 +04:00
const char * vg_name , const char * vgid )
2009-01-27 01:13:22 +03:00
{
int consistent = 1 ;
struct volume_group * vg ;
2010-05-19 05:16:40 +04:00
unlock_vg ( cmd , vg_name ) ;
2009-01-27 01:13:22 +03:00
dev_close_all ( ) ;
2013-03-18 00:29:58 +04:00
if ( ! lock_vol ( cmd , vg_name , LCK_VG_WRITE , NULL ) )
2009-01-27 01:13:22 +03:00
return_NULL ;
2014-10-07 04:04:09 +04:00
if ( ! ( vg = vg_read_internal ( cmd , vg_name , vgid , WARN_PV_READ , & consistent ) ) ) {
2014-04-18 05:27:16 +04:00
unlock_vg ( cmd , vg_name ) ;
2009-01-27 01:13:22 +03:00
return_NULL ;
2014-04-18 05:27:16 +04:00
}
2009-01-27 01:13:22 +03:00
2009-04-10 14:01:08 +04:00
if ( ! consistent ) {
2011-08-11 00:25:29 +04:00
release_vg ( vg ) ;
2014-04-18 05:27:16 +04:00
unlock_vg ( cmd , vg_name ) ;
2009-01-27 01:13:22 +03:00
return_NULL ;
2009-04-10 14:01:08 +04:00
}
2009-01-27 01:13:22 +03:00
2009-07-29 17:26:01 +04:00
return ( struct volume_group * ) vg ;
2009-01-27 01:13:22 +03:00
}
2015-07-14 22:42:18 +03:00
static int _allow_extra_system_id ( struct cmd_context * cmd , const char * system_id )
2014-10-24 21:29:04 +04:00
{
const struct dm_config_node * cn ;
const struct dm_config_value * cv ;
const char * str ;
2015-07-08 12:22:24 +03:00
if ( ! ( cn = find_config_tree_array ( cmd , local_extra_system_ids_CFG , NULL ) ) )
2014-10-24 21:29:04 +04:00
return 0 ;
for ( cv = cn - > v ; cv ; cv = cv - > next ) {
if ( cv - > type = = DM_CFG_EMPTY_ARRAY )
break ;
2015-02-24 01:19:08 +03:00
/* Ignore invalid data: Warning message already issued by config.c */
if ( cv - > type ! = DM_CFG_STRING )
2014-10-24 21:29:04 +04:00
continue ;
str = cv - > v . str ;
2015-02-24 01:19:08 +03:00
if ( ! * str )
2014-10-24 21:29:04 +04:00
continue ;
if ( ! strcmp ( str , system_id ) )
return 1 ;
}
return 0 ;
}
static int _access_vg_clustered ( struct cmd_context * cmd , struct volume_group * vg )
{
if ( vg_is_clustered ( vg ) & & ! locking_is_clustered ( ) ) {
if ( ! cmd - > ignore_clustered_vgs )
log_error ( " Skipping clustered volume group %s " , vg - > name ) ;
else
log_verbose ( " Skipping clustered volume group %s " , vg - > name ) ;
return 0 ;
}
2015-02-24 01:19:08 +03:00
2014-10-24 21:29:04 +04:00
return 1 ;
}
2015-03-05 23:00:44 +03:00
static int _access_vg_lock_type ( struct cmd_context * cmd , struct volume_group * vg ,
2015-07-14 19:36:04 +03:00
uint32_t lockd_state , uint32_t * failure )
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
{
if ( ! is_real_vg ( vg - > name ) )
return 1 ;
2015-03-05 23:00:44 +03:00
if ( cmd - > lockd_vg_disable )
return 1 ;
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
/*
2015-03-05 23:00:44 +03:00
* Local VG requires no lock from lvmlockd .
*/
if ( ! is_lockd_type ( vg - > lock_type ) )
return 1 ;
/*
* When lvmlockd is not used , lockd VGs are ignored by lvm
* and cannot be used , with two exceptions :
*
* . The - - shared option allows them to be revealed with
* reporting / display commands .
*
* . If a command asks to operate on one specifically
* by name , then an error is printed .
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
*/
2015-03-05 23:00:44 +03:00
if ( ! lvmlockd_use ( ) ) {
/*
* Some reporting / display commands have the - - shared option
* ( like - - foreign ) to allow them to reveal lockd VGs that
* are otherwise ignored . The - - shared option must only be
* permitted in commands that read the VG for report or display ,
* not any that write the VG or activate LVs .
*/
if ( cmd - > include_shared_vgs )
return 1 ;
/*
* Some commands want the error printed by vg_read , others by ignore_vg .
* Those using ignore_vg may choose to skip the error .
*/
if ( cmd - > vg_read_print_access_error ) {
log_error ( " Cannot access VG %s with lock type %s that requires lvmlockd. " ,
vg - > name , vg - > lock_type ) ;
}
2015-07-14 19:36:04 +03:00
* failure | = FAILED_LOCK_TYPE ;
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
return 0 ;
}
2015-03-05 23:00:44 +03:00
/*
* The lock request from lvmlockd failed . If the lock was ex ,
* we cannot continue . If the lock was sh , we could also fail
* to continue but since the lock was sh , it means the VG is
* only being read , and it doesn ' t hurt to allow reading with
* no lock .
*/
if ( lockd_state & LDST_FAIL ) {
vgchange/lvchange: enforce the shared VG lock from lvmlockd
The vgchange/lvchange activation commands read the VG, and
don't write it, so they acquire a shared VG lock from lvmlockd.
When other commands fail to acquire a shared VG lock from
lvmlockd, a warning is printed and they continue without it.
(Without it, the VG metadata they display from lvmetad may
not be up to date.)
vgchange/lvchange -a shouldn't continue without the shared
lock for a couple reasons:
. Usually they will just continue on and fail to acquire the
LV locks for activation, so continuing is pointless.
. More importantly, without the sh VG lock, the VG metadata
used by the command may be stale, and the LV locks shown
in the VG metadata may no longer be current. In the
case of sanlock, this would result in odd, unpredictable
errors when lvmlockd doesn't find the expected lock on
disk. In the case of dlm, the invalid LV lock could be
granted for the non-existing LV.
The solution is to not continue after the shared lock fails,
in the same way that a command fails if an exclusive lock fails.
2015-07-17 23:13:22 +03:00
if ( ( lockd_state & LDST_EX ) | | cmd - > lockd_vg_enforce_sh ) {
2015-03-05 23:00:44 +03:00
log_error ( " Cannot access VG %s due to failed lock. " , vg - > name ) ;
2015-07-14 19:36:04 +03:00
* failure | = FAILED_LOCK_MODE ;
2015-03-05 23:00:44 +03:00
return 0 ;
} else {
log_warn ( " Reading VG %s without a lock. " , vg - > name ) ;
return 1 ;
}
}
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
return 1 ;
}
2015-11-30 20:46:55 +03:00
int is_system_id_allowed ( struct cmd_context * cmd , const char * system_id )
2014-10-24 21:29:04 +04:00
{
2015-02-24 02:03:52 +03:00
/*
2015-11-30 20:46:55 +03:00
* A VG without a system_id can be accessed by anyone .
2015-02-24 02:03:52 +03:00
*/
2015-11-30 20:46:55 +03:00
if ( ! system_id | | ! system_id [ 0 ] )
return 1 ;
2015-02-24 02:03:52 +03:00
2014-10-24 21:29:04 +04:00
/*
2015-11-30 20:46:55 +03:00
* Allowed if the host and VG system_id ' s match .
2014-10-24 21:29:04 +04:00
*/
2015-11-30 20:46:55 +03:00
if ( cmd - > system_id & & ! strcmp ( cmd - > system_id , system_id ) )
2014-10-24 21:29:04 +04:00
return 1 ;
/*
2015-11-30 20:46:55 +03:00
* Allowed if a host ' s extra system_id matches .
2014-10-24 21:29:04 +04:00
*/
2015-11-30 20:46:55 +03:00
if ( cmd - > system_id & & _allow_extra_system_id ( cmd , system_id ) )
2014-10-24 21:29:04 +04:00
return 1 ;
2015-02-24 02:19:36 +03:00
/*
2015-11-30 20:46:55 +03:00
* Not allowed if the host does not have a system_id
* and the VG does , or if the host and VG ' s system_id ' s
* do not match .
2015-02-24 02:19:36 +03:00
*/
2015-11-30 20:46:55 +03:00
return 0 ;
}
static int _access_vg_systemid ( struct cmd_context * cmd , struct volume_group * vg )
{
2015-02-24 02:19:36 +03:00
/*
2015-11-30 20:46:55 +03:00
* LVM1 VGs must not be accessed if a new - style LVM2 system ID is set .
2015-02-24 02:19:36 +03:00
*/
2015-11-30 20:46:55 +03:00
if ( cmd - > system_id & & systemid_on_pvs ( vg ) ) {
log_error ( " Cannot access VG %s with LVM1 system ID %s when host system ID is set. " ,
vg - > name , vg - > lvm1_system_id ) ;
return 0 ;
}
/*
* A few commands allow read - only access to foreign VGs .
*/
if ( cmd - > include_foreign_vgs )
return 1 ;
if ( is_system_id_allowed ( cmd , vg - > system_id ) )
2015-02-24 02:19:36 +03:00
return 1 ;
2014-10-24 21:29:04 +04:00
/*
* Allow VG access if the local host has active LVs in it .
*/
if ( lvs_in_vg_activated ( vg ) ) {
2015-02-27 22:32:00 +03:00
log_warn ( " WARNING: Found LVs active in VG %s with foreign system ID %s. Possible data corruption. " ,
2014-10-24 21:29:04 +04:00
vg - > name , vg - > system_id ) ;
2015-02-25 20:33:11 +03:00
if ( cmd - > include_active_foreign_vgs )
return 1 ;
return 0 ;
2014-10-24 21:29:04 +04:00
}
/*
2015-11-30 20:46:55 +03:00
* Print an error when reading a VG that has a system_id
* and the host system_id is unknown .
2014-10-24 21:29:04 +04:00
*/
if ( ! cmd - > system_id | | cmd - > unknown_system_id ) {
2015-02-27 22:32:00 +03:00
log_error ( " Cannot access VG %s with system ID %s with unknown local system ID. " ,
2015-02-24 02:19:36 +03:00
vg - > name , vg - > system_id ) ;
2014-10-24 21:29:04 +04:00
return 0 ;
}
2015-02-25 19:44:42 +03:00
/*
2015-03-05 23:00:44 +03:00
* Some commands want the error printed by vg_read , others by ignore_vg .
* Those using ignore_vg may choose to skip the error .
2015-02-25 19:44:42 +03:00
*/
2015-03-05 23:00:44 +03:00
if ( cmd - > vg_read_print_access_error ) {
2015-02-27 22:32:00 +03:00
log_error ( " Cannot access VG %s with system ID %s with local system ID %s. " ,
2015-02-25 19:44:42 +03:00
vg - > name , vg - > system_id , cmd - > system_id ) ;
return 0 ;
}
2015-03-05 23:00:44 +03:00
/* Silently ignore foreign vgs. */
2014-10-24 21:29:04 +04:00
return 0 ;
}
/*
* FIXME : move _vg_bad_status_bits ( ) checks in here .
*/
2015-03-05 23:00:44 +03:00
static int _vg_access_permitted ( struct cmd_context * cmd , struct volume_group * vg ,
uint32_t lockd_state , uint32_t * failure )
2014-10-24 21:29:04 +04:00
{
2015-02-24 02:03:52 +03:00
if ( ! is_real_vg ( vg - > name ) ) {
/* Disallow use of LVM1 orphans when a host system ID is set. */
if ( cmd - > system_id & & * cmd - > system_id & & systemid_on_pvs ( vg ) ) {
* failure | = FAILED_SYSTEMID ;
return_0 ;
}
2014-10-24 21:29:04 +04:00
return 1 ;
2015-02-24 02:03:52 +03:00
}
2014-10-24 21:29:04 +04:00
if ( ! _access_vg_clustered ( cmd , vg ) ) {
* failure | = FAILED_CLUSTERED ;
return 0 ;
}
2015-07-14 19:36:04 +03:00
if ( ! _access_vg_lock_type ( cmd , vg , lockd_state , failure ) ) {
/* Either FAILED_LOCK_TYPE or FAILED_LOCK_MODE were set. */
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
return 0 ;
}
2014-10-24 21:29:04 +04:00
if ( ! _access_vg_systemid ( cmd , vg ) ) {
* failure | = FAILED_SYSTEMID ;
return 0 ;
}
return 1 ;
}
2009-01-27 01:13:22 +03:00
/*
* Consolidated locking , reading , and status flag checking .
*
* If the metadata is inconsistent , setting READ_ALLOW_INCONSISTENT in
2015-10-22 22:56:22 +03:00
* read_flags will return it with FAILED_INCONSISTENT set instead of
2009-01-27 01:13:22 +03:00
* giving you nothing .
*
* Use vg_read_error ( vg ) to determine the result . Nonzero means there were
* problems reading the volume group .
* Zero value means that the VG is open and appropriate locks are held .
*/
2009-07-29 17:26:01 +04:00
static struct volume_group * _vg_lock_and_read ( struct cmd_context * cmd , const char * vg_name ,
2015-10-22 22:56:22 +03:00
const char * vgid ,
uint32_t lock_flags ,
uint64_t status_flags ,
uint32_t read_flags ,
2015-03-05 23:00:44 +03:00
uint32_t lockd_state )
2009-01-27 01:13:22 +03:00
{
2009-04-10 14:01:08 +04:00
struct volume_group * vg = NULL ;
2014-03-19 03:24:09 +04:00
int consistent = 1 ;
2009-01-27 01:13:22 +03:00
int consistent_in ;
uint32_t failure = 0 ;
2014-10-07 04:04:09 +04:00
uint32_t warn_flags = 0 ;
2009-01-27 01:13:22 +03:00
int already_locked ;
2015-10-22 22:56:22 +03:00
if ( ( read_flags & READ_ALLOW_INCONSISTENT ) | | ( lock_flags ! = LCK_VG_WRITE ) )
2009-01-27 01:13:22 +03:00
consistent = 0 ;
if ( ! validate_name ( vg_name ) & & ! is_orphan_vg ( vg_name ) ) {
2014-03-19 03:24:36 +04:00
log_error ( " Volume group name \" %s \" has invalid characters. " ,
2009-01-27 01:13:22 +03:00
vg_name ) ;
return NULL ;
}
2012-02-10 05:28:27 +04:00
already_locked = lvmcache_vgname_is_locked ( vg_name ) ;
2009-01-27 01:13:22 +03:00
2014-10-25 00:54:04 +04:00
if ( ! already_locked & &
2013-03-18 00:29:58 +04:00
! lock_vol ( cmd , vg_name , lock_flags , NULL ) ) {
2009-01-27 01:13:22 +03:00
log_error ( " Can't get lock for %s " , vg_name ) ;
return _vg_make_handle ( cmd , vg , FAILED_LOCKING ) ;
}
if ( is_orphan_vg ( vg_name ) )
status_flags & = ~ LVM_WRITE ;
consistent_in = consistent ;
2014-10-07 04:04:09 +04:00
warn_flags = WARN_PV_READ ;
2015-10-22 22:56:22 +03:00
if ( consistent | | ( read_flags & READ_WARN_INCONSISTENT ) )
2014-10-07 04:04:09 +04:00
warn_flags | = WARN_INCONSISTENT ;
2009-01-27 01:13:22 +03:00
/* If consistent == 1, we get NULL here if correction fails. */
2014-10-07 04:04:09 +04:00
if ( ! ( vg = vg_read_internal ( cmd , vg_name , vgid , warn_flags , & consistent ) ) ) {
2009-01-27 01:13:22 +03:00
if ( consistent_in & & ! consistent ) {
failure | = FAILED_INCONSISTENT ;
2013-05-23 18:17:08 +04:00
goto bad ;
2009-01-27 01:13:22 +03:00
}
2015-10-22 22:56:22 +03:00
if ( ! ( read_flags & READ_OK_NOTFOUND ) )
log_error ( " Volume group \" %s \" not found " , vg_name ) ;
2009-01-27 01:13:22 +03:00
failure | = FAILED_NOTFOUND ;
2012-03-28 15:11:25 +04:00
goto bad ;
2009-01-27 01:13:22 +03:00
}
2015-03-05 23:00:44 +03:00
if ( ! _vg_access_permitted ( cmd , vg , lockd_state , & failure ) )
2012-03-28 15:11:25 +04:00
goto bad ;
2009-01-27 01:22:07 +03:00
2009-01-27 01:13:22 +03:00
/* consistent == 0 when VG is not found, but failed == FAILED_NOTFOUND */
2009-04-10 14:01:08 +04:00
if ( ! consistent & & ! failure ) {
2011-08-11 00:25:29 +04:00
release_vg ( vg ) ;
2010-08-20 03:26:31 +04:00
if ( ! ( vg = _recover_vg ( cmd , vg_name , vgid ) ) ) {
2009-01-27 01:13:22 +03:00
log_error ( " Recovery of volume group \" %s \" failed. " ,
vg_name ) ;
2014-11-14 16:55:57 +03:00
failure | = FAILED_RECOVERY ;
2014-04-18 05:27:16 +04:00
goto bad_no_unlock ;
2009-01-27 01:13:22 +03:00
}
2009-04-10 14:01:08 +04:00
}
2009-01-27 01:13:22 +03:00
2009-10-16 21:41:49 +04:00
/*
* Check that the tool can handle tricky cases - - missing PVs and
* unknown segment types .
*/
2009-07-15 09:47:55 +04:00
if ( ! cmd - > handles_missing_pvs & & vg_missing_pv_count ( vg ) & &
2010-08-20 03:26:31 +04:00
lock_flags = = LCK_VG_WRITE ) {
2009-11-19 16:44:37 +03:00
log_error ( " Cannot change VG %s while PVs are missing. " , vg - > name ) ;
log_error ( " Consider vgreduce --removemissing. " ) ;
2009-07-15 09:47:55 +04:00
failure | = FAILED_INCONSISTENT ; /* FIXME new failure code here? */
2013-05-23 18:17:08 +04:00
goto bad ;
2009-07-15 09:47:55 +04:00
}
2009-10-16 21:41:49 +04:00
if ( ! cmd - > handles_unknown_segments & & vg_has_unknown_segments ( vg ) & &
2010-08-20 03:26:31 +04:00
lock_flags = = LCK_VG_WRITE ) {
2009-10-16 21:41:49 +04:00
log_error ( " Cannot change VG %s with unknown segments in it! " ,
vg - > name ) ;
failure | = FAILED_INCONSISTENT ; /* FIXME new failure code here? */
2013-05-23 18:17:08 +04:00
goto bad ;
2009-10-16 21:41:49 +04:00
}
2009-06-10 20:14:40 +04:00
failure | = _vg_bad_status_bits ( vg , status_flags ) ;
2009-01-27 01:13:22 +03:00
if ( failure )
goto_bad ;
return _vg_make_handle ( cmd , vg , failure ) ;
bad :
2014-10-25 00:54:04 +04:00
if ( ! already_locked )
2010-05-19 05:16:40 +04:00
unlock_vg ( cmd , vg_name ) ;
2009-01-27 01:13:22 +03:00
2014-04-18 05:27:16 +04:00
bad_no_unlock :
2009-01-27 01:13:22 +03:00
return _vg_make_handle ( cmd , vg , failure ) ;
}
2009-01-27 03:40:44 +03:00
/*
* vg_read : High - level volume group metadata read function .
*
* vg_read_error ( ) must be used on any handle returned to check for errors .
*
* - metadata inconsistent and automatic correction failed : FAILED_INCONSISTENT
* - VG is read - only : FAILED_READ_ONLY
* - VG is EXPORTED , unless flags has READ_ALLOW_EXPORTED : FAILED_EXPORTED
2009-09-15 22:35:13 +04:00
* - VG is not RESIZEABLE : FAILED_RESIZEABLE
2009-01-27 03:40:44 +03:00
* - locking failed : FAILED_LOCKING
*
2009-07-08 18:28:30 +04:00
* On failures , all locks are released , unless one of the following applies :
* - vgname_is_locked ( lock_name ) is true
* FIXME : remove the above 2 conditions if possible and make an error always
* release the lock .
2009-01-27 03:40:44 +03:00
*
* Volume groups are opened read - only unless flags contains READ_FOR_UPDATE .
*
* Checking for VG existence :
*
* FIXME : We want vg_read to attempt automatic recovery after acquiring a
* temporary write lock : if that fails , we bail out as usual , with failed &
* FAILED_INCONSISTENT . If it works , we are good to go . Code that ' s been in
* toollib just set lock_flags to LCK_VG_WRITE and called vg_read_internal with
* * consistent = 1.
*/
2009-07-29 17:26:01 +04:00
struct volume_group * vg_read ( struct cmd_context * cmd , const char * vg_name ,
2015-10-22 22:56:22 +03:00
const char * vgid , uint32_t read_flags , uint32_t lockd_state )
2009-01-27 03:40:44 +03:00
{
2015-10-22 22:56:22 +03:00
uint64_t status_flags = UINT64_C ( 0 ) ;
2009-01-27 03:40:44 +03:00
uint32_t lock_flags = LCK_VG_READ ;
2015-10-22 22:56:22 +03:00
if ( read_flags & READ_FOR_UPDATE ) {
status_flags | = EXPORTED_VG | LVM_WRITE ;
2009-01-27 03:40:44 +03:00
lock_flags = LCK_VG_WRITE ;
}
2015-10-22 22:56:22 +03:00
if ( read_flags & READ_ALLOW_EXPORTED )
status_flags & = ~ EXPORTED_VG ;
2009-01-27 03:40:44 +03:00
2015-10-22 22:56:22 +03:00
return _vg_lock_and_read ( cmd , vg_name , vgid , lock_flags , status_flags , read_flags , lockd_state ) ;
2009-01-27 03:40:44 +03:00
}
/*
* A high - level volume group metadata reading function . Open a volume group for
* later update ( this means the user code can change the metadata and later
* request the new metadata to be written and committed ) .
*/
2009-07-29 17:26:01 +04:00
struct volume_group * vg_read_for_update ( struct cmd_context * cmd , const char * vg_name ,
2015-10-22 22:56:22 +03:00
const char * vgid , uint32_t read_flags , uint32_t lockd_state )
2009-01-27 03:40:44 +03:00
{
2015-10-22 22:56:22 +03:00
return vg_read ( cmd , vg_name , vgid , read_flags | READ_FOR_UPDATE , lockd_state ) ;
2009-01-27 03:40:44 +03:00
}
2009-01-27 04:48:47 +03:00
/*
* Test the validity of a VG handle returned by vg_read ( ) or vg_read_for_update ( ) .
*/
2009-07-29 17:26:01 +04:00
uint32_t vg_read_error ( struct volume_group * vg_handle )
2009-01-27 04:48:47 +03:00
{
if ( ! vg_handle )
return FAILED_ALLOCATION ;
return vg_handle - > read_status ;
}
Add vg_lock_newname() library function.
Various tools need to check for existence of a VG before doing something
(vgsplit, vgrename, vgcreate). Currently we don't have an interface to
check for existence, but the existence check is part of the vg_read* call(s).
This patch is an attempt to pull out some of that functionality into a
separate function, and hopefully simplify our vg_read interface, and
move those patches along.
vg_lock_newname() is only concerned about checking whether a vg exists in
the system. Unfortunately, we cannot just scan the system, but we must first
obtain a lock. Since we are reserving a vgname, we take a WRITE lock on
the vgname. Once obtained, we scan the system to ensure the name does
not exist. The return codes and behavior is in the function header.
You might think of this function as similar to an open() call with
O_CREAT and O_EXCL flags (returns failure with -EEXIST if file already
exists).
NOTE: I think including the word "lock" in the function name is important,
as it clearly states the function obtains a lock and makes the code more
readable, especially when it comes to cleanup / unlocking. The ultimate
function name is somewhat open for debate though so later we may rename.
2009-06-09 18:29:10 +04:00
/*
* Lock a vgname and / or check for existence .
* Takes a WRITE lock on the vgname before scanning .
* If scanning fails or vgname found , release the lock .
* NOTE : If you find the return codes confusing , you might think of this
* function as similar to an open ( ) call with O_CREAT and O_EXCL flags
* ( open returns fail with - EEXIST if file already exists ) .
*
* Returns :
* FAILED_LOCKING - Cannot lock name
* FAILED_EXIST - VG name already exists - cannot reserve
* SUCCESS - VG name does not exist in system and WRITE lock held
*/
uint32_t vg_lock_newname ( struct cmd_context * cmd , const char * vgname )
{
2013-03-18 00:29:58 +04:00
if ( ! lock_vol ( cmd , vgname , LCK_VG_WRITE , NULL ) ) {
Add vg_lock_newname() library function.
Various tools need to check for existence of a VG before doing something
(vgsplit, vgrename, vgcreate). Currently we don't have an interface to
check for existence, but the existence check is part of the vg_read* call(s).
This patch is an attempt to pull out some of that functionality into a
separate function, and hopefully simplify our vg_read interface, and
move those patches along.
vg_lock_newname() is only concerned about checking whether a vg exists in
the system. Unfortunately, we cannot just scan the system, but we must first
obtain a lock. Since we are reserving a vgname, we take a WRITE lock on
the vgname. Once obtained, we scan the system to ensure the name does
not exist. The return codes and behavior is in the function header.
You might think of this function as similar to an open() call with
O_CREAT and O_EXCL flags (returns failure with -EEXIST if file already
exists).
NOTE: I think including the word "lock" in the function name is important,
as it clearly states the function obtains a lock and makes the code more
readable, especially when it comes to cleanup / unlocking. The ultimate
function name is somewhat open for debate though so later we may rename.
2009-06-09 18:29:10 +04:00
return FAILED_LOCKING ;
}
/* Find the vgname in the cache */
/* If it's not there we must do full scan to be completely sure */
2012-02-23 17:11:07 +04:00
if ( ! lvmcache_fmt_from_vgname ( cmd , vgname , NULL , 1 ) ) {
2015-12-01 23:09:01 +03:00
lvmcache_label_scan ( cmd ) ;
2012-02-23 17:11:07 +04:00
if ( ! lvmcache_fmt_from_vgname ( cmd , vgname , NULL , 1 ) ) {
2010-12-11 01:39:52 +03:00
/* Independent MDAs aren't supported under low memory */
2011-02-18 17:16:11 +03:00
if ( ! cmd - > independent_metadata_areas & & critical_section ( ) ) {
Add vg_lock_newname() library function.
Various tools need to check for existence of a VG before doing something
(vgsplit, vgrename, vgcreate). Currently we don't have an interface to
check for existence, but the existence check is part of the vg_read* call(s).
This patch is an attempt to pull out some of that functionality into a
separate function, and hopefully simplify our vg_read interface, and
move those patches along.
vg_lock_newname() is only concerned about checking whether a vg exists in
the system. Unfortunately, we cannot just scan the system, but we must first
obtain a lock. Since we are reserving a vgname, we take a WRITE lock on
the vgname. Once obtained, we scan the system to ensure the name does
not exist. The return codes and behavior is in the function header.
You might think of this function as similar to an open() call with
O_CREAT and O_EXCL flags (returns failure with -EEXIST if file already
exists).
NOTE: I think including the word "lock" in the function name is important,
as it clearly states the function obtains a lock and makes the code more
readable, especially when it comes to cleanup / unlocking. The ultimate
function name is somewhat open for debate though so later we may rename.
2009-06-09 18:29:10 +04:00
/*
* FIXME : Disallow calling this function if
2011-02-18 17:16:11 +03:00
* critical_section ( ) is true .
Add vg_lock_newname() library function.
Various tools need to check for existence of a VG before doing something
(vgsplit, vgrename, vgcreate). Currently we don't have an interface to
check for existence, but the existence check is part of the vg_read* call(s).
This patch is an attempt to pull out some of that functionality into a
separate function, and hopefully simplify our vg_read interface, and
move those patches along.
vg_lock_newname() is only concerned about checking whether a vg exists in
the system. Unfortunately, we cannot just scan the system, but we must first
obtain a lock. Since we are reserving a vgname, we take a WRITE lock on
the vgname. Once obtained, we scan the system to ensure the name does
not exist. The return codes and behavior is in the function header.
You might think of this function as similar to an open() call with
O_CREAT and O_EXCL flags (returns failure with -EEXIST if file already
exists).
NOTE: I think including the word "lock" in the function name is important,
as it clearly states the function obtains a lock and makes the code more
readable, especially when it comes to cleanup / unlocking. The ultimate
function name is somewhat open for debate though so later we may rename.
2009-06-09 18:29:10 +04:00
*/
unlock_vg ( cmd , vgname ) ;
return FAILED_LOCKING ;
}
2015-12-01 23:09:01 +03:00
lvmcache_force_next_label_scan ( ) ;
lvmcache_label_scan ( cmd ) ;
2012-02-23 17:11:07 +04:00
if ( ! lvmcache_fmt_from_vgname ( cmd , vgname , NULL , 0 ) ) {
Add vg_lock_newname() library function.
Various tools need to check for existence of a VG before doing something
(vgsplit, vgrename, vgcreate). Currently we don't have an interface to
check for existence, but the existence check is part of the vg_read* call(s).
This patch is an attempt to pull out some of that functionality into a
separate function, and hopefully simplify our vg_read interface, and
move those patches along.
vg_lock_newname() is only concerned about checking whether a vg exists in
the system. Unfortunately, we cannot just scan the system, but we must first
obtain a lock. Since we are reserving a vgname, we take a WRITE lock on
the vgname. Once obtained, we scan the system to ensure the name does
not exist. The return codes and behavior is in the function header.
You might think of this function as similar to an open() call with
O_CREAT and O_EXCL flags (returns failure with -EEXIST if file already
exists).
NOTE: I think including the word "lock" in the function name is important,
as it clearly states the function obtains a lock and makes the code more
readable, especially when it comes to cleanup / unlocking. The ultimate
function name is somewhat open for debate though so later we may rename.
2009-06-09 18:29:10 +04:00
/* vgname not found after scanning */
return SUCCESS ;
}
}
}
2009-06-30 22:39:31 +04:00
/* Found vgname so cannot reserve. */
Add vg_lock_newname() library function.
Various tools need to check for existence of a VG before doing something
(vgsplit, vgrename, vgcreate). Currently we don't have an interface to
check for existence, but the existence check is part of the vg_read* call(s).
This patch is an attempt to pull out some of that functionality into a
separate function, and hopefully simplify our vg_read interface, and
move those patches along.
vg_lock_newname() is only concerned about checking whether a vg exists in
the system. Unfortunately, we cannot just scan the system, but we must first
obtain a lock. Since we are reserving a vgname, we take a WRITE lock on
the vgname. Once obtained, we scan the system to ensure the name does
not exist. The return codes and behavior is in the function header.
You might think of this function as similar to an open() call with
O_CREAT and O_EXCL flags (returns failure with -EEXIST if file already
exists).
NOTE: I think including the word "lock" in the function name is important,
as it clearly states the function obtains a lock and makes the code more
readable, especially when it comes to cleanup / unlocking. The ultimate
function name is somewhat open for debate though so later we may rename.
2009-06-09 18:29:10 +04:00
unlock_vg ( cmd , vgname ) ;
return FAILED_EXIST ;
}
2011-03-11 17:30:27 +03:00
struct format_instance * alloc_fid ( const struct format_type * fmt ,
const struct format_instance_ctx * fic )
{
2011-03-11 17:38:38 +03:00
struct dm_pool * mem ;
2011-03-11 17:30:27 +03:00
struct format_instance * fid ;
2011-03-11 17:38:38 +03:00
if ( ! ( mem = dm_pool_create ( " format_instance " , 1024 ) ) )
return_NULL ;
2011-03-11 18:10:16 +03:00
if ( ! ( fid = dm_pool_zalloc ( mem , sizeof ( * fid ) ) ) ) {
2011-03-11 17:30:27 +03:00
log_error ( " Couldn't allocate format_instance object. " ) ;
2011-03-11 17:38:38 +03:00
goto bad ;
2011-03-11 17:30:27 +03:00
}
2011-03-11 17:38:38 +03:00
fid - > ref_count = 1 ;
fid - > mem = mem ;
2011-03-11 17:30:27 +03:00
fid - > type = fic - > type ;
2011-03-11 17:38:38 +03:00
fid - > fmt = fmt ;
2011-03-11 17:30:27 +03:00
dm_list_init ( & fid - > metadata_areas_in_use ) ;
dm_list_init ( & fid - > metadata_areas_ignored ) ;
return fid ;
2011-03-11 17:38:38 +03:00
bad :
dm_pool_destroy ( mem ) ;
return NULL ;
2011-03-11 17:30:27 +03:00
}
2011-03-11 17:50:13 +03:00
void pv_set_fid ( struct physical_volume * pv ,
struct format_instance * fid )
{
2012-02-13 15:01:34 +04:00
if ( fid = = pv - > fid )
return ;
2011-04-01 18:54:20 +04:00
if ( fid )
fid - > ref_count + + ;
2011-03-11 17:50:13 +03:00
if ( pv - > fid )
pv - > fid - > fmt - > ops - > destroy_instance ( pv - > fid ) ;
pv - > fid = fid ;
}
2011-02-21 15:10:58 +03:00
void vg_set_fid ( struct volume_group * vg ,
struct format_instance * fid )
{
struct pv_list * pvl ;
2012-02-13 15:01:34 +04:00
if ( fid = = vg - > fid )
return ;
2011-03-11 17:50:13 +03:00
if ( fid )
fid - > ref_count + + ;
2011-02-21 15:10:58 +03:00
dm_list_iterate_items ( pvl , & vg - > pvs )
2011-03-11 17:50:13 +03:00
pv_set_fid ( pvl - > pv , fid ) ;
2011-04-01 18:54:20 +04:00
dm_list_iterate_items ( pvl , & vg - > removed_pvs )
pv_set_fid ( pvl - > pv , fid ) ;
if ( vg - > fid )
vg - > fid - > fmt - > ops - > destroy_instance ( vg - > fid ) ;
vg - > fid = fid ;
2011-02-21 15:10:58 +03:00
}
2011-02-21 15:05:49 +03:00
static int _convert_key_to_string ( const char * key , size_t key_len ,
unsigned sub_key , char * buf , size_t buf_len )
2010-06-29 00:33:22 +04:00
{
2011-02-21 15:05:49 +03:00
memcpy ( buf , key , key_len ) ;
buf + = key_len ;
buf_len - = key_len ;
if ( ( dm_snprintf ( buf , buf_len , " _%u " , sub_key ) = = - 1 ) )
return_0 ;
return 1 ;
}
int fid_add_mda ( struct format_instance * fid , struct metadata_area * mda ,
const char * key , size_t key_len , const unsigned sub_key )
{
2011-11-18 23:31:09 +04:00
static char full_key [ PATH_MAX ] ;
2012-02-23 17:11:07 +04:00
2010-06-30 21:13:05 +04:00
dm_list_add ( mda_is_ignored ( mda ) ? & fid - > metadata_areas_ignored :
2012-02-23 17:11:07 +04:00
& fid - > metadata_areas_in_use , & mda - > list ) ;
2011-02-21 15:05:49 +03:00
/* Return if the mda is not supposed to be indexed. */
if ( ! key )
return 1 ;
2013-11-18 21:00:49 +04:00
if ( ! fid - > metadata_areas_index )
return_0 ;
2011-02-21 15:05:49 +03:00
/* Add metadata area to index. */
2012-02-13 03:01:19 +04:00
if ( ! _convert_key_to_string ( key , key_len , sub_key ,
full_key , sizeof ( full_key ) ) )
2012-02-23 17:11:07 +04:00
return_0 ;
2011-02-21 15:05:49 +03:00
2012-02-28 15:12:58 +04:00
if ( ! dm_hash_insert ( fid - > metadata_areas_index ,
full_key , mda ) ) {
log_error ( " Failed to hash mda. " ) ;
return 0 ;
}
2011-02-21 15:05:49 +03:00
return 1 ;
2010-06-29 00:33:22 +04:00
}
2011-02-21 15:05:49 +03:00
int fid_add_mdas ( struct format_instance * fid , struct dm_list * mdas ,
const char * key , size_t key_len )
2010-06-29 00:33:22 +04:00
{
struct metadata_area * mda , * mda_new ;
2011-02-21 15:05:49 +03:00
unsigned mda_index = 0 ;
2010-06-29 00:33:22 +04:00
dm_list_iterate_items ( mda , mdas ) {
2011-03-11 18:10:16 +03:00
mda_new = mda_copy ( fid - > mem , mda ) ;
2010-06-29 00:33:22 +04:00
if ( ! mda_new )
return_0 ;
2011-02-25 16:59:47 +03:00
fid_remove_mda ( fid , NULL , key , key_len , mda_index ) ;
2011-02-21 15:05:49 +03:00
fid_add_mda ( fid , mda_new , key , key_len , mda_index ) ;
mda_index + + ;
2010-06-29 00:33:22 +04:00
}
2011-02-21 15:05:49 +03:00
return 1 ;
}
struct metadata_area * fid_get_mda_indexed ( struct format_instance * fid ,
const char * key , size_t key_len ,
const unsigned sub_key )
{
2011-11-18 23:31:09 +04:00
static char full_key [ PATH_MAX ] ;
2011-02-21 15:05:49 +03:00
struct metadata_area * mda = NULL ;
2013-11-18 21:00:49 +04:00
if ( ! fid - > metadata_areas_index )
return_NULL ;
2011-02-21 15:05:49 +03:00
2012-02-13 03:01:19 +04:00
if ( ! _convert_key_to_string ( key , key_len , sub_key ,
full_key , sizeof ( full_key ) ) )
return_NULL ;
2013-11-18 21:00:49 +04:00
2012-02-13 03:01:19 +04:00
mda = ( struct metadata_area * ) dm_hash_lookup ( fid - > metadata_areas_index ,
full_key ) ;
2011-02-21 15:05:49 +03:00
return mda ;
}
int fid_remove_mda ( struct format_instance * fid , struct metadata_area * mda ,
const char * key , size_t key_len , const unsigned sub_key )
{
2011-11-18 23:31:09 +04:00
static char full_key [ PATH_MAX ] ;
2011-02-21 15:05:49 +03:00
struct metadata_area * mda_indexed = NULL ;
/* At least one of mda or key must be specified. */
if ( ! mda & & ! key )
return 1 ;
if ( key ) {
/*
* If both mda and key specified , check given mda
* with what we find using the index and return
* immediately if these two do not match .
*/
if ( ! ( mda_indexed = fid_get_mda_indexed ( fid , key , key_len , sub_key ) ) | |
( mda & & mda ! = mda_indexed ) )
return 1 ;
mda = mda_indexed ;
2012-02-13 03:01:19 +04:00
if ( ! _convert_key_to_string ( key , key_len , sub_key ,
full_key , sizeof ( full_key ) ) )
return_0 ;
2011-02-21 15:05:49 +03:00
2012-02-13 03:01:19 +04:00
dm_hash_remove ( fid - > metadata_areas_index , full_key ) ;
2011-02-21 15:05:49 +03:00
}
dm_list_del ( & mda - > list ) ;
2010-06-29 00:33:22 +04:00
return 1 ;
}
2010-06-29 00:31:59 +04:00
/*
* Copy constructor for a metadata_area .
*/
struct metadata_area * mda_copy ( struct dm_pool * mem ,
struct metadata_area * mda )
{
struct metadata_area * mda_new ;
if ( ! ( mda_new = dm_pool_alloc ( mem , sizeof ( * mda_new ) ) ) ) {
log_error ( " metadata_area allocation failed " ) ;
return NULL ;
}
memcpy ( mda_new , mda , sizeof ( * mda ) ) ;
if ( mda - > ops - > mda_metadata_locn_copy & & mda - > metadata_locn ) {
mda_new - > metadata_locn =
mda - > ops - > mda_metadata_locn_copy ( mem , mda - > metadata_locn ) ;
if ( ! mda_new - > metadata_locn ) {
dm_pool_free ( mem , mda_new ) ;
return NULL ;
}
}
2010-07-08 21:41:46 +04:00
dm_list_init ( & mda_new - > list ) ;
2010-06-29 00:31:59 +04:00
return mda_new ;
}
2010-06-29 00:31:38 +04:00
/*
* This function provides a way to answer the question on a format specific
* basis - does the format specfic context of these two metadata areas
* match ?
*
* A metatdata_area is defined to be independent of the underlying context .
* This has the benefit that we can use the same abstraction to read disks
* ( see _metadata_text_raw_ops ) or files ( see _metadata_text_file_ops ) .
* However , one downside is there is no format - independent way to determine
* whether a given metadata_area is attached to a specific device - in fact ,
* it may not be attached to a device at all .
*
* Thus , LVM is structured such that an mda is not a member of struct
* physical_volume . The location of the mda depends on whether
* the PV is in a volume group . A PV not in a VG has an mda on the
* ' info - > mda ' list in lvmcache , while a PV in a VG has an mda on
2010-06-29 00:32:44 +04:00
* the vg - > fid - > metadata_areas_in_use list . For further details , see _vg_read ( ) ,
* and the sequence of creating the format_instance with fid - > metadata_areas_in_use
2010-06-29 00:31:38 +04:00
* list , as well as the construction of the VG , with list of PVs ( comes
* after the construction of the fid and list of mdas ) .
*/
unsigned mda_locns_match ( struct metadata_area * mda1 , struct metadata_area * mda2 )
{
if ( ! mda1 - > ops - > mda_locns_match | | ! mda2 - > ops - > mda_locns_match | |
mda1 - > ops - > mda_locns_match ! = mda2 - > ops - > mda_locns_match )
return 0 ;
return mda1 - > ops - > mda_locns_match ( mda1 , mda2 ) ;
}
2007-06-19 08:23:32 +04:00
2011-06-15 21:45:02 +04:00
struct device * mda_get_device ( struct metadata_area * mda )
{
if ( ! mda - > ops - > mda_get_device )
return NULL ;
return mda - > ops - > mda_get_device ( mda ) ;
}
2010-06-29 00:30:14 +04:00
unsigned mda_is_ignored ( struct metadata_area * mda )
{
2010-10-05 21:34:05 +04:00
return ( mda - > status & MDA_IGNORED ) ;
2010-06-29 00:30:14 +04:00
}
2010-06-30 21:13:05 +04:00
void mda_set_ignored ( struct metadata_area * mda , unsigned mda_ignored )
2010-06-29 00:30:14 +04:00
{
2010-06-30 02:37:32 +04:00
void * locn = mda - > metadata_locn ;
2010-06-30 21:13:05 +04:00
unsigned old_mda_ignored = mda_is_ignored ( mda ) ;
2010-06-30 02:37:32 +04:00
2010-06-30 21:13:05 +04:00
if ( mda_ignored & & ! old_mda_ignored )
2010-10-05 21:34:05 +04:00
mda - > status | = MDA_IGNORED ;
2010-06-30 21:13:05 +04:00
else if ( ! mda_ignored & & old_mda_ignored )
2010-10-05 21:34:05 +04:00
mda - > status & = ~ MDA_IGNORED ;
2010-06-30 02:37:32 +04:00
else
2010-06-30 17:51:11 +04:00
return ; /* No change */
2013-09-26 20:37:40 +04:00
log_debug_metadata ( " %s ignored flag for mda %s at offset % " PRIu64 " . " ,
2013-01-08 02:30:29 +04:00
mda_ignored ? " Setting " : " Clearing " ,
mda - > ops - > mda_metadata_locn_name ? mda - > ops - > mda_metadata_locn_name ( locn ) : " " ,
mda - > ops - > mda_metadata_locn_offset ? mda - > ops - > mda_metadata_locn_offset ( locn ) : UINT64_C ( 0 ) ) ;
2010-06-29 00:30:14 +04:00
}
2010-06-29 00:34:40 +04:00
int mdas_empty_or_ignored ( struct dm_list * mdas )
{
struct metadata_area * mda ;
2013-07-17 16:49:21 +04:00
if ( dm_list_empty ( mdas ) )
2010-06-29 00:34:40 +04:00
return 1 ;
dm_list_iterate_items ( mda , mdas ) {
if ( mda_is_ignored ( mda ) )
return 1 ;
}
return 0 ;
}
2010-07-01 00:03:52 +04:00
int pv_change_metadataignore ( struct physical_volume * pv , uint32_t mda_ignored )
2010-06-30 01:32:44 +04:00
{
const char * pv_name = pv_dev_name ( pv ) ;
2010-07-01 00:03:52 +04:00
if ( mda_ignored & & ! pv_mda_used_count ( pv ) ) {
2010-06-30 21:13:05 +04:00
log_error ( " Metadata areas on physical volume \" %s \" already "
" ignored. " , pv_name ) ;
2010-06-30 01:32:44 +04:00
return 0 ;
}
2010-06-30 21:13:05 +04:00
2010-07-01 00:03:52 +04:00
if ( ! mda_ignored & & ( pv_mda_used_count ( pv ) = = pv_mda_count ( pv ) ) ) {
2010-06-30 21:13:05 +04:00
log_error ( " Metadata areas on physical volume \" %s \" already "
" marked as in-use. " , pv_name ) ;
2010-06-30 01:32:44 +04:00
return 0 ;
}
2010-06-30 21:13:05 +04:00
2010-06-30 01:32:44 +04:00
if ( ! pv_mda_count ( pv ) ) {
log_error ( " Physical volume \" %s \" has no metadata "
2010-06-30 21:13:05 +04:00
" areas. " , pv_name ) ;
2010-06-30 01:32:44 +04:00
return 0 ;
}
2010-06-30 21:13:05 +04:00
log_verbose ( " Marking metadata areas on physical volume \" %s \" "
2010-07-01 00:03:52 +04:00
" as %s. " , pv_name , mda_ignored ? " ignored " : " in-use " ) ;
2010-06-30 21:13:05 +04:00
2010-07-01 00:03:52 +04:00
if ( ! pv_mda_set_ignored ( pv , mda_ignored ) )
2010-06-30 21:13:05 +04:00
return_0 ;
2010-06-30 01:32:44 +04:00
/*
* Update vg_mda_copies based on the mdas in this PV .
* This is most likely what the user would expect - if they
* specify a specific PV to be ignored / un - ignored , they will
* most likely not want LVM to turn around and change the
* ignore / un - ignore value when it writes the VG to disk .
* This does not guarantee this PV ' s ignore bits will be
* preserved in future operations .
*/
2010-07-07 22:59:45 +04:00
if ( ! is_orphan ( pv ) & &
vg_mda_copies ( pv - > vg ) ! = VGMETADATACOPIES_UNMANAGED ) {
log_warn ( " WARNING: Changing preferred number of copies of VG %s "
" metadata from % " PRIu32 " to % " PRIu32 , pv_vg_name ( pv ) ,
vg_mda_copies ( pv - > vg ) , vg_mda_used_count ( pv - > vg ) ) ;
2010-06-30 01:32:44 +04:00
vg_set_mda_copies ( pv - > vg , vg_mda_used_count ( pv - > vg ) ) ;
2010-07-07 22:59:45 +04:00
}
2010-06-30 21:13:05 +04:00
2010-06-30 01:32:44 +04:00
return 1 ;
}
2014-01-31 01:09:28 +04:00
char * tags_format_and_copy ( struct dm_pool * mem , const struct dm_list * tagsl )
2010-09-30 18:08:07 +04:00
{
2014-05-29 11:41:03 +04:00
struct dm_str_list * sl ;
2010-09-30 18:08:07 +04:00
if ( ! dm_pool_begin_object ( mem , 256 ) ) {
log_error ( " dm_pool_begin_object failed " ) ;
return NULL ;
}
2014-01-31 01:09:28 +04:00
dm_list_iterate_items ( sl , tagsl ) {
2010-09-30 18:08:07 +04:00
if ( ! dm_pool_grow_object ( mem , sl - > str , strlen ( sl - > str ) ) | |
2014-01-31 01:09:28 +04:00
( sl - > list . n ! = tagsl & & ! dm_pool_grow_object ( mem , " , " , 1 ) ) ) {
2010-09-30 18:08:07 +04:00
log_error ( " dm_pool_grow_object failed " ) ;
return NULL ;
}
}
if ( ! dm_pool_grow_object ( mem , " \0 " , 1 ) ) {
log_error ( " dm_pool_grow_object failed " ) ;
return NULL ;
}
return dm_pool_end_object ( mem ) ;
}
2013-03-17 19:27:44 +04:00
2015-11-25 13:10:32 +03:00
const struct logical_volume * lv_committed ( const struct logical_volume * lv )
2013-03-17 19:27:44 +04:00
{
struct volume_group * vg ;
2015-11-22 01:31:44 +03:00
struct logical_volume * found_lv ;
2013-03-17 19:27:44 +04:00
if ( ! lv )
return NULL ;
2015-11-25 01:29:18 +03:00
if ( ! lv - > vg - > vg_committed )
2014-09-22 01:10:04 +04:00
return lv ;
2013-03-17 19:27:44 +04:00
2015-11-25 01:29:18 +03:00
vg = lv - > vg - > vg_committed ;
2013-03-17 19:27:44 +04:00
2015-11-22 01:31:44 +03:00
if ( ! ( found_lv = find_lv_in_vg_by_lvid ( vg , & lv - > lvid ) ) ) {
2015-11-25 13:10:32 +03:00
log_error ( INTERNAL_ERROR " LV %s (UUID %s) not found in committed metadata. " ,
2014-09-21 13:46:34 +04:00
display_lvname ( lv ) , lv - > lvid . s ) ;
return NULL ;
}
2013-03-17 19:27:44 +04:00
2015-11-22 01:31:44 +03:00
return found_lv ;
2013-03-17 19:27:44 +04:00
}
2015-03-05 23:00:44 +03:00
/*
* Check if a lock_type uses lvmlockd .
* If not ( none , clvm ) , return 0.
* If so ( dlm , sanlock ) , return 1.
*/
int is_lockd_type ( const char * lock_type )
{
if ( ! lock_type )
return 0 ;
if ( ! strcmp ( lock_type , " dlm " ) )
return 1 ;
if ( ! strcmp ( lock_type , " sanlock " ) )
return 1 ;
return 0 ;
}