2001-10-09 20:05:34 +04:00
/*
* Copyright ( C ) 2001 Sistina Software ( UK ) Limited .
*
2001-10-31 15:47:01 +03:00
* This file is released under the LGPL .
2001-10-09 20:05:34 +04:00
*/
2002-11-18 17:01:16 +03:00
# include "lib.h"
2001-10-16 20:25:28 +04:00
# include "metadata.h"
2001-10-09 20:05:34 +04:00
# include "activate.h"
2001-11-15 17:27:51 +03:00
# include "display.h"
2001-11-12 15:20:58 +03:00
# include "fs.h"
2001-12-31 22:09:51 +03:00
# include "lvm-string.h"
2002-02-25 15:56:16 +03:00
# include "pool.h"
# include "toolcontext.h"
2002-02-26 14:49:17 +03:00
# include "dev_manager.h"
2002-01-21 14:06:32 +03:00
# include <limits.h>
2002-02-18 18:52:48 +03:00
# include <fcntl.h>
2002-01-21 14:06:32 +03:00
2002-02-11 18:48:34 +03:00
# define _skip(fmt, args...) log_very_verbose("Skipping: " fmt , ## args)
2001-10-09 20:05:34 +04:00
2003-01-09 01:44:07 +03:00
# ifndef DEVMAPPER_SUPPORT
void set_activation ( int act )
{
if ( act )
log_error ( " Compiled without libdevmapper support. "
" Can't enable activation. " ) ;
}
int activation ( void )
{
return 0 ;
}
int library_version ( char * version , size_t size )
{
return 0 ;
}
int driver_version ( char * version , size_t size )
{
return 0 ;
}
int lv_info ( const struct logical_volume * lv , struct lvinfo * info )
{
return 0 ;
}
int lv_snapshot_percent ( struct logical_volume * lv , float * percent )
{
return 0 ;
}
int lvs_in_vg_activated ( struct volume_group * vg )
{
return 0 ;
}
int lvs_in_vg_opened ( struct volume_group * vg )
{
return 0 ;
}
int lv_suspend_if_active ( struct cmd_context * cmd , const char * lvid_s )
{
return 1 ;
}
int lv_resume_if_active ( struct cmd_context * cmd , const char * lvid_s )
{
return 1 ;
}
int lv_deactivate ( struct cmd_context * cmd , const char * lvid_s )
{
return 1 ;
}
int lv_activate ( struct cmd_context * cmd , const char * lvid_s )
{
return 1 ;
}
# else /* DEVMAPPER_SUPPORT */
2002-11-18 17:01:16 +03:00
static int _activation = 1 ;
2002-12-20 02:25:55 +03:00
void set_activation ( int act )
2002-11-18 17:01:16 +03:00
{
2002-12-20 02:25:55 +03:00
if ( act = = _activation )
2002-11-18 17:01:16 +03:00
return ;
2002-12-20 02:25:55 +03:00
_activation = act ;
2002-11-18 17:01:16 +03:00
if ( _activation )
log_verbose ( " Activation enabled. Device-mapper kernel "
" driver will be used. " ) ;
else
log_verbose ( " Activation disabled. No device-mapper "
" interaction will be attempted. " ) ;
}
2002-12-20 02:25:55 +03:00
int activation ( void )
2002-11-18 17:01:16 +03:00
{
return _activation ;
}
2002-01-17 19:39:24 +03:00
int library_version ( char * version , size_t size )
{
2002-11-18 17:01:16 +03:00
if ( ! activation ( ) )
return 0 ;
2002-01-17 19:39:24 +03:00
if ( ! dm_get_library_version ( version , size ) )
return 0 ;
return 1 ;
}
int driver_version ( char * version , size_t size )
{
int r = 0 ;
struct dm_task * dmt ;
2002-11-18 17:01:16 +03:00
if ( ! activation ( ) )
return 0 ;
2002-01-17 19:39:24 +03:00
log_very_verbose ( " Getting driver version " ) ;
if ( ! ( dmt = dm_task_create ( DM_DEVICE_VERSION ) ) ) {
stack ;
return 0 ;
}
if ( ! dm_task_run ( dmt ) )
log_error ( " Failed to get driver version " ) ;
if ( ! dm_task_get_driver_version ( dmt , version , size ) )
goto out ;
r = 1 ;
2002-03-19 02:25:50 +03:00
out :
2002-01-17 19:39:24 +03:00
dm_task_destroy ( dmt ) ;
return r ;
}
2002-02-26 14:49:17 +03:00
/*
* Returns 1 if info structure populated , else 0 on failure .
*/
2003-01-09 01:44:07 +03:00
int lv_info ( const struct logical_volume * lv , struct lvinfo * info )
2001-11-07 14:51:42 +03:00
{
2002-02-26 14:49:17 +03:00
int r ;
struct dev_manager * dm ;
2003-01-09 01:44:07 +03:00
struct dm_info dminfo ;
2002-01-11 02:21:07 +03:00
2002-11-18 17:01:16 +03:00
if ( ! activation ( ) )
return 0 ;
2003-04-25 02:09:13 +04:00
if ( ! ( dm = dev_manager_create ( lv - > vg - > name , lv - > vg - > cmd - > cf ) ) ) {
2002-01-21 14:06:32 +03:00
stack ;
2002-02-26 14:49:17 +03:00
return 0 ;
2002-01-21 14:06:32 +03:00
}
2002-01-11 02:21:07 +03:00
2003-01-09 01:44:07 +03:00
if ( ! ( r = dev_manager_info ( dm , lv , & dminfo ) ) )
2002-02-26 14:49:17 +03:00
stack ;
2002-01-11 02:21:07 +03:00
2003-01-09 01:44:07 +03:00
info - > exists = dminfo . exists ;
info - > suspended = dminfo . suspended ;
info - > open_count = dminfo . open_count ;
info - > major = dminfo . major ;
info - > minor = dminfo . minor ;
info - > read_only = dminfo . read_only ;
2002-02-26 14:49:17 +03:00
dev_manager_destroy ( dm ) ;
return r ;
}
2002-02-11 20:42:02 +03:00
2002-05-10 01:17:57 +04:00
/*
* Returns 1 if percent set , else 0 on failure .
*/
2002-05-22 18:03:45 +04:00
int lv_snapshot_percent ( struct logical_volume * lv , float * percent )
2002-05-10 01:17:57 +04:00
{
int r ;
struct dev_manager * dm ;
2002-11-18 17:01:16 +03:00
if ( ! activation ( ) )
return 0 ;
2003-04-25 02:09:13 +04:00
if ( ! ( dm = dev_manager_create ( lv - > vg - > name , lv - > vg - > cmd - > cf ) ) ) {
2002-05-10 01:17:57 +04:00
stack ;
return 0 ;
}
2002-05-22 18:03:45 +04:00
if ( ! ( r = dev_manager_snapshot_percent ( dm , lv , percent ) ) )
2002-05-10 01:17:57 +04:00
stack ;
2002-05-22 18:03:45 +04:00
2002-05-10 01:17:57 +04:00
dev_manager_destroy ( dm ) ;
return r ;
}
2002-03-11 22:02:28 +03:00
static int _lv_active ( struct logical_volume * lv )
2001-11-07 18:02:07 +03:00
{
2003-01-09 01:44:07 +03:00
struct lvinfo info ;
2001-11-07 18:02:07 +03:00
2002-02-26 14:49:17 +03:00
if ( ! lv_info ( lv , & info ) ) {
2001-11-07 18:02:07 +03:00
stack ;
2002-02-26 14:49:17 +03:00
return - 1 ;
2001-11-07 18:02:07 +03:00
}
2002-02-26 14:49:17 +03:00
return info . exists ;
2001-11-07 18:02:07 +03:00
}
2002-03-11 22:02:28 +03:00
static int _lv_open_count ( struct logical_volume * lv )
2002-02-18 18:52:48 +03:00
{
2003-01-09 01:44:07 +03:00
struct lvinfo info ;
2002-02-18 18:52:48 +03:00
2002-02-26 14:49:17 +03:00
if ( ! lv_info ( lv , & info ) ) {
2002-02-18 18:52:48 +03:00
stack ;
2002-02-26 14:49:17 +03:00
return - 1 ;
2002-02-18 18:52:48 +03:00
}
2002-02-26 14:49:17 +03:00
return info . open_count ;
2002-02-18 18:52:48 +03:00
}
2002-03-01 22:08:11 +03:00
/* FIXME Need to detect and handle an lv rename */
2002-03-11 18:08:39 +03:00
static int _lv_activate ( struct logical_volume * lv )
2001-10-09 20:05:34 +04:00
{
2002-02-11 18:48:34 +03:00
int r ;
2002-02-26 14:49:17 +03:00
struct dev_manager * dm ;
2001-10-09 20:05:34 +04:00
2003-04-25 02:09:13 +04:00
if ( ! ( dm = dev_manager_create ( lv - > vg - > name , lv - > vg - > cmd - > cf ) ) ) {
2002-02-11 18:48:34 +03:00
stack ;
return 0 ;
2001-11-02 16:45:05 +03:00
}
2002-03-07 19:48:46 +03:00
if ( ! ( r = dev_manager_activate ( dm , lv ) ) )
2002-02-11 18:48:34 +03:00
stack ;
2001-11-02 16:45:05 +03:00
2002-02-26 14:49:17 +03:00
dev_manager_destroy ( dm ) ;
2001-11-02 16:45:05 +03:00
return r ;
2001-10-09 20:05:34 +04:00
}
2001-10-16 20:25:28 +04:00
2002-03-11 18:08:39 +03:00
static int _lv_deactivate ( struct logical_volume * lv )
2001-10-31 20:59:52 +03:00
{
2002-02-26 14:49:17 +03:00
int r ;
struct dev_manager * dm ;
2001-11-07 14:51:42 +03:00
2003-04-25 02:09:13 +04:00
if ( ! ( dm = dev_manager_create ( lv - > vg - > name , lv - > vg - > cmd - > cf ) ) ) {
2002-02-11 18:48:34 +03:00
stack ;
return 0 ;
}
2001-11-07 14:51:42 +03:00
2002-02-26 14:49:17 +03:00
if ( ! ( r = dev_manager_deactivate ( dm , lv ) ) )
2001-11-07 14:51:42 +03:00
stack ;
2002-02-26 14:49:17 +03:00
dev_manager_destroy ( dm ) ;
return r ;
2001-11-07 14:51:42 +03:00
}
2002-03-11 18:08:39 +03:00
static int _lv_suspend ( struct logical_volume * lv )
2002-01-11 02:21:07 +03:00
{
2002-03-14 18:36:07 +03:00
int r ;
struct dev_manager * dm ;
2001-11-28 21:03:11 +03:00
2003-04-25 02:09:13 +04:00
if ( ! ( dm = dev_manager_create ( lv - > vg - > name , lv - > vg - > cmd - > cf ) ) ) {
2001-11-07 14:51:42 +03:00
stack ;
return 0 ;
}
2001-10-31 20:59:52 +03:00
2002-03-14 18:36:07 +03:00
if ( ! ( r = dev_manager_suspend ( dm , lv ) ) )
2001-11-07 14:51:42 +03:00
stack ;
2001-10-31 20:59:52 +03:00
2002-03-14 18:36:07 +03:00
dev_manager_destroy ( dm ) ;
return r ;
2002-02-11 18:48:34 +03:00
}
2002-01-11 02:21:07 +03:00
2002-03-01 22:08:11 +03:00
/*
* These two functions return the number of LVs in the state ,
* or - 1 on error .
*/
2001-11-02 19:28:04 +03:00
int lvs_in_vg_activated ( struct volume_group * vg )
{
2001-11-07 14:51:42 +03:00
struct list * lvh ;
struct logical_volume * lv ;
2001-11-08 19:15:58 +03:00
int count = 0 ;
2001-11-07 14:51:42 +03:00
2002-11-18 17:01:16 +03:00
if ( ! activation ( ) )
return 0 ;
2001-11-07 14:51:42 +03:00
list_iterate ( lvh , & vg - > lvs ) {
2002-01-21 19:49:32 +03:00
lv = list_item ( lvh , struct lv_list ) - > lv ;
2002-03-11 22:02:28 +03:00
count + = ( _lv_active ( lv ) = = 1 ) ;
2001-11-07 14:51:42 +03:00
}
return count ;
2001-11-02 19:28:04 +03:00
}
2001-11-07 18:02:07 +03:00
int lvs_in_vg_opened ( struct volume_group * vg )
{
struct list * lvh ;
struct logical_volume * lv ;
2001-11-08 19:15:58 +03:00
int count = 0 ;
2001-11-07 18:02:07 +03:00
2002-11-18 17:01:16 +03:00
if ( ! activation ( ) )
return 0 ;
2001-11-07 18:02:07 +03:00
list_iterate ( lvh , & vg - > lvs ) {
2002-01-21 19:49:32 +03:00
lv = list_item ( lvh , struct lv_list ) - > lv ;
2002-03-11 22:02:28 +03:00
count + = ( _lv_open_count ( lv ) = = 1 ) ;
2001-11-07 18:02:07 +03:00
}
return count ;
}
2002-02-25 15:56:16 +03:00
2002-03-14 18:36:07 +03:00
/* These return success if the device is not active */
2002-03-05 23:03:09 +03:00
int lv_suspend_if_active ( struct cmd_context * cmd , const char * lvid_s )
2002-02-25 15:56:16 +03:00
{
struct logical_volume * lv ;
2003-01-09 01:44:07 +03:00
struct lvinfo info ;
2002-02-25 15:56:16 +03:00
2002-11-18 17:01:16 +03:00
if ( ! activation ( ) )
return 1 ;
if ( ! ( lv = lv_from_lvid ( cmd , lvid_s ) ) )
2002-02-25 15:56:16 +03:00
return 0 ;
2002-11-18 17:01:16 +03:00
if ( ! activation ( ) )
return 1 ;
2002-03-14 18:36:07 +03:00
if ( test_mode ( ) ) {
_skip ( " Suspending '%s'. " , lv - > name ) ;
2003-01-17 23:16:23 +03:00
return 1 ;
2002-03-14 18:36:07 +03:00
}
2002-03-19 02:25:50 +03:00
if ( ! lv_info ( lv , & info ) ) {
stack ;
return 0 ;
}
2002-03-11 22:02:28 +03:00
if ( info . exists & & ! info . suspended )
2002-04-11 13:14:04 +04:00
return _lv_suspend ( lv ) ;
2002-03-01 22:08:11 +03:00
2002-02-25 15:56:16 +03:00
return 1 ;
}
2002-03-05 23:03:09 +03:00
int lv_resume_if_active ( struct cmd_context * cmd , const char * lvid_s )
2002-02-25 15:56:16 +03:00
{
struct logical_volume * lv ;
2003-01-09 01:44:07 +03:00
struct lvinfo info ;
2002-02-25 15:56:16 +03:00
2002-11-18 17:01:16 +03:00
if ( ! activation ( ) )
return 1 ;
if ( ! ( lv = lv_from_lvid ( cmd , lvid_s ) ) )
2002-02-25 15:56:16 +03:00
return 0 ;
2002-03-14 18:36:07 +03:00
if ( test_mode ( ) ) {
_skip ( " Resuming '%s'. " , lv - > name ) ;
2003-01-17 23:16:23 +03:00
return 1 ;
2002-03-14 18:36:07 +03:00
}
2002-03-19 02:25:50 +03:00
if ( ! lv_info ( lv , & info ) ) {
stack ;
return 0 ;
}
2002-03-11 22:02:28 +03:00
if ( info . exists & & info . suspended )
2002-04-11 13:14:04 +04:00
return _lv_activate ( lv ) ;
2002-02-25 15:56:16 +03:00
return 1 ;
}
2002-03-11 18:08:39 +03:00
int lv_deactivate ( struct cmd_context * cmd , const char * lvid_s )
2002-02-27 15:26:41 +03:00
{
struct logical_volume * lv ;
2003-01-09 01:44:07 +03:00
struct lvinfo info ;
2002-02-27 15:26:41 +03:00
2002-11-18 17:01:16 +03:00
if ( ! activation ( ) )
return 1 ;
if ( ! ( lv = lv_from_lvid ( cmd , lvid_s ) ) )
2002-02-27 15:26:41 +03:00
return 0 ;
2002-03-14 18:36:07 +03:00
if ( test_mode ( ) ) {
_skip ( " Deactivating '%s'. " , lv - > name ) ;
2003-01-17 23:16:23 +03:00
return 1 ;
2002-03-14 18:36:07 +03:00
}
2002-03-19 02:25:50 +03:00
if ( ! lv_info ( lv , & info ) ) {
stack ;
return 0 ;
}
2002-03-11 22:02:28 +03:00
if ( info . exists )
2002-04-11 13:14:04 +04:00
return _lv_deactivate ( lv ) ;
2002-02-27 15:26:41 +03:00
return 1 ;
}
2002-03-11 18:08:39 +03:00
int lv_activate ( struct cmd_context * cmd , const char * lvid_s )
2002-02-27 15:26:41 +03:00
{
struct logical_volume * lv ;
2003-01-09 01:44:07 +03:00
struct lvinfo info ;
2002-02-27 15:26:41 +03:00
2002-11-18 17:01:16 +03:00
if ( ! activation ( ) )
return 1 ;
if ( ! ( lv = lv_from_lvid ( cmd , lvid_s ) ) )
2002-02-27 15:26:41 +03:00
return 0 ;
2002-03-14 18:36:07 +03:00
if ( test_mode ( ) ) {
_skip ( " Activating '%s'. " , lv - > name ) ;
2003-01-17 23:16:23 +03:00
return 1 ;
2002-03-14 18:36:07 +03:00
}
2002-03-19 02:25:50 +03:00
if ( ! lv_info ( lv , & info ) ) {
stack ;
return 0 ;
}
2002-03-01 22:08:11 +03:00
2002-03-11 22:02:28 +03:00
if ( ! info . exists | | info . suspended )
2002-04-11 13:14:04 +04:00
return _lv_activate ( lv ) ;
2002-02-27 15:26:41 +03:00
return 1 ;
}
2003-01-09 01:44:07 +03:00
# endif