| 1 | /* |
| 2 | * Handler for virtio-blk I/O |
| 3 | * |
| 4 | * Copyright (c) 2020 Red Hat, Inc. |
| 5 | * Copyright (C) 2022 Bytedance Inc. and/or its affiliates. All rights reserved. |
| 6 | * |
| 7 | * Author: |
| 8 | * Coiby Xu <coiby.xu@gmail.com> |
| 9 | * Xie Yongji <xieyongji@bytedance.com> |
| 10 | * |
| 11 | * This work is licensed under the terms of the GNU GPL, version 2 or |
| 12 | * later. See the COPYING file in the top-level directory. |
| 13 | */ |
| 14 | |
| 15 | #include "qemu/osdep.h" |
| 16 | #include "qemu/bswap.h" |
| 17 | #include "qemu/error-report.h" |
| 18 | #include "virtio-blk-handler.h" |
| 19 | |
| 20 | #include "standard-headers/linux/virtio_blk.h" |
| 21 | |
| 22 | struct virtio_blk_inhdr { |
| 23 | unsigned char status; |
| 24 | }; |
| 25 | |
| 26 | static bool coroutine_fn |
| 27 | virtio_blk_sect_range_ok(BlockBackend *blk, uint32_t block_size, |
| 28 | uint64_t sector, size_t size) |
| 29 | { |
| 30 | uint64_t nb_sectors; |
| 31 | uint64_t total_sectors; |
| 32 | |
| 33 | if (size % VIRTIO_BLK_SECTOR_SIZE) { |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | nb_sectors = size >> VIRTIO_BLK_SECTOR_BITS; |
| 38 | |
| 39 | QEMU_BUILD_BUG_ON(BDRV_SECTOR_SIZE != VIRTIO_BLK_SECTOR_SIZE); |
| 40 | if (nb_sectors > BDRV_REQUEST_MAX_SECTORS) { |
| 41 | return false; |
| 42 | } |
| 43 | if ((sector << VIRTIO_BLK_SECTOR_BITS) % block_size) { |
| 44 | return false; |
| 45 | } |
| 46 | blk_co_get_geometry(blk, &total_sectors); |
| 47 | if (sector > total_sectors || nb_sectors > total_sectors - sector) { |
| 48 | return false; |
| 49 | } |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | static int coroutine_fn |
| 54 | virtio_blk_discard_write_zeroes(VirtioBlkHandler *handler, struct iovec *iov, |
| 55 | uint32_t iovcnt, uint32_t type) |
| 56 | { |
| 57 | BlockBackend *blk = handler->blk; |
| 58 | struct virtio_blk_discard_write_zeroes desc; |
| 59 | ssize_t size; |
| 60 | uint64_t sector; |
| 61 | uint32_t num_sectors; |
| 62 | uint32_t max_sectors; |
| 63 | uint32_t flags; |
| 64 | int bytes; |
| 65 | |
| 66 | /* Only one desc is currently supported */ |
| 67 | if (unlikely(iov_size(iov, iovcnt) > sizeof(desc))) { |
| 68 | return VIRTIO_BLK_S_UNSUPP; |
| 69 | } |
| 70 | |
| 71 | size = iov_to_buf(iov, iovcnt, 0, &desc, sizeof(desc)); |
| 72 | if (unlikely(size != sizeof(desc))) { |
| 73 | error_report("Invalid size %zd, expected %zu", size, sizeof(desc)); |
| 74 | return VIRTIO_BLK_S_IOERR; |
| 75 | } |
| 76 | |
| 77 | sector = le64_to_cpu(desc.sector); |
| 78 | num_sectors = le32_to_cpu(desc.num_sectors); |
| 79 | flags = le32_to_cpu(desc.flags); |
| 80 | max_sectors = (type == VIRTIO_BLK_T_WRITE_ZEROES) ? |
| 81 | VIRTIO_BLK_MAX_WRITE_ZEROES_SECTORS : |
| 82 | VIRTIO_BLK_MAX_DISCARD_SECTORS; |
| 83 | |
| 84 | /* This check ensures that 'bytes' fits in an int */ |
| 85 | if (unlikely(num_sectors > max_sectors)) { |
| 86 | return VIRTIO_BLK_S_IOERR; |
| 87 | } |
| 88 | |
| 89 | bytes = num_sectors << VIRTIO_BLK_SECTOR_BITS; |
| 90 | |
| 91 | if (unlikely(!virtio_blk_sect_range_ok(blk, handler->logical_block_size, |
| 92 | sector, bytes))) { |
| 93 | return VIRTIO_BLK_S_IOERR; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for discard |
| 98 | * and write zeroes commands if any unknown flag is set. |
| 99 | */ |
| 100 | if (unlikely(flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) { |
| 101 | return VIRTIO_BLK_S_UNSUPP; |
| 102 | } |
| 103 | |
| 104 | if (type == VIRTIO_BLK_T_WRITE_ZEROES) { |
| 105 | int blk_flags = 0; |
| 106 | |
| 107 | if (flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) { |
| 108 | blk_flags |= BDRV_REQ_MAY_UNMAP; |
| 109 | } |
| 110 | |
| 111 | if (blk_co_pwrite_zeroes(blk, sector << VIRTIO_BLK_SECTOR_BITS, |
| 112 | bytes, blk_flags) == 0) { |
| 113 | return VIRTIO_BLK_S_OK; |
| 114 | } |
| 115 | } else if (type == VIRTIO_BLK_T_DISCARD) { |
| 116 | /* |
| 117 | * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for |
| 118 | * discard commands if the unmap flag is set. |
| 119 | */ |
| 120 | if (unlikely(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) { |
| 121 | return VIRTIO_BLK_S_UNSUPP; |
| 122 | } |
| 123 | |
| 124 | if (blk_co_pdiscard(blk, sector << VIRTIO_BLK_SECTOR_BITS, |
| 125 | bytes, 0) == 0) { |
| 126 | return VIRTIO_BLK_S_OK; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return VIRTIO_BLK_S_IOERR; |
| 131 | } |
| 132 | |
| 133 | int coroutine_fn virtio_blk_process_req(VirtioBlkHandler *handler, |
| 134 | struct iovec *in_iov, |
| 135 | struct iovec *out_iov, |
| 136 | unsigned int in_num, |
| 137 | unsigned int out_num) |
| 138 | { |
| 139 | BlockBackend *blk = handler->blk; |
| 140 | struct virtio_blk_inhdr *in; |
| 141 | struct virtio_blk_outhdr out; |
| 142 | uint32_t type; |
| 143 | int in_len; |
| 144 | |
| 145 | if (out_num < 1 || in_num < 1) { |
| 146 | error_report("virtio-blk request missing headers"); |
| 147 | return -EINVAL; |
| 148 | } |
| 149 | |
| 150 | if (unlikely(iov_to_buf(out_iov, out_num, 0, &out, |
| 151 | sizeof(out)) != sizeof(out))) { |
| 152 | error_report("virtio-blk request outhdr too short"); |
| 153 | return -EINVAL; |
| 154 | } |
| 155 | |
| 156 | iov_discard_front(&out_iov, &out_num, sizeof(out)); |
| 157 | |
| 158 | if (in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) { |
| 159 | error_report("virtio-blk request inhdr too short"); |
| 160 | return -EINVAL; |
| 161 | } |
| 162 | |
| 163 | /* We always touch the last byte, so just see how big in_iov is. */ |
| 164 | in_len = iov_size(in_iov, in_num); |
| 165 | in = (void *)in_iov[in_num - 1].iov_base |
| 166 | + in_iov[in_num - 1].iov_len |
| 167 | - sizeof(struct virtio_blk_inhdr); |
| 168 | iov_discard_back(in_iov, &in_num, sizeof(struct virtio_blk_inhdr)); |
| 169 | |
| 170 | type = le32_to_cpu(out.type); |
| 171 | switch (type & ~VIRTIO_BLK_T_BARRIER) { |
| 172 | case VIRTIO_BLK_T_IN: |
| 173 | case VIRTIO_BLK_T_OUT: { |
| 174 | QEMUIOVector qiov; |
| 175 | int64_t offset; |
| 176 | ssize_t ret = 0; |
| 177 | bool is_write = type & VIRTIO_BLK_T_OUT; |
| 178 | int64_t sector_num = le64_to_cpu(out.sector); |
| 179 | |
| 180 | if (is_write && !handler->writable) { |
| 181 | in->status = VIRTIO_BLK_S_IOERR; |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | if (is_write) { |
| 186 | qemu_iovec_init_external(&qiov, out_iov, out_num); |
| 187 | } else { |
| 188 | qemu_iovec_init_external(&qiov, in_iov, in_num); |
| 189 | } |
| 190 | |
| 191 | if (unlikely(!virtio_blk_sect_range_ok(blk, |
| 192 | handler->logical_block_size, |
| 193 | sector_num, qiov.size))) { |
| 194 | in->status = VIRTIO_BLK_S_IOERR; |
| 195 | break; |
| 196 | } |
| 197 | |
| 198 | offset = sector_num << VIRTIO_BLK_SECTOR_BITS; |
| 199 | |
| 200 | if (is_write) { |
| 201 | ret = blk_co_pwritev(blk, offset, qiov.size, &qiov, 0); |
| 202 | } else { |
| 203 | ret = blk_co_preadv(blk, offset, qiov.size, &qiov, 0); |
| 204 | } |
| 205 | if (ret >= 0) { |
| 206 | in->status = VIRTIO_BLK_S_OK; |
| 207 | } else { |
| 208 | in->status = VIRTIO_BLK_S_IOERR; |
| 209 | } |
| 210 | break; |
| 211 | } |
| 212 | case VIRTIO_BLK_T_FLUSH: |
| 213 | if (blk_co_flush(blk) == 0) { |
| 214 | in->status = VIRTIO_BLK_S_OK; |
| 215 | } else { |
| 216 | in->status = VIRTIO_BLK_S_IOERR; |
| 217 | } |
| 218 | break; |
| 219 | case VIRTIO_BLK_T_GET_ID: { |
| 220 | size_t size = MIN(strlen(handler->serial) + 1, |
| 221 | MIN(iov_size(in_iov, in_num), |
| 222 | VIRTIO_BLK_ID_BYTES)); |
| 223 | iov_from_buf(in_iov, in_num, 0, handler->serial, size); |
| 224 | in->status = VIRTIO_BLK_S_OK; |
| 225 | break; |
| 226 | } |
| 227 | case VIRTIO_BLK_T_DISCARD: |
| 228 | case VIRTIO_BLK_T_WRITE_ZEROES: |
| 229 | if (!handler->writable) { |
| 230 | in->status = VIRTIO_BLK_S_IOERR; |
| 231 | break; |
| 232 | } |
| 233 | in->status = virtio_blk_discard_write_zeroes(handler, out_iov, |
| 234 | out_num, type); |
| 235 | break; |
| 236 | default: |
| 237 | in->status = VIRTIO_BLK_S_UNSUPP; |
| 238 | break; |
| 239 | } |
| 240 | |
| 241 | return in_len; |
| 242 | } |