From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1O9Fcc-0001Am-7Z for qemu-devel@nongnu.org; Tue, 04 May 2010 06:45:02 -0400 Received: from [140.186.70.92] (port=41518 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O9FcW-00018k-Mf for qemu-devel@nongnu.org; Tue, 04 May 2010 06:45:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1O9FcU-0002uO-19 for qemu-devel@nongnu.org; Tue, 04 May 2010 06:44:56 -0400 Received: from verein.lst.de ([213.95.11.210]:37870) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1O9FcT-0002u8-Mb for qemu-devel@nongnu.org; Tue, 04 May 2010 06:44:53 -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 o44AiqWY006465 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Tue, 4 May 2010 12:44:52 +0200 Received: (from hch@localhost) by verein.lst.de (8.12.3/8.12.3/Debian-7.2) id o44Aiq0d006464 for qemu-devel@nongnu.org; Tue, 4 May 2010 12:44:52 +0200 Date: Tue, 4 May 2010 12:44:52 +0200 From: Christoph Hellwig Message-ID: <20100504104452.GD6388@lst.de> References: <20100504104351.GA6342@lst.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20100504104351.GA6342@lst.de> Subject: [Qemu-devel] [PATCH 4/6] cloop: use qemu block API List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Use bdrv_pwrite to access the backing device instead of pread, and convert the driver to implementing the bdrv_open method which gives it an already opened BlockDriverState for the underlying device. Signed-off-by: Christoph Hellwig Index: qemu-kevin/block/cloop.c =================================================================== --- qemu-kevin.orig/block/cloop.c 2010-05-03 13:01:57.918284307 +0200 +++ qemu-kevin/block/cloop.c 2010-05-03 13:02:27.836318598 +0200 @@ -27,7 +27,6 @@ #include typedef struct BDRVCloopState { - int fd; uint32_t block_size; uint32_t n_blocks; uint64_t* offsets; @@ -51,23 +50,20 @@ static int cloop_probe(const uint8_t *bu return 0; } -static int cloop_open(BlockDriverState *bs, const char *filename, int flags) +static int cloop_open(BlockDriverState *bs, int flags) { BDRVCloopState *s = bs->opaque; uint32_t offsets_size,max_compressed_block_size=1,i; - s->fd = open(filename, O_RDONLY | O_BINARY); - if (s->fd < 0) - return -errno; bs->read_only = 1; /* read header */ - if (pread(s->fd, &s->block_size, 4, 128) < 4) { + if (bdrv_pread(bs->file, 128, &s->block_size, 4) < 4) { goto cloop_close; } s->block_size = be32_to_cpu(s->block_size); - if (pread(s->fd, &s->n_blocks, 4, 128 + 4) < 4) { + if (bdrv_pread(bs->file, 128 + 4, &s->n_blocks, 4) < 4) { goto cloop_close; } s->n_blocks = be32_to_cpu(s->n_blocks); @@ -75,7 +71,8 @@ static int cloop_open(BlockDriverState * /* read offsets */ offsets_size = s->n_blocks * sizeof(uint64_t); s->offsets = qemu_malloc(offsets_size); - if (pread(s->fd, s->offsets, offsets_size, 128 + 4 + 4) < offsets_size) { + if (bdrv_pread(bs->file, 128 + 4 + 4, s->offsets, offsets_size) < + offsets_size) { goto cloop_close; } for(i=0;in_blocks;i++) { @@ -99,17 +96,19 @@ static int cloop_open(BlockDriverState * return 0; cloop_close: - close(s->fd); return -1; } -static inline int cloop_read_block(BDRVCloopState *s,int block_num) +static inline int cloop_read_block(BlockDriverState *bs, int block_num) { + BDRVCloopState *s = bs->opaque; + if(s->current_block != block_num) { int ret; uint32_t bytes = s->offsets[block_num+1]-s->offsets[block_num]; - ret = pread(s->fd, s->compressed_block, bytes, s->offsets[block_num]); + ret = bdrv_pread(bs->file, s->offsets[block_num], s->compressed_block, + bytes); if (ret != bytes) return -1; @@ -138,7 +137,7 @@ static int cloop_read(BlockDriverState * for(i=0;isectors_per_block), block_num=(sector_num+i)/s->sectors_per_block; - if(cloop_read_block(s, block_num) != 0) + if(cloop_read_block(bs, block_num) != 0) return -1; memcpy(buf+i*512,s->uncompressed_block+sector_offset_in_block*512,512); } @@ -148,7 +147,6 @@ static int cloop_read(BlockDriverState * static void cloop_close(BlockDriverState *bs) { BDRVCloopState *s = bs->opaque; - close(s->fd); if(s->n_blocks>0) free(s->offsets); free(s->compressed_block); @@ -160,7 +158,7 @@ static BlockDriver bdrv_cloop = { .format_name = "cloop", .instance_size = sizeof(BDRVCloopState), .bdrv_probe = cloop_probe, - .bdrv_file_open = cloop_open, + .bdrv_open = cloop_open, .bdrv_read = cloop_read, .bdrv_close = cloop_close, };