Statistics
| Branch: | Revision:

root / hw / virtio-9p.c @ 072c2c31

History | View | Annotate | Download (56.5 kB)

1 9f107513 Anthony Liguori
/*
2 9f107513 Anthony Liguori
 * Virtio 9p backend
3 9f107513 Anthony Liguori
 *
4 9f107513 Anthony Liguori
 * Copyright IBM, Corp. 2010
5 9f107513 Anthony Liguori
 *
6 9f107513 Anthony Liguori
 * Authors:
7 9f107513 Anthony Liguori
 *  Anthony Liguori   <aliguori@us.ibm.com>
8 9f107513 Anthony Liguori
 *
9 9f107513 Anthony Liguori
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10 9f107513 Anthony Liguori
 * the COPYING file in the top-level directory.
11 9f107513 Anthony Liguori
 *
12 9f107513 Anthony Liguori
 */
13 9f107513 Anthony Liguori
14 9f107513 Anthony Liguori
#include "virtio.h"
15 9f107513 Anthony Liguori
#include "pc.h"
16 9f107513 Anthony Liguori
#include "qemu_socket.h"
17 9f107513 Anthony Liguori
#include "virtio-9p.h"
18 9f107513 Anthony Liguori
#include "fsdev/qemu-fsdev.h"
19 9f107513 Anthony Liguori
#include "virtio-9p-debug.h"
20 9f107513 Anthony Liguori
21 9f107513 Anthony Liguori
int dotu = 1;
22 9f107513 Anthony Liguori
int debug_9p_pdu;
23 9f107513 Anthony Liguori
24 131dcb25 Anthony Liguori
static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf)
25 131dcb25 Anthony Liguori
{
26 131dcb25 Anthony Liguori
    return s->ops->lstat(&s->ctx, path->data, stbuf);
27 131dcb25 Anthony Liguori
}
28 131dcb25 Anthony Liguori
29 131dcb25 Anthony Liguori
static int v9fs_do_setuid(V9fsState *s, uid_t uid)
30 131dcb25 Anthony Liguori
{
31 131dcb25 Anthony Liguori
    return s->ops->setuid(&s->ctx, uid);
32 131dcb25 Anthony Liguori
}
33 131dcb25 Anthony Liguori
34 131dcb25 Anthony Liguori
static ssize_t v9fs_do_readlink(V9fsState *s, V9fsString *path, V9fsString *buf)
35 131dcb25 Anthony Liguori
{
36 131dcb25 Anthony Liguori
    ssize_t len;
37 131dcb25 Anthony Liguori
38 131dcb25 Anthony Liguori
    buf->data = qemu_malloc(1024);
39 131dcb25 Anthony Liguori
40 131dcb25 Anthony Liguori
    len = s->ops->readlink(&s->ctx, path->data, buf->data, 1024 - 1);
41 131dcb25 Anthony Liguori
    if (len > -1) {
42 131dcb25 Anthony Liguori
        buf->size = len;
43 131dcb25 Anthony Liguori
        buf->data[len] = 0;
44 131dcb25 Anthony Liguori
    }
45 131dcb25 Anthony Liguori
46 131dcb25 Anthony Liguori
    return len;
47 131dcb25 Anthony Liguori
}
48 131dcb25 Anthony Liguori
49 131dcb25 Anthony Liguori
static int v9fs_do_close(V9fsState *s, int fd)
50 131dcb25 Anthony Liguori
{
51 131dcb25 Anthony Liguori
    return s->ops->close(&s->ctx, fd);
52 131dcb25 Anthony Liguori
}
53 131dcb25 Anthony Liguori
54 131dcb25 Anthony Liguori
static int v9fs_do_closedir(V9fsState *s, DIR *dir)
55 131dcb25 Anthony Liguori
{
56 131dcb25 Anthony Liguori
    return s->ops->closedir(&s->ctx, dir);
57 131dcb25 Anthony Liguori
}
58 131dcb25 Anthony Liguori
59 a6568fe2 Anthony Liguori
static int v9fs_do_open(V9fsState *s, V9fsString *path, int flags)
60 a6568fe2 Anthony Liguori
{
61 a6568fe2 Anthony Liguori
    return s->ops->open(&s->ctx, path->data, flags);
62 a6568fe2 Anthony Liguori
}
63 a6568fe2 Anthony Liguori
64 a6568fe2 Anthony Liguori
static DIR *v9fs_do_opendir(V9fsState *s, V9fsString *path)
65 a6568fe2 Anthony Liguori
{
66 a6568fe2 Anthony Liguori
    return s->ops->opendir(&s->ctx, path->data);
67 a6568fe2 Anthony Liguori
}
68 a6568fe2 Anthony Liguori
69 a9231555 Anthony Liguori
static void v9fs_do_rewinddir(V9fsState *s, DIR *dir)
70 a9231555 Anthony Liguori
{
71 a9231555 Anthony Liguori
    return s->ops->rewinddir(&s->ctx, dir);
72 a9231555 Anthony Liguori
}
73 a9231555 Anthony Liguori
74 a9231555 Anthony Liguori
static off_t v9fs_do_telldir(V9fsState *s, DIR *dir)
75 a9231555 Anthony Liguori
{
76 a9231555 Anthony Liguori
    return s->ops->telldir(&s->ctx, dir);
77 a9231555 Anthony Liguori
}
78 a9231555 Anthony Liguori
79 a9231555 Anthony Liguori
static struct dirent *v9fs_do_readdir(V9fsState *s, DIR *dir)
80 a9231555 Anthony Liguori
{
81 a9231555 Anthony Liguori
    return s->ops->readdir(&s->ctx, dir);
82 a9231555 Anthony Liguori
}
83 a9231555 Anthony Liguori
84 a9231555 Anthony Liguori
static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off)
85 a9231555 Anthony Liguori
{
86 a9231555 Anthony Liguori
    return s->ops->seekdir(&s->ctx, dir, off);
87 a9231555 Anthony Liguori
}
88 a9231555 Anthony Liguori
89 a9231555 Anthony Liguori
static int v9fs_do_readv(V9fsState *s, int fd, const struct iovec *iov,
90 a9231555 Anthony Liguori
                            int iovcnt)
91 a9231555 Anthony Liguori
{
92 a9231555 Anthony Liguori
    return s->ops->readv(&s->ctx, fd, iov, iovcnt);
93 a9231555 Anthony Liguori
}
94 a9231555 Anthony Liguori
95 a9231555 Anthony Liguori
static off_t v9fs_do_lseek(V9fsState *s, int fd, off_t offset, int whence)
96 a9231555 Anthony Liguori
{
97 a9231555 Anthony Liguori
    return s->ops->lseek(&s->ctx, fd, offset, whence);
98 a9231555 Anthony Liguori
}
99 a9231555 Anthony Liguori
100 8449360c Anthony Liguori
static int v9fs_do_writev(V9fsState *s, int fd, const struct iovec *iov,
101 8449360c Anthony Liguori
                       int iovcnt)
102 8449360c Anthony Liguori
{
103 8449360c Anthony Liguori
    return s->ops->writev(&s->ctx, fd, iov, iovcnt);
104 8449360c Anthony Liguori
}
105 8449360c Anthony Liguori
106 c494dd6f Anthony Liguori
static int v9fs_do_chmod(V9fsState *s, V9fsString *path, mode_t mode)
107 c494dd6f Anthony Liguori
{
108 c494dd6f Anthony Liguori
    return s->ops->chmod(&s->ctx, path->data, mode);
109 c494dd6f Anthony Liguori
}
110 c494dd6f Anthony Liguori
111 c494dd6f Anthony Liguori
static int v9fs_do_mknod(V9fsState *s, V9fsString *path, mode_t mode, dev_t dev)
112 c494dd6f Anthony Liguori
{
113 c494dd6f Anthony Liguori
    return s->ops->mknod(&s->ctx, path->data, mode, dev);
114 c494dd6f Anthony Liguori
}
115 c494dd6f Anthony Liguori
116 c494dd6f Anthony Liguori
static int v9fs_do_mksock(V9fsState *s, V9fsString *path)
117 c494dd6f Anthony Liguori
{
118 c494dd6f Anthony Liguori
    return s->ops->mksock(&s->ctx, path->data);
119 c494dd6f Anthony Liguori
}
120 c494dd6f Anthony Liguori
121 c494dd6f Anthony Liguori
static int v9fs_do_mkdir(V9fsState *s, V9fsString *path, mode_t mode)
122 c494dd6f Anthony Liguori
{
123 c494dd6f Anthony Liguori
    return s->ops->mkdir(&s->ctx, path->data, mode);
124 c494dd6f Anthony Liguori
}
125 c494dd6f Anthony Liguori
126 c494dd6f Anthony Liguori
static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
127 c494dd6f Anthony Liguori
{
128 c494dd6f Anthony Liguori
    return s->ops->fstat(&s->ctx, fd, stbuf);
129 c494dd6f Anthony Liguori
}
130 c494dd6f Anthony Liguori
131 c494dd6f Anthony Liguori
static int v9fs_do_open2(V9fsState *s, V9fsString *path, int flags, mode_t mode)
132 c494dd6f Anthony Liguori
{
133 c494dd6f Anthony Liguori
    return s->ops->open2(&s->ctx, path->data, flags, mode);
134 c494dd6f Anthony Liguori
}
135 c494dd6f Anthony Liguori
136 c494dd6f Anthony Liguori
static int v9fs_do_symlink(V9fsState *s, V9fsString *oldpath,
137 c494dd6f Anthony Liguori
                            V9fsString *newpath)
138 c494dd6f Anthony Liguori
{
139 c494dd6f Anthony Liguori
    return s->ops->symlink(&s->ctx, oldpath->data, newpath->data);
140 c494dd6f Anthony Liguori
}
141 c494dd6f Anthony Liguori
142 c494dd6f Anthony Liguori
static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
143 c494dd6f Anthony Liguori
{
144 c494dd6f Anthony Liguori
    return s->ops->link(&s->ctx, oldpath->data, newpath->data);
145 c494dd6f Anthony Liguori
}
146 c494dd6f Anthony Liguori
147 8cf89e00 Anthony Liguori
static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
148 8cf89e00 Anthony Liguori
{
149 8cf89e00 Anthony Liguori
    return s->ops->truncate(&s->ctx, path->data, size);
150 8cf89e00 Anthony Liguori
}
151 8cf89e00 Anthony Liguori
152 8cf89e00 Anthony Liguori
static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath,
153 8cf89e00 Anthony Liguori
                            V9fsString *newpath)
154 8cf89e00 Anthony Liguori
{
155 8cf89e00 Anthony Liguori
    return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
156 8cf89e00 Anthony Liguori
}
157 8cf89e00 Anthony Liguori
158 8cf89e00 Anthony Liguori
static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
159 8cf89e00 Anthony Liguori
{
160 8cf89e00 Anthony Liguori
    return s->ops->chown(&s->ctx, path->data, uid, gid);
161 8cf89e00 Anthony Liguori
}
162 8cf89e00 Anthony Liguori
163 8cf89e00 Anthony Liguori
static int v9fs_do_utime(V9fsState *s, V9fsString *path,
164 8cf89e00 Anthony Liguori
                            const struct utimbuf *buf)
165 8cf89e00 Anthony Liguori
{
166 8cf89e00 Anthony Liguori
    return s->ops->utime(&s->ctx, path->data, buf);
167 8cf89e00 Anthony Liguori
}
168 8cf89e00 Anthony Liguori
169 5bae1900 Anthony Liguori
static int v9fs_do_remove(V9fsState *s, V9fsString *path)
170 5bae1900 Anthony Liguori
{
171 5bae1900 Anthony Liguori
    return s->ops->remove(&s->ctx, path->data);
172 5bae1900 Anthony Liguori
}
173 5bae1900 Anthony Liguori
174 8cf89e00 Anthony Liguori
static int v9fs_do_fsync(V9fsState *s, int fd)
175 8cf89e00 Anthony Liguori
{
176 8cf89e00 Anthony Liguori
    return s->ops->fsync(&s->ctx, fd);
177 8cf89e00 Anthony Liguori
}
178 8cf89e00 Anthony Liguori
179 a03f7874 Anthony Liguori
static void v9fs_string_init(V9fsString *str)
180 a03f7874 Anthony Liguori
{
181 a03f7874 Anthony Liguori
    str->data = NULL;
182 a03f7874 Anthony Liguori
    str->size = 0;
183 a03f7874 Anthony Liguori
}
184 a03f7874 Anthony Liguori
185 a03f7874 Anthony Liguori
static void v9fs_string_free(V9fsString *str)
186 a03f7874 Anthony Liguori
{
187 a03f7874 Anthony Liguori
    qemu_free(str->data);
188 a03f7874 Anthony Liguori
    str->data = NULL;
189 a03f7874 Anthony Liguori
    str->size = 0;
190 a03f7874 Anthony Liguori
}
191 a03f7874 Anthony Liguori
192 a03f7874 Anthony Liguori
static void v9fs_string_null(V9fsString *str)
193 a03f7874 Anthony Liguori
{
194 a03f7874 Anthony Liguori
    v9fs_string_free(str);
195 a03f7874 Anthony Liguori
}
196 a03f7874 Anthony Liguori
197 a03f7874 Anthony Liguori
static int number_to_string(void *arg, char type)
198 a03f7874 Anthony Liguori
{
199 a03f7874 Anthony Liguori
    unsigned int ret = 0;
200 a03f7874 Anthony Liguori
201 a03f7874 Anthony Liguori
    switch (type) {
202 a03f7874 Anthony Liguori
    case 'u': {
203 a03f7874 Anthony Liguori
        unsigned int num = *(unsigned int *)arg;
204 a03f7874 Anthony Liguori
205 a03f7874 Anthony Liguori
        do {
206 a03f7874 Anthony Liguori
            ret++;
207 a03f7874 Anthony Liguori
            num = num/10;
208 a03f7874 Anthony Liguori
        } while (num);
209 a03f7874 Anthony Liguori
        break;
210 a03f7874 Anthony Liguori
    }
211 a03f7874 Anthony Liguori
    default:
212 a03f7874 Anthony Liguori
        printf("Number_to_string: Unknown number format\n");
213 a03f7874 Anthony Liguori
        return -1;
214 a03f7874 Anthony Liguori
    }
215 a03f7874 Anthony Liguori
216 a03f7874 Anthony Liguori
    return ret;
217 a03f7874 Anthony Liguori
}
218 a03f7874 Anthony Liguori
219 a03f7874 Anthony Liguori
static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
220 a03f7874 Anthony Liguori
{
221 a03f7874 Anthony Liguori
    va_list ap2;
222 a03f7874 Anthony Liguori
    char *iter = (char *)fmt;
223 a03f7874 Anthony Liguori
    int len = 0;
224 a03f7874 Anthony Liguori
    int nr_args = 0;
225 a03f7874 Anthony Liguori
    char *arg_char_ptr;
226 a03f7874 Anthony Liguori
    unsigned int arg_uint;
227 a03f7874 Anthony Liguori
228 a03f7874 Anthony Liguori
    /* Find the number of %'s that denotes an argument */
229 a03f7874 Anthony Liguori
    for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
230 a03f7874 Anthony Liguori
        nr_args++;
231 a03f7874 Anthony Liguori
        iter++;
232 a03f7874 Anthony Liguori
    }
233 a03f7874 Anthony Liguori
234 a03f7874 Anthony Liguori
    len = strlen(fmt) - 2*nr_args;
235 a03f7874 Anthony Liguori
236 a03f7874 Anthony Liguori
    if (!nr_args) {
237 a03f7874 Anthony Liguori
        goto alloc_print;
238 a03f7874 Anthony Liguori
    }
239 a03f7874 Anthony Liguori
240 a03f7874 Anthony Liguori
    va_copy(ap2, ap);
241 a03f7874 Anthony Liguori
242 a03f7874 Anthony Liguori
    iter = (char *)fmt;
243 a03f7874 Anthony Liguori
244 a03f7874 Anthony Liguori
    /* Now parse the format string */
245 a03f7874 Anthony Liguori
    for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
246 a03f7874 Anthony Liguori
        iter++;
247 a03f7874 Anthony Liguori
        switch (*iter) {
248 a03f7874 Anthony Liguori
        case 'u':
249 a03f7874 Anthony Liguori
            arg_uint = va_arg(ap2, unsigned int);
250 a03f7874 Anthony Liguori
            len += number_to_string((void *)&arg_uint, 'u');
251 a03f7874 Anthony Liguori
            break;
252 a03f7874 Anthony Liguori
        case 's':
253 a03f7874 Anthony Liguori
            arg_char_ptr = va_arg(ap2, char *);
254 a03f7874 Anthony Liguori
            len += strlen(arg_char_ptr);
255 a03f7874 Anthony Liguori
            break;
256 a03f7874 Anthony Liguori
        case 'c':
257 a03f7874 Anthony Liguori
            len += 1;
258 a03f7874 Anthony Liguori
            break;
259 a03f7874 Anthony Liguori
        default:
260 a03f7874 Anthony Liguori
            fprintf(stderr,
261 a03f7874 Anthony Liguori
                    "v9fs_string_alloc_printf:Incorrect format %c", *iter);
262 a03f7874 Anthony Liguori
            return -1;
263 a03f7874 Anthony Liguori
        }
264 a03f7874 Anthony Liguori
        iter++;
265 a03f7874 Anthony Liguori
    }
266 a03f7874 Anthony Liguori
267 a03f7874 Anthony Liguori
alloc_print:
268 a03f7874 Anthony Liguori
    *strp = qemu_malloc((len + 1) * sizeof(**strp));
269 a03f7874 Anthony Liguori
270 a03f7874 Anthony Liguori
    return vsprintf(*strp, fmt, ap);
271 a03f7874 Anthony Liguori
}
272 a03f7874 Anthony Liguori
273 a03f7874 Anthony Liguori
static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
274 a03f7874 Anthony Liguori
{
275 a03f7874 Anthony Liguori
    va_list ap;
276 a03f7874 Anthony Liguori
    int err;
277 a03f7874 Anthony Liguori
278 a03f7874 Anthony Liguori
    v9fs_string_free(str);
279 a03f7874 Anthony Liguori
280 a03f7874 Anthony Liguori
    va_start(ap, fmt);
281 a03f7874 Anthony Liguori
    err = v9fs_string_alloc_printf(&str->data, fmt, ap);
282 a03f7874 Anthony Liguori
    BUG_ON(err == -1);
283 a03f7874 Anthony Liguori
    va_end(ap);
284 a03f7874 Anthony Liguori
285 a03f7874 Anthony Liguori
    str->size = err;
286 a03f7874 Anthony Liguori
}
287 a03f7874 Anthony Liguori
288 a03f7874 Anthony Liguori
static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
289 a03f7874 Anthony Liguori
{
290 a03f7874 Anthony Liguori
    v9fs_string_free(lhs);
291 a03f7874 Anthony Liguori
    v9fs_string_sprintf(lhs, "%s", rhs->data);
292 a03f7874 Anthony Liguori
}
293 a03f7874 Anthony Liguori
294 a03f7874 Anthony Liguori
static size_t v9fs_string_size(V9fsString *str)
295 a03f7874 Anthony Liguori
{
296 a03f7874 Anthony Liguori
    return str->size;
297 a03f7874 Anthony Liguori
}
298 a03f7874 Anthony Liguori
299 286d5652 Anthony Liguori
static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
300 286d5652 Anthony Liguori
{
301 286d5652 Anthony Liguori
    V9fsFidState *f;
302 286d5652 Anthony Liguori
303 286d5652 Anthony Liguori
    for (f = s->fid_list; f; f = f->next) {
304 286d5652 Anthony Liguori
        if (f->fid == fid) {
305 286d5652 Anthony Liguori
            v9fs_do_setuid(s, f->uid);
306 286d5652 Anthony Liguori
            return f;
307 286d5652 Anthony Liguori
        }
308 286d5652 Anthony Liguori
    }
309 286d5652 Anthony Liguori
310 286d5652 Anthony Liguori
    return NULL;
311 286d5652 Anthony Liguori
}
312 286d5652 Anthony Liguori
313 286d5652 Anthony Liguori
static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
314 286d5652 Anthony Liguori
{
315 286d5652 Anthony Liguori
    V9fsFidState *f;
316 286d5652 Anthony Liguori
317 286d5652 Anthony Liguori
    f = lookup_fid(s, fid);
318 286d5652 Anthony Liguori
    if (f) {
319 286d5652 Anthony Liguori
        return NULL;
320 286d5652 Anthony Liguori
    }
321 286d5652 Anthony Liguori
322 286d5652 Anthony Liguori
    f = qemu_mallocz(sizeof(V9fsFidState));
323 286d5652 Anthony Liguori
324 286d5652 Anthony Liguori
    f->fid = fid;
325 286d5652 Anthony Liguori
    f->fd = -1;
326 286d5652 Anthony Liguori
    f->dir = NULL;
327 286d5652 Anthony Liguori
328 286d5652 Anthony Liguori
    f->next = s->fid_list;
329 286d5652 Anthony Liguori
    s->fid_list = f;
330 286d5652 Anthony Liguori
331 286d5652 Anthony Liguori
    return f;
332 286d5652 Anthony Liguori
}
333 286d5652 Anthony Liguori
334 286d5652 Anthony Liguori
static int free_fid(V9fsState *s, int32_t fid)
335 286d5652 Anthony Liguori
{
336 286d5652 Anthony Liguori
    V9fsFidState **fidpp, *fidp;
337 286d5652 Anthony Liguori
338 286d5652 Anthony Liguori
    for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
339 286d5652 Anthony Liguori
        if ((*fidpp)->fid == fid) {
340 286d5652 Anthony Liguori
            break;
341 286d5652 Anthony Liguori
        }
342 286d5652 Anthony Liguori
    }
343 286d5652 Anthony Liguori
344 286d5652 Anthony Liguori
    if (*fidpp == NULL) {
345 286d5652 Anthony Liguori
        return -ENOENT;
346 286d5652 Anthony Liguori
    }
347 286d5652 Anthony Liguori
348 286d5652 Anthony Liguori
    fidp = *fidpp;
349 286d5652 Anthony Liguori
    *fidpp = fidp->next;
350 286d5652 Anthony Liguori
351 286d5652 Anthony Liguori
    if (fidp->fd != -1) {
352 286d5652 Anthony Liguori
        v9fs_do_close(s, fidp->fd);
353 286d5652 Anthony Liguori
    }
354 286d5652 Anthony Liguori
    if (fidp->dir) {
355 286d5652 Anthony Liguori
        v9fs_do_closedir(s, fidp->dir);
356 286d5652 Anthony Liguori
    }
357 286d5652 Anthony Liguori
    v9fs_string_free(&fidp->path);
358 286d5652 Anthony Liguori
    qemu_free(fidp);
359 286d5652 Anthony Liguori
360 286d5652 Anthony Liguori
    return 0;
361 286d5652 Anthony Liguori
}
362 286d5652 Anthony Liguori
363 286d5652 Anthony Liguori
#define P9_QID_TYPE_DIR         0x80
364 286d5652 Anthony Liguori
#define P9_QID_TYPE_SYMLINK     0x02
365 286d5652 Anthony Liguori
366 286d5652 Anthony Liguori
#define P9_STAT_MODE_DIR        0x80000000
367 286d5652 Anthony Liguori
#define P9_STAT_MODE_APPEND     0x40000000
368 286d5652 Anthony Liguori
#define P9_STAT_MODE_EXCL       0x20000000
369 286d5652 Anthony Liguori
#define P9_STAT_MODE_MOUNT      0x10000000
370 286d5652 Anthony Liguori
#define P9_STAT_MODE_AUTH       0x08000000
371 286d5652 Anthony Liguori
#define P9_STAT_MODE_TMP        0x04000000
372 286d5652 Anthony Liguori
#define P9_STAT_MODE_SYMLINK    0x02000000
373 286d5652 Anthony Liguori
#define P9_STAT_MODE_LINK       0x01000000
374 286d5652 Anthony Liguori
#define P9_STAT_MODE_DEVICE     0x00800000
375 286d5652 Anthony Liguori
#define P9_STAT_MODE_NAMED_PIPE 0x00200000
376 286d5652 Anthony Liguori
#define P9_STAT_MODE_SOCKET     0x00100000
377 286d5652 Anthony Liguori
#define P9_STAT_MODE_SETUID     0x00080000
378 286d5652 Anthony Liguori
#define P9_STAT_MODE_SETGID     0x00040000
379 286d5652 Anthony Liguori
#define P9_STAT_MODE_SETVTX     0x00010000
380 286d5652 Anthony Liguori
381 286d5652 Anthony Liguori
#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR |          \
382 286d5652 Anthony Liguori
                                P9_STAT_MODE_SYMLINK |      \
383 286d5652 Anthony Liguori
                                P9_STAT_MODE_LINK |         \
384 286d5652 Anthony Liguori
                                P9_STAT_MODE_DEVICE |       \
385 286d5652 Anthony Liguori
                                P9_STAT_MODE_NAMED_PIPE |   \
386 286d5652 Anthony Liguori
                                P9_STAT_MODE_SOCKET)
387 286d5652 Anthony Liguori
388 286d5652 Anthony Liguori
/* This is the algorithm from ufs in spfs */
389 286d5652 Anthony Liguori
static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
390 286d5652 Anthony Liguori
{
391 286d5652 Anthony Liguori
    size_t size;
392 286d5652 Anthony Liguori
393 286d5652 Anthony Liguori
    size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
394 286d5652 Anthony Liguori
    memcpy(&qidp->path, &stbuf->st_ino, size);
395 286d5652 Anthony Liguori
    qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
396 286d5652 Anthony Liguori
    qidp->type = 0;
397 286d5652 Anthony Liguori
    if (S_ISDIR(stbuf->st_mode)) {
398 286d5652 Anthony Liguori
        qidp->type |= P9_QID_TYPE_DIR;
399 286d5652 Anthony Liguori
    }
400 286d5652 Anthony Liguori
    if (S_ISLNK(stbuf->st_mode)) {
401 286d5652 Anthony Liguori
        qidp->type |= P9_QID_TYPE_SYMLINK;
402 286d5652 Anthony Liguori
    }
403 286d5652 Anthony Liguori
}
404 286d5652 Anthony Liguori
405 286d5652 Anthony Liguori
static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
406 286d5652 Anthony Liguori
{
407 286d5652 Anthony Liguori
    struct stat stbuf;
408 286d5652 Anthony Liguori
    int err;
409 286d5652 Anthony Liguori
410 286d5652 Anthony Liguori
    err = v9fs_do_lstat(s, &fidp->path, &stbuf);
411 286d5652 Anthony Liguori
    if (err) {
412 286d5652 Anthony Liguori
        return err;
413 286d5652 Anthony Liguori
    }
414 286d5652 Anthony Liguori
415 286d5652 Anthony Liguori
    stat_to_qid(&stbuf, qidp);
416 286d5652 Anthony Liguori
    return 0;
417 286d5652 Anthony Liguori
}
418 286d5652 Anthony Liguori
419 9f107513 Anthony Liguori
static V9fsPDU *alloc_pdu(V9fsState *s)
420 9f107513 Anthony Liguori
{
421 9f107513 Anthony Liguori
    V9fsPDU *pdu = NULL;
422 9f107513 Anthony Liguori
423 9f107513 Anthony Liguori
    if (!QLIST_EMPTY(&s->free_list)) {
424 9f107513 Anthony Liguori
        pdu = QLIST_FIRST(&s->free_list);
425 9f107513 Anthony Liguori
        QLIST_REMOVE(pdu, next);
426 9f107513 Anthony Liguori
    }
427 9f107513 Anthony Liguori
    return pdu;
428 9f107513 Anthony Liguori
}
429 9f107513 Anthony Liguori
430 9f107513 Anthony Liguori
static void free_pdu(V9fsState *s, V9fsPDU *pdu)
431 9f107513 Anthony Liguori
{
432 9f107513 Anthony Liguori
    if (pdu) {
433 9f107513 Anthony Liguori
        QLIST_INSERT_HEAD(&s->free_list, pdu, next);
434 9f107513 Anthony Liguori
    }
435 9f107513 Anthony Liguori
}
436 9f107513 Anthony Liguori
437 9f107513 Anthony Liguori
size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
438 9f107513 Anthony Liguori
                        size_t offset, size_t size, int pack)
439 9f107513 Anthony Liguori
{
440 9f107513 Anthony Liguori
    int i = 0;
441 9f107513 Anthony Liguori
    size_t copied = 0;
442 9f107513 Anthony Liguori
443 9f107513 Anthony Liguori
    for (i = 0; size && i < sg_count; i++) {
444 9f107513 Anthony Liguori
        size_t len;
445 9f107513 Anthony Liguori
        if (offset >= sg[i].iov_len) {
446 9f107513 Anthony Liguori
            /* skip this sg */
447 9f107513 Anthony Liguori
            offset -= sg[i].iov_len;
448 9f107513 Anthony Liguori
            continue;
449 9f107513 Anthony Liguori
        } else {
450 9f107513 Anthony Liguori
            len = MIN(sg[i].iov_len - offset, size);
451 9f107513 Anthony Liguori
            if (pack) {
452 9f107513 Anthony Liguori
                memcpy(sg[i].iov_base + offset, addr, len);
453 9f107513 Anthony Liguori
            } else {
454 9f107513 Anthony Liguori
                memcpy(addr, sg[i].iov_base + offset, len);
455 9f107513 Anthony Liguori
            }
456 9f107513 Anthony Liguori
            size -= len;
457 9f107513 Anthony Liguori
            copied += len;
458 9f107513 Anthony Liguori
            addr += len;
459 9f107513 Anthony Liguori
            if (size) {
460 9f107513 Anthony Liguori
                offset = 0;
461 9f107513 Anthony Liguori
                continue;
462 9f107513 Anthony Liguori
            }
463 9f107513 Anthony Liguori
        }
464 9f107513 Anthony Liguori
    }
465 9f107513 Anthony Liguori
466 9f107513 Anthony Liguori
    return copied;
467 9f107513 Anthony Liguori
}
468 9f107513 Anthony Liguori
469 405a549a Anthony Liguori
static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
470 405a549a Anthony Liguori
{
471 405a549a Anthony Liguori
    return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
472 405a549a Anthony Liguori
                         offset, size, 0);
473 405a549a Anthony Liguori
}
474 405a549a Anthony Liguori
475 405a549a Anthony Liguori
static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
476 405a549a Anthony Liguori
                        size_t size)
477 405a549a Anthony Liguori
{
478 405a549a Anthony Liguori
    return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
479 405a549a Anthony Liguori
                             offset, size, 1);
480 405a549a Anthony Liguori
}
481 405a549a Anthony Liguori
482 405a549a Anthony Liguori
static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
483 405a549a Anthony Liguori
{
484 405a549a Anthony Liguori
    size_t pos = 0;
485 405a549a Anthony Liguori
    int i, j;
486 405a549a Anthony Liguori
    struct iovec *src_sg;
487 405a549a Anthony Liguori
    unsigned int num;
488 405a549a Anthony Liguori
489 405a549a Anthony Liguori
    if (rx) {
490 405a549a Anthony Liguori
        src_sg = pdu->elem.in_sg;
491 405a549a Anthony Liguori
        num = pdu->elem.in_num;
492 405a549a Anthony Liguori
    } else {
493 405a549a Anthony Liguori
        src_sg = pdu->elem.out_sg;
494 405a549a Anthony Liguori
        num = pdu->elem.out_num;
495 405a549a Anthony Liguori
    }
496 405a549a Anthony Liguori
497 405a549a Anthony Liguori
    j = 0;
498 405a549a Anthony Liguori
    for (i = 0; i < num; i++) {
499 405a549a Anthony Liguori
        if (offset <= pos) {
500 405a549a Anthony Liguori
            sg[j].iov_base = src_sg[i].iov_base;
501 405a549a Anthony Liguori
            sg[j].iov_len = src_sg[i].iov_len;
502 405a549a Anthony Liguori
            j++;
503 405a549a Anthony Liguori
        } else if (offset < (src_sg[i].iov_len + pos)) {
504 405a549a Anthony Liguori
            sg[j].iov_base = src_sg[i].iov_base;
505 405a549a Anthony Liguori
            sg[j].iov_len = src_sg[i].iov_len;
506 405a549a Anthony Liguori
            sg[j].iov_base += (offset - pos);
507 405a549a Anthony Liguori
            sg[j].iov_len -= (offset - pos);
508 405a549a Anthony Liguori
            j++;
509 405a549a Anthony Liguori
        }
510 405a549a Anthony Liguori
        pos += src_sg[i].iov_len;
511 405a549a Anthony Liguori
    }
512 405a549a Anthony Liguori
513 405a549a Anthony Liguori
    return j;
514 405a549a Anthony Liguori
}
515 405a549a Anthony Liguori
516 405a549a Anthony Liguori
static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
517 405a549a Anthony Liguori
{
518 405a549a Anthony Liguori
    size_t old_offset = offset;
519 405a549a Anthony Liguori
    va_list ap;
520 405a549a Anthony Liguori
    int i;
521 405a549a Anthony Liguori
522 405a549a Anthony Liguori
    va_start(ap, fmt);
523 405a549a Anthony Liguori
    for (i = 0; fmt[i]; i++) {
524 405a549a Anthony Liguori
        switch (fmt[i]) {
525 405a549a Anthony Liguori
        case 'b': {
526 405a549a Anthony Liguori
            uint8_t *valp = va_arg(ap, uint8_t *);
527 405a549a Anthony Liguori
            offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
528 405a549a Anthony Liguori
            break;
529 405a549a Anthony Liguori
        }
530 405a549a Anthony Liguori
        case 'w': {
531 405a549a Anthony Liguori
            uint16_t val, *valp;
532 405a549a Anthony Liguori
            valp = va_arg(ap, uint16_t *);
533 405a549a Anthony Liguori
            val = le16_to_cpupu(valp);
534 405a549a Anthony Liguori
            offset += pdu_unpack(&val, pdu, offset, sizeof(val));
535 405a549a Anthony Liguori
            *valp = val;
536 405a549a Anthony Liguori
            break;
537 405a549a Anthony Liguori
        }
538 405a549a Anthony Liguori
        case 'd': {
539 405a549a Anthony Liguori
            uint32_t val, *valp;
540 405a549a Anthony Liguori
            valp = va_arg(ap, uint32_t *);
541 405a549a Anthony Liguori
            val = le32_to_cpupu(valp);
542 405a549a Anthony Liguori
            offset += pdu_unpack(&val, pdu, offset, sizeof(val));
543 405a549a Anthony Liguori
            *valp = val;
544 405a549a Anthony Liguori
            break;
545 405a549a Anthony Liguori
        }
546 405a549a Anthony Liguori
        case 'q': {
547 405a549a Anthony Liguori
            uint64_t val, *valp;
548 405a549a Anthony Liguori
            valp = va_arg(ap, uint64_t *);
549 405a549a Anthony Liguori
            val = le64_to_cpup(valp);
550 405a549a Anthony Liguori
            offset += pdu_unpack(&val, pdu, offset, sizeof(val));
551 405a549a Anthony Liguori
            *valp = val;
552 405a549a Anthony Liguori
            break;
553 405a549a Anthony Liguori
        }
554 405a549a Anthony Liguori
        case 'v': {
555 405a549a Anthony Liguori
            struct iovec *iov = va_arg(ap, struct iovec *);
556 405a549a Anthony Liguori
            int *iovcnt = va_arg(ap, int *);
557 405a549a Anthony Liguori
            *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
558 405a549a Anthony Liguori
            break;
559 405a549a Anthony Liguori
        }
560 405a549a Anthony Liguori
        case 's': {
561 405a549a Anthony Liguori
            V9fsString *str = va_arg(ap, V9fsString *);
562 405a549a Anthony Liguori
            offset += pdu_unmarshal(pdu, offset, "w", &str->size);
563 405a549a Anthony Liguori
            /* FIXME: sanity check str->size */
564 405a549a Anthony Liguori
            str->data = qemu_malloc(str->size + 1);
565 405a549a Anthony Liguori
            offset += pdu_unpack(str->data, pdu, offset, str->size);
566 405a549a Anthony Liguori
            str->data[str->size] = 0;
567 405a549a Anthony Liguori
            break;
568 405a549a Anthony Liguori
        }
569 405a549a Anthony Liguori
        case 'Q': {
570 405a549a Anthony Liguori
            V9fsQID *qidp = va_arg(ap, V9fsQID *);
571 405a549a Anthony Liguori
            offset += pdu_unmarshal(pdu, offset, "bdq",
572 405a549a Anthony Liguori
                        &qidp->type, &qidp->version, &qidp->path);
573 405a549a Anthony Liguori
            break;
574 405a549a Anthony Liguori
        }
575 405a549a Anthony Liguori
        case 'S': {
576 405a549a Anthony Liguori
            V9fsStat *statp = va_arg(ap, V9fsStat *);
577 405a549a Anthony Liguori
            offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
578 405a549a Anthony Liguori
                        &statp->size, &statp->type, &statp->dev,
579 405a549a Anthony Liguori
                        &statp->qid, &statp->mode, &statp->atime,
580 405a549a Anthony Liguori
                        &statp->mtime, &statp->length,
581 405a549a Anthony Liguori
                        &statp->name, &statp->uid, &statp->gid,
582 405a549a Anthony Liguori
                        &statp->muid, &statp->extension,
583 405a549a Anthony Liguori
                        &statp->n_uid, &statp->n_gid,
584 405a549a Anthony Liguori
                        &statp->n_muid);
585 405a549a Anthony Liguori
            break;
586 405a549a Anthony Liguori
        }
587 405a549a Anthony Liguori
        default:
588 405a549a Anthony Liguori
            break;
589 405a549a Anthony Liguori
        }
590 405a549a Anthony Liguori
    }
591 405a549a Anthony Liguori
592 405a549a Anthony Liguori
    va_end(ap);
593 405a549a Anthony Liguori
594 405a549a Anthony Liguori
    return offset - old_offset;
595 405a549a Anthony Liguori
}
596 405a549a Anthony Liguori
597 405a549a Anthony Liguori
static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
598 405a549a Anthony Liguori
{
599 405a549a Anthony Liguori
    size_t old_offset = offset;
600 405a549a Anthony Liguori
    va_list ap;
601 405a549a Anthony Liguori
    int i;
602 405a549a Anthony Liguori
603 405a549a Anthony Liguori
    va_start(ap, fmt);
604 405a549a Anthony Liguori
    for (i = 0; fmt[i]; i++) {
605 405a549a Anthony Liguori
        switch (fmt[i]) {
606 405a549a Anthony Liguori
        case 'b': {
607 405a549a Anthony Liguori
            uint8_t val = va_arg(ap, int);
608 405a549a Anthony Liguori
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
609 405a549a Anthony Liguori
            break;
610 405a549a Anthony Liguori
        }
611 405a549a Anthony Liguori
        case 'w': {
612 405a549a Anthony Liguori
            uint16_t val;
613 405a549a Anthony Liguori
            cpu_to_le16w(&val, va_arg(ap, int));
614 405a549a Anthony Liguori
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
615 405a549a Anthony Liguori
            break;
616 405a549a Anthony Liguori
        }
617 405a549a Anthony Liguori
        case 'd': {
618 405a549a Anthony Liguori
            uint32_t val;
619 405a549a Anthony Liguori
            cpu_to_le32w(&val, va_arg(ap, uint32_t));
620 405a549a Anthony Liguori
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
621 405a549a Anthony Liguori
            break;
622 405a549a Anthony Liguori
        }
623 405a549a Anthony Liguori
        case 'q': {
624 405a549a Anthony Liguori
            uint64_t val;
625 405a549a Anthony Liguori
            cpu_to_le64w(&val, va_arg(ap, uint64_t));
626 405a549a Anthony Liguori
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
627 405a549a Anthony Liguori
            break;
628 405a549a Anthony Liguori
        }
629 405a549a Anthony Liguori
        case 'v': {
630 405a549a Anthony Liguori
            struct iovec *iov = va_arg(ap, struct iovec *);
631 405a549a Anthony Liguori
            int *iovcnt = va_arg(ap, int *);
632 405a549a Anthony Liguori
            *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
633 405a549a Anthony Liguori
            break;
634 405a549a Anthony Liguori
        }
635 405a549a Anthony Liguori
        case 's': {
636 405a549a Anthony Liguori
            V9fsString *str = va_arg(ap, V9fsString *);
637 405a549a Anthony Liguori
            offset += pdu_marshal(pdu, offset, "w", str->size);
638 405a549a Anthony Liguori
            offset += pdu_pack(pdu, offset, str->data, str->size);
639 405a549a Anthony Liguori
            break;
640 405a549a Anthony Liguori
        }
641 405a549a Anthony Liguori
        case 'Q': {
642 405a549a Anthony Liguori
            V9fsQID *qidp = va_arg(ap, V9fsQID *);
643 405a549a Anthony Liguori
            offset += pdu_marshal(pdu, offset, "bdq",
644 405a549a Anthony Liguori
                        qidp->type, qidp->version, qidp->path);
645 405a549a Anthony Liguori
            break;
646 405a549a Anthony Liguori
        }
647 405a549a Anthony Liguori
        case 'S': {
648 405a549a Anthony Liguori
            V9fsStat *statp = va_arg(ap, V9fsStat *);
649 405a549a Anthony Liguori
            offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
650 405a549a Anthony Liguori
                        statp->size, statp->type, statp->dev,
651 405a549a Anthony Liguori
                        &statp->qid, statp->mode, statp->atime,
652 405a549a Anthony Liguori
                        statp->mtime, statp->length, &statp->name,
653 405a549a Anthony Liguori
                        &statp->uid, &statp->gid, &statp->muid,
654 405a549a Anthony Liguori
                        &statp->extension, statp->n_uid,
655 405a549a Anthony Liguori
                        statp->n_gid, statp->n_muid);
656 405a549a Anthony Liguori
            break;
657 405a549a Anthony Liguori
        }
658 405a549a Anthony Liguori
        default:
659 405a549a Anthony Liguori
            break;
660 405a549a Anthony Liguori
        }
661 405a549a Anthony Liguori
    }
662 405a549a Anthony Liguori
    va_end(ap);
663 405a549a Anthony Liguori
664 405a549a Anthony Liguori
    return offset - old_offset;
665 405a549a Anthony Liguori
}
666 405a549a Anthony Liguori
667 405a549a Anthony Liguori
static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
668 405a549a Anthony Liguori
{
669 405a549a Anthony Liguori
    int8_t id = pdu->id + 1; /* Response */
670 405a549a Anthony Liguori
671 405a549a Anthony Liguori
    if (len < 0) {
672 405a549a Anthony Liguori
        V9fsString str;
673 405a549a Anthony Liguori
        int err = -len;
674 405a549a Anthony Liguori
675 405a549a Anthony Liguori
        str.data = strerror(err);
676 405a549a Anthony Liguori
        str.size = strlen(str.data);
677 405a549a Anthony Liguori
678 405a549a Anthony Liguori
        len = 7;
679 405a549a Anthony Liguori
        len += pdu_marshal(pdu, len, "s", &str);
680 405a549a Anthony Liguori
        if (dotu) {
681 405a549a Anthony Liguori
            len += pdu_marshal(pdu, len, "d", err);
682 405a549a Anthony Liguori
        }
683 405a549a Anthony Liguori
684 405a549a Anthony Liguori
        id = P9_RERROR;
685 405a549a Anthony Liguori
    }
686 405a549a Anthony Liguori
687 405a549a Anthony Liguori
    /* fill out the header */
688 405a549a Anthony Liguori
    pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
689 405a549a Anthony Liguori
690 405a549a Anthony Liguori
    /* keep these in sync */
691 405a549a Anthony Liguori
    pdu->size = len;
692 405a549a Anthony Liguori
    pdu->id = id;
693 405a549a Anthony Liguori
694 405a549a Anthony Liguori
    /* push onto queue and notify */
695 405a549a Anthony Liguori
    virtqueue_push(s->vq, &pdu->elem, len);
696 405a549a Anthony Liguori
697 405a549a Anthony Liguori
    /* FIXME: we should batch these completions */
698 405a549a Anthony Liguori
    virtio_notify(&s->vdev, s->vq);
699 405a549a Anthony Liguori
700 405a549a Anthony Liguori
    free_pdu(s, pdu);
701 405a549a Anthony Liguori
}
702 405a549a Anthony Liguori
703 bb9e3216 Anthony Liguori
static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
704 bb9e3216 Anthony Liguori
{
705 bb9e3216 Anthony Liguori
    mode_t ret;
706 bb9e3216 Anthony Liguori
707 bb9e3216 Anthony Liguori
    ret = mode & 0777;
708 bb9e3216 Anthony Liguori
    if (mode & P9_STAT_MODE_DIR) {
709 bb9e3216 Anthony Liguori
        ret |= S_IFDIR;
710 bb9e3216 Anthony Liguori
    }
711 bb9e3216 Anthony Liguori
712 bb9e3216 Anthony Liguori
    if (dotu) {
713 bb9e3216 Anthony Liguori
        if (mode & P9_STAT_MODE_SYMLINK) {
714 bb9e3216 Anthony Liguori
            ret |= S_IFLNK;
715 bb9e3216 Anthony Liguori
        }
716 bb9e3216 Anthony Liguori
        if (mode & P9_STAT_MODE_SOCKET) {
717 bb9e3216 Anthony Liguori
            ret |= S_IFSOCK;
718 bb9e3216 Anthony Liguori
        }
719 bb9e3216 Anthony Liguori
        if (mode & P9_STAT_MODE_NAMED_PIPE) {
720 bb9e3216 Anthony Liguori
            ret |= S_IFIFO;
721 bb9e3216 Anthony Liguori
        }
722 bb9e3216 Anthony Liguori
        if (mode & P9_STAT_MODE_DEVICE) {
723 bb9e3216 Anthony Liguori
            if (extension && extension->data[0] == 'c') {
724 bb9e3216 Anthony Liguori
                ret |= S_IFCHR;
725 bb9e3216 Anthony Liguori
            } else {
726 bb9e3216 Anthony Liguori
                ret |= S_IFBLK;
727 bb9e3216 Anthony Liguori
            }
728 bb9e3216 Anthony Liguori
        }
729 bb9e3216 Anthony Liguori
    }
730 bb9e3216 Anthony Liguori
731 bb9e3216 Anthony Liguori
    if (!(ret&~0777)) {
732 bb9e3216 Anthony Liguori
        ret |= S_IFREG;
733 bb9e3216 Anthony Liguori
    }
734 bb9e3216 Anthony Liguori
735 bb9e3216 Anthony Liguori
    if (mode & P9_STAT_MODE_SETUID) {
736 bb9e3216 Anthony Liguori
        ret |= S_ISUID;
737 bb9e3216 Anthony Liguori
    }
738 bb9e3216 Anthony Liguori
    if (mode & P9_STAT_MODE_SETGID) {
739 bb9e3216 Anthony Liguori
        ret |= S_ISGID;
740 bb9e3216 Anthony Liguori
    }
741 bb9e3216 Anthony Liguori
    if (mode & P9_STAT_MODE_SETVTX) {
742 bb9e3216 Anthony Liguori
        ret |= S_ISVTX;
743 bb9e3216 Anthony Liguori
    }
744 bb9e3216 Anthony Liguori
745 bb9e3216 Anthony Liguori
    return ret;
746 bb9e3216 Anthony Liguori
}
747 bb9e3216 Anthony Liguori
748 bb9e3216 Anthony Liguori
static int donttouch_stat(V9fsStat *stat)
749 bb9e3216 Anthony Liguori
{
750 bb9e3216 Anthony Liguori
    if (stat->type == -1 &&
751 bb9e3216 Anthony Liguori
        stat->dev == -1 &&
752 bb9e3216 Anthony Liguori
        stat->qid.type == -1 &&
753 bb9e3216 Anthony Liguori
        stat->qid.version == -1 &&
754 bb9e3216 Anthony Liguori
        stat->qid.path == -1 &&
755 bb9e3216 Anthony Liguori
        stat->mode == -1 &&
756 bb9e3216 Anthony Liguori
        stat->atime == -1 &&
757 bb9e3216 Anthony Liguori
        stat->mtime == -1 &&
758 bb9e3216 Anthony Liguori
        stat->length == -1 &&
759 bb9e3216 Anthony Liguori
        !stat->name.size &&
760 bb9e3216 Anthony Liguori
        !stat->uid.size &&
761 bb9e3216 Anthony Liguori
        !stat->gid.size &&
762 bb9e3216 Anthony Liguori
        !stat->muid.size &&
763 bb9e3216 Anthony Liguori
        stat->n_uid == -1 &&
764 bb9e3216 Anthony Liguori
        stat->n_gid == -1 &&
765 bb9e3216 Anthony Liguori
        stat->n_muid == -1) {
766 bb9e3216 Anthony Liguori
        return 1;
767 bb9e3216 Anthony Liguori
    }
768 bb9e3216 Anthony Liguori
769 bb9e3216 Anthony Liguori
    return 0;
770 bb9e3216 Anthony Liguori
}
771 bb9e3216 Anthony Liguori
772 bb9e3216 Anthony Liguori
static void v9fs_stat_free(V9fsStat *stat)
773 bb9e3216 Anthony Liguori
{
774 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->name);
775 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->uid);
776 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->gid);
777 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->muid);
778 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->extension);
779 bb9e3216 Anthony Liguori
}
780 bb9e3216 Anthony Liguori
781 bb9e3216 Anthony Liguori
static uint32_t stat_to_v9mode(const struct stat *stbuf)
782 bb9e3216 Anthony Liguori
{
783 bb9e3216 Anthony Liguori
    uint32_t mode;
784 bb9e3216 Anthony Liguori
785 bb9e3216 Anthony Liguori
    mode = stbuf->st_mode & 0777;
786 bb9e3216 Anthony Liguori
    if (S_ISDIR(stbuf->st_mode)) {
787 bb9e3216 Anthony Liguori
        mode |= P9_STAT_MODE_DIR;
788 bb9e3216 Anthony Liguori
    }
789 bb9e3216 Anthony Liguori
790 bb9e3216 Anthony Liguori
    if (dotu) {
791 bb9e3216 Anthony Liguori
        if (S_ISLNK(stbuf->st_mode)) {
792 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_SYMLINK;
793 bb9e3216 Anthony Liguori
        }
794 bb9e3216 Anthony Liguori
795 bb9e3216 Anthony Liguori
        if (S_ISSOCK(stbuf->st_mode)) {
796 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_SOCKET;
797 bb9e3216 Anthony Liguori
        }
798 bb9e3216 Anthony Liguori
799 bb9e3216 Anthony Liguori
        if (S_ISFIFO(stbuf->st_mode)) {
800 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_NAMED_PIPE;
801 bb9e3216 Anthony Liguori
        }
802 bb9e3216 Anthony Liguori
803 bb9e3216 Anthony Liguori
        if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
804 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_DEVICE;
805 bb9e3216 Anthony Liguori
        }
806 bb9e3216 Anthony Liguori
807 bb9e3216 Anthony Liguori
        if (stbuf->st_mode & S_ISUID) {
808 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_SETUID;
809 bb9e3216 Anthony Liguori
        }
810 bb9e3216 Anthony Liguori
811 bb9e3216 Anthony Liguori
        if (stbuf->st_mode & S_ISGID) {
812 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_SETGID;
813 bb9e3216 Anthony Liguori
        }
814 bb9e3216 Anthony Liguori
815 bb9e3216 Anthony Liguori
        if (stbuf->st_mode & S_ISVTX) {
816 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_SETVTX;
817 bb9e3216 Anthony Liguori
        }
818 bb9e3216 Anthony Liguori
    }
819 bb9e3216 Anthony Liguori
820 bb9e3216 Anthony Liguori
    return mode;
821 bb9e3216 Anthony Liguori
}
822 bb9e3216 Anthony Liguori
823 bb9e3216 Anthony Liguori
static int stat_to_v9stat(V9fsState *s, V9fsString *name,
824 bb9e3216 Anthony Liguori
                            const struct stat *stbuf,
825 bb9e3216 Anthony Liguori
                            V9fsStat *v9stat)
826 bb9e3216 Anthony Liguori
{
827 bb9e3216 Anthony Liguori
    int err;
828 bb9e3216 Anthony Liguori
    const char *str;
829 bb9e3216 Anthony Liguori
830 bb9e3216 Anthony Liguori
    memset(v9stat, 0, sizeof(*v9stat));
831 bb9e3216 Anthony Liguori
832 bb9e3216 Anthony Liguori
    stat_to_qid(stbuf, &v9stat->qid);
833 bb9e3216 Anthony Liguori
    v9stat->mode = stat_to_v9mode(stbuf);
834 bb9e3216 Anthony Liguori
    v9stat->atime = stbuf->st_atime;
835 bb9e3216 Anthony Liguori
    v9stat->mtime = stbuf->st_mtime;
836 bb9e3216 Anthony Liguori
    v9stat->length = stbuf->st_size;
837 bb9e3216 Anthony Liguori
838 bb9e3216 Anthony Liguori
    v9fs_string_null(&v9stat->uid);
839 bb9e3216 Anthony Liguori
    v9fs_string_null(&v9stat->gid);
840 bb9e3216 Anthony Liguori
    v9fs_string_null(&v9stat->muid);
841 bb9e3216 Anthony Liguori
842 bb9e3216 Anthony Liguori
    if (dotu) {
843 bb9e3216 Anthony Liguori
        v9stat->n_uid = stbuf->st_uid;
844 bb9e3216 Anthony Liguori
        v9stat->n_gid = stbuf->st_gid;
845 bb9e3216 Anthony Liguori
        v9stat->n_muid = 0;
846 bb9e3216 Anthony Liguori
847 bb9e3216 Anthony Liguori
        v9fs_string_null(&v9stat->extension);
848 bb9e3216 Anthony Liguori
849 bb9e3216 Anthony Liguori
        if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
850 bb9e3216 Anthony Liguori
            err = v9fs_do_readlink(s, name, &v9stat->extension);
851 bb9e3216 Anthony Liguori
            if (err == -1) {
852 bb9e3216 Anthony Liguori
                err = -errno;
853 bb9e3216 Anthony Liguori
                return err;
854 bb9e3216 Anthony Liguori
            }
855 bb9e3216 Anthony Liguori
            v9stat->extension.data[err] = 0;
856 bb9e3216 Anthony Liguori
            v9stat->extension.size = err;
857 bb9e3216 Anthony Liguori
        } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
858 bb9e3216 Anthony Liguori
            v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
859 bb9e3216 Anthony Liguori
                    S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
860 bb9e3216 Anthony Liguori
                    major(stbuf->st_rdev), minor(stbuf->st_rdev));
861 bb9e3216 Anthony Liguori
        } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
862 bb9e3216 Anthony Liguori
            v9fs_string_sprintf(&v9stat->extension, "%s %u",
863 bb9e3216 Anthony Liguori
                    "HARDLINKCOUNT", stbuf->st_nlink);
864 bb9e3216 Anthony Liguori
        }
865 bb9e3216 Anthony Liguori
    }
866 bb9e3216 Anthony Liguori
867 bb9e3216 Anthony Liguori
    str = strrchr(name->data, '/');
868 bb9e3216 Anthony Liguori
    if (str) {
869 bb9e3216 Anthony Liguori
        str += 1;
870 bb9e3216 Anthony Liguori
    } else {
871 bb9e3216 Anthony Liguori
        str = name->data;
872 bb9e3216 Anthony Liguori
    }
873 bb9e3216 Anthony Liguori
874 bb9e3216 Anthony Liguori
    v9fs_string_sprintf(&v9stat->name, "%s", str);
875 bb9e3216 Anthony Liguori
876 bb9e3216 Anthony Liguori
    v9stat->size = 61 +
877 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->name) +
878 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->uid) +
879 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->gid) +
880 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->muid) +
881 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->extension);
882 bb9e3216 Anthony Liguori
    return 0;
883 bb9e3216 Anthony Liguori
}
884 bb9e3216 Anthony Liguori
885 1f5a89bf Anthony Liguori
static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
886 1f5a89bf Anthony Liguori
{
887 1f5a89bf Anthony Liguori
    while (len && *iovcnt) {
888 1f5a89bf Anthony Liguori
        if (len < sg->iov_len) {
889 1f5a89bf Anthony Liguori
            sg->iov_len -= len;
890 1f5a89bf Anthony Liguori
            sg->iov_base += len;
891 1f5a89bf Anthony Liguori
            len = 0;
892 1f5a89bf Anthony Liguori
        } else {
893 1f5a89bf Anthony Liguori
            len -= sg->iov_len;
894 1f5a89bf Anthony Liguori
            sg++;
895 1f5a89bf Anthony Liguori
            *iovcnt -= 1;
896 1f5a89bf Anthony Liguori
        }
897 1f5a89bf Anthony Liguori
    }
898 1f5a89bf Anthony Liguori
899 1f5a89bf Anthony Liguori
    return sg;
900 1f5a89bf Anthony Liguori
}
901 1f5a89bf Anthony Liguori
902 1f5a89bf Anthony Liguori
static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
903 1f5a89bf Anthony Liguori
{
904 1f5a89bf Anthony Liguori
    int i;
905 1f5a89bf Anthony Liguori
    int total = 0;
906 1f5a89bf Anthony Liguori
907 1f5a89bf Anthony Liguori
    for (i = 0; i < *cnt; i++) {
908 1f5a89bf Anthony Liguori
        if ((total + sg[i].iov_len) > cap) {
909 1f5a89bf Anthony Liguori
            sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
910 1f5a89bf Anthony Liguori
            i++;
911 1f5a89bf Anthony Liguori
            break;
912 1f5a89bf Anthony Liguori
        }
913 1f5a89bf Anthony Liguori
        total += sg[i].iov_len;
914 1f5a89bf Anthony Liguori
    }
915 1f5a89bf Anthony Liguori
916 1f5a89bf Anthony Liguori
    *cnt = i;
917 1f5a89bf Anthony Liguori
918 1f5a89bf Anthony Liguori
    return sg;
919 1f5a89bf Anthony Liguori
}
920 1f5a89bf Anthony Liguori
921 1f5a89bf Anthony Liguori
static void print_sg(struct iovec *sg, int cnt)
922 1f5a89bf Anthony Liguori
{
923 1f5a89bf Anthony Liguori
    int i;
924 1f5a89bf Anthony Liguori
925 1f5a89bf Anthony Liguori
    printf("sg[%d]: {", cnt);
926 1f5a89bf Anthony Liguori
    for (i = 0; i < cnt; i++) {
927 1f5a89bf Anthony Liguori
        if (i) {
928 1f5a89bf Anthony Liguori
            printf(", ");
929 1f5a89bf Anthony Liguori
        }
930 1f5a89bf Anthony Liguori
        printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
931 1f5a89bf Anthony Liguori
    }
932 1f5a89bf Anthony Liguori
    printf("}\n");
933 1f5a89bf Anthony Liguori
}
934 1f5a89bf Anthony Liguori
935 8cf89e00 Anthony Liguori
static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
936 8cf89e00 Anthony Liguori
{
937 8cf89e00 Anthony Liguori
    V9fsString str;
938 8cf89e00 Anthony Liguori
    v9fs_string_init(&str);
939 8cf89e00 Anthony Liguori
    v9fs_string_copy(&str, dst);
940 8cf89e00 Anthony Liguori
    v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
941 8cf89e00 Anthony Liguori
    v9fs_string_free(&str);
942 8cf89e00 Anthony Liguori
}
943 8cf89e00 Anthony Liguori
944 9f107513 Anthony Liguori
static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
945 9f107513 Anthony Liguori
{
946 92c1ad03 Anthony Liguori
    int32_t msize;
947 92c1ad03 Anthony Liguori
    V9fsString version;
948 92c1ad03 Anthony Liguori
    size_t offset = 7;
949 92c1ad03 Anthony Liguori
950 92c1ad03 Anthony Liguori
    pdu_unmarshal(pdu, offset, "ds", &msize, &version);
951 92c1ad03 Anthony Liguori
952 92c1ad03 Anthony Liguori
    if (strcmp(version.data, "9P2000.u")) {
953 92c1ad03 Anthony Liguori
        v9fs_string_sprintf(&version, "unknown");
954 9f107513 Anthony Liguori
    }
955 92c1ad03 Anthony Liguori
956 92c1ad03 Anthony Liguori
    offset += pdu_marshal(pdu, offset, "ds", msize, &version);
957 92c1ad03 Anthony Liguori
    complete_pdu(s, pdu, offset);
958 92c1ad03 Anthony Liguori
959 92c1ad03 Anthony Liguori
    v9fs_string_free(&version);
960 9f107513 Anthony Liguori
}
961 9f107513 Anthony Liguori
962 9f107513 Anthony Liguori
static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
963 9f107513 Anthony Liguori
{
964 955efc47 Anthony Liguori
    int32_t fid, afid, n_uname;
965 955efc47 Anthony Liguori
    V9fsString uname, aname;
966 955efc47 Anthony Liguori
    V9fsFidState *fidp;
967 955efc47 Anthony Liguori
    V9fsQID qid;
968 955efc47 Anthony Liguori
    size_t offset = 7;
969 955efc47 Anthony Liguori
    ssize_t err;
970 955efc47 Anthony Liguori
971 955efc47 Anthony Liguori
    pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
972 955efc47 Anthony Liguori
973 955efc47 Anthony Liguori
    fidp = alloc_fid(s, fid);
974 955efc47 Anthony Liguori
    if (fidp == NULL) {
975 955efc47 Anthony Liguori
        err = -EINVAL;
976 955efc47 Anthony Liguori
        goto out;
977 9f107513 Anthony Liguori
    }
978 955efc47 Anthony Liguori
979 955efc47 Anthony Liguori
    fidp->uid = n_uname;
980 955efc47 Anthony Liguori
981 955efc47 Anthony Liguori
    v9fs_string_sprintf(&fidp->path, "%s", "/");
982 955efc47 Anthony Liguori
    err = fid_to_qid(s, fidp, &qid);
983 955efc47 Anthony Liguori
    if (err) {
984 955efc47 Anthony Liguori
        err = -EINVAL;
985 955efc47 Anthony Liguori
        free_fid(s, fid);
986 955efc47 Anthony Liguori
        goto out;
987 955efc47 Anthony Liguori
    }
988 955efc47 Anthony Liguori
989 955efc47 Anthony Liguori
    offset += pdu_marshal(pdu, offset, "Q", &qid);
990 955efc47 Anthony Liguori
991 955efc47 Anthony Liguori
    err = offset;
992 955efc47 Anthony Liguori
out:
993 955efc47 Anthony Liguori
    complete_pdu(s, pdu, err);
994 955efc47 Anthony Liguori
    v9fs_string_free(&uname);
995 955efc47 Anthony Liguori
    v9fs_string_free(&aname);
996 9f107513 Anthony Liguori
}
997 9f107513 Anthony Liguori
998 4da7d3fa Anthony Liguori
typedef struct V9fsStatState {
999 4da7d3fa Anthony Liguori
    V9fsPDU *pdu;
1000 4da7d3fa Anthony Liguori
    size_t offset;
1001 4da7d3fa Anthony Liguori
    V9fsStat v9stat;
1002 4da7d3fa Anthony Liguori
    V9fsFidState *fidp;
1003 4da7d3fa Anthony Liguori
    struct stat stbuf;
1004 4da7d3fa Anthony Liguori
} V9fsStatState;
1005 4da7d3fa Anthony Liguori
1006 4da7d3fa Anthony Liguori
static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1007 4da7d3fa Anthony Liguori
{
1008 4da7d3fa Anthony Liguori
    if (err == -1) {
1009 4da7d3fa Anthony Liguori
        err = -errno;
1010 4da7d3fa Anthony Liguori
        goto out;
1011 4da7d3fa Anthony Liguori
    }
1012 4da7d3fa Anthony Liguori
1013 4da7d3fa Anthony Liguori
    err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1014 4da7d3fa Anthony Liguori
    if (err) {
1015 4da7d3fa Anthony Liguori
        goto out;
1016 4da7d3fa Anthony Liguori
    }
1017 4da7d3fa Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1018 4da7d3fa Anthony Liguori
    err = vs->offset;
1019 4da7d3fa Anthony Liguori
1020 4da7d3fa Anthony Liguori
out:
1021 4da7d3fa Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1022 4da7d3fa Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
1023 4da7d3fa Anthony Liguori
    qemu_free(vs);
1024 4da7d3fa Anthony Liguori
}
1025 4da7d3fa Anthony Liguori
1026 9f107513 Anthony Liguori
static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
1027 9f107513 Anthony Liguori
{
1028 4da7d3fa Anthony Liguori
    int32_t fid;
1029 4da7d3fa Anthony Liguori
    V9fsStatState *vs;
1030 4da7d3fa Anthony Liguori
    ssize_t err = 0;
1031 4da7d3fa Anthony Liguori
1032 4da7d3fa Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
1033 4da7d3fa Anthony Liguori
    vs->pdu = pdu;
1034 4da7d3fa Anthony Liguori
    vs->offset = 7;
1035 4da7d3fa Anthony Liguori
1036 4da7d3fa Anthony Liguori
    memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1037 4da7d3fa Anthony Liguori
1038 4da7d3fa Anthony Liguori
    pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1039 4da7d3fa Anthony Liguori
1040 4da7d3fa Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
1041 4da7d3fa Anthony Liguori
    if (vs->fidp == NULL) {
1042 4da7d3fa Anthony Liguori
        err = -ENOENT;
1043 4da7d3fa Anthony Liguori
        goto out;
1044 9f107513 Anthony Liguori
    }
1045 4da7d3fa Anthony Liguori
1046 4da7d3fa Anthony Liguori
    err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1047 4da7d3fa Anthony Liguori
    v9fs_stat_post_lstat(s, vs, err);
1048 4da7d3fa Anthony Liguori
    return;
1049 4da7d3fa Anthony Liguori
1050 4da7d3fa Anthony Liguori
out:
1051 4da7d3fa Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1052 4da7d3fa Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
1053 4da7d3fa Anthony Liguori
    qemu_free(vs);
1054 9f107513 Anthony Liguori
}
1055 9f107513 Anthony Liguori
1056 ff5e54c9 Anthony Liguori
typedef struct V9fsWalkState {
1057 ff5e54c9 Anthony Liguori
    V9fsPDU *pdu;
1058 ff5e54c9 Anthony Liguori
    size_t offset;
1059 ff5e54c9 Anthony Liguori
    int16_t nwnames;
1060 ff5e54c9 Anthony Liguori
    int name_idx;
1061 ff5e54c9 Anthony Liguori
    V9fsQID *qids;
1062 ff5e54c9 Anthony Liguori
    V9fsFidState *fidp;
1063 ff5e54c9 Anthony Liguori
    V9fsFidState *newfidp;
1064 ff5e54c9 Anthony Liguori
    V9fsString path;
1065 ff5e54c9 Anthony Liguori
    V9fsString *wnames;
1066 ff5e54c9 Anthony Liguori
    struct stat stbuf;
1067 ff5e54c9 Anthony Liguori
} V9fsWalkState;
1068 ff5e54c9 Anthony Liguori
1069 ff5e54c9 Anthony Liguori
static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1070 ff5e54c9 Anthony Liguori
{
1071 ff5e54c9 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1072 ff5e54c9 Anthony Liguori
1073 ff5e54c9 Anthony Liguori
    if (vs->nwnames) {
1074 ff5e54c9 Anthony Liguori
        for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1075 ff5e54c9 Anthony Liguori
            v9fs_string_free(&vs->wnames[vs->name_idx]);
1076 ff5e54c9 Anthony Liguori
        }
1077 ff5e54c9 Anthony Liguori
1078 ff5e54c9 Anthony Liguori
        qemu_free(vs->wnames);
1079 ff5e54c9 Anthony Liguori
        qemu_free(vs->qids);
1080 ff5e54c9 Anthony Liguori
    }
1081 ff5e54c9 Anthony Liguori
}
1082 ff5e54c9 Anthony Liguori
1083 ff5e54c9 Anthony Liguori
static void v9fs_walk_marshal(V9fsWalkState *vs)
1084 ff5e54c9 Anthony Liguori
{
1085 ff5e54c9 Anthony Liguori
    int i;
1086 ff5e54c9 Anthony Liguori
    vs->offset = 7;
1087 ff5e54c9 Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1088 ff5e54c9 Anthony Liguori
1089 ff5e54c9 Anthony Liguori
    for (i = 0; i < vs->nwnames; i++) {
1090 ff5e54c9 Anthony Liguori
        vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1091 ff5e54c9 Anthony Liguori
    }
1092 ff5e54c9 Anthony Liguori
}
1093 ff5e54c9 Anthony Liguori
1094 ff5e54c9 Anthony Liguori
static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1095 ff5e54c9 Anthony Liguori
                                                                int err)
1096 ff5e54c9 Anthony Liguori
{
1097 ff5e54c9 Anthony Liguori
    if (err == -1) {
1098 ff5e54c9 Anthony Liguori
        free_fid(s, vs->newfidp->fid);
1099 ff5e54c9 Anthony Liguori
        v9fs_string_free(&vs->path);
1100 ff5e54c9 Anthony Liguori
        err = -ENOENT;
1101 ff5e54c9 Anthony Liguori
        goto out;
1102 ff5e54c9 Anthony Liguori
    }
1103 ff5e54c9 Anthony Liguori
1104 ff5e54c9 Anthony Liguori
    stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1105 ff5e54c9 Anthony Liguori
1106 ff5e54c9 Anthony Liguori
    vs->name_idx++;
1107 ff5e54c9 Anthony Liguori
    if (vs->name_idx < vs->nwnames) {
1108 ff5e54c9 Anthony Liguori
        v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1109 ff5e54c9 Anthony Liguori
                                            vs->wnames[vs->name_idx].data);
1110 ff5e54c9 Anthony Liguori
        v9fs_string_copy(&vs->newfidp->path, &vs->path);
1111 ff5e54c9 Anthony Liguori
1112 ff5e54c9 Anthony Liguori
        err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1113 ff5e54c9 Anthony Liguori
        v9fs_walk_post_newfid_lstat(s, vs, err);
1114 ff5e54c9 Anthony Liguori
        return;
1115 ff5e54c9 Anthony Liguori
    }
1116 ff5e54c9 Anthony Liguori
1117 ff5e54c9 Anthony Liguori
    v9fs_string_free(&vs->path);
1118 ff5e54c9 Anthony Liguori
    v9fs_walk_marshal(vs);
1119 ff5e54c9 Anthony Liguori
    err = vs->offset;
1120 ff5e54c9 Anthony Liguori
out:
1121 ff5e54c9 Anthony Liguori
    v9fs_walk_complete(s, vs, err);
1122 ff5e54c9 Anthony Liguori
}
1123 ff5e54c9 Anthony Liguori
1124 ff5e54c9 Anthony Liguori
static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1125 ff5e54c9 Anthony Liguori
        int err)
1126 ff5e54c9 Anthony Liguori
{
1127 ff5e54c9 Anthony Liguori
    if (err == -1) {
1128 ff5e54c9 Anthony Liguori
        v9fs_string_free(&vs->path);
1129 ff5e54c9 Anthony Liguori
        err = -ENOENT;
1130 ff5e54c9 Anthony Liguori
        goto out;
1131 ff5e54c9 Anthony Liguori
    }
1132 ff5e54c9 Anthony Liguori
1133 ff5e54c9 Anthony Liguori
    stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1134 ff5e54c9 Anthony Liguori
    vs->name_idx++;
1135 ff5e54c9 Anthony Liguori
    if (vs->name_idx < vs->nwnames) {
1136 ff5e54c9 Anthony Liguori
1137 ff5e54c9 Anthony Liguori
        v9fs_string_sprintf(&vs->path, "%s/%s",
1138 ff5e54c9 Anthony Liguori
                vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1139 ff5e54c9 Anthony Liguori
        v9fs_string_copy(&vs->fidp->path, &vs->path);
1140 ff5e54c9 Anthony Liguori
1141 ff5e54c9 Anthony Liguori
        err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1142 ff5e54c9 Anthony Liguori
        v9fs_walk_post_oldfid_lstat(s, vs, err);
1143 ff5e54c9 Anthony Liguori
        return;
1144 ff5e54c9 Anthony Liguori
    }
1145 ff5e54c9 Anthony Liguori
1146 ff5e54c9 Anthony Liguori
    v9fs_string_free(&vs->path);
1147 ff5e54c9 Anthony Liguori
    v9fs_walk_marshal(vs);
1148 ff5e54c9 Anthony Liguori
    err = vs->offset;
1149 ff5e54c9 Anthony Liguori
out:
1150 ff5e54c9 Anthony Liguori
    v9fs_walk_complete(s, vs, err);
1151 ff5e54c9 Anthony Liguori
}
1152 ff5e54c9 Anthony Liguori
1153 9f107513 Anthony Liguori
static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1154 9f107513 Anthony Liguori
{
1155 ff5e54c9 Anthony Liguori
    int32_t fid, newfid;
1156 ff5e54c9 Anthony Liguori
    V9fsWalkState *vs;
1157 ff5e54c9 Anthony Liguori
    int err = 0;
1158 ff5e54c9 Anthony Liguori
    int i;
1159 ff5e54c9 Anthony Liguori
1160 ff5e54c9 Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
1161 ff5e54c9 Anthony Liguori
    vs->pdu = pdu;
1162 ff5e54c9 Anthony Liguori
    vs->wnames = NULL;
1163 ff5e54c9 Anthony Liguori
    vs->qids = NULL;
1164 ff5e54c9 Anthony Liguori
    vs->offset = 7;
1165 ff5e54c9 Anthony Liguori
1166 ff5e54c9 Anthony Liguori
    vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1167 ff5e54c9 Anthony Liguori
                                            &newfid, &vs->nwnames);
1168 ff5e54c9 Anthony Liguori
1169 ff5e54c9 Anthony Liguori
    if (vs->nwnames) {
1170 ff5e54c9 Anthony Liguori
        vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1171 ff5e54c9 Anthony Liguori
1172 ff5e54c9 Anthony Liguori
        vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1173 ff5e54c9 Anthony Liguori
1174 ff5e54c9 Anthony Liguori
        for (i = 0; i < vs->nwnames; i++) {
1175 ff5e54c9 Anthony Liguori
            vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1176 ff5e54c9 Anthony Liguori
                                            &vs->wnames[i]);
1177 ff5e54c9 Anthony Liguori
        }
1178 ff5e54c9 Anthony Liguori
    }
1179 ff5e54c9 Anthony Liguori
1180 ff5e54c9 Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
1181 ff5e54c9 Anthony Liguori
    if (vs->fidp == NULL) {
1182 ff5e54c9 Anthony Liguori
        err = -ENOENT;
1183 ff5e54c9 Anthony Liguori
        goto out;
1184 ff5e54c9 Anthony Liguori
    }
1185 ff5e54c9 Anthony Liguori
1186 ff5e54c9 Anthony Liguori
    /* FIXME: is this really valid? */
1187 ff5e54c9 Anthony Liguori
    if (fid == newfid) {
1188 ff5e54c9 Anthony Liguori
1189 ff5e54c9 Anthony Liguori
        BUG_ON(vs->fidp->fd != -1);
1190 ff5e54c9 Anthony Liguori
        BUG_ON(vs->fidp->dir);
1191 ff5e54c9 Anthony Liguori
        v9fs_string_init(&vs->path);
1192 ff5e54c9 Anthony Liguori
        vs->name_idx = 0;
1193 ff5e54c9 Anthony Liguori
1194 ff5e54c9 Anthony Liguori
        if (vs->name_idx < vs->nwnames) {
1195 ff5e54c9 Anthony Liguori
            v9fs_string_sprintf(&vs->path, "%s/%s",
1196 ff5e54c9 Anthony Liguori
                vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1197 ff5e54c9 Anthony Liguori
            v9fs_string_copy(&vs->fidp->path, &vs->path);
1198 ff5e54c9 Anthony Liguori
1199 ff5e54c9 Anthony Liguori
            err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1200 ff5e54c9 Anthony Liguori
            v9fs_walk_post_oldfid_lstat(s, vs, err);
1201 ff5e54c9 Anthony Liguori
            return;
1202 ff5e54c9 Anthony Liguori
        }
1203 ff5e54c9 Anthony Liguori
    } else {
1204 ff5e54c9 Anthony Liguori
        vs->newfidp = alloc_fid(s, newfid);
1205 ff5e54c9 Anthony Liguori
        if (vs->newfidp == NULL) {
1206 ff5e54c9 Anthony Liguori
            err = -EINVAL;
1207 ff5e54c9 Anthony Liguori
            goto out;
1208 ff5e54c9 Anthony Liguori
        }
1209 ff5e54c9 Anthony Liguori
1210 ff5e54c9 Anthony Liguori
        vs->newfidp->uid = vs->fidp->uid;
1211 ff5e54c9 Anthony Liguori
        v9fs_string_init(&vs->path);
1212 ff5e54c9 Anthony Liguori
        vs->name_idx = 0;
1213 ff5e54c9 Anthony Liguori
        v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1214 ff5e54c9 Anthony Liguori
1215 ff5e54c9 Anthony Liguori
        if (vs->name_idx < vs->nwnames) {
1216 ff5e54c9 Anthony Liguori
            v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1217 ff5e54c9 Anthony Liguori
                                vs->wnames[vs->name_idx].data);
1218 ff5e54c9 Anthony Liguori
            v9fs_string_copy(&vs->newfidp->path, &vs->path);
1219 ff5e54c9 Anthony Liguori
1220 ff5e54c9 Anthony Liguori
            err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1221 ff5e54c9 Anthony Liguori
            v9fs_walk_post_newfid_lstat(s, vs, err);
1222 ff5e54c9 Anthony Liguori
            return;
1223 ff5e54c9 Anthony Liguori
        }
1224 9f107513 Anthony Liguori
    }
1225 ff5e54c9 Anthony Liguori
1226 ff5e54c9 Anthony Liguori
    v9fs_walk_marshal(vs);
1227 ff5e54c9 Anthony Liguori
    err = vs->offset;
1228 ff5e54c9 Anthony Liguori
out:
1229 ff5e54c9 Anthony Liguori
    v9fs_walk_complete(s, vs, err);
1230 9f107513 Anthony Liguori
}
1231 9f107513 Anthony Liguori
1232 a6568fe2 Anthony Liguori
typedef struct V9fsOpenState {
1233 a6568fe2 Anthony Liguori
    V9fsPDU *pdu;
1234 a6568fe2 Anthony Liguori
    size_t offset;
1235 a6568fe2 Anthony Liguori
    int8_t mode;
1236 a6568fe2 Anthony Liguori
    V9fsFidState *fidp;
1237 a6568fe2 Anthony Liguori
    V9fsQID qid;
1238 a6568fe2 Anthony Liguori
    struct stat stbuf;
1239 a6568fe2 Anthony Liguori
1240 a6568fe2 Anthony Liguori
} V9fsOpenState;
1241 a6568fe2 Anthony Liguori
1242 a6568fe2 Anthony Liguori
enum {
1243 a6568fe2 Anthony Liguori
    Oread   = 0x00,
1244 a6568fe2 Anthony Liguori
    Owrite  = 0x01,
1245 a6568fe2 Anthony Liguori
    Ordwr   = 0x02,
1246 a6568fe2 Anthony Liguori
    Oexec   = 0x03,
1247 a6568fe2 Anthony Liguori
    Oexcl   = 0x04,
1248 a6568fe2 Anthony Liguori
    Otrunc  = 0x10,
1249 a6568fe2 Anthony Liguori
    Orexec  = 0x20,
1250 a6568fe2 Anthony Liguori
    Orclose = 0x40,
1251 a6568fe2 Anthony Liguori
    Oappend = 0x80,
1252 a6568fe2 Anthony Liguori
};
1253 a6568fe2 Anthony Liguori
1254 a6568fe2 Anthony Liguori
static int omode_to_uflags(int8_t mode)
1255 9f107513 Anthony Liguori
{
1256 a6568fe2 Anthony Liguori
    int ret = 0;
1257 a6568fe2 Anthony Liguori
1258 a6568fe2 Anthony Liguori
    switch (mode & 3) {
1259 a6568fe2 Anthony Liguori
    case Oread:
1260 a6568fe2 Anthony Liguori
        ret = O_RDONLY;
1261 a6568fe2 Anthony Liguori
        break;
1262 a6568fe2 Anthony Liguori
    case Ordwr:
1263 a6568fe2 Anthony Liguori
        ret = O_RDWR;
1264 a6568fe2 Anthony Liguori
        break;
1265 a6568fe2 Anthony Liguori
    case Owrite:
1266 a6568fe2 Anthony Liguori
        ret = O_WRONLY;
1267 a6568fe2 Anthony Liguori
        break;
1268 a6568fe2 Anthony Liguori
    case Oexec:
1269 a6568fe2 Anthony Liguori
        ret = O_RDONLY;
1270 a6568fe2 Anthony Liguori
        break;
1271 a6568fe2 Anthony Liguori
    }
1272 a6568fe2 Anthony Liguori
1273 a6568fe2 Anthony Liguori
    if (mode & Otrunc) {
1274 a6568fe2 Anthony Liguori
        ret |= O_TRUNC;
1275 a6568fe2 Anthony Liguori
    }
1276 a6568fe2 Anthony Liguori
1277 a6568fe2 Anthony Liguori
    if (mode & Oappend) {
1278 a6568fe2 Anthony Liguori
        ret |= O_APPEND;
1279 9f107513 Anthony Liguori
    }
1280 a6568fe2 Anthony Liguori
1281 a6568fe2 Anthony Liguori
    if (mode & Oexcl) {
1282 a6568fe2 Anthony Liguori
        ret |= O_EXCL;
1283 a6568fe2 Anthony Liguori
    }
1284 a6568fe2 Anthony Liguori
1285 a6568fe2 Anthony Liguori
    return ret;
1286 a6568fe2 Anthony Liguori
}
1287 a6568fe2 Anthony Liguori
1288 a6568fe2 Anthony Liguori
static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1289 a6568fe2 Anthony Liguori
{
1290 a6568fe2 Anthony Liguori
    if (vs->fidp->dir == NULL) {
1291 a6568fe2 Anthony Liguori
        err = -errno;
1292 a6568fe2 Anthony Liguori
        goto out;
1293 a6568fe2 Anthony Liguori
    }
1294 a6568fe2 Anthony Liguori
1295 a6568fe2 Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1296 a6568fe2 Anthony Liguori
    err = vs->offset;
1297 a6568fe2 Anthony Liguori
out:
1298 a6568fe2 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1299 a6568fe2 Anthony Liguori
    qemu_free(vs);
1300 a6568fe2 Anthony Liguori
1301 a6568fe2 Anthony Liguori
}
1302 a6568fe2 Anthony Liguori
1303 a6568fe2 Anthony Liguori
static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1304 a6568fe2 Anthony Liguori
{
1305 a6568fe2 Anthony Liguori
    if (vs->fidp->fd == -1) {
1306 a6568fe2 Anthony Liguori
        err = -errno;
1307 a6568fe2 Anthony Liguori
        goto out;
1308 a6568fe2 Anthony Liguori
    }
1309 a6568fe2 Anthony Liguori
1310 a6568fe2 Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1311 a6568fe2 Anthony Liguori
    err = vs->offset;
1312 a6568fe2 Anthony Liguori
out:
1313 a6568fe2 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1314 a6568fe2 Anthony Liguori
    qemu_free(vs);
1315 a6568fe2 Anthony Liguori
}
1316 a6568fe2 Anthony Liguori
1317 a6568fe2 Anthony Liguori
static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1318 a6568fe2 Anthony Liguori
{
1319 a6568fe2 Anthony Liguori
    if (err) {
1320 a6568fe2 Anthony Liguori
        err = -errno;
1321 a6568fe2 Anthony Liguori
        goto out;
1322 a6568fe2 Anthony Liguori
    }
1323 a6568fe2 Anthony Liguori
1324 a6568fe2 Anthony Liguori
    stat_to_qid(&vs->stbuf, &vs->qid);
1325 a6568fe2 Anthony Liguori
1326 a6568fe2 Anthony Liguori
    if (S_ISDIR(vs->stbuf.st_mode)) {
1327 a6568fe2 Anthony Liguori
        vs->fidp->dir = v9fs_do_opendir(s, &vs->fidp->path);
1328 a6568fe2 Anthony Liguori
        v9fs_open_post_opendir(s, vs, err);
1329 a6568fe2 Anthony Liguori
    } else {
1330 a6568fe2 Anthony Liguori
        vs->fidp->fd = v9fs_do_open(s, &vs->fidp->path,
1331 a6568fe2 Anthony Liguori
                                    omode_to_uflags(vs->mode));
1332 a6568fe2 Anthony Liguori
        v9fs_open_post_open(s, vs, err);
1333 a6568fe2 Anthony Liguori
    }
1334 a6568fe2 Anthony Liguori
    return;
1335 a6568fe2 Anthony Liguori
out:
1336 a6568fe2 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1337 a6568fe2 Anthony Liguori
    qemu_free(vs);
1338 9f107513 Anthony Liguori
}
1339 9f107513 Anthony Liguori
1340 9f107513 Anthony Liguori
static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
1341 a6568fe2 Anthony Liguori
{
1342 a6568fe2 Anthony Liguori
    int32_t fid;
1343 a6568fe2 Anthony Liguori
    V9fsOpenState *vs;
1344 a6568fe2 Anthony Liguori
    ssize_t err = 0;
1345 a6568fe2 Anthony Liguori
1346 a6568fe2 Anthony Liguori
1347 a6568fe2 Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
1348 a6568fe2 Anthony Liguori
    vs->pdu = pdu;
1349 a6568fe2 Anthony Liguori
    vs->offset = 7;
1350 a6568fe2 Anthony Liguori
1351 a6568fe2 Anthony Liguori
    pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1352 a6568fe2 Anthony Liguori
1353 a6568fe2 Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
1354 a6568fe2 Anthony Liguori
    if (vs->fidp == NULL) {
1355 a6568fe2 Anthony Liguori
        err = -ENOENT;
1356 a6568fe2 Anthony Liguori
        goto out;
1357 a6568fe2 Anthony Liguori
    }
1358 a6568fe2 Anthony Liguori
1359 a6568fe2 Anthony Liguori
    BUG_ON(vs->fidp->fd != -1);
1360 a6568fe2 Anthony Liguori
    BUG_ON(vs->fidp->dir);
1361 a6568fe2 Anthony Liguori
1362 a6568fe2 Anthony Liguori
    err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1363 a6568fe2 Anthony Liguori
1364 a6568fe2 Anthony Liguori
    v9fs_open_post_lstat(s, vs, err);
1365 a6568fe2 Anthony Liguori
    return;
1366 a6568fe2 Anthony Liguori
out:
1367 a6568fe2 Anthony Liguori
    complete_pdu(s, pdu, err);
1368 a6568fe2 Anthony Liguori
    qemu_free(vs);
1369 a6568fe2 Anthony Liguori
}
1370 a6568fe2 Anthony Liguori
1371 a6568fe2 Anthony Liguori
static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1372 a6568fe2 Anthony Liguori
{
1373 bbd5697b Anthony Liguori
    int32_t fid;
1374 bbd5697b Anthony Liguori
    size_t offset = 7;
1375 bbd5697b Anthony Liguori
    int err;
1376 bbd5697b Anthony Liguori
1377 bbd5697b Anthony Liguori
    pdu_unmarshal(pdu, offset, "d", &fid);
1378 bbd5697b Anthony Liguori
1379 bbd5697b Anthony Liguori
    err = free_fid(s, fid);
1380 bbd5697b Anthony Liguori
    if (err < 0) {
1381 bbd5697b Anthony Liguori
        goto out;
1382 a6568fe2 Anthony Liguori
    }
1383 bbd5697b Anthony Liguori
1384 bbd5697b Anthony Liguori
    offset = 7;
1385 bbd5697b Anthony Liguori
    err = offset;
1386 bbd5697b Anthony Liguori
out:
1387 bbd5697b Anthony Liguori
    complete_pdu(s, pdu, err);
1388 9f107513 Anthony Liguori
}
1389 9f107513 Anthony Liguori
1390 a9231555 Anthony Liguori
typedef struct V9fsReadState {
1391 a9231555 Anthony Liguori
    V9fsPDU *pdu;
1392 a9231555 Anthony Liguori
    size_t offset;
1393 a9231555 Anthony Liguori
    int32_t count;
1394 a9231555 Anthony Liguori
    int32_t total;
1395 a9231555 Anthony Liguori
    int64_t off;
1396 a9231555 Anthony Liguori
    V9fsFidState *fidp;
1397 a9231555 Anthony Liguori
    struct iovec iov[128]; /* FIXME: bad, bad, bad */
1398 a9231555 Anthony Liguori
    struct iovec *sg;
1399 a9231555 Anthony Liguori
    off_t dir_pos;
1400 a9231555 Anthony Liguori
    struct dirent *dent;
1401 a9231555 Anthony Liguori
    struct stat stbuf;
1402 a9231555 Anthony Liguori
    V9fsString name;
1403 a9231555 Anthony Liguori
    V9fsStat v9stat;
1404 a9231555 Anthony Liguori
    int32_t len;
1405 a9231555 Anthony Liguori
    int32_t cnt;
1406 a9231555 Anthony Liguori
    int32_t max_count;
1407 a9231555 Anthony Liguori
} V9fsReadState;
1408 a9231555 Anthony Liguori
1409 a9231555 Anthony Liguori
static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1410 a9231555 Anthony Liguori
1411 a9231555 Anthony Liguori
static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1412 a9231555 Anthony Liguori
{
1413 a9231555 Anthony Liguori
    if (err) {
1414 a9231555 Anthony Liguori
        goto out;
1415 a9231555 Anthony Liguori
    }
1416 a9231555 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
1417 a9231555 Anthony Liguori
    v9fs_string_free(&vs->name);
1418 a9231555 Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1419 a9231555 Anthony Liguori
    vs->offset += vs->count;
1420 a9231555 Anthony Liguori
    err = vs->offset;
1421 a9231555 Anthony Liguori
out:
1422 a9231555 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1423 a9231555 Anthony Liguori
    qemu_free(vs);
1424 a9231555 Anthony Liguori
    return;
1425 a9231555 Anthony Liguori
}
1426 a9231555 Anthony Liguori
1427 a9231555 Anthony Liguori
static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1428 a9231555 Anthony Liguori
                                    ssize_t err)
1429 a9231555 Anthony Liguori
{
1430 a9231555 Anthony Liguori
    if (err) {
1431 a9231555 Anthony Liguori
        err = -errno;
1432 a9231555 Anthony Liguori
        goto out;
1433 a9231555 Anthony Liguori
    }
1434 a9231555 Anthony Liguori
    err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1435 a9231555 Anthony Liguori
    if (err) {
1436 a9231555 Anthony Liguori
        goto out;
1437 a9231555 Anthony Liguori
    }
1438 a9231555 Anthony Liguori
1439 a9231555 Anthony Liguori
    vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1440 a9231555 Anthony Liguori
                            &vs->v9stat);
1441 a9231555 Anthony Liguori
    if ((vs->len != (vs->v9stat.size + 2)) ||
1442 a9231555 Anthony Liguori
            ((vs->count + vs->len) > vs->max_count)) {
1443 a9231555 Anthony Liguori
        v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1444 a9231555 Anthony Liguori
        v9fs_read_post_seekdir(s, vs, err);
1445 a9231555 Anthony Liguori
        return;
1446 a9231555 Anthony Liguori
    }
1447 a9231555 Anthony Liguori
    vs->count += vs->len;
1448 a9231555 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
1449 a9231555 Anthony Liguori
    v9fs_string_free(&vs->name);
1450 a9231555 Anthony Liguori
    vs->dir_pos = vs->dent->d_off;
1451 a9231555 Anthony Liguori
    vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1452 a9231555 Anthony Liguori
    v9fs_read_post_readdir(s, vs, err);
1453 a9231555 Anthony Liguori
    return;
1454 a9231555 Anthony Liguori
out:
1455 a9231555 Anthony Liguori
    v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1456 a9231555 Anthony Liguori
    v9fs_read_post_seekdir(s, vs, err);
1457 a9231555 Anthony Liguori
    return;
1458 a9231555 Anthony Liguori
1459 a9231555 Anthony Liguori
}
1460 a9231555 Anthony Liguori
1461 a9231555 Anthony Liguori
static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1462 a9231555 Anthony Liguori
{
1463 a9231555 Anthony Liguori
    if (vs->dent) {
1464 a9231555 Anthony Liguori
        memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1465 a9231555 Anthony Liguori
        v9fs_string_init(&vs->name);
1466 a9231555 Anthony Liguori
        v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1467 a9231555 Anthony Liguori
                            vs->dent->d_name);
1468 a9231555 Anthony Liguori
        err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1469 a9231555 Anthony Liguori
        v9fs_read_post_dir_lstat(s, vs, err);
1470 a9231555 Anthony Liguori
        return;
1471 a9231555 Anthony Liguori
    }
1472 a9231555 Anthony Liguori
1473 a9231555 Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1474 a9231555 Anthony Liguori
    vs->offset += vs->count;
1475 a9231555 Anthony Liguori
    err = vs->offset;
1476 a9231555 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1477 a9231555 Anthony Liguori
    qemu_free(vs);
1478 a9231555 Anthony Liguori
    return;
1479 a9231555 Anthony Liguori
}
1480 a9231555 Anthony Liguori
1481 a9231555 Anthony Liguori
static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1482 a9231555 Anthony Liguori
{
1483 a9231555 Anthony Liguori
    vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1484 a9231555 Anthony Liguori
    v9fs_read_post_readdir(s, vs, err);
1485 a9231555 Anthony Liguori
    return;
1486 a9231555 Anthony Liguori
}
1487 a9231555 Anthony Liguori
1488 a9231555 Anthony Liguori
static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
1489 a9231555 Anthony Liguori
                                       ssize_t err)
1490 a9231555 Anthony Liguori
{
1491 a9231555 Anthony Liguori
    vs->dir_pos = v9fs_do_telldir(s, vs->fidp->dir);
1492 a9231555 Anthony Liguori
    v9fs_read_post_telldir(s, vs, err);
1493 a9231555 Anthony Liguori
    return;
1494 a9231555 Anthony Liguori
}
1495 a9231555 Anthony Liguori
1496 a9231555 Anthony Liguori
static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *vs, ssize_t err)
1497 a9231555 Anthony Liguori
{
1498 a9231555 Anthony Liguori
    if (err  < 0) {
1499 a9231555 Anthony Liguori
        /* IO error return the error */
1500 a9231555 Anthony Liguori
        err = -errno;
1501 a9231555 Anthony Liguori
        goto out;
1502 a9231555 Anthony Liguori
    }
1503 a9231555 Anthony Liguori
    vs->total += vs->len;
1504 a9231555 Anthony Liguori
    vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1505 a9231555 Anthony Liguori
    if (vs->total < vs->count && vs->len > 0) {
1506 a9231555 Anthony Liguori
        do {
1507 a9231555 Anthony Liguori
            if (0) {
1508 a9231555 Anthony Liguori
                print_sg(vs->sg, vs->cnt);
1509 a9231555 Anthony Liguori
            }
1510 a9231555 Anthony Liguori
            vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1511 a9231555 Anthony Liguori
        } while (vs->len == -1 && errno == EINTR);
1512 a9231555 Anthony Liguori
        if (vs->len == -1) {
1513 a9231555 Anthony Liguori
            err  = -errno;
1514 a9231555 Anthony Liguori
        }
1515 a9231555 Anthony Liguori
        v9fs_read_post_readv(s, vs, err);
1516 a9231555 Anthony Liguori
        return;
1517 a9231555 Anthony Liguori
    }
1518 a9231555 Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1519 a9231555 Anthony Liguori
    vs->offset += vs->count;
1520 a9231555 Anthony Liguori
    err = vs->offset;
1521 a9231555 Anthony Liguori
1522 a9231555 Anthony Liguori
out:
1523 a9231555 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1524 a9231555 Anthony Liguori
    qemu_free(vs);
1525 a9231555 Anthony Liguori
}
1526 a9231555 Anthony Liguori
1527 a9231555 Anthony Liguori
static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err)
1528 a9231555 Anthony Liguori
{
1529 a9231555 Anthony Liguori
    if (err == -1) {
1530 a9231555 Anthony Liguori
        err = -errno;
1531 a9231555 Anthony Liguori
        goto out;
1532 a9231555 Anthony Liguori
    }
1533 a9231555 Anthony Liguori
    vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1534 a9231555 Anthony Liguori
1535 a9231555 Anthony Liguori
    if (vs->total < vs->count) {
1536 a9231555 Anthony Liguori
        do {
1537 a9231555 Anthony Liguori
            if (0) {
1538 a9231555 Anthony Liguori
                print_sg(vs->sg, vs->cnt);
1539 a9231555 Anthony Liguori
            }
1540 a9231555 Anthony Liguori
            vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1541 a9231555 Anthony Liguori
        } while (vs->len == -1 && errno == EINTR);
1542 a9231555 Anthony Liguori
        if (vs->len == -1) {
1543 a9231555 Anthony Liguori
            err  = -errno;
1544 a9231555 Anthony Liguori
        }
1545 a9231555 Anthony Liguori
        v9fs_read_post_readv(s, vs, err);
1546 a9231555 Anthony Liguori
        return;
1547 a9231555 Anthony Liguori
    }
1548 a9231555 Anthony Liguori
out:
1549 a9231555 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1550 a9231555 Anthony Liguori
    qemu_free(vs);
1551 a9231555 Anthony Liguori
}
1552 a9231555 Anthony Liguori
1553 9f107513 Anthony Liguori
static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
1554 9f107513 Anthony Liguori
{
1555 a9231555 Anthony Liguori
    int32_t fid;
1556 a9231555 Anthony Liguori
    V9fsReadState *vs;
1557 a9231555 Anthony Liguori
    ssize_t err = 0;
1558 a9231555 Anthony Liguori
1559 a9231555 Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
1560 a9231555 Anthony Liguori
    vs->pdu = pdu;
1561 a9231555 Anthony Liguori
    vs->offset = 7;
1562 a9231555 Anthony Liguori
    vs->total = 0;
1563 a9231555 Anthony Liguori
    vs->len = 0;
1564 a9231555 Anthony Liguori
    vs->count = 0;
1565 a9231555 Anthony Liguori
1566 a9231555 Anthony Liguori
    pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count);
1567 a9231555 Anthony Liguori
1568 a9231555 Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
1569 a9231555 Anthony Liguori
    if (vs->fidp == NULL) {
1570 a9231555 Anthony Liguori
        err = -EINVAL;
1571 a9231555 Anthony Liguori
        goto out;
1572 a9231555 Anthony Liguori
    }
1573 a9231555 Anthony Liguori
1574 a9231555 Anthony Liguori
    if (vs->fidp->dir) {
1575 a9231555 Anthony Liguori
        vs->max_count = vs->count;
1576 a9231555 Anthony Liguori
        vs->count = 0;
1577 a9231555 Anthony Liguori
        if (vs->off == 0) {
1578 a9231555 Anthony Liguori
            v9fs_do_rewinddir(s, vs->fidp->dir);
1579 a9231555 Anthony Liguori
        }
1580 a9231555 Anthony Liguori
        v9fs_read_post_rewinddir(s, vs, err);
1581 a9231555 Anthony Liguori
        return;
1582 a9231555 Anthony Liguori
    } else if (vs->fidp->fd != -1) {
1583 a9231555 Anthony Liguori
        vs->sg = vs->iov;
1584 a9231555 Anthony Liguori
        pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
1585 a9231555 Anthony Liguori
        err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1586 a9231555 Anthony Liguori
        v9fs_read_post_lseek(s, vs, err);
1587 a9231555 Anthony Liguori
        return;
1588 a9231555 Anthony Liguori
    } else {
1589 a9231555 Anthony Liguori
        err = -EINVAL;
1590 9f107513 Anthony Liguori
    }
1591 a9231555 Anthony Liguori
out:
1592 a9231555 Anthony Liguori
    complete_pdu(s, pdu, err);
1593 a9231555 Anthony Liguori
    qemu_free(vs);
1594 9f107513 Anthony Liguori
}
1595 9f107513 Anthony Liguori
1596 8449360c Anthony Liguori
typedef struct V9fsWriteState {
1597 8449360c Anthony Liguori
    V9fsPDU *pdu;
1598 8449360c Anthony Liguori
    size_t offset;
1599 8449360c Anthony Liguori
    int32_t len;
1600 8449360c Anthony Liguori
    int32_t count;
1601 8449360c Anthony Liguori
    int32_t total;
1602 8449360c Anthony Liguori
    int64_t off;
1603 8449360c Anthony Liguori
    V9fsFidState *fidp;
1604 8449360c Anthony Liguori
    struct iovec iov[128]; /* FIXME: bad, bad, bad */
1605 8449360c Anthony Liguori
    struct iovec *sg;
1606 8449360c Anthony Liguori
    int cnt;
1607 8449360c Anthony Liguori
} V9fsWriteState;
1608 8449360c Anthony Liguori
1609 8449360c Anthony Liguori
static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs,
1610 8449360c Anthony Liguori
                                   ssize_t err)
1611 8449360c Anthony Liguori
{
1612 8449360c Anthony Liguori
    if (err  < 0) {
1613 8449360c Anthony Liguori
        /* IO error return the error */
1614 8449360c Anthony Liguori
        err = -errno;
1615 8449360c Anthony Liguori
        goto out;
1616 8449360c Anthony Liguori
    }
1617 8449360c Anthony Liguori
    vs->total += vs->len;
1618 8449360c Anthony Liguori
    vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1619 8449360c Anthony Liguori
    if (vs->total < vs->count && vs->len > 0) {
1620 8449360c Anthony Liguori
        do {
1621 8449360c Anthony Liguori
            if (0) {
1622 8449360c Anthony Liguori
                print_sg(vs->sg, vs->cnt);
1623 8449360c Anthony Liguori
            }
1624 8449360c Anthony Liguori
            vs->len =  v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
1625 8449360c Anthony Liguori
        } while (vs->len == -1 && errno == EINTR);
1626 8449360c Anthony Liguori
        if (vs->len == -1) {
1627 8449360c Anthony Liguori
            err  = -errno;
1628 8449360c Anthony Liguori
        }
1629 8449360c Anthony Liguori
        v9fs_write_post_writev(s, vs, err);
1630 8449360c Anthony Liguori
        return;
1631 8449360c Anthony Liguori
    }
1632 8449360c Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1633 8449360c Anthony Liguori
1634 8449360c Anthony Liguori
    err = vs->offset;
1635 8449360c Anthony Liguori
out:
1636 8449360c Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1637 8449360c Anthony Liguori
    qemu_free(vs);
1638 8449360c Anthony Liguori
}
1639 8449360c Anthony Liguori
1640 8449360c Anthony Liguori
static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err)
1641 8449360c Anthony Liguori
{
1642 8449360c Anthony Liguori
    if (err == -1) {
1643 8449360c Anthony Liguori
        err = -errno;
1644 8449360c Anthony Liguori
        goto out;
1645 8449360c Anthony Liguori
    }
1646 8449360c Anthony Liguori
    vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1647 8449360c Anthony Liguori
1648 8449360c Anthony Liguori
    if (vs->total < vs->count) {
1649 8449360c Anthony Liguori
        do {
1650 8449360c Anthony Liguori
            if (0) {
1651 8449360c Anthony Liguori
                print_sg(vs->sg, vs->cnt);
1652 8449360c Anthony Liguori
            }
1653 8449360c Anthony Liguori
            vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
1654 8449360c Anthony Liguori
        } while (vs->len == -1 && errno == EINTR);
1655 8449360c Anthony Liguori
        if (vs->len == -1) {
1656 8449360c Anthony Liguori
            err  = -errno;
1657 8449360c Anthony Liguori
        }
1658 8449360c Anthony Liguori
        v9fs_write_post_writev(s, vs, err);
1659 8449360c Anthony Liguori
        return;
1660 8449360c Anthony Liguori
    }
1661 8449360c Anthony Liguori
1662 8449360c Anthony Liguori
out:
1663 8449360c Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1664 8449360c Anthony Liguori
    qemu_free(vs);
1665 8449360c Anthony Liguori
}
1666 8449360c Anthony Liguori
1667 9f107513 Anthony Liguori
static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
1668 9f107513 Anthony Liguori
{
1669 8449360c Anthony Liguori
    int32_t fid;
1670 8449360c Anthony Liguori
    V9fsWriteState *vs;
1671 8449360c Anthony Liguori
    ssize_t err;
1672 8449360c Anthony Liguori
1673 8449360c Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
1674 8449360c Anthony Liguori
1675 8449360c Anthony Liguori
    vs->pdu = pdu;
1676 8449360c Anthony Liguori
    vs->offset = 7;
1677 8449360c Anthony Liguori
    vs->sg = vs->iov;
1678 8449360c Anthony Liguori
    vs->total = 0;
1679 8449360c Anthony Liguori
    vs->len = 0;
1680 8449360c Anthony Liguori
1681 8449360c Anthony Liguori
    pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
1682 8449360c Anthony Liguori
                    vs->sg, &vs->cnt);
1683 8449360c Anthony Liguori
1684 8449360c Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
1685 8449360c Anthony Liguori
    if (vs->fidp == NULL) {
1686 8449360c Anthony Liguori
        err = -EINVAL;
1687 8449360c Anthony Liguori
        goto out;
1688 9f107513 Anthony Liguori
    }
1689 8449360c Anthony Liguori
1690 8449360c Anthony Liguori
    if (vs->fidp->fd == -1) {
1691 8449360c Anthony Liguori
        err = -EINVAL;
1692 8449360c Anthony Liguori
        goto out;
1693 8449360c Anthony Liguori
    }
1694 8449360c Anthony Liguori
1695 8449360c Anthony Liguori
    err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1696 8449360c Anthony Liguori
1697 8449360c Anthony Liguori
    v9fs_write_post_lseek(s, vs, err);
1698 8449360c Anthony Liguori
    return;
1699 8449360c Anthony Liguori
1700 8449360c Anthony Liguori
out:
1701 8449360c Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1702 8449360c Anthony Liguori
    qemu_free(vs);
1703 9f107513 Anthony Liguori
}
1704 9f107513 Anthony Liguori
1705 c494dd6f Anthony Liguori
typedef struct V9fsCreateState {
1706 c494dd6f Anthony Liguori
    V9fsPDU *pdu;
1707 c494dd6f Anthony Liguori
    size_t offset;
1708 c494dd6f Anthony Liguori
    V9fsFidState *fidp;
1709 c494dd6f Anthony Liguori
    V9fsQID qid;
1710 c494dd6f Anthony Liguori
    int32_t perm;
1711 c494dd6f Anthony Liguori
    int8_t mode;
1712 c494dd6f Anthony Liguori
    struct stat stbuf;
1713 c494dd6f Anthony Liguori
    V9fsString name;
1714 c494dd6f Anthony Liguori
    V9fsString extension;
1715 c494dd6f Anthony Liguori
    V9fsString fullname;
1716 c494dd6f Anthony Liguori
} V9fsCreateState;
1717 c494dd6f Anthony Liguori
1718 c494dd6f Anthony Liguori
static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
1719 c494dd6f Anthony Liguori
{
1720 c494dd6f Anthony Liguori
    if (err == 0) {
1721 c494dd6f Anthony Liguori
        v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1722 c494dd6f Anthony Liguori
        stat_to_qid(&vs->stbuf, &vs->qid);
1723 c494dd6f Anthony Liguori
1724 c494dd6f Anthony Liguori
        vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1725 c494dd6f Anthony Liguori
1726 c494dd6f Anthony Liguori
        err = vs->offset;
1727 c494dd6f Anthony Liguori
    }
1728 c494dd6f Anthony Liguori
1729 c494dd6f Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1730 c494dd6f Anthony Liguori
    v9fs_string_free(&vs->name);
1731 c494dd6f Anthony Liguori
    v9fs_string_free(&vs->extension);
1732 c494dd6f Anthony Liguori
    v9fs_string_free(&vs->fullname);
1733 c494dd6f Anthony Liguori
    qemu_free(vs);
1734 c494dd6f Anthony Liguori
}
1735 c494dd6f Anthony Liguori
1736 c494dd6f Anthony Liguori
static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
1737 c494dd6f Anthony Liguori
{
1738 c494dd6f Anthony Liguori
    if (err) {
1739 c494dd6f Anthony Liguori
        err = -errno;
1740 c494dd6f Anthony Liguori
    }
1741 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
1742 c494dd6f Anthony Liguori
}
1743 c494dd6f Anthony Liguori
1744 c494dd6f Anthony Liguori
static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
1745 c494dd6f Anthony Liguori
                                                                    int err)
1746 c494dd6f Anthony Liguori
{
1747 c494dd6f Anthony Liguori
    if (!vs->fidp->dir) {
1748 c494dd6f Anthony Liguori
        err = -errno;
1749 c494dd6f Anthony Liguori
    }
1750 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
1751 c494dd6f Anthony Liguori
}
1752 c494dd6f Anthony Liguori
1753 c494dd6f Anthony Liguori
static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
1754 c494dd6f Anthony Liguori
                                                                    int err)
1755 c494dd6f Anthony Liguori
{
1756 c494dd6f Anthony Liguori
    if (err) {
1757 c494dd6f Anthony Liguori
        err = -errno;
1758 c494dd6f Anthony Liguori
        goto out;
1759 c494dd6f Anthony Liguori
    }
1760 c494dd6f Anthony Liguori
1761 c494dd6f Anthony Liguori
    vs->fidp->dir = v9fs_do_opendir(s, &vs->fullname);
1762 c494dd6f Anthony Liguori
    v9fs_create_post_opendir(s, vs, err);
1763 c494dd6f Anthony Liguori
    return;
1764 c494dd6f Anthony Liguori
1765 c494dd6f Anthony Liguori
out:
1766 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
1767 c494dd6f Anthony Liguori
}
1768 c494dd6f Anthony Liguori
1769 c494dd6f Anthony Liguori
static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
1770 c494dd6f Anthony Liguori
{
1771 c494dd6f Anthony Liguori
    if (err) {
1772 c494dd6f Anthony Liguori
        err = -errno;
1773 c494dd6f Anthony Liguori
        goto out;
1774 c494dd6f Anthony Liguori
    }
1775 c494dd6f Anthony Liguori
1776 c494dd6f Anthony Liguori
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1777 c494dd6f Anthony Liguori
    v9fs_create_post_dir_lstat(s, vs, err);
1778 c494dd6f Anthony Liguori
    return;
1779 c494dd6f Anthony Liguori
1780 c494dd6f Anthony Liguori
out:
1781 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
1782 c494dd6f Anthony Liguori
}
1783 c494dd6f Anthony Liguori
1784 c494dd6f Anthony Liguori
static void v9fs_create_post_mksock(V9fsState *s, V9fsCreateState *vs,
1785 c494dd6f Anthony Liguori
                                                                int err)
1786 c494dd6f Anthony Liguori
{
1787 c494dd6f Anthony Liguori
    if (err) {
1788 c494dd6f Anthony Liguori
        err = -errno;
1789 c494dd6f Anthony Liguori
        goto out;
1790 c494dd6f Anthony Liguori
    }
1791 c494dd6f Anthony Liguori
1792 c494dd6f Anthony Liguori
    err = v9fs_do_chmod(s, &vs->fullname, vs->perm & 0777);
1793 c494dd6f Anthony Liguori
    v9fs_create_post_perms(s, vs, err);
1794 c494dd6f Anthony Liguori
    return;
1795 c494dd6f Anthony Liguori
1796 c494dd6f Anthony Liguori
out:
1797 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
1798 c494dd6f Anthony Liguori
}
1799 c494dd6f Anthony Liguori
1800 c494dd6f Anthony Liguori
static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
1801 c494dd6f Anthony Liguori
{
1802 c494dd6f Anthony Liguori
    if (err) {
1803 c494dd6f Anthony Liguori
        vs->fidp->fd = -1;
1804 c494dd6f Anthony Liguori
        err = -errno;
1805 c494dd6f Anthony Liguori
    }
1806 c494dd6f Anthony Liguori
1807 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
1808 c494dd6f Anthony Liguori
    return;
1809 c494dd6f Anthony Liguori
}
1810 c494dd6f Anthony Liguori
1811 c494dd6f Anthony Liguori
static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
1812 c494dd6f Anthony Liguori
{
1813 c494dd6f Anthony Liguori
    if (vs->fidp->fd == -1) {
1814 c494dd6f Anthony Liguori
        err = -errno;
1815 c494dd6f Anthony Liguori
        goto out;
1816 c494dd6f Anthony Liguori
    }
1817 c494dd6f Anthony Liguori
1818 c494dd6f Anthony Liguori
    err = v9fs_do_fstat(s, vs->fidp->fd, &vs->stbuf);
1819 c494dd6f Anthony Liguori
    v9fs_create_post_fstat(s, vs, err);
1820 c494dd6f Anthony Liguori
1821 c494dd6f Anthony Liguori
    return;
1822 c494dd6f Anthony Liguori
1823 c494dd6f Anthony Liguori
out:
1824 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
1825 c494dd6f Anthony Liguori
1826 c494dd6f Anthony Liguori
}
1827 c494dd6f Anthony Liguori
1828 c494dd6f Anthony Liguori
static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
1829 c494dd6f Anthony Liguori
{
1830 c494dd6f Anthony Liguori
1831 c494dd6f Anthony Liguori
    if (err == 0 || errno != ENOENT) {
1832 c494dd6f Anthony Liguori
        err = -errno;
1833 c494dd6f Anthony Liguori
        goto out;
1834 c494dd6f Anthony Liguori
    }
1835 c494dd6f Anthony Liguori
1836 c494dd6f Anthony Liguori
    if (vs->perm & P9_STAT_MODE_DIR) {
1837 c494dd6f Anthony Liguori
        err = v9fs_do_mkdir(s, &vs->fullname, vs->perm & 0777);
1838 c494dd6f Anthony Liguori
        v9fs_create_post_mkdir(s, vs, err);
1839 c494dd6f Anthony Liguori
    } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
1840 c494dd6f Anthony Liguori
        err = v9fs_do_symlink(s, &vs->extension, &vs->fullname);
1841 c494dd6f Anthony Liguori
        v9fs_create_post_perms(s, vs, err);
1842 c494dd6f Anthony Liguori
    } else if (vs->perm & P9_STAT_MODE_LINK) {
1843 c494dd6f Anthony Liguori
        int32_t nfid = atoi(vs->extension.data);
1844 c494dd6f Anthony Liguori
        V9fsFidState *nfidp = lookup_fid(s, nfid);
1845 c494dd6f Anthony Liguori
        if (nfidp == NULL) {
1846 c494dd6f Anthony Liguori
            err = -errno;
1847 c494dd6f Anthony Liguori
            v9fs_post_create(s, vs, err);
1848 c494dd6f Anthony Liguori
        }
1849 c494dd6f Anthony Liguori
        err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
1850 c494dd6f Anthony Liguori
        v9fs_create_post_perms(s, vs, err);
1851 c494dd6f Anthony Liguori
    } else if (vs->perm & P9_STAT_MODE_DEVICE) {
1852 c494dd6f Anthony Liguori
        char ctype;
1853 c494dd6f Anthony Liguori
        uint32_t major, minor;
1854 c494dd6f Anthony Liguori
        mode_t nmode = 0;
1855 c494dd6f Anthony Liguori
1856 c494dd6f Anthony Liguori
        if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
1857 c494dd6f Anthony Liguori
                                        &minor) != 3) {
1858 c494dd6f Anthony Liguori
            err = -errno;
1859 c494dd6f Anthony Liguori
            v9fs_post_create(s, vs, err);
1860 c494dd6f Anthony Liguori
        }
1861 c494dd6f Anthony Liguori
1862 c494dd6f Anthony Liguori
        switch (ctype) {
1863 c494dd6f Anthony Liguori
        case 'c':
1864 c494dd6f Anthony Liguori
            nmode = S_IFCHR;
1865 c494dd6f Anthony Liguori
            break;
1866 c494dd6f Anthony Liguori
        case 'b':
1867 c494dd6f Anthony Liguori
            nmode = S_IFBLK;
1868 c494dd6f Anthony Liguori
            break;
1869 c494dd6f Anthony Liguori
        default:
1870 c494dd6f Anthony Liguori
            err = -EIO;
1871 c494dd6f Anthony Liguori
            v9fs_post_create(s, vs, err);
1872 c494dd6f Anthony Liguori
        }
1873 c494dd6f Anthony Liguori
1874 c494dd6f Anthony Liguori
        nmode |= vs->perm & 0777;
1875 c494dd6f Anthony Liguori
        err = v9fs_do_mknod(s, &vs->fullname, nmode, makedev(major, minor));
1876 c494dd6f Anthony Liguori
        v9fs_create_post_perms(s, vs, err);
1877 c494dd6f Anthony Liguori
    } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
1878 c494dd6f Anthony Liguori
        err = v9fs_do_mknod(s, &vs->fullname, S_IFIFO | (vs->mode & 0777), 0);
1879 c494dd6f Anthony Liguori
        v9fs_post_create(s, vs, err);
1880 c494dd6f Anthony Liguori
    } else if (vs->perm & P9_STAT_MODE_SOCKET) {
1881 c494dd6f Anthony Liguori
        err = v9fs_do_mksock(s, &vs->fullname);
1882 c494dd6f Anthony Liguori
        v9fs_create_post_mksock(s, vs, err);
1883 c494dd6f Anthony Liguori
    } else {
1884 c494dd6f Anthony Liguori
        vs->fidp->fd = v9fs_do_open2(s, &vs->fullname,
1885 c494dd6f Anthony Liguori
                                omode_to_uflags(vs->mode) | O_CREAT,
1886 c494dd6f Anthony Liguori
                                vs->perm & 0777);
1887 c494dd6f Anthony Liguori
        v9fs_create_post_open2(s, vs, err);
1888 c494dd6f Anthony Liguori
    }
1889 c494dd6f Anthony Liguori
1890 c494dd6f Anthony Liguori
    return;
1891 c494dd6f Anthony Liguori
1892 c494dd6f Anthony Liguori
out:
1893 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
1894 c494dd6f Anthony Liguori
}
1895 c494dd6f Anthony Liguori
1896 9f107513 Anthony Liguori
static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
1897 9f107513 Anthony Liguori
{
1898 c494dd6f Anthony Liguori
    int32_t fid;
1899 c494dd6f Anthony Liguori
    V9fsCreateState *vs;
1900 c494dd6f Anthony Liguori
    int err = 0;
1901 c494dd6f Anthony Liguori
1902 c494dd6f Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
1903 c494dd6f Anthony Liguori
    vs->pdu = pdu;
1904 c494dd6f Anthony Liguori
    vs->offset = 7;
1905 c494dd6f Anthony Liguori
1906 c494dd6f Anthony Liguori
    v9fs_string_init(&vs->fullname);
1907 c494dd6f Anthony Liguori
1908 c494dd6f Anthony Liguori
    pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
1909 c494dd6f Anthony Liguori
                                &vs->perm, &vs->mode, &vs->extension);
1910 c494dd6f Anthony Liguori
1911 c494dd6f Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
1912 c494dd6f Anthony Liguori
    if (vs->fidp == NULL) {
1913 c494dd6f Anthony Liguori
        err = -EINVAL;
1914 c494dd6f Anthony Liguori
        goto out;
1915 9f107513 Anthony Liguori
    }
1916 c494dd6f Anthony Liguori
1917 c494dd6f Anthony Liguori
    v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
1918 c494dd6f Anthony Liguori
                                                        vs->name.data);
1919 c494dd6f Anthony Liguori
1920 c494dd6f Anthony Liguori
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1921 c494dd6f Anthony Liguori
    v9fs_create_post_lstat(s, vs, err);
1922 c494dd6f Anthony Liguori
    return;
1923 c494dd6f Anthony Liguori
1924 c494dd6f Anthony Liguori
out:
1925 c494dd6f Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1926 c494dd6f Anthony Liguori
    v9fs_string_free(&vs->name);
1927 c494dd6f Anthony Liguori
    v9fs_string_free(&vs->extension);
1928 c494dd6f Anthony Liguori
    qemu_free(vs);
1929 9f107513 Anthony Liguori
}
1930 9f107513 Anthony Liguori
1931 9f107513 Anthony Liguori
static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
1932 9f107513 Anthony Liguori
{
1933 9c5e9d89 Anthony Liguori
    /* A nop call with no return */
1934 9c5e9d89 Anthony Liguori
    complete_pdu(s, pdu, 7);
1935 9f107513 Anthony Liguori
}
1936 9f107513 Anthony Liguori
1937 5bae1900 Anthony Liguori
typedef struct V9fsRemoveState {
1938 5bae1900 Anthony Liguori
    V9fsPDU *pdu;
1939 5bae1900 Anthony Liguori
    size_t offset;
1940 5bae1900 Anthony Liguori
    V9fsFidState *fidp;
1941 5bae1900 Anthony Liguori
} V9fsRemoveState;
1942 5bae1900 Anthony Liguori
1943 5bae1900 Anthony Liguori
static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
1944 5bae1900 Anthony Liguori
                                                                int err)
1945 5bae1900 Anthony Liguori
{
1946 5bae1900 Anthony Liguori
    /* For TREMOVE we need to clunk the fid even on failed remove */
1947 5bae1900 Anthony Liguori
    err = free_fid(s, vs->fidp->fid);
1948 5bae1900 Anthony Liguori
    if (err < 0) {
1949 5bae1900 Anthony Liguori
        goto out;
1950 5bae1900 Anthony Liguori
    }
1951 5bae1900 Anthony Liguori
1952 5bae1900 Anthony Liguori
    err = vs->offset;
1953 5bae1900 Anthony Liguori
out:
1954 5bae1900 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1955 5bae1900 Anthony Liguori
    qemu_free(vs);
1956 5bae1900 Anthony Liguori
}
1957 5bae1900 Anthony Liguori
1958 9f107513 Anthony Liguori
static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
1959 9f107513 Anthony Liguori
{
1960 5bae1900 Anthony Liguori
    int32_t fid;
1961 5bae1900 Anthony Liguori
    V9fsRemoveState *vs;
1962 5bae1900 Anthony Liguori
    int err = 0;
1963 5bae1900 Anthony Liguori
1964 5bae1900 Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
1965 5bae1900 Anthony Liguori
    vs->pdu = pdu;
1966 5bae1900 Anthony Liguori
    vs->offset = 7;
1967 5bae1900 Anthony Liguori
1968 5bae1900 Anthony Liguori
    pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1969 5bae1900 Anthony Liguori
1970 5bae1900 Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
1971 5bae1900 Anthony Liguori
    if (vs->fidp == NULL) {
1972 5bae1900 Anthony Liguori
        err = -EINVAL;
1973 5bae1900 Anthony Liguori
        goto out;
1974 9f107513 Anthony Liguori
    }
1975 5bae1900 Anthony Liguori
1976 5bae1900 Anthony Liguori
    err = v9fs_do_remove(s, &vs->fidp->path);
1977 5bae1900 Anthony Liguori
    v9fs_remove_post_remove(s, vs, err);
1978 5bae1900 Anthony Liguori
    return;
1979 5bae1900 Anthony Liguori
1980 5bae1900 Anthony Liguori
out:
1981 5bae1900 Anthony Liguori
    complete_pdu(s, pdu, err);
1982 5bae1900 Anthony Liguori
    qemu_free(vs);
1983 9f107513 Anthony Liguori
}
1984 9f107513 Anthony Liguori
1985 8cf89e00 Anthony Liguori
typedef struct V9fsWstatState
1986 8cf89e00 Anthony Liguori
{
1987 8cf89e00 Anthony Liguori
    V9fsPDU *pdu;
1988 8cf89e00 Anthony Liguori
    size_t offset;
1989 8cf89e00 Anthony Liguori
    int16_t unused;
1990 8cf89e00 Anthony Liguori
    V9fsStat v9stat;
1991 8cf89e00 Anthony Liguori
    V9fsFidState *fidp;
1992 8cf89e00 Anthony Liguori
    struct stat stbuf;
1993 8cf89e00 Anthony Liguori
    V9fsString nname;
1994 8cf89e00 Anthony Liguori
} V9fsWstatState;
1995 8cf89e00 Anthony Liguori
1996 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
1997 8cf89e00 Anthony Liguori
{
1998 8cf89e00 Anthony Liguori
    if (err < 0) {
1999 8cf89e00 Anthony Liguori
        goto out;
2000 8cf89e00 Anthony Liguori
    }
2001 8cf89e00 Anthony Liguori
2002 8cf89e00 Anthony Liguori
    err = vs->offset;
2003 8cf89e00 Anthony Liguori
2004 8cf89e00 Anthony Liguori
out:
2005 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2006 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2007 8cf89e00 Anthony Liguori
    qemu_free(vs);
2008 8cf89e00 Anthony Liguori
}
2009 8cf89e00 Anthony Liguori
2010 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
2011 8cf89e00 Anthony Liguori
{
2012 8cf89e00 Anthony Liguori
    if (err < 0) {
2013 8cf89e00 Anthony Liguori
        goto out;
2014 8cf89e00 Anthony Liguori
    }
2015 8cf89e00 Anthony Liguori
2016 8cf89e00 Anthony Liguori
    if (vs->v9stat.name.size != 0) {
2017 8cf89e00 Anthony Liguori
        v9fs_string_free(&vs->nname);
2018 8cf89e00 Anthony Liguori
    }
2019 8cf89e00 Anthony Liguori
2020 8cf89e00 Anthony Liguori
    if (vs->v9stat.length != -1) {
2021 8cf89e00 Anthony Liguori
        if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
2022 8cf89e00 Anthony Liguori
            err = -errno;
2023 8cf89e00 Anthony Liguori
        }
2024 8cf89e00 Anthony Liguori
    }
2025 8cf89e00 Anthony Liguori
    v9fs_wstat_post_truncate(s, vs, err);
2026 8cf89e00 Anthony Liguori
    return;
2027 8cf89e00 Anthony Liguori
2028 8cf89e00 Anthony Liguori
out:
2029 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2030 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2031 8cf89e00 Anthony Liguori
    qemu_free(vs);
2032 8cf89e00 Anthony Liguori
}
2033 8cf89e00 Anthony Liguori
2034 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
2035 8cf89e00 Anthony Liguori
{
2036 8cf89e00 Anthony Liguori
    V9fsFidState *fidp;
2037 8cf89e00 Anthony Liguori
    if (err < 0) {
2038 8cf89e00 Anthony Liguori
        goto out;
2039 8cf89e00 Anthony Liguori
    }
2040 8cf89e00 Anthony Liguori
2041 8cf89e00 Anthony Liguori
    if (vs->v9stat.name.size != 0) {
2042 8cf89e00 Anthony Liguori
        char *old_name, *new_name;
2043 8cf89e00 Anthony Liguori
        char *end;
2044 8cf89e00 Anthony Liguori
2045 8cf89e00 Anthony Liguori
        old_name = vs->fidp->path.data;
2046 8cf89e00 Anthony Liguori
        end = strrchr(old_name, '/');
2047 8cf89e00 Anthony Liguori
        if (end) {
2048 8cf89e00 Anthony Liguori
            end++;
2049 8cf89e00 Anthony Liguori
        } else {
2050 8cf89e00 Anthony Liguori
            end = old_name;
2051 8cf89e00 Anthony Liguori
        }
2052 8cf89e00 Anthony Liguori
2053 8cf89e00 Anthony Liguori
        new_name = qemu_malloc(end - old_name + vs->v9stat.name.size + 1);
2054 8cf89e00 Anthony Liguori
2055 8cf89e00 Anthony Liguori
        memset(new_name, 0, end - old_name + vs->v9stat.name.size + 1);
2056 8cf89e00 Anthony Liguori
        memcpy(new_name, old_name, end - old_name);
2057 8cf89e00 Anthony Liguori
        memcpy(new_name + (end - old_name), vs->v9stat.name.data,
2058 8cf89e00 Anthony Liguori
                vs->v9stat.name.size);
2059 8cf89e00 Anthony Liguori
        vs->nname.data = new_name;
2060 8cf89e00 Anthony Liguori
        vs->nname.size = strlen(new_name);
2061 8cf89e00 Anthony Liguori
2062 8cf89e00 Anthony Liguori
        if (strcmp(new_name, vs->fidp->path.data) != 0) {
2063 8cf89e00 Anthony Liguori
            if (v9fs_do_rename(s, &vs->fidp->path, &vs->nname)) {
2064 8cf89e00 Anthony Liguori
                err = -errno;
2065 8cf89e00 Anthony Liguori
            } else {
2066 8cf89e00 Anthony Liguori
                /*
2067 8cf89e00 Anthony Liguori
                 * Fixup fid's pointing to the old name to
2068 8cf89e00 Anthony Liguori
                 * start pointing to the new name
2069 8cf89e00 Anthony Liguori
                 */
2070 8cf89e00 Anthony Liguori
                for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2071 8cf89e00 Anthony Liguori
2072 8cf89e00 Anthony Liguori
                    if (vs->fidp == fidp) {
2073 8cf89e00 Anthony Liguori
                        /*
2074 8cf89e00 Anthony Liguori
                         * we replace name of this fid towards the end
2075 8cf89e00 Anthony Liguori
                         * so that our below strcmp will work
2076 8cf89e00 Anthony Liguori
                         */
2077 8cf89e00 Anthony Liguori
                        continue;
2078 8cf89e00 Anthony Liguori
                    }
2079 8cf89e00 Anthony Liguori
                    if (!strncmp(vs->fidp->path.data, fidp->path.data,
2080 8cf89e00 Anthony Liguori
                                 strlen(vs->fidp->path.data))) {
2081 8cf89e00 Anthony Liguori
                        /* replace the name */
2082 8cf89e00 Anthony Liguori
                        v9fs_fix_path(&fidp->path, &vs->nname,
2083 8cf89e00 Anthony Liguori
                                      strlen(vs->fidp->path.data));
2084 8cf89e00 Anthony Liguori
                    }
2085 8cf89e00 Anthony Liguori
                }
2086 8cf89e00 Anthony Liguori
                v9fs_string_copy(&vs->fidp->path, &vs->nname);
2087 8cf89e00 Anthony Liguori
            }
2088 8cf89e00 Anthony Liguori
        }
2089 8cf89e00 Anthony Liguori
    }
2090 8cf89e00 Anthony Liguori
    v9fs_wstat_post_rename(s, vs, err);
2091 8cf89e00 Anthony Liguori
    return;
2092 8cf89e00 Anthony Liguori
2093 8cf89e00 Anthony Liguori
out:
2094 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2095 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2096 8cf89e00 Anthony Liguori
    qemu_free(vs);
2097 8cf89e00 Anthony Liguori
}
2098 8cf89e00 Anthony Liguori
2099 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2100 8cf89e00 Anthony Liguori
{
2101 8cf89e00 Anthony Liguori
    if (err < 0) {
2102 8cf89e00 Anthony Liguori
        goto out;
2103 8cf89e00 Anthony Liguori
    }
2104 8cf89e00 Anthony Liguori
2105 8cf89e00 Anthony Liguori
    if (vs->v9stat.n_gid != -1) {
2106 8cf89e00 Anthony Liguori
        if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2107 8cf89e00 Anthony Liguori
                    vs->v9stat.n_gid)) {
2108 8cf89e00 Anthony Liguori
            err = -errno;
2109 8cf89e00 Anthony Liguori
        }
2110 8cf89e00 Anthony Liguori
    }
2111 8cf89e00 Anthony Liguori
    v9fs_wstat_post_chown(s, vs, err);
2112 8cf89e00 Anthony Liguori
    return;
2113 8cf89e00 Anthony Liguori
2114 8cf89e00 Anthony Liguori
out:
2115 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2116 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2117 8cf89e00 Anthony Liguori
    qemu_free(vs);
2118 8cf89e00 Anthony Liguori
}
2119 8cf89e00 Anthony Liguori
2120 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2121 8cf89e00 Anthony Liguori
{
2122 8cf89e00 Anthony Liguori
    if (err < 0) {
2123 8cf89e00 Anthony Liguori
        goto out;
2124 8cf89e00 Anthony Liguori
    }
2125 8cf89e00 Anthony Liguori
2126 8cf89e00 Anthony Liguori
    if (vs->v9stat.mtime != -1) {
2127 8cf89e00 Anthony Liguori
        struct utimbuf tb;
2128 8cf89e00 Anthony Liguori
        tb.actime = 0;
2129 8cf89e00 Anthony Liguori
        tb.modtime = vs->v9stat.mtime;
2130 8cf89e00 Anthony Liguori
        if (v9fs_do_utime(s, &vs->fidp->path, &tb)) {
2131 8cf89e00 Anthony Liguori
            err = -errno;
2132 8cf89e00 Anthony Liguori
        }
2133 8cf89e00 Anthony Liguori
    }
2134 8cf89e00 Anthony Liguori
2135 8cf89e00 Anthony Liguori
    v9fs_wstat_post_utime(s, vs, err);
2136 8cf89e00 Anthony Liguori
    return;
2137 8cf89e00 Anthony Liguori
2138 8cf89e00 Anthony Liguori
out:
2139 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2140 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2141 8cf89e00 Anthony Liguori
    qemu_free(vs);
2142 8cf89e00 Anthony Liguori
}
2143 8cf89e00 Anthony Liguori
2144 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2145 8cf89e00 Anthony Liguori
{
2146 8cf89e00 Anthony Liguori
    if (err == -1) {
2147 8cf89e00 Anthony Liguori
        err = -errno;
2148 8cf89e00 Anthony Liguori
    }
2149 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2150 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2151 8cf89e00 Anthony Liguori
    qemu_free(vs);
2152 8cf89e00 Anthony Liguori
}
2153 8cf89e00 Anthony Liguori
2154 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2155 8cf89e00 Anthony Liguori
{
2156 8cf89e00 Anthony Liguori
    uint32_t v9_mode;
2157 8cf89e00 Anthony Liguori
2158 8cf89e00 Anthony Liguori
    if (err == -1) {
2159 8cf89e00 Anthony Liguori
        err = -errno;
2160 8cf89e00 Anthony Liguori
        goto out;
2161 8cf89e00 Anthony Liguori
    }
2162 8cf89e00 Anthony Liguori
2163 8cf89e00 Anthony Liguori
    v9_mode = stat_to_v9mode(&vs->stbuf);
2164 8cf89e00 Anthony Liguori
2165 8cf89e00 Anthony Liguori
    if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2166 8cf89e00 Anthony Liguori
        (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2167 8cf89e00 Anthony Liguori
            /* Attempting to change the type */
2168 8cf89e00 Anthony Liguori
            err = -EIO;
2169 8cf89e00 Anthony Liguori
            goto out;
2170 8cf89e00 Anthony Liguori
    }
2171 8cf89e00 Anthony Liguori
2172 8cf89e00 Anthony Liguori
    if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2173 8cf89e00 Anthony Liguori
                    &vs->v9stat.extension))) {
2174 8cf89e00 Anthony Liguori
            err = -errno;
2175 8cf89e00 Anthony Liguori
     }
2176 8cf89e00 Anthony Liguori
    v9fs_wstat_post_chmod(s, vs, err);
2177 8cf89e00 Anthony Liguori
    return;
2178 8cf89e00 Anthony Liguori
2179 8cf89e00 Anthony Liguori
out:
2180 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2181 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2182 8cf89e00 Anthony Liguori
    qemu_free(vs);
2183 8cf89e00 Anthony Liguori
}
2184 8cf89e00 Anthony Liguori
2185 9f107513 Anthony Liguori
static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
2186 9f107513 Anthony Liguori
{
2187 8cf89e00 Anthony Liguori
    int32_t fid;
2188 8cf89e00 Anthony Liguori
    V9fsWstatState *vs;
2189 8cf89e00 Anthony Liguori
    int err = 0;
2190 8cf89e00 Anthony Liguori
2191 8cf89e00 Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
2192 8cf89e00 Anthony Liguori
    vs->pdu = pdu;
2193 8cf89e00 Anthony Liguori
    vs->offset = 7;
2194 8cf89e00 Anthony Liguori
2195 8cf89e00 Anthony Liguori
    pdu_unmarshal(pdu, vs->offset, "dwS", &fid, &vs->unused, &vs->v9stat);
2196 8cf89e00 Anthony Liguori
2197 8cf89e00 Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
2198 8cf89e00 Anthony Liguori
    if (vs->fidp == NULL) {
2199 8cf89e00 Anthony Liguori
        err = -EINVAL;
2200 8cf89e00 Anthony Liguori
        goto out;
2201 9f107513 Anthony Liguori
    }
2202 8cf89e00 Anthony Liguori
2203 8cf89e00 Anthony Liguori
    /* do we need to sync the file? */
2204 8cf89e00 Anthony Liguori
    if (donttouch_stat(&vs->v9stat)) {
2205 8cf89e00 Anthony Liguori
        err = v9fs_do_fsync(s, vs->fidp->fd);
2206 8cf89e00 Anthony Liguori
        v9fs_wstat_post_fsync(s, vs, err);
2207 8cf89e00 Anthony Liguori
        return;
2208 8cf89e00 Anthony Liguori
    }
2209 8cf89e00 Anthony Liguori
2210 8cf89e00 Anthony Liguori
    if (vs->v9stat.mode != -1) {
2211 8cf89e00 Anthony Liguori
        err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
2212 8cf89e00 Anthony Liguori
        v9fs_wstat_post_lstat(s, vs, err);
2213 8cf89e00 Anthony Liguori
        return;
2214 8cf89e00 Anthony Liguori
    }
2215 8cf89e00 Anthony Liguori
2216 8cf89e00 Anthony Liguori
    v9fs_wstat_post_chmod(s, vs, err);
2217 8cf89e00 Anthony Liguori
    return;
2218 8cf89e00 Anthony Liguori
2219 8cf89e00 Anthony Liguori
out:
2220 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2221 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2222 8cf89e00 Anthony Liguori
    qemu_free(vs);
2223 9f107513 Anthony Liguori
}
2224 9f107513 Anthony Liguori
2225 9f107513 Anthony Liguori
typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
2226 9f107513 Anthony Liguori
2227 9f107513 Anthony Liguori
static pdu_handler_t *pdu_handlers[] = {
2228 9f107513 Anthony Liguori
    [P9_TVERSION] = v9fs_version,
2229 9f107513 Anthony Liguori
    [P9_TATTACH] = v9fs_attach,
2230 9f107513 Anthony Liguori
    [P9_TSTAT] = v9fs_stat,
2231 9f107513 Anthony Liguori
    [P9_TWALK] = v9fs_walk,
2232 9f107513 Anthony Liguori
    [P9_TCLUNK] = v9fs_clunk,
2233 9f107513 Anthony Liguori
    [P9_TOPEN] = v9fs_open,
2234 9f107513 Anthony Liguori
    [P9_TREAD] = v9fs_read,
2235 9f107513 Anthony Liguori
#if 0
2236 9f107513 Anthony Liguori
    [P9_TAUTH] = v9fs_auth,
2237 9f107513 Anthony Liguori
#endif
2238 9f107513 Anthony Liguori
    [P9_TFLUSH] = v9fs_flush,
2239 9f107513 Anthony Liguori
    [P9_TCREATE] = v9fs_create,
2240 9f107513 Anthony Liguori
    [P9_TWRITE] = v9fs_write,
2241 9f107513 Anthony Liguori
    [P9_TWSTAT] = v9fs_wstat,
2242 9f107513 Anthony Liguori
    [P9_TREMOVE] = v9fs_remove,
2243 9f107513 Anthony Liguori
};
2244 9f107513 Anthony Liguori
2245 9f107513 Anthony Liguori
static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
2246 9f107513 Anthony Liguori
{
2247 9f107513 Anthony Liguori
    pdu_handler_t *handler;
2248 9f107513 Anthony Liguori
2249 9f107513 Anthony Liguori
    if (debug_9p_pdu) {
2250 9f107513 Anthony Liguori
        pprint_pdu(pdu);
2251 9f107513 Anthony Liguori
    }
2252 9f107513 Anthony Liguori
2253 9f107513 Anthony Liguori
    BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
2254 9f107513 Anthony Liguori
2255 9f107513 Anthony Liguori
    handler = pdu_handlers[pdu->id];
2256 9f107513 Anthony Liguori
    BUG_ON(handler == NULL);
2257 9f107513 Anthony Liguori
2258 9f107513 Anthony Liguori
    handler(s, pdu);
2259 9f107513 Anthony Liguori
}
2260 9f107513 Anthony Liguori
2261 9f107513 Anthony Liguori
static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
2262 9f107513 Anthony Liguori
{
2263 9f107513 Anthony Liguori
    V9fsState *s = (V9fsState *)vdev;
2264 9f107513 Anthony Liguori
    V9fsPDU *pdu;
2265 9f107513 Anthony Liguori
    ssize_t len;
2266 9f107513 Anthony Liguori
2267 9f107513 Anthony Liguori
    while ((pdu = alloc_pdu(s)) &&
2268 9f107513 Anthony Liguori
            (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
2269 9f107513 Anthony Liguori
        uint8_t *ptr;
2270 9f107513 Anthony Liguori
2271 9f107513 Anthony Liguori
        BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
2272 9f107513 Anthony Liguori
        BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
2273 9f107513 Anthony Liguori
2274 9f107513 Anthony Liguori
        ptr = pdu->elem.out_sg[0].iov_base;
2275 9f107513 Anthony Liguori
2276 9f107513 Anthony Liguori
        memcpy(&pdu->size, ptr, 4);
2277 9f107513 Anthony Liguori
        pdu->id = ptr[4];
2278 9f107513 Anthony Liguori
        memcpy(&pdu->tag, ptr + 5, 2);
2279 9f107513 Anthony Liguori
2280 9f107513 Anthony Liguori
        submit_pdu(s, pdu);
2281 9f107513 Anthony Liguori
    }
2282 9f107513 Anthony Liguori
2283 9f107513 Anthony Liguori
    free_pdu(s, pdu);
2284 9f107513 Anthony Liguori
}
2285 9f107513 Anthony Liguori
2286 9f107513 Anthony Liguori
static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
2287 9f107513 Anthony Liguori
{
2288 9f107513 Anthony Liguori
    features |= 1 << VIRTIO_9P_MOUNT_TAG;
2289 9f107513 Anthony Liguori
    return features;
2290 9f107513 Anthony Liguori
}
2291 9f107513 Anthony Liguori
2292 9f107513 Anthony Liguori
static V9fsState *to_virtio_9p(VirtIODevice *vdev)
2293 9f107513 Anthony Liguori
{
2294 9f107513 Anthony Liguori
    return (V9fsState *)vdev;
2295 9f107513 Anthony Liguori
}
2296 9f107513 Anthony Liguori
2297 9f107513 Anthony Liguori
static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
2298 9f107513 Anthony Liguori
{
2299 9f107513 Anthony Liguori
    struct virtio_9p_config *cfg;
2300 9f107513 Anthony Liguori
    V9fsState *s = to_virtio_9p(vdev);
2301 9f107513 Anthony Liguori
2302 9f107513 Anthony Liguori
    cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
2303 9f107513 Anthony Liguori
                        s->tag_len);
2304 9f107513 Anthony Liguori
    stw_raw(&cfg->tag_len, s->tag_len);
2305 9f107513 Anthony Liguori
    memcpy(cfg->tag, s->tag, s->tag_len);
2306 9f107513 Anthony Liguori
    memcpy(config, cfg, s->config_size);
2307 9f107513 Anthony Liguori
    qemu_free(cfg);
2308 9f107513 Anthony Liguori
}
2309 9f107513 Anthony Liguori
2310 9f107513 Anthony Liguori
VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
2311 9f107513 Anthony Liguori
 {
2312 9f107513 Anthony Liguori
    V9fsState *s;
2313 9f107513 Anthony Liguori
    int i, len;
2314 9f107513 Anthony Liguori
    struct stat stat;
2315 9f107513 Anthony Liguori
    FsTypeEntry *fse;
2316 9f107513 Anthony Liguori
2317 9f107513 Anthony Liguori
2318 9f107513 Anthony Liguori
    s = (V9fsState *)virtio_common_init("virtio-9p",
2319 9f107513 Anthony Liguori
                                    VIRTIO_ID_9P,
2320 9f107513 Anthony Liguori
                                    sizeof(struct virtio_9p_config)+
2321 9f107513 Anthony Liguori
                                    MAX_TAG_LEN,
2322 9f107513 Anthony Liguori
                                    sizeof(V9fsState));
2323 9f107513 Anthony Liguori
2324 9f107513 Anthony Liguori
    /* initialize pdu allocator */
2325 9f107513 Anthony Liguori
    QLIST_INIT(&s->free_list);
2326 9f107513 Anthony Liguori
    for (i = 0; i < (MAX_REQ - 1); i++) {
2327 9f107513 Anthony Liguori
        QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
2328 9f107513 Anthony Liguori
    }
2329 9f107513 Anthony Liguori
2330 9f107513 Anthony Liguori
    s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
2331 9f107513 Anthony Liguori
2332 9f107513 Anthony Liguori
    fse = get_fsdev_fsentry(conf->fsdev_id);
2333 9f107513 Anthony Liguori
2334 9f107513 Anthony Liguori
    if (!fse) {
2335 9f107513 Anthony Liguori
        /* We don't have a fsdev identified by fsdev_id */
2336 9f107513 Anthony Liguori
        fprintf(stderr, "Virtio-9p device couldn't find fsdev "
2337 9f107513 Anthony Liguori
                    "with the id %s\n", conf->fsdev_id);
2338 9f107513 Anthony Liguori
        exit(1);
2339 9f107513 Anthony Liguori
    }
2340 9f107513 Anthony Liguori
2341 9f107513 Anthony Liguori
    if (!fse->path || !conf->tag) {
2342 9f107513 Anthony Liguori
        /* we haven't specified a mount_tag or the path */
2343 9f107513 Anthony Liguori
        fprintf(stderr, "fsdev with id %s needs path "
2344 9f107513 Anthony Liguori
                "and Virtio-9p device needs mount_tag arguments\n",
2345 9f107513 Anthony Liguori
                conf->fsdev_id);
2346 9f107513 Anthony Liguori
        exit(1);
2347 9f107513 Anthony Liguori
    }
2348 9f107513 Anthony Liguori
2349 9f107513 Anthony Liguori
    if (lstat(fse->path, &stat)) {
2350 9f107513 Anthony Liguori
        fprintf(stderr, "share path %s does not exist\n", fse->path);
2351 9f107513 Anthony Liguori
        exit(1);
2352 9f107513 Anthony Liguori
    } else if (!S_ISDIR(stat.st_mode)) {
2353 9f107513 Anthony Liguori
        fprintf(stderr, "share path %s is not a directory \n", fse->path);
2354 9f107513 Anthony Liguori
        exit(1);
2355 9f107513 Anthony Liguori
    }
2356 9f107513 Anthony Liguori
2357 9f107513 Anthony Liguori
    s->ctx.fs_root = qemu_strdup(fse->path);
2358 9f107513 Anthony Liguori
    len = strlen(conf->tag);
2359 9f107513 Anthony Liguori
    if (len > MAX_TAG_LEN) {
2360 9f107513 Anthony Liguori
        len = MAX_TAG_LEN;
2361 9f107513 Anthony Liguori
    }
2362 9f107513 Anthony Liguori
    /* s->tag is non-NULL terminated string */
2363 9f107513 Anthony Liguori
    s->tag = qemu_malloc(len);
2364 9f107513 Anthony Liguori
    memcpy(s->tag, conf->tag, len);
2365 9f107513 Anthony Liguori
    s->tag_len = len;
2366 9f107513 Anthony Liguori
    s->ctx.uid = -1;
2367 9f107513 Anthony Liguori
2368 9f107513 Anthony Liguori
    s->ops = fse->ops;
2369 9f107513 Anthony Liguori
    s->vdev.get_features = virtio_9p_get_features;
2370 9f107513 Anthony Liguori
    s->config_size = sizeof(struct virtio_9p_config) +
2371 9f107513 Anthony Liguori
                        s->tag_len;
2372 9f107513 Anthony Liguori
    s->vdev.get_config = virtio_9p_get_config;
2373 9f107513 Anthony Liguori
2374 9f107513 Anthony Liguori
    return &s->vdev;
2375 9f107513 Anthony Liguori
}