Commit 22afa7b5b67bb8bc58939d100363ee6753a50467

Authored by Kevin Wolf
Committed by Anthony Liguori
1 parent ae95ade0

block-raw: Allow pread beyond the end of growable images

When using O_DIRECT, qcow2 snapshots didn't work any more for me. In the
process of creating the snapshot, qcow2 tries to pwrite some new information
(e.g. new L1 table) which will often end up being after the old end of the
image file. Now pwrite tries to align things and reads the old contents of the
file, read returns 0 because there is nothing to read after the end of file and
pwrite is stuck in an endless loop.

This patch allows to pread beyond the end of an image file. Whenever the
given offset is after the end of the image file, the read succeeds and fills
the buffer with zeros.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Showing 1 changed file with 11 additions and 0 deletions
block/raw-posix.c
... ... @@ -117,6 +117,7 @@ typedef struct BDRVRawState {
117 117 static int posix_aio_init(void);
118 118  
119 119 static int fd_open(BlockDriverState *bs);
  120 +static int64_t raw_getlength(BlockDriverState *bs);
120 121  
121 122 #if defined(__FreeBSD__)
122 123 static int cdrom_reopen(BlockDriverState *bs);
... ... @@ -231,6 +232,16 @@ static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
231 232 if (ret == count)
232 233 goto label__raw_read__success;
233 234  
  235 + /* Allow reads beyond the end (needed for pwrite) */
  236 + if ((ret == 0) && bs->growable) {
  237 + int64_t size = raw_getlength(bs);
  238 + if (offset >= size) {
  239 + memset(buf, 0, count);
  240 + ret = count;
  241 + goto label__raw_read__success;
  242 + }
  243 + }
  244 +
234 245 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
235 246 "] read failed %d : %d = %s\n",
236 247 s->fd, bs->filename, offset, buf, count,
... ...