diff -r b240698146a2 tools/blktap/drivers/Makefile --- a/tools/blktap/drivers/Makefile Thu Dec 06 17:47:53 2007 +0000 +++ b/tools/blktap/drivers/Makefile Thu Dec 06 21:48:44 2007 +0100 @@ -28,28 +28,29 @@ LIBS += -L$(XEN_XENSTORE) -lxenstor 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 -r b240698146a2 tools/blktap/drivers/blk.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/blktap/drivers/blk.h Thu Dec 06 21:48:44 2007 +0100 @@ -0,0 +1,3 @@ + +int blk_getimagesize(int fd, uint64_t *size); +int blk_getsectorsize(int fd, uint64_t *sector_size); diff -r b240698146a2 tools/blktap/drivers/blk_linux.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/blktap/drivers/blk_linux.c Thu Dec 06 21:48:44 2007 +0100 @@ -0,0 +1,42 @@ + +#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 %ld (not %d)\n", + *sector_size, DEFAULT_SECTOR_SIZE); +#else + *sector_size = DEFAULT_SECTOR_SIZE; +#endif + + return 0; +} + diff -r b240698146a2 tools/blktap/drivers/block-aio.c --- a/tools/blktap/drivers/block-aio.c Thu Dec 06 17:47:53 2007 +0000 +++ b/tools/blktap/drivers/block-aio.c Thu Dec 06 21:48:44 2007 +0100 @@ -41,11 +41,16 @@ #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; @@ -87,11 +92,8 @@ static int get_image_info(struct td_stat 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_stat (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 -r b240698146a2 tools/blktap/drivers/block-qcow.c --- a/tools/blktap/drivers/block-qcow.c Thu Dec 06 17:47:53 2007 +0000 +++ b/tools/blktap/drivers/block-qcow.c Thu Dec 06 21:48:44 2007 +0100 @@ -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, 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, 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, ui 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 -r b240698146a2 tools/blktap/drivers/block-ram.c --- a/tools/blktap/drivers/block-ram.c Thu Dec 06 17:47:53 2007 +0000 +++ b/tools/blktap/drivers/block-ram.c Thu Dec 06 21:48:44 2007 +0100 @@ -37,11 +37,16 @@ #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; @@ -71,11 +76,8 @@ static int get_image_info(struct td_stat 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 +85,8 @@ static int get_image_info(struct td_stat (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 -r b240698146a2 tools/blktap/drivers/block-sync.c --- a/tools/blktap/drivers/block-sync.c Thu Dec 06 17:47:53 2007 +0000 +++ b/tools/blktap/drivers/block-sync.c Thu Dec 06 21:48:44 2007 +0100 @@ -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_stat 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_stat (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 -r b240698146a2 tools/blktap/drivers/block-vmdk.c --- a/tools/blktap/drivers/block-vmdk.c Thu Dec 06 17:47:53 2007 +0000 +++ b/tools/blktap/drivers/block-vmdk.c Thu Dec 06 21:48:44 2007 +0100 @@ -42,10 +42,14 @@ #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 { \ diff -r b240698146a2 tools/blktap/drivers/img2qcow.c --- a/tools/blktap/drivers/img2qcow.c Thu Dec 06 17:47:53 2007 +0000 +++ b/tools/blktap/drivers/img2qcow.c Thu Dec 06 21:48:44 2007 +0100 @@ -37,15 +37,21 @@ #include #include #include -#include #include #include "tapdisk.h" +#include "blk.h" #if 1 #define DFPRINTF(_f, _a...) fprintf ( stderr, _f , ## _a ) #else #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_stat 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_stat (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 -r b240698146a2 tools/blktap/drivers/qcow2raw.c --- a/tools/blktap/drivers/qcow2raw.c Thu Dec 06 17:47:53 2007 +0000 +++ b/tools/blktap/drivers/qcow2raw.c Thu Dec 06 21:48:44 2007 +0100 @@ -37,14 +37,20 @@ #include #include #include -#include #include #include "tapdisk.h" +#include "blk.h" #if 1 #define DFPRINTF(_f, _a...) fprintf ( stderr, _f , ## _a ) #else #define DFPRINTF(_f, _a...) ((void)0) +#endif + + +/* *BSD has no O_LARGEFILE */ +#ifndef O_LARGEFILE +#define O_LARGEFILE 0 #endif #define TAPDISK 1 @@ -142,7 +148,7 @@ int main(int argc, char *argv[]) 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,13 +233,11 @@ 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]); - close(fd); - exit(-1); - } + if (blk_getimagesize(fd, &size) != 0) { + close(fd); + return -1; + } + if (size < ddqcow.td_state->size<<9) { DFPRINTF("ERROR: Not enough space on device " "%s (%lu bytes available, %llu bytes required\n", diff -r b240698146a2 tools/blktap/drivers/tapdisk.h --- a/tools/blktap/drivers/tapdisk.h Thu Dec 06 17:47:53 2007 +0000 +++ b/tools/blktap/drivers/tapdisk.h Thu Dec 06 21:48:44 2007 +0100 @@ -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; };