qemu-img rebase: Fix for undersized backing files
Backing files may be smaller than the corresponding COW file. When reading directly from the backing file, qemu-img rebase must consider this and assume zero sectors after the end of backing files. Signed-off-by: Kevin Wolf <kwolf@redhat.com> Reviewed-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
This commit is contained in:
parent
b9c532903f
commit
87a1b3e381
42
qemu-img.c
42
qemu-img.c
|
@ -1420,6 +1420,8 @@ static int img_rebase(int argc, char **argv)
|
||||||
*/
|
*/
|
||||||
if (!unsafe) {
|
if (!unsafe) {
|
||||||
uint64_t num_sectors;
|
uint64_t num_sectors;
|
||||||
|
uint64_t old_backing_num_sectors;
|
||||||
|
uint64_t new_backing_num_sectors;
|
||||||
uint64_t sector;
|
uint64_t sector;
|
||||||
int n;
|
int n;
|
||||||
uint8_t * buf_old;
|
uint8_t * buf_old;
|
||||||
|
@ -1430,6 +1432,8 @@ static int img_rebase(int argc, char **argv)
|
||||||
buf_new = qemu_blockalign(bs, IO_BUF_SIZE);
|
buf_new = qemu_blockalign(bs, IO_BUF_SIZE);
|
||||||
|
|
||||||
bdrv_get_geometry(bs, &num_sectors);
|
bdrv_get_geometry(bs, &num_sectors);
|
||||||
|
bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors);
|
||||||
|
bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors);
|
||||||
|
|
||||||
local_progress = (float)100 /
|
local_progress = (float)100 /
|
||||||
(num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
|
(num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
|
||||||
|
@ -1448,16 +1452,36 @@ static int img_rebase(int argc, char **argv)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read old and new backing file */
|
/*
|
||||||
ret = bdrv_read(bs_old_backing, sector, buf_old, n);
|
* Read old and new backing file and take into consideration that
|
||||||
if (ret < 0) {
|
* backing files may be smaller than the COW image.
|
||||||
error_report("error while reading from old backing file");
|
*/
|
||||||
goto out;
|
if (sector >= old_backing_num_sectors) {
|
||||||
|
memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
|
||||||
|
} else {
|
||||||
|
if (sector + n > old_backing_num_sectors) {
|
||||||
|
n = old_backing_num_sectors - sector;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = bdrv_read(bs_old_backing, sector, buf_old, n);
|
||||||
|
if (ret < 0) {
|
||||||
|
error_report("error while reading from old backing file");
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ret = bdrv_read(bs_new_backing, sector, buf_new, n);
|
|
||||||
if (ret < 0) {
|
if (sector >= new_backing_num_sectors) {
|
||||||
error_report("error while reading from new backing file");
|
memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
|
||||||
goto out;
|
} else {
|
||||||
|
if (sector + n > new_backing_num_sectors) {
|
||||||
|
n = new_backing_num_sectors - sector;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = bdrv_read(bs_new_backing, sector, buf_new, n);
|
||||||
|
if (ret < 0) {
|
||||||
|
error_report("error while reading from new backing file");
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If they differ, we need to write to the COW file */
|
/* If they differ, we need to write to the COW file */
|
||||||
|
|
Loading…
Reference in New Issue