debuggers.hg
changeset 16752:b6cc74f275fd
blktap: factor out linux specific code
Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
author | Keir Fraser <keir.fraser@citrix.com> |
---|---|
date | Tue Jan 15 14:17:22 2008 +0000 (2008-01-15) |
parents | 973221f4d9c7 |
children | e6e165f72e57 |
files | tools/blktap/drivers/Makefile tools/blktap/drivers/blk.h tools/blktap/drivers/blk_linux.c tools/blktap/drivers/block-aio.c tools/blktap/drivers/block-qcow.c tools/blktap/drivers/block-ram.c tools/blktap/drivers/block-sync.c tools/blktap/drivers/block-vmdk.c tools/blktap/drivers/img2qcow.c tools/blktap/drivers/qcow2raw.c tools/blktap/drivers/tapdisk.h |
line diff
1.1 --- a/tools/blktap/drivers/Makefile Tue Jan 15 11:29:15 2008 +0000 1.2 +++ b/tools/blktap/drivers/Makefile Tue Jan 15 14:17:22 2008 +0000 1.3 @@ -28,28 +28,29 @@ LIBS += -L$(XEN_XENSTORE) -lxenstor 1.4 1.5 AIOLIBS := $(LIBAIO_DIR)/libaio.a 1.6 1.7 -BLK-OBJS := block-aio.o 1.8 -BLK-OBJS += block-sync.o 1.9 -BLK-OBJS += block-vmdk.o 1.10 -BLK-OBJS += block-ram.o 1.11 -BLK-OBJS += block-qcow.o 1.12 -BLK-OBJS += aes.o 1.13 -BLK-OBJS += tapaio.o 1.14 +BLK-OBJS-y := block-aio.o 1.15 +BLK-OBJS-y += block-sync.o 1.16 +BLK-OBJS-y += block-vmdk.o 1.17 +BLK-OBJS-y += block-ram.o 1.18 +BLK-OBJS-y += block-qcow.o 1.19 +BLK-OBJS-y += aes.o 1.20 +BLK-OBJS-y += tapaio.o 1.21 +BLK-OBJS-$(CONFIG_Linux) += blk_linux.c 1.22 1.23 all: $(IBIN) qcow-util 1.24 1.25 blktapctrl: blktapctrl.c 1.26 $(CC) $(CFLAGS) -o blktapctrl $(LIBS) blktapctrl.c 1.27 1.28 -tapdisk: $(BLK-OBJS) tapdisk.c 1.29 - $(CC) $(CFLAGS) -o tapdisk $(BLK-OBJS) tapdisk.c \ 1.30 +tapdisk: $(BLK-OBJS-y) tapdisk.c 1.31 + $(CC) $(CFLAGS) -o tapdisk $(BLK-OBJS-y) tapdisk.c \ 1.32 $(AIOLIBS) $(LIBS) 1.33 1.34 .PHONY: qcow-util 1.35 qcow-util: img2qcow qcow2raw qcow-create 1.36 1.37 -img2qcow qcow2raw qcow-create: %: $(BLK-OBJS) 1.38 - $(CC) $(CFLAGS) -o $* $(BLK-OBJS) $*.c $(AIOLIBS) $(LIBS) 1.39 +img2qcow qcow2raw qcow-create: %: $(BLK-OBJS-y) 1.40 + $(CC) $(CFLAGS) -o $* $(BLK-OBJS-y) $*.c $(AIOLIBS) $(LIBS) 1.41 1.42 install: all 1.43 $(INSTALL_PROG) $(IBIN) $(QCOW_UTIL) $(VHD_UTIL) $(DESTDIR)$(INST_DIR)
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/tools/blktap/drivers/blk.h Tue Jan 15 14:17:22 2008 +0000 2.3 @@ -0,0 +1,3 @@ 2.4 + 2.5 +int blk_getimagesize(int fd, uint64_t *size); 2.6 +int blk_getsectorsize(int fd, uint64_t *sector_size);
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/tools/blktap/drivers/blk_linux.c Tue Jan 15 14:17:22 2008 +0000 3.3 @@ -0,0 +1,42 @@ 3.4 +#include <inttypes.h> 3.5 +#include <sys/ioctl.h> 3.6 +#include <linux/fs.h> 3.7 +#include "tapdisk.h" 3.8 +#include "blk.h" 3.9 + 3.10 +int blk_getimagesize(int fd, uint64_t *size) 3.11 +{ 3.12 + int rc; 3.13 + 3.14 + *size = 0; 3.15 + rc = ioctl(fd, BLKGETSIZE, size); 3.16 + if (rc) { 3.17 + DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image"); 3.18 + return -EINVAL; 3.19 + } 3.20 + 3.21 + return 0; 3.22 +} 3.23 + 3.24 +int blk_getsectorsize(int fd, uint64_t *sector_size) 3.25 +{ 3.26 +#if defined(BLKSSZGET) 3.27 + int rc; 3.28 + 3.29 + *sector_size = DEFAULT_SECTOR_SIZE; 3.30 + rc = ioctl(fd, BLKSSZGET, sector_size); 3.31 + if (rc) { 3.32 + DPRINTF("ERR: BLKSSZGET failed. Falling back to use default sector size"); 3.33 + *sector_size = DEFAULT_SECTOR_SIZE; 3.34 + } 3.35 + 3.36 + if (*sector_size != DEFAULT_SECTOR_SIZE) 3.37 + DPRINTF("Note: sector size is %"PRIu64" (not %u)\n", 3.38 + *sector_size, DEFAULT_SECTOR_SIZE); 3.39 +#else 3.40 + *sector_size = DEFAULT_SECTOR_SIZE; 3.41 +#endif 3.42 + 3.43 + return 0; 3.44 +} 3.45 +
4.1 --- a/tools/blktap/drivers/block-aio.c Tue Jan 15 11:29:15 2008 +0000 4.2 +++ b/tools/blktap/drivers/block-aio.c Tue Jan 15 14:17:22 2008 +0000 4.3 @@ -41,12 +41,17 @@ 4.4 #include <sys/statvfs.h> 4.5 #include <sys/stat.h> 4.6 #include <sys/ioctl.h> 4.7 -#include <linux/fs.h> 4.8 #include "tapdisk.h" 4.9 #include "tapaio.h" 4.10 +#include "blk.h" 4.11 4.12 #define MAX_AIO_REQS (MAX_REQUESTS * MAX_SEGMENTS_PER_REQ) 4.13 4.14 +/* *BSD has no O_LARGEFILE */ 4.15 +#ifndef O_LARGEFILE 4.16 +#define O_LARGEFILE 0 4.17 +#endif 4.18 + 4.19 struct pending_aio { 4.20 td_callback_t cb; 4.21 int id; 4.22 @@ -87,11 +92,8 @@ static int get_image_info(struct td_stat 4.23 4.24 if (S_ISBLK(stat.st_mode)) { 4.25 /*Accessing block device directly*/ 4.26 - s->size = 0; 4.27 - if (ioctl(fd,BLKGETSIZE,&s->size)!=0) { 4.28 - DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image"); 4.29 + if (blk_getimagesize(fd, &s->size) != 0) 4.30 return -EINVAL; 4.31 - } 4.32 4.33 DPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost " 4.34 "sector_shift [%llu]\n", 4.35 @@ -99,19 +101,8 @@ static int get_image_info(struct td_stat 4.36 (long long unsigned)s->size); 4.37 4.38 /*Get the sector size*/ 4.39 -#if defined(BLKSSZGET) 4.40 - { 4.41 - int arg; 4.42 + if (blk_getsectorsize(fd, &s->sector_size) != 0) 4.43 s->sector_size = DEFAULT_SECTOR_SIZE; 4.44 - ioctl(fd, BLKSSZGET, &s->sector_size); 4.45 - 4.46 - if (s->sector_size != DEFAULT_SECTOR_SIZE) 4.47 - DPRINTF("Note: sector size is %ld (not %d)\n", 4.48 - s->sector_size, DEFAULT_SECTOR_SIZE); 4.49 - } 4.50 -#else 4.51 - s->sector_size = DEFAULT_SECTOR_SIZE; 4.52 -#endif 4.53 4.54 } else { 4.55 /*Local file? try fstat instead*/
5.1 --- a/tools/blktap/drivers/block-qcow.c Tue Jan 15 11:29:15 2008 +0000 5.2 +++ b/tools/blktap/drivers/block-qcow.c Tue Jan 15 14:17:22 2008 +0000 5.3 @@ -29,7 +29,6 @@ 5.4 #include <sys/statvfs.h> 5.5 #include <sys/stat.h> 5.6 #include <sys/ioctl.h> 5.7 -#include <linux/fs.h> 5.8 #include <string.h> 5.9 #include <zlib.h> 5.10 #include <inttypes.h> 5.11 @@ -39,6 +38,12 @@ 5.12 #include "aes.h" 5.13 #include "tapdisk.h" 5.14 #include "tapaio.h" 5.15 +#include "blk.h" 5.16 + 5.17 +/* *BSD has no O_LARGEFILE */ 5.18 +#ifndef O_LARGEFILE 5.19 +#define O_LARGEFILE 0 5.20 +#endif 5.21 5.22 #if 1 5.23 #define ASSERT(_p) \ 5.24 @@ -284,8 +289,7 @@ static int get_filesize(char *filename, 5.25 fd = open(filename, O_RDONLY); 5.26 if (fd < 0) 5.27 return -1; 5.28 - if (ioctl(fd,BLKGETSIZE,size)!=0) { 5.29 - printf("Unable to get Block device size\n"); 5.30 + if (blk_getimagesize(fd, size) != 0) { 5.31 close(fd); 5.32 return -1; 5.33 } 5.34 @@ -990,8 +994,8 @@ int tdqcow_open (struct disk_driver *dd, 5.35 if (!final_cluster) 5.36 s->fd_end = s->l1_table_offset + l1_table_size; 5.37 else { 5.38 - s->fd_end = lseek64(fd, 0, SEEK_END); 5.39 - if (s->fd_end == (off64_t)-1) 5.40 + s->fd_end = lseek(fd, 0, SEEK_END); 5.41 + if (s->fd_end == (off_t)-1) 5.42 goto fail; 5.43 } 5.44 5.45 @@ -1230,7 +1234,7 @@ int qcow_create(const char *filename, ui 5.46 DPRINTF("Qcow_create: size %llu\n",(long long unsigned)total_size); 5.47 5.48 fd = open(filename, 5.49 - O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 5.50 + O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 5.51 0644); 5.52 if (fd < 0) 5.53 return -1;
6.1 --- a/tools/blktap/drivers/block-ram.c Tue Jan 15 11:29:15 2008 +0000 6.2 +++ b/tools/blktap/drivers/block-ram.c Tue Jan 15 14:17:22 2008 +0000 6.3 @@ -33,16 +33,22 @@ 6.4 #include <fcntl.h> 6.5 #include <stdio.h> 6.6 #include <stdlib.h> 6.7 +#include <inttypes.h> 6.8 #include <unistd.h> 6.9 #include <sys/statvfs.h> 6.10 #include <sys/stat.h> 6.11 #include <sys/ioctl.h> 6.12 -#include <linux/fs.h> 6.13 #include <string.h> 6.14 #include "tapdisk.h" 6.15 +#include "blk.h" 6.16 6.17 #define MAX_DISK_SIZE 1024000 /*500MB disk limit*/ 6.18 6.19 +/* *BSD has no O_LARGEFILE */ 6.20 +#ifndef O_LARGEFILE 6.21 +#define O_LARGEFILE 0 6.22 +#endif 6.23 + 6.24 char *img; 6.25 long int disksector_size; 6.26 long int disksize; 6.27 @@ -71,11 +77,8 @@ static int get_image_info(struct td_stat 6.28 6.29 if (S_ISBLK(stat.st_mode)) { 6.30 /*Accessing block device directly*/ 6.31 - s->size = 0; 6.32 - if (ioctl(fd,BLKGETSIZE,&s->size)!=0) { 6.33 - DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image"); 6.34 + if (blk_getimagesize(fd, &s->size) != 0) 6.35 return -EINVAL; 6.36 - } 6.37 6.38 DPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost " 6.39 "sector_shift [%llu]\n", 6.40 @@ -83,19 +86,8 @@ static int get_image_info(struct td_stat 6.41 (long long unsigned)s->size); 6.42 6.43 /*Get the sector size*/ 6.44 -#if defined(BLKSSZGET) 6.45 - { 6.46 - int arg; 6.47 + if (blk_getsectorsize(fd, &s->sector_size) != 0) 6.48 s->sector_size = DEFAULT_SECTOR_SIZE; 6.49 - ioctl(fd, BLKSSZGET, &s->sector_size); 6.50 - 6.51 - if (s->sector_size != DEFAULT_SECTOR_SIZE) 6.52 - DPRINTF("Note: sector size is %ld (not %d)\n", 6.53 - s->sector_size, DEFAULT_SECTOR_SIZE); 6.54 - } 6.55 -#else 6.56 - s->sector_size = DEFAULT_SECTOR_SIZE; 6.57 -#endif 6.58 6.59 } else { 6.60 /*Local file? try fstat instead*/ 6.61 @@ -117,7 +109,7 @@ static int get_image_info(struct td_stat 6.62 disksector_size = s->sector_size; 6.63 disksize = s->size; 6.64 diskinfo = s->info; 6.65 - DPRINTF("Image sector_size: \n\t[%lu]\n", 6.66 + DPRINTF("Image sector_size: \n\t[%"PRIu64"]\n", 6.67 s->sector_size); 6.68 6.69 return 0; 6.70 @@ -159,7 +151,7 @@ int tdram_open (struct disk_driver *dd, 6.71 "sector_shift [%llu]\n", 6.72 (long long unsigned)(s->size << SECTOR_SHIFT), 6.73 (long long unsigned)s->size); 6.74 - DPRINTF("Image sector_size: \n\t[%lu]\n", 6.75 + DPRINTF("Image sector_size: \n\t[%"PRIu64"]\n", 6.76 s->sector_size); 6.77 6.78 prv->fd = -1;
7.1 --- a/tools/blktap/drivers/block-sync.c Tue Jan 15 11:29:15 2008 +0000 7.2 +++ b/tools/blktap/drivers/block-sync.c Tue Jan 15 14:17:22 2008 +0000 7.3 @@ -37,8 +37,13 @@ 7.4 #include <sys/statvfs.h> 7.5 #include <sys/stat.h> 7.6 #include <sys/ioctl.h> 7.7 -#include <linux/fs.h> 7.8 #include "tapdisk.h" 7.9 +#include "blk.h" 7.10 + 7.11 +/* *BSD has no O_LARGEFILE */ 7.12 +#ifndef O_LARGEFILE 7.13 +#define O_LARGEFILE 0 7.14 +#endif 7.15 7.16 struct tdsync_state { 7.17 int fd; 7.18 @@ -62,11 +67,8 @@ static int get_image_info(struct td_stat 7.19 7.20 if (S_ISBLK(stat.st_mode)) { 7.21 /*Accessing block device directly*/ 7.22 - s->size = 0; 7.23 - if (ioctl(fd,BLKGETSIZE,&s->size)!=0) { 7.24 - DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image"); 7.25 + if (blk_getimagesize(fd, &s->size) != 0) 7.26 return -EINVAL; 7.27 - } 7.28 7.29 DPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost " 7.30 "sector_shift [%llu]\n", 7.31 @@ -74,19 +76,8 @@ static int get_image_info(struct td_stat 7.32 (long long unsigned)s->size); 7.33 7.34 /*Get the sector size*/ 7.35 -#if defined(BLKSSZGET) 7.36 - { 7.37 - int arg; 7.38 + if (blk_getsectorsize(fd, &s->sector_size) != 0) 7.39 s->sector_size = DEFAULT_SECTOR_SIZE; 7.40 - ioctl(fd, BLKSSZGET, &s->sector_size); 7.41 - 7.42 - if (s->sector_size != DEFAULT_SECTOR_SIZE) 7.43 - DPRINTF("Note: sector size is %ld (not %d)\n", 7.44 - s->sector_size, DEFAULT_SECTOR_SIZE); 7.45 - } 7.46 -#else 7.47 - s->sector_size = DEFAULT_SECTOR_SIZE; 7.48 -#endif 7.49 7.50 } else { 7.51 /*Local file? try fstat instead*/
8.1 --- a/tools/blktap/drivers/block-vmdk.c Tue Jan 15 11:29:15 2008 +0000 8.2 +++ b/tools/blktap/drivers/block-vmdk.c Tue Jan 15 14:17:22 2008 +0000 8.3 @@ -42,11 +42,15 @@ 8.4 #include <sys/statvfs.h> 8.5 #include <sys/stat.h> 8.6 #include <sys/ioctl.h> 8.7 -#include <linux/fs.h> 8.8 #include <string.h> 8.9 #include "tapdisk.h" 8.10 #include "bswap.h" 8.11 8.12 +/* *BSD has no O_LARGEFILE */ 8.13 +#ifndef O_LARGEFILE 8.14 +#define O_LARGEFILE 0 8.15 +#endif 8.16 + 8.17 #define safer_free(_x) \ 8.18 do { \ 8.19 if (NULL != _x) { \
9.1 --- a/tools/blktap/drivers/img2qcow.c Tue Jan 15 11:29:15 2008 +0000 9.2 +++ b/tools/blktap/drivers/img2qcow.c Tue Jan 15 14:17:22 2008 +0000 9.3 @@ -37,9 +37,9 @@ 9.4 #include <sys/statvfs.h> 9.5 #include <sys/stat.h> 9.6 #include <sys/ioctl.h> 9.7 -#include <linux/fs.h> 9.8 #include <string.h> 9.9 #include "tapdisk.h" 9.10 +#include "blk.h" 9.11 9.12 #if 1 9.13 #define DFPRINTF(_f, _a...) fprintf ( stderr, _f , ## _a ) 9.14 @@ -47,6 +47,12 @@ 9.15 #define DFPRINTF(_f, _a...) ((void)0) 9.16 #endif 9.17 9.18 +/* *BSD has no O_LARGEFILE */ 9.19 +#ifndef O_LARGEFILE 9.20 +#define O_LARGEFILE 0 9.21 +#endif 9.22 + 9.23 + 9.24 #define TAPDISK 1 9.25 #define BLOCK_PROCESSSZ 4096 9.26 9.27 @@ -109,12 +115,8 @@ static int get_image_info(struct td_stat 9.28 9.29 if (S_ISBLK(stat.st_mode)) { 9.30 /*Accessing block device directly*/ 9.31 - s->size = 0; 9.32 - if (ioctl(fd,BLKGETSIZE,&s->size)!=0) { 9.33 - DFPRINTF("ERR: BLKGETSIZE failed, " 9.34 - "couldn't stat image"); 9.35 + if (blk_getimagesize(fd, &s->size) != 0) 9.36 return -EINVAL; 9.37 - } 9.38 9.39 DFPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost " 9.40 "sector_shift [%llu]\n", 9.41 @@ -122,19 +124,8 @@ static int get_image_info(struct td_stat 9.42 (long long unsigned)s->size); 9.43 9.44 /*Get the sector size*/ 9.45 -#if defined(BLKSSZGET) 9.46 - { 9.47 - int arg; 9.48 + if (blk_getsectorsize(fd, &s->sector_size) != 0) 9.49 s->sector_size = DEFAULT_SECTOR_SIZE; 9.50 - ioctl(fd, BLKSSZGET, &s->sector_size); 9.51 - 9.52 - if (s->sector_size != DEFAULT_SECTOR_SIZE) 9.53 - DFPRINTF("Note: sector size is %ld (not %d)\n", 9.54 - s->sector_size, DEFAULT_SECTOR_SIZE); 9.55 - } 9.56 -#else 9.57 - s->sector_size = DEFAULT_SECTOR_SIZE; 9.58 -#endif 9.59 9.60 } else { 9.61 /*Local file? try fstat instead*/
10.1 --- a/tools/blktap/drivers/qcow2raw.c Tue Jan 15 11:29:15 2008 +0000 10.2 +++ b/tools/blktap/drivers/qcow2raw.c Tue Jan 15 14:17:22 2008 +0000 10.3 @@ -33,13 +33,14 @@ 10.4 #include <fcntl.h> 10.5 #include <stdio.h> 10.6 #include <stdlib.h> 10.7 +#include <inttypes.h> 10.8 #include <unistd.h> 10.9 #include <sys/statvfs.h> 10.10 #include <sys/stat.h> 10.11 #include <sys/ioctl.h> 10.12 -#include <linux/fs.h> 10.13 #include <string.h> 10.14 #include "tapdisk.h" 10.15 +#include "blk.h" 10.16 10.17 #if 1 10.18 #define DFPRINTF(_f, _a...) fprintf ( stderr, _f , ## _a ) 10.19 @@ -47,6 +48,12 @@ 10.20 #define DFPRINTF(_f, _a...) ((void)0) 10.21 #endif 10.22 10.23 + 10.24 +/* *BSD has no O_LARGEFILE */ 10.25 +#ifndef O_LARGEFILE 10.26 +#define O_LARGEFILE 0 10.27 +#endif 10.28 + 10.29 #define TAPDISK 1 10.30 #define BLOCK_PROCESSSZ 4096 10.31 10.32 @@ -142,7 +149,7 @@ static int send_read_responses(struct di 10.33 int main(int argc, char *argv[]) 10.34 { 10.35 int ret = -1, fd, len,input; 10.36 - long int size; 10.37 + uint64_t size; 10.38 fd_set readfds; 10.39 struct timeval timeout; 10.40 uint64_t i; 10.41 @@ -227,16 +234,15 @@ int main(int argc, char *argv[]) 10.42 } 10.43 10.44 if (S_ISBLK(finfo.st_mode)) { 10.45 - if(ioctl(fd,BLKGETSIZE,&size)!=0) { 10.46 - DFPRINTF("ERROR: BLKGETSIZE failed, " 10.47 - "couldn't stat image [%s]\n", 10.48 - argv[1]); 10.49 + if (blk_getimagesize(fd, &size) != 0) { 10.50 close(fd); 10.51 - exit(-1); 10.52 + return -1; 10.53 } 10.54 + 10.55 if (size < ddqcow.td_state->size<<9) { 10.56 DFPRINTF("ERROR: Not enough space on device " 10.57 - "%s (%lu bytes available, %llu bytes required\n", 10.58 + "%s (%"PRIu64" bytes available, " 10.59 + "%llu bytes required\n", 10.60 argv[1], size, 10.61 (long long unsigned)ddqcow.td_state->size<<9); 10.62 close(fd);
11.1 --- a/tools/blktap/drivers/tapdisk.h Tue Jan 15 11:29:15 2008 +0000 11.2 +++ b/tools/blktap/drivers/tapdisk.h Tue Jan 15 14:17:22 2008 +0000 11.3 @@ -108,8 +108,8 @@ struct td_state { 11.4 void *image; 11.5 void *ring_info; 11.6 void *fd_entry; 11.7 - unsigned long sector_size; 11.8 - unsigned long long size; 11.9 + uint64_t sector_size; 11.10 + uint64_t size; 11.11 unsigned int info; 11.12 }; 11.13