198fc1adaSDr. David Alan Gilbert /*
298fc1adaSDr. David Alan Gilbert * Vhost-user filesystem virtio device
398fc1adaSDr. David Alan Gilbert *
498fc1adaSDr. David Alan Gilbert * Copyright 2018-2019 Red Hat, Inc.
598fc1adaSDr. David Alan Gilbert *
698fc1adaSDr. David Alan Gilbert * Authors:
798fc1adaSDr. David Alan Gilbert * Stefan Hajnoczi <stefanha@redhat.com>
898fc1adaSDr. David Alan Gilbert *
998fc1adaSDr. David Alan Gilbert * This work is licensed under the terms of the GNU GPL, version 2 or
1098fc1adaSDr. David Alan Gilbert * (at your option) any later version. See the COPYING file in the
1198fc1adaSDr. David Alan Gilbert * top-level directory.
1298fc1adaSDr. David Alan Gilbert */
1398fc1adaSDr. David Alan Gilbert
1498fc1adaSDr. David Alan Gilbert #include "qemu/osdep.h"
1598fc1adaSDr. David Alan Gilbert #include <sys/ioctl.h>
1698fc1adaSDr. David Alan Gilbert #include "standard-headers/linux/virtio_fs.h"
1798fc1adaSDr. David Alan Gilbert #include "qapi/error.h"
1898fc1adaSDr. David Alan Gilbert #include "hw/qdev-properties.h"
19ce35e229SEduardo Habkost #include "hw/qdev-properties-system.h"
2098fc1adaSDr. David Alan Gilbert #include "hw/virtio/virtio-bus.h"
2198fc1adaSDr. David Alan Gilbert #include "hw/virtio/virtio-access.h"
2298fc1adaSDr. David Alan Gilbert #include "qemu/error-report.h"
23b8f3e6a1SAlex Bennée #include "hw/virtio/vhost.h"
2498fc1adaSDr. David Alan Gilbert #include "hw/virtio/vhost-user-fs.h"
2598fc1adaSDr. David Alan Gilbert #include "monitor/monitor.h"
266da32fe5SLaszlo Ersek #include "sysemu/sysemu.h"
2798fc1adaSDr. David Alan Gilbert
28ace66791SAnton Kuchin static const int user_feature_bits[] = {
29ace66791SAnton Kuchin VIRTIO_F_VERSION_1,
30ace66791SAnton Kuchin VIRTIO_RING_F_INDIRECT_DESC,
31ace66791SAnton Kuchin VIRTIO_RING_F_EVENT_IDX,
32ace66791SAnton Kuchin VIRTIO_F_NOTIFY_ON_EMPTY,
33ace66791SAnton Kuchin VIRTIO_F_RING_PACKED,
34ace66791SAnton Kuchin VIRTIO_F_IOMMU_PLATFORM,
35562a7d23SStefano Garzarella VIRTIO_F_RING_RESET,
36*c03213fdSJonah Palmer VIRTIO_F_IN_ORDER,
37b937fa89SJonah Palmer VIRTIO_F_NOTIFICATION_DATA,
38ace66791SAnton Kuchin VHOST_INVALID_FEATURE_BIT
39ace66791SAnton Kuchin };
40ace66791SAnton Kuchin
vuf_get_config(VirtIODevice * vdev,uint8_t * config)4198fc1adaSDr. David Alan Gilbert static void vuf_get_config(VirtIODevice *vdev, uint8_t *config)
4298fc1adaSDr. David Alan Gilbert {
4398fc1adaSDr. David Alan Gilbert VHostUserFS *fs = VHOST_USER_FS(vdev);
4498fc1adaSDr. David Alan Gilbert struct virtio_fs_config fscfg = {};
4598fc1adaSDr. David Alan Gilbert
4698fc1adaSDr. David Alan Gilbert memcpy((char *)fscfg.tag, fs->conf.tag,
4798fc1adaSDr. David Alan Gilbert MIN(strlen(fs->conf.tag) + 1, sizeof(fscfg.tag)));
4898fc1adaSDr. David Alan Gilbert
4998fc1adaSDr. David Alan Gilbert virtio_stl_p(vdev, &fscfg.num_request_queues, fs->conf.num_request_queues);
5098fc1adaSDr. David Alan Gilbert
5198fc1adaSDr. David Alan Gilbert memcpy(config, &fscfg, sizeof(fscfg));
5298fc1adaSDr. David Alan Gilbert }
5398fc1adaSDr. David Alan Gilbert
vuf_start(VirtIODevice * vdev)5498fc1adaSDr. David Alan Gilbert static void vuf_start(VirtIODevice *vdev)
5598fc1adaSDr. David Alan Gilbert {
5698fc1adaSDr. David Alan Gilbert VHostUserFS *fs = VHOST_USER_FS(vdev);
5798fc1adaSDr. David Alan Gilbert BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
5898fc1adaSDr. David Alan Gilbert VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
5998fc1adaSDr. David Alan Gilbert int ret;
6098fc1adaSDr. David Alan Gilbert int i;
6198fc1adaSDr. David Alan Gilbert
6298fc1adaSDr. David Alan Gilbert if (!k->set_guest_notifiers) {
6398fc1adaSDr. David Alan Gilbert error_report("binding does not support guest notifiers");
6498fc1adaSDr. David Alan Gilbert return;
6598fc1adaSDr. David Alan Gilbert }
6698fc1adaSDr. David Alan Gilbert
6798fc1adaSDr. David Alan Gilbert ret = vhost_dev_enable_notifiers(&fs->vhost_dev, vdev);
6898fc1adaSDr. David Alan Gilbert if (ret < 0) {
6998fc1adaSDr. David Alan Gilbert error_report("Error enabling host notifiers: %d", -ret);
7098fc1adaSDr. David Alan Gilbert return;
7198fc1adaSDr. David Alan Gilbert }
7298fc1adaSDr. David Alan Gilbert
7398fc1adaSDr. David Alan Gilbert ret = k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, true);
7498fc1adaSDr. David Alan Gilbert if (ret < 0) {
7598fc1adaSDr. David Alan Gilbert error_report("Error binding guest notifier: %d", -ret);
7698fc1adaSDr. David Alan Gilbert goto err_host_notifiers;
7798fc1adaSDr. David Alan Gilbert }
7898fc1adaSDr. David Alan Gilbert
7998fc1adaSDr. David Alan Gilbert fs->vhost_dev.acked_features = vdev->guest_features;
804daa5054SStefano Garzarella ret = vhost_dev_start(&fs->vhost_dev, vdev, true);
8198fc1adaSDr. David Alan Gilbert if (ret < 0) {
8298fc1adaSDr. David Alan Gilbert error_report("Error starting vhost: %d", -ret);
8398fc1adaSDr. David Alan Gilbert goto err_guest_notifiers;
8498fc1adaSDr. David Alan Gilbert }
8598fc1adaSDr. David Alan Gilbert
8698fc1adaSDr. David Alan Gilbert /*
8798fc1adaSDr. David Alan Gilbert * guest_notifier_mask/pending not used yet, so just unmask
8898fc1adaSDr. David Alan Gilbert * everything here. virtio-pci will do the right thing by
8998fc1adaSDr. David Alan Gilbert * enabling/disabling irqfd.
9098fc1adaSDr. David Alan Gilbert */
9198fc1adaSDr. David Alan Gilbert for (i = 0; i < fs->vhost_dev.nvqs; i++) {
9298fc1adaSDr. David Alan Gilbert vhost_virtqueue_mask(&fs->vhost_dev, vdev, i, false);
9398fc1adaSDr. David Alan Gilbert }
9498fc1adaSDr. David Alan Gilbert
9598fc1adaSDr. David Alan Gilbert return;
9698fc1adaSDr. David Alan Gilbert
9798fc1adaSDr. David Alan Gilbert err_guest_notifiers:
9898fc1adaSDr. David Alan Gilbert k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, false);
9998fc1adaSDr. David Alan Gilbert err_host_notifiers:
10098fc1adaSDr. David Alan Gilbert vhost_dev_disable_notifiers(&fs->vhost_dev, vdev);
10198fc1adaSDr. David Alan Gilbert }
10298fc1adaSDr. David Alan Gilbert
vuf_stop(VirtIODevice * vdev)10398fc1adaSDr. David Alan Gilbert static void vuf_stop(VirtIODevice *vdev)
10498fc1adaSDr. David Alan Gilbert {
10598fc1adaSDr. David Alan Gilbert VHostUserFS *fs = VHOST_USER_FS(vdev);
10698fc1adaSDr. David Alan Gilbert BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
10798fc1adaSDr. David Alan Gilbert VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
10898fc1adaSDr. David Alan Gilbert int ret;
10998fc1adaSDr. David Alan Gilbert
11098fc1adaSDr. David Alan Gilbert if (!k->set_guest_notifiers) {
11198fc1adaSDr. David Alan Gilbert return;
11298fc1adaSDr. David Alan Gilbert }
11398fc1adaSDr. David Alan Gilbert
1144daa5054SStefano Garzarella vhost_dev_stop(&fs->vhost_dev, vdev, true);
11598fc1adaSDr. David Alan Gilbert
11698fc1adaSDr. David Alan Gilbert ret = k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, false);
11798fc1adaSDr. David Alan Gilbert if (ret < 0) {
11898fc1adaSDr. David Alan Gilbert error_report("vhost guest notifier cleanup failed: %d", ret);
11998fc1adaSDr. David Alan Gilbert return;
12098fc1adaSDr. David Alan Gilbert }
12198fc1adaSDr. David Alan Gilbert
12298fc1adaSDr. David Alan Gilbert vhost_dev_disable_notifiers(&fs->vhost_dev, vdev);
12398fc1adaSDr. David Alan Gilbert }
12498fc1adaSDr. David Alan Gilbert
vuf_set_status(VirtIODevice * vdev,uint8_t status)12598fc1adaSDr. David Alan Gilbert static void vuf_set_status(VirtIODevice *vdev, uint8_t status)
12698fc1adaSDr. David Alan Gilbert {
12798fc1adaSDr. David Alan Gilbert VHostUserFS *fs = VHOST_USER_FS(vdev);
128259d69c0SAlex Bennée bool should_start = virtio_device_should_start(vdev, status);
12998fc1adaSDr. David Alan Gilbert
130b8f3e6a1SAlex Bennée if (vhost_dev_is_started(&fs->vhost_dev) == should_start) {
13198fc1adaSDr. David Alan Gilbert return;
13298fc1adaSDr. David Alan Gilbert }
13398fc1adaSDr. David Alan Gilbert
13498fc1adaSDr. David Alan Gilbert if (should_start) {
13598fc1adaSDr. David Alan Gilbert vuf_start(vdev);
13698fc1adaSDr. David Alan Gilbert } else {
13798fc1adaSDr. David Alan Gilbert vuf_stop(vdev);
13898fc1adaSDr. David Alan Gilbert }
13998fc1adaSDr. David Alan Gilbert }
14098fc1adaSDr. David Alan Gilbert
vuf_get_features(VirtIODevice * vdev,uint64_t features,Error ** errp)14198fc1adaSDr. David Alan Gilbert static uint64_t vuf_get_features(VirtIODevice *vdev,
142ace66791SAnton Kuchin uint64_t features,
14398fc1adaSDr. David Alan Gilbert Error **errp)
14498fc1adaSDr. David Alan Gilbert {
145ace66791SAnton Kuchin VHostUserFS *fs = VHOST_USER_FS(vdev);
146ace66791SAnton Kuchin
147ace66791SAnton Kuchin return vhost_get_features(&fs->vhost_dev, user_feature_bits, features);
14898fc1adaSDr. David Alan Gilbert }
14998fc1adaSDr. David Alan Gilbert
vuf_handle_output(VirtIODevice * vdev,VirtQueue * vq)15098fc1adaSDr. David Alan Gilbert static void vuf_handle_output(VirtIODevice *vdev, VirtQueue *vq)
15198fc1adaSDr. David Alan Gilbert {
15298fc1adaSDr. David Alan Gilbert /*
15398fc1adaSDr. David Alan Gilbert * Not normally called; it's the daemon that handles the queue;
15498fc1adaSDr. David Alan Gilbert * however virtio's cleanup path can call this.
15598fc1adaSDr. David Alan Gilbert */
15698fc1adaSDr. David Alan Gilbert }
15798fc1adaSDr. David Alan Gilbert
vuf_guest_notifier_mask(VirtIODevice * vdev,int idx,bool mask)15898fc1adaSDr. David Alan Gilbert static void vuf_guest_notifier_mask(VirtIODevice *vdev, int idx,
15998fc1adaSDr. David Alan Gilbert bool mask)
16098fc1adaSDr. David Alan Gilbert {
16198fc1adaSDr. David Alan Gilbert VHostUserFS *fs = VHOST_USER_FS(vdev);
16298fc1adaSDr. David Alan Gilbert
163544f0278SCindy Lu /*
164544f0278SCindy Lu * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
1657e8094f0SAlex Bennée * as the macro of configure interrupt's IDX, If this driver does not
166544f0278SCindy Lu * support, the function will return
167544f0278SCindy Lu */
168544f0278SCindy Lu
169544f0278SCindy Lu if (idx == VIRTIO_CONFIG_IRQ_IDX) {
170544f0278SCindy Lu return;
171544f0278SCindy Lu }
17298fc1adaSDr. David Alan Gilbert vhost_virtqueue_mask(&fs->vhost_dev, vdev, idx, mask);
17398fc1adaSDr. David Alan Gilbert }
17498fc1adaSDr. David Alan Gilbert
vuf_guest_notifier_pending(VirtIODevice * vdev,int idx)17598fc1adaSDr. David Alan Gilbert static bool vuf_guest_notifier_pending(VirtIODevice *vdev, int idx)
17698fc1adaSDr. David Alan Gilbert {
17798fc1adaSDr. David Alan Gilbert VHostUserFS *fs = VHOST_USER_FS(vdev);
17898fc1adaSDr. David Alan Gilbert
179544f0278SCindy Lu /*
180544f0278SCindy Lu * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
1817e8094f0SAlex Bennée * as the macro of configure interrupt's IDX, If this driver does not
182544f0278SCindy Lu * support, the function will return
183544f0278SCindy Lu */
184544f0278SCindy Lu
185544f0278SCindy Lu if (idx == VIRTIO_CONFIG_IRQ_IDX) {
186544f0278SCindy Lu return false;
187544f0278SCindy Lu }
18898fc1adaSDr. David Alan Gilbert return vhost_virtqueue_pending(&fs->vhost_dev, idx);
18998fc1adaSDr. David Alan Gilbert }
19098fc1adaSDr. David Alan Gilbert
vuf_device_realize(DeviceState * dev,Error ** errp)19198fc1adaSDr. David Alan Gilbert static void vuf_device_realize(DeviceState *dev, Error **errp)
19298fc1adaSDr. David Alan Gilbert {
19398fc1adaSDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(dev);
19498fc1adaSDr. David Alan Gilbert VHostUserFS *fs = VHOST_USER_FS(dev);
19598fc1adaSDr. David Alan Gilbert unsigned int i;
19698fc1adaSDr. David Alan Gilbert size_t len;
19798fc1adaSDr. David Alan Gilbert int ret;
19898fc1adaSDr. David Alan Gilbert
19998fc1adaSDr. David Alan Gilbert if (!fs->conf.chardev.chr) {
20098fc1adaSDr. David Alan Gilbert error_setg(errp, "missing chardev");
20198fc1adaSDr. David Alan Gilbert return;
20298fc1adaSDr. David Alan Gilbert }
20398fc1adaSDr. David Alan Gilbert
20498fc1adaSDr. David Alan Gilbert if (!fs->conf.tag) {
20598fc1adaSDr. David Alan Gilbert error_setg(errp, "missing tag property");
20698fc1adaSDr. David Alan Gilbert return;
20798fc1adaSDr. David Alan Gilbert }
20898fc1adaSDr. David Alan Gilbert len = strlen(fs->conf.tag);
20998fc1adaSDr. David Alan Gilbert if (len == 0) {
21098fc1adaSDr. David Alan Gilbert error_setg(errp, "tag property cannot be empty");
21198fc1adaSDr. David Alan Gilbert return;
21298fc1adaSDr. David Alan Gilbert }
21398fc1adaSDr. David Alan Gilbert if (len > sizeof_field(struct virtio_fs_config, tag)) {
21498fc1adaSDr. David Alan Gilbert error_setg(errp, "tag property must be %zu bytes or less",
21598fc1adaSDr. David Alan Gilbert sizeof_field(struct virtio_fs_config, tag));
21698fc1adaSDr. David Alan Gilbert return;
21798fc1adaSDr. David Alan Gilbert }
21898fc1adaSDr. David Alan Gilbert
21998fc1adaSDr. David Alan Gilbert if (fs->conf.num_request_queues == 0) {
22098fc1adaSDr. David Alan Gilbert error_setg(errp, "num-request-queues property must be larger than 0");
22198fc1adaSDr. David Alan Gilbert return;
22298fc1adaSDr. David Alan Gilbert }
22398fc1adaSDr. David Alan Gilbert
22498fc1adaSDr. David Alan Gilbert if (!is_power_of_2(fs->conf.queue_size)) {
22598fc1adaSDr. David Alan Gilbert error_setg(errp, "queue-size property must be a power of 2");
22698fc1adaSDr. David Alan Gilbert return;
22798fc1adaSDr. David Alan Gilbert }
22898fc1adaSDr. David Alan Gilbert
22998fc1adaSDr. David Alan Gilbert if (fs->conf.queue_size > VIRTQUEUE_MAX_SIZE) {
23098fc1adaSDr. David Alan Gilbert error_setg(errp, "queue-size property must be %u or smaller",
23198fc1adaSDr. David Alan Gilbert VIRTQUEUE_MAX_SIZE);
23298fc1adaSDr. David Alan Gilbert return;
23398fc1adaSDr. David Alan Gilbert }
23498fc1adaSDr. David Alan Gilbert
23598fc1adaSDr. David Alan Gilbert if (!vhost_user_init(&fs->vhost_user, &fs->conf.chardev, errp)) {
23698fc1adaSDr. David Alan Gilbert return;
23798fc1adaSDr. David Alan Gilbert }
23898fc1adaSDr. David Alan Gilbert
2393857cd5cSJonah Palmer virtio_init(vdev, VIRTIO_ID_FS, sizeof(struct virtio_fs_config));
24098fc1adaSDr. David Alan Gilbert
24198fc1adaSDr. David Alan Gilbert /* Hiprio queue */
2422e5bc659SPan Nengyuan fs->hiprio_vq = virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output);
24398fc1adaSDr. David Alan Gilbert
24498fc1adaSDr. David Alan Gilbert /* Request queues */
2452e5bc659SPan Nengyuan fs->req_vqs = g_new(VirtQueue *, fs->conf.num_request_queues);
24698fc1adaSDr. David Alan Gilbert for (i = 0; i < fs->conf.num_request_queues; i++) {
2472e5bc659SPan Nengyuan fs->req_vqs[i] = virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output);
24898fc1adaSDr. David Alan Gilbert }
24998fc1adaSDr. David Alan Gilbert
25098fc1adaSDr. David Alan Gilbert /* 1 high prio queue, plus the number configured */
25198fc1adaSDr. David Alan Gilbert fs->vhost_dev.nvqs = 1 + fs->conf.num_request_queues;
25298fc1adaSDr. David Alan Gilbert fs->vhost_dev.vqs = g_new0(struct vhost_virtqueue, fs->vhost_dev.nvqs);
25398fc1adaSDr. David Alan Gilbert ret = vhost_dev_init(&fs->vhost_dev, &fs->vhost_user,
254a6945f22SKevin Wolf VHOST_BACKEND_TYPE_USER, 0, errp);
25598fc1adaSDr. David Alan Gilbert if (ret < 0) {
25698fc1adaSDr. David Alan Gilbert goto err_virtio;
25798fc1adaSDr. David Alan Gilbert }
25898fc1adaSDr. David Alan Gilbert
25998fc1adaSDr. David Alan Gilbert return;
26098fc1adaSDr. David Alan Gilbert
26198fc1adaSDr. David Alan Gilbert err_virtio:
26298fc1adaSDr. David Alan Gilbert vhost_user_cleanup(&fs->vhost_user);
2632e5bc659SPan Nengyuan virtio_delete_queue(fs->hiprio_vq);
264ba07cf5dSPan Nengyuan for (i = 0; i < fs->conf.num_request_queues; i++) {
2652e5bc659SPan Nengyuan virtio_delete_queue(fs->req_vqs[i]);
266ba07cf5dSPan Nengyuan }
2672e5bc659SPan Nengyuan g_free(fs->req_vqs);
26898fc1adaSDr. David Alan Gilbert virtio_cleanup(vdev);
26998fc1adaSDr. David Alan Gilbert g_free(fs->vhost_dev.vqs);
27098fc1adaSDr. David Alan Gilbert return;
27198fc1adaSDr. David Alan Gilbert }
27298fc1adaSDr. David Alan Gilbert
vuf_device_unrealize(DeviceState * dev)273b69c3c21SMarkus Armbruster static void vuf_device_unrealize(DeviceState *dev)
27498fc1adaSDr. David Alan Gilbert {
27598fc1adaSDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(dev);
27698fc1adaSDr. David Alan Gilbert VHostUserFS *fs = VHOST_USER_FS(dev);
277331acddcSAkihiko Odaki struct vhost_virtqueue *vhost_vqs = fs->vhost_dev.vqs;
278ba07cf5dSPan Nengyuan int i;
27998fc1adaSDr. David Alan Gilbert
28098fc1adaSDr. David Alan Gilbert /* This will stop vhost backend if appropriate. */
28198fc1adaSDr. David Alan Gilbert vuf_set_status(vdev, 0);
28298fc1adaSDr. David Alan Gilbert
28398fc1adaSDr. David Alan Gilbert vhost_dev_cleanup(&fs->vhost_dev);
28498fc1adaSDr. David Alan Gilbert
28598fc1adaSDr. David Alan Gilbert vhost_user_cleanup(&fs->vhost_user);
28698fc1adaSDr. David Alan Gilbert
2872e5bc659SPan Nengyuan virtio_delete_queue(fs->hiprio_vq);
288ba07cf5dSPan Nengyuan for (i = 0; i < fs->conf.num_request_queues; i++) {
2892e5bc659SPan Nengyuan virtio_delete_queue(fs->req_vqs[i]);
290ba07cf5dSPan Nengyuan }
2912e5bc659SPan Nengyuan g_free(fs->req_vqs);
29298fc1adaSDr. David Alan Gilbert virtio_cleanup(vdev);
293331acddcSAkihiko Odaki g_free(vhost_vqs);
29498fc1adaSDr. David Alan Gilbert }
29598fc1adaSDr. David Alan Gilbert
vuf_get_vhost(VirtIODevice * vdev)296c255488dSJonah Palmer static struct vhost_dev *vuf_get_vhost(VirtIODevice *vdev)
297c255488dSJonah Palmer {
298c255488dSJonah Palmer VHostUserFS *fs = VHOST_USER_FS(vdev);
299c255488dSJonah Palmer return &fs->vhost_dev;
300c255488dSJonah Palmer }
301c255488dSJonah Palmer
302bca3e2a1SHanna Czenczek /**
303bca3e2a1SHanna Czenczek * Fetch the internal state from virtiofsd and save it to `f`.
304bca3e2a1SHanna Czenczek */
vuf_save_state(QEMUFile * f,void * pv,size_t size,const VMStateField * field,JSONWriter * vmdesc)305bca3e2a1SHanna Czenczek static int vuf_save_state(QEMUFile *f, void *pv, size_t size,
306bca3e2a1SHanna Czenczek const VMStateField *field, JSONWriter *vmdesc)
307bca3e2a1SHanna Czenczek {
308bca3e2a1SHanna Czenczek VirtIODevice *vdev = pv;
309bca3e2a1SHanna Czenczek VHostUserFS *fs = VHOST_USER_FS(vdev);
310bca3e2a1SHanna Czenczek Error *local_error = NULL;
311bca3e2a1SHanna Czenczek int ret;
312bca3e2a1SHanna Czenczek
313bca3e2a1SHanna Czenczek ret = vhost_save_backend_state(&fs->vhost_dev, f, &local_error);
314bca3e2a1SHanna Czenczek if (ret < 0) {
315bca3e2a1SHanna Czenczek error_reportf_err(local_error,
316bca3e2a1SHanna Czenczek "Error saving back-end state of %s device %s "
317bca3e2a1SHanna Czenczek "(tag: \"%s\"): ",
318bca3e2a1SHanna Czenczek vdev->name, vdev->parent_obj.canonical_path,
319bca3e2a1SHanna Czenczek fs->conf.tag ?: "<none>");
320bca3e2a1SHanna Czenczek return ret;
321bca3e2a1SHanna Czenczek }
322bca3e2a1SHanna Czenczek
323bca3e2a1SHanna Czenczek return 0;
324bca3e2a1SHanna Czenczek }
325bca3e2a1SHanna Czenczek
326bca3e2a1SHanna Czenczek /**
327bca3e2a1SHanna Czenczek * Load virtiofsd's internal state from `f` and send it over to virtiofsd.
328bca3e2a1SHanna Czenczek */
vuf_load_state(QEMUFile * f,void * pv,size_t size,const VMStateField * field)329bca3e2a1SHanna Czenczek static int vuf_load_state(QEMUFile *f, void *pv, size_t size,
330bca3e2a1SHanna Czenczek const VMStateField *field)
331bca3e2a1SHanna Czenczek {
332bca3e2a1SHanna Czenczek VirtIODevice *vdev = pv;
333bca3e2a1SHanna Czenczek VHostUserFS *fs = VHOST_USER_FS(vdev);
334bca3e2a1SHanna Czenczek Error *local_error = NULL;
335bca3e2a1SHanna Czenczek int ret;
336bca3e2a1SHanna Czenczek
337bca3e2a1SHanna Czenczek ret = vhost_load_backend_state(&fs->vhost_dev, f, &local_error);
338bca3e2a1SHanna Czenczek if (ret < 0) {
339bca3e2a1SHanna Czenczek error_reportf_err(local_error,
340bca3e2a1SHanna Czenczek "Error loading back-end state of %s device %s "
341bca3e2a1SHanna Czenczek "(tag: \"%s\"): ",
342bca3e2a1SHanna Czenczek vdev->name, vdev->parent_obj.canonical_path,
343bca3e2a1SHanna Czenczek fs->conf.tag ?: "<none>");
344bca3e2a1SHanna Czenczek return ret;
345bca3e2a1SHanna Czenczek }
346bca3e2a1SHanna Czenczek
347bca3e2a1SHanna Czenczek return 0;
348bca3e2a1SHanna Czenczek }
349bca3e2a1SHanna Czenczek
vuf_is_internal_migration(void * opaque)350bca3e2a1SHanna Czenczek static bool vuf_is_internal_migration(void *opaque)
351bca3e2a1SHanna Czenczek {
352bca3e2a1SHanna Czenczek /* TODO: Return false when an external migration is requested */
353bca3e2a1SHanna Czenczek return true;
354bca3e2a1SHanna Czenczek }
355bca3e2a1SHanna Czenczek
vuf_check_migration_support(void * opaque)356bca3e2a1SHanna Czenczek static int vuf_check_migration_support(void *opaque)
357bca3e2a1SHanna Czenczek {
358bca3e2a1SHanna Czenczek VirtIODevice *vdev = opaque;
359bca3e2a1SHanna Czenczek VHostUserFS *fs = VHOST_USER_FS(vdev);
360bca3e2a1SHanna Czenczek
361bca3e2a1SHanna Czenczek if (!vhost_supports_device_state(&fs->vhost_dev)) {
362bca3e2a1SHanna Czenczek error_report("Back-end of %s device %s (tag: \"%s\") does not support "
363bca3e2a1SHanna Czenczek "migration through qemu",
364bca3e2a1SHanna Czenczek vdev->name, vdev->parent_obj.canonical_path,
365bca3e2a1SHanna Czenczek fs->conf.tag ?: "<none>");
366bca3e2a1SHanna Czenczek return -ENOTSUP;
367bca3e2a1SHanna Czenczek }
368bca3e2a1SHanna Czenczek
369bca3e2a1SHanna Czenczek return 0;
370bca3e2a1SHanna Czenczek }
371bca3e2a1SHanna Czenczek
372bca3e2a1SHanna Czenczek static const VMStateDescription vuf_backend_vmstate;
373bca3e2a1SHanna Czenczek
37498fc1adaSDr. David Alan Gilbert static const VMStateDescription vuf_vmstate = {
37598fc1adaSDr. David Alan Gilbert .name = "vhost-user-fs",
376bca3e2a1SHanna Czenczek .version_id = 0,
377ca02a170SRichard Henderson .fields = (const VMStateField[]) {
378bca3e2a1SHanna Czenczek VMSTATE_VIRTIO_DEVICE,
379bca3e2a1SHanna Czenczek VMSTATE_END_OF_LIST()
380bca3e2a1SHanna Czenczek },
381ca02a170SRichard Henderson .subsections = (const VMStateDescription * const []) {
382bca3e2a1SHanna Czenczek &vuf_backend_vmstate,
383bca3e2a1SHanna Czenczek NULL,
384bca3e2a1SHanna Czenczek }
385bca3e2a1SHanna Czenczek };
386bca3e2a1SHanna Czenczek
387bca3e2a1SHanna Czenczek static const VMStateDescription vuf_backend_vmstate = {
388bca3e2a1SHanna Czenczek .name = "vhost-user-fs-backend",
389bca3e2a1SHanna Czenczek .version_id = 0,
390bca3e2a1SHanna Czenczek .needed = vuf_is_internal_migration,
391bca3e2a1SHanna Czenczek .pre_load = vuf_check_migration_support,
392bca3e2a1SHanna Czenczek .pre_save = vuf_check_migration_support,
393ca02a170SRichard Henderson .fields = (const VMStateField[]) {
394bca3e2a1SHanna Czenczek {
395bca3e2a1SHanna Czenczek .name = "back-end",
396bca3e2a1SHanna Czenczek .info = &(const VMStateInfo) {
397bca3e2a1SHanna Czenczek .name = "virtio-fs back-end state",
398bca3e2a1SHanna Czenczek .get = vuf_load_state,
399bca3e2a1SHanna Czenczek .put = vuf_save_state,
400bca3e2a1SHanna Czenczek },
401bca3e2a1SHanna Czenczek },
402bca3e2a1SHanna Czenczek VMSTATE_END_OF_LIST()
403bca3e2a1SHanna Czenczek },
40498fc1adaSDr. David Alan Gilbert };
40598fc1adaSDr. David Alan Gilbert
40698fc1adaSDr. David Alan Gilbert static Property vuf_properties[] = {
40798fc1adaSDr. David Alan Gilbert DEFINE_PROP_CHR("chardev", VHostUserFS, conf.chardev),
40898fc1adaSDr. David Alan Gilbert DEFINE_PROP_STRING("tag", VHostUserFS, conf.tag),
40998fc1adaSDr. David Alan Gilbert DEFINE_PROP_UINT16("num-request-queues", VHostUserFS,
41098fc1adaSDr. David Alan Gilbert conf.num_request_queues, 1),
41198fc1adaSDr. David Alan Gilbert DEFINE_PROP_UINT16("queue-size", VHostUserFS, conf.queue_size, 128),
41298fc1adaSDr. David Alan Gilbert DEFINE_PROP_END_OF_LIST(),
41398fc1adaSDr. David Alan Gilbert };
41498fc1adaSDr. David Alan Gilbert
vuf_instance_init(Object * obj)4156da32fe5SLaszlo Ersek static void vuf_instance_init(Object *obj)
4166da32fe5SLaszlo Ersek {
4176da32fe5SLaszlo Ersek VHostUserFS *fs = VHOST_USER_FS(obj);
4186da32fe5SLaszlo Ersek
4196da32fe5SLaszlo Ersek device_add_bootindex_property(obj, &fs->bootindex, "bootindex",
4206da32fe5SLaszlo Ersek "/filesystem@0", DEVICE(obj));
4216da32fe5SLaszlo Ersek }
4226da32fe5SLaszlo Ersek
vuf_class_init(ObjectClass * klass,void * data)42398fc1adaSDr. David Alan Gilbert static void vuf_class_init(ObjectClass *klass, void *data)
42498fc1adaSDr. David Alan Gilbert {
42598fc1adaSDr. David Alan Gilbert DeviceClass *dc = DEVICE_CLASS(klass);
42698fc1adaSDr. David Alan Gilbert VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
42798fc1adaSDr. David Alan Gilbert
4284f67d30bSMarc-André Lureau device_class_set_props(dc, vuf_properties);
42998fc1adaSDr. David Alan Gilbert dc->vmsd = &vuf_vmstate;
43098fc1adaSDr. David Alan Gilbert set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
43198fc1adaSDr. David Alan Gilbert vdc->realize = vuf_device_realize;
43298fc1adaSDr. David Alan Gilbert vdc->unrealize = vuf_device_unrealize;
43398fc1adaSDr. David Alan Gilbert vdc->get_features = vuf_get_features;
43498fc1adaSDr. David Alan Gilbert vdc->get_config = vuf_get_config;
43598fc1adaSDr. David Alan Gilbert vdc->set_status = vuf_set_status;
43698fc1adaSDr. David Alan Gilbert vdc->guest_notifier_mask = vuf_guest_notifier_mask;
43798fc1adaSDr. David Alan Gilbert vdc->guest_notifier_pending = vuf_guest_notifier_pending;
438c255488dSJonah Palmer vdc->get_vhost = vuf_get_vhost;
43998fc1adaSDr. David Alan Gilbert }
44098fc1adaSDr. David Alan Gilbert
44198fc1adaSDr. David Alan Gilbert static const TypeInfo vuf_info = {
44298fc1adaSDr. David Alan Gilbert .name = TYPE_VHOST_USER_FS,
44398fc1adaSDr. David Alan Gilbert .parent = TYPE_VIRTIO_DEVICE,
44498fc1adaSDr. David Alan Gilbert .instance_size = sizeof(VHostUserFS),
4456da32fe5SLaszlo Ersek .instance_init = vuf_instance_init,
44698fc1adaSDr. David Alan Gilbert .class_init = vuf_class_init,
44798fc1adaSDr. David Alan Gilbert };
44898fc1adaSDr. David Alan Gilbert
vuf_register_types(void)44998fc1adaSDr. David Alan Gilbert static void vuf_register_types(void)
45098fc1adaSDr. David Alan Gilbert {
45198fc1adaSDr. David Alan Gilbert type_register_static(&vuf_info);
45298fc1adaSDr. David Alan Gilbert }
45398fc1adaSDr. David Alan Gilbert
45498fc1adaSDr. David Alan Gilbert type_init(vuf_register_types)
455