xref: /qemu/hw/9pfs/virtio-9p-device.c (revision e8111e50557761b0d86cd5c90fe7a272aeddd7a3)
1f4f61d27SAneesh Kumar K.V /*
2f4f61d27SAneesh Kumar K.V  * Virtio 9p backend
3f4f61d27SAneesh Kumar K.V  *
4f4f61d27SAneesh Kumar K.V  * Copyright IBM, Corp. 2010
5f4f61d27SAneesh Kumar K.V  *
6f4f61d27SAneesh Kumar K.V  * Authors:
7f4f61d27SAneesh Kumar K.V  *  Anthony Liguori   <aliguori@us.ibm.com>
8f4f61d27SAneesh Kumar K.V  *
9f4f61d27SAneesh Kumar K.V  * This work is licensed under the terms of the GNU GPL, version 2.  See
10f4f61d27SAneesh Kumar K.V  * the COPYING file in the top-level directory.
11f4f61d27SAneesh Kumar K.V  *
12f4f61d27SAneesh Kumar K.V  */
13f4f61d27SAneesh Kumar K.V 
140d09e41aSPaolo Bonzini #include "hw/virtio/virtio.h"
1593b48c20SHans de Goede #include "hw/virtio/virtio-9p.h"
160d09e41aSPaolo Bonzini #include "hw/i386/pc.h"
171de7afc9SPaolo Bonzini #include "qemu/sockets.h"
18f4f61d27SAneesh Kumar K.V #include "virtio-9p.h"
19f4f61d27SAneesh Kumar K.V #include "fsdev/qemu-fsdev.h"
20f4f61d27SAneesh Kumar K.V #include "virtio-9p-xattr.h"
2139c0564eSVenkateswararao Jujjuri (JV) #include "virtio-9p-coth.h"
22f4f61d27SAneesh Kumar K.V 
23f4f61d27SAneesh Kumar K.V static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
24f4f61d27SAneesh Kumar K.V {
25f4f61d27SAneesh Kumar K.V     features |= 1 << VIRTIO_9P_MOUNT_TAG;
26f4f61d27SAneesh Kumar K.V     return features;
27f4f61d27SAneesh Kumar K.V }
28f4f61d27SAneesh Kumar K.V 
29f4f61d27SAneesh Kumar K.V static V9fsState *to_virtio_9p(VirtIODevice *vdev)
30f4f61d27SAneesh Kumar K.V {
31f4f61d27SAneesh Kumar K.V     return (V9fsState *)vdev;
32f4f61d27SAneesh Kumar K.V }
33f4f61d27SAneesh Kumar K.V 
34f4f61d27SAneesh Kumar K.V static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
35f4f61d27SAneesh Kumar K.V {
36e9a0152bSAneesh Kumar K.V     int len;
37f4f61d27SAneesh Kumar K.V     struct virtio_9p_config *cfg;
38f4f61d27SAneesh Kumar K.V     V9fsState *s = to_virtio_9p(vdev);
39f4f61d27SAneesh Kumar K.V 
40e9a0152bSAneesh Kumar K.V     len = strlen(s->tag);
41e9a0152bSAneesh Kumar K.V     cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
42e9a0152bSAneesh Kumar K.V     stw_raw(&cfg->tag_len, len);
43e9a0152bSAneesh Kumar K.V     /* We don't copy the terminating null to config space */
44e9a0152bSAneesh Kumar K.V     memcpy(cfg->tag, s->tag, len);
45f4f61d27SAneesh Kumar K.V     memcpy(config, cfg, s->config_size);
467267c094SAnthony Liguori     g_free(cfg);
47f4f61d27SAneesh Kumar K.V }
48f4f61d27SAneesh Kumar K.V 
49*e8111e50SKONRAD Frederic static int virtio_9p_device_init(VirtIODevice *vdev)
50f4f61d27SAneesh Kumar K.V {
51*e8111e50SKONRAD Frederic     V9fsState *s = VIRTIO_9P(vdev);
52f4f61d27SAneesh Kumar K.V     int i, len;
53f4f61d27SAneesh Kumar K.V     struct stat stat;
54fbcbf101SAneesh Kumar K.V     FsDriverEntry *fse;
557cca27dfSM. Mohan Kumar     V9fsPath path;
56f4f61d27SAneesh Kumar K.V 
57e7303c43SKONRAD Frederic     virtio_init(VIRTIO_DEVICE(s), "virtio-9p", VIRTIO_ID_9P,
58e7303c43SKONRAD Frederic                 sizeof(struct virtio_9p_config) + MAX_TAG_LEN);
59*e8111e50SKONRAD Frederic 
60f4f61d27SAneesh Kumar K.V     /* initialize pdu allocator */
61f4f61d27SAneesh Kumar K.V     QLIST_INIT(&s->free_list);
62bccacf6cSAneesh Kumar K.V     QLIST_INIT(&s->active_list);
63f4f61d27SAneesh Kumar K.V     for (i = 0; i < (MAX_REQ - 1); i++) {
64f4f61d27SAneesh Kumar K.V         QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
65f4f61d27SAneesh Kumar K.V     }
66f4f61d27SAneesh Kumar K.V 
67*e8111e50SKONRAD Frederic     s->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output);
68f4f61d27SAneesh Kumar K.V 
69*e8111e50SKONRAD Frederic     fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
70f4f61d27SAneesh Kumar K.V 
71f4f61d27SAneesh Kumar K.V     if (!fse) {
72f4f61d27SAneesh Kumar K.V         /* We don't have a fsdev identified by fsdev_id */
73f4f61d27SAneesh Kumar K.V         fprintf(stderr, "Virtio-9p device couldn't find fsdev with the "
74*e8111e50SKONRAD Frederic                 "id = %s\n",
75*e8111e50SKONRAD Frederic                 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
76*e8111e50SKONRAD Frederic         return -1;
77f4f61d27SAneesh Kumar K.V     }
78f4f61d27SAneesh Kumar K.V 
79*e8111e50SKONRAD Frederic     if (!s->fsconf.tag) {
8099519f0aSAneesh Kumar K.V         /* we haven't specified a mount_tag */
8199519f0aSAneesh Kumar K.V         fprintf(stderr, "fsdev with id %s needs mount_tag arguments\n",
82*e8111e50SKONRAD Frederic                 s->fsconf.fsdev_id);
83*e8111e50SKONRAD Frederic         return -1;
84f4f61d27SAneesh Kumar K.V     }
85f4f61d27SAneesh Kumar K.V 
86d3ab98e6SAneesh Kumar K.V     s->ctx.export_flags = fse->export_flags;
877267c094SAnthony Liguori     s->ctx.fs_root = g_strdup(fse->path);
88e06a765eSHarsh Prateek Bora     s->ctx.exops.get_st_gen = NULL;
89*e8111e50SKONRAD Frederic     len = strlen(s->fsconf.tag);
90e9a0152bSAneesh Kumar K.V     if (len > MAX_TAG_LEN - 1) {
91a2f507d9SDaniel P. Berrange         fprintf(stderr, "mount tag '%s' (%d bytes) is longer than "
92*e8111e50SKONRAD Frederic                 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
93*e8111e50SKONRAD Frederic         return -1;
94f4f61d27SAneesh Kumar K.V     }
95e9a0152bSAneesh Kumar K.V 
96*e8111e50SKONRAD Frederic     s->tag = strdup(s->fsconf.tag);
97f4f61d27SAneesh Kumar K.V     s->ctx.uid = -1;
98f4f61d27SAneesh Kumar K.V 
99f4f61d27SAneesh Kumar K.V     s->ops = fse->ops;
100f4f61d27SAneesh Kumar K.V     s->vdev.get_features = virtio_9p_get_features;
101e9a0152bSAneesh Kumar K.V     s->config_size = sizeof(struct virtio_9p_config) + len;
102f4f61d27SAneesh Kumar K.V     s->vdev.get_config = virtio_9p_get_config;
1039e5b2247SAneesh Kumar K.V     s->fid_list = NULL;
10402cb7f3aSAneesh Kumar K.V     qemu_co_rwlock_init(&s->rename_lock);
105f4f61d27SAneesh Kumar K.V 
1060174fe73SAneesh Kumar K.V     if (s->ops->init(&s->ctx) < 0) {
1070174fe73SAneesh Kumar K.V         fprintf(stderr, "Virtio-9p Failed to initialize fs-driver with id:%s"
108*e8111e50SKONRAD Frederic                 " and export path:%s\n", s->fsconf.fsdev_id, s->ctx.fs_root);
109*e8111e50SKONRAD Frederic         return -1;
1100174fe73SAneesh Kumar K.V     }
11139c0564eSVenkateswararao Jujjuri (JV)     if (v9fs_init_worker_threads() < 0) {
11239c0564eSVenkateswararao Jujjuri (JV)         fprintf(stderr, "worker thread initialization failed\n");
113*e8111e50SKONRAD Frederic         return -1;
11439c0564eSVenkateswararao Jujjuri (JV)     }
1157cca27dfSM. Mohan Kumar 
1167cca27dfSM. Mohan Kumar     /*
1177cca27dfSM. Mohan Kumar      * Check details of export path, We need to use fs driver
1187cca27dfSM. Mohan Kumar      * call back to do that. Since we are in the init path, we don't
1197cca27dfSM. Mohan Kumar      * use co-routines here.
1207cca27dfSM. Mohan Kumar      */
1217cca27dfSM. Mohan Kumar     v9fs_path_init(&path);
1227cca27dfSM. Mohan Kumar     if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
1237cca27dfSM. Mohan Kumar         fprintf(stderr,
1247cca27dfSM. Mohan Kumar                 "error in converting name to path %s", strerror(errno));
125*e8111e50SKONRAD Frederic         return -1;
1267cca27dfSM. Mohan Kumar     }
1277cca27dfSM. Mohan Kumar     if (s->ops->lstat(&s->ctx, &path, &stat)) {
1287cca27dfSM. Mohan Kumar         fprintf(stderr, "share path %s does not exist\n", fse->path);
129*e8111e50SKONRAD Frederic         return -1;
1307cca27dfSM. Mohan Kumar     } else if (!S_ISDIR(stat.st_mode)) {
1317cca27dfSM. Mohan Kumar         fprintf(stderr, "share path %s is not a directory\n", fse->path);
132*e8111e50SKONRAD Frederic         return -1;
1337cca27dfSM. Mohan Kumar     }
1347cca27dfSM. Mohan Kumar     v9fs_path_free(&path);
1357cca27dfSM. Mohan Kumar 
136*e8111e50SKONRAD Frederic     return 0;
137e7303c43SKONRAD Frederic }
138e7303c43SKONRAD Frederic 
139e7303c43SKONRAD Frederic /* virtio-9p device */
140e7303c43SKONRAD Frederic 
141e7303c43SKONRAD Frederic static Property virtio_9p_properties[] = {
142e7303c43SKONRAD Frederic     DEFINE_VIRTIO_9P_PROPERTIES(V9fsState, fsconf),
143e7303c43SKONRAD Frederic     DEFINE_PROP_END_OF_LIST(),
144e7303c43SKONRAD Frederic };
145e7303c43SKONRAD Frederic 
146e7303c43SKONRAD Frederic static void virtio_9p_class_init(ObjectClass *klass, void *data)
147e7303c43SKONRAD Frederic {
148e7303c43SKONRAD Frederic     DeviceClass *dc = DEVICE_CLASS(klass);
149e7303c43SKONRAD Frederic     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
150e7303c43SKONRAD Frederic     dc->props = virtio_9p_properties;
151e7303c43SKONRAD Frederic     vdc->init = virtio_9p_device_init;
152e7303c43SKONRAD Frederic     vdc->get_features = virtio_9p_get_features;
153e7303c43SKONRAD Frederic     vdc->get_config = virtio_9p_get_config;
154e7303c43SKONRAD Frederic }
155e7303c43SKONRAD Frederic 
156e7303c43SKONRAD Frederic static const TypeInfo virtio_device_info = {
157e7303c43SKONRAD Frederic     .name = TYPE_VIRTIO_9P,
158e7303c43SKONRAD Frederic     .parent = TYPE_VIRTIO_DEVICE,
159e7303c43SKONRAD Frederic     .instance_size = sizeof(V9fsState),
160e7303c43SKONRAD Frederic     .class_init = virtio_9p_class_init,
161e7303c43SKONRAD Frederic };
162e7303c43SKONRAD Frederic 
163e7303c43SKONRAD Frederic static void virtio_9p_register_types(void)
164e7303c43SKONRAD Frederic {
165e7303c43SKONRAD Frederic     type_register_static(&virtio_device_info);
166e7303c43SKONRAD Frederic }
167e7303c43SKONRAD Frederic 
168e7303c43SKONRAD Frederic type_init(virtio_9p_register_types)
169