From: Christoph Hellwig <hch@lst.de>
To: Aleksej Saushev <asau@inbox.ru>
Cc: qemu-devel@nongnu.org
Subject: [Qemu-devel] raw_getlength, was [patch] NetBSD and Dragonfly support
Date: Tue, 8 Sep 2009 16:51:42 +0200 [thread overview]
Message-ID: <20090908145142.GB5169@lst.de> (raw)
In-Reply-To: <87ljkv416m.fsf@inbox.ru>
On Fri, Sep 04, 2009 at 11:30:09AM +0400, Aleksej Saushev wrote:
> --- block-raw-posix.c.orig 2009-07-17 03:56:22 +0300
> +++ block-raw-posix.c 2009-08-30 15:51:48 +0300
> @@ -63,6 +63,11 @@
> #include <sys/dkio.h>
> #endif
>
> +#ifdef __DragonFly__
> +#include <sys/ioctl.h>
> +#include <sys/diskslice.h>
> +#endif
This code already exists in current gemu git HEAD.
> //#define DEBUG_BLOCK
> @@ -766,6 +771,15 @@ static int64_t raw_getlength(BlockDrive
> if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
> #ifdef DIOCGMEDIASIZE
> if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
> +#elif defined(DIOCGPART)
> + {
> + struct partinfo pi;
> + if (ioctl(fd, DIOCGPART, &pi) == 0)
> + size = pi.media_size;
> + else
> + size = 0;
> + }
> + if (size == 0)
> #endif
Same for this. But raw_getlength has been turned into an utter
mess. Any chance you could see if the patch below works on the various
BSD platforms?
Signed-off-by: Christoph Hellwig <hch@lst.de>
Index: qemu/block/raw-posix.c
===================================================================
--- qemu.orig/block/raw-posix.c 2009-09-08 11:43:05.933253874 -0300
+++ qemu/block/raw-posix.c 2009-09-08 11:45:01.000031738 -0300
@@ -124,9 +124,6 @@ typedef struct BDRVRawState {
static int fd_open(BlockDriverState *bs);
static int64_t raw_getlength(BlockDriverState *bs);
-#if defined(__FreeBSD__)
-static int cdrom_reopen(BlockDriverState *bs);
-#endif
static int raw_open_common(BlockDriverState *bs, const char *filename,
int bdrv_flags, int open_flags)
@@ -614,80 +611,74 @@ static int64_t raw_getlength(BlockDriver
} else
return st.st_size;
}
-#else /* !__OpenBSD__ */
-static int64_t raw_getlength(BlockDriverState *bs)
+#elif defined(CONFIG_BSD)
+static int64_t raw_getlength(BlockDriverState *bs)
{
BDRVRawState *s = bs->opaque;
int fd = s->fd;
- int64_t size;
-#ifdef CONFIG_BSD
+ int64_t size = 0;
struct stat sb;
-#ifdef __FreeBSD__
- int reopened = 0;
-#endif
-#endif
-#ifdef __sun__
- struct dk_minfo minfo;
- int rv;
-#endif
int ret;
ret = fd_open(bs);
if (ret < 0)
return ret;
-#ifdef CONFIG_BSD
-#ifdef __FreeBSD__
-again:
-#endif
- if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
+ if (fstat(fd, &sb) != 0 || !(S_IFCHR & sb.st_mode)) {
+ return lseek(fd, 0, SEEK_END);
+
#ifdef DIOCGMEDIASIZE
- if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
+ if (!ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
+ return size;
#elif defined(DIOCGPART)
- {
- struct partinfo pi;
- if (ioctl(fd, DIOCGPART, &pi) == 0)
- size = pi.media_size;
- else
- size = 0;
- }
- if (size == 0)
+ {
+ struct partinfo pi;
+ if (ioctl(fd, DIOCGPART, &pi) == 0)
+ return pi.media_size;
+ }
#endif
+
#ifdef CONFIG_COCOA
- size = LONG_LONG_MAX;
+ return LONG_LONG_MAX;
#else
- size = lseek(fd, 0LL, SEEK_END);
-#endif
-#ifdef __FreeBSD__
- switch(s->type) {
- case FTYPE_CD:
- /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
- if (size == 2048LL * (unsigned)-1)
- size = 0;
- /* XXX no disc? maybe we need to reopen... */
- if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
- reopened = 1;
- goto again;
- }
- }
-#endif
- } else
+ return lseek(fd, 0LL, SEEK_END);
#endif
-#ifdef __sun__
+}
+#elif defined(__sun__)
+static int64_t raw_getlength(BlockDriverState *bs)
+{
+ BDRVRawState *s = bs->opaque;
+ struct dk_minfo minfo;
+ int ret;
+
+ ret = fd_open(bs);
+ if (ret < 0)
+ return ret;
+
/*
- * use the DKIOCGMEDIAINFO ioctl to read the size.
+ * Use the DKIOCGMEDIAINFO ioctl to read the size.
*/
- rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
- if ( rv != -1 ) {
- size = minfo.dki_lbsize * minfo.dki_capacity;
- } else /* there are reports that lseek on some devices
- fails, but irc discussion said that contingency
- on contingency was overkill */
-#endif
- {
- size = lseek(fd, 0, SEEK_END);
- }
- return size;
+ ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
+ if (ret != -1) {
+ return minfo.dki_lbsize * minfo.dki_capacity;
+
+ /*
+ * There are reports that lseek on some devices fails, but irc
+ * irc discussion said that contingency on contingency was overkill.
+ */
+ return lseek(s->fd, 0, SEEK_END);
+}
+#else
+static int64_t raw_getlength(BlockDriverState *bs)
+{
+ BDRVRawState *s = bs->opaque;
+ int ret;
+
+ ret = fd_open(bs);
+ if (ret < 0)
+ return ret;
+
+ return lseek(s->fd, 0, SEEK_END);
}
#endif
@@ -1240,9 +1231,27 @@ static int cdrom_reopen(BlockDriverState
return 0;
}
+static int64_t cdrom_getlength(BlockDriverState *bs)
+{
+ int64_t size;
+ int reopened = 0;
+
+ for (;;) {
+ size = raw_getlength(bs);
+
+ /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
+ if (size == 2048LL * (unsigned)-1)
+ size = 0;
+ /* XXX no disc? maybe we need to reopen... */
+ if (size > 0 || reopened || cdrom_reopen(bs) < 0)
+ return size;
+ reopened = 1;
+ }
+}
+
static int cdrom_is_inserted(BlockDriverState *bs)
{
- return raw_getlength(bs) > 0;
+ return cdrom_getlength(bs) > 0;
}
static int cdrom_eject(BlockDriverState *bs, int eject_flag)
@@ -1298,7 +1307,7 @@ static BlockDriver bdrv_host_cdrom = {
.bdrv_read = raw_read,
.bdrv_write = raw_write,
- .bdrv_getlength = raw_getlength,
+ .bdrv_getlength = cdrom_getlength,
/* removable device support */
.bdrv_is_inserted = cdrom_is_inserted,
prev parent reply other threads:[~2009-09-08 14:51 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-09-04 7:30 [Qemu-devel] [patch] NetBSD and Dragonfly support Aleksej Saushev
2009-09-08 14:48 ` Christoph Hellwig
2009-09-08 14:51 ` Christoph Hellwig [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20090908145142.GB5169@lst.de \
--to=hch@lst.de \
--cc=asau@inbox.ru \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).