diff options
author | Simon Rettberg | 2013-08-01 19:35:48 +0200 |
---|---|---|
committer | Simon Rettberg | 2013-08-01 19:35:48 +0200 |
commit | e7ad62c1b1627f7bab2524a4c30f1833f6b6767d (patch) | |
tree | 090473456331c3fa25eafc2db60e4c2808d808a9 /src/server/image.c | |
parent | [SERVER] Add command line options to create empty image of certain size with ... (diff) | |
download | dnbd3-e7ad62c1b1627f7bab2524a4c30f1833f6b6767d.tar.gz dnbd3-e7ad62c1b1627f7bab2524a4c30f1833f6b6767d.tar.xz dnbd3-e7ad62c1b1627f7bab2524a4c30f1833f6b6767d.zip |
[SERVER] Fix create_image() by adding fallback solutions for fallocate()
Diffstat (limited to 'src/server/image.c')
-rw-r--r-- | src/server/image.c | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/src/server/image.c b/src/server/image.c index 556e83b..6b6fb99 100644 --- a/src/server/image.c +++ b/src/server/image.c @@ -14,7 +14,6 @@ #include <dirent.h> #include <zlib.h> #include <inttypes.h> -#include <fcntl.h> // ########################################## @@ -555,7 +554,7 @@ int image_create(char *image, int revision, uint64_t size) assert( image != NULL ); assert( size >= DNBD3_BLOCK_SIZE ); if ( revision <= 0 ) { - printf( "[ERROR] revision id invalid: %d\n", revision ); + memlogf( "[ERROR] revision id invalid: %d", revision ); return FALSE; } const int PATHLEN = 2000; @@ -571,7 +570,7 @@ int image_create(char *image, int revision, uint64_t size) snprintf( path, PATHLEN, "%s/%s.r%d", _basePath, image, revision ); } if ( file_exists( path ) ) { - printf( "[ERROR] Image %s with rid %d already exists!\n", image, revision ); + memlogf( "[ERROR] Image %s with rid %d already exists!", image, revision ); return FALSE; } snprintf( cache, PATHLEN, "%s.map", path ); @@ -579,26 +578,26 @@ int image_create(char *image, int revision, uint64_t size) const int mapsize = IMGSIZE_TO_MAPBYTES(size); // Write files int fdImage = -1, fdCache = -1; - fdImage = open( path, O_WRONLY | O_TRUNC, 0640 ); - fdCache = open( cache, O_WRONLY | O_TRUNC, 0640 ); + fdImage = open( path, O_RDWR | O_TRUNC | O_CREAT, 0640 ); + fdCache = open( cache, O_RDWR | O_TRUNC | O_CREAT, 0640 ); if ( fdImage < 0 ) { - printf( "[ERROR] Could not open %s for writing.\n", path ); + memlogf( "[ERROR] Could not open %s for writing.", path ); goto failure_cleanup; } if ( fdCache < 0 ) { - printf( "[ERROR] Could not open %s for writing.\n", cache ); + memlogf( "[ERROR] Could not open %s for writing.", cache ); goto failure_cleanup; } // Try cache map first - if ( fallocate( fdCache, 0, 0, mapsize ) != 0 ) { + if ( !file_alloc( fdCache, 0, mapsize ) ) { const int err = errno; - printf( "[ERROR] Could not allocate %d bytes for %s (errno=%d)", mapsize, cache, err ); + memlogf( "[ERROR] Could not allocate %d bytes for %s (errno=%d)", mapsize, cache, err ); goto failure_cleanup; } // Now write image - if ( fallocate( fdImage, 0, 0, size ) != 0 ) { + if ( !file_alloc( fdImage, 0, size ) ) { const int err = errno; - printf( "[ERROR] Could not allocate %" PRIu64 " bytes for %s (errno=%d)", size, path, err ); + memlogf( "[ERROR] Could not allocate %" PRIu64 " bytes for %s (errno=%d)", size, path, err ); goto failure_cleanup; } close( fdImage ); |