Merge "virtio-9p: enable the virtfs on Windows." into tizen
[sdk/emulator/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 #ifdef CONFIG_MARU
24 #include "../tizen/src/maru_err_table.h"
25 // extern char* maru_convert_path(char* msg, const char *path);
26 #endif
27
28 static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
29 {
30     features |= 1 << VIRTIO_9P_MOUNT_TAG;
31     return features;
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 = 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 int virtio_9p_device_init(VirtIODevice *vdev)
50 {
51     V9fsState *s = VIRTIO_9P(vdev);
52     int i, len;
53     struct stat stat;
54     FsDriverEntry *fse;
55     V9fsPath path;
56
57     virtio_init(VIRTIO_DEVICE(s), "virtio-9p", VIRTIO_ID_9P,
58                 sizeof(struct virtio_9p_config) + MAX_TAG_LEN);
59
60     /* initialize pdu allocator */
61     QLIST_INIT(&s->free_list);
62     QLIST_INIT(&s->active_list);
63     for (i = 0; i < (MAX_REQ - 1); i++) {
64         QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
65     }
66
67     s->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output);
68
69     v9fs_path_init(&path);
70
71     fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
72
73     if (!fse) {
74         /* We don't have a fsdev identified by fsdev_id */
75         fprintf(stderr, "Virtio-9p device couldn't find fsdev with the "
76                 "id = %s\n",
77                 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
78         goto out;
79     }
80
81     if (!s->fsconf.tag) {
82         /* we haven't specified a mount_tag */
83         fprintf(stderr, "fsdev with id %s needs mount_tag arguments\n",
84                 s->fsconf.fsdev_id);
85         goto out;
86     }
87
88     s->ctx.export_flags = fse->export_flags;
89     s->ctx.fs_root = g_strdup(fse->path);
90     s->ctx.exops.get_st_gen = NULL;
91     len = strlen(s->fsconf.tag);
92     if (len > MAX_TAG_LEN - 1) {
93         fprintf(stderr, "mount tag '%s' (%d bytes) is longer than "
94                 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
95         goto out;
96     }
97
98     s->tag = g_strdup(s->fsconf.tag);
99     s->ctx.uid = -1;
100
101     s->ops = fse->ops;
102     s->config_size = sizeof(struct virtio_9p_config) + len;
103     s->fid_list = NULL;
104     qemu_co_rwlock_init(&s->rename_lock);
105
106     if (s->ops->init(&s->ctx) < 0) {
107         fprintf(stderr, "Virtio-9p Failed to initialize fs-driver with id:%s"
108                 " and export path:%s\n", s->fsconf.fsdev_id, s->ctx.fs_root);
109 #ifdef CONFIG_MARU
110         const char _msg[] = "Failed to find the file sharing path. Check if the path is correct or not.\n\n";
111         char* err_msg = NULL;
112         err_msg = maru_convert_path((char*)_msg, s->ctx.fs_root);
113                 maru_register_exit_msg(MARU_EXIT_UNKNOWN, err_msg);
114         if (err_msg) {
115             g_free(err_msg);
116         }
117 #endif
118
119         goto out;
120     }
121     if (v9fs_init_worker_threads() < 0) {
122         fprintf(stderr, "worker thread initialization failed\n");
123         goto out;
124     }
125
126     /*
127      * Check details of export path, We need to use fs driver
128      * call back to do that. Since we are in the init path, we don't
129      * use co-routines here.
130      */
131 #ifdef CONFIG_MARU
132 #ifndef CONFIG_WIN32
133     if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
134 #else
135     if (s->ops->name_to_path(&s->ctx, NULL, "\\", &path) < 0) {
136 #endif
137 #else
138     if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
139 #endif
140         fprintf(stderr,
141                 "error in converting name to path %s", strerror(errno));
142         goto out;
143     }
144     if (s->ops->lstat(&s->ctx, &path, &stat)) {
145         fprintf(stderr, "share path %s does not exist\n", fse->path);
146         goto out;
147     } else if (!S_ISDIR(stat.st_mode)) {
148         fprintf(stderr, "share path %s is not a directory\n", fse->path);
149         goto out;
150     }
151     v9fs_path_free(&path);
152
153     return 0;
154 out:
155     g_free(s->ctx.fs_root);
156     g_free(s->tag);
157     virtio_cleanup(vdev);
158     v9fs_path_free(&path);
159
160     return -1;
161
162 }
163
164 /* virtio-9p device */
165
166 static Property virtio_9p_properties[] = {
167     DEFINE_VIRTIO_9P_PROPERTIES(V9fsState, fsconf),
168     DEFINE_PROP_END_OF_LIST(),
169 };
170
171 static void virtio_9p_class_init(ObjectClass *klass, void *data)
172 {
173     DeviceClass *dc = DEVICE_CLASS(klass);
174     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
175     dc->props = virtio_9p_properties;
176     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
177     vdc->init = virtio_9p_device_init;
178     vdc->get_features = virtio_9p_get_features;
179     vdc->get_config = virtio_9p_get_config;
180 }
181
182 static const TypeInfo virtio_device_info = {
183     .name = TYPE_VIRTIO_9P,
184     .parent = TYPE_VIRTIO_DEVICE,
185     .instance_size = sizeof(V9fsState),
186     .class_init = virtio_9p_class_init,
187 };
188
189 static void virtio_9p_register_types(void)
190 {
191     type_register_static(&virtio_device_info);
192 }
193
194 type_init(virtio_9p_register_types)