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