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