vhdx: mark more functions as coroutine_fns and GRAPH_RDLOCK

Mark functions as coroutine_fn when they are only called by other coroutine_fns
and they can suspend.  Change calls to co_wrappers to use the non-wrapped
functions, which in turn requires adding GRAPH_RDLOCK annotations.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Message-ID: <20230601115145.196465-10-pbonzini@redhat.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Paolo Bonzini 2023-06-01 13:51:42 +02:00 committed by Kevin Wolf
parent 28944f99c4
commit f6b0899493
3 changed files with 57 additions and 57 deletions

View File

@ -169,9 +169,10 @@ exit:
* It is assumed that 'buffer' is at least 4096*num_sectors large. * It is assumed that 'buffer' is at least 4096*num_sectors large.
* *
* 0 is returned on success, -errno otherwise */ * 0 is returned on success, -errno otherwise */
static int vhdx_log_write_sectors(BlockDriverState *bs, VHDXLogEntries *log, static int coroutine_fn GRAPH_RDLOCK
uint32_t *sectors_written, void *buffer, vhdx_log_write_sectors(BlockDriverState *bs, VHDXLogEntries *log,
uint32_t num_sectors) uint32_t *sectors_written, void *buffer,
uint32_t num_sectors)
{ {
int ret = 0; int ret = 0;
uint64_t offset; uint64_t offset;
@ -195,8 +196,7 @@ static int vhdx_log_write_sectors(BlockDriverState *bs, VHDXLogEntries *log,
/* full */ /* full */
break; break;
} }
ret = bdrv_pwrite(bs->file, offset, VHDX_LOG_SECTOR_SIZE, buffer_tmp, ret = bdrv_co_pwrite(bs->file, offset, VHDX_LOG_SECTOR_SIZE, buffer_tmp, 0);
0);
if (ret < 0) { if (ret < 0) {
goto exit; goto exit;
} }
@ -853,8 +853,9 @@ static void vhdx_log_raw_to_le_sector(VHDXLogDescriptor *desc,
} }
static int vhdx_log_write(BlockDriverState *bs, BDRVVHDXState *s, static int coroutine_fn GRAPH_RDLOCK
void *data, uint32_t length, uint64_t offset) vhdx_log_write(BlockDriverState *bs, BDRVVHDXState *s,
void *data, uint32_t length, uint64_t offset)
{ {
int ret = 0; int ret = 0;
void *buffer = NULL; void *buffer = NULL;
@ -924,7 +925,7 @@ static int vhdx_log_write(BlockDriverState *bs, BDRVVHDXState *s,
sectors += partial_sectors; sectors += partial_sectors;
file_length = bdrv_getlength(bs->file->bs); file_length = bdrv_co_getlength(bs->file->bs);
if (file_length < 0) { if (file_length < 0) {
ret = file_length; ret = file_length;
goto exit; goto exit;
@ -971,8 +972,8 @@ static int vhdx_log_write(BlockDriverState *bs, BDRVVHDXState *s,
if (i == 0 && leading_length) { if (i == 0 && leading_length) {
/* partial sector at the front of the buffer */ /* partial sector at the front of the buffer */
ret = bdrv_pread(bs->file, file_offset, VHDX_LOG_SECTOR_SIZE, ret = bdrv_co_pread(bs->file, file_offset, VHDX_LOG_SECTOR_SIZE,
merged_sector, 0); merged_sector, 0);
if (ret < 0) { if (ret < 0) {
goto exit; goto exit;
} }
@ -981,9 +982,9 @@ static int vhdx_log_write(BlockDriverState *bs, BDRVVHDXState *s,
sector_write = merged_sector; sector_write = merged_sector;
} else if (i == sectors - 1 && trailing_length) { } else if (i == sectors - 1 && trailing_length) {
/* partial sector at the end of the buffer */ /* partial sector at the end of the buffer */
ret = bdrv_pread(bs->file, file_offset + trailing_length, ret = bdrv_co_pread(bs->file, file_offset + trailing_length,
VHDX_LOG_SECTOR_SIZE - trailing_length, VHDX_LOG_SECTOR_SIZE - trailing_length,
merged_sector + trailing_length, 0); merged_sector + trailing_length, 0);
if (ret < 0) { if (ret < 0) {
goto exit; goto exit;
} }
@ -1036,8 +1037,9 @@ exit:
} }
/* Perform a log write, and then immediately flush the entire log */ /* Perform a log write, and then immediately flush the entire log */
int vhdx_log_write_and_flush(BlockDriverState *bs, BDRVVHDXState *s, int coroutine_fn
void *data, uint32_t length, uint64_t offset) vhdx_log_write_and_flush(BlockDriverState *bs, BDRVVHDXState *s,
void *data, uint32_t length, uint64_t offset)
{ {
int ret = 0; int ret = 0;
VHDXLogSequence logs = { .valid = true, VHDXLogSequence logs = { .valid = true,
@ -1047,7 +1049,7 @@ int vhdx_log_write_and_flush(BlockDriverState *bs, BDRVVHDXState *s,
/* Make sure data written (new and/or changed blocks) is stable /* Make sure data written (new and/or changed blocks) is stable
* on disk, before creating log entry */ * on disk, before creating log entry */
ret = bdrv_flush(bs); ret = bdrv_co_flush(bs);
if (ret < 0) { if (ret < 0) {
goto exit; goto exit;
} }
@ -1059,7 +1061,7 @@ int vhdx_log_write_and_flush(BlockDriverState *bs, BDRVVHDXState *s,
logs.log = s->log; logs.log = s->log;
/* Make sure log is stable on disk */ /* Make sure log is stable on disk */
ret = bdrv_flush(bs); ret = bdrv_co_flush(bs);
if (ret < 0) { if (ret < 0) {
goto exit; goto exit;
} }

View File

@ -1250,12 +1250,13 @@ exit:
* *
* Returns the file offset start of the new payload block * Returns the file offset start of the new payload block
*/ */
static int vhdx_allocate_block(BlockDriverState *bs, BDRVVHDXState *s, static int coroutine_fn GRAPH_RDLOCK
uint64_t *new_offset, bool *need_zero) vhdx_allocate_block(BlockDriverState *bs, BDRVVHDXState *s,
uint64_t *new_offset, bool *need_zero)
{ {
int64_t current_len; int64_t current_len;
current_len = bdrv_getlength(bs->file->bs); current_len = bdrv_co_getlength(bs->file->bs);
if (current_len < 0) { if (current_len < 0) {
return current_len; return current_len;
} }
@ -1271,16 +1272,16 @@ static int vhdx_allocate_block(BlockDriverState *bs, BDRVVHDXState *s,
if (*need_zero) { if (*need_zero) {
int ret; int ret;
ret = bdrv_truncate(bs->file, *new_offset + s->block_size, false, ret = bdrv_co_truncate(bs->file, *new_offset + s->block_size, false,
PREALLOC_MODE_OFF, BDRV_REQ_ZERO_WRITE, NULL); PREALLOC_MODE_OFF, BDRV_REQ_ZERO_WRITE, NULL);
if (ret != -ENOTSUP) { if (ret != -ENOTSUP) {
*need_zero = false; *need_zero = false;
return ret; return ret;
} }
} }
return bdrv_truncate(bs->file, *new_offset + s->block_size, false, return bdrv_co_truncate(bs->file, *new_offset + s->block_size, false,
PREALLOC_MODE_OFF, 0, NULL); PREALLOC_MODE_OFF, 0, NULL);
} }
/* /*
@ -1572,12 +1573,10 @@ exit:
* The first 64KB of the Metadata section is reserved for the metadata * The first 64KB of the Metadata section is reserved for the metadata
* header and entries; beyond that, the metadata items themselves reside. * header and entries; beyond that, the metadata items themselves reside.
*/ */
static int vhdx_create_new_metadata(BlockBackend *blk, static int coroutine_fn
uint64_t image_size, vhdx_create_new_metadata(BlockBackend *blk, uint64_t image_size,
uint32_t block_size, uint32_t block_size, uint32_t sector_size,
uint32_t sector_size, uint64_t metadata_offset, VHDXImageType type)
uint64_t metadata_offset,
VHDXImageType type)
{ {
int ret = 0; int ret = 0;
uint32_t offset = 0; uint32_t offset = 0;
@ -1668,13 +1667,13 @@ static int vhdx_create_new_metadata(BlockBackend *blk,
VHDX_META_FLAGS_IS_VIRTUAL_DISK; VHDX_META_FLAGS_IS_VIRTUAL_DISK;
vhdx_metadata_entry_le_export(&md_table_entry[4]); vhdx_metadata_entry_le_export(&md_table_entry[4]);
ret = blk_pwrite(blk, metadata_offset, VHDX_HEADER_BLOCK_SIZE, buffer, 0); ret = blk_co_pwrite(blk, metadata_offset, VHDX_HEADER_BLOCK_SIZE, buffer, 0);
if (ret < 0) { if (ret < 0) {
goto exit; goto exit;
} }
ret = blk_pwrite(blk, metadata_offset + (64 * KiB), ret = blk_co_pwrite(blk, metadata_offset + (64 * KiB),
VHDX_METADATA_ENTRY_BUFFER_SIZE, entry_buffer, 0); VHDX_METADATA_ENTRY_BUFFER_SIZE, entry_buffer, 0);
if (ret < 0) { if (ret < 0) {
goto exit; goto exit;
} }
@ -1694,10 +1693,11 @@ exit:
* Fixed images: default state of the BAT is fully populated, with * Fixed images: default state of the BAT is fully populated, with
* file offsets and state PAYLOAD_BLOCK_FULLY_PRESENT. * file offsets and state PAYLOAD_BLOCK_FULLY_PRESENT.
*/ */
static int vhdx_create_bat(BlockBackend *blk, BDRVVHDXState *s, static int coroutine_fn
uint64_t image_size, VHDXImageType type, vhdx_create_bat(BlockBackend *blk, BDRVVHDXState *s,
bool use_zero_blocks, uint64_t file_offset, uint64_t image_size, VHDXImageType type,
uint32_t length, Error **errp) bool use_zero_blocks, uint64_t file_offset,
uint32_t length, Error **errp)
{ {
int ret = 0; int ret = 0;
uint64_t data_file_offset; uint64_t data_file_offset;
@ -1718,14 +1718,14 @@ static int vhdx_create_bat(BlockBackend *blk, BDRVVHDXState *s,
if (type == VHDX_TYPE_DYNAMIC) { if (type == VHDX_TYPE_DYNAMIC) {
/* All zeroes, so we can just extend the file - the end of the BAT /* All zeroes, so we can just extend the file - the end of the BAT
* is the furthest thing we have written yet */ * is the furthest thing we have written yet */
ret = blk_truncate(blk, data_file_offset, false, PREALLOC_MODE_OFF, ret = blk_co_truncate(blk, data_file_offset, false, PREALLOC_MODE_OFF,
0, errp); 0, errp);
if (ret < 0) { if (ret < 0) {
goto exit; goto exit;
} }
} else if (type == VHDX_TYPE_FIXED) { } else if (type == VHDX_TYPE_FIXED) {
ret = blk_truncate(blk, data_file_offset + image_size, false, ret = blk_co_truncate(blk, data_file_offset + image_size, false,
PREALLOC_MODE_OFF, 0, errp); PREALLOC_MODE_OFF, 0, errp);
if (ret < 0) { if (ret < 0) {
goto exit; goto exit;
} }
@ -1759,7 +1759,7 @@ static int vhdx_create_bat(BlockBackend *blk, BDRVVHDXState *s,
s->bat[sinfo.bat_idx] = cpu_to_le64(s->bat[sinfo.bat_idx]); s->bat[sinfo.bat_idx] = cpu_to_le64(s->bat[sinfo.bat_idx]);
sector_num += s->sectors_per_block; sector_num += s->sectors_per_block;
} }
ret = blk_pwrite(blk, file_offset, length, s->bat, 0); ret = blk_co_pwrite(blk, file_offset, length, s->bat, 0);
if (ret < 0) { if (ret < 0) {
error_setg_errno(errp, -ret, "Failed to write the BAT"); error_setg_errno(errp, -ret, "Failed to write the BAT");
goto exit; goto exit;
@ -1780,15 +1780,12 @@ exit:
* to create the BAT itself, we will also cause the BAT to be * to create the BAT itself, we will also cause the BAT to be
* created. * created.
*/ */
static int vhdx_create_new_region_table(BlockBackend *blk, static int coroutine_fn
uint64_t image_size, vhdx_create_new_region_table(BlockBackend *blk, uint64_t image_size,
uint32_t block_size, uint32_t block_size, uint32_t sector_size,
uint32_t sector_size, uint32_t log_size, bool use_zero_blocks,
uint32_t log_size, VHDXImageType type, uint64_t *metadata_offset,
bool use_zero_blocks, Error **errp)
VHDXImageType type,
uint64_t *metadata_offset,
Error **errp)
{ {
int ret = 0; int ret = 0;
uint32_t offset = 0; uint32_t offset = 0;
@ -1863,15 +1860,15 @@ static int vhdx_create_new_region_table(BlockBackend *blk,
} }
/* Now write out the region headers to disk */ /* Now write out the region headers to disk */
ret = blk_pwrite(blk, VHDX_REGION_TABLE_OFFSET, VHDX_HEADER_BLOCK_SIZE, ret = blk_co_pwrite(blk, VHDX_REGION_TABLE_OFFSET, VHDX_HEADER_BLOCK_SIZE,
buffer, 0); buffer, 0);
if (ret < 0) { if (ret < 0) {
error_setg_errno(errp, -ret, "Failed to write first region table"); error_setg_errno(errp, -ret, "Failed to write first region table");
goto exit; goto exit;
} }
ret = blk_pwrite(blk, VHDX_REGION_TABLE2_OFFSET, VHDX_HEADER_BLOCK_SIZE, ret = blk_co_pwrite(blk, VHDX_REGION_TABLE2_OFFSET, VHDX_HEADER_BLOCK_SIZE,
buffer, 0); buffer, 0);
if (ret < 0) { if (ret < 0) {
error_setg_errno(errp, -ret, "Failed to write second region table"); error_setg_errno(errp, -ret, "Failed to write second region table");
goto exit; goto exit;

View File

@ -413,8 +413,9 @@ bool vhdx_checksum_is_valid(uint8_t *buf, size_t size, int crc_offset);
int vhdx_parse_log(BlockDriverState *bs, BDRVVHDXState *s, bool *flushed, int vhdx_parse_log(BlockDriverState *bs, BDRVVHDXState *s, bool *flushed,
Error **errp); Error **errp);
int vhdx_log_write_and_flush(BlockDriverState *bs, BDRVVHDXState *s, int coroutine_fn GRAPH_RDLOCK
void *data, uint32_t length, uint64_t offset); vhdx_log_write_and_flush(BlockDriverState *bs, BDRVVHDXState *s,
void *data, uint32_t length, uint64_t offset);
static inline void leguid_to_cpus(MSGUID *guid) static inline void leguid_to_cpus(MSGUID *guid)
{ {