4 * Copyright IBM, Corp. 2007
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "qemu/defer-call.h"
16 #include "qapi/error.h"
18 #include "qemu/module.h"
19 #include "qemu/error-report.h"
20 #include "qemu/main-loop.h"
21 #include "block/block_int.h"
23 #include "hw/block/block.h"
24 #include "hw/qdev-properties.h"
25 #include "sysemu/blockdev.h"
26 #include "sysemu/block-ram-registrar.h"
27 #include "sysemu/sysemu.h"
28 #include "sysemu/runstate.h"
29 #include "hw/virtio/virtio-blk.h"
30 #include "scsi/constants.h"
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"
40 static void virtio_blk_ioeventfd_attach(VirtIOBlock
*s
);
42 static void virtio_blk_init_request(VirtIOBlock
*s
, VirtQueue
*vq
,
53 static void virtio_blk_free_request(VirtIOBlockReq
*req
)
58 static void virtio_blk_req_complete(VirtIOBlockReq
*req
, unsigned char status
)
60 VirtIOBlock
*s
= req
->dev
;
61 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
63 trace_virtio_blk_req_complete(vdev
, req
, status
);
65 stb_p(&req
->in
->status
, status
);
66 iov_discard_undo(&req
->inhdr_undo
);
67 iov_discard_undo(&req
->outhdr_undo
);
68 virtqueue_push(req
->vq
, &req
->elem
, req
->in_len
);
69 if (qemu_in_iothread()) {
70 virtio_notify_irqfd(vdev
, req
->vq
);
72 virtio_notify(vdev
, req
->vq
);
76 static int virtio_blk_handle_rw_error(VirtIOBlockReq
*req
, int error
,
77 bool is_read
, bool acct_failed
)
79 VirtIOBlock
*s
= req
->dev
;
80 BlockErrorAction action
= blk_get_error_action(s
->blk
, is_read
, error
);
82 if (action
== BLOCK_ERROR_ACTION_STOP
) {
83 /* Break the link as the next request is going to be parsed from the
84 * ring again. Otherwise we may end up doing a double completion! */
87 WITH_QEMU_LOCK_GUARD(&s
->rq_lock
) {
91 } else if (action
== BLOCK_ERROR_ACTION_REPORT
) {
92 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
94 block_acct_failed(blk_get_stats(s
->blk
), &req
->acct
);
96 virtio_blk_free_request(req
);
99 blk_error_action(s
->blk
, action
, is_read
, error
);
100 return action
!= BLOCK_ERROR_ACTION_IGNORE
;
103 static void virtio_blk_rw_complete(void *opaque
, int ret
)
105 VirtIOBlockReq
*next
= opaque
;
106 VirtIOBlock
*s
= next
->dev
;
107 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
110 VirtIOBlockReq
*req
= next
;
112 trace_virtio_blk_rw_complete(vdev
, req
, ret
);
114 if (req
->qiov
.nalloc
!= -1) {
115 /* If nalloc is != -1 req->qiov is a local copy of the original
116 * external iovec. It was allocated in submit_requests to be
117 * able to merge requests. */
118 qemu_iovec_destroy(&req
->qiov
);
122 int p
= virtio_ldl_p(VIRTIO_DEVICE(s
), &req
->out
.type
);
123 bool is_read
= !(p
& VIRTIO_BLK_T_OUT
);
124 /* Note that memory may be dirtied on read failure. If the
125 * virtio request is not completed here, as is the case for
126 * BLOCK_ERROR_ACTION_STOP, the memory may not be copied
127 * correctly during live migration. While this is ugly,
128 * it is acceptable because the device is free to write to
129 * the memory until the request is completed (which will
130 * happen on the other side of the migration).
132 if (virtio_blk_handle_rw_error(req
, -ret
, is_read
, true)) {
137 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
138 block_acct_done(blk_get_stats(s
->blk
), &req
->acct
);
139 virtio_blk_free_request(req
);
143 static void virtio_blk_flush_complete(void *opaque
, int ret
)
145 VirtIOBlockReq
*req
= opaque
;
146 VirtIOBlock
*s
= req
->dev
;
148 if (ret
&& virtio_blk_handle_rw_error(req
, -ret
, 0, true)) {
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
);
157 static void virtio_blk_discard_write_zeroes_complete(void *opaque
, int ret
)
159 VirtIOBlockReq
*req
= opaque
;
160 VirtIOBlock
*s
= req
->dev
;
161 bool is_write_zeroes
= (virtio_ldl_p(VIRTIO_DEVICE(s
), &req
->out
.type
) &
162 ~VIRTIO_BLK_T_BARRIER
) == VIRTIO_BLK_T_WRITE_ZEROES
;
164 if (ret
&& virtio_blk_handle_rw_error(req
, -ret
, false, is_write_zeroes
)) {
168 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
169 if (is_write_zeroes
) {
170 block_acct_done(blk_get_stats(s
->blk
), &req
->acct
);
172 virtio_blk_free_request(req
);
179 struct sg_io_hdr hdr
;
180 } VirtIOBlockIoctlReq
;
182 static void virtio_blk_ioctl_complete(void *opaque
, int status
)
184 VirtIOBlockIoctlReq
*ioctl_req
= opaque
;
185 VirtIOBlockReq
*req
= ioctl_req
->req
;
186 VirtIOBlock
*s
= req
->dev
;
187 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
188 struct virtio_scsi_inhdr
*scsi
;
189 struct sg_io_hdr
*hdr
;
191 scsi
= (void *)req
->elem
.in_sg
[req
->elem
.in_num
- 2].iov_base
;
194 status
= VIRTIO_BLK_S_UNSUPP
;
195 virtio_stl_p(vdev
, &scsi
->errors
, 255);
199 hdr
= &ioctl_req
->hdr
;
201 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
202 * clear the masked_status field [hence status gets cleared too, see
203 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
204 * status has occurred. However they do set DRIVER_SENSE in driver_status
205 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
207 if (hdr
->status
== 0 && hdr
->sb_len_wr
> 0) {
208 hdr
->status
= CHECK_CONDITION
;
211 virtio_stl_p(vdev
, &scsi
->errors
,
212 hdr
->status
| (hdr
->msg_status
<< 8) |
213 (hdr
->host_status
<< 16) | (hdr
->driver_status
<< 24));
214 virtio_stl_p(vdev
, &scsi
->residual
, hdr
->resid
);
215 virtio_stl_p(vdev
, &scsi
->sense_len
, hdr
->sb_len_wr
);
216 virtio_stl_p(vdev
, &scsi
->data_len
, hdr
->dxfer_len
);
219 virtio_blk_req_complete(req
, status
);
220 virtio_blk_free_request(req
);
226 static VirtIOBlockReq
*virtio_blk_get_request(VirtIOBlock
*s
, VirtQueue
*vq
)
228 VirtIOBlockReq
*req
= virtqueue_pop(vq
, sizeof(VirtIOBlockReq
));
231 virtio_blk_init_request(s
, vq
, req
);
236 static int virtio_blk_handle_scsi_req(VirtIOBlockReq
*req
)
238 int status
= VIRTIO_BLK_S_OK
;
239 struct virtio_scsi_inhdr
*scsi
= NULL
;
240 VirtIOBlock
*blk
= req
->dev
;
241 VirtIODevice
*vdev
= VIRTIO_DEVICE(blk
);
242 VirtQueueElement
*elem
= &req
->elem
;
246 VirtIOBlockIoctlReq
*ioctl_req
;
251 * We require at least one output segment each for the virtio_blk_outhdr
252 * and the SCSI command block.
254 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
255 * and the sense buffer pointer in the input segments.
257 if (elem
->out_num
< 2 || elem
->in_num
< 3) {
258 status
= VIRTIO_BLK_S_IOERR
;
263 * The scsi inhdr is placed in the second-to-last input segment, just
264 * before the regular inhdr.
266 scsi
= (void *)elem
->in_sg
[elem
->in_num
- 2].iov_base
;
268 if (!virtio_has_feature(blk
->host_features
, VIRTIO_BLK_F_SCSI
)) {
269 status
= VIRTIO_BLK_S_UNSUPP
;
274 * No support for bidirection commands yet.
276 if (elem
->out_num
> 2 && elem
->in_num
> 3) {
277 status
= VIRTIO_BLK_S_UNSUPP
;
282 ioctl_req
= g_new0(VirtIOBlockIoctlReq
, 1);
283 ioctl_req
->req
= req
;
284 ioctl_req
->hdr
.interface_id
= 'S';
285 ioctl_req
->hdr
.cmd_len
= elem
->out_sg
[1].iov_len
;
286 ioctl_req
->hdr
.cmdp
= elem
->out_sg
[1].iov_base
;
287 ioctl_req
->hdr
.dxfer_len
= 0;
289 if (elem
->out_num
> 2) {
291 * If there are more than the minimally required 2 output segments
292 * there is write payload starting from the third iovec.
294 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
295 ioctl_req
->hdr
.iovec_count
= elem
->out_num
- 2;
297 for (i
= 0; i
< ioctl_req
->hdr
.iovec_count
; i
++) {
298 ioctl_req
->hdr
.dxfer_len
+= elem
->out_sg
[i
+ 2].iov_len
;
301 ioctl_req
->hdr
.dxferp
= elem
->out_sg
+ 2;
303 } else if (elem
->in_num
> 3) {
305 * If we have more than 3 input segments the guest wants to actually
308 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
309 ioctl_req
->hdr
.iovec_count
= elem
->in_num
- 3;
310 for (i
= 0; i
< ioctl_req
->hdr
.iovec_count
; i
++) {
311 ioctl_req
->hdr
.dxfer_len
+= elem
->in_sg
[i
].iov_len
;
314 ioctl_req
->hdr
.dxferp
= elem
->in_sg
;
317 * Some SCSI commands don't actually transfer any data.
319 ioctl_req
->hdr
.dxfer_direction
= SG_DXFER_NONE
;
322 ioctl_req
->hdr
.sbp
= elem
->in_sg
[elem
->in_num
- 3].iov_base
;
323 ioctl_req
->hdr
.mx_sb_len
= elem
->in_sg
[elem
->in_num
- 3].iov_len
;
325 acb
= blk_aio_ioctl(blk
->blk
, SG_IO
, &ioctl_req
->hdr
,
326 virtio_blk_ioctl_complete
, ioctl_req
);
329 status
= VIRTIO_BLK_S_UNSUPP
;
338 /* Just put anything nonzero so that the ioctl fails in the guest. */
340 virtio_stl_p(vdev
, &scsi
->errors
, 255);
345 static void virtio_blk_handle_scsi(VirtIOBlockReq
*req
)
349 status
= virtio_blk_handle_scsi_req(req
);
350 if (status
!= -EINPROGRESS
) {
351 virtio_blk_req_complete(req
, status
);
352 virtio_blk_free_request(req
);
356 static inline void submit_requests(VirtIOBlock
*s
, MultiReqBuffer
*mrb
,
357 int start
, int num_reqs
, int niov
)
359 BlockBackend
*blk
= s
->blk
;
360 QEMUIOVector
*qiov
= &mrb
->reqs
[start
]->qiov
;
361 int64_t sector_num
= mrb
->reqs
[start
]->sector_num
;
362 bool is_write
= mrb
->is_write
;
363 BdrvRequestFlags flags
= 0;
367 struct iovec
*tmp_iov
= qiov
->iov
;
368 int tmp_niov
= qiov
->niov
;
370 /* mrb->reqs[start]->qiov was initialized from external so we can't
371 * modify it here. We need to initialize it locally and then add the
372 * external iovecs. */
373 qemu_iovec_init(qiov
, niov
);
375 for (i
= 0; i
< tmp_niov
; i
++) {
376 qemu_iovec_add(qiov
, tmp_iov
[i
].iov_base
, tmp_iov
[i
].iov_len
);
379 for (i
= start
+ 1; i
< start
+ num_reqs
; i
++) {
380 qemu_iovec_concat(qiov
, &mrb
->reqs
[i
]->qiov
, 0,
381 mrb
->reqs
[i
]->qiov
.size
);
382 mrb
->reqs
[i
- 1]->mr_next
= mrb
->reqs
[i
];
385 trace_virtio_blk_submit_multireq(VIRTIO_DEVICE(mrb
->reqs
[start
]->dev
),
386 mrb
, start
, num_reqs
,
387 sector_num
<< BDRV_SECTOR_BITS
,
388 qiov
->size
, is_write
);
389 block_acct_merge_done(blk_get_stats(blk
),
390 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
,
394 if (blk_ram_registrar_ok(&s
->blk_ram_registrar
)) {
395 flags
|= BDRV_REQ_REGISTERED_BUF
;
399 blk_aio_pwritev(blk
, sector_num
<< BDRV_SECTOR_BITS
, qiov
,
400 flags
, virtio_blk_rw_complete
,
403 blk_aio_preadv(blk
, sector_num
<< BDRV_SECTOR_BITS
, qiov
,
404 flags
, virtio_blk_rw_complete
,
409 static int multireq_compare(const void *a
, const void *b
)
411 const VirtIOBlockReq
*req1
= *(VirtIOBlockReq
**)a
,
412 *req2
= *(VirtIOBlockReq
**)b
;
415 * Note that we can't simply subtract sector_num1 from sector_num2
416 * here as that could overflow the return value.
418 if (req1
->sector_num
> req2
->sector_num
) {
420 } else if (req1
->sector_num
< req2
->sector_num
) {
427 static void virtio_blk_submit_multireq(VirtIOBlock
*s
, MultiReqBuffer
*mrb
)
429 int i
= 0, start
= 0, num_reqs
= 0, niov
= 0, nb_sectors
= 0;
430 uint32_t max_transfer
;
431 int64_t sector_num
= 0;
433 if (mrb
->num_reqs
== 1) {
434 submit_requests(s
, mrb
, 0, 1, -1);
439 max_transfer
= blk_get_max_transfer(mrb
->reqs
[0]->dev
->blk
);
441 qsort(mrb
->reqs
, mrb
->num_reqs
, sizeof(*mrb
->reqs
),
444 for (i
= 0; i
< mrb
->num_reqs
; i
++) {
445 VirtIOBlockReq
*req
= mrb
->reqs
[i
];
448 * NOTE: We cannot merge the requests in below situations:
449 * 1. requests are not sequential
450 * 2. merge would exceed maximum number of IOVs
451 * 3. merge would exceed maximum transfer length of backend device
453 if (sector_num
+ nb_sectors
!= req
->sector_num
||
454 niov
> blk_get_max_iov(s
->blk
) - req
->qiov
.niov
||
455 req
->qiov
.size
> max_transfer
||
456 nb_sectors
> (max_transfer
-
457 req
->qiov
.size
) / BDRV_SECTOR_SIZE
) {
458 submit_requests(s
, mrb
, start
, num_reqs
, niov
);
464 sector_num
= req
->sector_num
;
465 nb_sectors
= niov
= 0;
469 nb_sectors
+= req
->qiov
.size
/ BDRV_SECTOR_SIZE
;
470 niov
+= req
->qiov
.niov
;
474 submit_requests(s
, mrb
, start
, num_reqs
, niov
);
478 static void virtio_blk_handle_flush(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
480 VirtIOBlock
*s
= req
->dev
;
482 block_acct_start(blk_get_stats(s
->blk
), &req
->acct
, 0,
486 * Make sure all outstanding writes are posted to the backing device.
488 if (mrb
->is_write
&& mrb
->num_reqs
> 0) {
489 virtio_blk_submit_multireq(s
, mrb
);
491 blk_aio_flush(s
->blk
, virtio_blk_flush_complete
, req
);
494 static bool virtio_blk_sect_range_ok(VirtIOBlock
*dev
,
495 uint64_t sector
, size_t size
)
497 uint64_t nb_sectors
= size
>> BDRV_SECTOR_BITS
;
498 uint64_t total_sectors
;
500 if (nb_sectors
> BDRV_REQUEST_MAX_SECTORS
) {
503 if (sector
& dev
->sector_mask
) {
506 if (size
% dev
->conf
.conf
.logical_block_size
) {
509 blk_get_geometry(dev
->blk
, &total_sectors
);
510 if (sector
> total_sectors
|| nb_sectors
> total_sectors
- sector
) {
516 static uint8_t virtio_blk_handle_discard_write_zeroes(VirtIOBlockReq
*req
,
517 struct virtio_blk_discard_write_zeroes
*dwz_hdr
, bool is_write_zeroes
)
519 VirtIOBlock
*s
= req
->dev
;
520 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
522 uint32_t num_sectors
, flags
, max_sectors
;
526 sector
= virtio_ldq_p(vdev
, &dwz_hdr
->sector
);
527 num_sectors
= virtio_ldl_p(vdev
, &dwz_hdr
->num_sectors
);
528 flags
= virtio_ldl_p(vdev
, &dwz_hdr
->flags
);
529 max_sectors
= is_write_zeroes
? s
->conf
.max_write_zeroes_sectors
:
530 s
->conf
.max_discard_sectors
;
533 * max_sectors is at most BDRV_REQUEST_MAX_SECTORS, this check
534 * make us sure that "num_sectors << BDRV_SECTOR_BITS" can fit in
535 * the integer variable.
537 if (unlikely(num_sectors
> max_sectors
)) {
538 err_status
= VIRTIO_BLK_S_IOERR
;
542 bytes
= num_sectors
<< BDRV_SECTOR_BITS
;
544 if (unlikely(!virtio_blk_sect_range_ok(s
, sector
, bytes
))) {
545 err_status
= VIRTIO_BLK_S_IOERR
;
550 * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for discard
551 * and write zeroes commands if any unknown flag is set.
553 if (unlikely(flags
& ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP
)) {
554 err_status
= VIRTIO_BLK_S_UNSUPP
;
558 if (is_write_zeroes
) { /* VIRTIO_BLK_T_WRITE_ZEROES */
559 int blk_aio_flags
= 0;
561 if (flags
& VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP
) {
562 blk_aio_flags
|= BDRV_REQ_MAY_UNMAP
;
565 block_acct_start(blk_get_stats(s
->blk
), &req
->acct
, bytes
,
568 blk_aio_pwrite_zeroes(s
->blk
, sector
<< BDRV_SECTOR_BITS
,
569 bytes
, blk_aio_flags
,
570 virtio_blk_discard_write_zeroes_complete
, req
);
571 } else { /* VIRTIO_BLK_T_DISCARD */
573 * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for
574 * discard commands if the unmap flag is set.
576 if (unlikely(flags
& VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP
)) {
577 err_status
= VIRTIO_BLK_S_UNSUPP
;
581 blk_aio_pdiscard(s
->blk
, sector
<< BDRV_SECTOR_BITS
, bytes
,
582 virtio_blk_discard_write_zeroes_complete
, req
);
585 return VIRTIO_BLK_S_OK
;
588 if (is_write_zeroes
) {
589 block_acct_invalid(blk_get_stats(s
->blk
), BLOCK_ACCT_WRITE
);
594 typedef struct ZoneCmdData
{
596 struct iovec
*in_iov
;
600 unsigned int nr_zones
;
601 BlockZoneDescriptor
*zones
;
610 * check zoned_request: error checking before issuing requests. If all checks
611 * passed, return true.
612 * append: true if only zone append requests issued.
614 static bool check_zoned_request(VirtIOBlock
*s
, int64_t offset
, int64_t len
,
615 bool append
, uint8_t *status
) {
616 BlockDriverState
*bs
= blk_bs(s
->blk
);
619 if (!virtio_has_feature(s
->host_features
, VIRTIO_BLK_F_ZONED
)) {
620 *status
= VIRTIO_BLK_S_UNSUPP
;
624 if (offset
< 0 || len
< 0 || len
> (bs
->total_sectors
<< BDRV_SECTOR_BITS
)
625 || offset
> (bs
->total_sectors
<< BDRV_SECTOR_BITS
) - len
) {
626 *status
= VIRTIO_BLK_S_ZONE_INVALID_CMD
;
631 if (bs
->bl
.write_granularity
) {
632 if ((offset
% bs
->bl
.write_granularity
) != 0) {
633 *status
= VIRTIO_BLK_S_ZONE_UNALIGNED_WP
;
638 index
= offset
/ bs
->bl
.zone_size
;
639 if (BDRV_ZT_IS_CONV(bs
->wps
->wp
[index
])) {
640 *status
= VIRTIO_BLK_S_ZONE_INVALID_CMD
;
644 if (len
/ 512 > bs
->bl
.max_append_sectors
) {
645 if (bs
->bl
.max_append_sectors
== 0) {
646 *status
= VIRTIO_BLK_S_UNSUPP
;
648 *status
= VIRTIO_BLK_S_ZONE_INVALID_CMD
;
656 static void virtio_blk_zone_report_complete(void *opaque
, int ret
)
658 ZoneCmdData
*data
= opaque
;
659 VirtIOBlockReq
*req
= data
->req
;
660 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
661 struct iovec
*in_iov
= data
->in_iov
;
662 unsigned in_num
= data
->in_num
;
663 int64_t zrp_size
, n
, j
= 0;
664 int64_t nz
= data
->zone_report_data
.nr_zones
;
665 int8_t err_status
= VIRTIO_BLK_S_OK
;
666 struct virtio_blk_zone_report zrp_hdr
= (struct virtio_blk_zone_report
) {
667 .nr_zones
= cpu_to_le64(nz
),
670 trace_virtio_blk_zone_report_complete(vdev
, req
, nz
, ret
);
672 err_status
= VIRTIO_BLK_S_ZONE_INVALID_CMD
;
676 zrp_size
= sizeof(struct virtio_blk_zone_report
)
677 + sizeof(struct virtio_blk_zone_descriptor
) * nz
;
678 n
= iov_from_buf(in_iov
, in_num
, 0, &zrp_hdr
, sizeof(zrp_hdr
));
679 if (n
!= sizeof(zrp_hdr
)) {
680 virtio_error(vdev
, "Driver provided input buffer that is too small!");
681 err_status
= VIRTIO_BLK_S_ZONE_INVALID_CMD
;
685 for (size_t i
= sizeof(zrp_hdr
); i
< zrp_size
;
686 i
+= sizeof(struct virtio_blk_zone_descriptor
), ++j
) {
687 struct virtio_blk_zone_descriptor desc
=
688 (struct virtio_blk_zone_descriptor
) {
689 .z_start
= cpu_to_le64(data
->zone_report_data
.zones
[j
].start
690 >> BDRV_SECTOR_BITS
),
691 .z_cap
= cpu_to_le64(data
->zone_report_data
.zones
[j
].cap
692 >> BDRV_SECTOR_BITS
),
693 .z_wp
= cpu_to_le64(data
->zone_report_data
.zones
[j
].wp
694 >> BDRV_SECTOR_BITS
),
697 switch (data
->zone_report_data
.zones
[j
].type
) {
699 desc
.z_type
= VIRTIO_BLK_ZT_CONV
;
702 desc
.z_type
= VIRTIO_BLK_ZT_SWR
;
705 desc
.z_type
= VIRTIO_BLK_ZT_SWP
;
708 g_assert_not_reached();
711 switch (data
->zone_report_data
.zones
[j
].state
) {
713 desc
.z_state
= VIRTIO_BLK_ZS_RDONLY
;
716 desc
.z_state
= VIRTIO_BLK_ZS_OFFLINE
;
719 desc
.z_state
= VIRTIO_BLK_ZS_EMPTY
;
722 desc
.z_state
= VIRTIO_BLK_ZS_CLOSED
;
725 desc
.z_state
= VIRTIO_BLK_ZS_FULL
;
728 desc
.z_state
= VIRTIO_BLK_ZS_EOPEN
;
731 desc
.z_state
= VIRTIO_BLK_ZS_IOPEN
;
734 desc
.z_state
= VIRTIO_BLK_ZS_NOT_WP
;
737 g_assert_not_reached();
740 /* TODO: it takes O(n^2) time complexity. Optimizations required. */
741 n
= iov_from_buf(in_iov
, in_num
, i
, &desc
, sizeof(desc
));
742 if (n
!= sizeof(desc
)) {
743 virtio_error(vdev
, "Driver provided input buffer "
744 "for descriptors that is too small!");
745 err_status
= VIRTIO_BLK_S_ZONE_INVALID_CMD
;
750 virtio_blk_req_complete(req
, err_status
);
751 virtio_blk_free_request(req
);
752 g_free(data
->zone_report_data
.zones
);
756 static void virtio_blk_handle_zone_report(VirtIOBlockReq
*req
,
757 struct iovec
*in_iov
,
760 VirtIOBlock
*s
= req
->dev
;
761 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
762 unsigned int nr_zones
;
764 int64_t zone_size
, offset
;
767 if (req
->in_len
< sizeof(struct virtio_blk_inhdr
) +
768 sizeof(struct virtio_blk_zone_report
) +
769 sizeof(struct virtio_blk_zone_descriptor
)) {
770 virtio_error(vdev
, "in buffer too small for zone report");
774 /* start byte offset of the zone report */
775 offset
= virtio_ldq_p(vdev
, &req
->out
.sector
) << BDRV_SECTOR_BITS
;
776 if (!check_zoned_request(s
, offset
, 0, false, &err_status
)) {
779 nr_zones
= (req
->in_len
- sizeof(struct virtio_blk_inhdr
) -
780 sizeof(struct virtio_blk_zone_report
)) /
781 sizeof(struct virtio_blk_zone_descriptor
);
782 trace_virtio_blk_handle_zone_report(vdev
, req
,
783 offset
>> BDRV_SECTOR_BITS
, nr_zones
);
785 zone_size
= sizeof(BlockZoneDescriptor
) * nr_zones
;
786 data
= g_malloc(sizeof(ZoneCmdData
));
788 data
->in_iov
= in_iov
;
789 data
->in_num
= in_num
;
790 data
->zone_report_data
.nr_zones
= nr_zones
;
791 data
->zone_report_data
.zones
= g_malloc(zone_size
),
793 blk_aio_zone_report(s
->blk
, offset
, &data
->zone_report_data
.nr_zones
,
794 data
->zone_report_data
.zones
,
795 virtio_blk_zone_report_complete
, data
);
798 virtio_blk_req_complete(req
, err_status
);
799 virtio_blk_free_request(req
);
802 static void virtio_blk_zone_mgmt_complete(void *opaque
, int ret
)
804 VirtIOBlockReq
*req
= opaque
;
805 VirtIOBlock
*s
= req
->dev
;
806 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
807 int8_t err_status
= VIRTIO_BLK_S_OK
;
808 trace_virtio_blk_zone_mgmt_complete(vdev
, req
,ret
);
811 err_status
= VIRTIO_BLK_S_ZONE_INVALID_CMD
;
814 virtio_blk_req_complete(req
, err_status
);
815 virtio_blk_free_request(req
);
818 static int virtio_blk_handle_zone_mgmt(VirtIOBlockReq
*req
, BlockZoneOp op
)
820 VirtIOBlock
*s
= req
->dev
;
821 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
822 BlockDriverState
*bs
= blk_bs(s
->blk
);
823 int64_t offset
= virtio_ldq_p(vdev
, &req
->out
.sector
) << BDRV_SECTOR_BITS
;
825 uint64_t capacity
= bs
->total_sectors
<< BDRV_SECTOR_BITS
;
826 uint8_t err_status
= VIRTIO_BLK_S_OK
;
828 uint32_t type
= virtio_ldl_p(vdev
, &req
->out
.type
);
829 if (type
== VIRTIO_BLK_T_ZONE_RESET_ALL
) {
830 /* Entire drive capacity */
833 trace_virtio_blk_handle_zone_reset_all(vdev
, req
, 0,
836 if (bs
->bl
.zone_size
> capacity
- offset
) {
837 /* The zoned device allows the last smaller zone. */
838 len
= capacity
- bs
->bl
.zone_size
* (bs
->bl
.nr_zones
- 1);
840 len
= bs
->bl
.zone_size
;
842 trace_virtio_blk_handle_zone_mgmt(vdev
, req
, op
,
843 offset
>> BDRV_SECTOR_BITS
,
844 len
>> BDRV_SECTOR_BITS
);
847 if (!check_zoned_request(s
, offset
, len
, false, &err_status
)) {
851 blk_aio_zone_mgmt(s
->blk
, op
, offset
, len
,
852 virtio_blk_zone_mgmt_complete
, req
);
856 virtio_blk_req_complete(req
, err_status
);
857 virtio_blk_free_request(req
);
861 static void virtio_blk_zone_append_complete(void *opaque
, int ret
)
863 ZoneCmdData
*data
= opaque
;
864 VirtIOBlockReq
*req
= data
->req
;
865 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
866 int64_t append_sector
, n
;
867 uint8_t err_status
= VIRTIO_BLK_S_OK
;
870 err_status
= VIRTIO_BLK_S_ZONE_INVALID_CMD
;
874 virtio_stq_p(vdev
, &append_sector
,
875 data
->zone_append_data
.offset
>> BDRV_SECTOR_BITS
);
876 n
= iov_from_buf(data
->in_iov
, data
->in_num
, 0, &append_sector
,
877 sizeof(append_sector
));
878 if (n
!= sizeof(append_sector
)) {
879 virtio_error(vdev
, "Driver provided input buffer less than size of "
881 err_status
= VIRTIO_BLK_S_ZONE_INVALID_CMD
;
884 trace_virtio_blk_zone_append_complete(vdev
, req
, append_sector
, ret
);
887 virtio_blk_req_complete(req
, err_status
);
888 virtio_blk_free_request(req
);
892 static int virtio_blk_handle_zone_append(VirtIOBlockReq
*req
,
893 struct iovec
*out_iov
,
894 struct iovec
*in_iov
,
897 VirtIOBlock
*s
= req
->dev
;
898 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
899 uint8_t err_status
= VIRTIO_BLK_S_OK
;
901 int64_t offset
= virtio_ldq_p(vdev
, &req
->out
.sector
) << BDRV_SECTOR_BITS
;
902 int64_t len
= iov_size(out_iov
, out_num
);
905 trace_virtio_blk_handle_zone_append(vdev
, req
, offset
>> BDRV_SECTOR_BITS
);
906 if (!check_zoned_request(s
, offset
, len
, true, &err_status
)) {
910 data
= g_malloc(sizeof(ZoneCmdData
));
912 data
->in_iov
= in_iov
;
913 data
->in_num
= in_num
;
914 data
->zone_append_data
.offset
= offset
;
915 qemu_iovec_init_external(&req
->qiov
, out_iov
, out_num
);
917 block_acct_start(blk_get_stats(s
->blk
), &req
->acct
, len
,
918 BLOCK_ACCT_ZONE_APPEND
);
920 blk_aio_zone_append(s
->blk
, &data
->zone_append_data
.offset
, &req
->qiov
, 0,
921 virtio_blk_zone_append_complete
, data
);
925 virtio_blk_req_complete(req
, err_status
);
926 virtio_blk_free_request(req
);
930 static int virtio_blk_handle_request(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
)
933 struct iovec
*in_iov
= req
->elem
.in_sg
;
934 struct iovec
*out_iov
= req
->elem
.out_sg
;
935 unsigned in_num
= req
->elem
.in_num
;
936 unsigned out_num
= req
->elem
.out_num
;
937 VirtIOBlock
*s
= req
->dev
;
938 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
940 if (req
->elem
.out_num
< 1 || req
->elem
.in_num
< 1) {
941 virtio_error(vdev
, "virtio-blk missing headers");
945 if (unlikely(iov_to_buf(out_iov
, out_num
, 0, &req
->out
,
946 sizeof(req
->out
)) != sizeof(req
->out
))) {
947 virtio_error(vdev
, "virtio-blk request outhdr too short");
951 iov_discard_front_undoable(&out_iov
, &out_num
, sizeof(req
->out
),
954 if (in_iov
[in_num
- 1].iov_len
< sizeof(struct virtio_blk_inhdr
)) {
955 virtio_error(vdev
, "virtio-blk request inhdr too short");
956 iov_discard_undo(&req
->outhdr_undo
);
960 /* We always touch the last byte, so just see how big in_iov is. */
961 req
->in_len
= iov_size(in_iov
, in_num
);
962 req
->in
= (void *)in_iov
[in_num
- 1].iov_base
963 + in_iov
[in_num
- 1].iov_len
964 - sizeof(struct virtio_blk_inhdr
);
965 iov_discard_back_undoable(in_iov
, &in_num
, sizeof(struct virtio_blk_inhdr
),
968 type
= virtio_ldl_p(vdev
, &req
->out
.type
);
970 /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER
971 * is an optional flag. Although a guest should not send this flag if
972 * not negotiated we ignored it in the past. So keep ignoring it. */
973 switch (type
& ~(VIRTIO_BLK_T_OUT
| VIRTIO_BLK_T_BARRIER
)) {
974 case VIRTIO_BLK_T_IN
:
976 bool is_write
= type
& VIRTIO_BLK_T_OUT
;
977 req
->sector_num
= virtio_ldq_p(vdev
, &req
->out
.sector
);
980 qemu_iovec_init_external(&req
->qiov
, out_iov
, out_num
);
981 trace_virtio_blk_handle_write(vdev
, req
, req
->sector_num
,
982 req
->qiov
.size
/ BDRV_SECTOR_SIZE
);
984 qemu_iovec_init_external(&req
->qiov
, in_iov
, in_num
);
985 trace_virtio_blk_handle_read(vdev
, req
, req
->sector_num
,
986 req
->qiov
.size
/ BDRV_SECTOR_SIZE
);
989 if (!virtio_blk_sect_range_ok(s
, req
->sector_num
, req
->qiov
.size
)) {
990 virtio_blk_req_complete(req
, VIRTIO_BLK_S_IOERR
);
991 block_acct_invalid(blk_get_stats(s
->blk
),
992 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
);
993 virtio_blk_free_request(req
);
997 block_acct_start(blk_get_stats(s
->blk
), &req
->acct
, req
->qiov
.size
,
998 is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
);
1000 /* merge would exceed maximum number of requests or IO direction
1002 if (mrb
->num_reqs
> 0 && (mrb
->num_reqs
== VIRTIO_BLK_MAX_MERGE_REQS
||
1003 is_write
!= mrb
->is_write
||
1004 !s
->conf
.request_merging
)) {
1005 virtio_blk_submit_multireq(s
, mrb
);
1008 assert(mrb
->num_reqs
< VIRTIO_BLK_MAX_MERGE_REQS
);
1009 mrb
->reqs
[mrb
->num_reqs
++] = req
;
1010 mrb
->is_write
= is_write
;
1013 case VIRTIO_BLK_T_FLUSH
:
1014 virtio_blk_handle_flush(req
, mrb
);
1016 case VIRTIO_BLK_T_ZONE_REPORT
:
1017 virtio_blk_handle_zone_report(req
, in_iov
, in_num
);
1019 case VIRTIO_BLK_T_ZONE_OPEN
:
1020 virtio_blk_handle_zone_mgmt(req
, BLK_ZO_OPEN
);
1022 case VIRTIO_BLK_T_ZONE_CLOSE
:
1023 virtio_blk_handle_zone_mgmt(req
, BLK_ZO_CLOSE
);
1025 case VIRTIO_BLK_T_ZONE_FINISH
:
1026 virtio_blk_handle_zone_mgmt(req
, BLK_ZO_FINISH
);
1028 case VIRTIO_BLK_T_ZONE_RESET
:
1029 virtio_blk_handle_zone_mgmt(req
, BLK_ZO_RESET
);
1031 case VIRTIO_BLK_T_ZONE_RESET_ALL
:
1032 virtio_blk_handle_zone_mgmt(req
, BLK_ZO_RESET
);
1034 case VIRTIO_BLK_T_SCSI_CMD
:
1035 virtio_blk_handle_scsi(req
);
1037 case VIRTIO_BLK_T_GET_ID
:
1040 * NB: per existing s/n string convention the string is
1041 * terminated by '\0' only when shorter than buffer.
1043 const char *serial
= s
->conf
.serial
? s
->conf
.serial
: "";
1044 size_t size
= MIN(strlen(serial
) + 1,
1045 MIN(iov_size(in_iov
, in_num
),
1046 VIRTIO_BLK_ID_BYTES
));
1047 iov_from_buf(in_iov
, in_num
, 0, serial
, size
);
1048 virtio_blk_req_complete(req
, VIRTIO_BLK_S_OK
);
1049 virtio_blk_free_request(req
);
1052 case VIRTIO_BLK_T_ZONE_APPEND
& ~VIRTIO_BLK_T_OUT
:
1054 * Passing out_iov/out_num and in_iov/in_num is not safe
1055 * to access req->elem.out_sg directly because it may be
1056 * modified by virtio_blk_handle_request().
1058 virtio_blk_handle_zone_append(req
, out_iov
, in_iov
, out_num
, in_num
);
1061 * VIRTIO_BLK_T_DISCARD and VIRTIO_BLK_T_WRITE_ZEROES are defined with
1062 * VIRTIO_BLK_T_OUT flag set. We masked this flag in the switch statement,
1063 * so we must mask it for these requests, then we will check if it is set.
1065 case VIRTIO_BLK_T_DISCARD
& ~VIRTIO_BLK_T_OUT
:
1066 case VIRTIO_BLK_T_WRITE_ZEROES
& ~VIRTIO_BLK_T_OUT
:
1068 struct virtio_blk_discard_write_zeroes dwz_hdr
;
1069 size_t out_len
= iov_size(out_iov
, out_num
);
1070 bool is_write_zeroes
= (type
& ~VIRTIO_BLK_T_BARRIER
) ==
1071 VIRTIO_BLK_T_WRITE_ZEROES
;
1075 * Unsupported if VIRTIO_BLK_T_OUT is not set or the request contains
1076 * more than one segment.
1078 if (unlikely(!(type
& VIRTIO_BLK_T_OUT
) ||
1079 out_len
> sizeof(dwz_hdr
))) {
1080 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
1081 virtio_blk_free_request(req
);
1085 if (unlikely(iov_to_buf(out_iov
, out_num
, 0, &dwz_hdr
,
1086 sizeof(dwz_hdr
)) != sizeof(dwz_hdr
))) {
1087 iov_discard_undo(&req
->inhdr_undo
);
1088 iov_discard_undo(&req
->outhdr_undo
);
1089 virtio_error(vdev
, "virtio-blk discard/write_zeroes header"
1094 err_status
= virtio_blk_handle_discard_write_zeroes(req
, &dwz_hdr
,
1096 if (err_status
!= VIRTIO_BLK_S_OK
) {
1097 virtio_blk_req_complete(req
, err_status
);
1098 virtio_blk_free_request(req
);
1104 virtio_blk_req_complete(req
, VIRTIO_BLK_S_UNSUPP
);
1105 virtio_blk_free_request(req
);
1110 void virtio_blk_handle_vq(VirtIOBlock
*s
, VirtQueue
*vq
)
1112 VirtIOBlockReq
*req
;
1113 MultiReqBuffer mrb
= {};
1114 bool suppress_notifications
= virtio_queue_get_notification(vq
);
1119 if (suppress_notifications
) {
1120 virtio_queue_set_notification(vq
, 0);
1123 while ((req
= virtio_blk_get_request(s
, vq
))) {
1124 if (virtio_blk_handle_request(req
, &mrb
)) {
1125 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
1126 virtio_blk_free_request(req
);
1131 if (suppress_notifications
) {
1132 virtio_queue_set_notification(vq
, 1);
1134 } while (!virtio_queue_empty(vq
));
1137 virtio_blk_submit_multireq(s
, &mrb
);
1143 static void virtio_blk_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
1145 VirtIOBlock
*s
= (VirtIOBlock
*)vdev
;
1147 if (!s
->ioeventfd_disabled
&& !s
->ioeventfd_started
) {
1148 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
1149 * ioeventfd here instead of waiting for .set_status().
1151 virtio_device_start_ioeventfd(vdev
);
1152 if (!s
->ioeventfd_disabled
) {
1157 virtio_blk_handle_vq(s
, vq
);
1160 static void virtio_blk_dma_restart_bh(void *opaque
)
1162 VirtIOBlockReq
*req
= opaque
;
1163 VirtIOBlock
*s
= req
->dev
; /* we're called with at least one request */
1165 MultiReqBuffer mrb
= {};
1168 VirtIOBlockReq
*next
= req
->next
;
1169 if (virtio_blk_handle_request(req
, &mrb
)) {
1170 /* Device is now broken and won't do any processing until it gets
1171 * reset. Already queued requests will be lost: let's purge them.
1175 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
1176 virtio_blk_free_request(req
);
1185 virtio_blk_submit_multireq(s
, &mrb
);
1188 /* Paired with inc in virtio_blk_dma_restart_cb() */
1189 blk_dec_in_flight(s
->conf
.conf
.blk
);
1192 static void virtio_blk_dma_restart_cb(void *opaque
, bool running
,
1195 VirtIOBlock
*s
= opaque
;
1196 uint16_t num_queues
= s
->conf
.num_queues
;
1197 g_autofree VirtIOBlockReq
**vq_rq
= NULL
;
1204 /* Split the device-wide s->rq request list into per-vq request lists */
1205 vq_rq
= g_new0(VirtIOBlockReq
*, num_queues
);
1207 WITH_QEMU_LOCK_GUARD(&s
->rq_lock
) {
1213 VirtIOBlockReq
*next
= rq
->next
;
1214 uint16_t idx
= virtio_get_queue_index(rq
->vq
);
1216 /* Only num_queues vqs were created so vq_rq[idx] is within bounds */
1217 assert(idx
< num_queues
);
1218 rq
->next
= vq_rq
[idx
];
1223 /* Schedule a BH to submit the requests in each vq's AioContext */
1224 for (uint16_t i
= 0; i
< num_queues
; i
++) {
1229 /* Paired with dec in virtio_blk_dma_restart_bh() */
1230 blk_inc_in_flight(s
->conf
.conf
.blk
);
1232 aio_bh_schedule_oneshot(s
->vq_aio_context
[i
],
1233 virtio_blk_dma_restart_bh
,
1238 static void virtio_blk_reset(VirtIODevice
*vdev
)
1240 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
1241 VirtIOBlockReq
*req
;
1243 /* Dataplane has stopped... */
1244 assert(!s
->ioeventfd_started
);
1246 /* ...but requests may still be in flight. */
1249 /* We drop queued requests after blk_drain() because blk_drain() itself can
1251 WITH_QEMU_LOCK_GUARD(&s
->rq_lock
) {
1256 /* No other threads can access req->vq here */
1257 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
1259 virtio_blk_free_request(req
);
1263 blk_set_enable_write_cache(s
->blk
, s
->original_wce
);
1266 /* coalesce internal state, copy to pci i/o region 0
1268 static void virtio_blk_update_config(VirtIODevice
*vdev
, uint8_t *config
)
1270 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
1271 BlockConf
*conf
= &s
->conf
.conf
;
1272 BlockDriverState
*bs
= blk_bs(s
->blk
);
1273 struct virtio_blk_config blkcfg
;
1276 int blk_size
= conf
->logical_block_size
;
1278 blk_get_geometry(s
->blk
, &capacity
);
1279 memset(&blkcfg
, 0, sizeof(blkcfg
));
1280 virtio_stq_p(vdev
, &blkcfg
.capacity
, capacity
);
1281 virtio_stl_p(vdev
, &blkcfg
.seg_max
,
1282 s
->conf
.seg_max_adjust
? s
->conf
.queue_size
- 2 : 128 - 2);
1283 virtio_stw_p(vdev
, &blkcfg
.geometry
.cylinders
, conf
->cyls
);
1284 virtio_stl_p(vdev
, &blkcfg
.blk_size
, blk_size
);
1285 virtio_stw_p(vdev
, &blkcfg
.min_io_size
, conf
->min_io_size
/ blk_size
);
1286 virtio_stl_p(vdev
, &blkcfg
.opt_io_size
, conf
->opt_io_size
/ blk_size
);
1287 blkcfg
.geometry
.heads
= conf
->heads
;
1289 * We must ensure that the block device capacity is a multiple of
1290 * the logical block size. If that is not the case, let's use
1291 * sector_mask to adopt the geometry to have a correct picture.
1292 * For those devices where the capacity is ok for the given geometry
1293 * we don't touch the sector value of the geometry, since some devices
1294 * (like s390 dasd) need a specific value. Here the capacity is already
1295 * cyls*heads*secs*blk_size and the sector value is not block size
1296 * divided by 512 - instead it is the amount of blk_size blocks
1297 * per track (cylinder).
1299 length
= blk_getlength(s
->blk
);
1300 if (length
> 0 && length
/ conf
->heads
/ conf
->secs
% blk_size
) {
1301 blkcfg
.geometry
.sectors
= conf
->secs
& ~s
->sector_mask
;
1303 blkcfg
.geometry
.sectors
= conf
->secs
;
1305 blkcfg
.size_max
= 0;
1306 blkcfg
.physical_block_exp
= get_physical_block_exp(conf
);
1307 blkcfg
.alignment_offset
= 0;
1308 blkcfg
.wce
= blk_enable_write_cache(s
->blk
);
1309 virtio_stw_p(vdev
, &blkcfg
.num_queues
, s
->conf
.num_queues
);
1310 if (virtio_has_feature(s
->host_features
, VIRTIO_BLK_F_DISCARD
)) {
1311 uint32_t discard_granularity
= conf
->discard_granularity
;
1312 if (discard_granularity
== -1 || !s
->conf
.report_discard_granularity
) {
1313 discard_granularity
= blk_size
;
1315 virtio_stl_p(vdev
, &blkcfg
.max_discard_sectors
,
1316 s
->conf
.max_discard_sectors
);
1317 virtio_stl_p(vdev
, &blkcfg
.discard_sector_alignment
,
1318 discard_granularity
>> BDRV_SECTOR_BITS
);
1320 * We support only one segment per request since multiple segments
1321 * are not widely used and there are no userspace APIs that allow
1322 * applications to submit multiple segments in a single call.
1324 virtio_stl_p(vdev
, &blkcfg
.max_discard_seg
, 1);
1326 if (virtio_has_feature(s
->host_features
, VIRTIO_BLK_F_WRITE_ZEROES
)) {
1327 virtio_stl_p(vdev
, &blkcfg
.max_write_zeroes_sectors
,
1328 s
->conf
.max_write_zeroes_sectors
);
1329 blkcfg
.write_zeroes_may_unmap
= 1;
1330 virtio_stl_p(vdev
, &blkcfg
.max_write_zeroes_seg
, 1);
1332 if (bs
->bl
.zoned
!= BLK_Z_NONE
) {
1333 switch (bs
->bl
.zoned
) {
1335 blkcfg
.zoned
.model
= VIRTIO_BLK_Z_HM
;
1338 blkcfg
.zoned
.model
= VIRTIO_BLK_Z_HA
;
1341 g_assert_not_reached();
1344 virtio_stl_p(vdev
, &blkcfg
.zoned
.zone_sectors
,
1345 bs
->bl
.zone_size
/ 512);
1346 virtio_stl_p(vdev
, &blkcfg
.zoned
.max_active_zones
,
1347 bs
->bl
.max_active_zones
);
1348 virtio_stl_p(vdev
, &blkcfg
.zoned
.max_open_zones
,
1349 bs
->bl
.max_open_zones
);
1350 virtio_stl_p(vdev
, &blkcfg
.zoned
.write_granularity
, blk_size
);
1351 virtio_stl_p(vdev
, &blkcfg
.zoned
.max_append_sectors
,
1352 bs
->bl
.max_append_sectors
);
1354 blkcfg
.zoned
.model
= VIRTIO_BLK_Z_NONE
;
1356 memcpy(config
, &blkcfg
, s
->config_size
);
1359 static void virtio_blk_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
1361 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
1362 struct virtio_blk_config blkcfg
;
1364 memcpy(&blkcfg
, config
, s
->config_size
);
1366 blk_set_enable_write_cache(s
->blk
, blkcfg
.wce
!= 0);
1369 static uint64_t virtio_blk_get_features(VirtIODevice
*vdev
, uint64_t features
,
1372 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
1374 /* Firstly sync all virtio-blk possible supported features */
1375 features
|= s
->host_features
;
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");
1387 virtio_clear_feature(&features
, VIRTIO_F_ANY_LAYOUT
);
1388 virtio_add_feature(&features
, VIRTIO_BLK_F_SCSI
);
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
);
1396 if (!blk_is_writable(s
->blk
)) {
1397 virtio_add_feature(&features
, VIRTIO_BLK_F_RO
);
1399 if (s
->conf
.num_queues
> 1) {
1400 virtio_add_feature(&features
, VIRTIO_BLK_F_MQ
);
1406 static void virtio_blk_set_status(VirtIODevice
*vdev
, uint8_t status
)
1408 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
1410 if (!(status
& (VIRTIO_CONFIG_S_DRIVER
| VIRTIO_CONFIG_S_DRIVER_OK
))) {
1411 assert(!s
->ioeventfd_started
);
1414 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
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:
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
1431 * s->blk would erroneously be placed in writethrough mode.
1433 if (!virtio_vdev_has_feature(vdev
, VIRTIO_BLK_F_CONFIG_WCE
)) {
1434 blk_set_enable_write_cache(s
->blk
,
1435 virtio_vdev_has_feature(vdev
,
1440 static void virtio_blk_save_device(VirtIODevice
*vdev
, QEMUFile
*f
)
1442 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
1444 WITH_QEMU_LOCK_GUARD(&s
->rq_lock
) {
1445 VirtIOBlockReq
*req
= s
->rq
;
1448 qemu_put_sbyte(f
, 1);
1450 if (s
->conf
.num_queues
> 1) {
1451 qemu_put_be32(f
, virtio_get_queue_index(req
->vq
));
1454 qemu_put_virtqueue_element(vdev
, f
, &req
->elem
);
1459 qemu_put_sbyte(f
, 0);
1462 static int virtio_blk_load_device(VirtIODevice
*vdev
, QEMUFile
*f
,
1465 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
1467 while (qemu_get_sbyte(f
)) {
1468 unsigned nvqs
= s
->conf
.num_queues
;
1469 unsigned vq_idx
= 0;
1470 VirtIOBlockReq
*req
;
1473 vq_idx
= qemu_get_be32(f
);
1475 if (vq_idx
>= nvqs
) {
1476 error_report("Invalid virtqueue index in request list: %#x",
1482 req
= qemu_get_virtqueue_element(vdev
, f
, sizeof(VirtIOBlockReq
));
1483 virtio_blk_init_request(s
, virtio_get_queue(vdev
, vq_idx
), req
);
1485 WITH_QEMU_LOCK_GUARD(&s
->rq_lock
) {
1494 static void virtio_resize_cb(void *opaque
)
1496 VirtIODevice
*vdev
= opaque
;
1498 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1499 virtio_notify_config(vdev
);
1502 static void virtio_blk_resize(void *opaque
)
1504 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
1507 * virtio_notify_config() needs to acquire the BQL,
1508 * so it can't be called from an iothread. Instead, schedule
1509 * it to be run in the main context BH.
1511 aio_bh_schedule_oneshot(qemu_get_aio_context(), virtio_resize_cb
, vdev
);
1514 static void virtio_blk_ioeventfd_detach(VirtIOBlock
*s
)
1516 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
1518 for (uint16_t i
= 0; i
< s
->conf
.num_queues
; i
++) {
1519 VirtQueue
*vq
= virtio_get_queue(vdev
, i
);
1520 virtio_queue_aio_detach_host_notifier(vq
, s
->vq_aio_context
[i
]);
1524 static void virtio_blk_ioeventfd_attach(VirtIOBlock
*s
)
1526 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
1528 for (uint16_t i
= 0; i
< s
->conf
.num_queues
; i
++) {
1529 VirtQueue
*vq
= virtio_get_queue(vdev
, i
);
1530 virtio_queue_aio_attach_host_notifier(vq
, s
->vq_aio_context
[i
]);
1534 /* Suspend virtqueue ioeventfd processing during drain */
1535 static void virtio_blk_drained_begin(void *opaque
)
1537 VirtIOBlock
*s
= opaque
;
1539 if (s
->ioeventfd_started
) {
1540 virtio_blk_ioeventfd_detach(s
);
1544 /* Resume virtqueue ioeventfd processing after drain */
1545 static void virtio_blk_drained_end(void *opaque
)
1547 VirtIOBlock
*s
= opaque
;
1549 if (s
->ioeventfd_started
) {
1550 virtio_blk_ioeventfd_attach(s
);
1554 static const BlockDevOps virtio_block_ops
= {
1555 .resize_cb
= virtio_blk_resize
,
1556 .drained_begin
= virtio_blk_drained_begin
,
1557 .drained_end
= virtio_blk_drained_end
,
1561 validate_iothread_vq_mapping_list(IOThreadVirtQueueMappingList
*list
,
1562 uint16_t num_queues
, Error
**errp
)
1564 g_autofree
unsigned long *vqs
= bitmap_new(num_queues
);
1565 g_autoptr(GHashTable
) iothreads
=
1566 g_hash_table_new(g_str_hash
, g_str_equal
);
1568 for (IOThreadVirtQueueMappingList
*node
= list
; node
; node
= node
->next
) {
1569 const char *name
= node
->value
->iothread
;
1572 if (!iothread_by_id(name
)) {
1573 error_setg(errp
, "IOThread \"%s\" object does not exist", name
);
1577 if (!g_hash_table_add(iothreads
, (gpointer
)name
)) {
1579 "duplicate IOThread name \"%s\" in iothread-vq-mapping",
1585 if (!!node
->value
->vqs
!= !!list
->value
->vqs
) {
1586 error_setg(errp
, "either all items in iothread-vq-mapping "
1587 "must have vqs or none of them must have it");
1592 for (vq
= node
->value
->vqs
; vq
; vq
= vq
->next
) {
1593 if (vq
->value
>= num_queues
) {
1594 error_setg(errp
, "vq index %u for IOThread \"%s\" must be "
1595 "less than num_queues %u in iothread-vq-mapping",
1596 vq
->value
, name
, num_queues
);
1600 if (test_and_set_bit(vq
->value
, vqs
)) {
1601 error_setg(errp
, "cannot assign vq %u to IOThread \"%s\" "
1602 "because it is already assigned", vq
->value
, name
);
1608 if (list
->value
->vqs
) {
1609 for (uint16_t i
= 0; i
< num_queues
; i
++) {
1610 if (!test_bit(i
, vqs
)) {
1612 "missing vq %u IOThread assignment in iothread-vq-mapping",
1623 * apply_iothread_vq_mapping:
1624 * @iothread_vq_mapping_list: The mapping of virtqueues to IOThreads.
1625 * @vq_aio_context: The array of AioContext pointers to fill in.
1626 * @num_queues: The length of @vq_aio_context.
1627 * @errp: If an error occurs, a pointer to the area to store the error.
1629 * Fill in the AioContext for each virtqueue in the @vq_aio_context array given
1630 * the iothread-vq-mapping parameter in @iothread_vq_mapping_list.
1632 * Returns: %true on success, %false on failure.
1634 static bool apply_iothread_vq_mapping(
1635 IOThreadVirtQueueMappingList
*iothread_vq_mapping_list
,
1636 AioContext
**vq_aio_context
,
1637 uint16_t num_queues
,
1640 IOThreadVirtQueueMappingList
*node
;
1641 size_t num_iothreads
= 0;
1642 size_t cur_iothread
= 0;
1644 if (!validate_iothread_vq_mapping_list(iothread_vq_mapping_list
,
1645 num_queues
, errp
)) {
1649 for (node
= iothread_vq_mapping_list
; node
; node
= node
->next
) {
1653 for (node
= iothread_vq_mapping_list
; node
; node
= node
->next
) {
1654 IOThread
*iothread
= iothread_by_id(node
->value
->iothread
);
1655 AioContext
*ctx
= iothread_get_aio_context(iothread
);
1657 /* Released in virtio_blk_vq_aio_context_cleanup() */
1658 object_ref(OBJECT(iothread
));
1660 if (node
->value
->vqs
) {
1663 /* Explicit vq:IOThread assignment */
1664 for (vq
= node
->value
->vqs
; vq
; vq
= vq
->next
) {
1665 assert(vq
->value
< num_queues
);
1666 vq_aio_context
[vq
->value
] = ctx
;
1669 /* Round-robin vq:IOThread assignment */
1670 for (unsigned i
= cur_iothread
; i
< num_queues
;
1671 i
+= num_iothreads
) {
1672 vq_aio_context
[i
] = ctx
;
1682 /* Context: BQL held */
1683 static bool virtio_blk_vq_aio_context_init(VirtIOBlock
*s
, Error
**errp
)
1686 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
1687 VirtIOBlkConf
*conf
= &s
->conf
;
1688 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
1689 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1691 if (conf
->iothread
&& conf
->iothread_vq_mapping_list
) {
1693 "iothread and iothread-vq-mapping properties cannot be set "
1694 "at the same time");
1698 if (conf
->iothread
|| conf
->iothread_vq_mapping_list
) {
1699 if (!k
->set_guest_notifiers
|| !k
->ioeventfd_assign
) {
1701 "device is incompatible with iothread "
1702 "(transport does not support notifiers)");
1705 if (!virtio_device_ioeventfd_enabled(vdev
)) {
1706 error_setg(errp
, "ioeventfd is required for iothread");
1711 * If ioeventfd is (re-)enabled while the guest is running there could
1712 * be block jobs that can conflict.
1714 if (blk_op_is_blocked(conf
->conf
.blk
, BLOCK_OP_TYPE_DATAPLANE
, errp
)) {
1715 error_prepend(errp
, "cannot start virtio-blk ioeventfd: ");
1720 s
->vq_aio_context
= g_new(AioContext
*, conf
->num_queues
);
1722 if (conf
->iothread_vq_mapping_list
) {
1723 if (!apply_iothread_vq_mapping(conf
->iothread_vq_mapping_list
,
1727 g_free(s
->vq_aio_context
);
1728 s
->vq_aio_context
= NULL
;
1731 } else if (conf
->iothread
) {
1732 AioContext
*ctx
= iothread_get_aio_context(conf
->iothread
);
1733 for (unsigned i
= 0; i
< conf
->num_queues
; i
++) {
1734 s
->vq_aio_context
[i
] = ctx
;
1737 /* Released in virtio_blk_vq_aio_context_cleanup() */
1738 object_ref(OBJECT(conf
->iothread
));
1740 AioContext
*ctx
= qemu_get_aio_context();
1741 for (unsigned i
= 0; i
< conf
->num_queues
; i
++) {
1742 s
->vq_aio_context
[i
] = ctx
;
1749 /* Context: BQL held */
1750 static void virtio_blk_vq_aio_context_cleanup(VirtIOBlock
*s
)
1752 VirtIOBlkConf
*conf
= &s
->conf
;
1754 assert(!s
->ioeventfd_started
);
1756 if (conf
->iothread_vq_mapping_list
) {
1757 IOThreadVirtQueueMappingList
*node
;
1759 for (node
= conf
->iothread_vq_mapping_list
; node
; node
= node
->next
) {
1760 IOThread
*iothread
= iothread_by_id(node
->value
->iothread
);
1761 object_unref(OBJECT(iothread
));
1765 if (conf
->iothread
) {
1766 object_unref(OBJECT(conf
->iothread
));
1769 g_free(s
->vq_aio_context
);
1770 s
->vq_aio_context
= NULL
;
1773 /* Context: BQL held */
1774 static int virtio_blk_start_ioeventfd(VirtIODevice
*vdev
)
1776 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
1777 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(s
)));
1778 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1780 unsigned nvqs
= s
->conf
.num_queues
;
1781 Error
*local_err
= NULL
;
1784 if (s
->ioeventfd_started
|| s
->ioeventfd_starting
) {
1788 s
->ioeventfd_starting
= true;
1790 /* Set up guest notifier (irq) */
1791 r
= k
->set_guest_notifiers(qbus
->parent
, nvqs
, true);
1793 error_report("virtio-blk failed to set guest notifier (%d), "
1794 "ensure -accel kvm is set.", r
);
1795 goto fail_guest_notifiers
;
1799 * Batch all the host notifiers in a single transaction to avoid
1800 * quadratic time complexity in address_space_update_ioeventfds().
1802 memory_region_transaction_begin();
1804 /* Set up virtqueue notify */
1805 for (i
= 0; i
< nvqs
; i
++) {
1806 r
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, true);
1810 fprintf(stderr
, "virtio-blk failed to set host notifier (%d)\n", r
);
1812 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
1816 * The transaction expects the ioeventfds to be open when it
1817 * commits. Do it now, before the cleanup loop.
1819 memory_region_transaction_commit();
1822 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus
), j
);
1824 goto fail_host_notifiers
;
1828 memory_region_transaction_commit();
1831 * Try to change the AioContext so that block jobs and other operations can
1832 * co-locate their activity in the same AioContext. If it fails, nevermind.
1834 assert(nvqs
> 0); /* enforced during ->realize() */
1835 r
= blk_set_aio_context(s
->conf
.conf
.blk
, s
->vq_aio_context
[0],
1838 warn_report_err(local_err
);
1842 * These fields must be visible to the IOThread when it processes the
1843 * virtqueue, otherwise it will think ioeventfd has not started yet.
1845 * Make sure ->ioeventfd_started is false when blk_set_aio_context() is
1846 * called above so that draining does not cause the host notifier to be
1847 * detached/attached prematurely.
1849 s
->ioeventfd_starting
= false;
1850 s
->ioeventfd_started
= true;
1851 smp_wmb(); /* paired with aio_notify_accept() on the read side */
1854 * Get this show started by hooking up our callbacks. If drained now,
1855 * virtio_blk_drained_end() will do this later.
1856 * Attaching the notifier also kicks the virtqueues, processing any requests
1857 * they may already have.
1859 if (!blk_in_drain(s
->conf
.conf
.blk
)) {
1860 virtio_blk_ioeventfd_attach(s
);
1864 fail_host_notifiers
:
1865 k
->set_guest_notifiers(qbus
->parent
, nvqs
, false);
1866 fail_guest_notifiers
:
1867 s
->ioeventfd_disabled
= true;
1868 s
->ioeventfd_starting
= false;
1872 /* Stop notifications for new requests from guest.
1874 * Context: BH in IOThread
1876 static void virtio_blk_ioeventfd_stop_vq_bh(void *opaque
)
1878 VirtQueue
*vq
= opaque
;
1879 EventNotifier
*host_notifier
= virtio_queue_get_host_notifier(vq
);
1881 virtio_queue_aio_detach_host_notifier(vq
, qemu_get_current_aio_context());
1884 * Test and clear notifier after disabling event, in case poll callback
1885 * didn't have time to run.
1887 virtio_queue_host_notifier_read(host_notifier
);
1890 /* Context: BQL held */
1891 static void virtio_blk_stop_ioeventfd(VirtIODevice
*vdev
)
1893 VirtIOBlock
*s
= VIRTIO_BLK(vdev
);
1894 BusState
*qbus
= qdev_get_parent_bus(DEVICE(s
));
1895 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1897 unsigned nvqs
= s
->conf
.num_queues
;
1899 if (!s
->ioeventfd_started
|| s
->ioeventfd_stopping
) {
1903 /* Better luck next time. */
1904 if (s
->ioeventfd_disabled
) {
1905 s
->ioeventfd_disabled
= false;
1906 s
->ioeventfd_started
= false;
1909 s
->ioeventfd_stopping
= true;
1911 if (!blk_in_drain(s
->conf
.conf
.blk
)) {
1912 for (i
= 0; i
< nvqs
; i
++) {
1913 VirtQueue
*vq
= virtio_get_queue(vdev
, i
);
1914 AioContext
*ctx
= s
->vq_aio_context
[i
];
1916 aio_wait_bh_oneshot(ctx
, virtio_blk_ioeventfd_stop_vq_bh
, vq
);
1921 * Batch all the host notifiers in a single transaction to avoid
1922 * quadratic time complexity in address_space_update_ioeventfds().
1924 memory_region_transaction_begin();
1926 for (i
= 0; i
< nvqs
; i
++) {
1927 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
1931 * The transaction expects the ioeventfds to be open when it
1932 * commits. Do it now, before the cleanup loop.
1934 memory_region_transaction_commit();
1936 for (i
= 0; i
< nvqs
; i
++) {
1937 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus
), i
);
1941 * Set ->ioeventfd_started to false before draining so that host notifiers
1942 * are not detached/attached anymore.
1944 s
->ioeventfd_started
= false;
1946 /* Wait for virtio_blk_dma_restart_bh() and in flight I/O to complete */
1947 blk_drain(s
->conf
.conf
.blk
);
1950 * Try to switch bs back to the QEMU main loop. If other users keep the
1951 * BlockBackend in the iothread, that's ok
1953 blk_set_aio_context(s
->conf
.conf
.blk
, qemu_get_aio_context(), NULL
);
1955 /* Clean up guest notifier (irq) */
1956 k
->set_guest_notifiers(qbus
->parent
, nvqs
, false);
1958 s
->ioeventfd_stopping
= false;
1961 static void virtio_blk_device_realize(DeviceState
*dev
, Error
**errp
)
1963 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1964 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
1965 VirtIOBlkConf
*conf
= &s
->conf
;
1966 BlockDriverState
*bs
;
1970 if (!conf
->conf
.blk
) {
1971 error_setg(errp
, "drive property not set");
1974 if (!blk_is_inserted(conf
->conf
.blk
)) {
1975 error_setg(errp
, "Device needs media, but drive is empty");
1978 if (conf
->num_queues
== VIRTIO_BLK_AUTO_NUM_QUEUES
) {
1979 conf
->num_queues
= 1;
1981 if (!conf
->num_queues
) {
1982 error_setg(errp
, "num-queues property must be larger than 0");
1985 if (conf
->queue_size
<= 2) {
1986 error_setg(errp
, "invalid queue-size property (%" PRIu16
"), "
1987 "must be > 2", conf
->queue_size
);
1990 if (!is_power_of_2(conf
->queue_size
) ||
1991 conf
->queue_size
> VIRTQUEUE_MAX_SIZE
) {
1992 error_setg(errp
, "invalid queue-size property (%" PRIu16
"), "
1993 "must be a power of 2 (max %d)",
1994 conf
->queue_size
, VIRTQUEUE_MAX_SIZE
);
1998 if (!blkconf_apply_backend_options(&conf
->conf
,
1999 !blk_supports_write_perm(conf
->conf
.blk
),
2003 s
->original_wce
= blk_enable_write_cache(conf
->conf
.blk
);
2004 if (!blkconf_geometry(&conf
->conf
, NULL
, 65535, 255, 255, errp
)) {
2008 if (!blkconf_blocksizes(&conf
->conf
, errp
)) {
2012 bs
= blk_bs(conf
->conf
.blk
);
2013 if (bs
->bl
.zoned
!= BLK_Z_NONE
) {
2014 virtio_add_feature(&s
->host_features
, VIRTIO_BLK_F_ZONED
);
2015 if (bs
->bl
.zoned
== BLK_Z_HM
) {
2016 virtio_clear_feature(&s
->host_features
, VIRTIO_BLK_F_DISCARD
);
2020 if (virtio_has_feature(s
->host_features
, VIRTIO_BLK_F_DISCARD
) &&
2021 (!conf
->max_discard_sectors
||
2022 conf
->max_discard_sectors
> BDRV_REQUEST_MAX_SECTORS
)) {
2023 error_setg(errp
, "invalid max-discard-sectors property (%" PRIu32
")"
2024 ", must be between 1 and %d",
2025 conf
->max_discard_sectors
, (int)BDRV_REQUEST_MAX_SECTORS
);
2029 if (virtio_has_feature(s
->host_features
, VIRTIO_BLK_F_WRITE_ZEROES
) &&
2030 (!conf
->max_write_zeroes_sectors
||
2031 conf
->max_write_zeroes_sectors
> BDRV_REQUEST_MAX_SECTORS
)) {
2032 error_setg(errp
, "invalid max-write-zeroes-sectors property (%" PRIu32
2033 "), must be between 1 and %d",
2034 conf
->max_write_zeroes_sectors
,
2035 (int)BDRV_REQUEST_MAX_SECTORS
);
2039 s
->config_size
= virtio_get_config_size(&virtio_blk_cfg_size_params
,
2041 virtio_init(vdev
, VIRTIO_ID_BLOCK
, s
->config_size
);
2043 qemu_mutex_init(&s
->rq_lock
);
2045 s
->blk
= conf
->conf
.blk
;
2047 s
->sector_mask
= (s
->conf
.conf
.logical_block_size
/ BDRV_SECTOR_SIZE
) - 1;
2049 for (i
= 0; i
< conf
->num_queues
; i
++) {
2050 virtio_add_queue(vdev
, conf
->queue_size
, virtio_blk_handle_output
);
2052 qemu_coroutine_inc_pool_size(conf
->num_queues
* conf
->queue_size
/ 2);
2054 /* Don't start ioeventfd if transport does not support notifiers. */
2055 if (!virtio_device_ioeventfd_enabled(vdev
)) {
2056 s
->ioeventfd_disabled
= true;
2059 virtio_blk_vq_aio_context_init(s
, &err
);
2061 error_propagate(errp
, err
);
2062 for (i
= 0; i
< conf
->num_queues
; i
++) {
2063 virtio_del_queue(vdev
, i
);
2065 virtio_cleanup(vdev
);
2070 * This must be after virtio_init() so virtio_blk_dma_restart_cb() gets
2071 * called after ->start_ioeventfd() has already set blk's AioContext.
2074 qdev_add_vm_change_state_handler(dev
, virtio_blk_dma_restart_cb
, s
);
2076 blk_ram_registrar_init(&s
->blk_ram_registrar
, s
->blk
);
2077 blk_set_dev_ops(s
->blk
, &virtio_block_ops
, s
);
2079 blk_iostatus_enable(s
->blk
);
2081 add_boot_device_lchs(dev
, "/disk@0,0",
2087 static void virtio_blk_device_unrealize(DeviceState
*dev
)
2089 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
2090 VirtIOBlock
*s
= VIRTIO_BLK(dev
);
2091 VirtIOBlkConf
*conf
= &s
->conf
;
2095 del_boot_device_lchs(dev
, "/disk@0,0");
2096 virtio_blk_vq_aio_context_cleanup(s
);
2097 for (i
= 0; i
< conf
->num_queues
; i
++) {
2098 virtio_del_queue(vdev
, i
);
2100 qemu_coroutine_dec_pool_size(conf
->num_queues
* conf
->queue_size
/ 2);
2101 qemu_mutex_destroy(&s
->rq_lock
);
2102 blk_ram_registrar_destroy(&s
->blk_ram_registrar
);
2103 qemu_del_vm_change_state_handler(s
->change
);
2104 blockdev_mark_auto_del(s
->blk
);
2105 virtio_cleanup(vdev
);
2108 static void virtio_blk_instance_init(Object
*obj
)
2110 VirtIOBlock
*s
= VIRTIO_BLK(obj
);
2112 device_add_bootindex_property(obj
, &s
->conf
.conf
.bootindex
,
2113 "bootindex", "/disk@0,0",
2117 static const VMStateDescription vmstate_virtio_blk
= {
2118 .name
= "virtio-blk",
2119 .minimum_version_id
= 2,
2121 .fields
= (const VMStateField
[]) {
2122 VMSTATE_VIRTIO_DEVICE
,
2123 VMSTATE_END_OF_LIST()
2127 static Property virtio_blk_properties
[] = {
2128 DEFINE_BLOCK_PROPERTIES(VirtIOBlock
, conf
.conf
),
2129 DEFINE_BLOCK_ERROR_PROPERTIES(VirtIOBlock
, conf
.conf
),
2130 DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock
, conf
.conf
),
2131 DEFINE_PROP_STRING("serial", VirtIOBlock
, conf
.serial
),
2132 DEFINE_PROP_BIT64("config-wce", VirtIOBlock
, host_features
,
2133 VIRTIO_BLK_F_CONFIG_WCE
, true),
2135 DEFINE_PROP_BIT64("scsi", VirtIOBlock
, host_features
,
2136 VIRTIO_BLK_F_SCSI
, false),
2138 DEFINE_PROP_BIT("request-merging", VirtIOBlock
, conf
.request_merging
, 0,
2140 DEFINE_PROP_UINT16("num-queues", VirtIOBlock
, conf
.num_queues
,
2141 VIRTIO_BLK_AUTO_NUM_QUEUES
),
2142 DEFINE_PROP_UINT16("queue-size", VirtIOBlock
, conf
.queue_size
, 256),
2143 DEFINE_PROP_BOOL("seg-max-adjust", VirtIOBlock
, conf
.seg_max_adjust
, true),
2144 DEFINE_PROP_LINK("iothread", VirtIOBlock
, conf
.iothread
, TYPE_IOTHREAD
,
2146 DEFINE_PROP_IOTHREAD_VQ_MAPPING_LIST("iothread-vq-mapping", VirtIOBlock
,
2147 conf
.iothread_vq_mapping_list
),
2148 DEFINE_PROP_BIT64("discard", VirtIOBlock
, host_features
,
2149 VIRTIO_BLK_F_DISCARD
, true),
2150 DEFINE_PROP_BOOL("report-discard-granularity", VirtIOBlock
,
2151 conf
.report_discard_granularity
, true),
2152 DEFINE_PROP_BIT64("write-zeroes", VirtIOBlock
, host_features
,
2153 VIRTIO_BLK_F_WRITE_ZEROES
, true),
2154 DEFINE_PROP_UINT32("max-discard-sectors", VirtIOBlock
,
2155 conf
.max_discard_sectors
, BDRV_REQUEST_MAX_SECTORS
),
2156 DEFINE_PROP_UINT32("max-write-zeroes-sectors", VirtIOBlock
,
2157 conf
.max_write_zeroes_sectors
, BDRV_REQUEST_MAX_SECTORS
),
2158 DEFINE_PROP_BOOL("x-enable-wce-if-config-wce", VirtIOBlock
,
2159 conf
.x_enable_wce_if_config_wce
, true),
2160 DEFINE_PROP_END_OF_LIST(),
2163 static void virtio_blk_class_init(ObjectClass
*klass
, void *data
)
2165 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2166 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
2168 device_class_set_props(dc
, virtio_blk_properties
);
2169 dc
->vmsd
= &vmstate_virtio_blk
;
2170 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
2171 vdc
->realize
= virtio_blk_device_realize
;
2172 vdc
->unrealize
= virtio_blk_device_unrealize
;
2173 vdc
->get_config
= virtio_blk_update_config
;
2174 vdc
->set_config
= virtio_blk_set_config
;
2175 vdc
->get_features
= virtio_blk_get_features
;
2176 vdc
->set_status
= virtio_blk_set_status
;
2177 vdc
->reset
= virtio_blk_reset
;
2178 vdc
->save
= virtio_blk_save_device
;
2179 vdc
->load
= virtio_blk_load_device
;
2180 vdc
->start_ioeventfd
= virtio_blk_start_ioeventfd
;
2181 vdc
->stop_ioeventfd
= virtio_blk_stop_ioeventfd
;
2184 static const TypeInfo virtio_blk_info
= {
2185 .name
= TYPE_VIRTIO_BLK
,
2186 .parent
= TYPE_VIRTIO_DEVICE
,
2187 .instance_size
= sizeof(VirtIOBlock
),
2188 .instance_init
= virtio_blk_instance_init
,
2189 .class_init
= virtio_blk_class_init
,
2192 static void virtio_register_types(void)
2194 type_register_static(&virtio_blk_info
);
2197 type_init(virtio_register_types
)