Skip to content

Commit

Permalink
vhdx: Take locks for accessing bs->file
Browse files Browse the repository at this point in the history
This updates the vhdx code to add GRAPH_RDLOCK annotations for all
places that read bs->file.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Message-ID: <20231027155333.420094-23-kwolf@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
  • Loading branch information
kevmw committed Nov 8, 2023
1 parent 8f89734 commit 65ff757
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
40 changes: 22 additions & 18 deletions block/vhdx-log.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ static const MSGUID zero_guid = { 0 };

/* Allow peeking at the hdr entry at the beginning of the current
* read index, without advancing the read index */
static int vhdx_log_peek_hdr(BlockDriverState *bs, VHDXLogEntries *log,
VHDXLogEntryHeader *hdr)
static int GRAPH_RDLOCK
vhdx_log_peek_hdr(BlockDriverState *bs, VHDXLogEntries *log,
VHDXLogEntryHeader *hdr)
{
int ret = 0;
uint64_t offset;
Expand Down Expand Up @@ -107,7 +108,7 @@ static int vhdx_log_inc_idx(uint32_t idx, uint64_t length)


/* Reset the log to empty */
static void vhdx_log_reset(BlockDriverState *bs, BDRVVHDXState *s)
static void GRAPH_RDLOCK vhdx_log_reset(BlockDriverState *bs, BDRVVHDXState *s)
{
MSGUID guid = { 0 };
s->log.read = s->log.write = 0;
Expand All @@ -127,9 +128,10 @@ static void vhdx_log_reset(BlockDriverState *bs, BDRVVHDXState *s)
* not modified.
*
* 0 is returned on success, -errno otherwise. */
static int vhdx_log_read_sectors(BlockDriverState *bs, VHDXLogEntries *log,
uint32_t *sectors_read, void *buffer,
uint32_t num_sectors, bool peek)
static int GRAPH_RDLOCK
vhdx_log_read_sectors(BlockDriverState *bs, VHDXLogEntries *log,
uint32_t *sectors_read, void *buffer,
uint32_t num_sectors, bool peek)
{
int ret = 0;
uint64_t offset;
Expand Down Expand Up @@ -333,9 +335,9 @@ static int vhdx_compute_desc_sectors(uint32_t desc_cnt)
* will allocate all the space for buffer, which must be NULL when
* passed into this function. Each descriptor will also be validated,
* and error returned if any are invalid. */
static int vhdx_log_read_desc(BlockDriverState *bs, BDRVVHDXState *s,
VHDXLogEntries *log, VHDXLogDescEntries **buffer,
bool convert_endian)
static int GRAPH_RDLOCK
vhdx_log_read_desc(BlockDriverState *bs, BDRVVHDXState *s, VHDXLogEntries *log,
VHDXLogDescEntries **buffer, bool convert_endian)
{
int ret = 0;
uint32_t desc_sectors;
Expand Down Expand Up @@ -412,8 +414,9 @@ static int vhdx_log_read_desc(BlockDriverState *bs, BDRVVHDXState *s,
* For a zero descriptor, it may describe multiple sectors to fill with zeroes.
* In this case, it should be noted that zeroes are written to disk, and the
* image file is not extended as a sparse file. */
static int vhdx_log_flush_desc(BlockDriverState *bs, VHDXLogDescriptor *desc,
VHDXLogDataSector *data)
static int GRAPH_RDLOCK
vhdx_log_flush_desc(BlockDriverState *bs, VHDXLogDescriptor *desc,
VHDXLogDataSector *data)
{
int ret = 0;
uint64_t seq, file_offset;
Expand Down Expand Up @@ -484,8 +487,8 @@ static int vhdx_log_flush_desc(BlockDriverState *bs, VHDXLogDescriptor *desc,
* file, and then set the log to 'empty' status once complete.
*
* The log entries should be validate prior to flushing */
static int vhdx_log_flush(BlockDriverState *bs, BDRVVHDXState *s,
VHDXLogSequence *logs)
static int GRAPH_RDLOCK
vhdx_log_flush(BlockDriverState *bs, BDRVVHDXState *s, VHDXLogSequence *logs)
{
int ret = 0;
int i;
Expand Down Expand Up @@ -584,9 +587,10 @@ static int vhdx_log_flush(BlockDriverState *bs, BDRVVHDXState *s,
return ret;
}

static int vhdx_validate_log_entry(BlockDriverState *bs, BDRVVHDXState *s,
VHDXLogEntries *log, uint64_t seq,
bool *valid, VHDXLogEntryHeader *entry)
static int GRAPH_RDLOCK
vhdx_validate_log_entry(BlockDriverState *bs, BDRVVHDXState *s,
VHDXLogEntries *log, uint64_t seq,
bool *valid, VHDXLogEntryHeader *entry)
{
int ret = 0;
VHDXLogEntryHeader hdr;
Expand Down Expand Up @@ -663,8 +667,8 @@ static int vhdx_validate_log_entry(BlockDriverState *bs, BDRVVHDXState *s,
/* Search through the log circular buffer, and find the valid, active
* log sequence, if any exists
* */
static int vhdx_log_search(BlockDriverState *bs, BDRVVHDXState *s,
VHDXLogSequence *logs)
static int GRAPH_RDLOCK
vhdx_log_search(BlockDriverState *bs, BDRVVHDXState *s, VHDXLogSequence *logs)
{
int ret = 0;
uint32_t tail;
Expand Down
18 changes: 11 additions & 7 deletions block/vhdx.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,9 @@ static int vhdx_write_header(BdrvChild *file, VHDXHeader *hdr,
*
* - non-current header is updated with largest sequence number
*/
static int vhdx_update_header(BlockDriverState *bs, BDRVVHDXState *s,
bool generate_data_write_guid, MSGUID *log_guid)
static int GRAPH_RDLOCK
vhdx_update_header(BlockDriverState *bs, BDRVVHDXState *s,
bool generate_data_write_guid, MSGUID *log_guid)
{
int ret = 0;
int hdr_idx = 0;
Expand Down Expand Up @@ -416,8 +417,8 @@ int vhdx_update_headers(BlockDriverState *bs, BDRVVHDXState *s,
}

/* opens the specified header block from the VHDX file header section */
static void vhdx_parse_header(BlockDriverState *bs, BDRVVHDXState *s,
Error **errp)
static void GRAPH_RDLOCK
vhdx_parse_header(BlockDriverState *bs, BDRVVHDXState *s, Error **errp)
{
int ret;
VHDXHeader *header1;
Expand Down Expand Up @@ -517,7 +518,8 @@ static void vhdx_parse_header(BlockDriverState *bs, BDRVVHDXState *s,
}


static int vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s)
static int GRAPH_RDLOCK
vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s)
{
int ret = 0;
uint8_t *buffer;
Expand Down Expand Up @@ -634,7 +636,8 @@ static int vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s)
* Also, if the File Parameters indicate this is a differencing file,
* we must also look for the Parent Locator metadata item.
*/
static int vhdx_parse_metadata(BlockDriverState *bs, BDRVVHDXState *s)
static int GRAPH_RDLOCK
vhdx_parse_metadata(BlockDriverState *bs, BDRVVHDXState *s)
{
int ret = 0;
uint8_t *buffer;
Expand Down Expand Up @@ -885,7 +888,8 @@ static void vhdx_calc_bat_entries(BDRVVHDXState *s)

}

static int vhdx_check_bat_entries(BlockDriverState *bs, int *errcnt)
static int coroutine_mixed_fn GRAPH_RDLOCK
vhdx_check_bat_entries(BlockDriverState *bs, int *errcnt)
{
BDRVVHDXState *s = bs->opaque;
int64_t image_file_size = bdrv_getlength(bs->file->bs);
Expand Down
9 changes: 6 additions & 3 deletions block/vhdx.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,9 @@ typedef struct BDRVVHDXState {

void vhdx_guid_generate(MSGUID *guid);

int vhdx_update_headers(BlockDriverState *bs, BDRVVHDXState *s, bool rw,
MSGUID *log_guid);
int GRAPH_RDLOCK
vhdx_update_headers(BlockDriverState *bs, BDRVVHDXState *s, bool rw,
MSGUID *log_guid);

uint32_t vhdx_update_checksum(uint8_t *buf, size_t size, int crc_offset);
uint32_t vhdx_checksum_calc(uint32_t crc, uint8_t *buf, size_t size,
Expand Down Expand Up @@ -448,6 +449,8 @@ void vhdx_metadata_header_le_import(VHDXMetadataTableHeader *hdr);
void vhdx_metadata_header_le_export(VHDXMetadataTableHeader *hdr);
void vhdx_metadata_entry_le_import(VHDXMetadataTableEntry *e);
void vhdx_metadata_entry_le_export(VHDXMetadataTableEntry *e);
int vhdx_user_visible_write(BlockDriverState *bs, BDRVVHDXState *s);

int GRAPH_RDLOCK
vhdx_user_visible_write(BlockDriverState *bs, BDRVVHDXState *s);

#endif

0 comments on commit 65ff757

Please sign in to comment.