]> git.proxmox.com Git - mirror_qemu.git/blame - hw/9pfs/virtio-9p-device.c
9pfs: fix memory leak in v9fs_xattrcreate
[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"
d64ccb91 20#include "hw/virtio/virtio-access.h"
0192cc5d 21#include "qemu/iov.h"
f4f61d27 22
0d3716b4
WL
23void virtio_9p_push_and_notify(V9fsPDU *pdu)
24{
25 V9fsState *s = pdu->s;
00588a0a 26 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
51b19ebe 27 VirtQueueElement *elem = v->elems[pdu->idx];
0d3716b4
WL
28
29 /* push onto queue and notify */
00588a0a 30 virtqueue_push(v->vq, elem, pdu->size);
51b19ebe
PB
31 g_free(elem);
32 v->elems[pdu->idx] = NULL;
0d3716b4
WL
33
34 /* FIXME: we should batch these completions */
00588a0a 35 virtio_notify(VIRTIO_DEVICE(v), v->vq);
0d3716b4
WL
36}
37
0192cc5d
WL
38static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
39{
00588a0a
WL
40 V9fsVirtioState *v = (V9fsVirtioState *)vdev;
41 V9fsState *s = &v->state;
0192cc5d
WL
42 V9fsPDU *pdu;
43 ssize_t len;
d3d74d6f 44 VirtQueueElement *elem;
0192cc5d 45
00588a0a 46 while ((pdu = pdu_alloc(s))) {
0192cc5d
WL
47 struct {
48 uint32_t size_le;
49 uint8_t id;
50 uint16_t tag_le;
51 } QEMU_PACKED out;
0192cc5d 52
51b19ebe
PB
53 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
54 if (!elem) {
d3d74d6f 55 goto out_free_pdu;
00588a0a
WL
56 }
57
d3d74d6f
GK
58 if (elem->in_num == 0) {
59 virtio_error(vdev,
60 "The guest sent a VirtFS request without space for "
61 "the reply");
62 goto out_free_req;
63 }
e8582891 64 QEMU_BUILD_BUG_ON(sizeof(out) != 7);
0192cc5d 65
51b19ebe 66 v->elems[pdu->idx] = elem;
00588a0a 67 len = iov_to_buf(elem->out_sg, elem->out_num, 0,
e8582891 68 &out, sizeof(out));
d3d74d6f
GK
69 if (len != sizeof(out)) {
70 virtio_error(vdev, "The guest sent a malformed VirtFS request: "
71 "header size is %zd, should be 7", len);
72 goto out_free_req;
73 }
0192cc5d
WL
74
75 pdu->size = le32_to_cpu(out.size_le);
76
77 pdu->id = out.id;
78 pdu->tag = le16_to_cpu(out.tag_le);
79
80 qemu_co_queue_init(&pdu->complete);
81 pdu_submit(pdu);
82 }
d3d74d6f
GK
83
84 return;
85
86out_free_req:
87 virtqueue_detach_element(vq, elem, 0);
88 g_free(elem);
89out_free_pdu:
90 pdu_free(pdu);
0192cc5d
WL
91}
92
9d5b731d
JW
93static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features,
94 Error **errp)
f4f61d27 95{
0cd09c3a 96 virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG);
f4f61d27
AK
97 return features;
98}
99
f4f61d27
AK
100static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
101{
e9a0152b 102 int len;
f4f61d27 103 struct virtio_9p_config *cfg;
00588a0a
WL
104 V9fsVirtioState *v = VIRTIO_9P(vdev);
105 V9fsState *s = &v->state;
f4f61d27 106
e9a0152b
AK
107 len = strlen(s->tag);
108 cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
d64ccb91 109 virtio_stw_p(vdev, &cfg->tag_len, len);
e9a0152b
AK
110 /* We don't copy the terminating null to config space */
111 memcpy(cfg->tag, s->tag, len);
00588a0a 112 memcpy(config, cfg, v->config_size);
7267c094 113 g_free(cfg);
f4f61d27
AK
114}
115
59be7522 116static void virtio_9p_device_realize(DeviceState *dev, Error **errp)
0174fe73 117{
59be7522 118 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
00588a0a
WL
119 V9fsVirtioState *v = VIRTIO_9P(dev);
120 V9fsState *s = &v->state;
f4f61d27 121
2a0c56aa 122 if (v9fs_device_realize_common(s, errp)) {
92304bf3 123 goto out;
f4f61d27 124 }
e9a0152b 125
00588a0a
WL
126 v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag);
127 virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P, v->config_size);
128 v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output);
2a0c56aa 129
92304bf3 130out:
2a0c56aa 131 return;
e7303c43
FK
132}
133
6cecf093
GK
134static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp)
135{
136 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
00588a0a
WL
137 V9fsVirtioState *v = VIRTIO_9P(dev);
138 V9fsState *s = &v->state;
6cecf093
GK
139
140 virtio_cleanup(vdev);
2a0c56aa 141 v9fs_device_unrealize_common(s, errp);
6cecf093
GK
142}
143
0e44a0fd
GK
144static void virtio_9p_reset(VirtIODevice *vdev)
145{
146 V9fsVirtioState *v = (V9fsVirtioState *)vdev;
147
148 v9fs_reset(&v->state);
149}
150
fe9fa96d
WL
151ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset,
152 const char *fmt, va_list ap)
153{
00588a0a
WL
154 V9fsState *s = pdu->s;
155 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
51b19ebe 156 VirtQueueElement *elem = v->elems[pdu->idx];
00588a0a
WL
157
158 return v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap);
fe9fa96d
WL
159}
160
161ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset,
162 const char *fmt, va_list ap)
163{
00588a0a
WL
164 V9fsState *s = pdu->s;
165 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
51b19ebe 166 VirtQueueElement *elem = v->elems[pdu->idx];
00588a0a
WL
167
168 return v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap);
fe9fa96d
WL
169}
170
592707af
WL
171void virtio_init_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
172 unsigned int *pniov, bool is_write)
173{
00588a0a
WL
174 V9fsState *s = pdu->s;
175 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
51b19ebe 176 VirtQueueElement *elem = v->elems[pdu->idx];
00588a0a 177
592707af 178 if (is_write) {
00588a0a
WL
179 *piov = elem->out_sg;
180 *pniov = elem->out_num;
592707af 181 } else {
00588a0a
WL
182 *piov = elem->in_sg;
183 *pniov = elem->in_num;
592707af
WL
184 }
185}
186
e7303c43
FK
187/* virtio-9p device */
188
dcaf8dda
HP
189static const VMStateDescription vmstate_virtio_9p = {
190 .name = "virtio-9p",
191 .minimum_version_id = 1,
192 .version_id = 1,
193 .fields = (VMStateField[]) {
194 VMSTATE_VIRTIO_DEVICE,
195 VMSTATE_END_OF_LIST()
196 },
197};
18e0e5b2 198
e7303c43 199static Property virtio_9p_properties[] = {
00588a0a
WL
200 DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag),
201 DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id),
e7303c43
FK
202 DEFINE_PROP_END_OF_LIST(),
203};
204
205static void virtio_9p_class_init(ObjectClass *klass, void *data)
206{
207 DeviceClass *dc = DEVICE_CLASS(klass);
208 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
59be7522 209
e7303c43 210 dc->props = virtio_9p_properties;
18e0e5b2 211 dc->vmsd = &vmstate_virtio_9p;
125ee0ed 212 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
59be7522 213 vdc->realize = virtio_9p_device_realize;
6cecf093 214 vdc->unrealize = virtio_9p_device_unrealize;
e7303c43
FK
215 vdc->get_features = virtio_9p_get_features;
216 vdc->get_config = virtio_9p_get_config;
0e44a0fd 217 vdc->reset = virtio_9p_reset;
e7303c43
FK
218}
219
220static const TypeInfo virtio_device_info = {
221 .name = TYPE_VIRTIO_9P,
222 .parent = TYPE_VIRTIO_DEVICE,
00588a0a 223 .instance_size = sizeof(V9fsVirtioState),
e7303c43
FK
224 .class_init = virtio_9p_class_init,
225};
226
227static void virtio_9p_register_types(void)
228{
229 type_register_static(&virtio_device_info);
230}
231
232type_init(virtio_9p_register_types)