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