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

block: Pass truncate exact=true where reasonable

This is a change in behavior, so all instances need a good
justification. The comments added here should explain my reasoning.

qed already had a comment that suggests it always expected
bdrv_truncate()/blk_truncate() to behave as if exact=true were passed
(c743849bee7 came eight months before 55b949c8476), so it was simply
broken until now.

Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-id: 20190918095144.955-8-mreitz@redhat.com
Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com>
[mreitz: Changed comment in qed.c to explain why a new QED file must be
empty, as requested and suggested by Maxim]
Signed-off-by: Max Reitz <mreitz@redhat.com>

+31 -7
+9 -2
block/parallels.c
··· 487 487 res->leaks += count; 488 488 if (fix & BDRV_FIX_LEAKS) { 489 489 Error *local_err = NULL; 490 - ret = bdrv_truncate(bs->file, res->image_end_offset, false, 490 + 491 + /* 492 + * In order to really repair the image, we must shrink it. 493 + * That means we have to pass exact=true. 494 + */ 495 + ret = bdrv_truncate(bs->file, res->image_end_offset, true, 491 496 PREALLOC_MODE_OFF, &local_err); 492 497 if (ret < 0) { 493 498 error_report_err(local_err); ··· 880 885 if ((bs->open_flags & BDRV_O_RDWR) && !(bs->open_flags & BDRV_O_INACTIVE)) { 881 886 s->header->inuse = 0; 882 887 parallels_update_header(bs); 883 - bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS, false, 888 + 889 + /* errors are ignored, so we might as well pass exact=true */ 890 + bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS, true, 884 891 PREALLOC_MODE_OFF, NULL); 885 892 } 886 893
+5 -1
block/qcow2.c
··· 5323 5323 return ret; 5324 5324 } 5325 5325 5326 - ret = blk_truncate(blk, new_size, false, PREALLOC_MODE_OFF, errp); 5326 + /* 5327 + * Amending image options should ensure that the image has 5328 + * exactly the given new values, so pass exact=true here. 5329 + */ 5330 + ret = blk_truncate(blk, new_size, true, PREALLOC_MODE_OFF, errp); 5327 5331 blk_unref(blk); 5328 5332 if (ret < 0) { 5329 5333 return ret;
+5 -2
block/qed.c
··· 673 673 674 674 l1_size = header.cluster_size * header.table_size; 675 675 676 - /* File must start empty and grow, check truncate is supported */ 677 - ret = blk_truncate(blk, 0, false, PREALLOC_MODE_OFF, errp); 676 + /* 677 + * The QED format associates file length with allocation status, 678 + * so a new file (which is empty) must have a length of 0. 679 + */ 680 + ret = blk_truncate(blk, 0, true, PREALLOC_MODE_OFF, errp); 678 681 if (ret < 0) { 679 682 goto out; 680 683 }
+6 -1
qemu-img.c
··· 3831 3831 } 3832 3832 } 3833 3833 3834 - ret = blk_truncate(blk, total_size, false, prealloc, &err); 3834 + /* 3835 + * The user expects the image to have the desired size after 3836 + * resizing, so pass @exact=true. It is of no use to report 3837 + * success when the image has not actually been resized. 3838 + */ 3839 + ret = blk_truncate(blk, total_size, true, prealloc, &err); 3835 3840 if (ret < 0) { 3836 3841 error_report_err(err); 3837 3842 goto out;
+6 -1
qemu-io-cmds.c
··· 1710 1710 return offset; 1711 1711 } 1712 1712 1713 - ret = blk_truncate(blk, offset, false, PREALLOC_MODE_OFF, &local_err); 1713 + /* 1714 + * qemu-io is a debugging tool, so let us be strict here and pass 1715 + * exact=true. It is better to err on the "emit more errors" side 1716 + * than to be overly permissive. 1717 + */ 1718 + ret = blk_truncate(blk, offset, true, PREALLOC_MODE_OFF, &local_err); 1714 1719 if (ret < 0) { 1715 1720 error_report_err(local_err); 1716 1721 return ret;