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