xref: /openbmc/qemu/hw/block/virtio-blk.c (revision 701bff24)
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/osdep.h"
15 #include "qapi/error.h"
16 #include "qemu/iov.h"
17 #include "qemu/module.h"
18 #include "qemu/error-report.h"
19 #include "qemu/main-loop.h"
20 #include "trace.h"
21 #include "hw/block/block.h"
22 #include "hw/qdev-properties.h"
23 #include "sysemu/blockdev.h"
24 #include "sysemu/sysemu.h"
25 #include "sysemu/runstate.h"
26 #include "hw/virtio/virtio-blk.h"
27 #include "dataplane/virtio-blk.h"
28 #include "scsi/constants.h"
29 #ifdef __linux__
30 # include <scsi/sg.h>
31 #endif
32 #include "hw/virtio/virtio-bus.h"
33 #include "migration/qemu-file-types.h"
34 #include "hw/virtio/virtio-access.h"
35 #include "hw/virtio/virtio-blk-common.h"
36 #include "qemu/coroutine.h"
37 
38 static void virtio_blk_init_request(VirtIOBlock *s, VirtQueue *vq,
39                                     VirtIOBlockReq *req)
40 {
41     req->dev = s;
42     req->vq = vq;
43     req->qiov.size = 0;
44     req->in_len = 0;
45     req->next = NULL;
46     req->mr_next = NULL;
47 }
48 
49 static void virtio_blk_free_request(VirtIOBlockReq *req)
50 {
51     g_free(req);
52 }
53 
54 static void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status)
55 {
56     VirtIOBlock *s = req->dev;
57     VirtIODevice *vdev = VIRTIO_DEVICE(s);
58 
59     trace_virtio_blk_req_complete(vdev, req, status);
60 
61     stb_p(&req->in->status, status);
62     iov_discard_undo(&req->inhdr_undo);
63     iov_discard_undo(&req->outhdr_undo);
64     virtqueue_push(req->vq, &req->elem, req->in_len);
65     if (s->dataplane_started && !s->dataplane_disabled) {
66         virtio_blk_data_plane_notify(s->dataplane, req->vq);
67     } else {
68         virtio_notify(vdev, req->vq);
69     }
70 }
71 
72 static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
73     bool is_read, bool acct_failed)
74 {
75     VirtIOBlock *s = req->dev;
76     BlockErrorAction action = blk_get_error_action(s->blk, is_read, error);
77 
78     if (action == BLOCK_ERROR_ACTION_STOP) {
79         /* Break the link as the next request is going to be parsed from the
80          * ring again. Otherwise we may end up doing a double completion! */
81         req->mr_next = NULL;
82         req->next = s->rq;
83         s->rq = req;
84     } else if (action == BLOCK_ERROR_ACTION_REPORT) {
85         virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
86         if (acct_failed) {
87             block_acct_failed(blk_get_stats(s->blk), &req->acct);
88         }
89         virtio_blk_free_request(req);
90     }
91 
92     blk_error_action(s->blk, action, is_read, error);
93     return action != BLOCK_ERROR_ACTION_IGNORE;
94 }
95 
96 static void virtio_blk_rw_complete(void *opaque, int ret)
97 {
98     VirtIOBlockReq *next = opaque;
99     VirtIOBlock *s = next->dev;
100     VirtIODevice *vdev = VIRTIO_DEVICE(s);
101 
102     aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
103     while (next) {
104         VirtIOBlockReq *req = next;
105         next = req->mr_next;
106         trace_virtio_blk_rw_complete(vdev, req, ret);
107 
108         if (req->qiov.nalloc != -1) {
109             /* If nalloc is != -1 req->qiov is a local copy of the original
110              * external iovec. It was allocated in submit_requests to be
111              * able to merge requests. */
112             qemu_iovec_destroy(&req->qiov);
113         }
114 
115         if (ret) {
116             int p = virtio_ldl_p(VIRTIO_DEVICE(s), &req->out.type);
117             bool is_read = !(p & VIRTIO_BLK_T_OUT);
118             /* Note that memory may be dirtied on read failure.  If the
119              * virtio request is not completed here, as is the case for
120              * BLOCK_ERROR_ACTION_STOP, the memory may not be copied
121              * correctly during live migration.  While this is ugly,
122              * it is acceptable because the device is free to write to
123              * the memory until the request is completed (which will
124              * happen on the other side of the migration).
125              */
126             if (virtio_blk_handle_rw_error(req, -ret, is_read, true)) {
127                 continue;
128             }
129         }
130 
131         virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
132         block_acct_done(blk_get_stats(s->blk), &req->acct);
133         virtio_blk_free_request(req);
134     }
135     aio_context_release(blk_get_aio_context(s->conf.conf.blk));
136 }
137 
138 static void virtio_blk_flush_complete(void *opaque, int ret)
139 {
140     VirtIOBlockReq *req = opaque;
141     VirtIOBlock *s = req->dev;
142 
143     aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
144     if (ret) {
145         if (virtio_blk_handle_rw_error(req, -ret, 0, true)) {
146             goto out;
147         }
148     }
149 
150     virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
151     block_acct_done(blk_get_stats(s->blk), &req->acct);
152     virtio_blk_free_request(req);
153 
154 out:
155     aio_context_release(blk_get_aio_context(s->conf.conf.blk));
156 }
157 
158 static void virtio_blk_discard_write_zeroes_complete(void *opaque, int ret)
159 {
160     VirtIOBlockReq *req = opaque;
161     VirtIOBlock *s = req->dev;
162     bool is_write_zeroes = (virtio_ldl_p(VIRTIO_DEVICE(s), &req->out.type) &
163                             ~VIRTIO_BLK_T_BARRIER) == VIRTIO_BLK_T_WRITE_ZEROES;
164 
165     aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
166     if (ret) {
167         if (virtio_blk_handle_rw_error(req, -ret, false, is_write_zeroes)) {
168             goto out;
169         }
170     }
171 
172     virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
173     if (is_write_zeroes) {
174         block_acct_done(blk_get_stats(s->blk), &req->acct);
175     }
176     virtio_blk_free_request(req);
177 
178 out:
179     aio_context_release(blk_get_aio_context(s->conf.conf.blk));
180 }
181 
182 #ifdef __linux__
183 
184 typedef struct {
185     VirtIOBlockReq *req;
186     struct sg_io_hdr hdr;
187 } VirtIOBlockIoctlReq;
188 
189 static void virtio_blk_ioctl_complete(void *opaque, int status)
190 {
191     VirtIOBlockIoctlReq *ioctl_req = opaque;
192     VirtIOBlockReq *req = ioctl_req->req;
193     VirtIOBlock *s = req->dev;
194     VirtIODevice *vdev = VIRTIO_DEVICE(s);
195     struct virtio_scsi_inhdr *scsi;
196     struct sg_io_hdr *hdr;
197 
198     scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
199 
200     if (status) {
201         status = VIRTIO_BLK_S_UNSUPP;
202         virtio_stl_p(vdev, &scsi->errors, 255);
203         goto out;
204     }
205 
206     hdr = &ioctl_req->hdr;
207     /*
208      * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
209      * clear the masked_status field [hence status gets cleared too, see
210      * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
211      * status has occurred.  However they do set DRIVER_SENSE in driver_status
212      * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
213      */
214     if (hdr->status == 0 && hdr->sb_len_wr > 0) {
215         hdr->status = CHECK_CONDITION;
216     }
217 
218     virtio_stl_p(vdev, &scsi->errors,
219                  hdr->status | (hdr->msg_status << 8) |
220                  (hdr->host_status << 16) | (hdr->driver_status << 24));
221     virtio_stl_p(vdev, &scsi->residual, hdr->resid);
222     virtio_stl_p(vdev, &scsi->sense_len, hdr->sb_len_wr);
223     virtio_stl_p(vdev, &scsi->data_len, hdr->dxfer_len);
224 
225 out:
226     aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
227     virtio_blk_req_complete(req, status);
228     virtio_blk_free_request(req);
229     aio_context_release(blk_get_aio_context(s->conf.conf.blk));
230     g_free(ioctl_req);
231 }
232 
233 #endif
234 
235 static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s, VirtQueue *vq)
236 {
237     VirtIOBlockReq *req = virtqueue_pop(vq, sizeof(VirtIOBlockReq));
238 
239     if (req) {
240         virtio_blk_init_request(s, vq, req);
241     }
242     return req;
243 }
244 
245 static int virtio_blk_handle_scsi_req(VirtIOBlockReq *req)
246 {
247     int status = VIRTIO_BLK_S_OK;
248     struct virtio_scsi_inhdr *scsi = NULL;
249     VirtIOBlock *blk = req->dev;
250     VirtIODevice *vdev = VIRTIO_DEVICE(blk);
251     VirtQueueElement *elem = &req->elem;
252 
253 #ifdef __linux__
254     int i;
255     VirtIOBlockIoctlReq *ioctl_req;
256     BlockAIOCB *acb;
257 #endif
258 
259     /*
260      * We require at least one output segment each for the virtio_blk_outhdr
261      * and the SCSI command block.
262      *
263      * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
264      * and the sense buffer pointer in the input segments.
265      */
266     if (elem->out_num < 2 || elem->in_num < 3) {
267         status = VIRTIO_BLK_S_IOERR;
268         goto fail;
269     }
270 
271     /*
272      * The scsi inhdr is placed in the second-to-last input segment, just
273      * before the regular inhdr.
274      */
275     scsi = (void *)elem->in_sg[elem->in_num - 2].iov_base;
276 
277     if (!virtio_has_feature(blk->host_features, VIRTIO_BLK_F_SCSI)) {
278         status = VIRTIO_BLK_S_UNSUPP;
279         goto fail;
280     }
281 
282     /*
283      * No support for bidirection commands yet.
284      */
285     if (elem->out_num > 2 && elem->in_num > 3) {
286         status = VIRTIO_BLK_S_UNSUPP;
287         goto fail;
288     }
289 
290 #ifdef __linux__
291     ioctl_req = g_new0(VirtIOBlockIoctlReq, 1);
292     ioctl_req->req = req;
293     ioctl_req->hdr.interface_id = 'S';
294     ioctl_req->hdr.cmd_len = elem->out_sg[1].iov_len;
295     ioctl_req->hdr.cmdp = elem->out_sg[1].iov_base;
296     ioctl_req->hdr.dxfer_len = 0;
297 
298     if (elem->out_num > 2) {
299         /*
300          * If there are more than the minimally required 2 output segments
301          * there is write payload starting from the third iovec.
302          */
303         ioctl_req->hdr.dxfer_direction = SG_DXFER_TO_DEV;
304         ioctl_req->hdr.iovec_count = elem->out_num - 2;
305 
306         for (i = 0; i < ioctl_req->hdr.iovec_count; i++) {
307             ioctl_req->hdr.dxfer_len += elem->out_sg[i + 2].iov_len;
308         }
309 
310         ioctl_req->hdr.dxferp = elem->out_sg + 2;
311 
312     } else if (elem->in_num > 3) {
313         /*
314          * If we have more than 3 input segments the guest wants to actually
315          * read data.
316          */
317         ioctl_req->hdr.dxfer_direction = SG_DXFER_FROM_DEV;
318         ioctl_req->hdr.iovec_count = elem->in_num - 3;
319         for (i = 0; i < ioctl_req->hdr.iovec_count; i++) {
320             ioctl_req->hdr.dxfer_len += elem->in_sg[i].iov_len;
321         }
322 
323         ioctl_req->hdr.dxferp = elem->in_sg;
324     } else {
325         /*
326          * Some SCSI commands don't actually transfer any data.
327          */
328         ioctl_req->hdr.dxfer_direction = SG_DXFER_NONE;
329     }
330 
331     ioctl_req->hdr.sbp = elem->in_sg[elem->in_num - 3].iov_base;
332     ioctl_req->hdr.mx_sb_len = elem->in_sg[elem->in_num - 3].iov_len;
333 
334     acb = blk_aio_ioctl(blk->blk, SG_IO, &ioctl_req->hdr,
335                         virtio_blk_ioctl_complete, ioctl_req);
336     if (!acb) {
337         g_free(ioctl_req);
338         status = VIRTIO_BLK_S_UNSUPP;
339         goto fail;
340     }
341     return -EINPROGRESS;
342 #else
343     abort();
344 #endif
345 
346 fail:
347     /* Just put anything nonzero so that the ioctl fails in the guest.  */
348     if (scsi) {
349         virtio_stl_p(vdev, &scsi->errors, 255);
350     }
351     return status;
352 }
353 
354 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
355 {
356     int status;
357 
358     status = virtio_blk_handle_scsi_req(req);
359     if (status != -EINPROGRESS) {
360         virtio_blk_req_complete(req, status);
361         virtio_blk_free_request(req);
362     }
363 }
364 
365 static inline void submit_requests(BlockBackend *blk, MultiReqBuffer *mrb,
366                                    int start, int num_reqs, int niov)
367 {
368     QEMUIOVector *qiov = &mrb->reqs[start]->qiov;
369     int64_t sector_num = mrb->reqs[start]->sector_num;
370     bool is_write = mrb->is_write;
371 
372     if (num_reqs > 1) {
373         int i;
374         struct iovec *tmp_iov = qiov->iov;
375         int tmp_niov = qiov->niov;
376 
377         /* mrb->reqs[start]->qiov was initialized from external so we can't
378          * modify it here. We need to initialize it locally and then add the
379          * external iovecs. */
380         qemu_iovec_init(qiov, niov);
381 
382         for (i = 0; i < tmp_niov; i++) {
383             qemu_iovec_add(qiov, tmp_iov[i].iov_base, tmp_iov[i].iov_len);
384         }
385 
386         for (i = start + 1; i < start + num_reqs; i++) {
387             qemu_iovec_concat(qiov, &mrb->reqs[i]->qiov, 0,
388                               mrb->reqs[i]->qiov.size);
389             mrb->reqs[i - 1]->mr_next = mrb->reqs[i];
390         }
391 
392         trace_virtio_blk_submit_multireq(VIRTIO_DEVICE(mrb->reqs[start]->dev),
393                                          mrb, start, num_reqs,
394                                          sector_num << BDRV_SECTOR_BITS,
395                                          qiov->size, is_write);
396         block_acct_merge_done(blk_get_stats(blk),
397                               is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ,
398                               num_reqs - 1);
399     }
400 
401     if (is_write) {
402         blk_aio_pwritev(blk, sector_num << BDRV_SECTOR_BITS, qiov, 0,
403                         virtio_blk_rw_complete, mrb->reqs[start]);
404     } else {
405         blk_aio_preadv(blk, sector_num << BDRV_SECTOR_BITS, qiov, 0,
406                        virtio_blk_rw_complete, mrb->reqs[start]);
407     }
408 }
409 
410 static int multireq_compare(const void *a, const void *b)
411 {
412     const VirtIOBlockReq *req1 = *(VirtIOBlockReq **)a,
413                          *req2 = *(VirtIOBlockReq **)b;
414 
415     /*
416      * Note that we can't simply subtract sector_num1 from sector_num2
417      * here as that could overflow the return value.
418      */
419     if (req1->sector_num > req2->sector_num) {
420         return 1;
421     } else if (req1->sector_num < req2->sector_num) {
422         return -1;
423     } else {
424         return 0;
425     }
426 }
427 
428 static void virtio_blk_submit_multireq(BlockBackend *blk, MultiReqBuffer *mrb)
429 {
430     int i = 0, start = 0, num_reqs = 0, niov = 0, nb_sectors = 0;
431     uint32_t max_transfer;
432     int64_t sector_num = 0;
433 
434     if (mrb->num_reqs == 1) {
435         submit_requests(blk, mrb, 0, 1, -1);
436         mrb->num_reqs = 0;
437         return;
438     }
439 
440     max_transfer = blk_get_max_transfer(mrb->reqs[0]->dev->blk);
441 
442     qsort(mrb->reqs, mrb->num_reqs, sizeof(*mrb->reqs),
443           &multireq_compare);
444 
445     for (i = 0; i < mrb->num_reqs; i++) {
446         VirtIOBlockReq *req = mrb->reqs[i];
447         if (num_reqs > 0) {
448             /*
449              * NOTE: We cannot merge the requests in below situations:
450              * 1. requests are not sequential
451              * 2. merge would exceed maximum number of IOVs
452              * 3. merge would exceed maximum transfer length of backend device
453              */
454             if (sector_num + nb_sectors != req->sector_num ||
455                 niov > blk_get_max_iov(blk) - req->qiov.niov ||
456                 req->qiov.size > max_transfer ||
457                 nb_sectors > (max_transfer -
458                               req->qiov.size) / BDRV_SECTOR_SIZE) {
459                 submit_requests(blk, mrb, start, num_reqs, niov);
460                 num_reqs = 0;
461             }
462         }
463 
464         if (num_reqs == 0) {
465             sector_num = req->sector_num;
466             nb_sectors = niov = 0;
467             start = i;
468         }
469 
470         nb_sectors += req->qiov.size / BDRV_SECTOR_SIZE;
471         niov += req->qiov.niov;
472         num_reqs++;
473     }
474 
475     submit_requests(blk, mrb, start, num_reqs, niov);
476     mrb->num_reqs = 0;
477 }
478 
479 static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
480 {
481     VirtIOBlock *s = req->dev;
482 
483     block_acct_start(blk_get_stats(s->blk), &req->acct, 0,
484                      BLOCK_ACCT_FLUSH);
485 
486     /*
487      * Make sure all outstanding writes are posted to the backing device.
488      */
489     if (mrb->is_write && mrb->num_reqs > 0) {
490         virtio_blk_submit_multireq(s->blk, mrb);
491     }
492     blk_aio_flush(s->blk, virtio_blk_flush_complete, req);
493 }
494 
495 static bool virtio_blk_sect_range_ok(VirtIOBlock *dev,
496                                      uint64_t sector, size_t size)
497 {
498     uint64_t nb_sectors = size >> BDRV_SECTOR_BITS;
499     uint64_t total_sectors;
500 
501     if (nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
502         return false;
503     }
504     if (sector & dev->sector_mask) {
505         return false;
506     }
507     if (size % dev->conf.conf.logical_block_size) {
508         return false;
509     }
510     blk_get_geometry(dev->blk, &total_sectors);
511     if (sector > total_sectors || nb_sectors > total_sectors - sector) {
512         return false;
513     }
514     return true;
515 }
516 
517 static uint8_t virtio_blk_handle_discard_write_zeroes(VirtIOBlockReq *req,
518     struct virtio_blk_discard_write_zeroes *dwz_hdr, bool is_write_zeroes)
519 {
520     VirtIOBlock *s = req->dev;
521     VirtIODevice *vdev = VIRTIO_DEVICE(s);
522     uint64_t sector;
523     uint32_t num_sectors, flags, max_sectors;
524     uint8_t err_status;
525     int bytes;
526 
527     sector = virtio_ldq_p(vdev, &dwz_hdr->sector);
528     num_sectors = virtio_ldl_p(vdev, &dwz_hdr->num_sectors);
529     flags = virtio_ldl_p(vdev, &dwz_hdr->flags);
530     max_sectors = is_write_zeroes ? s->conf.max_write_zeroes_sectors :
531                   s->conf.max_discard_sectors;
532 
533     /*
534      * max_sectors is at most BDRV_REQUEST_MAX_SECTORS, this check
535      * make us sure that "num_sectors << BDRV_SECTOR_BITS" can fit in
536      * the integer variable.
537      */
538     if (unlikely(num_sectors > max_sectors)) {
539         err_status = VIRTIO_BLK_S_IOERR;
540         goto err;
541     }
542 
543     bytes = num_sectors << BDRV_SECTOR_BITS;
544 
545     if (unlikely(!virtio_blk_sect_range_ok(s, sector, bytes))) {
546         err_status = VIRTIO_BLK_S_IOERR;
547         goto err;
548     }
549 
550     /*
551      * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for discard
552      * and write zeroes commands if any unknown flag is set.
553      */
554     if (unlikely(flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) {
555         err_status = VIRTIO_BLK_S_UNSUPP;
556         goto err;
557     }
558 
559     if (is_write_zeroes) { /* VIRTIO_BLK_T_WRITE_ZEROES */
560         int blk_aio_flags = 0;
561 
562         if (flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) {
563             blk_aio_flags |= BDRV_REQ_MAY_UNMAP;
564         }
565 
566         block_acct_start(blk_get_stats(s->blk), &req->acct, bytes,
567                          BLOCK_ACCT_WRITE);
568 
569         blk_aio_pwrite_zeroes(s->blk, sector << BDRV_SECTOR_BITS,
570                               bytes, blk_aio_flags,
571                               virtio_blk_discard_write_zeroes_complete, req);
572     } else { /* VIRTIO_BLK_T_DISCARD */
573         /*
574          * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for
575          * discard commands if the unmap flag is set.
576          */
577         if (unlikely(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) {
578             err_status = VIRTIO_BLK_S_UNSUPP;
579             goto err;
580         }
581 
582         blk_aio_pdiscard(s->blk, sector << BDRV_SECTOR_BITS, bytes,
583                          virtio_blk_discard_write_zeroes_complete, req);
584     }
585 
586     return VIRTIO_BLK_S_OK;
587 
588 err:
589     if (is_write_zeroes) {
590         block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_WRITE);
591     }
592     return err_status;
593 }
594 
595 static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
596 {
597     uint32_t type;
598     struct iovec *in_iov = req->elem.in_sg;
599     struct iovec *out_iov = req->elem.out_sg;
600     unsigned in_num = req->elem.in_num;
601     unsigned out_num = req->elem.out_num;
602     VirtIOBlock *s = req->dev;
603     VirtIODevice *vdev = VIRTIO_DEVICE(s);
604 
605     if (req->elem.out_num < 1 || req->elem.in_num < 1) {
606         virtio_error(vdev, "virtio-blk missing headers");
607         return -1;
608     }
609 
610     if (unlikely(iov_to_buf(out_iov, out_num, 0, &req->out,
611                             sizeof(req->out)) != sizeof(req->out))) {
612         virtio_error(vdev, "virtio-blk request outhdr too short");
613         return -1;
614     }
615 
616     iov_discard_front_undoable(&out_iov, &out_num, sizeof(req->out),
617                                &req->outhdr_undo);
618 
619     if (in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) {
620         virtio_error(vdev, "virtio-blk request inhdr too short");
621         iov_discard_undo(&req->outhdr_undo);
622         return -1;
623     }
624 
625     /* We always touch the last byte, so just see how big in_iov is.  */
626     req->in_len = iov_size(in_iov, in_num);
627     req->in = (void *)in_iov[in_num - 1].iov_base
628               + in_iov[in_num - 1].iov_len
629               - sizeof(struct virtio_blk_inhdr);
630     iov_discard_back_undoable(in_iov, &in_num, sizeof(struct virtio_blk_inhdr),
631                               &req->inhdr_undo);
632 
633     type = virtio_ldl_p(vdev, &req->out.type);
634 
635     /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER
636      * is an optional flag. Although a guest should not send this flag if
637      * not negotiated we ignored it in the past. So keep ignoring it. */
638     switch (type & ~(VIRTIO_BLK_T_OUT | VIRTIO_BLK_T_BARRIER)) {
639     case VIRTIO_BLK_T_IN:
640     {
641         bool is_write = type & VIRTIO_BLK_T_OUT;
642         req->sector_num = virtio_ldq_p(vdev, &req->out.sector);
643 
644         if (is_write) {
645             qemu_iovec_init_external(&req->qiov, out_iov, out_num);
646             trace_virtio_blk_handle_write(vdev, req, req->sector_num,
647                                           req->qiov.size / BDRV_SECTOR_SIZE);
648         } else {
649             qemu_iovec_init_external(&req->qiov, in_iov, in_num);
650             trace_virtio_blk_handle_read(vdev, req, req->sector_num,
651                                          req->qiov.size / BDRV_SECTOR_SIZE);
652         }
653 
654         if (!virtio_blk_sect_range_ok(s, req->sector_num, req->qiov.size)) {
655             virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
656             block_acct_invalid(blk_get_stats(s->blk),
657                                is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ);
658             virtio_blk_free_request(req);
659             return 0;
660         }
661 
662         block_acct_start(blk_get_stats(s->blk), &req->acct, req->qiov.size,
663                          is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ);
664 
665         /* merge would exceed maximum number of requests or IO direction
666          * changes */
667         if (mrb->num_reqs > 0 && (mrb->num_reqs == VIRTIO_BLK_MAX_MERGE_REQS ||
668                                   is_write != mrb->is_write ||
669                                   !s->conf.request_merging)) {
670             virtio_blk_submit_multireq(s->blk, mrb);
671         }
672 
673         assert(mrb->num_reqs < VIRTIO_BLK_MAX_MERGE_REQS);
674         mrb->reqs[mrb->num_reqs++] = req;
675         mrb->is_write = is_write;
676         break;
677     }
678     case VIRTIO_BLK_T_FLUSH:
679         virtio_blk_handle_flush(req, mrb);
680         break;
681     case VIRTIO_BLK_T_SCSI_CMD:
682         virtio_blk_handle_scsi(req);
683         break;
684     case VIRTIO_BLK_T_GET_ID:
685     {
686         /*
687          * NB: per existing s/n string convention the string is
688          * terminated by '\0' only when shorter than buffer.
689          */
690         const char *serial = s->conf.serial ? s->conf.serial : "";
691         size_t size = MIN(strlen(serial) + 1,
692                           MIN(iov_size(in_iov, in_num),
693                               VIRTIO_BLK_ID_BYTES));
694         iov_from_buf(in_iov, in_num, 0, serial, size);
695         virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
696         virtio_blk_free_request(req);
697         break;
698     }
699     /*
700      * VIRTIO_BLK_T_DISCARD and VIRTIO_BLK_T_WRITE_ZEROES are defined with
701      * VIRTIO_BLK_T_OUT flag set. We masked this flag in the switch statement,
702      * so we must mask it for these requests, then we will check if it is set.
703      */
704     case VIRTIO_BLK_T_DISCARD & ~VIRTIO_BLK_T_OUT:
705     case VIRTIO_BLK_T_WRITE_ZEROES & ~VIRTIO_BLK_T_OUT:
706     {
707         struct virtio_blk_discard_write_zeroes dwz_hdr;
708         size_t out_len = iov_size(out_iov, out_num);
709         bool is_write_zeroes = (type & ~VIRTIO_BLK_T_BARRIER) ==
710                                VIRTIO_BLK_T_WRITE_ZEROES;
711         uint8_t err_status;
712 
713         /*
714          * Unsupported if VIRTIO_BLK_T_OUT is not set or the request contains
715          * more than one segment.
716          */
717         if (unlikely(!(type & VIRTIO_BLK_T_OUT) ||
718                      out_len > sizeof(dwz_hdr))) {
719             virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
720             virtio_blk_free_request(req);
721             return 0;
722         }
723 
724         if (unlikely(iov_to_buf(out_iov, out_num, 0, &dwz_hdr,
725                                 sizeof(dwz_hdr)) != sizeof(dwz_hdr))) {
726             iov_discard_undo(&req->inhdr_undo);
727             iov_discard_undo(&req->outhdr_undo);
728             virtio_error(vdev, "virtio-blk discard/write_zeroes header"
729                          " too short");
730             return -1;
731         }
732 
733         err_status = virtio_blk_handle_discard_write_zeroes(req, &dwz_hdr,
734                                                             is_write_zeroes);
735         if (err_status != VIRTIO_BLK_S_OK) {
736             virtio_blk_req_complete(req, err_status);
737             virtio_blk_free_request(req);
738         }
739 
740         break;
741     }
742     default:
743         virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
744         virtio_blk_free_request(req);
745     }
746     return 0;
747 }
748 
749 void virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq)
750 {
751     VirtIOBlockReq *req;
752     MultiReqBuffer mrb = {};
753     bool suppress_notifications = virtio_queue_get_notification(vq);
754 
755     aio_context_acquire(blk_get_aio_context(s->blk));
756     blk_io_plug(s->blk);
757 
758     do {
759         if (suppress_notifications) {
760             virtio_queue_set_notification(vq, 0);
761         }
762 
763         while ((req = virtio_blk_get_request(s, vq))) {
764             if (virtio_blk_handle_request(req, &mrb)) {
765                 virtqueue_detach_element(req->vq, &req->elem, 0);
766                 virtio_blk_free_request(req);
767                 break;
768             }
769         }
770 
771         if (suppress_notifications) {
772             virtio_queue_set_notification(vq, 1);
773         }
774     } while (!virtio_queue_empty(vq));
775 
776     if (mrb.num_reqs) {
777         virtio_blk_submit_multireq(s->blk, &mrb);
778     }
779 
780     blk_io_unplug(s->blk);
781     aio_context_release(blk_get_aio_context(s->blk));
782 }
783 
784 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
785 {
786     VirtIOBlock *s = (VirtIOBlock *)vdev;
787 
788     if (s->dataplane && !s->dataplane_started) {
789         /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
790          * dataplane here instead of waiting for .set_status().
791          */
792         virtio_device_start_ioeventfd(vdev);
793         if (!s->dataplane_disabled) {
794             return;
795         }
796     }
797     virtio_blk_handle_vq(s, vq);
798 }
799 
800 void virtio_blk_process_queued_requests(VirtIOBlock *s, bool is_bh)
801 {
802     VirtIOBlockReq *req = s->rq;
803     MultiReqBuffer mrb = {};
804 
805     s->rq = NULL;
806 
807     aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
808     while (req) {
809         VirtIOBlockReq *next = req->next;
810         if (virtio_blk_handle_request(req, &mrb)) {
811             /* Device is now broken and won't do any processing until it gets
812              * reset. Already queued requests will be lost: let's purge them.
813              */
814             while (req) {
815                 next = req->next;
816                 virtqueue_detach_element(req->vq, &req->elem, 0);
817                 virtio_blk_free_request(req);
818                 req = next;
819             }
820             break;
821         }
822         req = next;
823     }
824 
825     if (mrb.num_reqs) {
826         virtio_blk_submit_multireq(s->blk, &mrb);
827     }
828     if (is_bh) {
829         blk_dec_in_flight(s->conf.conf.blk);
830     }
831     aio_context_release(blk_get_aio_context(s->conf.conf.blk));
832 }
833 
834 static void virtio_blk_dma_restart_bh(void *opaque)
835 {
836     VirtIOBlock *s = opaque;
837 
838     qemu_bh_delete(s->bh);
839     s->bh = NULL;
840 
841     virtio_blk_process_queued_requests(s, true);
842 }
843 
844 static void virtio_blk_dma_restart_cb(void *opaque, bool running,
845                                       RunState state)
846 {
847     VirtIOBlock *s = opaque;
848     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
849     VirtioBusState *bus = VIRTIO_BUS(qbus);
850 
851     if (!running) {
852         return;
853     }
854 
855     /*
856      * If ioeventfd is enabled, don't schedule the BH here as queued
857      * requests will be processed while starting the data plane.
858      */
859     if (!s->bh && !virtio_bus_ioeventfd_enabled(bus)) {
860         s->bh = aio_bh_new(blk_get_aio_context(s->conf.conf.blk),
861                            virtio_blk_dma_restart_bh, s);
862         blk_inc_in_flight(s->conf.conf.blk);
863         qemu_bh_schedule(s->bh);
864     }
865 }
866 
867 static void virtio_blk_reset(VirtIODevice *vdev)
868 {
869     VirtIOBlock *s = VIRTIO_BLK(vdev);
870     AioContext *ctx;
871     VirtIOBlockReq *req;
872 
873     ctx = blk_get_aio_context(s->blk);
874     aio_context_acquire(ctx);
875     blk_drain(s->blk);
876 
877     /* We drop queued requests after blk_drain() because blk_drain() itself can
878      * produce them. */
879     while (s->rq) {
880         req = s->rq;
881         s->rq = req->next;
882         virtqueue_detach_element(req->vq, &req->elem, 0);
883         virtio_blk_free_request(req);
884     }
885 
886     aio_context_release(ctx);
887 
888     assert(!s->dataplane_started);
889     blk_set_enable_write_cache(s->blk, s->original_wce);
890 }
891 
892 /* coalesce internal state, copy to pci i/o region 0
893  */
894 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
895 {
896     VirtIOBlock *s = VIRTIO_BLK(vdev);
897     BlockConf *conf = &s->conf.conf;
898     struct virtio_blk_config blkcfg;
899     uint64_t capacity;
900     int64_t length;
901     int blk_size = conf->logical_block_size;
902 
903     blk_get_geometry(s->blk, &capacity);
904     memset(&blkcfg, 0, sizeof(blkcfg));
905     virtio_stq_p(vdev, &blkcfg.capacity, capacity);
906     virtio_stl_p(vdev, &blkcfg.seg_max,
907                  s->conf.seg_max_adjust ? s->conf.queue_size - 2 : 128 - 2);
908     virtio_stw_p(vdev, &blkcfg.geometry.cylinders, conf->cyls);
909     virtio_stl_p(vdev, &blkcfg.blk_size, blk_size);
910     virtio_stw_p(vdev, &blkcfg.min_io_size, conf->min_io_size / blk_size);
911     virtio_stl_p(vdev, &blkcfg.opt_io_size, conf->opt_io_size / blk_size);
912     blkcfg.geometry.heads = conf->heads;
913     /*
914      * We must ensure that the block device capacity is a multiple of
915      * the logical block size. If that is not the case, let's use
916      * sector_mask to adopt the geometry to have a correct picture.
917      * For those devices where the capacity is ok for the given geometry
918      * we don't touch the sector value of the geometry, since some devices
919      * (like s390 dasd) need a specific value. Here the capacity is already
920      * cyls*heads*secs*blk_size and the sector value is not block size
921      * divided by 512 - instead it is the amount of blk_size blocks
922      * per track (cylinder).
923      */
924     length = blk_getlength(s->blk);
925     if (length > 0 && length / conf->heads / conf->secs % blk_size) {
926         blkcfg.geometry.sectors = conf->secs & ~s->sector_mask;
927     } else {
928         blkcfg.geometry.sectors = conf->secs;
929     }
930     blkcfg.size_max = 0;
931     blkcfg.physical_block_exp = get_physical_block_exp(conf);
932     blkcfg.alignment_offset = 0;
933     blkcfg.wce = blk_enable_write_cache(s->blk);
934     virtio_stw_p(vdev, &blkcfg.num_queues, s->conf.num_queues);
935     if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_DISCARD)) {
936         uint32_t discard_granularity = conf->discard_granularity;
937         if (discard_granularity == -1 || !s->conf.report_discard_granularity) {
938             discard_granularity = blk_size;
939         }
940         virtio_stl_p(vdev, &blkcfg.max_discard_sectors,
941                      s->conf.max_discard_sectors);
942         virtio_stl_p(vdev, &blkcfg.discard_sector_alignment,
943                      discard_granularity >> BDRV_SECTOR_BITS);
944         /*
945          * We support only one segment per request since multiple segments
946          * are not widely used and there are no userspace APIs that allow
947          * applications to submit multiple segments in a single call.
948          */
949         virtio_stl_p(vdev, &blkcfg.max_discard_seg, 1);
950     }
951     if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES)) {
952         virtio_stl_p(vdev, &blkcfg.max_write_zeroes_sectors,
953                      s->conf.max_write_zeroes_sectors);
954         blkcfg.write_zeroes_may_unmap = 1;
955         virtio_stl_p(vdev, &blkcfg.max_write_zeroes_seg, 1);
956     }
957     memcpy(config, &blkcfg, s->config_size);
958 }
959 
960 static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
961 {
962     VirtIOBlock *s = VIRTIO_BLK(vdev);
963     struct virtio_blk_config blkcfg;
964 
965     memcpy(&blkcfg, config, s->config_size);
966 
967     aio_context_acquire(blk_get_aio_context(s->blk));
968     blk_set_enable_write_cache(s->blk, blkcfg.wce != 0);
969     aio_context_release(blk_get_aio_context(s->blk));
970 }
971 
972 static uint64_t virtio_blk_get_features(VirtIODevice *vdev, uint64_t features,
973                                         Error **errp)
974 {
975     VirtIOBlock *s = VIRTIO_BLK(vdev);
976 
977     /* Firstly sync all virtio-blk possible supported features */
978     features |= s->host_features;
979 
980     virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
981     virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY);
982     virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY);
983     virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
984     if (virtio_has_feature(features, VIRTIO_F_VERSION_1)) {
985         if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_SCSI)) {
986             error_setg(errp, "Please set scsi=off for virtio-blk devices in order to use virtio 1.0");
987             return 0;
988         }
989     } else {
990         virtio_clear_feature(&features, VIRTIO_F_ANY_LAYOUT);
991         virtio_add_feature(&features, VIRTIO_BLK_F_SCSI);
992     }
993 
994     if (blk_enable_write_cache(s->blk) ||
995         (s->conf.x_enable_wce_if_config_wce &&
996          virtio_has_feature(features, VIRTIO_BLK_F_CONFIG_WCE))) {
997         virtio_add_feature(&features, VIRTIO_BLK_F_WCE);
998     }
999     if (!blk_is_writable(s->blk)) {
1000         virtio_add_feature(&features, VIRTIO_BLK_F_RO);
1001     }
1002     if (s->conf.num_queues > 1) {
1003         virtio_add_feature(&features, VIRTIO_BLK_F_MQ);
1004     }
1005 
1006     return features;
1007 }
1008 
1009 static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
1010 {
1011     VirtIOBlock *s = VIRTIO_BLK(vdev);
1012 
1013     if (!(status & (VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK))) {
1014         assert(!s->dataplane_started);
1015     }
1016 
1017     if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1018         return;
1019     }
1020 
1021     /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
1022      * cache flushes.  Thus, the "auto writethrough" behavior is never
1023      * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
1024      * Leaving it enabled would break the following sequence:
1025      *
1026      *     Guest started with "-drive cache=writethrough"
1027      *     Guest sets status to 0
1028      *     Guest sets DRIVER bit in status field
1029      *     Guest reads host features (WCE=0, CONFIG_WCE=1)
1030      *     Guest writes guest features (WCE=0, CONFIG_WCE=1)
1031      *     Guest writes 1 to the WCE configuration field (writeback mode)
1032      *     Guest sets DRIVER_OK bit in status field
1033      *
1034      * s->blk would erroneously be placed in writethrough mode.
1035      */
1036     if (!virtio_vdev_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE)) {
1037         aio_context_acquire(blk_get_aio_context(s->blk));
1038         blk_set_enable_write_cache(s->blk,
1039                                    virtio_vdev_has_feature(vdev,
1040                                                            VIRTIO_BLK_F_WCE));
1041         aio_context_release(blk_get_aio_context(s->blk));
1042     }
1043 }
1044 
1045 static void virtio_blk_save_device(VirtIODevice *vdev, QEMUFile *f)
1046 {
1047     VirtIOBlock *s = VIRTIO_BLK(vdev);
1048     VirtIOBlockReq *req = s->rq;
1049 
1050     while (req) {
1051         qemu_put_sbyte(f, 1);
1052 
1053         if (s->conf.num_queues > 1) {
1054             qemu_put_be32(f, virtio_get_queue_index(req->vq));
1055         }
1056 
1057         qemu_put_virtqueue_element(vdev, f, &req->elem);
1058         req = req->next;
1059     }
1060     qemu_put_sbyte(f, 0);
1061 }
1062 
1063 static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f,
1064                                   int version_id)
1065 {
1066     VirtIOBlock *s = VIRTIO_BLK(vdev);
1067 
1068     while (qemu_get_sbyte(f)) {
1069         unsigned nvqs = s->conf.num_queues;
1070         unsigned vq_idx = 0;
1071         VirtIOBlockReq *req;
1072 
1073         if (nvqs > 1) {
1074             vq_idx = qemu_get_be32(f);
1075 
1076             if (vq_idx >= nvqs) {
1077                 error_report("Invalid virtqueue index in request list: %#x",
1078                              vq_idx);
1079                 return -EINVAL;
1080             }
1081         }
1082 
1083         req = qemu_get_virtqueue_element(vdev, f, sizeof(VirtIOBlockReq));
1084         virtio_blk_init_request(s, virtio_get_queue(vdev, vq_idx), req);
1085         req->next = s->rq;
1086         s->rq = req;
1087     }
1088 
1089     return 0;
1090 }
1091 
1092 static void virtio_resize_cb(void *opaque)
1093 {
1094     VirtIODevice *vdev = opaque;
1095 
1096     assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1097     virtio_notify_config(vdev);
1098 }
1099 
1100 static void virtio_blk_resize(void *opaque)
1101 {
1102     VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
1103 
1104     /*
1105      * virtio_notify_config() needs to acquire the global mutex,
1106      * so it can't be called from an iothread. Instead, schedule
1107      * it to be run in the main context BH.
1108      */
1109     aio_bh_schedule_oneshot(qemu_get_aio_context(), virtio_resize_cb, vdev);
1110 }
1111 
1112 static const BlockDevOps virtio_block_ops = {
1113     .resize_cb = virtio_blk_resize,
1114 };
1115 
1116 static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
1117 {
1118     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1119     VirtIOBlock *s = VIRTIO_BLK(dev);
1120     VirtIOBlkConf *conf = &s->conf;
1121     Error *err = NULL;
1122     unsigned i;
1123 
1124     if (!conf->conf.blk) {
1125         error_setg(errp, "drive property not set");
1126         return;
1127     }
1128     if (!blk_is_inserted(conf->conf.blk)) {
1129         error_setg(errp, "Device needs media, but drive is empty");
1130         return;
1131     }
1132     if (conf->num_queues == VIRTIO_BLK_AUTO_NUM_QUEUES) {
1133         conf->num_queues = 1;
1134     }
1135     if (!conf->num_queues) {
1136         error_setg(errp, "num-queues property must be larger than 0");
1137         return;
1138     }
1139     if (conf->queue_size <= 2) {
1140         error_setg(errp, "invalid queue-size property (%" PRIu16 "), "
1141                    "must be > 2", conf->queue_size);
1142         return;
1143     }
1144     if (!is_power_of_2(conf->queue_size) ||
1145         conf->queue_size > VIRTQUEUE_MAX_SIZE) {
1146         error_setg(errp, "invalid queue-size property (%" PRIu16 "), "
1147                    "must be a power of 2 (max %d)",
1148                    conf->queue_size, VIRTQUEUE_MAX_SIZE);
1149         return;
1150     }
1151 
1152     if (!blkconf_apply_backend_options(&conf->conf,
1153                                        !blk_supports_write_perm(conf->conf.blk),
1154                                        true, errp)) {
1155         return;
1156     }
1157     s->original_wce = blk_enable_write_cache(conf->conf.blk);
1158     if (!blkconf_geometry(&conf->conf, NULL, 65535, 255, 255, errp)) {
1159         return;
1160     }
1161 
1162     if (!blkconf_blocksizes(&conf->conf, errp)) {
1163         return;
1164     }
1165 
1166     if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_DISCARD) &&
1167         (!conf->max_discard_sectors ||
1168          conf->max_discard_sectors > BDRV_REQUEST_MAX_SECTORS)) {
1169         error_setg(errp, "invalid max-discard-sectors property (%" PRIu32 ")"
1170                    ", must be between 1 and %d",
1171                    conf->max_discard_sectors, (int)BDRV_REQUEST_MAX_SECTORS);
1172         return;
1173     }
1174 
1175     if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES) &&
1176         (!conf->max_write_zeroes_sectors ||
1177          conf->max_write_zeroes_sectors > BDRV_REQUEST_MAX_SECTORS)) {
1178         error_setg(errp, "invalid max-write-zeroes-sectors property (%" PRIu32
1179                    "), must be between 1 and %d",
1180                    conf->max_write_zeroes_sectors,
1181                    (int)BDRV_REQUEST_MAX_SECTORS);
1182         return;
1183     }
1184 
1185     s->config_size = virtio_get_config_size(&virtio_blk_cfg_size_params,
1186                                             s->host_features);
1187     virtio_init(vdev, VIRTIO_ID_BLOCK, s->config_size);
1188 
1189     s->blk = conf->conf.blk;
1190     s->rq = NULL;
1191     s->sector_mask = (s->conf.conf.logical_block_size / BDRV_SECTOR_SIZE) - 1;
1192 
1193     for (i = 0; i < conf->num_queues; i++) {
1194         virtio_add_queue(vdev, conf->queue_size, virtio_blk_handle_output);
1195     }
1196     qemu_coroutine_inc_pool_size(conf->num_queues * conf->queue_size / 2);
1197     virtio_blk_data_plane_create(vdev, conf, &s->dataplane, &err);
1198     if (err != NULL) {
1199         error_propagate(errp, err);
1200         for (i = 0; i < conf->num_queues; i++) {
1201             virtio_del_queue(vdev, i);
1202         }
1203         virtio_cleanup(vdev);
1204         return;
1205     }
1206 
1207     s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
1208     blk_set_dev_ops(s->blk, &virtio_block_ops, s);
1209 
1210     blk_iostatus_enable(s->blk);
1211 
1212     add_boot_device_lchs(dev, "/disk@0,0",
1213                          conf->conf.lcyls,
1214                          conf->conf.lheads,
1215                          conf->conf.lsecs);
1216 }
1217 
1218 static void virtio_blk_device_unrealize(DeviceState *dev)
1219 {
1220     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1221     VirtIOBlock *s = VIRTIO_BLK(dev);
1222     VirtIOBlkConf *conf = &s->conf;
1223     unsigned i;
1224 
1225     blk_drain(s->blk);
1226     del_boot_device_lchs(dev, "/disk@0,0");
1227     virtio_blk_data_plane_destroy(s->dataplane);
1228     s->dataplane = NULL;
1229     for (i = 0; i < conf->num_queues; i++) {
1230         virtio_del_queue(vdev, i);
1231     }
1232     qemu_coroutine_dec_pool_size(conf->num_queues * conf->queue_size / 2);
1233     qemu_del_vm_change_state_handler(s->change);
1234     blockdev_mark_auto_del(s->blk);
1235     virtio_cleanup(vdev);
1236 }
1237 
1238 static void virtio_blk_instance_init(Object *obj)
1239 {
1240     VirtIOBlock *s = VIRTIO_BLK(obj);
1241 
1242     device_add_bootindex_property(obj, &s->conf.conf.bootindex,
1243                                   "bootindex", "/disk@0,0",
1244                                   DEVICE(obj));
1245 }
1246 
1247 static const VMStateDescription vmstate_virtio_blk = {
1248     .name = "virtio-blk",
1249     .minimum_version_id = 2,
1250     .version_id = 2,
1251     .fields = (VMStateField[]) {
1252         VMSTATE_VIRTIO_DEVICE,
1253         VMSTATE_END_OF_LIST()
1254     },
1255 };
1256 
1257 static Property virtio_blk_properties[] = {
1258     DEFINE_BLOCK_PROPERTIES(VirtIOBlock, conf.conf),
1259     DEFINE_BLOCK_ERROR_PROPERTIES(VirtIOBlock, conf.conf),
1260     DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock, conf.conf),
1261     DEFINE_PROP_STRING("serial", VirtIOBlock, conf.serial),
1262     DEFINE_PROP_BIT64("config-wce", VirtIOBlock, host_features,
1263                       VIRTIO_BLK_F_CONFIG_WCE, true),
1264 #ifdef __linux__
1265     DEFINE_PROP_BIT64("scsi", VirtIOBlock, host_features,
1266                       VIRTIO_BLK_F_SCSI, false),
1267 #endif
1268     DEFINE_PROP_BIT("request-merging", VirtIOBlock, conf.request_merging, 0,
1269                     true),
1270     DEFINE_PROP_UINT16("num-queues", VirtIOBlock, conf.num_queues,
1271                        VIRTIO_BLK_AUTO_NUM_QUEUES),
1272     DEFINE_PROP_UINT16("queue-size", VirtIOBlock, conf.queue_size, 256),
1273     DEFINE_PROP_BOOL("seg-max-adjust", VirtIOBlock, conf.seg_max_adjust, true),
1274     DEFINE_PROP_LINK("iothread", VirtIOBlock, conf.iothread, TYPE_IOTHREAD,
1275                      IOThread *),
1276     DEFINE_PROP_BIT64("discard", VirtIOBlock, host_features,
1277                       VIRTIO_BLK_F_DISCARD, true),
1278     DEFINE_PROP_BOOL("report-discard-granularity", VirtIOBlock,
1279                      conf.report_discard_granularity, true),
1280     DEFINE_PROP_BIT64("write-zeroes", VirtIOBlock, host_features,
1281                       VIRTIO_BLK_F_WRITE_ZEROES, true),
1282     DEFINE_PROP_UINT32("max-discard-sectors", VirtIOBlock,
1283                        conf.max_discard_sectors, BDRV_REQUEST_MAX_SECTORS),
1284     DEFINE_PROP_UINT32("max-write-zeroes-sectors", VirtIOBlock,
1285                        conf.max_write_zeroes_sectors, BDRV_REQUEST_MAX_SECTORS),
1286     DEFINE_PROP_BOOL("x-enable-wce-if-config-wce", VirtIOBlock,
1287                      conf.x_enable_wce_if_config_wce, true),
1288     DEFINE_PROP_END_OF_LIST(),
1289 };
1290 
1291 static void virtio_blk_class_init(ObjectClass *klass, void *data)
1292 {
1293     DeviceClass *dc = DEVICE_CLASS(klass);
1294     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1295 
1296     device_class_set_props(dc, virtio_blk_properties);
1297     dc->vmsd = &vmstate_virtio_blk;
1298     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1299     vdc->realize = virtio_blk_device_realize;
1300     vdc->unrealize = virtio_blk_device_unrealize;
1301     vdc->get_config = virtio_blk_update_config;
1302     vdc->set_config = virtio_blk_set_config;
1303     vdc->get_features = virtio_blk_get_features;
1304     vdc->set_status = virtio_blk_set_status;
1305     vdc->reset = virtio_blk_reset;
1306     vdc->save = virtio_blk_save_device;
1307     vdc->load = virtio_blk_load_device;
1308     vdc->start_ioeventfd = virtio_blk_data_plane_start;
1309     vdc->stop_ioeventfd = virtio_blk_data_plane_stop;
1310 }
1311 
1312 static const TypeInfo virtio_blk_info = {
1313     .name = TYPE_VIRTIO_BLK,
1314     .parent = TYPE_VIRTIO_DEVICE,
1315     .instance_size = sizeof(VirtIOBlock),
1316     .instance_init = virtio_blk_instance_init,
1317     .class_init = virtio_blk_class_init,
1318 };
1319 
1320 static void virtio_register_types(void)
1321 {
1322     type_register_static(&virtio_blk_info);
1323 }
1324 
1325 type_init(virtio_register_types)
1326