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 14873c3213SStefan Weil #include "hw/virtio.h" 15873c3213SStefan Weil #include "hw/pc.h" 16f4f61d27SAneesh Kumar K.V #include "qemu_socket.h" 17873c3213SStefan Weil #include "hw/virtio-pci.h" 18f4f61d27SAneesh Kumar K.V #include "virtio-9p.h" 19f4f61d27SAneesh Kumar K.V #include "fsdev/qemu-fsdev.h" 20f4f61d27SAneesh Kumar K.V #include "virtio-9p-xattr.h" 2139c0564eSVenkateswararao Jujjuri (JV) #include "virtio-9p-coth.h" 22f4f61d27SAneesh Kumar K.V 23f4f61d27SAneesh Kumar K.V static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features) 24f4f61d27SAneesh Kumar K.V { 25f4f61d27SAneesh Kumar K.V features |= 1 << VIRTIO_9P_MOUNT_TAG; 26f4f61d27SAneesh Kumar K.V return features; 27f4f61d27SAneesh Kumar K.V } 28f4f61d27SAneesh Kumar K.V 29f4f61d27SAneesh Kumar K.V static V9fsState *to_virtio_9p(VirtIODevice *vdev) 30f4f61d27SAneesh Kumar K.V { 31f4f61d27SAneesh Kumar K.V return (V9fsState *)vdev; 32f4f61d27SAneesh Kumar K.V } 33f4f61d27SAneesh Kumar K.V 34f4f61d27SAneesh Kumar K.V static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config) 35f4f61d27SAneesh Kumar K.V { 36f4f61d27SAneesh Kumar K.V struct virtio_9p_config *cfg; 37f4f61d27SAneesh Kumar K.V V9fsState *s = to_virtio_9p(vdev); 38f4f61d27SAneesh Kumar K.V 397267c094SAnthony Liguori cfg = g_malloc0(sizeof(struct virtio_9p_config) + 40f4f61d27SAneesh Kumar K.V s->tag_len); 41f4f61d27SAneesh Kumar K.V stw_raw(&cfg->tag_len, s->tag_len); 42f4f61d27SAneesh Kumar K.V memcpy(cfg->tag, s->tag, s->tag_len); 43f4f61d27SAneesh Kumar K.V memcpy(config, cfg, s->config_size); 447267c094SAnthony Liguori g_free(cfg); 45f4f61d27SAneesh Kumar K.V } 46f4f61d27SAneesh Kumar K.V 47f4f61d27SAneesh Kumar K.V VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) 48f4f61d27SAneesh Kumar K.V { 49f4f61d27SAneesh Kumar K.V V9fsState *s; 50f4f61d27SAneesh Kumar K.V int i, len; 51f4f61d27SAneesh Kumar K.V struct stat stat; 52f4f61d27SAneesh Kumar K.V FsTypeEntry *fse; 53*7cca27dfSM. Mohan Kumar V9fsPath path; 54f4f61d27SAneesh Kumar K.V 55f4f61d27SAneesh Kumar K.V s = (V9fsState *)virtio_common_init("virtio-9p", 56f4f61d27SAneesh Kumar K.V VIRTIO_ID_9P, 57f4f61d27SAneesh Kumar K.V sizeof(struct virtio_9p_config)+ 58f4f61d27SAneesh Kumar K.V MAX_TAG_LEN, 59f4f61d27SAneesh Kumar K.V sizeof(V9fsState)); 60f4f61d27SAneesh Kumar K.V /* initialize pdu allocator */ 61f4f61d27SAneesh Kumar K.V QLIST_INIT(&s->free_list); 62bccacf6cSAneesh Kumar K.V QLIST_INIT(&s->active_list); 63f4f61d27SAneesh Kumar K.V for (i = 0; i < (MAX_REQ - 1); i++) { 64f4f61d27SAneesh Kumar K.V QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next); 65f4f61d27SAneesh Kumar K.V } 66f4f61d27SAneesh Kumar K.V 67f4f61d27SAneesh Kumar K.V s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output); 68f4f61d27SAneesh Kumar K.V 69f4f61d27SAneesh Kumar K.V fse = get_fsdev_fsentry(conf->fsdev_id); 70f4f61d27SAneesh Kumar K.V 71f4f61d27SAneesh Kumar K.V if (!fse) { 72f4f61d27SAneesh Kumar K.V /* We don't have a fsdev identified by fsdev_id */ 73f4f61d27SAneesh Kumar K.V fprintf(stderr, "Virtio-9p device couldn't find fsdev with the " 74f4f61d27SAneesh Kumar K.V "id = %s\n", conf->fsdev_id ? conf->fsdev_id : "NULL"); 75f4f61d27SAneesh Kumar K.V exit(1); 76f4f61d27SAneesh Kumar K.V } 77f4f61d27SAneesh Kumar K.V 78f4f61d27SAneesh Kumar K.V if (!fse->path || !conf->tag) { 79f4f61d27SAneesh Kumar K.V /* we haven't specified a mount_tag or the path */ 80f4f61d27SAneesh Kumar K.V fprintf(stderr, "fsdev with id %s needs path " 81f4f61d27SAneesh Kumar K.V "and Virtio-9p device needs mount_tag arguments\n", 82f4f61d27SAneesh Kumar K.V conf->fsdev_id); 83f4f61d27SAneesh Kumar K.V exit(1); 84f4f61d27SAneesh Kumar K.V } 85f4f61d27SAneesh Kumar K.V 86f4f61d27SAneesh Kumar K.V if (!strcmp(fse->security_model, "passthrough")) { 87f4f61d27SAneesh Kumar K.V /* Files on the Fileserver set to client user credentials */ 88f4f61d27SAneesh Kumar K.V s->ctx.fs_sm = SM_PASSTHROUGH; 89f4f61d27SAneesh Kumar K.V s->ctx.xops = passthrough_xattr_ops; 90f4f61d27SAneesh Kumar K.V } else if (!strcmp(fse->security_model, "mapped")) { 91f4f61d27SAneesh Kumar K.V /* Files on the fileserver are set to QEMU credentials. 92f4f61d27SAneesh Kumar K.V * Client user credentials are saved in extended attributes. 93f4f61d27SAneesh Kumar K.V */ 94f4f61d27SAneesh Kumar K.V s->ctx.fs_sm = SM_MAPPED; 95f4f61d27SAneesh Kumar K.V s->ctx.xops = mapped_xattr_ops; 96f4f61d27SAneesh Kumar K.V } else if (!strcmp(fse->security_model, "none")) { 97f4f61d27SAneesh Kumar K.V /* 98f4f61d27SAneesh Kumar K.V * Files on the fileserver are set to QEMU credentials. 99f4f61d27SAneesh Kumar K.V */ 100f4f61d27SAneesh Kumar K.V s->ctx.fs_sm = SM_NONE; 101f4f61d27SAneesh Kumar K.V s->ctx.xops = none_xattr_ops; 102f4f61d27SAneesh Kumar K.V } else { 103f4f61d27SAneesh Kumar K.V fprintf(stderr, "Default to security_model=none. You may want" 104f4f61d27SAneesh Kumar K.V " enable advanced security model using " 105f4f61d27SAneesh Kumar K.V "security option:\n\t security_model=passthrough\n\t " 106f4f61d27SAneesh Kumar K.V "security_model=mapped\n"); 107f4f61d27SAneesh Kumar K.V s->ctx.fs_sm = SM_NONE; 108f4f61d27SAneesh Kumar K.V s->ctx.xops = none_xattr_ops; 109f4f61d27SAneesh Kumar K.V } 110f4f61d27SAneesh Kumar K.V 111d3ab98e6SAneesh Kumar K.V s->ctx.export_flags = fse->export_flags; 1127267c094SAnthony Liguori s->ctx.fs_root = g_strdup(fse->path); 113e06a765eSHarsh Prateek Bora s->ctx.exops.get_st_gen = NULL; 114e06a765eSHarsh Prateek Bora 115f4f61d27SAneesh Kumar K.V len = strlen(conf->tag); 116f4f61d27SAneesh Kumar K.V if (len > MAX_TAG_LEN) { 117a2f507d9SDaniel P. Berrange fprintf(stderr, "mount tag '%s' (%d bytes) is longer than " 118a2f507d9SDaniel P. Berrange "maximum (%d bytes)", conf->tag, len, MAX_TAG_LEN); 119a2f507d9SDaniel P. Berrange exit(1); 120f4f61d27SAneesh Kumar K.V } 121f4f61d27SAneesh Kumar K.V /* s->tag is non-NULL terminated string */ 1227267c094SAnthony Liguori s->tag = g_malloc(len); 123f4f61d27SAneesh Kumar K.V memcpy(s->tag, conf->tag, len); 124f4f61d27SAneesh Kumar K.V s->tag_len = len; 125f4f61d27SAneesh Kumar K.V s->ctx.uid = -1; 126532decb7SAneesh Kumar K.V s->ctx.flags = 0; 127f4f61d27SAneesh Kumar K.V 128f4f61d27SAneesh Kumar K.V s->ops = fse->ops; 129f4f61d27SAneesh Kumar K.V s->vdev.get_features = virtio_9p_get_features; 130*7cca27dfSM. Mohan Kumar s->config_size = sizeof(struct virtio_9p_config) + s->tag_len; 131f4f61d27SAneesh Kumar K.V s->vdev.get_config = virtio_9p_get_config; 1329e5b2247SAneesh Kumar K.V s->fid_list = NULL; 13302cb7f3aSAneesh Kumar K.V qemu_co_rwlock_init(&s->rename_lock); 134f4f61d27SAneesh Kumar K.V 1350174fe73SAneesh Kumar K.V if (s->ops->init(&s->ctx) < 0) { 1360174fe73SAneesh Kumar K.V fprintf(stderr, "Virtio-9p Failed to initialize fs-driver with id:%s" 1370174fe73SAneesh Kumar K.V " and export path:%s\n", conf->fsdev_id, s->ctx.fs_root); 1380174fe73SAneesh Kumar K.V exit(1); 1390174fe73SAneesh Kumar K.V } 14039c0564eSVenkateswararao Jujjuri (JV) if (v9fs_init_worker_threads() < 0) { 14139c0564eSVenkateswararao Jujjuri (JV) fprintf(stderr, "worker thread initialization failed\n"); 14239c0564eSVenkateswararao Jujjuri (JV) exit(1); 14339c0564eSVenkateswararao Jujjuri (JV) } 144*7cca27dfSM. Mohan Kumar 145*7cca27dfSM. Mohan Kumar /* 146*7cca27dfSM. Mohan Kumar * Check details of export path, We need to use fs driver 147*7cca27dfSM. Mohan Kumar * call back to do that. Since we are in the init path, we don't 148*7cca27dfSM. Mohan Kumar * use co-routines here. 149*7cca27dfSM. Mohan Kumar */ 150*7cca27dfSM. Mohan Kumar v9fs_path_init(&path); 151*7cca27dfSM. Mohan Kumar if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) { 152*7cca27dfSM. Mohan Kumar fprintf(stderr, 153*7cca27dfSM. Mohan Kumar "error in converting name to path %s", strerror(errno)); 154*7cca27dfSM. Mohan Kumar exit(1); 155*7cca27dfSM. Mohan Kumar } 156*7cca27dfSM. Mohan Kumar if (s->ops->lstat(&s->ctx, &path, &stat)) { 157*7cca27dfSM. Mohan Kumar fprintf(stderr, "share path %s does not exist\n", fse->path); 158*7cca27dfSM. Mohan Kumar exit(1); 159*7cca27dfSM. Mohan Kumar } else if (!S_ISDIR(stat.st_mode)) { 160*7cca27dfSM. Mohan Kumar fprintf(stderr, "share path %s is not a directory\n", fse->path); 161*7cca27dfSM. Mohan Kumar exit(1); 162*7cca27dfSM. Mohan Kumar } 163*7cca27dfSM. Mohan Kumar v9fs_path_free(&path); 164*7cca27dfSM. Mohan Kumar 165f4f61d27SAneesh Kumar K.V return &s->vdev; 166f4f61d27SAneesh Kumar K.V } 167f4f61d27SAneesh Kumar K.V 168f4f61d27SAneesh Kumar K.V static int virtio_9p_init_pci(PCIDevice *pci_dev) 169f4f61d27SAneesh Kumar K.V { 170f4f61d27SAneesh Kumar K.V VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); 171f4f61d27SAneesh Kumar K.V VirtIODevice *vdev; 172f4f61d27SAneesh Kumar K.V 173f4f61d27SAneesh Kumar K.V vdev = virtio_9p_init(&pci_dev->qdev, &proxy->fsconf); 174f4f61d27SAneesh Kumar K.V vdev->nvectors = proxy->nvectors; 175befeac45SMichael S. Tsirkin virtio_init_pci(proxy, vdev); 176f4f61d27SAneesh Kumar K.V /* make the actual value visible */ 177f4f61d27SAneesh Kumar K.V proxy->nvectors = vdev->nvectors; 178f4f61d27SAneesh Kumar K.V return 0; 179f4f61d27SAneesh Kumar K.V } 180f4f61d27SAneesh Kumar K.V 181f4f61d27SAneesh Kumar K.V static PCIDeviceInfo virtio_9p_info = { 182f4f61d27SAneesh Kumar K.V .qdev.name = "virtio-9p-pci", 183f4f61d27SAneesh Kumar K.V .qdev.size = sizeof(VirtIOPCIProxy), 184f4f61d27SAneesh Kumar K.V .init = virtio_9p_init_pci, 185befeac45SMichael S. Tsirkin .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET, 186befeac45SMichael S. Tsirkin .device_id = 0x1009, 187befeac45SMichael S. Tsirkin .revision = VIRTIO_PCI_ABI_VERSION, 188befeac45SMichael S. Tsirkin .class_id = 0x2, 189f4f61d27SAneesh Kumar K.V .qdev.props = (Property[]) { 1905745e38aSAneesh Kumar K.V DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1915745e38aSAneesh Kumar K.V VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 192f4f61d27SAneesh Kumar K.V DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 193f4f61d27SAneesh Kumar K.V DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), 194f4f61d27SAneesh Kumar K.V DEFINE_PROP_STRING("mount_tag", VirtIOPCIProxy, fsconf.tag), 195f4f61d27SAneesh Kumar K.V DEFINE_PROP_STRING("fsdev", VirtIOPCIProxy, fsconf.fsdev_id), 196f4f61d27SAneesh Kumar K.V DEFINE_PROP_END_OF_LIST(), 197f4f61d27SAneesh Kumar K.V } 198f4f61d27SAneesh Kumar K.V }; 199f4f61d27SAneesh Kumar K.V 200f4f61d27SAneesh Kumar K.V static void virtio_9p_register_devices(void) 201f4f61d27SAneesh Kumar K.V { 202f4f61d27SAneesh Kumar K.V pci_qdev_register(&virtio_9p_info); 2037a462745SAneesh Kumar K.V virtio_9p_set_fd_limit(); 204f4f61d27SAneesh Kumar K.V } 205f4f61d27SAneesh Kumar K.V 206f4f61d27SAneesh Kumar K.V device_init(virtio_9p_register_devices) 207