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