# HG changeset patch
# User cegger
# Date 1351254326 -7200
NetBSD blktap support

diff -r 7a5eb2d4ffeb -r 39bda8366490 tools/Makefile
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -26,6 +26,8 @@ SUBDIRS-$(CONFIG_Linux) += memshr
 SUBDIRS-$(CONFIG_Linux) += blktap
 SUBDIRS-$(CONFIG_Linux) += blktap2
 SUBDIRS-$(CONFIG_NetBSD) += $(SUBDIRS-libaio)
+#SUBDIRS-$(CONFIG_NetBSD) += memshr 
+SUBDIRS-$(CONFIG_NetBSD) += blktap
 SUBDIRS-$(CONFIG_NetBSD) += blktap2
 SUBDIRS-$(CONFIG_NetBSD) += xenbackendd
 SUBDIRS-y += libfsimage
diff -r 7a5eb2d4ffeb -r 39bda8366490 tools/blktap/drivers/Makefile
--- a/tools/blktap/drivers/Makefile
+++ b/tools/blktap/drivers/Makefile
@@ -50,9 +50,11 @@ BLK-OBJS-y  += block-qcow2.o
 BLK-OBJS-y  += aes.o
 BLK-OBJS-y  += tapaio.o
 BLK-OBJS-$(CONFIG_Linux) += blk_linux.o
+BLK-OBJS-$(CONFIG_NetBSD) += blk_netbsd.o
 
 BLKTAB-OBJS-y := blktapctrl.o
 BLKTAB-OBJS-$(CONFIG_Linux) += blktapctrl_linux.o
+BLKTAB-OBJS-$(CONFIG_NetBSD) += blktapctrl_netbsd.o
 
 all: $(IBIN) qcow-util
 
diff -r 7a5eb2d4ffeb -r 39bda8366490 tools/blktap/drivers/blk_netbsd.c
--- /dev/null
+++ b/tools/blktap/drivers/blk_netbsd.c
@@ -0,0 +1,39 @@
+#include <inttypes.h>
+#include <sys/ioctl.h>
+#include <sys/disklabel.h>
+#include "tapdisk.h"
+#include "blk.h"
+
+int blk_getimagesize(int fd, uint64_t *size)
+{
+	int rc;
+	struct disklabel dl;
+
+	*size = 0;
+	rc = ioctl(fd, DIOCGDINFO, &dl);
+	if (rc) {
+		DPRINTF("ERR: DIOCGDINFO failed, couldn't stat image");
+		return -EINVAL;
+	}
+
+	*size = dl.d_secsize * dl.d_secpercyl;
+
+	return 0;
+}
+
+int blk_getsectorsize(int fd, uint64_t *sector_size)
+{
+	int rc;
+	struct disklabel dl;
+
+	*sector_size = DEV_BSIZE;
+	rc = ioctl(fd, DIOCGDINFO, &dl);
+	if (rc) {
+		DPRINTF("ERR: DIOCGDINFO failed, couldn't stat image");
+		return 0; /* fallback to DEV_BSIZE */
+	}
+
+	*sector_size = dl.d_secsize;
+	return 0;
+}
+
diff -r 7a5eb2d4ffeb -r 39bda8366490 tools/blktap/drivers/blktapctrl_netbsd.c
--- /dev/null
+++ b/tools/blktap/drivers/blktapctrl_netbsd.c
@@ -0,0 +1,40 @@
+
+#include "blktaplib.h"
+#include "blktapctrl.h"
+
+#include <dev/pud/pud_msgif.h>
+#include <fcntl.h>
+#include <paths.h>
+#include <stdio.h>
+#include <string.h>
+
+int blktap_interface_open(void)
+{
+	int fd;
+
+	fd = open(_PATH_PUD, O_RDWR);
+	if (fd == -1)
+		fprintf(stderr, "can't open %s\n", _PATH_PUD);
+
+	return fd;
+}
+
+int blktap_interface_create(int ctlfd, int *major, int *minor, blkif_t *blkif)
+{
+	struct pud_conf_reg pcr;
+	int ret;
+
+	memset(&pcr, 0, sizeof(pcr));
+	pcr.pm_pdr.pdr_pth.pth_framelen = sizeof(struct pud_conf_reg);
+	pcr.pm_version = PUD_VERSION;
+#if 0
+	pcr. = blkif->domid;
+	pcr. = blkif->be_id;
+#endif
+	ret = write(ctlfd, &pcr, pcr.pm_pdr.pdr_pth.pth_framelen);
+	if (ret == -1)
+		fprintf(stderr, "Can't configure interface\n");
+
+	/* not yet implemented */
+	return -1;
+}
diff -r 7a5eb2d4ffeb -r 39bda8366490 tools/blktap2/drivers/block-log.c
--- a/tools/blktap2/drivers/block-log.c
+++ b/tools/blktap2/drivers/block-log.c
@@ -227,11 +227,13 @@ static int shmem_open(struct tdlog_state
 
   path_escape(s->shmpath + 5, strlen(name));
 
+#if defined(__linux__)
   if ((fd = shm_open(s->shmpath, O_CREAT|O_RDWR, 0750)) < 0) {
     BWPRINTF("could not open shared memory file %s: %s", s->shmpath,
 	     strerror(errno));
     goto err;
   }
+#endif
   if (ftruncate(fd, SHMSIZE) < 0) {
     BWPRINTF("error truncating shmem to size %u", SHMSIZE);
     close(fd);
@@ -261,7 +263,9 @@ static int shmem_close(struct tdlog_stat
   }
 
   if (s->shmpath) {
+#if defined(__linux__)
     shm_unlink(s->shmpath);
+#endif
     s->shmpath = NULL;
   }
 
diff -r 7a5eb2d4ffeb -r 39bda8366490 tools/blktap2/drivers/tapdisk-client.c
--- a/tools/blktap2/drivers/tapdisk-client.c
+++ b/tools/blktap2/drivers/tapdisk-client.c
@@ -243,11 +243,13 @@ static int writelog_map(struct writelog*
   int fd;
   void* shm;
 
+#if defined(__linux__)
   if ((fd = shm_open(wl->shmpath, O_RDWR, 0750)) < 0) {
     BWPRINTF("could not open shared memory at %s: %s", wl->shmpath,
 	     strerror(errno));
     return -1;
   }
+#endif
 
   wl->shm = mmap(NULL, wl->shmsize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
   close(fd);
diff -r 7a5eb2d4ffeb -r 39bda8366490 tools/misc/xend
--- a/tools/misc/xend
+++ b/tools/misc/xend
@@ -74,7 +74,8 @@ def start_daemon(daemon, *args):
         os.execvp(daemon, (daemon,) + args)
 
 def start_blktapctrl():
-    start_daemon("blktapctrl", "")
+    if os.uname()[0] == 'Linux':
+        start_daemon("blktapctrl", "")
 
 def main():
     try: