2004-06-24 12:02:38 +04:00
/*
* Copyright ( C ) 2002 - 2004 Sistina Software , Inc . All rights reserved .
2011-09-25 23:37:00 +04:00
* Copyright ( C ) 2004 - 2011 Red Hat , Inc . All rights reserved .
2004-06-24 12:02:38 +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 .
*
* 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
*/
/*
CLVMD Cluster LVM daemon command processor .
To add commands to the daemon simply add a processor in do_command and return
and messages back in buf and the length in * retlen . The initial value of
buflen is the maximum size of the buffer . if buf is not large enough then it
may be reallocated by the functions in here to a suitable size bearing in
mind that anything larger than the passed - in size will have to be returned
using the system LV and so performance will suffer .
The status return will be negated and passed back to the originating node .
pre - and post - command routines are called only on the local node . The
purpose is primarily to get and release locks , though the pre - routine should
also do any other local setups required by the command ( if any ) and can
return a failure code that prevents the command from being distributed around
the cluster
The pre - and post - routines are run in their own thread so can block as long
they like , do_command is run in the main clvmd thread so should not block for
too long . If the pre - command returns an error code ( ! = 0 ) then the command
will not be propogated around the cluster but the post - command WILL be called
Also note that the pre and post routine are * always * called on the local
node , even if the command to be executed was only requested to run on a
remote node . It may peek inside the client structure to check the status of
the command .
The clients of the daemon must , naturally , understand the return messages and
codes .
Routines in here may only READ the values in the client structure passed in
apart from client - > private which they are free to do what they like with .
*/
2010-06-21 19:56:57 +04:00
# include "clvmd-common.h"
2004-06-24 12:02:38 +04:00
# include "clvmd-comms.h"
# include "clvm.h"
# include "clvmd.h"
2011-06-03 13:05:30 +04:00
# include "lvm-globals.h"
2010-06-21 19:56:57 +04:00
# include "lvm-functions.h"
# include "locking.h"
# include <sys/utsname.h>
2004-06-24 12:02:38 +04:00
2006-10-09 18:11:57 +04:00
extern struct cluster_ops * clops ;
2010-04-20 18:07:37 +04:00
static int restart_clvmd ( void ) ;
2006-10-09 18:11:57 +04:00
2004-06-24 12:02:38 +04:00
/* This is where all the real work happens:
NOTE : client will be NULL when this is executed on a remote node */
int do_command ( struct local_client * client , struct clvm_header * msg , int msglen ,
char * * buf , int buflen , int * retlen )
{
char * args = msg - > node + strlen ( msg - > node ) + 1 ;
int arglen = msglen - sizeof ( struct clvm_header ) - strlen ( msg - > node ) ;
int status = 0 ;
char * lockname ;
2009-05-19 14:38:58 +04:00
const char * locktype ;
2004-06-24 12:02:38 +04:00
struct utsname nodeinfo ;
unsigned char lock_cmd ;
unsigned char lock_flags ;
2011-06-02 01:16:55 +04:00
/* Reset test mode before we start */
init_test ( 0 ) ;
2004-06-24 12:02:38 +04:00
/* Do the command */
switch ( msg - > cmd ) {
/* Just a test message */
case CLVMD_CMD_TEST :
if ( arglen > buflen ) {
2007-05-02 16:22:40 +04:00
char * new_buf ;
2004-06-24 12:02:38 +04:00
buflen = arglen + 200 ;
2007-05-02 16:22:40 +04:00
new_buf = realloc ( * buf , buflen ) ;
if ( new_buf = = NULL ) {
status = errno ;
free ( * buf ) ;
}
* buf = new_buf ;
}
if ( * buf ) {
2012-01-26 02:17:57 +04:00
if ( uname ( & nodeinfo ) )
memset ( & nodeinfo , 0 , sizeof ( nodeinfo ) ) ;
2010-12-01 01:16:25 +03:00
* retlen = 1 + dm_snprintf ( * buf , buflen ,
" TEST from %s: %s v%s " ,
nodeinfo . nodename , args ,
nodeinfo . release ) ;
2004-06-24 12:02:38 +04:00
}
break ;
case CLVMD_CMD_LOCK_VG :
2010-01-05 19:05:12 +03:00
lock_cmd = args [ 0 ] ;
lock_flags = args [ 1 ] ;
2007-08-23 19:43:20 +04:00
lockname = & args [ 2 ] ;
2004-06-24 12:02:38 +04:00
/* Check to see if the VG is in use by LVM1 */
2007-08-23 19:43:20 +04:00
status = do_check_lvm1 ( lockname ) ;
2011-06-02 01:16:55 +04:00
if ( lock_flags & LCK_TEST_MODE )
init_test ( 1 ) ;
2010-01-05 19:05:12 +03:00
do_lock_vg ( lock_cmd , lock_flags , lockname ) ;
2004-06-24 12:02:38 +04:00
break ;
case CLVMD_CMD_LOCK_LV :
/* This is the biggie */
2011-12-08 22:32:33 +04:00
lock_cmd = args [ 0 ] ;
2004-06-24 12:02:38 +04:00
lock_flags = args [ 1 ] ;
lockname = & args [ 2 ] ;
2011-06-02 01:16:55 +04:00
if ( lock_flags & LCK_TEST_MODE )
init_test ( 1 ) ;
2004-06-24 12:02:38 +04:00
status = do_lock_lv ( lock_cmd , lock_flags , lockname ) ;
/* Replace EIO with something less scary */
if ( status = = EIO ) {
2010-12-01 01:16:25 +03:00
* retlen = 1 + dm_snprintf ( * buf , buflen , " %s " ,
get_last_lvm_error ( ) ) ;
2004-06-24 12:02:38 +04:00
return EIO ;
}
break ;
2009-05-19 14:38:58 +04:00
case CLVMD_CMD_LOCK_QUERY :
lockname = & args [ 2 ] ;
if ( buflen < 3 )
return EIO ;
if ( ( locktype = do_lock_query ( lockname ) ) )
2010-12-01 01:16:25 +03:00
* retlen = 1 + dm_snprintf ( * buf , buflen , " %s " , locktype ) ;
2009-05-19 14:38:58 +04:00
break ;
2006-10-04 12:22:16 +04:00
case CLVMD_CMD_REFRESH :
do_refresh_cache ( ) ;
break ;
2011-01-12 23:42:50 +03:00
case CLVMD_CMD_SYNC_NAMES :
lvm_do_fs_unlock ( ) ;
break ;
2007-08-17 15:51:23 +04:00
case CLVMD_CMD_SET_DEBUG :
2012-03-02 01:14:43 +04:00
clvmd_set_debug ( ( debug_t ) args [ 0 ] ) ;
2007-08-17 15:51:23 +04:00
break ;
2010-04-20 18:07:37 +04:00
case CLVMD_CMD_RESTART :
2011-01-20 02:09:31 +03:00
status = restart_clvmd ( ) ;
2010-04-20 18:07:37 +04:00
break ;
2006-10-09 18:11:57 +04:00
case CLVMD_CMD_GET_CLUSTERNAME :
status = clops - > get_cluster_name ( * buf , buflen ) ;
if ( ! status )
2007-05-02 16:22:40 +04:00
* retlen = strlen ( * buf ) + 1 ;
2006-10-09 18:11:57 +04:00
break ;
2007-12-04 18:39:26 +03:00
case CLVMD_CMD_VG_BACKUP :
2009-04-22 13:39:45 +04:00
/*
* Do not run backup on local node , caller should do that .
*/
if ( ! client )
lvm_do_backup ( & args [ 2 ] ) ;
2007-12-04 18:39:26 +03:00
break ;
2004-06-24 12:02:38 +04:00
default :
/* Won't get here because command is validated in pre_command */
break ;
}
/* Check the status of the command and return the error text */
if ( status ) {
2010-12-17 15:37:49 +03:00
* retlen = 1 + ( ( * buf ) ? dm_snprintf ( * buf , buflen , " %s " ,
strerror ( status ) ) : - 1 ) ;
2004-06-24 12:02:38 +04:00
}
return status ;
}
2004-09-24 13:39:57 +04:00
static int lock_vg ( struct local_client * client )
{
2014-03-07 23:24:46 +04:00
struct dm_hash_table * lock_hash ;
struct clvm_header * header =
( struct clvm_header * ) client - > bits . localsock . cmd ;
unsigned char lock_cmd ;
int lock_mode ;
char * args = header - > node + strlen ( header - > node ) + 1 ;
int lkid ;
int status ;
char * lockname ;
/*
* Keep a track of VG locks in our own hash table . In current
* practice there should only ever be more than two VGs locked
* if a user tries to merge lots of them at once
*/
if ( ! client - > bits . localsock . private ) {
if ( ! ( lock_hash = dm_hash_create ( 3 ) ) )
return ENOMEM ;
client - > bits . localsock . private = ( void * ) lock_hash ;
} else
lock_hash = ( struct dm_hash_table * ) client - > bits . localsock . private ;
lock_cmd = args [ 0 ] & ( LCK_NONBLOCK | LCK_HOLD | LCK_SCOPE_MASK | LCK_TYPE_MASK ) ;
lock_mode = ( ( int ) lock_cmd & LCK_TYPE_MASK ) ;
/* lock_flags = args[1]; */
lockname = & args [ 2 ] ;
DEBUGLOG ( " doing PRE command LOCK_VG '%s' at %x (client=%p) \n " , lockname , lock_cmd , client ) ;
if ( lock_mode = = LCK_UNLOCK ) {
if ( ! ( lkid = ( int ) ( long ) dm_hash_lookup ( lock_hash , lockname ) ) )
return EINVAL ;
if ( ( status = sync_unlock ( lockname , lkid ) ) )
status = errno ;
else
dm_hash_remove ( lock_hash , lockname ) ;
} else {
/* Read locks need to be PR; other modes get passed through */
if ( lock_mode = = LCK_READ )
lock_mode = LCK_PREAD ;
if ( ( status = sync_lock ( lockname , lock_mode , ( lock_cmd & LCK_NONBLOCK ) ? LCKF_NOQUEUE : 0 , & lkid ) ) )
status = errno ;
else if ( ! dm_hash_insert ( lock_hash , lockname , ( void * ) ( long ) lkid ) )
return ENOMEM ;
}
return status ;
2004-09-24 13:39:57 +04:00
}
2004-06-24 12:02:38 +04:00
/* Pre-command is a good place to get locks that are needed only for the duration
of the commands around the cluster ( don ' t forget to free them in post - command ) ,
and to sanity check the command arguments */
int do_pre_command ( struct local_client * client )
{
struct clvm_header * header =
( struct clvm_header * ) client - > bits . localsock . cmd ;
unsigned char lock_cmd ;
unsigned char lock_flags ;
char * args = header - > node + strlen ( header - > node ) + 1 ;
2012-01-26 02:20:11 +04:00
int lockid = 0 ;
2004-06-24 12:02:38 +04:00
int status = 0 ;
char * lockname ;
2011-06-02 01:16:55 +04:00
init_test ( 0 ) ;
2004-06-24 12:02:38 +04:00
switch ( header - > cmd ) {
case CLVMD_CMD_TEST :
2010-06-17 16:48:54 +04:00
status = sync_lock ( " CLVMD_TEST " , LCK_EXCL , 0 , & lockid ) ;
2006-12-02 02:10:26 +03:00
client - > bits . localsock . private = ( void * ) ( long ) lockid ;
2004-06-24 12:02:38 +04:00
break ;
case CLVMD_CMD_LOCK_VG :
2008-05-09 19:13:20 +04:00
lockname = & args [ 2 ] ;
2008-05-09 22:45:15 +04:00
/* We take out a real lock unless LCK_CACHE was set */
2008-05-09 19:13:20 +04:00
if ( ! strncmp ( lockname , " V_ " , 2 ) | |
2008-05-09 22:45:15 +04:00
! strncmp ( lockname , " P_# " , 3 ) )
2008-05-09 19:13:20 +04:00
status = lock_vg ( client ) ;
2004-06-24 12:02:38 +04:00
break ;
case CLVMD_CMD_LOCK_LV :
lock_cmd = args [ 0 ] ;
lock_flags = args [ 1 ] ;
lockname = & args [ 2 ] ;
2011-06-02 01:16:55 +04:00
if ( lock_flags & LCK_TEST_MODE )
init_test ( 1 ) ;
2004-06-24 12:02:38 +04:00
status = pre_lock_lv ( lock_cmd , lock_flags , lockname ) ;
break ;
2006-10-04 12:22:16 +04:00
case CLVMD_CMD_REFRESH :
2006-10-09 18:11:57 +04:00
case CLVMD_CMD_GET_CLUSTERNAME :
2007-08-17 15:51:23 +04:00
case CLVMD_CMD_SET_DEBUG :
2007-12-04 18:39:26 +03:00
case CLVMD_CMD_VG_BACKUP :
2011-01-12 23:42:50 +03:00
case CLVMD_CMD_SYNC_NAMES :
2009-05-19 14:38:58 +04:00
case CLVMD_CMD_LOCK_QUERY :
2010-04-20 18:07:37 +04:00
case CLVMD_CMD_RESTART :
2006-10-04 12:22:16 +04:00
break ;
2004-06-24 12:02:38 +04:00
default :
log_error ( " Unknown command %d received \n " , header - > cmd ) ;
status = EINVAL ;
}
return status ;
}
/* Note that the post-command routine is called even if the pre-command or the real command
failed */
int do_post_command ( struct local_client * client )
{
struct clvm_header * header =
( struct clvm_header * ) client - > bits . localsock . cmd ;
int status = 0 ;
unsigned char lock_cmd ;
unsigned char lock_flags ;
char * args = header - > node + strlen ( header - > node ) + 1 ;
char * lockname ;
2011-06-02 01:16:55 +04:00
init_test ( 0 ) ;
2004-06-24 12:02:38 +04:00
switch ( header - > cmd ) {
case CLVMD_CMD_TEST :
2014-03-10 15:16:09 +04:00
status = sync_unlock ( " CLVMD_TEST " , ( int ) ( long ) client - > bits . localsock . private ) ;
client - > bits . localsock . private = NULL ;
2004-06-24 12:02:38 +04:00
break ;
case CLVMD_CMD_LOCK_LV :
lock_cmd = args [ 0 ] ;
lock_flags = args [ 1 ] ;
lockname = & args [ 2 ] ;
2011-06-02 01:16:55 +04:00
if ( lock_flags & LCK_TEST_MODE )
init_test ( 1 ) ;
2004-06-24 12:02:38 +04:00
status = post_lock_lv ( lock_cmd , lock_flags , lockname ) ;
break ;
2011-01-18 02:13:14 +03:00
default :
/* Nothing to do here */
break ;
2004-06-24 12:02:38 +04:00
}
return status ;
}
2004-09-24 13:39:57 +04:00
/* Called when the client is about to be deleted */
void cmd_client_cleanup ( struct local_client * client )
{
2005-11-09 12:24:10 +03:00
struct dm_hash_node * v ;
2012-02-24 02:23:12 +04:00
struct dm_hash_table * lock_hash ;
int lkid ;
char * lockname ;
2004-09-24 13:39:57 +04:00
2012-02-24 02:23:12 +04:00
if ( ! client - > bits . localsock . private )
return ;
2004-09-24 13:39:57 +04:00
2012-02-24 02:23:12 +04:00
lock_hash = ( struct dm_hash_table * ) client - > bits . localsock . private ;
dm_hash_iterate ( v , lock_hash ) {
lkid = ( int ) ( long ) dm_hash_get_data ( lock_hash , v ) ;
lockname = dm_hash_get_key ( lock_hash , v ) ;
2005-03-07 20:03:44 +03:00
DEBUGLOG ( " cleanup: Unlocking lock %s %x \n " , lockname , lkid ) ;
2012-02-27 14:02:17 +04:00
( void ) sync_unlock ( lockname , lkid ) ;
2004-09-24 13:39:57 +04:00
}
2005-11-09 12:24:10 +03:00
dm_hash_destroy ( lock_hash ) ;
2014-03-10 15:16:09 +04:00
client - > bits . localsock . private = NULL ;
2004-09-24 13:39:57 +04:00
}
2010-04-20 18:07:37 +04:00
static int restart_clvmd ( void )
{
2011-09-22 13:47:34 +04:00
const char * * argv ;
char * lv_name ;
int argc = 0 , max_locks = 0 ;
2010-04-20 18:07:37 +04:00
struct dm_hash_node * hn = NULL ;
2011-09-22 13:47:34 +04:00
char debug_arg [ 16 ] ;
2011-09-25 00:50:35 +04:00
const char * clvmd = getenv ( " LVM_CLVMD_BINARY " ) ? : CLVMD_PATH ;
2010-04-20 18:07:37 +04:00
DEBUGLOG ( " clvmd restart requested \n " ) ;
2010-06-04 16:59:30 +04:00
/* Count exclusively-open LVs */
do {
hn = get_next_excl_lock ( hn , & lv_name ) ;
2011-09-22 13:47:34 +04:00
if ( lv_name ) {
2010-06-04 16:59:30 +04:00
max_locks + + ;
2011-09-22 13:47:34 +04:00
if ( ! * lv_name )
break ; /* FIXME: Is this error ? */
}
} while ( hn ) ;
2010-06-04 16:59:30 +04:00
/* clvmd + locks (-E uuid) + debug (-d X) + NULL */
2013-04-21 00:23:36 +04:00
if ( ! ( argv = malloc ( ( max_locks * 2 + 6 ) * sizeof ( * argv ) ) ) )
2010-06-04 16:59:30 +04:00
goto_out ;
2010-04-20 18:07:37 +04:00
/*
* Build the command - line
*/
2011-09-22 13:47:34 +04:00
argv [ argc + + ] = " clvmd " ;
2010-04-20 18:07:37 +04:00
2013-04-21 00:23:36 +04:00
/* Propagate debug options */
2011-01-18 02:13:14 +03:00
if ( clvmd_get_debug ( ) ) {
2011-09-22 13:47:34 +04:00
if ( dm_snprintf ( debug_arg , sizeof ( debug_arg ) , " -d%u " , clvmd_get_debug ( ) ) < 0 )
2010-06-04 16:59:30 +04:00
goto_out ;
argv [ argc + + ] = debug_arg ;
2010-04-20 18:07:37 +04:00
}
2013-04-21 00:23:36 +04:00
/* Propagate foreground options */
if ( clvmd_get_foreground ( ) )
argv [ argc + + ] = " -f " ;
2011-09-25 23:37:00 +04:00
argv [ argc + + ] = " -I " ;
argv [ argc + + ] = clops - > name ;
2011-01-20 02:09:31 +03:00
2010-04-20 18:07:37 +04:00
/* Now add the exclusively-open LVs */
2011-09-22 13:45:24 +04:00
hn = NULL ;
2010-04-20 18:07:37 +04:00
do {
hn = get_next_excl_lock ( hn , & lv_name ) ;
if ( lv_name ) {
2011-09-22 13:47:34 +04:00
if ( ! * lv_name )
break ; /* FIXME: Is this error ? */
argv [ argc + + ] = " -E " ;
argv [ argc + + ] = lv_name ;
2010-04-20 18:07:37 +04:00
DEBUGLOG ( " excl lock: %s \n " , lv_name ) ;
}
2011-09-22 13:47:34 +04:00
} while ( hn ) ;
argv [ argc ] = NULL ;
2010-04-20 18:07:37 +04:00
/* Exec new clvmd */
2011-09-25 00:50:35 +04:00
DEBUGLOG ( " --- Restarting %s --- \n " , clvmd ) ;
2011-09-22 13:47:34 +04:00
for ( argc = 1 ; argv [ argc ] ; argc + + ) DEBUGLOG ( " --- %d: %s \n " , argc , argv [ argc ] ) ;
2010-04-20 18:07:37 +04:00
/* NOTE: This will fail when downgrading! */
2011-09-26 11:51:23 +04:00
execvp ( clvmd , ( char * * ) argv ) ;
2010-06-04 16:59:30 +04:00
out :
2010-04-20 18:07:37 +04:00
/* We failed */
2010-06-04 16:59:30 +04:00
DEBUGLOG ( " Restart of clvmd failed. \n " ) ;
free ( argv ) ;
2011-01-20 02:09:31 +03:00
return EIO ;
2010-04-20 18:07:37 +04:00
}