Block patches
The following changes since commit 0a2923f8488498000eec54871456aa64a4391da4:
tcg/mips: fix store softmmu slow path (2014-11-02 13:30:00 +0100)
are available in the git repository at:
git://github.com/stefanha/qemu.git tags/block-pull-request
for you to fetch changes up to b112a65c52aa45a23b83b1e0d56db3b7cc44597e:
block: declare blockjobs and dataplane friends! (2014-11-03 11:41:49 +0000)
----------------------------------------------------------------
----------------------------------------------------------------
Adam Crume (1):
rbd: Add support for bdrv_invalidate_cache
Chris Spiegel (1):
snapshot: Reset err to NULL to avoid double free
Denis V. Lunev (3):
iotests: replace fake parallels image with authentic one
iotests: add v2 parallels sample image and simple test for it block/parallels: fix access to not initialized memory in
catalog_bitmap
John Snow (3):
ahci: Correct PIO/D2H FIS responses
ahci: Update byte count after DMA completion
ahci: Fix SDB FIS Construction
Max Reitz (24):
raw-posix: Fix raw_co_get_block_status() after EOF
raw-posix: raw_co_get_block_status() return value
iotests: Add test for external image truncation
qcow2: Allow "full" discard
qcow2: Implement bdrv_make_empty()
qcow2: Optimize bdrv_make_empty()
blockjob: Introduce block_job_complete_sync()
blockjob: Add "ready" field
iotests: Omit length/offset test in 040 and 041 block/mirror: Improve progress report
qemu-img: Implement commit like QMP
qemu-img: Empty image after commit
qemu-img: Enable progress output for commit
qemu-img: Specify backing file for commit
iotests: Add _filter_qemu_img_map
iotests: Add test for backing-chain commits
iotests: Add test for qcow2's bdrv_make_empty
block: Add status callback to bdrv_amend_options()
qemu-img: Add progress output for amend
qemu-img: Fix insignificant memleak block/qcow2: Implement status CB for amend block/qcow2: Make get_refcount() global block/qcow2: Simplify shared L2 handling in amend
iotests: Expand test 061
Peter Lieven (7):
util: introduce MIN_NON_ZERO
BlockLimits: introduce max_transfer_length block/iscsi: set max_transfer_length
block: avoid creating oversized writes in multiwrite_merge block/iscsi: use sector_limits_lun2qemu throughout
iscsi_refresh_limits
block/iscsi: check for oversized requests
block: qemu-iotest 107 supports NFS
Peter Maydell (1):
block.c: Fix type of IoOperationType variable in
send_qmp_error_event()
Richard W.M. Jones (1):
block/curl: Improve type safety of s->timeout.
Stefan Hajnoczi (11):
block: acquire AioContext in generic blockjob QMP commands
blockdev: acquire AioContext in do_qmp_query_block_jobs_one()
blockdev: acquire AioContext in blockdev_mark_auto_del()
blockdev: add note that block_job_cb() must be thread-safe
blockjob: add block_job_defer_to_main_loop()
block: add bdrv_drain()
block: let backup blockjob run in BDS AioContext
block: let stream blockjob run in BDS AioContext
block: let mirror blockjob run in BDS AioContext
block: let commit blockjob run in BDS AioContext
block: declare blockjobs and dataplane friends!
Zhang Haoyu (1):
snapshot: add bdrv_drain_all() to bdrv_snapshot_delete() to avoid
concurrency problem
block.c | 69 +++++-- block/Makefile.objs | 3 +- block/backup.c | 21 ++- block/blkdebug.c | 2 + block/commit.c | 70 ++++--- block/curl.c | 9 +- block/iscsi.c | 49 +++-- block/mirror.c | 119 ++++++++---- block/parallels.c | 2 +- block/qcow2-cluster.c | 144 ++++++++------- block/qcow2-refcount.c | 26 +-- block/qcow2-snapshot.c | 2 +- block/qcow2.c | 202 ++++++++++++++++++++- block/qcow2.h | 7 +- block/raw-posix.c | 42 +++-- block/rbd.c | 15 ++ block/snapshot.c | 4 + block/stream.c | 50 +++-- blockdev.c | 176 +++++++++++++----- blockjob.c | 88 ++++++++- hw/block/dataplane/virtio-blk.c | 5 + hw/ide/ahci.c | 107 ++++++----- hw/ide/ahci.h | 8 + hw/ide/core.c | 11 +- hw/ide/internal.h | 2 + include/block/block.h | 11 +- include/block/block_int.h | 6 +- include/block/blockjob.h | 39 ++++ include/qemu/osdep.h | 6 + qapi/block-core.json | 4 +- qemu-img-cmds.hx | 8 +- qemu-img.c | 176 +++++++++++++++--- qemu-img.texi | 18 +- savevm.c | 3 +- tests/qemu-iotests/040 | 4 +- tests/qemu-iotests/041 | 5 +- tests/qemu-iotests/061 | 25 +++ tests/qemu-iotests/061.out | 30 +++ tests/qemu-iotests/076 | 15 +- tests/qemu-iotests/076.out | 12 +- tests/qemu-iotests/097 | 122 +++++++++++++ tests/qemu-iotests/097.out | 119 ++++++++++++ tests/qemu-iotests/098 | 82 +++++++++ tests/qemu-iotests/098.out | 52 ++++++ tests/qemu-iotests/102 | 21 ++- tests/qemu-iotests/102.out | 11 ++ tests/qemu-iotests/107 | 2 +- tests/qemu-iotests/common.filter | 7 + tests/qemu-iotests/group | 4 +- tests/qemu-iotests/iotests.py | 3 +- .../qemu-iotests/sample_images/fake.parallels.bz2 | Bin 141 -> 0 bytes tests/qemu-iotests/sample_images/parallels-v1.bz2 | Bin 0 -> 147 bytes tests/qemu-iotests/sample_images/parallels-v2.bz2 | Bin 0 -> 150 bytes 53 files changed, 1639 insertions(+), 379 deletions(-) create mode 100755 tests/qemu-iotests/097 create mode 100644 tests/qemu-iotests/097.out create mode 100755 tests/qemu-iotests/098 create mode 100644 tests/qemu-iotests/098.out delete mode 100644 tests/qemu-iotests/sample_images/fake.parallels.bz2 create mode 100644 tests/qemu-iotests/sample_images/parallels-v1.bz2 create mode 100644 tests/qemu-iotests/sample_images/parallels-v2.bz2
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 02/53] BlockLimits: introduce max_transfer_length
From: Peter Lieven <[email protected]>
--- block.c | 4 ++++ include/block/block_int.h | 3 +++ 2 files changed, 7 insertions(+)
block.c +4 -0
@@ -519,6 +519,7 @@ void bdrv_refresh_limits(BlockDriverState *bs, Error **errp)return ; } bs->bl.opt_transfer_length = bs->file->bl.opt_transfer_length; + bs->bl.max_transfer_length = bs->file->bl.max_transfer_length; bs->bl.opt_mem_alignment = bs->file->bl.opt_mem_alignment; }else { bs->bl.opt_mem_alignment =512 ;
@@ -533,6 +534,9 @@ void bdrv_refresh_limits(BlockDriverState *bs, Error **errp) bs->bl.opt_transfer_length =MAX (bs->bl.opt_transfer_length, bs->backing_hd->bl.opt_transfer_length); + bs->bl.max_transfer_length = +MIN_NON_ZERO (bs->bl.max_transfer_length, + bs->backing_hd->bl.max_transfer_length); bs->bl.opt_mem_alignment =MAX (bs->bl.opt_mem_alignment, bs->backing_hd->bl.opt_mem_alignment);
include/block/block_int.h +3 -0
@@ -289,6 +289,9 @@ typedef struct BlockLimits {/* optimal transfer length in sectors */ int opt_transfer_length; +/* maximal transfer length in sectors */ +int max_transfer_length; +/* memory alignment so that no bounce buffer is needed */ size_t opt_mem_alignment; } BlockLimits;
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 01/53] util: introduce MIN_NON_ZERO
In reply to Stefan HajnocziFrom: Peter Lieven <[email protected]>
at least in block layer we have the case of limits being defined for a BlockDriverState. However, in this context often zero (0) has the special meanining of undefined which means no limit. If two of those limits are combined and the minimum is needed the minimum function should only return zero if both parameters are zero.
--- include/qemu/osdep.h | 6 ++++++ 1 file changed, 6 insertions(+)
include/qemu/osdep.h +6 -0
@@ -68,6 +68,12 @@ typedef signed int int_fast16_t;#define MAX (a, b) (((a) > (b)) ? (a) : (b))#endif +/* Minimum function that returns zero only iff both values are zero. +* Intended for use with unsigned values only. */ +#ifndef MIN_NON_ZERO +#define MIN_NON_ZERO (a, b) (((a) != 0 && (a) < (b)) ? (a) : (b)) +#endif +#ifndef ROUND_UP #define ROUND_UP (n,d) (((n) + (d) - 1) & -(d))#endif
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 03/53] block/iscsi: set max_transfer_length
In reply to Stefan HajnocziFrom: Peter Lieven <[email protected]>
Copy the max_xfer_len from the BlockLimits VPD or use the maximum value fitting in the CDB.
The helper function sector_limits_lun2qemu is introduced to convert and cap the limits from the VPD to the maximum power of two fitting in an integer; integer is the range for nb_sectors throughout the block layer.
--- block/iscsi.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
block/iscsi.c +15 -2
@@ -1447,12 +1447,25 @@ static void iscsi_close(BlockDriverState *bs)memset (iscsilun,0 ,sizeof (IscsiLun)); } -static void iscsi_refresh_limits (BlockDriverState *bs,Error **errp) +static int sector_limits_lun2qemu (int64_t sector,IscsiLun *iscsilun) { -IscsiLun *iscsilun = bs->opaque; +return MIN (sector_lun2qemu (sector, iscsilun),INT_MAX /2 +1 ); +} +static void iscsi_refresh_limits (BlockDriverState *bs,Error **errp) +{/* We don't actually refresh here, but just return data queried in * iscsi_open(): iscsi targets don't change their limits. */ + +IscsiLun *iscsilun = bs->opaque; +uint32_t max_xfer_len = iscsilun->use_16_for_rw ?0xffffffff :0xffff ; + +if (iscsilun->bl.max_xfer_len) { + max_xfer_len =MIN (max_xfer_len, iscsilun->bl.max_xfer_len); + } + + bs->bl.max_transfer_length =sector_limits_lun2qemu (max_xfer_len, iscsilun); +if (iscsilun->lbp.lbpu) {if (iscsilun->bl.max_unmap <0xffffffff ) { bs->bl.max_discard = sector_lun2qemu(iscsilun->bl.max_unmap,
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 04/53] block: avoid creating oversized writes in multiwrite_merge
In reply to Stefan HajnocziFrom: Peter Lieven <[email protected]>
--- block.c | 5 +++++ 1 file changed, 5 insertions(+)
block.c +5 -0
@@ -4446,6 +4446,11 @@ static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, merge =0 ; } +if (bs->bl.max_transfer_length && reqs[outidx].nb_sectors + + reqs[i].nb_sectors > bs->bl.max_transfer_length) { + merge =0 ; + } +if (merge) {size_t size;QEMUIOVector *qiov =g_malloc0 (sizeof (*qiov));
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 05/53] block/iscsi: use sector_limits_lun2qemu throughout iscsi_refresh_limits
In reply to Stefan HajnocziFrom: Peter Lieven <[email protected]>
As Max pointed out there is a hidden cast from int64_t to int for all limits. So use the newly introduced sector_limits_lun2qemu for all limits received from the target.
--- block/iscsi.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
block/iscsi.c +10 -10
@@ -1468,23 +1468,23 @@ static void iscsi_refresh_limits(BlockDriverState *bs, Error **errp)if (iscsilun->lbp.lbpu) {if (iscsilun->bl.max_unmap <0xffffffff ) { - bs->bl.max_discard =sector_lun2qemu (iscsilun->bl.max_unmap, - iscsilun); + bs->bl.max_discard = +sector_limits_lun2qemu (iscsilun->bl.max_unmap, iscsilun); } - bs->bl.discard_alignment =sector_lun2qemu (iscsilun->bl.opt_unmap_gran, - iscsilun); + bs->bl.discard_alignment = +sector_limits_lun2qemu (iscsilun->bl.opt_unmap_gran, iscsilun); }if (iscsilun->bl.max_ws_len <0xffffffff ) { - bs->bl.max_write_zeroes =sector_lun2qemu (iscsilun->bl.max_ws_len, - iscsilun); + bs->bl.max_write_zeroes = +sector_limits_lun2qemu (iscsilun->bl.max_ws_len, iscsilun); }if (iscsilun->lbp.lbpws) { - bs->bl.write_zeroes_alignment =sector_lun2qemu (iscsilun->bl.opt_unmap_gran, - iscsilun); + bs->bl.write_zeroes_alignment = +sector_limits_lun2qemu (iscsilun->bl.opt_unmap_gran, iscsilun); } - bs->bl.opt_transfer_length =sector_lun2qemu (iscsilun->bl.opt_xfer_len, - iscsilun); + bs->bl.opt_transfer_length = +sector_limits_lun2qemu (iscsilun->bl.opt_xfer_len, iscsilun); }/* Since iscsi_open() ignores bdrv_flags, there is nothing to do here in
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 06/53] block/iscsi: check for oversized requests
In reply to Stefan HajnocziFrom: Peter Lieven <[email protected]>
Cancel oversized requests early. They would generate an iSCSI protocol error anyway; after having transferred possibly a lot of data over the wire.
--- block/iscsi.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
block/iscsi.c +12 -0
@@ -362,6 +362,12 @@ static int coroutine_fn iscsi_co_writev(BlockDriverState *bs,return -EINVAL ; } +if (bs->bl.max_transfer_length && nb_sectors > bs->bl.max_transfer_length) { +error_report ("iSCSI Error: Write of %d sectors exceeds max_xfer_len " +"of %d sectors" , nb_sectors, bs->bl.max_transfer_length); +return -EINVAL ; + } + lba =sector_qemu2lun (sector_num, iscsilun); num_sectors =sector_qemu2lun (nb_sectors, iscsilun);iscsi_co_init_iscsitask (iscsilun, &iTask);
@@ -529,6 +535,12 @@ static int coroutine_fn iscsi_co_readv(BlockDriverState *bs,return -EINVAL ; } +if (bs->bl.max_transfer_length && nb_sectors > bs->bl.max_transfer_length) { +error_report ("iSCSI Error: Read of %d sectors exceeds max_xfer_len " +"of %d sectors" , nb_sectors, bs->bl.max_transfer_length); +return -EINVAL ; + } +if (iscsilun->lbprz && nb_sectors >=ISCSI_CHECKALLOC_THRES && !iscsi_allocationmap_is_allocated (iscsilun, sector_num, nb_sectors)) {int64_t ret;
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 07/53] ahci: Correct PIO/D2H FIS responses
In reply to Stefan HajnocziFrom: John Snow <[email protected]>
Currently, the D2H FIS packets AHCI generates simply parrot back the LBA that the guest sent to us in the cmd_fis. However, some commands (like READ NATIVE MAX) modify the LBA registers as a return value, through which the AHCI D2H FIS is the only response mechanism. Thus, the D2H response should use the current register values, not the initial ones.
This patch adjusts the LBA and drive select register responses for PIO Setup and D2H FIS response packets.
Additionally, the PIO and D2H FIS responses copy too many bytes from the command FIS that it is being generated from. Specifically, byte 11 which is the Features(15:8) field for Register Host to Device FIS packets, is instead reserved for the PIO Setup FIS and should always be 0.
Message-id: [email protected]
--- hw/ide/ahci.c | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-)
hw/ide/ahci.c +25 -23
@@ -600,6 +600,7 @@ static void ahci_write_fis_pio(AHCIDevice *ad, uint16_t len)uint8_t *pio_fis, *cmd_fis;uint64_t tbl_addr;dma_addr_t cmd_len =0x80 ; +IDEState *s = &ad->port.ifs[0 ];if (!ad->res_fis || !(pr->cmd &PORT_CMD_FIS_RX )) {return ;
@@ -629,21 +630,21 @@ static void ahci_write_fis_pio(AHCIDevice *ad, uint16_t len) pio_fis[0 ] =0x5f ; pio_fis[1 ] = (ad->hba->control_regs.irqstatus ? (1 <<6 ) :0 ); - pio_fis[2 ] = ad->port.ifs[0 ].status; - pio_fis[3 ] = ad->port.ifs[0 ].error; - - pio_fis[4 ] = cmd_fis[4 ]; - pio_fis[5 ] = cmd_fis[5 ]; - pio_fis[6 ] = cmd_fis[6 ]; - pio_fis[7 ] = cmd_fis[7 ]; - pio_fis[8 ] = cmd_fis[8 ]; - pio_fis[9 ] = cmd_fis[9 ]; - pio_fis[10 ] = cmd_fis[10 ]; - pio_fis[11 ] = cmd_fis[11 ]; + pio_fis[2 ] = s->status; + pio_fis[3 ] = s->error; + + pio_fis[4 ] = s->sector; + pio_fis[5 ] = s->lcyl; + pio_fis[6 ] = s->hcyl; + pio_fis[7 ] = s->select; + pio_fis[8 ] = s->hob_sector; + pio_fis[9 ] = s->hob_lcyl; + pio_fis[10 ] = s->hob_hcyl; + pio_fis[11 ] =0 ; pio_fis[12 ] = cmd_fis[12 ]; pio_fis[13 ] = cmd_fis[13 ]; pio_fis[14 ] =0 ; - pio_fis[15 ] = ad->port.ifs[0 ].status; + pio_fis[15 ] = s->status; pio_fis[16 ] = len &255 ; pio_fis[17 ] = len >>8 ; pio_fis[18 ] =0 ;
@@ -670,6 +671,7 @@ static void ahci_write_fis_d2h(AHCIDevice *ad, uint8_t *cmd_fis)int i;dma_addr_t cmd_len =0x80 ;int cmd_mapped =0 ; +IDEState *s = &ad->port.ifs[0 ];if (!ad->res_fis || !(pr->cmd &PORT_CMD_FIS_RX )) {return ;
@@ -687,17 +689,17 @@ static void ahci_write_fis_d2h(AHCIDevice *ad, uint8_t *cmd_fis) d2h_fis[0 ] =0x34 ; d2h_fis[1 ] = (ad->hba->control_regs.irqstatus ? (1 <<6 ) :0 ); - d2h_fis[2 ] = ad->port.ifs[0 ].status; - d2h_fis[3 ] = ad->port.ifs[0 ].error; - - d2h_fis[4 ] = cmd_fis[4 ]; - d2h_fis[5 ] = cmd_fis[5 ]; - d2h_fis[6 ] = cmd_fis[6 ]; - d2h_fis[7 ] = cmd_fis[7 ]; - d2h_fis[8 ] = cmd_fis[8 ]; - d2h_fis[9 ] = cmd_fis[9 ]; - d2h_fis[10 ] = cmd_fis[10 ]; - d2h_fis[11 ] = cmd_fis[11 ]; + d2h_fis[2 ] = s->status; + d2h_fis[3 ] = s->error; + + d2h_fis[4 ] = s->sector; + d2h_fis[5 ] = s->lcyl; + d2h_fis[6 ] = s->hcyl; + d2h_fis[7 ] = s->select; + d2h_fis[8 ] = s->hob_sector; + d2h_fis[9 ] = s->hob_lcyl; + d2h_fis[10 ] = s->hob_hcyl; + d2h_fis[11 ] =0 ; d2h_fis[12 ] = cmd_fis[12 ]; d2h_fis[13 ] = cmd_fis[13 ];for (i =14 ; i <20 ; i++) {
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 08/53] ahci: Update byte count after DMA completion
In reply to Stefan HajnocziFrom: John Snow <[email protected]>
Currently, DMA read/write operations neglect to update the byte count after a successful transfer like ATAPI DMA read or PIO read/write operations do.
We correct this oversight by adding another callback into the IDEDMAOps structure. The commit callback is called whenever we are cleaning up a scatter-gather list. AHCI can register this callback in order to update post- transfer information such as byte count updates.
We use this callback in AHCI to consolidate where we delete the SGlist as generated from the PRDT, as well as update the byte count after the transfer is complete.
The QEMUSGList structure has an init flag added to it in order to make qemu_sglist_destroy a nop if it is called when there is no sglist, which simplifies cleanup and error paths.
This patch fixes several AHCI problems, notably Non-NCQ modes of operation for Windows 7 as well as Hibernate support for Windows 7.
Message-id: [email protected]
--- hw/ide/ahci.c | 41 +++++++++++++++++++++++++++++++---------- hw/ide/core.c | 11 +++++++---- hw/ide/internal.h | 2 ++ 3 files changed, 40 insertions(+), 14 deletions(-)
hw/ide/ahci.c +31 -10
@@ -49,6 +49,9 @@ static int handle_cmd(AHCIState *s,int port,int slot);static void ahci_reset_port (AHCIState *s,int port);static void ahci_write_fis_d2h (AHCIDevice *ad,uint8_t *cmd_fis);static void ahci_init_d2h (AHCIDevice *ad); +static int ahci_dma_prepare_buf (IDEDMA *dma,int is_write); +static void ahci_commit_buf (IDEDMA *dma,uint32_t tx_bytes); +static uint32_t ahci_port_read (AHCIState *s,int port,int offset) {
@@ -1105,16 +1108,12 @@ static void ahci_start_transfer(IDEDMA *dma) } } -/* update number of transferred bytes */ - ad->cur_cmd->status =cpu_to_le32 (le32_to_cpu (ad->cur_cmd->status) + size); - out:/* declare that we processed everything */ s->data_ptr = s->data_end; -if (has_sglist) { -qemu_sglist_destroy (&s->sg); - } +/* Update number of transferred bytes, destroy sglist */ +ahci_commit_buf (dma, size); s->end_transfer_func (s);
@@ -1135,6 +1134,11 @@ static void ahci_start_dma(IDEDMA *dma, IDEState *s,dma_cb (s,0 ); } +/** +* Called in DMA R/W chains to read the PRDT, utilizing ahci_populate_sglist. +* Not currently invoked by PIO R/W chains, +* which invoke ahci_populate_sglist via ahci_start_transfer. +*/ static int ahci_dma_prepare_buf (IDEDMA *dma,int is_write) {AHCIDevice *ad =DO_UPCAST (AHCIDevice, dma, dma);
@@ -1147,6 +1151,24 @@ static int ahci_dma_prepare_buf(IDEDMA *dma, int is_write)return s->io_buffer_size !=0 ; } +/** +* Destroys the scatter-gather list, +* and updates the command header with a bytes-read value. +* called explicitly via ahci_dma_rw_buf (ATAPI DMA), +* and ahci_start_transfer (PIO R/W), +* and called via callback from ide_dma_cb for DMA R/W paths. +*/ +static void ahci_commit_buf (IDEDMA *dma,uint32_t tx_bytes) +{ +AHCIDevice *ad =DO_UPCAST (AHCIDevice, dma, dma); +IDEState *s = &ad->port.ifs[0 ]; + + tx_bytes +=le32_to_cpu (ad->cur_cmd->status); + ad->cur_cmd->status =cpu_to_le32 (tx_bytes); + +qemu_sglist_destroy (&s->sg); +} +static int ahci_dma_rw_buf (IDEDMA *dma,int is_write) {AHCIDevice *ad =DO_UPCAST (AHCIDevice, dma, dma);
@@ -1164,11 +1186,9 @@ static int ahci_dma_rw_buf(IDEDMA *dma, int is_write)dma_buf_write (p, l, &s->sg); } -/* free sglist that was created in ahci_populate_sglist() */ -qemu_sglist_destroy (&s->sg); +/* free sglist, update byte count */ +ahci_commit_buf (dma, l); -/* update number of transferred bytes */ - ad->cur_cmd->status =cpu_to_le32 (le32_to_cpu (ad->cur_cmd->status) + l); s->io_buffer_index += l; s->io_buffer_offset += l;
@@ -1211,6 +1231,7 @@ static const IDEDMAOps ahci_dma_ops = { .start_dma = ahci_start_dma, .start_transfer = ahci_start_transfer, .prepare_buf = ahci_dma_prepare_buf, + .commit_buf = ahci_commit_buf, .rw_buf = ahci_dma_rw_buf, .set_unit = ahci_dma_set_unit, .cmd_done = ahci_cmd_done,
hw/ide/core.c +7 -4
@@ -634,8 +634,11 @@ void ide_sector_read(IDEState *s) ide_sector_read_cb, s); } -static void dma_buf_commit (IDEState *s) +static void dma_buf_commit (IDEState *s,uint32_t tx_bytes) { +if (s->bus->dma->ops->commit_buf) { + s->bus->dma->ops->commit_buf (s->bus->dma, tx_bytes); + }qemu_sglist_destroy (&s->sg); }
@@ -650,6 +653,7 @@ void ide_set_inactive(IDEState *s, bool more)void ide_dma_error (IDEState *s) { +dma_buf_commit (s,0 );ide_abort_command (s);ide_set_inactive (s, false);ide_set_irq (s->bus);
@@ -665,7 +669,6 @@ static int ide_handle_rw_error(IDEState *s, int error, int op) s->bus->error_status = op; }else if (action ==BLOCK_ERROR_ACTION_REPORT ) {if (op &IDE_RETRY_DMA ) { -dma_buf_commit (s);ide_dma_error (s); }else {ide_rw_error (s);
@@ -709,7 +712,8 @@ void ide_dma_cb(void *opaque, int ret) sector_num =ide_get_sector (s);if (n >0 ) { -dma_buf_commit (s); +assert (s->io_buffer_size == s->sg.size); +dma_buf_commit (s, s->io_buffer_size); sector_num += n;ide_set_sector (s, sector_num); s->nsector -= n;
@@ -740,7 +744,6 @@ void ide_dma_cb(void *opaque, int ret)if ((s->dma_cmd ==IDE_DMA_READ || s->dma_cmd ==IDE_DMA_WRITE ) && !ide_sect_range_ok (s, sector_num, n)) { -dma_buf_commit (s);ide_dma_error (s);return ; }
hw/ide/internal.h +2 -0
@@ -322,6 +322,7 @@ typedef void EndTransferFunc(IDEState *);typedef void DMAStartFunc (IDEDMA *,IDEState *,BlockCompletionFunc *);typedef void DMAVoidFunc (IDEDMA *);typedef int DMAIntFunc (IDEDMA *,int ); +typedef void DMAu32Func (IDEDMA *,uint32_t );typedef void DMAStopFunc (IDEDMA *,bool );typedef void DMARestartFunc (void *,int ,RunState );
@@ -430,6 +431,7 @@ struct IDEDMAOps {DMAStartFunc *start_dma;DMAVoidFunc *start_transfer;DMAIntFunc *prepare_buf; +DMAu32Func *commit_buf;DMAIntFunc *rw_buf;DMAIntFunc *set_unit;DMAStopFunc *set_inactive;
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 09/53] ahci: Fix SDB FIS Construction
In reply to Stefan HajnocziFrom: John Snow <[email protected]>
The SDB FIS creation was mangled;
We were writing the error byte to byte 0, and omitting the SDB FIS magic byte.
Though the SDB packet layout states that:
byte 0: Must be 0xA1 to indicate SDB FIS. byte 1: Port multiplier select & other flags byte 2: status byte.
byte 3: error byte.
This patch adds an SDB FIS structure with human-readable names, and ensures that we are filling the structure appropriately.
Message-id: [email protected]
--- hw/ide/ahci.c | 18 +++++++++--------- hw/ide/ahci.h | 8 ++++++++ 2 files changed, 17 insertions(+), 9 deletions(-)
hw/ide/ahci.c +9 -9
@@ -570,24 +570,24 @@ static void ahci_write_fis_sdb(AHCIState *s, int port, uint32_t finished)AHCIDevice *ad = &s->dev[port];AHCIPortRegs *pr = &ad->port_regs;IDEState *ide_state; -uint8_t *sdb_fis; +SDBFIS *sdb_fis;if (!s->dev[port].res_fis || !(pr->cmd &PORT_CMD_FIS_RX )) {return ; } - sdb_fis = &ad->res_fis[RES_FIS_SDBFIS ]; + sdb_fis = (SDBFIS *)&ad->res_fis[RES_FIS_SDBFIS ]; ide_state = &ad->port.ifs[0 ]; -/* clear memory */ - *(uint32_t *)sdb_fis =0 ; - -/* write values */ - sdb_fis[0 ] = ide_state->error; - sdb_fis[2 ] = ide_state->status &0x77 ; + sdb_fis->type =0xA1 ; +/* Interrupt pending & Notification bit */ + sdb_fis->flags = (ad->hba->control_regs.irqstatus ? (1 <<6 ) :0 ); + sdb_fis->status = ide_state->status &0x77 ; + sdb_fis->error = ide_state->error; +/* update SAct field in SDB_FIS */ s->dev[port].finished |= finished; - *(uint32_t *)(sdb_fis +4 ) =cpu_to_le32 (ad->finished); + sdb_fis->payload =cpu_to_le32 (ad->finished);/* Update shadow registers (except BSY 0x80 and DRQ 0x08) */ pr->tfdata = (ad->port.ifs[0].error << 8) |
hw/ide/ahci.h +8 -0
@@ -327,6 +327,14 @@ typedef struct NCQFrame {uint8_t reserved10; }QEMU_PACKED NCQFrame; +typedef struct SDBFIS { +uint8_t type; +uint8_t flags; +uint8_t status; +uint8_t error; +uint32_t payload; +} QEMU_PACKED SDBFIS; +void ahci_init (AHCIState *s,DeviceState *qdev,AddressSpace *as,int ports);void ahci_uninit (AHCIState *s);
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 11/53] iotests: replace fake parallels image with authentic one
In reply to Stefan HajnocziFrom: "Denis V. Lunev" <[email protected]>
The image was generated using http://openvz.org/Ploop utility and properly filled with the same content as original one.
Message-id: [email protected]
--- tests/qemu-iotests/076 | 10 +++++----- tests/qemu-iotests/076.out | 8 ++++---- tests/qemu-iotests/sample_images/fake.parallels.bz2 | Bin 141 -> 0 bytes tests/qemu-iotests/sample_images/parallels-v1.bz2 | Bin 0 -> 147 bytes 4 files changed, 9 insertions(+), 9 deletions(-) delete mode 100644 tests/qemu-iotests/sample_images/fake.parallels.bz2 create mode 100644 tests/qemu-iotests/sample_images/parallels-v1.bz2
tests/qemu-iotests/076 +5 -5
@@ -47,26 +47,26 @@ catalog_entries_offset=$((0x20)) nb_sectors_offset=$((0x24)) echo -echo "== Read from a valid (enough) image ==" -_use_sample_img fake.parallels.bz2 +echo "== Read from a valid v1 image ==" +_use_sample_img parallels-v1.bz2 { $QEMU_IO -c "read -P 0x11 0 64k" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir echo echo "== Negative catalog size ==" -_use_sample_img fake.parallels.bz2 +_use_sample_img parallels-v1.bz2 poke_file "$TEST_IMG" "$catalog_entries_offset" "\xff\xff\xff\xff" { $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir echo echo "== Overflow in catalog allocation ==" -_use_sample_img fake.parallels.bz2 +_use_sample_img parallels-v1.bz2 poke_file "$TEST_IMG" "$nb_sectors_offset" "\xff\xff\xff\xff" poke_file "$TEST_IMG" "$catalog_entries_offset" "\x01\x00\x00\x40" { $QEMU_IO -c "read 64M 64M" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir echo echo "== Zero sectors per track ==" -_use_sample_img fake.parallels.bz2 +_use_sample_img parallels-v1.bz2 poke_file "$TEST_IMG" "$tracks_offset" "\x00\x00\x00\x00" { $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
tests/qemu-iotests/076.out +4 -4
@@ -1,18 +1,18 @@ QA output created by 076 -== Read from a valid (enough) image == +== Read from a valid v1 image == read 65536/65536 bytes at offset 0 64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) == Negative catalog size == -qemu-io: can't open device TEST_DIR/fake.parallels: Catalog too large +qemu-io: can't open device TEST_DIR/parallels-v1: Catalog too large no file open, try 'help open' == Overflow in catalog allocation == -qemu-io: can't open device TEST_DIR/fake.parallels: Catalog too large +qemu-io: can't open device TEST_DIR/parallels-v1: Catalog too large no file open, try 'help open' == Zero sectors per track == -qemu-io: can't open device TEST_DIR/fake.parallels: Invalid image: Zero sectors per track +qemu-io: can't open device TEST_DIR/parallels-v1: Invalid image: Zero sectors per track no file open, try 'help open' *** done
tests/qemu-iotests/sample_images/fake.parallels.bz2 deleted binary +0 -0
tests/qemu-iotests/sample_images/parallels-v1.bz2 new file binary +0 -0
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 10/53] snapshot: Reset err to NULL to avoid double free
In reply to Stefan HajnocziFrom: Chris Spiegel <[email protected]>
If an error occurs in bdrv_snapshot_delete_by_id_or_name(), "err" is freed. If "err" is not set to NULL before calling bdrv_snapshot_delete_by_id_or_name() again, it will not be updated on error, and will be freed again.
This can be triggered by starting a VM with at least two drives and then attempting to delete a non-existent snapshot.
Broken in commit a89d89d.
Message-id: [email protected]
--- savevm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
savevm.c +2 -1
@@ -1246,7 +1246,7 @@ int load_vmstate(const char *name)void do_delvm (Monitor *mon,const QDict *qdict) {BlockDriverState *bs; -Error *err =NULL ; +Error *err;const char *name =qdict_get_str (qdict,"name" );if (!find_vmstate_bs ()) {
@@ -1257,6 +1257,7 @@ void do_delvm(Monitor *mon, const QDict *qdict) bs =NULL ;while ((bs =bdrv_next (bs))) {if (bdrv_can_snapshot (bs)) { + err =NULL ;bdrv_snapshot_delete_by_id_or_name (bs, name, &err);if (err) { monitor_printf(mon,
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 12/53] iotests: add v2 parallels sample image and simple test for it
In reply to Stefan HajnocziFrom: "Denis V. Lunev" <[email protected]>
This is simple test image for the following commit made by me.
commit d25d59802021a747812472780d80a0e792078f40
Author: Denis V. Lunev <[email protected]>
Date: Mon Jul 28 20:23:55 2014 +0400
parallels: 2TB+ parallels images support
Message-id: [email protected]
--- tests/qemu-iotests/076 | 5 +++++ tests/qemu-iotests/076.out | 4 ++++ tests/qemu-iotests/sample_images/parallels-v2.bz2 | Bin 0 -> 150 bytes 3 files changed, 9 insertions(+) create mode 100644 tests/qemu-iotests/sample_images/parallels-v2.bz2
tests/qemu-iotests/076 +5 -0
@@ -70,6 +70,11 @@ _use_sample_img parallels-v1.bz2 poke_file "$TEST_IMG" "$tracks_offset" "\x00\x00\x00\x00" { $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir +echo +echo "== Read from a valid v2 image ==" +_use_sample_img parallels-v2.bz2 +{ $QEMU_IO -c "read -P 0x11 0 64k" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir + # success, all done echo "*** done" rm -f $seq.full
tests/qemu-iotests/076.out +4 -0
@@ -15,4 +15,8 @@ no file open, try 'help open' == Zero sectors per track == qemu-io: can't open device TEST_DIR/parallels-v1: Invalid image: Zero sectors per track no file open, try 'help open' + +== Read from a valid v2 image == +read 65536/65536 bytes at offset 0 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) *** done
tests/qemu-iotests/sample_images/parallels-v2.bz2 new file binary +0 -0
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 13/53] block/parallels: fix access to not initialized memory in catalog_bitmap
In reply to Stefan HajnocziFrom: "Denis V. Lunev" <[email protected]>
found by valgrind.
Command: ./qemu-img convert -f parallels -O qcow2 1.hds 1.img Invalid read of size 4
at 0x17D0EF: parallels_co_read (parallels.c:357) by 0x11FEE4: bdrv_aio_rw_vector (block.c:4640)
by 0x11FFBF: bdrv_aio_readv_em (block.c:4652)
by 0x11F55F: bdrv_co_readv_em (block.c:4862)
by 0x123428: bdrv_aligned_preadv (block.c:3056)
by 0x1239FA: bdrv_co_do_preadv (block.c:3162)
by 0x125424: bdrv_rw_co_entry (block.c:2706)
by 0x155DD9: coroutine_trampoline (coroutine-ucontext.c:118) by 0x6975B6F: ??? (in /lib/x86_64-linux-gnu/libc-2.19.so)
The problem is that s->catalog_bitmap is allocated/filled as gmalloc(s->catalog_size) thus index validity check must be inclusive, i.e. index >= s->catalog_size is invalid.
Message-id: [email protected]
--- block/parallels.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/parallels.c +1 -1
@@ -155,7 +155,7 @@ static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num) offset = sector_num % s->tracks;/* not allocated */ -if ((index > s->catalog_size) || (s->catalog_bitmap[index] ==0 )) +if ((index >= s->catalog_size) || (s->catalog_bitmap[index] ==0 ))return -1 ;return ((uint64_t )s->catalog_bitmap[index] * s->off_multiplier + offset) *512 ;
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 14/53] rbd: Add support for bdrv_invalidate_cache
In reply to Stefan HajnocziFrom: Adam Crume <[email protected]>
This fixes Ceph issue 2467: ttp://tracker.ceph.com/issues/2467
[Dropped return r in void function as suggested by Josh Durgin <[email protected]>.
--Stefan]
Message-id: [email protected]
--- block/rbd.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
block/rbd.c +15 -0
@@ -887,6 +887,18 @@ static BlockAIOCB* qemu_rbd_aio_discard(BlockDriverState *bs, }#endif +#ifdef LIBRBD_SUPPORTS_INVALIDATE +static void qemu_rbd_invalidate_cache (BlockDriverState *bs, +Error **errp) +{ +BDRVRBDState *s = bs->opaque; +int r =rbd_invalidate_cache (s->image); +if (r <0 ) { +error_setg_errno (errp, -r,"Failed to invalidate the cache" ); + } +} +#endif +static QemuOptsList qemu_rbd_create_opts = { .name ="rbd-create-opts" , .head =QTAILQ_HEAD_INITIALIZER (qemu_rbd_create_opts.head),
@@ -936,6 +948,9 @@ static BlockDriver bdrv_rbd = { .bdrv_snapshot_delete = qemu_rbd_snap_remove, .bdrv_snapshot_list = qemu_rbd_snap_list, .bdrv_snapshot_goto = qemu_rbd_snap_rollback, +#ifdef LIBRBD_SUPPORTS_INVALIDATE + .bdrv_invalidate_cache = qemu_rbd_invalidate_cache, +#endif };static void bdrv_rbd_init (void )
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 15/53] block.c: Fix type of IoOperationType variable in send_qmp_error_event()
In reply to Stefan HajnocziFrom: Peter Maydell <[email protected]>
The local variable 'ac' in send_qmp_error_event() is declared with the wrong type, which causes clang to complain when it is initialized and again when it is used:
block.c:3655:20: warning: implicit conversion from enumeration type 'enum IoOperationType' to different enumeration type 'BlockErrorAction' (aka 'enum BlockErrorAction') [-Wenum-conversion]
ac = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
~ ^~~~~~~~~~~~~~~~~~~~~~
block.c:3655:45: warning: implicit conversion from enumeration type 'enum IoOperationType' to different enumeration type 'BlockErrorAction' (aka 'enum BlockErrorAction') [-Wenum-conversion]
ac = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
~ ^~~~~~~~~~~~~~~~~~~~~~~block.c:3656:62: warning: implicit conversion from enumeration type 'BlockErrorAction' (aka 'enum BlockErrorAction') to different enumeration type 'IoOperationType' (aka 'enum IoOperationType') [-Wenum-conversion]
qapi_event_send_block_io_error(bdrv_get_device_name(bs), ac, action,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^~
Correct the type to IoOperationType, and rename the variable to 'optype' to match its correct type.
Message-id: [email protected]
--- block.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
block.c +3 -3
@@ -3544,10 +3544,10 @@ static void send_qmp_error_event(BlockDriverState *bs,BlockErrorAction action, bool is_read, int error) { - BlockErrorAction ac; + IoOperationType optype; - ac = is_read ?IO_OPERATION_TYPE_READ :IO_OPERATION_TYPE_WRITE ; -qapi_event_send_block_io_error (bdrv_get_device_name (bs), ac, action, + optype = is_read ?IO_OPERATION_TYPE_READ :IO_OPERATION_TYPE_WRITE ; +qapi_event_send_block_io_error (bdrv_get_device_name (bs), optype, action,bdrv_iostatus_is_enabled (bs), error ==ENOSPC ,strerror (error), &error_abort);
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 16/53] snapshot: add bdrv_drain_all() to bdrv_snapshot_delete() to avoid concurrency problem
In reply to Stefan HajnocziFrom: Zhang Haoyu <[email protected]>
If there are still pending i/o while deleting snapshot,
because deleting snapshot is done in non-coroutine context, and the pending i/o read/write (bdrv_co_do_rw) is done in coroutine context, so it's possible to cause concurrency problem between above two operations. Add bdrv_drain_all() to bdrv_snapshot_delete() to avoid this problem.
Message-id: [email protected]
--- block/snapshot.c | 4 ++++ 1 file changed, 4 insertions(+)
block/snapshot.c +4 -0
@@ -236,6 +236,10 @@ int bdrv_snapshot_delete(BlockDriverState *bs,error_setg (errp,"snapshot_id and name are both NULL" );return -EINVAL ; } + +/* drain all pending i/o before deleting snapshot */ +bdrv_drain_all (); +if (drv->bdrv_snapshot_delete) {return drv->bdrv_snapshot_delete (bs, snapshot_id, name, errp); }
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 17/53] block/curl: Improve type safety of s->timeout.
In reply to Stefan HajnocziFrom: "Richard W.M. Jones" <[email protected]>
qemu_opt_get_number returns a uint64_t, and curl_easy_setopt expects a long (not an int). There is no warning about the latter type error because curl_easy_setopt uses a varargs argument.
Store the timeout (which is a positive number of seconds) as a uint64_t. Check that the number given by the user is reasonable. Zero is permissible (meaning no timeout is enforced by cURL).
Cast it to long before calling curl_easy_setopt to fix the type error.
Example error message after this change has been applied:
$ ./qemu-img create -f qcow2 /tmp/test.qcow2 \
-b 'json: { "file.driver":"https",
"file.url":"https://foo/bar",
"file.timeout":-1 }'
qemu-img: /tmp/test.qcow2: Could not open 'json: { "file.driver":"https", "file.url":"https://foo/bar", "file.timeout":-1 }': timeout parameter is too large or negative: Invalid argument--- block/curl.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
block/curl.c +7 -2
@@ -64,6 +64,7 @@ static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,#define SECTOR_SIZE 512#define READ_AHEAD_DEFAULT (256 * 1024)#define CURL_TIMEOUT_DEFAULT 5 +#define CURL_TIMEOUT_MAX 10000#define FIND_RET_NONE 0#define FIND_RET_OK 1
@@ -112,7 +113,7 @@ typedef struct BDRVCURLState {char *url;size_t readahead_size;bool sslverify; -int timeout; +uint64_t timeout;char *cookie;bool accept_range;AioContext *aio_context;
@@ -390,7 +391,7 @@ static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s)if (s->cookie) {curl_easy_setopt (state->curl,CURLOPT_COOKIE , s->cookie); } -curl_easy_setopt (state->curl,CURLOPT_TIMEOUT , s->timeout); +curl_easy_setopt (state->curl,CURLOPT_TIMEOUT , (long )s->timeout);curl_easy_setopt (state->curl,CURLOPT_WRITEFUNCTION , (void *)curl_read_cb);curl_easy_setopt (state->curl,CURLOPT_WRITEDATA , (void *)state);
@@ -546,6 +547,10 @@ static int curl_open(BlockDriverState *bs, QDict *options, int flags, s->timeout =qemu_opt_get_number (opts,CURL_BLOCK_OPT_TIMEOUT ,CURL_TIMEOUT_DEFAULT ); +if (s->timeout >CURL_TIMEOUT_MAX ) { +error_setg (errp,"timeout parameter is too large or negative" ); + goto out_noclean; + } s->sslverify =qemu_opt_get_bool (opts,CURL_BLOCK_OPT_SSLVERIFY , true);
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 18/53] raw-posix: Fix raw_co_get_block_status() after EOF
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
As its comment states, raw_co_get_block_status() should unconditionally return 0 and set *pnum to 0 for after EOF.
An assertion after lseek(..., SEEK_HOLE) tried to catch this case by asserting that errno != -ENXIO (which would indicate a position after the EOF); but it should be errno != ENXIO instead. Regardless of that, there should be no such assertion at all. If bdrv_getlength() returned an outdated value and the image has been resized outside of qemu, lseek() will return with errno == ENXIO. Just return that value as an error then.
Setting *pnum to 0 and returning 0 should not be done here, as in that case we should update the device length as well. So, from qemu's perspective, the file has not been resized; it's just that there was an error querying sectors beyond a certain point (the actual file size).
Additionally, nb_sectors should be clamped against the image end. This was probably not an issue if FIEMAP or SEEK_HOLE/SEEK_DATA worked, but the fallback did not take this case into account.
Message-id: [email protected]
--- block/raw-posix.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
block/raw-posix.c +10 -4
@@ -1535,10 +1535,6 @@ static int64_t try_seek_hole(BlockDriverState *bs, off_t start, off_t *data, *hole =lseek (s->fd, start,SEEK_HOLE );if (*hole ==-1 ) { -/* -ENXIO indicates that sector_num was past the end of the file. -* There is a virtual hole there. */ -assert (errno != -ENXIO ); -return -errno; }
@@ -1578,6 +1574,7 @@ static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs, int nb_sectors, int *pnum) { off_t start, data = 0, hole = 0; +int64_t total_size;int64_t ret; ret =fd_open (bs);
@@ -1586,6 +1583,15 @@ static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs, } start = sector_num *BDRV_SECTOR_SIZE ; + total_size =bdrv_getlength (bs); +if (total_size <0 ) { +return total_size; + }else if (start >= total_size) { + *pnum =0 ; +return 0 ; + }else if (start + nb_sectors *BDRV_SECTOR_SIZE > total_size) { + nb_sectors =DIV_ROUND_UP (total_size - start,BDRV_SECTOR_SIZE ); + } ret =try_seek_hole (bs, start, &data, &hole, pnum);if (ret <0 ) {
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 19/53] raw-posix: raw_co_get_block_status() return value
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
Instead of generating the full return value thrice in try_fiemap(), try_seek_hole() and as a fall-back in raw_co_get_block_status() itself, generate the value only in raw_co_get_block_status().
While at it, also remove the pnum parameter from try_fiemap() and try_seek_hole().
Message-id: [email protected]
--- block/raw-posix.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)
block/raw-posix.c +14 -14
@@ -1481,12 +1481,12 @@ out:return result; } -static int64_t try_fiemap (BlockDriverState *bs,off_t start,off_t *data, -off_t *hole,int nb_sectors,int *pnum) +static int try_fiemap (BlockDriverState *bs,off_t start,off_t *data, +off_t *hole,int nb_sectors) { #ifdef CONFIG_FIEMAP BDRVRawState *s = bs->opaque; - int64_t ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | start; + int ret = 0; struct { struct fiemap fm; struct fiemap_extent fe;
@@ -1527,8 +1527,8 @@ static int64_t try_fiemap(BlockDriverState *bs, off_t start, off_t *data,#endif } -static int64_t try_seek_hole (BlockDriverState *bs,off_t start,off_t *data, -off_t *hole,int *pnum) +static int try_seek_hole (BlockDriverState *bs,off_t start,off_t *data, +off_t *hole) {#if definedSEEK_HOLE && definedSEEK_DATA BDRVRawState *s = bs->opaque;
@@ -1548,7 +1548,7 @@ static int64_t try_seek_hole(BlockDriverState *bs, off_t start, off_t *data, } } -return BDRV_BLOCK_DATA |BDRV_BLOCK_OFFSET_VALID | start; +return 0 ;#else return -ENOTSUP ;#endif
@@ -1575,7 +1575,7 @@ static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs, {off_t start, data =0 , hole =0 ;int64_t total_size; -int64_t ret; +int ret; ret =fd_open (bs);if (ret <0 ) {
@@ -1593,28 +1593,28 @@ static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs, nb_sectors =DIV_ROUND_UP (total_size - start,BDRV_SECTOR_SIZE ); } - ret =try_seek_hole (bs, start, &data, &hole, pnum); + ret =try_seek_hole (bs, start, &data, &hole);if (ret <0 ) { - ret =try_fiemap (bs, start, &data, &hole, nb_sectors, pnum); + ret =try_fiemap (bs, start, &data, &hole, nb_sectors);if (ret <0 ) {/* Assume everything is allocated. */ data =0 ; hole = start + nb_sectors *BDRV_SECTOR_SIZE ; - ret =BDRV_BLOCK_DATA |BDRV_BLOCK_OFFSET_VALID | start; + ret =0 ; } } +assert (ret >=0 ); +if (data <= start) {/* On a data extent, compute sectors to the end of the extent. */ *pnum =MIN (nb_sectors, (hole - start) /BDRV_SECTOR_SIZE ); +return ret |BDRV_BLOCK_DATA |BDRV_BLOCK_OFFSET_VALID | start; }else {/* On a hole, compute sectors to the beginning of the next extent. */ *pnum =MIN (nb_sectors, (data - start) /BDRV_SECTOR_SIZE ); - ret &= ~BDRV_BLOCK_DATA ; - ret |=BDRV_BLOCK_ZERO ; +return ret |BDRV_BLOCK_ZERO |BDRV_BLOCK_OFFSET_VALID | start; } - -return ret; } static coroutine_fn BlockAIOCB *raw_aio_discard(BlockDriverState *bs,
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 20/53] iotests: Add test for external image truncation
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
It should not be happening, but it is possible to truncate an image outside of qemu while qemu is running (or any of the qemu tools using the block layer. raw_co_get_block_status() should not break then.
While touching this test, replace the existing "truncate" invocation by "$QEMU_IMG convert -f raw".
Message-id: [email protected]
--- tests/qemu-iotests/102 | 21 +++++++++++++++++++-- tests/qemu-iotests/102.out | 11 +++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-)
tests/qemu-iotests/102 +19 -2
@@ -34,9 +34,10 @@ _cleanup() } trap "_cleanup; exit \$status" 0 1 2 3 15 -# get standard environment, filters and checks +# get standard environment, filters and qemu instance handling . ./common.rc . ./common.filter +. ./common.qemu _supported_fmt qcow2 _supported_proto file
@@ -53,11 +54,27 @@ _make_test_img $IMG_SIZE $QEMU_IO -c 'write 0 64k' "$TEST_IMG" | _filter_qemu_io # Remove data cluster from image (first cluster: image header, second: reftable, # third: refblock, fourth: L1 table, fifth: L2 table) -truncate -s $((5 * 64 * 1024)) "$TEST_IMG" +$QEMU_IMG resize -f raw "$TEST_IMG" $((5 * 64 * 1024)) $QEMU_IO -c map "$TEST_IMG" $QEMU_IMG map "$TEST_IMG" +echo +echo '=== Testing map on an image file truncated outside of qemu ===' +echo + +# Same as above, only now we concurrently truncate and map the image +_make_test_img $IMG_SIZE +$QEMU_IO -c 'write 0 64k' "$TEST_IMG" | _filter_qemu_io + +qemu_comm_method=monitor _launch_qemu -drive if=none,file="$TEST_IMG",id=drv0 + +$QEMU_IMG resize -f raw "$TEST_IMG" $((5 * 64 * 1024)) + +_send_qemu_cmd $QEMU_HANDLE 'qemu-io drv0 map' 'allocated' \ + | sed -e 's/^(qemu).*qemu-io drv0 map...$/(qemu) qemu-io drv0 map/' +_send_qemu_cmd $QEMU_HANDLE 'quit' '' + # success, all done echo '*** done' rm -f $seq.full
tests/qemu-iotests/102.out +11 -0
@@ -5,6 +5,17 @@ QA output created by 102 Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=65536 wrote 65536/65536 bytes at offset 0 64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +Image resized. [ 0] 128/ 128 sectors allocated at offset 0 bytes (1) Offset Length Mapped to File + +=== Testing map on an image file truncated outside of qemu === + +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=65536 +wrote 65536/65536 bytes at offset 0 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +Image resized. +QEMU X.Y.Z monitor - type 'help' for more information +(qemu) qemu-io drv0 map +[ 0] 128/ 128 sectors allocated at offset 0 bytes (1) *** done
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 21/53] qcow2: Allow "full" discard
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
Normally, discarded sectors should read back as zero. However, there are cases in which a sector (or rather cluster) should be discarded as if they were never written in the first place, that is, reading them should fall through to the backing file again.
Message-id: [email protected]
--- block/qcow2-cluster.c | 27 +++++++++++++++++---------- block/qcow2-snapshot.c | 2 +- block/qcow2.c | 2 +- block/qcow2.h | 2 +- 4 files changed, 20 insertions(+), 13 deletions(-)
block/qcow2-cluster.c +17 -10
@@ -1414,7 +1414,7 @@ int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)* clusters. */ static int discard_single_l2 (BlockDriverState *bs,uint64_t offset, -unsigned int nb_clusters,enum qcow2_discard_type type) +unsigned int nb_clusters,enum qcow2_discard_type type,bool full_discard) {BDRVQcowState *s = bs->opaque;uint64_t *l2_table;
@@ -1436,23 +1436,30 @@ static int discard_single_l2(BlockDriverState *bs, uint64_t offset, old_l2_entry =be64_to_cpu (l2_table[l2_index + i]);/* -* Make sure that a discarded area reads back as zeroes for v3 images -* (we cannot do it for v2 without actually writing a zero-filled -* buffer). We can skip the operation if the cluster is already marked -* as zero, or if it's unallocated and we don't have a backing file. +* If full_discard is false, make sure that a discarded area reads back +* as zeroes for v3 images (we cannot do it for v2 without actually +* writing a zero-filled buffer). We can skip the operation if the +* cluster is already marked as zero, or if it's unallocated and we +* don't have a backing file. * * TODO We might want to use bdrv_get_block_status(bs) here, but we're * holding s->lock, so that doesn't work today. +* +* If full_discard is true, the sector should not read back as zeroes, +* but rather fall through to the backing file. */ switch (qcow2_get_cluster_type (old_l2_entry)) {case QCOW2_CLUSTER_UNALLOCATED : -if (!bs->backing_hd) { +if (full_discard || !bs->backing_hd) {continue ; }break ;case QCOW2_CLUSTER_ZERO : -continue ; +if (!full_discard) { +continue ; + } +break ;case QCOW2_CLUSTER_NORMAL :case QCOW2_CLUSTER_COMPRESSED :
@@ -1464,7 +1471,7 @@ static int discard_single_l2(BlockDriverState *bs, uint64_t offset,/* First remove L2 entries */ qcow2_cache_entry_mark_dirty (s->l2_table_cache, l2_table); -if (s->qcow_version >=3 ) { +if (!full_discard && s->qcow_version >=3 ) { l2_table[l2_index + i] =cpu_to_be64 (QCOW_OFLAG_ZERO ); }else { l2_table[l2_index + i] =cpu_to_be64 (0 );
@@ -1483,7 +1490,7 @@ static int discard_single_l2(BlockDriverState *bs, uint64_t offset, }int qcow2_discard_clusters (BlockDriverState *bs,uint64_t offset, -int nb_sectors,enum qcow2_discard_type type) +int nb_sectors,enum qcow2_discard_type type,bool full_discard) {BDRVQcowState *s = bs->opaque;uint64_t end_offset;
@@ -1506,7 +1513,7 @@ int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,/* Each L2 table is handled by its own loop iteration */ while (nb_clusters >0 ) { - ret =discard_single_l2 (bs, offset, nb_clusters, type); + ret =discard_single_l2 (bs, offset, nb_clusters, type, full_discard);if (ret <0 ) { goto fail; }
block/qcow2-snapshot.c +1 -1
@@ -441,7 +441,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)qcow2_discard_clusters (bs,qcow2_vm_state_offset (s),align_offset (sn->vm_state_size, s->cluster_size) >>BDRV_SECTOR_BITS , -QCOW2_DISCARD_NEVER ); +QCOW2_DISCARD_NEVER , false);#ifdef DEBUG_ALLOC {
block/qcow2.c +1 -1
@@ -2089,7 +2089,7 @@ static coroutine_fn int qcow2_co_discard(BlockDriverState *bs,qemu_co_mutex_lock (&s->lock); ret =qcow2_discard_clusters (bs, sector_num <<BDRV_SECTOR_BITS , - nb_sectors,QCOW2_DISCARD_REQUEST ); + nb_sectors,QCOW2_DISCARD_REQUEST , false);qemu_co_mutex_unlock (&s->lock);return ret; }
block/qcow2.h +1 -1
@@ -534,7 +534,7 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m);int qcow2_discard_clusters (BlockDriverState *bs,uint64_t offset, -int nb_sectors,enum qcow2_discard_type type); +int nb_sectors,enum qcow2_discard_type type,bool full_discard);int qcow2_zero_clusters (BlockDriverState *bs,uint64_t offset,int nb_sectors);int qcow2_expand_zero_clusters (BlockDriverState *bs);
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 22/53] qcow2: Implement bdrv_make_empty()
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
Implement this function by making all clusters in the image file fall through to the backing file (by using the recently extended discard).
Message-id: [email protected]
--- block/qcow2.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
block/qcow2.c +27 -0
@@ -2230,6 +2230,32 @@ fail:return ret; } +static int qcow2_make_empty (BlockDriverState *bs) +{ +int ret =0 ; +uint64_t start_sector; +int sector_step =INT_MAX /BDRV_SECTOR_SIZE ; + +for (start_sector =0 ; start_sector < bs->total_sectors; + start_sector += sector_step) + { +/* As this function is generally used after committing an external +* snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the +* default action for this kind of discard is to pass the discard, +* which will ideally result in an actually smaller image file, as +* is probably desired. */ + ret =qcow2_discard_clusters (bs, start_sector *BDRV_SECTOR_SIZE , +MIN (sector_step, + bs->total_sectors - start_sector), +QCOW2_DISCARD_SNAPSHOT , true); +if (ret <0 ) { +break ; + } + } + +return ret; +} + static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs) {BDRVQcowState *s = bs->opaque;
@@ -2676,6 +2702,7 @@ static BlockDriver bdrv_qcow2 = { .bdrv_co_discard = qcow2_co_discard, .bdrv_truncate = qcow2_truncate, .bdrv_write_compressed = qcow2_write_compressed, + .bdrv_make_empty = qcow2_make_empty, .bdrv_snapshot_create = qcow2_snapshot_create, .bdrv_snapshot_goto = qcow2_snapshot_goto,
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 23/53] qcow2: Optimize bdrv_make_empty()
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
bdrv_make_empty() is currently only called if the current image represents an external snapshot that has been committed to its base image; it is therefore unlikely to have internal snapshots. In this case, bdrv_make_empty() can be greatly sped up by emptying the L1 and refcount table (while having the dirty flag set, which only works for compat=1.1) and creating a trivial refcount structure.
If there are snapshots or for compat=0.10, fall back to the simple implementation (discard all clusters).
[Applied s/clusters/cluster/ typo fix suggested by Eric Blake --Stefan]
Message-id: [email protected]
--- block/blkdebug.c | 2 + block/qcow2.c | 165 +++++++++++++++++++++++++++++++++++++++++++++++++- include/block/block.h | 2 + 3 files changed, 168 insertions(+), 1 deletion(-)
block/blkdebug.c +2 -0
@@ -195,6 +195,8 @@ static const char *event_names[BLKDBG_EVENT_MAX] = { [BLKDBG_PWRITEV] = "pwritev", [BLKDBG_PWRITEV_ZERO] = "pwritev_zero", [BLKDBG_PWRITEV_DONE] = "pwritev_done", + + [BLKDBG_EMPTY_IMAGE_PREPARE] = "empty_image_prepare", };static int get_event_by_name (const char *name,BlkDebugEvent *event)
block/qcow2.c +164 -1
176 lines not on this page · raw message
include/block/block.h +2 -0
@@ -498,6 +498,8 @@ typedef enum {BLKDBG_PWRITEV_ZERO ,BLKDBG_PWRITEV_DONE , +BLKDBG_EMPTY_IMAGE_PREPARE , +BLKDBG_EVENT_MAX , } BlkDebugEvent;
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 24/53] blockjob: Introduce block_job_complete_sync()
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
Implement block_job_complete_sync() by doing the exact same thing as block_job_cancel_sync() does, only with calling block_job_complete() instead of block_job_cancel().
Message-id: [email protected]
--- blockjob.c | 39 ++++++++++++++++++++++++++++++++------- include/block/blockjob.h | 15 +++++++++++++++ 2 files changed, 47 insertions(+), 7 deletions(-)
blockjob.c +32 -7
@@ -153,7 +153,7 @@ void block_job_iostatus_reset(BlockJob *job) } } -struct BlockCancelData { +struct BlockFinishData {BlockJob *job;BlockCompletionFunc *cb;void *opaque;
@@ -161,19 +161,22 @@ struct BlockCancelData {int ret; }; -static void block_job_cancel_cb (void *opaque,int ret) +static void block_job_finish_cb (void *opaque,int ret) { -struct BlockCancelData *data = opaque; +struct BlockFinishData *data = opaque; data->cancelled =block_job_is_cancelled (data->job); data->ret = ret; data->cb (data->opaque, ret); } -int block_job_cancel_sync (BlockJob *job) +static int block_job_finish_sync (BlockJob *job, +void (*finish)(BlockJob *,Error **errp), +Error **errp) { -struct BlockCancelData data; +struct BlockFinishData data;BlockDriverState *bs = job->bs; +Error *local_err =NULL ;assert (bs->job == job);
@@ -184,15 +187,37 @@ int block_job_cancel_sync(BlockJob *job) data.cb = job->cb; data.opaque = job->opaque; data.ret = -EINPROGRESS ; - job->cb = block_job_cancel_cb; + job->cb = block_job_finish_cb; job->opaque = &data; -block_job_cancel (job); +finish (job, &local_err); +if (local_err) { +error_propagate (errp, local_err); +return -EBUSY ; + }while (data.ret == -EINPROGRESS ) {aio_poll (bdrv_get_aio_context (bs), true); }return (data.cancelled && data.ret ==0 ) ? -ECANCELED : data.ret; } +/* A wrapper around block_job_cancel() taking an Error ** parameter so it may be +* used with block_job_finish_sync() without the need for (rather nasty) +* function pointer casts there. */ +static void block_job_cancel_err (BlockJob *job,Error **errp) +{ +block_job_cancel (job); +} + +int block_job_cancel_sync (BlockJob *job) +{ +return block_job_finish_sync (job, &block_job_cancel_err,NULL ); +} + +int block_job_complete_sync (BlockJob *job,Error **errp) +{ +return block_job_finish_sync (job, &block_job_complete, errp); +} +void block_job_sleep_ns (BlockJob *job,QEMUClockType type,int64_t ns) {assert (job->busy);
include/block/blockjob.h +15 -0
@@ -273,6 +273,21 @@ bool block_job_is_paused(BlockJob *job);int block_job_cancel_sync (BlockJob *job);/** +* block_job_complete_sync: +* @job: The job to be completed. +* @errp: Error object which may be set by block_job_complete(); this is not +* necessarily set on every error, the job return value has to be +* checked as well. +* +* Synchronously complete the job. The completion callback is called before the +* function returns, unless it is NULL (which is permissible when using this +* function). +* +* Returns the return value from the job. +*/ +int block_job_complete_sync (BlockJob *job,Error **errp); + +/** * block_job_iostatus_reset: * @job: The job whose I/O status should be reset. *
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 25/53] blockjob: Add "ready" field
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
When a block job signals readiness, this is currently reported only through QMP. If qemu wants to use block jobs for internal tasks, there needs to be another way to correctly detect when a block job may be completed.
For this reason, introduce a bool "ready" which is set when the block job may be completed.
Message-id: [email protected]
--- blockjob.c | 3 +++ include/block/blockjob.h | 5 +++++ qapi/block-core.json | 4 +++- 3 files changed, 11 insertions(+), 1 deletion(-)
blockjob.c +3 -0
@@ -261,6 +261,7 @@ BlockJobInfo *block_job_query(BlockJob *job) info->offset = job->offset; info->speed = job->speed; info->io_status = job->iostatus; + info->ready = job->ready;return info; }
@@ -296,6 +297,8 @@ void block_job_event_completed(BlockJob *job, const char *msg)void block_job_event_ready (BlockJob *job) { + job->ready = true; + qapi_event_send_block_job_ready(job->driver->job_type, bdrv_get_device_name(job->bs), job->len,
include/block/blockjob.h +5 -0
@@ -91,6 +91,11 @@ struct BlockJob {*/ bool busy; +/** +* Set to true when the job is ready to be completed. +*/ +bool ready; +/** Status that is published by the query-block-jobs QMP API */ BlockDeviceIoStatus iostatus;
qapi/block-core.json +3 -1
@@ -514,12 +514,14 @@ # # @io-status: the status of the job (since 1.3) # +# @ready: true if the job may be completed (since 2.2) +# # Since: 1.1 ## { 'type': 'BlockJobInfo', 'data': {'type': 'str', 'device': 'str', 'len': 'int', 'offset': 'int', 'busy': 'bool', 'paused': 'bool', 'speed': 'int', - 'io-status': 'BlockDeviceIoStatus'} } + 'io-status': 'BlockDeviceIoStatus', 'ready': 'bool'} } ## # @query-block-jobs:
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 26/53] iotests: Omit length/offset test in 040 and 041
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
As of a follow-up patch to this one, the length of a mirror block job will no longer directly depend on the size of the block device; therefore, drop these checks from this test. Instead, just check whether the final offset equals the block job length.
As 041 uses the wait_until_completed function from iotests.py, the same applies there as well which in turn affects tests 030, 055 and 056. On the other hand, a block job's length does not have to be related to the length of the image file in the first place, so that check was questionable anyway.
Message-id: [email protected]
--- tests/qemu-iotests/040 | 4 +--- tests/qemu-iotests/041 | 5 +---- tests/qemu-iotests/iotests.py | 3 +-- 3 files changed, 3 insertions(+), 9 deletions(-)
tests/qemu-iotests/040 +1 -3
@@ -43,8 +43,7 @@ class ImageCommitTestCase(iotests.QMPTestCase): if event['event'] == 'BLOCK_JOB_COMPLETED': self.assert_qmp(event, 'data/type', 'commit') self.assert_qmp(event, 'data/device', 'drive0') - self.assert_qmp(event, 'data/offset', self.image_len) - self.assert_qmp(event, 'data/len', self.image_len) + self.assert_qmp(event, 'data/offset', event['data']['len']) if need_ready: self.assertTrue(ready, "Expecting BLOCK_JOB_COMPLETED event") completed = True
@@ -52,7 +51,6 @@ class ImageCommitTestCase(iotests.QMPTestCase): ready = True self.assert_qmp(event, 'data/type', 'commit') self.assert_qmp(event, 'data/device', 'drive0') - self.assert_qmp(event, 'data/len', self.image_len) self.vm.qmp('block-job-complete', device='drive0') self.assert_no_active_block_jobs()
tests/qemu-iotests/041 +1 -4
@@ -52,8 +52,7 @@ class ImageMirroringTestCase(iotests.QMPTestCase): event = self.cancel_and_wait(drive=drive) self.assertEquals(event['event'], 'BLOCK_JOB_COMPLETED') self.assert_qmp(event, 'data/type', 'mirror') - self.assert_qmp(event, 'data/offset', self.image_len) - self.assert_qmp(event, 'data/len', self.image_len) + self.assert_qmp(event, 'data/offset', event['data']['len']) def complete_and_wait(self, drive='drive0', wait_ready=True): '''Complete a block job and wait for it to finish'''
@@ -417,7 +416,6 @@ new_state = "1" self.assert_qmp(event, 'data/type', 'mirror') self.assert_qmp(event, 'data/device', 'drive0') self.assert_qmp(event, 'data/error', 'Input/output error') - self.assert_qmp(event, 'data/len', self.image_len) completed = True self.assert_no_active_block_jobs()
@@ -568,7 +566,6 @@ new_state = "1" self.assert_qmp(event, 'data/type', 'mirror') self.assert_qmp(event, 'data/device', 'drive0') self.assert_qmp(event, 'data/error', 'Input/output error') - self.assert_qmp(event, 'data/len', self.image_len) completed = True self.assert_no_active_block_jobs()
tests/qemu-iotests/iotests.py +1 -2
@@ -267,8 +267,7 @@ class QMPTestCase(unittest.TestCase): self.assert_qmp(event, 'data/device', drive) self.assert_qmp_absent(event, 'data/error') if check_offset: - self.assert_qmp(event, 'data/offset', self.image_len) - self.assert_qmp(event, 'data/len', self.image_len) + self.assert_qmp(event, 'data/offset', event['data']['len']) completed = True self.assert_no_active_block_jobs()
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 27/53] block/mirror: Improve progress report
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
Instead of taking the total length of the block device as the block job's length, use the number of dirty sectors. The progress is now the number of sectors mirrored to the target block device. Note that this may result in the job's length increasing during operation, which is however in fact desirable.
Message-id: [email protected]
--- block/mirror.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-)
block/mirror.c +22 -12
@@ -45,6 +45,7 @@ typedef struct MirrorBlockJob {int64_t sector_num;int64_t granularity;size_t buf_size; +int64_t bdev_length;unsigned long *cow_bitmap;BdrvDirtyBitmap *dirty_bitmap;HBitmapIter hbi;
@@ -54,6 +55,7 @@ typedef struct MirrorBlockJob {unsigned long *in_flight_bitmap;int in_flight; +int sectors_in_flight;int ret; } MirrorBlockJob;
@@ -87,6 +89,7 @@ static void mirror_iteration_done(MirrorOp *op, int ret)trace_mirror_iteration_done (s, op->sector_num, op->nb_sectors, ret); s->in_flight--; + s->sectors_in_flight -= op->nb_sectors; iov = op->qiov.iov;for (i =0 ; i < op->qiov.niov; i++) {MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base;
@@ -98,8 +101,11 @@ static void mirror_iteration_done(MirrorOp *op, int ret) chunk_num = op->sector_num / sectors_per_chunk; nb_chunks = op->nb_sectors / sectors_per_chunk;bitmap_clear (s->in_flight_bitmap, chunk_num, nb_chunks); -if (s->cow_bitmap && ret >=0 ) { -bitmap_set (s->cow_bitmap, chunk_num, nb_chunks); +if (ret >=0 ) { +if (s->cow_bitmap) { +bitmap_set (s->cow_bitmap, chunk_num, nb_chunks); + } + s->common.offset += (uint64_t )op->nb_sectors *BDRV_SECTOR_SIZE ; }qemu_iovec_destroy (&op->qiov);
@@ -172,7 +178,7 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s) hbitmap_next_sector = s->sector_num; sector_num = s->sector_num; sectors_per_chunk = s->granularity >>BDRV_SECTOR_BITS ; - end = s->common.len >>BDRV_SECTOR_BITS ; + end = s->bdev_length /BDRV_SECTOR_SIZE ;/* Extend the QEMUIOVector to include all adjacent blocks that will * be copied in this operation.
@@ -284,6 +290,7 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)/* Copy the dirty cluster. */ s->in_flight++; + s->sectors_in_flight += nb_sectors;trace_mirror_one_iteration (s, sector_num, nb_sectors);bdrv_aio_readv (source, sector_num, &op->qiov, nb_sectors, mirror_read_complete, op);
@@ -329,11 +336,11 @@ static void coroutine_fn mirror_run(void *opaque) goto immediate_exit; } - s->common.len =bdrv_getlength (bs); -if (s->common.len <0 ) { - ret = s->common.len; + s->bdev_length =bdrv_getlength (bs); +if (s->bdev_length <0 ) { + ret = s->bdev_length; goto immediate_exit; - }else if (s->common.len ==0 ) { + }else if (s->bdev_length ==0 ) {/* Report BLOCK_JOB_READY and wait for complete. */ block_job_event_ready (&s->common); s->synced = true;
@@ -344,7 +351,7 @@ static void coroutine_fn mirror_run(void *opaque) goto immediate_exit; } - length =DIV_ROUND_UP (s->common.len, s->granularity); + length =DIV_ROUND_UP (s->bdev_length, s->granularity); s->in_flight_bitmap =bitmap_new (length);/* If we have no backing file yet in the destination, we cannot let
@@ -364,7 +371,7 @@ static void coroutine_fn mirror_run(void *opaque) } } - end = s->common.len >>BDRV_SECTOR_BITS ; + end = s->bdev_length /BDRV_SECTOR_SIZE ; s->buf =qemu_try_blockalign (bs, s->buf_size);if (s->buf ==NULL ) { ret = -ENOMEM ;
@@ -409,6 +416,12 @@ static void coroutine_fn mirror_run(void *opaque) } cnt =bdrv_get_dirty_count (bs, s->dirty_bitmap); +/* s->common.offset contains the number of bytes already processed so +* far, cnt is the number of dirty sectors remaining and +* s->sectors_in_flight is the number of sectors currently being +* processed; together those are the current total operation length */ + s->common.len = s->common.offset + + (cnt + s->sectors_in_flight) *BDRV_SECTOR_SIZE ;/* Note that even when no rate limit is applied we need to yield * periodically with no pending I/O so that qemu_aio_flush() returns.
@@ -445,7 +458,6 @@ static void coroutine_fn mirror_run(void *opaque)* report completion. This way, block-job-cancel will leave * the target in a consistent state. */ - s->common.offset = end *BDRV_SECTOR_SIZE ;if (!s->synced) {block_job_event_ready (&s->common); s->synced = true;
@@ -474,8 +486,6 @@ static void coroutine_fn mirror_run(void *opaque) ret =0 ;trace_mirror_before_sleep (s, cnt, s->synced, delay_ns);if (!s->synced) { -/* Publish progress */ - s->common.offset = (end - cnt) *BDRV_SECTOR_SIZE ;block_job_sleep_ns (&s->common,QEMU_CLOCK_REALTIME , delay_ns);if (block_job_is_cancelled (&s->common)) {break ;
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 28/53] qemu-img: Implement commit like QMP
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
qemu-img should use QMP commands whenever possible in order to ensure feature completeness of both online and offline image operations. As qemu-img itself has no access to QMP (since this would basically require just everything being linked into qemu-img), imitate QMP's implementation of block-commit by using commit_active_start() and then waiting for the block job to finish.
Message-id: [email protected]
--- block/Makefile.objs | 3 +-- qemu-img.c | 78 ++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 60 insertions(+), 21 deletions(-)
block/Makefile.objs +1 -2
@@ -9,7 +9,7 @@ block-obj-y += block-backend.o snapshot.o qapi.o block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o block-obj-$(CONFIG_POSIX) += raw-posix.o block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o -block-obj-y += null.o +block-obj-y += null.o mirror.o block-obj-y += nbd.o nbd-client.o sheepdog.o block-obj-$(CONFIG_LIBISCSI) += iscsi.o
@@ -23,7 +23,6 @@ block-obj-y += accounting.o common-obj-y += stream.o common-obj-y += commit.o -common-obj-y += mirror.o common-obj-y += backup.o iscsi.o-cflags := $(LIBISCSI_CFLAGS)
qemu-img.c +59 -19
107 lines not on this page · raw message
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 29/53] qemu-img: Empty image after commit
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
After the top image has been committed, it should be emptied unless specified otherwise.
Message-id: [email protected]
--- qemu-img-cmds.hx | 4 ++-- qemu-img.c | 34 +++++++++++++++++++++++++++++++--- qemu-img.texi | 6 +++++- 3 files changed, 38 insertions(+), 6 deletions(-)
qemu-img-cmds.hx +2 -2
@@ -22,9 +22,9 @@ STEXI ETEXI DEF("commit", img_commit, - "commit [-q] [-f fmt] [-t cache] filename") + "commit [-q] [-f fmt] [-t cache] [-d] filename") STEXI -@item commit [-q] [-f @var{fmt}] [-t @var{cache}] @var{filename} +@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-d] @var{filename} ETEXI DEF("compare", img_compare,
qemu-img.c +31 -3
@@ -757,14 +757,14 @@ static int img_commit(int argc, char **argv)const char *filename, *fmt, *cache;BlockBackend *blk;BlockDriverState *bs, *base_bs; -bool quiet = false; +bool quiet = false, drop = false;Error *local_err =NULL ;CommonBlockJobCBInfo cbi; fmt =NULL ; cache =BDRV_DEFAULT_CACHE ;for (;;) { - c =getopt (argc, argv,"f:ht:q" ); + c =getopt (argc, argv,"f:ht:dq" );if (c ==-1 ) {break ; }
@@ -779,6 +779,9 @@ static int img_commit(int argc, char **argv)case 't' : cache = optarg;break ; +case 'd' : + drop = true; +break ;case 'q' : quiet = true;break ;
@@ -789,7 +792,7 @@ static int img_commit(int argc, char **argv) } filename = argv[optind++]; - flags =BDRV_O_RDWR ; + flags =BDRV_O_RDWR |BDRV_O_UNMAP ; ret =bdrv_parse_cache_flags (cache, &flags);if (ret <0 ) {error_report ("Invalid cache option: %s" , cache);
@@ -822,7 +825,32 @@ static int img_commit(int argc, char **argv) goto done; } +/* The block job will swap base_bs and bs (which is not what we really want +* here, but okay) and unref base_bs (after the swap, i.e., the old top +* image). In order to still be able to empty that top image afterwards, +* increment the reference counter here preemptively. */ +if (!drop) { +bdrv_ref (base_bs); + } +run_block_job (bs->job, &local_err); +if (local_err) { + goto unref_backing; + } + +if (!drop && base_bs->drv->bdrv_make_empty) { + ret = base_bs->drv->bdrv_make_empty (base_bs); +if (ret) { +error_setg_errno (&local_err, -ret,"Could not empty %s" , + filename); + goto unref_backing; + } + } + +unref_backing: +if (!drop) { +bdrv_unref (base_bs); + } done:blk_unref (blk);
qemu-img.texi +5 -1
@@ -167,7 +167,7 @@ this case. @var{backing_file} will never be modified unless you use the The size can also be specified using the @var{size} option with @code{-o}, it doesn't need to be specified separately in this case. -@item commit [-f @var{fmt}] [-t @var{cache}] @var{filename} +@item commit [-f @var{fmt}] [-t @var{cache}] [-d] @var{filename} Commit the changes recorded in @var{filename} in its base image or backing file. If the backing file is smaller than the snapshot, then the backing file will be
@@ -176,6 +176,10 @@ the backing file, the backing file will not be truncated. If you want the backing file to match the size of the smaller snapshot, you can safely truncate it yourself once the commit operation successfully completes. +The image @var{filename} is emptied after the operation has succeeded. If you do +not need @var{filename} afterwards and intend to drop it, you may skip emptying +@var{filename} by specifying the @code{-d} flag. + @item compare [-f @var{fmt}] [-F @var{fmt}] [-T @var{src_cache}] [-p] [-s] [-q] @var{filename1} @var{filename2} Check if two images have the same content. You can compare images with
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 30/53] qemu-img: Enable progress output for commit
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
Implement progress output for the commit command by querying the progress of the block job.
Message-id: [email protected]
--- qemu-img-cmds.hx | 4 ++-- qemu-img.c | 23 +++++++++++++++++++++-- qemu-img.texi | 2 +- 3 files changed, 24 insertions(+), 5 deletions(-)
qemu-img-cmds.hx +2 -2
@@ -22,9 +22,9 @@ STEXI ETEXI DEF("commit", img_commit, - "commit [-q] [-f fmt] [-t cache] [-d] filename") + "commit [-q] [-f fmt] [-t cache] [-d] [-p] filename") STEXI -@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-d] @var{filename} +@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-d] [-p] @var{filename} ETEXI DEF("compare", img_compare,
qemu-img.c +21 -2
@@ -746,9 +746,14 @@ static void run_block_job(BlockJob *job, Error **errp)do {aio_poll (aio_context, true); +qemu_progress_print ((float )job->offset / job->len *100.f ,0 ); }while (!job->ready);block_job_complete_sync (job, errp); + +/* A block job may finish instantaneously without publishing any progress, +* so just signal completion here */ +qemu_progress_print (100.f ,0 ); }static int img_commit (int argc,char **argv)
@@ -757,14 +762,14 @@ static int img_commit(int argc, char **argv)const char *filename, *fmt, *cache;BlockBackend *blk;BlockDriverState *bs, *base_bs; -bool quiet = false, drop = false; +bool progress = false, quiet = false, drop = false;Error *local_err =NULL ;CommonBlockJobCBInfo cbi; fmt =NULL ; cache =BDRV_DEFAULT_CACHE ;for (;;) { - c =getopt (argc, argv,"f:ht:dq" ); + c =getopt (argc, argv,"f:ht:dpq" );if (c ==-1 ) {break ; }
@@ -782,11 +787,20 @@ static int img_commit(int argc, char **argv)case 'd' : drop = true;break ; +case 'p' : + progress = true; +break ;case 'q' : quiet = true;break ; } } + +/* Progress is not shown in Quiet mode */ +if (quiet) { + progress = false; + } +if (optind != argc -1 ) {error_exit ("Expecting one image file name" ); }
@@ -805,6 +819,9 @@ static int img_commit(int argc, char **argv) } bs =blk_bs (blk); +qemu_progress_init (progress,1.f ); +qemu_progress_print (0.f ,100 ); +/* This is different from QMP, which by default uses the deepest file in the * backing chain (i.e., the very base); however, the traditional behavior of * qemu-img commit is using the immediate backing file. */
@@ -853,6 +870,8 @@ unref_backing: } done: +qemu_progress_end (); +blk_unref (blk);if (local_err) {
qemu-img.texi +1 -1
@@ -167,7 +167,7 @@ this case. @var{backing_file} will never be modified unless you use the The size can also be specified using the @var{size} option with @code{-o}, it doesn't need to be specified separately in this case. -@item commit [-f @var{fmt}] [-t @var{cache}] [-d] @var{filename} +@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-d] [-p] @var{filename} Commit the changes recorded in @var{filename} in its base image or backing file. If the backing file is smaller than the snapshot, then the backing file will be
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 31/53] qemu-img: Specify backing file for commit
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
Introduce a new parameter for qemu-img commit which may be used to explicitly specify the backing file into which an image should be committed if the backing chain has more than a single layer.
[Applied Eric Blake's qemu-img.texi documentation rewording --Stefan]
Message-id: [email protected]
--- qemu-img-cmds.hx | 4 ++-- qemu-img.c | 32 +++++++++++++++++++++++--------- qemu-img.texi | 12 +++++++++++- 3 files changed, 36 insertions(+), 12 deletions(-)
qemu-img-cmds.hx +2 -2
@@ -22,9 +22,9 @@ STEXI ETEXI DEF("commit", img_commit, - "commit [-q] [-f fmt] [-t cache] [-d] [-p] filename") + "commit [-q] [-f fmt] [-t cache] [-b base] [-d] [-p] filename") STEXI -@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-d] [-p] @var{filename} +@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-b @var{base}] [-d] [-p] @var{filename} ETEXI DEF("compare", img_compare,
qemu-img.c +23 -9
@@ -759,7 +759,7 @@ static void run_block_job(BlockJob *job, Error **errp)static int img_commit (int argc,char **argv) {int c, ret, flags; -const char *filename, *fmt, *cache; +const char *filename, *fmt, *cache, *base;BlockBackend *blk;BlockDriverState *bs, *base_bs;bool progress = false, quiet = false, drop = false;
@@ -768,8 +768,9 @@ static int img_commit(int argc, char **argv) fmt =NULL ; cache =BDRV_DEFAULT_CACHE ; + base =NULL ;for (;;) { - c =getopt (argc, argv,"f:ht:dpq" ); + c =getopt (argc, argv,"f:ht:b:dpq" );if (c ==-1 ) {break ; }
@@ -784,6 +785,11 @@ static int img_commit(int argc, char **argv)case 't' : cache = optarg;break ; +case 'b' : + base = optarg; +/* -b implies -d */ + drop = true; +break ;case 'd' : drop = true;break ;
@@ -822,13 +828,21 @@ static int img_commit(int argc, char **argv)qemu_progress_init (progress,1.f );qemu_progress_print (0.f ,100 ); -/* This is different from QMP, which by default uses the deepest file in the -* backing chain (i.e., the very base); however, the traditional behavior of -* qemu-img commit is using the immediate backing file. */ - base_bs = bs->backing_hd; -if (!base_bs) { -error_setg (&local_err,"Image does not have a backing file" ); - goto done; +if (base) { + base_bs =bdrv_find_backing_image (bs, base); +if (!base_bs) { +error_set (&local_err,QERR_BASE_NOT_FOUND , base); + goto done; + } + }else { +/* This is different from QMP, which by default uses the deepest file in +* the backing chain (i.e., the very base); however, the traditional +* behavior of qemu-img commit is using the immediate backing file. */ + base_bs = bs->backing_hd; +if (!base_bs) { +error_setg (&local_err,"Image does not have a backing file" ); + goto done; + } } cbi = (CommonBlockJobCBInfo ){
qemu-img.texi +11 -1
@@ -167,7 +167,7 @@ this case. @var{backing_file} will never be modified unless you use the The size can also be specified using the @var{size} option with @code{-o}, it doesn't need to be specified separately in this case. -@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-d] [-p] @var{filename} +@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-b @var{base}] [-d] [-p] @var{filename} Commit the changes recorded in @var{filename} in its base image or backing file. If the backing file is smaller than the snapshot, then the backing file will be
@@ -180,6 +180,16 @@ The image @var{filename} is emptied after the operation has succeeded. If you do not need @var{filename} afterwards and intend to drop it, you may skip emptying @var{filename} by specifying the @code{-d} flag. +If the backing chain of the given image file @var{filename} has more than one +layer, the backing file into which the changes will be committed may be +specified as @var{base} (which has to be part of @var{filename}'s backing +chain). If @var{base} is not specified, the immediate backing file of the top +image (which is @var{filename}) will be used. For reasons of consistency, +explicitly specifying @var{base} will always imply @code{-d} (since emptying an +image after committing to an indirect backing file would lead to different data +being read from the image due to content in the intermediate backing chain +overruling the commit target). + @item compare [-f @var{fmt}] [-F @var{fmt}] [-T @var{src_cache}] [-p] [-s] [-q] @var{filename1} @var{filename2} Check if two images have the same content. You can compare images with
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 32/53] iotests: Add _filter_qemu_img_map
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
As different image formats most probably map guest addresses to different host addresses, add a filter to filter the host addresses out; also, the image filename should be filtered.
Message-id: [email protected]
--- tests/qemu-iotests/common.filter | 7 +++++++ 1 file changed, 7 insertions(+)
tests/qemu-iotests/common.filter +7 -0
@@ -213,5 +213,12 @@ _filter_img_info() -e "s/archipelago:a/TEST_DIR\//g" } +# filter out offsets and file names from qemu-img map +_filter_qemu_img_map() +{ + sed -e 's/\([0-9a-fx]* *[0-9a-fx]* *\)[0-9a-fx]* */\1/g' \ + -e 's/Mapped to *//' | _filter_testdir | _filter_imgfmt +} + # make sure this script returns success /bin/true
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 33/53] iotests: Add test for backing-chain commits
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
Add a test for qemu-img commit on backing chains with more than two images. This test also checks whether the top image is emptied (unless this is prevented by specifying either -d or -b) and does therefore not work for qed and vmdk which requires it to be separate from 020.
Message-id: [email protected]
--- tests/qemu-iotests/097 | 122 +++++++++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/097.out | 119 +++++++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/group | 1 + 3 files changed, 242 insertions(+) create mode 100755 tests/qemu-iotests/097 create mode 100644 tests/qemu-iotests/097.out
tests/qemu-iotests/097 new file +122 -0
@@ -0,0 +1,122 @@ +#!/bin/bash +# +# Commit changes into backing chains and empty the top image if the +# backing image is not explicitly specified +# +# Copyright (C) 2014 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +# creator [email protected] + +seq="$(basename $0)" +echo "QA output created by $seq" + +here="$PWD" +tmp=/tmp/$$ +status=1 # failure is the default! + +_cleanup() +{ + _cleanup_test_img + _rm_test_img "$TEST_IMG.itmd" +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +# get standard environment, filters and checks +. ./common.rc +. ./common.filter +. ./common.pattern + +# Any format supporting backing files and bdrv_make_empty +_supported_fmt qcow qcow2 +_supported_proto file +_supported_os Linux + + +# Four passes: +# 0: Two-layer backing chain, commit to upper backing file (implicitly) +# (in this case, the top image will be emptied) +# 1: Two-layer backing chain, commit to upper backing file (explicitly) +# (in this case, the top image will implicitly stay unchanged) +# 2: Two-layer backing chain, commit to upper backing file (implicitly with -d) +# (in this case, the top image will explicitly stay unchanged) +# 3: Two-layer backing chain, commit to lower backing file +# (in this case, the top image will implicitly stay unchanged) +# +# 020 already tests committing, so this only tests whether image chains are +# working properly and that all images above the base are emptied; therefore, +# no complicated patterns are necessary +for i in 0 1 2 3; do + +echo +echo "=== Test pass $i ===" +echo + +TEST_IMG="$TEST_IMG.base" _make_test_img 64M +TEST_IMG="$TEST_IMG.itmd" _make_test_img -b "$TEST_IMG.base" 64M +_make_test_img -b "$TEST_IMG.itmd" 64M + +$QEMU_IO -c 'write -P 1 0 192k' "$TEST_IMG.base" | _filter_qemu_io +$QEMU_IO -c 'write -P 2 64k 128k' "$TEST_IMG.itmd" | _filter_qemu_io +$QEMU_IO -c 'write -P 3 128k 64k' "$TEST_IMG" | _filter_qemu_io + +if [ $i -lt 3 ]; then + if [ $i == 0 ]; then + # -b "$TEST_IMG.itmd" should be the default (that is, committing to the + # first backing file in the chain) + $QEMU_IMG commit "$TEST_IMG" + elif [ $i == 1 ]; then + # explicitly specify the commit target (this should imply -d) + $QEMU_IMG commit -b "$TEST_IMG.itmd" "$TEST_IMG" + else + # do not explicitly specify the commit target, but use -d to leave the + # top image unchanged + $QEMU_IMG commit -d "$TEST_IMG" + fi + + # Bottom should be unchanged + $QEMU_IO -c 'read -P 1 0 192k' "$TEST_IMG.base" | _filter_qemu_io + + # Intermediate should contain changes from top + $QEMU_IO -c 'read -P 1 0 64k' "$TEST_IMG.itmd" | _filter_qemu_io + $QEMU_IO -c 'read -P 2 64k 64k' "$TEST_IMG.itmd" | _filter_qemu_io + $QEMU_IO -c 'read -P 3 128k 64k' "$TEST_IMG.itmd" | _filter_qemu_io + + # And in pass 0, the top image should be empty, whereas in both other passes + # it should be unchanged (which is both checked by qemu-img map) +else + $QEMU_IMG commit -b "$TEST_IMG.base" "$TEST_IMG" + + # Bottom should contain all changes + $QEMU_IO -c 'read -P 1 0 64k' "$TEST_IMG.base" | _filter_qemu_io + $QEMU_IO -c 'read -P 2 64k 64k' "$TEST_IMG.base" | _filter_qemu_io + $QEMU_IO -c 'read -P 3 128k 64k' "$TEST_IMG.base" | _filter_qemu_io + + # Both top and intermediate should be unchanged +fi + +$QEMU_IMG map "$TEST_IMG.base" | _filter_qemu_img_map +$QEMU_IMG map "$TEST_IMG.itmd" | _filter_qemu_img_map +$QEMU_IMG map "$TEST_IMG" | _filter_qemu_img_map + +done + + +# success, all done +echo "*** done" +rm -f $seq.full +status=0
tests/qemu-iotests/097.out new file +119 -0
119 lines not on this page · raw message
tests/qemu-iotests/group +1 -0
@@ -100,6 +100,7 @@ 091 rw auto quick 092 rw auto quick 095 rw auto quick +097 rw auto backing 099 rw auto quick 100 rw auto quick 101 rw auto quick
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 34/53] iotests: Add test for qcow2's bdrv_make_empty
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
Add a test for qcow2's fast bdrv_make_empty implementation on images without internal snapshots.
Message-id: [email protected]
--- tests/qemu-iotests/098 | 82 ++++++++++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/098.out | 52 +++++++++++++++++++++++++++++ tests/qemu-iotests/group | 1 + 3 files changed, 135 insertions(+) create mode 100755 tests/qemu-iotests/098 create mode 100644 tests/qemu-iotests/098.out
tests/qemu-iotests/098 new file +82 -0
@@ -0,0 +1,82 @@ +#!/bin/bash +# +# Test qcow2's bdrv_make_empty for images without internal snapshots +# +# Copyright (C) 2014 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +# creator [email protected] + +seq="$(basename $0)" +echo "QA output created by $seq" + +here="$PWD" +tmp=/tmp/$$ +status=1 # failure is the default! + +_cleanup() +{ + _cleanup_test_img + rm -f "$TEST_DIR/blkdebug.conf" +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +# get standard environment, filters and checks +. ./common.rc +. ./common.filter +. ./common.pattern + +_supported_fmt qcow2 +_supported_proto file +_supported_os Linux + +IMGOPTS="compat=1.1" + +for event in l1_update empty_image_prepare reftable_update refblock_alloc; do + +echo +echo "=== $event ===" +echo + +TEST_IMG="$TEST_IMG.base" _make_test_img 64M +_make_test_img -b "$TEST_IMG.base" 64M + +# Some data that can be leaked when emptying the top image +$QEMU_IO -c 'write 0 64k' "$TEST_IMG" | _filter_qemu_io + +cat > "$TEST_DIR/blkdebug.conf" <<EOF +[inject-error] +event = "$event" +EOF + +$QEMU_IMG commit "blkdebug:$TEST_DIR/blkdebug.conf:$TEST_IMG" 2>&1 \ + | _filter_testdir | _filter_imgfmt + +# There may be errors, but they should be fixed by opening the image +$QEMU_IO -c close "$TEST_IMG" + +_check_test_img + +done + + +rm -f "$TEST_DIR/blkdebug.conf" + +# success, all done +echo "*** done" +rm -f $seq.full +status=0
tests/qemu-iotests/098.out new file +52 -0
52 lines not on this page · raw message
tests/qemu-iotests/group +1 -0
@@ -101,6 +101,7 @@ 092 rw auto quick 095 rw auto quick 097 rw auto backing +098 rw auto backing quick 099 rw auto quick 100 rw auto quick 101 rw auto quick
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 36/53] block: Add status callback to bdrv_amend_options()
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
Depending on the changed options and the image format, bdrv_amend_options() may take a significant amount of time. In these cases, a way to be informed about the operation's status is desirable.
Since the operation is rather complex and may fundamentally change the image, implementing it as AIO or a coroutine does not seem feasible. On the other hand, implementing it as a block job would be significantly more difficult than a simple callback and would not add benefits other than progress report to the amending operation, because it should not actually be run as a block job at all.
A callback may not be very pretty, but it's very easy to implement and perfectly fits its purpose here.
Message-id: [email protected]
--- block.c | 5 +++-- block/qcow2.c | 3 ++- include/block/block.h | 8 +++++++- include/block/block_int.h | 3 ++- qemu-img.c | 2 +- 5 files changed, 15 insertions(+), 6 deletions(-)
block.c +3 -2
@@ -5759,12 +5759,13 @@ void bdrv_add_before_write_notifier(BlockDriverState *bs,notifier_with_return_list_add (&bs->before_write_notifiers, notifier); } -int bdrv_amend_options (BlockDriverState *bs,QemuOpts *opts) +int bdrv_amend_options (BlockDriverState *bs,QemuOpts *opts, +BlockDriverAmendStatusCB *status_cb) {if (!bs->drv->bdrv_amend_options) {return -ENOTSUP ; } -return bs->drv->bdrv_amend_options (bs, opts); +return bs->drv->bdrv_amend_options (bs, opts, status_cb); }/* This function will be called by the bdrv_recurse_is_first_non_filter method
block/qcow2.c +2 -1
@@ -2613,7 +2613,8 @@ static int qcow2_downgrade(BlockDriverState *bs, int target_version)return 0 ; } -static int qcow2_amend_options (BlockDriverState *bs,QemuOpts *opts) +static int qcow2_amend_options (BlockDriverState *bs,QemuOpts *opts, +BlockDriverAmendStatusCB *status_cb) {BDRVQcowState *s = bs->opaque;int old_version = s->qcow_version, new_version = old_version;
include/block/block.h +7 -1
@@ -268,7 +268,13 @@ typedef enum {int bdrv_check (BlockDriverState *bs,BdrvCheckResult *res,BdrvCheckMode fix); -int bdrv_amend_options (BlockDriverState *bs_new,QemuOpts *opts); +/* The units of offset and total_work_size may be chosen arbitrarily by the +* block driver; total_work_size may change during the course of the amendment +* operation */ +typedef void BlockDriverAmendStatusCB (BlockDriverState *bs,int64_t offset, +int64_t total_work_size); +int bdrv_amend_options (BlockDriverState *bs_new,QemuOpts *opts, +BlockDriverAmendStatusCB *status_cb);/* external snapshots */ bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs,
include/block/block_int.h +2 -1
@@ -232,7 +232,8 @@ struct BlockDriver {int (*bdrv_check)(BlockDriverState * bs,BdrvCheckResult *result,BdrvCheckMode fix); -int (*bdrv_amend_options)(BlockDriverState *bs,QemuOpts *opts); +int (*bdrv_amend_options)(BlockDriverState *bs,QemuOpts *opts, +BlockDriverAmendStatusCB *status_cb);void (*bdrv_debug_event)(BlockDriverState *bs,BlkDebugEvent event);
qemu-img.c +1 -1
@@ -2968,7 +2968,7 @@ static int img_amend(int argc, char **argv) goto out; } - ret =bdrv_amend_options (bs, opts); + ret =bdrv_amend_options (bs, opts,NULL );if (ret <0 ) {error_report ("Error while amending options: %s" ,strerror (-ret)); goto out;
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 35/53] block: qemu-iotest 107 supports NFS
In reply to Stefan HajnocziFrom: Peter Lieven <[email protected]>
As discussed during review a follow up for Max's fix.
Message-id: [email protected]
--- tests/qemu-iotests/107 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tests/qemu-iotests/107 +1 -1
@@ -39,7 +39,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter _supported_fmt qcow2 -_supported_proto file +_supported_proto file nfs _supported_os Linux
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 37/53] qemu-img: Add progress output for amend
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
Now that bdrv_amend_options() supports a status callback, use it to display a progress report.
Message-id: [email protected]
--- qemu-img-cmds.hx | 4 ++-- qemu-img.c | 25 ++++++++++++++++++++++--- qemu-img.texi | 2 +- 3 files changed, 25 insertions(+), 6 deletions(-)
qemu-img-cmds.hx +2 -2
@@ -70,8 +70,8 @@ STEXI ETEXI DEF("amend", img_amend, - "amend [-q] [-f fmt] [-t cache] -o options filename") + "amend [-p] [-q] [-f fmt] [-t cache] -o options filename") STEXI -@item amend [-q] [-f @var{fmt}] [-t @var{cache}] -o @var{options} @var{filename} +@item amend [-p] [-q] [-f @var{fmt}] [-t @var{cache}] -o @var{options} @var{filename} @end table ETEXI
qemu-img.c +22 -3
@@ -2871,6 +2871,12 @@ out:return 0 ; } +static void amend_status_cb (BlockDriverState *bs, +int64_t offset,int64_t total_work_size) +{ +qemu_progress_print (100.f * offset / total_work_size,0 ); +} +static int img_amend (int argc,char **argv) {int c, ret =0 ;
@@ -2879,13 +2885,13 @@ static int img_amend(int argc, char **argv)QemuOpts *opts =NULL ;const char *fmt =NULL , *filename, *cache;int flags; -bool quiet = false; +bool quiet = false, progress = false;BlockBackend *blk =NULL ;BlockDriverState *bs =NULL ; cache =BDRV_DEFAULT_CACHE ;for (;;) { - c =getopt (argc, argv,"ho:f:t:q" ); + c =getopt (argc, argv,"ho:f:t:pq" );if (c ==-1 ) {break ; }
@@ -2915,6 +2921,9 @@ static int img_amend(int argc, char **argv)case 't' : cache = optarg;break ; +case 'p' : + progress = true; +break ;case 'q' : quiet = true;break ;
@@ -2925,6 +2934,11 @@ static int img_amend(int argc, char **argv)error_exit ("Must specify options (-o)" ); } +if (quiet) { + progress = false; + } +qemu_progress_init (progress,1.0 ); + filename = (optind == argc -1 ) ? argv[argc -1 ] :NULL ;if (fmt &&has_help_option (options)) {/* If a format is explicitly specified (and possibly no filename is
@@ -2968,13 +2982,18 @@ static int img_amend(int argc, char **argv) goto out; } - ret =bdrv_amend_options (bs, opts,NULL ); +/* In case the driver does not call amend_status_cb() */ +qemu_progress_print (0.f ,0 ); + ret =bdrv_amend_options (bs, opts, &amend_status_cb); +qemu_progress_print (100.f ,0 );if (ret <0 ) {error_report ("Error while amending options: %s" ,strerror (-ret)); goto out; } out: +qemu_progress_end (); +blk_unref (blk);qemu_opts_del (opts);qemu_opts_free (create_opts);
qemu-img.texi +1 -1
@@ -412,7 +412,7 @@ After using this command to grow a disk image, you must use file system and partitioning tools inside the VM to actually begin using the new space on the device. -@item amend [-f @var{fmt}] [-t @var{cache}] -o @var{options} @var{filename} +@item amend [-p] [-f @var{fmt}] [-t @var{cache}] -o @var{options} @var{filename} Amends the image format specific @var{options} for the image file @var{filename}. Not all file formats support this operation.
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 38/53] qemu-img: Fix insignificant memleak
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
As soon as options is set in img_amend(), it needs to be freed before the function returns. This leak is rather insignificant, as qemu-img will exit subsequently anyway, but there's no point in not fixing it.
Message-id: [email protected]
--- qemu-img.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
qemu-img.c +3 -1
@@ -2948,7 +2948,9 @@ static int img_amend(int argc, char **argv) }if (optind != argc -1 ) { -error_exit ("Expecting one image file name" ); +error_report ("Expecting one image file name" ); + ret =-1 ; + goto out; } flags =BDRV_O_FLAGS |BDRV_O_RDWR ;
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 39/53] block/qcow2: Implement status CB for amend
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
The only really time-consuming operation potentially performed by qcow2_amend_options() is zero cluster expansion when downgrading qcow2 images from compat=1.1 to compat=0.10, so report status of that operation and that operation only through the status CB.
For this, approximate the progress as the number of L1 entries visited during the operation.
Message-id: [email protected]
--- block/qcow2-cluster.c | 37 +++++++++++++++++++++++++++++++++---- block/qcow2.c | 7 ++++--- block/qcow2.h | 3 ++- 3 files changed, 39 insertions(+), 8 deletions(-)
block/qcow2-cluster.c +33 -4
@@ -1618,10 +1618,17 @@ fail:* zero expansion (i.e., has been filled with zeroes and is referenced from an * L2 table). nb_clusters contains the total cluster count of the image file, * i.e., the number of bits in expanded_clusters. +* +* l1_entries and *visited_l1_entries are used to keep track of progress for +* status_cb(). l1_entries contains the total number of L1 entries and +* *visited_l1_entries counts all visited L1 entries. */ static int expand_zero_clusters_in_l1 (BlockDriverState *bs,uint64_t *l1_table,int l1_size,uint8_t **expanded_clusters, -uint64_t *nb_clusters) +uint64_t *nb_clusters, +int64_t *visited_l1_entries, +int64_t l1_entries, +BlockDriverAmendStatusCB *status_cb) {BDRVQcowState *s = bs->opaque;bool is_active_l1 = (l1_table == s->l1_table);
@@ -1644,6 +1651,10 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,if (!l2_offset) {/* unallocated */ + (*visited_l1_entries)++; +if (status_cb) { +status_cb (bs, *visited_l1_entries, l1_entries); + }continue ; }
@@ -1776,6 +1787,11 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table, } } } + + (*visited_l1_entries)++; +if (status_cb) { +status_cb (bs, *visited_l1_entries, l1_entries); + } } ret =0 ;
@@ -1802,15 +1818,24 @@ fail:* allocation for pre-allocated ones). This is important for downgrading to a * qcow2 version which doesn't yet support metadata zero clusters. */ -int qcow2_expand_zero_clusters (BlockDriverState *bs) +int qcow2_expand_zero_clusters (BlockDriverState *bs, +BlockDriverAmendStatusCB *status_cb) {BDRVQcowState *s = bs->opaque;uint64_t *l1_table =NULL ;uint64_t nb_clusters; +int64_t l1_entries =0 , visited_l1_entries =0 ;uint8_t *expanded_clusters;int ret;int i, j; +if (status_cb) { + l1_entries = s->l1_size; +for (i =0 ; i < s->nb_snapshots; i++) { + l1_entries += s->snapshots[i].l1_size; + } + } + nb_clusters =size_to_clusters (s, bs->file->total_sectors *BDRV_SECTOR_SIZE ); expanded_clusters =g_try_malloc0 ((nb_clusters +7 ) /8 );
@@ -1820,7 +1845,9 @@ int qcow2_expand_zero_clusters(BlockDriverState *bs) } ret =expand_zero_clusters_in_l1 (bs, s->l1_table, s->l1_size, - &expanded_clusters, &nb_clusters); + &expanded_clusters, &nb_clusters, + &visited_l1_entries, l1_entries, + status_cb);if (ret <0 ) { goto fail; }
@@ -1854,7 +1881,9 @@ int qcow2_expand_zero_clusters(BlockDriverState *bs) } ret =expand_zero_clusters_in_l1 (bs, l1_table, s->snapshots[i].l1_size, - &expanded_clusters, &nb_clusters); + &expanded_clusters, &nb_clusters, + &visited_l1_entries, l1_entries, + status_cb);if (ret <0 ) { goto fail; }
block/qcow2.c +4 -3
@@ -2550,7 +2550,8 @@ static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,* Downgrades an image's version. To achieve this, any incompatible features * have to be removed. */ -static int qcow2_downgrade (BlockDriverState *bs,int target_version) +static int qcow2_downgrade (BlockDriverState *bs,int target_version, +BlockDriverAmendStatusCB *status_cb) {BDRVQcowState *s = bs->opaque;int current_version = s->qcow_version;
@@ -2599,7 +2600,7 @@ static int qcow2_downgrade(BlockDriverState *bs, int target_version)/* clearing autoclear features is trivial */ s->autoclear_features =0 ; - ret =qcow2_expand_zero_clusters (bs); + ret =qcow2_expand_zero_clusters (bs, status_cb);if (ret <0 ) {return ret; }
@@ -2692,7 +2693,7 @@ static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,return ret; } }else { - ret =qcow2_downgrade (bs, new_version); + ret =qcow2_downgrade (bs, new_version, status_cb);if (ret <0 ) {return ret; }
block/qcow2.h +2 -1
@@ -537,7 +537,8 @@ int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors, enum qcow2_discard_type type, bool full_discard);int qcow2_zero_clusters (BlockDriverState *bs,uint64_t offset,int nb_sectors); -int qcow2_expand_zero_clusters (BlockDriverState *bs); +int qcow2_expand_zero_clusters (BlockDriverState *bs, +BlockDriverAmendStatusCB *status_cb);/* qcow2-snapshot.c functions */ int qcow2_snapshot_create (BlockDriverState *bs,QEMUSnapshotInfo *sn_info);
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 40/53] block/qcow2: Make get_refcount() global
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
Reading the refcount of a cluster is an operation which can be useful in all of the qcow2 code, so make that function globally available.
While touching this function, amend the comment describing the "addend"
parameter: It is (no longer, if it ever was) necessary to have it set to -1 or 1; any value is fine.
Message-id: [email protected]
--- block/qcow2-refcount.c | 26 +++++++++++++------------- block/qcow2.h | 2 ++ 2 files changed, 15 insertions(+), 13 deletions(-)
block/qcow2-refcount.c +13 -13
@@ -91,7 +91,7 @@ static int load_refcount_block(BlockDriverState *bs,* return value is the refcount of the cluster, negative values are -errno * and indicate an error. */ -static int get_refcount (BlockDriverState *bs,int64_t cluster_index) +int qcow2_get_refcount (BlockDriverState *bs,int64_t cluster_index) {BDRVQcowState *s = bs->opaque;uint64_t refcount_table_index, block_index;
@@ -629,8 +629,7 @@ fail: }/* -* Increases or decreases the refcount of a given cluster by one. -* addend must be 1 or -1. +* Increases or decreases the refcount of a given cluster. * * If the return value is non-negative, it is the new refcount of the cluster. * If it is negative, it is -errno and indicates an error.
@@ -649,7 +648,7 @@ int qcow2_update_cluster_refcount(BlockDriverState *bs,return ret; } -return get_refcount (bs, cluster_index); +return qcow2_get_refcount (bs, cluster_index); }
@@ -670,7 +669,7 @@ static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size) retry:for (i =0 ; i < nb_clusters; i++) {uint64_t next_cluster_index = s->free_cluster_index++; - refcount =get_refcount (bs, next_cluster_index); + refcount =qcow2_get_refcount (bs, next_cluster_index);if (refcount <0 ) {return refcount;
@@ -734,7 +733,7 @@ int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,/* Check how many clusters there are free */ cluster_index = offset >> s->cluster_bits;for (i =0 ; i < nb_clusters; i++) { - refcount =get_refcount (bs, cluster_index++); + refcount =qcow2_get_refcount (bs, cluster_index++);if (refcount <0 ) {return refcount;
@@ -981,7 +980,7 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs, cluster_index, addend, QCOW2_DISCARD_SNAPSHOT); }else { - refcount =get_refcount (bs, cluster_index); + refcount =qcow2_get_refcount (bs, cluster_index); }if (refcount <0 ) {
@@ -1021,7 +1020,7 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs, refcount =qcow2_update_cluster_refcount (bs, l2_offset >> s->cluster_bits, addend,QCOW2_DISCARD_SNAPSHOT ); }else { - refcount =get_refcount (bs, l2_offset >> s->cluster_bits); + refcount =qcow2_get_refcount (bs, l2_offset >> s->cluster_bits); }if (refcount <0 ) { ret = refcount;
@@ -1332,8 +1331,8 @@ fail:* Checks the OFLAG_COPIED flag for all L1 and L2 entries. * * This function does not print an error message nor does it increment -* check_errors if get_refcount fails (this is because such an error will have -* been already detected and sufficiently signaled by the calling function +* check_errors if qcow2_get_refcount fails (this is because such an error will +* have been already detected and sufficiently signaled by the calling function * (qcow2_check_refcounts) by the time this function is called). */ static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res,
@@ -1354,7 +1353,7 @@ static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res,continue ; } - refcount =get_refcount (bs, l2_offset >> s->cluster_bits); + refcount =qcow2_get_refcount (bs, l2_offset >> s->cluster_bits);if (refcount <0 ) {/* don't print message nor increment check_errors */ continue ;
@@ -1396,7 +1395,8 @@ static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res,if ((cluster_type ==QCOW2_CLUSTER_NORMAL ) || ((cluster_type ==QCOW2_CLUSTER_ZERO ) && (data_offset !=0 ))) { - refcount =get_refcount (bs, data_offset >> s->cluster_bits); + refcount =qcow2_get_refcount (bs, + data_offset >> s->cluster_bits);if (refcount <0 ) {/* don't print message nor increment check_errors */ continue ;
@@ -1632,7 +1632,7 @@ static void compare_refcounts(BlockDriverState *bs, BdrvCheckResult *res,int refcount1, refcount2, ret;for (i =0 , *highest_cluster =0 ; i < nb_clusters; i++) { - refcount1 =get_refcount (bs, i); + refcount1 =qcow2_get_refcount (bs, i);if (refcount1 <0 ) {fprintf (stderr,"Can't get refcount for cluster %" PRId64": %s\n" , i,strerror (-refcount1));
block/qcow2.h +2 -0
@@ -487,6 +487,8 @@ void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,int qcow2_refcount_init (BlockDriverState *bs);void qcow2_refcount_close (BlockDriverState *bs); +int qcow2_get_refcount (BlockDriverState *bs,int64_t cluster_index); +int qcow2_update_cluster_refcount (BlockDriverState *bs,int64_t cluster_index,int addend,enum qcow2_discard_type type);
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 41/53] block/qcow2: Simplify shared L2 handling in amend
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
Currently, we have a bitmap for keeping track of which clusters have been created during the zero cluster expansion process. This was necessary because we need to properly increase the refcount for shared L2 tables.
However, now we can simply take the L2 refcount and use it for the cluster allocated for expansion. This will be the correct refcount and therefore we don't have to remember that cluster having been allocated any more.
Message-id: [email protected]
--- block/qcow2-cluster.c | 94 +++++++++++++++------------------------------------ 1 file changed, 28 insertions(+), 66 deletions(-)
block/qcow2-cluster.c +28 -66
@@ -1613,20 +1613,12 @@ fail:* Expands all zero clusters in a specific L1 table (or deallocates them, for * non-backed non-pre-allocated zero clusters). * -* expanded_clusters is a bitmap where every bit corresponds to one cluster in -* the image file; a bit gets set if the corresponding cluster has been used for -* zero expansion (i.e., has been filled with zeroes and is referenced from an -* L2 table). nb_clusters contains the total cluster count of the image file, -* i.e., the number of bits in expanded_clusters. -* * l1_entries and *visited_l1_entries are used to keep track of progress for * status_cb(). l1_entries contains the total number of L1 entries and * *visited_l1_entries counts all visited L1 entries. */ static int expand_zero_clusters_in_l1 (BlockDriverState *bs,uint64_t *l1_table, -int l1_size,uint8_t **expanded_clusters, -uint64_t *nb_clusters, -int64_t *visited_l1_entries, +int l1_size,int64_t *visited_l1_entries,int64_t l1_entries,BlockDriverAmendStatusCB *status_cb) {
@@ -1648,6 +1640,7 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,for (i =0 ; i < l1_size; i++) {uint64_t l2_offset = l1_table[i] &L1E_OFFSET_MASK ;bool l2_dirty = false; +int l2_refcount;if (!l2_offset) {/* unallocated */
@@ -1671,33 +1664,19 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table, goto fail; } + l2_refcount =qcow2_get_refcount (bs, l2_offset >> s->cluster_bits); +if (l2_refcount <0 ) { + ret = l2_refcount; + goto fail; + } +for (j =0 ; j < s->l2_size; j++) {uint64_t l2_entry =be64_to_cpu (l2_table[j]); -int64_t offset = l2_entry &L2E_OFFSET_MASK , cluster_index; +int64_t offset = l2_entry &L2E_OFFSET_MASK ;int cluster_type =qcow2_get_cluster_type (l2_entry);bool preallocated = offset !=0 ; -if (cluster_type ==QCOW2_CLUSTER_NORMAL ) { - cluster_index = offset >> s->cluster_bits; -assert ((cluster_index >=0 ) && (cluster_index < *nb_clusters)); -if ((*expanded_clusters)[cluster_index /8 ] & - (1 << (cluster_index %8 ))) { -/* Probably a shared L2 table; this cluster was a zero -* cluster which has been expanded, its refcount -* therefore most likely requires an update. */ - ret =qcow2_update_cluster_refcount (bs, cluster_index,1 , -QCOW2_DISCARD_NEVER ); -if (ret <0 ) { - goto fail; - } -/* Since we just increased the refcount, the COPIED flag may -* no longer be set. */ - l2_table[j] =cpu_to_be64 (l2_entry & ~QCOW_OFLAG_COPIED ); - l2_dirty = true; - } -continue ; - } -else if (qcow2_get_cluster_type (l2_entry) !=QCOW2_CLUSTER_ZERO ) { +if (cluster_type !=QCOW2_CLUSTER_ZERO ) {continue ; }
@@ -1715,6 +1694,19 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table, ret = offset; goto fail; } + +if (l2_refcount >1 ) { +/* For shared L2 tables, set the refcount accordingly (it is +* already 1 and needs to be l2_refcount) */ + ret =qcow2_update_cluster_refcount (bs, + offset >> s->cluster_bits, l2_refcount -1 , +QCOW2_DISCARD_OTHER ); +if (ret <0 ) { +qcow2_free_clusters (bs, offset, s->cluster_size, +QCOW2_DISCARD_OTHER ); + goto fail; + } + } } ret =qcow2_pre_write_overlap_check (bs,0 , offset, s->cluster_size);
@@ -1736,29 +1728,12 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table, goto fail; } - l2_table[j] =cpu_to_be64 (offset |QCOW_OFLAG_COPIED ); - l2_dirty = true; - - cluster_index = offset >> s->cluster_bits; - -if (cluster_index >= *nb_clusters) { -uint64_t old_bitmap_size = (*nb_clusters +7 ) /8 ; -uint64_t new_bitmap_size; -/* The offset may lie beyond the old end of the underlying image -* file for growable files only */ -assert (bs->file->growable); - *nb_clusters =size_to_clusters (s, bs->file->total_sectors * -BDRV_SECTOR_SIZE ); - new_bitmap_size = (*nb_clusters +7 ) /8 ; - *expanded_clusters =g_realloc (*expanded_clusters, - new_bitmap_size); -/* clear the newly allocated space */ -memset (&(*expanded_clusters)[old_bitmap_size],0 , - new_bitmap_size - old_bitmap_size); +if (l2_refcount ==1 ) { + l2_table[j] =cpu_to_be64 (offset |QCOW_OFLAG_COPIED ); + }else { + l2_table[j] =cpu_to_be64 (offset); } - -assert ((cluster_index >=0 ) && (cluster_index < *nb_clusters)); - (*expanded_clusters)[cluster_index /8 ] |=1 << (cluster_index %8 ); + l2_dirty = true; }if (is_active_l1) {
@@ -1823,9 +1798,7 @@ int qcow2_expand_zero_clusters(BlockDriverState *bs, {BDRVQcowState *s = bs->opaque;uint64_t *l1_table =NULL ; -uint64_t nb_clusters;int64_t l1_entries =0 , visited_l1_entries =0 ; -uint8_t *expanded_clusters;int ret;int i, j;
@@ -1836,16 +1809,7 @@ int qcow2_expand_zero_clusters(BlockDriverState *bs, } } - nb_clusters =size_to_clusters (s, bs->file->total_sectors * -BDRV_SECTOR_SIZE ); - expanded_clusters =g_try_malloc0 ((nb_clusters +7 ) /8 ); -if (expanded_clusters ==NULL ) { - ret = -ENOMEM ; - goto fail; - } - ret =expand_zero_clusters_in_l1 (bs, s->l1_table, s->l1_size, - &expanded_clusters, &nb_clusters, &visited_l1_entries, l1_entries, status_cb);if (ret <0 ) {
@@ -1881,7 +1845,6 @@ int qcow2_expand_zero_clusters(BlockDriverState *bs, } ret =expand_zero_clusters_in_l1 (bs, l1_table, s->snapshots[i].l1_size, - &expanded_clusters, &nb_clusters, &visited_l1_entries, l1_entries, status_cb);if (ret <0 ) {
@@ -1892,7 +1855,6 @@ int qcow2_expand_zero_clusters(BlockDriverState *bs, ret =0 ; fail: -g_free (expanded_clusters);g_free (l1_table);return ret; }
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 42/53] iotests: Expand test 061
In reply to Stefan HajnocziFrom: Max Reitz <[email protected]>
Add some tests for progress output to 061.
Message-id: [email protected]
--- tests/qemu-iotests/061 | 25 +++++++++++++++++++++++++ tests/qemu-iotests/061.out | 30 ++++++++++++++++++++++++++++++ tests/qemu-iotests/group | 2 +- 3 files changed, 56 insertions(+), 1 deletion(-)
tests/qemu-iotests/061 +25 -0
@@ -209,6 +209,31 @@ $QEMU_IMG amend -o "compat=0.10" "$TEST_IMG" _check_test_img $QEMU_IO -c "read -P 0 0 64M" "$TEST_IMG" | _filter_qemu_io +echo +echo "=== Testing progress report without snapshot ===" +echo +IMGOPTS="compat=1.1" TEST_IMG="$TEST_IMG.base" _make_test_img 4G +IMGOPTS="compat=1.1" _make_test_img -b "$TEST_IMG.base" 4G +$QEMU_IO -c "write -z 0 64k" \ + -c "write -z 1G 64k" \ + -c "write -z 2G 64k" \ + -c "write -z 3G 64k" "$TEST_IMG" | _filter_qemu_io +$QEMU_IMG amend -p -o "compat=0.10" "$TEST_IMG" +_check_test_img + +echo +echo "=== Testing progress report with snapshot ===" +echo +IMGOPTS="compat=1.1" TEST_IMG="$TEST_IMG.base" _make_test_img 4G +IMGOPTS="compat=1.1" _make_test_img -b "$TEST_IMG.base" 4G +$QEMU_IO -c "write -z 0 64k" \ + -c "write -z 1G 64k" \ + -c "write -z 2G 64k" \ + -c "write -z 3G 64k" "$TEST_IMG" | _filter_qemu_io +$QEMU_IMG snapshot -c foo "$TEST_IMG" +$QEMU_IMG amend -p -o "compat=0.10" "$TEST_IMG" +_check_test_img + # success, all done echo "*** done" rm -f $seq.full
tests/qemu-iotests/061.out +30 -0
@@ -390,4 +390,34 @@ wrote 67108864/67108864 bytes at offset 0 No errors were found on the image. read 67108864/67108864 bytes at offset 0 64 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +=== Testing progress report without snapshot === + +Formatting 'TEST_DIR/t.IMGFMT.base', fmt=IMGFMT size=4294967296 +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=4294967296 backing_file='TEST_DIR/t.IMGFMT.base' +wrote 65536/65536 bytes at offset 0 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +wrote 65536/65536 bytes at offset 1073741824 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +wrote 65536/65536 bytes at offset 2147483648 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +wrote 65536/65536 bytes at offset 3221225472 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + (0.00/100%) (12.50/100%) (25.00/100%) (37.50/100%) (50.00/100%) (62.50/100%) (75.00/100%) (87.50/100%) (100.00/100%) (100.00/100%) +No errors were found on the image. + +=== Testing progress report with snapshot === + +Formatting 'TEST_DIR/t.IMGFMT.base', fmt=IMGFMT size=4294967296 +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=4294967296 backing_file='TEST_DIR/t.IMGFMT.base' +wrote 65536/65536 bytes at offset 0 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +wrote 65536/65536 bytes at offset 1073741824 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +wrote 65536/65536 bytes at offset 2147483648 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +wrote 65536/65536 bytes at offset 3221225472 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + (0.00/100%) (6.25/100%) (12.50/100%) (18.75/100%) (25.00/100%) (31.25/100%) (37.50/100%) (43.75/100%) (50.00/100%) (56.25/100%) (62.50/100%) (68.75/100%) (75.00/100%) (81.25/100%) (87.50/100%) (93.75/100%) (100.00/100%) (100.00/100%) +No errors were found on the image. *** done
tests/qemu-iotests/group +1 -1
@@ -67,7 +67,7 @@ 058 rw auto quick 059 rw auto quick 060 rw auto quick -061 rw auto quick +061 rw auto 062 rw auto quick 063 rw auto quick 064 rw auto quick
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 43/53] block: acquire AioContext in generic blockjob QMP commands
In reply to Stefan Hajnocziblock-job-set-speed, block-job-cancel, block-job-pause, block-job-resume, and block-job-complete must acquire the BlockDriverState AioContext so that it is safe to access bs.
At the moment bs->job is always NULL when dataplane is active because op blockers prevent blockjobs from starting. Once the rest of the blockjob API has been made aware of AioContext we can drop the op blocker.
Message-id: [email protected]
--- blockdev.c | 52 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 13 deletions(-)
blockdev.c +39 -13
@@ -2375,20 +2375,35 @@ void qmp_drive_mirror(const char *device, const char *target, } } -static BlockJob *find_block_job (const char *device) +/* Get the block job for a given device name and acquire its AioContext */ +static BlockJob *find_block_job (const char *device,AioContext **aio_context) {BlockDriverState *bs; bs =bdrv_find (device); -if (!bs || !bs->job) { -return NULL ; +if (!bs) { + goto notfound; + } + + *aio_context =bdrv_get_aio_context (bs); +aio_context_acquire (*aio_context); + +if (!bs->job) { +aio_context_release (*aio_context); + goto notfound; } +return bs->job; + +notfound: + *aio_context =NULL ; +return NULL ; }void qmp_block_job_set_speed (const char *device,int64_t speed,Error **errp) { -BlockJob *job =find_block_job (device); +AioContext *aio_context; +BlockJob *job =find_block_job (device, &aio_context);if (!job) {error_set (errp,QERR_BLOCK_JOB_NOT_ACTIVE , device);
@@ -2396,34 +2411,40 @@ void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) }block_job_set_speed (job, speed, errp); +aio_context_release (aio_context); }void qmp_block_job_cancel (const char *device,bool has_force,bool force,Error **errp) { -BlockJob *job =find_block_job (device); - -if (!has_force) { - force = false; - } +AioContext *aio_context; +BlockJob *job =find_block_job (device, &aio_context);if (!job) {error_set (errp,QERR_BLOCK_JOB_NOT_ACTIVE , device);return ; } + +if (!has_force) { + force = false; + } +if (job->paused && !force) {error_setg (errp,"The block job for device '%s' is currently paused" , device); -return ; + goto out; }trace_qmp_block_job_cancel (job);block_job_cancel (job); +out: +aio_context_release (aio_context); }void qmp_block_job_pause (const char *device,Error **errp) { -BlockJob *job =find_block_job (device); +AioContext *aio_context; +BlockJob *job =find_block_job (device, &aio_context);if (!job) {error_set (errp,QERR_BLOCK_JOB_NOT_ACTIVE , device);
@@ -2432,11 +2453,13 @@ void qmp_block_job_pause(const char *device, Error **errp)trace_qmp_block_job_pause (job);block_job_pause (job); +aio_context_release (aio_context); }void qmp_block_job_resume (const char *device,Error **errp) { -BlockJob *job =find_block_job (device); +AioContext *aio_context; +BlockJob *job =find_block_job (device, &aio_context);if (!job) {error_set (errp,QERR_BLOCK_JOB_NOT_ACTIVE , device);
@@ -2445,11 +2468,13 @@ void qmp_block_job_resume(const char *device, Error **errp)trace_qmp_block_job_resume (job);block_job_resume (job); +aio_context_release (aio_context); }void qmp_block_job_complete (const char *device,Error **errp) { -BlockJob *job =find_block_job (device); +AioContext *aio_context; +BlockJob *job =find_block_job (device, &aio_context);if (!job) {error_set (errp,QERR_BLOCK_JOB_NOT_ACTIVE , device);
@@ -2458,6 +2483,7 @@ void qmp_block_job_complete(const char *device, Error **errp)trace_qmp_block_job_complete (job);block_job_complete (job, errp); +aio_context_release (aio_context); } void qmp_change_backing_file(const char *device,
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 44/53] blockdev: acquire AioContext in do_qmp_query_block_jobs_one()
In reply to Stefan HajnocziMake sure that query-block-jobs acquires the BlockDriverState AioContext so that the blockjob isn't running in another thread while we access its state.
Message-id: [email protected]
--- blockdev.c | 6 ++++++ 1 file changed, 6 insertions(+)
blockdev.c +6 -0
@@ -2628,12 +2628,18 @@ BlockJobInfoList *qmp_query_block_jobs(Error **errp)BlockDriverState *bs;for (bs =bdrv_next (NULL ); bs; bs =bdrv_next (bs)) { +AioContext *aio_context =bdrv_get_aio_context (bs); + +aio_context_acquire (aio_context); +if (bs->job) {BlockJobInfoList *elem =g_new0 (BlockJobInfoList,1 ); elem->value =block_job_query (bs->job); *p_next = elem; p_next = &elem->next; } + +aio_context_release (aio_context); }return head;
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 45/53] blockdev: acquire AioContext in blockdev_mark_auto_del()
In reply to Stefan HajnocziWhen an emulated storage controller is unrealized it will call blockdev_mark_auto_del(). This will cancel any running block job (and that eventually releases its reference to the BDS so it can be freed).
Since the block job may be executing in another AioContext we must acquire/release to ensure thread safety.
Message-id: [email protected]
--- blockdev.c | 7 +++++++ 1 file changed, 7 insertions(+)
blockdev.c +7 -0
@@ -115,14 +115,21 @@ void blockdev_mark_auto_del(BlockBackend *blk) {DriveInfo *dinfo =blk_legacy_dinfo (blk);BlockDriverState *bs =blk_bs (blk); +AioContext *aio_context;if (!dinfo) {return ; } + aio_context =bdrv_get_aio_context (bs); +aio_context_acquire (aio_context); +if (bs->job) {block_job_cancel (bs->job); } + +aio_context_release (aio_context); + dinfo->auto_del =1 ; }
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 46/53] blockdev: add note that block_job_cb() must be thread-safe
In reply to Stefan HajnocziThis function is correct but we should document the constraint that everything must be thread-safe.
Emitting QMP events and scheduling BHs are both thread-safe so nothing needs to be done here.
Message-id: [email protected]
--- blockdev.c | 5 +++++ 1 file changed, 5 insertions(+)
blockdev.c +5 -0
@@ -1929,6 +1929,11 @@ out:static void block_job_cb (void *opaque,int ret) { +/* Note that this function may be executed from another AioContext besides +* the QEMU main loop. If you need to access anything that assumes the +* QEMU global mutex, use a BH or introduce a mutex. +*/ +BlockDriverState *bs = opaque;const char *msg =NULL ;
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 47/53] blockjob: add block_job_defer_to_main_loop()
In reply to Stefan HajnocziBlock jobs will run in the BlockDriverState's AioContext, which may not always be the QEMU main loop.
There are some block layer APIs that are either not thread-safe or risk lock ordering problems. This includes bdrv_unref(), bdrv_close(), and anything that calls bdrv_drain_all().
The block_job_defer_to_main_loop() API allows a block job to schedule a function to run in the main loop with the BlockDriverState AioContext held.
This function will be used to perform cleanup and backing chain manipulations in block jobs.
Message-id: [email protected]
--- blockjob.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ include/block/blockjob.h | 19 +++++++++++++++++++ 2 files changed, 64 insertions(+)
blockjob.c +45 -0
@@ -342,3 +342,48 @@ BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs, }return action; } + +typedef struct { +BlockJob *job; +QEMUBH *bh; +AioContext *aio_context; +BlockJobDeferToMainLoopFn *fn; +void *opaque; +}BlockJobDeferToMainLoopData ; + +static void block_job_defer_to_main_loop_bh (void *opaque) +{ +BlockJobDeferToMainLoopData *data = opaque; +AioContext *aio_context; + +qemu_bh_delete (data->bh); + +/* Prevent race with block_job_defer_to_main_loop() */ +aio_context_acquire (data->aio_context); + +/* Fetch BDS AioContext again, in case it has changed */ + aio_context =bdrv_get_aio_context (data->job->bs); +aio_context_acquire (aio_context); + + data->fn (data->job, data->opaque); + +aio_context_release (aio_context); + +aio_context_release (data->aio_context); + +g_free (data); +} + +void block_job_defer_to_main_loop (BlockJob *job, +BlockJobDeferToMainLoopFn *fn, +void *opaque) +{ +BlockJobDeferToMainLoopData *data =g_malloc (sizeof (*data)); + data->job = job; + data->bh =qemu_bh_new (block_job_defer_to_main_loop_bh, data); + data->aio_context =bdrv_get_aio_context (job->bs); + data->fn = fn; + data->opaque = opaque; + +qemu_bh_schedule (data->bh); +}
include/block/blockjob.h +19 -0
@@ -315,4 +315,23 @@ void block_job_iostatus_reset(BlockJob *job);BlockErrorAction block_job_error_action (BlockJob *job,BlockDriverState *bs,BlockdevOnError on_err,int is_read,int error); + +typedef void BlockJobDeferToMainLoopFn (BlockJob *job,void *opaque); + +/** +* block_job_defer_to_main_loop: +* @job: The job +* @fn: The function to run in the main loop +* @opaque: The opaque value that is passed to @fn +* +* Execute a given function in the main loop with the BlockDriverState +* AioContext acquired. Block jobs must call bdrv_unref(), bdrv_close(), and +* anything that uses bdrv_drain_all() in the main loop. +* +* The @job AioContext is held while @fn executes. +*/ +void block_job_defer_to_main_loop (BlockJob *job, +BlockJobDeferToMainLoopFn *fn, +void *opaque); +#endif
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 48/53] block: add bdrv_drain()
In reply to Stefan HajnocziNow that op blockers are in use, we can ensure that no other sources are generating I/O on a BlockDriverState. Therefore it is possible to drain requests for a single BDS.
Message-id: [email protected]
--- block.c | 36 +++++++++++++++++++++++++++++------- include/block/block.h | 1 + 2 files changed, 30 insertions(+), 7 deletions(-)
block.c +29 -7
@@ -1904,6 +1904,34 @@ static bool bdrv_requests_pending(BlockDriverState *bs)return false; } +static bool bdrv_drain_one (BlockDriverState *bs) +{ +bool bs_busy; + +bdrv_flush_io_queue (bs); +bdrv_start_throttled_reqs (bs); + bs_busy =bdrv_requests_pending (bs); + bs_busy |=aio_poll (bdrv_get_aio_context (bs), bs_busy); +return bs_busy; +} + +/* +* Wait for pending requests to complete on a single BlockDriverState subtree +* +* See the warning in bdrv_drain_all(). This function can only be called if +* you are sure nothing can generate I/O because you have op blockers +* installed. +* +* Note that unlike bdrv_drain_all(), the caller must hold the BlockDriverState +* AioContext. +*/ +void bdrv_drain (BlockDriverState *bs) +{ +while (bdrv_drain_one (bs)) { +/* Keep iterating */ + } +} +/* * Wait for pending requests to complete across all BlockDriverStates *
@@ -1927,16 +1955,10 @@ void bdrv_drain_all(void) QTAILQ_FOREACH(bs, &bdrv_states, device_list) {AioContext *aio_context =bdrv_get_aio_context (bs); -bool bs_busy;aio_context_acquire (aio_context); -bdrv_flush_io_queue (bs); -bdrv_start_throttled_reqs (bs); - bs_busy =bdrv_requests_pending (bs); - bs_busy |=aio_poll (aio_context, bs_busy); + busy |=bdrv_drain_one (bs);aio_context_release (aio_context); - - busy |= bs_busy; } } }
include/block/block.h +1 -0
@@ -334,6 +334,7 @@ int bdrv_flush(BlockDriverState *bs); int coroutine_fn bdrv_co_flush(BlockDriverState *bs);int bdrv_flush_all (void );void bdrv_close_all (void ); +void bdrv_drain (BlockDriverState *bs);void bdrv_drain_all (void );int bdrv_discard (BlockDriverState *bs,int64_t sector_num,int nb_sectors);
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 49/53] block: let backup blockjob run in BDS AioContext
In reply to Stefan HajnocziThe backup block job must run in the BlockDriverState AioContext so that it works with dataplane.
The basics of acquiring the AioContext are easy in blockdev.c.
The completion code in block/backup.c must call bdrv_unref() from the main loop. Use block_job_defer_to_main_loop() to achieve that.
Message-id: [email protected]
--- block/backup.c | 21 +++++++++++++++++++-- blockdev.c | 23 ++++++++++++++++------- 2 files changed, 35 insertions(+), 9 deletions(-)
block/backup.c +19 -2
@@ -227,9 +227,25 @@ static BlockErrorAction backup_error_action(BackupBlockJob *job, } } +typedef struct { +int ret; +}BackupCompleteData ; + +static void backup_complete (BlockJob *job,void *opaque) +{ +BackupBlockJob *s =container_of (job, BackupBlockJob, common); +BackupCompleteData *data = opaque; + +bdrv_unref (s->target); + +block_job_completed (job, data->ret); +g_free (data); +} + static void coroutine_fn backup_run(void *opaque) {BackupBlockJob *job = opaque; +BackupCompleteData *data;BlockDriverState *bs = job->common.bs;BlockDriverState *target = job->target;BlockdevOnError on_target_error = job->on_target_error;
@@ -344,9 +360,10 @@ static void coroutine_fn backup_run(void *opaque)hbitmap_free (job->bitmap);bdrv_iostatus_disable (target); -bdrv_unref (target); -block_job_completed (&job->common, ret); + data =g_malloc (sizeof (*data)); + data->ret = ret; +block_job_defer_to_main_loop (&job->common, backup_complete, data); } void backup_start(BlockDriverState *bs, BlockDriverState *target,
blockdev.c +16 -7
@@ -2108,6 +2108,7 @@ void qmp_drive_backup(const char *device, const char *target,BlockDriverState *bs;BlockDriverState *target_bs;BlockDriverState *source =NULL ; +AioContext *aio_context;BlockDriver *drv =NULL ;Error *local_err =NULL ;int flags;
@@ -2133,9 +2134,12 @@ void qmp_drive_backup(const char *device, const char *target,return ; } + aio_context =bdrv_get_aio_context (bs); +aio_context_acquire (aio_context); +if (!bdrv_is_inserted (bs)) {error_set (errp,QERR_DEVICE_HAS_NO_MEDIUM , device); -return ; + goto out; }if (!has_format) {
@@ -2145,12 +2149,12 @@ void qmp_drive_backup(const char *device, const char *target, drv =bdrv_find_format (format);if (!drv) {error_set (errp,QERR_INVALID_BLOCK_FORMAT , format); -return ; + goto out; } }if (bdrv_op_is_blocked (bs,BLOCK_OP_TYPE_BACKUP_SOURCE , errp)) { -return ; + goto out; } flags = bs->open_flags |BDRV_O_RDWR ;
@@ -2170,7 +2174,7 @@ void qmp_drive_backup(const char *device, const char *target, size =bdrv_getlength (bs);if (size <0 ) {error_setg_errno (errp, -size,"bdrv_getlength failed" ); -return ; + goto out; }if (mode !=NEW_IMAGE_MODE_EXISTING ) {
@@ -2187,23 +2191,28 @@ void qmp_drive_backup(const char *device, const char *target,if (local_err) {error_propagate (errp, local_err); -return ; + goto out; } target_bs =NULL ; ret =bdrv_open (&target_bs, target,NULL ,NULL , flags, drv, &local_err);if (ret <0 ) {error_propagate (errp, local_err); -return ; + goto out; } +bdrv_set_aio_context (target_bs, aio_context); +backup_start (bs, target_bs, speed, sync, on_source_error, on_target_error, block_job_cb, bs, &local_err);if (local_err !=NULL ) {bdrv_unref (target_bs);error_propagate (errp, local_err); -return ; + goto out; } + +out: +aio_context_release (aio_context); }BlockDeviceInfoList *qmp_query_named_block_nodes (Error **errp)
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 50/53] block: let stream blockjob run in BDS AioContext
In reply to Stefan HajnocziThe stream block job must run in the BlockDriverState AioContext so that it works with dataplane.
The basics of acquiring the AioContext are easy in blockdev.c.
The tricky part is the completion code which drops part of the backing file chain. This must be done in the main loop where bdrv_unref() and bdrv_close() are safe to call. Use block_job_defer_to_main_loop() to achieve that.
Message-id: [email protected]
--- block/stream.c | 50 ++++++++++++++++++++++++++++++++++++-------------- blockdev.c | 16 ++++++++++++---- 2 files changed, 48 insertions(+), 18 deletions(-)
block/stream.c +36 -14
@@ -79,9 +79,39 @@ static void close_unused_images(BlockDriverState *top, BlockDriverState *base,bdrv_refresh_limits (top,NULL ); } +typedef struct { +int ret; +bool reached_end; +}StreamCompleteData ; + +static void stream_complete (BlockJob *job,void *opaque) +{ +StreamBlockJob *s =container_of (job, StreamBlockJob, common); +StreamCompleteData *data = opaque; +BlockDriverState *base = s->base; + +if (!block_job_is_cancelled (&s->common) && data->reached_end && + data->ret ==0 ) { +const char *base_id =NULL , *base_fmt =NULL ; +if (base) { + base_id = s->backing_file_str; +if (base->drv) { + base_fmt = base->drv->format_name; + } + } + data->ret =bdrv_change_backing_file (job->bs, base_id, base_fmt); +close_unused_images (job->bs, base, base_id); + } + +g_free (s->backing_file_str); +block_job_completed (&s->common, data->ret); +g_free (data); +} + static void coroutine_fn stream_run(void *opaque) {StreamBlockJob *s = opaque; +StreamCompleteData *data;BlockDriverState *bs = s->common.bs;BlockDriverState *base = s->base;int64_t sector_num, end;
@@ -183,21 +213,13 @@ wait:/* Do not remove the backing file if an error was there but ignored. */ ret = error; -if (!block_job_is_cancelled (&s->common) && sector_num == end && ret ==0 ) { -const char *base_id =NULL , *base_fmt =NULL ; -if (base) { - base_id = s->backing_file_str; -if (base->drv) { - base_fmt = base->drv->format_name; - } - } - ret =bdrv_change_backing_file (bs, base_id, base_fmt); -close_unused_images (bs, base, base_id); - } -qemu_vfree (buf); -g_free (s->backing_file_str); -block_job_completed (&s->common, ret); + +/* Modify backing chain and close BDSes in main loop */ + data =g_malloc (sizeof (*data)); + data->ret = ret; + data->reached_end = sector_num == end; +block_job_defer_to_main_loop (&s->common, stream_complete, data); }static void stream_set_speed (BlockJob *job,int64_t speed,Error **errp)
blockdev.c +12 -4
@@ -1963,6 +1963,7 @@ void qmp_block_stream(const char *device, {BlockDriverState *bs;BlockDriverState *base_bs =NULL ; +AioContext *aio_context;Error *local_err =NULL ;const char *base_name =NULL ;
@@ -1976,16 +1977,20 @@ void qmp_block_stream(const char *device,return ; } + aio_context =bdrv_get_aio_context (bs); +aio_context_acquire (aio_context); +if (bdrv_op_is_blocked (bs,BLOCK_OP_TYPE_STREAM , errp)) { -return ; + goto out; }if (has_base) { base_bs =bdrv_find_backing_image (bs, base);if (base_bs ==NULL ) {error_set (errp,QERR_BASE_NOT_FOUND , base); -return ; + goto out; } +assert (bdrv_get_aio_context (base_bs) == aio_context); base_name = base; }
@@ -1994,7 +1999,7 @@ void qmp_block_stream(const char *device,if (base_bs ==NULL && has_backing_file) {error_setg (errp,"backing file specified, but streaming the " "entire chain" ); -return ; + goto out; }/* backing_file string overrides base bs filename */
@@ -2004,10 +2009,13 @@ void qmp_block_stream(const char *device, on_error, block_job_cb, bs, &local_err);if (local_err) {error_propagate (errp, local_err); -return ; + goto out; }trace_qmp_block_stream (bs, bs->job); + +out: +aio_context_release (aio_context); } void qmp_block_commit(const char *device,
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 52/53] block: let commit blockjob run in BDS AioContext
In reply to Stefan HajnocziThe commit block job must run in the BlockDriverState AioContext so that it works with dataplane.
Acquire the AioContext in blockdev.c so starting the block job is safe. One detail here is that the bdrv_drain_all() must be moved inside the aio_context_acquire() region so requests cannot sneak in between the drain and acquire.
The completion code in block/commit.c must perform backing chain manipulation and bdrv_reopen() from the main loop. Use block_job_defer_to_main_loop() to achieve that.
Message-id: [email protected]
--- block/commit.c | 70 ++++++++++++++++++++++++++++++++++++---------------------- blockdev.c | 29 ++++++++++++++++-------- 2 files changed, 64 insertions(+), 35 deletions(-)
block/commit.c +44 -26
@@ -60,17 +60,50 @@ static int coroutine_fn commit_populate(BlockDriverState *bs,return 0 ; } -static void coroutine_fn commit_run(void *opaque) +typedef struct { +int ret; +}CommitCompleteData ; + +static void commit_complete (BlockJob *job,void *opaque) { -CommitBlockJob *s = opaque; +CommitBlockJob *s =container_of (job, CommitBlockJob, common); +CommitCompleteData *data = opaque;BlockDriverState *active = s->active;BlockDriverState *top = s->top;BlockDriverState *base = s->base;BlockDriverState *overlay_bs; +int ret = data->ret; + +if (!block_job_is_cancelled (&s->common) && ret ==0 ) { +/* success */ + ret =bdrv_drop_intermediate (active, top, base, s->backing_file_str); + } + +/* restore base open flags here if appropriate (e.g., change the base back +* to r/o). These reopens do not need to be atomic, since we won't abort +* even on failure here */ +if (s->base_flags !=bdrv_get_flags (base)) { +bdrv_reopen (base, s->base_flags,NULL ); + } + overlay_bs =bdrv_find_overlay (active, top); +if (overlay_bs && s->orig_overlay_flags !=bdrv_get_flags (overlay_bs)) { +bdrv_reopen (overlay_bs, s->orig_overlay_flags,NULL ); + } +g_free (s->backing_file_str); +block_job_completed (&s->common, ret); +g_free (data); +} + +static void coroutine_fn commit_run(void *opaque) +{ +CommitBlockJob *s = opaque; +CommitCompleteData *data; +BlockDriverState *top = s->top; +BlockDriverState *base = s->base;int64_t sector_num, end;int ret =0 ;int n =0 ; -void *buf; +void *buf =NULL ;int bytes_written =0 ;int64_t base_len;
@@ -78,18 +111,18 @@ static void coroutine_fn commit_run(void *opaque)if (s->common.len <0 ) { - goto exit_restore_reopen; + goto out; } ret = base_len =bdrv_getlength (base);if (base_len <0 ) { - goto exit_restore_reopen; + goto out; }if (base_len < s->common.len) { ret =bdrv_truncate (base, s->common.len);if (ret) { - goto exit_restore_reopen; + goto out; } }
@@ -128,7 +161,7 @@ wait:if (s->on_error ==BLOCKDEV_ON_ERROR_STOP || s->on_error ==BLOCKDEV_ON_ERROR_REPORT || (s->on_error ==BLOCKDEV_ON_ERROR_ENOSPC && ret == -ENOSPC )) { - goto exit_free_buf; + goto out; }else { n =0 ;continue ;
@@ -140,27 +173,12 @@ wait: ret =0 ; -if (!block_job_is_cancelled (&s->common) && sector_num == end) { -/* success */ - ret =bdrv_drop_intermediate (active, top, base, s->backing_file_str); - } - -exit_free_buf: +out:qemu_vfree (buf); -exit_restore_reopen: -/* restore base open flags here if appropriate (e.g., change the base back -* to r/o). These reopens do not need to be atomic, since we won't abort -* even on failure here */ -if (s->base_flags !=bdrv_get_flags (base)) { -bdrv_reopen (base, s->base_flags,NULL ); - } - overlay_bs =bdrv_find_overlay (active, top); -if (overlay_bs && s->orig_overlay_flags !=bdrv_get_flags (overlay_bs)) { -bdrv_reopen (overlay_bs, s->orig_overlay_flags,NULL ); - } -g_free (s->backing_file_str); -block_job_completed (&s->common, ret); + data =g_malloc (sizeof (*data)); + data->ret = ret; +block_job_defer_to_main_loop (&s->common, commit_complete, data); }static void commit_set_speed (BlockJob *job,int64_t speed,Error **errp)
blockdev.c +20 -9
@@ -2027,6 +2027,7 @@ void qmp_block_commit(const char *device, {BlockDriverState *bs;BlockDriverState *base_bs, *top_bs; +AioContext *aio_context;Error *local_err =NULL ;/* This will be part of the QMP command, if/when the * BlockdevOnError change for blkmirror makes it in
@@ -2037,9 +2038,6 @@ void qmp_block_commit(const char *device, speed =0 ; } -/* drain all i/o before commits */ -bdrv_drain_all (); -/* Important Note: * libvirt relies on the DeviceNotFound error class in order to probe for * live commit feature versions; for this to work, we must make sure to
@@ -2051,8 +2049,14 @@ void qmp_block_commit(const char *device,return ; } + aio_context =bdrv_get_aio_context (bs); +aio_context_acquire (aio_context); + +/* drain all i/o before commits */ +bdrv_drain_all (); +if (bdrv_op_is_blocked (bs,BLOCK_OP_TYPE_COMMIT , errp)) { -return ; + goto out; }/* default top_bs is the active layer */
@@ -2066,9 +2070,11 @@ void qmp_block_commit(const char *device,if (top_bs ==NULL ) {error_setg (errp,"Top image file %s not found" , top ? top :"NULL" ); -return ; + goto out; } +assert (bdrv_get_aio_context (top_bs) == aio_context); +if (has_base && base) { base_bs =bdrv_find_backing_image (top_bs, base); }else {
@@ -2077,20 +2083,22 @@ void qmp_block_commit(const char *device,if (base_bs ==NULL ) {error_set (errp,QERR_BASE_NOT_FOUND , base ? base :"NULL" ); -return ; + goto out; } +assert (bdrv_get_aio_context (base_bs) == aio_context); +/* Do not allow attempts to commit an image into itself */ if (top_bs == base_bs) {error_setg (errp,"cannot commit an image into itself" ); -return ; + goto out; }if (top_bs == bs) {if (has_backing_file) {error_setg (errp,"'backing-file' specified," " but 'top' is the active layer" ); -return ; + goto out; }commit_active_start (bs, base_bs, speed, on_error, block_job_cb, bs, &local_err);
@@ -2100,8 +2108,11 @@ void qmp_block_commit(const char *device, }if (local_err !=NULL ) {error_propagate (errp, local_err); -return ; + goto out; } + +out: +aio_context_release (aio_context); } void qmp_drive_backup(const char *device, const char *target,
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 53/53] block: declare blockjobs and dataplane friends!
In reply to Stefan HajnocziNow that blockjobs use AioContext they are safe for use with dataplane. Unblock them!
Message-id: [email protected]
--- blockjob.c | 1 + hw/block/dataplane/virtio-blk.c | 5 +++++ 2 files changed, 6 insertions(+)
blockjob.c +1 -0
@@ -50,6 +50,7 @@ void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,error_setg (&job->blocker,"block device is in use by block job: %s" , BlockJobType_lookup[driver->job_type]);bdrv_op_block_all (bs, job->blocker); +bdrv_op_unblock (bs,BLOCK_OP_TYPE_DATAPLANE , job->blocker); job->driver = driver; job->bs = bs;
hw/block/dataplane/virtio-blk.c +5 -0
@@ -196,6 +196,11 @@ void virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *conf,blk_op_block_all (conf->conf.blk, s->blocker);blk_op_unblock (conf->conf.blk,BLOCK_OP_TYPE_RESIZE , s->blocker);blk_op_unblock (conf->conf.blk,BLOCK_OP_TYPE_DRIVE_DEL , s->blocker); +blk_op_unblock (conf->conf.blk,BLOCK_OP_TYPE_BACKUP_SOURCE , s->blocker); +blk_op_unblock (conf->conf.blk,BLOCK_OP_TYPE_COMMIT , s->blocker); +blk_op_unblock (conf->conf.blk,BLOCK_OP_TYPE_MIRROR , s->blocker); +blk_op_unblock (conf->conf.blk,BLOCK_OP_TYPE_STREAM , s->blocker); +blk_op_unblock (conf->conf.blk,BLOCK_OP_TYPE_REPLACE , s->blocker); *dataplane = s; }
Signature · 1 line
1.9.3
[Qemu-devel] [PULL 51/53] block: let mirror blockjob run in BDS AioContext
In reply to Stefan HajnocziThe mirror block job must run in the BlockDriverState AioContext so that it works with dataplane.
Acquire the AioContext in blockdev.c so starting the block job is safe.
Note that to_replace is treated separately from other BlockDriverStates in that it does not need to be in the same AioContext. Explicitly acquire/release to_replace's AioContext when accessing it.
The completion code in block/mirror.c must perform BDS graph manipulation and bdrv_reopen() from the main loop. Use block_job_defer_to_main_loop() to achieve that.
The bdrv_drain_all() call is not allowed outside the main loop since it could lead to lock ordering problems. Use bdrv_drain(bs) instead because we have acquired the AioContext so nothing else can sneak in I/O.
Message-id: [email protected]
--- block.c | 13 +++++++-- block/mirror.c | 85 ++++++++++++++++++++++++++++++++++++++++------------------ blockdev.c | 38 ++++++++++++++++++-------- 3 files changed, 97 insertions(+), 39 deletions(-)
block.c +11 -2
@@ -5850,13 +5850,19 @@ bool bdrv_is_first_non_filter(BlockDriverState *candidate)BlockDriverState *check_to_replace_node (const char *node_name,Error **errp) {BlockDriverState *to_replace_bs =bdrv_find_node (node_name); +AioContext *aio_context; +if (!to_replace_bs) {error_setg (errp,"Node name '%s' not found" , node_name);return NULL ; } + aio_context =bdrv_get_aio_context (to_replace_bs); +aio_context_acquire (aio_context); +if (bdrv_op_is_blocked (to_replace_bs,BLOCK_OP_TYPE_REPLACE , errp)) { -return NULL ; + to_replace_bs =NULL ; + goto out; }/* We don't want arbitrary node of the BDS chain to be replaced only the top
@@ -5866,9 +5872,12 @@ BlockDriverState *check_to_replace_node(const char *node_name, Error **errp)*/ if (!bdrv_is_first_non_filter (to_replace_bs)) {error_setg (errp,"Only top most non filter can be replaced" ); -return NULL ; + to_replace_bs =NULL ; + goto out; } +out: +aio_context_release (aio_context);return to_replace_bs; }
block/mirror.c +59 -26
122 lines not on this page · raw message
blockdev.c +27 -11
@@ -2245,6 +2245,7 @@ void qmp_drive_mirror(const char *device, const char *target, {BlockDriverState *bs;BlockDriverState *source, *target_bs; +AioContext *aio_context;BlockDriver *drv =NULL ;Error *local_err =NULL ;QDict *options =NULL ;
@@ -2287,9 +2288,12 @@ void qmp_drive_mirror(const char *device, const char *target,return ; } + aio_context =bdrv_get_aio_context (bs); +aio_context_acquire (aio_context); +if (!bdrv_is_inserted (bs)) {error_set (errp,QERR_DEVICE_HAS_NO_MEDIUM , device); -return ; + goto out; }if (!has_format) {
@@ -2299,12 +2303,12 @@ void qmp_drive_mirror(const char *device, const char *target, drv =bdrv_find_format (format);if (!drv) {error_set (errp,QERR_INVALID_BLOCK_FORMAT , format); -return ; + goto out; } }if (bdrv_op_is_blocked (bs,BLOCK_OP_TYPE_MIRROR , errp)) { -return ; + goto out; } flags = bs->open_flags |BDRV_O_RDWR ;
@@ -2319,29 +2323,36 @@ void qmp_drive_mirror(const char *device, const char *target, size =bdrv_getlength (bs);if (size <0 ) {error_setg_errno (errp, -size,"bdrv_getlength failed" ); -return ; + goto out; }if (has_replaces) {BlockDriverState *to_replace_bs; +AioContext *replace_aio_context; +int64_t replace_size;if (!has_node_name) {error_setg (errp,"a node-name must be provided when replacing a" " named node of the graph" ); -return ; + goto out; } to_replace_bs =check_to_replace_node (replaces, &local_err);if (!to_replace_bs) {error_propagate (errp, local_err); -return ; + goto out; } -if (size !=bdrv_getlength (to_replace_bs)) { + replace_aio_context =bdrv_get_aio_context (to_replace_bs); +aio_context_acquire (replace_aio_context); + replace_size =bdrv_getlength (to_replace_bs); +aio_context_release (replace_aio_context); + +if (size != replace_size) {error_setg (errp,"cannot replace image with a mirror image of " "different size" ); -return ; + goto out; } }
@@ -2370,7 +2381,7 @@ void qmp_drive_mirror(const char *device, const char *target,if (local_err) {error_propagate (errp, local_err); -return ; + goto out; }if (has_node_name) {
@@ -2386,9 +2397,11 @@ void qmp_drive_mirror(const char *device, const char *target, flags | BDRV_O_NO_BACKING, drv, &local_err);if (ret <0 ) {error_propagate (errp, local_err); -return ; + goto out; } +bdrv_set_aio_context (target_bs, aio_context); +/* pass the node name to replace to mirror start since it's loose coupling * and will allow to check whether the node still exist at mirror completion */
@@ -2400,8 +2413,11 @@ void qmp_drive_mirror(const char *device, const char *target,if (local_err !=NULL ) {bdrv_unref (target_bs);error_propagate (errp, local_err); -return ; + goto out; } + +out: +aio_context_release (aio_context); }/* Get the block job for a given device name and acquire its AioContext */
Signature · 1 line
1.9.3
Peter Maydell
AppliedQuoted 8 lines · Stefan Hajnoczi
The following changes since commit
0a2923f8488498000eec54871456aa64a4391da4:tcg/mips: fix store softmmu slow path (2014-11-02 13:30:00 +0100)
are available in the git repository at:
git://github.com/stefanha/qemu.gittags/block-pull-request
Quoted 7 lines
for you to fetch changes up to
b112a65c52aa45a23b83b1e0d56db3b7cc44597e:block: declare blockjobs and dataplane friends! (2014-11-03 11:41:49 +0000)
----------------------------------------------------------------
----------------------------------------------------------------
Applied, thanks.
-- PMM