From 95a64fe27f01bfe5019ee9dd92d2cceb5b4b2742 Mon Sep 17 00:00:00 2001 From: ln-tech Date: Fri, 15 Nov 2019 08:14:12 +0100 Subject: code fixes, keepRunning is back, threads seem to be safe --- src/fuse/connection.c | 18 +++++++----------- src/fuse/connection.h | 3 +-- src/fuse/main.c | 28 ++++++++++------------------ 3 files changed, 18 insertions(+), 31 deletions(-) (limited to 'src/fuse') diff --git a/src/fuse/connection.c b/src/fuse/connection.c index 58a7923..cbfaf8c 100644 --- a/src/fuse/connection.c +++ b/src/fuse/connection.c @@ -31,8 +31,8 @@ static const int FAIL_BACKOFF_START_COUNT = 8; // Init guard static bool connectionInitDone = false; static bool threadInitDone = false; -pthread_mutex_t mutexInit = PTHREAD_MUTEX_INITIALIZER; -volatile bool keepRunning = true; +static pthread_mutex_t mutexInit = PTHREAD_MUTEX_INITIALIZER; +extern bool keepRunning = true; static bool learnNewServers; // List of pending requests @@ -267,7 +267,7 @@ void connection_close() logadd( LOG_INFO, "Tearing down dnbd3 connections and workers" ); } pthread_mutex_lock( &mutexInit ); - //keepRunning = false; + keepRunning = false; if ( !connectionInitDone ) { pthread_mutex_unlock( &mutexInit ); return; @@ -349,21 +349,17 @@ static void* connection_receiveThreadMain(void *sockPtr) pthread_detach( pthread_self() ); - while ( true ) { + while ( keepRunning ) { int ret; struct fuse_bufvec splice_buf; do { ret = dnbd3_read_reply( sockFd, &reply, true ); - printf("\n ret %d %d", ret); if ( ret == REPLY_OK ) break; } while ( ret == REPLY_INTR || ret == REPLY_AGAIN ); - //printf("\n hallo %d", keepRunning); if ( ret != REPLY_OK ) { logadd( LOG_DEBUG1, "Error receiving reply on receiveThread (%d)", ret ); - if (ret == REPLY_CLOSED) return NULL; goto fail; } - printf("\n Rep cmd %d", reply.cmd); if ( reply.cmd == CMD_GET_BLOCK ) { // Get block reply. find matching request dnbd3_async_t *request = removeRequest( (dnbd3_async_t*)reply.handle ); @@ -447,6 +443,7 @@ static void* connection_receiveThreadMain(void *sockPtr) } } } + if (!keepRunning) connection_close(); logadd( LOG_DEBUG1, "Aus der Schleife rausgeflogen! ARRRRRRRRRR" ); fail:; // Make sure noone is trying to use the socket for sending by locking, @@ -470,8 +467,7 @@ static void* connection_backgroundThread(void *something UNUSED) timing_get( &nextKeepalive ); nextRttCheck = nextKeepalive; - while ( true) { - //if ( !keepRunning ) break; + while ( keepRunning ) { ticks now; timing_get( &now ); uint32_t wt1 = timing_diffMs( &now, &nextKeepalive ); @@ -733,7 +729,7 @@ static void probeAltServers() // Panic mode? Just switch to server if ( panic ) { unlock_rw( &altLock ); - switchConnection( sock, srv ); + if (keepRunning) switchConnection( sock, srv ); return; } // Non-panic mode: diff --git a/src/fuse/connection.h b/src/fuse/connection.h index ceb5050..d0a89dc 100644 --- a/src/fuse/connection.h +++ b/src/fuse/connection.h @@ -12,9 +12,8 @@ #define NO_SPLICE 0 #define SPLICE 1 -extern volatile bool keepRunning; +extern bool keepRunning; struct _dnbd3_async; -extern pthread_mutex_t mutexInit; typedef struct _dnbd3_async { struct _dnbd3_async *next; // Next in this linked list (provate field, not set by caller) diff --git a/src/fuse/main.c b/src/fuse/main.c index 175cbb2..e3e818b 100644 --- a/src/fuse/main.c +++ b/src/fuse/main.c @@ -32,7 +32,8 @@ #include #define debugf(...) do { logadd( LOG_DEBUG1, __VA_ARGS__ ); } while (0) -#define MODE 1 +#define MODE 0 + static const char * const IMAGE_PATH = "/img"; static const char * const STATS_PATH = "/status"; @@ -239,7 +240,7 @@ static void image_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off ++logInfo.blockRequestCount[startBlock]; } } - + if (!keepRunning) connection_close(); if (ino == 2 && size != 0) // with size == 0 there is nothing to do { dnbd3_async_t *request = malloc(sizeof(dnbd3_async_t)); @@ -253,10 +254,7 @@ static void image_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off } static void image_sigHandler(int signum) { - pthread_mutex_lock( &mutexInit ); keepRunning = false; - pthread_mutex_unlock( &mutexInit ); - //printf("\n hallo222 %d", keepRunning); if ( signum == SIGINT && fuse_sigIntHandler != NULL ) { fuse_sigIntHandler(signum); } @@ -307,24 +305,18 @@ flags contains FUSE_BUF_SPLICE_MOVE printf("\tFUSE_CAP_IOCTL_DIR\n"); if(conn->capable & FUSE_CAP_BIG_WRITES) printf("\tFUSE_CAP_BIG_WRITES\n"); - if(conn->capable & FUSE_CAP_ASYNC_READ) - printf("\tFUSE_CAP_ASYNC_READ\n"); - if(conn->capable & FUSE_CAP_FLOCK_LOCKS) - printf("\tFUSE_CAP_FLOCK_LOCKS\n"); - if(conn->capable & FUSE_CAP_EXPORT_SUPPORT) - printf("\tFUSE_CAP_EXPORT_SUPPORT\n"); - if(conn->capable & FUSE_CAP_ASYNC_READ) - printf("\tFUSE_CAP_ASYNC_READ\n"); - if(conn->capable & FUSE_CAP_POSIX_LOCKS) - printf("\tFUSE_CAP_POSIX_LOCKS\n"); - conn->want |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE | FUSE_CAP_SPLICE_READ; + if ( MODE == NO_SPLICE ) conn->want &= ~FUSE_CAP_SPLICE_WRITE & ~FUSE_CAP_SPLICE_MOVE & ~FUSE_CAP_SPLICE_READ; + else if ( MODE == SPLICE ) conn->want |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE | FUSE_CAP_SPLICE_READ; if(conn->want & FUSE_CAP_SPLICE_WRITE) printf("\tFUSE_CAP_SPLICE_WRITE on\n"); + else printf("\tFUSE_CAP_SPLICE_WRITE off\n"); if(conn->want & FUSE_CAP_SPLICE_MOVE) printf("\tFUSE_CAP_SPLICE_MOVE on\n"); + else printf("\tFUSE_CAP_SPLICE_MOVE off\n"); if(conn->want & FUSE_CAP_SPLICE_READ) printf("\tFUSE_CAP_SPLICE_READ on\n"); + else printf("\tFUSE_CAP_SPLICE_READ off\n"); if ( !connection_initThreads() ) { @@ -336,7 +328,7 @@ flags contains FUSE_BUF_SPLICE_MOVE memset( &newHandler, 0, sizeof(newHandler) ); newHandler.sa_handler = &image_sigHandler; //newHandler.sa_flags &= ~SA_RESTART; - newHandler.sa_flags |= SA_RESETHAND; + newHandler.sa_flags |= SA_RESETHAND; // allows SIGINT with SPLICE sigemptyset( &newHandler.sa_mask ); //rt_sigaction(sa_flags=SA_RESTORER|SA_ONSTACK|SA_INTERRUPT|SA_NODEFER|SA_RESETHAND|SA_SIGINFO|SA_NOCLDSTOP|SA_NOCLDWAIT|0x3fffff8 struct sigaction oldHandler; @@ -526,7 +518,7 @@ int main(int argc, char *argv[]) // Since dnbd3 is always read only and the remote image will not change newArgv[newArgc++] = "-o"; //newArgv[newArgc++] = "ro,auto_cache,default_permissions"; - newArgv[newArgc++] = "ro,splice_read,default_permissions"; + newArgv[newArgc++] = "ro,default_permissions"; // Mount point goes last newArgv[newArgc++] = argv[optind]; -- cgit v1.2.3-55-g7522