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 239d5b731dSJason Wang static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features, 249d5b731dSJason Wang Error **errp) 25f4f61d27SAneesh Kumar K.V { 260cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG); 27f4f61d27SAneesh Kumar K.V return features; 28f4f61d27SAneesh Kumar K.V } 29f4f61d27SAneesh Kumar K.V 30f4f61d27SAneesh Kumar K.V static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config) 31f4f61d27SAneesh Kumar K.V { 32e9a0152bSAneesh Kumar K.V int len; 33f4f61d27SAneesh Kumar K.V struct virtio_9p_config *cfg; 3413daf6caSKONRAD Frederic V9fsState *s = VIRTIO_9P(vdev); 35f4f61d27SAneesh Kumar K.V 36e9a0152bSAneesh Kumar K.V len = strlen(s->tag); 37e9a0152bSAneesh Kumar K.V cfg = g_malloc0(sizeof(struct virtio_9p_config) + len); 38d64ccb91SGreg Kurz virtio_stw_p(vdev, &cfg->tag_len, len); 39e9a0152bSAneesh Kumar K.V /* We don't copy the terminating null to config space */ 40e9a0152bSAneesh Kumar K.V memcpy(cfg->tag, s->tag, len); 41f4f61d27SAneesh Kumar K.V memcpy(config, cfg, s->config_size); 427267c094SAnthony Liguori g_free(cfg); 43f4f61d27SAneesh Kumar K.V } 44f4f61d27SAneesh Kumar K.V 454652f164SGreg Kurz static void virtio_9p_save(QEMUFile *f, void *opaque) 464652f164SGreg Kurz { 474652f164SGreg Kurz virtio_save(VIRTIO_DEVICE(opaque), f); 484652f164SGreg Kurz } 494652f164SGreg Kurz 504652f164SGreg Kurz static int virtio_9p_load(QEMUFile *f, void *opaque, int version_id) 514652f164SGreg Kurz { 524652f164SGreg Kurz return virtio_load(VIRTIO_DEVICE(opaque), f, version_id); 534652f164SGreg Kurz } 544652f164SGreg Kurz 5559be7522SAndreas Färber static void virtio_9p_device_realize(DeviceState *dev, Error **errp) 56f4f61d27SAneesh Kumar K.V { 5759be7522SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 5859be7522SAndreas Färber V9fsState *s = VIRTIO_9P(dev); 59f4f61d27SAneesh Kumar K.V int i, len; 60f4f61d27SAneesh Kumar K.V struct stat stat; 61fbcbf101SAneesh Kumar K.V FsDriverEntry *fse; 627cca27dfSM. Mohan Kumar V9fsPath path; 63f4f61d27SAneesh Kumar K.V 640f3657ecSAndreas Färber virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P, 65e7303c43SKONRAD Frederic sizeof(struct virtio_9p_config) + MAX_TAG_LEN); 66e8111e50SKONRAD Frederic 67f4f61d27SAneesh Kumar K.V /* initialize pdu allocator */ 68f4f61d27SAneesh Kumar K.V QLIST_INIT(&s->free_list); 69bccacf6cSAneesh Kumar K.V QLIST_INIT(&s->active_list); 70f4f61d27SAneesh Kumar K.V for (i = 0; i < (MAX_REQ - 1); i++) { 71f4f61d27SAneesh Kumar K.V QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next); 72*ad38ce9eSWei Liu s->pdus[i].s = s; 73f4f61d27SAneesh Kumar K.V } 74f4f61d27SAneesh Kumar K.V 75e8111e50SKONRAD Frederic s->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output); 76f4f61d27SAneesh Kumar K.V 7727915efbSAndreas Färber v9fs_path_init(&path); 7827915efbSAndreas Färber 79e8111e50SKONRAD Frederic fse = get_fsdev_fsentry(s->fsconf.fsdev_id); 80f4f61d27SAneesh Kumar K.V 81f4f61d27SAneesh Kumar K.V if (!fse) { 82f4f61d27SAneesh Kumar K.V /* We don't have a fsdev identified by fsdev_id */ 8359be7522SAndreas Färber error_setg(errp, "Virtio-9p device couldn't find fsdev with the " 8459be7522SAndreas Färber "id = %s", 85e8111e50SKONRAD Frederic s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL"); 8692304bf3SM. Mohan Kumar goto out; 87f4f61d27SAneesh Kumar K.V } 88f4f61d27SAneesh Kumar K.V 89e8111e50SKONRAD Frederic if (!s->fsconf.tag) { 9099519f0aSAneesh Kumar K.V /* we haven't specified a mount_tag */ 9159be7522SAndreas Färber error_setg(errp, "fsdev with id %s needs mount_tag arguments", 92e8111e50SKONRAD Frederic s->fsconf.fsdev_id); 9392304bf3SM. Mohan Kumar goto out; 94f4f61d27SAneesh Kumar K.V } 95f4f61d27SAneesh Kumar K.V 96d3ab98e6SAneesh Kumar K.V s->ctx.export_flags = fse->export_flags; 977267c094SAnthony Liguori s->ctx.fs_root = g_strdup(fse->path); 98e06a765eSHarsh Prateek Bora s->ctx.exops.get_st_gen = NULL; 99e8111e50SKONRAD Frederic len = strlen(s->fsconf.tag); 100e9a0152bSAneesh Kumar K.V if (len > MAX_TAG_LEN - 1) { 10159be7522SAndreas Färber error_setg(errp, "mount tag '%s' (%d bytes) is longer than " 102e8111e50SKONRAD Frederic "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1); 10392304bf3SM. Mohan Kumar goto out; 104f4f61d27SAneesh Kumar K.V } 105e9a0152bSAneesh Kumar K.V 10692304bf3SM. Mohan Kumar s->tag = g_strdup(s->fsconf.tag); 107f4f61d27SAneesh Kumar K.V s->ctx.uid = -1; 108f4f61d27SAneesh Kumar K.V 109f4f61d27SAneesh Kumar K.V s->ops = fse->ops; 110e9a0152bSAneesh Kumar K.V s->config_size = sizeof(struct virtio_9p_config) + len; 1119e5b2247SAneesh Kumar K.V s->fid_list = NULL; 11202cb7f3aSAneesh Kumar K.V qemu_co_rwlock_init(&s->rename_lock); 113f4f61d27SAneesh Kumar K.V 1140174fe73SAneesh Kumar K.V if (s->ops->init(&s->ctx) < 0) { 11559be7522SAndreas Färber error_setg(errp, "Virtio-9p Failed to initialize fs-driver with id:%s" 11659be7522SAndreas Färber " and export path:%s", s->fsconf.fsdev_id, s->ctx.fs_root); 11792304bf3SM. Mohan Kumar goto out; 1180174fe73SAneesh Kumar K.V } 1197cca27dfSM. Mohan Kumar 1207cca27dfSM. Mohan Kumar /* 1217cca27dfSM. Mohan Kumar * Check details of export path, We need to use fs driver 1227cca27dfSM. Mohan Kumar * call back to do that. Since we are in the init path, we don't 1237cca27dfSM. Mohan Kumar * use co-routines here. 1247cca27dfSM. Mohan Kumar */ 1257cca27dfSM. Mohan Kumar if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) { 12659be7522SAndreas Färber error_setg(errp, 1277cca27dfSM. Mohan Kumar "error in converting name to path %s", strerror(errno)); 12892304bf3SM. Mohan Kumar goto out; 1297cca27dfSM. Mohan Kumar } 1307cca27dfSM. Mohan Kumar if (s->ops->lstat(&s->ctx, &path, &stat)) { 13159be7522SAndreas Färber error_setg(errp, "share path %s does not exist", fse->path); 13292304bf3SM. Mohan Kumar goto out; 1337cca27dfSM. Mohan Kumar } else if (!S_ISDIR(stat.st_mode)) { 13459be7522SAndreas Färber error_setg(errp, "share path %s is not a directory", fse->path); 13592304bf3SM. Mohan Kumar goto out; 1367cca27dfSM. Mohan Kumar } 1377cca27dfSM. Mohan Kumar v9fs_path_free(&path); 1387cca27dfSM. Mohan Kumar 1394652f164SGreg Kurz register_savevm(dev, "virtio-9p", -1, 1, virtio_9p_save, virtio_9p_load, s); 14059be7522SAndreas Färber return; 14192304bf3SM. Mohan Kumar out: 14292304bf3SM. Mohan Kumar g_free(s->ctx.fs_root); 14392304bf3SM. Mohan Kumar g_free(s->tag); 14492304bf3SM. Mohan Kumar virtio_cleanup(vdev); 14592304bf3SM. Mohan Kumar v9fs_path_free(&path); 146e7303c43SKONRAD Frederic } 147e7303c43SKONRAD Frederic 1486cecf093SGreg Kurz static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp) 1496cecf093SGreg Kurz { 1506cecf093SGreg Kurz VirtIODevice *vdev = VIRTIO_DEVICE(dev); 1516cecf093SGreg Kurz V9fsState *s = VIRTIO_9P(dev); 1526cecf093SGreg Kurz 1536cecf093SGreg Kurz virtio_cleanup(vdev); 1546cecf093SGreg Kurz unregister_savevm(dev, "virtio-9p", s); 1556cecf093SGreg Kurz g_free(s->ctx.fs_root); 1566cecf093SGreg Kurz g_free(s->tag); 1576cecf093SGreg Kurz } 1586cecf093SGreg Kurz 159e7303c43SKONRAD Frederic /* virtio-9p device */ 160e7303c43SKONRAD Frederic 161e7303c43SKONRAD Frederic static Property virtio_9p_properties[] = { 16283a84878SShannon Zhao DEFINE_PROP_STRING("mount_tag", V9fsState, fsconf.tag), 16383a84878SShannon Zhao DEFINE_PROP_STRING("fsdev", V9fsState, fsconf.fsdev_id), 164e7303c43SKONRAD Frederic DEFINE_PROP_END_OF_LIST(), 165e7303c43SKONRAD Frederic }; 166e7303c43SKONRAD Frederic 167e7303c43SKONRAD Frederic static void virtio_9p_class_init(ObjectClass *klass, void *data) 168e7303c43SKONRAD Frederic { 169e7303c43SKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 170e7303c43SKONRAD Frederic VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 17159be7522SAndreas Färber 172e7303c43SKONRAD Frederic dc->props = virtio_9p_properties; 173125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 17459be7522SAndreas Färber vdc->realize = virtio_9p_device_realize; 1756cecf093SGreg Kurz vdc->unrealize = virtio_9p_device_unrealize; 176e7303c43SKONRAD Frederic vdc->get_features = virtio_9p_get_features; 177e7303c43SKONRAD Frederic vdc->get_config = virtio_9p_get_config; 178e7303c43SKONRAD Frederic } 179e7303c43SKONRAD Frederic 180e7303c43SKONRAD Frederic static const TypeInfo virtio_device_info = { 181e7303c43SKONRAD Frederic .name = TYPE_VIRTIO_9P, 182e7303c43SKONRAD Frederic .parent = TYPE_VIRTIO_DEVICE, 183e7303c43SKONRAD Frederic .instance_size = sizeof(V9fsState), 184e7303c43SKONRAD Frederic .class_init = virtio_9p_class_init, 185e7303c43SKONRAD Frederic }; 186e7303c43SKONRAD Frederic 187e7303c43SKONRAD Frederic static void virtio_9p_register_types(void) 188e7303c43SKONRAD Frederic { 189e7303c43SKONRAD Frederic type_register_static(&virtio_device_info); 190e7303c43SKONRAD Frederic } 191e7303c43SKONRAD Frederic 192e7303c43SKONRAD Frederic type_init(virtio_9p_register_types) 193