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