1f4f61d27SAneesh Kumar K.V /* 2f4f61d27SAneesh Kumar K.V * Virtio 9p backend 3f4f61d27SAneesh Kumar K.V * 4f4f61d27SAneesh Kumar K.V * Copyright IBM, Corp. 2010 5f4f61d27SAneesh Kumar K.V * 6f4f61d27SAneesh Kumar K.V * Authors: 7f4f61d27SAneesh Kumar K.V * Anthony Liguori <aliguori@us.ibm.com> 8f4f61d27SAneesh Kumar K.V * 9f4f61d27SAneesh Kumar K.V * This work is licensed under the terms of the GNU GPL, version 2. See 10f4f61d27SAneesh Kumar K.V * the COPYING file in the top-level directory. 11f4f61d27SAneesh Kumar K.V * 12f4f61d27SAneesh Kumar K.V */ 13f4f61d27SAneesh Kumar K.V 140d09e41aSPaolo Bonzini #include "hw/virtio/virtio.h" 150d09e41aSPaolo Bonzini #include "hw/i386/pc.h" 161de7afc9SPaolo Bonzini #include "qemu/sockets.h" 17f4f61d27SAneesh Kumar K.V #include "virtio-9p.h" 18f4f61d27SAneesh Kumar K.V #include "fsdev/qemu-fsdev.h" 19267ae092SWei Liu #include "9p-xattr.h" 20fe52840cSWei Liu #include "coth.h" 21d64ccb91SGreg Kurz #include "hw/virtio/virtio-access.h" 22f4f61d27SAneesh Kumar K.V 23*0d3716b4SWei Liu void virtio_9p_push_and_notify(V9fsPDU *pdu) 24*0d3716b4SWei Liu { 25*0d3716b4SWei Liu V9fsState *s = pdu->s; 26*0d3716b4SWei Liu 27*0d3716b4SWei Liu /* push onto queue and notify */ 28*0d3716b4SWei Liu virtqueue_push(s->vq, &pdu->elem, pdu->size); 29*0d3716b4SWei Liu 30*0d3716b4SWei Liu /* FIXME: we should batch these completions */ 31*0d3716b4SWei Liu virtio_notify(VIRTIO_DEVICE(s), s->vq); 32*0d3716b4SWei Liu } 33*0d3716b4SWei Liu 349d5b731dSJason Wang static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features, 359d5b731dSJason Wang Error **errp) 36f4f61d27SAneesh Kumar K.V { 370cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG); 38f4f61d27SAneesh Kumar K.V return features; 39f4f61d27SAneesh Kumar K.V } 40f4f61d27SAneesh Kumar K.V 41f4f61d27SAneesh Kumar K.V static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config) 42f4f61d27SAneesh Kumar K.V { 43e9a0152bSAneesh Kumar K.V int len; 44f4f61d27SAneesh Kumar K.V struct virtio_9p_config *cfg; 4513daf6caSKONRAD Frederic V9fsState *s = VIRTIO_9P(vdev); 46f4f61d27SAneesh Kumar K.V 47e9a0152bSAneesh Kumar K.V len = strlen(s->tag); 48e9a0152bSAneesh Kumar K.V cfg = g_malloc0(sizeof(struct virtio_9p_config) + len); 49d64ccb91SGreg Kurz virtio_stw_p(vdev, &cfg->tag_len, len); 50e9a0152bSAneesh Kumar K.V /* We don't copy the terminating null to config space */ 51e9a0152bSAneesh Kumar K.V memcpy(cfg->tag, s->tag, len); 52f4f61d27SAneesh Kumar K.V memcpy(config, cfg, s->config_size); 537267c094SAnthony Liguori g_free(cfg); 54f4f61d27SAneesh Kumar K.V } 55f4f61d27SAneesh Kumar K.V 564652f164SGreg Kurz static void virtio_9p_save(QEMUFile *f, void *opaque) 574652f164SGreg Kurz { 584652f164SGreg Kurz virtio_save(VIRTIO_DEVICE(opaque), f); 594652f164SGreg Kurz } 604652f164SGreg Kurz 614652f164SGreg Kurz static int virtio_9p_load(QEMUFile *f, void *opaque, int version_id) 624652f164SGreg Kurz { 634652f164SGreg Kurz return virtio_load(VIRTIO_DEVICE(opaque), f, version_id); 644652f164SGreg Kurz } 654652f164SGreg Kurz 6659be7522SAndreas Färber static void virtio_9p_device_realize(DeviceState *dev, Error **errp) 67f4f61d27SAneesh Kumar K.V { 6859be7522SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 6959be7522SAndreas Färber V9fsState *s = VIRTIO_9P(dev); 70f4f61d27SAneesh Kumar K.V int i, len; 71f4f61d27SAneesh Kumar K.V struct stat stat; 72fbcbf101SAneesh Kumar K.V FsDriverEntry *fse; 737cca27dfSM. Mohan Kumar V9fsPath path; 74f4f61d27SAneesh Kumar K.V 750f3657ecSAndreas Färber virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P, 76e7303c43SKONRAD Frederic sizeof(struct virtio_9p_config) + MAX_TAG_LEN); 77e8111e50SKONRAD Frederic 78f4f61d27SAneesh Kumar K.V /* initialize pdu allocator */ 79f4f61d27SAneesh Kumar K.V QLIST_INIT(&s->free_list); 80bccacf6cSAneesh Kumar K.V QLIST_INIT(&s->active_list); 81f4f61d27SAneesh Kumar K.V for (i = 0; i < (MAX_REQ - 1); i++) { 82f4f61d27SAneesh Kumar K.V QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next); 83ad38ce9eSWei Liu s->pdus[i].s = s; 84f4f61d27SAneesh Kumar K.V } 85f4f61d27SAneesh Kumar K.V 86e8111e50SKONRAD Frederic s->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output); 87f4f61d27SAneesh Kumar K.V 8827915efbSAndreas Färber v9fs_path_init(&path); 8927915efbSAndreas Färber 90e8111e50SKONRAD Frederic fse = get_fsdev_fsentry(s->fsconf.fsdev_id); 91f4f61d27SAneesh Kumar K.V 92f4f61d27SAneesh Kumar K.V if (!fse) { 93f4f61d27SAneesh Kumar K.V /* We don't have a fsdev identified by fsdev_id */ 9459be7522SAndreas Färber error_setg(errp, "Virtio-9p device couldn't find fsdev with the " 9559be7522SAndreas Färber "id = %s", 96e8111e50SKONRAD Frederic s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL"); 9792304bf3SM. Mohan Kumar goto out; 98f4f61d27SAneesh Kumar K.V } 99f4f61d27SAneesh Kumar K.V 100e8111e50SKONRAD Frederic if (!s->fsconf.tag) { 10199519f0aSAneesh Kumar K.V /* we haven't specified a mount_tag */ 10259be7522SAndreas Färber error_setg(errp, "fsdev with id %s needs mount_tag arguments", 103e8111e50SKONRAD Frederic s->fsconf.fsdev_id); 10492304bf3SM. Mohan Kumar goto out; 105f4f61d27SAneesh Kumar K.V } 106f4f61d27SAneesh Kumar K.V 107d3ab98e6SAneesh Kumar K.V s->ctx.export_flags = fse->export_flags; 1087267c094SAnthony Liguori s->ctx.fs_root = g_strdup(fse->path); 109e06a765eSHarsh Prateek Bora s->ctx.exops.get_st_gen = NULL; 110e8111e50SKONRAD Frederic len = strlen(s->fsconf.tag); 111e9a0152bSAneesh Kumar K.V if (len > MAX_TAG_LEN - 1) { 11259be7522SAndreas Färber error_setg(errp, "mount tag '%s' (%d bytes) is longer than " 113e8111e50SKONRAD Frederic "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1); 11492304bf3SM. Mohan Kumar goto out; 115f4f61d27SAneesh Kumar K.V } 116e9a0152bSAneesh Kumar K.V 11792304bf3SM. Mohan Kumar s->tag = g_strdup(s->fsconf.tag); 118f4f61d27SAneesh Kumar K.V s->ctx.uid = -1; 119f4f61d27SAneesh Kumar K.V 120f4f61d27SAneesh Kumar K.V s->ops = fse->ops; 121e9a0152bSAneesh Kumar K.V s->config_size = sizeof(struct virtio_9p_config) + len; 1229e5b2247SAneesh Kumar K.V s->fid_list = NULL; 12302cb7f3aSAneesh Kumar K.V qemu_co_rwlock_init(&s->rename_lock); 124f4f61d27SAneesh Kumar K.V 1250174fe73SAneesh Kumar K.V if (s->ops->init(&s->ctx) < 0) { 12659be7522SAndreas Färber error_setg(errp, "Virtio-9p Failed to initialize fs-driver with id:%s" 12759be7522SAndreas Färber " and export path:%s", s->fsconf.fsdev_id, s->ctx.fs_root); 12892304bf3SM. Mohan Kumar goto out; 1290174fe73SAneesh Kumar K.V } 1307cca27dfSM. Mohan Kumar 1317cca27dfSM. Mohan Kumar /* 1327cca27dfSM. Mohan Kumar * Check details of export path, We need to use fs driver 1337cca27dfSM. Mohan Kumar * call back to do that. Since we are in the init path, we don't 1347cca27dfSM. Mohan Kumar * use co-routines here. 1357cca27dfSM. Mohan Kumar */ 1367cca27dfSM. Mohan Kumar if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) { 13759be7522SAndreas Färber error_setg(errp, 1387cca27dfSM. Mohan Kumar "error in converting name to path %s", strerror(errno)); 13992304bf3SM. Mohan Kumar goto out; 1407cca27dfSM. Mohan Kumar } 1417cca27dfSM. Mohan Kumar if (s->ops->lstat(&s->ctx, &path, &stat)) { 14259be7522SAndreas Färber error_setg(errp, "share path %s does not exist", fse->path); 14392304bf3SM. Mohan Kumar goto out; 1447cca27dfSM. Mohan Kumar } else if (!S_ISDIR(stat.st_mode)) { 14559be7522SAndreas Färber error_setg(errp, "share path %s is not a directory", fse->path); 14692304bf3SM. Mohan Kumar goto out; 1477cca27dfSM. Mohan Kumar } 1487cca27dfSM. Mohan Kumar v9fs_path_free(&path); 1497cca27dfSM. Mohan Kumar 1504652f164SGreg Kurz register_savevm(dev, "virtio-9p", -1, 1, virtio_9p_save, virtio_9p_load, s); 15159be7522SAndreas Färber return; 15292304bf3SM. Mohan Kumar out: 15392304bf3SM. Mohan Kumar g_free(s->ctx.fs_root); 15492304bf3SM. Mohan Kumar g_free(s->tag); 15592304bf3SM. Mohan Kumar virtio_cleanup(vdev); 15692304bf3SM. Mohan Kumar v9fs_path_free(&path); 157e7303c43SKONRAD Frederic } 158e7303c43SKONRAD Frederic 1596cecf093SGreg Kurz static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp) 1606cecf093SGreg Kurz { 1616cecf093SGreg Kurz VirtIODevice *vdev = VIRTIO_DEVICE(dev); 1626cecf093SGreg Kurz V9fsState *s = VIRTIO_9P(dev); 1636cecf093SGreg Kurz 1646cecf093SGreg Kurz virtio_cleanup(vdev); 1656cecf093SGreg Kurz unregister_savevm(dev, "virtio-9p", s); 1666cecf093SGreg Kurz g_free(s->ctx.fs_root); 1676cecf093SGreg Kurz g_free(s->tag); 1686cecf093SGreg Kurz } 1696cecf093SGreg Kurz 170fe9fa96dSWei Liu ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset, 171fe9fa96dSWei Liu const char *fmt, va_list ap) 172fe9fa96dSWei Liu { 173fe9fa96dSWei Liu return v9fs_iov_vmarshal(pdu->elem.in_sg, pdu->elem.in_num, 174fe9fa96dSWei Liu offset, 1, fmt, ap); 175fe9fa96dSWei Liu } 176fe9fa96dSWei Liu 177fe9fa96dSWei Liu ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset, 178fe9fa96dSWei Liu const char *fmt, va_list ap) 179fe9fa96dSWei Liu { 180fe9fa96dSWei Liu return v9fs_iov_vunmarshal(pdu->elem.out_sg, pdu->elem.out_num, 181fe9fa96dSWei Liu offset, 1, fmt, ap); 182fe9fa96dSWei Liu } 183fe9fa96dSWei Liu 184592707afSWei Liu void virtio_init_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov, 185592707afSWei Liu unsigned int *pniov, bool is_write) 186592707afSWei Liu { 187592707afSWei Liu if (is_write) { 188592707afSWei Liu *piov = pdu->elem.out_sg; 189592707afSWei Liu *pniov = pdu->elem.out_num; 190592707afSWei Liu } else { 191592707afSWei Liu *piov = pdu->elem.in_sg; 192592707afSWei Liu *pniov = pdu->elem.in_num; 193592707afSWei Liu } 194592707afSWei Liu } 195592707afSWei Liu 196e7303c43SKONRAD Frederic /* virtio-9p device */ 197e7303c43SKONRAD Frederic 198e7303c43SKONRAD Frederic static Property virtio_9p_properties[] = { 19983a84878SShannon Zhao DEFINE_PROP_STRING("mount_tag", V9fsState, fsconf.tag), 20083a84878SShannon Zhao DEFINE_PROP_STRING("fsdev", V9fsState, fsconf.fsdev_id), 201e7303c43SKONRAD Frederic DEFINE_PROP_END_OF_LIST(), 202e7303c43SKONRAD Frederic }; 203e7303c43SKONRAD Frederic 204e7303c43SKONRAD Frederic static void virtio_9p_class_init(ObjectClass *klass, void *data) 205e7303c43SKONRAD Frederic { 206e7303c43SKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 207e7303c43SKONRAD Frederic VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 20859be7522SAndreas Färber 209e7303c43SKONRAD Frederic dc->props = virtio_9p_properties; 210125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 21159be7522SAndreas Färber vdc->realize = virtio_9p_device_realize; 2126cecf093SGreg Kurz vdc->unrealize = virtio_9p_device_unrealize; 213e7303c43SKONRAD Frederic vdc->get_features = virtio_9p_get_features; 214e7303c43SKONRAD Frederic vdc->get_config = virtio_9p_get_config; 215e7303c43SKONRAD Frederic } 216e7303c43SKONRAD Frederic 217e7303c43SKONRAD Frederic static const TypeInfo virtio_device_info = { 218e7303c43SKONRAD Frederic .name = TYPE_VIRTIO_9P, 219e7303c43SKONRAD Frederic .parent = TYPE_VIRTIO_DEVICE, 220e7303c43SKONRAD Frederic .instance_size = sizeof(V9fsState), 221e7303c43SKONRAD Frederic .class_init = virtio_9p_class_init, 222e7303c43SKONRAD Frederic }; 223e7303c43SKONRAD Frederic 224e7303c43SKONRAD Frederic static void virtio_9p_register_types(void) 225e7303c43SKONRAD Frederic { 226e7303c43SKONRAD Frederic type_register_static(&virtio_device_info); 227e7303c43SKONRAD Frederic } 228e7303c43SKONRAD Frederic 229e7303c43SKONRAD Frederic type_init(virtio_9p_register_types) 230