Skip to content

Commit 365fed5

Browse files
ebblakeXanClic
authored andcommitted
qed: Simplify backing reads
The other four drivers that support backing files (qcow, qcow2, parallels, vmdk) all rely on the block layer to populate zeroes when reading beyond EOF of a short backing file. We can simplify the qed code by doing likewise. Signed-off-by: Eric Blake <[email protected]> Reviewed-by: Vladimir Sementsov-Ogievskiy <[email protected]> Message-Id: <[email protected]> Signed-off-by: Max Reitz <[email protected]>
1 parent a2adbbf commit 365fed5

File tree

2 files changed

+6
-59
lines changed

2 files changed

+6
-59
lines changed

block/qed.c

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -849,56 +849,18 @@ static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
849849
* @s: QED state
850850
* @pos: Byte position in device
851851
* @qiov: Destination I/O vector
852-
* @backing_qiov: Possibly shortened copy of qiov, to be allocated here
853-
* @cb: Completion function
854-
* @opaque: User data for completion function
855852
*
856853
* This function reads qiov->size bytes starting at pos from the backing file.
857854
* If there is no backing file then zeroes are read.
858855
*/
859856
static int coroutine_fn qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
860-
QEMUIOVector *qiov,
861-
QEMUIOVector **backing_qiov)
857+
QEMUIOVector *qiov)
862858
{
863-
uint64_t backing_length = 0;
864-
size_t size;
865-
int ret;
866-
867-
/* If there is a backing file, get its length. Treat the absence of a
868-
* backing file like a zero length backing file.
869-
*/
870859
if (s->bs->backing) {
871-
int64_t l = bdrv_getlength(s->bs->backing->bs);
872-
if (l < 0) {
873-
return l;
874-
}
875-
backing_length = l;
876-
}
877-
878-
/* Zero all sectors if reading beyond the end of the backing file */
879-
if (pos >= backing_length ||
880-
pos + qiov->size > backing_length) {
881-
qemu_iovec_memset(qiov, 0, 0, qiov->size);
882-
}
883-
884-
/* Complete now if there are no backing file sectors to read */
885-
if (pos >= backing_length) {
886-
return 0;
887-
}
888-
889-
/* If the read straddles the end of the backing file, shorten it */
890-
size = MIN((uint64_t)backing_length - pos, qiov->size);
891-
892-
assert(*backing_qiov == NULL);
893-
*backing_qiov = g_new(QEMUIOVector, 1);
894-
qemu_iovec_init(*backing_qiov, qiov->niov);
895-
qemu_iovec_concat(*backing_qiov, qiov, 0, size);
896-
897-
BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO);
898-
ret = bdrv_co_preadv(s->bs->backing, pos, size, *backing_qiov, 0);
899-
if (ret < 0) {
900-
return ret;
860+
BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO);
861+
return bdrv_co_preadv(s->bs->backing, pos, qiov->size, qiov, 0);
901862
}
863+
qemu_iovec_memset(qiov, 0, 0, qiov->size);
902864
return 0;
903865
}
904866

@@ -915,7 +877,6 @@ static int coroutine_fn qed_copy_from_backing_file(BDRVQEDState *s,
915877
uint64_t offset)
916878
{
917879
QEMUIOVector qiov;
918-
QEMUIOVector *backing_qiov = NULL;
919880
int ret;
920881

921882
/* Skip copy entirely if there is no work to do */
@@ -925,13 +886,7 @@ static int coroutine_fn qed_copy_from_backing_file(BDRVQEDState *s,
925886

926887
qemu_iovec_init_buf(&qiov, qemu_blockalign(s->bs, len), len);
927888

928-
ret = qed_read_backing_file(s, pos, &qiov, &backing_qiov);
929-
930-
if (backing_qiov) {
931-
qemu_iovec_destroy(backing_qiov);
932-
g_free(backing_qiov);
933-
backing_qiov = NULL;
934-
}
889+
ret = qed_read_backing_file(s, pos, &qiov);
935890

936891
if (ret) {
937892
goto out;
@@ -1339,8 +1294,7 @@ static int coroutine_fn qed_aio_read_data(void *opaque, int ret,
13391294
qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size);
13401295
r = 0;
13411296
} else if (ret != QED_CLUSTER_FOUND) {
1342-
r = qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov,
1343-
&acb->backing_qiov);
1297+
r = qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov);
13441298
} else {
13451299
BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
13461300
r = bdrv_co_preadv(bs->file, offset, acb->cur_qiov.size,
@@ -1365,12 +1319,6 @@ static int coroutine_fn qed_aio_next_io(QEDAIOCB *acb)
13651319
while (1) {
13661320
trace_qed_aio_next_io(s, acb, 0, acb->cur_pos + acb->cur_qiov.size);
13671321

1368-
if (acb->backing_qiov) {
1369-
qemu_iovec_destroy(acb->backing_qiov);
1370-
g_free(acb->backing_qiov);
1371-
acb->backing_qiov = NULL;
1372-
}
1373-
13741322
acb->qiov_offset += acb->cur_qiov.size;
13751323
acb->cur_pos += acb->cur_qiov.size;
13761324
qemu_iovec_reset(&acb->cur_qiov);

block/qed.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ typedef struct QEDAIOCB {
140140

141141
/* Current cluster scatter-gather list */
142142
QEMUIOVector cur_qiov;
143-
QEMUIOVector *backing_qiov;
144143
uint64_t cur_pos; /* position on block device, in bytes */
145144
uint64_t cur_cluster; /* cluster offset in image file */
146145
unsigned int cur_nclusters; /* number of clusters being accessed */

0 commit comments

Comments
 (0)