2 * Vhost-user filesystem virtio device
4 * Copyright 2018-2019 Red Hat, Inc.
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.h"
24 #include "hw/virtio/vhost-user-fs.h"
25 #include "monitor/monitor.h"
26 #include "sysemu/sysemu.h"
28 static const int user_feature_bits
[] = {
30 VIRTIO_RING_F_INDIRECT_DESC
,
31 VIRTIO_RING_F_EVENT_IDX
,
32 VIRTIO_F_NOTIFY_ON_EMPTY
,
34 VIRTIO_F_IOMMU_PLATFORM
,
36 VHOST_INVALID_FEATURE_BIT
39 static void vuf_get_config(VirtIODevice
*vdev
, uint8_t *config
)
41 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
42 struct virtio_fs_config fscfg
= {};
44 memcpy((char *)fscfg
.tag
, fs
->conf
.tag
,
45 MIN(strlen(fs
->conf
.tag
) + 1, sizeof(fscfg
.tag
)));
47 virtio_stl_p(vdev
, &fscfg
.num_request_queues
, fs
->conf
.num_request_queues
);
49 memcpy(config
, &fscfg
, sizeof(fscfg
));
52 static void vuf_start(VirtIODevice
*vdev
)
54 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
55 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
56 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
60 if (!k
->set_guest_notifiers
) {
61 error_report("binding does not support guest notifiers");
65 ret
= vhost_dev_enable_notifiers(&fs
->vhost_dev
, vdev
);
67 error_report("Error enabling host notifiers: %d", -ret
);
71 ret
= k
->set_guest_notifiers(qbus
->parent
, fs
->vhost_dev
.nvqs
, true);
73 error_report("Error binding guest notifier: %d", -ret
);
74 goto err_host_notifiers
;
77 fs
->vhost_dev
.acked_features
= vdev
->guest_features
;
78 ret
= vhost_dev_start(&fs
->vhost_dev
, vdev
);
80 error_report("Error starting vhost: %d", -ret
);
81 goto err_guest_notifiers
;
85 * guest_notifier_mask/pending not used yet, so just unmask
86 * everything here. virtio-pci will do the right thing by
87 * enabling/disabling irqfd.
89 for (i
= 0; i
< fs
->vhost_dev
.nvqs
; i
++) {
90 vhost_virtqueue_mask(&fs
->vhost_dev
, vdev
, i
, false);
96 k
->set_guest_notifiers(qbus
->parent
, fs
->vhost_dev
.nvqs
, false);
98 vhost_dev_disable_notifiers(&fs
->vhost_dev
, vdev
);
101 static void vuf_stop(VirtIODevice
*vdev
)
103 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
104 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
105 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
108 if (!k
->set_guest_notifiers
) {
112 vhost_dev_stop(&fs
->vhost_dev
, vdev
);
114 ret
= k
->set_guest_notifiers(qbus
->parent
, fs
->vhost_dev
.nvqs
, false);
116 error_report("vhost guest notifier cleanup failed: %d", ret
);
120 vhost_dev_disable_notifiers(&fs
->vhost_dev
, vdev
);
123 static void vuf_set_status(VirtIODevice
*vdev
, uint8_t status
)
125 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
126 bool should_start
= virtio_device_started(vdev
, status
);
128 if (vhost_dev_is_started(&fs
->vhost_dev
) == should_start
) {
139 static uint64_t vuf_get_features(VirtIODevice
*vdev
,
143 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
145 return vhost_get_features(&fs
->vhost_dev
, user_feature_bits
, features
);
148 static void vuf_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
151 * Not normally called; it's the daemon that handles the queue;
152 * however virtio's cleanup path can call this.
156 static void vuf_guest_notifier_mask(VirtIODevice
*vdev
, int idx
,
159 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
161 vhost_virtqueue_mask(&fs
->vhost_dev
, vdev
, idx
, mask
);
164 static bool vuf_guest_notifier_pending(VirtIODevice
*vdev
, int idx
)
166 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
168 return vhost_virtqueue_pending(&fs
->vhost_dev
, idx
);
171 static void vuf_device_realize(DeviceState
*dev
, Error
**errp
)
173 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
174 VHostUserFS
*fs
= VHOST_USER_FS(dev
);
179 if (!fs
->conf
.chardev
.chr
) {
180 error_setg(errp
, "missing chardev");
185 error_setg(errp
, "missing tag property");
188 len
= strlen(fs
->conf
.tag
);
190 error_setg(errp
, "tag property cannot be empty");
193 if (len
> sizeof_field(struct virtio_fs_config
, tag
)) {
194 error_setg(errp
, "tag property must be %zu bytes or less",
195 sizeof_field(struct virtio_fs_config
, tag
));
199 if (fs
->conf
.num_request_queues
== 0) {
200 error_setg(errp
, "num-request-queues property must be larger than 0");
204 if (!is_power_of_2(fs
->conf
.queue_size
)) {
205 error_setg(errp
, "queue-size property must be a power of 2");
209 if (fs
->conf
.queue_size
> VIRTQUEUE_MAX_SIZE
) {
210 error_setg(errp
, "queue-size property must be %u or smaller",
215 if (!vhost_user_init(&fs
->vhost_user
, &fs
->conf
.chardev
, errp
)) {
219 virtio_init(vdev
, VIRTIO_ID_FS
, sizeof(struct virtio_fs_config
));
222 fs
->hiprio_vq
= virtio_add_queue(vdev
, fs
->conf
.queue_size
, vuf_handle_output
);
225 fs
->req_vqs
= g_new(VirtQueue
*, fs
->conf
.num_request_queues
);
226 for (i
= 0; i
< fs
->conf
.num_request_queues
; i
++) {
227 fs
->req_vqs
[i
] = virtio_add_queue(vdev
, fs
->conf
.queue_size
, vuf_handle_output
);
230 /* 1 high prio queue, plus the number configured */
231 fs
->vhost_dev
.nvqs
= 1 + fs
->conf
.num_request_queues
;
232 fs
->vhost_dev
.vqs
= g_new0(struct vhost_virtqueue
, fs
->vhost_dev
.nvqs
);
233 ret
= vhost_dev_init(&fs
->vhost_dev
, &fs
->vhost_user
,
234 VHOST_BACKEND_TYPE_USER
, 0, errp
);
242 vhost_user_cleanup(&fs
->vhost_user
);
243 virtio_delete_queue(fs
->hiprio_vq
);
244 for (i
= 0; i
< fs
->conf
.num_request_queues
; i
++) {
245 virtio_delete_queue(fs
->req_vqs
[i
]);
248 virtio_cleanup(vdev
);
249 g_free(fs
->vhost_dev
.vqs
);
253 static void vuf_device_unrealize(DeviceState
*dev
)
255 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
256 VHostUserFS
*fs
= VHOST_USER_FS(dev
);
259 /* This will stop vhost backend if appropriate. */
260 vuf_set_status(vdev
, 0);
262 vhost_dev_cleanup(&fs
->vhost_dev
);
264 vhost_user_cleanup(&fs
->vhost_user
);
266 virtio_delete_queue(fs
->hiprio_vq
);
267 for (i
= 0; i
< fs
->conf
.num_request_queues
; i
++) {
268 virtio_delete_queue(fs
->req_vqs
[i
]);
271 virtio_cleanup(vdev
);
272 g_free(fs
->vhost_dev
.vqs
);
273 fs
->vhost_dev
.vqs
= NULL
;
276 static struct vhost_dev
*vuf_get_vhost(VirtIODevice
*vdev
)
278 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
279 return &fs
->vhost_dev
;
282 static const VMStateDescription vuf_vmstate
= {
283 .name
= "vhost-user-fs",
287 static Property vuf_properties
[] = {
288 DEFINE_PROP_CHR("chardev", VHostUserFS
, conf
.chardev
),
289 DEFINE_PROP_STRING("tag", VHostUserFS
, conf
.tag
),
290 DEFINE_PROP_UINT16("num-request-queues", VHostUserFS
,
291 conf
.num_request_queues
, 1),
292 DEFINE_PROP_UINT16("queue-size", VHostUserFS
, conf
.queue_size
, 128),
293 DEFINE_PROP_END_OF_LIST(),
296 static void vuf_instance_init(Object
*obj
)
298 VHostUserFS
*fs
= VHOST_USER_FS(obj
);
300 device_add_bootindex_property(obj
, &fs
->bootindex
, "bootindex",
301 "/filesystem@0", DEVICE(obj
));
304 static void vuf_class_init(ObjectClass
*klass
, void *data
)
306 DeviceClass
*dc
= DEVICE_CLASS(klass
);
307 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
309 device_class_set_props(dc
, vuf_properties
);
310 dc
->vmsd
= &vuf_vmstate
;
311 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
312 vdc
->realize
= vuf_device_realize
;
313 vdc
->unrealize
= vuf_device_unrealize
;
314 vdc
->get_features
= vuf_get_features
;
315 vdc
->get_config
= vuf_get_config
;
316 vdc
->set_status
= vuf_set_status
;
317 vdc
->guest_notifier_mask
= vuf_guest_notifier_mask
;
318 vdc
->guest_notifier_pending
= vuf_guest_notifier_pending
;
319 vdc
->get_vhost
= vuf_get_vhost
;
322 static const TypeInfo vuf_info
= {
323 .name
= TYPE_VHOST_USER_FS
,
324 .parent
= TYPE_VIRTIO_DEVICE
,
325 .instance_size
= sizeof(VHostUserFS
),
326 .instance_init
= vuf_instance_init
,
327 .class_init
= vuf_class_init
,
330 static void vuf_register_types(void)
332 type_register_static(&vuf_info
);
335 type_init(vuf_register_types
)