summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-08-22 12:57:51 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-08-22 13:37:35 +0200
commite5164735e5b674a10134894589a060a0f5f32ccc (patch)
tree7fc7bff17b3c7831c126da8b144165c3b00137ad
parentad5bc00194a981af3435835b5ceb5d624a98caf7 (diff)
downloadqemu-e5164735e5b674a10134894589a060a0f5f32ccc.tar.gz
block: do not directly set total_sectors
To prepare for expressing block sizes in bytes instead of sector counts, this patch replaces all direct assignments of bs->total_sectors. This converts the expression `bs->total_sectors = X` (where X is sector count) to `bdrv_setlength(bs, BDRV_SECTOR_SIZE * X)`. Expressions such as `bs->total_sectors = Y / 512` are changed to `bdrv_setlength(bs, BDRV_SECTOR_SIZE * (Y / 512))` to account for the floor division change. If these conditions can be relaxed, it will be done in the future. Signed-off-by: Peter Wu <peter@lekensteyn.nl>
-rw-r--r--block.c22
-rw-r--r--block/bochs.c6
-rw-r--r--block/cloop.c2
-rw-r--r--block/cow.c2
-rw-r--r--block/iscsi.c3
-rw-r--r--block/nfs.c2
-rw-r--r--block/parallels.c4
-rw-r--r--block/qcow.c2
-rw-r--r--block/qcow2.c4
-rw-r--r--block/sheepdog.c2
-rw-r--r--block/vdi.c2
-rw-r--r--block/vhdx.c3
-rw-r--r--block/vmdk.c2
-rw-r--r--block/vpc.c7
-rw-r--r--block/vvfat.c2
-rw-r--r--include/block/block.h1
16 files changed, 43 insertions, 23 deletions
diff --git a/block.c b/block.c
index e9380f6c58..c91549bb5d 100644
--- a/block.c
+++ b/block.c
@@ -702,7 +702,7 @@ static int find_image_format(BlockDriverState *bs, const char *filename,
}
/**
- * Set the current 'total_sectors' value
+ * Set the current 'total_sectors' and 'total_bytes' value
* Return 0 on success, -errno on error.
*/
static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
@@ -722,7 +722,7 @@ static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE);
}
- bs->total_sectors = hint;
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE * hint);
return 0;
}
@@ -1840,7 +1840,7 @@ void bdrv_close(BlockDriverState *bs)
bs->copy_on_read = 0;
bs->backing_file[0] = '\0';
bs->backing_format[0] = '\0';
- bs->total_sectors = 0;
+ bdrv_setlength(bs, 0);
bs->encrypted = 0;
bs->valid_key = 0;
bs->sg = 0;
@@ -3360,7 +3360,8 @@ static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs,
bs->wr_highest_sector = sector_num + nb_sectors - 1;
}
if (bs->growable && ret >= 0) {
- bs->total_sectors = MAX(bs->total_sectors, sector_num + nb_sectors);
+ bdrv_setlength(bs, MAX(bs->total_bytes,
+ BDRV_SECTOR_SIZE * (sector_num + nb_sectors)));
}
return ret;
@@ -3563,6 +3564,19 @@ int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
return -ENOTSUP;
}
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+/**
+ * Sets the initial size of a block.
+ */
+void bdrv_setlength(BlockDriverState *bs, uint64_t bytes)
+{
+ bs->total_bytes = bytes;
+ /* Be careful with division here, see commit 7e38200 for VMDK */
+ bs->total_sectors = DIV_ROUND_UP(bytes, BDRV_SECTOR_SIZE);
+}
+#pragma GCC diagnostic pop
+
/**
* Return number of sectors on success, -errno on error.
*/
diff --git a/block/bochs.c b/block/bochs.c
index 199ac2b9af..d2053094bc 100644
--- a/block/bochs.c
+++ b/block/bochs.c
@@ -118,9 +118,11 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
}
if (le32_to_cpu(bochs.version) == HEADER_V1) {
- bs->total_sectors = le64_to_cpu(bochs.extra.redolog_v1.disk) / 512;
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE *
+ (le64_to_cpu(bochs.extra.redolog_v1.disk) / 512));
} else {
- bs->total_sectors = le64_to_cpu(bochs.extra.redolog.disk) / 512;
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE *
+ (le64_to_cpu(bochs.extra.redolog.disk) / 512));
}
/* Limit to 1M entries to avoid unbounded allocation. This is what is
diff --git a/block/cloop.c b/block/cloop.c
index f328be06f8..d25dd31aa1 100644
--- a/block/cloop.c
+++ b/block/cloop.c
@@ -184,7 +184,7 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
s->current_block = s->n_blocks;
s->sectors_per_block = s->block_size/512;
- bs->total_sectors = s->n_blocks * s->sectors_per_block;
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE * s->n_blocks * s->sectors_per_block);
qemu_co_mutex_init(&s->lock);
return 0;
diff --git a/block/cow.c b/block/cow.c
index 6ee483327f..1911bc6f03 100644
--- a/block/cow.c
+++ b/block/cow.c
@@ -91,7 +91,7 @@ static int cow_open(BlockDriverState *bs, QDict *options, int flags,
/* cow image found */
size = be64_to_cpu(cow_header.size);
- bs->total_sectors = size / 512;
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE * (size / 512));
pstrcpy(bs->backing_file, sizeof(bs->backing_file),
cow_header.backing_file);
diff --git a/block/iscsi.c b/block/iscsi.c
index 3e19202488..38777d3dba 100644
--- a/block/iscsi.c
+++ b/block/iscsi.c
@@ -1352,7 +1352,8 @@ static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
ret = -EINVAL;
goto out;
}
- bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE *
+ sector_lun2qemu(iscsilun->num_blocks, iscsilun));
bs->request_alignment = iscsilun->block_size;
/* We don't have any emulation for devices other than disks and CD-ROMs, so
diff --git a/block/nfs.c b/block/nfs.c
index 93d87f3256..a5fc328b09 100644
--- a/block/nfs.c
+++ b/block/nfs.c
@@ -401,7 +401,7 @@ static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
if (ret < 0) {
return ret;
}
- bs->total_sectors = ret;
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE * ret);
return 0;
}
diff --git a/block/parallels.c b/block/parallels.c
index 2a814f3db4..58bb052e83 100644
--- a/block/parallels.c
+++ b/block/parallels.c
@@ -89,14 +89,14 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
goto fail;
}
- bs->total_sectors = le64_to_cpu(ph.nb_sectors);
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE * le64_to_cpu(ph.nb_sectors));
if (le32_to_cpu(ph.version) != HEADER_VERSION) {
goto fail_format;
}
if (!memcmp(ph.magic, HEADER_MAGIC, 16)) {
s->off_multiplier = 1;
- bs->total_sectors = 0xffffffff & bs->total_sectors;
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE * (0xffffffff & bs->total_sectors));
} else if (!memcmp(ph.magic, HEADER_MAGIC2, 16)) {
s->off_multiplier = le32_to_cpu(ph.tracks);
} else {
diff --git a/block/qcow.c b/block/qcow.c
index 67c237fe7d..7f142f78c8 100644
--- a/block/qcow.c
+++ b/block/qcow.c
@@ -162,7 +162,7 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
s->cluster_sectors = 1 << (s->cluster_bits - 9);
s->l2_bits = header.l2_bits;
s->l2_size = 1 << s->l2_bits;
- bs->total_sectors = header.size / 512;
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE * (header.size / 512));
s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1;
/* read the level 1 table */
diff --git a/block/qcow2.c b/block/qcow2.c
index f9e045ff2b..8854d96279 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -689,7 +689,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
s->l2_size = 1 << s->l2_bits;
- bs->total_sectors = header.size / 512;
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE * (header.size / 512));
s->csize_shift = (62 - (s->cluster_bits - 8));
s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
@@ -2254,7 +2254,7 @@ static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
/* bdrv_co_do_writev will have increased the total_sectors value to include
* the VM state - the VM state is however not an actual part of the block
* device, therefore, we need to restore the old value. */
- bs->total_sectors = total_sectors;
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE * total_sectors);
return ret;
}
diff --git a/block/sheepdog.c b/block/sheepdog.c
index 12cbd9dcb4..6072aea9e0 100644
--- a/block/sheepdog.c
+++ b/block/sheepdog.c
@@ -1496,7 +1496,7 @@ static int sd_open(BlockDriverState *bs, QDict *options, int flags,
s->min_dirty_data_idx = UINT32_MAX;
s->max_dirty_data_idx = 0;
- bs->total_sectors = s->inode.vdi_size / BDRV_SECTOR_SIZE;
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE * (s->inode.vdi_size / BDRV_SECTOR_SIZE));
pstrcpy(s->name, sizeof(s->name), vdi);
qemu_co_mutex_init(&s->lock);
qemu_opts_del(opts);
diff --git a/block/vdi.c b/block/vdi.c
index 4b10aacc3b..8086510edd 100644
--- a/block/vdi.c
+++ b/block/vdi.c
@@ -467,7 +467,7 @@ static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
goto fail;
}
- bs->total_sectors = header.disk_size / SECTOR_SIZE;
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE * (header.disk_size / SECTOR_SIZE));
s->block_size = header.block_size;
s->block_sectors = header.block_size / SECTOR_SIZE;
diff --git a/block/vhdx.c b/block/vhdx.c
index 87c99fc260..5819fcf2f6 100644
--- a/block/vhdx.c
+++ b/block/vhdx.c
@@ -945,7 +945,8 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags,
/* the VHDX spec dictates that virtual_disk_size is always a multiple of
* logical_sector_size */
- bs->total_sectors = s->virtual_disk_size >> s->logical_sector_size_bits;
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE *
+ (s->virtual_disk_size >> s->logical_sector_size_bits));
vhdx_calc_bat_entries(s);
diff --git a/block/vmdk.c b/block/vmdk.c
index f000d2a613..0b1c586727 100644
--- a/block/vmdk.c
+++ b/block/vmdk.c
@@ -440,7 +440,7 @@ static int vmdk_add_extent(BlockDriverState *bs,
} else {
extent->end_sector = extent->sectors;
}
- bs->total_sectors = extent->end_sector;
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE * extent->end_sector);
if (new_extent) {
*new_extent = extent;
}
diff --git a/block/vpc.c b/block/vpc.c
index 055efc42d2..f592505dce 100644
--- a/block/vpc.c
+++ b/block/vpc.c
@@ -212,8 +212,8 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags,
// The visible size of a image in Virtual PC depends on the geometry
// rather than on the size stored in the footer (the size in the footer
// is too large usually)
- bs->total_sectors = (int64_t)
- be16_to_cpu(footer->cyls) * footer->heads * footer->secs_per_cyl;
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE * (uint64_t)
+ be16_to_cpu(footer->cyls) * footer->heads * footer->secs_per_cyl);
/* images created with disk2vhd report a far higher virtual size
* than expected with the cyls * heads * sectors_per_cyl formula.
@@ -221,7 +221,8 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags,
* disk2vhd.
*/
if (!strncmp(footer->creator_app, "d2v", 4)) {
- bs->total_sectors = be64_to_cpu(footer->size) / BDRV_SECTOR_SIZE;
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE *
+ (be64_to_cpu(footer->size) / BDRV_SECTOR_SIZE));
}
/* Allow a maximum disk size of approximately 2 TB */
diff --git a/block/vvfat.c b/block/vvfat.c
index 731e591ec1..c187a5c639 100644
--- a/block/vvfat.c
+++ b/block/vvfat.c
@@ -1162,7 +1162,7 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
bs->read_only = 0;
}
- bs->total_sectors = cyls * heads * secs;
+ bdrv_setlength(bs, BDRV_SECTOR_SIZE * cyls * heads * secs);
if (init_directories(s, dirname, heads, secs, errp)) {
ret = -EIO;
diff --git a/include/block/block.h b/include/block/block.h
index 8f4ad16d8f..2ac8a43239 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -276,6 +276,7 @@ BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
int bdrv_get_backing_file_depth(BlockDriverState *bs);
void bdrv_refresh_filename(BlockDriverState *bs);
int bdrv_truncate(BlockDriverState *bs, int64_t offset);
+void bdrv_setlength(BlockDriverState *bs, uint64_t bytes);
int64_t bdrv_nb_sectors(BlockDriverState *bs);
int64_t bdrv_getlength(BlockDriverState *bs);
int64_t bdrv_get_allocated_file_size(BlockDriverState *bs);