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-user-fs.h"
24 #include "monitor/monitor.h"
25 #include "sysemu/sysemu.h"
27 static const int user_feature_bits
[] = {
29 VIRTIO_RING_F_INDIRECT_DESC
,
30 VIRTIO_RING_F_EVENT_IDX
,
31 VIRTIO_F_NOTIFY_ON_EMPTY
,
33 VIRTIO_F_IOMMU_PLATFORM
,
35 VHOST_INVALID_FEATURE_BIT
38 static void vuf_get_config(VirtIODevice
*vdev
, uint8_t *config
)
40 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
41 struct virtio_fs_config fscfg
= {};
43 memcpy((char *)fscfg
.tag
, fs
->conf
.tag
,
44 MIN(strlen(fs
->conf
.tag
) + 1, sizeof(fscfg
.tag
)));
46 virtio_stl_p(vdev
, &fscfg
.num_request_queues
, fs
->conf
.num_request_queues
);
48 memcpy(config
, &fscfg
, sizeof(fscfg
));
51 static void vuf_start(VirtIODevice
*vdev
)
53 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
54 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
55 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
59 if (!k
->set_guest_notifiers
) {
60 error_report("binding does not support guest notifiers");
64 ret
= vhost_dev_enable_notifiers(&fs
->vhost_dev
, vdev
);
66 error_report("Error enabling host notifiers: %d", -ret
);
70 ret
= k
->set_guest_notifiers(qbus
->parent
, fs
->vhost_dev
.nvqs
, true);
72 error_report("Error binding guest notifier: %d", -ret
);
73 goto err_host_notifiers
;
76 fs
->vhost_dev
.acked_features
= vdev
->guest_features
;
77 ret
= vhost_dev_start(&fs
->vhost_dev
, vdev
);
79 error_report("Error starting vhost: %d", -ret
);
80 goto err_guest_notifiers
;
84 * guest_notifier_mask/pending not used yet, so just unmask
85 * everything here. virtio-pci will do the right thing by
86 * enabling/disabling irqfd.
88 for (i
= 0; i
< fs
->vhost_dev
.nvqs
; i
++) {
89 vhost_virtqueue_mask(&fs
->vhost_dev
, vdev
, i
, false);
95 k
->set_guest_notifiers(qbus
->parent
, fs
->vhost_dev
.nvqs
, false);
97 vhost_dev_disable_notifiers(&fs
->vhost_dev
, vdev
);
100 static void vuf_stop(VirtIODevice
*vdev
)
102 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
103 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
104 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
107 if (!k
->set_guest_notifiers
) {
111 vhost_dev_stop(&fs
->vhost_dev
, vdev
);
113 ret
= k
->set_guest_notifiers(qbus
->parent
, fs
->vhost_dev
.nvqs
, false);
115 error_report("vhost guest notifier cleanup failed: %d", ret
);
119 vhost_dev_disable_notifiers(&fs
->vhost_dev
, vdev
);
122 static void vuf_set_status(VirtIODevice
*vdev
, uint8_t status
)
124 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
125 bool should_start
= status
& VIRTIO_CONFIG_S_DRIVER_OK
;
127 if (!vdev
->vm_running
) {
128 should_start
= false;
131 if (fs
->vhost_dev
.started
== should_start
) {
142 static uint64_t vuf_get_features(VirtIODevice
*vdev
,
146 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
148 return vhost_get_features(&fs
->vhost_dev
, user_feature_bits
, features
);
151 static void vuf_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
154 * Not normally called; it's the daemon that handles the queue;
155 * however virtio's cleanup path can call this.
159 static void vuf_guest_notifier_mask(VirtIODevice
*vdev
, int idx
,
162 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
164 if (idx
== VIRTIO_CONFIG_IRQ_IDX
) {
167 vhost_virtqueue_mask(&fs
->vhost_dev
, vdev
, idx
, mask
);
170 static bool vuf_guest_notifier_pending(VirtIODevice
*vdev
, int idx
)
172 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
174 if (idx
== VIRTIO_CONFIG_IRQ_IDX
) {
177 return vhost_virtqueue_pending(&fs
->vhost_dev
, idx
);
180 static void vuf_device_realize(DeviceState
*dev
, Error
**errp
)
182 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
183 VHostUserFS
*fs
= VHOST_USER_FS(dev
);
188 if (!fs
->conf
.chardev
.chr
) {
189 error_setg(errp
, "missing chardev");
194 error_setg(errp
, "missing tag property");
197 len
= strlen(fs
->conf
.tag
);
199 error_setg(errp
, "tag property cannot be empty");
202 if (len
> sizeof_field(struct virtio_fs_config
, tag
)) {
203 error_setg(errp
, "tag property must be %zu bytes or less",
204 sizeof_field(struct virtio_fs_config
, tag
));
208 if (fs
->conf
.num_request_queues
== 0) {
209 error_setg(errp
, "num-request-queues property must be larger than 0");
213 if (!is_power_of_2(fs
->conf
.queue_size
)) {
214 error_setg(errp
, "queue-size property must be a power of 2");
218 if (fs
->conf
.queue_size
> VIRTQUEUE_MAX_SIZE
) {
219 error_setg(errp
, "queue-size property must be %u or smaller",
224 if (!vhost_user_init(&fs
->vhost_user
, &fs
->conf
.chardev
, errp
)) {
228 virtio_init(vdev
, "vhost-user-fs", VIRTIO_ID_FS
,
229 sizeof(struct virtio_fs_config
));
232 fs
->hiprio_vq
= virtio_add_queue(vdev
, fs
->conf
.queue_size
, vuf_handle_output
);
235 fs
->req_vqs
= g_new(VirtQueue
*, fs
->conf
.num_request_queues
);
236 for (i
= 0; i
< fs
->conf
.num_request_queues
; i
++) {
237 fs
->req_vqs
[i
] = virtio_add_queue(vdev
, fs
->conf
.queue_size
, vuf_handle_output
);
240 /* 1 high prio queue, plus the number configured */
241 fs
->vhost_dev
.nvqs
= 1 + fs
->conf
.num_request_queues
;
242 fs
->vhost_dev
.vqs
= g_new0(struct vhost_virtqueue
, fs
->vhost_dev
.nvqs
);
243 ret
= vhost_dev_init(&fs
->vhost_dev
, &fs
->vhost_user
,
244 VHOST_BACKEND_TYPE_USER
, 0, errp
);
252 vhost_user_cleanup(&fs
->vhost_user
);
253 virtio_delete_queue(fs
->hiprio_vq
);
254 for (i
= 0; i
< fs
->conf
.num_request_queues
; i
++) {
255 virtio_delete_queue(fs
->req_vqs
[i
]);
258 virtio_cleanup(vdev
);
259 g_free(fs
->vhost_dev
.vqs
);
263 static void vuf_device_unrealize(DeviceState
*dev
)
265 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
266 VHostUserFS
*fs
= VHOST_USER_FS(dev
);
269 /* This will stop vhost backend if appropriate. */
270 vuf_set_status(vdev
, 0);
272 vhost_dev_cleanup(&fs
->vhost_dev
);
274 vhost_user_cleanup(&fs
->vhost_user
);
276 virtio_delete_queue(fs
->hiprio_vq
);
277 for (i
= 0; i
< fs
->conf
.num_request_queues
; i
++) {
278 virtio_delete_queue(fs
->req_vqs
[i
]);
281 virtio_cleanup(vdev
);
282 g_free(fs
->vhost_dev
.vqs
);
283 fs
->vhost_dev
.vqs
= NULL
;
286 static const VMStateDescription vuf_vmstate
= {
287 .name
= "vhost-user-fs",
291 static Property vuf_properties
[] = {
292 DEFINE_PROP_CHR("chardev", VHostUserFS
, conf
.chardev
),
293 DEFINE_PROP_STRING("tag", VHostUserFS
, conf
.tag
),
294 DEFINE_PROP_UINT16("num-request-queues", VHostUserFS
,
295 conf
.num_request_queues
, 1),
296 DEFINE_PROP_UINT16("queue-size", VHostUserFS
, conf
.queue_size
, 128),
297 DEFINE_PROP_END_OF_LIST(),
300 static void vuf_instance_init(Object
*obj
)
302 VHostUserFS
*fs
= VHOST_USER_FS(obj
);
304 device_add_bootindex_property(obj
, &fs
->bootindex
, "bootindex",
305 "/filesystem@0", DEVICE(obj
));
308 static void vuf_class_init(ObjectClass
*klass
, void *data
)
310 DeviceClass
*dc
= DEVICE_CLASS(klass
);
311 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
313 device_class_set_props(dc
, vuf_properties
);
314 dc
->vmsd
= &vuf_vmstate
;
315 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
316 vdc
->realize
= vuf_device_realize
;
317 vdc
->unrealize
= vuf_device_unrealize
;
318 vdc
->get_features
= vuf_get_features
;
319 vdc
->get_config
= vuf_get_config
;
320 vdc
->set_status
= vuf_set_status
;
321 vdc
->guest_notifier_mask
= vuf_guest_notifier_mask
;
322 vdc
->guest_notifier_pending
= vuf_guest_notifier_pending
;
325 static const TypeInfo vuf_info
= {
326 .name
= TYPE_VHOST_USER_FS
,
327 .parent
= TYPE_VIRTIO_DEVICE
,
328 .instance_size
= sizeof(VHostUserFS
),
329 .instance_init
= vuf_instance_init
,
330 .class_init
= vuf_class_init
,
333 static void vuf_register_types(void)
335 type_register_static(&vuf_info
);
338 type_init(vuf_register_types
)