From b94a2610573cd9314f244207c8b04cb75e42d7f8 Mon Sep 17 00:00:00 2001 From: Kevin Wolf Date: Tue, 29 Oct 2013 12:18:58 +0100 Subject: block: Avoid unecessary drv->bdrv_getlength() calls The block layer generally keeps the size of an image cached in bs->total_sectors so that it doesn't have to perform expensive operations to get the size whenever it needs it. This doesn't work however when using a backend that can change its size without qemu being aware of it, i.e. passthrough of removable media like CD-ROMs or floppy disks. For this reason, the caching is disabled when a removable device is used. It is obvious that checking whether the _guest_ device has removable media isn't the right thing to do when we want to know whether the size of the host backend can change. To make things worse, non-top-level BlockDriverStates never have any device attached, which makes qemu assume they are removable, so drv->bdrv_getlength() is always called on the protocol layer. In the case of raw-posix, this causes unnecessary lseek() system calls, which turned out to be rather expensive. This patch completely changes the logic and disables bs->total_sectors caching only for certain block driver types, for which a size change is expected: host_cdrom and host_floppy on POSIX, host_device on win32; also the raw format in case it sits on top of one of these protocols, but in the common case the nested bdrv_getlength() call on the protocol driver will use the cache again and avoid an expensive drv->bdrv_getlength() call. Signed-off-by: Kevin Wolf Reviewed-by: Paolo Bonzini --- block/raw-win32.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'block/raw-win32.c') diff --git a/block/raw-win32.c b/block/raw-win32.c index 676b5701db..2741e4de10 100644 --- a/block/raw-win32.c +++ b/block/raw-win32.c @@ -616,7 +616,9 @@ static BlockDriver bdrv_host_device = { .bdrv_aio_writev = raw_aio_writev, .bdrv_aio_flush = raw_aio_flush, - .bdrv_getlength = raw_getlength, + .bdrv_getlength = raw_getlength, + .has_variable_length = true, + .bdrv_get_allocated_file_size = raw_get_allocated_file_size, }; -- cgit v1.2.1