From 407305389eea981165dbb9665eb765b06d3b6865 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 23 Nov 2015 11:09:38 +0100 Subject: [FUSE] Refactoring --- src/fuse/connection.c | 120 +++++++++++++++++++++++++++++--------------------- 1 file changed, 69 insertions(+), 51 deletions(-) (limited to 'src/fuse') diff --git a/src/fuse/connection.c b/src/fuse/connection.c index 3e1bf38..039c532 100644 --- a/src/fuse/connection.c +++ b/src/fuse/connection.c @@ -9,17 +9,24 @@ #include #include +/* Constants */ static const size_t SHORTBUF = 100; +#define MAX_ALTS (8) +/* Module variables */ + +// Init guard static bool initDone = false; -pthread_mutex_t mutexInit = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t mutexInit = PTHREAD_MUTEX_INITIALIZER; +// List of pending requests static struct { dnbd3_async_t *head; dnbd3_async_t *tail; pthread_spinlock_t lock; } requests; +// Connection for the image static struct { char *name; uint16_t rid; @@ -29,12 +36,21 @@ static struct { pthread_t receiveThread; } image; +// Known alt servers +static struct _alt_server { + +} altservers[MAX_ALTS]; +typedef struct _alt_server alt_server_t; + +/* Static methods */ + + +static void* connection_receiveThreadMain(void *sock); + static bool throwDataAway(int sockFd, uint32_t amount); static void enqueueRequest(dnbd3_async_t *request); static dnbd3_async_t* removeRequest(dnbd3_async_t *request); -static void* connection_receiveThreadMain(void *sock); - bool connection_init(const char *hosts, const char *lowerImage, const uint16_t rid) { int sock = -1; @@ -114,53 +130,6 @@ void connection_close() // } -static bool throwDataAway(int sockFd, uint32_t amount) -{ - uint32_t done = 0; - char tempBuffer[SHORTBUF]; - while ( done < amount ) { - if ( recv( sockFd, tempBuffer, MIN( amount - done, SHORTBUF ), 0 ) <= 0 ) - return false; - } - return true; -} - -static void enqueueRequest(dnbd3_async_t *request) -{ - request->next = NULL; - request->finished = false; - request->success = false; - pthread_spin_lock( &requests.lock ); - if ( requests.head == NULL ) { - requests.head = requests.tail = request; - } else { - requests.tail->next = request; - requests.tail = request; - } - pthread_spin_unlock( &requests.lock ); -} - -static dnbd3_async_t* removeRequest(dnbd3_async_t *request) -{ - pthread_spin_lock( &requests.lock ); - dnbd3_async_t *iterator, *prev = NULL; - for ( iterator = requests.head; iterator != NULL; iterator = iterator->next ) { - if ( iterator == request ) { - // Found it, break! - if ( prev != NULL ) { - prev->next = iterator->next; - } - if ( requests.tail == iterator ) { - requests.tail = prev; - } - break; - } - prev = iterator; - } - pthread_spin_unlock( &requests.lock ); - return iterator; -} - static void* connection_receiveThreadMain(void *sockPtr) { int sockFd = (int)(size_t)sockPtr; @@ -168,7 +137,7 @@ static void* connection_receiveThreadMain(void *sockPtr) for ( ;; ) { if ( !dnbd3_get_reply( image.sockFd, &reply ) ) goto fail; - // TODO: Ignoring anything but get block replies for now; handle the others + // TODO: Ignoring anything but block replies for now; handle the others if ( reply.cmd != CMD_GET_BLOCK ) { if ( reply.size != 0 && !throwDataAway( sockFd, reply.size ) ) goto fail; @@ -209,3 +178,52 @@ fail:; close( sockFd ); return NULL; } + +// Private quick helpers + +static bool throwDataAway(int sockFd, uint32_t amount) +{ + uint32_t done = 0; + char tempBuffer[SHORTBUF]; + while ( done < amount ) { + if ( recv( sockFd, tempBuffer, MIN( amount - done, SHORTBUF ), 0 ) <= 0 ) + return false; + } + return true; +} + +static void enqueueRequest(dnbd3_async_t *request) +{ + request->next = NULL; + request->finished = false; + request->success = false; + pthread_spin_lock( &requests.lock ); + if ( requests.head == NULL ) { + requests.head = requests.tail = request; + } else { + requests.tail->next = request; + requests.tail = request; + } + pthread_spin_unlock( &requests.lock ); +} + +static dnbd3_async_t* removeRequest(dnbd3_async_t *request) +{ + pthread_spin_lock( &requests.lock ); + dnbd3_async_t *iterator, *prev = NULL; + for ( iterator = requests.head; iterator != NULL; iterator = iterator->next ) { + if ( iterator == request ) { + // Found it, break! + if ( prev != NULL ) { + prev->next = iterator->next; + } + if ( requests.tail == iterator ) { + requests.tail = prev; + } + break; + } + prev = iterator; + } + pthread_spin_unlock( &requests.lock ); + return iterator; +} -- cgit v1.2.3-55-g7522