qapi: Improve specificity of type/member descriptions
[qemu/armbru.git] / hw / 9pfs / virtio-9p-device.c
blob5f522e68e9f2a9b7030ebe9883f16c6a25c1ea89
1 /*
2 * Virtio 9p backend
4 * Copyright IBM, Corp. 2010
6 * Authors:
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.
15 * Not so fast! You might want to read the 9p developer docs first:
16 * https://wiki.qemu.org/Documentation/9p
19 #include "qemu/osdep.h"
20 #include "hw/virtio/virtio.h"
21 #include "qemu/sockets.h"
22 #include "virtio-9p.h"
23 #include "fsdev/qemu-fsdev.h"
24 #include "coth.h"
25 #include "hw/qdev-properties.h"
26 #include "hw/virtio/virtio-access.h"
27 #include "qemu/iov.h"
28 #include "qemu/module.h"
29 #include "sysemu/qtest.h"
31 static void virtio_9p_push_and_notify(V9fsPDU *pdu)
33 V9fsState *s = pdu->s;
34 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
35 VirtQueueElement *elem = v->elems[pdu->idx];
37 /* push onto queue and notify */
38 virtqueue_push(v->vq, elem, pdu->size);
39 g_free(elem);
40 v->elems[pdu->idx] = NULL;
42 /* FIXME: we should batch these completions */
43 virtio_notify(VIRTIO_DEVICE(v), v->vq);
46 static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
48 V9fsVirtioState *v = (V9fsVirtioState *)vdev;
49 V9fsState *s = &v->state;
50 V9fsPDU *pdu;
51 ssize_t len;
52 VirtQueueElement *elem;
54 while ((pdu = pdu_alloc(s))) {
55 P9MsgHeader out;
57 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
58 if (!elem) {
59 goto out_free_pdu;
62 if (iov_size(elem->in_sg, elem->in_num) < 7) {
63 virtio_error(vdev,
64 "The guest sent a VirtFS request without space for "
65 "the reply");
66 goto out_free_req;
69 len = iov_to_buf(elem->out_sg, elem->out_num, 0, &out, 7);
70 if (len != 7) {
71 virtio_error(vdev, "The guest sent a malformed VirtFS request: "
72 "header size is %zd, should be 7", len);
73 goto out_free_req;
76 v->elems[pdu->idx] = elem;
78 pdu_submit(pdu, &out);
81 return;
83 out_free_req:
84 virtqueue_detach_element(vq, elem, 0);
85 g_free(elem);
86 out_free_pdu:
87 pdu_free(pdu);
90 static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features,
91 Error **errp)
93 virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG);
94 return features;
97 static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
99 int len;
100 struct virtio_9p_config *cfg;
101 V9fsVirtioState *v = VIRTIO_9P(vdev);
102 V9fsState *s = &v->state;
104 len = strlen(s->tag);
105 cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
106 virtio_stw_p(vdev, &cfg->tag_len, len);
107 /* We don't copy the terminating null to config space */
108 memcpy(cfg->tag, s->tag, len);
109 memcpy(config, cfg, v->config_size);
110 g_free(cfg);
113 static void virtio_9p_reset(VirtIODevice *vdev)
115 V9fsVirtioState *v = (V9fsVirtioState *)vdev;
117 v9fs_reset(&v->state);
120 static ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset,
121 const char *fmt, va_list ap)
123 V9fsState *s = pdu->s;
124 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
125 VirtQueueElement *elem = v->elems[pdu->idx];
126 ssize_t ret;
128 ret = v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap);
129 if (ret < 0) {
130 VirtIODevice *vdev = VIRTIO_DEVICE(v);
132 virtio_error(vdev, "Failed to encode VirtFS reply type %d",
133 pdu->id + 1);
135 return ret;
138 static ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset,
139 const char *fmt, va_list ap)
141 V9fsState *s = pdu->s;
142 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
143 VirtQueueElement *elem = v->elems[pdu->idx];
144 ssize_t ret;
146 ret = v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap);
147 if (ret < 0) {
148 VirtIODevice *vdev = VIRTIO_DEVICE(v);
150 virtio_error(vdev, "Failed to decode VirtFS request type %d", pdu->id);
152 return ret;
155 static void virtio_init_in_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
156 unsigned int *pniov, size_t size)
158 V9fsState *s = pdu->s;
159 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
160 VirtQueueElement *elem = v->elems[pdu->idx];
161 size_t buf_size = iov_size(elem->in_sg, elem->in_num);
163 if (buf_size < size) {
164 VirtIODevice *vdev = VIRTIO_DEVICE(v);
166 virtio_error(vdev,
167 "VirtFS reply type %d needs %zu bytes, buffer has %zu",
168 pdu->id + 1, size, buf_size);
171 *piov = elem->in_sg;
172 *pniov = elem->in_num;
175 static void virtio_init_out_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
176 unsigned int *pniov, size_t size)
178 V9fsState *s = pdu->s;
179 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
180 VirtQueueElement *elem = v->elems[pdu->idx];
181 size_t buf_size = iov_size(elem->out_sg, elem->out_num);
183 if (buf_size < size) {
184 VirtIODevice *vdev = VIRTIO_DEVICE(v);
186 virtio_error(vdev,
187 "VirtFS request type %d needs %zu bytes, buffer has %zu",
188 pdu->id, size, buf_size);
191 *piov = elem->out_sg;
192 *pniov = elem->out_num;
195 static const V9fsTransport virtio_9p_transport = {
196 .pdu_vmarshal = virtio_pdu_vmarshal,
197 .pdu_vunmarshal = virtio_pdu_vunmarshal,
198 .init_in_iov_from_pdu = virtio_init_in_iov_from_pdu,
199 .init_out_iov_from_pdu = virtio_init_out_iov_from_pdu,
200 .push_and_notify = virtio_9p_push_and_notify,
203 static void virtio_9p_device_realize(DeviceState *dev, Error **errp)
205 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
206 V9fsVirtioState *v = VIRTIO_9P(dev);
207 V9fsState *s = &v->state;
208 FsDriverEntry *fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
210 if (qtest_enabled() && fse) {
211 fse->export_flags |= V9FS_NO_PERF_WARN;
214 if (v9fs_device_realize_common(s, &virtio_9p_transport, errp)) {
215 return;
218 v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag);
219 virtio_init(vdev, VIRTIO_ID_9P, v->config_size);
220 v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output);
223 static void virtio_9p_device_unrealize(DeviceState *dev)
225 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
226 V9fsVirtioState *v = VIRTIO_9P(dev);
227 V9fsState *s = &v->state;
229 virtio_delete_queue(v->vq);
230 virtio_cleanup(vdev);
231 v9fs_device_unrealize_common(s);
234 /* virtio-9p device */
236 static const VMStateDescription vmstate_virtio_9p = {
237 .name = "virtio-9p",
238 .minimum_version_id = 1,
239 .version_id = 1,
240 .fields = (VMStateField[]) {
241 VMSTATE_VIRTIO_DEVICE,
242 VMSTATE_END_OF_LIST()
246 static Property virtio_9p_properties[] = {
247 DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag),
248 DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id),
249 DEFINE_PROP_END_OF_LIST(),
252 static void virtio_9p_class_init(ObjectClass *klass, void *data)
254 DeviceClass *dc = DEVICE_CLASS(klass);
255 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
257 device_class_set_props(dc, virtio_9p_properties);
258 dc->vmsd = &vmstate_virtio_9p;
259 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
260 vdc->realize = virtio_9p_device_realize;
261 vdc->unrealize = virtio_9p_device_unrealize;
262 vdc->get_features = virtio_9p_get_features;
263 vdc->get_config = virtio_9p_get_config;
264 vdc->reset = virtio_9p_reset;
267 static const TypeInfo virtio_device_info = {
268 .name = TYPE_VIRTIO_9P,
269 .parent = TYPE_VIRTIO_DEVICE,
270 .instance_size = sizeof(V9fsVirtioState),
271 .class_init = virtio_9p_class_init,
274 static void virtio_9p_register_types(void)
276 type_register_static(&virtio_device_info);
279 type_init(virtio_9p_register_types)