curl: remove compatibility code, require 7.29.0
[qemu/ar7.git] / hw / virtio / vhost-user-fs.c
blobed036ad9c13f06e2b1c1fcd5acb43d07456efb07
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/qdev-properties-system.h"
20 #include "hw/virtio/virtio-bus.h"
21 #include "hw/virtio/virtio-access.h"
22 #include "qemu/error-report.h"
23 #include "hw/virtio/vhost-user-fs.h"
24 #include "monitor/monitor.h"
26 static void vuf_get_config(VirtIODevice *vdev, uint8_t *config)
28 VHostUserFS *fs = VHOST_USER_FS(vdev);
29 struct virtio_fs_config fscfg = {};
31 memcpy((char *)fscfg.tag, fs->conf.tag,
32 MIN(strlen(fs->conf.tag) + 1, sizeof(fscfg.tag)));
34 virtio_stl_p(vdev, &fscfg.num_request_queues, fs->conf.num_request_queues);
36 memcpy(config, &fscfg, sizeof(fscfg));
39 static void vuf_start(VirtIODevice *vdev)
41 VHostUserFS *fs = VHOST_USER_FS(vdev);
42 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
43 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
44 int ret;
45 int i;
47 if (!k->set_guest_notifiers) {
48 error_report("binding does not support guest notifiers");
49 return;
52 ret = vhost_dev_enable_notifiers(&fs->vhost_dev, vdev);
53 if (ret < 0) {
54 error_report("Error enabling host notifiers: %d", -ret);
55 return;
58 ret = k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, true);
59 if (ret < 0) {
60 error_report("Error binding guest notifier: %d", -ret);
61 goto err_host_notifiers;
64 fs->vhost_dev.acked_features = vdev->guest_features;
65 ret = vhost_dev_start(&fs->vhost_dev, vdev);
66 if (ret < 0) {
67 error_report("Error starting vhost: %d", -ret);
68 goto err_guest_notifiers;
72 * guest_notifier_mask/pending not used yet, so just unmask
73 * everything here. virtio-pci will do the right thing by
74 * enabling/disabling irqfd.
76 for (i = 0; i < fs->vhost_dev.nvqs; i++) {
77 vhost_virtqueue_mask(&fs->vhost_dev, vdev, i, false);
80 return;
82 err_guest_notifiers:
83 k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, false);
84 err_host_notifiers:
85 vhost_dev_disable_notifiers(&fs->vhost_dev, vdev);
88 static void vuf_stop(VirtIODevice *vdev)
90 VHostUserFS *fs = VHOST_USER_FS(vdev);
91 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
92 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
93 int ret;
95 if (!k->set_guest_notifiers) {
96 return;
99 vhost_dev_stop(&fs->vhost_dev, vdev);
101 ret = k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, false);
102 if (ret < 0) {
103 error_report("vhost guest notifier cleanup failed: %d", ret);
104 return;
107 vhost_dev_disable_notifiers(&fs->vhost_dev, vdev);
110 static void vuf_set_status(VirtIODevice *vdev, uint8_t status)
112 VHostUserFS *fs = VHOST_USER_FS(vdev);
113 bool should_start = status & VIRTIO_CONFIG_S_DRIVER_OK;
115 if (!vdev->vm_running) {
116 should_start = false;
119 if (fs->vhost_dev.started == should_start) {
120 return;
123 if (should_start) {
124 vuf_start(vdev);
125 } else {
126 vuf_stop(vdev);
130 static uint64_t vuf_get_features(VirtIODevice *vdev,
131 uint64_t requested_features,
132 Error **errp)
134 /* No feature bits used yet */
135 return requested_features;
138 static void vuf_handle_output(VirtIODevice *vdev, VirtQueue *vq)
141 * Not normally called; it's the daemon that handles the queue;
142 * however virtio's cleanup path can call this.
146 static void vuf_guest_notifier_mask(VirtIODevice *vdev, int idx,
147 bool mask)
149 VHostUserFS *fs = VHOST_USER_FS(vdev);
151 vhost_virtqueue_mask(&fs->vhost_dev, vdev, idx, mask);
154 static bool vuf_guest_notifier_pending(VirtIODevice *vdev, int idx)
156 VHostUserFS *fs = VHOST_USER_FS(vdev);
158 return vhost_virtqueue_pending(&fs->vhost_dev, idx);
161 static void vuf_device_realize(DeviceState *dev, Error **errp)
163 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
164 VHostUserFS *fs = VHOST_USER_FS(dev);
165 unsigned int i;
166 size_t len;
167 int ret;
169 if (!fs->conf.chardev.chr) {
170 error_setg(errp, "missing chardev");
171 return;
174 if (!fs->conf.tag) {
175 error_setg(errp, "missing tag property");
176 return;
178 len = strlen(fs->conf.tag);
179 if (len == 0) {
180 error_setg(errp, "tag property cannot be empty");
181 return;
183 if (len > sizeof_field(struct virtio_fs_config, tag)) {
184 error_setg(errp, "tag property must be %zu bytes or less",
185 sizeof_field(struct virtio_fs_config, tag));
186 return;
189 if (fs->conf.num_request_queues == 0) {
190 error_setg(errp, "num-request-queues property must be larger than 0");
191 return;
194 if (!is_power_of_2(fs->conf.queue_size)) {
195 error_setg(errp, "queue-size property must be a power of 2");
196 return;
199 if (fs->conf.queue_size > VIRTQUEUE_MAX_SIZE) {
200 error_setg(errp, "queue-size property must be %u or smaller",
201 VIRTQUEUE_MAX_SIZE);
202 return;
205 if (!vhost_user_init(&fs->vhost_user, &fs->conf.chardev, errp)) {
206 return;
209 virtio_init(vdev, "vhost-user-fs", VIRTIO_ID_FS,
210 sizeof(struct virtio_fs_config));
212 /* Hiprio queue */
213 fs->hiprio_vq = virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output);
215 /* Request queues */
216 fs->req_vqs = g_new(VirtQueue *, fs->conf.num_request_queues);
217 for (i = 0; i < fs->conf.num_request_queues; i++) {
218 fs->req_vqs[i] = virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output);
221 /* 1 high prio queue, plus the number configured */
222 fs->vhost_dev.nvqs = 1 + fs->conf.num_request_queues;
223 fs->vhost_dev.vqs = g_new0(struct vhost_virtqueue, fs->vhost_dev.nvqs);
224 ret = vhost_dev_init(&fs->vhost_dev, &fs->vhost_user,
225 VHOST_BACKEND_TYPE_USER, 0);
226 if (ret < 0) {
227 error_setg_errno(errp, -ret, "vhost_dev_init failed");
228 goto err_virtio;
231 return;
233 err_virtio:
234 vhost_user_cleanup(&fs->vhost_user);
235 virtio_delete_queue(fs->hiprio_vq);
236 for (i = 0; i < fs->conf.num_request_queues; i++) {
237 virtio_delete_queue(fs->req_vqs[i]);
239 g_free(fs->req_vqs);
240 virtio_cleanup(vdev);
241 g_free(fs->vhost_dev.vqs);
242 return;
245 static void vuf_device_unrealize(DeviceState *dev)
247 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
248 VHostUserFS *fs = VHOST_USER_FS(dev);
249 int i;
251 /* This will stop vhost backend if appropriate. */
252 vuf_set_status(vdev, 0);
254 vhost_dev_cleanup(&fs->vhost_dev);
256 vhost_user_cleanup(&fs->vhost_user);
258 virtio_delete_queue(fs->hiprio_vq);
259 for (i = 0; i < fs->conf.num_request_queues; i++) {
260 virtio_delete_queue(fs->req_vqs[i]);
262 g_free(fs->req_vqs);
263 virtio_cleanup(vdev);
264 g_free(fs->vhost_dev.vqs);
265 fs->vhost_dev.vqs = NULL;
268 static const VMStateDescription vuf_vmstate = {
269 .name = "vhost-user-fs",
270 .unmigratable = 1,
273 static Property vuf_properties[] = {
274 DEFINE_PROP_CHR("chardev", VHostUserFS, conf.chardev),
275 DEFINE_PROP_STRING("tag", VHostUserFS, conf.tag),
276 DEFINE_PROP_UINT16("num-request-queues", VHostUserFS,
277 conf.num_request_queues, 1),
278 DEFINE_PROP_UINT16("queue-size", VHostUserFS, conf.queue_size, 128),
279 DEFINE_PROP_END_OF_LIST(),
282 static void vuf_class_init(ObjectClass *klass, void *data)
284 DeviceClass *dc = DEVICE_CLASS(klass);
285 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
287 device_class_set_props(dc, vuf_properties);
288 dc->vmsd = &vuf_vmstate;
289 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
290 vdc->realize = vuf_device_realize;
291 vdc->unrealize = vuf_device_unrealize;
292 vdc->get_features = vuf_get_features;
293 vdc->get_config = vuf_get_config;
294 vdc->set_status = vuf_set_status;
295 vdc->guest_notifier_mask = vuf_guest_notifier_mask;
296 vdc->guest_notifier_pending = vuf_guest_notifier_pending;
299 static const TypeInfo vuf_info = {
300 .name = TYPE_VHOST_USER_FS,
301 .parent = TYPE_VIRTIO_DEVICE,
302 .instance_size = sizeof(VHostUserFS),
303 .class_init = vuf_class_init,
306 static void vuf_register_types(void)
308 type_register_static(&vuf_info);
311 type_init(vuf_register_types)