2002-02-12 16:31:31 +00:00
/*
2004-03-30 19:35:44 +00:00
* Copyright ( C ) 2002 - 2004 Sistina Software , Inc . All rights reserved .
* Copyright ( C ) 2004 Red Hat , Inc . All rights reserved .
2002-02-12 16:31:31 +00:00
*
2004-03-30 19:35:44 +00: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 .
*
* 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
2002-02-12 16:31:31 +00:00
*/
2002-11-18 14:04:08 +00:00
# include "lib.h"
2002-02-12 16:31:31 +00:00
# include "metadata.h"
# include "toolcontext.h"
2005-04-07 12:39:44 +00:00
# include "lv_alloc.h"
2002-02-12 16:31:31 +00:00
2002-12-19 23:25:55 +00:00
int lv_is_origin ( const struct logical_volume * lv )
2002-02-12 16:31:31 +00:00
{
2005-04-07 12:39:44 +00:00
return lv - > origin_count ? 1 : 0 ;
2002-02-12 16:31:31 +00:00
}
2002-12-19 23:25:55 +00:00
int lv_is_cow ( const struct logical_volume * lv )
2002-02-12 16:31:31 +00:00
{
2005-04-07 12:39:44 +00:00
return lv - > snapshot ? 1 : 0 ;
2002-04-24 18:20:51 +00:00
}
2006-04-07 17:41:56 +00:00
int lv_is_visible ( const struct logical_volume * lv )
{
if ( lv_is_cow ( lv ) )
return lv_is_visible ( find_cow ( lv ) - > lv ) ;
return lv - > status & VISIBLE_LV ? 1 : 0 ;
}
2005-04-07 12:39:44 +00:00
/* Given a cow LV, return the snapshot lv_segment that uses it */
struct lv_segment * find_cow ( const struct logical_volume * lv )
2002-02-21 10:16:33 +00:00
{
2005-04-07 12:39:44 +00:00
return lv - > snapshot ;
2002-02-21 10:16:33 +00:00
}
2006-04-06 13:39:16 +00:00
/* Given a cow LV, return its origin */
struct logical_volume * origin_from_cow ( const struct logical_volume * lv )
{
return lv - > snapshot - > origin ;
}
2005-04-07 12:39:44 +00:00
int vg_add_snapshot ( struct format_instance * fid , const char * name ,
struct logical_volume * origin ,
struct logical_volume * cow , union lvid * lvid ,
uint32_t extent_count , uint32_t chunk_size )
2002-05-08 16:57:46 +00:00
{
2005-04-07 12:39:44 +00:00
struct logical_volume * snap ;
struct lv_segment * seg ;
2002-02-12 16:31:31 +00:00
/*
* Is the cow device already being used ?
*/
2002-02-20 19:04:55 +00:00
if ( lv_is_cow ( cow ) ) {
2002-02-12 16:31:31 +00:00
log_err ( " '%s' is already in use as a snapshot. " , cow - > name ) ;
return 0 ;
}
2005-06-03 14:49:51 +00:00
if ( ! ( snap = lv_create_empty ( fid , name ? name : " snapshot%d " ,
2005-04-07 12:39:44 +00:00
lvid , LVM_READ | LVM_WRITE | VISIBLE_LV ,
ALLOC_INHERIT , 1 , origin - > vg ) ) ) {
2002-02-12 16:31:31 +00:00
stack ;
return 0 ;
}
2005-04-07 12:39:44 +00:00
snap - > le_count = extent_count ;
2002-02-12 16:31:31 +00:00
2005-06-01 16:51:55 +00:00
if ( ! ( seg = alloc_snapshot_seg ( snap , 0 , 0 ) ) ) {
2002-02-12 16:31:31 +00:00
stack ;
return 0 ;
}
2005-04-07 12:39:44 +00:00
seg - > chunk_size = chunk_size ;
seg - > origin = origin ;
seg - > cow = cow ;
seg - > lv - > status | = SNAPSHOT ;
origin - > origin_count + + ;
2002-12-05 22:30:39 +00:00
origin - > vg - > snapshot_count + + ;
2005-04-07 12:39:44 +00:00
origin - > vg - > lv_count - - ;
cow - > snapshot = seg ;
cow - > status & = ~ VISIBLE_LV ;
list_add ( & origin - > snapshot_segs , & seg - > origin_list ) ;
2002-02-12 16:31:31 +00:00
return 1 ;
}
2005-05-09 16:59:01 +00:00
int vg_remove_snapshot ( struct logical_volume * cow )
2002-02-12 16:31:31 +00:00
{
2005-04-07 12:39:44 +00:00
list_del ( & cow - > snapshot - > origin_list ) ;
cow - > snapshot - > origin - > origin_count - - ;
2002-02-12 16:31:31 +00:00
2005-05-09 16:59:01 +00:00
if ( ! lv_remove ( cow - > snapshot - > lv ) ) {
2005-04-07 12:39:44 +00:00
log_error ( " Failed to remove internal snapshot LV %s " ,
cow - > snapshot - > lv - > name ) ;
return 0 ;
2002-02-12 16:31:31 +00:00
}
2005-04-07 12:39:44 +00:00
cow - > snapshot = NULL ;
2005-05-09 16:59:01 +00:00
cow - > vg - > snapshot_count - - ;
cow - > vg - > lv_count + + ;
2005-04-07 12:39:44 +00:00
cow - > status | = VISIBLE_LV ;
return 1 ;
2002-02-12 16:31:31 +00:00
}