From a0b1537f9cdff74cf558e4ae08a6eafabfa8c27f Mon Sep 17 00:00:00 2001 From: Gleb Natapov <gleb@redhat.com> Date: Mon, 9 Feb 2009 17:12:39 -0200 Subject: [PATCH 56/70] Stop VM on ENOSPC error (IDE) This version of the patch adds new option "werror" to -drive flag. Possible values are: report - report errors to a guest as IO errors ignore - continue as if nothing happened stop - stop VM on any error and retry last command on resume enospc - stop vm on ENOSPC error and retry last command on resume all other errors are reported to a guest. Default is "report" to maintain current behaviour. [ehabkost: solved conflicts with the "avoid cancelling dma" patch] Conflicts: qemu/hw/ide.c qemu/vl.c Signed-off-by: Gleb Natapov <gleb@redhat.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com> Acked-by: Gleb Natapov <gleb@redhat.com> git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6388 c046a42c-6fe2-441c-8c8c-71466251a162 --- qemu/hw/ide.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++----- qemu/sysemu.h | 7 +++++ qemu/vl.c | 36 +++++++++++++++++++++++- 3 files changed, 116 insertions(+), 10 deletions(-) diff --git a/qemu/hw/ide.c b/qemu/hw/ide.c index 9b725e6..7a2ab9e 100644 --- a/qemu/hw/ide.c +++ b/qemu/hw/ide.c @@ -458,6 +458,8 @@ static inline int media_is_cd(IDEState *s) #define BM_STATUS_DMAING 0x01 #define BM_STATUS_ERROR 0x02 #define BM_STATUS_INT 0x04 +#define BM_STATUS_DMA_RETRY 0x08 +#define BM_STATUS_PIO_RETRY 0x10 #define BM_CMD_START 0x01 #define BM_CMD_READ 0x08 @@ -489,6 +491,8 @@ typedef struct BMDMAState { IDEState *ide_if; BlockDriverCompletionFunc *dma_cb; BlockDriverAIOCB *aiocb; + int64_t sector_num; + uint32_t nsector; } BMDMAState; typedef struct PCIIDEState { @@ -499,6 +503,7 @@ typedef struct PCIIDEState { } PCIIDEState; static void ide_dma_start(IDEState *s, BlockDriverCompletionFunc *dma_cb); +static void ide_dma_restart(IDEState *s); static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret); static void padstr(char *str, const char *src, int len) @@ -866,6 +871,28 @@ static void ide_dma_error(IDEState *s) ide_set_irq(s); } +static int ide_handle_write_error(IDEState *s, int error, int op) +{ + BlockInterfaceErrorAction action = drive_get_onerror(s->bs); + + if (action == BLOCK_ERR_IGNORE) + return 0; + + if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC) + || action == BLOCK_ERR_STOP_ANY) { + s->bmdma->ide_if = s; + s->bmdma->status |= op; + vm_stop(0); + } else { + if (op == BM_STATUS_DMA_RETRY) + ide_dma_error(s); + else + ide_rw_error(s); + } + + return 1; +} + /* return 0 if buffer completed */ static int dma_buf_rw(BMDMAState *bm, int is_write) { @@ -991,9 +1018,10 @@ static void ide_sector_write(IDEState *s) if (n > s->req_nb_sectors) n = s->req_nb_sectors; ret = bdrv_write(s->bs, sector_num, s->io_buffer, n); + if (ret != 0) { - ide_rw_error(s); - return; + if (ide_handle_write_error(s, -ret, BM_STATUS_PIO_RETRY)) + return; } s->nsector -= n; @@ -1025,6 +1053,20 @@ static void ide_sector_write(IDEState *s) } } +static void ide_dma_restart_cb(void *opaque, int running) +{ + BMDMAState *bm = opaque; + if (!running) + return; + if (bm->status & BM_STATUS_DMA_RETRY) { + bm->status &= ~BM_STATUS_DMA_RETRY; + ide_dma_restart(bm->ide_if); + } else if (bm->status & BM_STATUS_PIO_RETRY) { + bm->status &= ~BM_STATUS_PIO_RETRY; + ide_sector_write(bm->ide_if); + } +} + static void ide_write_dma_cb(void *opaque, int ret) { BMDMAState *bm = opaque; @@ -1033,8 +1075,8 @@ static void ide_write_dma_cb(void *opaque, int ret) int64_t sector_num; if (ret < 0) { - ide_dma_error(s); - return; + if (ide_handle_write_error(s, -ret, BM_STATUS_DMA_RETRY)) + return; } n = s->io_buffer_size >> 9; @@ -2850,11 +2892,25 @@ static void ide_dma_start(IDEState *s, BlockDriverCompletionFunc *dma_cb) bm->cur_prd_last = 0; bm->cur_prd_addr = 0; bm->cur_prd_len = 0; + bm->sector_num = ide_get_sector(s); + bm->nsector = s->nsector; if (bm->status & BM_STATUS_DMAING) { bm->dma_cb(bm, 0); } } +static void ide_dma_restart(IDEState *s) +{ + BMDMAState *bm = s->bmdma; + ide_set_sector(s, bm->sector_num); + s->io_buffer_index = 0; + s->io_buffer_size = 0; + s->nsector = bm->nsector; + bm->cur_addr = bm->addr; + bm->dma_cb = ide_write_dma_cb; + ide_dma_start(s, bm->dma_cb); +} + static void ide_dma_cancel(BMDMAState *bm) { if (bm->status & BM_STATUS_DMAING) { @@ -3061,6 +3117,7 @@ static void bmdma_map(PCIDevice *pci_dev, int region_num, d->ide_if[2 * i].bmdma = bm; d->ide_if[2 * i + 1].bmdma = bm; bm->pci_dev = (PCIIDEState *)pci_dev; + qemu_add_vm_change_state_handler(ide_dma_restart_cb, bm); register_ioport_write(addr, 1, 1, bmdma_cmd_writeb, bm); @@ -3086,9 +3143,14 @@ static void pci_ide_save(QEMUFile* f, void *opaque) for(i = 0; i < 2; i++) { BMDMAState *bm = &d->bmdma[i]; + uint8_t ifidx; qemu_put_8s(f, &bm->cmd); qemu_put_8s(f, &bm->status); qemu_put_be32s(f, &bm->addr); + qemu_put_sbe64s(f, &bm->sector_num); + qemu_put_be32s(f, &bm->nsector); + ifidx = bm->ide_if ? bm->ide_if - d->ide_if : 0; + qemu_put_8s(f, &ifidx); /* XXX: if a transfer is pending, we do not save it yet */ } @@ -3112,7 +3174,7 @@ static int pci_ide_load(QEMUFile* f, void *opaque, int version_id) PCIIDEState *d = opaque; int ret, i; - if (version_id != 1) + if (version_id != 2) return -EINVAL; ret = pci_device_load(&d->dev, f); if (ret < 0) @@ -3120,9 +3182,14 @@ static int pci_ide_load(QEMUFile* f, void *opaque, int version_id) for(i = 0; i < 2; i++) { BMDMAState *bm = &d->bmdma[i]; + uint8_t ifidx; qemu_get_8s(f, &bm->cmd); qemu_get_8s(f, &bm->status); qemu_get_be32s(f, &bm->addr); + qemu_get_sbe64s(f, &bm->sector_num); + qemu_get_be32s(f, &bm->nsector); + qemu_get_8s(f, &ifidx); + bm->ide_if = &d->ide_if[ifidx]; /* XXX: if a transfer is pending, we do not save it yet */ } @@ -3230,7 +3297,7 @@ void pci_cmd646_ide_init(PCIBus *bus, BlockDriverState **hd_table, ide_init2(&d->ide_if[0], hd_table[0], hd_table[1], irq[0]); ide_init2(&d->ide_if[2], hd_table[2], hd_table[3], irq[1]); - register_savevm("ide", 0, 1, pci_ide_save, pci_ide_load, d); + register_savevm("ide", 0, 2, pci_ide_save, pci_ide_load, d); qemu_register_reset(cmd646_reset, d); cmd646_reset(d); } @@ -3292,7 +3359,7 @@ void pci_piix3_ide_init(PCIBus *bus, BlockDriverState **hd_table, int devfn, if (hd_table[i]) hd_table[i]->devfn = d->dev.devfn; - register_savevm("ide", 0, 1, pci_ide_save, pci_ide_load, d); + register_savevm("ide", 0, 2, pci_ide_save, pci_ide_load, d); } /* hd_table must contain 4 block drivers */ @@ -3331,7 +3398,7 @@ void pci_piix4_ide_init(PCIBus *bus, BlockDriverState **hd_table, int devfn, ide_init_ioport(&d->ide_if[0], 0x1f0, 0x3f6); ide_init_ioport(&d->ide_if[2], 0x170, 0x376); - register_savevm("ide", 0, 1, pci_ide_save, pci_ide_load, d); + register_savevm("ide", 0, 2, pci_ide_save, pci_ide_load, d); } /***********************************************************/ diff --git a/qemu/sysemu.h b/qemu/sysemu.h index d368a91..48f8464 100644 --- a/qemu/sysemu.h +++ b/qemu/sysemu.h @@ -132,11 +132,17 @@ typedef enum { IF_IDE, IF_SCSI, IF_FLOPPY, IF_PFLASH, IF_MTD, IF_SD, IF_VIRTIO } BlockInterfaceType; +typedef enum { + BLOCK_ERR_REPORT, BLOCK_ERR_IGNORE, BLOCK_ERR_STOP_ENOSPC, + BLOCK_ERR_STOP_ANY +} BlockInterfaceErrorAction; + typedef struct DriveInfo { BlockDriverState *bdrv; BlockInterfaceType type; int bus; int unit; + BlockInterfaceErrorAction onerror; char serial[21]; int used; int drive_opt_idx; @@ -153,6 +159,7 @@ extern int extboot_drive; extern int drive_get_index(BlockInterfaceType type, int bus, int unit); extern int drive_get_max_bus(BlockInterfaceType type); extern const char *drive_get_serial(BlockDriverState *bdrv); +extern BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv); extern void drive_uninit(BlockDriverState *bdrv); extern void drive_remove(int index); diff --git a/qemu/vl.c b/qemu/vl.c index 3e068c9..facb653 100644 --- a/qemu/vl.c +++ b/qemu/vl.c @@ -2311,6 +2311,17 @@ const char *drive_get_serial(BlockDriverState *bdrv) return "\0"; } +BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv) +{ + int index; + + for (index = 0; index < nb_drives; index++) + if (drives_table[index].bdrv == bdrv) + return drives_table[index].onerror; + + return BLOCK_ERR_REPORT; +} + static void bdrv_format_print(void *opaque, const char *name) { fprintf(stderr, " %s", name); @@ -2347,13 +2358,13 @@ int drive_init(struct drive_opt *arg, int snapshot, int max_devs; int index; int cache; - int bdrv_flags; + int bdrv_flags, onerror; int drives_table_idx; char *str = arg->opt; static const char * const params[] = { "bus", "unit", "if", "index", "cyls", "heads", "secs", "trans", "media", "snapshot", "file", - "cache", "format", "serial", + "cache", "format", "serial", "werror", "boot", NULL }; if (check_params(buf, sizeof(buf), params, str) < 0) { @@ -2557,6 +2568,26 @@ int drive_init(struct drive_opt *arg, int snapshot, if (!get_param_value(serial, sizeof(serial), "serial", str)) memset(serial, 0, sizeof(serial)); + onerror = BLOCK_ERR_REPORT; + if (get_param_value(buf, sizeof(serial), "werror", str)) { + if (type != IF_IDE) { + fprintf(stderr, "werror is supported only by IDE\n"); + return -1; + } + if (!strcmp(buf, "ignore")) + onerror = BLOCK_ERR_IGNORE; + else if (!strcmp(buf, "enospc")) + onerror = BLOCK_ERR_STOP_ENOSPC; + else if (!strcmp(buf, "stop")) + onerror = BLOCK_ERR_STOP_ANY; + else if (!strcmp(buf, "report")) + onerror = BLOCK_ERR_REPORT; + else { + fprintf(stderr, "qemu: '%s' invalid write error action\n", buf); + return -1; + } + } + /* compute bus and unit according index */ if (index != -1) { @@ -2622,6 +2653,7 @@ int drive_init(struct drive_opt *arg, int snapshot, drives_table[drives_table_idx].bus = bus_id; drives_table[drives_table_idx].unit = unit_id; drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt; + drives_table[drives_table_idx].onerror = onerror; strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial)); nb_drives++; -- 1.6.1