]> git.proxmox.com Git - mirror_qemu.git/blob - hw/9pfs/virtio-9p-device.c
virtio-9p: add the virtio-9p device.
[mirror_qemu.git] / hw / 9pfs / virtio-9p-device.c
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/virtio.h"
15 #include "hw/virtio/virtio-9p.h"
16 #include "hw/i386/pc.h"
17 #include "qemu/sockets.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 int len;
37 struct virtio_9p_config *cfg;
38 V9fsState *s = to_virtio_9p(vdev);
39
40 len = strlen(s->tag);
41 cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
42 stw_raw(&cfg->tag_len, len);
43 /* We don't copy the terminating null to config space */
44 memcpy(cfg->tag, s->tag, len);
45 memcpy(config, cfg, s->config_size);
46 g_free(cfg);
47 }
48
49 static VirtIODevice *virtio_9p_common_init(DeviceState *dev, V9fsConf *conf,
50 V9fsState **ps)
51 {
52 V9fsState *s = *ps;
53 int i, len;
54 struct stat stat;
55 FsDriverEntry *fse;
56 V9fsPath path;
57
58 /*
59 * We have two cases here: the old virtio-9p-pci device, and the
60 * refactored virtio-9p.
61 */
62
63 if (s == NULL) {
64 s = (V9fsState *)virtio_common_init("virtio-9p",
65 VIRTIO_ID_9P,
66 sizeof(struct virtio_9p_config)+
67 MAX_TAG_LEN,
68 sizeof(V9fsState));
69 } else {
70 virtio_init(VIRTIO_DEVICE(s), "virtio-9p", VIRTIO_ID_9P,
71 sizeof(struct virtio_9p_config) + MAX_TAG_LEN);
72 }
73 /* initialize pdu allocator */
74 QLIST_INIT(&s->free_list);
75 QLIST_INIT(&s->active_list);
76 for (i = 0; i < (MAX_REQ - 1); i++) {
77 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
78 }
79
80 s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
81
82 fse = get_fsdev_fsentry(conf->fsdev_id);
83
84 if (!fse) {
85 /* We don't have a fsdev identified by fsdev_id */
86 fprintf(stderr, "Virtio-9p device couldn't find fsdev with the "
87 "id = %s\n", conf->fsdev_id ? conf->fsdev_id : "NULL");
88 exit(1);
89 }
90
91 if (!conf->tag) {
92 /* we haven't specified a mount_tag */
93 fprintf(stderr, "fsdev with id %s needs mount_tag arguments\n",
94 conf->fsdev_id);
95 exit(1);
96 }
97
98 s->ctx.export_flags = fse->export_flags;
99 s->ctx.fs_root = g_strdup(fse->path);
100 s->ctx.exops.get_st_gen = NULL;
101 len = strlen(conf->tag);
102 if (len > MAX_TAG_LEN - 1) {
103 fprintf(stderr, "mount tag '%s' (%d bytes) is longer than "
104 "maximum (%d bytes)", conf->tag, len, MAX_TAG_LEN - 1);
105 exit(1);
106 }
107
108 s->tag = g_strdup(conf->tag);
109 s->ctx.uid = -1;
110
111 s->ops = fse->ops;
112 s->vdev.get_features = virtio_9p_get_features;
113 s->config_size = sizeof(struct virtio_9p_config) + len;
114 s->vdev.get_config = virtio_9p_get_config;
115 s->fid_list = NULL;
116 qemu_co_rwlock_init(&s->rename_lock);
117
118 if (s->ops->init(&s->ctx) < 0) {
119 fprintf(stderr, "Virtio-9p Failed to initialize fs-driver with id:%s"
120 " and export path:%s\n", conf->fsdev_id, s->ctx.fs_root);
121 exit(1);
122 }
123 if (v9fs_init_worker_threads() < 0) {
124 fprintf(stderr, "worker thread initialization failed\n");
125 exit(1);
126 }
127
128 /*
129 * Check details of export path, We need to use fs driver
130 * call back to do that. Since we are in the init path, we don't
131 * use co-routines here.
132 */
133 v9fs_path_init(&path);
134 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
135 fprintf(stderr,
136 "error in converting name to path %s", strerror(errno));
137 exit(1);
138 }
139 if (s->ops->lstat(&s->ctx, &path, &stat)) {
140 fprintf(stderr, "share path %s does not exist\n", fse->path);
141 exit(1);
142 } else if (!S_ISDIR(stat.st_mode)) {
143 fprintf(stderr, "share path %s is not a directory\n", fse->path);
144 exit(1);
145 }
146 v9fs_path_free(&path);
147
148 return &s->vdev;
149 }
150
151 VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
152 {
153 V9fsState *s = NULL;
154 return virtio_9p_common_init(dev, conf, &s);
155 }
156
157 /* virtio-9p device */
158
159 static int virtio_9p_device_init(VirtIODevice *vdev)
160 {
161 DeviceState *qdev = DEVICE(vdev);
162 V9fsState *s = VIRTIO_9P(vdev);
163 V9fsConf *fsconf = &(s->fsconf);
164 if (virtio_9p_common_init(qdev, fsconf, &s) == NULL) {
165 return -1;
166 }
167 return 0;
168 }
169
170 static Property virtio_9p_properties[] = {
171 DEFINE_VIRTIO_9P_PROPERTIES(V9fsState, fsconf),
172 DEFINE_PROP_END_OF_LIST(),
173 };
174
175 static void virtio_9p_class_init(ObjectClass *klass, void *data)
176 {
177 DeviceClass *dc = DEVICE_CLASS(klass);
178 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
179 dc->props = virtio_9p_properties;
180 vdc->init = virtio_9p_device_init;
181 vdc->get_features = virtio_9p_get_features;
182 vdc->get_config = virtio_9p_get_config;
183 }
184
185 static const TypeInfo virtio_device_info = {
186 .name = TYPE_VIRTIO_9P,
187 .parent = TYPE_VIRTIO_DEVICE,
188 .instance_size = sizeof(V9fsState),
189 .class_init = virtio_9p_class_init,
190 };
191
192 static void virtio_9p_register_types(void)
193 {
194 type_register_static(&virtio_device_info);
195 }
196
197 type_init(virtio_9p_register_types)