2008-01-09 18:32:19 +03:00
/*
2010-05-11 12:32:22 +04:00
* Copyright ( C ) 2007 - 2010 Red Hat , Inc . All rights reserved .
2008-01-09 18:32:19 +03: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 Lesser General Public License v .2 .1 .
*
* You should have received a copy of the GNU Lesser 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
*/
2010-01-22 03:18:37 +03:00
# include "lib.h"
2010-01-22 01:15:45 +03:00
2008-01-09 18:32:19 +03:00
# include "lvm2cmd.h"
2010-01-22 01:15:45 +03:00
# include "errors.h"
# include "libdevmapper-event.h"
# include "dmeventd_lvm.h"
2008-01-09 18:32:19 +03:00
# include "lvm-string.h"
# include <syslog.h> /* FIXME Replace syslog with multilog */
/* FIXME Missing openlog? */
/* First warning when snapshot is 80% full. */
# define WARNING_THRESH 80
/* Further warnings at 85%, 90% and 95% fullness. */
# define WARNING_STEP 5
struct snap_status {
int invalid ;
int used ;
int max ;
} ;
/* FIXME possibly reconcile this with target_percent when we gain
access to regular LVM library here . */
2010-02-15 15:55:20 +03:00
static void _parse_snapshot_params ( char * params , struct snap_status * status )
2008-01-09 18:32:19 +03:00
{
char * p ;
/*
* xx / xx - - fractions used / max
* Invalid - - snapshot invalidated
* Unknown - - status unknown
*/
2010-02-15 15:55:20 +03:00
status - > used = status - > max = 0 ;
2008-01-09 18:32:19 +03:00
if ( ! strncmp ( params , " Invalid " , 7 ) ) {
2010-02-15 15:55:20 +03:00
status - > invalid = 1 ;
2008-01-09 18:32:19 +03:00
return ;
}
/*
* When we return without setting non - zero max , the parent is
* responsible for reporting errors .
*/
if ( ! strncmp ( params , " Unknown " , 7 ) )
return ;
if ( ! ( p = strstr ( params , " / " ) ) )
return ;
* p = ' \0 ' ;
p + + ;
2010-02-15 15:55:20 +03:00
status - > used = atoi ( params ) ;
status - > max = atoi ( p ) ;
2008-01-09 18:32:19 +03:00
}
2008-01-31 15:19:36 +03:00
void process_event ( struct dm_task * dmt ,
2010-07-09 19:34:40 +04:00
enum dm_event_mask event __attribute__ ( ( unused ) ) ,
2008-01-09 18:32:19 +03:00
void * * private )
{
void * next = NULL ;
uint64_t start , length ;
char * target_type = NULL ;
char * params ;
2010-02-15 15:55:20 +03:00
struct snap_status status = { 0 } ;
2008-01-09 18:32:19 +03:00
const char * device = dm_task_get_name ( dmt ) ;
int percent , * percent_warning = ( int * ) private ;
/* No longer monitoring, waiting for remove */
if ( ! * percent_warning )
return ;
2010-01-22 01:15:45 +03:00
dmeventd_lvm2_lock ( ) ;
2008-01-09 18:32:19 +03:00
dm_get_next_target ( dmt , next , & start , & length , & target_type , & params ) ;
if ( ! target_type )
goto out ;
2010-02-15 15:55:20 +03:00
_parse_snapshot_params ( params , & status ) ;
2008-01-09 18:32:19 +03:00
/*
* If the snapshot has been invalidated or we failed to parse
* the status string . Report the full status string to syslog .
*/
2010-02-15 15:55:20 +03:00
if ( status . invalid | | ! status . max ) {
2008-01-09 18:32:19 +03:00
syslog ( LOG_ERR , " Snapshot %s changed state to: %s \n " , device , params ) ;
* percent_warning = 0 ;
goto out ;
}
2010-02-15 15:55:20 +03:00
percent = 100 * status . used / status . max ;
2008-01-09 18:32:19 +03:00
if ( percent > = * percent_warning ) {
syslog ( LOG_WARNING , " Snapshot %s is now %i%% full. \n " , device , percent ) ;
/* Print warning on the next multiple of WARNING_STEP. */
* percent_warning = ( percent / WARNING_STEP ) * WARNING_STEP + WARNING_STEP ;
}
out :
2010-01-22 01:15:45 +03:00
dmeventd_lvm2_unlock ( ) ;
2008-01-09 18:32:19 +03:00
}
2008-01-31 15:19:36 +03:00
int register_device ( const char * device ,
2010-07-09 19:34:40 +04:00
const char * uuid __attribute__ ( ( unused ) ) ,
int major __attribute__ ( ( unused ) ) ,
int minor __attribute__ ( ( unused ) ) ,
2008-01-31 15:19:36 +03:00
void * * private )
2008-01-09 18:32:19 +03:00
{
int * percent_warning = ( int * ) private ;
2010-01-22 15:48:58 +03:00
int r = dmeventd_lvm2_init ( ) ;
2008-01-09 18:32:19 +03:00
* percent_warning = WARNING_THRESH ; /* Print warning if snapshot is full */
syslog ( LOG_INFO , " Monitoring snapshot %s \n " , device ) ;
2010-01-22 15:48:58 +03:00
return r ;
2008-01-09 18:32:19 +03:00
}
2008-01-31 15:19:36 +03:00
int unregister_device ( const char * device ,
2010-07-09 19:34:40 +04:00
const char * uuid __attribute__ ( ( unused ) ) ,
int major __attribute__ ( ( unused ) ) ,
int minor __attribute__ ( ( unused ) ) ,
void * * unused __attribute__ ( ( unused ) ) )
2008-01-09 18:32:19 +03:00
{
syslog ( LOG_INFO , " No longer monitoring snapshot %s \n " ,
device ) ;
2010-01-22 01:15:45 +03:00
dmeventd_lvm2_exit ( ) ;
2008-01-09 18:32:19 +03:00
return 1 ;
}