From 6aef553b661b7504ff927ee5e120a69b44ba92aa Mon Sep 17 00:00:00 2001 From: Keir Fraser Date: Tue, 15 Jan 2008 14:17:22 +0000 Subject: blktap: factor out linux specific code Signed-off-by: Christoph Egger --- tools/blktap/drivers/Makefile | 23 +++++++++++---------- tools/blktap/drivers/blk.h | 3 +++ tools/blktap/drivers/blk_linux.c | 42 +++++++++++++++++++++++++++++++++++++++ tools/blktap/drivers/block-aio.c | 25 ++++++++--------------- tools/blktap/drivers/block-qcow.c | 16 +++++++++------ tools/blktap/drivers/block-ram.c | 30 ++++++++++------------------ tools/blktap/drivers/block-sync.c | 25 ++++++++--------------- tools/blktap/drivers/block-vmdk.c | 6 +++++- tools/blktap/drivers/img2qcow.c | 27 +++++++++---------------- tools/blktap/drivers/qcow2raw.c | 22 ++++++++++++-------- tools/blktap/drivers/tapdisk.h | 4 ++-- 11 files changed, 124 insertions(+), 99 deletions(-) create mode 100644 tools/blktap/drivers/blk.h create mode 100644 tools/blktap/drivers/blk_linux.c (limited to 'tools/blktap') diff --git a/tools/blktap/drivers/Makefile b/tools/blktap/drivers/Makefile index fca48b6ced..b3c8fbaee9 100644 --- a/tools/blktap/drivers/Makefile +++ b/tools/blktap/drivers/Makefile @@ -28,28 +28,29 @@ LIBS += -L$(XEN_XENSTORE) -lxenstore AIOLIBS := $(LIBAIO_DIR)/libaio.a -BLK-OBJS := block-aio.o -BLK-OBJS += block-sync.o -BLK-OBJS += block-vmdk.o -BLK-OBJS += block-ram.o -BLK-OBJS += block-qcow.o -BLK-OBJS += aes.o -BLK-OBJS += tapaio.o +BLK-OBJS-y := block-aio.o +BLK-OBJS-y += block-sync.o +BLK-OBJS-y += block-vmdk.o +BLK-OBJS-y += block-ram.o +BLK-OBJS-y += block-qcow.o +BLK-OBJS-y += aes.o +BLK-OBJS-y += tapaio.o +BLK-OBJS-$(CONFIG_Linux) += blk_linux.c all: $(IBIN) qcow-util blktapctrl: blktapctrl.c $(CC) $(CFLAGS) -o blktapctrl $(LIBS) blktapctrl.c -tapdisk: $(BLK-OBJS) tapdisk.c - $(CC) $(CFLAGS) -o tapdisk $(BLK-OBJS) tapdisk.c \ +tapdisk: $(BLK-OBJS-y) tapdisk.c + $(CC) $(CFLAGS) -o tapdisk $(BLK-OBJS-y) tapdisk.c \ $(AIOLIBS) $(LIBS) .PHONY: qcow-util qcow-util: img2qcow qcow2raw qcow-create -img2qcow qcow2raw qcow-create: %: $(BLK-OBJS) - $(CC) $(CFLAGS) -o $* $(BLK-OBJS) $*.c $(AIOLIBS) $(LIBS) +img2qcow qcow2raw qcow-create: %: $(BLK-OBJS-y) + $(CC) $(CFLAGS) -o $* $(BLK-OBJS-y) $*.c $(AIOLIBS) $(LIBS) install: all $(INSTALL_PROG) $(IBIN) $(QCOW_UTIL) $(VHD_UTIL) $(DESTDIR)$(INST_DIR) diff --git a/tools/blktap/drivers/blk.h b/tools/blktap/drivers/blk.h new file mode 100644 index 0000000000..1cdc98098c --- /dev/null +++ b/tools/blktap/drivers/blk.h @@ -0,0 +1,3 @@ + +int blk_getimagesize(int fd, uint64_t *size); +int blk_getsectorsize(int fd, uint64_t *sector_size); diff --git a/tools/blktap/drivers/blk_linux.c b/tools/blktap/drivers/blk_linux.c new file mode 100644 index 0000000000..f1b14bd6e8 --- /dev/null +++ b/tools/blktap/drivers/blk_linux.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include "tapdisk.h" +#include "blk.h" + +int blk_getimagesize(int fd, uint64_t *size) +{ + int rc; + + *size = 0; + rc = ioctl(fd, BLKGETSIZE, size); + if (rc) { + DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image"); + return -EINVAL; + } + + return 0; +} + +int blk_getsectorsize(int fd, uint64_t *sector_size) +{ +#if defined(BLKSSZGET) + int rc; + + *sector_size = DEFAULT_SECTOR_SIZE; + rc = ioctl(fd, BLKSSZGET, sector_size); + if (rc) { + DPRINTF("ERR: BLKSSZGET failed. Falling back to use default sector size"); + *sector_size = DEFAULT_SECTOR_SIZE; + } + + if (*sector_size != DEFAULT_SECTOR_SIZE) + DPRINTF("Note: sector size is %"PRIu64" (not %u)\n", + *sector_size, DEFAULT_SECTOR_SIZE); +#else + *sector_size = DEFAULT_SECTOR_SIZE; +#endif + + return 0; +} + diff --git a/tools/blktap/drivers/block-aio.c b/tools/blktap/drivers/block-aio.c index 5c1ac373a7..d600899b61 100644 --- a/tools/blktap/drivers/block-aio.c +++ b/tools/blktap/drivers/block-aio.c @@ -41,12 +41,17 @@ #include #include #include -#include #include "tapdisk.h" #include "tapaio.h" +#include "blk.h" #define MAX_AIO_REQS (MAX_REQUESTS * MAX_SEGMENTS_PER_REQ) +/* *BSD has no O_LARGEFILE */ +#ifndef O_LARGEFILE +#define O_LARGEFILE 0 +#endif + struct pending_aio { td_callback_t cb; int id; @@ -87,11 +92,8 @@ static int get_image_info(struct td_state *s, int fd) if (S_ISBLK(stat.st_mode)) { /*Accessing block device directly*/ - s->size = 0; - if (ioctl(fd,BLKGETSIZE,&s->size)!=0) { - DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image"); + if (blk_getimagesize(fd, &s->size) != 0) return -EINVAL; - } DPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost " "sector_shift [%llu]\n", @@ -99,19 +101,8 @@ static int get_image_info(struct td_state *s, int fd) (long long unsigned)s->size); /*Get the sector size*/ -#if defined(BLKSSZGET) - { - int arg; + if (blk_getsectorsize(fd, &s->sector_size) != 0) s->sector_size = DEFAULT_SECTOR_SIZE; - ioctl(fd, BLKSSZGET, &s->sector_size); - - if (s->sector_size != DEFAULT_SECTOR_SIZE) - DPRINTF("Note: sector size is %ld (not %d)\n", - s->sector_size, DEFAULT_SECTOR_SIZE); - } -#else - s->sector_size = DEFAULT_SECTOR_SIZE; -#endif } else { /*Local file? try fstat instead*/ diff --git a/tools/blktap/drivers/block-qcow.c b/tools/blktap/drivers/block-qcow.c index fe70326c1f..0bc8374fab 100644 --- a/tools/blktap/drivers/block-qcow.c +++ b/tools/blktap/drivers/block-qcow.c @@ -29,7 +29,6 @@ #include #include #include -#include #include #include #include @@ -39,6 +38,12 @@ #include "aes.h" #include "tapdisk.h" #include "tapaio.h" +#include "blk.h" + +/* *BSD has no O_LARGEFILE */ +#ifndef O_LARGEFILE +#define O_LARGEFILE 0 +#endif #if 1 #define ASSERT(_p) \ @@ -284,8 +289,7 @@ static int get_filesize(char *filename, uint64_t *size, struct stat *st) fd = open(filename, O_RDONLY); if (fd < 0) return -1; - if (ioctl(fd,BLKGETSIZE,size)!=0) { - printf("Unable to get Block device size\n"); + if (blk_getimagesize(fd, size) != 0) { close(fd); return -1; } @@ -990,8 +994,8 @@ int tdqcow_open (struct disk_driver *dd, const char *name, td_flag_t flags) if (!final_cluster) s->fd_end = s->l1_table_offset + l1_table_size; else { - s->fd_end = lseek64(fd, 0, SEEK_END); - if (s->fd_end == (off64_t)-1) + s->fd_end = lseek(fd, 0, SEEK_END); + if (s->fd_end == (off_t)-1) goto fail; } @@ -1230,7 +1234,7 @@ int qcow_create(const char *filename, uint64_t total_size, DPRINTF("Qcow_create: size %llu\n",(long long unsigned)total_size); fd = open(filename, - O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, + O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644); if (fd < 0) return -1; diff --git a/tools/blktap/drivers/block-ram.c b/tools/blktap/drivers/block-ram.c index 3b4ef7ceb5..f39d6422b2 100644 --- a/tools/blktap/drivers/block-ram.c +++ b/tools/blktap/drivers/block-ram.c @@ -33,16 +33,22 @@ #include #include #include +#include #include #include #include #include -#include #include #include "tapdisk.h" +#include "blk.h" #define MAX_DISK_SIZE 1024000 /*500MB disk limit*/ +/* *BSD has no O_LARGEFILE */ +#ifndef O_LARGEFILE +#define O_LARGEFILE 0 +#endif + char *img; long int disksector_size; long int disksize; @@ -71,11 +77,8 @@ static int get_image_info(struct td_state *s, int fd) if (S_ISBLK(stat.st_mode)) { /*Accessing block device directly*/ - s->size = 0; - if (ioctl(fd,BLKGETSIZE,&s->size)!=0) { - DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image"); + if (blk_getimagesize(fd, &s->size) != 0) return -EINVAL; - } DPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost " "sector_shift [%llu]\n", @@ -83,19 +86,8 @@ static int get_image_info(struct td_state *s, int fd) (long long unsigned)s->size); /*Get the sector size*/ -#if defined(BLKSSZGET) - { - int arg; + if (blk_getsectorsize(fd, &s->sector_size) != 0) s->sector_size = DEFAULT_SECTOR_SIZE; - ioctl(fd, BLKSSZGET, &s->sector_size); - - if (s->sector_size != DEFAULT_SECTOR_SIZE) - DPRINTF("Note: sector size is %ld (not %d)\n", - s->sector_size, DEFAULT_SECTOR_SIZE); - } -#else - s->sector_size = DEFAULT_SECTOR_SIZE; -#endif } else { /*Local file? try fstat instead*/ @@ -117,7 +109,7 @@ static int get_image_info(struct td_state *s, int fd) disksector_size = s->sector_size; disksize = s->size; diskinfo = s->info; - DPRINTF("Image sector_size: \n\t[%lu]\n", + DPRINTF("Image sector_size: \n\t[%"PRIu64"]\n", s->sector_size); return 0; @@ -159,7 +151,7 @@ int tdram_open (struct disk_driver *dd, const char *name, td_flag_t flags) "sector_shift [%llu]\n", (long long unsigned)(s->size << SECTOR_SHIFT), (long long unsigned)s->size); - DPRINTF("Image sector_size: \n\t[%lu]\n", + DPRINTF("Image sector_size: \n\t[%"PRIu64"]\n", s->sector_size); prv->fd = -1; diff --git a/tools/blktap/drivers/block-sync.c b/tools/blktap/drivers/block-sync.c index 74f77d6f61..8e6697c329 100644 --- a/tools/blktap/drivers/block-sync.c +++ b/tools/blktap/drivers/block-sync.c @@ -37,8 +37,13 @@ #include #include #include -#include #include "tapdisk.h" +#include "blk.h" + +/* *BSD has no O_LARGEFILE */ +#ifndef O_LARGEFILE +#define O_LARGEFILE 0 +#endif struct tdsync_state { int fd; @@ -62,11 +67,8 @@ static int get_image_info(struct td_state *s, int fd) if (S_ISBLK(stat.st_mode)) { /*Accessing block device directly*/ - s->size = 0; - if (ioctl(fd,BLKGETSIZE,&s->size)!=0) { - DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image"); + if (blk_getimagesize(fd, &s->size) != 0) return -EINVAL; - } DPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost " "sector_shift [%llu]\n", @@ -74,19 +76,8 @@ static int get_image_info(struct td_state *s, int fd) (long long unsigned)s->size); /*Get the sector size*/ -#if defined(BLKSSZGET) - { - int arg; + if (blk_getsectorsize(fd, &s->sector_size) != 0) s->sector_size = DEFAULT_SECTOR_SIZE; - ioctl(fd, BLKSSZGET, &s->sector_size); - - if (s->sector_size != DEFAULT_SECTOR_SIZE) - DPRINTF("Note: sector size is %ld (not %d)\n", - s->sector_size, DEFAULT_SECTOR_SIZE); - } -#else - s->sector_size = DEFAULT_SECTOR_SIZE; -#endif } else { /*Local file? try fstat instead*/ diff --git a/tools/blktap/drivers/block-vmdk.c b/tools/blktap/drivers/block-vmdk.c index f6b720e1e0..4d16965213 100644 --- a/tools/blktap/drivers/block-vmdk.c +++ b/tools/blktap/drivers/block-vmdk.c @@ -42,11 +42,15 @@ #include #include #include -#include #include #include "tapdisk.h" #include "bswap.h" +/* *BSD has no O_LARGEFILE */ +#ifndef O_LARGEFILE +#define O_LARGEFILE 0 +#endif + #define safer_free(_x) \ do { \ if (NULL != _x) { \ diff --git a/tools/blktap/drivers/img2qcow.c b/tools/blktap/drivers/img2qcow.c index 926cf2742e..61a30087ec 100644 --- a/tools/blktap/drivers/img2qcow.c +++ b/tools/blktap/drivers/img2qcow.c @@ -37,9 +37,9 @@ #include #include #include -#include #include #include "tapdisk.h" +#include "blk.h" #if 1 #define DFPRINTF(_f, _a...) fprintf ( stderr, _f , ## _a ) @@ -47,6 +47,12 @@ #define DFPRINTF(_f, _a...) ((void)0) #endif +/* *BSD has no O_LARGEFILE */ +#ifndef O_LARGEFILE +#define O_LARGEFILE 0 +#endif + + #define TAPDISK 1 #define BLOCK_PROCESSSZ 4096 @@ -109,12 +115,8 @@ static int get_image_info(struct td_state *s, int fd) if (S_ISBLK(stat.st_mode)) { /*Accessing block device directly*/ - s->size = 0; - if (ioctl(fd,BLKGETSIZE,&s->size)!=0) { - DFPRINTF("ERR: BLKGETSIZE failed, " - "couldn't stat image"); + if (blk_getimagesize(fd, &s->size) != 0) return -EINVAL; - } DFPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost " "sector_shift [%llu]\n", @@ -122,19 +124,8 @@ static int get_image_info(struct td_state *s, int fd) (long long unsigned)s->size); /*Get the sector size*/ -#if defined(BLKSSZGET) - { - int arg; + if (blk_getsectorsize(fd, &s->sector_size) != 0) s->sector_size = DEFAULT_SECTOR_SIZE; - ioctl(fd, BLKSSZGET, &s->sector_size); - - if (s->sector_size != DEFAULT_SECTOR_SIZE) - DFPRINTF("Note: sector size is %ld (not %d)\n", - s->sector_size, DEFAULT_SECTOR_SIZE); - } -#else - s->sector_size = DEFAULT_SECTOR_SIZE; -#endif } else { /*Local file? try fstat instead*/ diff --git a/tools/blktap/drivers/qcow2raw.c b/tools/blktap/drivers/qcow2raw.c index cbfe55b8e3..d553dbcdbf 100644 --- a/tools/blktap/drivers/qcow2raw.c +++ b/tools/blktap/drivers/qcow2raw.c @@ -33,13 +33,14 @@ #include #include #include +#include #include #include #include #include -#include #include #include "tapdisk.h" +#include "blk.h" #if 1 #define DFPRINTF(_f, _a...) fprintf ( stderr, _f , ## _a ) @@ -47,6 +48,12 @@ #define DFPRINTF(_f, _a...) ((void)0) #endif + +/* *BSD has no O_LARGEFILE */ +#ifndef O_LARGEFILE +#define O_LARGEFILE 0 +#endif + #define TAPDISK 1 #define BLOCK_PROCESSSZ 4096 @@ -142,7 +149,7 @@ static int send_read_responses(struct disk_driver *dd, int res, uint64_t sec, int main(int argc, char *argv[]) { int ret = -1, fd, len,input; - long int size; + uint64_t size; fd_set readfds; struct timeval timeout; uint64_t i; @@ -227,16 +234,15 @@ int main(int argc, char *argv[]) } if (S_ISBLK(finfo.st_mode)) { - if(ioctl(fd,BLKGETSIZE,&size)!=0) { - DFPRINTF("ERROR: BLKGETSIZE failed, " - "couldn't stat image [%s]\n", - argv[1]); + if (blk_getimagesize(fd, &size) != 0) { close(fd); - exit(-1); + return -1; } + if (size < ddqcow.td_state->size<<9) { DFPRINTF("ERROR: Not enough space on device " - "%s (%lu bytes available, %llu bytes required\n", + "%s (%"PRIu64" bytes available, " + "%llu bytes required\n", argv[1], size, (long long unsigned)ddqcow.td_state->size<<9); close(fd); diff --git a/tools/blktap/drivers/tapdisk.h b/tools/blktap/drivers/tapdisk.h index 5c68a5e9c8..025df95407 100644 --- a/tools/blktap/drivers/tapdisk.h +++ b/tools/blktap/drivers/tapdisk.h @@ -108,8 +108,8 @@ struct td_state { void *image; void *ring_info; void *fd_entry; - unsigned long sector_size; - unsigned long long size; + uint64_t sector_size; + uint64_t size; unsigned int info; }; -- cgit v1.2.3