Remove ->bdrv_pread() internal block layer API (v2)
cover Remove ->bdrv_pread() internal block layer API (v2)
The internal block layer API has a wart in the form of the ->bdrv_pread() and ->bdrv_pwrite() methods. These duplicate bdrv_read() and bdrv_write(), and are unnatural for a block device in that they are not sector oriented.
One of the reasons for their existence is the scsi-generic host support. That is also a wart, as scsi-generic is really a packet (command/response) rather than a block (random access to sectors) API. There is also the wierd negative sector count == byte count thing.
This patchset addresses these warts by adding a new API for scsi-generic and dropping ->bdrv_pread() and bdrv_pwrite(). While the improvement falls short of spectacular, it does make further hacking on the block layer easier as there are fewer callbacks to worry about.
---
Changes since v1:
- rebased against trunk
- performance testing: tested 'rm -rf *; tar xjf ../linux-2.6.22.tar.bz2' with a vm snapshot. While there are significant variances among runs (15-25sec), likely due to different ext3 allocation decisions and different host caching decisions, there seems to be no regression due to this patchset.
Avi Kivity (3):
Add specialized block driver scsi generic API
Add internal scsi generic block API
Drop internal bdrv_pread()/bdrv_pwrite() APIs
block-raw-posix.c | 50 ++++++++++++++++++++++++-- block-raw-win32.c | 20 ++++++---- block.c | 101 +++++++++++++++-------------------------------------- block.h | 8 ++++ block_int.h | 14 +++++-- hw/scsi-generic.c | 39 +++++++++++--------- 6 files changed, 126 insertions(+), 106 deletions(-)
PATCH 1/3 Add specialized block driver scsi generic API
When a scsi device is backed by a scsi generic device instead of an ordinary host block device, the block API is abused in a couple of annoying ways:
- nb_sectors is negative, and specifies a byte count instead of a sector count
- offset is ignored, since scsi-generic is essentially a packet protocol
This overloading makes hacking the block layer difficult. Remove it by introducing a new explicit API for scsi-generic devices. The new API is still backed by the old implementation, but at least the users are insulated.
--- block.c | 22 ++++++++++++++++++++++ block.h | 8 ++++++++ hw/scsi-generic.c | 39 ++++++++++++++++++++++----------------- 3 files changed, 52 insertions(+), 17 deletions(-)
block.c +22 -0
@@ -1675,3 +1675,25 @@ int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)return drv->bdrv_ioctl (bs, req, buf);return -ENOTSUP ; } + +int bdrv_sg_send_command (BlockDriverState *bs,void *buf,int count) +{ +return bdrv_pwrite (bs,-1 , buf, count); +} + +int bdrv_sg_recv_response (BlockDriverState *bs,void *buf,int count) +{ +return bdrv_pread (bs,-1 , buf, count); +} + +BlockDriverAIOCB *bdrv_sg_aio_read (BlockDriverState *bs,void *buf,int count, +BlockDriverCompletionFunc *cb,void *opaque) +{ +return bdrv_aio_read (bs,0 , buf, -(int64_t )count, cb, opaque); +} + +BlockDriverAIOCB *bdrv_sg_aio_write (BlockDriverState *bs,void *buf,int count, +BlockDriverCompletionFunc *cb,void *opaque) +{ +return bdrv_aio_write (bs,0 , buf, -(int64_t )count, cb, opaque); +}
block.h +8 -0
@@ -101,6 +101,14 @@ BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num, BlockDriverCompletionFunc *cb, void *opaque);void bdrv_aio_cancel (BlockDriverAIOCB *acb); +/* sg packet commands */ +int bdrv_sg_send_command (BlockDriverState *bs,void *buf,int count); +int bdrv_sg_recv_response (BlockDriverState *bs,void *buf,int count); +BlockDriverAIOCB *bdrv_sg_aio_read (BlockDriverState *bs,void *buf,int count, +BlockDriverCompletionFunc *cb,void *opaque); +BlockDriverAIOCB *bdrv_sg_aio_write (BlockDriverState *bs,void *buf,int count, +BlockDriverCompletionFunc *cb,void *opaque); +/* Ensure contents are flushed to disk. */ void bdrv_flush (BlockDriverState *bs);void bdrv_flush_all (void );
hw/scsi-generic.c +22 -17
@@ -201,6 +201,7 @@ static int execute_command(BlockDriverState *bdrv, SCSIRequest *r, int direction, BlockDriverCompletionFunc *complete) { + int ret; r->io_header.interface_id ='S' ; r->io_header.dxfer_direction = direction;
@@ -214,25 +215,27 @@ static int execute_command(BlockDriverState *bdrv, r->io_header.usr_ptr = r; r->io_header.flags |=SG_FLAG_DIRECT_IO ; -if (bdrv_pwrite (bdrv,-1 , &r->io_header,sizeof (r->io_header)) ==-1 ) { + ret =bdrv_sg_send_command (bdrv, &r->io_header,sizeof (r->io_header)); +if (ret <0 ) {BADF ("execute_command: write failed ! (%d)\n" , errno);return -1 ; }if (complete ==NULL ) {int ret; r->aiocb =NULL ; -while ((ret =bdrv_pread (bdrv,-1 , &r->io_header, -sizeof (r->io_header))) ==-1 && - errno ==EINTR ); -if (ret ==-1 ) { +while ((ret =bdrv_sg_recv_response (bdrv, &r->io_header, +sizeof (r->io_header))) <0 && + ret == -EINTR ) + ; +if (ret <0 ) {BADF ("execute_command: read failed !\n" );return -1 ; }return 0 ; } - r->aiocb =bdrv_aio_read (bdrv,0 , (uint8_t *)&r->io_header, - -(int64_t )sizeof (r->io_header), complete, r); + r->aiocb =bdrv_sg_aio_read (bdrv, (uint8_t *)&r->io_header, +sizeof (r->io_header), complete, r);if (r->aiocb ==NULL ) {BADF ("execute_command: read failed !\n" );return -1 ;
@@ -634,14 +637,15 @@ static int get_blocksize(BlockDriverState *bdrv) io_header.sbp = sensebuf; io_header.timeout =6000 ;/* XXX */ - ret =bdrv_pwrite (bdrv,-1 , &io_header,sizeof (io_header)); -if (ret ==-1 ) + ret =bdrv_sg_send_command (bdrv, &io_header,sizeof (io_header)); +if (ret <0 )return -1 ; -while ((ret =bdrv_pread (bdrv,-1 , &io_header,sizeof (io_header))) ==-1 && - errno ==EINTR ); +while ((ret =bdrv_sg_recv_response (bdrv, &io_header,sizeof (io_header))) <0 && + ret == -EINTR ) + ; -if (ret ==-1 ) +if (ret <0 )return -1 ;return (buf[4 ] <<24 ) | (buf[5 ] <<16 ) | (buf[6 ] <<8 ) | buf[7 ];
@@ -671,14 +675,15 @@ static int get_stream_blocksize(BlockDriverState *bdrv) io_header.sbp = sensebuf; io_header.timeout =6000 ;/* XXX */ - ret =bdrv_pwrite (bdrv,-1 , &io_header,sizeof (io_header)); -if (ret ==-1 ) + ret =bdrv_sg_send_command (bdrv, &io_header,sizeof (io_header)); +if (ret <0 )return -1 ; -while ((ret =bdrv_pread (bdrv,-1 , &io_header,sizeof (io_header))) ==-1 && - errno ==EINTR ); +while ((ret =bdrv_sg_recv_response (bdrv, &io_header,sizeof (io_header))) <0 && + ret == -EINTR ) + ; -if (ret ==-1 ) +if (ret <0 )return -1 ;return (buf[9 ] <<16 ) | (buf[10 ] <<8 ) | buf[11 ];
Signature · 1 line
1.6.0.6
Re: [Qemu-devel] [PATCH 1/3] Add specialized block driver scsi generic API
Quoted 11 lines · Avi Kivity
When a scsi device is backed by a scsi generic device instead of an ordinary host block device, the block API is abused in a couple of annoying ways:
-
nb_sectorsis negative, and specifies a byte count instead of a sector count
- offset is ignored, since scsi-generic is essentially a packet protocolThis overloading makes hacking the block layer difficult. Remove it by introducing a new explicit API for scsi-generic devices. The new API is still backed by the old implementation, but at least the users are insulated.
Getting rid of this is good, I found tons of issues in that area with the I/O path exerciser I wrote last week. Hower the way the new API is designed somewhat gets in the way of my patch series to support Gerd's native preadv/pwritev.
One thing I wonder is why we go through the block layer at all for SG. It is actually backed by a char device that doesn't have semantics like a block device at all, given that it can't be seekend and is accessed at byte granularity. We could just go directly to the native Linus syscalls from scsi-generic.c (or a scsi-generic-linux.c if we want some level of abstraction). I'll cook up a patch trying that once I'm back home.
Re: [Qemu-devel] [PATCH 1/3] Add specialized block driver scsi generic API
Quoted 5 lines · Christoph Hellwig
Hower the way the new
API is designed somewhat gets in the way of my patch series to support Gerd's native preadv/pwritev.
Can you point out specific issues?
Quoted 7 lines
One thing I wonder is why we go through the block layer at all for SG. It is actually backed by a char device that doesn't have semantics like a block device at all, given that it can't be seekend and is accessed at byte granularity. We could just go directly to the native Linus syscalls from
scsi-generic.c(or ascsi-generic-linux.cif we want some level of abstraction). I'll cook up a patch trying that once I'm back home
Using the block layer has the advantage of common setup, statistics, and management. I agree that the actual data movement is horribly out of sync with the other format drivers.
Come to think of it, I don't see how statistics can work, so maybe it is a good idea to separate the two paths completely.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [Qemu-devel] [PATCH 1/3] Add specialized block driver scsi generic API
Quoted 7 lines · Avi Kivity
Quoted 5 lines
Hower the way the new
API is designed somewhat gets in the way of my patch series to support Gerd's native preadv/pwritev.Can you point out specific issues?
The patch series (now posted) makes all aio APIs use iovecs. The current non-iovec based SG API would require some nasty shims allocating nested aiocbs. Or we could just make the sg aio methods fake-ioveced which is the solution I had before your split, always adding a one-element iovec to the scsi-generic request structure.
Quoted 3 lines
Using the block layer has the advantage of common setup, statistics, and management. I agree that the actual data movement is horribly out of sync with the other format drivers.
Yeah, the setup is kinda interwinded with the block layer. I wonder whether the device-tree ideas will help sorting that out.
Note that I eventually want to support SG_IO on cdrom devices for burning etc at which point we need to support scsi-generic even if another driver is attached. Note entirely sure how to handle that yet.
Re: [Qemu-devel] [PATCH 1/3] Add specialized block driver scsi generic API
Quoted 6 lines · Christoph Hellwig
The patch series (now posted) makes all aio APIs use iovecs. The current non-iovec based SG API would require some nasty shims allocating nested aiocbs. Or we could just make the sg aio methods fake-ioveced which is the solution I had before your split, always adding a one-element iovec to the scsi-generic request structure.
I'd go for having everything vectored. It's how non-1962 hardware works.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [Qemu-devel] [PATCH 1/3] Add specialized block driver scsi generic API
Quoted 9 lines · Avi Kivity
Quoted 6 lines · Christoph Hellwig
The patch series (now posted) makes all aio APIs use iovecs. The current non-iovec based SG API would require some nasty shims allocating nested aiocbs. Or we could just make the sg aio methods fake-ioveced which is the solution I had before your split, always adding a one-element iovec to the scsi-generic request structure.
I'd go for having everything vectored. It's how non-1962 hardware works.
Yeah, that's what I did for regular block I/O in my series. scsi-generic is a little different beast, though. qemu uses the legacy (not sure if it's formally deprecated) read/write support on /dev/sg which doen't support vectored I/O. If we want to use vectored I/O with sg we'd have to use the SG_IO ioctl (which also has the advantage of working with all sd/sr/ide/etc block devices). But supporting that would probably require very different APIs then the current (aio-) read/writeish one.
Re: [Qemu-devel] [PATCH 1/3] Add specialized block driver scsi generic API
In reply to Avi KivityQuoted 1 line · Avi Kivity
I'd go for having everything vectored. It's how non-1962 hardware works.
Hey, I'm working right now with a chip which has non-vectored IDE hardware!
(Yes, that bit sucks.)
-- Jamie
PATCH 2/3 Add internal scsi generic block API
In reply to Avi KivityAdd an internal API for the generic block layer to send scsi generic commands to block format driver. This means block format drivers no longer need to consider overloaded nb_sectors parameters.
--- block-raw-posix.c | 30 ++++++++++++++++++++++++++++++ block.c | 8 ++++---- block_int.h | 10 ++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-)
block-raw-posix.c +30 -0
@@ -1179,6 +1179,32 @@ static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) }#endif /* !linux */ +static int raw_sg_send_command (BlockDriverState *bs,void *buf,int count) +{ +return raw_pwrite (bs,-1 , buf, count); +} + +static int raw_sg_recv_response (BlockDriverState *bs,void *buf,int count) +{ +return raw_pread (bs,-1 , buf, count); +} + +static BlockDriverAIOCB *raw_sg_aio_read (BlockDriverState *bs, +void *buf,int count, +BlockDriverCompletionFunc *cb, +void *opaque) +{ +return raw_aio_read (bs,0 , buf, -(int64_t )count, cb, opaque); +} + +static BlockDriverAIOCB *raw_sg_aio_write (BlockDriverState *bs, +void *buf,int count, +BlockDriverCompletionFunc *cb, +void *opaque) +{ +return raw_aio_write (bs,0 , buf, -(int64_t )count, cb, opaque); +} +BlockDriver bdrv_host_device = { .format_name ="host_device" , .instance_size =sizeof (BDRVRawState),
@@ -1204,4 +1230,8 @@ BlockDriver bdrv_host_device = { .bdrv_set_locked = raw_set_locked, /* generic scsi device */ .bdrv_ioctl = raw_ioctl, + .bdrv_sg_send_command = raw_sg_send_command, + .bdrv_sg_recv_response = raw_sg_recv_response, + .bdrv_sg_aio_read = raw_sg_aio_read, + .bdrv_sg_aio_write = raw_sg_aio_write, };
block.c +4 -4
@@ -1678,22 +1678,22 @@ int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)int bdrv_sg_send_command (BlockDriverState *bs,void *buf,int count) { -return bdrv_pwrite (bs,-1 , buf, count); +return bs->drv->bdrv_sg_send_command (bs, buf, count); }int bdrv_sg_recv_response (BlockDriverState *bs,void *buf,int count) { -return bdrv_pread (bs,-1 , buf, count); +return bs->drv->bdrv_sg_recv_response (bs, buf, count); }BlockDriverAIOCB *bdrv_sg_aio_read (BlockDriverState *bs,void *buf,int count,BlockDriverCompletionFunc *cb,void *opaque) { -return bdrv_aio_read (bs,0 , buf, -(int64_t )count, cb, opaque); +return bs->drv->bdrv_sg_aio_read (bs, buf, count, cb, opaque); }BlockDriverAIOCB *bdrv_sg_aio_write (BlockDriverState *bs,void *buf,int count,BlockDriverCompletionFunc *cb,void *opaque) { -return bdrv_aio_write (bs,0 , buf, -(int64_t )count, cb, opaque); +return bs->drv->bdrv_sg_aio_write (bs, buf, count, cb, opaque); }
block_int.h +10 -0
@@ -84,6 +84,16 @@ struct BlockDriver {/* to control generic scsi devices */ int (*bdrv_ioctl)(BlockDriverState *bs,unsigned long int req,void *buf); +int (*bdrv_sg_send_command)(BlockDriverState *bs,void *buf,int count); +int (*bdrv_sg_recv_response)(BlockDriverState *bs,void *buf,int count); +BlockDriverAIOCB *(*bdrv_sg_aio_read)(BlockDriverState *bs, +void *buf,int count, +BlockDriverCompletionFunc *cb, +void *opaque); +BlockDriverAIOCB *(*bdrv_sg_aio_write)(BlockDriverState *bs, +void *buf,int count, +BlockDriverCompletionFunc *cb, +void *opaque);BlockDriverAIOCB *free_aiocb;struct BlockDriver *next;
Signature · 1 line
1.6.0.6
PATCH 3/3 Drop internal bdrv_pread()/bdrv_pwrite() APIs
In reply to Avi KivityNow that scsi generic no longer uses bdrv_pread() and bdrv_pwrite(), we can drop the corresponding internal APIs, which overlap bdrv_read()/bdrv_write() and, being byte oriented, are unnatural for a block device.
--- block-raw-posix.c | 20 +++++++++++--- block-raw-win32.c | 20 ++++++++----- block.c | 79 ++++------------------------------------------------- block_int.h | 4 --- 4 files changed, 34 insertions(+), 89 deletions(-)
block-raw-posix.c +16 -4
@@ -358,6 +358,12 @@ static int raw_pread(BlockDriverState *bs, int64_t offset,return raw_pread_aligned (bs, offset, buf, count) + sum; } +static int raw_read (BlockDriverState *bs,int64_t sector_num, +uint8_t *buf,int nb_sectors) +{ +return raw_pread (bs, sector_num *512 , buf, (uint64_t )nb_sectors *512 ); +} +/* * offset and count are in bytes and possibly not aligned. For files opened * with O_DIRECT, necessary alignments are ensured before calling
@@ -436,6 +442,12 @@ static int raw_pwrite(BlockDriverState *bs, int64_t offset,return raw_pwrite_aligned (bs, offset, buf, count) + sum; } +static int raw_write (BlockDriverState *bs,int64_t sector_num, +const uint8_t *buf,int nb_sectors) +{ +return raw_pwrite (bs, sector_num *512 , buf, (uint64_t )nb_sectors *512 ); +} +#ifdef CONFIG_AIO /***********************************************************/ /* Unix AIO using POSIX AIO */
@@ -843,8 +855,8 @@ BlockDriver bdrv_raw = { .aiocb_size = sizeof(RawAIOCB), #endif - .bdrv_pread = raw_pread, - .bdrv_pwrite = raw_pwrite, + .bdrv_read = raw_read, + .bdrv_write = raw_write, .bdrv_truncate = raw_truncate, .bdrv_getlength = raw_getlength, };
@@ -1219,8 +1231,8 @@ BlockDriver bdrv_host_device = { .aiocb_size = sizeof(RawAIOCB), #endif - .bdrv_pread = raw_pread, - .bdrv_pwrite = raw_pwrite, + .bdrv_read = raw_read, + .bdrv_write = raw_write, .bdrv_getlength = raw_getlength,/* removable device support */
block-raw-win32.c +12 -8
@@ -122,13 +122,15 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags)return 0 ; } -static int raw_pread (BlockDriverState *bs,int64_t offset, -uint8_t *buf,int count) +static int raw_read (BlockDriverState *bs,int64_t sector_num, +uint8_t *buf,int nb_sectors) {BDRVRawState *s = bs->opaque;OVERLAPPED ov;DWORD ret_count;int ret; +int64_t offset = sector_num *512 ; +int count = nb_sectors *512 ;memset (&ov,0 ,sizeof (ov)); ov.Offset = offset;
@@ -146,13 +148,15 @@ static int raw_pread(BlockDriverState *bs, int64_t offset,return ret_count; } -static int raw_pwrite (BlockDriverState *bs,int64_t offset, -const uint8_t *buf,int count) +static int raw_write (BlockDriverState *bs,int64_t sector_num, +const uint8_t *buf,int nb_sectors) {BDRVRawState *s = bs->opaque;OVERLAPPED ov;DWORD ret_count;int ret; +int64_t offset = sector_num *512 ; +int count = nb_sectors *512 ;memset (&ov,0 ,sizeof (ov)); ov.Offset = offset;
@@ -359,8 +363,8 @@ BlockDriver bdrv_raw = { .bdrv_aio_cancel = raw_aio_cancel, .aiocb_size = sizeof(RawAIOCB);#endif - .bdrv_pread = raw_pread, - .bdrv_pwrite = raw_pwrite, + .bdrv_read = raw_read, + .bdrv_write = raw_write, .bdrv_truncate = raw_truncate, .bdrv_getlength = raw_getlength, };
@@ -508,7 +512,7 @@ BlockDriver bdrv_host_device = { .bdrv_aio_cancel = raw_aio_cancel, .aiocb_size = sizeof(RawAIOCB);#endif - .bdrv_pread = raw_pread, - .bdrv_pwrite = raw_pwrite, + .bdrv_read = raw_read, + .bdrv_write = raw_write, .bdrv_getlength = raw_getlength, };
block.c +6 -73
112 lines not on this page · raw message
block_int.h +0 -4
@@ -58,10 +58,6 @@ struct BlockDriver {int aiocb_size;const char *protocol_name; -int (*bdrv_pread)(BlockDriverState *bs,int64_t offset, -uint8_t *buf,int count); -int (*bdrv_pwrite)(BlockDriverState *bs,int64_t offset, -const uint8_t *buf,int count);int (*bdrv_truncate)(BlockDriverState *bs,int64_t offset);int64_t (*bdrv_getlength)(BlockDriverState *bs); int (*bdrv_write_compressed)(BlockDriverState *bs, int64_t sector_num,
Signature · 1 line
1.6.0.6
Anthony Liguori
AppliedQuoted 10 lines · Avi Kivity
The internal block layer API has a wart in the form of the ->bdrv_pread() and ->bdrv_pwrite() methods. These duplicate
bdrv_read()andbdrv_write(), and are unnatural for a block device in that they are not sector oriented.One of the reasons for their existence is the scsi-generic host support. That is also a wart, as scsi-generic is really a packet (command/response) rather than a block (random access to sectors) API. There is also the wierd negative sector count == byte count thing.
Quoted 5 lines
This patchset addresses these warts by adding a new API for scsi-generic and dropping ->bdrv_pread() and
bdrv_pwrite(). While the improvement falls short of spectacular, it does make further hacking on the block layer easier as there are fewer callbacks to worry about.
Applied all. Thanks.
Regards,
Anthony Liguori
Quoted 24 lines
---
Changes since v1:
- rebased against trunk
- performance testing: tested 'rm -rf *; tar xjf ../linux-2.6.22.tar.bz2' with a vm snapshot. While there are significant variances among runs (15-25sec), likely due to different ext3 allocation decisions and different host caching decisions, there seems to be no regression due to this patchset.Avi Kivity (3):
Add specialized block driver scsi generic API
Add internal scsi generic block API
Drop internal bdrv_pread()/bdrv_pwrite() APIsblock-raw-posix.c | 50 ++++++++++++++++++++++++-- block-raw-win32.c | 20 ++++++---- block.c | 101 +++++++++++++++-------------------------------------- block.h | 8 ++++ block_int.h | 14 +++++-- hw/scsi-generic.c | 39 +++++++++++--------- 6 files changed, 126 insertions(+), 106 deletions(-)