From 011d4e6ff76c76fa7c49540282dee0bafef54fac Mon Sep 17 00:00:00 2001 From: Michael Scherle Date: Fri, 2 Nov 2018 05:13:09 -0700 Subject: cleaned up code and added improvements - cleaned up code - using fixed size type variables in file - using types.h fixup macros - replaced write/read with pwrite/pread (except for 2 occurrences) - added create fuse callback--- src/fuse/cow.c | 635 ++++++++++++++---------------------- src/fuse/cow.h | 23 +- src/fuse/cowMerger/src/Cow_Merger.c | 10 +- src/fuse/cowMerger/src/merger.c | 113 +++---- src/fuse/cowMerger/src/merger.h | 37 +++ src/fuse/main.c | 47 ++- src/types.h | 7 + 7 files changed, 391 insertions(+), 481 deletions(-) diff --git a/src/fuse/cow.c b/src/fuse/cow.c index 37345a1..bd61704 100644 --- a/src/fuse/cow.c +++ b/src/fuse/cow.c @@ -2,13 +2,10 @@ * cow.c * * Created on: 22.08.2018 - * Author: michael + * Author: Michael Scherle */ - #include "cow.h" - - - +#include "../types.h" #include #include #include @@ -19,21 +16,22 @@ #include #include #include +#include + #define ClearBit(A,k) ( A[(k/32)] &= ~(1 << (k%32)) ) #define TestBit(A,k) ( A[(k/32)] & (1 << (k%32)) ) #define SetBit(A,k) ( A[(k/32)] |= (1 << (k%32)) ) const unsigned int version = 1; -uint64_t MaxImageSizeInBytes=1099511627776; //1 Tebibyte in byte +int fh; +uint64_t MaxImageSizeInBytes = 1099511627776; //1 Tebibyte in byte off_t *filePointers; uint64_t imageSubBlockCount; size_t imageBlockCount; -int fh; -off_t dataStart; bool debug = true; uint64_t remoteImageSize; - +cow_metadata metadata; typedef struct { @@ -57,6 +55,7 @@ static void signalInit() signalPool[i] = NULL; } } + static inline dnbd3_signal_t *signalGet() { pthread_spin_lock( &sigLock ); @@ -71,6 +70,7 @@ static inline dnbd3_signal_t *signalGet() pthread_spin_unlock( &sigLock ); return signal_newBlocking(); } + static inline void signalPut(dnbd3_signal_t *signal) { pthread_spin_lock( &sigLock ); @@ -119,183 +119,164 @@ static cow_request* removeCowRequest(cow_requests_queue queue,cow_request *reque } -void writeImageSizeToFile(uint64_t size){ - lseek(fh,sizeof(unsigned int),SEEK_SET); - write(fh, &size, sizeof(uint64_t)); +void writeImageSizeToFile(uint64_t size) +{ + if(debug) { + printf("ImageSize Changed to %"PRIu64"\n", size); + } + size = net_order_64(size); + metadata.imageSize = size; + pwrite( fh, &metadata, sizeof(cow_metadata), 0 ); } -bool create_cow_file(char *cow_path, char *image_Name,uint64_t imageSize){ - - remoteImageSize =(size_t) imageSize; - if((fh = open (cow_path, O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR))==-1){ +bool cow_createFile(char *cow_path, char *image_Name, uint64_t imageSize) +{ + remoteImageSize = (size_t) imageSize; + if(( fh = open (cow_path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR) ) == -1 ){ puts( "Could not create COW File. Bye.\n" ); return false; } - write(fh,&version,sizeof(unsigned int)); - write(fh,&imageSize,sizeof(uint64_t)); - int nameLenght =(unsigned int) strlen(image_Name); - write(fh,&nameLenght,sizeof(unsigned int)); - write(fh,image_Name,sizeof(char)*strlen(image_Name)); - int pageSize = getpagesize(); - write(fh,&pageSize,sizeof(int)); - imageBlockCount = (MaxImageSizeInBytes/(1024*1024)); - - - off_t mmapStart = lseek(fh, 0L, SEEK_CUR); - + int nameLenght = (unsigned int) strlen( image_Name ); + metadata.version = version; + metadata.imageSize = imageSize; + metadata.nameLenght = nameLenght; + metadata.pageSize = pageSize; + fixup_cow_metadata(metadata); + write( fh, &metadata, sizeof(metadata)); + write( fh, image_Name, sizeof(char)*strlen( image_Name )); + imageBlockCount = ( MaxImageSizeInBytes / (1024 * 1024)); + off_t mmapStart = lseek( fh, 0L, SEEK_CUR); int maxPageSize = 8192; //compute next fitting multiple of getpagesize() - - - mmapStart= ((mmapStart+maxPageSize-1)/maxPageSize)*maxPageSize; - //write(fh,&mmapStart,sizeof(mmapStart)); - - - - dataStart = lseek(fh, mmapStart+imageBlockCount*sizeof(off_t), SEEK_CUR); - if(dataStart ==-1){ - close(fh); - printf("Error expanding file for mmap.Bye.\n "); - return false; - } - write(fh, "", 1); - dataStart = ((dataStart+pageSize-1)/pageSize)*pageSize; - - filePointers = mmap(NULL,imageBlockCount*sizeof(off_t), PROT_READ | PROT_WRITE, MAP_SHARED,fh, mmapStart); + mmapStart = (( mmapStart + maxPageSize - 1 ) / maxPageSize ) * maxPageSize; + pwrite( fh, "", 1, mmapStart + imageBlockCount * sizeof( uint64_t )); + filePointers = mmap( NULL, imageBlockCount * sizeof( uint64_t ), PROT_READ | PROT_WRITE, MAP_SHARED, fh, mmapStart ); if(filePointers == MAP_FAILED ){ - close(fh); - printf("Error creating mmap in COW File.\n%s\nBye.\n ", strerror(errno)); + close( fh ); + printf( "Error creating mmap in COW File.\n%s\nBye.\n ", strerror( errno ) ); return false; } - for(unsigned int i = 0; i < imageBlockCount;i++){ - filePointers[i]=0; + for( unsigned int i = 0; i < imageBlockCount; i++ ){ + filePointers[i] = 0; } - if(debug){ - printf("Creating COW File\n"); - printf("Version: %u\n",version); - printf("ImageName: %s\n",image_Name); - printf("Size: %ld\n", (long)imageSize); - printf("Blocks: %i\n",imageBlockCount); - printf("mmap start: %i\n",mmapStart); - printf("End: %lu\n",lseek(fh,0L,SEEK_CUR)); + if( debug ){ + printf( "Creating COW File\n" ); + printf( "Version: %u\n", version ); + printf( "ImageName: %s\n", image_Name ); + printf( "Size: %"PRIu64"\n", imageSize ); + printf( "Blocks: %"PRIu64"\n", imageBlockCount ); + printf( "mmap start: %"PRIu64"\n", mmapStart ); + printf( "End: %lu\n", lseek( fh, 0L, SEEK_CUR ) ); } signalInit(); - if(pthread_spin_init( &requestsQueueLock, PTHREAD_PROCESS_PRIVATE ) != 0){ - printf("Spinlock init failure"); + if( pthread_spin_init( &requestsQueueLock, PTHREAD_PROCESS_PRIVATE ) != 0 ){ + printf( "Spinlock init failure" ); } return true; } +uint64_t cow_loadFile( char *cow_path, uint64_t imageSize ) +{ + remoteImageSize = ( size_t ) imageSize; + if(( fh = open (cow_path, O_RDWR, S_IRUSR|S_IWUSR)) == -1 ){ + printf( "Could not open Cow File.\n" ); + return false; + } + read( fh, &metadata, sizeof( cow_metadata ) ); + fixup_cow_metadata( metadata ); + char *buffer = malloc(sizeof(char) * ( metadata.nameLenght + 1 ) ); + read( fh, buffer, metadata.nameLenght * sizeof( char ) ); + off_t mmapStart = lseek( fh, 0L, SEEK_CUR ); + int maxPageSize = 8192; + mmapStart = ( ( mmapStart + maxPageSize - 1) / maxPageSize) * maxPageSize; + if( debug ){ + printf( "Version: %u\n", metadata.version ); + printf( "länge: %i \n", metadata.nameLenght); + printf( "Image Name: %s\n", buffer ); + printf( "Size: %ld\n", (long) metadata.imageSize ); + printf( "pageSize: %i\n", metadata.pageSize ); + printf( "mmap start: %"PRIu64"\n", mmapStart ); - -bool load_cow_file(char *cow_path, uint64_t imageSize){ - remoteImageSize =(size_t) imageSize; - if((fh = open (cow_path, O_RDONLY))==-1){ - printf( "Could not open Cow File.\n" ); - return false; - } - unsigned int version ; - uint64_t size; - read(fh,&version,sizeof(unsigned int)); - read(fh,&size,sizeof(uint64_t)); - int l; - read(fh,&l,sizeof(int)); - - char *buffer = malloc(sizeof(char)*(l+1)); - read(fh,buffer,l*sizeof(char)); - buffer[l]='\0'; - - int pageSize; - read(fh,&pageSize,sizeof(int)); - if(debug){ - printf("Version: %u\n",version); - printf("länge: %i \n",l); - printf("Image Name: %s\n",buffer); - printf("Size: %ld\n", (long)size); - printf("pageSize: %i\n", (pageSize)); - } - free(buffer); - //TODO Image Validation - - - setImagesize(size); - off_t mmapStart = lseek(fh, 0L, SEEK_CUR); - int maxPageSize = 8192; - mmapStart= ((mmapStart+maxPageSize-1)/maxPageSize)*maxPageSize; - imageBlockCount = (MaxImageSizeInBytes/(1024*1024)); - filePointers = mmap(NULL,imageBlockCount*sizeof(off_t), PROT_READ, MAP_SHARED,fh, mmapStart); - if(filePointers == MAP_FAILED ){ - printf("Error creating mmap in COW File.\n%s\nBye.\n ", strerror(errno)); - close(fh); - return false; - } - signalInit(); - if(pthread_spin_init( &requestsQueueLock, PTHREAD_PROCESS_PRIVATE ) != 0){ - printf("Spinlock init failure"); - } - return true; + } + free( buffer ); + imageBlockCount = ( MaxImageSizeInBytes / (1024 * 1024) ); + filePointers = mmap( NULL, imageBlockCount * sizeof( uint64_t ), PROT_READ, MAP_SHARED, fh, mmapStart ); + if( filePointers == MAP_FAILED ){ + printf( "Error creating mmap in COW File.\n%s\nBye.\n ", strerror( errno ) ); + close( fh ); + return 0; + } + signalInit(); + if( pthread_spin_init( &requestsQueueLock, PTHREAD_PROCESS_PRIVATE ) != 0 ){ + printf( "Spinlock init failure" ); + } + return metadata.imageSize ; } - -bool createBigBlock(unsigned long id){ - +bool createBigBlock(unsigned long id) +{ //goto File end and then to next 4096 Block - off_t blockStart = lseek(fh,0, SEEK_END); - blockStart= ((blockStart+4096-1)/4096)*4096; - filePointers[id] = blockStart; + off_t blockStart = lseek( fh, 0, SEEK_END ); + blockStart = ( ( blockStart + 4096 - 1) / 4096) * 4096; + filePointers[id] = (uint64_t) blockStart; // go to next Page size - off_t currentFilePos= lseek(fh,blockStart, SEEK_SET); int blockState[8] = {0}; - write(fh,&blockState,sizeof(int)*8); + /* + if( debug ){ + printf("Creating BigBlock: %lu offset: %"PRIu64"\n", id, blockStart); + }*/ + pwrite( fh, &blockState, sizeof( int ) * 8, blockStart); // go to next Page size - currentFilePos = (((currentFilePos+sizeof(int)*8)+4096-1)/4096)*4096; - currentFilePos = lseek(fh,currentFilePos, SEEK_SET); - char data[256*4096] = {0}; - write(fh,&data,sizeof(char)*4096*256); + blockStart = ( ( ( blockStart + sizeof( int ) * 8 ) + 4096 - 1 ) / 4096 ) * 4096; + + char data[ 256 * 4096 ] = {0}; + pwrite( fh, &data, sizeof( char ) * 4096 * 256, blockStart); return true; } -void onClose(){ +void onClose() +{ close(fh); } -bool queckQueuesForDependencies(cow_request *request,cow_requests_queue queue){ +bool queckAndUpdateQueues(cow_request *request, cow_requests_queue queue) +{ bool foundDependencie = false; - if(queue.head !=NULL){ + if( queue.head != NULL ){ cow_request *iterator = NULL; for ( iterator = queue.head; iterator != NULL; iterator = iterator->next ) { - if(!(((request->end)offset)||(request->offset >(iterator->end)))){ + if( !( ( ( request->end ) < iterator->offset ) || ( request->offset > ( iterator->end ) ) ) ) { foundDependencie = true; - int i=0; - while(i<=6) + int i = 0; + while( i <= 6) { - if(i==6){ - printf("Error to much requests in queue"); + if( i == 6 ){ + printf( "Error to much requests in queue" ); break; - }else if(iterator->dependencies[i]==NULL){ + }else if( iterator->dependencies[i] == NULL ) { iterator->dependencies[i] = request; break; } i++; } - i=0; - while(i<=6) + i = 0; + while( i <= 6 ) { - if(i==6){ - printf("Error to much requests in queue"); + if( i == 6 ) { + printf( "Error to much requests in queue" ); break; - }else if(request->myDependencies[i]==NULL){ - request->myDependencies[i]=request; + } else if ( request->myDependencies[i] == NULL ) { + request->myDependencies[i] = request; break; } i++; @@ -306,56 +287,58 @@ bool queckQueuesForDependencies(cow_request *request,cow_requests_queue queue){ return foundDependencie; } -cow_request getAccess(off_t offset,size_t size){ +cow_request getAccess(off_t offset, size_t size) +{ cow_request request; - request.offset = (offset-(offset % 4096)); - request.end =(offset+(off_t)size)+( (offset+(off_t)size)% 4096)-1; + request.offset = ( offset - ( offset % 4096 ) ); + request.end = ( offset + (off_t) size) + + ( ( offset + (off_t)size ) % 4096) - 1; request.signal = signalGet(); - for (int i = 0; i < 6; i++){ + for ( int i = 0; i < 6; i++ ) { request.dependencies[i] = NULL; } - for (int i = 0; i < 6; i++){ + for ( int i = 0; i < 6; i++ ) { request.myDependencies[i] = NULL; } pthread_spin_lock( &requestsQueueLock ); - if(queckQueuesForDependencies(&request, cowRequestsactive)){ - queckQueuesForDependencies(&request, cowRequestsQueued); - enqueueCowRequest(cowRequestsQueued,&request); + if( queckAndUpdateQueues( &request, cowRequestsactive ) ) { + queckAndUpdateQueues( &request, cowRequestsQueued); + enqueueCowRequest(cowRequestsQueued, &request); pthread_spin_unlock( &requestsQueueLock ); int ret = -1; - while(ret<0){ + while( ret < 0 ){ int ret = signal_wait( request.signal, 5000 ); - if(ret<0){ - printf("Error Cow Request timed out"); + if( ret < 0 ) { + printf( "Error Cow Request timed out" ); } } - signalPut(request.signal); + signalPut( request.signal ); }else{ - enqueueCowRequest(cowRequestsactive,&request); + enqueueCowRequest( cowRequestsactive, &request ); pthread_spin_unlock( &requestsQueueLock ); } return request; } - -void closeAcccess(cow_request *request){ - pthread_spin_lock(&requestsQueueLock); - removeCowRequest(cowRequestsactive, request); - for (int i = 0; i< 6; i++){ +void closeAcccess(cow_request *request) +{ + pthread_spin_lock( &requestsQueueLock ); + removeCowRequest( cowRequestsactive, request ); + for ( int i = 0; i< 6; i++ ) { cow_request *otherRequest = request->dependencies[i]; - if(otherRequest != NULL){ + if( otherRequest != NULL ) { bool canStart = true; - for (int j = 0; j< 6; j++){ - if(otherRequest->myDependencies[j] == request){ + for ( int j = 0; j< 6; j++ ) { + if( otherRequest->myDependencies[j] == request ) { otherRequest->myDependencies[j] = NULL; - }else if(otherRequest->myDependencies[j] != NULL){ + } else if( otherRequest->myDependencies[j] != NULL ) { canStart=false; } } - if(canStart){ - removeCowRequest(cowRequestsQueued, otherRequest); - enqueueCowRequest(cowRequestsactive, otherRequest); - signal_call(otherRequest->signal); + if( canStart ) { + removeCowRequest( cowRequestsQueued, otherRequest ); + enqueueCowRequest( cowRequestsactive, otherRequest ); + signal_call( otherRequest->signal ); } } request->dependencies[i] = NULL; @@ -363,342 +346,228 @@ void closeAcccess(cow_request *request){ pthread_spin_unlock(&requestsQueueLock); } - -int write_cow(const char *data, size_t size, off_t offset) { - +int writeCow(const char *data, size_t size, off_t offset) +{ int writtenBytes = 0; size_t sizeToBigBlock = 0; off_t bigBlockOffset = 0; off_t bigBlockStart = 0; - unsigned long bigBlockStartId = (offset)/(4096*256); + unsigned long bigBlockStartId = offset / ( 4096 * 256 ); unsigned long bigBlockId = bigBlockStartId; - cow_request request = getAccess(offset,size); - while(writtenBytes < size){ - - - - bigBlockStart = (bigBlockId*(4096*256)); - bigBlockOffset =(offset+writtenBytes)- bigBlockStart; - - + cow_request request = getAccess( offset, size ); + while ( writtenBytes < (int) size ) { + bigBlockStart = ( bigBlockId * ( 4096 * 256 ) ); + bigBlockOffset =( offset + writtenBytes )- bigBlockStart; // how much i can write in this block //TODO CHECK THIS //sizeToBigBlock = 4096*256 - (bigBlockOffset -bigBlockStart); - sizeToBigBlock = 4096*256 - bigBlockOffset; - - if((size-writtenBytes)< sizeToBigBlock){ - sizeToBigBlock = size-writtenBytes; + sizeToBigBlock = 4096 * 256 - bigBlockOffset; + if( ( size - writtenBytes ) < sizeToBigBlock ) { + sizeToBigBlock = size - writtenBytes; } - - writtenBytes += writeToBigBlock(bigBlockId,data+writtenBytes,bigBlockOffset,sizeToBigBlock); - + writtenBytes += writeToBigBlock( bigBlockId, data + writtenBytes, bigBlockOffset, sizeToBigBlock ); bigBlockId++; } /////////////////////////////// - char *tmp = malloc(size); - cow_read(tmp, size, offset); - if(debug){ - if(strncmp(data, tmp,size) != 0){ - printf("Error\n"); - printf("%.*s", size, data); - printf("\n"); - printf("%.*s", size, tmp); + if( debug ){ + char *tmp = malloc( size ); + cow_read( tmp, size, offset ); + if( strncmp( data, tmp, size ) != 0 ) { + printf( "Error\n" ); + printf( "%.*s", size, data ); + printf( "\n"); + printf( "%.*s", size, tmp ); } + free(tmp); } - free(tmp); /////////////////////////////// - closeAcccess(&request); + closeAcccess( &request ); return writtenBytes; } - /* * Writes Data in a bigblock, offset reltive to start of bigblock * */ - - -int writeToBigBlock(unsigned long bigBlockId,const char *data, off_t offset,size_t size){ +int writeToBigBlock(unsigned long bigBlockId, const char *data, off_t offset,size_t size) +{ int writtenBytes = 0; - if(filePointers[bigBlockId]==0){ - createBigBlock(bigBlockId); + if( filePointers[bigBlockId] == 0 ) { + createBigBlock( bigBlockId ); } - int firstSmallBlock = getSmallBlockId(offset); - int lastSmallBlock = getSmallBlockId(offset+size-1); - if(firstSmallBlock>255||lastSmallBlock>255){ - printf("Error SmallBLock > 255"); + int firstSmallBlock = getSmallBlockId( offset ); + int lastSmallBlock = getSmallBlockId( offset + size - 1) ; + if( firstSmallBlock > 255 || lastSmallBlock > 255 ) { + printf( "Error SmallBLock > 255" ); } - lseek(fh,filePointers[bigBlockId],SEEK_SET); int blockState[8] ; - read(fh,&blockState,sizeof(int)*8); + pread( fh, &blockState, sizeof( int ) * 8, ( (off_t) filePointers[bigBlockId] ) ); //If not on Block border and don't have this block, get the data. - if((offset % 4096 !=0 )&&(!TestBit(blockState,firstSmallBlock))){ + if( ( offset % 4096 != 0 ) && ( !TestBit( blockState, firstSmallBlock ) ) ) { size_t sizeToPrepend = offset % 4096; - lseek(fh,filePointers[bigBlockId]+4096+offset-sizeToPrepend,SEEK_SET); + //lseek( fh, filePointers[bigBlockId] + 4096 + offset - sizeToPrepend, SEEK_SET ); char *startData = calloc(sizeToPrepend,1); - off_t offsetToPrepend =offset+(bigBlockId*(4096*256))-sizeToPrepend; + off_t offsetToPrepend = offset + ( bigBlockId * ( 4096 * 256 ) ) - sizeToPrepend; // TODO CHECK THIS //offsetToPrepend = offsetToPrepend - (offsetToPrepend % 4096); - if((((uint64_t)offsetToPrepend)+((uint64_t)sizeToPrepend)) > remoteImageSize ){ + if( ( ( ( uint64_t ) offsetToPrepend ) + ( ( uint64_t ) sizeToPrepend) ) > remoteImageSize ) { sizeToPrepend = remoteImageSize-offsetToPrepend; } - if(((uint64_t)offsetToPrepend)< remoteImageSize){ - image_read_internal(startData,sizeToPrepend, offsetToPrepend); + if( ( ( uint64_t ) offsetToPrepend ) < remoteImageSize ) { + imageReadInternal( startData, sizeToPrepend, offsetToPrepend ); } - write(fh,startData,(offset % 4096)); - free(startData); + pwrite( fh, startData, ( offset % 4096 ),( (off_t) filePointers[bigBlockId] + 4096 + offset - sizeToPrepend ) ); + free( startData ); + + } + - } else{ - lseek(fh,filePointers[bigBlockId]+4096+offset,SEEK_SET); + int written = ( int ) pwrite( fh, data, size, ( (off_t) filePointers[bigBlockId] + 4096 + offset ) ); + if( written < 0 ){ + printf ( "Error on writing to Cow File, size: %zu offset: %"PRIu64"\n Error: %s \n", size, filePointers[bigBlockId] + 4096 + offset, strerror( errno ) ); + }else { + writtenBytes += written; } - writtenBytes +=(int)write(fh,data,size); //If not on Block border and don't have this block, get the data. - if(((offset+size) % 4096 !=0 )&&(!TestBit(blockState,lastSmallBlock))){ + if( ( ( offset + size ) % 4096 != 0 ) && ( !TestBit( blockState, lastSmallBlock ) ) ) { - size_t sizeToAppend= 4096-((((size_t)offset)+size)% 4096); + size_t sizeToAppend= 4096 - ( ( ( ( size_t ) offset ) + size ) % 4096 ); //TODO TEST -1? - off_t offsetToAppend = bigBlockId*256*4096+((off_t)offset)+size; - - char *startData = calloc(sizeToAppend,1); + off_t offsetToAppend = bigBlockId * 256 * 4096 +( ( off_t ) offset ) + size; + char *startData = calloc( sizeToAppend, 1 ); - if((((size_t)offsetToAppend)+sizeToAppend) > remoteImageSize ){ - sizeToAppend = remoteImageSize-offsetToAppend; + if( ( ( ( size_t ) offsetToAppend ) + sizeToAppend ) > remoteImageSize ) { + sizeToAppend = remoteImageSize - offsetToAppend; } - if(((size_t)offsetToAppend)< remoteImageSize){ - image_read_internal(startData,sizeToAppend, offsetToAppend); + if( ( ( size_t ) offsetToAppend) < remoteImageSize ) { + imageReadInternal( startData, sizeToAppend, offsetToAppend ); } //lseek(fh,filePointers[bigBlockId]+4096+offset,SEEK_SET); - write(fh,startData,(4096-((((size_t)offset)+size)% 4096))); - free(startData); + pwrite( fh, startData, ( 4096 -( ( ( ( size_t ) offset ) + size ) % 4096 ) ), ( (off_t) filePointers[bigBlockId] + 4096 + offset + size ) ); + free( startData ); } - for (long i = firstSmallBlock; i <= lastSmallBlock;i++ ){ - SetBit(blockState,i); + for ( long i = firstSmallBlock; i <= lastSmallBlock; i++ ) { + SetBit( blockState, i ); } - lseek(fh,filePointers[bigBlockId],SEEK_SET); - write(fh,&blockState,sizeof(int)*8); + pwrite( fh, &blockState, sizeof( int32_t ) * 8, ( (off_t) filePointers[bigBlockId] ) ); return writtenBytes; } -int getSmallBlockId(off_t offset){ +int getSmallBlockId(off_t offset) +{ - return (int)(offset/4096) % 256; + return ( int ) ( offset / 4096 ) % 256; } + int cow_read(char *buf, size_t size, off_t offset) { - - unsigned long bigBlockStartId = (offset)/(4096*256); + unsigned long bigBlockStartId = offset / ( 4096 * 256 ); //TODO CHECK THIS - unsigned long bigBlockEndId = (offset+size-1)/(4096*256); + unsigned long bigBlockEndId = ( offset + size - 1) / ( 4096 * 256 ); unsigned long bigBlockId = bigBlockStartId; - cow_request request =getAccess(offset,size); - size_t bigBlockStart = (bigBlockId*(4096*256)); - size_t bigBlockOffset =offset- bigBlockStart; + cow_request request = getAccess( offset, size ); + size_t bigBlockStart = ( bigBlockId * ( 4096 * 256 ) ); + size_t bigBlockOffset = offset- bigBlockStart; // how much i can write from this block //TODO IS THIS RIGHT? - size_t sizeToBigBlock = ((4096*256) - (bigBlockOffset)); - if(sizeToBigBlock>size){ + size_t sizeToBigBlock = ( ( 4096 * 256 ) - bigBlockOffset ); + if( sizeToBigBlock > size) { sizeToBigBlock = size; } - int bytesRead = readBigBlock(bigBlockStartId, buf,sizeToBigBlock, bigBlockOffset); - if(bigBlockStartId != bigBlockEndId && (size-sizeToBigBlock)>0){ + int bytesRead = readBigBlock( bigBlockStartId, buf,sizeToBigBlock, bigBlockOffset ); + if( bigBlockStartId != bigBlockEndId && ( size - sizeToBigBlock ) > 0 ) { - bytesRead +=readBigBlock(bigBlockEndId, buf+sizeToBigBlock,(size-sizeToBigBlock), 0); + bytesRead += readBigBlock( bigBlockEndId, buf + sizeToBigBlock, ( size - sizeToBigBlock ), 0 ); } - closeAcccess(&request); + closeAcccess( &request ); return bytesRead; } - -int readBigBlock(long bigBlockId ,char *buf,size_t size, off_t offset){ +int readBigBlock(long bigBlockId, char *buf, size_t size, off_t offset) +{ // If block isn't local - if(filePointers[bigBlockId] == 0){ - return image_read_internal(buf, size, (offset+(bigBlockId*(4096*256)))); + if(filePointers[bigBlockId] == 0) { + return imageReadInternal( buf, size, ( offset + ( bigBlockId * ( 4096 * 256 ) ) ) ); } int blockState[8]; - lseek(fh,filePointers[bigBlockId],SEEK_SET); - read(fh,&blockState,sizeof(int)*8); - int block = getSmallBlockId(offset); - int endBlock = getSmallBlockId(offset+size-1); + pread( fh, &blockState, sizeof( int ) * 8, ( (off_t) filePointers[bigBlockId] )); + int block = getSmallBlockId( offset ); + int endBlock = getSmallBlockId( offset + size - 1 ); int startBlock; char *curBuf = buf; size_t readBytes = 0; - while(readBytes < size){ - startBlock=block; - if(!TestBit(blockState,block)){ - while(!TestBit(blockState,block)&& block!= endBlock){ + while( readBytes < size ) { + startBlock = block; + if( !TestBit( blockState, block ) ) { + while( !TestBit( blockState, block ) && block!= endBlock ) { block++; - if(block >255){ - printf("ERROR SmallBlack id > 255"); + if( block > 255 ) { + printf( "ERROR SmallBlack id > 255" ); } } - off_t startOffset = offset+readBytes; - size_t sizeToRead = ((block+1)*4096)-startOffset; - if(sizeToRead>size-readBytes){ - sizeToRead= size-readBytes; + off_t startOffset = offset + readBytes; + size_t sizeToRead = ( ( block + 1 ) * 4096 ) - startOffset; + if( sizeToRead > size - readBytes ) { + sizeToRead = size - readBytes; } size_t sizeToRemoteRead = sizeToRead; - if(((uint64_t)sizeToRead)+((uint64_t)startOffset)>remoteImageSize){ - if(((uint64_t)startOffset)>remoteImageSize){ + if( ( ( uint64_t ) sizeToRead ) + ( ( uint64_t ) startOffset ) > remoteImageSize ) { + if( ( ( uint64_t ) startOffset ) > remoteImageSize ) { sizeToRemoteRead = 0; - }else{ - sizeToRemoteRead =((size_t)remoteImageSize)-startOffset; + } else { + sizeToRemoteRead = ( ( size_t ) remoteImageSize ) - startOffset; } } - startOffset = startOffset+(bigBlockId*4096*256); - if(sizeToRemoteRead > 0){ - readBytes +=image_read_internal(curBuf, sizeToRemoteRead,startOffset); + startOffset = startOffset + ( bigBlockId * 4096 * 256 ); + if( sizeToRemoteRead > 0 ) { + readBytes += imageReadInternal( curBuf, sizeToRemoteRead, startOffset ); } - if(readBytes < sizeToRead){ - for(int i =((int)readBytes); readBytes < sizeToRead; i++){ + if( readBytes < sizeToRead ) { + for( int i = ( ( int )readBytes ); readBytes < sizeToRead; i++ ) { curBuf[i] = 0; readBytes++; } } - curBuf +=sizeToRead; - /* - off_t startOffset = startBlock*4096; - if(startOffset < offset){ - startOffset = offset; - } - - - size_t sizeToRead = ((block+1)*4096)-startOffset; - if(sizeToRead>size-readBytes){ - sizeToRead= size-readBytes; - } - size_t sizeToRemoteRead = sizeToRead; - - - if(((uint64_t)sizeToRead)+((uint64_t)startOffset)>remoteImageSize){ - if(((uint64_t)startOffset)>remoteImageSize){ - sizeToRemoteRead = 0; - }else{ - sizeToRemoteRead =((size_t)remoteImageSize)-startOffset; - } - } - //request data over network - - //TODO Check if offset compution is correct - startOffset = startOffset+(bigBlockId*4096*256); - if(sizeToRemoteRead > 0){ - readBytes +=image_read_internal(curBuf, sizeToRemoteRead,startOffset); - } - - // If on File End fill up rest with 0 - if(((uint64_t)sizeToRead)+((uint64_t)startOffset)>remoteImageSize){ - for(size_t i = 0; i<(sizeToRead-sizeToRemoteRead); i++){ - curBuf[i] = 0; - } - readBytes+=(sizeToRead-sizeToRemoteRead); - } - - - curBuf +=sizeToRead; - */ - - }else{ - while(TestBit(blockState,block)&& block!= endBlock){ + curBuf += sizeToRead; + } else { + while( TestBit( blockState, block ) && block != endBlock) { block++; - if(block >255){ - printf("ERROR SmallBlack id > 255"); + if( block > 255 ) { + printf( "ERROR SmallBlack id > 255" ); } } off_t startOffset = offset+readBytes; - size_t sizeToRead = ((block+1)*4096)-startOffset; - if(sizeToRead>size-readBytes){ - sizeToRead= size-readBytes; - } - //read Data local - lseek(fh,filePointers[bigBlockId]+4096+startOffset,SEEK_SET); - readBytes += read(fh,curBuf,sizeToRead); - curBuf +=readBytes; - /* - off_t startOffset = startBlock*4096; - if(startOffset < offset){ - startOffset = offset; - } - size_t sizeToRead = ((block+1)*4096)-startOffset; - if(sizeToRead>size-readBytes){ - sizeToRead= size-readBytes; + size_t sizeToRead = ( ( block + 1 ) * 4096) - startOffset; + if( sizeToRead > size - readBytes ) { + sizeToRead = size - readBytes; } //read Data local - lseek(fh,filePointers[bigBlockId]+4096+startOffset,SEEK_SET); - readBytes += read(fh,curBuf,sizeToRead); - curBuf +=readBytes; - */ - + readBytes += pread(fh, curBuf, sizeToRead, ( (off_t) filePointers[bigBlockId] + 4096 + startOffset ) ); + curBuf += readBytes; } } return (int) readBytes; } -/* -void test(){ - create_cow_file("cow.bin","sampleImage",10000000); - printf("====Test=====\n"); - unsigned int versionAfterRead ; - lseek(fh,0, SEEK_SET); - read(fh,&versionAfterRead,sizeof(unsigned int)); - - unsigned int l; - read(fh,&l,sizeof(unsigned int)); - - char *buffer = malloc(sizeof(char)*(l+1)); - read(fh,buffer,l*sizeof(char)); - buffer[l]='\0'; - uint64_t size; - read(fh,&size,sizeof(uint64_t)); - int pageSize; - read(fh,&pageSize,sizeof(int)); - - - - printf("Version: %u\n",versionAfterRead); - printf("länge: %i \n",l); - printf("Image Name: %s\n",buffer); - printf("Size: %ld\n", (long)size); - printf("pageSize: %i\n", pageSize); - free(buffer); - - - //int imageSubBlockCount = (int)( size + 4095 ) / 4096; - //int imageBlockCount= (imageSubBlockCount+255)/256; - - //for(int i = 0; i < imageBlockCount;i++){ - // printf("Pointer: %i Value: %ld \n",i, (long)filePointers[i]); - //} - - char *smpledata = "1111111"; - write_cow(smpledata, 7, 310); - createBigBlock(0); - createBigBlock(5); - createBigBlock(2); - for(int i = 0; i < imageBlockCount;i++){ - printf("Pointer: %i Value: %ld \n",i, (long)filePointers[i]); - } - puts("success"); - onClose(); -} -*/ diff --git a/src/fuse/cow.h b/src/fuse/cow.h index 298ce4e..7f2c6bb 100644 --- a/src/fuse/cow.h +++ b/src/fuse/cow.h @@ -2,7 +2,7 @@ * cow.h * * Created on: 22.08.2018 - * Author: michael + * Author: Michael Scherle */ #ifndef COW_H_ @@ -22,20 +22,29 @@ typedef struct cow_request{ struct cow_request *dependencies[6]; struct cow_request *myDependencies[6]; dnbd3_signal_t* signal; -}cow_request; +} cow_request; -bool create_cow_file(char *cow_path, char *image_Name,uint64_t imageSize); +typedef struct { + uint32_t version; + uint64_t imageSize; + uint32_t nameLenght; + uint32_t pageSize; +} cow_metadata; + + +bool cow_createFile(char *cow_path, char *image_Name,uint64_t imageSize); bool createBigBlock(unsigned long id); +int cow_read(char *buf, size_t size, off_t offset); +uint64_t cow_loadFile( char *cow_path, uint64_t imageSize ); + +int readBigBlock(long bigBlockId ,char *buf,size_t size, off_t offset); void onClose(); cow_request getAccess(off_t offset,size_t size); void closeAcccess(cow_request *request); int write_cow(const char *data, size_t size, off_t offset); - int writeToBigBlock(unsigned long bigBlockId,const char *data, off_t offset,size_t size); int getSmallBlockId(off_t offset); -int cow_read(char *buf, size_t size, off_t offset); -int readBigBlock(long bigBlockId ,char *buf,size_t size, off_t offset); -void test(); +void test(); #endif /* COW_H_ */ diff --git a/src/fuse/cowMerger/src/Cow_Merger.c b/src/fuse/cowMerger/src/Cow_Merger.c index b73cadc..f7d35aa 100644 --- a/src/fuse/cowMerger/src/Cow_Merger.c +++ b/src/fuse/cowMerger/src/Cow_Merger.c @@ -1,16 +1,20 @@ #include #include #include "merger.h" -int main(int argc, char *argv[]) { +#include +int main(int argc, char *argv[]) { if(argc != 3){ printf("Error, Check your Command Line Arguments.\nExample: ./Cow_Merger \n"); return EXIT_SUCCESS; } - char *imageFilePath = argv[1]; char *cowFilePath = argv[2]; - + clock_t t; + t = clock(); merger(imageFilePath, cowFilePath); + t = clock() - t; + double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds + printf("merger took %f seconds to execute \n", time_taken); return EXIT_SUCCESS; } diff --git a/src/fuse/cowMerger/src/merger.c b/src/fuse/cowMerger/src/merger.c index 17926f7..3f4aa89 100644 --- a/src/fuse/cowMerger/src/merger.c +++ b/src/fuse/cowMerger/src/merger.c @@ -1,4 +1,5 @@ #include "merger.h" +#include #define TestBit(A,k) ( A[(k/32)] & (1 << (k%32)) ) int fhImage; int fhCow; @@ -6,102 +7,88 @@ off_t *filePointers; uint64_t imageBlockCount; uint64_t size; -bool merger(char *imageFilePath, char *cowFilePath){ - - if(!loadFiles(imageFilePath, cowFilePath)){ +bool merger( char *imageFilePath, char *cowFilePath ) +{ + if( !loadFiles( imageFilePath, cowFilePath ) ) { return false; } - if (merge()){ + if ( merge() ) { printf("File Merged"); return true; } return false; } - - - -bool loadFiles(char *imageFilePath, char *cowFilePath){ - - if((fhImage = open (imageFilePath, O_RDWR))==-1){ +bool loadFiles(char *imageFilePath, char *cowFilePath) +{ + if ( ( fhImage = open ( imageFilePath, O_RDWR ) ) == -1 ) { printf( "Could not open Image.\n" ); return false; } - if((fhCow = open (cowFilePath, O_RDONLY))==-1){ + if ( ( fhCow = open ( cowFilePath, O_RDONLY ) ) == -1 ) { printf( "Could not open Cow File.\n" ); - close(fhImage); + close( fhImage ); return false; } - unsigned int version ; - - read(fhCow,&version,sizeof(unsigned int)); - read(fhCow,&size,sizeof(uint64_t)); - int l; - read(fhCow,&l,sizeof(int)); - - char *buffer = malloc(sizeof(char)*(l+1)); - read(fhCow,buffer,l*sizeof(char)); - buffer[l]='\0'; - - int pageSize; - read(fhCow,&pageSize,sizeof(int)); - printf("Version: %u\n",version); - printf("länge: %i \n",l); - printf("Image Name: %s\n",buffer); - printf("Size: %ld\n", (long)size); - printf("pageSize: %i\n", (pageSize)); - free(buffer); + cow_metadata metadata; + read( fhCow, &metadata, sizeof( cow_metadata ) ); + fixup_cow_metadata( metadata ); + char *buffer = malloc(sizeof(char) * ( metadata.nameLenght + 1 ) ); + read( fhCow, buffer, metadata.nameLenght * sizeof( char ) ); + size = metadata.imageSize; + printf( "Version: %u\n",metadata.version ); + printf( "Länge: %i \n", metadata.nameLenght ); + printf( "Image Name: %s\n", buffer ); + printf( "Size: %"PRIu64"\n", size ); + printf( "pageSize: %i\n", metadata.pageSize ); + free( buffer ); //TODO Image Validation - imageBlockCount = ( size + (4096*256)-1 ) / (4096*256); - - off_t mmapStart = lseek(fhCow, 0L, SEEK_CUR); + off_t mmapStart = lseek( fhCow, 0L, SEEK_CUR ); int maxPageSize = 8192; - mmapStart= ((mmapStart+maxPageSize-1)/maxPageSize)*maxPageSize; - filePointers = mmap(NULL,imageBlockCount*sizeof(off_t), PROT_READ, MAP_SHARED,fhCow, mmapStart); + mmapStart = ( ( mmapStart + maxPageSize - 1) / maxPageSize ) * maxPageSize; + printf( "mmapStart: %"PRIu64"\n", mmapStart ); + filePointers = mmap( NULL, imageBlockCount * sizeof(uint64_t), PROT_READ, MAP_SHARED, fhCow, mmapStart); if(filePointers == MAP_FAILED ){ - printf("Error creating mmap in COW File.\n%s\nBye.\n ", strerror(errno)); - close(fhCow); - close(fhImage); + printf("Error creating mmap in COW File.\n%s\nBye.\n ", strerror(errno) ); + close( fhCow ); + close( fhImage ); return false; } - return true; } - -bool merge(){ +bool merge() +{ uint64_t mergedBlocks = 0; - for(uint64_t i = 0; i < imageBlockCount;i++){ - printf("Merged %llu of %llu Blocks \n",i,imageBlockCount); - off_t pointer =(off_t)filePointers[i]; - if(pointer !=0){ + for( uint64_t i = 0; i < imageBlockCount; i++ ) { + printf( "Merged %"PRIu64" of %"PRIu64" Blocks \n", i, imageBlockCount ); + off_t pointer = (off_t) filePointers[i]; + if( pointer != 0) { int blockState[8]; - lseek(fhCow,filePointers[i],SEEK_SET); - read(fhCow,&blockState,sizeof(int)*8); - for(int j = 0; j < 256;j++){ - if(TestBit(blockState,j)){ - lseek(fhCow,filePointers[i]+(4096*(j+1)),SEEK_SET); + lseek( fhCow, filePointers[i], SEEK_SET ); + read( fhCow, &blockState, sizeof(int) * 8 ); + for( int j = 0; j < 256; j++ ) { + if( TestBit( blockState, j ) ) { + lseek( fhCow, ((off_t) filePointers[i] + ( 4096 * ( j + 1) ) ), SEEK_SET ); char buff[4096] = {0}; - if(read(fhCow, &buff, 4096) < 0){ - printf("Error on reading Cow File. BigBlock: %llu\n",i); + if( read(fhCow, &buff, 4096) < 0) { + printf("Error on reading Cow File." + " BigBlock: %"PRIu64"n", i ); return false; } - lseek(fhImage,(i*4096*256)+(4096*j),SEEK_SET); - if(write(fhImage, &buff, 4096) < 0){ - printf("Error on writing to File. BigBlock: %llu\n",i); + lseek( fhImage, ( i * 4096 * 256) + ( 4096 * j ), SEEK_SET ); + if( write( fhImage, &buff, 4096 ) < 0) { + printf("Error on writing to File." + " BigBlock: %"PRIu64"\n", i ); return false; } - + mergedBlocks++; } } - mergedBlocks++; } } - printf("Merged %llu Blocks\n",mergedBlocks); - ftruncate(fhImage, size); + printf( "Merged %"PRIu64" Blocks\n", mergedBlocks ); + ftruncate( fhImage, size); return true; } - - - diff --git a/src/fuse/cowMerger/src/merger.h b/src/fuse/cowMerger/src/merger.h index c7d256d..c665df7 100644 --- a/src/fuse/cowMerger/src/merger.h +++ b/src/fuse/cowMerger/src/merger.h @@ -13,3 +13,40 @@ bool merger(char *imageFilePath, char *cowFilePath); bool loadFiles(char *imageFilePath, char *cowFilePath); bool merge(); + +typedef struct { + uint32_t version; + uint64_t imageSize; + uint32_t nameLenght; + uint32_t pageSize; +}cow_metadata; + +#if defined(__BIG_ENDIAN__) || (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN) || (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +static const uint16_t dnbd3_packet_magic = (0x73 << 8) | (0x72); +// Flip bytes around on big endian when putting stuff on the net +#define net_order_64(a) ((uint64_t)((((a) & 0xFFull) << 56) | (((a) & 0xFF00ull) << 40) | (((a) & 0xFF0000ull) << 24) | (((a) & 0xFF000000ull) << 8) | (((a) & 0xFF00000000ull) >> 8) | (((a) & 0xFF0000000000ull) >> 24) | (((a) & 0xFF000000000000ull) >> 40) | (((a) & 0xFF00000000000000ull) >> 56))) +#define net_order_32(a) ((uint32_t)((((a) & (uint32_t)0xFF) << 24) | (((a) & (uint32_t)0xFF00) << 8) | (((a) & (uint32_t)0xFF0000) >> 8) | (((a) & (uint32_t)0xFF000000) >> 24))) +#define net_order_16(a) ((uint16_t)((((a) & (uint16_t)0xFF) << 8) | (((a) & (uint16_t)0xFF00) >> 8))) +#define fixup_cow_metadata(a) do { \ + (a).version = net_order_32((a).version); \ + (a).imageSize = net_order_64((a).imageSize); \ + (a).nameLenght = net_order_32((a).nameLenght); \ + (a).pageSize = net_order_32((a).pageSize); \ +} while (0) +#define ENDIAN_MODE "Big Endian" +#ifndef BIG_ENDIAN +#define BIG_ENDIAN +#endif +#elif defined(__LITTLE_ENDIAN__) || (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN) || (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || defined(__i386__) || defined(__i386) || defined(__x86_64) +// Make little endian our network byte order as probably 99.999% of machines this will be used on are LE +#define net_order_64(a) (a) +#define net_order_32(a) (a) +#define net_order_16(a) (a) +#define fixup_cow_metadata(a) while(0) +#define ENDIAN_MODE "Little Endian" +#ifndef LITTLE_ENDIAN +#define LITTLE_ENDIAN +#endif +#else +#error "Unknown Endianness" +#endif diff --git a/src/fuse/main.c b/src/fuse/main.c index 5c2db34..ee52695 100755 --- a/src/fuse/main.c +++ b/src/fuse/main.c @@ -162,13 +162,13 @@ static int image_write(const char *path, char *buf, size_t size, off_t offset, s if ( path[1] == STATS_PATH[1] ) { return -EIO; } - if((((size_t)offset)+size) > imageSize){ - imageSize = ((size_t)offset)+size; - writeImageSizeToFile((uint64_t)size); + if( ( ( ( size_t ) offset ) + size ) > imageSize ){ + imageSize = ( ( size_t ) offset ) + size; + writeImageSizeToFile( imageSize ); } - return write_cow(buf,size, offset); + return writeCow(buf,size, offset); } -int image_read_internal(char *buf,size_t size, off_t offset){ +int imageReadInternal(char *buf,size_t size, off_t offset){ dnbd3_async_t request; request.buffer = buf; request.length = (uint32_t)size; @@ -232,7 +232,7 @@ static int image_read(const char *path, char *buf, size_t size, off_t offset, st if(useCow){ return cow_read(buf, size, offset); } else { - return image_read_internal(buf,size,offset); + return imageReadInternal(buf,size,offset); } } @@ -279,15 +279,7 @@ static void image_destroy(void *private_data UNUSED) } -static int image_truncate(const char *path, off_t size, struct fuse_file_info *fi UNUSED){ - if ( strcmp( path, IMAGE_PATH ) == 0 ) { - imageSize=size; - writeImageSizeToFile((uint64_t)size); - return 0; - } - return -1; -} -static int image_ftruncate(const char *path, off_t size, struct fuse_file_info *fi UNUSED){ +static int image_truncate(const char *path, off_t size, struct fuse_file_info *fi UNUSED) { if ( strcmp( path, IMAGE_PATH ) == 0 ) { imageSize=size; writeImageSizeToFile((uint64_t)size); @@ -295,13 +287,16 @@ static int image_ftruncate(const char *path, off_t size, struct fuse_file_info * } return -1; } -static int image_flush(const char *path, struct fuse_file_info *fi UNUSED){ + +static int image_flush(const char *path UNUSED, struct fuse_file_info *fi UNUSED) { return 0; } -static int image_release(const char *path, struct fuse_file_info *fi UNUSED){ +static int image_release(const char *path UNUSED, struct fuse_file_info *fi UNUSED) { return 0; } - +static int image_create(const char *path UNUSED, mode_t mode UNUSED, struct fuse_file_info *fi UNUSED){ + return -EPERM; +} @@ -322,16 +317,14 @@ static struct fuse_operations image_oper_cow = { .read = image_read, .init = image_init, .destroy = image_destroy, - .ftruncate = image_ftruncate, .flush = image_flush, .release = image_release, .write = image_write, - .truncate =image_truncate, + .truncate = image_truncate, + .create = image_create, }; -void setImagesize(uint64_t size){ - imageSize = size; -} + static void printVersion() { @@ -496,12 +489,16 @@ int main(int argc, char *argv[]) printf("Using Cow"); if(loadCow){ printf("Loading Cow"); - if(!load_cow_file(cow_path, imageSize)){ + uint64_t cowSize = cow_loadFile(cow_path, imageSize); + + if(cowSize == 0){ logadd( LOG_ERROR, "Could not load COW FIle. Bye.\n" ); return EXIT_FAILURE; + } else{ + imageSize = cowSize; } } else { - if(!create_cow_file(cow_path, image_Name, imageSize)){ + if(!cow_createFile(cow_path, image_Name, imageSize)){ logadd( LOG_ERROR, "Could not create COW FIle. Bye.\n" ); return EXIT_FAILURE; } diff --git a/src/types.h b/src/types.h index ec37d9b..9b7160e 100644 --- a/src/types.h +++ b/src/types.h @@ -91,6 +91,12 @@ static const uint16_t dnbd3_packet_magic = (0x73 << 8) | (0x72); (a).cmd = net_order_16((a).cmd); \ (a).size = net_order_32((a).size); \ } while (0) +#define fixup_cow_metadata(a) do { \ + (a).version = net_order_32((a).version); \ + (a).imageSize = net_order_64((a).imageSize); \ + (a).nameLenght = net_order_32((a).nameLenght); \ + (a).pageSize = net_order_32((a).pageSize); \ +} while (0) #define ENDIAN_MODE "Big Endian" #ifndef BIG_ENDIAN #define BIG_ENDIAN @@ -103,6 +109,7 @@ static const uint16_t dnbd3_packet_magic = (0x73) | (0x72 << 8); #define net_order_16(a) (a) #define fixup_request(a) while(0) #define fixup_reply(a) while(0) +#define fixup_cow_metadata(a) while(0) #define ENDIAN_MODE "Little Endian" #ifndef LITTLE_ENDIAN #define LITTLE_ENDIAN -- cgit v1.2.3-55-g7522