From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Ml22y-0004RQ-33 for qemu-devel@nongnu.org; Tue, 08 Sep 2009 10:51:52 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Ml22t-0004PM-H6 for qemu-devel@nongnu.org; Tue, 08 Sep 2009 10:51:51 -0400 Received: from [199.232.76.173] (port=50514 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ml22s-0004Os-KJ for qemu-devel@nongnu.org; Tue, 08 Sep 2009 10:51:46 -0400 Received: from verein.lst.de ([213.95.11.210]:59265) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_3DES_EDE_CBC_SHA1:24) (Exim 4.60) (envelope-from ) id 1Ml22r-0000l9-R1 for qemu-devel@nongnu.org; Tue, 08 Sep 2009 10:51:46 -0400 Date: Tue, 8 Sep 2009 16:51:42 +0200 From: Christoph Hellwig Message-ID: <20090908145142.GB5169@lst.de> References: <87ljkv416m.fsf@inbox.ru> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87ljkv416m.fsf@inbox.ru> Subject: [Qemu-devel] raw_getlength, was [patch] NetBSD and Dragonfly support List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Aleksej Saushev Cc: qemu-devel@nongnu.org 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 > #endif > > +#ifdef __DragonFly__ > +#include > +#include > +#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 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,