Skip to content

Commit

Permalink
block/bochs: Fix error handling for seek_to_sector()
Browse files Browse the repository at this point in the history
Currently, seek_to_sector() returns -1 both for errors and unallocated
sectors, resulting in silent errors. As 0 is an invalid offset of data
clusters (bitmap_offset is greater than 0 because s->data_offset is
greater than 0), just return 0 for unallocated sectors and -errno in
case of error. This should then be propagated by bochs_read(), the sole
user of seek_to_sector().

That function also has a case of "return -1 in case of error", which is
fixed by this patch as well.

bochs_read() is called by bochs_co_read() which passes the return value
through, therefore it is indeed correct for bochs_read() to return
-errno.

Signed-off-by: Max Reitz <mreitz@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
  • Loading branch information
XanClic authored and kevmw committed Apr 30, 2014
1 parent b93f995 commit e1b42f4
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions block/bochs.c
Expand Up @@ -187,27 +187,29 @@ static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
uint64_t offset = sector_num * 512;
uint64_t extent_index, extent_offset, bitmap_offset;
char bitmap_entry;
int ret;

// seek to sector
extent_index = offset / s->extent_size;
extent_offset = (offset % s->extent_size) / 512;

if (s->catalog_bitmap[extent_index] == 0xffffffff) {
return -1; /* not allocated */
return 0; /* not allocated */
}

bitmap_offset = s->data_offset +
(512 * (uint64_t) s->catalog_bitmap[extent_index] *
(s->extent_blocks + s->bitmap_blocks));

/* read in bitmap for current extent */
if (bdrv_pread(bs->file, bitmap_offset + (extent_offset / 8),
&bitmap_entry, 1) != 1) {
return -1;
ret = bdrv_pread(bs->file, bitmap_offset + (extent_offset / 8),
&bitmap_entry, 1);
if (ret < 0) {
return ret;
}

if (!((bitmap_entry >> (extent_offset % 8)) & 1)) {
return -1; /* not allocated */
return 0; /* not allocated */
}

return bitmap_offset + (512 * (s->bitmap_blocks + extent_offset));
Expand All @@ -220,13 +222,16 @@ static int bochs_read(BlockDriverState *bs, int64_t sector_num,

while (nb_sectors > 0) {
int64_t block_offset = seek_to_sector(bs, sector_num);
if (block_offset >= 0) {
if (block_offset < 0) {
return block_offset;
} else if (block_offset > 0) {
ret = bdrv_pread(bs->file, block_offset, buf, 512);
if (ret != 512) {
return -1;
if (ret < 0) {
return ret;
}
} else
} else {
memset(buf, 0, 512);
}
nb_sectors--;
sector_num++;
buf += 512;
Expand Down

0 comments on commit e1b42f4

Please sign in to comment.