$NetBSD$

--- lib/device/dev-io.c	2007/11/21 07:34:34	1.1
+++ lib/device/dev-io.c	2007/11/21 07:37:33
@@ -36,6 +36,9 @@
 #  ifndef BLKGETSIZE64		/* fs.h out-of-date */
 #    define BLKGETSIZE64 _IOR(0x12, 114, size_t)
 #  endif /* BLKGETSIZE64 */
+#elif __NetBSD__
+#  include <sys/disk.h>
+#  include <sys/disklabel.h>
 #else
 #  include <sys/disk.h>
 #  define BLKBSZGET DKIOCGETBLOCKSIZE
@@ -51,7 +54,11 @@
 #  endif
 #endif
 
+#ifdef __NetBSD__
+static struct list _open_devices = { &(_open_devices), &(_open_devices) }; /* inline expand the LIST_INIT macro, as it clashes with the one in <sys/queue.h> */
+#else
 static LIST_INIT(_open_devices);
+#endif
 
 /*-----------------------------------------------------------------
  * The standard io loop that keeps submitting an io until it's
@@ -125,12 +132,23 @@
 static int _get_block_size(struct device *dev, unsigned int *size)
 {
 	const char *name = dev_name(dev);
+#ifdef __NetBSD__
+	struct disklabel	lab;
+#endif
 
 	if ((dev->block_size == -1)) {
+#ifdef __NetBSD__
+		if (ioctl(dev_fd(dev), DIOCGDINFO, &lab) < 0) {
+			log_sys_error("ioctl DIOCGDINFO", name);
+			return 0;
+		}
+		dev->block_size = lab.d_secsize;
+#else
 		if (ioctl(dev_fd(dev), BLKBSZGET, &dev->block_size) < 0) {
 			log_sys_error("ioctl BLKBSZGET", name);
 			return 0;
 		}
+#endif
 		log_debug("%s: block size is %u bytes", name, dev->block_size);
 	}
 
@@ -244,12 +262,29 @@
 {
 	int fd;
 	const char *name = dev_name(dev);
+#ifdef __NetBSD__
+	struct disklabel	lab;
+#endif
 
 	if ((fd = open(name, O_RDONLY)) < 0) {
 		log_sys_error("open", name);
 		return 0;
 	}
 
+#ifdef __NetBSD__
+	if ((*size = lseek (fd, 0, SEEK_END)) < 0) {
+		log_sys_error("lseek SEEK_END", name);
+		close(fd);
+		return 0;
+	}
+	if (ioctl(fd, DIOCGDINFO, &lab) < 0) {
+		log_sys_error("ioctl DIOCGDINFO", name);
+		close(fd);
+		return 0;	
+	}
+	if (lab.d_secsize)
+		*size /= lab.d_secsize;
+#else
 	if (ioctl(fd, BLKGETSIZE64, size) < 0) {
 		log_sys_error("ioctl BLKGETSIZE64", name);
 		if (close(fd))
@@ -258,6 +293,7 @@
 	}
 
 	*size >>= BLKSIZE_SHIFT;	/* Convert to sectors */
+#endif
 	if (close(fd))
 		log_sys_error("close", name);
 
@@ -310,8 +346,10 @@
 
 void dev_flush(struct device *dev)
 {
+#ifdef __linux__
 	if (!(dev->flags & DEV_REGULAR) && ioctl(dev->fd, BLKFLSBUF, 0) >= 0)
 		return;
+#endif
 
 	if (fsync(dev->fd) >= 0)
 		return;
