1 /* 2 * Virtio 9p backend 3 * 4 * Copyright IBM, Corp. 2010 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 "hw/virtio/virtio.h" 16 #include "qemu/sockets.h" 17 #include "virtio-9p.h" 18 #include "fsdev/qemu-fsdev.h" 19 #include "coth.h" 20 #include "hw/virtio/virtio-access.h" 21 #include "qemu/iov.h" 22 #include "qemu/module.h" 23 24 static void virtio_9p_push_and_notify(V9fsPDU *pdu) 25 { 26 V9fsState *s = pdu->s; 27 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 28 VirtQueueElement *elem = v->elems[pdu->idx]; 29 30 /* push onto queue and notify */ 31 virtqueue_push(v->vq, elem, pdu->size); 32 g_free(elem); 33 v->elems[pdu->idx] = NULL; 34 35 /* FIXME: we should batch these completions */ 36 virtio_notify(VIRTIO_DEVICE(v), v->vq); 37 } 38 39 static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq) 40 { 41 V9fsVirtioState *v = (V9fsVirtioState *)vdev; 42 V9fsState *s = &v->state; 43 V9fsPDU *pdu; 44 ssize_t len; 45 VirtQueueElement *elem; 46 47 while ((pdu = pdu_alloc(s))) { 48 P9MsgHeader out; 49 50 elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); 51 if (!elem) { 52 goto out_free_pdu; 53 } 54 55 if (iov_size(elem->in_sg, elem->in_num) < 7) { 56 virtio_error(vdev, 57 "The guest sent a VirtFS request without space for " 58 "the reply"); 59 goto out_free_req; 60 } 61 62 len = iov_to_buf(elem->out_sg, elem->out_num, 0, &out, 7); 63 if (len != 7) { 64 virtio_error(vdev, "The guest sent a malformed VirtFS request: " 65 "header size is %zd, should be 7", len); 66 goto out_free_req; 67 } 68 69 v->elems[pdu->idx] = elem; 70 71 pdu_submit(pdu, &out); 72 } 73 74 return; 75 76 out_free_req: 77 virtqueue_detach_element(vq, elem, 0); 78 g_free(elem); 79 out_free_pdu: 80 pdu_free(pdu); 81 } 82 83 static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features, 84 Error **errp) 85 { 86 virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG); 87 return features; 88 } 89 90 static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config) 91 { 92 int len; 93 struct virtio_9p_config *cfg; 94 V9fsVirtioState *v = VIRTIO_9P(vdev); 95 V9fsState *s = &v->state; 96 97 len = strlen(s->tag); 98 cfg = g_malloc0(sizeof(struct virtio_9p_config) + len); 99 virtio_stw_p(vdev, &cfg->tag_len, len); 100 /* We don't copy the terminating null to config space */ 101 memcpy(cfg->tag, s->tag, len); 102 memcpy(config, cfg, v->config_size); 103 g_free(cfg); 104 } 105 106 static void virtio_9p_reset(VirtIODevice *vdev) 107 { 108 V9fsVirtioState *v = (V9fsVirtioState *)vdev; 109 110 v9fs_reset(&v->state); 111 } 112 113 static ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset, 114 const char *fmt, va_list ap) 115 { 116 V9fsState *s = pdu->s; 117 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 118 VirtQueueElement *elem = v->elems[pdu->idx]; 119 ssize_t ret; 120 121 ret = v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap); 122 if (ret < 0) { 123 VirtIODevice *vdev = VIRTIO_DEVICE(v); 124 125 virtio_error(vdev, "Failed to encode VirtFS reply type %d", 126 pdu->id + 1); 127 } 128 return ret; 129 } 130 131 static ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset, 132 const char *fmt, va_list ap) 133 { 134 V9fsState *s = pdu->s; 135 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 136 VirtQueueElement *elem = v->elems[pdu->idx]; 137 ssize_t ret; 138 139 ret = v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap); 140 if (ret < 0) { 141 VirtIODevice *vdev = VIRTIO_DEVICE(v); 142 143 virtio_error(vdev, "Failed to decode VirtFS request type %d", pdu->id); 144 } 145 return ret; 146 } 147 148 static void virtio_init_in_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov, 149 unsigned int *pniov, size_t size) 150 { 151 V9fsState *s = pdu->s; 152 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 153 VirtQueueElement *elem = v->elems[pdu->idx]; 154 size_t buf_size = iov_size(elem->in_sg, elem->in_num); 155 156 if (buf_size < size) { 157 VirtIODevice *vdev = VIRTIO_DEVICE(v); 158 159 virtio_error(vdev, 160 "VirtFS reply type %d needs %zu bytes, buffer has %zu", 161 pdu->id + 1, size, buf_size); 162 } 163 164 *piov = elem->in_sg; 165 *pniov = elem->in_num; 166 } 167 168 static void virtio_init_out_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov, 169 unsigned int *pniov, size_t size) 170 { 171 V9fsState *s = pdu->s; 172 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 173 VirtQueueElement *elem = v->elems[pdu->idx]; 174 size_t buf_size = iov_size(elem->out_sg, elem->out_num); 175 176 if (buf_size < size) { 177 VirtIODevice *vdev = VIRTIO_DEVICE(v); 178 179 virtio_error(vdev, 180 "VirtFS request type %d needs %zu bytes, buffer has %zu", 181 pdu->id, size, buf_size); 182 } 183 184 *piov = elem->out_sg; 185 *pniov = elem->out_num; 186 } 187 188 static const V9fsTransport virtio_9p_transport = { 189 .pdu_vmarshal = virtio_pdu_vmarshal, 190 .pdu_vunmarshal = virtio_pdu_vunmarshal, 191 .init_in_iov_from_pdu = virtio_init_in_iov_from_pdu, 192 .init_out_iov_from_pdu = virtio_init_out_iov_from_pdu, 193 .push_and_notify = virtio_9p_push_and_notify, 194 }; 195 196 static void virtio_9p_device_realize(DeviceState *dev, Error **errp) 197 { 198 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 199 V9fsVirtioState *v = VIRTIO_9P(dev); 200 V9fsState *s = &v->state; 201 202 if (v9fs_device_realize_common(s, &virtio_9p_transport, errp)) { 203 return; 204 } 205 206 v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag); 207 virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P, v->config_size); 208 v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output); 209 } 210 211 static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp) 212 { 213 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 214 V9fsVirtioState *v = VIRTIO_9P(dev); 215 V9fsState *s = &v->state; 216 217 virtio_cleanup(vdev); 218 v9fs_device_unrealize_common(s, errp); 219 } 220 221 /* virtio-9p device */ 222 223 static const VMStateDescription vmstate_virtio_9p = { 224 .name = "virtio-9p", 225 .minimum_version_id = 1, 226 .version_id = 1, 227 .fields = (VMStateField[]) { 228 VMSTATE_VIRTIO_DEVICE, 229 VMSTATE_END_OF_LIST() 230 }, 231 }; 232 233 static Property virtio_9p_properties[] = { 234 DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag), 235 DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id), 236 DEFINE_PROP_END_OF_LIST(), 237 }; 238 239 static void virtio_9p_class_init(ObjectClass *klass, void *data) 240 { 241 DeviceClass *dc = DEVICE_CLASS(klass); 242 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 243 244 dc->props = virtio_9p_properties; 245 dc->vmsd = &vmstate_virtio_9p; 246 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 247 vdc->realize = virtio_9p_device_realize; 248 vdc->unrealize = virtio_9p_device_unrealize; 249 vdc->get_features = virtio_9p_get_features; 250 vdc->get_config = virtio_9p_get_config; 251 vdc->reset = virtio_9p_reset; 252 } 253 254 static const TypeInfo virtio_device_info = { 255 .name = TYPE_VIRTIO_9P, 256 .parent = TYPE_VIRTIO_DEVICE, 257 .instance_size = sizeof(V9fsVirtioState), 258 .class_init = virtio_9p_class_init, 259 }; 260 261 static void virtio_9p_register_types(void) 262 { 263 type_register_static(&virtio_device_info); 264 } 265 266 type_init(virtio_9p_register_types) 267