Statistics
| Branch: | Revision:

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

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