From e71e747cf7173f6fe4d1b113364c0e458e83f129 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 21 Jul 2020 15:47:28 +0200 Subject: [FUSE] Formatting --- src/fuse/connection.c | 93 ++++++++++--------- src/fuse/connection.h | 6 +- src/fuse/helper.c | 6 +- src/fuse/helper.h | 12 +-- src/fuse/main.c | 246 +++++++++++++++++++++++++------------------------- 5 files changed, 183 insertions(+), 180 deletions(-) (limited to 'src/fuse') diff --git a/src/fuse/connection.c b/src/fuse/connection.c index f79e8e5..c4f8de3 100644 --- a/src/fuse/connection.c +++ b/src/fuse/connection.c @@ -84,20 +84,20 @@ static pthread_rwlock_t altLock = PTHREAD_RWLOCK_INITIALIZER; /* Static methods */ -static void* connection_receiveThreadMain(void *sock); -static void* connection_backgroundThread(void *something); +static void* connection_receiveThreadMain( void *sock ); +static void* connection_backgroundThread( void *something ); static void addAltServers(); static void sortAltServers(); static void probeAltServers(); -static void switchConnection(int sockFd, alt_server_t *srv); +static void switchConnection( int sockFd, alt_server_t *srv ); static void requestAltServers(); -static bool throwDataAway(int sockFd, uint32_t amount); +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 enqueueRequest( dnbd3_async_t *request ); +static dnbd3_async_t* removeRequest( dnbd3_async_t *request ); -bool connection_init(const char *hosts, const char *lowerImage, const uint16_t rid, const bool doLearnNew) +bool connection_init( const char *hosts, const char *lowerImage, const uint16_t rid, const bool doLearnNew ) { int sock = -1; char host[SHORTBUF]; @@ -124,7 +124,7 @@ bool connection_init(const char *hosts, const char *lowerImage, const uint16_t r // Get next host from string while ( *current == ' ' ) current++; end = strchr( current, ' ' ); - size_t len = (end == NULL ? SHORTBUF : (size_t)( end - current ) + 1); + size_t len = ( end == NULL ? SHORTBUF : (size_t)( end - current ) + 1 ); if ( len > SHORTBUF ) len = SHORTBUF; snprintf( host, len, "%s", current ); int newHosts = sock_resolveToDnbd3Host( host, tempHosts, MAX_HOSTS_PER_ADDRESS ); @@ -156,14 +156,14 @@ bool connection_init(const char *hosts, const char *lowerImage, const uint16_t r } if ( sock == -2 || sock == -1 ) continue; - salen = sizeof(sa); + salen = sizeof( sa ); if ( getpeername( sock, (struct sockaddr*)&sa, &salen ) == -1 ) { logadd( LOG_ERROR, "getpeername on successful connection failed!? (errno=%d)", errno ); close( sock ); sock = -1; continue; } - hlen = sock_printable( (struct sockaddr*)&sa, salen, host, sizeof(host) ); + hlen = sock_printable( (struct sockaddr*)&sa, salen, host, sizeof( host ) ); logadd( LOG_INFO, "Connected to %.*s", (int)hlen, host ); if ( !dnbd3_select_image( sock, lowerImage, rid, 0 ) ) { logadd( LOG_ERROR, "Could not send select image" ); @@ -221,10 +221,10 @@ bool connection_initThreads() logadd( LOG_ERROR, "Mutex or spinlock init failure" ); success = false; } else { - if (pthread_create( &thread, NULL, &connection_receiveThreadMain, (void*)(size_t)connection.sockFd ) != 0 ) { + if ( pthread_create( &thread, NULL, &connection_receiveThreadMain, ( void* )(size_t)connection.sockFd ) != 0 ) { logadd( LOG_ERROR, "Could not create receive thread" ); success = false; - } else if (pthread_create( &thread, NULL, &connection_backgroundThread, NULL ) != 0 ) { + } else if ( pthread_create( &thread, NULL, &connection_backgroundThread, NULL ) != 0 ) { logadd( LOG_ERROR, "Could not create background thread" ); success = false; } @@ -242,7 +242,7 @@ uint64_t connection_getImageSize() return image.size; } -bool connection_read(dnbd3_async_t *request) +bool connection_read( dnbd3_async_t *request ) { if ( !connectionInitDone ) return false; pthread_mutex_lock( &connection.sendMutex ); @@ -280,14 +280,14 @@ void connection_close() logadd( LOG_DEBUG1, "Connection closed." ); } -size_t connection_printStats(char *buffer, const size_t len) +size_t connection_printStats( char *buffer, const size_t len ) { int ret; size_t remaining = len; declare_now; if ( remaining > 0 ) { ret = snprintf( buffer, remaining, "Image: %s\nRevision: %d\n\nCurrent connection time: %" PRIu32 "s\n\n", - image.name, (int)image.rid, timing_diff( &connection.startupTime, &now ) ); + image.name, (int)image.rid, timing_diff( &connection.startupTime, &now ) ); if ( ret < 0 ) { ret = 0; } @@ -310,7 +310,7 @@ size_t connection_printStats(char *buffer, const size_t len) *buffer++ = ' '; } const size_t addrlen = sock_printHost( &altservers[i].host, buffer, remaining ); - remaining -= (addrlen + 1); // For space or * above + remaining -= ( addrlen + 1 ); // For space or * above buffer += addrlen; if ( remaining < 3 ) break; @@ -326,7 +326,7 @@ size_t connection_printStats(char *buffer, const size_t len) width += 3; } ret = snprintf( buffer, remaining, "% *d %s Unreachable:% 5d BestCount:% 5d Live:% 5dµs\n", - width, value, unit, altservers[i].consecutiveFails, altservers[i].bestCount, altservers[i].liveRtt ); + width, value, unit, altservers[i].consecutiveFails, altservers[i].bestCount, altservers[i].liveRtt ); if ( ret < 0 ) { ret = 0; } @@ -341,7 +341,7 @@ size_t connection_printStats(char *buffer, const size_t len) return len - remaining; } -static void* connection_receiveThreadMain(void *sockPtr) +static void* connection_receiveThreadMain( void *sockPtr ) { int sockFd = (int)(size_t)sockPtr; dnbd3_reply_t reply; @@ -370,12 +370,12 @@ static void* connection_receiveThreadMain(void *sockPtr) } } else { // Found a match - request->buffer = malloc(request->length); // char* + request->buffer = malloc( request->length ); // char* const ssize_t ret = sock_recv( sockFd, request->buffer, request->length ); if ( ret != (ssize_t)request->length ) { logadd( LOG_DEBUG1, "receiving payload for a block reply failed" ); connection_read( request ); - free(request->buffer); + free( request->buffer ); request->buffer = NULL; goto fail; } @@ -394,14 +394,14 @@ static void* connection_receiveThreadMain(void *sockPtr) } unlock_rw( &altLock ); } - int fuse_reply = fuse_reply_buf(request->fuse_req, request->buffer, request->length); - if (fuse_reply != 0) { - printf("ERROR ON FUSE REPLY %i \n", fuse_reply); - fuse_reply_err(request->fuse_req, fuse_reply); + int fuse_reply = fuse_reply_buf( request->fuse_req, request->buffer, request->length ); + if ( fuse_reply != 0 ) { + printf( "ERROR ON FUSE REPLY %i \n", fuse_reply ); + fuse_reply_err( request->fuse_req, fuse_reply ); } - free(request->buffer); + free( request->buffer ); request->buffer = NULL; - free(request); + free( request ); request = NULL; } } else if ( reply.cmd == CMD_GET_SERVERS ) { @@ -425,9 +425,10 @@ static void* connection_receiveThreadMain(void *sockPtr) } } } - if(!keepRunning) connection_close(); + if( !keepRunning ) connection_close(); logadd( LOG_DEBUG1, "Aus der Schleife rausgeflogen! ARRRRRRRRRR" ); -fail:; +fail: + ; // Make sure noone is trying to use the socket for sending by locking, pthread_mutex_lock( &connection.sendMutex ); // then just set the fd to -1, but only if it's the same fd as ours, @@ -442,7 +443,7 @@ fail:; return NULL; } -static void* connection_backgroundThread(void *something UNUSED) +static void* connection_backgroundThread( void *something UNUSED ) { pthread_detach( pthread_self() ); // fixes thread leak after fuse termination ticks nextKeepalive; @@ -539,9 +540,10 @@ static void addAltServers() altservers[slot].host = newservers[nIdx].host; altservers[slot].liveRtt = 0; } -skip_server:; +skip_server: + ; } - memset( newservers, 0, sizeof(newservers) ); + memset( newservers, 0, sizeof( newservers ) ); unlock_rw( &altLock ); pthread_mutex_unlock( &newAltLock ); } @@ -637,7 +639,7 @@ static void probeAltServers() } lock_read( &altLock ); - for ( int altIndex = 0; altIndex < (panic ? MAX_ALTS : MAX_ALTS_ACTIVE); ++altIndex ) { + for ( int altIndex = 0; altIndex < ( panic ? MAX_ALTS : MAX_ALTS_ACTIVE ); ++altIndex ) { alt_server_t * const srv = &altservers[altIndex]; if ( srv->host.type == 0 ) continue; @@ -663,7 +665,7 @@ static void probeAltServers() logadd( LOG_DEBUG1, "probe: select_image failed" ); goto fail; } - if ( !dnbd3_select_image_reply( &buffer, sock, &remoteProto, &remoteName, &remoteRid, &remoteSize )) { + if ( !dnbd3_select_image_reply( &buffer, sock, &remoteProto, &remoteName, &remoteRid, &remoteSize ) ) { logadd( LOG_DEBUG1, "probe: select image reply failed" ); goto fail; } @@ -682,7 +684,7 @@ static void probeAltServers() goto fail; } int a = 111; - if ( !(a = dnbd3_get_reply( sock, &reply )) || reply.size != testLength ) { + if ( !( a = dnbd3_get_reply( sock, &reply ) ) || reply.size != testLength ) { logadd( LOG_DEBUG1, "<- get block reply fail %d %d", a, (int)reply.size ); goto fail; } @@ -709,7 +711,7 @@ static void probeAltServers() // Panic mode? Just switch to server if ( panic ) { unlock_rw( &altLock ); - if (keepRunning) switchConnection( sock, srv ); + if ( keepRunning ) switchConnection( sock, srv ); return; } // Non-panic mode: @@ -741,7 +743,8 @@ static void probeAltServers() close( sock ); } continue; -fail:; +fail: + ; if ( sock != -1 ) { close( sock ); } @@ -782,7 +785,7 @@ fail:; // Regular logic: Apply threshold when considering switch if ( !doSwitch && current != NULL ) { doSwitch = current->rtt > best->rtt + RTT_ABSOLUTE_THRESHOLD - || RTT_THRESHOLD_FACTOR(current->rtt) > best->rtt + 1000; + || RTT_THRESHOLD_FACTOR( current->rtt ) > best->rtt + 1000; } } // Switch if a better server was found @@ -804,11 +807,11 @@ fail:; } } -static void switchConnection(int sockFd, alt_server_t *srv) +static void switchConnection( int sockFd, alt_server_t *srv ) { pthread_t thread; struct sockaddr_storage addr; - socklen_t addrLen = sizeof(addr); + socklen_t addrLen = sizeof( addr ); char message[200] = "Connection switched to "; const size_t len = strlen( message ); int ret; @@ -838,8 +841,8 @@ static void switchConnection(int sockFd, alt_server_t *srv) return; } timing_get( &connection.startupTime ); - pthread_create( &thread, NULL, &connection_receiveThreadMain, (void*)(size_t)sockFd ); - sock_printable( (struct sockaddr*)&addr, sizeof(addr), message + len, sizeof(message) - len ); + pthread_create( &thread, NULL, &connection_receiveThreadMain, ( void* )(size_t)sockFd ); + sock_printable( (struct sockaddr*)&addr, sizeof( addr ), message + len, sizeof( message ) - len ); logadd( LOG_INFO, "%s", message ); // resend queue if ( queue != NULL ) { @@ -871,14 +874,14 @@ static void requestAltServers() request.magic = dnbd3_packet_magic; request.cmd = CMD_GET_SERVERS; fixup_request( request ); - if ( sock_sendAll( connection.sockFd, &request, sizeof(request), 2 ) != (ssize_t)sizeof(request) ) { + if ( sock_sendAll( connection.sockFd, &request, sizeof( request ), 2 ) != (ssize_t)sizeof( request ) ) { logadd( LOG_WARNING, "Connection failed while requesting alt server list" ); shutdown( connection.sockFd, SHUT_RDWR ); connection.sockFd = -1; } } -static bool throwDataAway(int sockFd, uint32_t amount) +static bool throwDataAway( int sockFd, uint32_t amount ) { size_t done = 0; char tempBuffer[SHORTBUF]; @@ -891,7 +894,7 @@ static bool throwDataAway(int sockFd, uint32_t amount) return true; } -static void enqueueRequest(dnbd3_async_t *request) +static void enqueueRequest( dnbd3_async_t *request ) { request->next = NULL; //logadd( LOG_DEBUG2, "Queue: %p @ %s : %d", request, file, line ); @@ -907,7 +910,7 @@ static void enqueueRequest(dnbd3_async_t *request) pthread_spin_unlock( &requests.lock ); } -static dnbd3_async_t* removeRequest(dnbd3_async_t *request) +static dnbd3_async_t* removeRequest( dnbd3_async_t *request ) { pthread_spin_lock( &requests.lock ); //logadd( LOG_DEBUG2, "Remov: %p @ %s : %d", request, file, line ); diff --git a/src/fuse/connection.h b/src/fuse/connection.h index c64806c..cd9846e 100644 --- a/src/fuse/connection.h +++ b/src/fuse/connection.h @@ -23,16 +23,16 @@ typedef struct _dnbd3_async { fuse_req_t fuse_req; } dnbd3_async_t; -bool connection_init(const char *hosts, const char *image, const uint16_t rid, const bool learnNewServers); +bool connection_init( const char *hosts, const char *image, const uint16_t rid, const bool learnNewServers ); bool connection_initThreads(); uint64_t connection_getImageSize(); -bool connection_read(dnbd3_async_t *request); +bool connection_read( dnbd3_async_t *request ); void connection_close(); -size_t connection_printStats(char *buffer, const size_t len); +size_t connection_printStats( char *buffer, const size_t len ); #endif /* CONNECTION_H_ */ diff --git a/src/fuse/helper.c b/src/fuse/helper.c index d81b08f..f54073b 100644 --- a/src/fuse/helper.c +++ b/src/fuse/helper.c @@ -18,8 +18,8 @@ void printLog( log_info *info ) } //rewind(file); - fprintf( logFile, "ImageSize: %"PRIu64" MiB\n", ( uint64_t )( info->imageSize/ ( 1024ll*1024ll ) ) ); - fprintf( logFile, "ReceivedMiB: %"PRIu64" MiB\n", ( uint64_t )( info->receivedBytes/ ( 1024ll*1024ll ) ) ); + fprintf( logFile, "ImageSize: %"PRIu64" MiB\n", (uint64_t)( info->imageSize/ ( 1024ll*1024ll ) ) ); + fprintf( logFile, "ReceivedMiB: %"PRIu64" MiB\n", (uint64_t)( info->receivedBytes/ ( 1024ll*1024ll ) ) ); fprintf( logFile, "imageBlockCount: %"PRIu64"\n", info->imageBlockCount ); fprintf( logFile, "Blocksize: 4KiB\n\n" ); fprintf( logFile, "Block access count:\n" ); @@ -29,7 +29,7 @@ void printLog( log_info *info ) if ( i % 50 == 0 ) { fprintf( logFile, "\n" ); } - fprintf( logFile, "%i ", ( int ) info->blockRequestCount[i] ); + fprintf( logFile, "%i ", (int) info->blockRequestCount[i] ); } fprintf( logFile, "\n" ); fclose( logFile ); diff --git a/src/fuse/helper.h b/src/fuse/helper.h index 9e5d127..65cca2c 100644 --- a/src/fuse/helper.h +++ b/src/fuse/helper.h @@ -18,18 +18,18 @@ typedef struct log_info { -void printLog(log_info *info); +void printLog( log_info *info ); -int connect_to_server(char *server_adress, int port); +int connect_to_server( char *server_adress, int port ); -static inline bool isSameAddressPort(const dnbd3_host_t * const a, const dnbd3_host_t * const b) +static inline bool isSameAddressPort( const dnbd3_host_t * const a, const dnbd3_host_t * const b ) { - return (a->type == b->type) && (a->port == b->port) && (0 == memcmp( a->addr, b->addr, (a->type == HOST_IP4 ? 4 : 16) )); + return ( a->type == b->type ) && ( a->port == b->port ) && ( 0 == memcmp( a->addr, b->addr, ( a->type == HOST_IP4 ? 4 : 16 ) ) ); } -static inline bool isSameAddress(const dnbd3_host_t * const a, const dnbd3_host_t * const b) +static inline bool isSameAddress( const dnbd3_host_t * const a, const dnbd3_host_t * const b ) { - return (a->type == b->type) && (0 == memcmp( a->addr, b->addr, (a->type == HOST_IP4 ? 4 : 16) )); + return ( a->type == b->type ) && ( 0 == memcmp( a->addr, b->addr, ( a->type == HOST_IP4 ? 4 : 16 ) ) ); } #endif diff --git a/src/fuse/main.c b/src/fuse/main.c index 99cf228..295c194 100644 --- a/src/fuse/main.c +++ b/src/fuse/main.c @@ -43,28 +43,28 @@ static bool useDebug = false; static log_info logInfo; static struct timespec startupTime; static uid_t owner; -static void (*fuse_sigIntHandler)(int) = NULL; -static void (*fuse_sigTermHandler)(int) = NULL; - -static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize, off_t off, size_t maxsize); -static int fillStatsFile(char *buf, size_t size, off_t offset); -static void image_destroy(void *private_data); -static void image_ll_getattr(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi); -static void image_ll_init(void *userdata, struct fuse_conn_info *conn); -static void image_ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name); -static void image_ll_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi); -static void image_ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, struct fuse_file_info *fi); -static void image_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t offset, struct fuse_file_info *fi); -static int image_stat(fuse_ino_t ino, struct stat *stbuf); -static void printUsage(char *argv0, int exitCode); +static void ( *fuse_sigIntHandler )(int) = NULL; +static void ( *fuse_sigTermHandler )(int) = NULL; + +static int reply_buf_limited( fuse_req_t req, const char *buf, size_t bufsize, off_t off, size_t maxsize ); +static int fillStatsFile( char *buf, size_t size, off_t offset ); +static void image_destroy( void *private_data ); +static void image_ll_getattr( fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi ); +static void image_ll_init( void *userdata, struct fuse_conn_info *conn ); +static void image_ll_lookup( fuse_req_t req, fuse_ino_t parent, const char *name ); +static void image_ll_open( fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi ); +static void image_ll_readdir( fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, struct fuse_file_info *fi ); +static void image_ll_read( fuse_req_t req, fuse_ino_t ino, size_t size, off_t offset, struct fuse_file_info *fi ); +static int image_stat( fuse_ino_t ino, struct stat *stbuf ); +static void printUsage( char *argv0, int exitCode ); static void printVersion(); -static int image_stat(fuse_ino_t ino, struct stat *stbuf) +static int image_stat( fuse_ino_t ino, struct stat *stbuf ) { stbuf->st_ctim = stbuf->st_atim = stbuf->st_mtim = startupTime; stbuf->st_uid = owner; stbuf->st_ino = ino; - switch (ino) { + switch ( ino ) { case 1: stbuf->st_mode = S_IFDIR | 0550; stbuf->st_nlink = 2; @@ -88,38 +88,38 @@ static int image_stat(fuse_ino_t ino, struct stat *stbuf) return 0; } -static void image_ll_getattr(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) +static void image_ll_getattr( fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi ) { struct stat stbuf; - (void) fi; + ( void ) fi; - memset(&stbuf, 0, sizeof(stbuf)); - if (image_stat(ino, &stbuf) == -1) - fuse_reply_err(req, ENOENT); + memset( &stbuf, 0, sizeof( stbuf ) ); + if ( image_stat( ino, &stbuf ) == -1 ) + fuse_reply_err( req, ENOENT ); else - fuse_reply_attr(req, &stbuf, 1.0); // 1.0 seconds validity timeout + fuse_reply_attr( req, &stbuf, 1.0 ); // 1.0 seconds validity timeout } -static void image_ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name) +static void image_ll_lookup( fuse_req_t req, fuse_ino_t parent, const char *name ) { struct fuse_entry_param e; - (void)parent; + ( void )parent; - if (strcmp(name, IMAGE_NAME) == 0 || strcmp(name, STATS_NAME) == 0) { - memset(&e, 0, sizeof(e)); - if (strcmp(name, IMAGE_NAME) == 0) { + if ( strcmp( name, IMAGE_NAME ) == 0 || strcmp( name, STATS_NAME ) == 0 ) { + memset( &e, 0, sizeof( e ) ); + if ( strcmp( name, IMAGE_NAME ) == 0 ) { e.ino = 2; } else { e.ino = 3; } e.attr_timeout = 1.0; e.entry_timeout = 1.0; - image_stat(e.ino, &e.attr); + image_stat( e.ino, &e.attr ); - fuse_reply_entry(req, &e); + fuse_reply_entry( req, &e ); } - else fuse_reply_err(req, ENOENT); + else fuse_reply_err( req, ENOENT ); } struct dirbuf { @@ -127,61 +127,61 @@ struct dirbuf { size_t size; }; -static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name, fuse_ino_t ino) +static void dirbuf_add( fuse_req_t req, struct dirbuf *b, const char *name, fuse_ino_t ino ) { struct stat stbuf; size_t oldsize = b->size; - b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0); - b->p = (char *) realloc(b->p, b->size); - memset(&stbuf, 0, sizeof(stbuf)); + b->size += fuse_add_direntry( req, NULL, 0, name, NULL, 0 ); + b->p = ( char * ) realloc( b->p, b->size ); + memset( &stbuf, 0, sizeof( stbuf ) ); stbuf.st_ino = ino; - fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf, b->size); + fuse_add_direntry( req, b->p + oldsize, b->size - oldsize, name, &stbuf, b->size ); return; } #define min(x, y) ((x) < (y) ? (x) : (y)) -static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize, off_t off, size_t maxsize) +static int reply_buf_limited( fuse_req_t req, const char *buf, size_t bufsize, off_t off, size_t maxsize ) { - if (off >= 0 && off < (off_t)bufsize) - return fuse_reply_buf(req, buf + off, min(bufsize - off, maxsize)); + if ( off >= 0 && off < (off_t)bufsize ) + return fuse_reply_buf( req, buf + off, min( bufsize - off, maxsize ) ); else - return fuse_reply_buf(req, NULL, 0); + return fuse_reply_buf( req, NULL, 0 ); } -static void image_ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, struct fuse_file_info *fi) +static void image_ll_readdir( fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, struct fuse_file_info *fi ) { - (void) fi; + ( void ) fi; - if (ino != 1) - fuse_reply_err(req, ENOTDIR); + if ( ino != 1 ) + fuse_reply_err( req, ENOTDIR ); else { struct dirbuf b; - memset(&b, 0, sizeof(b)); - dirbuf_add(req, &b, ".", 1); - dirbuf_add(req, &b, "..", 1); - dirbuf_add(req, &b, IMAGE_NAME, 2); - dirbuf_add(req, &b, STATS_NAME, 3); - reply_buf_limited(req, b.p, b.size, off, size); - free(b.p); + memset( &b, 0, sizeof( b ) ); + dirbuf_add( req, &b, ".", 1 ); + dirbuf_add( req, &b, "..", 1 ); + dirbuf_add( req, &b, IMAGE_NAME, 2 ); + dirbuf_add( req, &b, STATS_NAME, 3 ); + reply_buf_limited( req, b.p, b.size, off, size ); + free( b.p ); } } -static void image_ll_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) +static void image_ll_open( fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi ) { - if (ino != 2 && ino != 3) - fuse_reply_err(req, EISDIR); - else if ((fi->flags & 3) != O_RDONLY) - fuse_reply_err(req, EACCES); + if ( ino != 2 && ino != 3 ) + fuse_reply_err( req, EISDIR ); + else if ( ( fi->flags & 3 ) != O_RDONLY ) + fuse_reply_err( req, EACCES ); else { - // auto caching + // auto caching fi->keep_cache = 1; - fuse_reply_open(req, fi); + fuse_reply_open( req, fi ); } } -static int fillStatsFile(char *buf, size_t size, off_t offset) { +static int fillStatsFile( char *buf, size_t size, off_t offset ) { if ( offset == 0 ) { return (int)connection_printStats( buf, size ); } @@ -197,15 +197,15 @@ static int fillStatsFile(char *buf, size_t size, off_t offset) { return len; } -static void image_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t offset, struct fuse_file_info *fi) +static void image_ll_read( fuse_req_t req, fuse_ino_t ino, size_t size, off_t offset, struct fuse_file_info *fi ) { - assert(ino == 2 || ino == 3); + assert( ino == 2 || ino == 3 ); - (void)fi; + ( void )fi; int len = 0; char *buf = NULL; - if (size > __INT_MAX__) + if ( size > __INT_MAX__ ) { // fuse docs say we MUST fill the buffer with exactly size bytes and return size, // otherwise the buffer will we padded with zeros. Since the return value is just @@ -213,66 +213,66 @@ static void image_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off // mention of a guarantee that this will never happen, better add a safety check. // Way to go fuse. // return -EIO; - fuse_reply_err(req, EIO); + fuse_reply_err( req, EIO ); } - if (ino == 3) + if ( ino == 3 ) { - buf = (char *)malloc(4096); // they use 4096 byte buffer in fillStatsFile() for the status-file - len = fillStatsFile(buf, size, offset); - fuse_reply_buf(req, buf, len); - free(buf); + buf = ( char * )malloc( 4096 ); // they use 4096 byte buffer in fillStatsFile() for the status-file + len = fillStatsFile( buf, size, offset ); + fuse_reply_buf( req, buf, len ); + free( buf ); buf = NULL; } - if ((uint64_t)offset >= imageSize) + if ( (uint64_t)offset >= imageSize ) { - fuse_reply_err(req, 0); + fuse_reply_err( req, 0 ); } - if (offset + size > imageSize) + if ( offset + size > imageSize ) { size = imageSize - offset; } - if (useDebug) + if ( useDebug ) { - uint64_t startBlock = offset / (4096); - const uint64_t endBlock = (offset + size - 1) / (4096); + uint64_t startBlock = offset / ( 4096 ); + const uint64_t endBlock = ( offset + size - 1 ) / ( 4096 ); - for (; startBlock <= endBlock; startBlock++) + for ( ; startBlock <= endBlock; startBlock++ ) { ++logInfo.blockRequestCount[startBlock]; } } - if (!keepRunning) connection_close(); - if (ino == 2 && size != 0) // with size == 0 there is nothing to do + 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)); + dnbd3_async_t *request = malloc( sizeof(dnbd3_async_t) ); request->length = (uint32_t)size; request->offset = offset; request->fuse_req = req; - if (!connection_read(request)) fuse_reply_err(req, EINVAL); + if ( !connection_read( request ) ) fuse_reply_err( req, EINVAL ); } } -static void image_sigHandler(int signum) { +static void image_sigHandler( int signum ) { int temp_errno = errno; // Threadsanitizer: don't spoil errno if ( signum == SIGINT && fuse_sigIntHandler != NULL ) { keepRunning = false; - fuse_sigIntHandler(signum); + fuse_sigIntHandler( signum ); } if ( signum == SIGTERM && fuse_sigTermHandler != NULL ) { keepRunning = false; - fuse_sigTermHandler(signum); + fuse_sigTermHandler( signum ); } errno = temp_errno; } -static void image_ll_init(void *userdata, struct fuse_conn_info *conn) +static void image_ll_init( void *userdata, struct fuse_conn_info *conn ) { - (void) userdata; - (void) conn; + ( void ) userdata; + ( void ) conn; if ( !connection_initThreads() ) { logadd( LOG_ERROR, "Could not initialize threads for dnbd3 connection, exiting..." ); exit( EXIT_FAILURE ); @@ -280,21 +280,21 @@ static void image_ll_init(void *userdata, struct fuse_conn_info *conn) // Prepare our handler struct sigaction newHandler; - memset( &newHandler, 0, sizeof(newHandler) ); + memset( &newHandler, 0, sizeof( newHandler ) ); newHandler.sa_handler = &image_sigHandler; sigemptyset( &newHandler.sa_mask ); struct sigaction oldHandler; // Retrieve old handlers when setting sigaction( SIGINT, &newHandler, &oldHandler ); fuse_sigIntHandler = oldHandler.sa_handler; - logadd( LOG_DEBUG1, "Previous SIGINT handler was %p", (void*)(uintptr_t)fuse_sigIntHandler ); + logadd( LOG_DEBUG1, "Previous SIGINT handler was %p", ( void* )(uintptr_t)fuse_sigIntHandler ); sigaction( SIGTERM, &newHandler, &oldHandler ); fuse_sigTermHandler = oldHandler.sa_handler; - logadd( LOG_DEBUG1, "Previous SIGTERM handler was %p", (void*)(uintptr_t)fuse_sigIntHandler ); + logadd( LOG_DEBUG1, "Previous SIGTERM handler was %p", ( void* )(uintptr_t)fuse_sigIntHandler ); } /* close the connection */ -static void image_destroy(void *private_data UNUSED) +static void image_destroy( void *private_data UNUSED ) { if ( useDebug ) { printLog( &logInfo ); @@ -318,16 +318,16 @@ static void printVersion() { char *arg[] = { "foo", "-V" }; printf( "DNBD3-Fuse Version 1.2.3.4, protocol version %d\n", (int)PROTOCOL_VERSION ); - struct fuse_args args = FUSE_ARGS_INIT(2, arg); - fuse_parse_cmdline(&args, NULL, NULL, NULL); + struct fuse_args args = FUSE_ARGS_INIT( 2, arg ); + fuse_parse_cmdline( &args, NULL, NULL, NULL ); exit( 0 ); } -static void printUsage(char *argv0, int exitCode) +static void printUsage( char *argv0, int exitCode ) { char *arg[] = { argv0, "-h" }; - struct fuse_args args = FUSE_ARGS_INIT(2, arg); - fuse_parse_cmdline(&args, NULL, NULL, NULL); + struct fuse_args args = FUSE_ARGS_INIT( 2, arg ); + fuse_parse_cmdline( &args, NULL, NULL, NULL ); printf( "\n" ); printf( "Usage: %s [--debug] [--option mountOpts] --host --image [--rid revision] \n", argv0 ); printf( "Or: %s [-d] [-o mountOpts] -h -i [-r revision] \n", argv0 ); @@ -345,19 +345,19 @@ static void printUsage(char *argv0, int exitCode) static const char *optString = "dfHh:i:l:o:r:SsVv"; static const struct option longOpts[] = { - { "debug", no_argument, NULL, 'd' }, - { "help", no_argument, NULL, 'H' }, - { "host", required_argument, NULL, 'h' }, - { "image", required_argument, NULL, 'i' }, - { "log", required_argument, NULL, 'l' }, - { "option", required_argument, NULL, 'o' }, - { "rid", required_argument, NULL, 'r' }, - { "sticky", no_argument, NULL, 'S' }, - { "version", no_argument, NULL, 'v' }, - { 0, 0, 0, 0 } + { "debug", no_argument, NULL, 'd' }, + { "help", no_argument, NULL, 'H' }, + { "host", required_argument, NULL, 'h' }, + { "image", required_argument, NULL, 'i' }, + { "log", required_argument, NULL, 'l' }, + { "option", required_argument, NULL, 'o' }, + { "rid", required_argument, NULL, 'r' }, + { "sticky", no_argument, NULL, 'S' }, + { "version", no_argument, NULL, 'v' }, + { 0, 0, 0, 0 } }; -int main(int argc, char *argv[]) +int main( int argc, char *argv[] ) { char *server_address = NULL; char *image_Name = NULL; @@ -381,7 +381,7 @@ int main(int argc, char *argv[]) log_setConsoleTimestamps( true ); log_setFileMask( 65535 ); - newArgv = calloc( argc + 10, sizeof(char*) ); + newArgv = calloc( argc + 10, sizeof( char* ) ); newArgv[0] = argv[0]; newArgc = 1; @@ -394,7 +394,7 @@ int main(int argc, char *argv[]) image_Name = optarg; break; case 'r': - rid = (uint16_t)atoi(optarg); + rid = (uint16_t)atoi( optarg ); break; case 'o': newArgv[newArgc++] = "-o"; @@ -479,32 +479,32 @@ int main(int argc, char *argv[]) for ( int i = 0; i < newArgc; ++i ) { printf( " '%s'", newArgv[i] ); } - putchar('\n'); + putchar( '\n' ); clock_gettime( CLOCK_REALTIME, &startupTime ); owner = getuid(); // Fuse lowlevel loop - struct fuse_args args = FUSE_ARGS_INIT(newArgc, newArgv); + struct fuse_args args = FUSE_ARGS_INIT( newArgc, newArgv ); int fuse_err = 1; - if (fuse_parse_cmdline(&args, &mountpoint, NULL, NULL) != -1 && (ch = fuse_mount(mountpoint, &args)) != NULL) { + if ( fuse_parse_cmdline( &args, &mountpoint, NULL, NULL ) != -1 && ( ch = fuse_mount( mountpoint, &args ) ) != NULL ) { struct fuse_session *se; - se = fuse_lowlevel_new(&args, &image_oper, sizeof(image_oper), NULL); - if (se != NULL) { - if (fuse_set_signal_handlers(se) != -1) { - fuse_session_add_chan(se, ch); + se = fuse_lowlevel_new( &args, &image_oper, sizeof( image_oper ), NULL ); + if ( se != NULL ) { + if ( fuse_set_signal_handlers( se ) != -1 ) { + fuse_session_add_chan( se, ch ); //fuse_daemonize(foreground); - if (single_thread) fuse_err = fuse_session_loop(se); - else fuse_err = fuse_session_loop_mt(se); //MT produces errors (race conditions) in libfuse and didnt improve speed at all - fuse_remove_signal_handlers(se); - fuse_session_remove_chan(ch); + if ( single_thread ) fuse_err = fuse_session_loop( se ); + else fuse_err = fuse_session_loop_mt( se ); //MT produces errors (race conditions) in libfuse and didnt improve speed at all + fuse_remove_signal_handlers( se ); + fuse_session_remove_chan( ch ); } - fuse_session_destroy(se); + fuse_session_destroy( se ); } - fuse_unmount(mountpoint, ch); + fuse_unmount( mountpoint, ch ); } - fuse_opt_free_args(&args); - free(newArgv); - logadd( LOG_DEBUG1, "Terminating. FUSE REPLIED: %d\n", fuse_err); + fuse_opt_free_args( &args ); + free( newArgv ); + logadd( LOG_DEBUG1, "Terminating. FUSE REPLIED: %d\n", fuse_err ); return fuse_err; } -- cgit v1.2.3-55-g7522