1 /* 2 * Vhost-user filesystem virtio device 3 * 4 * Copyright 2018-2019 Red Hat, Inc. 5 * 6 * Authors: 7 * Stefan Hajnoczi <stefanha@redhat.com> 8 * 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. 12 */ 13 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" 27 28 static const int user_feature_bits[] = { 29 VIRTIO_F_VERSION_1, 30 VIRTIO_RING_F_INDIRECT_DESC, 31 VIRTIO_RING_F_EVENT_IDX, 32 VIRTIO_F_NOTIFY_ON_EMPTY, 33 VIRTIO_F_RING_PACKED, 34 VIRTIO_F_IOMMU_PLATFORM, 35 VIRTIO_F_RING_RESET, 36 37 VHOST_INVALID_FEATURE_BIT 38 }; 39 40 static void vuf_get_config(VirtIODevice *vdev, uint8_t *config) 41 { 42 VHostUserFS *fs = VHOST_USER_FS(vdev); 43 struct virtio_fs_config fscfg = {}; 44 45 memcpy((char *)fscfg.tag, fs->conf.tag, 46 MIN(strlen(fs->conf.tag) + 1, sizeof(fscfg.tag))); 47 48 virtio_stl_p(vdev, &fscfg.num_request_queues, fs->conf.num_request_queues); 49 50 memcpy(config, &fscfg, sizeof(fscfg)); 51 } 52 53 static void vuf_start(VirtIODevice *vdev) 54 { 55 VHostUserFS *fs = VHOST_USER_FS(vdev); 56 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 57 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 58 int ret; 59 int i; 60 61 if (!k->set_guest_notifiers) { 62 error_report("binding does not support guest notifiers"); 63 return; 64 } 65 66 ret = vhost_dev_enable_notifiers(&fs->vhost_dev, vdev); 67 if (ret < 0) { 68 error_report("Error enabling host notifiers: %d", -ret); 69 return; 70 } 71 72 ret = k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, true); 73 if (ret < 0) { 74 error_report("Error binding guest notifier: %d", -ret); 75 goto err_host_notifiers; 76 } 77 78 fs->vhost_dev.acked_features = vdev->guest_features; 79 ret = vhost_dev_start(&fs->vhost_dev, vdev, true); 80 if (ret < 0) { 81 error_report("Error starting vhost: %d", -ret); 82 goto err_guest_notifiers; 83 } 84 85 /* 86 * guest_notifier_mask/pending not used yet, so just unmask 87 * everything here. virtio-pci will do the right thing by 88 * enabling/disabling irqfd. 89 */ 90 for (i = 0; i < fs->vhost_dev.nvqs; i++) { 91 vhost_virtqueue_mask(&fs->vhost_dev, vdev, i, false); 92 } 93 94 return; 95 96 err_guest_notifiers: 97 k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, false); 98 err_host_notifiers: 99 vhost_dev_disable_notifiers(&fs->vhost_dev, vdev); 100 } 101 102 static void vuf_stop(VirtIODevice *vdev) 103 { 104 VHostUserFS *fs = VHOST_USER_FS(vdev); 105 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 106 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 107 int ret; 108 109 if (!k->set_guest_notifiers) { 110 return; 111 } 112 113 vhost_dev_stop(&fs->vhost_dev, vdev, true); 114 115 ret = k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, false); 116 if (ret < 0) { 117 error_report("vhost guest notifier cleanup failed: %d", ret); 118 return; 119 } 120 121 vhost_dev_disable_notifiers(&fs->vhost_dev, vdev); 122 } 123 124 static void vuf_set_status(VirtIODevice *vdev, uint8_t status) 125 { 126 VHostUserFS *fs = VHOST_USER_FS(vdev); 127 bool should_start = virtio_device_should_start(vdev, status); 128 129 if (vhost_dev_is_started(&fs->vhost_dev) == should_start) { 130 return; 131 } 132 133 if (should_start) { 134 vuf_start(vdev); 135 } else { 136 vuf_stop(vdev); 137 } 138 } 139 140 static uint64_t vuf_get_features(VirtIODevice *vdev, 141 uint64_t features, 142 Error **errp) 143 { 144 VHostUserFS *fs = VHOST_USER_FS(vdev); 145 146 return vhost_get_features(&fs->vhost_dev, user_feature_bits, features); 147 } 148 149 static void vuf_handle_output(VirtIODevice *vdev, VirtQueue *vq) 150 { 151 /* 152 * Not normally called; it's the daemon that handles the queue; 153 * however virtio's cleanup path can call this. 154 */ 155 } 156 157 static void vuf_guest_notifier_mask(VirtIODevice *vdev, int idx, 158 bool mask) 159 { 160 VHostUserFS *fs = VHOST_USER_FS(vdev); 161 162 vhost_virtqueue_mask(&fs->vhost_dev, vdev, idx, mask); 163 } 164 165 static bool vuf_guest_notifier_pending(VirtIODevice *vdev, int idx) 166 { 167 VHostUserFS *fs = VHOST_USER_FS(vdev); 168 169 return vhost_virtqueue_pending(&fs->vhost_dev, idx); 170 } 171 172 static void vuf_device_realize(DeviceState *dev, Error **errp) 173 { 174 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 175 VHostUserFS *fs = VHOST_USER_FS(dev); 176 unsigned int i; 177 size_t len; 178 int ret; 179 180 if (!fs->conf.chardev.chr) { 181 error_setg(errp, "missing chardev"); 182 return; 183 } 184 185 if (!fs->conf.tag) { 186 error_setg(errp, "missing tag property"); 187 return; 188 } 189 len = strlen(fs->conf.tag); 190 if (len == 0) { 191 error_setg(errp, "tag property cannot be empty"); 192 return; 193 } 194 if (len > sizeof_field(struct virtio_fs_config, tag)) { 195 error_setg(errp, "tag property must be %zu bytes or less", 196 sizeof_field(struct virtio_fs_config, tag)); 197 return; 198 } 199 200 if (fs->conf.num_request_queues == 0) { 201 error_setg(errp, "num-request-queues property must be larger than 0"); 202 return; 203 } 204 205 if (!is_power_of_2(fs->conf.queue_size)) { 206 error_setg(errp, "queue-size property must be a power of 2"); 207 return; 208 } 209 210 if (fs->conf.queue_size > VIRTQUEUE_MAX_SIZE) { 211 error_setg(errp, "queue-size property must be %u or smaller", 212 VIRTQUEUE_MAX_SIZE); 213 return; 214 } 215 216 if (!vhost_user_init(&fs->vhost_user, &fs->conf.chardev, errp)) { 217 return; 218 } 219 220 virtio_init(vdev, VIRTIO_ID_FS, sizeof(struct virtio_fs_config)); 221 222 /* Hiprio queue */ 223 fs->hiprio_vq = virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output); 224 225 /* Request queues */ 226 fs->req_vqs = g_new(VirtQueue *, fs->conf.num_request_queues); 227 for (i = 0; i < fs->conf.num_request_queues; i++) { 228 fs->req_vqs[i] = virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output); 229 } 230 231 /* 1 high prio queue, plus the number configured */ 232 fs->vhost_dev.nvqs = 1 + fs->conf.num_request_queues; 233 fs->vhost_dev.vqs = g_new0(struct vhost_virtqueue, fs->vhost_dev.nvqs); 234 ret = vhost_dev_init(&fs->vhost_dev, &fs->vhost_user, 235 VHOST_BACKEND_TYPE_USER, 0, errp); 236 if (ret < 0) { 237 goto err_virtio; 238 } 239 240 return; 241 242 err_virtio: 243 vhost_user_cleanup(&fs->vhost_user); 244 virtio_delete_queue(fs->hiprio_vq); 245 for (i = 0; i < fs->conf.num_request_queues; i++) { 246 virtio_delete_queue(fs->req_vqs[i]); 247 } 248 g_free(fs->req_vqs); 249 virtio_cleanup(vdev); 250 g_free(fs->vhost_dev.vqs); 251 return; 252 } 253 254 static void vuf_device_unrealize(DeviceState *dev) 255 { 256 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 257 VHostUserFS *fs = VHOST_USER_FS(dev); 258 int i; 259 260 /* This will stop vhost backend if appropriate. */ 261 vuf_set_status(vdev, 0); 262 263 vhost_dev_cleanup(&fs->vhost_dev); 264 265 vhost_user_cleanup(&fs->vhost_user); 266 267 virtio_delete_queue(fs->hiprio_vq); 268 for (i = 0; i < fs->conf.num_request_queues; i++) { 269 virtio_delete_queue(fs->req_vqs[i]); 270 } 271 g_free(fs->req_vqs); 272 virtio_cleanup(vdev); 273 g_free(fs->vhost_dev.vqs); 274 fs->vhost_dev.vqs = NULL; 275 } 276 277 static struct vhost_dev *vuf_get_vhost(VirtIODevice *vdev) 278 { 279 VHostUserFS *fs = VHOST_USER_FS(vdev); 280 return &fs->vhost_dev; 281 } 282 283 static const VMStateDescription vuf_vmstate = { 284 .name = "vhost-user-fs", 285 .unmigratable = 1, 286 }; 287 288 static Property vuf_properties[] = { 289 DEFINE_PROP_CHR("chardev", VHostUserFS, conf.chardev), 290 DEFINE_PROP_STRING("tag", VHostUserFS, conf.tag), 291 DEFINE_PROP_UINT16("num-request-queues", VHostUserFS, 292 conf.num_request_queues, 1), 293 DEFINE_PROP_UINT16("queue-size", VHostUserFS, conf.queue_size, 128), 294 DEFINE_PROP_END_OF_LIST(), 295 }; 296 297 static void vuf_instance_init(Object *obj) 298 { 299 VHostUserFS *fs = VHOST_USER_FS(obj); 300 301 device_add_bootindex_property(obj, &fs->bootindex, "bootindex", 302 "/filesystem@0", DEVICE(obj)); 303 } 304 305 static void vuf_class_init(ObjectClass *klass, void *data) 306 { 307 DeviceClass *dc = DEVICE_CLASS(klass); 308 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 309 310 device_class_set_props(dc, vuf_properties); 311 dc->vmsd = &vuf_vmstate; 312 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 313 vdc->realize = vuf_device_realize; 314 vdc->unrealize = vuf_device_unrealize; 315 vdc->get_features = vuf_get_features; 316 vdc->get_config = vuf_get_config; 317 vdc->set_status = vuf_set_status; 318 vdc->guest_notifier_mask = vuf_guest_notifier_mask; 319 vdc->guest_notifier_pending = vuf_guest_notifier_pending; 320 vdc->get_vhost = vuf_get_vhost; 321 } 322 323 static const TypeInfo vuf_info = { 324 .name = TYPE_VHOST_USER_FS, 325 .parent = TYPE_VIRTIO_DEVICE, 326 .instance_size = sizeof(VHostUserFS), 327 .instance_init = vuf_instance_init, 328 .class_init = vuf_class_init, 329 }; 330 331 static void vuf_register_types(void) 332 { 333 type_register_static(&vuf_info); 334 } 335 336 type_init(vuf_register_types) 337