qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PULL 00/11] Block patches
@ 2010-05-07 15:13 Kevin Wolf
  2010-05-07 15:13 ` [Qemu-devel] [PATCH 01/11] block: Remove semicolon in BDRV_SECTOR_MASK macro Kevin Wolf
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: Kevin Wolf @ 2010-05-07 15:13 UTC (permalink / raw)
  To: aliguori; +Cc: kwolf, qemu-devel

The following changes since commit 2065061ede22d401aae2ce995c3af54db9d28639:
  Igor V. Kovalenko (1):
        sparc64: handle asi referencing nucleus and secondary MMU contexts

are available in the git repository at:

  git://repo.or.cz/qemu/kevin.git for-anthony

Christoph Hellwig (4):
      cloop: use pread
      cloop: use qemu block API
      bochs: use pread
      bochs: use qemu block API

Kevin Wolf (4):
      ide: Fix ide_dma_cancel
      block: Avoid unchecked casts for AIOCBs
      block: Fix protocol detection for Windows devices
      block: Fix bdrv_commit

Ryota Ozaki (1):
      qemu-nbd: Improve error reporting

Stefan Hajnoczi (1):
      block: Remove semicolon in BDRV_SECTOR_MASK macro

Stefan Weil (1):
      block/vdi: Allow disk images of size 0

 block.c          |   19 +++++++-----
 block.h          |    2 +-
 block/blkdebug.c |    4 +-
 block/bochs.c    |   81 +++++++++++++++--------------------------------------
 block/cloop.c    |   48 ++++++++++++++++----------------
 block/qcow.c     |    2 +-
 block/qcow2.c    |    2 +-
 block/vdi.c      |   11 +++++--
 hw/ide/core.c    |    8 ++--
 qemu-nbd.c       |   34 ++++++++++++++++------
 10 files changed, 99 insertions(+), 112 deletions(-)

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Qemu-devel] [PATCH 01/11] block: Remove semicolon in BDRV_SECTOR_MASK macro
  2010-05-07 15:13 [Qemu-devel] [PULL 00/11] Block patches Kevin Wolf
@ 2010-05-07 15:13 ` Kevin Wolf
  2010-05-07 15:13 ` [Qemu-devel] [PATCH 02/11] qemu-nbd: Improve error reporting Kevin Wolf
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Kevin Wolf @ 2010-05-07 15:13 UTC (permalink / raw)
  To: aliguori; +Cc: kwolf, qemu-devel

From: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>

Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/block.h b/block.h
index f87d24e..278259c 100644
--- a/block.h
+++ b/block.h
@@ -38,7 +38,7 @@ typedef struct QEMUSnapshotInfo {
 
 #define BDRV_SECTOR_BITS   9
 #define BDRV_SECTOR_SIZE   (1 << BDRV_SECTOR_BITS)
-#define BDRV_SECTOR_MASK   ~(BDRV_SECTOR_SIZE - 1);
+#define BDRV_SECTOR_MASK   ~(BDRV_SECTOR_SIZE - 1)
 
 typedef enum {
     BDRV_ACTION_REPORT, BDRV_ACTION_IGNORE, BDRV_ACTION_STOP
-- 
1.6.6.1

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [Qemu-devel] [PATCH 02/11] qemu-nbd: Improve error reporting
  2010-05-07 15:13 [Qemu-devel] [PULL 00/11] Block patches Kevin Wolf
  2010-05-07 15:13 ` [Qemu-devel] [PATCH 01/11] block: Remove semicolon in BDRV_SECTOR_MASK macro Kevin Wolf
@ 2010-05-07 15:13 ` Kevin Wolf
  2010-05-07 15:13 ` [Qemu-devel] [PATCH 03/11] cloop: use pread Kevin Wolf
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Kevin Wolf @ 2010-05-07 15:13 UTC (permalink / raw)
  To: aliguori; +Cc: kwolf, qemu-devel

From: Ryota Ozaki <ozaki.ryota@gmail.com>

- use err(3) instead of errx(3) if errno is available
  to report why failed
- let fail prior to daemon(3) if opening a nbd file
  is likely to fail after daemonizing to avoid silent
  failure exit
- add missing 'ret = 1' when unix_socket_outgoing failed

Signed-off-by: Ryota Ozaki <ozaki.ryota@gmail.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 qemu-nbd.c |   34 ++++++++++++++++++++++++----------
 1 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/qemu-nbd.c b/qemu-nbd.c
index 25aa913..4e607cf 100644
--- a/qemu-nbd.c
+++ b/qemu-nbd.c
@@ -112,9 +112,12 @@ static int find_partition(BlockDriverState *bs, int partition,
     uint8_t data[512];
     int i;
     int ext_partnum = 4;
+    int ret;
 
-    if (bdrv_read(bs, 0, data, 1))
-        errx(EXIT_FAILURE, "error while reading");
+    if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
+        errno = -ret;
+        err(EXIT_FAILURE, "error while reading");
+    }
 
     if (data[510] != 0x55 || data[511] != 0xaa) {
         errno = -EINVAL;
@@ -132,8 +135,10 @@ static int find_partition(BlockDriverState *bs, int partition,
             uint8_t data1[512];
             int j;
 
-            if (bdrv_read(bs, mbr[i].start_sector_abs, data1, 1))
-                errx(EXIT_FAILURE, "error while reading");
+            if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
+                errno = -ret;
+                err(EXIT_FAILURE, "error while reading");
+            }
 
             for (j = 0; j < 4; j++) {
                 read_partition(&data1[446 + 16 * j], &ext[j]);
@@ -316,7 +321,7 @@ int main(int argc, char **argv)
     if (disconnect) {
         fd = open(argv[optind], O_RDWR);
         if (fd == -1)
-            errx(EXIT_FAILURE, "Cannot open %s", argv[optind]);
+            err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
 
         nbd_disconnect(fd);
 
@@ -333,23 +338,30 @@ int main(int argc, char **argv)
     if (bs == NULL)
         return 1;
 
-    if (bdrv_open(bs, argv[optind], flags, NULL) < 0)
-        return 1;
+    if ((ret = bdrv_open(bs, argv[optind], flags, NULL)) < 0) {
+        errno = -ret;
+        err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]);
+    }
 
     fd_size = bs->total_sectors * 512;
 
     if (partition != -1 &&
         find_partition(bs, partition, &dev_offset, &fd_size))
-        errx(EXIT_FAILURE, "Could not find partition %d", partition);
+        err(EXIT_FAILURE, "Could not find partition %d", partition);
 
     if (device) {
         pid_t pid;
         int sock;
 
+        /* want to fail before daemonizing */
+        if (access(device, R_OK|W_OK) == -1) {
+            err(EXIT_FAILURE, "Could not access '%s'", device);
+        }
+
         if (!verbose) {
             /* detach client and server */
             if (daemon(0, 0) == -1) {
-                errx(EXIT_FAILURE, "Failed to daemonize");
+                err(EXIT_FAILURE, "Failed to daemonize");
             }
         }
 
@@ -372,8 +384,10 @@ int main(int argc, char **argv)
             do {
                 sock = unix_socket_outgoing(socket);
                 if (sock == -1) {
-                    if (errno != ENOENT && errno != ECONNREFUSED)
+                    if (errno != ENOENT && errno != ECONNREFUSED) {
+                        ret = 1;
                         goto out;
+                    }
                     sleep(1);	/* wait children */
                 }
             } while (sock == -1);
-- 
1.6.6.1

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [Qemu-devel] [PATCH 03/11] cloop: use pread
  2010-05-07 15:13 [Qemu-devel] [PULL 00/11] Block patches Kevin Wolf
  2010-05-07 15:13 ` [Qemu-devel] [PATCH 01/11] block: Remove semicolon in BDRV_SECTOR_MASK macro Kevin Wolf
  2010-05-07 15:13 ` [Qemu-devel] [PATCH 02/11] qemu-nbd: Improve error reporting Kevin Wolf
@ 2010-05-07 15:13 ` Kevin Wolf
  2010-05-07 15:14 ` [Qemu-devel] [PATCH 04/11] cloop: use qemu block API Kevin Wolf
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Kevin Wolf @ 2010-05-07 15:13 UTC (permalink / raw)
  To: aliguori; +Cc: kwolf, qemu-devel

From: Christoph Hellwig <hch@lst.de>

Use pread instead of lseek + read in preparation of using the qemu
block API.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/cloop.c |   32 +++++++++++++++++---------------
 1 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/block/cloop.c b/block/cloop.c
index e4f995b..9fe2a42 100644
--- a/block/cloop.c
+++ b/block/cloop.c
@@ -62,23 +62,22 @@ static int cloop_open(BlockDriverState *bs, const char *filename, int flags)
     bs->read_only = 1;
 
     /* read header */
-    if(lseek(s->fd,128,SEEK_SET)<0) {
-cloop_close:
-	close(s->fd);
-	return -1;
+    if (pread(s->fd, &s->block_size, 4, 128) < 4) {
+        goto cloop_close;
     }
-    if(read(s->fd,&s->block_size,4)<4)
-	goto cloop_close;
-    s->block_size=be32_to_cpu(s->block_size);
-    if(read(s->fd,&s->n_blocks,4)<4)
-	goto cloop_close;
-    s->n_blocks=be32_to_cpu(s->n_blocks);
+    s->block_size = be32_to_cpu(s->block_size);
+
+    if (pread(s->fd, &s->n_blocks, 4, 128 + 4) < 4) {
+        goto cloop_close;
+    }
+    s->n_blocks = be32_to_cpu(s->n_blocks);
 
     /* read offsets */
-    offsets_size=s->n_blocks*sizeof(uint64_t);
-    s->offsets=(uint64_t*)qemu_malloc(offsets_size);
-    if(read(s->fd,s->offsets,offsets_size)<offsets_size)
+    offsets_size = s->n_blocks * sizeof(uint64_t);
+    s->offsets = qemu_malloc(offsets_size);
+    if (pread(s->fd, s->offsets, offsets_size, 128 + 4 + 4) < offsets_size) {
 	goto cloop_close;
+    }
     for(i=0;i<s->n_blocks;i++) {
 	s->offsets[i]=be64_to_cpu(s->offsets[i]);
 	if(i>0) {
@@ -98,6 +97,10 @@ cloop_close:
     s->sectors_per_block = s->block_size/512;
     bs->total_sectors = s->n_blocks*s->sectors_per_block;
     return 0;
+
+cloop_close:
+    close(s->fd);
+    return -1;
 }
 
 static inline int cloop_read_block(BDRVCloopState *s,int block_num)
@@ -106,8 +109,7 @@ static inline int cloop_read_block(BDRVCloopState *s,int block_num)
 	int ret;
         uint32_t bytes = s->offsets[block_num+1]-s->offsets[block_num];
 
-	lseek(s->fd, s->offsets[block_num], SEEK_SET);
-        ret = read(s->fd, s->compressed_block, bytes);
+        ret = pread(s->fd, s->compressed_block, bytes, s->offsets[block_num]);
         if (ret != bytes)
             return -1;
 
-- 
1.6.6.1

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [Qemu-devel] [PATCH 04/11] cloop: use qemu block API
  2010-05-07 15:13 [Qemu-devel] [PULL 00/11] Block patches Kevin Wolf
                   ` (2 preceding siblings ...)
  2010-05-07 15:13 ` [Qemu-devel] [PATCH 03/11] cloop: use pread Kevin Wolf
@ 2010-05-07 15:14 ` Kevin Wolf
  2010-05-07 15:14 ` [Qemu-devel] [PATCH 05/11] ide: Fix ide_dma_cancel Kevin Wolf
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Kevin Wolf @ 2010-05-07 15:14 UTC (permalink / raw)
  To: aliguori; +Cc: kwolf, qemu-devel

From: Christoph Hellwig <hch@lst.de>

Use bdrv_pwrite to access the backing device instead of pread, and
convert the driver to implementing the bdrv_open method which gives
it an already opened BlockDriverState for the underlying device.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/cloop.c |   26 ++++++++++++--------------
 1 files changed, 12 insertions(+), 14 deletions(-)

diff --git a/block/cloop.c b/block/cloop.c
index 9fe2a42..fe015c4 100644
--- a/block/cloop.c
+++ b/block/cloop.c
@@ -27,7 +27,6 @@
 #include <zlib.h>
 
 typedef struct BDRVCloopState {
-    int fd;
     uint32_t block_size;
     uint32_t n_blocks;
     uint64_t* offsets;
@@ -51,23 +50,20 @@ static int cloop_probe(const uint8_t *buf, int buf_size, const char *filename)
     return 0;
 }
 
-static int cloop_open(BlockDriverState *bs, const char *filename, int flags)
+static int cloop_open(BlockDriverState *bs, int flags)
 {
     BDRVCloopState *s = bs->opaque;
     uint32_t offsets_size,max_compressed_block_size=1,i;
 
-    s->fd = open(filename, O_RDONLY | O_BINARY);
-    if (s->fd < 0)
-        return -errno;
     bs->read_only = 1;
 
     /* read header */
-    if (pread(s->fd, &s->block_size, 4, 128) < 4) {
+    if (bdrv_pread(bs->file, 128, &s->block_size, 4) < 4) {
         goto cloop_close;
     }
     s->block_size = be32_to_cpu(s->block_size);
 
-    if (pread(s->fd, &s->n_blocks, 4, 128 + 4) < 4) {
+    if (bdrv_pread(bs->file, 128 + 4, &s->n_blocks, 4) < 4) {
         goto cloop_close;
     }
     s->n_blocks = be32_to_cpu(s->n_blocks);
@@ -75,7 +71,8 @@ static int cloop_open(BlockDriverState *bs, const char *filename, int flags)
     /* read offsets */
     offsets_size = s->n_blocks * sizeof(uint64_t);
     s->offsets = qemu_malloc(offsets_size);
-    if (pread(s->fd, s->offsets, offsets_size, 128 + 4 + 4) < offsets_size) {
+    if (bdrv_pread(bs->file, 128 + 4 + 4, s->offsets, offsets_size) <
+            offsets_size) {
 	goto cloop_close;
     }
     for(i=0;i<s->n_blocks;i++) {
@@ -99,17 +96,19 @@ static int cloop_open(BlockDriverState *bs, const char *filename, int flags)
     return 0;
 
 cloop_close:
-    close(s->fd);
     return -1;
 }
 
-static inline int cloop_read_block(BDRVCloopState *s,int block_num)
+static inline int cloop_read_block(BlockDriverState *bs, int block_num)
 {
+    BDRVCloopState *s = bs->opaque;
+
     if(s->current_block != block_num) {
 	int ret;
         uint32_t bytes = s->offsets[block_num+1]-s->offsets[block_num];
 
-        ret = pread(s->fd, s->compressed_block, bytes, s->offsets[block_num]);
+        ret = bdrv_pread(bs->file, s->offsets[block_num], s->compressed_block,
+                         bytes);
         if (ret != bytes)
             return -1;
 
@@ -138,7 +137,7 @@ static int cloop_read(BlockDriverState *bs, int64_t sector_num,
     for(i=0;i<nb_sectors;i++) {
 	uint32_t sector_offset_in_block=((sector_num+i)%s->sectors_per_block),
 	    block_num=(sector_num+i)/s->sectors_per_block;
-	if(cloop_read_block(s, block_num) != 0)
+	if(cloop_read_block(bs, block_num) != 0)
 	    return -1;
 	memcpy(buf+i*512,s->uncompressed_block+sector_offset_in_block*512,512);
     }
@@ -148,7 +147,6 @@ static int cloop_read(BlockDriverState *bs, int64_t sector_num,
 static void cloop_close(BlockDriverState *bs)
 {
     BDRVCloopState *s = bs->opaque;
-    close(s->fd);
     if(s->n_blocks>0)
 	free(s->offsets);
     free(s->compressed_block);
@@ -160,7 +158,7 @@ static BlockDriver bdrv_cloop = {
     .format_name	= "cloop",
     .instance_size	= sizeof(BDRVCloopState),
     .bdrv_probe		= cloop_probe,
-    .bdrv_file_open	= cloop_open,
+    .bdrv_open		= cloop_open,
     .bdrv_read		= cloop_read,
     .bdrv_close		= cloop_close,
 };
-- 
1.6.6.1

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [Qemu-devel] [PATCH 05/11] ide: Fix ide_dma_cancel
  2010-05-07 15:13 [Qemu-devel] [PULL 00/11] Block patches Kevin Wolf
                   ` (3 preceding siblings ...)
  2010-05-07 15:14 ` [Qemu-devel] [PATCH 04/11] cloop: use qemu block API Kevin Wolf
@ 2010-05-07 15:14 ` Kevin Wolf
  2010-05-07 15:14 ` [Qemu-devel] [PATCH 06/11] bochs: use pread Kevin Wolf
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Kevin Wolf @ 2010-05-07 15:14 UTC (permalink / raw)
  To: aliguori; +Cc: kwolf, qemu-devel

When cancelling a request, bdrv_aio_cancel may decide that it waits for
completion of a request rather than for cancellation. IDE therefore can't
abandon its DMA status before calling bdrv_aio_cancel; otherwise the callback
of a completed request would use invalid data.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 hw/ide/core.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/hw/ide/core.c b/hw/ide/core.c
index 0757528..3cd55e3 100644
--- a/hw/ide/core.c
+++ b/hw/ide/core.c
@@ -2838,10 +2838,6 @@ static void ide_dma_restart(IDEState *s, int is_read)
 void ide_dma_cancel(BMDMAState *bm)
 {
     if (bm->status & BM_STATUS_DMAING) {
-        bm->status &= ~BM_STATUS_DMAING;
-        /* cancel DMA request */
-        bm->unit = -1;
-        bm->dma_cb = NULL;
         if (bm->aiocb) {
 #ifdef DEBUG_AIO
             printf("aio_cancel\n");
@@ -2849,6 +2845,10 @@ void ide_dma_cancel(BMDMAState *bm)
             bdrv_aio_cancel(bm->aiocb);
             bm->aiocb = NULL;
         }
+        bm->status &= ~BM_STATUS_DMAING;
+        /* cancel DMA request */
+        bm->unit = -1;
+        bm->dma_cb = NULL;
     }
 }
 
-- 
1.6.6.1

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [Qemu-devel] [PATCH 06/11] bochs: use pread
  2010-05-07 15:13 [Qemu-devel] [PULL 00/11] Block patches Kevin Wolf
                   ` (4 preceding siblings ...)
  2010-05-07 15:14 ` [Qemu-devel] [PATCH 05/11] ide: Fix ide_dma_cancel Kevin Wolf
@ 2010-05-07 15:14 ` Kevin Wolf
  2010-05-07 15:14 ` [Qemu-devel] [PATCH 07/11] bochs: use qemu block API Kevin Wolf
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Kevin Wolf @ 2010-05-07 15:14 UTC (permalink / raw)
  To: aliguori; +Cc: kwolf, qemu-devel

From: Christoph Hellwig <hch@lst.de>

Use pread instead of lseek + read in preparation of using the qemu
block API.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/bochs.c |   63 ++++++++++++++++++--------------------------------------
 1 files changed, 20 insertions(+), 43 deletions(-)

diff --git a/block/bochs.c b/block/bochs.c
index e952670..b54f54d 100644
--- a/block/bochs.c
+++ b/block/bochs.c
@@ -125,7 +125,7 @@ static int bochs_open(BlockDriverState *bs, const char *filename, int flags)
 
     s->fd = fd;
 
-    if (read(fd, &bochs, sizeof(bochs)) != sizeof(bochs)) {
+    if (pread(fd, &bochs, sizeof(bochs), 0) != sizeof(bochs)) {
         goto fail;
     }
 
@@ -144,14 +144,10 @@ static int bochs_open(BlockDriverState *bs, const char *filename, int flags)
       bs->total_sectors = le64_to_cpu(bochs.extra.redolog.disk) / 512;
     }
 
-    if (lseek(s->fd, le32_to_cpu(bochs.header), SEEK_SET) == (off_t)-1) {
-        goto fail;
-    }
-
     s->catalog_size = le32_to_cpu(bochs.extra.redolog.catalog);
     s->catalog_bitmap = qemu_malloc(s->catalog_size * 4);
-    if (read(s->fd, s->catalog_bitmap, s->catalog_size * 4) !=
-	s->catalog_size * 4)
+    if (pread(s->fd, s->catalog_bitmap, s->catalog_size * 4,
+              le32_to_cpu(bochs.header)) != s->catalog_size * 4)
 	goto fail;
     for (i = 0; i < s->catalog_size; i++)
 	le32_to_cpus(&s->catalog_bitmap[i]);
@@ -169,54 +165,35 @@ static int bochs_open(BlockDriverState *bs, const char *filename, int flags)
     return -1;
 }
 
-static inline int seek_to_sector(BlockDriverState *bs, int64_t sector_num)
+static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
 {
     BDRVBochsState *s = bs->opaque;
     int64_t offset = sector_num * 512;
-    int64_t extent_index, extent_offset, bitmap_offset, block_offset;
+    int64_t extent_index, extent_offset, bitmap_offset;
     char bitmap_entry;
 
     // seek to sector
     extent_index = offset / s->extent_size;
     extent_offset = (offset % s->extent_size) / 512;
 
-    if (s->catalog_bitmap[extent_index] == 0xffffffff)
-    {
-//	fprintf(stderr, "page not allocated [%x - %x:%x]\n",
-//	    sector_num, extent_index, extent_offset);
-	return -1; // not allocated
+    if (s->catalog_bitmap[extent_index] == 0xffffffff) {
+	return -1; /* not allocated */
     }
 
     bitmap_offset = s->data_offset + (512 * s->catalog_bitmap[extent_index] *
 	(s->extent_blocks + s->bitmap_blocks));
-    block_offset = bitmap_offset + (512 * (s->bitmap_blocks + extent_offset));
-
-//    fprintf(stderr, "sect: %x [ext i: %x o: %x] -> %x bitmap: %x block: %x\n",
-//	sector_num, extent_index, extent_offset,
-//	le32_to_cpu(s->catalog_bitmap[extent_index]),
-//	bitmap_offset, block_offset);
-
-    // read in bitmap for current extent
-    if (lseek(s->fd, bitmap_offset + (extent_offset / 8), SEEK_SET) ==
-        (off_t)-1) {
-        return -1;
-    }
 
-    if (read(s->fd, &bitmap_entry, 1) != 1)
+    /* read in bitmap for current extent */
+    if (pread(s->fd, &bitmap_entry, 1, bitmap_offset + (extent_offset / 8))
+            != 1) {
         return -1;
-
-    if (!((bitmap_entry >> (extent_offset % 8)) & 1))
-    {
-//	fprintf(stderr, "sector (%x) in bitmap not allocated\n",
-//	    sector_num);
-	return -1; // not allocated
     }
 
-    if (lseek(s->fd, block_offset, SEEK_SET) == (off_t)-1) {
-        return -1;
+    if (!((bitmap_entry >> (extent_offset % 8)) & 1)) {
+	return -1; /* not allocated */
     }
 
-    return 0;
+    return bitmap_offset + (512 * (s->bitmap_blocks + extent_offset));
 }
 
 static int bochs_read(BlockDriverState *bs, int64_t sector_num,
@@ -226,13 +203,13 @@ static int bochs_read(BlockDriverState *bs, int64_t sector_num,
     int ret;
 
     while (nb_sectors > 0) {
-	if (!seek_to_sector(bs, sector_num))
-	{
-	    ret = read(s->fd, buf, 512);
-	    if (ret != 512)
-		return -1;
-	}
-	else
+        int64_t block_offset = seek_to_sector(bs, sector_num);
+        if (block_offset >= 0) {
+            ret = pread(s->fd, buf, 512, block_offset);
+            if (ret != 512) {
+                return -1;
+            }
+        } else
             memset(buf, 0, 512);
         nb_sectors--;
         sector_num++;
-- 
1.6.6.1

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [Qemu-devel] [PATCH 07/11] bochs: use qemu block API
  2010-05-07 15:13 [Qemu-devel] [PULL 00/11] Block patches Kevin Wolf
                   ` (5 preceding siblings ...)
  2010-05-07 15:14 ` [Qemu-devel] [PATCH 06/11] bochs: use pread Kevin Wolf
@ 2010-05-07 15:14 ` Kevin Wolf
  2010-05-07 15:14 ` [Qemu-devel] [PATCH 08/11] block: Avoid unchecked casts for AIOCBs Kevin Wolf
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Kevin Wolf @ 2010-05-07 15:14 UTC (permalink / raw)
  To: aliguori; +Cc: kwolf, qemu-devel

From: Christoph Hellwig <hch@lst.de>

Use bdrv_pwrite to access the backing device instead of pread, and
convert the driver to implementing the bdrv_open method which gives
it an already opened BlockDriverState for the underlying device.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/bochs.c |   30 +++++++++---------------------
 1 files changed, 9 insertions(+), 21 deletions(-)

diff --git a/block/bochs.c b/block/bochs.c
index b54f54d..5fe2fa3 100644
--- a/block/bochs.c
+++ b/block/bochs.c
@@ -80,8 +80,6 @@ struct bochs_header {
 };
 
 typedef struct BDRVBochsState {
-    int fd;
-
     uint32_t *catalog_bitmap;
     int catalog_size;
 
@@ -109,23 +107,16 @@ static int bochs_probe(const uint8_t *buf, int buf_size, const char *filename)
     return 0;
 }
 
-static int bochs_open(BlockDriverState *bs, const char *filename, int flags)
+static int bochs_open(BlockDriverState *bs, int flags)
 {
     BDRVBochsState *s = bs->opaque;
-    int fd, i;
+    int i;
     struct bochs_header bochs;
     struct bochs_header_v1 header_v1;
 
-    fd = open(filename, O_RDONLY | O_BINARY);
-    if (fd < 0) {
-        return -1;
-    }
-
     bs->read_only = 1; // no write support yet
 
-    s->fd = fd;
-
-    if (pread(fd, &bochs, sizeof(bochs), 0) != sizeof(bochs)) {
+    if (bdrv_pread(bs->file, 0, &bochs, sizeof(bochs)) != sizeof(bochs)) {
         goto fail;
     }
 
@@ -146,8 +137,8 @@ static int bochs_open(BlockDriverState *bs, const char *filename, int flags)
 
     s->catalog_size = le32_to_cpu(bochs.extra.redolog.catalog);
     s->catalog_bitmap = qemu_malloc(s->catalog_size * 4);
-    if (pread(s->fd, s->catalog_bitmap, s->catalog_size * 4,
-              le32_to_cpu(bochs.header)) != s->catalog_size * 4)
+    if (bdrv_pread(bs->file, le32_to_cpu(bochs.header), s->catalog_bitmap,
+                   s->catalog_size * 4) != s->catalog_size * 4)
 	goto fail;
     for (i = 0; i < s->catalog_size; i++)
 	le32_to_cpus(&s->catalog_bitmap[i]);
@@ -161,7 +152,6 @@ static int bochs_open(BlockDriverState *bs, const char *filename, int flags)
 
     return 0;
  fail:
-    close(fd);
     return -1;
 }
 
@@ -184,8 +174,8 @@ static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
 	(s->extent_blocks + s->bitmap_blocks));
 
     /* read in bitmap for current extent */
-    if (pread(s->fd, &bitmap_entry, 1, bitmap_offset + (extent_offset / 8))
-            != 1) {
+    if (bdrv_pread(bs->file, bitmap_offset + (extent_offset / 8),
+                   &bitmap_entry, 1) != 1) {
         return -1;
     }
 
@@ -199,13 +189,12 @@ static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
 static int bochs_read(BlockDriverState *bs, int64_t sector_num,
                     uint8_t *buf, int nb_sectors)
 {
-    BDRVBochsState *s = bs->opaque;
     int ret;
 
     while (nb_sectors > 0) {
         int64_t block_offset = seek_to_sector(bs, sector_num);
         if (block_offset >= 0) {
-            ret = pread(s->fd, buf, 512, block_offset);
+            ret = bdrv_pread(bs->file, block_offset, buf, 512);
             if (ret != 512) {
                 return -1;
             }
@@ -222,14 +211,13 @@ static void bochs_close(BlockDriverState *bs)
 {
     BDRVBochsState *s = bs->opaque;
     qemu_free(s->catalog_bitmap);
-    close(s->fd);
 }
 
 static BlockDriver bdrv_bochs = {
     .format_name	= "bochs",
     .instance_size	= sizeof(BDRVBochsState),
     .bdrv_probe		= bochs_probe,
-    .bdrv_file_open	= bochs_open,
+    .bdrv_open		= bochs_open,
     .bdrv_read		= bochs_read,
     .bdrv_close		= bochs_close,
 };
-- 
1.6.6.1

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [Qemu-devel] [PATCH 08/11] block: Avoid unchecked casts for AIOCBs
  2010-05-07 15:13 [Qemu-devel] [PULL 00/11] Block patches Kevin Wolf
                   ` (6 preceding siblings ...)
  2010-05-07 15:14 ` [Qemu-devel] [PATCH 07/11] bochs: use qemu block API Kevin Wolf
@ 2010-05-07 15:14 ` Kevin Wolf
  2010-05-07 15:14 ` [Qemu-devel] [PATCH 09/11] block: Fix protocol detection for Windows devices Kevin Wolf
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Kevin Wolf @ 2010-05-07 15:14 UTC (permalink / raw)
  To: aliguori; +Cc: kwolf, qemu-devel

Use container_of for one direction and &acb->common for the other one.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block.c          |    3 ++-
 block/blkdebug.c |    4 ++--
 block/qcow.c     |    2 +-
 block/qcow2.c    |    2 +-
 block/vdi.c      |    2 +-
 5 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/block.c b/block.c
index 48305b7..6345599 100644
--- a/block.c
+++ b/block.c
@@ -2108,7 +2108,8 @@ typedef struct BlockDriverAIOCBSync {
 
 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
 {
-    BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
+    BlockDriverAIOCBSync *acb =
+        container_of(blockacb, BlockDriverAIOCBSync, common);
     qemu_bh_delete(acb->bh);
     acb->bh = NULL;
     qemu_aio_release(acb);
diff --git a/block/blkdebug.c b/block/blkdebug.c
index bb4a91a..8325f75 100644
--- a/block/blkdebug.c
+++ b/block/blkdebug.c
@@ -320,7 +320,7 @@ static void error_callback_bh(void *opaque)
 
 static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb)
 {
-    BlkdebugAIOCB *acb = (BlkdebugAIOCB*) blockacb;
+    BlkdebugAIOCB *acb = container_of(blockacb, BlkdebugAIOCB, common);
     qemu_aio_release(acb);
 }
 
@@ -347,7 +347,7 @@ static BlockDriverAIOCB *inject_error(BlockDriverState *bs,
     acb->bh = bh;
     qemu_bh_schedule(bh);
 
-    return (BlockDriverAIOCB*) acb;
+    return &acb->common;
 }
 
 static BlockDriverAIOCB *blkdebug_aio_readv(BlockDriverState *bs,
diff --git a/block/qcow.c b/block/qcow.c
index 2883c40..449858f 100644
--- a/block/qcow.c
+++ b/block/qcow.c
@@ -502,7 +502,7 @@ typedef struct QCowAIOCB {
 
 static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
 {
-    QCowAIOCB *acb = (QCowAIOCB *)blockacb;
+    QCowAIOCB *acb = container_of(blockacb, QCowAIOCB, common);
     if (acb->hd_aiocb)
         bdrv_aio_cancel(acb->hd_aiocb);
     qemu_aio_release(acb);
diff --git a/block/qcow2.c b/block/qcow2.c
index 21ed6f8..40bd32a 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -340,7 +340,7 @@ typedef struct QCowAIOCB {
 
 static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
 {
-    QCowAIOCB *acb = (QCowAIOCB *)blockacb;
+    QCowAIOCB *acb = container_of(blockacb, QCowAIOCB, common);
     if (acb->hd_aiocb)
         bdrv_aio_cancel(acb->hd_aiocb);
     qemu_aio_release(acb);
diff --git a/block/vdi.c b/block/vdi.c
index 1d257b4..2b4d2c2 100644
--- a/block/vdi.c
+++ b/block/vdi.c
@@ -469,7 +469,7 @@ static int vdi_is_allocated(BlockDriverState *bs, int64_t sector_num,
 static void vdi_aio_cancel(BlockDriverAIOCB *blockacb)
 {
     /* TODO: This code is untested. How can I get it executed? */
-    VdiAIOCB *acb = (VdiAIOCB *)blockacb;
+    VdiAIOCB *acb = container_of(blockacb, VdiAIOCB, common);
     logout("\n");
     if (acb->hd_aiocb) {
         bdrv_aio_cancel(acb->hd_aiocb);
-- 
1.6.6.1

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [Qemu-devel] [PATCH 09/11] block: Fix protocol detection for Windows devices
  2010-05-07 15:13 [Qemu-devel] [PULL 00/11] Block patches Kevin Wolf
                   ` (7 preceding siblings ...)
  2010-05-07 15:14 ` [Qemu-devel] [PATCH 08/11] block: Avoid unchecked casts for AIOCBs Kevin Wolf
@ 2010-05-07 15:14 ` Kevin Wolf
  2010-05-07 15:14 ` [Qemu-devel] [PATCH 10/11] block: Fix bdrv_commit Kevin Wolf
  2010-05-07 15:14 ` [Qemu-devel] [PATCH 11/11] block/vdi: Allow disk images of size 0 Kevin Wolf
  10 siblings, 0 replies; 12+ messages in thread
From: Kevin Wolf @ 2010-05-07 15:14 UTC (permalink / raw)
  To: aliguori; +Cc: kwolf, qemu-devel

We can't assume the file protocol for Windows devices, they need the same
detection as other files for which an explicit protocol is not specified.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/block.c b/block.c
index 6345599..977d01f 100644
--- a/block.c
+++ b/block.c
@@ -287,16 +287,18 @@ static BlockDriver *find_protocol(const char *filename)
     char protocol[128];
     int len;
     const char *p;
+    int is_drive;
 
     /* TODO Drivers without bdrv_file_open must be specified explicitly */
 
 #ifdef _WIN32
-    if (is_windows_drive(filename) ||
-        is_windows_drive_prefix(filename))
-        return bdrv_find_format("file");
+    is_drive = is_windows_drive(filename) ||
+        is_windows_drive_prefix(filename);
+#else
+    is_drive = 0;
 #endif
     p = strchr(filename, ':');
-    if (!p) {
+    if (!p || is_drive) {
         drv1 = find_hdev_driver(filename);
         if (!drv1) {
             drv1 = bdrv_find_format("file");
-- 
1.6.6.1

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [Qemu-devel] [PATCH 10/11] block: Fix bdrv_commit
  2010-05-07 15:13 [Qemu-devel] [PULL 00/11] Block patches Kevin Wolf
                   ` (8 preceding siblings ...)
  2010-05-07 15:14 ` [Qemu-devel] [PATCH 09/11] block: Fix protocol detection for Windows devices Kevin Wolf
@ 2010-05-07 15:14 ` Kevin Wolf
  2010-05-07 15:14 ` [Qemu-devel] [PATCH 11/11] block/vdi: Allow disk images of size 0 Kevin Wolf
  10 siblings, 0 replies; 12+ messages in thread
From: Kevin Wolf @ 2010-05-07 15:14 UTC (permalink / raw)
  To: aliguori; +Cc: kwolf, qemu-devel

When reopening the image, don't guess the driver, but use the same driver as
was used before. This is important if the format=... option was used for that
image.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/block.c b/block.c
index 977d01f..c134c2b 100644
--- a/block.c
+++ b/block.c
@@ -701,12 +701,12 @@ int bdrv_commit(BlockDriverState *bs)
         bdrv_delete(bs->backing_hd);
         bs->backing_hd = NULL;
         bs_rw = bdrv_new("");
-        rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, NULL);
+        rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, drv);
         if (rw_ret < 0) {
             bdrv_delete(bs_rw);
             /* try to re-open read-only */
             bs_ro = bdrv_new("");
-            ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, NULL);
+            ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
             if (ret < 0) {
                 bdrv_delete(bs_ro);
                 /* drive not functional anymore */
@@ -758,7 +758,7 @@ ro_cleanup:
         bdrv_delete(bs->backing_hd);
         bs->backing_hd = NULL;
         bs_ro = bdrv_new("");
-        ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, NULL);
+        ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
         if (ret < 0) {
             bdrv_delete(bs_ro);
             /* drive not functional anymore */
-- 
1.6.6.1

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [Qemu-devel] [PATCH 11/11] block/vdi: Allow disk images of size 0
  2010-05-07 15:13 [Qemu-devel] [PULL 00/11] Block patches Kevin Wolf
                   ` (9 preceding siblings ...)
  2010-05-07 15:14 ` [Qemu-devel] [PATCH 10/11] block: Fix bdrv_commit Kevin Wolf
@ 2010-05-07 15:14 ` Kevin Wolf
  10 siblings, 0 replies; 12+ messages in thread
From: Kevin Wolf @ 2010-05-07 15:14 UTC (permalink / raw)
  To: aliguori; +Cc: kwolf, qemu-devel

From: Stefan Weil <weil@mail.berlios.de>

Even it is not very useful, users may create images of size 0.

Without the special option CONFIG_ZERO_MALLOC, qemu_mallocz
aborts execution when it is told to allocate 0 bytes,
so avoid this kind of call.

Cc: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Stefan Weil <weil@mail.berlios.de>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/vdi.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/block/vdi.c b/block/vdi.c
index 2b4d2c2..3ea4103 100644
--- a/block/vdi.c
+++ b/block/vdi.c
@@ -435,7 +435,9 @@ static int vdi_open(BlockDriverState *bs, int flags)
 
     bmap_size = header.blocks_in_image * sizeof(uint32_t);
     bmap_size = (bmap_size + SECTOR_SIZE - 1) / SECTOR_SIZE;
-    s->bmap = qemu_malloc(bmap_size * SECTOR_SIZE);
+    if (bmap_size > 0) {
+        s->bmap = qemu_malloc(bmap_size * SECTOR_SIZE);
+    }
     if (bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap, bmap_size) < 0) {
         goto fail_free_bmap;
     }
@@ -857,7 +859,10 @@ static int vdi_create(const char *filename, QEMUOptionParameter *options)
         result = -errno;
     }
 
-    bmap = (uint32_t *)qemu_mallocz(bmap_size);
+    bmap = NULL;
+    if (bmap_size > 0) {
+        bmap = (uint32_t *)qemu_mallocz(bmap_size);
+    }
     for (i = 0; i < blocks; i++) {
         if (image_type == VDI_TYPE_STATIC) {
             bmap[i] = i;
-- 
1.6.6.1

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2010-05-07 15:15 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-05-07 15:13 [Qemu-devel] [PULL 00/11] Block patches Kevin Wolf
2010-05-07 15:13 ` [Qemu-devel] [PATCH 01/11] block: Remove semicolon in BDRV_SECTOR_MASK macro Kevin Wolf
2010-05-07 15:13 ` [Qemu-devel] [PATCH 02/11] qemu-nbd: Improve error reporting Kevin Wolf
2010-05-07 15:13 ` [Qemu-devel] [PATCH 03/11] cloop: use pread Kevin Wolf
2010-05-07 15:14 ` [Qemu-devel] [PATCH 04/11] cloop: use qemu block API Kevin Wolf
2010-05-07 15:14 ` [Qemu-devel] [PATCH 05/11] ide: Fix ide_dma_cancel Kevin Wolf
2010-05-07 15:14 ` [Qemu-devel] [PATCH 06/11] bochs: use pread Kevin Wolf
2010-05-07 15:14 ` [Qemu-devel] [PATCH 07/11] bochs: use qemu block API Kevin Wolf
2010-05-07 15:14 ` [Qemu-devel] [PATCH 08/11] block: Avoid unchecked casts for AIOCBs Kevin Wolf
2010-05-07 15:14 ` [Qemu-devel] [PATCH 09/11] block: Fix protocol detection for Windows devices Kevin Wolf
2010-05-07 15:14 ` [Qemu-devel] [PATCH 10/11] block: Fix bdrv_commit Kevin Wolf
2010-05-07 15:14 ` [Qemu-devel] [PATCH 11/11] block/vdi: Allow disk images of size 0 Kevin Wolf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).