2001-10-09 18:26:45 +04:00
/*
2004-03-30 23:35:44 +04:00
* Copyright ( C ) 2001 - 2004 Sistina Software , Inc . All rights reserved .
* Copyright ( C ) 2004 Red Hat , Inc . All rights reserved .
2001-10-09 18:26:45 +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
* of the GNU General Public License v .2 .
2001-10-31 20:59:52 +03:00
*
2004-03-30 23:35:44 +04:00
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software Foundation ,
* Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
*/
/*
* Translates between disk and in - core formats .
2001-10-09 18:26:45 +04:00
*/
2002-11-18 17:01:16 +03:00
# include "lib.h"
2001-10-09 18:26:45 +04:00
# include "disk-rep.h"
# include "pool.h"
# include "hash.h"
# include "list.h"
2002-01-29 20:23:33 +03:00
# include "lvm-string.h"
2003-03-24 21:08:53 +03:00
# include "filter.h"
2004-03-08 20:25:59 +03:00
# include "toolcontext.h"
2004-09-16 22:40:56 +04:00
# include "segtype.h"
2001-10-09 18:26:45 +04:00
2001-10-11 17:22:51 +04:00
# include <time.h>
2001-10-10 13:25:04 +04:00
2001-10-09 18:26:45 +04:00
static int _check_vg_name ( const char * name )
{
return strlen ( name ) < NAME_LEN ;
}
/*
* Extracts the last part of a path .
*/
static char * _create_lv_name ( struct pool * mem , const char * full_name )
{
2001-10-11 14:08:44 +04:00
const char * ptr = strrchr ( full_name , ' / ' ) ;
if ( ! ptr )
ptr = full_name ;
else
ptr + + ;
2001-10-09 18:26:45 +04:00
return pool_strdup ( mem , ptr ) ;
}
int import_pv ( struct pool * mem , struct device * dev ,
2002-01-29 20:23:33 +03:00
struct volume_group * vg ,
2001-10-09 18:26:45 +04:00
struct physical_volume * pv , struct pv_disk * pvd )
{
memset ( pv , 0 , sizeof ( * pv ) ) ;
2001-10-11 14:08:44 +04:00
memcpy ( & pv - > id , pvd - > pv_uuid , ID_LEN ) ;
2001-10-09 18:26:45 +04:00
pv - > dev = dev ;
if ( ! ( pv - > vg_name = pool_strdup ( mem , pvd - > vg_name ) ) ) {
stack ;
return 0 ;
}
2002-01-30 15:47:29 +03:00
/* Store system_id from first PV if PV belongs to a VG */
if ( vg & & ! * vg - > system_id )
2002-04-24 22:20:51 +04:00
strncpy ( vg - > system_id , pvd - > system_id , NAME_LEN ) ;
2002-01-29 20:23:33 +03:00
2002-01-30 15:47:29 +03:00
if ( vg & &
2002-01-29 20:23:33 +03:00
strncmp ( vg - > system_id , pvd - > system_id , sizeof ( pvd - > system_id ) ) )
2004-03-08 20:19:15 +03:00
log_very_verbose ( " System ID %s on %s differs from %s for "
" volume group " , pvd - > system_id ,
dev_name ( pv - > dev ) , vg - > system_id ) ;
2002-02-12 19:31:31 +03:00
/*
2002-01-29 20:23:33 +03:00
* If exported , we still need to flag in pv - > status too because
* we don ' t always have a struct volume_group when we need this .
*/
if ( pvd - > pv_status & VG_EXPORTED )
pv - > status | = EXPORTED_VG ;
2001-10-09 18:26:45 +04:00
if ( pvd - > pv_allocatable )
2002-01-10 18:09:51 +03:00
pv - > status | = ALLOCATABLE_PV ;
2001-10-09 18:26:45 +04:00
pv - > size = pvd - > pv_size ;
pv - > pe_size = pvd - > pe_size ;
pv - > pe_start = pvd - > pe_start ;
pv - > pe_count = pvd - > pe_total ;
2002-04-24 22:20:51 +04:00
pv - > pe_alloc_count = pvd - > pe_allocated ;
2001-11-27 19:37:33 +03:00
2004-03-08 20:19:15 +03:00
list_init ( & pv - > tags ) ;
2001-10-09 18:26:45 +04:00
return 1 ;
}
2004-03-08 20:25:59 +03:00
static int _system_id ( struct cmd_context * cmd , char * s , const char * prefix )
2001-10-11 17:22:51 +04:00
{
2002-02-12 19:31:31 +03:00
if ( lvm_snprintf ( s , NAME_LEN , " %s%s%lu " ,
2004-03-08 20:25:59 +03:00
prefix , cmd - > hostname , time ( NULL ) ) < 0 ) {
2002-01-29 20:23:33 +03:00
log_error ( " Generated system_id too long " ) ;
return 0 ;
}
2001-10-11 17:22:51 +04:00
return 1 ;
}
2004-03-08 20:25:59 +03:00
int export_pv ( struct cmd_context * cmd , struct pool * mem ,
struct volume_group * vg ,
2002-01-29 20:23:33 +03:00
struct pv_disk * pvd , struct physical_volume * pv )
2001-10-09 18:26:45 +04:00
{
memset ( pvd , 0 , sizeof ( * pvd ) ) ;
pvd - > id [ 0 ] = ' H ' ;
pvd - > id [ 1 ] = ' M ' ;
pvd - > version = 1 ;
2001-10-11 14:08:44 +04:00
memcpy ( pvd - > pv_uuid , pv - > id . uuid , ID_LEN ) ;
2001-10-09 18:26:45 +04:00
if ( ! _check_vg_name ( pv - > vg_name ) ) {
stack ;
return 0 ;
}
2001-10-10 14:05:29 +04:00
memset ( pvd - > vg_name , 0 , sizeof ( pvd - > vg_name ) ) ;
if ( pv - > vg_name )
2001-10-10 17:09:40 +04:00
strncpy ( pvd - > vg_name , pv - > vg_name , sizeof ( pvd - > vg_name ) ) ;
2001-10-09 18:26:45 +04:00
2002-01-29 20:23:33 +03:00
/* Preserve existing system_id if it exists */
2002-02-12 19:31:31 +03:00
if ( vg & & * vg - > system_id )
2002-01-29 20:23:33 +03:00
strncpy ( pvd - > system_id , vg - > system_id , sizeof ( pvd - > system_id ) ) ;
/* Is VG already exported or being exported? */
if ( vg & & ( vg - > status & EXPORTED_VG ) ) {
/* Does system_id need setting? */
2002-02-12 19:31:31 +03:00
if ( ! * vg - > system_id | |
strncmp ( vg - > system_id , EXPORTED_TAG ,
2002-01-29 22:19:37 +03:00
sizeof ( EXPORTED_TAG ) - 1 ) ) {
2004-03-08 20:25:59 +03:00
if ( ! _system_id ( cmd , pvd - > system_id , EXPORTED_TAG ) ) {
2002-01-29 20:23:33 +03:00
stack ;
return 0 ;
}
}
if ( strlen ( pvd - > vg_name ) + sizeof ( EXPORTED_TAG ) >
sizeof ( pvd - > vg_name ) ) {
log_error ( " Volume group name %s too long to export " ,
pvd - > vg_name ) ;
return 0 ;
}
strcat ( pvd - > vg_name , EXPORTED_TAG ) ;
}
/* Is VG being imported? */
2002-01-30 15:47:29 +03:00
if ( vg & & ! ( vg - > status & EXPORTED_VG ) & & * vg - > system_id & &
2002-01-29 22:19:37 +03:00
! strncmp ( vg - > system_id , EXPORTED_TAG , sizeof ( EXPORTED_TAG ) - 1 ) ) {
2004-03-08 20:25:59 +03:00
if ( ! _system_id ( cmd , pvd - > system_id , IMPORTED_TAG ) ) {
2002-01-29 20:23:33 +03:00
stack ;
return 0 ;
}
}
/* Generate system_id if PV is in VG */
if ( ! pvd - > system_id | | ! * pvd - > system_id )
2004-03-08 20:25:59 +03:00
if ( ! _system_id ( cmd , pvd - > system_id , " " ) ) {
2002-01-29 20:23:33 +03:00
stack ;
return 0 ;
}
/* Update internal system_id if we changed it */
2002-02-12 19:31:31 +03:00
if ( vg & &
2002-01-30 15:47:29 +03:00
( ! * vg - > system_id | |
strncmp ( vg - > system_id , pvd - > system_id , sizeof ( pvd - > system_id ) ) ) )
2004-03-08 20:19:15 +03:00
strncpy ( vg - > system_id , pvd - > system_id , NAME_LEN ) ;
2002-01-29 20:23:33 +03:00
2001-10-09 18:26:45 +04:00
//pvd->pv_major = MAJOR(pv->dev);
2002-01-10 18:09:51 +03:00
if ( pv - > status & ALLOCATABLE_PV )
2001-10-11 17:34:17 +04:00
pvd - > pv_allocatable = PV_ALLOCATABLE ;
2001-10-09 18:26:45 +04:00
pvd - > pv_size = pv - > size ;
pvd - > lv_cur = 0 ; /* this is set when exporting the lv list */
2002-11-18 17:01:16 +03:00
if ( vg )
pvd - > pe_size = vg - > extent_size ;
else
pvd - > pe_size = pv - > pe_size ;
2001-10-09 18:26:45 +04:00
pvd - > pe_total = pv - > pe_count ;
2002-04-24 22:20:51 +04:00
pvd - > pe_allocated = pv - > pe_alloc_count ;
2001-10-09 18:26:45 +04:00
pvd - > pe_start = pv - > pe_start ;
2001-10-11 17:22:51 +04:00
2001-10-09 18:26:45 +04:00
return 1 ;
}
int import_vg ( struct pool * mem ,
2002-04-24 22:20:51 +04:00
struct volume_group * vg , struct disk_list * dl , int partial )
2001-10-09 18:26:45 +04:00
{
2001-10-31 20:59:52 +03:00
struct vg_disk * vgd = & dl - > vgd ;
2001-10-11 14:08:44 +04:00
memcpy ( vg - > id . uuid , vgd - > vg_uuid , ID_LEN ) ;
2001-10-09 18:26:45 +04:00
2001-10-31 20:59:52 +03:00
if ( ! _check_vg_name ( dl - > pvd . vg_name ) ) {
2001-10-09 18:26:45 +04:00
stack ;
return 0 ;
}
2001-10-31 20:59:52 +03:00
if ( ! ( vg - > name = pool_strdup ( mem , dl - > pvd . vg_name ) ) ) {
2001-10-09 18:26:45 +04:00
stack ;
return 0 ;
}
2002-01-30 15:47:29 +03:00
if ( ! ( vg - > system_id = pool_alloc ( mem , NAME_LEN ) ) ) {
stack ;
return 0 ;
}
* vg - > system_id = ' \0 ' ;
2001-10-09 18:26:45 +04:00
if ( vgd - > vg_status & VG_EXPORTED )
vg - > status | = EXPORTED_VG ;
if ( vgd - > vg_status & VG_EXTENDABLE )
2002-01-10 18:09:51 +03:00
vg - > status | = RESIZEABLE_VG ;
2001-10-09 18:26:45 +04:00
2002-01-29 20:23:33 +03:00
if ( partial | | ( vgd - > vg_access & VG_READ ) )
2001-10-09 18:26:45 +04:00
vg - > status | = LVM_READ ;
2002-01-29 20:23:33 +03:00
if ( ! partial & & ( vgd - > vg_access & VG_WRITE ) )
2001-10-09 18:26:45 +04:00
vg - > status | = LVM_WRITE ;
if ( vgd - > vg_access & VG_CLUSTERED )
vg - > status | = CLUSTERED ;
if ( vgd - > vg_access & VG_SHARED )
vg - > status | = SHARED ;
vg - > extent_size = vgd - > pe_size ;
vg - > extent_count = vgd - > pe_total ;
vg - > free_count = vgd - > pe_total - vgd - > pe_allocated ;
vg - > max_lv = vgd - > lv_max ;
vg - > max_pv = vgd - > pv_max ;
2004-05-19 02:12:53 +04:00
vg - > alloc = ALLOC_NORMAL ;
2002-01-29 20:23:33 +03:00
if ( partial )
vg - > status | = PARTIAL_VG ;
2001-10-09 18:26:45 +04:00
return 1 ;
}
int export_vg ( struct vg_disk * vgd , struct volume_group * vg )
{
memset ( vgd , 0 , sizeof ( * vgd ) ) ;
2001-10-11 14:08:44 +04:00
memcpy ( vgd - > vg_uuid , vg - > id . uuid , ID_LEN ) ;
2001-10-09 18:26:45 +04:00
2001-10-11 19:09:31 +04:00
if ( vg - > status & LVM_READ )
2001-10-09 18:26:45 +04:00
vgd - > vg_access | = VG_READ ;
if ( vg - > status & LVM_WRITE )
vgd - > vg_access | = VG_WRITE ;
if ( vg - > status & CLUSTERED )
vgd - > vg_access | = VG_CLUSTERED ;
if ( vg - > status & SHARED )
vgd - > vg_access | = VG_SHARED ;
if ( vg - > status & EXPORTED_VG )
vgd - > vg_status | = VG_EXPORTED ;
2002-01-10 18:09:51 +03:00
if ( vg - > status & RESIZEABLE_VG )
2001-10-09 18:26:45 +04:00
vgd - > vg_status | = VG_EXTENDABLE ;
vgd - > lv_max = vg - > max_lv ;
vgd - > lv_cur = vg - > lv_count ;
vgd - > pv_max = vg - > max_pv ;
vgd - > pv_cur = vg - > pv_count ;
vgd - > pe_size = vg - > extent_size ;
vgd - > pe_total = vg - > extent_count ;
vgd - > pe_allocated = vg - > extent_count - vg - > free_count ;
2001-10-11 17:22:51 +04:00
2001-10-09 18:26:45 +04:00
return 1 ;
}
int import_lv ( struct pool * mem , struct logical_volume * lv , struct lv_disk * lvd )
{
2002-03-05 23:03:09 +03:00
lvid_from_lvnum ( & lv - > lvid , & lv - > vg - > id , lvd - > lv_number ) ;
2002-02-21 18:26:44 +03:00
2002-04-24 22:20:51 +04:00
if ( ! ( lv - > name = _create_lv_name ( mem , lvd - > lv_name ) ) ) {
2001-10-09 18:26:45 +04:00
stack ;
return 0 ;
}
2002-11-18 17:01:16 +03:00
lv - > status | = VISIBLE_LV ;
2001-10-09 18:26:45 +04:00
if ( lvd - > lv_status & LV_SPINDOWN )
lv - > status | = SPINDOWN_LV ;
2002-02-01 20:54:39 +03:00
if ( lvd - > lv_status & LV_PERSISTENT_MINOR ) {
lv - > status | = FIXED_MINOR ;
lv - > minor = MINOR ( lvd - > lv_dev ) ;
2003-04-02 23:14:43 +04:00
lv - > major = MAJOR ( lvd - > lv_dev ) ;
} else {
lv - > major = - 1 ;
2002-02-01 20:54:39 +03:00
lv - > minor = - 1 ;
2003-04-02 23:14:43 +04:00
}
2002-02-01 20:54:39 +03:00
2001-10-09 18:26:45 +04:00
if ( lvd - > lv_access & LV_READ )
lv - > status | = LVM_READ ;
if ( lvd - > lv_access & LV_WRITE )
lv - > status | = LVM_WRITE ;
if ( lvd - > lv_badblock )
lv - > status | = BADBLOCK_ON ;
2002-11-18 17:01:16 +03:00
/* Drop the unused LV_STRICT here */
2001-11-12 20:55:05 +03:00
if ( lvd - > lv_allocation & LV_CONTIGUOUS )
2002-07-11 18:21:49 +04:00
lv - > alloc = ALLOC_CONTIGUOUS ;
2001-11-12 20:55:05 +03:00
else
2004-05-19 02:12:53 +04:00
lv - > alloc = ALLOC_NORMAL ;
2001-10-09 18:26:45 +04:00
2001-10-29 18:34:56 +03:00
lv - > read_ahead = lvd - > lv_read_ahead ;
2002-04-24 22:20:51 +04:00
lv - > size = lvd - > lv_size ;
lv - > le_count = lvd - > lv_allocated_le ;
2001-10-09 18:26:45 +04:00
2001-11-27 19:37:33 +03:00
list_init ( & lv - > segments ) ;
2004-03-08 20:19:15 +03:00
list_init ( & lv - > tags ) ;
2001-11-27 19:37:33 +03:00
2001-10-09 18:26:45 +04:00
return 1 ;
}
2002-11-18 17:01:16 +03:00
static void _export_lv ( struct lv_disk * lvd , struct volume_group * vg ,
struct logical_volume * lv , const char * dev_dir )
2001-10-09 18:26:45 +04:00
{
memset ( lvd , 0 , sizeof ( * lvd ) ) ;
2001-10-29 18:28:00 +03:00
snprintf ( lvd - > lv_name , sizeof ( lvd - > lv_name ) , " %s%s/%s " ,
2001-11-14 16:52:38 +03:00
dev_dir , vg - > name , lv - > name ) ;
2001-10-09 18:26:45 +04:00
strcpy ( lvd - > vg_name , vg - > name ) ;
if ( lv - > status & LVM_READ )
lvd - > lv_access | = LV_READ ;
if ( lv - > status & LVM_WRITE )
lvd - > lv_access | = LV_WRITE ;
if ( lv - > status & SPINDOWN_LV )
lvd - > lv_status | = LV_SPINDOWN ;
2002-02-01 20:54:39 +03:00
if ( lv - > status & FIXED_MINOR ) {
lvd - > lv_status | = LV_PERSISTENT_MINOR ;
2003-04-02 23:14:43 +04:00
lvd - > lv_dev = MKDEV ( lv - > major , lv - > minor ) ;
2003-11-06 19:58:38 +03:00
} else {
lvd - > lv_dev = MKDEV ( LVM_BLK_MAJOR , lvnum_from_lvid ( & lv - > lvid ) ) ;
2002-02-01 20:54:39 +03:00
}
2001-10-29 18:34:56 +03:00
lvd - > lv_read_ahead = lv - > read_ahead ;
2003-04-25 02:23:24 +04:00
lvd - > lv_stripes =
list_item ( lv - > segments . n , struct lv_segment ) - > area_count ;
lvd - > lv_stripesize =
list_item ( lv - > segments . n , struct lv_segment ) - > stripe_size ;
2001-10-29 18:34:56 +03:00
2002-04-24 22:20:51 +04:00
lvd - > lv_size = lv - > size ;
lvd - > lv_allocated_le = lv - > le_count ;
2001-10-09 18:26:45 +04:00
if ( lv - > status & BADBLOCK_ON )
lvd - > lv_badblock = LV_BADBLOCK_ON ;
2002-07-11 18:21:49 +04:00
if ( lv - > alloc = = ALLOC_CONTIGUOUS )
2001-11-12 20:55:05 +03:00
lvd - > lv_allocation | = LV_CONTIGUOUS ;
2001-10-09 18:26:45 +04:00
}
2002-12-20 02:25:55 +03:00
int export_extents ( struct disk_list * dl , uint32_t lv_num ,
2002-04-24 22:20:51 +04:00
struct logical_volume * lv , struct physical_volume * pv )
2001-10-09 18:26:45 +04:00
{
2001-11-27 19:37:33 +03:00
struct list * segh ;
struct pe_disk * ped ;
2002-11-18 17:01:16 +03:00
struct lv_segment * seg ;
2001-11-27 20:29:56 +03:00
uint32_t pe , s ;
2001-10-09 18:26:45 +04:00
2002-04-24 22:20:51 +04:00
list_iterate ( segh , & lv - > segments ) {
2002-11-18 17:01:16 +03:00
seg = list_item ( segh , struct lv_segment ) ;
2001-10-09 18:26:45 +04:00
2003-04-25 02:23:24 +04:00
for ( s = 0 ; s < seg - > area_count ; s + + ) {
2004-05-05 01:25:57 +04:00
if ( ! ( seg - > segtype - > flags & SEG_FORMAT1_SUPPORT ) ) {
log_error ( " Segment type %s in LV %s: "
" unsupported by format1 " ,
seg - > segtype - > name , lv - > name ) ;
2003-04-25 02:23:24 +04:00
return 0 ;
}
if ( seg - > area [ s ] . type ! = AREA_PV ) {
log_error ( " LV stripe found in LV %s: "
" unsupported by format1 " , lv - > name ) ;
return 0 ;
}
if ( seg - > area [ s ] . u . pv . pv ! = pv )
2002-04-24 22:20:51 +04:00
continue ; /* not our pv */
2001-10-09 18:26:45 +04:00
2003-04-25 02:23:24 +04:00
for ( pe = 0 ; pe < ( seg - > len / seg - > area_count ) ; pe + + ) {
ped = & dl - > extents [ pe + seg - > area [ s ] . u . pv . pe ] ;
2001-11-27 19:37:33 +03:00
ped - > lv_num = lv_num ;
2003-04-25 02:23:24 +04:00
ped - > le_num = ( seg - > le / seg - > area_count ) + pe +
s * ( lv - > le_count / seg - > area_count ) ;
2001-10-09 18:26:45 +04:00
}
}
}
return 1 ;
}
2002-12-20 02:25:55 +03:00
int import_pvs ( const struct format_type * fmt , struct pool * mem ,
2002-04-24 22:20:51 +04:00
struct volume_group * vg ,
2002-01-29 20:23:33 +03:00
struct list * pvds , struct list * results , int * count )
2001-10-09 18:26:45 +04:00
{
2001-11-14 16:52:38 +03:00
struct list * pvdh ;
2001-10-09 18:26:45 +04:00
struct disk_list * dl ;
struct pv_list * pvl ;
* count = 0 ;
2001-11-14 16:52:38 +03:00
list_iterate ( pvdh , pvds ) {
2002-01-21 19:05:23 +03:00
2001-11-14 16:52:38 +03:00
dl = list_item ( pvdh , struct disk_list ) ;
2001-10-09 18:26:45 +04:00
2003-04-25 02:23:24 +04:00
if ( ! ( pvl = pool_zalloc ( mem , sizeof ( * pvl ) ) ) | |
2002-01-21 19:05:23 +03:00
! ( pvl - > pv = pool_alloc ( mem , sizeof ( * pvl - > pv ) ) ) ) {
2001-10-09 18:26:45 +04:00
stack ;
return 0 ;
}
2002-01-29 20:23:33 +03:00
if ( ! import_pv ( mem , dl - > dev , vg , pvl - > pv , & dl - > pvd ) ) {
2001-10-09 18:26:45 +04:00
stack ;
return 0 ;
}
2002-11-18 17:01:16 +03:00
pvl - > pv - > fmt = fmt ;
2001-10-31 15:47:01 +03:00
list_add ( results , & pvl - > list ) ;
2001-10-09 18:26:45 +04:00
( * count ) + + ;
}
return 1 ;
}
static struct logical_volume * _add_lv ( struct pool * mem ,
struct volume_group * vg ,
struct lv_disk * lvd )
{
2002-01-21 19:49:32 +03:00
struct lv_list * ll ;
2001-10-09 18:26:45 +04:00
struct logical_volume * lv ;
2002-01-21 19:49:32 +03:00
if ( ! ( ll = pool_zalloc ( mem , sizeof ( * ll ) ) ) | |
! ( ll - > lv = pool_zalloc ( mem , sizeof ( * ll - > lv ) ) ) ) {
2001-10-09 18:26:45 +04:00
stack ;
return NULL ;
}
2002-01-21 19:49:32 +03:00
lv = ll - > lv ;
2002-03-05 23:03:09 +03:00
lv - > vg = vg ;
2001-10-09 18:26:45 +04:00
2002-01-21 19:49:32 +03:00
if ( ! import_lv ( mem , lv , lvd ) ) {
2001-10-09 18:26:45 +04:00
stack ;
return NULL ;
}
2001-10-31 15:47:01 +03:00
list_add ( & vg - > lvs , & ll - > list ) ;
2001-10-09 18:26:45 +04:00
vg - > lv_count + + ;
return lv ;
}
2002-04-24 22:20:51 +04:00
int import_lvs ( struct pool * mem , struct volume_group * vg , struct list * pvds )
2001-10-09 18:26:45 +04:00
{
struct disk_list * dl ;
struct lvd_list * ll ;
struct lv_disk * lvd ;
2001-11-14 16:52:38 +03:00
struct list * pvdh , * lvdh ;
2001-10-09 18:26:45 +04:00
2001-11-14 16:52:38 +03:00
list_iterate ( pvdh , pvds ) {
dl = list_item ( pvdh , struct disk_list ) ;
list_iterate ( lvdh , & dl - > lvds ) {
ll = list_item ( lvdh , struct lvd_list ) ;
2001-10-31 20:59:52 +03:00
lvd = & ll - > lvd ;
2001-10-09 18:26:45 +04:00
2001-10-29 16:52:23 +03:00
if ( ! find_lv ( vg , lvd - > lv_name ) & &
2001-10-09 18:26:45 +04:00
! _add_lv ( mem , vg , lvd ) ) {
stack ;
return 0 ;
}
}
}
return 1 ;
}
2002-02-13 14:43:29 +03:00
/* FIXME: tidy */
2001-10-09 18:26:45 +04:00
int export_lvs ( struct disk_list * dl , struct volume_group * vg ,
2001-11-14 16:52:38 +03:00
struct physical_volume * pv , const char * dev_dir )
2001-10-09 18:26:45 +04:00
{
2002-02-13 14:43:29 +03:00
int r = 0 ;
struct list * lvh , * sh ;
2001-10-09 18:26:45 +04:00
struct lv_list * ll ;
struct lvd_list * lvdl ;
2002-12-20 02:25:55 +03:00
size_t len ;
uint32_t lv_num ;
2002-02-13 14:43:29 +03:00
struct hash_table * lvd_hash ;
2002-11-18 17:01:16 +03:00
if ( ! _check_vg_name ( vg - > name ) ) {
stack ;
return 0 ;
}
2002-02-13 14:43:29 +03:00
if ( ! ( lvd_hash = hash_create ( 32 ) ) ) {
stack ;
return 0 ;
}
2001-10-10 18:56:53 +04:00
/*
* setup the pv ' s extents array
*/
2001-10-31 20:59:52 +03:00
len = sizeof ( struct pe_disk ) * dl - > pvd . pe_total ;
2001-10-10 18:56:53 +04:00
if ( ! ( dl - > extents = pool_alloc ( dl - > mem , len ) ) ) {
stack ;
2002-02-13 14:43:29 +03:00
goto out ;
2001-10-10 18:56:53 +04:00
}
2001-10-11 14:08:44 +04:00
memset ( dl - > extents , 0 , len ) ;
2001-10-10 18:56:53 +04:00
2002-04-24 22:20:51 +04:00
list_iterate ( lvh , & vg - > lvs ) {
2001-10-31 15:47:01 +03:00
ll = list_item ( lvh , struct lv_list ) ;
2001-10-09 18:26:45 +04:00
if ( ! ( lvdl = pool_alloc ( dl - > mem , sizeof ( * lvdl ) ) ) ) {
stack ;
2002-02-13 14:43:29 +03:00
goto out ;
2001-10-09 18:26:45 +04:00
}
2002-11-18 17:01:16 +03:00
_export_lv ( & lvdl - > lvd , vg , ll - > lv , dev_dir ) ;
2002-02-13 14:43:29 +03:00
2002-03-05 23:03:09 +03:00
lv_num = lvnum_from_lvid ( & ll - > lv - > lvid ) ;
2002-02-21 22:04:37 +03:00
2001-11-10 01:01:04 +03:00
lvdl - > lvd . lv_number = lv_num ;
2002-02-13 14:43:29 +03:00
2002-02-18 14:25:43 +03:00
if ( ! hash_insert ( lvd_hash , ll - > lv - > name , & lvdl - > lvd ) ) {
2002-02-13 14:43:29 +03:00
stack ;
goto out ;
}
2002-01-21 19:49:32 +03:00
if ( ! export_extents ( dl , lv_num + 1 , ll - > lv , pv ) ) {
2001-10-09 18:26:45 +04:00
stack ;
2002-02-13 14:43:29 +03:00
goto out ;
2001-10-09 18:26:45 +04:00
}
2001-10-31 20:59:52 +03:00
list_add ( & dl - > lvds , & lvdl - > list ) ;
dl - > pvd . lv_cur + + ;
2001-10-09 18:26:45 +04:00
}
2002-02-13 14:43:29 +03:00
/*
* Now we need to run through the snapshots , exporting
* the SNAPSHOT_ORG flags etc .
*/
2002-04-24 22:20:51 +04:00
list_iterate ( sh , & vg - > snapshots ) {
2002-02-13 14:43:29 +03:00
struct lv_disk * org , * cow ;
struct snapshot * s = list_item ( sh ,
struct snapshot_list ) - > snapshot ;
if ( ! ( org = hash_lookup ( lvd_hash , s - > origin - > name ) ) ) {
2002-02-18 14:25:43 +03:00
log_err ( " Couldn't find snapshot origin '%s'. " ,
s - > origin - > name ) ;
2002-02-13 14:43:29 +03:00
goto out ;
}
if ( ! ( cow = hash_lookup ( lvd_hash , s - > cow - > name ) ) ) {
2002-02-18 14:25:43 +03:00
log_err ( " Couldn't find snapshot cow store '%s'. " ,
s - > cow - > name ) ;
2002-02-13 14:43:29 +03:00
goto out ;
}
org - > lv_access | = LV_SNAPSHOT_ORG ;
cow - > lv_access | = LV_SNAPSHOT ;
2002-02-21 22:04:37 +03:00
cow - > lv_snapshot_minor = org - > lv_number ;
2002-02-26 19:48:52 +03:00
cow - > lv_chunk_size = s - > chunk_size ;
2002-02-13 14:43:29 +03:00
}
r = 1 ;
2002-04-24 22:20:51 +04:00
out :
2002-02-13 14:43:29 +03:00
hash_destroy ( lvd_hash ) ;
return r ;
}
/*
* FIXME : More inefficient code .
*/
int import_snapshots ( struct pool * mem , struct volume_group * vg ,
struct list * pvds )
{
struct logical_volume * lvs [ MAX_LV ] ;
struct list * pvdh , * lvdh ;
struct disk_list * dl ;
struct lv_disk * lvd ;
2002-02-21 22:04:37 +03:00
int lvnum ;
2002-02-13 14:43:29 +03:00
struct logical_volume * org , * cow ;
2002-02-21 22:04:37 +03:00
/* build an index of lv numbers */
2002-02-13 14:43:29 +03:00
memset ( lvs , 0 , sizeof ( lvs ) ) ;
2002-04-24 22:20:51 +04:00
list_iterate ( pvdh , pvds ) {
2002-02-13 14:43:29 +03:00
dl = list_item ( pvdh , struct disk_list ) ;
2002-04-24 22:20:51 +04:00
list_iterate ( lvdh , & dl - > lvds ) {
2002-02-13 14:43:29 +03:00
lvd = & ( list_item ( lvdh , struct lvd_list ) - > lvd ) ;
2002-02-21 22:04:37 +03:00
lvnum = lvd - > lv_number ;
2002-02-13 14:43:29 +03:00
2002-02-21 22:04:37 +03:00
if ( lvnum > MAX_LV ) {
log_err ( " Logical volume number "
2002-02-13 14:43:29 +03:00
" out of bounds. " ) ;
return 0 ;
}
2002-02-21 22:04:37 +03:00
if ( ! lvs [ lvnum ] & &
! ( lvs [ lvnum ] = find_lv ( vg , lvd - > lv_name ) ) ) {
2002-02-13 14:43:29 +03:00
log_err ( " Couldn't find logical volume '%s'. " ,
lvd - > lv_name ) ;
return 0 ;
}
}
}
/*
* Now iterate through yet again adding the snapshots .
*/
2002-04-24 22:20:51 +04:00
list_iterate ( pvdh , pvds ) {
2002-02-13 14:43:29 +03:00
dl = list_item ( pvdh , struct disk_list ) ;
2002-04-24 22:20:51 +04:00
list_iterate ( lvdh , & dl - > lvds ) {
2002-02-13 14:43:29 +03:00
lvd = & ( list_item ( lvdh , struct lvd_list ) - > lvd ) ;
2002-02-21 00:24:45 +03:00
if ( ! ( lvd - > lv_access & LV_SNAPSHOT ) )
2002-02-13 14:43:29 +03:00
continue ;
2002-02-21 22:04:37 +03:00
lvnum = lvd - > lv_number ;
cow = lvs [ lvnum ] ;
2002-02-13 14:43:29 +03:00
if ( ! ( org = lvs [ lvd - > lv_snapshot_minor ] ) ) {
log_err ( " Couldn't find origin logical volume "
" for snapshot '%s'. " , lvd - > lv_name ) ;
return 0 ;
}
/* we may have already added this snapshot */
2002-02-20 22:04:55 +03:00
if ( lv_is_cow ( cow ) )
2002-02-13 14:43:29 +03:00
continue ;
/* insert the snapshot */
2005-01-19 20:19:39 +03:00
if ( ! vg_add_snapshot ( org , cow , 1 , NULL , org - > le_count ,
2002-11-18 17:01:16 +03:00
lvd - > lv_chunk_size ) ) {
2002-02-13 14:43:29 +03:00
log_err ( " Couldn't add snapshot. " ) ;
return 0 ;
}
}
}
2001-10-09 18:26:45 +04:00
return 1 ;
}
int export_uuids ( struct disk_list * dl , struct volume_group * vg )
{
struct uuid_list * ul ;
struct pv_list * pvl ;
2001-10-31 15:47:01 +03:00
struct list * pvh ;
2001-10-09 18:26:45 +04:00
2001-10-31 15:47:01 +03:00
list_iterate ( pvh , & vg - > pvs ) {
pvl = list_item ( pvh , struct pv_list ) ;
2001-10-09 18:26:45 +04:00
if ( ! ( ul = pool_alloc ( dl - > mem , sizeof ( * ul ) ) ) ) {
stack ;
return 0 ;
}
2001-10-11 14:08:44 +04:00
memset ( ul - > uuid , 0 , sizeof ( ul - > uuid ) ) ;
2002-01-21 19:05:23 +03:00
memcpy ( ul - > uuid , pvl - > pv - > id . uuid , ID_LEN ) ;
2001-10-09 18:26:45 +04:00
2001-10-31 15:47:01 +03:00
list_add ( & dl - > uuids , & ul - > list ) ;
2001-10-09 18:26:45 +04:00
}
return 1 ;
}
2001-10-11 14:55:19 +04:00
/*
2002-02-21 22:04:37 +03:00
* This calculates the nasty pv_number field
* used by LVM1 .
2001-10-11 14:55:19 +04:00
*/
2001-11-14 16:52:38 +03:00
void export_numbers ( struct list * pvds , struct volume_group * vg )
2001-10-11 14:55:19 +04:00
{
2001-11-14 16:52:38 +03:00
struct list * pvdh ;
2001-10-11 14:55:19 +04:00
struct disk_list * dl ;
2001-10-11 20:31:09 +04:00
int pv_num = 1 ;
2001-10-11 14:55:19 +04:00
2001-11-14 16:52:38 +03:00
list_iterate ( pvdh , pvds ) {
dl = list_item ( pvdh , struct disk_list ) ;
2001-10-31 20:59:52 +03:00
dl - > pvd . pv_number = pv_num + + ;
2001-10-11 14:55:19 +04:00
}
}
2001-10-11 17:05:55 +04:00
/*
* Calculate vg_disk - > pv_act .
*/
2001-11-14 16:52:38 +03:00
void export_pv_act ( struct list * pvds )
2001-10-11 17:05:55 +04:00
{
2001-11-14 16:52:38 +03:00
struct list * pvdh ;
2001-10-11 17:05:55 +04:00
struct disk_list * dl ;
int act = 0 ;
2001-11-14 16:52:38 +03:00
list_iterate ( pvdh , pvds ) {
dl = list_item ( pvdh , struct disk_list ) ;
2001-10-31 20:59:52 +03:00
if ( dl - > pvd . pv_status & PV_ACTIVE )
2001-10-11 17:05:55 +04:00
act + + ;
}
2001-11-14 16:52:38 +03:00
list_iterate ( pvdh , pvds ) {
dl = list_item ( pvdh , struct disk_list ) ;
2001-10-31 20:59:52 +03:00
dl - > vgd . pv_act = act ;
2001-10-11 17:05:55 +04:00
}
}
2001-10-11 18:10:18 +04:00
2002-04-24 22:20:51 +04:00
int export_vg_number ( struct format_instance * fid , struct list * pvds ,
const char * vg_name , struct dev_filter * filter )
2001-10-11 18:10:18 +04:00
{
2001-11-14 16:52:38 +03:00
struct list * pvdh ;
2001-10-11 18:10:18 +04:00
struct disk_list * dl ;
int vg_num ;
2002-04-24 22:20:51 +04:00
if ( ! get_free_vg_number ( fid , filter , vg_name , & vg_num ) ) {
2001-10-11 18:10:18 +04:00
stack ;
return 0 ;
}
2001-11-14 16:52:38 +03:00
list_iterate ( pvdh , pvds ) {
dl = list_item ( pvdh , struct disk_list ) ;
2001-10-31 20:59:52 +03:00
dl - > vgd . vg_number = vg_num ;
2001-10-11 18:10:18 +04:00
}
return 1 ;
}