svn commit: r274783 - in user/marcel/libvdsk: bhyve bhyveload

Marcel Moolenaar marcel at FreeBSD.org
Fri Nov 21 03:22:23 UTC 2014


Author: marcel
Date: Fri Nov 21 03:22:22 2014
New Revision: 274783
URL: https://svnweb.freebsd.org/changeset/base/274783

Log:
  Use libvdsk(3) to implement block devices.

Modified:
  user/marcel/libvdsk/bhyve/Makefile
  user/marcel/libvdsk/bhyve/block_if.c
  user/marcel/libvdsk/bhyveload/Makefile
  user/marcel/libvdsk/bhyveload/bhyveload.c

Modified: user/marcel/libvdsk/bhyve/Makefile
==============================================================================
--- user/marcel/libvdsk/bhyve/Makefile	Fri Nov 21 03:19:51 2014	(r274782)
+++ user/marcel/libvdsk/bhyve/Makefile	Fri Nov 21 03:22:22 2014	(r274783)
@@ -43,8 +43,8 @@ SRCS=	\
 .PATH:	/sys/amd64/vmm
 SRCS+=	vmm_instruction_emul.c
 
-DPADD=	${LIBVMMAPI} ${LIBMD} ${LIBUTIL} ${LIBPTHREAD}
-LDADD=	-lvmmapi -lmd -lutil -lpthread
+DPADD=	${LIBVDSK} ${LIBVMMAPI} ${LIBMD} ${LIBUTIL} ${LIBPTHREAD}
+LDADD=	-lvdsk -lvmmapi -lmd -lutil -lpthread
 
 WARNS?=	2
 

Modified: user/marcel/libvdsk/bhyve/block_if.c
==============================================================================
--- user/marcel/libvdsk/bhyve/block_if.c	Fri Nov 21 03:19:51 2014	(r274782)
+++ user/marcel/libvdsk/bhyve/block_if.c	Fri Nov 21 03:22:22 2014	(r274783)
@@ -45,6 +45,7 @@ __FBSDID("$FreeBSD$");
 #include <pthread_np.h>
 #include <signal.h>
 #include <unistd.h>
+#include <vdsk.h>
 
 #include <machine/atomic.h>
 
@@ -79,10 +80,7 @@ struct blockif_elem {
 
 struct blockif_ctxt {
 	int			bc_magic;
-	int			bc_fd;
 	int			bc_rdonly;
-	off_t			bc_size;
-	int			bc_sectsz;
 	pthread_t		bc_btid;
         pthread_mutex_t		bc_mtx;
         pthread_cond_t		bc_cond;
@@ -176,18 +174,17 @@ blockif_proc(struct blockif_ctxt *bc, st
 
 	switch (be->be_op) {
 	case BOP_READ:
-		if (preadv(bc->bc_fd, br->br_iov, br->br_iovcnt,
-			   br->br_offset) < 0)
+		if (vdsk_readv(bc, br->br_iov, br->br_iovcnt,
+		    br->br_offset) < 0)
 			err = errno;
 		break;
 	case BOP_WRITE:
-		if (bc->bc_rdonly)
-			err = EROFS;
-		else if (pwritev(bc->bc_fd, br->br_iov, br->br_iovcnt,
-			     br->br_offset) < 0)
+		if (vdsk_writev(bc, br->br_iov, br->br_iovcnt,
+		    br->br_offset) < 0)
 			err = errno;
 		break;
 	case BOP_FLUSH:
+		err = vdsk_flush(bc);
 		break;
 	default:
 		err = EINVAL;
@@ -267,9 +264,7 @@ blockif_open(const char *optstr, const c
 	char tname[MAXCOMLEN + 1];
 	char *nopt, *xopts;
 	struct blockif_ctxt *bc;
-	struct stat sbuf;
-	off_t size;
-	int extra, fd, i, sectsz;
+	int extra, i;
 	int nocache, sync, ro;
 
 	pthread_once(&blockif_once, blockif_init);
@@ -300,51 +295,20 @@ blockif_open(const char *optstr, const c
 	if (sync)
 		extra |= O_SYNC;
 
-	fd = open(nopt, (ro ? O_RDONLY : O_RDWR) | extra);
-	if (fd < 0 && !ro) {
+	bc = vdsk_open(nopt, (ro ? O_RDONLY : O_RDWR) | extra, sizeof(*bc));
+	if (bc == NULL && !ro) {
 		/* Attempt a r/w fail with a r/o open */
-		fd = open(nopt, O_RDONLY | extra);
+		bc = vdsk_open(nopt, O_RDONLY | extra, sizeof(*bc));
 		ro = 1;
 	}
 
-	if (fd < 0) {
-		perror("Could not open backing file");
-		return (NULL);
-	}
-
-        if (fstat(fd, &sbuf) < 0) {
-                perror("Could not stat backing file");
-                close(fd);
-                return (NULL);
-        }
-
-        /*
-	 * Deal with raw devices
-	 */
-        size = sbuf.st_size;
-	sectsz = DEV_BSIZE;
-	if (S_ISCHR(sbuf.st_mode)) {
-		if (ioctl(fd, DIOCGMEDIASIZE, &size) < 0 ||
-		    ioctl(fd, DIOCGSECTORSIZE, &sectsz)) {
-			perror("Could not fetch dev blk/sector size");
-			close(fd);
-			return (NULL);
-		}
-		assert(size != 0);
-		assert(sectsz != 0);
-	}
-
-	bc = calloc(1, sizeof(struct blockif_ctxt));
 	if (bc == NULL) {
-		close(fd);
+		perror("Could not open backing file");
 		return (NULL);
 	}
 
 	bc->bc_magic = BLOCKIF_SIG;
-	bc->bc_fd = fd;
 	bc->bc_rdonly = ro;
-	bc->bc_size = size;
-	bc->bc_sectsz = sectsz;
 	pthread_mutex_init(&bc->bc_mtx, NULL);
 	pthread_cond_init(&bc->bc_cond, NULL);
 	TAILQ_INIT(&bc->bc_freeq);
@@ -521,8 +485,7 @@ blockif_close(struct blockif_ctxt *bc)
 	 * Release resources
 	 */
 	bc->bc_magic = 0;
-	close(bc->bc_fd);
-	free(bc);
+	vdsk_close(bc);
 
 	return (0);
 }
@@ -541,7 +504,7 @@ blockif_chs(struct blockif_ctxt *bc, uin
 
 	assert(bc->bc_magic == BLOCKIF_SIG);
 
-	sectors = bc->bc_size / bc->bc_sectsz;
+	sectors = vdsk_capacity(bc) / vdsk_sectorsize(bc);
 
 	/* Clamp the size to the largest possible with CHS */
 	if (sectors > 65535UL*16*255)
@@ -584,7 +547,7 @@ blockif_size(struct blockif_ctxt *bc)
 {
 
 	assert(bc->bc_magic == BLOCKIF_SIG);
-	return (bc->bc_size);
+	return (vdsk_capacity(bc));
 }
 
 int
@@ -592,7 +555,7 @@ blockif_sectsz(struct blockif_ctxt *bc)
 {
 
 	assert(bc->bc_magic == BLOCKIF_SIG);
-	return (bc->bc_sectsz);
+	return (vdsk_sectorsize(bc));
 }
 
 int

Modified: user/marcel/libvdsk/bhyveload/Makefile
==============================================================================
--- user/marcel/libvdsk/bhyveload/Makefile	Fri Nov 21 03:19:51 2014	(r274782)
+++ user/marcel/libvdsk/bhyveload/Makefile	Fri Nov 21 03:22:22 2014	(r274783)
@@ -4,8 +4,8 @@ PROG=	bhyveload
 SRCS=	bhyveload.c
 MAN=	bhyveload.8
 
-DPADD+=	${LIBVMMAPI} ${LIBUTIL}
-LDADD+=	-lvmmapi -lutil
+DPADD+=	${LIBVDSK} ${LIBVMMAPI} ${LIBUTIL}
+LDADD+=	-lvdsk -lvmmapi -lutil
 
 WARNS?=	3
 

Modified: user/marcel/libvdsk/bhyveload/bhyveload.c
==============================================================================
--- user/marcel/libvdsk/bhyveload/bhyveload.c	Fri Nov 21 03:19:51 2014	(r274782)
+++ user/marcel/libvdsk/bhyveload/bhyveload.c	Fri Nov 21 03:22:22 2014	(r274783)
@@ -79,7 +79,7 @@ __FBSDID("$FreeBSD$");
 #include <sysexits.h>
 #include <termios.h>
 #include <unistd.h>
-
+#include <vdsk.h>
 #include <vmmapi.h>
 
 #include "userboot.h"
@@ -92,7 +92,7 @@ __FBSDID("$FreeBSD$");
 
 static char *host_base;
 static struct termios term, oldterm;
-static int disk_fd[NDISKS];
+static vdskctx disk[NDISKS];
 static int ndisks;
 static int consin_fd, consout_fd;
 
@@ -290,9 +290,9 @@ cb_diskread(void *arg, int unit, uint64_
 {
 	ssize_t n;
 
-	if (unit < 0 || unit >= ndisks )
+	if (unit < 0 || unit >= ndisks)
 		return (EIO);
-	n = pread(disk_fd[unit], to, size, from);
+	n = vdsk_read(disk[unit], to, size, from);
 	if (n < 0)
 		return (errno);
 	*resid = size - n;
@@ -302,20 +302,16 @@ cb_diskread(void *arg, int unit, uint64_
 static int
 cb_diskioctl(void *arg, int unit, u_long cmd, void *data)
 {
-	struct stat sb;
 
 	if (unit < 0 || unit >= ndisks)
 		return (EBADF);
 
 	switch (cmd) {
 	case DIOCGSECTORSIZE:
-		*(u_int *)data = 512;
+		*(u_int *)data = vdsk_sectorsize(disk[unit]);
 		break;
 	case DIOCGMEDIASIZE:
-		if (fstat(disk_fd[unit], &sb) == 0)
-			*(off_t *)data = sb.st_size;
-		else
-			return (ENOTTY);
+		*(off_t *)data = vdsk_capacity(disk[unit]);
 		break;
 	default:
 		return (ENOTTY);
@@ -607,21 +603,17 @@ altcons_open(char *path)
 static int
 disk_open(char *path)
 {
-	int err, fd;
+	vdskctx vdsk;
 
-	if (ndisks > NDISKS)
+	if (ndisks >= NDISKS)
 		return (ERANGE);
 
-	err = 0;
-	fd = open(path, O_RDONLY);
-
-	if (fd > 0) {
-		disk_fd[ndisks] = fd;
-		ndisks++;
-	} else 
-		err = errno;
+	vdsk = vdsk_open(path, O_RDONLY, 0);
+	if (vdsk == NULL)
+		return (errno);
 
-	return (err);
+	disk[ndisks++] = vdsk;
+	return (0);
 }
 
 static void


More information about the svn-src-user mailing list