Added the flexnbd break
command to stop mirroring
This commit is contained in:
@@ -428,6 +428,51 @@ int control_acl(struct control_client* client, int linesc, char** lines)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int control_break(
|
||||
struct control_client* client,
|
||||
int linesc __attribute__ ((unused)),
|
||||
char** lines __attribute__((unused))
|
||||
)
|
||||
{
|
||||
NULLCHECK( client );
|
||||
NULLCHECK( client->flexnbd );
|
||||
|
||||
int result = 0;
|
||||
struct flexnbd* flexnbd = client->flexnbd;
|
||||
|
||||
flexnbd_lock_switch( flexnbd );
|
||||
{
|
||||
struct server * serve = flexnbd_server( flexnbd );
|
||||
if ( server_is_mirroring( serve ) ) {
|
||||
|
||||
info( "Signaling to abandon mirror" );
|
||||
server_abandon_mirror( serve );
|
||||
debug( "Abandon signaled" );
|
||||
|
||||
if ( server_is_closed( serve ) ) {
|
||||
info( "Mirror completed while canceling" );
|
||||
write( client->socket,
|
||||
"1: mirror completed\n", 20 );
|
||||
}
|
||||
else {
|
||||
info( "Mirror successfully stopped." );
|
||||
write( client->socket,
|
||||
"0: mirror stopped\n", 18 );
|
||||
result = 1;
|
||||
}
|
||||
|
||||
} else {
|
||||
warn( "Not mirroring." );
|
||||
write( client->socket, "1: not mirroring\n", 17 );
|
||||
}
|
||||
}
|
||||
flexnbd_unlock_switch( flexnbd );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/** FIXME: add some useful statistics */
|
||||
int control_status(
|
||||
struct control_client* client,
|
||||
@@ -486,6 +531,12 @@ void control_respond(struct control_client * client)
|
||||
debug("mirror command failed");
|
||||
}
|
||||
}
|
||||
else if (strcmp(lines[0], "break") == 0) {
|
||||
info( "break command received" );
|
||||
if ( control_break( client, linesc-1, lines+1) < 0) {
|
||||
debug( "break command failed" );
|
||||
}
|
||||
}
|
||||
else if (strcmp(lines[0], "status") == 0) {
|
||||
info("status command received" );
|
||||
if (control_status(client, linesc-1, lines+1) < 0) {
|
||||
|
63
src/mode.c
63
src/mode.c
@@ -140,6 +140,22 @@ static char mirror_help_text[] =
|
||||
VERBOSE_LINE
|
||||
QUIET_LINE;
|
||||
|
||||
static struct option break_options[] = {
|
||||
GETOPT_HELP,
|
||||
GETOPT_SOCK,
|
||||
GETOPT_QUIET,
|
||||
GETOPT_VERBOSE,
|
||||
{0}
|
||||
};
|
||||
static char break_short_options[] = "hs:" SOPT_QUIET SOPT_VERBOSE;
|
||||
static char break_help_text[] =
|
||||
"Usage: flexnbd " CMD_BREAK " <options>\n\n"
|
||||
"Stop mirroring from the server with control socket SOCK.\n\n"
|
||||
HELP_LINE
|
||||
SOCK_LINE
|
||||
VERBOSE_LINE
|
||||
QUIET_LINE;
|
||||
|
||||
|
||||
static struct option status_options[] = {
|
||||
GETOPT_HELP,
|
||||
@@ -355,6 +371,29 @@ void read_mirror_param( int c, char **sock, char **ip_addr, char **ip_port, char
|
||||
}
|
||||
}
|
||||
|
||||
void read_break_param( int c, char **sock )
|
||||
{
|
||||
switch( c ) {
|
||||
case 'h':
|
||||
fprintf( stdout, "%s\n", break_help_text );
|
||||
exit( 0 );
|
||||
break;
|
||||
case 's':
|
||||
*sock = optarg;
|
||||
break;
|
||||
case 'q':
|
||||
log_level = 4;
|
||||
break;
|
||||
case 'v':
|
||||
log_level = VERBOSE_LOG_LEVEL;
|
||||
break;
|
||||
default:
|
||||
exit_err( break_help_text );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void read_status_param( int c, char **sock )
|
||||
{
|
||||
read_sock_param( c, sock, status_help_text );
|
||||
@@ -649,6 +688,27 @@ int mode_mirror( int argc, char *argv[] )
|
||||
}
|
||||
|
||||
|
||||
int mode_break( int argc, char *argv[] )
|
||||
{
|
||||
int c;
|
||||
char *sock = NULL;
|
||||
|
||||
while (1) {
|
||||
c = getopt_long( argc, argv, break_short_options, break_options, NULL );
|
||||
if ( -1 == c ) { break; }
|
||||
read_break_param( c, &sock );
|
||||
}
|
||||
|
||||
if ( NULL == sock ){
|
||||
fprintf( stderr, "--sock is required.\n" );
|
||||
exit_err( acl_help_text );
|
||||
}
|
||||
|
||||
do_remote_command( "break", sock, argc - optind, argv + optind );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mode_status( int argc, char *argv[] )
|
||||
{
|
||||
int c;
|
||||
@@ -722,6 +782,9 @@ void mode(char* mode, int argc, char **argv)
|
||||
else if ( IS_CMD( CMD_MIRROR, mode ) ) {
|
||||
mode_mirror( argc, argv );
|
||||
}
|
||||
else if ( IS_CMD( CMD_BREAK, mode ) ) {
|
||||
mode_break( argc, argv );
|
||||
}
|
||||
else if ( IS_CMD( CMD_STATUS, mode ) ) {
|
||||
mode_status( argc, argv );
|
||||
}
|
||||
|
@@ -28,6 +28,7 @@ void mode(char* mode, int argc, char **argv);
|
||||
#define CMD_WRITE "write"
|
||||
#define CMD_ACL "acl"
|
||||
#define CMD_MIRROR "mirror"
|
||||
#define CMD_BREAK "break"
|
||||
#define CMD_STATUS "status"
|
||||
#define CMD_HELP "help"
|
||||
#define LEN_CMD_MAX 7
|
||||
|
29
src/serve.c
29
src/serve.c
@@ -722,11 +722,8 @@ void serve_cleanup(struct server* params,
|
||||
free(params->allocation_map);
|
||||
}
|
||||
|
||||
if (params->mirror_super) {
|
||||
/* AWOOGA! RACE! */
|
||||
pthread_t mirror_t = params->mirror_super->thread;
|
||||
params->mirror->signal_abandon = 1;
|
||||
pthread_join( mirror_t, NULL );
|
||||
if ( server_is_mirroring( params ) ) {
|
||||
server_abandon_mirror( params );
|
||||
}
|
||||
|
||||
for (i=0; i < params->max_nbd_clients; i++) {
|
||||
@@ -753,6 +750,28 @@ int server_is_in_control( struct server *serve )
|
||||
return serve->has_control;
|
||||
}
|
||||
|
||||
int server_is_mirroring( struct server * serve )
|
||||
{
|
||||
NULLCHECK( serve );
|
||||
return !!serve->mirror_super;
|
||||
}
|
||||
|
||||
void server_abandon_mirror( struct server * serve )
|
||||
{
|
||||
NULLCHECK( serve );
|
||||
if ( serve->mirror_super ) {
|
||||
/* FIXME: AWOOGA! RACE!
|
||||
* We can set signal_abandon after mirror_super has
|
||||
* checked it, but before the reset. This would lead to
|
||||
* a hang. However, mirror_reset doesn't change the
|
||||
* signal_abandon flag, so it'll just terminate early on
|
||||
* the next pass.
|
||||
* */
|
||||
serve->mirror->signal_abandon = 1;
|
||||
pthread_join( serve->mirror_super->thread, NULL );
|
||||
}
|
||||
}
|
||||
|
||||
int server_default_deny( struct server * serve )
|
||||
{
|
||||
NULLCHECK( serve );
|
||||
|
@@ -96,6 +96,8 @@ int server_io_locked( struct server * serve );
|
||||
int server_acl_locked( struct server * serve );
|
||||
void server_lock_acl( struct server *serve );
|
||||
void server_unlock_acl( struct server *serve );
|
||||
int server_is_mirroring( struct server * serve );
|
||||
void server_abandon_mirror( struct server * serve );
|
||||
|
||||
|
||||
int do_serve( struct server * );
|
||||
|
Reference in New Issue
Block a user