From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1M8V5m-0004ev-P7 for qemu-devel@nongnu.org; Mon, 25 May 2009 03:59:30 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1M8V5i-0004d1-7m for qemu-devel@nongnu.org; Mon, 25 May 2009 03:59:30 -0400 Received: from [199.232.76.173] (port=43357 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1M8V5h-0004cw-Vf for qemu-devel@nongnu.org; Mon, 25 May 2009 03:59:26 -0400 Received: from mx20.gnu.org ([199.232.41.8]:41421) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1M8V5h-0005gZ-Ci for qemu-devel@nongnu.org; Mon, 25 May 2009 03:59:25 -0400 Received: from verein.lst.de ([213.95.11.210]) by mx20.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1M8V5g-000519-GE for qemu-devel@nongnu.org; Mon, 25 May 2009 03:59:24 -0400 Received: from verein.lst.de (localhost [127.0.0.1]) by verein.lst.de (8.12.3/8.12.3/Debian-7.1) with ESMTP id n4P7xMIF003021 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Mon, 25 May 2009 09:59:23 +0200 Received: (from hch@localhost) by verein.lst.de (8.12.3/8.12.3/Debian-6.6) id n4P7xMjB003019 for qemu-devel@nongnu.org; Mon, 25 May 2009 09:59:22 +0200 Date: Mon, 25 May 2009 09:59:22 +0200 From: Christoph Hellwig Message-ID: <20090525075922.GB2936@lst.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Subject: [Qemu-devel] [PATCH 2/4] raw-posix: add a raw_open_common helper List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org raw_open and hdev_open contain the same basic logic. Add a new raw_open_common helper containing the guts of the open routine and call it from raw_open and hdev_open. We use the new open_flags field in BDRVRawState to allow passing additional open flags to raw_open_common from both. Signed-off-by: Christoph Hellwig Index: qemu/block/raw-posix.c =================================================================== --- qemu.orig/block/raw-posix.c 2009-05-25 09:20:13.949939473 +0200 +++ qemu/block/raw-posix.c 2009-05-25 09:21:56.307963685 +0200 @@ -124,7 +124,8 @@ static int cd_open(BlockDriverState *bs) static int raw_is_inserted(BlockDriverState *bs); -static int raw_open(BlockDriverState *bs, const char *filename, int flags) +static int raw_open_common(BlockDriverState *bs, const char *filename, + int flags) { BDRVRawState *s = bs->opaque; int fd, ret; @@ -140,8 +141,6 @@ static int raw_open(BlockDriverState *bs s->open_flags |= O_RDONLY; bs->read_only = 1; } - if (flags & BDRV_O_CREAT) - s->open_flags |= O_CREAT | O_TRUNC; /* Use O_DSYNC for write-through caching, no flags for write-back caching, * and O_DIRECT for no caching. */ @@ -150,8 +149,7 @@ static int raw_open(BlockDriverState *bs else if (!(flags & BDRV_O_CACHE_WB)) s->open_flags |= O_DSYNC; - s->type = FTYPE_FILE; - + s->fd = -1; fd = open(filename, s->open_flags, 0644); if (fd < 0) { ret = -errno; @@ -172,6 +170,17 @@ static int raw_open(BlockDriverState *bs return 0; } +static int raw_open(BlockDriverState *bs, const char *filename, int flags) +{ + BDRVRawState *s = bs->opaque; + + s->type = FTYPE_FILE; + if (flags & BDRV_O_CREAT) + s->open_flags |= O_CREAT | O_TRUNC; + + return raw_open_common(bs, filename, flags); +} + /* XXX: use host sector size if necessary with: #ifdef DIOCGSECTORSIZE { @@ -942,9 +951,7 @@ kern_return_t GetBSDPath( io_iterator_t static int hdev_open(BlockDriverState *bs, const char *filename, int flags) { BDRVRawState *s = bs->opaque; - int fd, ret; - - posix_aio_init(); + int ret; #ifdef CONFIG_COCOA if (strstart(filename, "/dev/cdrom", NULL)) { @@ -972,19 +979,6 @@ static int hdev_open(BlockDriverState *b IOObjectRelease( mediaIterator ); } #endif - s->open_flags |= O_BINARY; - if ((flags & BDRV_O_ACCESS) == O_RDWR) { - s->open_flags |= O_RDWR; - } else { - s->open_flags |= O_RDONLY; - bs->read_only = 1; - } - /* Use O_DSYNC for write-through caching, no flags for write-back caching, - * and O_DIRECT for no caching. */ - if ((flags & BDRV_O_NOCACHE)) - s->open_flags |= O_DIRECT; - else if (!(flags & BDRV_O_CACHE_WB)) - s->open_flags |= O_DSYNC; s->type = FTYPE_FILE; #if defined(__linux__) @@ -1008,15 +1002,11 @@ static int hdev_open(BlockDriverState *b s->type = FTYPE_CD; } #endif - s->fd = -1; - fd = open(filename, s->open_flags, 0644); - if (fd < 0) { - ret = -errno; - if (ret == -EROFS) - ret = -EACCES; + + ret = raw_open_common(bs, filename, flags); + if (ret) return ret; - } - s->fd = fd; + #if defined(__FreeBSD__) /* make sure the door isnt locked at this time */ if (s->type == FTYPE_CD)