Skip to contentSkip to conversation

blktap: factor out linux specific code

PATCH In review Christoph Egger 3 participants 3 messages Dec 6, 2007 – Dec 7, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 blktap: factor out linux specific code

Hi!

Attached patch factors out linux specific code from block drivers into a separate file.
This makes it easy to add support for *BSD and Solaris.

No functional change on Linux-side.

P.S.: There can be factored out a lot of code from the block drivers into OS and block common files.

Signature · 12 lines
AMD Saxony, Dresden, Germany
Operating System Research Center

Legal Information:
AMD Saxony Limited Liability Company & Co. KG
Sitz (Geschäftsanschrift):
   Wilschdorfer Landstr. 101, 01109 Dresden, Deutschland
Registergericht Dresden: HRA 4896
vertretungsberechtigter Komplementär:
   AMD Saxony LLC (Sitz Wilmington, Delaware, USA)
Geschäftsführer der AMD Saxony LLC:
   Dr. Hans-R. Deppe, Thomas McCoy
tools/blktap/drivers/Makefile +12 -11
diff -r b240698146a2 tools/blktap/drivers/Makefile
@@ -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)
tools/blktap/drivers/blk.h new file +3 -0
diff -r b240698146a2 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);
tools/blktap/drivers/blk_linux.c new file +42 -0

42 lines not on this page · raw message

tools/blktap/drivers/block-aio.c +8 -17
diff -r b240698146a2 tools/blktap/drivers/block-aio.c
@@ -41,11 +41,16 @@
 #include <sys/statvfs.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
-#include <linux/fs.h>
 #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*/
tools/blktap/drivers/block-qcow.c +10 -6
diff -r b240698146a2 tools/blktap/drivers/block-qcow.c
@@ -29,7 +29,6 @@
 #include <sys/statvfs.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
-#include <linux/fs.h>
 #include <string.h>
 #include <zlib.h>
 #include <inttypes.h>
@@ -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;
tools/blktap/drivers/block-ram.c +8 -17
diff -r b240698146a2 tools/blktap/drivers/block-ram.c
@@ -37,11 +37,16 @@
 #include <sys/statvfs.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
-#include <linux/fs.h>
 #include <string.h>
 #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*/
tools/blktap/drivers/block-sync.c +8 -17
diff -r b240698146a2 tools/blktap/drivers/block-sync.c
@@ -37,8 +37,13 @@
 #include <sys/statvfs.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
-#include <linux/fs.h>
 #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*/
tools/blktap/drivers/block-vmdk.c +5 -1
diff -r b240698146a2 tools/blktap/drivers/block-vmdk.c
@@ -42,10 +42,14 @@
 #include <sys/statvfs.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
-#include <linux/fs.h>
 #include <string.h>
 #include "tapdisk.h"
 #include "bswap.h"
+
+/* *BSD has no O_LARGEFILE */
+#ifndef O_LARGEFILE
+#define O_LARGEFILE	0
+#endif
 
 #define safer_free(_x)       \
   do {                       \
tools/blktap/drivers/img2qcow.c +9 -18
diff -r b240698146a2 tools/blktap/drivers/img2qcow.c
@@ -37,15 +37,21 @@
 #include <sys/statvfs.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
-#include <linux/fs.h>
 #include <string.h>
 #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*/
tools/blktap/drivers/qcow2raw.c +13 -9
diff -r b240698146a2 tools/blktap/drivers/qcow2raw.c
@@ -37,14 +37,20 @@
 #include <sys/statvfs.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
-#include <linux/fs.h>
 #include <string.h>
 #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",
tools/blktap/drivers/tapdisk.h +2 -2
diff -r b240698146a2 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;
 };
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw

tools/blktap/drivers/block-qcow.c int tdqcow_open (struct disk_driver *dd, · line 998

Quoted 4 lines · Christoph Egger
tools/blktap/drivers/block-qcow.c +2 -2
-		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)

Why this change? Seems that it will break for any large file?

regards
john

#2 lore raw
Quoted 8 lines · John Levon

tools/blktap/drivers/block-qcow.c int tdqcow_open (struct disk_driver *dd, · line 998

Quoted 4 lines · Christoph Egger
tools/blktap/drivers/block-qcow.c +2 -2
-  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)

Why this change? Seems that it will break for any large file?

Nope, we turn on implicit 64-bit-safe file operations by throwing LFS goodness into CFLAGS and LDFLAGS in tools/Rules.mk. There's no need to use the explicit *64 function and typedef names.

-- Keir