提交 41536287 编写于 作者: E Eric Blake 提交者: Kevin Wolf

qemu-img: Change img_rebase() to be byte-based

In the continuing quest to make more things byte-based, change
the internal iteration of img_rebase().  We can finally drop the
TODO assertion added earlier, now that the entire algorithm is
byte-based and no longer has to shift from bytes to sectors.

Most of the change is mechanical ('num_sectors' becomes 'size',
'sector' becomes 'offset', 'n' goes from sectors to bytes); some
of it is also a cleanup (use of MIN() instead of open-coding,
loss of variable 'count' added earlier in commit d6a644bb).
Signed-off-by: NEric Blake <eblake@redhat.com>
Reviewed-by: NJohn Snow <jsnow@redhat.com>
Signed-off-by: NKevin Wolf <kwolf@redhat.com>
上级 dc61cd3b
...@@ -3248,70 +3248,58 @@ static int img_rebase(int argc, char **argv) ...@@ -3248,70 +3248,58 @@ static int img_rebase(int argc, char **argv)
* the image is the same as the original one at any time. * the image is the same as the original one at any time.
*/ */
if (!unsafe) { if (!unsafe) {
int64_t num_sectors; int64_t size;
int64_t old_backing_num_sectors; int64_t old_backing_size;
int64_t new_backing_num_sectors = 0; int64_t new_backing_size = 0;
uint64_t sector; uint64_t offset;
int n; int64_t n;
int64_t count;
float local_progress = 0; float local_progress = 0;
buf_old = blk_blockalign(blk, IO_BUF_SIZE); buf_old = blk_blockalign(blk, IO_BUF_SIZE);
buf_new = blk_blockalign(blk, IO_BUF_SIZE); buf_new = blk_blockalign(blk, IO_BUF_SIZE);
num_sectors = blk_nb_sectors(blk); size = blk_getlength(blk);
if (num_sectors < 0) { if (size < 0) {
error_report("Could not get size of '%s': %s", error_report("Could not get size of '%s': %s",
filename, strerror(-num_sectors)); filename, strerror(-size));
ret = -1; ret = -1;
goto out; goto out;
} }
old_backing_num_sectors = blk_nb_sectors(blk_old_backing); old_backing_size = blk_getlength(blk_old_backing);
if (old_backing_num_sectors < 0) { if (old_backing_size < 0) {
char backing_name[PATH_MAX]; char backing_name[PATH_MAX];
bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
error_report("Could not get size of '%s': %s", error_report("Could not get size of '%s': %s",
backing_name, strerror(-old_backing_num_sectors)); backing_name, strerror(-old_backing_size));
ret = -1; ret = -1;
goto out; goto out;
} }
if (blk_new_backing) { if (blk_new_backing) {
new_backing_num_sectors = blk_nb_sectors(blk_new_backing); new_backing_size = blk_getlength(blk_new_backing);
if (new_backing_num_sectors < 0) { if (new_backing_size < 0) {
error_report("Could not get size of '%s': %s", error_report("Could not get size of '%s': %s",
out_baseimg, strerror(-new_backing_num_sectors)); out_baseimg, strerror(-new_backing_size));
ret = -1; ret = -1;
goto out; goto out;
} }
} }
if (num_sectors != 0) { if (size != 0) {
local_progress = (float)100 / local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
(num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
} }
for (sector = 0; sector < num_sectors; sector += n) { for (offset = 0; offset < size; offset += n) {
/* How many bytes can we handle with the next read? */
/* How many sectors can we handle with the next read? */ n = MIN(IO_BUF_SIZE, size - offset);
if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
n = (IO_BUF_SIZE / 512);
} else {
n = num_sectors - sector;
}
/* If the cluster is allocated, we don't need to take action */ /* If the cluster is allocated, we don't need to take action */
ret = bdrv_is_allocated(bs, sector << BDRV_SECTOR_BITS, ret = bdrv_is_allocated(bs, offset, n, &n);
n << BDRV_SECTOR_BITS, &count);
if (ret < 0) { if (ret < 0) {
error_report("error while reading image metadata: %s", error_report("error while reading image metadata: %s",
strerror(-ret)); strerror(-ret));
goto out; goto out;
} }
/* TODO relax this once bdrv_is_allocated does not enforce
* sector alignment */
assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
n = count >> BDRV_SECTOR_BITS;
if (ret) { if (ret) {
continue; continue;
} }
...@@ -3320,30 +3308,28 @@ static int img_rebase(int argc, char **argv) ...@@ -3320,30 +3308,28 @@ static int img_rebase(int argc, char **argv)
* Read old and new backing file and take into consideration that * Read old and new backing file and take into consideration that
* backing files may be smaller than the COW image. * backing files may be smaller than the COW image.
*/ */
if (sector >= old_backing_num_sectors) { if (offset >= old_backing_size) {
memset(buf_old, 0, n * BDRV_SECTOR_SIZE); memset(buf_old, 0, n);
} else { } else {
if (sector + n > old_backing_num_sectors) { if (offset + n > old_backing_size) {
n = old_backing_num_sectors - sector; n = old_backing_size - offset;
} }
ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS, ret = blk_pread(blk_old_backing, offset, buf_old, n);
buf_old, n << BDRV_SECTOR_BITS);
if (ret < 0) { if (ret < 0) {
error_report("error while reading from old backing file"); error_report("error while reading from old backing file");
goto out; goto out;
} }
} }
if (sector >= new_backing_num_sectors || !blk_new_backing) { if (offset >= new_backing_size || !blk_new_backing) {
memset(buf_new, 0, n * BDRV_SECTOR_SIZE); memset(buf_new, 0, n);
} else { } else {
if (sector + n > new_backing_num_sectors) { if (offset + n > new_backing_size) {
n = new_backing_num_sectors - sector; n = new_backing_size - offset;
} }
ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS, ret = blk_pread(blk_new_backing, offset, buf_new, n);
buf_new, n << BDRV_SECTOR_BITS);
if (ret < 0) { if (ret < 0) {
error_report("error while reading from new backing file"); error_report("error while reading from new backing file");
goto out; goto out;
...@@ -3353,15 +3339,13 @@ static int img_rebase(int argc, char **argv) ...@@ -3353,15 +3339,13 @@ static int img_rebase(int argc, char **argv)
/* If they differ, we need to write to the COW file */ /* If they differ, we need to write to the COW file */
uint64_t written = 0; uint64_t written = 0;
while (written < n * BDRV_SECTOR_SIZE) { while (written < n) {
int64_t pnum; int64_t pnum;
if (compare_buffers(buf_old + written, if (compare_buffers(buf_old + written, buf_new + written,
buf_new + written, n - written, &pnum))
n * BDRV_SECTOR_SIZE - written, &pnum))
{ {
ret = blk_pwrite(blk, ret = blk_pwrite(blk, offset + written,
(sector << BDRV_SECTOR_BITS) + written,
buf_old + written, pnum, 0); buf_old + written, pnum, 0);
if (ret < 0) { if (ret < 0) {
error_report("Error while writing to COW image: %s", error_report("Error while writing to COW image: %s",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册