xref: /qemu/hw/9pfs/virtio-9p-device.c (revision 13744bd0a054bc7a4b1432cc8facd23d41a9806e)
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"
161de7afc9SPaolo Bonzini #include "qemu/sockets.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 {
36e9a0152bSAneesh Kumar K.V     int len;
37f4f61d27SAneesh Kumar K.V     struct virtio_9p_config *cfg;
38f4f61d27SAneesh Kumar K.V     V9fsState *s = to_virtio_9p(vdev);
39f4f61d27SAneesh Kumar K.V 
40e9a0152bSAneesh Kumar K.V     len = strlen(s->tag);
41e9a0152bSAneesh Kumar K.V     cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
42e9a0152bSAneesh Kumar K.V     stw_raw(&cfg->tag_len, len);
43e9a0152bSAneesh Kumar K.V     /* We don't copy the terminating null to config space */
44e9a0152bSAneesh Kumar K.V     memcpy(cfg->tag, s->tag, len);
45f4f61d27SAneesh Kumar K.V     memcpy(config, cfg, s->config_size);
467267c094SAnthony Liguori     g_free(cfg);
47f4f61d27SAneesh Kumar K.V }
48f4f61d27SAneesh Kumar K.V 
49f4f61d27SAneesh Kumar K.V VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
50f4f61d27SAneesh Kumar K.V {
51f4f61d27SAneesh Kumar K.V     V9fsState *s;
52f4f61d27SAneesh Kumar K.V     int i, len;
53f4f61d27SAneesh Kumar K.V     struct stat stat;
54fbcbf101SAneesh Kumar K.V     FsDriverEntry *fse;
557cca27dfSM. Mohan Kumar     V9fsPath path;
56f4f61d27SAneesh Kumar K.V 
57f4f61d27SAneesh Kumar K.V     s = (V9fsState *)virtio_common_init("virtio-9p",
58f4f61d27SAneesh Kumar K.V                                     VIRTIO_ID_9P,
59f4f61d27SAneesh Kumar K.V                                     sizeof(struct virtio_9p_config)+
60f4f61d27SAneesh Kumar K.V                                     MAX_TAG_LEN,
61f4f61d27SAneesh Kumar K.V                                     sizeof(V9fsState));
62f4f61d27SAneesh Kumar K.V     /* initialize pdu allocator */
63f4f61d27SAneesh Kumar K.V     QLIST_INIT(&s->free_list);
64bccacf6cSAneesh Kumar K.V     QLIST_INIT(&s->active_list);
65f4f61d27SAneesh Kumar K.V     for (i = 0; i < (MAX_REQ - 1); i++) {
66f4f61d27SAneesh Kumar K.V         QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
67f4f61d27SAneesh Kumar K.V     }
68f4f61d27SAneesh Kumar K.V 
69f4f61d27SAneesh Kumar K.V     s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
70f4f61d27SAneesh Kumar K.V 
71f4f61d27SAneesh Kumar K.V     fse = get_fsdev_fsentry(conf->fsdev_id);
72f4f61d27SAneesh Kumar K.V 
73f4f61d27SAneesh Kumar K.V     if (!fse) {
74f4f61d27SAneesh Kumar K.V         /* We don't have a fsdev identified by fsdev_id */
75f4f61d27SAneesh Kumar K.V         fprintf(stderr, "Virtio-9p device couldn't find fsdev with the "
76f4f61d27SAneesh Kumar K.V                 "id = %s\n", conf->fsdev_id ? conf->fsdev_id : "NULL");
77f4f61d27SAneesh Kumar K.V         exit(1);
78f4f61d27SAneesh Kumar K.V     }
79f4f61d27SAneesh Kumar K.V 
8099519f0aSAneesh Kumar K.V     if (!conf->tag) {
8199519f0aSAneesh Kumar K.V         /* we haven't specified a mount_tag */
8299519f0aSAneesh Kumar K.V         fprintf(stderr, "fsdev with id %s needs mount_tag arguments\n",
83f4f61d27SAneesh Kumar K.V                 conf->fsdev_id);
84f4f61d27SAneesh Kumar K.V         exit(1);
85f4f61d27SAneesh Kumar K.V     }
86f4f61d27SAneesh Kumar K.V 
87d3ab98e6SAneesh Kumar K.V     s->ctx.export_flags = fse->export_flags;
8899519f0aSAneesh Kumar K.V     if (fse->path) {
897267c094SAnthony Liguori         s->ctx.fs_root = g_strdup(fse->path);
9099519f0aSAneesh Kumar K.V     } else {
9199519f0aSAneesh Kumar K.V         s->ctx.fs_root = NULL;
9299519f0aSAneesh Kumar K.V     }
93e06a765eSHarsh Prateek Bora     s->ctx.exops.get_st_gen = NULL;
94f4f61d27SAneesh Kumar K.V     len = strlen(conf->tag);
95e9a0152bSAneesh Kumar K.V     if (len > MAX_TAG_LEN - 1) {
96a2f507d9SDaniel P. Berrange         fprintf(stderr, "mount tag '%s' (%d bytes) is longer than "
97e9a0152bSAneesh Kumar K.V                 "maximum (%d bytes)", conf->tag, len, MAX_TAG_LEN - 1);
98a2f507d9SDaniel P. Berrange         exit(1);
99f4f61d27SAneesh Kumar K.V     }
100e9a0152bSAneesh Kumar K.V 
101e9a0152bSAneesh Kumar K.V     s->tag = strdup(conf->tag);
102f4f61d27SAneesh Kumar K.V     s->ctx.uid = -1;
103f4f61d27SAneesh Kumar K.V 
104f4f61d27SAneesh Kumar K.V     s->ops = fse->ops;
105f4f61d27SAneesh Kumar K.V     s->vdev.get_features = virtio_9p_get_features;
106e9a0152bSAneesh Kumar K.V     s->config_size = sizeof(struct virtio_9p_config) + len;
107f4f61d27SAneesh Kumar K.V     s->vdev.get_config = virtio_9p_get_config;
1089e5b2247SAneesh Kumar K.V     s->fid_list = NULL;
10902cb7f3aSAneesh Kumar K.V     qemu_co_rwlock_init(&s->rename_lock);
110f4f61d27SAneesh Kumar K.V 
1110174fe73SAneesh Kumar K.V     if (s->ops->init(&s->ctx) < 0) {
1120174fe73SAneesh Kumar K.V         fprintf(stderr, "Virtio-9p Failed to initialize fs-driver with id:%s"
1130174fe73SAneesh Kumar K.V                 " and export path:%s\n", conf->fsdev_id, s->ctx.fs_root);
1140174fe73SAneesh Kumar K.V         exit(1);
1150174fe73SAneesh Kumar K.V     }
11639c0564eSVenkateswararao Jujjuri (JV)     if (v9fs_init_worker_threads() < 0) {
11739c0564eSVenkateswararao Jujjuri (JV)         fprintf(stderr, "worker thread initialization failed\n");
11839c0564eSVenkateswararao Jujjuri (JV)         exit(1);
11939c0564eSVenkateswararao Jujjuri (JV)     }
1207cca27dfSM. Mohan Kumar 
1217cca27dfSM. Mohan Kumar     /*
1227cca27dfSM. Mohan Kumar      * Check details of export path, We need to use fs driver
1237cca27dfSM. Mohan Kumar      * call back to do that. Since we are in the init path, we don't
1247cca27dfSM. Mohan Kumar      * use co-routines here.
1257cca27dfSM. Mohan Kumar      */
1267cca27dfSM. Mohan Kumar     v9fs_path_init(&path);
1277cca27dfSM. Mohan Kumar     if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
1287cca27dfSM. Mohan Kumar         fprintf(stderr,
1297cca27dfSM. Mohan Kumar                 "error in converting name to path %s", strerror(errno));
1307cca27dfSM. Mohan Kumar         exit(1);
1317cca27dfSM. Mohan Kumar     }
1327cca27dfSM. Mohan Kumar     if (s->ops->lstat(&s->ctx, &path, &stat)) {
1337cca27dfSM. Mohan Kumar         fprintf(stderr, "share path %s does not exist\n", fse->path);
1347cca27dfSM. Mohan Kumar         exit(1);
1357cca27dfSM. Mohan Kumar     } else if (!S_ISDIR(stat.st_mode)) {
1367cca27dfSM. Mohan Kumar         fprintf(stderr, "share path %s is not a directory\n", fse->path);
1377cca27dfSM. Mohan Kumar         exit(1);
1387cca27dfSM. Mohan Kumar     }
1397cca27dfSM. Mohan Kumar     v9fs_path_free(&path);
1407cca27dfSM. Mohan Kumar 
141f4f61d27SAneesh Kumar K.V     return &s->vdev;
142f4f61d27SAneesh Kumar K.V }
143f4f61d27SAneesh Kumar K.V 
144f4f61d27SAneesh Kumar K.V static int virtio_9p_init_pci(PCIDevice *pci_dev)
145f4f61d27SAneesh Kumar K.V {
146f4f61d27SAneesh Kumar K.V     VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
147f4f61d27SAneesh Kumar K.V     VirtIODevice *vdev;
148f4f61d27SAneesh Kumar K.V 
149f4f61d27SAneesh Kumar K.V     vdev = virtio_9p_init(&pci_dev->qdev, &proxy->fsconf);
150f4f61d27SAneesh Kumar K.V     vdev->nvectors = proxy->nvectors;
151befeac45SMichael S. Tsirkin     virtio_init_pci(proxy, vdev);
152f4f61d27SAneesh Kumar K.V     /* make the actual value visible */
153f4f61d27SAneesh Kumar K.V     proxy->nvectors = vdev->nvectors;
154f4f61d27SAneesh Kumar K.V     return 0;
155f4f61d27SAneesh Kumar K.V }
156f4f61d27SAneesh Kumar K.V 
15740021f08SAnthony Liguori static Property virtio_9p_properties[] = {
158e855761cSAnthony Liguori     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
159f4f61d27SAneesh Kumar K.V     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
160f4f61d27SAneesh Kumar K.V     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
161f4f61d27SAneesh Kumar K.V     DEFINE_PROP_STRING("mount_tag", VirtIOPCIProxy, fsconf.tag),
162f4f61d27SAneesh Kumar K.V     DEFINE_PROP_STRING("fsdev", VirtIOPCIProxy, fsconf.fsdev_id),
163f4f61d27SAneesh Kumar K.V     DEFINE_PROP_END_OF_LIST(),
16440021f08SAnthony Liguori };
16540021f08SAnthony Liguori 
16640021f08SAnthony Liguori static void virtio_9p_class_init(ObjectClass *klass, void *data)
16740021f08SAnthony Liguori {
16839bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
16940021f08SAnthony Liguori     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
17040021f08SAnthony Liguori 
17140021f08SAnthony Liguori     k->init = virtio_9p_init_pci;
17240021f08SAnthony Liguori     k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
173*13744bd0SPaolo Bonzini     k->device_id = PCI_DEVICE_ID_VIRTIO_9P;
17440021f08SAnthony Liguori     k->revision = VIRTIO_PCI_ABI_VERSION;
17540021f08SAnthony Liguori     k->class_id = 0x2;
17639bffca2SAnthony Liguori     dc->props = virtio_9p_properties;
17739bffca2SAnthony Liguori     dc->reset = virtio_pci_reset;
17840021f08SAnthony Liguori }
17940021f08SAnthony Liguori 
18039bffca2SAnthony Liguori static TypeInfo virtio_9p_info = {
18140021f08SAnthony Liguori     .name          = "virtio-9p-pci",
18239bffca2SAnthony Liguori     .parent        = TYPE_PCI_DEVICE,
18339bffca2SAnthony Liguori     .instance_size = sizeof(VirtIOPCIProxy),
18440021f08SAnthony Liguori     .class_init    = virtio_9p_class_init,
185f4f61d27SAneesh Kumar K.V };
186f4f61d27SAneesh Kumar K.V 
18783f7d43aSAndreas Färber static void virtio_9p_register_types(void)
188f4f61d27SAneesh Kumar K.V {
18939bffca2SAnthony Liguori     type_register_static(&virtio_9p_info);
1907a462745SAneesh Kumar K.V     virtio_9p_set_fd_limit();
191f4f61d27SAneesh Kumar K.V }
192f4f61d27SAneesh Kumar K.V 
19383f7d43aSAndreas Färber type_init(virtio_9p_register_types)
194