xref: /openbmc/qemu/hw/block/virtio-blk.c (revision 70f168f8)
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 "block/block_int.h"
21 #include "trace.h"
22 #include "hw/block/block.h"
23 #include "hw/qdev-properties.h"
24 #include "sysemu/blockdev.h"
25 #include "sysemu/block-ram-registrar.h"
26 #include "sysemu/sysemu.h"
27 #include "sysemu/runstate.h"
28 #include "hw/virtio/virtio-blk.h"
29 #include "dataplane/virtio-blk.h"
30 #include "scsi/constants.h"
31 #ifdef __linux__
32 # include <scsi/sg.h>
33 #endif
34 #include "hw/virtio/virtio-bus.h"
35 #include "migration/qemu-file-types.h"
36 #include "hw/virtio/virtio-access.h"
37 #include "hw/virtio/virtio-blk-common.h"
38 #include "qemu/coroutine.h"
39 
40 static void virtio_blk_init_request(VirtIOBlock *s, VirtQueue *vq,
41                                     VirtIOBlockReq *req)
42 {
43     req->dev = s;
44     req->vq = vq;
45     req->qiov.size = 0;
46     req->in_len = 0;
47     req->next = NULL;
48     req->mr_next = NULL;
49 }
50 
51 static void virtio_blk_free_request(VirtIOBlockReq *req)
52 {
53     g_free(req);
54 }
55 
56 static void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status)
57 {
58     VirtIOBlock *s = req->dev;
59     VirtIODevice *vdev = VIRTIO_DEVICE(s);
60 
61     trace_virtio_blk_req_complete(vdev, req, status);
62 
63     stb_p(&req->in->status, status);
64     iov_discard_undo(&req->inhdr_undo);
65     iov_discard_undo(&req->outhdr_undo);
66     virtqueue_push(req->vq, &req->elem, req->in_len);
67     if (s->dataplane_started && !s->dataplane_disabled) {
68         virtio_blk_data_plane_notify(s->dataplane, req->vq);
69     } else {
70         virtio_notify(vdev, req->vq);
71     }
72 }
73 
74 static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
75     bool is_read, bool acct_failed)
76 {
77     VirtIOBlock *s = req->dev;
78     BlockErrorAction action = blk_get_error_action(s->blk, is_read, error);
79 
80     if (action == BLOCK_ERROR_ACTION_STOP) {
81         /* Break the link as the next request is going to be parsed from the
82          * ring again. Otherwise we may end up doing a double completion! */
83         req->mr_next = NULL;
84         req->next = s->rq;
85         s->rq = req;
86     } else if (action == BLOCK_ERROR_ACTION_REPORT) {
87         virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
88         if (acct_failed) {
89             block_acct_failed(blk_get_stats(s->blk), &req->acct);
90         }
91         virtio_blk_free_request(req);
92     }
93 
94     blk_error_action(s->blk, action, is_read, error);
95     return action != BLOCK_ERROR_ACTION_IGNORE;
96 }
97 
98 static void virtio_blk_rw_complete(void *opaque, int ret)
99 {
100     VirtIOBlockReq *next = opaque;
101     VirtIOBlock *s = next->dev;
102     VirtIODevice *vdev = VIRTIO_DEVICE(s);
103 
104     aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
105     while (next) {
106         VirtIOBlockReq *req = next;
107         next = req->mr_next;
108         trace_virtio_blk_rw_complete(vdev, req, ret);
109 
110         if (req->qiov.nalloc != -1) {
111             /* If nalloc is != -1 req->qiov is a local copy of the original
112              * external iovec. It was allocated in submit_requests to be
113              * able to merge requests. */
114             qemu_iovec_destroy(&req->qiov);
115         }
116 
117         if (ret) {
118             int p = virtio_ldl_p(VIRTIO_DEVICE(s), &req->out.type);
119             bool is_read = !(p & VIRTIO_BLK_T_OUT);
120             /* Note that memory may be dirtied on read failure.  If the
121              * virtio request is not completed here, as is the case for
122              * BLOCK_ERROR_ACTION_STOP, the memory may not be copied
123              * correctly during live migration.  While this is ugly,
124              * it is acceptable because the device is free to write to
125              * the memory until the request is completed (which will
126              * happen on the other side of the migration).
127              */
128             if (virtio_blk_handle_rw_error(req, -ret, is_read, true)) {
129                 continue;
130             }
131         }
132 
133         virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
134         block_acct_done(blk_get_stats(s->blk), &req->acct);
135         virtio_blk_free_request(req);
136     }
137     aio_context_release(blk_get_aio_context(s->conf.conf.blk));
138 }
139 
140 static void virtio_blk_flush_complete(void *opaque, int ret)
141 {
142     VirtIOBlockReq *req = opaque;
143     VirtIOBlock *s = req->dev;
144 
145     aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
146     if (ret) {
147         if (virtio_blk_handle_rw_error(req, -ret, 0, true)) {
148             goto out;
149         }
150     }
151 
152     virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
153     block_acct_done(blk_get_stats(s->blk), &req->acct);
154     virtio_blk_free_request(req);
155 
156 out:
157     aio_context_release(blk_get_aio_context(s->conf.conf.blk));
158 }
159 
160 static void virtio_blk_discard_write_zeroes_complete(void *opaque, int ret)
161 {
162     VirtIOBlockReq *req = opaque;
163     VirtIOBlock *s = req->dev;
164     bool is_write_zeroes = (virtio_ldl_p(VIRTIO_DEVICE(s), &req->out.type) &
165                             ~VIRTIO_BLK_T_BARRIER) == VIRTIO_BLK_T_WRITE_ZEROES;
166 
167     aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
168     if (ret) {
169         if (virtio_blk_handle_rw_error(req, -ret, false, is_write_zeroes)) {
170             goto out;
171         }
172     }
173 
174     virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
175     if (is_write_zeroes) {
176         block_acct_done(blk_get_stats(s->blk), &req->acct);
177     }
178     virtio_blk_free_request(req);
179 
180 out:
181     aio_context_release(blk_get_aio_context(s->conf.conf.blk));
182 }
183 
184 #ifdef __linux__
185 
186 typedef struct {
187     VirtIOBlockReq *req;
188     struct sg_io_hdr hdr;
189 } VirtIOBlockIoctlReq;
190 
191 static void virtio_blk_ioctl_complete(void *opaque, int status)
192 {
193     VirtIOBlockIoctlReq *ioctl_req = opaque;
194     VirtIOBlockReq *req = ioctl_req->req;
195     VirtIOBlock *s = req->dev;
196     VirtIODevice *vdev = VIRTIO_DEVICE(s);
197     struct virtio_scsi_inhdr *scsi;
198     struct sg_io_hdr *hdr;
199 
200     scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
201 
202     if (status) {
203         status = VIRTIO_BLK_S_UNSUPP;
204         virtio_stl_p(vdev, &scsi->errors, 255);
205         goto out;
206     }
207 
208     hdr = &ioctl_req->hdr;
209     /*
210      * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
211      * clear the masked_status field [hence status gets cleared too, see
212      * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
213      * status has occurred.  However they do set DRIVER_SENSE in driver_status
214      * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
215      */
216     if (hdr->status == 0 && hdr->sb_len_wr > 0) {
217         hdr->status = CHECK_CONDITION;
218     }
219 
220     virtio_stl_p(vdev, &scsi->errors,
221                  hdr->status | (hdr->msg_status << 8) |
222                  (hdr->host_status << 16) | (hdr->driver_status << 24));
223     virtio_stl_p(vdev, &scsi->residual, hdr->resid);
224     virtio_stl_p(vdev, &scsi->sense_len, hdr->sb_len_wr);
225     virtio_stl_p(vdev, &scsi->data_len, hdr->dxfer_len);
226 
227 out:
228     aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
229     virtio_blk_req_complete(req, status);
230     virtio_blk_free_request(req);
231     aio_context_release(blk_get_aio_context(s->conf.conf.blk));
232     g_free(ioctl_req);
233 }
234 
235 #endif
236 
237 static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s, VirtQueue *vq)
238 {
239     VirtIOBlockReq *req = virtqueue_pop(vq, sizeof(VirtIOBlockReq));
240 
241     if (req) {
242         virtio_blk_init_request(s, vq, req);
243     }
244     return req;
245 }
246 
247 static int virtio_blk_handle_scsi_req(VirtIOBlockReq *req)
248 {
249     int status = VIRTIO_BLK_S_OK;
250     struct virtio_scsi_inhdr *scsi = NULL;
251     VirtIOBlock *blk = req->dev;
252     VirtIODevice *vdev = VIRTIO_DEVICE(blk);
253     VirtQueueElement *elem = &req->elem;
254 
255 #ifdef __linux__
256     int i;
257     VirtIOBlockIoctlReq *ioctl_req;
258     BlockAIOCB *acb;
259 #endif
260 
261     /*
262      * We require at least one output segment each for the virtio_blk_outhdr
263      * and the SCSI command block.
264      *
265      * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
266      * and the sense buffer pointer in the input segments.
267      */
268     if (elem->out_num < 2 || elem->in_num < 3) {
269         status = VIRTIO_BLK_S_IOERR;
270         goto fail;
271     }
272 
273     /*
274      * The scsi inhdr is placed in the second-to-last input segment, just
275      * before the regular inhdr.
276      */
277     scsi = (void *)elem->in_sg[elem->in_num - 2].iov_base;
278 
279     if (!virtio_has_feature(blk->host_features, VIRTIO_BLK_F_SCSI)) {
280         status = VIRTIO_BLK_S_UNSUPP;
281         goto fail;
282     }
283 
284     /*
285      * No support for bidirection commands yet.
286      */
287     if (elem->out_num > 2 && elem->in_num > 3) {
288         status = VIRTIO_BLK_S_UNSUPP;
289         goto fail;
290     }
291 
292 #ifdef __linux__
293     ioctl_req = g_new0(VirtIOBlockIoctlReq, 1);
294     ioctl_req->req = req;
295     ioctl_req->hdr.interface_id = 'S';
296     ioctl_req->hdr.cmd_len = elem->out_sg[1].iov_len;
297     ioctl_req->hdr.cmdp = elem->out_sg[1].iov_base;
298     ioctl_req->hdr.dxfer_len = 0;
299 
300     if (elem->out_num > 2) {
301         /*
302          * If there are more than the minimally required 2 output segments
303          * there is write payload starting from the third iovec.
304          */
305         ioctl_req->hdr.dxfer_direction = SG_DXFER_TO_DEV;
306         ioctl_req->hdr.iovec_count = elem->out_num - 2;
307 
308         for (i = 0; i < ioctl_req->hdr.iovec_count; i++) {
309             ioctl_req->hdr.dxfer_len += elem->out_sg[i + 2].iov_len;
310         }
311 
312         ioctl_req->hdr.dxferp = elem->out_sg + 2;
313 
314     } else if (elem->in_num > 3) {
315         /*
316          * If we have more than 3 input segments the guest wants to actually
317          * read data.
318          */
319         ioctl_req->hdr.dxfer_direction = SG_DXFER_FROM_DEV;
320         ioctl_req->hdr.iovec_count = elem->in_num - 3;
321         for (i = 0; i < ioctl_req->hdr.iovec_count; i++) {
322             ioctl_req->hdr.dxfer_len += elem->in_sg[i].iov_len;
323         }
324 
325         ioctl_req->hdr.dxferp = elem->in_sg;
326     } else {
327         /*
328          * Some SCSI commands don't actually transfer any data.
329          */
330         ioctl_req->hdr.dxfer_direction = SG_DXFER_NONE;
331     }
332 
333     ioctl_req->hdr.sbp = elem->in_sg[elem->in_num - 3].iov_base;
334     ioctl_req->hdr.mx_sb_len = elem->in_sg[elem->in_num - 3].iov_len;
335 
336     acb = blk_aio_ioctl(blk->blk, SG_IO, &ioctl_req->hdr,
337                         virtio_blk_ioctl_complete, ioctl_req);
338     if (!acb) {
339         g_free(ioctl_req);
340         status = VIRTIO_BLK_S_UNSUPP;
341         goto fail;
342     }
343     return -EINPROGRESS;
344 #else
345     abort();
346 #endif
347 
348 fail:
349     /* Just put anything nonzero so that the ioctl fails in the guest.  */
350     if (scsi) {
351         virtio_stl_p(vdev, &scsi->errors, 255);
352     }
353     return status;
354 }
355 
356 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
357 {
358     int status;
359 
360     status = virtio_blk_handle_scsi_req(req);
361     if (status != -EINPROGRESS) {
362         virtio_blk_req_complete(req, status);
363         virtio_blk_free_request(req);
364     }
365 }
366 
367 static inline void submit_requests(VirtIOBlock *s, MultiReqBuffer *mrb,
368                                    int start, int num_reqs, int niov)
369 {
370     BlockBackend *blk = s->blk;
371     QEMUIOVector *qiov = &mrb->reqs[start]->qiov;
372     int64_t sector_num = mrb->reqs[start]->sector_num;
373     bool is_write = mrb->is_write;
374     BdrvRequestFlags flags = 0;
375 
376     if (num_reqs > 1) {
377         int i;
378         struct iovec *tmp_iov = qiov->iov;
379         int tmp_niov = qiov->niov;
380 
381         /* mrb->reqs[start]->qiov was initialized from external so we can't
382          * modify it here. We need to initialize it locally and then add the
383          * external iovecs. */
384         qemu_iovec_init(qiov, niov);
385 
386         for (i = 0; i < tmp_niov; i++) {
387             qemu_iovec_add(qiov, tmp_iov[i].iov_base, tmp_iov[i].iov_len);
388         }
389 
390         for (i = start + 1; i < start + num_reqs; i++) {
391             qemu_iovec_concat(qiov, &mrb->reqs[i]->qiov, 0,
392                               mrb->reqs[i]->qiov.size);
393             mrb->reqs[i - 1]->mr_next = mrb->reqs[i];
394         }
395 
396         trace_virtio_blk_submit_multireq(VIRTIO_DEVICE(mrb->reqs[start]->dev),
397                                          mrb, start, num_reqs,
398                                          sector_num << BDRV_SECTOR_BITS,
399                                          qiov->size, is_write);
400         block_acct_merge_done(blk_get_stats(blk),
401                               is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ,
402                               num_reqs - 1);
403     }
404 
405     if (blk_ram_registrar_ok(&s->blk_ram_registrar)) {
406         flags |= BDRV_REQ_REGISTERED_BUF;
407     }
408 
409     if (is_write) {
410         blk_aio_pwritev(blk, sector_num << BDRV_SECTOR_BITS, qiov,
411                         flags, virtio_blk_rw_complete,
412                         mrb->reqs[start]);
413     } else {
414         blk_aio_preadv(blk, sector_num << BDRV_SECTOR_BITS, qiov,
415                        flags, virtio_blk_rw_complete,
416                        mrb->reqs[start]);
417     }
418 }
419 
420 static int multireq_compare(const void *a, const void *b)
421 {
422     const VirtIOBlockReq *req1 = *(VirtIOBlockReq **)a,
423                          *req2 = *(VirtIOBlockReq **)b;
424 
425     /*
426      * Note that we can't simply subtract sector_num1 from sector_num2
427      * here as that could overflow the return value.
428      */
429     if (req1->sector_num > req2->sector_num) {
430         return 1;
431     } else if (req1->sector_num < req2->sector_num) {
432         return -1;
433     } else {
434         return 0;
435     }
436 }
437 
438 static void virtio_blk_submit_multireq(VirtIOBlock *s, MultiReqBuffer *mrb)
439 {
440     int i = 0, start = 0, num_reqs = 0, niov = 0, nb_sectors = 0;
441     uint32_t max_transfer;
442     int64_t sector_num = 0;
443 
444     if (mrb->num_reqs == 1) {
445         submit_requests(s, mrb, 0, 1, -1);
446         mrb->num_reqs = 0;
447         return;
448     }
449 
450     max_transfer = blk_get_max_transfer(mrb->reqs[0]->dev->blk);
451 
452     qsort(mrb->reqs, mrb->num_reqs, sizeof(*mrb->reqs),
453           &multireq_compare);
454 
455     for (i = 0; i < mrb->num_reqs; i++) {
456         VirtIOBlockReq *req = mrb->reqs[i];
457         if (num_reqs > 0) {
458             /*
459              * NOTE: We cannot merge the requests in below situations:
460              * 1. requests are not sequential
461              * 2. merge would exceed maximum number of IOVs
462              * 3. merge would exceed maximum transfer length of backend device
463              */
464             if (sector_num + nb_sectors != req->sector_num ||
465                 niov > blk_get_max_iov(s->blk) - req->qiov.niov ||
466                 req->qiov.size > max_transfer ||
467                 nb_sectors > (max_transfer -
468                               req->qiov.size) / BDRV_SECTOR_SIZE) {
469                 submit_requests(s, mrb, start, num_reqs, niov);
470                 num_reqs = 0;
471             }
472         }
473 
474         if (num_reqs == 0) {
475             sector_num = req->sector_num;
476             nb_sectors = niov = 0;
477             start = i;
478         }
479 
480         nb_sectors += req->qiov.size / BDRV_SECTOR_SIZE;
481         niov += req->qiov.niov;
482         num_reqs++;
483     }
484 
485     submit_requests(s, mrb, start, num_reqs, niov);
486     mrb->num_reqs = 0;
487 }
488 
489 static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
490 {
491     VirtIOBlock *s = req->dev;
492 
493     block_acct_start(blk_get_stats(s->blk), &req->acct, 0,
494                      BLOCK_ACCT_FLUSH);
495 
496     /*
497      * Make sure all outstanding writes are posted to the backing device.
498      */
499     if (mrb->is_write && mrb->num_reqs > 0) {
500         virtio_blk_submit_multireq(s, mrb);
501     }
502     blk_aio_flush(s->blk, virtio_blk_flush_complete, req);
503 }
504 
505 static bool virtio_blk_sect_range_ok(VirtIOBlock *dev,
506                                      uint64_t sector, size_t size)
507 {
508     uint64_t nb_sectors = size >> BDRV_SECTOR_BITS;
509     uint64_t total_sectors;
510 
511     if (nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
512         return false;
513     }
514     if (sector & dev->sector_mask) {
515         return false;
516     }
517     if (size % dev->conf.conf.logical_block_size) {
518         return false;
519     }
520     blk_get_geometry(dev->blk, &total_sectors);
521     if (sector > total_sectors || nb_sectors > total_sectors - sector) {
522         return false;
523     }
524     return true;
525 }
526 
527 static uint8_t virtio_blk_handle_discard_write_zeroes(VirtIOBlockReq *req,
528     struct virtio_blk_discard_write_zeroes *dwz_hdr, bool is_write_zeroes)
529 {
530     VirtIOBlock *s = req->dev;
531     VirtIODevice *vdev = VIRTIO_DEVICE(s);
532     uint64_t sector;
533     uint32_t num_sectors, flags, max_sectors;
534     uint8_t err_status;
535     int bytes;
536 
537     sector = virtio_ldq_p(vdev, &dwz_hdr->sector);
538     num_sectors = virtio_ldl_p(vdev, &dwz_hdr->num_sectors);
539     flags = virtio_ldl_p(vdev, &dwz_hdr->flags);
540     max_sectors = is_write_zeroes ? s->conf.max_write_zeroes_sectors :
541                   s->conf.max_discard_sectors;
542 
543     /*
544      * max_sectors is at most BDRV_REQUEST_MAX_SECTORS, this check
545      * make us sure that "num_sectors << BDRV_SECTOR_BITS" can fit in
546      * the integer variable.
547      */
548     if (unlikely(num_sectors > max_sectors)) {
549         err_status = VIRTIO_BLK_S_IOERR;
550         goto err;
551     }
552 
553     bytes = num_sectors << BDRV_SECTOR_BITS;
554 
555     if (unlikely(!virtio_blk_sect_range_ok(s, sector, bytes))) {
556         err_status = VIRTIO_BLK_S_IOERR;
557         goto err;
558     }
559 
560     /*
561      * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for discard
562      * and write zeroes commands if any unknown flag is set.
563      */
564     if (unlikely(flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) {
565         err_status = VIRTIO_BLK_S_UNSUPP;
566         goto err;
567     }
568 
569     if (is_write_zeroes) { /* VIRTIO_BLK_T_WRITE_ZEROES */
570         int blk_aio_flags = 0;
571 
572         if (flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) {
573             blk_aio_flags |= BDRV_REQ_MAY_UNMAP;
574         }
575 
576         block_acct_start(blk_get_stats(s->blk), &req->acct, bytes,
577                          BLOCK_ACCT_WRITE);
578 
579         blk_aio_pwrite_zeroes(s->blk, sector << BDRV_SECTOR_BITS,
580                               bytes, blk_aio_flags,
581                               virtio_blk_discard_write_zeroes_complete, req);
582     } else { /* VIRTIO_BLK_T_DISCARD */
583         /*
584          * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for
585          * discard commands if the unmap flag is set.
586          */
587         if (unlikely(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) {
588             err_status = VIRTIO_BLK_S_UNSUPP;
589             goto err;
590         }
591 
592         blk_aio_pdiscard(s->blk, sector << BDRV_SECTOR_BITS, bytes,
593                          virtio_blk_discard_write_zeroes_complete, req);
594     }
595 
596     return VIRTIO_BLK_S_OK;
597 
598 err:
599     if (is_write_zeroes) {
600         block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_WRITE);
601     }
602     return err_status;
603 }
604 
605 typedef struct ZoneCmdData {
606     VirtIOBlockReq *req;
607     struct iovec *in_iov;
608     unsigned in_num;
609     union {
610         struct {
611             unsigned int nr_zones;
612             BlockZoneDescriptor *zones;
613         } zone_report_data;
614         struct {
615             int64_t offset;
616         } zone_append_data;
617     };
618 } ZoneCmdData;
619 
620 /*
621  * check zoned_request: error checking before issuing requests. If all checks
622  * passed, return true.
623  * append: true if only zone append requests issued.
624  */
625 static bool check_zoned_request(VirtIOBlock *s, int64_t offset, int64_t len,
626                              bool append, uint8_t *status) {
627     BlockDriverState *bs = blk_bs(s->blk);
628     int index;
629 
630     if (!virtio_has_feature(s->host_features, VIRTIO_BLK_F_ZONED)) {
631         *status = VIRTIO_BLK_S_UNSUPP;
632         return false;
633     }
634 
635     if (offset < 0 || len < 0 || len > (bs->total_sectors << BDRV_SECTOR_BITS)
636         || offset > (bs->total_sectors << BDRV_SECTOR_BITS) - len) {
637         *status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
638         return false;
639     }
640 
641     if (append) {
642         if (bs->bl.write_granularity) {
643             if ((offset % bs->bl.write_granularity) != 0) {
644                 *status = VIRTIO_BLK_S_ZONE_UNALIGNED_WP;
645                 return false;
646             }
647         }
648 
649         index = offset / bs->bl.zone_size;
650         if (BDRV_ZT_IS_CONV(bs->wps->wp[index])) {
651             *status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
652             return false;
653         }
654 
655         if (len / 512 > bs->bl.max_append_sectors) {
656             if (bs->bl.max_append_sectors == 0) {
657                 *status = VIRTIO_BLK_S_UNSUPP;
658             } else {
659                 *status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
660             }
661             return false;
662         }
663     }
664     return true;
665 }
666 
667 static void virtio_blk_zone_report_complete(void *opaque, int ret)
668 {
669     ZoneCmdData *data = opaque;
670     VirtIOBlockReq *req = data->req;
671     VirtIOBlock *s = req->dev;
672     VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
673     struct iovec *in_iov = data->in_iov;
674     unsigned in_num = data->in_num;
675     int64_t zrp_size, n, j = 0;
676     int64_t nz = data->zone_report_data.nr_zones;
677     int8_t err_status = VIRTIO_BLK_S_OK;
678 
679     trace_virtio_blk_zone_report_complete(vdev, req, nz, ret);
680     if (ret) {
681         err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
682         goto out;
683     }
684 
685     struct virtio_blk_zone_report zrp_hdr = (struct virtio_blk_zone_report) {
686         .nr_zones = cpu_to_le64(nz),
687     };
688     zrp_size = sizeof(struct virtio_blk_zone_report)
689                + sizeof(struct virtio_blk_zone_descriptor) * nz;
690     n = iov_from_buf(in_iov, in_num, 0, &zrp_hdr, sizeof(zrp_hdr));
691     if (n != sizeof(zrp_hdr)) {
692         virtio_error(vdev, "Driver provided input buffer that is too small!");
693         err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
694         goto out;
695     }
696 
697     for (size_t i = sizeof(zrp_hdr); i < zrp_size;
698         i += sizeof(struct virtio_blk_zone_descriptor), ++j) {
699         struct virtio_blk_zone_descriptor desc =
700             (struct virtio_blk_zone_descriptor) {
701                 .z_start = cpu_to_le64(data->zone_report_data.zones[j].start
702                     >> BDRV_SECTOR_BITS),
703                 .z_cap = cpu_to_le64(data->zone_report_data.zones[j].cap
704                     >> BDRV_SECTOR_BITS),
705                 .z_wp = cpu_to_le64(data->zone_report_data.zones[j].wp
706                     >> BDRV_SECTOR_BITS),
707         };
708 
709         switch (data->zone_report_data.zones[j].type) {
710         case BLK_ZT_CONV:
711             desc.z_type = VIRTIO_BLK_ZT_CONV;
712             break;
713         case BLK_ZT_SWR:
714             desc.z_type = VIRTIO_BLK_ZT_SWR;
715             break;
716         case BLK_ZT_SWP:
717             desc.z_type = VIRTIO_BLK_ZT_SWP;
718             break;
719         default:
720             g_assert_not_reached();
721         }
722 
723         switch (data->zone_report_data.zones[j].state) {
724         case BLK_ZS_RDONLY:
725             desc.z_state = VIRTIO_BLK_ZS_RDONLY;
726             break;
727         case BLK_ZS_OFFLINE:
728             desc.z_state = VIRTIO_BLK_ZS_OFFLINE;
729             break;
730         case BLK_ZS_EMPTY:
731             desc.z_state = VIRTIO_BLK_ZS_EMPTY;
732             break;
733         case BLK_ZS_CLOSED:
734             desc.z_state = VIRTIO_BLK_ZS_CLOSED;
735             break;
736         case BLK_ZS_FULL:
737             desc.z_state = VIRTIO_BLK_ZS_FULL;
738             break;
739         case BLK_ZS_EOPEN:
740             desc.z_state = VIRTIO_BLK_ZS_EOPEN;
741             break;
742         case BLK_ZS_IOPEN:
743             desc.z_state = VIRTIO_BLK_ZS_IOPEN;
744             break;
745         case BLK_ZS_NOT_WP:
746             desc.z_state = VIRTIO_BLK_ZS_NOT_WP;
747             break;
748         default:
749             g_assert_not_reached();
750         }
751 
752         /* TODO: it takes O(n^2) time complexity. Optimizations required. */
753         n = iov_from_buf(in_iov, in_num, i, &desc, sizeof(desc));
754         if (n != sizeof(desc)) {
755             virtio_error(vdev, "Driver provided input buffer "
756                                "for descriptors that is too small!");
757             err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
758         }
759     }
760 
761 out:
762     aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
763     virtio_blk_req_complete(req, err_status);
764     virtio_blk_free_request(req);
765     aio_context_release(blk_get_aio_context(s->conf.conf.blk));
766     g_free(data->zone_report_data.zones);
767     g_free(data);
768 }
769 
770 static void virtio_blk_handle_zone_report(VirtIOBlockReq *req,
771                                          struct iovec *in_iov,
772                                          unsigned in_num)
773 {
774     VirtIOBlock *s = req->dev;
775     VirtIODevice *vdev = VIRTIO_DEVICE(s);
776     unsigned int nr_zones;
777     ZoneCmdData *data;
778     int64_t zone_size, offset;
779     uint8_t err_status;
780 
781     if (req->in_len < sizeof(struct virtio_blk_inhdr) +
782             sizeof(struct virtio_blk_zone_report) +
783             sizeof(struct virtio_blk_zone_descriptor)) {
784         virtio_error(vdev, "in buffer too small for zone report");
785         return;
786     }
787 
788     /* start byte offset of the zone report */
789     offset = virtio_ldq_p(vdev, &req->out.sector) << BDRV_SECTOR_BITS;
790     if (!check_zoned_request(s, offset, 0, false, &err_status)) {
791         goto out;
792     }
793     nr_zones = (req->in_len - sizeof(struct virtio_blk_inhdr) -
794                 sizeof(struct virtio_blk_zone_report)) /
795                sizeof(struct virtio_blk_zone_descriptor);
796     trace_virtio_blk_handle_zone_report(vdev, req,
797                                         offset >> BDRV_SECTOR_BITS, nr_zones);
798 
799     zone_size = sizeof(BlockZoneDescriptor) * nr_zones;
800     data = g_malloc(sizeof(ZoneCmdData));
801     data->req = req;
802     data->in_iov = in_iov;
803     data->in_num = in_num;
804     data->zone_report_data.nr_zones = nr_zones;
805     data->zone_report_data.zones = g_malloc(zone_size),
806 
807     blk_aio_zone_report(s->blk, offset, &data->zone_report_data.nr_zones,
808                         data->zone_report_data.zones,
809                         virtio_blk_zone_report_complete, data);
810     return;
811 out:
812     virtio_blk_req_complete(req, err_status);
813     virtio_blk_free_request(req);
814 }
815 
816 static void virtio_blk_zone_mgmt_complete(void *opaque, int ret)
817 {
818     VirtIOBlockReq *req = opaque;
819     VirtIOBlock *s = req->dev;
820     VirtIODevice *vdev = VIRTIO_DEVICE(s);
821     int8_t err_status = VIRTIO_BLK_S_OK;
822     trace_virtio_blk_zone_mgmt_complete(vdev, req,ret);
823 
824     if (ret) {
825         err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
826     }
827 
828     aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
829     virtio_blk_req_complete(req, err_status);
830     virtio_blk_free_request(req);
831     aio_context_release(blk_get_aio_context(s->conf.conf.blk));
832 }
833 
834 static int virtio_blk_handle_zone_mgmt(VirtIOBlockReq *req, BlockZoneOp op)
835 {
836     VirtIOBlock *s = req->dev;
837     VirtIODevice *vdev = VIRTIO_DEVICE(s);
838     BlockDriverState *bs = blk_bs(s->blk);
839     int64_t offset = virtio_ldq_p(vdev, &req->out.sector) << BDRV_SECTOR_BITS;
840     uint64_t len;
841     uint64_t capacity = bs->total_sectors << BDRV_SECTOR_BITS;
842     uint8_t err_status = VIRTIO_BLK_S_OK;
843 
844     uint32_t type = virtio_ldl_p(vdev, &req->out.type);
845     if (type == VIRTIO_BLK_T_ZONE_RESET_ALL) {
846         /* Entire drive capacity */
847         offset = 0;
848         len = capacity;
849         trace_virtio_blk_handle_zone_reset_all(vdev, req, 0,
850                                                bs->total_sectors);
851     } else {
852         if (bs->bl.zone_size > capacity - offset) {
853             /* The zoned device allows the last smaller zone. */
854             len = capacity - bs->bl.zone_size * (bs->bl.nr_zones - 1);
855         } else {
856             len = bs->bl.zone_size;
857         }
858         trace_virtio_blk_handle_zone_mgmt(vdev, req, op,
859                                           offset >> BDRV_SECTOR_BITS,
860                                           len >> BDRV_SECTOR_BITS);
861     }
862 
863     if (!check_zoned_request(s, offset, len, false, &err_status)) {
864         goto out;
865     }
866 
867     blk_aio_zone_mgmt(s->blk, op, offset, len,
868                       virtio_blk_zone_mgmt_complete, req);
869 
870     return 0;
871 out:
872     virtio_blk_req_complete(req, err_status);
873     virtio_blk_free_request(req);
874     return err_status;
875 }
876 
877 static void virtio_blk_zone_append_complete(void *opaque, int ret)
878 {
879     ZoneCmdData *data = opaque;
880     VirtIOBlockReq *req = data->req;
881     VirtIOBlock *s = req->dev;
882     VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
883     int64_t append_sector, n;
884     uint8_t err_status = VIRTIO_BLK_S_OK;
885 
886     if (ret) {
887         err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
888         goto out;
889     }
890 
891     virtio_stq_p(vdev, &append_sector,
892                  data->zone_append_data.offset >> BDRV_SECTOR_BITS);
893     n = iov_from_buf(data->in_iov, data->in_num, 0, &append_sector,
894                      sizeof(append_sector));
895     if (n != sizeof(append_sector)) {
896         virtio_error(vdev, "Driver provided input buffer less than size of "
897                            "append_sector");
898         err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
899         goto out;
900     }
901     trace_virtio_blk_zone_append_complete(vdev, req, append_sector, ret);
902 
903 out:
904     aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
905     virtio_blk_req_complete(req, err_status);
906     virtio_blk_free_request(req);
907     aio_context_release(blk_get_aio_context(s->conf.conf.blk));
908     g_free(data);
909 }
910 
911 static int virtio_blk_handle_zone_append(VirtIOBlockReq *req,
912                                          struct iovec *out_iov,
913                                          struct iovec *in_iov,
914                                          uint64_t out_num,
915                                          unsigned in_num) {
916     VirtIOBlock *s = req->dev;
917     VirtIODevice *vdev = VIRTIO_DEVICE(s);
918     uint8_t err_status = VIRTIO_BLK_S_OK;
919 
920     int64_t offset = virtio_ldq_p(vdev, &req->out.sector) << BDRV_SECTOR_BITS;
921     int64_t len = iov_size(out_iov, out_num);
922 
923     trace_virtio_blk_handle_zone_append(vdev, req, offset >> BDRV_SECTOR_BITS);
924     if (!check_zoned_request(s, offset, len, true, &err_status)) {
925         goto out;
926     }
927 
928     ZoneCmdData *data = g_malloc(sizeof(ZoneCmdData));
929     data->req = req;
930     data->in_iov = in_iov;
931     data->in_num = in_num;
932     data->zone_append_data.offset = offset;
933     qemu_iovec_init_external(&req->qiov, out_iov, out_num);
934 
935     block_acct_start(blk_get_stats(s->blk), &req->acct, len,
936                      BLOCK_ACCT_ZONE_APPEND);
937 
938     blk_aio_zone_append(s->blk, &data->zone_append_data.offset, &req->qiov, 0,
939                         virtio_blk_zone_append_complete, data);
940     return 0;
941 
942 out:
943     aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
944     virtio_blk_req_complete(req, err_status);
945     virtio_blk_free_request(req);
946     aio_context_release(blk_get_aio_context(s->conf.conf.blk));
947     return err_status;
948 }
949 
950 static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
951 {
952     uint32_t type;
953     struct iovec *in_iov = req->elem.in_sg;
954     struct iovec *out_iov = req->elem.out_sg;
955     unsigned in_num = req->elem.in_num;
956     unsigned out_num = req->elem.out_num;
957     VirtIOBlock *s = req->dev;
958     VirtIODevice *vdev = VIRTIO_DEVICE(s);
959 
960     if (req->elem.out_num < 1 || req->elem.in_num < 1) {
961         virtio_error(vdev, "virtio-blk missing headers");
962         return -1;
963     }
964 
965     if (unlikely(iov_to_buf(out_iov, out_num, 0, &req->out,
966                             sizeof(req->out)) != sizeof(req->out))) {
967         virtio_error(vdev, "virtio-blk request outhdr too short");
968         return -1;
969     }
970 
971     iov_discard_front_undoable(&out_iov, &out_num, sizeof(req->out),
972                                &req->outhdr_undo);
973 
974     if (in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) {
975         virtio_error(vdev, "virtio-blk request inhdr too short");
976         iov_discard_undo(&req->outhdr_undo);
977         return -1;
978     }
979 
980     /* We always touch the last byte, so just see how big in_iov is.  */
981     req->in_len = iov_size(in_iov, in_num);
982     req->in = (void *)in_iov[in_num - 1].iov_base
983               + in_iov[in_num - 1].iov_len
984               - sizeof(struct virtio_blk_inhdr);
985     iov_discard_back_undoable(in_iov, &in_num, sizeof(struct virtio_blk_inhdr),
986                               &req->inhdr_undo);
987 
988     type = virtio_ldl_p(vdev, &req->out.type);
989 
990     /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER
991      * is an optional flag. Although a guest should not send this flag if
992      * not negotiated we ignored it in the past. So keep ignoring it. */
993     switch (type & ~(VIRTIO_BLK_T_OUT | VIRTIO_BLK_T_BARRIER)) {
994     case VIRTIO_BLK_T_IN:
995     {
996         bool is_write = type & VIRTIO_BLK_T_OUT;
997         req->sector_num = virtio_ldq_p(vdev, &req->out.sector);
998 
999         if (is_write) {
1000             qemu_iovec_init_external(&req->qiov, out_iov, out_num);
1001             trace_virtio_blk_handle_write(vdev, req, req->sector_num,
1002                                           req->qiov.size / BDRV_SECTOR_SIZE);
1003         } else {
1004             qemu_iovec_init_external(&req->qiov, in_iov, in_num);
1005             trace_virtio_blk_handle_read(vdev, req, req->sector_num,
1006                                          req->qiov.size / BDRV_SECTOR_SIZE);
1007         }
1008 
1009         if (!virtio_blk_sect_range_ok(s, req->sector_num, req->qiov.size)) {
1010             virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
1011             block_acct_invalid(blk_get_stats(s->blk),
1012                                is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ);
1013             virtio_blk_free_request(req);
1014             return 0;
1015         }
1016 
1017         block_acct_start(blk_get_stats(s->blk), &req->acct, req->qiov.size,
1018                          is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ);
1019 
1020         /* merge would exceed maximum number of requests or IO direction
1021          * changes */
1022         if (mrb->num_reqs > 0 && (mrb->num_reqs == VIRTIO_BLK_MAX_MERGE_REQS ||
1023                                   is_write != mrb->is_write ||
1024                                   !s->conf.request_merging)) {
1025             virtio_blk_submit_multireq(s, mrb);
1026         }
1027 
1028         assert(mrb->num_reqs < VIRTIO_BLK_MAX_MERGE_REQS);
1029         mrb->reqs[mrb->num_reqs++] = req;
1030         mrb->is_write = is_write;
1031         break;
1032     }
1033     case VIRTIO_BLK_T_FLUSH:
1034         virtio_blk_handle_flush(req, mrb);
1035         break;
1036     case VIRTIO_BLK_T_ZONE_REPORT:
1037         virtio_blk_handle_zone_report(req, in_iov, in_num);
1038         break;
1039     case VIRTIO_BLK_T_ZONE_OPEN:
1040         virtio_blk_handle_zone_mgmt(req, BLK_ZO_OPEN);
1041         break;
1042     case VIRTIO_BLK_T_ZONE_CLOSE:
1043         virtio_blk_handle_zone_mgmt(req, BLK_ZO_CLOSE);
1044         break;
1045     case VIRTIO_BLK_T_ZONE_FINISH:
1046         virtio_blk_handle_zone_mgmt(req, BLK_ZO_FINISH);
1047         break;
1048     case VIRTIO_BLK_T_ZONE_RESET:
1049         virtio_blk_handle_zone_mgmt(req, BLK_ZO_RESET);
1050         break;
1051     case VIRTIO_BLK_T_ZONE_RESET_ALL:
1052         virtio_blk_handle_zone_mgmt(req, BLK_ZO_RESET);
1053         break;
1054     case VIRTIO_BLK_T_SCSI_CMD:
1055         virtio_blk_handle_scsi(req);
1056         break;
1057     case VIRTIO_BLK_T_GET_ID:
1058     {
1059         /*
1060          * NB: per existing s/n string convention the string is
1061          * terminated by '\0' only when shorter than buffer.
1062          */
1063         const char *serial = s->conf.serial ? s->conf.serial : "";
1064         size_t size = MIN(strlen(serial) + 1,
1065                           MIN(iov_size(in_iov, in_num),
1066                               VIRTIO_BLK_ID_BYTES));
1067         iov_from_buf(in_iov, in_num, 0, serial, size);
1068         virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
1069         virtio_blk_free_request(req);
1070         break;
1071     }
1072     case VIRTIO_BLK_T_ZONE_APPEND & ~VIRTIO_BLK_T_OUT:
1073         /*
1074          * Passing out_iov/out_num and in_iov/in_num is not safe
1075          * to access req->elem.out_sg directly because it may be
1076          * modified by virtio_blk_handle_request().
1077          */
1078         virtio_blk_handle_zone_append(req, out_iov, in_iov, out_num, in_num);
1079         break;
1080     /*
1081      * VIRTIO_BLK_T_DISCARD and VIRTIO_BLK_T_WRITE_ZEROES are defined with
1082      * VIRTIO_BLK_T_OUT flag set. We masked this flag in the switch statement,
1083      * so we must mask it for these requests, then we will check if it is set.
1084      */
1085     case VIRTIO_BLK_T_DISCARD & ~VIRTIO_BLK_T_OUT:
1086     case VIRTIO_BLK_T_WRITE_ZEROES & ~VIRTIO_BLK_T_OUT:
1087     {
1088         struct virtio_blk_discard_write_zeroes dwz_hdr;
1089         size_t out_len = iov_size(out_iov, out_num);
1090         bool is_write_zeroes = (type & ~VIRTIO_BLK_T_BARRIER) ==
1091                                VIRTIO_BLK_T_WRITE_ZEROES;
1092         uint8_t err_status;
1093 
1094         /*
1095          * Unsupported if VIRTIO_BLK_T_OUT is not set or the request contains
1096          * more than one segment.
1097          */
1098         if (unlikely(!(type & VIRTIO_BLK_T_OUT) ||
1099                      out_len > sizeof(dwz_hdr))) {
1100             virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
1101             virtio_blk_free_request(req);
1102             return 0;
1103         }
1104 
1105         if (unlikely(iov_to_buf(out_iov, out_num, 0, &dwz_hdr,
1106                                 sizeof(dwz_hdr)) != sizeof(dwz_hdr))) {
1107             iov_discard_undo(&req->inhdr_undo);
1108             iov_discard_undo(&req->outhdr_undo);
1109             virtio_error(vdev, "virtio-blk discard/write_zeroes header"
1110                          " too short");
1111             return -1;
1112         }
1113 
1114         err_status = virtio_blk_handle_discard_write_zeroes(req, &dwz_hdr,
1115                                                             is_write_zeroes);
1116         if (err_status != VIRTIO_BLK_S_OK) {
1117             virtio_blk_req_complete(req, err_status);
1118             virtio_blk_free_request(req);
1119         }
1120 
1121         break;
1122     }
1123     default:
1124         virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
1125         virtio_blk_free_request(req);
1126     }
1127     return 0;
1128 }
1129 
1130 void virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq)
1131 {
1132     VirtIOBlockReq *req;
1133     MultiReqBuffer mrb = {};
1134     bool suppress_notifications = virtio_queue_get_notification(vq);
1135 
1136     aio_context_acquire(blk_get_aio_context(s->blk));
1137     blk_io_plug();
1138 
1139     do {
1140         if (suppress_notifications) {
1141             virtio_queue_set_notification(vq, 0);
1142         }
1143 
1144         while ((req = virtio_blk_get_request(s, vq))) {
1145             if (virtio_blk_handle_request(req, &mrb)) {
1146                 virtqueue_detach_element(req->vq, &req->elem, 0);
1147                 virtio_blk_free_request(req);
1148                 break;
1149             }
1150         }
1151 
1152         if (suppress_notifications) {
1153             virtio_queue_set_notification(vq, 1);
1154         }
1155     } while (!virtio_queue_empty(vq));
1156 
1157     if (mrb.num_reqs) {
1158         virtio_blk_submit_multireq(s, &mrb);
1159     }
1160 
1161     blk_io_unplug();
1162     aio_context_release(blk_get_aio_context(s->blk));
1163 }
1164 
1165 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
1166 {
1167     VirtIOBlock *s = (VirtIOBlock *)vdev;
1168 
1169     if (s->dataplane && !s->dataplane_started) {
1170         /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
1171          * dataplane here instead of waiting for .set_status().
1172          */
1173         virtio_device_start_ioeventfd(vdev);
1174         if (!s->dataplane_disabled) {
1175             return;
1176         }
1177     }
1178     virtio_blk_handle_vq(s, vq);
1179 }
1180 
1181 static void virtio_blk_dma_restart_bh(void *opaque)
1182 {
1183     VirtIOBlock *s = opaque;
1184 
1185     VirtIOBlockReq *req = s->rq;
1186     MultiReqBuffer mrb = {};
1187 
1188     s->rq = NULL;
1189 
1190     aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
1191     while (req) {
1192         VirtIOBlockReq *next = req->next;
1193         if (virtio_blk_handle_request(req, &mrb)) {
1194             /* Device is now broken and won't do any processing until it gets
1195              * reset. Already queued requests will be lost: let's purge them.
1196              */
1197             while (req) {
1198                 next = req->next;
1199                 virtqueue_detach_element(req->vq, &req->elem, 0);
1200                 virtio_blk_free_request(req);
1201                 req = next;
1202             }
1203             break;
1204         }
1205         req = next;
1206     }
1207 
1208     if (mrb.num_reqs) {
1209         virtio_blk_submit_multireq(s, &mrb);
1210     }
1211 
1212     /* Paired with inc in virtio_blk_dma_restart_cb() */
1213     blk_dec_in_flight(s->conf.conf.blk);
1214 
1215     aio_context_release(blk_get_aio_context(s->conf.conf.blk));
1216 }
1217 
1218 static void virtio_blk_dma_restart_cb(void *opaque, bool running,
1219                                       RunState state)
1220 {
1221     VirtIOBlock *s = opaque;
1222 
1223     if (!running) {
1224         return;
1225     }
1226 
1227     /* Paired with dec in virtio_blk_dma_restart_bh() */
1228     blk_inc_in_flight(s->conf.conf.blk);
1229 
1230     aio_bh_schedule_oneshot(blk_get_aio_context(s->conf.conf.blk),
1231             virtio_blk_dma_restart_bh, s);
1232 }
1233 
1234 static void virtio_blk_reset(VirtIODevice *vdev)
1235 {
1236     VirtIOBlock *s = VIRTIO_BLK(vdev);
1237     AioContext *ctx;
1238     VirtIOBlockReq *req;
1239 
1240     ctx = blk_get_aio_context(s->blk);
1241     aio_context_acquire(ctx);
1242     blk_drain(s->blk);
1243 
1244     /* We drop queued requests after blk_drain() because blk_drain() itself can
1245      * produce them. */
1246     while (s->rq) {
1247         req = s->rq;
1248         s->rq = req->next;
1249         virtqueue_detach_element(req->vq, &req->elem, 0);
1250         virtio_blk_free_request(req);
1251     }
1252 
1253     aio_context_release(ctx);
1254 
1255     assert(!s->dataplane_started);
1256     blk_set_enable_write_cache(s->blk, s->original_wce);
1257 }
1258 
1259 /* coalesce internal state, copy to pci i/o region 0
1260  */
1261 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
1262 {
1263     VirtIOBlock *s = VIRTIO_BLK(vdev);
1264     BlockConf *conf = &s->conf.conf;
1265     BlockDriverState *bs = blk_bs(s->blk);
1266     struct virtio_blk_config blkcfg;
1267     uint64_t capacity;
1268     int64_t length;
1269     int blk_size = conf->logical_block_size;
1270     AioContext *ctx;
1271 
1272     ctx = blk_get_aio_context(s->blk);
1273     aio_context_acquire(ctx);
1274 
1275     blk_get_geometry(s->blk, &capacity);
1276     memset(&blkcfg, 0, sizeof(blkcfg));
1277     virtio_stq_p(vdev, &blkcfg.capacity, capacity);
1278     virtio_stl_p(vdev, &blkcfg.seg_max,
1279                  s->conf.seg_max_adjust ? s->conf.queue_size - 2 : 128 - 2);
1280     virtio_stw_p(vdev, &blkcfg.geometry.cylinders, conf->cyls);
1281     virtio_stl_p(vdev, &blkcfg.blk_size, blk_size);
1282     virtio_stw_p(vdev, &blkcfg.min_io_size, conf->min_io_size / blk_size);
1283     virtio_stl_p(vdev, &blkcfg.opt_io_size, conf->opt_io_size / blk_size);
1284     blkcfg.geometry.heads = conf->heads;
1285     /*
1286      * We must ensure that the block device capacity is a multiple of
1287      * the logical block size. If that is not the case, let's use
1288      * sector_mask to adopt the geometry to have a correct picture.
1289      * For those devices where the capacity is ok for the given geometry
1290      * we don't touch the sector value of the geometry, since some devices
1291      * (like s390 dasd) need a specific value. Here the capacity is already
1292      * cyls*heads*secs*blk_size and the sector value is not block size
1293      * divided by 512 - instead it is the amount of blk_size blocks
1294      * per track (cylinder).
1295      */
1296     length = blk_getlength(s->blk);
1297     aio_context_release(ctx);
1298     if (length > 0 && length / conf->heads / conf->secs % blk_size) {
1299         blkcfg.geometry.sectors = conf->secs & ~s->sector_mask;
1300     } else {
1301         blkcfg.geometry.sectors = conf->secs;
1302     }
1303     blkcfg.size_max = 0;
1304     blkcfg.physical_block_exp = get_physical_block_exp(conf);
1305     blkcfg.alignment_offset = 0;
1306     blkcfg.wce = blk_enable_write_cache(s->blk);
1307     virtio_stw_p(vdev, &blkcfg.num_queues, s->conf.num_queues);
1308     if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_DISCARD)) {
1309         uint32_t discard_granularity = conf->discard_granularity;
1310         if (discard_granularity == -1 || !s->conf.report_discard_granularity) {
1311             discard_granularity = blk_size;
1312         }
1313         virtio_stl_p(vdev, &blkcfg.max_discard_sectors,
1314                      s->conf.max_discard_sectors);
1315         virtio_stl_p(vdev, &blkcfg.discard_sector_alignment,
1316                      discard_granularity >> BDRV_SECTOR_BITS);
1317         /*
1318          * We support only one segment per request since multiple segments
1319          * are not widely used and there are no userspace APIs that allow
1320          * applications to submit multiple segments in a single call.
1321          */
1322         virtio_stl_p(vdev, &blkcfg.max_discard_seg, 1);
1323     }
1324     if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES)) {
1325         virtio_stl_p(vdev, &blkcfg.max_write_zeroes_sectors,
1326                      s->conf.max_write_zeroes_sectors);
1327         blkcfg.write_zeroes_may_unmap = 1;
1328         virtio_stl_p(vdev, &blkcfg.max_write_zeroes_seg, 1);
1329     }
1330     if (bs->bl.zoned != BLK_Z_NONE) {
1331         switch (bs->bl.zoned) {
1332         case BLK_Z_HM:
1333             blkcfg.zoned.model = VIRTIO_BLK_Z_HM;
1334             break;
1335         case BLK_Z_HA:
1336             blkcfg.zoned.model = VIRTIO_BLK_Z_HA;
1337             break;
1338         default:
1339             g_assert_not_reached();
1340         }
1341 
1342         virtio_stl_p(vdev, &blkcfg.zoned.zone_sectors,
1343                      bs->bl.zone_size / 512);
1344         virtio_stl_p(vdev, &blkcfg.zoned.max_active_zones,
1345                      bs->bl.max_active_zones);
1346         virtio_stl_p(vdev, &blkcfg.zoned.max_open_zones,
1347                      bs->bl.max_open_zones);
1348         virtio_stl_p(vdev, &blkcfg.zoned.write_granularity, blk_size);
1349         virtio_stl_p(vdev, &blkcfg.zoned.max_append_sectors,
1350                      bs->bl.max_append_sectors);
1351     } else {
1352         blkcfg.zoned.model = VIRTIO_BLK_Z_NONE;
1353     }
1354     memcpy(config, &blkcfg, s->config_size);
1355 }
1356 
1357 static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
1358 {
1359     VirtIOBlock *s = VIRTIO_BLK(vdev);
1360     struct virtio_blk_config blkcfg;
1361 
1362     memcpy(&blkcfg, config, s->config_size);
1363 
1364     aio_context_acquire(blk_get_aio_context(s->blk));
1365     blk_set_enable_write_cache(s->blk, blkcfg.wce != 0);
1366     aio_context_release(blk_get_aio_context(s->blk));
1367 }
1368 
1369 static uint64_t virtio_blk_get_features(VirtIODevice *vdev, uint64_t features,
1370                                         Error **errp)
1371 {
1372     VirtIOBlock *s = VIRTIO_BLK(vdev);
1373 
1374     /* Firstly sync all virtio-blk possible supported features */
1375     features |= s->host_features;
1376 
1377     virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
1378     virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY);
1379     virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY);
1380     virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
1381     if (virtio_has_feature(features, VIRTIO_F_VERSION_1)) {
1382         if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_SCSI)) {
1383             error_setg(errp, "Please set scsi=off for virtio-blk devices in order to use virtio 1.0");
1384             return 0;
1385         }
1386     } else {
1387         virtio_clear_feature(&features, VIRTIO_F_ANY_LAYOUT);
1388         virtio_add_feature(&features, VIRTIO_BLK_F_SCSI);
1389     }
1390 
1391     if (blk_enable_write_cache(s->blk) ||
1392         (s->conf.x_enable_wce_if_config_wce &&
1393          virtio_has_feature(features, VIRTIO_BLK_F_CONFIG_WCE))) {
1394         virtio_add_feature(&features, VIRTIO_BLK_F_WCE);
1395     }
1396     if (!blk_is_writable(s->blk)) {
1397         virtio_add_feature(&features, VIRTIO_BLK_F_RO);
1398     }
1399     if (s->conf.num_queues > 1) {
1400         virtio_add_feature(&features, VIRTIO_BLK_F_MQ);
1401     }
1402 
1403     return features;
1404 }
1405 
1406 static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
1407 {
1408     VirtIOBlock *s = VIRTIO_BLK(vdev);
1409 
1410     if (!(status & (VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK))) {
1411         assert(!s->dataplane_started);
1412     }
1413 
1414     if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1415         return;
1416     }
1417 
1418     /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
1419      * cache flushes.  Thus, the "auto writethrough" behavior is never
1420      * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
1421      * Leaving it enabled would break the following sequence:
1422      *
1423      *     Guest started with "-drive cache=writethrough"
1424      *     Guest sets status to 0
1425      *     Guest sets DRIVER bit in status field
1426      *     Guest reads host features (WCE=0, CONFIG_WCE=1)
1427      *     Guest writes guest features (WCE=0, CONFIG_WCE=1)
1428      *     Guest writes 1 to the WCE configuration field (writeback mode)
1429      *     Guest sets DRIVER_OK bit in status field
1430      *
1431      * s->blk would erroneously be placed in writethrough mode.
1432      */
1433     if (!virtio_vdev_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE)) {
1434         aio_context_acquire(blk_get_aio_context(s->blk));
1435         blk_set_enable_write_cache(s->blk,
1436                                    virtio_vdev_has_feature(vdev,
1437                                                            VIRTIO_BLK_F_WCE));
1438         aio_context_release(blk_get_aio_context(s->blk));
1439     }
1440 }
1441 
1442 static void virtio_blk_save_device(VirtIODevice *vdev, QEMUFile *f)
1443 {
1444     VirtIOBlock *s = VIRTIO_BLK(vdev);
1445     VirtIOBlockReq *req = s->rq;
1446 
1447     while (req) {
1448         qemu_put_sbyte(f, 1);
1449 
1450         if (s->conf.num_queues > 1) {
1451             qemu_put_be32(f, virtio_get_queue_index(req->vq));
1452         }
1453 
1454         qemu_put_virtqueue_element(vdev, f, &req->elem);
1455         req = req->next;
1456     }
1457     qemu_put_sbyte(f, 0);
1458 }
1459 
1460 static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f,
1461                                   int version_id)
1462 {
1463     VirtIOBlock *s = VIRTIO_BLK(vdev);
1464 
1465     while (qemu_get_sbyte(f)) {
1466         unsigned nvqs = s->conf.num_queues;
1467         unsigned vq_idx = 0;
1468         VirtIOBlockReq *req;
1469 
1470         if (nvqs > 1) {
1471             vq_idx = qemu_get_be32(f);
1472 
1473             if (vq_idx >= nvqs) {
1474                 error_report("Invalid virtqueue index in request list: %#x",
1475                              vq_idx);
1476                 return -EINVAL;
1477             }
1478         }
1479 
1480         req = qemu_get_virtqueue_element(vdev, f, sizeof(VirtIOBlockReq));
1481         virtio_blk_init_request(s, virtio_get_queue(vdev, vq_idx), req);
1482         req->next = s->rq;
1483         s->rq = req;
1484     }
1485 
1486     return 0;
1487 }
1488 
1489 static void virtio_resize_cb(void *opaque)
1490 {
1491     VirtIODevice *vdev = opaque;
1492 
1493     assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1494     virtio_notify_config(vdev);
1495 }
1496 
1497 static void virtio_blk_resize(void *opaque)
1498 {
1499     VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
1500 
1501     /*
1502      * virtio_notify_config() needs to acquire the global mutex,
1503      * so it can't be called from an iothread. Instead, schedule
1504      * it to be run in the main context BH.
1505      */
1506     aio_bh_schedule_oneshot(qemu_get_aio_context(), virtio_resize_cb, vdev);
1507 }
1508 
1509 /* Suspend virtqueue ioeventfd processing during drain */
1510 static void virtio_blk_drained_begin(void *opaque)
1511 {
1512     VirtIOBlock *s = opaque;
1513     VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
1514     AioContext *ctx = blk_get_aio_context(s->conf.conf.blk);
1515 
1516     if (!s->dataplane || !s->dataplane_started) {
1517         return;
1518     }
1519 
1520     for (uint16_t i = 0; i < s->conf.num_queues; i++) {
1521         VirtQueue *vq = virtio_get_queue(vdev, i);
1522         virtio_queue_aio_detach_host_notifier(vq, ctx);
1523     }
1524 }
1525 
1526 /* Resume virtqueue ioeventfd processing after drain */
1527 static void virtio_blk_drained_end(void *opaque)
1528 {
1529     VirtIOBlock *s = opaque;
1530     VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
1531     AioContext *ctx = blk_get_aio_context(s->conf.conf.blk);
1532 
1533     if (!s->dataplane || !s->dataplane_started) {
1534         return;
1535     }
1536 
1537     for (uint16_t i = 0; i < s->conf.num_queues; i++) {
1538         VirtQueue *vq = virtio_get_queue(vdev, i);
1539         virtio_queue_aio_attach_host_notifier(vq, ctx);
1540     }
1541 }
1542 
1543 static const BlockDevOps virtio_block_ops = {
1544     .resize_cb     = virtio_blk_resize,
1545     .drained_begin = virtio_blk_drained_begin,
1546     .drained_end   = virtio_blk_drained_end,
1547 };
1548 
1549 static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
1550 {
1551     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1552     VirtIOBlock *s = VIRTIO_BLK(dev);
1553     VirtIOBlkConf *conf = &s->conf;
1554     Error *err = NULL;
1555     unsigned i;
1556 
1557     if (!conf->conf.blk) {
1558         error_setg(errp, "drive property not set");
1559         return;
1560     }
1561     if (!blk_is_inserted(conf->conf.blk)) {
1562         error_setg(errp, "Device needs media, but drive is empty");
1563         return;
1564     }
1565     if (conf->num_queues == VIRTIO_BLK_AUTO_NUM_QUEUES) {
1566         conf->num_queues = 1;
1567     }
1568     if (!conf->num_queues) {
1569         error_setg(errp, "num-queues property must be larger than 0");
1570         return;
1571     }
1572     if (conf->queue_size <= 2) {
1573         error_setg(errp, "invalid queue-size property (%" PRIu16 "), "
1574                    "must be > 2", conf->queue_size);
1575         return;
1576     }
1577     if (!is_power_of_2(conf->queue_size) ||
1578         conf->queue_size > VIRTQUEUE_MAX_SIZE) {
1579         error_setg(errp, "invalid queue-size property (%" PRIu16 "), "
1580                    "must be a power of 2 (max %d)",
1581                    conf->queue_size, VIRTQUEUE_MAX_SIZE);
1582         return;
1583     }
1584 
1585     if (!blkconf_apply_backend_options(&conf->conf,
1586                                        !blk_supports_write_perm(conf->conf.blk),
1587                                        true, errp)) {
1588         return;
1589     }
1590     s->original_wce = blk_enable_write_cache(conf->conf.blk);
1591     if (!blkconf_geometry(&conf->conf, NULL, 65535, 255, 255, errp)) {
1592         return;
1593     }
1594 
1595     if (!blkconf_blocksizes(&conf->conf, errp)) {
1596         return;
1597     }
1598 
1599     BlockDriverState *bs = blk_bs(conf->conf.blk);
1600     if (bs->bl.zoned != BLK_Z_NONE) {
1601         virtio_add_feature(&s->host_features, VIRTIO_BLK_F_ZONED);
1602         if (bs->bl.zoned == BLK_Z_HM) {
1603             virtio_clear_feature(&s->host_features, VIRTIO_BLK_F_DISCARD);
1604         }
1605     }
1606 
1607     if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_DISCARD) &&
1608         (!conf->max_discard_sectors ||
1609          conf->max_discard_sectors > BDRV_REQUEST_MAX_SECTORS)) {
1610         error_setg(errp, "invalid max-discard-sectors property (%" PRIu32 ")"
1611                    ", must be between 1 and %d",
1612                    conf->max_discard_sectors, (int)BDRV_REQUEST_MAX_SECTORS);
1613         return;
1614     }
1615 
1616     if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES) &&
1617         (!conf->max_write_zeroes_sectors ||
1618          conf->max_write_zeroes_sectors > BDRV_REQUEST_MAX_SECTORS)) {
1619         error_setg(errp, "invalid max-write-zeroes-sectors property (%" PRIu32
1620                    "), must be between 1 and %d",
1621                    conf->max_write_zeroes_sectors,
1622                    (int)BDRV_REQUEST_MAX_SECTORS);
1623         return;
1624     }
1625 
1626     s->config_size = virtio_get_config_size(&virtio_blk_cfg_size_params,
1627                                             s->host_features);
1628     virtio_init(vdev, VIRTIO_ID_BLOCK, s->config_size);
1629 
1630     s->blk = conf->conf.blk;
1631     s->rq = NULL;
1632     s->sector_mask = (s->conf.conf.logical_block_size / BDRV_SECTOR_SIZE) - 1;
1633 
1634     for (i = 0; i < conf->num_queues; i++) {
1635         virtio_add_queue(vdev, conf->queue_size, virtio_blk_handle_output);
1636     }
1637     qemu_coroutine_inc_pool_size(conf->num_queues * conf->queue_size / 2);
1638     virtio_blk_data_plane_create(vdev, conf, &s->dataplane, &err);
1639     if (err != NULL) {
1640         error_propagate(errp, err);
1641         for (i = 0; i < conf->num_queues; i++) {
1642             virtio_del_queue(vdev, i);
1643         }
1644         virtio_cleanup(vdev);
1645         return;
1646     }
1647 
1648     /*
1649      * This must be after virtio_init() so virtio_blk_dma_restart_cb() gets
1650      * called after ->start_ioeventfd() has already set blk's AioContext.
1651      */
1652     s->change =
1653         qdev_add_vm_change_state_handler(dev, virtio_blk_dma_restart_cb, s);
1654 
1655     blk_ram_registrar_init(&s->blk_ram_registrar, s->blk);
1656     blk_set_dev_ops(s->blk, &virtio_block_ops, s);
1657 
1658     blk_iostatus_enable(s->blk);
1659 
1660     add_boot_device_lchs(dev, "/disk@0,0",
1661                          conf->conf.lcyls,
1662                          conf->conf.lheads,
1663                          conf->conf.lsecs);
1664 }
1665 
1666 static void virtio_blk_device_unrealize(DeviceState *dev)
1667 {
1668     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1669     VirtIOBlock *s = VIRTIO_BLK(dev);
1670     VirtIOBlkConf *conf = &s->conf;
1671     unsigned i;
1672 
1673     blk_drain(s->blk);
1674     del_boot_device_lchs(dev, "/disk@0,0");
1675     virtio_blk_data_plane_destroy(s->dataplane);
1676     s->dataplane = NULL;
1677     for (i = 0; i < conf->num_queues; i++) {
1678         virtio_del_queue(vdev, i);
1679     }
1680     qemu_coroutine_dec_pool_size(conf->num_queues * conf->queue_size / 2);
1681     blk_ram_registrar_destroy(&s->blk_ram_registrar);
1682     qemu_del_vm_change_state_handler(s->change);
1683     blockdev_mark_auto_del(s->blk);
1684     virtio_cleanup(vdev);
1685 }
1686 
1687 static void virtio_blk_instance_init(Object *obj)
1688 {
1689     VirtIOBlock *s = VIRTIO_BLK(obj);
1690 
1691     device_add_bootindex_property(obj, &s->conf.conf.bootindex,
1692                                   "bootindex", "/disk@0,0",
1693                                   DEVICE(obj));
1694 }
1695 
1696 static const VMStateDescription vmstate_virtio_blk = {
1697     .name = "virtio-blk",
1698     .minimum_version_id = 2,
1699     .version_id = 2,
1700     .fields = (VMStateField[]) {
1701         VMSTATE_VIRTIO_DEVICE,
1702         VMSTATE_END_OF_LIST()
1703     },
1704 };
1705 
1706 static Property virtio_blk_properties[] = {
1707     DEFINE_BLOCK_PROPERTIES(VirtIOBlock, conf.conf),
1708     DEFINE_BLOCK_ERROR_PROPERTIES(VirtIOBlock, conf.conf),
1709     DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock, conf.conf),
1710     DEFINE_PROP_STRING("serial", VirtIOBlock, conf.serial),
1711     DEFINE_PROP_BIT64("config-wce", VirtIOBlock, host_features,
1712                       VIRTIO_BLK_F_CONFIG_WCE, true),
1713 #ifdef __linux__
1714     DEFINE_PROP_BIT64("scsi", VirtIOBlock, host_features,
1715                       VIRTIO_BLK_F_SCSI, false),
1716 #endif
1717     DEFINE_PROP_BIT("request-merging", VirtIOBlock, conf.request_merging, 0,
1718                     true),
1719     DEFINE_PROP_UINT16("num-queues", VirtIOBlock, conf.num_queues,
1720                        VIRTIO_BLK_AUTO_NUM_QUEUES),
1721     DEFINE_PROP_UINT16("queue-size", VirtIOBlock, conf.queue_size, 256),
1722     DEFINE_PROP_BOOL("seg-max-adjust", VirtIOBlock, conf.seg_max_adjust, true),
1723     DEFINE_PROP_LINK("iothread", VirtIOBlock, conf.iothread, TYPE_IOTHREAD,
1724                      IOThread *),
1725     DEFINE_PROP_BIT64("discard", VirtIOBlock, host_features,
1726                       VIRTIO_BLK_F_DISCARD, true),
1727     DEFINE_PROP_BOOL("report-discard-granularity", VirtIOBlock,
1728                      conf.report_discard_granularity, true),
1729     DEFINE_PROP_BIT64("write-zeroes", VirtIOBlock, host_features,
1730                       VIRTIO_BLK_F_WRITE_ZEROES, true),
1731     DEFINE_PROP_UINT32("max-discard-sectors", VirtIOBlock,
1732                        conf.max_discard_sectors, BDRV_REQUEST_MAX_SECTORS),
1733     DEFINE_PROP_UINT32("max-write-zeroes-sectors", VirtIOBlock,
1734                        conf.max_write_zeroes_sectors, BDRV_REQUEST_MAX_SECTORS),
1735     DEFINE_PROP_BOOL("x-enable-wce-if-config-wce", VirtIOBlock,
1736                      conf.x_enable_wce_if_config_wce, true),
1737     DEFINE_PROP_END_OF_LIST(),
1738 };
1739 
1740 static void virtio_blk_class_init(ObjectClass *klass, void *data)
1741 {
1742     DeviceClass *dc = DEVICE_CLASS(klass);
1743     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1744 
1745     device_class_set_props(dc, virtio_blk_properties);
1746     dc->vmsd = &vmstate_virtio_blk;
1747     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1748     vdc->realize = virtio_blk_device_realize;
1749     vdc->unrealize = virtio_blk_device_unrealize;
1750     vdc->get_config = virtio_blk_update_config;
1751     vdc->set_config = virtio_blk_set_config;
1752     vdc->get_features = virtio_blk_get_features;
1753     vdc->set_status = virtio_blk_set_status;
1754     vdc->reset = virtio_blk_reset;
1755     vdc->save = virtio_blk_save_device;
1756     vdc->load = virtio_blk_load_device;
1757     vdc->start_ioeventfd = virtio_blk_data_plane_start;
1758     vdc->stop_ioeventfd = virtio_blk_data_plane_stop;
1759 }
1760 
1761 static const TypeInfo virtio_blk_info = {
1762     .name = TYPE_VIRTIO_BLK,
1763     .parent = TYPE_VIRTIO_DEVICE,
1764     .instance_size = sizeof(VirtIOBlock),
1765     .instance_init = virtio_blk_instance_init,
1766     .class_init = virtio_blk_class_init,
1767 };
1768 
1769 static void virtio_register_types(void)
1770 {
1771     type_register_static(&virtio_blk_info);
1772 }
1773 
1774 type_init(virtio_register_types)
1775