devices: block: let resize convert to non-sparse

Change the behavior of the resize operation on virtio-block devices so
that it causes a disk to become fully allocated (non-sparse) even if it
had previously been sparse.

This means that we could have a disk that was previously sparse and is
now non-sparse, so treat discard requests for sparse disks as a no-op
instead of an error.  This is acceptable since discard is a hint and
doing nothing is a valid implementation.

BUG=chromium:858815
TEST=`crosvm disk resize` a sparse disk

Change-Id: I8b79e460e5432cc71bed98172527fe1cd2e726ed
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2070846
Reviewed-by: David Munro <davidmunro@google.com>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Daniel Verkamp 2020-02-25 13:31:28 -08:00 committed by Commit Bot
parent 1f9c1bb88b
commit 7673338200

View file

@ -343,14 +343,14 @@ impl Worker {
return DiskControlResult::Err(SysError::new(libc::EIO));
}
if !self.sparse {
// Allocate new space if the disk image is not sparse.
if let Err(e) = self.disk_image.allocate(0, new_size) {
error!("Allocating disk space after resize failed! {}", e);
return DiskControlResult::Err(SysError::new(libc::EIO));
}
// Allocate new space if the disk image is not sparse.
if let Err(e) = self.disk_image.allocate(0, new_size) {
error!("Allocating disk space after resize failed! {}", e);
return DiskControlResult::Err(SysError::new(libc::EIO));
}
self.sparse = false;
if let Ok(new_disk_size) = self.disk_image.get_len() {
let mut disk_size = self.disk_size.lock();
*disk_size = new_disk_size;
@ -624,7 +624,8 @@ impl Block {
}
VIRTIO_BLK_T_DISCARD | VIRTIO_BLK_T_WRITE_ZEROES => {
if req_type == VIRTIO_BLK_T_DISCARD && !sparse {
return Err(ExecuteError::Unsupported(req_type));
// Discard is a hint; if this is a non-sparse disk, just ignore it.
return Ok(());
}
while reader.available_bytes() >= size_of::<virtio_blk_discard_write_zeroes>() {