From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LRV0I-0003ko-46 for qemu-devel@nongnu.org; Mon, 26 Jan 2009 12:12:06 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LRV0G-0003jk-Jv for qemu-devel@nongnu.org; Mon, 26 Jan 2009 12:12:05 -0500 Received: from [199.232.76.173] (port=38140 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LRV0G-0003jG-8C for qemu-devel@nongnu.org; Mon, 26 Jan 2009 12:12:04 -0500 Received: from mx2.suse.de ([195.135.220.15]:50812) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1LRV0B-00047s-NV for qemu-devel@nongnu.org; Mon, 26 Jan 2009 12:12:00 -0500 Received: from Relay2.suse.de (mail2.suse.de [195.135.221.8]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx2.suse.de (Postfix) with ESMTP id CCCFF47794 for ; Mon, 26 Jan 2009 17:53:07 +0100 (CET) From: Kevin Wolf Date: Mon, 26 Jan 2009 17:59:26 +0100 Message-Id: <1232989168-27759-6-git-send-email-kwolf@suse.de> In-Reply-To: <1232989168-27759-1-git-send-email-kwolf@suse.de> References: <1232989168-27759-1-git-send-email-kwolf@suse.de> Subject: [Qemu-devel] [PATCH 5/7] block-vpc: Use the qemu block layer Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf Instead of accessing the file directly, use the qemu block layer. Signed-off-by: Kevin Wolf --- block-vpc.c | 59 ++++++++++++++++++++++++++++++++--------------------------- 1 files changed, 32 insertions(+), 27 deletions(-) diff --git a/block-vpc.c b/block-vpc.c index 51644e6..c1d61de 100644 --- a/block-vpc.c +++ b/block-vpc.c @@ -105,7 +105,7 @@ struct vhd_dyndisk_header { }; typedef struct BDRVVPCState { - int fd; + BlockDriverState *hd; int max_table_entries; uint32_t *pagetable; @@ -130,20 +130,18 @@ static int vpc_probe(const uint8_t *buf, int buf_size, const char *filename) static int vpc_open(BlockDriverState *bs, const char *filename, int flags) { BDRVVPCState *s = bs->opaque; - int fd, i; + int ret, i; struct vhd_footer* footer; struct vhd_dyndisk_header* dyndisk_header; uint8_t buf[HEADER_SIZE]; - fd = open(filename, O_RDONLY | O_BINARY); - if (fd < 0) - return -1; - bs->read_only = 1; // no write support yet - s->fd = fd; + ret = bdrv_file_open(&s->hd, filename, flags); + if (ret < 0) + return ret; - if (read(fd, buf, HEADER_SIZE) != HEADER_SIZE) + if (bdrv_pread(s->hd, 0, buf, HEADER_SIZE) != HEADER_SIZE) goto fail; footer = (struct vhd_footer*) buf; @@ -156,8 +154,8 @@ static int vpc_open(BlockDriverState *bs, const char *filename, int flags) bs->total_sectors = (int64_t) be16_to_cpu(footer->cyls) * footer->heads * footer->secs_per_cyl; - lseek(s->fd, be64_to_cpu(footer->data_offset), SEEK_SET); - if (read(fd, buf, HEADER_SIZE) != HEADER_SIZE) + if (bdrv_pread(s->hd, be64_to_cpu(footer->data_offset), buf, HEADER_SIZE) + != HEADER_SIZE) goto fail; footer = NULL; @@ -166,15 +164,16 @@ static int vpc_open(BlockDriverState *bs, const char *filename, int flags) if (strncmp(dyndisk_header->magic, "cxsparse", 8)) goto fail; - lseek(s->fd, be64_to_cpu(dyndisk_header->table_offset), SEEK_SET); s->max_table_entries = be32_to_cpu(dyndisk_header->max_table_entries); s->pagetable = qemu_malloc(s->max_table_entries * 4); if (!s->pagetable) - goto fail; - if (read(s->fd, s->pagetable, s->max_table_entries * 4) != - s->max_table_entries * 4) - goto fail; + goto fail; + + if (bdrv_pread(s->hd, be64_to_cpu(dyndisk_header->table_offset), + s->pagetable, s->max_table_entries * 4) != s->max_table_entries * 4) + goto fail; + for (i = 0; i < s->max_table_entries; i++) be32_to_cpus(&s->pagetable[i]); @@ -190,11 +189,15 @@ static int vpc_open(BlockDriverState *bs, const char *filename, int flags) return 0; fail: - close(fd); + bdrv_delete(s->hd); return -1; } -static inline int seek_to_sector(BlockDriverState *bs, int64_t sector_num) +/* + * Returns the absolute byte offset of the given sector in the image file. + * If the sector is not allocated, -1 is returned instead. + */ +static inline int64_t get_sector_offset(BlockDriverState *bs, int64_t sector_num) { BDRVVPCState *s = bs->opaque; uint64_t offset = sector_num * 512; @@ -241,9 +244,8 @@ static inline int seek_to_sector(BlockDriverState *bs, int64_t sector_num) return -1; // not allocated #endif #endif - lseek(s->fd, block_offset, SEEK_SET); - return 0; + return block_offset; } static int vpc_read(BlockDriverState *bs, int64_t sector_num, @@ -251,16 +253,19 @@ static int vpc_read(BlockDriverState *bs, int64_t sector_num, { BDRVVPCState *s = bs->opaque; int ret; + int64_t offset; while (nb_sectors > 0) { - if (!seek_to_sector(bs, sector_num)) - { - ret = read(s->fd, buf, 512); - if (ret != 512) - return -1; - } - else + offset = get_sector_offset(bs, sector_num); + + if (offset == -1) { memset(buf, 0, 512); + } else { + ret = bdrv_pread(s->hd, offset, buf, 512); + if (ret != 512) + return -1; + } + nb_sectors--; sector_num++; buf += 512; @@ -275,7 +280,7 @@ static void vpc_close(BlockDriverState *bs) #ifdef CACHE qemu_free(s->pageentry_u8); #endif - close(s->fd); + bdrv_delete(s->hd); } BlockDriver bdrv_vpc = { -- 1.6.0.2