qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

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 <eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20200528094405.145708-11-vsementsov@virtuozzo.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>

authored by

Eric Blake and committed by
Max Reitz
365fed51 a2adbbf6

+6 -59
+6 -58
block/qed.c
··· 849 849 * @s: QED state 850 850 * @pos: Byte position in device 851 851 * @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 855 852 * 856 853 * This function reads qiov->size bytes starting at pos from the backing file. 857 854 * If there is no backing file then zeroes are read. 858 855 */ 859 856 static int coroutine_fn qed_read_backing_file(BDRVQEDState *s, uint64_t pos, 860 - QEMUIOVector *qiov, 861 - QEMUIOVector **backing_qiov) 857 + QEMUIOVector *qiov) 862 858 { 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 - */ 870 859 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); 860 + BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO); 861 + return bdrv_co_preadv(s->bs->backing, pos, qiov->size, qiov, 0); 882 862 } 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; 901 - } 863 + qemu_iovec_memset(qiov, 0, 0, qiov->size); 902 864 return 0; 903 865 } 904 866 ··· 915 877 uint64_t offset) 916 878 { 917 879 QEMUIOVector qiov; 918 - QEMUIOVector *backing_qiov = NULL; 919 880 int ret; 920 881 921 882 /* Skip copy entirely if there is no work to do */ ··· 925 886 926 887 qemu_iovec_init_buf(&qiov, qemu_blockalign(s->bs, len), len); 927 888 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); 935 890 936 891 if (ret) { 937 892 goto out; ··· 1339 1294 qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size); 1340 1295 r = 0; 1341 1296 } 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); 1344 1298 } else { 1345 1299 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); 1346 1300 r = bdrv_co_preadv(bs->file, offset, acb->cur_qiov.size, ··· 1364 1318 qemu_co_mutex_lock(&s->table_lock); 1365 1319 while (1) { 1366 1320 trace_qed_aio_next_io(s, acb, 0, acb->cur_pos + acb->cur_qiov.size); 1367 - 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 1321 1374 1322 acb->qiov_offset += acb->cur_qiov.size; 1375 1323 acb->cur_pos += acb->cur_qiov.size;
-1
block/qed.h
··· 140 140 141 141 /* Current cluster scatter-gather list */ 142 142 QEMUIOVector cur_qiov; 143 - QEMUIOVector *backing_qiov; 144 143 uint64_t cur_pos; /* position on block device, in bytes */ 145 144 uint64_t cur_cluster; /* cluster offset in image file */ 146 145 unsigned int cur_nclusters; /* number of clusters being accessed */