xref: /openbmc/qemu/hw/block/virtio-blk.c (revision acb0ef58)
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 
14 #include "qemu-common.h"
15 #include "qemu/error-report.h"
16 #include "trace.h"
17 #include "hw/block/block.h"
18 #include "sysemu/blockdev.h"
19 #include "hw/virtio/virtio-blk.h"
20 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
21 # include "dataplane/virtio-blk.h"
22 # include "migration/migration.h"
23 #endif
24 #include "block/scsi.h"
25 #ifdef __linux__
26 # include <scsi/sg.h>
27 #endif
28 #include "hw/virtio/virtio-bus.h"
29 
30 typedef struct VirtIOBlockReq
31 {
32     VirtIOBlock *dev;
33     VirtQueueElement elem;
34     struct virtio_blk_inhdr *in;
35     struct virtio_blk_outhdr *out;
36     QEMUIOVector qiov;
37     struct VirtIOBlockReq *next;
38     BlockAcctCookie acct;
39 } VirtIOBlockReq;
40 
41 static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
42 {
43     VirtIOBlock *s = req->dev;
44     VirtIODevice *vdev = VIRTIO_DEVICE(s);
45 
46     trace_virtio_blk_req_complete(req, status);
47 
48     stb_p(&req->in->status, status);
49     virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
50     virtio_notify(vdev, s->vq);
51 }
52 
53 static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
54     bool is_read)
55 {
56     BlockErrorAction action = bdrv_get_error_action(req->dev->bs, is_read, error);
57     VirtIOBlock *s = req->dev;
58 
59     if (action == BDRV_ACTION_STOP) {
60         req->next = s->rq;
61         s->rq = req;
62     } else if (action == BDRV_ACTION_REPORT) {
63         virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
64         bdrv_acct_done(s->bs, &req->acct);
65         g_free(req);
66     }
67 
68     bdrv_error_action(s->bs, action, is_read, error);
69     return action != BDRV_ACTION_IGNORE;
70 }
71 
72 static void virtio_blk_rw_complete(void *opaque, int ret)
73 {
74     VirtIOBlockReq *req = opaque;
75 
76     trace_virtio_blk_rw_complete(req, ret);
77 
78     if (ret) {
79         bool is_read = !(ldl_p(&req->out->type) & VIRTIO_BLK_T_OUT);
80         if (virtio_blk_handle_rw_error(req, -ret, is_read))
81             return;
82     }
83 
84     virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
85     bdrv_acct_done(req->dev->bs, &req->acct);
86     g_free(req);
87 }
88 
89 static void virtio_blk_flush_complete(void *opaque, int ret)
90 {
91     VirtIOBlockReq *req = opaque;
92 
93     if (ret) {
94         if (virtio_blk_handle_rw_error(req, -ret, 0)) {
95             return;
96         }
97     }
98 
99     virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
100     bdrv_acct_done(req->dev->bs, &req->acct);
101     g_free(req);
102 }
103 
104 static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
105 {
106     VirtIOBlockReq *req = g_malloc(sizeof(*req));
107     req->dev = s;
108     req->qiov.size = 0;
109     req->next = NULL;
110     return req;
111 }
112 
113 static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
114 {
115     VirtIOBlockReq *req = virtio_blk_alloc_request(s);
116 
117     if (req != NULL) {
118         if (!virtqueue_pop(s->vq, &req->elem)) {
119             g_free(req);
120             return NULL;
121         }
122     }
123 
124     return req;
125 }
126 
127 int virtio_blk_handle_scsi_req(VirtIOBlock *blk,
128                                VirtQueueElement *elem)
129 {
130     int status = VIRTIO_BLK_S_OK;
131     struct virtio_scsi_inhdr *scsi = NULL;
132 #ifdef __linux__
133     int i;
134     struct sg_io_hdr hdr;
135 #endif
136 
137     /*
138      * We require at least one output segment each for the virtio_blk_outhdr
139      * and the SCSI command block.
140      *
141      * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
142      * and the sense buffer pointer in the input segments.
143      */
144     if (elem->out_num < 2 || elem->in_num < 3) {
145         status = VIRTIO_BLK_S_IOERR;
146         goto fail;
147     }
148 
149     /*
150      * The scsi inhdr is placed in the second-to-last input segment, just
151      * before the regular inhdr.
152      */
153     scsi = (void *)elem->in_sg[elem->in_num - 2].iov_base;
154 
155     if (!blk->blk.scsi) {
156         status = VIRTIO_BLK_S_UNSUPP;
157         goto fail;
158     }
159 
160     /*
161      * No support for bidirection commands yet.
162      */
163     if (elem->out_num > 2 && elem->in_num > 3) {
164         status = VIRTIO_BLK_S_UNSUPP;
165         goto fail;
166     }
167 
168 #ifdef __linux__
169     memset(&hdr, 0, sizeof(struct sg_io_hdr));
170     hdr.interface_id = 'S';
171     hdr.cmd_len = elem->out_sg[1].iov_len;
172     hdr.cmdp = elem->out_sg[1].iov_base;
173     hdr.dxfer_len = 0;
174 
175     if (elem->out_num > 2) {
176         /*
177          * If there are more than the minimally required 2 output segments
178          * there is write payload starting from the third iovec.
179          */
180         hdr.dxfer_direction = SG_DXFER_TO_DEV;
181         hdr.iovec_count = elem->out_num - 2;
182 
183         for (i = 0; i < hdr.iovec_count; i++)
184             hdr.dxfer_len += elem->out_sg[i + 2].iov_len;
185 
186         hdr.dxferp = elem->out_sg + 2;
187 
188     } else if (elem->in_num > 3) {
189         /*
190          * If we have more than 3 input segments the guest wants to actually
191          * read data.
192          */
193         hdr.dxfer_direction = SG_DXFER_FROM_DEV;
194         hdr.iovec_count = elem->in_num - 3;
195         for (i = 0; i < hdr.iovec_count; i++)
196             hdr.dxfer_len += elem->in_sg[i].iov_len;
197 
198         hdr.dxferp = elem->in_sg;
199     } else {
200         /*
201          * Some SCSI commands don't actually transfer any data.
202          */
203         hdr.dxfer_direction = SG_DXFER_NONE;
204     }
205 
206     hdr.sbp = elem->in_sg[elem->in_num - 3].iov_base;
207     hdr.mx_sb_len = elem->in_sg[elem->in_num - 3].iov_len;
208 
209     status = bdrv_ioctl(blk->bs, SG_IO, &hdr);
210     if (status) {
211         status = VIRTIO_BLK_S_UNSUPP;
212         goto fail;
213     }
214 
215     /*
216      * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
217      * clear the masked_status field [hence status gets cleared too, see
218      * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
219      * status has occurred.  However they do set DRIVER_SENSE in driver_status
220      * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
221      */
222     if (hdr.status == 0 && hdr.sb_len_wr > 0) {
223         hdr.status = CHECK_CONDITION;
224     }
225 
226     stl_p(&scsi->errors,
227           hdr.status | (hdr.msg_status << 8) |
228           (hdr.host_status << 16) | (hdr.driver_status << 24));
229     stl_p(&scsi->residual, hdr.resid);
230     stl_p(&scsi->sense_len, hdr.sb_len_wr);
231     stl_p(&scsi->data_len, hdr.dxfer_len);
232 
233     return status;
234 #else
235     abort();
236 #endif
237 
238 fail:
239     /* Just put anything nonzero so that the ioctl fails in the guest.  */
240     if (scsi) {
241         stl_p(&scsi->errors, 255);
242     }
243     return status;
244 }
245 
246 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
247 {
248     int status;
249 
250     status = virtio_blk_handle_scsi_req(req->dev, &req->elem);
251     virtio_blk_req_complete(req, status);
252     g_free(req);
253 }
254 
255 typedef struct MultiReqBuffer {
256     BlockRequest        blkreq[32];
257     unsigned int        num_writes;
258 } MultiReqBuffer;
259 
260 static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
261 {
262     int i, ret;
263 
264     if (!mrb->num_writes) {
265         return;
266     }
267 
268     ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes);
269     if (ret != 0) {
270         for (i = 0; i < mrb->num_writes; i++) {
271             if (mrb->blkreq[i].error) {
272                 virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO);
273             }
274         }
275     }
276 
277     mrb->num_writes = 0;
278 }
279 
280 static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
281 {
282     bdrv_acct_start(req->dev->bs, &req->acct, 0, BDRV_ACCT_FLUSH);
283 
284     /*
285      * Make sure all outstanding writes are posted to the backing device.
286      */
287     virtio_submit_multiwrite(req->dev->bs, mrb);
288     bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
289 }
290 
291 static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
292 {
293     BlockRequest *blkreq;
294     uint64_t sector;
295 
296     sector = ldq_p(&req->out->sector);
297 
298     bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE);
299 
300     trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512);
301 
302     if (sector & req->dev->sector_mask) {
303         virtio_blk_rw_complete(req, -EIO);
304         return;
305     }
306     if (req->qiov.size % req->dev->conf->logical_block_size) {
307         virtio_blk_rw_complete(req, -EIO);
308         return;
309     }
310 
311     if (mrb->num_writes == 32) {
312         virtio_submit_multiwrite(req->dev->bs, mrb);
313     }
314 
315     blkreq = &mrb->blkreq[mrb->num_writes];
316     blkreq->sector = sector;
317     blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE;
318     blkreq->qiov = &req->qiov;
319     blkreq->cb = virtio_blk_rw_complete;
320     blkreq->opaque = req;
321     blkreq->error = 0;
322 
323     mrb->num_writes++;
324 }
325 
326 static void virtio_blk_handle_read(VirtIOBlockReq *req)
327 {
328     uint64_t sector;
329 
330     sector = ldq_p(&req->out->sector);
331 
332     bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ);
333 
334     trace_virtio_blk_handle_read(req, sector, req->qiov.size / 512);
335 
336     if (sector & req->dev->sector_mask) {
337         virtio_blk_rw_complete(req, -EIO);
338         return;
339     }
340     if (req->qiov.size % req->dev->conf->logical_block_size) {
341         virtio_blk_rw_complete(req, -EIO);
342         return;
343     }
344     bdrv_aio_readv(req->dev->bs, sector, &req->qiov,
345                    req->qiov.size / BDRV_SECTOR_SIZE,
346                    virtio_blk_rw_complete, req);
347 }
348 
349 static void virtio_blk_handle_request(VirtIOBlockReq *req,
350     MultiReqBuffer *mrb)
351 {
352     uint32_t type;
353 
354     if (req->elem.out_num < 1 || req->elem.in_num < 1) {
355         error_report("virtio-blk missing headers");
356         exit(1);
357     }
358 
359     if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
360         req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
361         error_report("virtio-blk header not in correct element");
362         exit(1);
363     }
364 
365     req->out = (void *)req->elem.out_sg[0].iov_base;
366     req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
367 
368     type = ldl_p(&req->out->type);
369 
370     if (type & VIRTIO_BLK_T_FLUSH) {
371         virtio_blk_handle_flush(req, mrb);
372     } else if (type & VIRTIO_BLK_T_SCSI_CMD) {
373         virtio_blk_handle_scsi(req);
374     } else if (type & VIRTIO_BLK_T_GET_ID) {
375         VirtIOBlock *s = req->dev;
376 
377         /*
378          * NB: per existing s/n string convention the string is
379          * terminated by '\0' only when shorter than buffer.
380          */
381         strncpy(req->elem.in_sg[0].iov_base,
382                 s->blk.serial ? s->blk.serial : "",
383                 MIN(req->elem.in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES));
384         virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
385         g_free(req);
386     } else if (type & VIRTIO_BLK_T_OUT) {
387         qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
388                                  req->elem.out_num - 1);
389         virtio_blk_handle_write(req, mrb);
390     } else if (type == VIRTIO_BLK_T_IN || type == VIRTIO_BLK_T_BARRIER) {
391         /* VIRTIO_BLK_T_IN is 0, so we can't just & it. */
392         qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
393                                  req->elem.in_num - 1);
394         virtio_blk_handle_read(req);
395     } else {
396         virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
397         g_free(req);
398     }
399 }
400 
401 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
402 {
403     VirtIOBlock *s = VIRTIO_BLK(vdev);
404     VirtIOBlockReq *req;
405     MultiReqBuffer mrb = {
406         .num_writes = 0,
407     };
408 
409 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
410     /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
411      * dataplane here instead of waiting for .set_status().
412      */
413     if (s->dataplane) {
414         virtio_blk_data_plane_start(s->dataplane);
415         return;
416     }
417 #endif
418 
419     while ((req = virtio_blk_get_request(s))) {
420         virtio_blk_handle_request(req, &mrb);
421     }
422 
423     virtio_submit_multiwrite(s->bs, &mrb);
424 
425     /*
426      * FIXME: Want to check for completions before returning to guest mode,
427      * so cached reads and writes are reported as quickly as possible. But
428      * that should be done in the generic block layer.
429      */
430 }
431 
432 static void virtio_blk_dma_restart_bh(void *opaque)
433 {
434     VirtIOBlock *s = opaque;
435     VirtIOBlockReq *req = s->rq;
436     MultiReqBuffer mrb = {
437         .num_writes = 0,
438     };
439 
440     qemu_bh_delete(s->bh);
441     s->bh = NULL;
442 
443     s->rq = NULL;
444 
445     while (req) {
446         virtio_blk_handle_request(req, &mrb);
447         req = req->next;
448     }
449 
450     virtio_submit_multiwrite(s->bs, &mrb);
451 }
452 
453 static void virtio_blk_dma_restart_cb(void *opaque, int running,
454                                       RunState state)
455 {
456     VirtIOBlock *s = opaque;
457 
458     if (!running) {
459         return;
460     }
461 
462     if (!s->bh) {
463         s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
464         qemu_bh_schedule(s->bh);
465     }
466 }
467 
468 static void virtio_blk_reset(VirtIODevice *vdev)
469 {
470     VirtIOBlock *s = VIRTIO_BLK(vdev);
471 
472 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
473     if (s->dataplane) {
474         virtio_blk_data_plane_stop(s->dataplane);
475     }
476 #endif
477 
478     /*
479      * This should cancel pending requests, but can't do nicely until there
480      * are per-device request lists.
481      */
482     bdrv_drain_all();
483     bdrv_set_enable_write_cache(s->bs, s->original_wce);
484 }
485 
486 /* coalesce internal state, copy to pci i/o region 0
487  */
488 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
489 {
490     VirtIOBlock *s = VIRTIO_BLK(vdev);
491     struct virtio_blk_config blkcfg;
492     uint64_t capacity;
493     int blk_size = s->conf->logical_block_size;
494 
495     bdrv_get_geometry(s->bs, &capacity);
496     memset(&blkcfg, 0, sizeof(blkcfg));
497     stq_p(&blkcfg.capacity, capacity);
498     stl_p(&blkcfg.seg_max, 128 - 2);
499     stw_p(&blkcfg.cylinders, s->conf->cyls);
500     stl_p(&blkcfg.blk_size, blk_size);
501     stw_p(&blkcfg.min_io_size, s->conf->min_io_size / blk_size);
502     stw_p(&blkcfg.opt_io_size, s->conf->opt_io_size / blk_size);
503     blkcfg.heads = s->conf->heads;
504     /*
505      * We must ensure that the block device capacity is a multiple of
506      * the logical block size. If that is not the case, let's use
507      * sector_mask to adopt the geometry to have a correct picture.
508      * For those devices where the capacity is ok for the given geometry
509      * we don't touch the sector value of the geometry, since some devices
510      * (like s390 dasd) need a specific value. Here the capacity is already
511      * cyls*heads*secs*blk_size and the sector value is not block size
512      * divided by 512 - instead it is the amount of blk_size blocks
513      * per track (cylinder).
514      */
515     if (bdrv_getlength(s->bs) /  s->conf->heads / s->conf->secs % blk_size) {
516         blkcfg.sectors = s->conf->secs & ~s->sector_mask;
517     } else {
518         blkcfg.sectors = s->conf->secs;
519     }
520     blkcfg.size_max = 0;
521     blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
522     blkcfg.alignment_offset = 0;
523     blkcfg.wce = bdrv_enable_write_cache(s->bs);
524     memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
525 }
526 
527 static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
528 {
529     VirtIOBlock *s = VIRTIO_BLK(vdev);
530     struct virtio_blk_config blkcfg;
531 
532     memcpy(&blkcfg, config, sizeof(blkcfg));
533 
534     aio_context_acquire(bdrv_get_aio_context(s->bs));
535     bdrv_set_enable_write_cache(s->bs, blkcfg.wce != 0);
536     aio_context_release(bdrv_get_aio_context(s->bs));
537 }
538 
539 static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
540 {
541     VirtIOBlock *s = VIRTIO_BLK(vdev);
542 
543     features |= (1 << VIRTIO_BLK_F_SEG_MAX);
544     features |= (1 << VIRTIO_BLK_F_GEOMETRY);
545     features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
546     features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
547     features |= (1 << VIRTIO_BLK_F_SCSI);
548 
549     if (s->blk.config_wce) {
550         features |= (1 << VIRTIO_BLK_F_CONFIG_WCE);
551     }
552     if (bdrv_enable_write_cache(s->bs))
553         features |= (1 << VIRTIO_BLK_F_WCE);
554 
555     if (bdrv_is_read_only(s->bs))
556         features |= 1 << VIRTIO_BLK_F_RO;
557 
558     return features;
559 }
560 
561 static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
562 {
563     VirtIOBlock *s = VIRTIO_BLK(vdev);
564     uint32_t features;
565 
566 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
567     if (s->dataplane && !(status & (VIRTIO_CONFIG_S_DRIVER |
568                                     VIRTIO_CONFIG_S_DRIVER_OK))) {
569         virtio_blk_data_plane_stop(s->dataplane);
570     }
571 #endif
572 
573     if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
574         return;
575     }
576 
577     features = vdev->guest_features;
578 
579     /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
580      * cache flushes.  Thus, the "auto writethrough" behavior is never
581      * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
582      * Leaving it enabled would break the following sequence:
583      *
584      *     Guest started with "-drive cache=writethrough"
585      *     Guest sets status to 0
586      *     Guest sets DRIVER bit in status field
587      *     Guest reads host features (WCE=0, CONFIG_WCE=1)
588      *     Guest writes guest features (WCE=0, CONFIG_WCE=1)
589      *     Guest writes 1 to the WCE configuration field (writeback mode)
590      *     Guest sets DRIVER_OK bit in status field
591      *
592      * s->bs would erroneously be placed in writethrough mode.
593      */
594     if (!(features & (1 << VIRTIO_BLK_F_CONFIG_WCE))) {
595         aio_context_acquire(bdrv_get_aio_context(s->bs));
596         bdrv_set_enable_write_cache(s->bs,
597                                     !!(features & (1 << VIRTIO_BLK_F_WCE)));
598         aio_context_release(bdrv_get_aio_context(s->bs));
599     }
600 }
601 
602 static void virtio_blk_save(QEMUFile *f, void *opaque)
603 {
604     VirtIOBlock *s = opaque;
605     VirtIODevice *vdev = VIRTIO_DEVICE(s);
606     VirtIOBlockReq *req = s->rq;
607 
608     virtio_save(vdev, f);
609 
610     while (req) {
611         qemu_put_sbyte(f, 1);
612         qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
613         req = req->next;
614     }
615     qemu_put_sbyte(f, 0);
616 }
617 
618 static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
619 {
620     VirtIOBlock *s = opaque;
621     VirtIODevice *vdev = VIRTIO_DEVICE(s);
622     int ret;
623 
624     if (version_id != 2)
625         return -EINVAL;
626 
627     ret = virtio_load(vdev, f);
628     if (ret) {
629         return ret;
630     }
631 
632     while (qemu_get_sbyte(f)) {
633         VirtIOBlockReq *req = virtio_blk_alloc_request(s);
634         qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
635         req->next = s->rq;
636         s->rq = req;
637 
638         virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr,
639             req->elem.in_num, 1);
640         virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr,
641             req->elem.out_num, 0);
642     }
643 
644     return 0;
645 }
646 
647 static void virtio_blk_resize(void *opaque)
648 {
649     VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
650 
651     virtio_notify_config(vdev);
652 }
653 
654 static const BlockDevOps virtio_block_ops = {
655     .resize_cb = virtio_blk_resize,
656 };
657 
658 void virtio_blk_set_conf(DeviceState *dev, VirtIOBlkConf *blk)
659 {
660     VirtIOBlock *s = VIRTIO_BLK(dev);
661     memcpy(&(s->blk), blk, sizeof(struct VirtIOBlkConf));
662 }
663 
664 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
665 /* Disable dataplane thread during live migration since it does not
666  * update the dirty memory bitmap yet.
667  */
668 static void virtio_blk_migration_state_changed(Notifier *notifier, void *data)
669 {
670     VirtIOBlock *s = container_of(notifier, VirtIOBlock,
671                                   migration_state_notifier);
672     MigrationState *mig = data;
673     Error *err = NULL;
674 
675     if (migration_in_setup(mig)) {
676         if (!s->dataplane) {
677             return;
678         }
679         virtio_blk_data_plane_destroy(s->dataplane);
680         s->dataplane = NULL;
681     } else if (migration_has_finished(mig) ||
682                migration_has_failed(mig)) {
683         if (s->dataplane) {
684             return;
685         }
686         bdrv_drain_all(); /* complete in-flight non-dataplane requests */
687         virtio_blk_data_plane_create(VIRTIO_DEVICE(s), &s->blk,
688                                      &s->dataplane, &err);
689         if (err != NULL) {
690             error_report("%s", error_get_pretty(err));
691             error_free(err);
692         }
693     }
694 }
695 #endif /* CONFIG_VIRTIO_BLK_DATA_PLANE */
696 
697 static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
698 {
699     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
700     VirtIOBlock *s = VIRTIO_BLK(dev);
701     VirtIOBlkConf *blk = &(s->blk);
702 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
703     Error *err = NULL;
704 #endif
705     static int virtio_blk_id;
706 
707     if (!blk->conf.bs) {
708         error_setg(errp, "drive property not set");
709         return;
710     }
711     if (!bdrv_is_inserted(blk->conf.bs)) {
712         error_setg(errp, "Device needs media, but drive is empty");
713         return;
714     }
715 
716     blkconf_serial(&blk->conf, &blk->serial);
717     s->original_wce = bdrv_enable_write_cache(blk->conf.bs);
718     if (blkconf_geometry(&blk->conf, NULL, 65535, 255, 255) < 0) {
719         error_setg(errp, "Error setting geometry");
720         return;
721     }
722 
723     virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
724                 sizeof(struct virtio_blk_config));
725 
726     s->bs = blk->conf.bs;
727     s->conf = &blk->conf;
728     s->rq = NULL;
729     s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
730 
731     s->vq = virtio_add_queue(vdev, 128, virtio_blk_handle_output);
732 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
733     virtio_blk_data_plane_create(vdev, blk, &s->dataplane, &err);
734     if (err != NULL) {
735         error_propagate(errp, err);
736         virtio_cleanup(vdev);
737         return;
738     }
739     s->migration_state_notifier.notify = virtio_blk_migration_state_changed;
740     add_migration_state_change_notifier(&s->migration_state_notifier);
741 #endif
742 
743     s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
744     register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
745                     virtio_blk_save, virtio_blk_load, s);
746     bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
747     bdrv_set_guest_block_size(s->bs, s->conf->logical_block_size);
748 
749     bdrv_iostatus_enable(s->bs);
750 
751     add_boot_device_path(s->conf->bootindex, dev, "/disk@0,0");
752 }
753 
754 static void virtio_blk_device_unrealize(DeviceState *dev, Error **errp)
755 {
756     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
757     VirtIOBlock *s = VIRTIO_BLK(dev);
758 
759 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
760     remove_migration_state_change_notifier(&s->migration_state_notifier);
761     virtio_blk_data_plane_destroy(s->dataplane);
762     s->dataplane = NULL;
763 #endif
764     qemu_del_vm_change_state_handler(s->change);
765     unregister_savevm(dev, "virtio-blk", s);
766     blockdev_mark_auto_del(s->bs);
767     virtio_cleanup(vdev);
768 }
769 
770 static Property virtio_blk_properties[] = {
771     DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlock, blk),
772     DEFINE_PROP_END_OF_LIST(),
773 };
774 
775 static void virtio_blk_class_init(ObjectClass *klass, void *data)
776 {
777     DeviceClass *dc = DEVICE_CLASS(klass);
778     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
779 
780     dc->props = virtio_blk_properties;
781     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
782     vdc->realize = virtio_blk_device_realize;
783     vdc->unrealize = virtio_blk_device_unrealize;
784     vdc->get_config = virtio_blk_update_config;
785     vdc->set_config = virtio_blk_set_config;
786     vdc->get_features = virtio_blk_get_features;
787     vdc->set_status = virtio_blk_set_status;
788     vdc->reset = virtio_blk_reset;
789 }
790 
791 static const TypeInfo virtio_device_info = {
792     .name = TYPE_VIRTIO_BLK,
793     .parent = TYPE_VIRTIO_DEVICE,
794     .instance_size = sizeof(VirtIOBlock),
795     .class_init = virtio_blk_class_init,
796 };
797 
798 static void virtio_register_types(void)
799 {
800     type_register_static(&virtio_device_info);
801 }
802 
803 type_init(virtio_register_types)
804