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