Statistics
| Branch: | Revision:

root / hw / virtio-9p.c @ 9f107513

History | View | Annotate | Download (6.7 kB)

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 "virtio.h"
15
#include "pc.h"
16
#include "qemu_socket.h"
17
#include "virtio-9p.h"
18
#include "fsdev/qemu-fsdev.h"
19
#include "virtio-9p-debug.h"
20

    
21
int dotu = 1;
22
int debug_9p_pdu;
23

    
24
static V9fsPDU *alloc_pdu(V9fsState *s)
25
{
26
    V9fsPDU *pdu = NULL;
27

    
28
    if (!QLIST_EMPTY(&s->free_list)) {
29
        pdu = QLIST_FIRST(&s->free_list);
30
        QLIST_REMOVE(pdu, next);
31
    }
32
    return pdu;
33
}
34

    
35
static void free_pdu(V9fsState *s, V9fsPDU *pdu)
36
{
37
    if (pdu) {
38
        QLIST_INSERT_HEAD(&s->free_list, pdu, next);
39
    }
40
}
41

    
42
size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
43
                        size_t offset, size_t size, int pack)
44
{
45
    int i = 0;
46
    size_t copied = 0;
47

    
48
    for (i = 0; size && i < sg_count; i++) {
49
        size_t len;
50
        if (offset >= sg[i].iov_len) {
51
            /* skip this sg */
52
            offset -= sg[i].iov_len;
53
            continue;
54
        } else {
55
            len = MIN(sg[i].iov_len - offset, size);
56
            if (pack) {
57
                memcpy(sg[i].iov_base + offset, addr, len);
58
            } else {
59
                memcpy(addr, sg[i].iov_base + offset, len);
60
            }
61
            size -= len;
62
            copied += len;
63
            addr += len;
64
            if (size) {
65
                offset = 0;
66
                continue;
67
            }
68
        }
69
    }
70

    
71
    return copied;
72
}
73

    
74
static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
75
{
76
    if (debug_9p_pdu) {
77
        pprint_pdu(pdu);
78
    }
79
}
80

    
81
static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
82
{
83
    if (debug_9p_pdu) {
84
        pprint_pdu(pdu);
85
    }
86
}
87

    
88
static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
89
{
90
    if (debug_9p_pdu) {
91
        pprint_pdu(pdu);
92
    }
93
}
94

    
95
static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
96
{
97
    if (debug_9p_pdu) {
98
        pprint_pdu(pdu);
99
    }
100
}
101

    
102
static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
103
{
104
    if (debug_9p_pdu) {
105
        pprint_pdu(pdu);
106
    }
107
}
108

    
109
static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
110
{    if (debug_9p_pdu) {
111
        pprint_pdu(pdu);
112
     }
113
}
114

    
115
static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
116
{
117
    if (debug_9p_pdu) {
118
        pprint_pdu(pdu);
119
    }
120
}
121

    
122
static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
123
{
124
    if (debug_9p_pdu) {
125
        pprint_pdu(pdu);
126
    }
127
}
128

    
129
static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
130
{
131
    if (debug_9p_pdu) {
132
        pprint_pdu(pdu);
133
    }
134
}
135

    
136
static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
137
{
138
    if (debug_9p_pdu) {
139
        pprint_pdu(pdu);
140
    }
141
}
142

    
143
static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
144
{
145
    if (debug_9p_pdu) {
146
        pprint_pdu(pdu);
147
    }
148
}
149

    
150
static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
151
{
152
    if (debug_9p_pdu) {
153
        pprint_pdu(pdu);
154
    }
155
}
156

    
157
typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
158

    
159
static pdu_handler_t *pdu_handlers[] = {
160
    [P9_TVERSION] = v9fs_version,
161
    [P9_TATTACH] = v9fs_attach,
162
    [P9_TSTAT] = v9fs_stat,
163
    [P9_TWALK] = v9fs_walk,
164
    [P9_TCLUNK] = v9fs_clunk,
165
    [P9_TOPEN] = v9fs_open,
166
    [P9_TREAD] = v9fs_read,
167
#if 0
168
    [P9_TAUTH] = v9fs_auth,
169
#endif
170
    [P9_TFLUSH] = v9fs_flush,
171
    [P9_TCREATE] = v9fs_create,
172
    [P9_TWRITE] = v9fs_write,
173
    [P9_TWSTAT] = v9fs_wstat,
174
    [P9_TREMOVE] = v9fs_remove,
175
};
176

    
177
static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
178
{
179
    pdu_handler_t *handler;
180

    
181
    if (debug_9p_pdu) {
182
        pprint_pdu(pdu);
183
    }
184

    
185
    BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
186

    
187
    handler = pdu_handlers[pdu->id];
188
    BUG_ON(handler == NULL);
189

    
190
    handler(s, pdu);
191
}
192

    
193
static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
194
{
195
    V9fsState *s = (V9fsState *)vdev;
196
    V9fsPDU *pdu;
197
    ssize_t len;
198

    
199
    while ((pdu = alloc_pdu(s)) &&
200
            (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
201
        uint8_t *ptr;
202

    
203
        BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
204
        BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
205

    
206
        ptr = pdu->elem.out_sg[0].iov_base;
207

    
208
        memcpy(&pdu->size, ptr, 4);
209
        pdu->id = ptr[4];
210
        memcpy(&pdu->tag, ptr + 5, 2);
211

    
212
        submit_pdu(s, pdu);
213
    }
214

    
215
    free_pdu(s, pdu);
216
}
217

    
218
static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
219
{
220
    features |= 1 << VIRTIO_9P_MOUNT_TAG;
221
    return features;
222
}
223

    
224
static V9fsState *to_virtio_9p(VirtIODevice *vdev)
225
{
226
    return (V9fsState *)vdev;
227
}
228

    
229
static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
230
{
231
    struct virtio_9p_config *cfg;
232
    V9fsState *s = to_virtio_9p(vdev);
233

    
234
    cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
235
                        s->tag_len);
236
    stw_raw(&cfg->tag_len, s->tag_len);
237
    memcpy(cfg->tag, s->tag, s->tag_len);
238
    memcpy(config, cfg, s->config_size);
239
    qemu_free(cfg);
240
}
241

    
242
VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
243
 {
244
    V9fsState *s;
245
    int i, len;
246
    struct stat stat;
247
    FsTypeEntry *fse;
248

    
249

    
250
    s = (V9fsState *)virtio_common_init("virtio-9p",
251
                                    VIRTIO_ID_9P,
252
                                    sizeof(struct virtio_9p_config)+
253
                                    MAX_TAG_LEN,
254
                                    sizeof(V9fsState));
255

    
256
    /* initialize pdu allocator */
257
    QLIST_INIT(&s->free_list);
258
    for (i = 0; i < (MAX_REQ - 1); i++) {
259
        QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
260
    }
261

    
262
    s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
263

    
264
    fse = get_fsdev_fsentry(conf->fsdev_id);
265

    
266
    if (!fse) {
267
        /* We don't have a fsdev identified by fsdev_id */
268
        fprintf(stderr, "Virtio-9p device couldn't find fsdev "
269
                    "with the id %s\n", conf->fsdev_id);
270
        exit(1);
271
    }
272

    
273
    if (!fse->path || !conf->tag) {
274
        /* we haven't specified a mount_tag or the path */
275
        fprintf(stderr, "fsdev with id %s needs path "
276
                "and Virtio-9p device needs mount_tag arguments\n",
277
                conf->fsdev_id);
278
        exit(1);
279
    }
280

    
281
    if (lstat(fse->path, &stat)) {
282
        fprintf(stderr, "share path %s does not exist\n", fse->path);
283
        exit(1);
284
    } else if (!S_ISDIR(stat.st_mode)) {
285
        fprintf(stderr, "share path %s is not a directory \n", fse->path);
286
        exit(1);
287
    }
288

    
289
    s->ctx.fs_root = qemu_strdup(fse->path);
290
    len = strlen(conf->tag);
291
    if (len > MAX_TAG_LEN) {
292
        len = MAX_TAG_LEN;
293
    }
294
    /* s->tag is non-NULL terminated string */
295
    s->tag = qemu_malloc(len);
296
    memcpy(s->tag, conf->tag, len);
297
    s->tag_len = len;
298
    s->ctx.uid = -1;
299

    
300
    s->ops = fse->ops;
301
    s->vdev.get_features = virtio_9p_get_features;
302
    s->config_size = sizeof(struct virtio_9p_config) +
303
                        s->tag_len;
304
    s->vdev.get_config = virtio_9p_get_config;
305

    
306
    return &s->vdev;
307
}