root / hw / 9pfs / virtio-9p-device.c @ 9bfa659e
History | View | Annotate | Download (5.6 kB)
1 | f4f61d27 | Aneesh Kumar K.V | /*
|
---|---|---|---|
2 | f4f61d27 | Aneesh Kumar K.V | * Virtio 9p backend
|
3 | f4f61d27 | Aneesh Kumar K.V | *
|
4 | f4f61d27 | Aneesh Kumar K.V | * Copyright IBM, Corp. 2010
|
5 | f4f61d27 | Aneesh Kumar K.V | *
|
6 | f4f61d27 | Aneesh Kumar K.V | * Authors:
|
7 | f4f61d27 | Aneesh Kumar K.V | * Anthony Liguori <aliguori@us.ibm.com>
|
8 | f4f61d27 | Aneesh Kumar K.V | *
|
9 | f4f61d27 | Aneesh Kumar K.V | * This work is licensed under the terms of the GNU GPL, version 2. See
|
10 | f4f61d27 | Aneesh Kumar K.V | * the COPYING file in the top-level directory.
|
11 | f4f61d27 | Aneesh Kumar K.V | *
|
12 | f4f61d27 | Aneesh Kumar K.V | */
|
13 | f4f61d27 | Aneesh Kumar K.V | |
14 | 873c3213 | Stefan Weil | #include "hw/virtio.h" |
15 | 873c3213 | Stefan Weil | #include "hw/pc.h" |
16 | f4f61d27 | Aneesh Kumar K.V | #include "qemu_socket.h" |
17 | 873c3213 | Stefan Weil | #include "hw/virtio-pci.h" |
18 | f4f61d27 | Aneesh Kumar K.V | #include "virtio-9p.h" |
19 | f4f61d27 | Aneesh Kumar K.V | #include "fsdev/qemu-fsdev.h" |
20 | f4f61d27 | Aneesh Kumar K.V | #include "virtio-9p-xattr.h" |
21 | 39c0564e | Venkateswararao Jujjuri (JV) | #include "virtio-9p-coth.h" |
22 | f4f61d27 | Aneesh Kumar K.V | |
23 | f4f61d27 | Aneesh Kumar K.V | static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
|
24 | f4f61d27 | Aneesh Kumar K.V | { |
25 | f4f61d27 | Aneesh Kumar K.V | features |= 1 << VIRTIO_9P_MOUNT_TAG;
|
26 | f4f61d27 | Aneesh Kumar K.V | return features;
|
27 | f4f61d27 | Aneesh Kumar K.V | } |
28 | f4f61d27 | Aneesh Kumar K.V | |
29 | f4f61d27 | Aneesh Kumar K.V | static V9fsState *to_virtio_9p(VirtIODevice *vdev)
|
30 | f4f61d27 | Aneesh Kumar K.V | { |
31 | f4f61d27 | Aneesh Kumar K.V | return (V9fsState *)vdev;
|
32 | f4f61d27 | Aneesh Kumar K.V | } |
33 | f4f61d27 | Aneesh Kumar K.V | |
34 | f4f61d27 | Aneesh Kumar K.V | static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config) |
35 | f4f61d27 | Aneesh Kumar K.V | { |
36 | e9a0152b | Aneesh Kumar K.V | int len;
|
37 | f4f61d27 | Aneesh Kumar K.V | struct virtio_9p_config *cfg;
|
38 | f4f61d27 | Aneesh Kumar K.V | V9fsState *s = to_virtio_9p(vdev); |
39 | f4f61d27 | Aneesh Kumar K.V | |
40 | e9a0152b | Aneesh Kumar K.V | len = strlen(s->tag); |
41 | e9a0152b | Aneesh Kumar K.V | cfg = g_malloc0(sizeof(struct virtio_9p_config) + len); |
42 | e9a0152b | Aneesh Kumar K.V | stw_raw(&cfg->tag_len, len); |
43 | e9a0152b | Aneesh Kumar K.V | /* We don't copy the terminating null to config space */
|
44 | e9a0152b | Aneesh Kumar K.V | memcpy(cfg->tag, s->tag, len); |
45 | f4f61d27 | Aneesh Kumar K.V | memcpy(config, cfg, s->config_size); |
46 | 7267c094 | Anthony Liguori | g_free(cfg); |
47 | f4f61d27 | Aneesh Kumar K.V | } |
48 | f4f61d27 | Aneesh Kumar K.V | |
49 | f4f61d27 | Aneesh Kumar K.V | VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) |
50 | 0174fe73 | Aneesh Kumar K.V | { |
51 | f4f61d27 | Aneesh Kumar K.V | V9fsState *s; |
52 | f4f61d27 | Aneesh Kumar K.V | int i, len;
|
53 | f4f61d27 | Aneesh Kumar K.V | struct stat stat;
|
54 | fbcbf101 | Aneesh Kumar K.V | FsDriverEntry *fse; |
55 | 7cca27df | M. Mohan Kumar | V9fsPath path; |
56 | f4f61d27 | Aneesh Kumar K.V | |
57 | f4f61d27 | Aneesh Kumar K.V | s = (V9fsState *)virtio_common_init("virtio-9p",
|
58 | f4f61d27 | Aneesh Kumar K.V | VIRTIO_ID_9P, |
59 | f4f61d27 | Aneesh Kumar K.V | sizeof(struct virtio_9p_config)+ |
60 | f4f61d27 | Aneesh Kumar K.V | MAX_TAG_LEN, |
61 | f4f61d27 | Aneesh Kumar K.V | sizeof(V9fsState));
|
62 | f4f61d27 | Aneesh Kumar K.V | /* initialize pdu allocator */
|
63 | f4f61d27 | Aneesh Kumar K.V | QLIST_INIT(&s->free_list); |
64 | bccacf6c | Aneesh Kumar K.V | QLIST_INIT(&s->active_list); |
65 | f4f61d27 | Aneesh Kumar K.V | for (i = 0; i < (MAX_REQ - 1); i++) { |
66 | f4f61d27 | Aneesh Kumar K.V | QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next); |
67 | f4f61d27 | Aneesh Kumar K.V | } |
68 | f4f61d27 | Aneesh Kumar K.V | |
69 | f4f61d27 | Aneesh Kumar K.V | s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output); |
70 | f4f61d27 | Aneesh Kumar K.V | |
71 | f4f61d27 | Aneesh Kumar K.V | fse = get_fsdev_fsentry(conf->fsdev_id); |
72 | f4f61d27 | Aneesh Kumar K.V | |
73 | f4f61d27 | Aneesh Kumar K.V | if (!fse) {
|
74 | f4f61d27 | Aneesh Kumar K.V | /* We don't have a fsdev identified by fsdev_id */
|
75 | f4f61d27 | Aneesh Kumar K.V | fprintf(stderr, "Virtio-9p device couldn't find fsdev with the "
|
76 | f4f61d27 | Aneesh Kumar K.V | "id = %s\n", conf->fsdev_id ? conf->fsdev_id : "NULL"); |
77 | f4f61d27 | Aneesh Kumar K.V | exit(1);
|
78 | f4f61d27 | Aneesh Kumar K.V | } |
79 | f4f61d27 | Aneesh Kumar K.V | |
80 | 99519f0a | Aneesh Kumar K.V | if (!conf->tag) {
|
81 | 99519f0a | Aneesh Kumar K.V | /* we haven't specified a mount_tag */
|
82 | 99519f0a | Aneesh Kumar K.V | fprintf(stderr, "fsdev with id %s needs mount_tag arguments\n",
|
83 | f4f61d27 | Aneesh Kumar K.V | conf->fsdev_id); |
84 | f4f61d27 | Aneesh Kumar K.V | exit(1);
|
85 | f4f61d27 | Aneesh Kumar K.V | } |
86 | f4f61d27 | Aneesh Kumar K.V | |
87 | b97400ca | Aneesh Kumar K.V | s->ctx.export_flags = fse->export_flags; |
88 | 99519f0a | Aneesh Kumar K.V | if (fse->path) {
|
89 | 99519f0a | Aneesh Kumar K.V | s->ctx.fs_root = g_strdup(fse->path); |
90 | 99519f0a | Aneesh Kumar K.V | } else {
|
91 | 99519f0a | Aneesh Kumar K.V | s->ctx.fs_root = NULL;
|
92 | 99519f0a | Aneesh Kumar K.V | } |
93 | b97400ca | Aneesh Kumar K.V | s->ctx.exops.get_st_gen = NULL;
|
94 | f4f61d27 | Aneesh Kumar K.V | len = strlen(conf->tag); |
95 | e9a0152b | Aneesh Kumar K.V | if (len > MAX_TAG_LEN - 1) { |
96 | a2f507d9 | Daniel P. Berrange | fprintf(stderr, "mount tag '%s' (%d bytes) is longer than "
|
97 | e9a0152b | Aneesh Kumar K.V | "maximum (%d bytes)", conf->tag, len, MAX_TAG_LEN - 1); |
98 | a2f507d9 | Daniel P. Berrange | exit(1);
|
99 | f4f61d27 | Aneesh Kumar K.V | } |
100 | e9a0152b | Aneesh Kumar K.V | |
101 | e9a0152b | Aneesh Kumar K.V | s->tag = strdup(conf->tag); |
102 | f4f61d27 | Aneesh Kumar K.V | s->ctx.uid = -1;
|
103 | f4f61d27 | Aneesh Kumar K.V | |
104 | f4f61d27 | Aneesh Kumar K.V | s->ops = fse->ops; |
105 | f4f61d27 | Aneesh Kumar K.V | s->vdev.get_features = virtio_9p_get_features; |
106 | e9a0152b | Aneesh Kumar K.V | s->config_size = sizeof(struct virtio_9p_config) + len; |
107 | f4f61d27 | Aneesh Kumar K.V | s->vdev.get_config = virtio_9p_get_config; |
108 | 9e5b2247 | Aneesh Kumar K.V | s->fid_list = NULL;
|
109 | 02cb7f3a | Aneesh Kumar K.V | qemu_co_rwlock_init(&s->rename_lock); |
110 | f4f61d27 | Aneesh Kumar K.V | |
111 | 0174fe73 | Aneesh Kumar K.V | if (s->ops->init(&s->ctx) < 0) { |
112 | 0174fe73 | Aneesh Kumar K.V | fprintf(stderr, "Virtio-9p Failed to initialize fs-driver with id:%s"
|
113 | 0174fe73 | Aneesh Kumar K.V | " and export path:%s\n", conf->fsdev_id, s->ctx.fs_root);
|
114 | 0174fe73 | Aneesh Kumar K.V | exit(1);
|
115 | 0174fe73 | Aneesh Kumar K.V | } |
116 | 39c0564e | Venkateswararao Jujjuri (JV) | if (v9fs_init_worker_threads() < 0) { |
117 | 39c0564e | Venkateswararao Jujjuri (JV) | fprintf(stderr, "worker thread initialization failed\n");
|
118 | 39c0564e | Venkateswararao Jujjuri (JV) | exit(1);
|
119 | 39c0564e | Venkateswararao Jujjuri (JV) | } |
120 | 7cca27df | M. Mohan Kumar | |
121 | 7cca27df | M. Mohan Kumar | /*
|
122 | 7cca27df | M. Mohan Kumar | * Check details of export path, We need to use fs driver
|
123 | 7cca27df | M. Mohan Kumar | * call back to do that. Since we are in the init path, we don't
|
124 | 7cca27df | M. Mohan Kumar | * use co-routines here.
|
125 | 7cca27df | M. Mohan Kumar | */
|
126 | 7cca27df | M. Mohan Kumar | v9fs_path_init(&path); |
127 | 7cca27df | M. Mohan Kumar | if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) { |
128 | 7cca27df | M. Mohan Kumar | fprintf(stderr, |
129 | 7cca27df | M. Mohan Kumar | "error in converting name to path %s", strerror(errno));
|
130 | 7cca27df | M. Mohan Kumar | exit(1);
|
131 | 7cca27df | M. Mohan Kumar | } |
132 | 7cca27df | M. Mohan Kumar | if (s->ops->lstat(&s->ctx, &path, &stat)) {
|
133 | 7cca27df | M. Mohan Kumar | fprintf(stderr, "share path %s does not exist\n", fse->path);
|
134 | 7cca27df | M. Mohan Kumar | exit(1);
|
135 | 7cca27df | M. Mohan Kumar | } else if (!S_ISDIR(stat.st_mode)) { |
136 | 7cca27df | M. Mohan Kumar | fprintf(stderr, "share path %s is not a directory\n", fse->path);
|
137 | 7cca27df | M. Mohan Kumar | exit(1);
|
138 | 7cca27df | M. Mohan Kumar | } |
139 | 7cca27df | M. Mohan Kumar | v9fs_path_free(&path); |
140 | 7cca27df | M. Mohan Kumar | |
141 | f4f61d27 | Aneesh Kumar K.V | return &s->vdev;
|
142 | f4f61d27 | Aneesh Kumar K.V | } |
143 | f4f61d27 | Aneesh Kumar K.V | |
144 | f4f61d27 | Aneesh Kumar K.V | static int virtio_9p_init_pci(PCIDevice *pci_dev) |
145 | f4f61d27 | Aneesh Kumar K.V | { |
146 | f4f61d27 | Aneesh Kumar K.V | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
147 | f4f61d27 | Aneesh Kumar K.V | VirtIODevice *vdev; |
148 | f4f61d27 | Aneesh Kumar K.V | |
149 | f4f61d27 | Aneesh Kumar K.V | vdev = virtio_9p_init(&pci_dev->qdev, &proxy->fsconf); |
150 | f4f61d27 | Aneesh Kumar K.V | vdev->nvectors = proxy->nvectors; |
151 | befeac45 | Michael S. Tsirkin | virtio_init_pci(proxy, vdev); |
152 | f4f61d27 | Aneesh Kumar K.V | /* make the actual value visible */
|
153 | f4f61d27 | Aneesh Kumar K.V | proxy->nvectors = vdev->nvectors; |
154 | f4f61d27 | Aneesh Kumar K.V | return 0; |
155 | f4f61d27 | Aneesh Kumar K.V | } |
156 | f4f61d27 | Aneesh Kumar K.V | |
157 | 40021f08 | Anthony Liguori | static Property virtio_9p_properties[] = {
|
158 | 40021f08 | Anthony Liguori | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), |
159 | 40021f08 | Anthony Liguori | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), |
160 | 40021f08 | Anthony Liguori | DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), |
161 | 40021f08 | Anthony Liguori | DEFINE_PROP_STRING("mount_tag", VirtIOPCIProxy, fsconf.tag),
|
162 | 40021f08 | Anthony Liguori | DEFINE_PROP_STRING("fsdev", VirtIOPCIProxy, fsconf.fsdev_id),
|
163 | 40021f08 | Anthony Liguori | DEFINE_PROP_END_OF_LIST(), |
164 | 40021f08 | Anthony Liguori | }; |
165 | 40021f08 | Anthony Liguori | |
166 | 40021f08 | Anthony Liguori | static void virtio_9p_class_init(ObjectClass *klass, void *data) |
167 | 40021f08 | Anthony Liguori | { |
168 | 39bffca2 | Anthony Liguori | DeviceClass *dc = DEVICE_CLASS(klass); |
169 | 40021f08 | Anthony Liguori | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
170 | 40021f08 | Anthony Liguori | |
171 | 40021f08 | Anthony Liguori | k->init = virtio_9p_init_pci; |
172 | 40021f08 | Anthony Liguori | k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; |
173 | 40021f08 | Anthony Liguori | k->device_id = 0x1009;
|
174 | 40021f08 | Anthony Liguori | k->revision = VIRTIO_PCI_ABI_VERSION; |
175 | 40021f08 | Anthony Liguori | k->class_id = 0x2;
|
176 | 39bffca2 | Anthony Liguori | dc->props = virtio_9p_properties; |
177 | 39bffca2 | Anthony Liguori | dc->reset = virtio_pci_reset; |
178 | 40021f08 | Anthony Liguori | } |
179 | 40021f08 | Anthony Liguori | |
180 | 39bffca2 | Anthony Liguori | static TypeInfo virtio_9p_info = {
|
181 | 39bffca2 | Anthony Liguori | .name = "virtio-9p-pci",
|
182 | 39bffca2 | Anthony Liguori | .parent = TYPE_PCI_DEVICE, |
183 | 39bffca2 | Anthony Liguori | .instance_size = sizeof(VirtIOPCIProxy),
|
184 | 39bffca2 | Anthony Liguori | .class_init = virtio_9p_class_init, |
185 | f4f61d27 | Aneesh Kumar K.V | }; |
186 | f4f61d27 | Aneesh Kumar K.V | |
187 | 83f7d43a | Andreas Färber | static void virtio_9p_register_types(void) |
188 | f4f61d27 | Aneesh Kumar K.V | { |
189 | 39bffca2 | Anthony Liguori | type_register_static(&virtio_9p_info); |
190 | 7a462745 | Aneesh Kumar K.V | virtio_9p_set_fd_limit(); |
191 | f4f61d27 | Aneesh Kumar K.V | } |
192 | f4f61d27 | Aneesh Kumar K.V | |
193 | 83f7d43a | Andreas Färber | type_init(virtio_9p_register_types) |