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