simple block driver cleanups
This series cleans up the simple read-only block drivers to use the qemu block device API to access their backing devices, making the code simpler and usable over nbd/curl. I've not touched dmg yet as it's even more bitrot than usual and deserves it's own series.
PATCH 2/6 bochs: use qemu block API
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.
block/bochs.c +9 -21
@@ -80,8 +80,6 @@ struct bochs_header { }; typedef struct BDRVBochsState { - int fd; - uint32_t *catalog_bitmap; int catalog_size;
@@ -109,23 +107,16 @@ static int bochs_probe(const uint8_t *bureturn 0 ; } -static int bochs_open (BlockDriverState *bs,const char *filename,int flags) +static int bochs_open (BlockDriverState *bs,int flags) {BDRVBochsState *s = bs->opaque; -int fd, i; +int i;struct bochs_header bochs;struct bochs_header_v1 header_v1; - fd =open (filename,O_RDONLY |O_BINARY ); -if (fd <0 ) { -return -1 ; - } - bs->read_only =1 ;// no write support yet - s->fd = fd; - -if (pread (fd, &bochs,sizeof (bochs),0 ) !=sizeof (bochs)) { +if (bdrv_pread (bs->file,0 , &bochs,sizeof (bochs)) !=sizeof (bochs)) { goto fail; }
@@ -146,8 +137,8 @@ static int bochs_open(BlockDriverState * s->catalog_size =le32_to_cpu (bochs.extra.redolog.catalog); s->catalog_bitmap =qemu_malloc (s->catalog_size *4 ); -if (pread (s->fd, s->catalog_bitmap, s->catalog_size *4 , -le32_to_cpu (bochs.header)) != s->catalog_size *4 ) +if (bdrv_pread (bs->file,le32_to_cpu (bochs.header), s->catalog_bitmap, + s->catalog_size *4 ) != s->catalog_size *4 ) goto fail;for (i =0 ; i < s->catalog_size; i++)le32_to_cpus (&s->catalog_bitmap[i]);
@@ -161,7 +152,6 @@ static int bochs_open(BlockDriverState *return 0 ; fail: -close (fd);return -1 ; }
@@ -184,8 +174,8 @@ static int64_t seek_to_sector(BlockDrive (s->extent_blocks + s->bitmap_blocks));/* read in bitmap for current extent */ -if (pread (s->fd, &bitmap_entry,1 , bitmap_offset + (extent_offset /8 )) - !=1 ) { +if (bdrv_pread (bs->file, bitmap_offset + (extent_offset /8 ), + &bitmap_entry,1 ) !=1 ) {return -1 ; }
@@ -199,13 +189,12 @@ static int64_t seek_to_sector(BlockDrivestatic int bochs_read (BlockDriverState *bs,int64_t sector_num,uint8_t *buf,int nb_sectors) { -BDRVBochsState *s = bs->opaque;int ret;while (nb_sectors >0 ) {int64_t block_offset =seek_to_sector (bs, sector_num);if (block_offset >=0 ) { - ret =pread (s->fd, buf,512 , block_offset); + ret =bdrv_pread (bs->file, block_offset, buf,512 );if (ret !=512 ) {return -1 ; }
@@ -222,14 +211,13 @@ static void bochs_close(BlockDriverState {BDRVBochsState *s = bs->opaque;qemu_free (s->catalog_bitmap); -close (s->fd); }static BlockDriver bdrv_bochs = { .format_name ="bochs" , .instance_size =sizeof (BDRVBochsState), .bdrv_probe = bochs_probe, - .bdrv_file_open = bochs_open, + .bdrv_open = bochs_open, .bdrv_read = bochs_read, .bdrv_close = bochs_close, };
PATCH 3/6 cloop: use pread
In reply to Christoph HellwigUse pread instead of lseek + read in preparation of using the qemu block API.
block/cloop.c +17 -15
@@ -62,23 +62,22 @@ static int cloop_open(BlockDriverState * bs->read_only =1 ;/* read header */ -if (lseek (s->fd,128 ,SEEK_SET )<0 ) { -cloop_close: -close (s->fd); -return -1 ; +if (pread (s->fd, &s->block_size,4 ,128 ) <4 ) { + goto cloop_close; } -if (read (s->fd,&s->block_size,4 )<4 ) - goto cloop_close; - s->block_size=be32_to_cpu (s->block_size); -if (read (s->fd,&s->n_blocks,4 )<4 ) - goto cloop_close; - s->n_blocks=be32_to_cpu (s->n_blocks); + s->block_size =be32_to_cpu (s->block_size); + +if (pread (s->fd, &s->n_blocks,4 ,128 +4 ) <4 ) { + goto cloop_close; + } + s->n_blocks =be32_to_cpu (s->n_blocks);/* read offsets */ - offsets_size=s->n_blocks*sizeof (uint64_t ); - s->offsets=(uint64_t *)qemu_malloc (offsets_size); -if (read (s->fd,s->offsets,offsets_size)<offsets_size) + 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) { goto cloop_close; + }for (i=0 ;i<s->n_blocks;i++) { s->offsets[i]=be64_to_cpu (s->offsets[i]);if (i>0 ) {
@@ -98,6 +97,10 @@ cloop_close: s->sectors_per_block = s->block_size/512 ; bs->total_sectors = s->n_blocks*s->sectors_per_block;return 0 ; + +cloop_close: +close (s->fd); +return -1 ; }static inline int cloop_read_block (BDRVCloopState *s,int block_num)
@@ -106,8 +109,7 @@ static inline int cloop_read_block(BDRVCint ret;uint32_t bytes = s->offsets[block_num+1 ]-s->offsets[block_num]; -lseek (s->fd, s->offsets[block_num],SEEK_SET ); - ret =read (s->fd, s->compressed_block, bytes); + ret =pread (s->fd, s->compressed_block, bytes, s->offsets[block_num]);if (ret != bytes)return -1 ;
PATCH 4/6 cloop: use qemu block API
In reply to Christoph HellwigUse 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.
block/cloop.c +12 -14
@@ -27,7 +27,6 @@#include <zlib.h> 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 *bureturn 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 ;i<s->n_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 ;i<nb_sectors;i++) {uint32_t sector_offset_in_block=((sector_num+i)%s->sectors_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, };
PATCH 5/6 parallels: use pread
In reply to Christoph HellwigUse pread instead of lseek + read in preparation of using the qemu block API.
block/parallels.c +11 -22
@@ -83,7 +83,7 @@ static int parallels_open(BlockDriverSta s->fd = fd; -if (read (fd, &ph,sizeof (ph)) !=sizeof (ph)) +if (pread (fd, &ph,sizeof (ph),0 ) !=sizeof (ph)) goto fail; if (memcmp(ph.magic, HEADER_MAGIC, 16) ||
@@ -93,14 +93,11 @@ static int parallels_open(BlockDriverSta bs->total_sectors =le32_to_cpu (ph.nb_sectors); -if (lseek (s->fd,64 ,SEEK_SET ) !=64 ) - goto fail; - s->tracks =le32_to_cpu (ph.tracks); s->catalog_size =le32_to_cpu (ph.catalog_entries); s->catalog_bitmap =qemu_malloc (s->catalog_size *4 ); -if (read (s->fd, s->catalog_bitmap, s->catalog_size *4 ) != +if (pread (s->fd, s->catalog_bitmap, s->catalog_size *4 ,64 ) != s->catalog_size *4 ) goto fail;for (i =0 ; i < s->catalog_size; i++)
@@ -114,28 +111,18 @@ fail:return -1 ; } -static inline int seek_to_sector (BlockDriverState *bs,int64_t sector_num) +static int64_t seek_to_sector (BlockDriverState *bs,int64_t sector_num) {BDRVParallelsState *s = bs->opaque;uint32_t index, offset; -uint64_t position; index = sector_num / s->tracks; offset = sector_num % s->tracks; -// not allocated +/* not allocated */ if ((index > s->catalog_size) || (s->catalog_bitmap[index] ==0 ))return -1 ; - - position = (uint64_t )(s->catalog_bitmap[index] + offset) *512 ; - -// fprintf(stderr, "sector: %llx index=%x offset=%x pointer=%x position=%x\n", -// sector_num, index, offset, s->catalog_bitmap[index], position); - -if (lseek (s->fd, position,SEEK_SET ) != position) -return -1 ; - -return 0 ; +return (uint64_t )(s->catalog_bitmap[index] + offset) *512 ; } static int parallels_read(BlockDriverState *bs, int64_t sector_num,
@@ -144,11 +131,13 @@ static int parallels_read(BlockDriverStaBDRVParallelsState *s = bs->opaque;while (nb_sectors >0 ) { -if (!seek_to_sector (bs, sector_num)) { -if (read (s->fd, buf,512 ) !=512 ) -return -1 ; - }else +uint64_t position =seek_to_sector (bs, sector_num); +if (position >=0 ) { +if (pread (s->fd, buf,512 , position) !=512 ) +return -1 ; + }else {memset (buf,0 ,512 ); + } nb_sectors--; sector_num++; buf +=512 ;
PATCH 6/6 parallels: use qemu block API
In reply to Christoph HellwigUse 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.
block/parallels.c +6 -18
@@ -46,7 +46,6 @@ struct parallels_header { } __attribute__((packed)); typedef struct BDRVParallelsState { - int fd; uint32_t *catalog_bitmap; int catalog_size;
@@ -68,22 +67,15 @@ static int parallels_probe(const uint8_treturn 0 ; } -static int parallels_open (BlockDriverState *bs,const char *filename,int flags) +static int parallels_open (BlockDriverState *bs,int flags) {BDRVParallelsState *s = bs->opaque; -int fd, i; +int i;struct parallels_header ph; - fd =open (filename,O_RDONLY |O_BINARY |O_LARGEFILE ); -if (fd <0 ) { -return -1 ; - } - bs->read_only =1 ;// no write support yet - s->fd = fd; - -if (pread (fd, &ph,sizeof (ph),0 ) !=sizeof (ph)) +if (bdrv_pread (bs->file,0 , &ph,sizeof (ph)) !=sizeof (ph)) goto fail; if (memcmp(ph.magic, HEADER_MAGIC, 16) ||
@@ -97,7 +89,7 @@ static int parallels_open(BlockDriverSta s->catalog_size =le32_to_cpu (ph.catalog_entries); s->catalog_bitmap =qemu_malloc (s->catalog_size *4 ); -if (pread (s->fd, s->catalog_bitmap, s->catalog_size *4 ,64 ) != +if (bdrv_pread (bs->file,64 , s->catalog_bitmap, s->catalog_size *4 ) != s->catalog_size *4 ) goto fail;for (i =0 ; i < s->catalog_size; i++)
@@ -107,7 +99,6 @@ static int parallels_open(BlockDriverSta fail:if (s->catalog_bitmap)qemu_free (s->catalog_bitmap); -close (fd);return -1 ; }
@@ -128,12 +119,10 @@ static int64_t seek_to_sector(BlockDrivestatic int parallels_read (BlockDriverState *bs,int64_t sector_num,uint8_t *buf,int nb_sectors) { -BDRVParallelsState *s = bs->opaque; -while (nb_sectors >0 ) {uint64_t position =seek_to_sector (bs, sector_num);if (position >=0 ) { -if (pread (s->fd, buf,512 , position) !=512 ) +if (bdrv_pread (bs->file, position, buf,512 ) !=512 )return -1 ; }else {memset (buf,0 ,512 );
@@ -149,14 +138,13 @@ static void parallels_close(BlockDriverS {BDRVParallelsState *s = bs->opaque;qemu_free (s->catalog_bitmap); -close (s->fd); }static BlockDriver bdrv_parallels = { .format_name ="parallels" , .instance_size =sizeof (BDRVParallelsState), .bdrv_probe = parallels_probe, - .bdrv_file_open = parallels_open, + .bdrv_open = parallels_open, .bdrv_read = parallels_read, .bdrv_close = parallels_close, };
Am 04.05.2010 12:43, schrieb Christoph Hellwig:
Quoted 4 lines
This series cleans up the simple read-only block drivers to use the qemu block device API to access their backing devices, making the code simpler and usable over nbd/curl. I've not touched dmg yet as it's even more bitrot than usual and deserves it's own series.
Have you already added something to qemu-iotests locally to test this or did you test manually?
Kevin
Quoted 8 lines · Kevin Wolf
Am 04.05.2010 12:43, schrieb Christoph Hellwig:
Quoted 4 lines
This series cleans up the simple read-only block drivers to use the qemu block device API to access their backing devices, making the code simpler and usable over nbd/curl. I've not touched dmg yet as it's even more bitrot than usual and deserves it's own series.
Have you already added something to qemu-iotests locally to test this or did you test manually?
I tested cloop manually, I haven't found usable creation tools for the others yet.
Re: [Qemu-devel] [PATCH 5/6] parallels: use pread
In reply to Christoph HellwigAm 04.05.2010 12:45, schrieb Christoph Hellwig:
Quoted 67 lines
Use pread instead of lseek + read in preparation of using the qemu block API.
Signed-off-by Christoph Hellwig <[email protected]>Index:
qemu-kevin/block/parallels.c
===================================================================block/parallels.c +5
-18@@ -83,7 +83,7 @@ static int parallels_open(BlockDriverSta s->fd = fd; -if (read (fd, &ph,sizeof (ph)) !=sizeof (ph)) +if (pread (fd, &ph,sizeof (ph),0 ) !=sizeof (ph)) goto fail; if (memcmp(ph.magic, HEADER_MAGIC, 16) ||@@ -93,14 +93,11 @@ static int parallels_open(BlockDriverSta bs->total_sectors =le32_to_cpu (ph.nb_sectors); -if (lseek (s->fd,64 ,SEEK_SET ) !=64 ) - goto fail; - s->tracks =le32_to_cpu (ph.tracks); s->catalog_size =le32_to_cpu (ph.catalog_entries); s->catalog_bitmap =qemu_malloc (s->catalog_size *4 ); -if (read (s->fd, s->catalog_bitmap, s->catalog_size *4 ) != +if (pread (s->fd, s->catalog_bitmap, s->catalog_size *4 ,64 ) != s->catalog_size *4 ) goto fail;for (i =0 ; i < s->catalog_size; i++)@@ -114,28 +111,18 @@ fail:return -1 ; } -static inline int seek_to_sector (BlockDriverState *bs,int64_t sector_num) +static int64_t seek_to_sector (BlockDriverState *bs,int64_t sector_num) {BDRVParallelsState *s = bs->opaque;uint32_t index, offset; -uint64_t position; index = sector_num / s->tracks; offset = sector_num % s->tracks; -// not allocated +/* not allocated */ if ((index > s->catalog_size) || (s->catalog_bitmap[index] ==0 ))return -1 ; - - position = (uint64_t )(s->catalog_bitmap[index] + offset) *512 ; - -// fprintf(stderr, "sector: %llx index=%x offset=%x pointer=%x position=%x\n", -// sector_num, index, offset, s->catalog_bitmap[index], position); - -if (lseek (s->fd, position,SEEK_SET ) != position) -return -1 ; - -return 0 ; +return (uint64_t )(s->catalog_bitmap[index] + offset) *512 ; } static int parallels_read(BlockDriverState *bs, int64_t sector_num,
block/parallels.c static int parallels_read(BlockDriverSta · line 135
Quoted 9 lines
block/parallels.c +2
-4@@ -144,11 +131,13 @@ static int parallels_read(BlockDriverStaBDRVParallelsState *s = bs->opaque;while (nb_sectors >0 ) { -if (!seek_to_sector (bs, sector_num)) { -if (read (s->fd, buf,512 ) !=512 ) -return -1 ; - }else +uint64_t position =seek_to_sector (bs, sector_num); +if (position >=0 ) {
position should be a signed int64_t, otherwise the condition is always true.
Kevin
Am 04.05.2010 15:22, schrieb Christoph Hellwig:
Quoted 7 lines · Kevin Wolf
Quoted 6 lines
Am 04.05.2010 12:43, schrieb Christoph Hellwig:
Quoted 4 lines
This series cleans up the simple read-only block drivers to use the qemu block device API to access their backing devices, making the code simpler and usable over nbd/curl. I've not touched dmg yet as it's even more bitrot than usual and deserves it's own series.
Quoted 5 lines
Quoted 2 lines
Have you already added something to qemu-iotests locally to test this or did you test manually?
I tested cloop manually, I haven't found usable creation tools for the others yet.
Okay, so I have reviewed the patches now and except for that one thing in parallels they look good.
For cloop I trust your test, and for bochs I did a very basic test myself (however, I doubt that anyone uses this driver, considering how hard it is to create such an image...). For parallels I still need to find out how to create an image.
I've applied cloop and bochs to the block branch, and I'll wait for a v2 with the parallels patches.
Kevin
Quoted 4 lines · Kevin Wolf
For cloop I trust your test, and for bochs I did a very basic test myself (however, I doubt that anyone uses this driver, considering how hard it is to create such an image...). For parallels I still need to find out how to create an image.
Btw, I did a bit more testing than my trivial qemu-io testing before, and it seem that cloop is utterly broken in qemu mainline already. Just booting with a small cloop compressed filesystem image attached is enough to give me tons of I/O errors. When using ide I can even crash qemu by trying to mount it.
Re: [Qemu-devel] [PATCH 5/6] parallels: use pread
In reply to Kevin WolfQuoted 1 line · Kevin Wolf
position should be a signed
int64_t, otherwise the condition is always true.
Indeed, I'll update the patch.