xref: /qemu/hw/9pfs/virtio-9p-device.c (revision befeac45d4d9afb587eca9a27d975db4a7950960)
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"
21f4f61d27SAneesh Kumar K.V 
22f4f61d27SAneesh Kumar K.V static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
23f4f61d27SAneesh Kumar K.V {
24f4f61d27SAneesh Kumar K.V     features |= 1 << VIRTIO_9P_MOUNT_TAG;
25f4f61d27SAneesh Kumar K.V     return features;
26f4f61d27SAneesh Kumar K.V }
27f4f61d27SAneesh Kumar K.V 
28f4f61d27SAneesh Kumar K.V static V9fsState *to_virtio_9p(VirtIODevice *vdev)
29f4f61d27SAneesh Kumar K.V {
30f4f61d27SAneesh Kumar K.V     return (V9fsState *)vdev;
31f4f61d27SAneesh Kumar K.V }
32f4f61d27SAneesh Kumar K.V 
33f4f61d27SAneesh Kumar K.V static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
34f4f61d27SAneesh Kumar K.V {
35f4f61d27SAneesh Kumar K.V     struct virtio_9p_config *cfg;
36f4f61d27SAneesh Kumar K.V     V9fsState *s = to_virtio_9p(vdev);
37f4f61d27SAneesh Kumar K.V 
38f4f61d27SAneesh Kumar K.V     cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
39f4f61d27SAneesh Kumar K.V                         s->tag_len);
40f4f61d27SAneesh Kumar K.V     stw_raw(&cfg->tag_len, s->tag_len);
41f4f61d27SAneesh Kumar K.V     memcpy(cfg->tag, s->tag, s->tag_len);
42f4f61d27SAneesh Kumar K.V     memcpy(config, cfg, s->config_size);
43f4f61d27SAneesh Kumar K.V     qemu_free(cfg);
44f4f61d27SAneesh Kumar K.V }
45f4f61d27SAneesh Kumar K.V 
46f4f61d27SAneesh Kumar K.V VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
47f4f61d27SAneesh Kumar K.V  {
48f4f61d27SAneesh Kumar K.V     V9fsState *s;
49f4f61d27SAneesh Kumar K.V     int i, len;
50f4f61d27SAneesh Kumar K.V     struct stat stat;
51f4f61d27SAneesh Kumar K.V     FsTypeEntry *fse;
52f4f61d27SAneesh Kumar K.V 
53f4f61d27SAneesh Kumar K.V 
54f4f61d27SAneesh Kumar K.V     s = (V9fsState *)virtio_common_init("virtio-9p",
55f4f61d27SAneesh Kumar K.V                                     VIRTIO_ID_9P,
56f4f61d27SAneesh Kumar K.V                                     sizeof(struct virtio_9p_config)+
57f4f61d27SAneesh Kumar K.V                                     MAX_TAG_LEN,
58f4f61d27SAneesh Kumar K.V                                     sizeof(V9fsState));
59f4f61d27SAneesh Kumar K.V 
60f4f61d27SAneesh Kumar K.V     /* initialize pdu allocator */
61f4f61d27SAneesh Kumar K.V     QLIST_INIT(&s->free_list);
62f4f61d27SAneesh Kumar K.V     for (i = 0; i < (MAX_REQ - 1); i++) {
63f4f61d27SAneesh Kumar K.V         QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
64f4f61d27SAneesh Kumar K.V     }
65f4f61d27SAneesh Kumar K.V 
66f4f61d27SAneesh Kumar K.V     s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
67f4f61d27SAneesh Kumar K.V 
68f4f61d27SAneesh Kumar K.V     fse = get_fsdev_fsentry(conf->fsdev_id);
69f4f61d27SAneesh Kumar K.V 
70f4f61d27SAneesh Kumar K.V     if (!fse) {
71f4f61d27SAneesh Kumar K.V         /* We don't have a fsdev identified by fsdev_id */
72f4f61d27SAneesh Kumar K.V         fprintf(stderr, "Virtio-9p device couldn't find fsdev with the "
73f4f61d27SAneesh Kumar K.V                 "id = %s\n", conf->fsdev_id ? conf->fsdev_id : "NULL");
74f4f61d27SAneesh Kumar K.V         exit(1);
75f4f61d27SAneesh Kumar K.V     }
76f4f61d27SAneesh Kumar K.V 
77f4f61d27SAneesh Kumar K.V     if (!fse->path || !conf->tag) {
78f4f61d27SAneesh Kumar K.V         /* we haven't specified a mount_tag or the path */
79f4f61d27SAneesh Kumar K.V         fprintf(stderr, "fsdev with id %s needs path "
80f4f61d27SAneesh Kumar K.V                 "and Virtio-9p device needs mount_tag arguments\n",
81f4f61d27SAneesh Kumar K.V                 conf->fsdev_id);
82f4f61d27SAneesh Kumar K.V         exit(1);
83f4f61d27SAneesh Kumar K.V     }
84f4f61d27SAneesh Kumar K.V 
85f4f61d27SAneesh Kumar K.V     if (!strcmp(fse->security_model, "passthrough")) {
86f4f61d27SAneesh Kumar K.V         /* Files on the Fileserver set to client user credentials */
87f4f61d27SAneesh Kumar K.V         s->ctx.fs_sm = SM_PASSTHROUGH;
88f4f61d27SAneesh Kumar K.V         s->ctx.xops = passthrough_xattr_ops;
89f4f61d27SAneesh Kumar K.V     } else if (!strcmp(fse->security_model, "mapped")) {
90f4f61d27SAneesh Kumar K.V         /* Files on the fileserver are set to QEMU credentials.
91f4f61d27SAneesh Kumar K.V          * Client user credentials are saved in extended attributes.
92f4f61d27SAneesh Kumar K.V          */
93f4f61d27SAneesh Kumar K.V         s->ctx.fs_sm = SM_MAPPED;
94f4f61d27SAneesh Kumar K.V         s->ctx.xops = mapped_xattr_ops;
95f4f61d27SAneesh Kumar K.V     } else if (!strcmp(fse->security_model, "none")) {
96f4f61d27SAneesh Kumar K.V         /*
97f4f61d27SAneesh Kumar K.V          * Files on the fileserver are set to QEMU credentials.
98f4f61d27SAneesh Kumar K.V          */
99f4f61d27SAneesh Kumar K.V         s->ctx.fs_sm = SM_NONE;
100f4f61d27SAneesh Kumar K.V         s->ctx.xops = none_xattr_ops;
101f4f61d27SAneesh Kumar K.V     } else {
102f4f61d27SAneesh Kumar K.V         fprintf(stderr, "Default to security_model=none. You may want"
103f4f61d27SAneesh Kumar K.V                 " enable advanced security model using "
104f4f61d27SAneesh Kumar K.V                 "security option:\n\t security_model=passthrough\n\t "
105f4f61d27SAneesh Kumar K.V                 "security_model=mapped\n");
106f4f61d27SAneesh Kumar K.V         s->ctx.fs_sm = SM_NONE;
107f4f61d27SAneesh Kumar K.V         s->ctx.xops = none_xattr_ops;
108f4f61d27SAneesh Kumar K.V     }
109f4f61d27SAneesh Kumar K.V 
110f4f61d27SAneesh Kumar K.V     if (lstat(fse->path, &stat)) {
111f4f61d27SAneesh Kumar K.V         fprintf(stderr, "share path %s does not exist\n", fse->path);
112f4f61d27SAneesh Kumar K.V         exit(1);
113f4f61d27SAneesh Kumar K.V     } else if (!S_ISDIR(stat.st_mode)) {
114f4f61d27SAneesh Kumar K.V         fprintf(stderr, "share path %s is not a directory\n", fse->path);
115f4f61d27SAneesh Kumar K.V         exit(1);
116f4f61d27SAneesh Kumar K.V     }
117f4f61d27SAneesh Kumar K.V 
118f4f61d27SAneesh Kumar K.V     s->ctx.fs_root = qemu_strdup(fse->path);
119f4f61d27SAneesh Kumar K.V     len = strlen(conf->tag);
120f4f61d27SAneesh Kumar K.V     if (len > MAX_TAG_LEN) {
121f4f61d27SAneesh Kumar K.V         len = MAX_TAG_LEN;
122f4f61d27SAneesh Kumar K.V     }
123f4f61d27SAneesh Kumar K.V     /* s->tag is non-NULL terminated string */
124f4f61d27SAneesh Kumar K.V     s->tag = qemu_malloc(len);
125f4f61d27SAneesh Kumar K.V     memcpy(s->tag, conf->tag, len);
126f4f61d27SAneesh Kumar K.V     s->tag_len = len;
127f4f61d27SAneesh Kumar K.V     s->ctx.uid = -1;
128f4f61d27SAneesh Kumar K.V 
129f4f61d27SAneesh Kumar K.V     s->ops = fse->ops;
130f4f61d27SAneesh Kumar K.V     s->vdev.get_features = virtio_9p_get_features;
131f4f61d27SAneesh Kumar K.V     s->config_size = sizeof(struct virtio_9p_config) +
132f4f61d27SAneesh Kumar K.V                         s->tag_len;
133f4f61d27SAneesh Kumar K.V     s->vdev.get_config = virtio_9p_get_config;
134f4f61d27SAneesh Kumar K.V 
135f4f61d27SAneesh Kumar K.V     return &s->vdev;
136f4f61d27SAneesh Kumar K.V }
137f4f61d27SAneesh Kumar K.V 
138f4f61d27SAneesh Kumar K.V static int virtio_9p_init_pci(PCIDevice *pci_dev)
139f4f61d27SAneesh Kumar K.V {
140f4f61d27SAneesh Kumar K.V     VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
141f4f61d27SAneesh Kumar K.V     VirtIODevice *vdev;
142f4f61d27SAneesh Kumar K.V 
143f4f61d27SAneesh Kumar K.V     vdev = virtio_9p_init(&pci_dev->qdev, &proxy->fsconf);
144f4f61d27SAneesh Kumar K.V     vdev->nvectors = proxy->nvectors;
145*befeac45SMichael S. Tsirkin     virtio_init_pci(proxy, vdev);
146f4f61d27SAneesh Kumar K.V     /* make the actual value visible */
147f4f61d27SAneesh Kumar K.V     proxy->nvectors = vdev->nvectors;
148f4f61d27SAneesh Kumar K.V     return 0;
149f4f61d27SAneesh Kumar K.V }
150f4f61d27SAneesh Kumar K.V 
151f4f61d27SAneesh Kumar K.V static PCIDeviceInfo virtio_9p_info = {
152f4f61d27SAneesh Kumar K.V     .qdev.name = "virtio-9p-pci",
153f4f61d27SAneesh Kumar K.V     .qdev.size = sizeof(VirtIOPCIProxy),
154f4f61d27SAneesh Kumar K.V     .init      = virtio_9p_init_pci,
155*befeac45SMichael S. Tsirkin     .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
156*befeac45SMichael S. Tsirkin     .device_id = 0x1009,
157*befeac45SMichael S. Tsirkin     .revision  = VIRTIO_PCI_ABI_VERSION,
158*befeac45SMichael S. Tsirkin     .class_id  = 0x2,
159f4f61d27SAneesh Kumar K.V     .qdev.props = (Property[]) {
160f4f61d27SAneesh Kumar K.V         DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
161f4f61d27SAneesh Kumar K.V         DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
162f4f61d27SAneesh Kumar K.V         DEFINE_PROP_STRING("mount_tag", VirtIOPCIProxy, fsconf.tag),
163f4f61d27SAneesh Kumar K.V         DEFINE_PROP_STRING("fsdev", VirtIOPCIProxy, fsconf.fsdev_id),
164f4f61d27SAneesh Kumar K.V         DEFINE_PROP_END_OF_LIST(),
165f4f61d27SAneesh Kumar K.V     }
166f4f61d27SAneesh Kumar K.V };
167f4f61d27SAneesh Kumar K.V 
168f4f61d27SAneesh Kumar K.V static void virtio_9p_register_devices(void)
169f4f61d27SAneesh Kumar K.V {
170f4f61d27SAneesh Kumar K.V     pci_qdev_register(&virtio_9p_info);
171f4f61d27SAneesh Kumar K.V }
172f4f61d27SAneesh Kumar K.V 
173f4f61d27SAneesh Kumar K.V device_init(virtio_9p_register_devices)
174