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 void vuf_get_config(VirtIODevice
*vdev
, uint8_t *config
)
29 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
30 struct virtio_fs_config fscfg
= {};
32 memcpy((char *)fscfg
.tag
, fs
->conf
.tag
,
33 MIN(strlen(fs
->conf
.tag
) + 1, sizeof(fscfg
.tag
)));
35 virtio_stl_p(vdev
, &fscfg
.num_request_queues
, fs
->conf
.num_request_queues
);
37 memcpy(config
, &fscfg
, sizeof(fscfg
));
40 static void vuf_start(VirtIODevice
*vdev
)
42 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
43 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
44 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
48 if (!k
->set_guest_notifiers
) {
49 error_report("binding does not support guest notifiers");
53 ret
= vhost_dev_enable_notifiers(&fs
->vhost_dev
, vdev
);
55 error_report("Error enabling host notifiers: %d", -ret
);
59 ret
= k
->set_guest_notifiers(qbus
->parent
, fs
->vhost_dev
.nvqs
, true);
61 error_report("Error binding guest notifier: %d", -ret
);
62 goto err_host_notifiers
;
65 fs
->vhost_dev
.acked_features
= vdev
->guest_features
;
66 ret
= vhost_dev_start(&fs
->vhost_dev
, vdev
);
68 error_report("Error starting vhost: %d", -ret
);
69 goto err_guest_notifiers
;
73 * guest_notifier_mask/pending not used yet, so just unmask
74 * everything here. virtio-pci will do the right thing by
75 * enabling/disabling irqfd.
77 for (i
= 0; i
< fs
->vhost_dev
.nvqs
; i
++) {
78 vhost_virtqueue_mask(&fs
->vhost_dev
, vdev
, i
, false);
84 k
->set_guest_notifiers(qbus
->parent
, fs
->vhost_dev
.nvqs
, false);
86 vhost_dev_disable_notifiers(&fs
->vhost_dev
, vdev
);
89 static void vuf_stop(VirtIODevice
*vdev
)
91 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
92 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
93 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
96 if (!k
->set_guest_notifiers
) {
100 vhost_dev_stop(&fs
->vhost_dev
, vdev
);
102 ret
= k
->set_guest_notifiers(qbus
->parent
, fs
->vhost_dev
.nvqs
, false);
104 error_report("vhost guest notifier cleanup failed: %d", ret
);
108 vhost_dev_disable_notifiers(&fs
->vhost_dev
, vdev
);
111 static void vuf_set_status(VirtIODevice
*vdev
, uint8_t status
)
113 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
114 bool should_start
= status
& VIRTIO_CONFIG_S_DRIVER_OK
;
116 if (!vdev
->vm_running
) {
117 should_start
= false;
120 if (fs
->vhost_dev
.started
== should_start
) {
131 static uint64_t vuf_get_features(VirtIODevice
*vdev
,
132 uint64_t requested_features
,
135 /* No feature bits used yet */
136 return requested_features
;
139 static void vuf_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
142 * Not normally called; it's the daemon that handles the queue;
143 * however virtio's cleanup path can call this.
147 static void vuf_guest_notifier_mask(VirtIODevice
*vdev
, int idx
,
150 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
152 vhost_virtqueue_mask(&fs
->vhost_dev
, vdev
, idx
, mask
);
155 static bool vuf_guest_notifier_pending(VirtIODevice
*vdev
, int idx
)
157 VHostUserFS
*fs
= VHOST_USER_FS(vdev
);
159 return vhost_virtqueue_pending(&fs
->vhost_dev
, idx
);
162 static void vuf_device_realize(DeviceState
*dev
, Error
**errp
)
164 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
165 VHostUserFS
*fs
= VHOST_USER_FS(dev
);
170 if (!fs
->conf
.chardev
.chr
) {
171 error_setg(errp
, "missing chardev");
176 error_setg(errp
, "missing tag property");
179 len
= strlen(fs
->conf
.tag
);
181 error_setg(errp
, "tag property cannot be empty");
184 if (len
> sizeof_field(struct virtio_fs_config
, tag
)) {
185 error_setg(errp
, "tag property must be %zu bytes or less",
186 sizeof_field(struct virtio_fs_config
, tag
));
190 if (fs
->conf
.num_request_queues
== 0) {
191 error_setg(errp
, "num-request-queues property must be larger than 0");
195 if (!is_power_of_2(fs
->conf
.queue_size
)) {
196 error_setg(errp
, "queue-size property must be a power of 2");
200 if (fs
->conf
.queue_size
> VIRTQUEUE_MAX_SIZE
) {
201 error_setg(errp
, "queue-size property must be %u or smaller",
206 if (!vhost_user_init(&fs
->vhost_user
, &fs
->conf
.chardev
, errp
)) {
210 virtio_init(vdev
, "vhost-user-fs", VIRTIO_ID_FS
,
211 sizeof(struct virtio_fs_config
));
214 fs
->hiprio_vq
= virtio_add_queue(vdev
, fs
->conf
.queue_size
, vuf_handle_output
);
217 fs
->req_vqs
= g_new(VirtQueue
*, fs
->conf
.num_request_queues
);
218 for (i
= 0; i
< fs
->conf
.num_request_queues
; i
++) {
219 fs
->req_vqs
[i
] = virtio_add_queue(vdev
, fs
->conf
.queue_size
, vuf_handle_output
);
222 /* 1 high prio queue, plus the number configured */
223 fs
->vhost_dev
.nvqs
= 1 + fs
->conf
.num_request_queues
;
224 fs
->vhost_dev
.vqs
= g_new0(struct vhost_virtqueue
, fs
->vhost_dev
.nvqs
);
225 ret
= vhost_dev_init(&fs
->vhost_dev
, &fs
->vhost_user
,
226 VHOST_BACKEND_TYPE_USER
, 0);
228 error_setg_errno(errp
, -ret
, "vhost_dev_init failed");
235 vhost_user_cleanup(&fs
->vhost_user
);
236 virtio_delete_queue(fs
->hiprio_vq
);
237 for (i
= 0; i
< fs
->conf
.num_request_queues
; i
++) {
238 virtio_delete_queue(fs
->req_vqs
[i
]);
241 virtio_cleanup(vdev
);
242 g_free(fs
->vhost_dev
.vqs
);
246 static void vuf_device_unrealize(DeviceState
*dev
)
248 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
249 VHostUserFS
*fs
= VHOST_USER_FS(dev
);
252 /* This will stop vhost backend if appropriate. */
253 vuf_set_status(vdev
, 0);
255 vhost_dev_cleanup(&fs
->vhost_dev
);
257 vhost_user_cleanup(&fs
->vhost_user
);
259 virtio_delete_queue(fs
->hiprio_vq
);
260 for (i
= 0; i
< fs
->conf
.num_request_queues
; i
++) {
261 virtio_delete_queue(fs
->req_vqs
[i
]);
264 virtio_cleanup(vdev
);
265 g_free(fs
->vhost_dev
.vqs
);
266 fs
->vhost_dev
.vqs
= NULL
;
269 static const VMStateDescription vuf_vmstate
= {
270 .name
= "vhost-user-fs",
274 static Property vuf_properties
[] = {
275 DEFINE_PROP_CHR("chardev", VHostUserFS
, conf
.chardev
),
276 DEFINE_PROP_STRING("tag", VHostUserFS
, conf
.tag
),
277 DEFINE_PROP_UINT16("num-request-queues", VHostUserFS
,
278 conf
.num_request_queues
, 1),
279 DEFINE_PROP_UINT16("queue-size", VHostUserFS
, conf
.queue_size
, 128),
280 DEFINE_PROP_END_OF_LIST(),
283 static void vuf_instance_init(Object
*obj
)
285 VHostUserFS
*fs
= VHOST_USER_FS(obj
);
287 device_add_bootindex_property(obj
, &fs
->bootindex
, "bootindex",
288 "/filesystem@0", DEVICE(obj
));
291 static void vuf_class_init(ObjectClass
*klass
, void *data
)
293 DeviceClass
*dc
= DEVICE_CLASS(klass
);
294 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
296 device_class_set_props(dc
, vuf_properties
);
297 dc
->vmsd
= &vuf_vmstate
;
298 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
299 vdc
->realize
= vuf_device_realize
;
300 vdc
->unrealize
= vuf_device_unrealize
;
301 vdc
->get_features
= vuf_get_features
;
302 vdc
->get_config
= vuf_get_config
;
303 vdc
->set_status
= vuf_set_status
;
304 vdc
->guest_notifier_mask
= vuf_guest_notifier_mask
;
305 vdc
->guest_notifier_pending
= vuf_guest_notifier_pending
;
308 static const TypeInfo vuf_info
= {
309 .name
= TYPE_VHOST_USER_FS
,
310 .parent
= TYPE_VIRTIO_DEVICE
,
311 .instance_size
= sizeof(VHostUserFS
),
312 .instance_init
= vuf_instance_init
,
313 .class_init
= vuf_class_init
,
316 static void vuf_register_types(void)
318 type_register_static(&vuf_info
);
321 type_init(vuf_register_types
)