summaryrefslogtreecommitdiffstats
path: root/src/fuse
diff options
context:
space:
mode:
authorSimon Rettberg2015-11-23 11:09:38 +0100
committerSimon Rettberg2015-11-23 11:09:38 +0100
commit407305389eea981165dbb9665eb765b06d3b6865 (patch)
treefddca5d38bf4a2192cb351510706f1493cd98a5b /src/fuse
parent[FUSE] Start refactoring so we can handle multithread fuse (diff)
downloaddnbd3-407305389eea981165dbb9665eb765b06d3b6865.tar.gz
dnbd3-407305389eea981165dbb9665eb765b06d3b6865.tar.xz
dnbd3-407305389eea981165dbb9665eb765b06d3b6865.zip
[FUSE] Refactoring
Diffstat (limited to 'src/fuse')
-rw-r--r--src/fuse/connection.c120
1 files changed, 69 insertions, 51 deletions
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 <stdio.h>
#include <unistd.h>
+/* 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;
+}