fuzz: add documentation to docs/devel/
[qemu/ar7.git] / hw / virtio / vhost-user-fs.c
blob33b17848c214cbd9091fbbff4890857c89110d0a
1 /*
2 * Vhost-user filesystem virtio device
4 * Copyright 2018-2019 Red Hat, Inc.
6 * Authors:
7 * Stefan Hajnoczi <stefanha@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or
10 * (at your option) any later version. See the COPYING file in the
11 * top-level directory.
14 #include "qemu/osdep.h"
15 #include <sys/ioctl.h>
16 #include "standard-headers/linux/virtio_fs.h"
17 #include "qapi/error.h"
18 #include "hw/qdev-properties.h"
19 #include "hw/virtio/virtio-bus.h"
20 #include "hw/virtio/virtio-access.h"
21 #include "qemu/error-report.h"
22 #include "hw/virtio/vhost-user-fs.h"
23 #include "monitor/monitor.h"
25 static void vuf_get_config(VirtIODevice *vdev, uint8_t *config)
27 VHostUserFS *fs = VHOST_USER_FS(vdev);
28 struct virtio_fs_config fscfg = {};
30 memcpy((char *)fscfg.tag, fs->conf.tag,
31 MIN(strlen(fs->conf.tag) + 1, sizeof(fscfg.tag)));
33 virtio_stl_p(vdev, &fscfg.num_request_queues, fs->conf.num_request_queues);
35 memcpy(config, &fscfg, sizeof(fscfg));
38 static void vuf_start(VirtIODevice *vdev)
40 VHostUserFS *fs = VHOST_USER_FS(vdev);
41 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
42 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
43 int ret;
44 int i;
46 if (!k->set_guest_notifiers) {
47 error_report("binding does not support guest notifiers");
48 return;
51 ret = vhost_dev_enable_notifiers(&fs->vhost_dev, vdev);
52 if (ret < 0) {
53 error_report("Error enabling host notifiers: %d", -ret);
54 return;
57 ret = k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, true);
58 if (ret < 0) {
59 error_report("Error binding guest notifier: %d", -ret);
60 goto err_host_notifiers;
63 fs->vhost_dev.acked_features = vdev->guest_features;
64 ret = vhost_dev_start(&fs->vhost_dev, vdev);
65 if (ret < 0) {
66 error_report("Error starting vhost: %d", -ret);
67 goto err_guest_notifiers;
71 * guest_notifier_mask/pending not used yet, so just unmask
72 * everything here. virtio-pci will do the right thing by
73 * enabling/disabling irqfd.
75 for (i = 0; i < fs->vhost_dev.nvqs; i++) {
76 vhost_virtqueue_mask(&fs->vhost_dev, vdev, i, false);
79 return;
81 err_guest_notifiers:
82 k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, false);
83 err_host_notifiers:
84 vhost_dev_disable_notifiers(&fs->vhost_dev, vdev);
87 static void vuf_stop(VirtIODevice *vdev)
89 VHostUserFS *fs = VHOST_USER_FS(vdev);
90 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
91 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
92 int ret;
94 if (!k->set_guest_notifiers) {
95 return;
98 vhost_dev_stop(&fs->vhost_dev, vdev);
100 ret = k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, false);
101 if (ret < 0) {
102 error_report("vhost guest notifier cleanup failed: %d", ret);
103 return;
106 vhost_dev_disable_notifiers(&fs->vhost_dev, vdev);
109 static void vuf_set_status(VirtIODevice *vdev, uint8_t status)
111 VHostUserFS *fs = VHOST_USER_FS(vdev);
112 bool should_start = status & VIRTIO_CONFIG_S_DRIVER_OK;
114 if (!vdev->vm_running) {
115 should_start = false;
118 if (fs->vhost_dev.started == should_start) {
119 return;
122 if (should_start) {
123 vuf_start(vdev);
124 } else {
125 vuf_stop(vdev);
129 static uint64_t vuf_get_features(VirtIODevice *vdev,
130 uint64_t requested_features,
131 Error **errp)
133 /* No feature bits used yet */
134 return requested_features;
137 static void vuf_handle_output(VirtIODevice *vdev, VirtQueue *vq)
140 * Not normally called; it's the daemon that handles the queue;
141 * however virtio's cleanup path can call this.
145 static void vuf_guest_notifier_mask(VirtIODevice *vdev, int idx,
146 bool mask)
148 VHostUserFS *fs = VHOST_USER_FS(vdev);
150 vhost_virtqueue_mask(&fs->vhost_dev, vdev, idx, mask);
153 static bool vuf_guest_notifier_pending(VirtIODevice *vdev, int idx)
155 VHostUserFS *fs = VHOST_USER_FS(vdev);
157 return vhost_virtqueue_pending(&fs->vhost_dev, idx);
160 static void vuf_device_realize(DeviceState *dev, Error **errp)
162 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
163 VHostUserFS *fs = VHOST_USER_FS(dev);
164 unsigned int i;
165 size_t len;
166 int ret;
168 if (!fs->conf.chardev.chr) {
169 error_setg(errp, "missing chardev");
170 return;
173 if (!fs->conf.tag) {
174 error_setg(errp, "missing tag property");
175 return;
177 len = strlen(fs->conf.tag);
178 if (len == 0) {
179 error_setg(errp, "tag property cannot be empty");
180 return;
182 if (len > sizeof_field(struct virtio_fs_config, tag)) {
183 error_setg(errp, "tag property must be %zu bytes or less",
184 sizeof_field(struct virtio_fs_config, tag));
185 return;
188 if (fs->conf.num_request_queues == 0) {
189 error_setg(errp, "num-request-queues property must be larger than 0");
190 return;
193 if (!is_power_of_2(fs->conf.queue_size)) {
194 error_setg(errp, "queue-size property must be a power of 2");
195 return;
198 if (fs->conf.queue_size > VIRTQUEUE_MAX_SIZE) {
199 error_setg(errp, "queue-size property must be %u or smaller",
200 VIRTQUEUE_MAX_SIZE);
201 return;
204 if (!vhost_user_init(&fs->vhost_user, &fs->conf.chardev, errp)) {
205 return;
208 virtio_init(vdev, "vhost-user-fs", VIRTIO_ID_FS,
209 sizeof(struct virtio_fs_config));
211 /* Hiprio queue */
212 virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output);
214 /* Request queues */
215 for (i = 0; i < fs->conf.num_request_queues; i++) {
216 virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output);
219 /* 1 high prio queue, plus the number configured */
220 fs->vhost_dev.nvqs = 1 + fs->conf.num_request_queues;
221 fs->vhost_dev.vqs = g_new0(struct vhost_virtqueue, fs->vhost_dev.nvqs);
222 ret = vhost_dev_init(&fs->vhost_dev, &fs->vhost_user,
223 VHOST_BACKEND_TYPE_USER, 0);
224 if (ret < 0) {
225 error_setg_errno(errp, -ret, "vhost_dev_init failed");
226 goto err_virtio;
229 return;
231 err_virtio:
232 vhost_user_cleanup(&fs->vhost_user);
233 virtio_cleanup(vdev);
234 g_free(fs->vhost_dev.vqs);
235 return;
238 static void vuf_device_unrealize(DeviceState *dev, Error **errp)
240 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
241 VHostUserFS *fs = VHOST_USER_FS(dev);
243 /* This will stop vhost backend if appropriate. */
244 vuf_set_status(vdev, 0);
246 vhost_dev_cleanup(&fs->vhost_dev);
248 vhost_user_cleanup(&fs->vhost_user);
250 virtio_cleanup(vdev);
251 g_free(fs->vhost_dev.vqs);
252 fs->vhost_dev.vqs = NULL;
255 static const VMStateDescription vuf_vmstate = {
256 .name = "vhost-user-fs",
257 .unmigratable = 1,
260 static Property vuf_properties[] = {
261 DEFINE_PROP_CHR("chardev", VHostUserFS, conf.chardev),
262 DEFINE_PROP_STRING("tag", VHostUserFS, conf.tag),
263 DEFINE_PROP_UINT16("num-request-queues", VHostUserFS,
264 conf.num_request_queues, 1),
265 DEFINE_PROP_UINT16("queue-size", VHostUserFS, conf.queue_size, 128),
266 DEFINE_PROP_END_OF_LIST(),
269 static void vuf_class_init(ObjectClass *klass, void *data)
271 DeviceClass *dc = DEVICE_CLASS(klass);
272 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
274 device_class_set_props(dc, vuf_properties);
275 dc->vmsd = &vuf_vmstate;
276 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
277 vdc->realize = vuf_device_realize;
278 vdc->unrealize = vuf_device_unrealize;
279 vdc->get_features = vuf_get_features;
280 vdc->get_config = vuf_get_config;
281 vdc->set_status = vuf_set_status;
282 vdc->guest_notifier_mask = vuf_guest_notifier_mask;
283 vdc->guest_notifier_pending = vuf_guest_notifier_pending;
286 static const TypeInfo vuf_info = {
287 .name = TYPE_VHOST_USER_FS,
288 .parent = TYPE_VIRTIO_DEVICE,
289 .instance_size = sizeof(VHostUserFS),
290 .class_init = vuf_class_init,
293 static void vuf_register_types(void)
295 type_register_static(&vuf_info);
298 type_init(vuf_register_types)