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 "hw/i386/pc.h" 17 #include "qemu/sockets.h" 18 #include "virtio-9p.h" 19 #include "fsdev/qemu-fsdev.h" 20 #include "9p-xattr.h" 21 #include "coth.h" 22 #include "hw/virtio/virtio-access.h" 23 #include "qemu/iov.h" 24 25 void virtio_9p_push_and_notify(V9fsPDU *pdu) 26 { 27 V9fsState *s = pdu->s; 28 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 29 VirtQueueElement *elem = v->elems[pdu->idx]; 30 31 /* push onto queue and notify */ 32 virtqueue_push(v->vq, elem, pdu->size); 33 g_free(elem); 34 v->elems[pdu->idx] = NULL; 35 36 /* FIXME: we should batch these completions */ 37 virtio_notify(VIRTIO_DEVICE(v), v->vq); 38 } 39 40 static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq) 41 { 42 V9fsVirtioState *v = (V9fsVirtioState *)vdev; 43 V9fsState *s = &v->state; 44 V9fsPDU *pdu; 45 ssize_t len; 46 47 while ((pdu = pdu_alloc(s))) { 48 struct { 49 uint32_t size_le; 50 uint8_t id; 51 uint16_t tag_le; 52 } QEMU_PACKED out; 53 VirtQueueElement *elem; 54 55 elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); 56 if (!elem) { 57 pdu_free(pdu); 58 break; 59 } 60 61 BUG_ON(elem->out_num == 0 || elem->in_num == 0); 62 QEMU_BUILD_BUG_ON(sizeof out != 7); 63 64 v->elems[pdu->idx] = elem; 65 len = iov_to_buf(elem->out_sg, elem->out_num, 0, 66 &out, sizeof out); 67 BUG_ON(len != sizeof out); 68 69 pdu->size = le32_to_cpu(out.size_le); 70 71 pdu->id = out.id; 72 pdu->tag = le16_to_cpu(out.tag_le); 73 74 qemu_co_queue_init(&pdu->complete); 75 pdu_submit(pdu); 76 } 77 } 78 79 static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features, 80 Error **errp) 81 { 82 virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG); 83 return features; 84 } 85 86 static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config) 87 { 88 int len; 89 struct virtio_9p_config *cfg; 90 V9fsVirtioState *v = VIRTIO_9P(vdev); 91 V9fsState *s = &v->state; 92 93 len = strlen(s->tag); 94 cfg = g_malloc0(sizeof(struct virtio_9p_config) + len); 95 virtio_stw_p(vdev, &cfg->tag_len, len); 96 /* We don't copy the terminating null to config space */ 97 memcpy(cfg->tag, s->tag, len); 98 memcpy(config, cfg, v->config_size); 99 g_free(cfg); 100 } 101 102 static void virtio_9p_save(QEMUFile *f, void *opaque) 103 { 104 virtio_save(VIRTIO_DEVICE(opaque), f); 105 } 106 107 static int virtio_9p_load(QEMUFile *f, void *opaque, int version_id) 108 { 109 return virtio_load(VIRTIO_DEVICE(opaque), f, version_id); 110 } 111 112 static void virtio_9p_device_realize(DeviceState *dev, Error **errp) 113 { 114 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 115 V9fsVirtioState *v = VIRTIO_9P(dev); 116 V9fsState *s = &v->state; 117 118 if (v9fs_device_realize_common(s, errp)) { 119 goto out; 120 } 121 122 v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag); 123 virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P, v->config_size); 124 v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output); 125 register_savevm(dev, "virtio-9p", -1, 1, virtio_9p_save, virtio_9p_load, v); 126 127 out: 128 return; 129 } 130 131 static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp) 132 { 133 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 134 V9fsVirtioState *v = VIRTIO_9P(dev); 135 V9fsState *s = &v->state; 136 137 virtio_cleanup(vdev); 138 unregister_savevm(dev, "virtio-9p", v); 139 v9fs_device_unrealize_common(s, errp); 140 } 141 142 ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset, 143 const char *fmt, va_list ap) 144 { 145 V9fsState *s = pdu->s; 146 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 147 VirtQueueElement *elem = v->elems[pdu->idx]; 148 149 return v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap); 150 } 151 152 ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset, 153 const char *fmt, va_list ap) 154 { 155 V9fsState *s = pdu->s; 156 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 157 VirtQueueElement *elem = v->elems[pdu->idx]; 158 159 return v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap); 160 } 161 162 void virtio_init_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov, 163 unsigned int *pniov, bool is_write) 164 { 165 V9fsState *s = pdu->s; 166 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 167 VirtQueueElement *elem = v->elems[pdu->idx]; 168 169 if (is_write) { 170 *piov = elem->out_sg; 171 *pniov = elem->out_num; 172 } else { 173 *piov = elem->in_sg; 174 *pniov = elem->in_num; 175 } 176 } 177 178 /* virtio-9p device */ 179 180 static Property virtio_9p_properties[] = { 181 DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag), 182 DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id), 183 DEFINE_PROP_END_OF_LIST(), 184 }; 185 186 static void virtio_9p_class_init(ObjectClass *klass, void *data) 187 { 188 DeviceClass *dc = DEVICE_CLASS(klass); 189 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 190 191 dc->props = virtio_9p_properties; 192 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 193 vdc->realize = virtio_9p_device_realize; 194 vdc->unrealize = virtio_9p_device_unrealize; 195 vdc->get_features = virtio_9p_get_features; 196 vdc->get_config = virtio_9p_get_config; 197 } 198 199 static const TypeInfo virtio_device_info = { 200 .name = TYPE_VIRTIO_9P, 201 .parent = TYPE_VIRTIO_DEVICE, 202 .instance_size = sizeof(V9fsVirtioState), 203 .class_init = virtio_9p_class_init, 204 }; 205 206 static void virtio_9p_register_types(void) 207 { 208 type_register_static(&virtio_device_info); 209 } 210 211 type_init(virtio_9p_register_types) 212