block: Change bdrv_eject() not to drop the image
[qemu/stefanha.git] / hw / virtio-blk.c
blob490cd410507e883a94fa9f529d2e7d6a06f21e45
1 /*
2 * Virtio Block Device
4 * Copyright IBM, Corp. 2007
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include <qemu-common.h>
15 #include "qemu-error.h"
16 #include "virtio-blk.h"
17 #ifdef __linux__
18 # include <scsi/sg.h>
19 #endif
21 typedef struct VirtIOBlock
23 VirtIODevice vdev;
24 BlockDriverState *bs;
25 VirtQueue *vq;
26 void *rq;
27 QEMUBH *bh;
28 BlockConf *conf;
29 unsigned short sector_mask;
30 char sn[BLOCK_SERIAL_STRLEN];
31 DeviceState *qdev;
32 } VirtIOBlock;
34 static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
36 return (VirtIOBlock *)vdev;
39 typedef struct VirtIOBlockReq
41 VirtIOBlock *dev;
42 VirtQueueElement elem;
43 struct virtio_blk_inhdr *in;
44 struct virtio_blk_outhdr *out;
45 struct virtio_scsi_inhdr *scsi;
46 QEMUIOVector qiov;
47 struct VirtIOBlockReq *next;
48 } VirtIOBlockReq;
50 static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
52 VirtIOBlock *s = req->dev;
54 req->in->status = status;
55 virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
56 virtio_notify(&s->vdev, s->vq);
58 qemu_free(req);
61 static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
62 int is_read)
64 BlockErrorAction action = bdrv_get_on_error(req->dev->bs, is_read);
65 VirtIOBlock *s = req->dev;
67 if (action == BLOCK_ERR_IGNORE) {
68 bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
69 return 0;
72 if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
73 || action == BLOCK_ERR_STOP_ANY) {
74 req->next = s->rq;
75 s->rq = req;
76 bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
77 vm_stop(0);
78 } else {
79 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
80 bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
83 return 1;
86 static void virtio_blk_rw_complete(void *opaque, int ret)
88 VirtIOBlockReq *req = opaque;
90 if (ret) {
91 int is_read = !(req->out->type & VIRTIO_BLK_T_OUT);
92 if (virtio_blk_handle_rw_error(req, -ret, is_read))
93 return;
96 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
99 static void virtio_blk_flush_complete(void *opaque, int ret)
101 VirtIOBlockReq *req = opaque;
103 virtio_blk_req_complete(req, ret ? VIRTIO_BLK_S_IOERR : VIRTIO_BLK_S_OK);
106 static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
108 VirtIOBlockReq *req = qemu_malloc(sizeof(*req));
109 req->dev = s;
110 req->qiov.size = 0;
111 req->next = NULL;
112 return req;
115 static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
117 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
119 if (req != NULL) {
120 if (!virtqueue_pop(s->vq, &req->elem)) {
121 qemu_free(req);
122 return NULL;
126 return req;
129 #ifdef __linux__
130 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
132 struct sg_io_hdr hdr;
133 int ret;
134 int status;
135 int i;
138 * We require at least one output segment each for the virtio_blk_outhdr
139 * and the SCSI command block.
141 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
142 * and the sense buffer pointer in the input segments.
144 if (req->elem.out_num < 2 || req->elem.in_num < 3) {
145 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
146 return;
150 * No support for bidirection commands yet.
152 if (req->elem.out_num > 2 && req->elem.in_num > 3) {
153 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
154 return;
158 * The scsi inhdr is placed in the second-to-last input segment, just
159 * before the regular inhdr.
161 req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
163 memset(&hdr, 0, sizeof(struct sg_io_hdr));
164 hdr.interface_id = 'S';
165 hdr.cmd_len = req->elem.out_sg[1].iov_len;
166 hdr.cmdp = req->elem.out_sg[1].iov_base;
167 hdr.dxfer_len = 0;
169 if (req->elem.out_num > 2) {
171 * If there are more than the minimally required 2 output segments
172 * there is write payload starting from the third iovec.
174 hdr.dxfer_direction = SG_DXFER_TO_DEV;
175 hdr.iovec_count = req->elem.out_num - 2;
177 for (i = 0; i < hdr.iovec_count; i++)
178 hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
180 hdr.dxferp = req->elem.out_sg + 2;
182 } else if (req->elem.in_num > 3) {
184 * If we have more than 3 input segments the guest wants to actually
185 * read data.
187 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
188 hdr.iovec_count = req->elem.in_num - 3;
189 for (i = 0; i < hdr.iovec_count; i++)
190 hdr.dxfer_len += req->elem.in_sg[i].iov_len;
192 hdr.dxferp = req->elem.in_sg;
193 } else {
195 * Some SCSI commands don't actually transfer any data.
197 hdr.dxfer_direction = SG_DXFER_NONE;
200 hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
201 hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
203 ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
204 if (ret) {
205 status = VIRTIO_BLK_S_UNSUPP;
206 hdr.status = ret;
207 hdr.resid = hdr.dxfer_len;
208 } else if (hdr.status) {
209 status = VIRTIO_BLK_S_IOERR;
210 } else {
211 status = VIRTIO_BLK_S_OK;
214 req->scsi->errors = hdr.status;
215 req->scsi->residual = hdr.resid;
216 req->scsi->sense_len = hdr.sb_len_wr;
217 req->scsi->data_len = hdr.dxfer_len;
219 virtio_blk_req_complete(req, status);
221 #else
222 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
224 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
226 #endif /* __linux__ */
228 typedef struct MultiReqBuffer {
229 BlockRequest blkreq[32];
230 unsigned int num_writes;
231 } MultiReqBuffer;
233 static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
235 int i, ret;
237 if (!mrb->num_writes) {
238 return;
241 ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes);
242 if (ret != 0) {
243 for (i = 0; i < mrb->num_writes; i++) {
244 if (mrb->blkreq[i].error) {
245 virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO);
250 mrb->num_writes = 0;
253 static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
255 BlockDriverAIOCB *acb;
258 * Make sure all outstanding writes are posted to the backing device.
260 virtio_submit_multiwrite(req->dev->bs, mrb);
262 acb = bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
263 if (!acb) {
264 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
268 static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
270 BlockRequest *blkreq;
272 if (req->out->sector & req->dev->sector_mask) {
273 virtio_blk_rw_complete(req, -EIO);
274 return;
277 if (mrb->num_writes == 32) {
278 virtio_submit_multiwrite(req->dev->bs, mrb);
281 blkreq = &mrb->blkreq[mrb->num_writes];
282 blkreq->sector = req->out->sector;
283 blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE;
284 blkreq->qiov = &req->qiov;
285 blkreq->cb = virtio_blk_rw_complete;
286 blkreq->opaque = req;
287 blkreq->error = 0;
289 mrb->num_writes++;
292 static void virtio_blk_handle_read(VirtIOBlockReq *req)
294 BlockDriverAIOCB *acb;
296 if (req->out->sector & req->dev->sector_mask) {
297 virtio_blk_rw_complete(req, -EIO);
298 return;
301 acb = bdrv_aio_readv(req->dev->bs, req->out->sector, &req->qiov,
302 req->qiov.size / BDRV_SECTOR_SIZE,
303 virtio_blk_rw_complete, req);
304 if (!acb) {
305 virtio_blk_rw_complete(req, -EIO);
309 static void virtio_blk_handle_request(VirtIOBlockReq *req,
310 MultiReqBuffer *mrb)
312 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
313 fprintf(stderr, "virtio-blk missing headers\n");
314 exit(1);
317 if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
318 req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
319 fprintf(stderr, "virtio-blk header not in correct element\n");
320 exit(1);
323 req->out = (void *)req->elem.out_sg[0].iov_base;
324 req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
326 if (req->out->type & VIRTIO_BLK_T_FLUSH) {
327 virtio_blk_handle_flush(req, mrb);
328 } else if (req->out->type & VIRTIO_BLK_T_SCSI_CMD) {
329 virtio_blk_handle_scsi(req);
330 } else if (req->out->type & VIRTIO_BLK_T_GET_ID) {
331 VirtIOBlock *s = req->dev;
333 memcpy(req->elem.in_sg[0].iov_base, s->sn,
334 MIN(req->elem.in_sg[0].iov_len, sizeof(s->sn)));
335 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
336 } else if (req->out->type & VIRTIO_BLK_T_OUT) {
337 qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
338 req->elem.out_num - 1);
339 virtio_blk_handle_write(req, mrb);
340 } else {
341 qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
342 req->elem.in_num - 1);
343 virtio_blk_handle_read(req);
347 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
349 VirtIOBlock *s = to_virtio_blk(vdev);
350 VirtIOBlockReq *req;
351 MultiReqBuffer mrb = {
352 .num_writes = 0,
355 while ((req = virtio_blk_get_request(s))) {
356 virtio_blk_handle_request(req, &mrb);
359 virtio_submit_multiwrite(s->bs, &mrb);
362 * FIXME: Want to check for completions before returning to guest mode,
363 * so cached reads and writes are reported as quickly as possible. But
364 * that should be done in the generic block layer.
368 static void virtio_blk_dma_restart_bh(void *opaque)
370 VirtIOBlock *s = opaque;
371 VirtIOBlockReq *req = s->rq;
372 MultiReqBuffer mrb = {
373 .num_writes = 0,
376 qemu_bh_delete(s->bh);
377 s->bh = NULL;
379 s->rq = NULL;
381 while (req) {
382 virtio_blk_handle_request(req, &mrb);
383 req = req->next;
386 virtio_submit_multiwrite(s->bs, &mrb);
389 static void virtio_blk_dma_restart_cb(void *opaque, int running, int reason)
391 VirtIOBlock *s = opaque;
393 if (!running)
394 return;
396 if (!s->bh) {
397 s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
398 qemu_bh_schedule(s->bh);
402 static void virtio_blk_reset(VirtIODevice *vdev)
405 * This should cancel pending requests, but can't do nicely until there
406 * are per-device request lists.
408 qemu_aio_flush();
411 /* coalesce internal state, copy to pci i/o region 0
413 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
415 VirtIOBlock *s = to_virtio_blk(vdev);
416 struct virtio_blk_config blkcfg;
417 uint64_t capacity;
418 int cylinders, heads, secs;
420 bdrv_get_geometry(s->bs, &capacity);
421 bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
422 memset(&blkcfg, 0, sizeof(blkcfg));
423 stq_raw(&blkcfg.capacity, capacity);
424 stl_raw(&blkcfg.seg_max, 128 - 2);
425 stw_raw(&blkcfg.cylinders, cylinders);
426 blkcfg.heads = heads;
427 blkcfg.sectors = secs & ~s->sector_mask;
428 blkcfg.blk_size = s->conf->logical_block_size;
429 blkcfg.size_max = 0;
430 blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
431 blkcfg.alignment_offset = 0;
432 blkcfg.min_io_size = s->conf->min_io_size / blkcfg.blk_size;
433 blkcfg.opt_io_size = s->conf->opt_io_size / blkcfg.blk_size;
434 memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
437 static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
439 VirtIOBlock *s = to_virtio_blk(vdev);
441 features |= (1 << VIRTIO_BLK_F_SEG_MAX);
442 features |= (1 << VIRTIO_BLK_F_GEOMETRY);
443 features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
444 features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
446 if (bdrv_enable_write_cache(s->bs))
447 features |= (1 << VIRTIO_BLK_F_WCACHE);
449 if (bdrv_is_read_only(s->bs))
450 features |= 1 << VIRTIO_BLK_F_RO;
452 return features;
455 static void virtio_blk_save(QEMUFile *f, void *opaque)
457 VirtIOBlock *s = opaque;
458 VirtIOBlockReq *req = s->rq;
460 virtio_save(&s->vdev, f);
462 while (req) {
463 qemu_put_sbyte(f, 1);
464 qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
465 req = req->next;
467 qemu_put_sbyte(f, 0);
470 static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
472 VirtIOBlock *s = opaque;
474 if (version_id != 2)
475 return -EINVAL;
477 virtio_load(&s->vdev, f);
478 while (qemu_get_sbyte(f)) {
479 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
480 qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
481 req->next = s->rq;
482 s->rq = req;
485 return 0;
488 VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf)
490 VirtIOBlock *s;
491 int cylinders, heads, secs;
492 static int virtio_blk_id;
493 DriveInfo *dinfo;
495 if (!conf->bs) {
496 error_report("virtio-blk-pci: drive property not set");
497 return NULL;
499 if (!bdrv_is_inserted(conf->bs)) {
500 error_report("Device needs media, but drive is empty");
501 return NULL;
504 s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
505 sizeof(struct virtio_blk_config),
506 sizeof(VirtIOBlock));
508 s->vdev.get_config = virtio_blk_update_config;
509 s->vdev.get_features = virtio_blk_get_features;
510 s->vdev.reset = virtio_blk_reset;
511 s->bs = conf->bs;
512 s->conf = conf;
513 s->rq = NULL;
514 s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
515 bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
517 /* NB: per existing s/n string convention the string is terminated
518 * by '\0' only when less than sizeof (s->sn)
520 dinfo = drive_get_by_blockdev(s->bs);
521 strncpy(s->sn, dinfo->serial, sizeof (s->sn));
523 s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
525 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
526 s->qdev = dev;
527 register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
528 virtio_blk_save, virtio_blk_load, s);
529 bdrv_set_removable(s->bs, 0);
531 return &s->vdev;
534 void virtio_blk_exit(VirtIODevice *vdev)
536 VirtIOBlock *s = to_virtio_blk(vdev);
537 unregister_savevm(s->qdev, "virtio-blk", s);