]> git.proxmox.com Git - qemu.git/blame - hw/virtio-blk.c
block migration: propagate return value when bdrv_write() returns < 0
[qemu.git] / hw / virtio-blk.c
CommitLineData
6e02c38d
AL
1/*
2 * Virtio Block Device
3 *
4 * Copyright IBM, Corp. 2007
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
869a5c6d 14#include <qemu-common.h>
d75d25e3 15#include "qemu-error.h"
6e02c38d 16#include "virtio-blk.h"
1063b8b1
CH
17#ifdef __linux__
18# include <scsi/sg.h>
19#endif
6e02c38d
AL
20
21typedef struct VirtIOBlock
22{
23 VirtIODevice vdev;
24 BlockDriverState *bs;
25 VirtQueue *vq;
869a5c6d 26 void *rq;
213189ab 27 QEMUBH *bh;
9752c371 28 BlockConf *conf;
8cfacf07 29 unsigned short sector_mask;
2930b313 30 char sn[BLOCK_SERIAL_STRLEN];
6e02c38d
AL
31} VirtIOBlock;
32
33static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
34{
35 return (VirtIOBlock *)vdev;
36}
37
38typedef struct VirtIOBlockReq
39{
40 VirtIOBlock *dev;
41 VirtQueueElement elem;
42 struct virtio_blk_inhdr *in;
43 struct virtio_blk_outhdr *out;
1063b8b1 44 struct virtio_scsi_inhdr *scsi;
d28a1b6e 45 QEMUIOVector qiov;
869a5c6d 46 struct VirtIOBlockReq *next;
6e02c38d
AL
47} VirtIOBlockReq;
48
869a5c6d
AL
49static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
50{
51 VirtIOBlock *s = req->dev;
52
53 req->in->status = status;
d28a1b6e 54 virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
869a5c6d
AL
55 virtio_notify(&s->vdev, s->vq);
56
869a5c6d
AL
57 qemu_free(req);
58}
59
f35d68f0
KW
60static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
61 int is_read)
869a5c6d 62{
abd7f68d 63 BlockErrorAction action = bdrv_get_on_error(req->dev->bs, is_read);
869a5c6d
AL
64 VirtIOBlock *s = req->dev;
65
eaa6c85f 66 if (action == BLOCK_ERR_IGNORE) {
908bb949 67 bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
869a5c6d 68 return 0;
eaa6c85f 69 }
869a5c6d
AL
70
71 if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
72 || action == BLOCK_ERR_STOP_ANY) {
73 req->next = s->rq;
74 s->rq = req;
908bb949 75 bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
554a310b 76 vm_stop(0);
869a5c6d
AL
77 } else {
78 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
908bb949 79 bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
869a5c6d
AL
80 }
81
82 return 1;
83}
84
6e02c38d
AL
85static void virtio_blk_rw_complete(void *opaque, int ret)
86{
87 VirtIOBlockReq *req = opaque;
6e02c38d 88
f35d68f0
KW
89 if (ret) {
90 int is_read = !(req->out->type & VIRTIO_BLK_T_OUT);
91 if (virtio_blk_handle_rw_error(req, -ret, is_read))
869a5c6d 92 return;
6e02c38d
AL
93 }
94
f35d68f0 95 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
869a5c6d 96}
6e02c38d 97
aa659be3
CH
98static void virtio_blk_flush_complete(void *opaque, int ret)
99{
100 VirtIOBlockReq *req = opaque;
101
102 virtio_blk_req_complete(req, ret ? VIRTIO_BLK_S_IOERR : VIRTIO_BLK_S_OK);
103}
104
869a5c6d
AL
105static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
106{
de6c8042 107 VirtIOBlockReq *req = qemu_malloc(sizeof(*req));
487414f1 108 req->dev = s;
de6c8042
SH
109 req->qiov.size = 0;
110 req->next = NULL;
869a5c6d 111 return req;
6e02c38d
AL
112}
113
114static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
115{
869a5c6d 116 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
6e02c38d 117
869a5c6d
AL
118 if (req != NULL) {
119 if (!virtqueue_pop(s->vq, &req->elem)) {
120 qemu_free(req);
121 return NULL;
122 }
6e02c38d
AL
123 }
124
125 return req;
126}
127
1063b8b1
CH
128#ifdef __linux__
129static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
130{
131 struct sg_io_hdr hdr;
4277906d 132 int ret;
1063b8b1
CH
133 int status;
134 int i;
135
136 /*
137 * We require at least one output segment each for the virtio_blk_outhdr
138 * and the SCSI command block.
139 *
140 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
141 * and the sense buffer pointer in the input segments.
142 */
143 if (req->elem.out_num < 2 || req->elem.in_num < 3) {
144 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
145 return;
146 }
147
148 /*
149 * No support for bidirection commands yet.
150 */
151 if (req->elem.out_num > 2 && req->elem.in_num > 3) {
152 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
153 return;
154 }
155
156 /*
157 * The scsi inhdr is placed in the second-to-last input segment, just
158 * before the regular inhdr.
159 */
160 req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
1063b8b1
CH
161
162 memset(&hdr, 0, sizeof(struct sg_io_hdr));
163 hdr.interface_id = 'S';
164 hdr.cmd_len = req->elem.out_sg[1].iov_len;
165 hdr.cmdp = req->elem.out_sg[1].iov_base;
166 hdr.dxfer_len = 0;
167
168 if (req->elem.out_num > 2) {
169 /*
170 * If there are more than the minimally required 2 output segments
171 * there is write payload starting from the third iovec.
172 */
173 hdr.dxfer_direction = SG_DXFER_TO_DEV;
174 hdr.iovec_count = req->elem.out_num - 2;
175
176 for (i = 0; i < hdr.iovec_count; i++)
177 hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
178
179 hdr.dxferp = req->elem.out_sg + 2;
180
181 } else if (req->elem.in_num > 3) {
182 /*
183 * If we have more than 3 input segments the guest wants to actually
184 * read data.
185 */
186 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
187 hdr.iovec_count = req->elem.in_num - 3;
188 for (i = 0; i < hdr.iovec_count; i++)
189 hdr.dxfer_len += req->elem.in_sg[i].iov_len;
190
191 hdr.dxferp = req->elem.in_sg;
1063b8b1
CH
192 } else {
193 /*
194 * Some SCSI commands don't actually transfer any data.
195 */
196 hdr.dxfer_direction = SG_DXFER_NONE;
197 }
198
199 hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
200 hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
1063b8b1
CH
201
202 ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
203 if (ret) {
204 status = VIRTIO_BLK_S_UNSUPP;
205 hdr.status = ret;
206 hdr.resid = hdr.dxfer_len;
207 } else if (hdr.status) {
208 status = VIRTIO_BLK_S_IOERR;
209 } else {
210 status = VIRTIO_BLK_S_OK;
211 }
212
213 req->scsi->errors = hdr.status;
214 req->scsi->residual = hdr.resid;
215 req->scsi->sense_len = hdr.sb_len_wr;
216 req->scsi->data_len = hdr.dxfer_len;
217
218 virtio_blk_req_complete(req, status);
219}
220#else
221static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
222{
223 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
224}
225#endif /* __linux__ */
226
c20fd872
CH
227typedef struct MultiReqBuffer {
228 BlockRequest blkreq[32];
229 unsigned int num_writes;
230} MultiReqBuffer;
231
232static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
869a5c6d 233{
91553dcc 234 int i, ret;
91553dcc 235
c20fd872
CH
236 if (!mrb->num_writes) {
237 return;
238 }
239
240 ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes);
91553dcc 241 if (ret != 0) {
c20fd872
CH
242 for (i = 0; i < mrb->num_writes; i++) {
243 if (mrb->blkreq[i].error) {
244 virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO);
91553dcc
KW
245 }
246 }
247 }
c20fd872
CH
248
249 mrb->num_writes = 0;
91553dcc 250}
87b245db 251
c20fd872 252static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
aa659be3
CH
253{
254 BlockDriverAIOCB *acb;
255
618fbb84
CH
256 /*
257 * Make sure all outstanding writes are posted to the backing device.
258 */
c20fd872 259 virtio_submit_multiwrite(req->dev->bs, mrb);
618fbb84 260
aa659be3
CH
261 acb = bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
262 if (!acb) {
263 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
264 }
265}
266
c20fd872 267static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
91553dcc 268{
c20fd872
CH
269 BlockRequest *blkreq;
270
8cfacf07
CH
271 if (req->out->sector & req->dev->sector_mask) {
272 virtio_blk_rw_complete(req, -EIO);
273 return;
274 }
275
c20fd872
CH
276 if (mrb->num_writes == 32) {
277 virtio_submit_multiwrite(req->dev->bs, mrb);
87b245db 278 }
91553dcc 279
c20fd872
CH
280 blkreq = &mrb->blkreq[mrb->num_writes];
281 blkreq->sector = req->out->sector;
282 blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE;
283 blkreq->qiov = &req->qiov;
284 blkreq->cb = virtio_blk_rw_complete;
285 blkreq->opaque = req;
286 blkreq->error = 0;
91553dcc 287
c20fd872 288 mrb->num_writes++;
d28a1b6e 289}
869a5c6d 290
d28a1b6e
AL
291static void virtio_blk_handle_read(VirtIOBlockReq *req)
292{
87b245db
CH
293 BlockDriverAIOCB *acb;
294
8cfacf07
CH
295 if (req->out->sector & req->dev->sector_mask) {
296 virtio_blk_rw_complete(req, -EIO);
297 return;
298 }
299
87b245db 300 acb = bdrv_aio_readv(req->dev->bs, req->out->sector, &req->qiov,
1573a35d
JS
301 req->qiov.size / BDRV_SECTOR_SIZE,
302 virtio_blk_rw_complete, req);
87b245db 303 if (!acb) {
6c510fbf 304 virtio_blk_rw_complete(req, -EIO);
87b245db 305 }
869a5c6d
AL
306}
307
bc6694d4
KW
308static void virtio_blk_handle_request(VirtIOBlockReq *req,
309 MultiReqBuffer *mrb)
310{
311 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
312 fprintf(stderr, "virtio-blk missing headers\n");
313 exit(1);
314 }
315
316 if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
317 req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
318 fprintf(stderr, "virtio-blk header not in correct element\n");
319 exit(1);
320 }
321
322 req->out = (void *)req->elem.out_sg[0].iov_base;
323 req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
324
325 if (req->out->type & VIRTIO_BLK_T_FLUSH) {
c20fd872 326 virtio_blk_handle_flush(req, mrb);
bc6694d4
KW
327 } else if (req->out->type & VIRTIO_BLK_T_SCSI_CMD) {
328 virtio_blk_handle_scsi(req);
2930b313 329 } else if (req->out->type & VIRTIO_BLK_T_GET_ID) {
330 VirtIOBlock *s = req->dev;
331
332 memcpy(req->elem.in_sg[0].iov_base, s->sn,
333 MIN(req->elem.in_sg[0].iov_len, sizeof(s->sn)));
334 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
bc6694d4
KW
335 } else if (req->out->type & VIRTIO_BLK_T_OUT) {
336 qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
337 req->elem.out_num - 1);
c20fd872 338 virtio_blk_handle_write(req, mrb);
bc6694d4
KW
339 } else {
340 qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
341 req->elem.in_num - 1);
342 virtio_blk_handle_read(req);
343 }
344}
345
6e02c38d
AL
346static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
347{
348 VirtIOBlock *s = to_virtio_blk(vdev);
349 VirtIOBlockReq *req;
bc6694d4
KW
350 MultiReqBuffer mrb = {
351 .num_writes = 0,
bc6694d4 352 };
6e02c38d
AL
353
354 while ((req = virtio_blk_get_request(s))) {
bc6694d4 355 virtio_blk_handle_request(req, &mrb);
6e02c38d 356 }
91553dcc 357
c20fd872 358 virtio_submit_multiwrite(s->bs, &mrb);
91553dcc 359
6e02c38d
AL
360 /*
361 * FIXME: Want to check for completions before returning to guest mode,
362 * so cached reads and writes are reported as quickly as possible. But
363 * that should be done in the generic block layer.
364 */
365}
366
213189ab 367static void virtio_blk_dma_restart_bh(void *opaque)
869a5c6d
AL
368{
369 VirtIOBlock *s = opaque;
370 VirtIOBlockReq *req = s->rq;
f1b52868
KW
371 MultiReqBuffer mrb = {
372 .num_writes = 0,
f1b52868 373 };
869a5c6d 374
213189ab
MA
375 qemu_bh_delete(s->bh);
376 s->bh = NULL;
869a5c6d
AL
377
378 s->rq = NULL;
379
380 while (req) {
f1b52868 381 virtio_blk_handle_request(req, &mrb);
869a5c6d
AL
382 req = req->next;
383 }
f1b52868 384
c20fd872 385 virtio_submit_multiwrite(s->bs, &mrb);
869a5c6d
AL
386}
387
213189ab
MA
388static void virtio_blk_dma_restart_cb(void *opaque, int running, int reason)
389{
390 VirtIOBlock *s = opaque;
391
392 if (!running)
393 return;
394
395 if (!s->bh) {
396 s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
397 qemu_bh_schedule(s->bh);
398 }
399}
400
6e02c38d
AL
401static void virtio_blk_reset(VirtIODevice *vdev)
402{
403 /*
404 * This should cancel pending requests, but can't do nicely until there
405 * are per-device request lists.
406 */
407 qemu_aio_flush();
408}
409
bf011293 410/* coalesce internal state, copy to pci i/o region 0
411 */
6e02c38d
AL
412static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
413{
414 VirtIOBlock *s = to_virtio_blk(vdev);
415 struct virtio_blk_config blkcfg;
416 uint64_t capacity;
417 int cylinders, heads, secs;
418
419 bdrv_get_geometry(s->bs, &capacity);
420 bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
5c5dafdc 421 memset(&blkcfg, 0, sizeof(blkcfg));
6e02c38d
AL
422 stq_raw(&blkcfg.capacity, capacity);
423 stl_raw(&blkcfg.seg_max, 128 - 2);
424 stw_raw(&blkcfg.cylinders, cylinders);
425 blkcfg.heads = heads;
8cfacf07
CH
426 blkcfg.sectors = secs & ~s->sector_mask;
427 blkcfg.blk_size = s->conf->logical_block_size;
c7085da7 428 blkcfg.size_max = 0;
9752c371
CH
429 blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
430 blkcfg.alignment_offset = 0;
8cfacf07
CH
431 blkcfg.min_io_size = s->conf->min_io_size / blkcfg.blk_size;
432 blkcfg.opt_io_size = s->conf->opt_io_size / blkcfg.blk_size;
37d5ddd6 433 memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
6e02c38d
AL
434}
435
8172539d 436static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
6e02c38d 437{
bf011293 438 VirtIOBlock *s = to_virtio_blk(vdev);
1063b8b1
CH
439
440 features |= (1 << VIRTIO_BLK_F_SEG_MAX);
441 features |= (1 << VIRTIO_BLK_F_GEOMETRY);
9752c371 442 features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
8cfacf07 443 features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
aa659be3
CH
444
445 if (bdrv_enable_write_cache(s->bs))
446 features |= (1 << VIRTIO_BLK_F_WCACHE);
c79662f7
NS
447
448 if (bdrv_is_read_only(s->bs))
449 features |= 1 << VIRTIO_BLK_F_RO;
1063b8b1
CH
450
451 return features;
6e02c38d
AL
452}
453
454static void virtio_blk_save(QEMUFile *f, void *opaque)
455{
456 VirtIOBlock *s = opaque;
869a5c6d
AL
457 VirtIOBlockReq *req = s->rq;
458
6e02c38d 459 virtio_save(&s->vdev, f);
869a5c6d
AL
460
461 while (req) {
462 qemu_put_sbyte(f, 1);
463 qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
464 req = req->next;
465 }
466 qemu_put_sbyte(f, 0);
6e02c38d
AL
467}
468
469static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
470{
471 VirtIOBlock *s = opaque;
472
869a5c6d 473 if (version_id != 2)
6e02c38d
AL
474 return -EINVAL;
475
476 virtio_load(&s->vdev, f);
869a5c6d
AL
477 while (qemu_get_sbyte(f)) {
478 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
479 qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
480 req->next = s->rq;
20a81e4d 481 s->rq = req;
869a5c6d 482 }
6e02c38d
AL
483
484 return 0;
485}
486
428c149b 487VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf)
6e02c38d
AL
488{
489 VirtIOBlock *s;
490 int cylinders, heads, secs;
491 static int virtio_blk_id;
2930b313 492 DriveInfo *dinfo;
cf21e106 493
d75d25e3
MA
494 if (!conf->bs) {
495 error_report("virtio-blk-pci: drive property not set");
496 return NULL;
497 }
98f28ad7
MA
498 if (!bdrv_is_inserted(conf->bs)) {
499 error_report("Device needs media, but drive is empty");
500 return NULL;
501 }
d75d25e3 502
53c25cea 503 s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
37d5ddd6 504 sizeof(struct virtio_blk_config),
53c25cea 505 sizeof(VirtIOBlock));
6e02c38d
AL
506
507 s->vdev.get_config = virtio_blk_update_config;
508 s->vdev.get_features = virtio_blk_get_features;
509 s->vdev.reset = virtio_blk_reset;
f8b6cc00 510 s->bs = conf->bs;
9752c371 511 s->conf = conf;
869a5c6d 512 s->rq = NULL;
1573a35d 513 s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
6e02c38d 514 bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
6e02c38d 515
2930b313 516 /* NB: per existing s/n string convention the string is terminated
517 * by '\0' only when less than sizeof (s->sn)
518 */
519 dinfo = drive_get_by_blockdev(s->bs);
520 strncpy(s->sn, dinfo->serial, sizeof (s->sn));
521
6e02c38d
AL
522 s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
523
869a5c6d 524 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
0be71e32 525 register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
6e02c38d 526 virtio_blk_save, virtio_blk_load, s);
7d0d6950 527 bdrv_set_removable(s->bs, 0);
6e02c38d 528
53c25cea 529 return &s->vdev;
6e02c38d 530}