Skip to contentSkip to conversation

Asynchronous reliable and configurable cache flush

PATCH In review Ian Jackson 4 participants 15 messages Apr 1, 2008 – Apr 4, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Asynchronous reliable and configurable cache flush

This patch does the following, as previously discussed here:
* Makes guests able to control the caching of emulated IDE disks using the SETFEATURES 0x02 and 0x82 commands.
* Introduces a new block entrypoint bdrv_aio_flush for asynchronous flushing of the write cache.
* Makes IDE FLUSH_CACHE and FLUSH_CACHE_EXT use the new asynchronous function so that guests can continue to execute, receive timer interrupts, etc.
* Does likewise for the SCSI FLUSH CACHE command.

I think this should address all of the comments.

Regards,
Ian.

block-qcow.c +8 -0
index 59f1c66..4798ade 100644
@@ -725,6 +725,13 @@ static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
     qemu_aio_release(acb);
 }
 
+static BlockDriverAIOCB *qcow_aio_flush(BlockDriverState *bs,
+        BlockDriverCompletionFunc *cb, void *opaque)
+{
+    BDRVQcowState *s = bs->opaque;
+    return bdrv_aio_flush(s->hd, cb, opaque);
+}
+
 static void qcow_close(BlockDriverState *bs)
 {
     BDRVQcowState *s = bs->opaque;
@@ -913,6 +920,7 @@ BlockDriver bdrv_qcow = {
     .bdrv_aio_read = qcow_aio_read,
     .bdrv_aio_write = qcow_aio_write,
     .bdrv_aio_cancel = qcow_aio_cancel,
+    .bdrv_aio_flush = qcow_aio_flush,
     .aiocb_size = sizeof(QCowAIOCB),
     .bdrv_write_compressed = qcow_write_compressed,
     .bdrv_get_info = qcow_get_info,
block-qcow2.c +8 -0
index 9bad090..9c63a63 100644
@@ -1007,6 +1007,13 @@ static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
     qemu_aio_release(acb);
 }
 
+static BlockDriverAIOCB *qcow_aio_flush(BlockDriverState *bs,
+        BlockDriverCompletionFunc *cb, void *opaque)
+{
+    BDRVQcowState *s = bs->opaque;
+    return bdrv_aio_flush(s->hd, cb, opaque);
+}
+
 static void qcow_close(BlockDriverState *bs)
 {
     BDRVQcowState *s = bs->opaque;
@@ -2241,6 +2248,7 @@ BlockDriver bdrv_qcow2 = {
     .bdrv_aio_read = qcow_aio_read,
     .bdrv_aio_write = qcow_aio_write,
     .bdrv_aio_cancel = qcow_aio_cancel,
+    .bdrv_aio_flush = qcow_aio_flush,
     .aiocb_size = sizeof(QCowAIOCB),
     .bdrv_write_compressed = qcow_write_compressed,
 
block-raw-posix.c +16 -0
index 45d0a93..71e304d 100644
@@ -429,6 +429,20 @@ static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
     return &acb->common;
 }
 
+static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
+        BlockDriverCompletionFunc *cb, void *opaque)
+{
+    RawAIOCB *acb;
+
+    acb = raw_aio_setup(bs, 0, NULL, 0, cb, opaque);
+    if (!acb)
+        return NULL;
+    if (aio_fsync(O_SYNC, &acb->aiocb) < 0) {
+        qemu_aio_release(acb);
+        return NULL;
+    }
+    return &acb->common;
+}
 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
 {
     int ret;
@@ -762,6 +776,7 @@ static int fd_open(BlockDriverState *bs)
 {
     return 0;
 }
+
 #endif
 
 #if defined(__linux__)
@@ -913,6 +928,7 @@ BlockDriver bdrv_host_device = {
     .bdrv_aio_read = raw_aio_read,
     .bdrv_aio_write = raw_aio_write,
     .bdrv_aio_cancel = raw_aio_cancel,
+    .bdrv_aio_flush = raw_aio_flush,
     .aiocb_size = sizeof(RawAIOCB),
     .bdrv_pread = raw_pread,
     .bdrv_pwrite = raw_pwrite,
block.c +39 -0
index fc32a60..348a215 100644
@@ -50,6 +50,8 @@ static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
         int64_t sector_num, const uint8_t *buf, int nb_sectors,
         BlockDriverCompletionFunc *cb, void *opaque);
+static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
+        BlockDriverCompletionFunc *cb, void *opaque);
 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
                         uint8_t *buf, int nb_sectors);
@@ -137,6 +139,8 @@ static void bdrv_register(BlockDriver *bdrv)
         bdrv->bdrv_read = bdrv_read_em;
         bdrv->bdrv_write = bdrv_write_em;
     }
+    if (!bdrv->bdrv_aio_flush)
+        bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
     bdrv->next = first_drv;
     first_drv = bdrv;
 }
@@ -1156,6 +1160,17 @@ void bdrv_aio_cancel(BlockDriverAIOCB *acb)
     drv->bdrv_aio_cancel(acb);
 }
 
+BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 
+                                 BlockDriverCompletionFunc *cb, void *opaque)
+{
+    BlockDriver *drv = bs->drv;
+
+    if (!drv)
+        return NULL;
+
+    return drv->bdrv_aio_flush(bs, cb, opaque);
+}
+
 
 /**************************************************************/
 /* async block device emulation */
@@ -1181,6 +1196,15 @@ static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
     return NULL;
 }
 
+static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
+        BlockDriverCompletionFunc *cb, void *opaque)
+{
+    int ret;
+    ret = bdrv_flush(bs);
+    cb(opaque, ret);
+    return NULL;
+}
+
 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
 {
 }
@@ -1224,6 +1248,21 @@ static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
     return &acb->common;
 }
 
+static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
+        BlockDriverCompletionFunc *cb, void *opaque)
+{
+    BlockDriverAIOCBSync *acb;
+    int ret;
+
+    acb = qemu_aio_get(bs, cb, opaque);
+    if (!acb->bh)
+        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
+    ret = bdrv_flush(bs);
+    acb->ret = ret;
+    qemu_bh_schedule(acb->bh);
+    return &acb->common;
+}
+
 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
 {
     BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
block.h +2 -0
index 861a65b..0ecb0f8 100644
@@ -86,6 +86,8 @@ BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
                                  const uint8_t *buf, int nb_sectors,
                                  BlockDriverCompletionFunc *cb, void *opaque);
+BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 
+                                 BlockDriverCompletionFunc *cb, void *opaque);
 void bdrv_aio_cancel(BlockDriverAIOCB *acb);
 
 void qemu_aio_init(void);
block_int.h +2 -0
index 796ce29..bda0eab 100644
@@ -55,6 +55,8 @@ struct BlockDriver {
         int64_t sector_num, const uint8_t *buf, int nb_sectors,
         BlockDriverCompletionFunc *cb, void *opaque);
     void (*bdrv_aio_cancel)(BlockDriverAIOCB *acb);
+    BlockDriverAIOCB *(*bdrv_aio_flush)(BlockDriverState *bs,
+        BlockDriverCompletionFunc *cb, void *opaque);
     int aiocb_size;
 
     const char *protocol_name;
hw/ide.c +81 -17

236 lines not on this page · raw message

hw/scsi-disk.c +12 -7
index 30cc906..99aa259 100644
@@ -276,6 +276,16 @@ static uint8_t *scsi_get_buf(SCSIDevice *d, uint32_t tag)
     return r->dma_buf;
 }
 
+static void scsi_flush_cb(void *opaque, int ret) {
+    SCSIRequest *r = opaque;
+    if (ret) {
+        BADF("scsi-disc: IO error on flush: %s\n", strerror(-ret));
+	scsi_command_complete(r, SENSE_HARDWARE_ERROR);
+    } else {
+	scsi_command_complete(r, SENSE_NO_SENSE);
+    }
+}
+
 /* Execute a scsi command.  Returns the length of the data expected by the
    command.  This will be Positive for data transfers from the device
    (eg. disk reads), negative for transfers to the device (eg. disk writes),
@@ -621,13 +631,8 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
         break;
     case 0x35:
         DPRINTF("Synchronise cache (sector %d, count %d)\n", lba, len);
-        ret = bdrv_flush(s->bdrv);
-        if (ret) {
-            DPRINTF("IO error on bdrv_flush\n");
-            scsi_command_complete(r, SENSE_HARDWARE_ERROR);
-            return 0;
-        }
-        break;
+	bdrv_aio_flush(s->bdrv, scsi_flush_cb, r);
+	return 0;
     case 0x43:
         {
             int start_track, format, msf, toclen;
#1 lore raw
Quoted 1 line · Ian Jackson

+    if (!s->bs) return; /* yikes */

This comment is distinctly unhelpful. I guess this is the condition mentioned in ide_device_utterly_broken, but it's hard to tell.

Quoted 11 lines
(quoted diff) +0 -0
@@ -1021,6 +1031,13 @@ static void ide_write_dma_cb(void *opaque, int ret)


     /* end of transfer ? */
     if (s->nsector == 0) {
+       if (!s->write_cache) {
+           ret = bdrv_flush(s->bs);
+           if (ret != 0) {
+               ide_dma_error(s);
+               return;
+           }
+       }

By my reading this is adding a synchronous flush to the end of an async write operation, which in practice makes the whole operation synchronous.

Quoted 4 lines

+static void scsi_flush_cb(void *opaque, int ret) {
+    SCSIRequest *r = opaque;
+    if (ret) {
+        BADF("scsi-disc: IO error on flush: %s\n", strerror(-ret));

This is wrong for two reasons: BADF already adds a suitable prefix, and is for reporting qemu bugs (i.e. missing features or things that should never happen). This is just a normal IO error, which we correctly report to the guest.

Paul

#11 lore raw
In reply to Ian Jackson

Hello,

Oh, by the way, shouldn't this:

hw/ide.c static void ide_identify(IDEState *s) · line 565

Quoted 5 lines · Ian Jackson, le Tue 01 Apr 2008 18:18:59 +0100, a écrit :
     put_le16(p + 81, 0x16); /* conforms to ata5 */
hw/ide.c +2 -1
-    put_le16(p + 82, (1 << 14));
+    /* 14=nop 5=write_cache */
+    put_le16(p + 82, (1 << 14) | (1 << 5));
     /* 13=flush_cache_ext,12=flush_cache,10=lba48 */

use s->write_cache instead of always 1? Else when using hdparm -i one would think that write cache is always enabled.

Samuel

#2 lore raw
In reply to Paul Brook
Quoted 7 lines · Paul Brook
Quoted 7 lines
(quoted diff) +0 -0
@@ -1021,6 +1031,13 @@ static void ide_write_dma_cb(void *opaque, int ret)


     /* end of transfer ? */
     if (s->nsector == 0) {
+       if (!s->write_cache) {
+           ret = bdrv_flush(s->bs);
+           if (ret != 0) {

Quoted 8 lines
Quoted 4 lines

+               ide_dma_error(s);
+               return;
+           }
+       }

By my reading this is adding a synchronous flush to the end of an async write operation, which in practice makes the whole operation synchronous.

Looks that way to me too. It might be simplest to open the device with O_DSYNC when !s->write_cache and user actually wants fdatasync, so that async write can be used. I suspect every platform with useful Posix AIO has O_DSYNC.

Otherwise, chaining where the completion of aio_write triggers aio_fsync instead of reporting completion to the guest?

-- Jamie

#3 lore raw
Quoted 17 lines · Jamie Lokier
Quoted 7 lines · Paul Brook
Quoted 7 lines
hw/ide.c +3 -0
@@ -1021,6 +1031,13 @@ static void ide_write_dma_cb(void *opaque, int
 ret) 
      /* end of transfer ? */
      if (s->nsector == 0) {
+       if (!s->write_cache) {
+           ret = bdrv_flush(s->bs);
+           if (ret != 0) {
Quoted 8 lines

hw/ide.c static void ide_write_dma_cb(void *opaque, int ret) · line 1040

Quoted 4 lines
hw/ide.c +4 -0
+               ide_dma_error(s);
+               return;
+           }
+       }

By my reading this is adding a synchronous flush to the end of an async write operation, which in practice makes the whole operation synchronous.

Quoted 4 lines

Looks that way to me too. It might be simplest to open the device with O_DSYNC when !s->write_cache and user actually wants fdatasync, so that async write can be used. I suspect every platform with useful Posix AIO has O_DSYNC.

The cache is dynamically enabled/disabled by the target. This means you've got to close and repopen the file every time it changes, which is likely to get really hairy.

Quoted 2 lines

Otherwise, chaining where the completion of aio_write triggers aio_fsync instead of reporting completion to the guest?

Yes, that should do it.

Paul

#12 lore raw
In reply to Samuel Thibault

Samuel Thibault writes ("Re: [Qemu-devel] [PATCH] Asynchronous reliable and configurable cache flush"):

Quoted 10 lines

Oh, by the way, shouldn't this:

hw/ide.c static void ide_identify(IDEState *s) · line 565

Quoted 5 lines · Ian Jackson, le Tue 01 Apr 2008 18:18:59 +0100, a écrit :
     put_le16(p + 81, 0x16); /* conforms to ata5 */
hw/ide.c +2 -1
-    put_le16(p + 82, (1 << 14));
+    /* 14=nop 5=write_cache */
+    put_le16(p + 82, (1 << 14) | (1 << 5));
     /* 13=flush_cache_ext,12=flush_cache,10=lba48 */

use s->write_cache instead of always 1? Else when using hdparm -i one would think that write cache is always enabled.

No. According to the ATA-7 draft I have in front of me, the meaning of word 82 is Command set supported' and the meaning of bit 5 is is write cache supported'.

Word 85 bit 5 is `write cache enabled'. I see that the patch I supplied earlier is missing this, which I will fix.

Ian.

#4 lore raw
In reply to Paul Brook
Quoted 8 lines · Paul Brook
Quoted 4 lines

Looks that way to me too. It might be simplest to open the device with O_DSYNC when !s->write_cache and user actually wants fdatasync, so that async write can be used. I suspect every platform with useful Posix AIO has O_DSYNC.

The cache is dynamically enabled/disabled by the target. This means you've got to close and repopen the file every time it changes, which is likely to get really hairy.

Just open two descriptors :-)

I'm not sure if F_SETFL can be used.

Both descriptors are useful with the cache enabled, if the SATA FUA (force unit access) bit is set on a write command. Only those writes would use the O_DSYNC descriptor.

Quoted 4 lines
Quoted 2 lines

Otherwise, chaining where the completion of aio_write triggers aio_fsync instead of reporting completion to the guest?

Yes, that should do it.

Yes, though O_DSYNC will save a system call.

-- Jamie

#5 lore raw

Jamie Lokier writes ("Re: [Qemu-devel] [PATCH] Asynchronous reliable and configurable cache flush"):

Quoted 6 lines
Quoted 3 lines · Paul Brook

The cache is dynamically enabled/disabled by the target. This means you've got to close and repopen the file every time it changes, which is likely to get really hairy.

Just open two descriptors :-)

Doing it with bdrv_aio_flush wasn't too hard, so that's what'll be in my revised patch shortly.

(The original patch I'm porting from xen-unstable predates my introduction of bdrv_aio_flush, hence the synchronous flush at that point.)

Ian.

#8 lore raw
In reply to Paul Brook

Paul Brook writes ("Re: [Qemu-devel] [PATCH] Asynchronous reliable and configurable cache flush"):

Quoted 10 lines · Ian Jackson
Quoted 4 lines

+static void scsi_flush_cb(void *opaque, int ret) {
+    SCSIRequest *r = opaque;
+    if (ret) {
+        BADF("scsi-disc: IO error on flush: %s\n", strerror(-ret));

This is wrong for two reasons: BADF already adds a suitable prefix, and is for reporting qemu bugs (i.e. missing features or things that should never happen). This is just a normal IO error, which we correctly report to the guest.

I don't think flush failures are a normal IO error. They can only occur when the corresponding host block device is having serious trouble. Normally (if it's actually a host disk) this is very bad.

The principle I'm trying to follow with this is to not throw away the errno value. When we turn a block device write failure (which might be EIO or perhaps something weirder like EBADF indicating a bug in qemu) into a SCSI error, we don't have the ability to properly report that errno value to the guest. So we should log it somewhere.

In a previous thread on this subject we seemed to conclude that qemu's stderr is fine for this purpose. But if not then I'm happy to be advised to send it somewhere else ...

You're right about the prefix though (and it was the wrong prefix, too, with a variant spelling of `disk').

Ian.

#14 lore raw
In reply to Ian Jackson

This patch does the following, as previously discussed here:
* Makes guests able to control the caching of emulated IDE disks using the SETFEATURES 0x02 and 0x82 commands.
* Introduces a new block entrypoint bdrv_aio_flush for asynchronous flushing of the write cache.
* Makes IDE FLUSH_CACHE and FLUSH_CACHE_EXT use the new asynchronous function so that guests can continue to execute, receive timer interrupts, etc.
* Does likewise for the SCSI FLUSH CACHE command.
* Removes some clone-and-hackery in DMA completions so that the code can be reused for the flush due to dma writes with write_cache turned off.

Thanks for the review and comments, particularly to Paul Brook.

Ian.

block-qcow.c +8 -0
index 59f1c66..4798ade 100644
@@ -725,6 +725,13 @@ static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
     qemu_aio_release(acb);
 }
 
+static BlockDriverAIOCB *qcow_aio_flush(BlockDriverState *bs,
+        BlockDriverCompletionFunc *cb, void *opaque)
+{
+    BDRVQcowState *s = bs->opaque;
+    return bdrv_aio_flush(s->hd, cb, opaque);
+}
+
 static void qcow_close(BlockDriverState *bs)
 {
     BDRVQcowState *s = bs->opaque;
@@ -913,6 +920,7 @@ BlockDriver bdrv_qcow = {
     .bdrv_aio_read = qcow_aio_read,
     .bdrv_aio_write = qcow_aio_write,
     .bdrv_aio_cancel = qcow_aio_cancel,
+    .bdrv_aio_flush = qcow_aio_flush,
     .aiocb_size = sizeof(QCowAIOCB),
     .bdrv_write_compressed = qcow_write_compressed,
     .bdrv_get_info = qcow_get_info,
block-qcow2.c +8 -0
index 9bad090..9c63a63 100644
@@ -1007,6 +1007,13 @@ static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
     qemu_aio_release(acb);
 }
 
+static BlockDriverAIOCB *qcow_aio_flush(BlockDriverState *bs,
+        BlockDriverCompletionFunc *cb, void *opaque)
+{
+    BDRVQcowState *s = bs->opaque;
+    return bdrv_aio_flush(s->hd, cb, opaque);
+}
+
 static void qcow_close(BlockDriverState *bs)
 {
     BDRVQcowState *s = bs->opaque;
@@ -2241,6 +2248,7 @@ BlockDriver bdrv_qcow2 = {
     .bdrv_aio_read = qcow_aio_read,
     .bdrv_aio_write = qcow_aio_write,
     .bdrv_aio_cancel = qcow_aio_cancel,
+    .bdrv_aio_flush = qcow_aio_flush,
     .aiocb_size = sizeof(QCowAIOCB),
     .bdrv_write_compressed = qcow_write_compressed,
 
block-raw-posix.c +16 -0
index 45d0a93..71e304d 100644
@@ -429,6 +429,20 @@ static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
     return &acb->common;
 }
 
+static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
+        BlockDriverCompletionFunc *cb, void *opaque)
+{
+    RawAIOCB *acb;
+
+    acb = raw_aio_setup(bs, 0, NULL, 0, cb, opaque);
+    if (!acb)
+        return NULL;
+    if (aio_fsync(O_SYNC, &acb->aiocb) < 0) {
+        qemu_aio_release(acb);
+        return NULL;
+    }
+    return &acb->common;
+}
 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
 {
     int ret;
@@ -762,6 +776,7 @@ static int fd_open(BlockDriverState *bs)
 {
     return 0;
 }
+
 #endif
 
 #if defined(__linux__)
@@ -913,6 +928,7 @@ BlockDriver bdrv_host_device = {
     .bdrv_aio_read = raw_aio_read,
     .bdrv_aio_write = raw_aio_write,
     .bdrv_aio_cancel = raw_aio_cancel,
+    .bdrv_aio_flush = raw_aio_flush,
     .aiocb_size = sizeof(RawAIOCB),
     .bdrv_pread = raw_pread,
     .bdrv_pwrite = raw_pwrite,
block.c +39 -0
index fc32a60..348a215 100644
@@ -50,6 +50,8 @@ static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
         int64_t sector_num, const uint8_t *buf, int nb_sectors,
         BlockDriverCompletionFunc *cb, void *opaque);
+static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
+        BlockDriverCompletionFunc *cb, void *opaque);
 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
                         uint8_t *buf, int nb_sectors);
@@ -137,6 +139,8 @@ static void bdrv_register(BlockDriver *bdrv)
         bdrv->bdrv_read = bdrv_read_em;
         bdrv->bdrv_write = bdrv_write_em;
     }
+    if (!bdrv->bdrv_aio_flush)
+        bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
     bdrv->next = first_drv;
     first_drv = bdrv;
 }
@@ -1156,6 +1160,17 @@ void bdrv_aio_cancel(BlockDriverAIOCB *acb)
     drv->bdrv_aio_cancel(acb);
 }
 
+BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 
+                                 BlockDriverCompletionFunc *cb, void *opaque)
+{
+    BlockDriver *drv = bs->drv;
+
+    if (!drv)
+        return NULL;
+
+    return drv->bdrv_aio_flush(bs, cb, opaque);
+}
+
 
 /**************************************************************/
 /* async block device emulation */
@@ -1181,6 +1196,15 @@ static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
     return NULL;
 }
 
+static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
+        BlockDriverCompletionFunc *cb, void *opaque)
+{
+    int ret;
+    ret = bdrv_flush(bs);
+    cb(opaque, ret);
+    return NULL;
+}
+
 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
 {
 }
@@ -1224,6 +1248,21 @@ static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
     return &acb->common;
 }
 
+static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
+        BlockDriverCompletionFunc *cb, void *opaque)
+{
+    BlockDriverAIOCBSync *acb;
+    int ret;
+
+    acb = qemu_aio_get(bs, cb, opaque);
+    if (!acb->bh)
+        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
+    ret = bdrv_flush(bs);
+    acb->ret = ret;
+    qemu_bh_schedule(acb->bh);
+    return &acb->common;
+}
+
 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
 {
     BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
block.h +2 -0
index 861a65b..0ecb0f8 100644
@@ -86,6 +86,8 @@ BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
                                  const uint8_t *buf, int nb_sectors,
                                  BlockDriverCompletionFunc *cb, void *opaque);
+BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 
+                                 BlockDriverCompletionFunc *cb, void *opaque);
 void bdrv_aio_cancel(BlockDriverAIOCB *acb);
 
 void qemu_aio_init(void);
block_int.h +2 -0
index 796ce29..bda0eab 100644
@@ -55,6 +55,8 @@ struct BlockDriver {
         int64_t sector_num, const uint8_t *buf, int nb_sectors,
         BlockDriverCompletionFunc *cb, void *opaque);
     void (*bdrv_aio_cancel)(BlockDriverAIOCB *acb);
+    BlockDriverAIOCB *(*bdrv_aio_flush)(BlockDriverState *bs,
+        BlockDriverCompletionFunc *cb, void *opaque);
     int aiocb_size;
 
     const char *protocol_name;
hw/ide.c +104 -33

305 lines not on this page · raw message

hw/scsi-disk.c +12 -7
index 30cc906..91fcc70 100644
@@ -276,6 +276,16 @@ static uint8_t *scsi_get_buf(SCSIDevice *d, uint32_t tag)
     return r->dma_buf;
 }
 
+static void scsi_flush_cb(void *opaque, int ret) {
+    SCSIRequest *r = opaque;
+    if (ret) {
+        BADF("IO error on flush: %s\n", strerror(-ret));
+	scsi_command_complete(r, SENSE_HARDWARE_ERROR);
+    } else {
+	scsi_command_complete(r, SENSE_NO_SENSE);
+    }
+}
+
 /* Execute a scsi command.  Returns the length of the data expected by the
    command.  This will be Positive for data transfers from the device
    (eg. disk reads), negative for transfers to the device (eg. disk writes),
@@ -621,13 +631,8 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
         break;
     case 0x35:
         DPRINTF("Synchronise cache (sector %d, count %d)\n", lba, len);
-        ret = bdrv_flush(s->bdrv);
-        if (ret) {
-            DPRINTF("IO error on bdrv_flush\n");
-            scsi_command_complete(r, SENSE_HARDWARE_ERROR);
-            return 0;
-        }
-        break;
+	bdrv_aio_flush(s->bdrv, scsi_flush_cb, r);
+	return 0;
     case 0x43:
         {
             int start_track, format, msf, toclen;
#9 lore raw
In reply to Ian Jackson
Quoted 11 lines
Quoted 7 lines
Quoted 2 lines

+    if (ret) {
+        BADF("scsi-disc: IO error on flush: %s\n", strerror(-ret));

This is wrong for two reasons: BADF already adds a suitable prefix, and is for reporting qemu bugs (i.e. missing features or things that should never happen). This is just a normal IO error, which we correctly report to the guest.

I don't think flush failures are a normal IO error. They can only occur when the corresponding host block device is having serious trouble. Normally (if it's actually a host disk) this is very bad.

In practice any IO error indicates that the target device is pretty screwed. I don't see why flushes should be special. When caches are enabled I'd be fairly surprised if normal write operations ever returned an error.

Paul

#10 lore raw

Paul Brook writes ("Re: [Qemu-devel] [PATCH] Asynchronous reliable and configurable cache flush"):

Quoted 8 lines

[Ian Jackson:]

Quoted 3 lines

I don't think flush failures are a normal IO error. They can only occur when the corresponding host block device is having serious trouble. Normally (if it's actually a host disk) this is very bad.

In practice any IO error indicates that the target device is pretty screwed. I don't see why flushes should be special. When caches are enabled I'd be fairly surprised if normal write operations ever returned an error.

The most common kind of `IO error' will be disk full (or the equivalent) on the difference file for a COW device. That can't be detected at flush time (at least, not with most host filesystems).

Ian.

#6 lore raw
In reply to Ian Jackson
Quoted 8 lines · Ian Jackson
Quoted 5 lines
Quoted 3 lines

The cache is dynamically enabled/disabled by the target. This means you've got to close and repopen the file every time it changes, which is likely to get really hairy.

Just open two descriptors :-)

Doing it with bdrv_aio_flush wasn't too hard, so that's what'll be in my revised patch shortly.

For "uncached" writes, do you wait until _after_ the aio_write has completed before calling aio_fsync, or do you assume the aio_fsync will wait for all aio_writes queued (but not completed) prior to it, as Marcelo Tosatti believes the documentation implies?

Thanks,
-- Jamie

#7 lore raw

Jamie Lokier writes ("Re: [Qemu-devel] [PATCH] Asynchronous reliable and configurable cache flush"):

Quoted 8 lines
Quoted 2 lines · Ian Jackson

Doing it with bdrv_aio_flush wasn't too hard, so that's what'll be in my revised patch shortly.

For "uncached" writes, do you wait until _after_ the aio_write has completed before calling aio_fsync, or do you assume the aio_fsync will wait for all aio_writes queued (but not completed) prior to it, as Marcelo Tosatti believes the documentation implies?

I do the former. In general, I assume that in the following case:

aio_write
returns 0, operation is in progress
.
. aio_fsync
. returns 0, operation is in progress
. .
aio_return on the aio_write .
says completed OK .
.
aio_return on the aio_fsync
says completed OK

the aio_f(data)sync completion does not tell us that the write (which had not been completed at the time aio_fsync was called) has been synchronised.

I don't agree with Marcelo's analysis.

I think SuSv3 unhelpfully uses the word `queued' in two different
ways: firstly, to refer to data for which a write(2) or equivalent has completed but where the data has not yet reached stable storage, and secondly to refer to asynchronous IO operations (including aio_write and aio_fsync) which have been submitted but not yet completed.

The former usage is demonstrated clearly in the SuSv3 page for write(2) which describes the data as having been `queued' when write(2) completes - ie when it is in the buffer cache. This interpretation for write(2) is supported by the page for read(2) not mentioning queues (except for STREAMS queues which are irrelevant).

The latter usage is demonstrated by the spec for aio_read, which talks about a read having been queued, and even for aio_fsync which is specified to return when `the synchronisation request has been ... queued'.

Clearly it is the former meaning of `queue' which is referred to by the specification of fsync and fdatasync since those calls apply to data from write(2) as well.

In the spec for aio_write, `queued' is used in the first paragraph with an identical wording to that for aio_read, so has the latter of the two meanings above.

Sadly in aio_fsync these two meanings of `queued' appear in different parts of the page. Firstly, we have the statement that aio_fsync shall return when the synchronisation request has been queued (identical wording to that used for aio_read and aio_write). Then in the second paragraph we refer to queued IO operations but this can only refer to the former, fsync, sense (since the text explicitly says `as if by a call to fsync and since otherwise the reference would be semi-recursive as the aio_fsync would itself be a queued IO operation).

If one thinks that both of these meanings of `queue' are the same:
what is the difference between aio_write and write(2) ? Why can't write(2) return immediately just like aio_write ?

Ian.

#13 lore raw
In reply to Ian Jackson
Quoted 9 lines · Ian Jackson, le Wed 02 Apr 2008 15:21:32 +0100, a écrit :

Samuel Thibault writes ("Re: [Qemu-devel] [PATCH] Asynchronous reliable and configurable cache flush"):

Quoted 8 lines

Oh, by the way, shouldn't this:

Quoted 5 lines · Ian Jackson, le Tue 01 Apr 2008 18:18:59 +0100, a écrit :
     put_le16(p + 81, 0x16); /* conforms to ata5 */
hw/ide.c +2 -1
-    put_le16(p + 82, (1 << 14));
+    /* 14=nop 5=write_cache */
+    put_le16(p + 82, (1 << 14) | (1 << 5));
     /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
Quoted 6 lines
Quoted 2 lines

use s->write_cache instead of always 1? Else when using hdparm -i one would think that write cache is always enabled.

No. According to the ATA-7 draft I have in front of me, the meaning of word 82 is Command set supported' and the meaning of bit 5 is is write cache supported'.

Oh, I was actually talking about word 85 indeed. In Xen we lack the word 82 bit, I'll make a patch.

Samuel