Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (70.2 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 8fc39ae4 Sripathi Kodi
static int v9fs_do_utimensat(V9fsState *s, V9fsString *path,
241 8fc39ae4 Sripathi Kodi
                                           const struct timespec times[2])
242 8cf89e00 Anthony Liguori
{
243 8fc39ae4 Sripathi Kodi
    return s->ops->utimensat(&s->ctx, path->data, times);
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 5e94c103 M. Mohan Kumar
static int v9fs_do_statfs(V9fsState *s, V9fsString *path, struct statfs *stbuf)
257 5e94c103 M. Mohan Kumar
{
258 5e94c103 M. Mohan Kumar
    return s->ops->statfs(&s->ctx, path->data, stbuf);
259 5e94c103 M. Mohan Kumar
}
260 5e94c103 M. Mohan Kumar
261 a03f7874 Anthony Liguori
static void v9fs_string_init(V9fsString *str)
262 a03f7874 Anthony Liguori
{
263 a03f7874 Anthony Liguori
    str->data = NULL;
264 a03f7874 Anthony Liguori
    str->size = 0;
265 a03f7874 Anthony Liguori
}
266 a03f7874 Anthony Liguori
267 a03f7874 Anthony Liguori
static void v9fs_string_free(V9fsString *str)
268 a03f7874 Anthony Liguori
{
269 a03f7874 Anthony Liguori
    qemu_free(str->data);
270 a03f7874 Anthony Liguori
    str->data = NULL;
271 a03f7874 Anthony Liguori
    str->size = 0;
272 a03f7874 Anthony Liguori
}
273 a03f7874 Anthony Liguori
274 a03f7874 Anthony Liguori
static void v9fs_string_null(V9fsString *str)
275 a03f7874 Anthony Liguori
{
276 a03f7874 Anthony Liguori
    v9fs_string_free(str);
277 a03f7874 Anthony Liguori
}
278 a03f7874 Anthony Liguori
279 a03f7874 Anthony Liguori
static int number_to_string(void *arg, char type)
280 a03f7874 Anthony Liguori
{
281 a03f7874 Anthony Liguori
    unsigned int ret = 0;
282 a03f7874 Anthony Liguori
283 a03f7874 Anthony Liguori
    switch (type) {
284 a03f7874 Anthony Liguori
    case 'u': {
285 a03f7874 Anthony Liguori
        unsigned int num = *(unsigned int *)arg;
286 a03f7874 Anthony Liguori
287 a03f7874 Anthony Liguori
        do {
288 a03f7874 Anthony Liguori
            ret++;
289 a03f7874 Anthony Liguori
            num = num/10;
290 a03f7874 Anthony Liguori
        } while (num);
291 a03f7874 Anthony Liguori
        break;
292 a03f7874 Anthony Liguori
    }
293 a03f7874 Anthony Liguori
    default:
294 a03f7874 Anthony Liguori
        printf("Number_to_string: Unknown number format\n");
295 a03f7874 Anthony Liguori
        return -1;
296 a03f7874 Anthony Liguori
    }
297 a03f7874 Anthony Liguori
298 a03f7874 Anthony Liguori
    return ret;
299 a03f7874 Anthony Liguori
}
300 a03f7874 Anthony Liguori
301 a03f7874 Anthony Liguori
static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
302 a03f7874 Anthony Liguori
{
303 a03f7874 Anthony Liguori
    va_list ap2;
304 a03f7874 Anthony Liguori
    char *iter = (char *)fmt;
305 a03f7874 Anthony Liguori
    int len = 0;
306 a03f7874 Anthony Liguori
    int nr_args = 0;
307 a03f7874 Anthony Liguori
    char *arg_char_ptr;
308 a03f7874 Anthony Liguori
    unsigned int arg_uint;
309 a03f7874 Anthony Liguori
310 a03f7874 Anthony Liguori
    /* Find the number of %'s that denotes an argument */
311 a03f7874 Anthony Liguori
    for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
312 a03f7874 Anthony Liguori
        nr_args++;
313 a03f7874 Anthony Liguori
        iter++;
314 a03f7874 Anthony Liguori
    }
315 a03f7874 Anthony Liguori
316 a03f7874 Anthony Liguori
    len = strlen(fmt) - 2*nr_args;
317 a03f7874 Anthony Liguori
318 a03f7874 Anthony Liguori
    if (!nr_args) {
319 a03f7874 Anthony Liguori
        goto alloc_print;
320 a03f7874 Anthony Liguori
    }
321 a03f7874 Anthony Liguori
322 a03f7874 Anthony Liguori
    va_copy(ap2, ap);
323 a03f7874 Anthony Liguori
324 a03f7874 Anthony Liguori
    iter = (char *)fmt;
325 a03f7874 Anthony Liguori
326 a03f7874 Anthony Liguori
    /* Now parse the format string */
327 a03f7874 Anthony Liguori
    for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
328 a03f7874 Anthony Liguori
        iter++;
329 a03f7874 Anthony Liguori
        switch (*iter) {
330 a03f7874 Anthony Liguori
        case 'u':
331 a03f7874 Anthony Liguori
            arg_uint = va_arg(ap2, unsigned int);
332 a03f7874 Anthony Liguori
            len += number_to_string((void *)&arg_uint, 'u');
333 a03f7874 Anthony Liguori
            break;
334 a03f7874 Anthony Liguori
        case 's':
335 a03f7874 Anthony Liguori
            arg_char_ptr = va_arg(ap2, char *);
336 a03f7874 Anthony Liguori
            len += strlen(arg_char_ptr);
337 a03f7874 Anthony Liguori
            break;
338 a03f7874 Anthony Liguori
        case 'c':
339 a03f7874 Anthony Liguori
            len += 1;
340 a03f7874 Anthony Liguori
            break;
341 a03f7874 Anthony Liguori
        default:
342 a03f7874 Anthony Liguori
            fprintf(stderr,
343 a03f7874 Anthony Liguori
                    "v9fs_string_alloc_printf:Incorrect format %c", *iter);
344 a03f7874 Anthony Liguori
            return -1;
345 a03f7874 Anthony Liguori
        }
346 a03f7874 Anthony Liguori
        iter++;
347 a03f7874 Anthony Liguori
    }
348 a03f7874 Anthony Liguori
349 a03f7874 Anthony Liguori
alloc_print:
350 a03f7874 Anthony Liguori
    *strp = qemu_malloc((len + 1) * sizeof(**strp));
351 a03f7874 Anthony Liguori
352 a03f7874 Anthony Liguori
    return vsprintf(*strp, fmt, ap);
353 a03f7874 Anthony Liguori
}
354 a03f7874 Anthony Liguori
355 a03f7874 Anthony Liguori
static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
356 a03f7874 Anthony Liguori
{
357 a03f7874 Anthony Liguori
    va_list ap;
358 a03f7874 Anthony Liguori
    int err;
359 a03f7874 Anthony Liguori
360 a03f7874 Anthony Liguori
    v9fs_string_free(str);
361 a03f7874 Anthony Liguori
362 a03f7874 Anthony Liguori
    va_start(ap, fmt);
363 a03f7874 Anthony Liguori
    err = v9fs_string_alloc_printf(&str->data, fmt, ap);
364 a03f7874 Anthony Liguori
    BUG_ON(err == -1);
365 a03f7874 Anthony Liguori
    va_end(ap);
366 a03f7874 Anthony Liguori
367 a03f7874 Anthony Liguori
    str->size = err;
368 a03f7874 Anthony Liguori
}
369 a03f7874 Anthony Liguori
370 a03f7874 Anthony Liguori
static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
371 a03f7874 Anthony Liguori
{
372 a03f7874 Anthony Liguori
    v9fs_string_free(lhs);
373 a03f7874 Anthony Liguori
    v9fs_string_sprintf(lhs, "%s", rhs->data);
374 a03f7874 Anthony Liguori
}
375 a03f7874 Anthony Liguori
376 a03f7874 Anthony Liguori
static size_t v9fs_string_size(V9fsString *str)
377 a03f7874 Anthony Liguori
{
378 a03f7874 Anthony Liguori
    return str->size;
379 a03f7874 Anthony Liguori
}
380 a03f7874 Anthony Liguori
381 286d5652 Anthony Liguori
static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
382 286d5652 Anthony Liguori
{
383 286d5652 Anthony Liguori
    V9fsFidState *f;
384 286d5652 Anthony Liguori
385 286d5652 Anthony Liguori
    for (f = s->fid_list; f; f = f->next) {
386 286d5652 Anthony Liguori
        if (f->fid == fid) {
387 286d5652 Anthony Liguori
            return f;
388 286d5652 Anthony Liguori
        }
389 286d5652 Anthony Liguori
    }
390 286d5652 Anthony Liguori
391 286d5652 Anthony Liguori
    return NULL;
392 286d5652 Anthony Liguori
}
393 286d5652 Anthony Liguori
394 286d5652 Anthony Liguori
static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
395 286d5652 Anthony Liguori
{
396 286d5652 Anthony Liguori
    V9fsFidState *f;
397 286d5652 Anthony Liguori
398 286d5652 Anthony Liguori
    f = lookup_fid(s, fid);
399 286d5652 Anthony Liguori
    if (f) {
400 286d5652 Anthony Liguori
        return NULL;
401 286d5652 Anthony Liguori
    }
402 286d5652 Anthony Liguori
403 286d5652 Anthony Liguori
    f = qemu_mallocz(sizeof(V9fsFidState));
404 286d5652 Anthony Liguori
405 286d5652 Anthony Liguori
    f->fid = fid;
406 286d5652 Anthony Liguori
    f->fd = -1;
407 286d5652 Anthony Liguori
    f->dir = NULL;
408 286d5652 Anthony Liguori
409 286d5652 Anthony Liguori
    f->next = s->fid_list;
410 286d5652 Anthony Liguori
    s->fid_list = f;
411 286d5652 Anthony Liguori
412 286d5652 Anthony Liguori
    return f;
413 286d5652 Anthony Liguori
}
414 286d5652 Anthony Liguori
415 286d5652 Anthony Liguori
static int free_fid(V9fsState *s, int32_t fid)
416 286d5652 Anthony Liguori
{
417 286d5652 Anthony Liguori
    V9fsFidState **fidpp, *fidp;
418 286d5652 Anthony Liguori
419 286d5652 Anthony Liguori
    for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
420 286d5652 Anthony Liguori
        if ((*fidpp)->fid == fid) {
421 286d5652 Anthony Liguori
            break;
422 286d5652 Anthony Liguori
        }
423 286d5652 Anthony Liguori
    }
424 286d5652 Anthony Liguori
425 286d5652 Anthony Liguori
    if (*fidpp == NULL) {
426 286d5652 Anthony Liguori
        return -ENOENT;
427 286d5652 Anthony Liguori
    }
428 286d5652 Anthony Liguori
429 286d5652 Anthony Liguori
    fidp = *fidpp;
430 286d5652 Anthony Liguori
    *fidpp = fidp->next;
431 286d5652 Anthony Liguori
432 286d5652 Anthony Liguori
    if (fidp->fd != -1) {
433 286d5652 Anthony Liguori
        v9fs_do_close(s, fidp->fd);
434 286d5652 Anthony Liguori
    }
435 286d5652 Anthony Liguori
    if (fidp->dir) {
436 286d5652 Anthony Liguori
        v9fs_do_closedir(s, fidp->dir);
437 286d5652 Anthony Liguori
    }
438 286d5652 Anthony Liguori
    v9fs_string_free(&fidp->path);
439 286d5652 Anthony Liguori
    qemu_free(fidp);
440 286d5652 Anthony Liguori
441 286d5652 Anthony Liguori
    return 0;
442 286d5652 Anthony Liguori
}
443 286d5652 Anthony Liguori
444 286d5652 Anthony Liguori
#define P9_QID_TYPE_DIR         0x80
445 286d5652 Anthony Liguori
#define P9_QID_TYPE_SYMLINK     0x02
446 286d5652 Anthony Liguori
447 286d5652 Anthony Liguori
#define P9_STAT_MODE_DIR        0x80000000
448 286d5652 Anthony Liguori
#define P9_STAT_MODE_APPEND     0x40000000
449 286d5652 Anthony Liguori
#define P9_STAT_MODE_EXCL       0x20000000
450 286d5652 Anthony Liguori
#define P9_STAT_MODE_MOUNT      0x10000000
451 286d5652 Anthony Liguori
#define P9_STAT_MODE_AUTH       0x08000000
452 286d5652 Anthony Liguori
#define P9_STAT_MODE_TMP        0x04000000
453 286d5652 Anthony Liguori
#define P9_STAT_MODE_SYMLINK    0x02000000
454 286d5652 Anthony Liguori
#define P9_STAT_MODE_LINK       0x01000000
455 286d5652 Anthony Liguori
#define P9_STAT_MODE_DEVICE     0x00800000
456 286d5652 Anthony Liguori
#define P9_STAT_MODE_NAMED_PIPE 0x00200000
457 286d5652 Anthony Liguori
#define P9_STAT_MODE_SOCKET     0x00100000
458 286d5652 Anthony Liguori
#define P9_STAT_MODE_SETUID     0x00080000
459 286d5652 Anthony Liguori
#define P9_STAT_MODE_SETGID     0x00040000
460 286d5652 Anthony Liguori
#define P9_STAT_MODE_SETVTX     0x00010000
461 286d5652 Anthony Liguori
462 286d5652 Anthony Liguori
#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR |          \
463 286d5652 Anthony Liguori
                                P9_STAT_MODE_SYMLINK |      \
464 286d5652 Anthony Liguori
                                P9_STAT_MODE_LINK |         \
465 286d5652 Anthony Liguori
                                P9_STAT_MODE_DEVICE |       \
466 286d5652 Anthony Liguori
                                P9_STAT_MODE_NAMED_PIPE |   \
467 286d5652 Anthony Liguori
                                P9_STAT_MODE_SOCKET)
468 286d5652 Anthony Liguori
469 286d5652 Anthony Liguori
/* This is the algorithm from ufs in spfs */
470 286d5652 Anthony Liguori
static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
471 286d5652 Anthony Liguori
{
472 286d5652 Anthony Liguori
    size_t size;
473 286d5652 Anthony Liguori
474 286d5652 Anthony Liguori
    size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
475 286d5652 Anthony Liguori
    memcpy(&qidp->path, &stbuf->st_ino, size);
476 286d5652 Anthony Liguori
    qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
477 286d5652 Anthony Liguori
    qidp->type = 0;
478 286d5652 Anthony Liguori
    if (S_ISDIR(stbuf->st_mode)) {
479 286d5652 Anthony Liguori
        qidp->type |= P9_QID_TYPE_DIR;
480 286d5652 Anthony Liguori
    }
481 286d5652 Anthony Liguori
    if (S_ISLNK(stbuf->st_mode)) {
482 286d5652 Anthony Liguori
        qidp->type |= P9_QID_TYPE_SYMLINK;
483 286d5652 Anthony Liguori
    }
484 286d5652 Anthony Liguori
}
485 286d5652 Anthony Liguori
486 286d5652 Anthony Liguori
static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
487 286d5652 Anthony Liguori
{
488 286d5652 Anthony Liguori
    struct stat stbuf;
489 286d5652 Anthony Liguori
    int err;
490 286d5652 Anthony Liguori
491 286d5652 Anthony Liguori
    err = v9fs_do_lstat(s, &fidp->path, &stbuf);
492 286d5652 Anthony Liguori
    if (err) {
493 286d5652 Anthony Liguori
        return err;
494 286d5652 Anthony Liguori
    }
495 286d5652 Anthony Liguori
496 286d5652 Anthony Liguori
    stat_to_qid(&stbuf, qidp);
497 286d5652 Anthony Liguori
    return 0;
498 286d5652 Anthony Liguori
}
499 286d5652 Anthony Liguori
500 9f107513 Anthony Liguori
static V9fsPDU *alloc_pdu(V9fsState *s)
501 9f107513 Anthony Liguori
{
502 9f107513 Anthony Liguori
    V9fsPDU *pdu = NULL;
503 9f107513 Anthony Liguori
504 9f107513 Anthony Liguori
    if (!QLIST_EMPTY(&s->free_list)) {
505 9f107513 Anthony Liguori
        pdu = QLIST_FIRST(&s->free_list);
506 9f107513 Anthony Liguori
        QLIST_REMOVE(pdu, next);
507 9f107513 Anthony Liguori
    }
508 9f107513 Anthony Liguori
    return pdu;
509 9f107513 Anthony Liguori
}
510 9f107513 Anthony Liguori
511 9f107513 Anthony Liguori
static void free_pdu(V9fsState *s, V9fsPDU *pdu)
512 9f107513 Anthony Liguori
{
513 9f107513 Anthony Liguori
    if (pdu) {
514 9f107513 Anthony Liguori
        QLIST_INSERT_HEAD(&s->free_list, pdu, next);
515 9f107513 Anthony Liguori
    }
516 9f107513 Anthony Liguori
}
517 9f107513 Anthony Liguori
518 9f107513 Anthony Liguori
size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
519 9f107513 Anthony Liguori
                        size_t offset, size_t size, int pack)
520 9f107513 Anthony Liguori
{
521 9f107513 Anthony Liguori
    int i = 0;
522 9f107513 Anthony Liguori
    size_t copied = 0;
523 9f107513 Anthony Liguori
524 9f107513 Anthony Liguori
    for (i = 0; size && i < sg_count; i++) {
525 9f107513 Anthony Liguori
        size_t len;
526 9f107513 Anthony Liguori
        if (offset >= sg[i].iov_len) {
527 9f107513 Anthony Liguori
            /* skip this sg */
528 9f107513 Anthony Liguori
            offset -= sg[i].iov_len;
529 9f107513 Anthony Liguori
            continue;
530 9f107513 Anthony Liguori
        } else {
531 9f107513 Anthony Liguori
            len = MIN(sg[i].iov_len - offset, size);
532 9f107513 Anthony Liguori
            if (pack) {
533 9f107513 Anthony Liguori
                memcpy(sg[i].iov_base + offset, addr, len);
534 9f107513 Anthony Liguori
            } else {
535 9f107513 Anthony Liguori
                memcpy(addr, sg[i].iov_base + offset, len);
536 9f107513 Anthony Liguori
            }
537 9f107513 Anthony Liguori
            size -= len;
538 9f107513 Anthony Liguori
            copied += len;
539 9f107513 Anthony Liguori
            addr += len;
540 9f107513 Anthony Liguori
            if (size) {
541 9f107513 Anthony Liguori
                offset = 0;
542 9f107513 Anthony Liguori
                continue;
543 9f107513 Anthony Liguori
            }
544 9f107513 Anthony Liguori
        }
545 9f107513 Anthony Liguori
    }
546 9f107513 Anthony Liguori
547 9f107513 Anthony Liguori
    return copied;
548 9f107513 Anthony Liguori
}
549 9f107513 Anthony Liguori
550 405a549a Anthony Liguori
static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
551 405a549a Anthony Liguori
{
552 405a549a Anthony Liguori
    return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
553 405a549a Anthony Liguori
                         offset, size, 0);
554 405a549a Anthony Liguori
}
555 405a549a Anthony Liguori
556 405a549a Anthony Liguori
static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
557 405a549a Anthony Liguori
                        size_t size)
558 405a549a Anthony Liguori
{
559 405a549a Anthony Liguori
    return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
560 405a549a Anthony Liguori
                             offset, size, 1);
561 405a549a Anthony Liguori
}
562 405a549a Anthony Liguori
563 405a549a Anthony Liguori
static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
564 405a549a Anthony Liguori
{
565 405a549a Anthony Liguori
    size_t pos = 0;
566 405a549a Anthony Liguori
    int i, j;
567 405a549a Anthony Liguori
    struct iovec *src_sg;
568 405a549a Anthony Liguori
    unsigned int num;
569 405a549a Anthony Liguori
570 405a549a Anthony Liguori
    if (rx) {
571 405a549a Anthony Liguori
        src_sg = pdu->elem.in_sg;
572 405a549a Anthony Liguori
        num = pdu->elem.in_num;
573 405a549a Anthony Liguori
    } else {
574 405a549a Anthony Liguori
        src_sg = pdu->elem.out_sg;
575 405a549a Anthony Liguori
        num = pdu->elem.out_num;
576 405a549a Anthony Liguori
    }
577 405a549a Anthony Liguori
578 405a549a Anthony Liguori
    j = 0;
579 405a549a Anthony Liguori
    for (i = 0; i < num; i++) {
580 405a549a Anthony Liguori
        if (offset <= pos) {
581 405a549a Anthony Liguori
            sg[j].iov_base = src_sg[i].iov_base;
582 405a549a Anthony Liguori
            sg[j].iov_len = src_sg[i].iov_len;
583 405a549a Anthony Liguori
            j++;
584 405a549a Anthony Liguori
        } else if (offset < (src_sg[i].iov_len + pos)) {
585 405a549a Anthony Liguori
            sg[j].iov_base = src_sg[i].iov_base;
586 405a549a Anthony Liguori
            sg[j].iov_len = src_sg[i].iov_len;
587 405a549a Anthony Liguori
            sg[j].iov_base += (offset - pos);
588 405a549a Anthony Liguori
            sg[j].iov_len -= (offset - pos);
589 405a549a Anthony Liguori
            j++;
590 405a549a Anthony Liguori
        }
591 405a549a Anthony Liguori
        pos += src_sg[i].iov_len;
592 405a549a Anthony Liguori
    }
593 405a549a Anthony Liguori
594 405a549a Anthony Liguori
    return j;
595 405a549a Anthony Liguori
}
596 405a549a Anthony Liguori
597 405a549a Anthony Liguori
static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
598 405a549a Anthony Liguori
{
599 405a549a Anthony Liguori
    size_t old_offset = offset;
600 405a549a Anthony Liguori
    va_list ap;
601 405a549a Anthony Liguori
    int i;
602 405a549a Anthony Liguori
603 405a549a Anthony Liguori
    va_start(ap, fmt);
604 405a549a Anthony Liguori
    for (i = 0; fmt[i]; i++) {
605 405a549a Anthony Liguori
        switch (fmt[i]) {
606 405a549a Anthony Liguori
        case 'b': {
607 405a549a Anthony Liguori
            uint8_t *valp = va_arg(ap, uint8_t *);
608 405a549a Anthony Liguori
            offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
609 405a549a Anthony Liguori
            break;
610 405a549a Anthony Liguori
        }
611 405a549a Anthony Liguori
        case 'w': {
612 405a549a Anthony Liguori
            uint16_t val, *valp;
613 405a549a Anthony Liguori
            valp = va_arg(ap, uint16_t *);
614 405a549a Anthony Liguori
            val = le16_to_cpupu(valp);
615 405a549a Anthony Liguori
            offset += pdu_unpack(&val, pdu, offset, sizeof(val));
616 405a549a Anthony Liguori
            *valp = val;
617 405a549a Anthony Liguori
            break;
618 405a549a Anthony Liguori
        }
619 405a549a Anthony Liguori
        case 'd': {
620 405a549a Anthony Liguori
            uint32_t val, *valp;
621 405a549a Anthony Liguori
            valp = va_arg(ap, uint32_t *);
622 405a549a Anthony Liguori
            val = le32_to_cpupu(valp);
623 405a549a Anthony Liguori
            offset += pdu_unpack(&val, pdu, offset, sizeof(val));
624 405a549a Anthony Liguori
            *valp = val;
625 405a549a Anthony Liguori
            break;
626 405a549a Anthony Liguori
        }
627 405a549a Anthony Liguori
        case 'q': {
628 405a549a Anthony Liguori
            uint64_t val, *valp;
629 405a549a Anthony Liguori
            valp = va_arg(ap, uint64_t *);
630 405a549a Anthony Liguori
            val = le64_to_cpup(valp);
631 405a549a Anthony Liguori
            offset += pdu_unpack(&val, pdu, offset, sizeof(val));
632 405a549a Anthony Liguori
            *valp = val;
633 405a549a Anthony Liguori
            break;
634 405a549a Anthony Liguori
        }
635 405a549a Anthony Liguori
        case 'v': {
636 405a549a Anthony Liguori
            struct iovec *iov = va_arg(ap, struct iovec *);
637 405a549a Anthony Liguori
            int *iovcnt = va_arg(ap, int *);
638 405a549a Anthony Liguori
            *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
639 405a549a Anthony Liguori
            break;
640 405a549a Anthony Liguori
        }
641 405a549a Anthony Liguori
        case 's': {
642 405a549a Anthony Liguori
            V9fsString *str = va_arg(ap, V9fsString *);
643 405a549a Anthony Liguori
            offset += pdu_unmarshal(pdu, offset, "w", &str->size);
644 405a549a Anthony Liguori
            /* FIXME: sanity check str->size */
645 405a549a Anthony Liguori
            str->data = qemu_malloc(str->size + 1);
646 405a549a Anthony Liguori
            offset += pdu_unpack(str->data, pdu, offset, str->size);
647 405a549a Anthony Liguori
            str->data[str->size] = 0;
648 405a549a Anthony Liguori
            break;
649 405a549a Anthony Liguori
        }
650 405a549a Anthony Liguori
        case 'Q': {
651 405a549a Anthony Liguori
            V9fsQID *qidp = va_arg(ap, V9fsQID *);
652 405a549a Anthony Liguori
            offset += pdu_unmarshal(pdu, offset, "bdq",
653 405a549a Anthony Liguori
                        &qidp->type, &qidp->version, &qidp->path);
654 405a549a Anthony Liguori
            break;
655 405a549a Anthony Liguori
        }
656 405a549a Anthony Liguori
        case 'S': {
657 405a549a Anthony Liguori
            V9fsStat *statp = va_arg(ap, V9fsStat *);
658 405a549a Anthony Liguori
            offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
659 405a549a Anthony Liguori
                        &statp->size, &statp->type, &statp->dev,
660 405a549a Anthony Liguori
                        &statp->qid, &statp->mode, &statp->atime,
661 405a549a Anthony Liguori
                        &statp->mtime, &statp->length,
662 405a549a Anthony Liguori
                        &statp->name, &statp->uid, &statp->gid,
663 405a549a Anthony Liguori
                        &statp->muid, &statp->extension,
664 405a549a Anthony Liguori
                        &statp->n_uid, &statp->n_gid,
665 405a549a Anthony Liguori
                        &statp->n_muid);
666 405a549a Anthony Liguori
            break;
667 405a549a Anthony Liguori
        }
668 c79ce737 Sripathi Kodi
        case 'I': {
669 c79ce737 Sripathi Kodi
            V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
670 c79ce737 Sripathi Kodi
            offset += pdu_unmarshal(pdu, offset, "ddddqqqqq",
671 c79ce737 Sripathi Kodi
                        &iattr->valid, &iattr->mode,
672 c79ce737 Sripathi Kodi
                        &iattr->uid, &iattr->gid, &iattr->size,
673 c79ce737 Sripathi Kodi
                        &iattr->atime_sec, &iattr->atime_nsec,
674 c79ce737 Sripathi Kodi
                        &iattr->mtime_sec, &iattr->mtime_nsec);
675 c79ce737 Sripathi Kodi
            break;
676 c79ce737 Sripathi Kodi
        }
677 405a549a Anthony Liguori
        default:
678 405a549a Anthony Liguori
            break;
679 405a549a Anthony Liguori
        }
680 405a549a Anthony Liguori
    }
681 405a549a Anthony Liguori
682 405a549a Anthony Liguori
    va_end(ap);
683 405a549a Anthony Liguori
684 405a549a Anthony Liguori
    return offset - old_offset;
685 405a549a Anthony Liguori
}
686 405a549a Anthony Liguori
687 405a549a Anthony Liguori
static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
688 405a549a Anthony Liguori
{
689 405a549a Anthony Liguori
    size_t old_offset = offset;
690 405a549a Anthony Liguori
    va_list ap;
691 405a549a Anthony Liguori
    int i;
692 405a549a Anthony Liguori
693 405a549a Anthony Liguori
    va_start(ap, fmt);
694 405a549a Anthony Liguori
    for (i = 0; fmt[i]; i++) {
695 405a549a Anthony Liguori
        switch (fmt[i]) {
696 405a549a Anthony Liguori
        case 'b': {
697 405a549a Anthony Liguori
            uint8_t val = va_arg(ap, int);
698 405a549a Anthony Liguori
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
699 405a549a Anthony Liguori
            break;
700 405a549a Anthony Liguori
        }
701 405a549a Anthony Liguori
        case 'w': {
702 405a549a Anthony Liguori
            uint16_t val;
703 405a549a Anthony Liguori
            cpu_to_le16w(&val, va_arg(ap, int));
704 405a549a Anthony Liguori
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
705 405a549a Anthony Liguori
            break;
706 405a549a Anthony Liguori
        }
707 405a549a Anthony Liguori
        case 'd': {
708 405a549a Anthony Liguori
            uint32_t val;
709 405a549a Anthony Liguori
            cpu_to_le32w(&val, va_arg(ap, uint32_t));
710 405a549a Anthony Liguori
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
711 405a549a Anthony Liguori
            break;
712 405a549a Anthony Liguori
        }
713 405a549a Anthony Liguori
        case 'q': {
714 405a549a Anthony Liguori
            uint64_t val;
715 405a549a Anthony Liguori
            cpu_to_le64w(&val, va_arg(ap, uint64_t));
716 405a549a Anthony Liguori
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
717 405a549a Anthony Liguori
            break;
718 405a549a Anthony Liguori
        }
719 405a549a Anthony Liguori
        case 'v': {
720 405a549a Anthony Liguori
            struct iovec *iov = va_arg(ap, struct iovec *);
721 405a549a Anthony Liguori
            int *iovcnt = va_arg(ap, int *);
722 405a549a Anthony Liguori
            *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
723 405a549a Anthony Liguori
            break;
724 405a549a Anthony Liguori
        }
725 405a549a Anthony Liguori
        case 's': {
726 405a549a Anthony Liguori
            V9fsString *str = va_arg(ap, V9fsString *);
727 405a549a Anthony Liguori
            offset += pdu_marshal(pdu, offset, "w", str->size);
728 405a549a Anthony Liguori
            offset += pdu_pack(pdu, offset, str->data, str->size);
729 405a549a Anthony Liguori
            break;
730 405a549a Anthony Liguori
        }
731 405a549a Anthony Liguori
        case 'Q': {
732 405a549a Anthony Liguori
            V9fsQID *qidp = va_arg(ap, V9fsQID *);
733 405a549a Anthony Liguori
            offset += pdu_marshal(pdu, offset, "bdq",
734 405a549a Anthony Liguori
                        qidp->type, qidp->version, qidp->path);
735 405a549a Anthony Liguori
            break;
736 405a549a Anthony Liguori
        }
737 405a549a Anthony Liguori
        case 'S': {
738 405a549a Anthony Liguori
            V9fsStat *statp = va_arg(ap, V9fsStat *);
739 405a549a Anthony Liguori
            offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
740 405a549a Anthony Liguori
                        statp->size, statp->type, statp->dev,
741 405a549a Anthony Liguori
                        &statp->qid, statp->mode, statp->atime,
742 405a549a Anthony Liguori
                        statp->mtime, statp->length, &statp->name,
743 405a549a Anthony Liguori
                        &statp->uid, &statp->gid, &statp->muid,
744 405a549a Anthony Liguori
                        &statp->extension, statp->n_uid,
745 405a549a Anthony Liguori
                        statp->n_gid, statp->n_muid);
746 405a549a Anthony Liguori
            break;
747 405a549a Anthony Liguori
        }
748 00ede4c2 Sripathi Kodi
        case 'A': {
749 00ede4c2 Sripathi Kodi
            V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
750 00ede4c2 Sripathi Kodi
            offset += pdu_marshal(pdu, offset, "qQdddqqqqqqqqqqqqqqq",
751 00ede4c2 Sripathi Kodi
                        statp->st_result_mask,
752 00ede4c2 Sripathi Kodi
                        &statp->qid, statp->st_mode,
753 00ede4c2 Sripathi Kodi
                        statp->st_uid, statp->st_gid,
754 00ede4c2 Sripathi Kodi
                        statp->st_nlink, statp->st_rdev,
755 00ede4c2 Sripathi Kodi
                        statp->st_size, statp->st_blksize, statp->st_blocks,
756 00ede4c2 Sripathi Kodi
                        statp->st_atime_sec, statp->st_atime_nsec,
757 00ede4c2 Sripathi Kodi
                        statp->st_mtime_sec, statp->st_mtime_nsec,
758 00ede4c2 Sripathi Kodi
                        statp->st_ctime_sec, statp->st_ctime_nsec,
759 00ede4c2 Sripathi Kodi
                        statp->st_btime_sec, statp->st_btime_nsec,
760 00ede4c2 Sripathi Kodi
                        statp->st_gen, statp->st_data_version);
761 00ede4c2 Sripathi Kodi
            break;
762 00ede4c2 Sripathi Kodi
        }
763 405a549a Anthony Liguori
        default:
764 405a549a Anthony Liguori
            break;
765 405a549a Anthony Liguori
        }
766 405a549a Anthony Liguori
    }
767 405a549a Anthony Liguori
    va_end(ap);
768 405a549a Anthony Liguori
769 405a549a Anthony Liguori
    return offset - old_offset;
770 405a549a Anthony Liguori
}
771 405a549a Anthony Liguori
772 405a549a Anthony Liguori
static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
773 405a549a Anthony Liguori
{
774 405a549a Anthony Liguori
    int8_t id = pdu->id + 1; /* Response */
775 405a549a Anthony Liguori
776 405a549a Anthony Liguori
    if (len < 0) {
777 405a549a Anthony Liguori
        V9fsString str;
778 405a549a Anthony Liguori
        int err = -len;
779 405a549a Anthony Liguori
780 405a549a Anthony Liguori
        str.data = strerror(err);
781 405a549a Anthony Liguori
        str.size = strlen(str.data);
782 405a549a Anthony Liguori
783 405a549a Anthony Liguori
        len = 7;
784 405a549a Anthony Liguori
        len += pdu_marshal(pdu, len, "s", &str);
785 405a549a Anthony Liguori
        if (dotu) {
786 405a549a Anthony Liguori
            len += pdu_marshal(pdu, len, "d", err);
787 405a549a Anthony Liguori
        }
788 405a549a Anthony Liguori
789 405a549a Anthony Liguori
        id = P9_RERROR;
790 405a549a Anthony Liguori
    }
791 405a549a Anthony Liguori
792 405a549a Anthony Liguori
    /* fill out the header */
793 405a549a Anthony Liguori
    pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
794 405a549a Anthony Liguori
795 405a549a Anthony Liguori
    /* keep these in sync */
796 405a549a Anthony Liguori
    pdu->size = len;
797 405a549a Anthony Liguori
    pdu->id = id;
798 405a549a Anthony Liguori
799 405a549a Anthony Liguori
    /* push onto queue and notify */
800 405a549a Anthony Liguori
    virtqueue_push(s->vq, &pdu->elem, len);
801 405a549a Anthony Liguori
802 405a549a Anthony Liguori
    /* FIXME: we should batch these completions */
803 405a549a Anthony Liguori
    virtio_notify(&s->vdev, s->vq);
804 405a549a Anthony Liguori
805 405a549a Anthony Liguori
    free_pdu(s, pdu);
806 405a549a Anthony Liguori
}
807 405a549a Anthony Liguori
808 bb9e3216 Anthony Liguori
static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
809 bb9e3216 Anthony Liguori
{
810 bb9e3216 Anthony Liguori
    mode_t ret;
811 bb9e3216 Anthony Liguori
812 bb9e3216 Anthony Liguori
    ret = mode & 0777;
813 bb9e3216 Anthony Liguori
    if (mode & P9_STAT_MODE_DIR) {
814 bb9e3216 Anthony Liguori
        ret |= S_IFDIR;
815 bb9e3216 Anthony Liguori
    }
816 bb9e3216 Anthony Liguori
817 bb9e3216 Anthony Liguori
    if (dotu) {
818 bb9e3216 Anthony Liguori
        if (mode & P9_STAT_MODE_SYMLINK) {
819 bb9e3216 Anthony Liguori
            ret |= S_IFLNK;
820 bb9e3216 Anthony Liguori
        }
821 bb9e3216 Anthony Liguori
        if (mode & P9_STAT_MODE_SOCKET) {
822 bb9e3216 Anthony Liguori
            ret |= S_IFSOCK;
823 bb9e3216 Anthony Liguori
        }
824 bb9e3216 Anthony Liguori
        if (mode & P9_STAT_MODE_NAMED_PIPE) {
825 bb9e3216 Anthony Liguori
            ret |= S_IFIFO;
826 bb9e3216 Anthony Liguori
        }
827 bb9e3216 Anthony Liguori
        if (mode & P9_STAT_MODE_DEVICE) {
828 bb9e3216 Anthony Liguori
            if (extension && extension->data[0] == 'c') {
829 bb9e3216 Anthony Liguori
                ret |= S_IFCHR;
830 bb9e3216 Anthony Liguori
            } else {
831 bb9e3216 Anthony Liguori
                ret |= S_IFBLK;
832 bb9e3216 Anthony Liguori
            }
833 bb9e3216 Anthony Liguori
        }
834 bb9e3216 Anthony Liguori
    }
835 bb9e3216 Anthony Liguori
836 bb9e3216 Anthony Liguori
    if (!(ret&~0777)) {
837 bb9e3216 Anthony Liguori
        ret |= S_IFREG;
838 bb9e3216 Anthony Liguori
    }
839 bb9e3216 Anthony Liguori
840 bb9e3216 Anthony Liguori
    if (mode & P9_STAT_MODE_SETUID) {
841 bb9e3216 Anthony Liguori
        ret |= S_ISUID;
842 bb9e3216 Anthony Liguori
    }
843 bb9e3216 Anthony Liguori
    if (mode & P9_STAT_MODE_SETGID) {
844 bb9e3216 Anthony Liguori
        ret |= S_ISGID;
845 bb9e3216 Anthony Liguori
    }
846 bb9e3216 Anthony Liguori
    if (mode & P9_STAT_MODE_SETVTX) {
847 bb9e3216 Anthony Liguori
        ret |= S_ISVTX;
848 bb9e3216 Anthony Liguori
    }
849 bb9e3216 Anthony Liguori
850 bb9e3216 Anthony Liguori
    return ret;
851 bb9e3216 Anthony Liguori
}
852 bb9e3216 Anthony Liguori
853 bb9e3216 Anthony Liguori
static int donttouch_stat(V9fsStat *stat)
854 bb9e3216 Anthony Liguori
{
855 bb9e3216 Anthony Liguori
    if (stat->type == -1 &&
856 bb9e3216 Anthony Liguori
        stat->dev == -1 &&
857 bb9e3216 Anthony Liguori
        stat->qid.type == -1 &&
858 bb9e3216 Anthony Liguori
        stat->qid.version == -1 &&
859 bb9e3216 Anthony Liguori
        stat->qid.path == -1 &&
860 bb9e3216 Anthony Liguori
        stat->mode == -1 &&
861 bb9e3216 Anthony Liguori
        stat->atime == -1 &&
862 bb9e3216 Anthony Liguori
        stat->mtime == -1 &&
863 bb9e3216 Anthony Liguori
        stat->length == -1 &&
864 bb9e3216 Anthony Liguori
        !stat->name.size &&
865 bb9e3216 Anthony Liguori
        !stat->uid.size &&
866 bb9e3216 Anthony Liguori
        !stat->gid.size &&
867 bb9e3216 Anthony Liguori
        !stat->muid.size &&
868 bb9e3216 Anthony Liguori
        stat->n_uid == -1 &&
869 bb9e3216 Anthony Liguori
        stat->n_gid == -1 &&
870 bb9e3216 Anthony Liguori
        stat->n_muid == -1) {
871 bb9e3216 Anthony Liguori
        return 1;
872 bb9e3216 Anthony Liguori
    }
873 bb9e3216 Anthony Liguori
874 bb9e3216 Anthony Liguori
    return 0;
875 bb9e3216 Anthony Liguori
}
876 bb9e3216 Anthony Liguori
877 bb9e3216 Anthony Liguori
static void v9fs_stat_free(V9fsStat *stat)
878 bb9e3216 Anthony Liguori
{
879 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->name);
880 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->uid);
881 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->gid);
882 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->muid);
883 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->extension);
884 bb9e3216 Anthony Liguori
}
885 bb9e3216 Anthony Liguori
886 bb9e3216 Anthony Liguori
static uint32_t stat_to_v9mode(const struct stat *stbuf)
887 bb9e3216 Anthony Liguori
{
888 bb9e3216 Anthony Liguori
    uint32_t mode;
889 bb9e3216 Anthony Liguori
890 bb9e3216 Anthony Liguori
    mode = stbuf->st_mode & 0777;
891 bb9e3216 Anthony Liguori
    if (S_ISDIR(stbuf->st_mode)) {
892 bb9e3216 Anthony Liguori
        mode |= P9_STAT_MODE_DIR;
893 bb9e3216 Anthony Liguori
    }
894 bb9e3216 Anthony Liguori
895 bb9e3216 Anthony Liguori
    if (dotu) {
896 bb9e3216 Anthony Liguori
        if (S_ISLNK(stbuf->st_mode)) {
897 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_SYMLINK;
898 bb9e3216 Anthony Liguori
        }
899 bb9e3216 Anthony Liguori
900 bb9e3216 Anthony Liguori
        if (S_ISSOCK(stbuf->st_mode)) {
901 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_SOCKET;
902 bb9e3216 Anthony Liguori
        }
903 bb9e3216 Anthony Liguori
904 bb9e3216 Anthony Liguori
        if (S_ISFIFO(stbuf->st_mode)) {
905 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_NAMED_PIPE;
906 bb9e3216 Anthony Liguori
        }
907 bb9e3216 Anthony Liguori
908 bb9e3216 Anthony Liguori
        if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
909 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_DEVICE;
910 bb9e3216 Anthony Liguori
        }
911 bb9e3216 Anthony Liguori
912 bb9e3216 Anthony Liguori
        if (stbuf->st_mode & S_ISUID) {
913 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_SETUID;
914 bb9e3216 Anthony Liguori
        }
915 bb9e3216 Anthony Liguori
916 bb9e3216 Anthony Liguori
        if (stbuf->st_mode & S_ISGID) {
917 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_SETGID;
918 bb9e3216 Anthony Liguori
        }
919 bb9e3216 Anthony Liguori
920 bb9e3216 Anthony Liguori
        if (stbuf->st_mode & S_ISVTX) {
921 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_SETVTX;
922 bb9e3216 Anthony Liguori
        }
923 bb9e3216 Anthony Liguori
    }
924 bb9e3216 Anthony Liguori
925 bb9e3216 Anthony Liguori
    return mode;
926 bb9e3216 Anthony Liguori
}
927 bb9e3216 Anthony Liguori
928 bb9e3216 Anthony Liguori
static int stat_to_v9stat(V9fsState *s, V9fsString *name,
929 bb9e3216 Anthony Liguori
                            const struct stat *stbuf,
930 bb9e3216 Anthony Liguori
                            V9fsStat *v9stat)
931 bb9e3216 Anthony Liguori
{
932 bb9e3216 Anthony Liguori
    int err;
933 bb9e3216 Anthony Liguori
    const char *str;
934 bb9e3216 Anthony Liguori
935 bb9e3216 Anthony Liguori
    memset(v9stat, 0, sizeof(*v9stat));
936 bb9e3216 Anthony Liguori
937 bb9e3216 Anthony Liguori
    stat_to_qid(stbuf, &v9stat->qid);
938 bb9e3216 Anthony Liguori
    v9stat->mode = stat_to_v9mode(stbuf);
939 bb9e3216 Anthony Liguori
    v9stat->atime = stbuf->st_atime;
940 bb9e3216 Anthony Liguori
    v9stat->mtime = stbuf->st_mtime;
941 bb9e3216 Anthony Liguori
    v9stat->length = stbuf->st_size;
942 bb9e3216 Anthony Liguori
943 bb9e3216 Anthony Liguori
    v9fs_string_null(&v9stat->uid);
944 bb9e3216 Anthony Liguori
    v9fs_string_null(&v9stat->gid);
945 bb9e3216 Anthony Liguori
    v9fs_string_null(&v9stat->muid);
946 bb9e3216 Anthony Liguori
947 bb9e3216 Anthony Liguori
    if (dotu) {
948 bb9e3216 Anthony Liguori
        v9stat->n_uid = stbuf->st_uid;
949 bb9e3216 Anthony Liguori
        v9stat->n_gid = stbuf->st_gid;
950 bb9e3216 Anthony Liguori
        v9stat->n_muid = 0;
951 bb9e3216 Anthony Liguori
952 bb9e3216 Anthony Liguori
        v9fs_string_null(&v9stat->extension);
953 bb9e3216 Anthony Liguori
954 bb9e3216 Anthony Liguori
        if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
955 bb9e3216 Anthony Liguori
            err = v9fs_do_readlink(s, name, &v9stat->extension);
956 bb9e3216 Anthony Liguori
            if (err == -1) {
957 bb9e3216 Anthony Liguori
                err = -errno;
958 bb9e3216 Anthony Liguori
                return err;
959 bb9e3216 Anthony Liguori
            }
960 bb9e3216 Anthony Liguori
            v9stat->extension.data[err] = 0;
961 bb9e3216 Anthony Liguori
            v9stat->extension.size = err;
962 bb9e3216 Anthony Liguori
        } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
963 bb9e3216 Anthony Liguori
            v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
964 bb9e3216 Anthony Liguori
                    S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
965 bb9e3216 Anthony Liguori
                    major(stbuf->st_rdev), minor(stbuf->st_rdev));
966 bb9e3216 Anthony Liguori
        } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
967 bb9e3216 Anthony Liguori
            v9fs_string_sprintf(&v9stat->extension, "%s %u",
968 bb9e3216 Anthony Liguori
                    "HARDLINKCOUNT", stbuf->st_nlink);
969 bb9e3216 Anthony Liguori
        }
970 bb9e3216 Anthony Liguori
    }
971 bb9e3216 Anthony Liguori
972 bb9e3216 Anthony Liguori
    str = strrchr(name->data, '/');
973 bb9e3216 Anthony Liguori
    if (str) {
974 bb9e3216 Anthony Liguori
        str += 1;
975 bb9e3216 Anthony Liguori
    } else {
976 bb9e3216 Anthony Liguori
        str = name->data;
977 bb9e3216 Anthony Liguori
    }
978 bb9e3216 Anthony Liguori
979 bb9e3216 Anthony Liguori
    v9fs_string_sprintf(&v9stat->name, "%s", str);
980 bb9e3216 Anthony Liguori
981 bb9e3216 Anthony Liguori
    v9stat->size = 61 +
982 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->name) +
983 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->uid) +
984 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->gid) +
985 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->muid) +
986 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->extension);
987 bb9e3216 Anthony Liguori
    return 0;
988 bb9e3216 Anthony Liguori
}
989 bb9e3216 Anthony Liguori
990 00ede4c2 Sripathi Kodi
#define P9_STATS_MODE          0x00000001ULL
991 00ede4c2 Sripathi Kodi
#define P9_STATS_NLINK         0x00000002ULL
992 00ede4c2 Sripathi Kodi
#define P9_STATS_UID           0x00000004ULL
993 00ede4c2 Sripathi Kodi
#define P9_STATS_GID           0x00000008ULL
994 00ede4c2 Sripathi Kodi
#define P9_STATS_RDEV          0x00000010ULL
995 00ede4c2 Sripathi Kodi
#define P9_STATS_ATIME         0x00000020ULL
996 00ede4c2 Sripathi Kodi
#define P9_STATS_MTIME         0x00000040ULL
997 00ede4c2 Sripathi Kodi
#define P9_STATS_CTIME         0x00000080ULL
998 00ede4c2 Sripathi Kodi
#define P9_STATS_INO           0x00000100ULL
999 00ede4c2 Sripathi Kodi
#define P9_STATS_SIZE          0x00000200ULL
1000 00ede4c2 Sripathi Kodi
#define P9_STATS_BLOCKS        0x00000400ULL
1001 00ede4c2 Sripathi Kodi
1002 00ede4c2 Sripathi Kodi
#define P9_STATS_BTIME         0x00000800ULL
1003 00ede4c2 Sripathi Kodi
#define P9_STATS_GEN           0x00001000ULL
1004 00ede4c2 Sripathi Kodi
#define P9_STATS_DATA_VERSION  0x00002000ULL
1005 00ede4c2 Sripathi Kodi
1006 00ede4c2 Sripathi Kodi
#define P9_STATS_BASIC         0x000007ffULL /* Mask for fields up to BLOCKS */
1007 00ede4c2 Sripathi Kodi
#define P9_STATS_ALL           0x00003fffULL /* Mask for All fields above */
1008 00ede4c2 Sripathi Kodi
1009 00ede4c2 Sripathi Kodi
1010 00ede4c2 Sripathi Kodi
static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
1011 00ede4c2 Sripathi Kodi
                            V9fsStatDotl *v9lstat)
1012 00ede4c2 Sripathi Kodi
{
1013 00ede4c2 Sripathi Kodi
    memset(v9lstat, 0, sizeof(*v9lstat));
1014 00ede4c2 Sripathi Kodi
1015 00ede4c2 Sripathi Kodi
    v9lstat->st_mode = stbuf->st_mode;
1016 00ede4c2 Sripathi Kodi
    v9lstat->st_nlink = stbuf->st_nlink;
1017 00ede4c2 Sripathi Kodi
    v9lstat->st_uid = stbuf->st_uid;
1018 00ede4c2 Sripathi Kodi
    v9lstat->st_gid = stbuf->st_gid;
1019 00ede4c2 Sripathi Kodi
    v9lstat->st_rdev = stbuf->st_rdev;
1020 00ede4c2 Sripathi Kodi
    v9lstat->st_size = stbuf->st_size;
1021 00ede4c2 Sripathi Kodi
    v9lstat->st_blksize = stbuf->st_blksize;
1022 00ede4c2 Sripathi Kodi
    v9lstat->st_blocks = stbuf->st_blocks;
1023 00ede4c2 Sripathi Kodi
    v9lstat->st_atime_sec = stbuf->st_atime;
1024 00ede4c2 Sripathi Kodi
    v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1025 00ede4c2 Sripathi Kodi
    v9lstat->st_mtime_sec = stbuf->st_mtime;
1026 00ede4c2 Sripathi Kodi
    v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
1027 00ede4c2 Sripathi Kodi
    v9lstat->st_ctime_sec = stbuf->st_ctime;
1028 00ede4c2 Sripathi Kodi
    v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1029 00ede4c2 Sripathi Kodi
    /* Currently we only support BASIC fields in stat */
1030 00ede4c2 Sripathi Kodi
    v9lstat->st_result_mask = P9_STATS_BASIC;
1031 00ede4c2 Sripathi Kodi
1032 00ede4c2 Sripathi Kodi
    stat_to_qid(stbuf, &v9lstat->qid);
1033 00ede4c2 Sripathi Kodi
}
1034 00ede4c2 Sripathi Kodi
1035 1f5a89bf Anthony Liguori
static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
1036 1f5a89bf Anthony Liguori
{
1037 1f5a89bf Anthony Liguori
    while (len && *iovcnt) {
1038 1f5a89bf Anthony Liguori
        if (len < sg->iov_len) {
1039 1f5a89bf Anthony Liguori
            sg->iov_len -= len;
1040 1f5a89bf Anthony Liguori
            sg->iov_base += len;
1041 1f5a89bf Anthony Liguori
            len = 0;
1042 1f5a89bf Anthony Liguori
        } else {
1043 1f5a89bf Anthony Liguori
            len -= sg->iov_len;
1044 1f5a89bf Anthony Liguori
            sg++;
1045 1f5a89bf Anthony Liguori
            *iovcnt -= 1;
1046 1f5a89bf Anthony Liguori
        }
1047 1f5a89bf Anthony Liguori
    }
1048 1f5a89bf Anthony Liguori
1049 1f5a89bf Anthony Liguori
    return sg;
1050 1f5a89bf Anthony Liguori
}
1051 1f5a89bf Anthony Liguori
1052 1f5a89bf Anthony Liguori
static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
1053 1f5a89bf Anthony Liguori
{
1054 1f5a89bf Anthony Liguori
    int i;
1055 1f5a89bf Anthony Liguori
    int total = 0;
1056 1f5a89bf Anthony Liguori
1057 1f5a89bf Anthony Liguori
    for (i = 0; i < *cnt; i++) {
1058 1f5a89bf Anthony Liguori
        if ((total + sg[i].iov_len) > cap) {
1059 1f5a89bf Anthony Liguori
            sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
1060 1f5a89bf Anthony Liguori
            i++;
1061 1f5a89bf Anthony Liguori
            break;
1062 1f5a89bf Anthony Liguori
        }
1063 1f5a89bf Anthony Liguori
        total += sg[i].iov_len;
1064 1f5a89bf Anthony Liguori
    }
1065 1f5a89bf Anthony Liguori
1066 1f5a89bf Anthony Liguori
    *cnt = i;
1067 1f5a89bf Anthony Liguori
1068 1f5a89bf Anthony Liguori
    return sg;
1069 1f5a89bf Anthony Liguori
}
1070 1f5a89bf Anthony Liguori
1071 1f5a89bf Anthony Liguori
static void print_sg(struct iovec *sg, int cnt)
1072 1f5a89bf Anthony Liguori
{
1073 1f5a89bf Anthony Liguori
    int i;
1074 1f5a89bf Anthony Liguori
1075 1f5a89bf Anthony Liguori
    printf("sg[%d]: {", cnt);
1076 1f5a89bf Anthony Liguori
    for (i = 0; i < cnt; i++) {
1077 1f5a89bf Anthony Liguori
        if (i) {
1078 1f5a89bf Anthony Liguori
            printf(", ");
1079 1f5a89bf Anthony Liguori
        }
1080 1f5a89bf Anthony Liguori
        printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1081 1f5a89bf Anthony Liguori
    }
1082 1f5a89bf Anthony Liguori
    printf("}\n");
1083 1f5a89bf Anthony Liguori
}
1084 1f5a89bf Anthony Liguori
1085 8cf89e00 Anthony Liguori
static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
1086 8cf89e00 Anthony Liguori
{
1087 8cf89e00 Anthony Liguori
    V9fsString str;
1088 8cf89e00 Anthony Liguori
    v9fs_string_init(&str);
1089 8cf89e00 Anthony Liguori
    v9fs_string_copy(&str, dst);
1090 8cf89e00 Anthony Liguori
    v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
1091 8cf89e00 Anthony Liguori
    v9fs_string_free(&str);
1092 8cf89e00 Anthony Liguori
}
1093 8cf89e00 Anthony Liguori
1094 9f107513 Anthony Liguori
static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
1095 9f107513 Anthony Liguori
{
1096 92c1ad03 Anthony Liguori
    V9fsString version;
1097 92c1ad03 Anthony Liguori
    size_t offset = 7;
1098 92c1ad03 Anthony Liguori
1099 5e94c103 M. Mohan Kumar
    pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1100 92c1ad03 Anthony Liguori
1101 84151514 M. Mohan Kumar
    if (!strcmp(version.data, "9P2000.u")) {
1102 84151514 M. Mohan Kumar
        s->proto_version = V9FS_PROTO_2000U;
1103 84151514 M. Mohan Kumar
    } else if (!strcmp(version.data, "9P2000.L")) {
1104 84151514 M. Mohan Kumar
        s->proto_version = V9FS_PROTO_2000L;
1105 84151514 M. Mohan Kumar
    } else {
1106 92c1ad03 Anthony Liguori
        v9fs_string_sprintf(&version, "unknown");
1107 9f107513 Anthony Liguori
    }
1108 92c1ad03 Anthony Liguori
1109 5e94c103 M. Mohan Kumar
    offset += pdu_marshal(pdu, offset, "ds", s->msize, &version);
1110 92c1ad03 Anthony Liguori
    complete_pdu(s, pdu, offset);
1111 92c1ad03 Anthony Liguori
1112 92c1ad03 Anthony Liguori
    v9fs_string_free(&version);
1113 9f107513 Anthony Liguori
}
1114 9f107513 Anthony Liguori
1115 9f107513 Anthony Liguori
static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
1116 9f107513 Anthony Liguori
{
1117 955efc47 Anthony Liguori
    int32_t fid, afid, n_uname;
1118 955efc47 Anthony Liguori
    V9fsString uname, aname;
1119 955efc47 Anthony Liguori
    V9fsFidState *fidp;
1120 955efc47 Anthony Liguori
    V9fsQID qid;
1121 955efc47 Anthony Liguori
    size_t offset = 7;
1122 955efc47 Anthony Liguori
    ssize_t err;
1123 955efc47 Anthony Liguori
1124 955efc47 Anthony Liguori
    pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1125 955efc47 Anthony Liguori
1126 955efc47 Anthony Liguori
    fidp = alloc_fid(s, fid);
1127 955efc47 Anthony Liguori
    if (fidp == NULL) {
1128 955efc47 Anthony Liguori
        err = -EINVAL;
1129 955efc47 Anthony Liguori
        goto out;
1130 9f107513 Anthony Liguori
    }
1131 955efc47 Anthony Liguori
1132 955efc47 Anthony Liguori
    fidp->uid = n_uname;
1133 955efc47 Anthony Liguori
1134 955efc47 Anthony Liguori
    v9fs_string_sprintf(&fidp->path, "%s", "/");
1135 955efc47 Anthony Liguori
    err = fid_to_qid(s, fidp, &qid);
1136 955efc47 Anthony Liguori
    if (err) {
1137 955efc47 Anthony Liguori
        err = -EINVAL;
1138 955efc47 Anthony Liguori
        free_fid(s, fid);
1139 955efc47 Anthony Liguori
        goto out;
1140 955efc47 Anthony Liguori
    }
1141 955efc47 Anthony Liguori
1142 955efc47 Anthony Liguori
    offset += pdu_marshal(pdu, offset, "Q", &qid);
1143 955efc47 Anthony Liguori
1144 955efc47 Anthony Liguori
    err = offset;
1145 955efc47 Anthony Liguori
out:
1146 955efc47 Anthony Liguori
    complete_pdu(s, pdu, err);
1147 955efc47 Anthony Liguori
    v9fs_string_free(&uname);
1148 955efc47 Anthony Liguori
    v9fs_string_free(&aname);
1149 9f107513 Anthony Liguori
}
1150 9f107513 Anthony Liguori
1151 4da7d3fa Anthony Liguori
static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1152 4da7d3fa Anthony Liguori
{
1153 4da7d3fa Anthony Liguori
    if (err == -1) {
1154 4da7d3fa Anthony Liguori
        err = -errno;
1155 4da7d3fa Anthony Liguori
        goto out;
1156 4da7d3fa Anthony Liguori
    }
1157 4da7d3fa Anthony Liguori
1158 4da7d3fa Anthony Liguori
    err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1159 4da7d3fa Anthony Liguori
    if (err) {
1160 4da7d3fa Anthony Liguori
        goto out;
1161 4da7d3fa Anthony Liguori
    }
1162 4da7d3fa Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1163 4da7d3fa Anthony Liguori
    err = vs->offset;
1164 4da7d3fa Anthony Liguori
1165 4da7d3fa Anthony Liguori
out:
1166 4da7d3fa Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1167 4da7d3fa Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
1168 4da7d3fa Anthony Liguori
    qemu_free(vs);
1169 4da7d3fa Anthony Liguori
}
1170 4da7d3fa Anthony Liguori
1171 9f107513 Anthony Liguori
static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
1172 9f107513 Anthony Liguori
{
1173 4da7d3fa Anthony Liguori
    int32_t fid;
1174 4da7d3fa Anthony Liguori
    V9fsStatState *vs;
1175 4da7d3fa Anthony Liguori
    ssize_t err = 0;
1176 4da7d3fa Anthony Liguori
1177 4da7d3fa Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
1178 4da7d3fa Anthony Liguori
    vs->pdu = pdu;
1179 4da7d3fa Anthony Liguori
    vs->offset = 7;
1180 4da7d3fa Anthony Liguori
1181 4da7d3fa Anthony Liguori
    memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1182 4da7d3fa Anthony Liguori
1183 4da7d3fa Anthony Liguori
    pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1184 4da7d3fa Anthony Liguori
1185 4da7d3fa Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
1186 4da7d3fa Anthony Liguori
    if (vs->fidp == NULL) {
1187 4da7d3fa Anthony Liguori
        err = -ENOENT;
1188 4da7d3fa Anthony Liguori
        goto out;
1189 9f107513 Anthony Liguori
    }
1190 4da7d3fa Anthony Liguori
1191 4da7d3fa Anthony Liguori
    err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1192 4da7d3fa Anthony Liguori
    v9fs_stat_post_lstat(s, vs, err);
1193 4da7d3fa Anthony Liguori
    return;
1194 4da7d3fa Anthony Liguori
1195 4da7d3fa Anthony Liguori
out:
1196 4da7d3fa Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1197 4da7d3fa Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
1198 4da7d3fa Anthony Liguori
    qemu_free(vs);
1199 9f107513 Anthony Liguori
}
1200 9f107513 Anthony Liguori
1201 00ede4c2 Sripathi Kodi
static void v9fs_getattr_post_lstat(V9fsState *s, V9fsStatStateDotl *vs,
1202 00ede4c2 Sripathi Kodi
                                                                int err)
1203 00ede4c2 Sripathi Kodi
{
1204 00ede4c2 Sripathi Kodi
    if (err == -1) {
1205 00ede4c2 Sripathi Kodi
        err = -errno;
1206 00ede4c2 Sripathi Kodi
        goto out;
1207 00ede4c2 Sripathi Kodi
    }
1208 00ede4c2 Sripathi Kodi
1209 00ede4c2 Sripathi Kodi
    stat_to_v9stat_dotl(s, &vs->stbuf, &vs->v9stat_dotl);
1210 00ede4c2 Sripathi Kodi
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "A", &vs->v9stat_dotl);
1211 00ede4c2 Sripathi Kodi
    err = vs->offset;
1212 00ede4c2 Sripathi Kodi
1213 00ede4c2 Sripathi Kodi
out:
1214 00ede4c2 Sripathi Kodi
    complete_pdu(s, vs->pdu, err);
1215 00ede4c2 Sripathi Kodi
    qemu_free(vs);
1216 00ede4c2 Sripathi Kodi
}
1217 00ede4c2 Sripathi Kodi
1218 00ede4c2 Sripathi Kodi
static void v9fs_getattr(V9fsState *s, V9fsPDU *pdu)
1219 00ede4c2 Sripathi Kodi
{
1220 00ede4c2 Sripathi Kodi
    int32_t fid;
1221 00ede4c2 Sripathi Kodi
    V9fsStatStateDotl *vs;
1222 00ede4c2 Sripathi Kodi
    ssize_t err = 0;
1223 00ede4c2 Sripathi Kodi
    V9fsFidState *fidp;
1224 00ede4c2 Sripathi Kodi
    uint64_t request_mask;
1225 00ede4c2 Sripathi Kodi
1226 00ede4c2 Sripathi Kodi
    vs = qemu_malloc(sizeof(*vs));
1227 00ede4c2 Sripathi Kodi
    vs->pdu = pdu;
1228 00ede4c2 Sripathi Kodi
    vs->offset = 7;
1229 00ede4c2 Sripathi Kodi
1230 00ede4c2 Sripathi Kodi
    memset(&vs->v9stat_dotl, 0, sizeof(vs->v9stat_dotl));
1231 00ede4c2 Sripathi Kodi
1232 00ede4c2 Sripathi Kodi
    pdu_unmarshal(vs->pdu, vs->offset, "dq", &fid, &request_mask);
1233 00ede4c2 Sripathi Kodi
1234 00ede4c2 Sripathi Kodi
    fidp = lookup_fid(s, fid);
1235 00ede4c2 Sripathi Kodi
    if (fidp == NULL) {
1236 00ede4c2 Sripathi Kodi
        err = -ENOENT;
1237 00ede4c2 Sripathi Kodi
        goto out;
1238 00ede4c2 Sripathi Kodi
    }
1239 00ede4c2 Sripathi Kodi
1240 00ede4c2 Sripathi Kodi
    /* Currently we only support BASIC fields in stat, so there is no
1241 00ede4c2 Sripathi Kodi
     * need to look at request_mask.
1242 00ede4c2 Sripathi Kodi
     */
1243 00ede4c2 Sripathi Kodi
    err = v9fs_do_lstat(s, &fidp->path, &vs->stbuf);
1244 00ede4c2 Sripathi Kodi
    v9fs_getattr_post_lstat(s, vs, err);
1245 00ede4c2 Sripathi Kodi
    return;
1246 00ede4c2 Sripathi Kodi
1247 00ede4c2 Sripathi Kodi
out:
1248 00ede4c2 Sripathi Kodi
    complete_pdu(s, vs->pdu, err);
1249 00ede4c2 Sripathi Kodi
    qemu_free(vs);
1250 00ede4c2 Sripathi Kodi
}
1251 00ede4c2 Sripathi Kodi
1252 c79ce737 Sripathi Kodi
/* From Linux kernel code */
1253 c79ce737 Sripathi Kodi
#define ATTR_MODE    (1 << 0)
1254 c79ce737 Sripathi Kodi
#define ATTR_UID     (1 << 1)
1255 c79ce737 Sripathi Kodi
#define ATTR_GID     (1 << 2)
1256 c79ce737 Sripathi Kodi
#define ATTR_SIZE    (1 << 3)
1257 c79ce737 Sripathi Kodi
#define ATTR_ATIME   (1 << 4)
1258 c79ce737 Sripathi Kodi
#define ATTR_MTIME   (1 << 5)
1259 c79ce737 Sripathi Kodi
#define ATTR_CTIME   (1 << 6)
1260 c79ce737 Sripathi Kodi
#define ATTR_MASK    127
1261 c79ce737 Sripathi Kodi
#define ATTR_ATIME_SET  (1 << 7)
1262 c79ce737 Sripathi Kodi
#define ATTR_MTIME_SET  (1 << 8)
1263 c79ce737 Sripathi Kodi
1264 c79ce737 Sripathi Kodi
static void v9fs_setattr_post_truncate(V9fsState *s, V9fsSetattrState *vs,
1265 c79ce737 Sripathi Kodi
                                                                  int err)
1266 c79ce737 Sripathi Kodi
{
1267 c79ce737 Sripathi Kodi
    if (err == -1) {
1268 c79ce737 Sripathi Kodi
        err = -errno;
1269 c79ce737 Sripathi Kodi
        goto out;
1270 c79ce737 Sripathi Kodi
    }
1271 c79ce737 Sripathi Kodi
    err = vs->offset;
1272 c79ce737 Sripathi Kodi
1273 c79ce737 Sripathi Kodi
out:
1274 c79ce737 Sripathi Kodi
    complete_pdu(s, vs->pdu, err);
1275 c79ce737 Sripathi Kodi
    qemu_free(vs);
1276 c79ce737 Sripathi Kodi
}
1277 c79ce737 Sripathi Kodi
1278 c79ce737 Sripathi Kodi
static void v9fs_setattr_post_chown(V9fsState *s, V9fsSetattrState *vs, int err)
1279 c79ce737 Sripathi Kodi
{
1280 c79ce737 Sripathi Kodi
    if (err == -1) {
1281 c79ce737 Sripathi Kodi
        err = -errno;
1282 c79ce737 Sripathi Kodi
        goto out;
1283 c79ce737 Sripathi Kodi
    }
1284 c79ce737 Sripathi Kodi
1285 c79ce737 Sripathi Kodi
    if (vs->v9iattr.valid & (ATTR_SIZE)) {
1286 c79ce737 Sripathi Kodi
        err = v9fs_do_truncate(s, &vs->fidp->path, vs->v9iattr.size);
1287 c79ce737 Sripathi Kodi
    }
1288 c79ce737 Sripathi Kodi
    v9fs_setattr_post_truncate(s, vs, err);
1289 c79ce737 Sripathi Kodi
    return;
1290 c79ce737 Sripathi Kodi
1291 c79ce737 Sripathi Kodi
out:
1292 c79ce737 Sripathi Kodi
    complete_pdu(s, vs->pdu, err);
1293 c79ce737 Sripathi Kodi
    qemu_free(vs);
1294 c79ce737 Sripathi Kodi
}
1295 c79ce737 Sripathi Kodi
1296 c79ce737 Sripathi Kodi
static void v9fs_setattr_post_utimensat(V9fsState *s, V9fsSetattrState *vs,
1297 c79ce737 Sripathi Kodi
                                                                   int err)
1298 c79ce737 Sripathi Kodi
{
1299 c79ce737 Sripathi Kodi
    if (err == -1) {
1300 c79ce737 Sripathi Kodi
        err = -errno;
1301 c79ce737 Sripathi Kodi
        goto out;
1302 c79ce737 Sripathi Kodi
    }
1303 c79ce737 Sripathi Kodi
1304 c79ce737 Sripathi Kodi
    /* If the only valid entry in iattr is ctime we can call
1305 c79ce737 Sripathi Kodi
     * chown(-1,-1) to update the ctime of the file
1306 c79ce737 Sripathi Kodi
     */
1307 c79ce737 Sripathi Kodi
    if ((vs->v9iattr.valid & (ATTR_UID | ATTR_GID)) ||
1308 c79ce737 Sripathi Kodi
            ((vs->v9iattr.valid & ATTR_CTIME)
1309 c79ce737 Sripathi Kodi
            && !((vs->v9iattr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
1310 c79ce737 Sripathi Kodi
        if (!(vs->v9iattr.valid & ATTR_UID)) {
1311 c79ce737 Sripathi Kodi
            vs->v9iattr.uid = -1;
1312 c79ce737 Sripathi Kodi
        }
1313 c79ce737 Sripathi Kodi
        if (!(vs->v9iattr.valid & ATTR_GID)) {
1314 c79ce737 Sripathi Kodi
            vs->v9iattr.gid = -1;
1315 c79ce737 Sripathi Kodi
        }
1316 c79ce737 Sripathi Kodi
        err = v9fs_do_chown(s, &vs->fidp->path, vs->v9iattr.uid,
1317 c79ce737 Sripathi Kodi
                                                vs->v9iattr.gid);
1318 c79ce737 Sripathi Kodi
    }
1319 c79ce737 Sripathi Kodi
    v9fs_setattr_post_chown(s, vs, err);
1320 c79ce737 Sripathi Kodi
    return;
1321 c79ce737 Sripathi Kodi
1322 c79ce737 Sripathi Kodi
out:
1323 c79ce737 Sripathi Kodi
    complete_pdu(s, vs->pdu, err);
1324 c79ce737 Sripathi Kodi
    qemu_free(vs);
1325 c79ce737 Sripathi Kodi
}
1326 c79ce737 Sripathi Kodi
1327 c79ce737 Sripathi Kodi
static void v9fs_setattr_post_chmod(V9fsState *s, V9fsSetattrState *vs, int err)
1328 c79ce737 Sripathi Kodi
{
1329 c79ce737 Sripathi Kodi
    if (err == -1) {
1330 c79ce737 Sripathi Kodi
        err = -errno;
1331 c79ce737 Sripathi Kodi
        goto out;
1332 c79ce737 Sripathi Kodi
    }
1333 c79ce737 Sripathi Kodi
1334 c79ce737 Sripathi Kodi
    if (vs->v9iattr.valid & (ATTR_ATIME | ATTR_MTIME)) {
1335 c79ce737 Sripathi Kodi
        struct timespec times[2];
1336 c79ce737 Sripathi Kodi
        if (vs->v9iattr.valid & ATTR_ATIME) {
1337 c79ce737 Sripathi Kodi
            if (vs->v9iattr.valid & ATTR_ATIME_SET) {
1338 c79ce737 Sripathi Kodi
                times[0].tv_sec = vs->v9iattr.atime_sec;
1339 c79ce737 Sripathi Kodi
                times[0].tv_nsec = vs->v9iattr.atime_nsec;
1340 c79ce737 Sripathi Kodi
            } else {
1341 c79ce737 Sripathi Kodi
                times[0].tv_nsec = UTIME_NOW;
1342 c79ce737 Sripathi Kodi
            }
1343 c79ce737 Sripathi Kodi
        } else {
1344 c79ce737 Sripathi Kodi
            times[0].tv_nsec = UTIME_OMIT;
1345 c79ce737 Sripathi Kodi
        }
1346 c79ce737 Sripathi Kodi
1347 c79ce737 Sripathi Kodi
        if (vs->v9iattr.valid & ATTR_MTIME) {
1348 c79ce737 Sripathi Kodi
            if (vs->v9iattr.valid & ATTR_MTIME_SET) {
1349 c79ce737 Sripathi Kodi
                times[1].tv_sec = vs->v9iattr.mtime_sec;
1350 c79ce737 Sripathi Kodi
                times[1].tv_nsec = vs->v9iattr.mtime_nsec;
1351 c79ce737 Sripathi Kodi
            } else {
1352 c79ce737 Sripathi Kodi
                times[1].tv_nsec = UTIME_NOW;
1353 c79ce737 Sripathi Kodi
            }
1354 c79ce737 Sripathi Kodi
        } else {
1355 c79ce737 Sripathi Kodi
            times[1].tv_nsec = UTIME_OMIT;
1356 c79ce737 Sripathi Kodi
        }
1357 c79ce737 Sripathi Kodi
        err = v9fs_do_utimensat(s, &vs->fidp->path, times);
1358 c79ce737 Sripathi Kodi
    }
1359 c79ce737 Sripathi Kodi
    v9fs_setattr_post_utimensat(s, vs, err);
1360 c79ce737 Sripathi Kodi
    return;
1361 c79ce737 Sripathi Kodi
1362 c79ce737 Sripathi Kodi
out:
1363 c79ce737 Sripathi Kodi
    complete_pdu(s, vs->pdu, err);
1364 c79ce737 Sripathi Kodi
    qemu_free(vs);
1365 c79ce737 Sripathi Kodi
}
1366 c79ce737 Sripathi Kodi
1367 c79ce737 Sripathi Kodi
static void v9fs_setattr(V9fsState *s, V9fsPDU *pdu)
1368 c79ce737 Sripathi Kodi
{
1369 c79ce737 Sripathi Kodi
    int32_t fid;
1370 c79ce737 Sripathi Kodi
    V9fsSetattrState *vs;
1371 c79ce737 Sripathi Kodi
    int err = 0;
1372 c79ce737 Sripathi Kodi
1373 c79ce737 Sripathi Kodi
    vs = qemu_malloc(sizeof(*vs));
1374 c79ce737 Sripathi Kodi
    vs->pdu = pdu;
1375 c79ce737 Sripathi Kodi
    vs->offset = 7;
1376 c79ce737 Sripathi Kodi
1377 c79ce737 Sripathi Kodi
    pdu_unmarshal(pdu, vs->offset, "dI", &fid, &vs->v9iattr);
1378 c79ce737 Sripathi Kodi
1379 c79ce737 Sripathi Kodi
    vs->fidp = lookup_fid(s, fid);
1380 c79ce737 Sripathi Kodi
    if (vs->fidp == NULL) {
1381 c79ce737 Sripathi Kodi
        err = -EINVAL;
1382 c79ce737 Sripathi Kodi
        goto out;
1383 c79ce737 Sripathi Kodi
    }
1384 c79ce737 Sripathi Kodi
1385 c79ce737 Sripathi Kodi
    if (vs->v9iattr.valid & ATTR_MODE) {
1386 c79ce737 Sripathi Kodi
        err = v9fs_do_chmod(s, &vs->fidp->path, vs->v9iattr.mode);
1387 c79ce737 Sripathi Kodi
    }
1388 c79ce737 Sripathi Kodi
1389 c79ce737 Sripathi Kodi
    v9fs_setattr_post_chmod(s, vs, err);
1390 c79ce737 Sripathi Kodi
    return;
1391 c79ce737 Sripathi Kodi
1392 c79ce737 Sripathi Kodi
out:
1393 c79ce737 Sripathi Kodi
    complete_pdu(s, vs->pdu, err);
1394 c79ce737 Sripathi Kodi
    qemu_free(vs);
1395 c79ce737 Sripathi Kodi
}
1396 c79ce737 Sripathi Kodi
1397 ff5e54c9 Anthony Liguori
static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1398 ff5e54c9 Anthony Liguori
{
1399 ff5e54c9 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1400 ff5e54c9 Anthony Liguori
1401 ff5e54c9 Anthony Liguori
    if (vs->nwnames) {
1402 ff5e54c9 Anthony Liguori
        for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1403 ff5e54c9 Anthony Liguori
            v9fs_string_free(&vs->wnames[vs->name_idx]);
1404 ff5e54c9 Anthony Liguori
        }
1405 ff5e54c9 Anthony Liguori
1406 ff5e54c9 Anthony Liguori
        qemu_free(vs->wnames);
1407 ff5e54c9 Anthony Liguori
        qemu_free(vs->qids);
1408 ff5e54c9 Anthony Liguori
    }
1409 ff5e54c9 Anthony Liguori
}
1410 ff5e54c9 Anthony Liguori
1411 ff5e54c9 Anthony Liguori
static void v9fs_walk_marshal(V9fsWalkState *vs)
1412 ff5e54c9 Anthony Liguori
{
1413 ff5e54c9 Anthony Liguori
    int i;
1414 ff5e54c9 Anthony Liguori
    vs->offset = 7;
1415 ff5e54c9 Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1416 ff5e54c9 Anthony Liguori
1417 ff5e54c9 Anthony Liguori
    for (i = 0; i < vs->nwnames; i++) {
1418 ff5e54c9 Anthony Liguori
        vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1419 ff5e54c9 Anthony Liguori
    }
1420 ff5e54c9 Anthony Liguori
}
1421 ff5e54c9 Anthony Liguori
1422 ff5e54c9 Anthony Liguori
static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1423 ff5e54c9 Anthony Liguori
                                                                int err)
1424 ff5e54c9 Anthony Liguori
{
1425 ff5e54c9 Anthony Liguori
    if (err == -1) {
1426 ff5e54c9 Anthony Liguori
        free_fid(s, vs->newfidp->fid);
1427 ff5e54c9 Anthony Liguori
        v9fs_string_free(&vs->path);
1428 ff5e54c9 Anthony Liguori
        err = -ENOENT;
1429 ff5e54c9 Anthony Liguori
        goto out;
1430 ff5e54c9 Anthony Liguori
    }
1431 ff5e54c9 Anthony Liguori
1432 ff5e54c9 Anthony Liguori
    stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1433 ff5e54c9 Anthony Liguori
1434 ff5e54c9 Anthony Liguori
    vs->name_idx++;
1435 ff5e54c9 Anthony Liguori
    if (vs->name_idx < vs->nwnames) {
1436 ff5e54c9 Anthony Liguori
        v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1437 ff5e54c9 Anthony Liguori
                                            vs->wnames[vs->name_idx].data);
1438 ff5e54c9 Anthony Liguori
        v9fs_string_copy(&vs->newfidp->path, &vs->path);
1439 ff5e54c9 Anthony Liguori
1440 ff5e54c9 Anthony Liguori
        err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1441 ff5e54c9 Anthony Liguori
        v9fs_walk_post_newfid_lstat(s, vs, err);
1442 ff5e54c9 Anthony Liguori
        return;
1443 ff5e54c9 Anthony Liguori
    }
1444 ff5e54c9 Anthony Liguori
1445 ff5e54c9 Anthony Liguori
    v9fs_string_free(&vs->path);
1446 ff5e54c9 Anthony Liguori
    v9fs_walk_marshal(vs);
1447 ff5e54c9 Anthony Liguori
    err = vs->offset;
1448 ff5e54c9 Anthony Liguori
out:
1449 ff5e54c9 Anthony Liguori
    v9fs_walk_complete(s, vs, err);
1450 ff5e54c9 Anthony Liguori
}
1451 ff5e54c9 Anthony Liguori
1452 ff5e54c9 Anthony Liguori
static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1453 ff5e54c9 Anthony Liguori
        int err)
1454 ff5e54c9 Anthony Liguori
{
1455 ff5e54c9 Anthony Liguori
    if (err == -1) {
1456 ff5e54c9 Anthony Liguori
        v9fs_string_free(&vs->path);
1457 ff5e54c9 Anthony Liguori
        err = -ENOENT;
1458 ff5e54c9 Anthony Liguori
        goto out;
1459 ff5e54c9 Anthony Liguori
    }
1460 ff5e54c9 Anthony Liguori
1461 ff5e54c9 Anthony Liguori
    stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1462 ff5e54c9 Anthony Liguori
    vs->name_idx++;
1463 ff5e54c9 Anthony Liguori
    if (vs->name_idx < vs->nwnames) {
1464 ff5e54c9 Anthony Liguori
1465 ff5e54c9 Anthony Liguori
        v9fs_string_sprintf(&vs->path, "%s/%s",
1466 ff5e54c9 Anthony Liguori
                vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1467 ff5e54c9 Anthony Liguori
        v9fs_string_copy(&vs->fidp->path, &vs->path);
1468 ff5e54c9 Anthony Liguori
1469 ff5e54c9 Anthony Liguori
        err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1470 ff5e54c9 Anthony Liguori
        v9fs_walk_post_oldfid_lstat(s, vs, err);
1471 ff5e54c9 Anthony Liguori
        return;
1472 ff5e54c9 Anthony Liguori
    }
1473 ff5e54c9 Anthony Liguori
1474 ff5e54c9 Anthony Liguori
    v9fs_string_free(&vs->path);
1475 ff5e54c9 Anthony Liguori
    v9fs_walk_marshal(vs);
1476 ff5e54c9 Anthony Liguori
    err = vs->offset;
1477 ff5e54c9 Anthony Liguori
out:
1478 ff5e54c9 Anthony Liguori
    v9fs_walk_complete(s, vs, err);
1479 ff5e54c9 Anthony Liguori
}
1480 ff5e54c9 Anthony Liguori
1481 9f107513 Anthony Liguori
static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1482 9f107513 Anthony Liguori
{
1483 ff5e54c9 Anthony Liguori
    int32_t fid, newfid;
1484 ff5e54c9 Anthony Liguori
    V9fsWalkState *vs;
1485 ff5e54c9 Anthony Liguori
    int err = 0;
1486 ff5e54c9 Anthony Liguori
    int i;
1487 ff5e54c9 Anthony Liguori
1488 ff5e54c9 Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
1489 ff5e54c9 Anthony Liguori
    vs->pdu = pdu;
1490 ff5e54c9 Anthony Liguori
    vs->wnames = NULL;
1491 ff5e54c9 Anthony Liguori
    vs->qids = NULL;
1492 ff5e54c9 Anthony Liguori
    vs->offset = 7;
1493 ff5e54c9 Anthony Liguori
1494 ff5e54c9 Anthony Liguori
    vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1495 ff5e54c9 Anthony Liguori
                                            &newfid, &vs->nwnames);
1496 ff5e54c9 Anthony Liguori
1497 ff5e54c9 Anthony Liguori
    if (vs->nwnames) {
1498 ff5e54c9 Anthony Liguori
        vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1499 ff5e54c9 Anthony Liguori
1500 ff5e54c9 Anthony Liguori
        vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1501 ff5e54c9 Anthony Liguori
1502 ff5e54c9 Anthony Liguori
        for (i = 0; i < vs->nwnames; i++) {
1503 ff5e54c9 Anthony Liguori
            vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1504 ff5e54c9 Anthony Liguori
                                            &vs->wnames[i]);
1505 ff5e54c9 Anthony Liguori
        }
1506 ff5e54c9 Anthony Liguori
    }
1507 ff5e54c9 Anthony Liguori
1508 ff5e54c9 Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
1509 ff5e54c9 Anthony Liguori
    if (vs->fidp == NULL) {
1510 ff5e54c9 Anthony Liguori
        err = -ENOENT;
1511 ff5e54c9 Anthony Liguori
        goto out;
1512 ff5e54c9 Anthony Liguori
    }
1513 ff5e54c9 Anthony Liguori
1514 ff5e54c9 Anthony Liguori
    /* FIXME: is this really valid? */
1515 ff5e54c9 Anthony Liguori
    if (fid == newfid) {
1516 ff5e54c9 Anthony Liguori
1517 ff5e54c9 Anthony Liguori
        BUG_ON(vs->fidp->fd != -1);
1518 ff5e54c9 Anthony Liguori
        BUG_ON(vs->fidp->dir);
1519 ff5e54c9 Anthony Liguori
        v9fs_string_init(&vs->path);
1520 ff5e54c9 Anthony Liguori
        vs->name_idx = 0;
1521 ff5e54c9 Anthony Liguori
1522 ff5e54c9 Anthony Liguori
        if (vs->name_idx < vs->nwnames) {
1523 ff5e54c9 Anthony Liguori
            v9fs_string_sprintf(&vs->path, "%s/%s",
1524 ff5e54c9 Anthony Liguori
                vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1525 ff5e54c9 Anthony Liguori
            v9fs_string_copy(&vs->fidp->path, &vs->path);
1526 ff5e54c9 Anthony Liguori
1527 ff5e54c9 Anthony Liguori
            err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1528 ff5e54c9 Anthony Liguori
            v9fs_walk_post_oldfid_lstat(s, vs, err);
1529 ff5e54c9 Anthony Liguori
            return;
1530 ff5e54c9 Anthony Liguori
        }
1531 ff5e54c9 Anthony Liguori
    } else {
1532 ff5e54c9 Anthony Liguori
        vs->newfidp = alloc_fid(s, newfid);
1533 ff5e54c9 Anthony Liguori
        if (vs->newfidp == NULL) {
1534 ff5e54c9 Anthony Liguori
            err = -EINVAL;
1535 ff5e54c9 Anthony Liguori
            goto out;
1536 ff5e54c9 Anthony Liguori
        }
1537 ff5e54c9 Anthony Liguori
1538 ff5e54c9 Anthony Liguori
        vs->newfidp->uid = vs->fidp->uid;
1539 ff5e54c9 Anthony Liguori
        v9fs_string_init(&vs->path);
1540 ff5e54c9 Anthony Liguori
        vs->name_idx = 0;
1541 ff5e54c9 Anthony Liguori
        v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1542 ff5e54c9 Anthony Liguori
1543 ff5e54c9 Anthony Liguori
        if (vs->name_idx < vs->nwnames) {
1544 ff5e54c9 Anthony Liguori
            v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1545 ff5e54c9 Anthony Liguori
                                vs->wnames[vs->name_idx].data);
1546 ff5e54c9 Anthony Liguori
            v9fs_string_copy(&vs->newfidp->path, &vs->path);
1547 ff5e54c9 Anthony Liguori
1548 ff5e54c9 Anthony Liguori
            err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1549 ff5e54c9 Anthony Liguori
            v9fs_walk_post_newfid_lstat(s, vs, err);
1550 ff5e54c9 Anthony Liguori
            return;
1551 ff5e54c9 Anthony Liguori
        }
1552 9f107513 Anthony Liguori
    }
1553 ff5e54c9 Anthony Liguori
1554 ff5e54c9 Anthony Liguori
    v9fs_walk_marshal(vs);
1555 ff5e54c9 Anthony Liguori
    err = vs->offset;
1556 ff5e54c9 Anthony Liguori
out:
1557 ff5e54c9 Anthony Liguori
    v9fs_walk_complete(s, vs, err);
1558 9f107513 Anthony Liguori
}
1559 9f107513 Anthony Liguori
1560 5e94c103 M. Mohan Kumar
static int32_t get_iounit(V9fsState *s, V9fsString *name)
1561 5e94c103 M. Mohan Kumar
{
1562 5e94c103 M. Mohan Kumar
    struct statfs stbuf;
1563 5e94c103 M. Mohan Kumar
    int32_t iounit = 0;
1564 5e94c103 M. Mohan Kumar
1565 5e94c103 M. Mohan Kumar
    /*
1566 5e94c103 M. Mohan Kumar
     * iounit should be multiples of f_bsize (host filesystem block size
1567 5e94c103 M. Mohan Kumar
     * and as well as less than (client msize - P9_IOHDRSZ))
1568 5e94c103 M. Mohan Kumar
     */
1569 5e94c103 M. Mohan Kumar
    if (!v9fs_do_statfs(s, name, &stbuf)) {
1570 5e94c103 M. Mohan Kumar
        iounit = stbuf.f_bsize;
1571 5e94c103 M. Mohan Kumar
        iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1572 5e94c103 M. Mohan Kumar
    }
1573 5e94c103 M. Mohan Kumar
1574 5e94c103 M. Mohan Kumar
    if (!iounit) {
1575 5e94c103 M. Mohan Kumar
        iounit = s->msize - P9_IOHDRSZ;
1576 5e94c103 M. Mohan Kumar
    }
1577 5e94c103 M. Mohan Kumar
    return iounit;
1578 5e94c103 M. Mohan Kumar
}
1579 5e94c103 M. Mohan Kumar
1580 a6568fe2 Anthony Liguori
static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1581 a6568fe2 Anthony Liguori
{
1582 a6568fe2 Anthony Liguori
    if (vs->fidp->dir == NULL) {
1583 a6568fe2 Anthony Liguori
        err = -errno;
1584 a6568fe2 Anthony Liguori
        goto out;
1585 a6568fe2 Anthony Liguori
    }
1586 a6568fe2 Anthony Liguori
1587 a6568fe2 Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1588 a6568fe2 Anthony Liguori
    err = vs->offset;
1589 a6568fe2 Anthony Liguori
out:
1590 a6568fe2 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1591 a6568fe2 Anthony Liguori
    qemu_free(vs);
1592 a6568fe2 Anthony Liguori
1593 a6568fe2 Anthony Liguori
}
1594 a6568fe2 Anthony Liguori
1595 5e94c103 M. Mohan Kumar
static void v9fs_open_post_getiounit(V9fsState *s, V9fsOpenState *vs)
1596 5e94c103 M. Mohan Kumar
{
1597 5e94c103 M. Mohan Kumar
    int err;
1598 5e94c103 M. Mohan Kumar
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
1599 5e94c103 M. Mohan Kumar
    err = vs->offset;
1600 5e94c103 M. Mohan Kumar
    complete_pdu(s, vs->pdu, err);
1601 5e94c103 M. Mohan Kumar
    qemu_free(vs);
1602 5e94c103 M. Mohan Kumar
}
1603 5e94c103 M. Mohan Kumar
1604 a6568fe2 Anthony Liguori
static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1605 a6568fe2 Anthony Liguori
{
1606 a6568fe2 Anthony Liguori
    if (vs->fidp->fd == -1) {
1607 a6568fe2 Anthony Liguori
        err = -errno;
1608 a6568fe2 Anthony Liguori
        goto out;
1609 a6568fe2 Anthony Liguori
    }
1610 a6568fe2 Anthony Liguori
1611 5e94c103 M. Mohan Kumar
    vs->iounit = get_iounit(s, &vs->fidp->path);
1612 5e94c103 M. Mohan Kumar
    v9fs_open_post_getiounit(s, vs);
1613 5e94c103 M. Mohan Kumar
    return;
1614 a6568fe2 Anthony Liguori
out:
1615 a6568fe2 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1616 a6568fe2 Anthony Liguori
    qemu_free(vs);
1617 a6568fe2 Anthony Liguori
}
1618 a6568fe2 Anthony Liguori
1619 a6568fe2 Anthony Liguori
static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1620 a6568fe2 Anthony Liguori
{
1621 a6568fe2 Anthony Liguori
    if (err) {
1622 a6568fe2 Anthony Liguori
        err = -errno;
1623 a6568fe2 Anthony Liguori
        goto out;
1624 a6568fe2 Anthony Liguori
    }
1625 a6568fe2 Anthony Liguori
1626 a6568fe2 Anthony Liguori
    stat_to_qid(&vs->stbuf, &vs->qid);
1627 a6568fe2 Anthony Liguori
1628 a6568fe2 Anthony Liguori
    if (S_ISDIR(vs->stbuf.st_mode)) {
1629 a6568fe2 Anthony Liguori
        vs->fidp->dir = v9fs_do_opendir(s, &vs->fidp->path);
1630 a6568fe2 Anthony Liguori
        v9fs_open_post_opendir(s, vs, err);
1631 a6568fe2 Anthony Liguori
    } else {
1632 a6568fe2 Anthony Liguori
        vs->fidp->fd = v9fs_do_open(s, &vs->fidp->path,
1633 a6568fe2 Anthony Liguori
                                    omode_to_uflags(vs->mode));
1634 a6568fe2 Anthony Liguori
        v9fs_open_post_open(s, vs, err);
1635 a6568fe2 Anthony Liguori
    }
1636 a6568fe2 Anthony Liguori
    return;
1637 a6568fe2 Anthony Liguori
out:
1638 a6568fe2 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1639 a6568fe2 Anthony Liguori
    qemu_free(vs);
1640 9f107513 Anthony Liguori
}
1641 9f107513 Anthony Liguori
1642 9f107513 Anthony Liguori
static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
1643 a6568fe2 Anthony Liguori
{
1644 a6568fe2 Anthony Liguori
    int32_t fid;
1645 a6568fe2 Anthony Liguori
    V9fsOpenState *vs;
1646 a6568fe2 Anthony Liguori
    ssize_t err = 0;
1647 a6568fe2 Anthony Liguori
1648 a6568fe2 Anthony Liguori
1649 a6568fe2 Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
1650 a6568fe2 Anthony Liguori
    vs->pdu = pdu;
1651 a6568fe2 Anthony Liguori
    vs->offset = 7;
1652 a6568fe2 Anthony Liguori
1653 a6568fe2 Anthony Liguori
    pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1654 a6568fe2 Anthony Liguori
1655 a6568fe2 Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
1656 a6568fe2 Anthony Liguori
    if (vs->fidp == NULL) {
1657 a6568fe2 Anthony Liguori
        err = -ENOENT;
1658 a6568fe2 Anthony Liguori
        goto out;
1659 a6568fe2 Anthony Liguori
    }
1660 a6568fe2 Anthony Liguori
1661 a6568fe2 Anthony Liguori
    BUG_ON(vs->fidp->fd != -1);
1662 a6568fe2 Anthony Liguori
    BUG_ON(vs->fidp->dir);
1663 a6568fe2 Anthony Liguori
1664 a6568fe2 Anthony Liguori
    err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1665 a6568fe2 Anthony Liguori
1666 a6568fe2 Anthony Liguori
    v9fs_open_post_lstat(s, vs, err);
1667 a6568fe2 Anthony Liguori
    return;
1668 a6568fe2 Anthony Liguori
out:
1669 a6568fe2 Anthony Liguori
    complete_pdu(s, pdu, err);
1670 a6568fe2 Anthony Liguori
    qemu_free(vs);
1671 a6568fe2 Anthony Liguori
}
1672 a6568fe2 Anthony Liguori
1673 a6568fe2 Anthony Liguori
static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1674 a6568fe2 Anthony Liguori
{
1675 bbd5697b Anthony Liguori
    int32_t fid;
1676 bbd5697b Anthony Liguori
    size_t offset = 7;
1677 bbd5697b Anthony Liguori
    int err;
1678 bbd5697b Anthony Liguori
1679 bbd5697b Anthony Liguori
    pdu_unmarshal(pdu, offset, "d", &fid);
1680 bbd5697b Anthony Liguori
1681 bbd5697b Anthony Liguori
    err = free_fid(s, fid);
1682 bbd5697b Anthony Liguori
    if (err < 0) {
1683 bbd5697b Anthony Liguori
        goto out;
1684 a6568fe2 Anthony Liguori
    }
1685 bbd5697b Anthony Liguori
1686 bbd5697b Anthony Liguori
    offset = 7;
1687 bbd5697b Anthony Liguori
    err = offset;
1688 bbd5697b Anthony Liguori
out:
1689 bbd5697b Anthony Liguori
    complete_pdu(s, pdu, err);
1690 9f107513 Anthony Liguori
}
1691 9f107513 Anthony Liguori
1692 a9231555 Anthony Liguori
static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1693 a9231555 Anthony Liguori
1694 a9231555 Anthony Liguori
static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1695 a9231555 Anthony Liguori
{
1696 a9231555 Anthony Liguori
    if (err) {
1697 a9231555 Anthony Liguori
        goto out;
1698 a9231555 Anthony Liguori
    }
1699 a9231555 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
1700 a9231555 Anthony Liguori
    v9fs_string_free(&vs->name);
1701 a9231555 Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1702 a9231555 Anthony Liguori
    vs->offset += vs->count;
1703 a9231555 Anthony Liguori
    err = vs->offset;
1704 a9231555 Anthony Liguori
out:
1705 a9231555 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1706 a9231555 Anthony Liguori
    qemu_free(vs);
1707 a9231555 Anthony Liguori
    return;
1708 a9231555 Anthony Liguori
}
1709 a9231555 Anthony Liguori
1710 a9231555 Anthony Liguori
static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1711 a9231555 Anthony Liguori
                                    ssize_t err)
1712 a9231555 Anthony Liguori
{
1713 a9231555 Anthony Liguori
    if (err) {
1714 a9231555 Anthony Liguori
        err = -errno;
1715 a9231555 Anthony Liguori
        goto out;
1716 a9231555 Anthony Liguori
    }
1717 a9231555 Anthony Liguori
    err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1718 a9231555 Anthony Liguori
    if (err) {
1719 a9231555 Anthony Liguori
        goto out;
1720 a9231555 Anthony Liguori
    }
1721 a9231555 Anthony Liguori
1722 a9231555 Anthony Liguori
    vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1723 a9231555 Anthony Liguori
                            &vs->v9stat);
1724 a9231555 Anthony Liguori
    if ((vs->len != (vs->v9stat.size + 2)) ||
1725 a9231555 Anthony Liguori
            ((vs->count + vs->len) > vs->max_count)) {
1726 a9231555 Anthony Liguori
        v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1727 a9231555 Anthony Liguori
        v9fs_read_post_seekdir(s, vs, err);
1728 a9231555 Anthony Liguori
        return;
1729 a9231555 Anthony Liguori
    }
1730 a9231555 Anthony Liguori
    vs->count += vs->len;
1731 a9231555 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
1732 a9231555 Anthony Liguori
    v9fs_string_free(&vs->name);
1733 a9231555 Anthony Liguori
    vs->dir_pos = vs->dent->d_off;
1734 a9231555 Anthony Liguori
    vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1735 a9231555 Anthony Liguori
    v9fs_read_post_readdir(s, vs, err);
1736 a9231555 Anthony Liguori
    return;
1737 a9231555 Anthony Liguori
out:
1738 a9231555 Anthony Liguori
    v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1739 a9231555 Anthony Liguori
    v9fs_read_post_seekdir(s, vs, err);
1740 a9231555 Anthony Liguori
    return;
1741 a9231555 Anthony Liguori
1742 a9231555 Anthony Liguori
}
1743 a9231555 Anthony Liguori
1744 a9231555 Anthony Liguori
static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1745 a9231555 Anthony Liguori
{
1746 a9231555 Anthony Liguori
    if (vs->dent) {
1747 a9231555 Anthony Liguori
        memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1748 a9231555 Anthony Liguori
        v9fs_string_init(&vs->name);
1749 a9231555 Anthony Liguori
        v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1750 a9231555 Anthony Liguori
                            vs->dent->d_name);
1751 a9231555 Anthony Liguori
        err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1752 a9231555 Anthony Liguori
        v9fs_read_post_dir_lstat(s, vs, err);
1753 a9231555 Anthony Liguori
        return;
1754 a9231555 Anthony Liguori
    }
1755 a9231555 Anthony Liguori
1756 a9231555 Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1757 a9231555 Anthony Liguori
    vs->offset += vs->count;
1758 a9231555 Anthony Liguori
    err = vs->offset;
1759 a9231555 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1760 a9231555 Anthony Liguori
    qemu_free(vs);
1761 a9231555 Anthony Liguori
    return;
1762 a9231555 Anthony Liguori
}
1763 a9231555 Anthony Liguori
1764 a9231555 Anthony Liguori
static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1765 a9231555 Anthony Liguori
{
1766 a9231555 Anthony Liguori
    vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1767 a9231555 Anthony Liguori
    v9fs_read_post_readdir(s, vs, err);
1768 a9231555 Anthony Liguori
    return;
1769 a9231555 Anthony Liguori
}
1770 a9231555 Anthony Liguori
1771 a9231555 Anthony Liguori
static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
1772 a9231555 Anthony Liguori
                                       ssize_t err)
1773 a9231555 Anthony Liguori
{
1774 a9231555 Anthony Liguori
    vs->dir_pos = v9fs_do_telldir(s, vs->fidp->dir);
1775 a9231555 Anthony Liguori
    v9fs_read_post_telldir(s, vs, err);
1776 a9231555 Anthony Liguori
    return;
1777 a9231555 Anthony Liguori
}
1778 a9231555 Anthony Liguori
1779 a9231555 Anthony Liguori
static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *vs, ssize_t err)
1780 a9231555 Anthony Liguori
{
1781 a9231555 Anthony Liguori
    if (err  < 0) {
1782 a9231555 Anthony Liguori
        /* IO error return the error */
1783 a9231555 Anthony Liguori
        err = -errno;
1784 a9231555 Anthony Liguori
        goto out;
1785 a9231555 Anthony Liguori
    }
1786 a9231555 Anthony Liguori
    vs->total += vs->len;
1787 a9231555 Anthony Liguori
    vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1788 a9231555 Anthony Liguori
    if (vs->total < vs->count && vs->len > 0) {
1789 a9231555 Anthony Liguori
        do {
1790 a9231555 Anthony Liguori
            if (0) {
1791 a9231555 Anthony Liguori
                print_sg(vs->sg, vs->cnt);
1792 a9231555 Anthony Liguori
            }
1793 a9231555 Anthony Liguori
            vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1794 a9231555 Anthony Liguori
        } while (vs->len == -1 && errno == EINTR);
1795 a9231555 Anthony Liguori
        if (vs->len == -1) {
1796 a9231555 Anthony Liguori
            err  = -errno;
1797 a9231555 Anthony Liguori
        }
1798 a9231555 Anthony Liguori
        v9fs_read_post_readv(s, vs, err);
1799 a9231555 Anthony Liguori
        return;
1800 a9231555 Anthony Liguori
    }
1801 a9231555 Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1802 a9231555 Anthony Liguori
    vs->offset += vs->count;
1803 a9231555 Anthony Liguori
    err = vs->offset;
1804 a9231555 Anthony Liguori
1805 a9231555 Anthony Liguori
out:
1806 a9231555 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1807 a9231555 Anthony Liguori
    qemu_free(vs);
1808 a9231555 Anthony Liguori
}
1809 a9231555 Anthony Liguori
1810 a9231555 Anthony Liguori
static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err)
1811 a9231555 Anthony Liguori
{
1812 a9231555 Anthony Liguori
    if (err == -1) {
1813 a9231555 Anthony Liguori
        err = -errno;
1814 a9231555 Anthony Liguori
        goto out;
1815 a9231555 Anthony Liguori
    }
1816 a9231555 Anthony Liguori
    vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1817 a9231555 Anthony Liguori
1818 a9231555 Anthony Liguori
    if (vs->total < vs->count) {
1819 a9231555 Anthony Liguori
        do {
1820 a9231555 Anthony Liguori
            if (0) {
1821 a9231555 Anthony Liguori
                print_sg(vs->sg, vs->cnt);
1822 a9231555 Anthony Liguori
            }
1823 a9231555 Anthony Liguori
            vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1824 a9231555 Anthony Liguori
        } while (vs->len == -1 && errno == EINTR);
1825 a9231555 Anthony Liguori
        if (vs->len == -1) {
1826 a9231555 Anthony Liguori
            err  = -errno;
1827 a9231555 Anthony Liguori
        }
1828 a9231555 Anthony Liguori
        v9fs_read_post_readv(s, vs, err);
1829 a9231555 Anthony Liguori
        return;
1830 a9231555 Anthony Liguori
    }
1831 a9231555 Anthony Liguori
out:
1832 a9231555 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1833 a9231555 Anthony Liguori
    qemu_free(vs);
1834 a9231555 Anthony Liguori
}
1835 a9231555 Anthony Liguori
1836 9f107513 Anthony Liguori
static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
1837 9f107513 Anthony Liguori
{
1838 a9231555 Anthony Liguori
    int32_t fid;
1839 a9231555 Anthony Liguori
    V9fsReadState *vs;
1840 a9231555 Anthony Liguori
    ssize_t err = 0;
1841 a9231555 Anthony Liguori
1842 a9231555 Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
1843 a9231555 Anthony Liguori
    vs->pdu = pdu;
1844 a9231555 Anthony Liguori
    vs->offset = 7;
1845 a9231555 Anthony Liguori
    vs->total = 0;
1846 a9231555 Anthony Liguori
    vs->len = 0;
1847 a9231555 Anthony Liguori
    vs->count = 0;
1848 a9231555 Anthony Liguori
1849 a9231555 Anthony Liguori
    pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count);
1850 a9231555 Anthony Liguori
1851 a9231555 Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
1852 a9231555 Anthony Liguori
    if (vs->fidp == NULL) {
1853 a9231555 Anthony Liguori
        err = -EINVAL;
1854 a9231555 Anthony Liguori
        goto out;
1855 a9231555 Anthony Liguori
    }
1856 a9231555 Anthony Liguori
1857 a9231555 Anthony Liguori
    if (vs->fidp->dir) {
1858 a9231555 Anthony Liguori
        vs->max_count = vs->count;
1859 a9231555 Anthony Liguori
        vs->count = 0;
1860 a9231555 Anthony Liguori
        if (vs->off == 0) {
1861 a9231555 Anthony Liguori
            v9fs_do_rewinddir(s, vs->fidp->dir);
1862 a9231555 Anthony Liguori
        }
1863 a9231555 Anthony Liguori
        v9fs_read_post_rewinddir(s, vs, err);
1864 a9231555 Anthony Liguori
        return;
1865 a9231555 Anthony Liguori
    } else if (vs->fidp->fd != -1) {
1866 a9231555 Anthony Liguori
        vs->sg = vs->iov;
1867 a9231555 Anthony Liguori
        pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
1868 a9231555 Anthony Liguori
        err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1869 a9231555 Anthony Liguori
        v9fs_read_post_lseek(s, vs, err);
1870 a9231555 Anthony Liguori
        return;
1871 a9231555 Anthony Liguori
    } else {
1872 a9231555 Anthony Liguori
        err = -EINVAL;
1873 9f107513 Anthony Liguori
    }
1874 a9231555 Anthony Liguori
out:
1875 a9231555 Anthony Liguori
    complete_pdu(s, pdu, err);
1876 a9231555 Anthony Liguori
    qemu_free(vs);
1877 9f107513 Anthony Liguori
}
1878 9f107513 Anthony Liguori
1879 c18e2f94 Sripathi Kodi
typedef struct V9fsReadDirState {
1880 c18e2f94 Sripathi Kodi
    V9fsPDU *pdu;
1881 c18e2f94 Sripathi Kodi
    V9fsFidState *fidp;
1882 c18e2f94 Sripathi Kodi
    V9fsQID qid;
1883 c18e2f94 Sripathi Kodi
    off_t saved_dir_pos;
1884 c18e2f94 Sripathi Kodi
    struct dirent *dent;
1885 c18e2f94 Sripathi Kodi
    int32_t count;
1886 c18e2f94 Sripathi Kodi
    int32_t max_count;
1887 c18e2f94 Sripathi Kodi
    size_t offset;
1888 c18e2f94 Sripathi Kodi
    int64_t initial_offset;
1889 c18e2f94 Sripathi Kodi
    V9fsString name;
1890 c18e2f94 Sripathi Kodi
} V9fsReadDirState;
1891 c18e2f94 Sripathi Kodi
1892 c18e2f94 Sripathi Kodi
static void v9fs_readdir_post_seekdir(V9fsState *s, V9fsReadDirState *vs)
1893 c18e2f94 Sripathi Kodi
{
1894 c18e2f94 Sripathi Kodi
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1895 c18e2f94 Sripathi Kodi
    vs->offset += vs->count;
1896 c18e2f94 Sripathi Kodi
    complete_pdu(s, vs->pdu, vs->offset);
1897 c18e2f94 Sripathi Kodi
    qemu_free(vs);
1898 c18e2f94 Sripathi Kodi
    return;
1899 c18e2f94 Sripathi Kodi
}
1900 c18e2f94 Sripathi Kodi
1901 c18e2f94 Sripathi Kodi
/* Size of each dirent on the wire: size of qid (13) + size of offset (8)
1902 c18e2f94 Sripathi Kodi
 * size of type (1) + size of name.size (2) + strlen(name.data)
1903 c18e2f94 Sripathi Kodi
 */
1904 c18e2f94 Sripathi Kodi
#define V9_READDIR_DATA_SZ (24 + strlen(vs->name.data))
1905 c18e2f94 Sripathi Kodi
1906 c18e2f94 Sripathi Kodi
static void v9fs_readdir_post_readdir(V9fsState *s, V9fsReadDirState *vs)
1907 c18e2f94 Sripathi Kodi
{
1908 c18e2f94 Sripathi Kodi
    int len;
1909 c18e2f94 Sripathi Kodi
    size_t size;
1910 c18e2f94 Sripathi Kodi
1911 c18e2f94 Sripathi Kodi
    if (vs->dent) {
1912 c18e2f94 Sripathi Kodi
        v9fs_string_init(&vs->name);
1913 c18e2f94 Sripathi Kodi
        v9fs_string_sprintf(&vs->name, "%s", vs->dent->d_name);
1914 c18e2f94 Sripathi Kodi
1915 c18e2f94 Sripathi Kodi
        if ((vs->count + V9_READDIR_DATA_SZ) > vs->max_count) {
1916 c18e2f94 Sripathi Kodi
            /* Ran out of buffer. Set dir back to old position and return */
1917 c18e2f94 Sripathi Kodi
            v9fs_do_seekdir(s, vs->fidp->dir, vs->saved_dir_pos);
1918 c18e2f94 Sripathi Kodi
            v9fs_readdir_post_seekdir(s, vs);
1919 c18e2f94 Sripathi Kodi
            return;
1920 c18e2f94 Sripathi Kodi
        }
1921 c18e2f94 Sripathi Kodi
1922 c18e2f94 Sripathi Kodi
        /* Fill up just the path field of qid because the client uses
1923 c18e2f94 Sripathi Kodi
         * only that. To fill the entire qid structure we will have
1924 c18e2f94 Sripathi Kodi
         * to stat each dirent found, which is expensive
1925 c18e2f94 Sripathi Kodi
         */
1926 c18e2f94 Sripathi Kodi
        size = MIN(sizeof(vs->dent->d_ino), sizeof(vs->qid.path));
1927 c18e2f94 Sripathi Kodi
        memcpy(&vs->qid.path, &vs->dent->d_ino, size);
1928 c18e2f94 Sripathi Kodi
        /* Fill the other fields with dummy values */
1929 c18e2f94 Sripathi Kodi
        vs->qid.type = 0;
1930 c18e2f94 Sripathi Kodi
        vs->qid.version = 0;
1931 c18e2f94 Sripathi Kodi
1932 c18e2f94 Sripathi Kodi
        len = pdu_marshal(vs->pdu, vs->offset+4+vs->count, "Qqbs",
1933 c18e2f94 Sripathi Kodi
                              &vs->qid, vs->dent->d_off,
1934 c18e2f94 Sripathi Kodi
                              vs->dent->d_type, &vs->name);
1935 c18e2f94 Sripathi Kodi
        vs->count += len;
1936 c18e2f94 Sripathi Kodi
        v9fs_string_free(&vs->name);
1937 c18e2f94 Sripathi Kodi
        vs->saved_dir_pos = vs->dent->d_off;
1938 c18e2f94 Sripathi Kodi
        vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1939 c18e2f94 Sripathi Kodi
        v9fs_readdir_post_readdir(s, vs);
1940 c18e2f94 Sripathi Kodi
        return;
1941 c18e2f94 Sripathi Kodi
    }
1942 c18e2f94 Sripathi Kodi
1943 c18e2f94 Sripathi Kodi
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1944 c18e2f94 Sripathi Kodi
    vs->offset += vs->count;
1945 c18e2f94 Sripathi Kodi
    complete_pdu(s, vs->pdu, vs->offset);
1946 c18e2f94 Sripathi Kodi
    qemu_free(vs);
1947 c18e2f94 Sripathi Kodi
    return;
1948 c18e2f94 Sripathi Kodi
}
1949 c18e2f94 Sripathi Kodi
1950 c18e2f94 Sripathi Kodi
static void v9fs_readdir_post_telldir(V9fsState *s, V9fsReadDirState *vs)
1951 c18e2f94 Sripathi Kodi
{
1952 c18e2f94 Sripathi Kodi
    vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1953 c18e2f94 Sripathi Kodi
    v9fs_readdir_post_readdir(s, vs);
1954 c18e2f94 Sripathi Kodi
    return;
1955 c18e2f94 Sripathi Kodi
}
1956 c18e2f94 Sripathi Kodi
1957 c18e2f94 Sripathi Kodi
static void v9fs_readdir_post_setdir(V9fsState *s, V9fsReadDirState *vs)
1958 c18e2f94 Sripathi Kodi
{
1959 c18e2f94 Sripathi Kodi
    vs->saved_dir_pos = v9fs_do_telldir(s, vs->fidp->dir);
1960 c18e2f94 Sripathi Kodi
    v9fs_readdir_post_telldir(s, vs);
1961 c18e2f94 Sripathi Kodi
    return;
1962 c18e2f94 Sripathi Kodi
}
1963 c18e2f94 Sripathi Kodi
1964 c18e2f94 Sripathi Kodi
static void v9fs_readdir(V9fsState *s, V9fsPDU *pdu)
1965 c18e2f94 Sripathi Kodi
{
1966 c18e2f94 Sripathi Kodi
    int32_t fid;
1967 c18e2f94 Sripathi Kodi
    V9fsReadDirState *vs;
1968 c18e2f94 Sripathi Kodi
    ssize_t err = 0;
1969 c18e2f94 Sripathi Kodi
    size_t offset = 7;
1970 c18e2f94 Sripathi Kodi
1971 c18e2f94 Sripathi Kodi
    vs = qemu_malloc(sizeof(*vs));
1972 c18e2f94 Sripathi Kodi
    vs->pdu = pdu;
1973 c18e2f94 Sripathi Kodi
    vs->offset = 7;
1974 c18e2f94 Sripathi Kodi
    vs->count = 0;
1975 c18e2f94 Sripathi Kodi
1976 c18e2f94 Sripathi Kodi
    pdu_unmarshal(vs->pdu, offset, "dqd", &fid, &vs->initial_offset,
1977 c18e2f94 Sripathi Kodi
                                                        &vs->max_count);
1978 c18e2f94 Sripathi Kodi
1979 c18e2f94 Sripathi Kodi
    vs->fidp = lookup_fid(s, fid);
1980 c18e2f94 Sripathi Kodi
    if (vs->fidp == NULL || !(vs->fidp->dir)) {
1981 c18e2f94 Sripathi Kodi
        err = -EINVAL;
1982 c18e2f94 Sripathi Kodi
        goto out;
1983 c18e2f94 Sripathi Kodi
    }
1984 c18e2f94 Sripathi Kodi
1985 c18e2f94 Sripathi Kodi
    if (vs->initial_offset == 0) {
1986 c18e2f94 Sripathi Kodi
        v9fs_do_rewinddir(s, vs->fidp->dir);
1987 c18e2f94 Sripathi Kodi
    } else {
1988 c18e2f94 Sripathi Kodi
        v9fs_do_seekdir(s, vs->fidp->dir, vs->initial_offset);
1989 c18e2f94 Sripathi Kodi
    }
1990 c18e2f94 Sripathi Kodi
1991 c18e2f94 Sripathi Kodi
    v9fs_readdir_post_setdir(s, vs);
1992 c18e2f94 Sripathi Kodi
    return;
1993 c18e2f94 Sripathi Kodi
1994 c18e2f94 Sripathi Kodi
out:
1995 c18e2f94 Sripathi Kodi
    complete_pdu(s, pdu, err);
1996 c18e2f94 Sripathi Kodi
    qemu_free(vs);
1997 c18e2f94 Sripathi Kodi
    return;
1998 c18e2f94 Sripathi Kodi
}
1999 c18e2f94 Sripathi Kodi
2000 8449360c Anthony Liguori
static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs,
2001 8449360c Anthony Liguori
                                   ssize_t err)
2002 8449360c Anthony Liguori
{
2003 8449360c Anthony Liguori
    if (err  < 0) {
2004 8449360c Anthony Liguori
        /* IO error return the error */
2005 8449360c Anthony Liguori
        err = -errno;
2006 8449360c Anthony Liguori
        goto out;
2007 8449360c Anthony Liguori
    }
2008 8449360c Anthony Liguori
    vs->total += vs->len;
2009 8449360c Anthony Liguori
    vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
2010 8449360c Anthony Liguori
    if (vs->total < vs->count && vs->len > 0) {
2011 8449360c Anthony Liguori
        do {
2012 8449360c Anthony Liguori
            if (0) {
2013 8449360c Anthony Liguori
                print_sg(vs->sg, vs->cnt);
2014 8449360c Anthony Liguori
            }
2015 8449360c Anthony Liguori
            vs->len =  v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
2016 8449360c Anthony Liguori
        } while (vs->len == -1 && errno == EINTR);
2017 8449360c Anthony Liguori
        if (vs->len == -1) {
2018 8449360c Anthony Liguori
            err  = -errno;
2019 8449360c Anthony Liguori
        }
2020 8449360c Anthony Liguori
        v9fs_write_post_writev(s, vs, err);
2021 8449360c Anthony Liguori
        return;
2022 8449360c Anthony Liguori
    }
2023 8449360c Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
2024 8449360c Anthony Liguori
2025 8449360c Anthony Liguori
    err = vs->offset;
2026 8449360c Anthony Liguori
out:
2027 8449360c Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2028 8449360c Anthony Liguori
    qemu_free(vs);
2029 8449360c Anthony Liguori
}
2030 8449360c Anthony Liguori
2031 8449360c Anthony Liguori
static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err)
2032 8449360c Anthony Liguori
{
2033 8449360c Anthony Liguori
    if (err == -1) {
2034 8449360c Anthony Liguori
        err = -errno;
2035 8449360c Anthony Liguori
        goto out;
2036 8449360c Anthony Liguori
    }
2037 8449360c Anthony Liguori
    vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
2038 8449360c Anthony Liguori
2039 8449360c Anthony Liguori
    if (vs->total < vs->count) {
2040 8449360c Anthony Liguori
        do {
2041 8449360c Anthony Liguori
            if (0) {
2042 8449360c Anthony Liguori
                print_sg(vs->sg, vs->cnt);
2043 8449360c Anthony Liguori
            }
2044 8449360c Anthony Liguori
            vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
2045 8449360c Anthony Liguori
        } while (vs->len == -1 && errno == EINTR);
2046 8449360c Anthony Liguori
        if (vs->len == -1) {
2047 8449360c Anthony Liguori
            err  = -errno;
2048 8449360c Anthony Liguori
        }
2049 8449360c Anthony Liguori
        v9fs_write_post_writev(s, vs, err);
2050 8449360c Anthony Liguori
        return;
2051 8449360c Anthony Liguori
    }
2052 8449360c Anthony Liguori
2053 8449360c Anthony Liguori
out:
2054 8449360c Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2055 8449360c Anthony Liguori
    qemu_free(vs);
2056 8449360c Anthony Liguori
}
2057 8449360c Anthony Liguori
2058 9f107513 Anthony Liguori
static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
2059 9f107513 Anthony Liguori
{
2060 8449360c Anthony Liguori
    int32_t fid;
2061 8449360c Anthony Liguori
    V9fsWriteState *vs;
2062 8449360c Anthony Liguori
    ssize_t err;
2063 8449360c Anthony Liguori
2064 8449360c Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
2065 8449360c Anthony Liguori
2066 8449360c Anthony Liguori
    vs->pdu = pdu;
2067 8449360c Anthony Liguori
    vs->offset = 7;
2068 8449360c Anthony Liguori
    vs->sg = vs->iov;
2069 8449360c Anthony Liguori
    vs->total = 0;
2070 8449360c Anthony Liguori
    vs->len = 0;
2071 8449360c Anthony Liguori
2072 8449360c Anthony Liguori
    pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
2073 8449360c Anthony Liguori
                    vs->sg, &vs->cnt);
2074 8449360c Anthony Liguori
2075 8449360c Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
2076 8449360c Anthony Liguori
    if (vs->fidp == NULL) {
2077 8449360c Anthony Liguori
        err = -EINVAL;
2078 8449360c Anthony Liguori
        goto out;
2079 9f107513 Anthony Liguori
    }
2080 8449360c Anthony Liguori
2081 8449360c Anthony Liguori
    if (vs->fidp->fd == -1) {
2082 8449360c Anthony Liguori
        err = -EINVAL;
2083 8449360c Anthony Liguori
        goto out;
2084 8449360c Anthony Liguori
    }
2085 8449360c Anthony Liguori
2086 8449360c Anthony Liguori
    err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
2087 8449360c Anthony Liguori
2088 8449360c Anthony Liguori
    v9fs_write_post_lseek(s, vs, err);
2089 8449360c Anthony Liguori
    return;
2090 8449360c Anthony Liguori
2091 8449360c Anthony Liguori
out:
2092 8449360c Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2093 8449360c Anthony Liguori
    qemu_free(vs);
2094 9f107513 Anthony Liguori
}
2095 9f107513 Anthony Liguori
2096 5e94c103 M. Mohan Kumar
static void v9fs_create_post_getiounit(V9fsState *s, V9fsCreateState *vs)
2097 c494dd6f Anthony Liguori
{
2098 5e94c103 M. Mohan Kumar
    int err;
2099 5e94c103 M. Mohan Kumar
    v9fs_string_copy(&vs->fidp->path, &vs->fullname);
2100 5e94c103 M. Mohan Kumar
    stat_to_qid(&vs->stbuf, &vs->qid);
2101 c494dd6f Anthony Liguori
2102 5e94c103 M. Mohan Kumar
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
2103 5e94c103 M. Mohan Kumar
    err = vs->offset;
2104 c494dd6f Anthony Liguori
2105 5e94c103 M. Mohan Kumar
    complete_pdu(s, vs->pdu, err);
2106 5e94c103 M. Mohan Kumar
    v9fs_string_free(&vs->name);
2107 5e94c103 M. Mohan Kumar
    v9fs_string_free(&vs->extension);
2108 5e94c103 M. Mohan Kumar
    v9fs_string_free(&vs->fullname);
2109 5e94c103 M. Mohan Kumar
    qemu_free(vs);
2110 5e94c103 M. Mohan Kumar
}
2111 5e94c103 M. Mohan Kumar
2112 5e94c103 M. Mohan Kumar
static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
2113 5e94c103 M. Mohan Kumar
{
2114 5e94c103 M. Mohan Kumar
    if (err == 0) {
2115 5e94c103 M. Mohan Kumar
        vs->iounit = get_iounit(s, &vs->fidp->path);
2116 5e94c103 M. Mohan Kumar
        v9fs_create_post_getiounit(s, vs);
2117 5e94c103 M. Mohan Kumar
        return;
2118 c494dd6f Anthony Liguori
    }
2119 c494dd6f Anthony Liguori
2120 c494dd6f Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2121 c494dd6f Anthony Liguori
    v9fs_string_free(&vs->name);
2122 c494dd6f Anthony Liguori
    v9fs_string_free(&vs->extension);
2123 c494dd6f Anthony Liguori
    v9fs_string_free(&vs->fullname);
2124 c494dd6f Anthony Liguori
    qemu_free(vs);
2125 c494dd6f Anthony Liguori
}
2126 c494dd6f Anthony Liguori
2127 c494dd6f Anthony Liguori
static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
2128 c494dd6f Anthony Liguori
{
2129 c494dd6f Anthony Liguori
    if (err) {
2130 c494dd6f Anthony Liguori
        err = -errno;
2131 c494dd6f Anthony Liguori
    }
2132 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
2133 c494dd6f Anthony Liguori
}
2134 c494dd6f Anthony Liguori
2135 c494dd6f Anthony Liguori
static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
2136 c494dd6f Anthony Liguori
                                                                    int err)
2137 c494dd6f Anthony Liguori
{
2138 c494dd6f Anthony Liguori
    if (!vs->fidp->dir) {
2139 c494dd6f Anthony Liguori
        err = -errno;
2140 c494dd6f Anthony Liguori
    }
2141 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
2142 c494dd6f Anthony Liguori
}
2143 c494dd6f Anthony Liguori
2144 c494dd6f Anthony Liguori
static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
2145 c494dd6f Anthony Liguori
                                                                    int err)
2146 c494dd6f Anthony Liguori
{
2147 c494dd6f Anthony Liguori
    if (err) {
2148 c494dd6f Anthony Liguori
        err = -errno;
2149 c494dd6f Anthony Liguori
        goto out;
2150 c494dd6f Anthony Liguori
    }
2151 c494dd6f Anthony Liguori
2152 c494dd6f Anthony Liguori
    vs->fidp->dir = v9fs_do_opendir(s, &vs->fullname);
2153 c494dd6f Anthony Liguori
    v9fs_create_post_opendir(s, vs, err);
2154 c494dd6f Anthony Liguori
    return;
2155 c494dd6f Anthony Liguori
2156 c494dd6f Anthony Liguori
out:
2157 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
2158 c494dd6f Anthony Liguori
}
2159 c494dd6f Anthony Liguori
2160 c494dd6f Anthony Liguori
static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
2161 c494dd6f Anthony Liguori
{
2162 c494dd6f Anthony Liguori
    if (err) {
2163 c494dd6f Anthony Liguori
        err = -errno;
2164 c494dd6f Anthony Liguori
        goto out;
2165 c494dd6f Anthony Liguori
    }
2166 c494dd6f Anthony Liguori
2167 c494dd6f Anthony Liguori
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2168 c494dd6f Anthony Liguori
    v9fs_create_post_dir_lstat(s, vs, err);
2169 c494dd6f Anthony Liguori
    return;
2170 c494dd6f Anthony Liguori
2171 c494dd6f Anthony Liguori
out:
2172 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
2173 c494dd6f Anthony Liguori
}
2174 c494dd6f Anthony Liguori
2175 c494dd6f Anthony Liguori
static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
2176 c494dd6f Anthony Liguori
{
2177 c494dd6f Anthony Liguori
    if (err) {
2178 c494dd6f Anthony Liguori
        vs->fidp->fd = -1;
2179 c494dd6f Anthony Liguori
        err = -errno;
2180 c494dd6f Anthony Liguori
    }
2181 c494dd6f Anthony Liguori
2182 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
2183 c494dd6f Anthony Liguori
    return;
2184 c494dd6f Anthony Liguori
}
2185 c494dd6f Anthony Liguori
2186 c494dd6f Anthony Liguori
static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
2187 c494dd6f Anthony Liguori
{
2188 c494dd6f Anthony Liguori
    if (vs->fidp->fd == -1) {
2189 c494dd6f Anthony Liguori
        err = -errno;
2190 c494dd6f Anthony Liguori
        goto out;
2191 c494dd6f Anthony Liguori
    }
2192 c494dd6f Anthony Liguori
2193 c494dd6f Anthony Liguori
    err = v9fs_do_fstat(s, vs->fidp->fd, &vs->stbuf);
2194 c494dd6f Anthony Liguori
    v9fs_create_post_fstat(s, vs, err);
2195 c494dd6f Anthony Liguori
2196 c494dd6f Anthony Liguori
    return;
2197 c494dd6f Anthony Liguori
2198 c494dd6f Anthony Liguori
out:
2199 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
2200 c494dd6f Anthony Liguori
2201 c494dd6f Anthony Liguori
}
2202 c494dd6f Anthony Liguori
2203 c494dd6f Anthony Liguori
static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
2204 c494dd6f Anthony Liguori
{
2205 c494dd6f Anthony Liguori
2206 c494dd6f Anthony Liguori
    if (err == 0 || errno != ENOENT) {
2207 c494dd6f Anthony Liguori
        err = -errno;
2208 c494dd6f Anthony Liguori
        goto out;
2209 c494dd6f Anthony Liguori
    }
2210 c494dd6f Anthony Liguori
2211 c494dd6f Anthony Liguori
    if (vs->perm & P9_STAT_MODE_DIR) {
2212 00ec5c37 Venkateswararao Jujjuri (JV)
        err = v9fs_do_mkdir(s, vs);
2213 c494dd6f Anthony Liguori
        v9fs_create_post_mkdir(s, vs, err);
2214 c494dd6f Anthony Liguori
    } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
2215 879c2813 Venkateswararao Jujjuri (JV)
        err = v9fs_do_symlink(s, vs);
2216 c494dd6f Anthony Liguori
        v9fs_create_post_perms(s, vs, err);
2217 c494dd6f Anthony Liguori
    } else if (vs->perm & P9_STAT_MODE_LINK) {
2218 c494dd6f Anthony Liguori
        int32_t nfid = atoi(vs->extension.data);
2219 c494dd6f Anthony Liguori
        V9fsFidState *nfidp = lookup_fid(s, nfid);
2220 c494dd6f Anthony Liguori
        if (nfidp == NULL) {
2221 c494dd6f Anthony Liguori
            err = -errno;
2222 c494dd6f Anthony Liguori
            v9fs_post_create(s, vs, err);
2223 c494dd6f Anthony Liguori
        }
2224 c494dd6f Anthony Liguori
        err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
2225 c494dd6f Anthony Liguori
        v9fs_create_post_perms(s, vs, err);
2226 c494dd6f Anthony Liguori
    } else if (vs->perm & P9_STAT_MODE_DEVICE) {
2227 c494dd6f Anthony Liguori
        char ctype;
2228 c494dd6f Anthony Liguori
        uint32_t major, minor;
2229 c494dd6f Anthony Liguori
        mode_t nmode = 0;
2230 c494dd6f Anthony Liguori
2231 c494dd6f Anthony Liguori
        if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
2232 c494dd6f Anthony Liguori
                                        &minor) != 3) {
2233 c494dd6f Anthony Liguori
            err = -errno;
2234 c494dd6f Anthony Liguori
            v9fs_post_create(s, vs, err);
2235 c494dd6f Anthony Liguori
        }
2236 c494dd6f Anthony Liguori
2237 c494dd6f Anthony Liguori
        switch (ctype) {
2238 c494dd6f Anthony Liguori
        case 'c':
2239 c494dd6f Anthony Liguori
            nmode = S_IFCHR;
2240 c494dd6f Anthony Liguori
            break;
2241 c494dd6f Anthony Liguori
        case 'b':
2242 c494dd6f Anthony Liguori
            nmode = S_IFBLK;
2243 c494dd6f Anthony Liguori
            break;
2244 c494dd6f Anthony Liguori
        default:
2245 c494dd6f Anthony Liguori
            err = -EIO;
2246 c494dd6f Anthony Liguori
            v9fs_post_create(s, vs, err);
2247 c494dd6f Anthony Liguori
        }
2248 c494dd6f Anthony Liguori
2249 c494dd6f Anthony Liguori
        nmode |= vs->perm & 0777;
2250 1c293312 Venkateswararao Jujjuri (JV)
        err = v9fs_do_mknod(s, vs, nmode, makedev(major, minor));
2251 c494dd6f Anthony Liguori
        v9fs_create_post_perms(s, vs, err);
2252 c494dd6f Anthony Liguori
    } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
2253 1c293312 Venkateswararao Jujjuri (JV)
        err = v9fs_do_mknod(s, vs, S_IFIFO | (vs->perm & 0777), 0);
2254 c494dd6f Anthony Liguori
        v9fs_post_create(s, vs, err);
2255 c494dd6f Anthony Liguori
    } else if (vs->perm & P9_STAT_MODE_SOCKET) {
2256 63729c36 Venkateswararao Jujjuri (JV)
        err = v9fs_do_mknod(s, vs, S_IFSOCK | (vs->perm & 0777), 0);
2257 63729c36 Venkateswararao Jujjuri (JV)
        v9fs_post_create(s, vs, err);
2258 c494dd6f Anthony Liguori
    } else {
2259 4750a96f Venkateswararao Jujjuri (JV)
        vs->fidp->fd = v9fs_do_open2(s, vs);
2260 c494dd6f Anthony Liguori
        v9fs_create_post_open2(s, vs, err);
2261 c494dd6f Anthony Liguori
    }
2262 c494dd6f Anthony Liguori
2263 c494dd6f Anthony Liguori
    return;
2264 c494dd6f Anthony Liguori
2265 c494dd6f Anthony Liguori
out:
2266 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
2267 c494dd6f Anthony Liguori
}
2268 c494dd6f Anthony Liguori
2269 9f107513 Anthony Liguori
static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
2270 9f107513 Anthony Liguori
{
2271 c494dd6f Anthony Liguori
    int32_t fid;
2272 c494dd6f Anthony Liguori
    V9fsCreateState *vs;
2273 c494dd6f Anthony Liguori
    int err = 0;
2274 c494dd6f Anthony Liguori
2275 c494dd6f Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
2276 c494dd6f Anthony Liguori
    vs->pdu = pdu;
2277 c494dd6f Anthony Liguori
    vs->offset = 7;
2278 c494dd6f Anthony Liguori
2279 c494dd6f Anthony Liguori
    v9fs_string_init(&vs->fullname);
2280 c494dd6f Anthony Liguori
2281 c494dd6f Anthony Liguori
    pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
2282 c494dd6f Anthony Liguori
                                &vs->perm, &vs->mode, &vs->extension);
2283 c494dd6f Anthony Liguori
2284 c494dd6f Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
2285 c494dd6f Anthony Liguori
    if (vs->fidp == NULL) {
2286 c494dd6f Anthony Liguori
        err = -EINVAL;
2287 c494dd6f Anthony Liguori
        goto out;
2288 9f107513 Anthony Liguori
    }
2289 c494dd6f Anthony Liguori
2290 c494dd6f Anthony Liguori
    v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
2291 c494dd6f Anthony Liguori
                                                        vs->name.data);
2292 c494dd6f Anthony Liguori
2293 c494dd6f Anthony Liguori
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2294 c494dd6f Anthony Liguori
    v9fs_create_post_lstat(s, vs, err);
2295 c494dd6f Anthony Liguori
    return;
2296 c494dd6f Anthony Liguori
2297 c494dd6f Anthony Liguori
out:
2298 c494dd6f Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2299 c494dd6f Anthony Liguori
    v9fs_string_free(&vs->name);
2300 c494dd6f Anthony Liguori
    v9fs_string_free(&vs->extension);
2301 c494dd6f Anthony Liguori
    qemu_free(vs);
2302 9f107513 Anthony Liguori
}
2303 9f107513 Anthony Liguori
2304 9f107513 Anthony Liguori
static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
2305 9f107513 Anthony Liguori
{
2306 9c5e9d89 Anthony Liguori
    /* A nop call with no return */
2307 9c5e9d89 Anthony Liguori
    complete_pdu(s, pdu, 7);
2308 9f107513 Anthony Liguori
}
2309 9f107513 Anthony Liguori
2310 5bae1900 Anthony Liguori
static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
2311 5bae1900 Anthony Liguori
                                                                int err)
2312 5bae1900 Anthony Liguori
{
2313 5bae1900 Anthony Liguori
    if (err < 0) {
2314 926487b7 Sripathi Kodi
        err = -errno;
2315 926487b7 Sripathi Kodi
    } else {
2316 926487b7 Sripathi Kodi
        err = vs->offset;
2317 5bae1900 Anthony Liguori
    }
2318 5bae1900 Anthony Liguori
2319 926487b7 Sripathi Kodi
    /* For TREMOVE we need to clunk the fid even on failed remove */
2320 926487b7 Sripathi Kodi
    free_fid(s, vs->fidp->fid);
2321 926487b7 Sripathi Kodi
2322 5bae1900 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2323 5bae1900 Anthony Liguori
    qemu_free(vs);
2324 5bae1900 Anthony Liguori
}
2325 5bae1900 Anthony Liguori
2326 9f107513 Anthony Liguori
static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
2327 9f107513 Anthony Liguori
{
2328 5bae1900 Anthony Liguori
    int32_t fid;
2329 5bae1900 Anthony Liguori
    V9fsRemoveState *vs;
2330 5bae1900 Anthony Liguori
    int err = 0;
2331 5bae1900 Anthony Liguori
2332 5bae1900 Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
2333 5bae1900 Anthony Liguori
    vs->pdu = pdu;
2334 5bae1900 Anthony Liguori
    vs->offset = 7;
2335 5bae1900 Anthony Liguori
2336 5bae1900 Anthony Liguori
    pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
2337 5bae1900 Anthony Liguori
2338 5bae1900 Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
2339 5bae1900 Anthony Liguori
    if (vs->fidp == NULL) {
2340 5bae1900 Anthony Liguori
        err = -EINVAL;
2341 5bae1900 Anthony Liguori
        goto out;
2342 9f107513 Anthony Liguori
    }
2343 5bae1900 Anthony Liguori
2344 5bae1900 Anthony Liguori
    err = v9fs_do_remove(s, &vs->fidp->path);
2345 5bae1900 Anthony Liguori
    v9fs_remove_post_remove(s, vs, err);
2346 5bae1900 Anthony Liguori
    return;
2347 5bae1900 Anthony Liguori
2348 5bae1900 Anthony Liguori
out:
2349 5bae1900 Anthony Liguori
    complete_pdu(s, pdu, err);
2350 5bae1900 Anthony Liguori
    qemu_free(vs);
2351 9f107513 Anthony Liguori
}
2352 9f107513 Anthony Liguori
2353 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
2354 8cf89e00 Anthony Liguori
{
2355 8cf89e00 Anthony Liguori
    if (err < 0) {
2356 8cf89e00 Anthony Liguori
        goto out;
2357 8cf89e00 Anthony Liguori
    }
2358 8cf89e00 Anthony Liguori
2359 8cf89e00 Anthony Liguori
    err = vs->offset;
2360 8cf89e00 Anthony Liguori
2361 8cf89e00 Anthony Liguori
out:
2362 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2363 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2364 8cf89e00 Anthony Liguori
    qemu_free(vs);
2365 8cf89e00 Anthony Liguori
}
2366 8cf89e00 Anthony Liguori
2367 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
2368 8cf89e00 Anthony Liguori
{
2369 8cf89e00 Anthony Liguori
    if (err < 0) {
2370 8cf89e00 Anthony Liguori
        goto out;
2371 8cf89e00 Anthony Liguori
    }
2372 8cf89e00 Anthony Liguori
2373 8cf89e00 Anthony Liguori
    if (vs->v9stat.name.size != 0) {
2374 8cf89e00 Anthony Liguori
        v9fs_string_free(&vs->nname);
2375 8cf89e00 Anthony Liguori
    }
2376 8cf89e00 Anthony Liguori
2377 8cf89e00 Anthony Liguori
    if (vs->v9stat.length != -1) {
2378 8cf89e00 Anthony Liguori
        if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
2379 8cf89e00 Anthony Liguori
            err = -errno;
2380 8cf89e00 Anthony Liguori
        }
2381 8cf89e00 Anthony Liguori
    }
2382 8cf89e00 Anthony Liguori
    v9fs_wstat_post_truncate(s, vs, err);
2383 8cf89e00 Anthony Liguori
    return;
2384 8cf89e00 Anthony Liguori
2385 8cf89e00 Anthony Liguori
out:
2386 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2387 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2388 8cf89e00 Anthony Liguori
    qemu_free(vs);
2389 8cf89e00 Anthony Liguori
}
2390 8cf89e00 Anthony Liguori
2391 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
2392 8cf89e00 Anthony Liguori
{
2393 8cf89e00 Anthony Liguori
    V9fsFidState *fidp;
2394 8cf89e00 Anthony Liguori
    if (err < 0) {
2395 8cf89e00 Anthony Liguori
        goto out;
2396 8cf89e00 Anthony Liguori
    }
2397 8cf89e00 Anthony Liguori
2398 8cf89e00 Anthony Liguori
    if (vs->v9stat.name.size != 0) {
2399 8cf89e00 Anthony Liguori
        char *old_name, *new_name;
2400 8cf89e00 Anthony Liguori
        char *end;
2401 8cf89e00 Anthony Liguori
2402 8cf89e00 Anthony Liguori
        old_name = vs->fidp->path.data;
2403 8cf89e00 Anthony Liguori
        end = strrchr(old_name, '/');
2404 8cf89e00 Anthony Liguori
        if (end) {
2405 8cf89e00 Anthony Liguori
            end++;
2406 8cf89e00 Anthony Liguori
        } else {
2407 8cf89e00 Anthony Liguori
            end = old_name;
2408 8cf89e00 Anthony Liguori
        }
2409 8cf89e00 Anthony Liguori
2410 cc597832 Blue Swirl
        new_name = qemu_mallocz(end - old_name + vs->v9stat.name.size + 1);
2411 8cf89e00 Anthony Liguori
2412 8cf89e00 Anthony Liguori
        memcpy(new_name, old_name, end - old_name);
2413 8cf89e00 Anthony Liguori
        memcpy(new_name + (end - old_name), vs->v9stat.name.data,
2414 8cf89e00 Anthony Liguori
                vs->v9stat.name.size);
2415 8cf89e00 Anthony Liguori
        vs->nname.data = new_name;
2416 8cf89e00 Anthony Liguori
        vs->nname.size = strlen(new_name);
2417 8cf89e00 Anthony Liguori
2418 8cf89e00 Anthony Liguori
        if (strcmp(new_name, vs->fidp->path.data) != 0) {
2419 8cf89e00 Anthony Liguori
            if (v9fs_do_rename(s, &vs->fidp->path, &vs->nname)) {
2420 8cf89e00 Anthony Liguori
                err = -errno;
2421 8cf89e00 Anthony Liguori
            } else {
2422 8cf89e00 Anthony Liguori
                /*
2423 8cf89e00 Anthony Liguori
                 * Fixup fid's pointing to the old name to
2424 8cf89e00 Anthony Liguori
                 * start pointing to the new name
2425 8cf89e00 Anthony Liguori
                 */
2426 8cf89e00 Anthony Liguori
                for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2427 8cf89e00 Anthony Liguori
2428 8cf89e00 Anthony Liguori
                    if (vs->fidp == fidp) {
2429 8cf89e00 Anthony Liguori
                        /*
2430 8cf89e00 Anthony Liguori
                         * we replace name of this fid towards the end
2431 8cf89e00 Anthony Liguori
                         * so that our below strcmp will work
2432 8cf89e00 Anthony Liguori
                         */
2433 8cf89e00 Anthony Liguori
                        continue;
2434 8cf89e00 Anthony Liguori
                    }
2435 8cf89e00 Anthony Liguori
                    if (!strncmp(vs->fidp->path.data, fidp->path.data,
2436 8cf89e00 Anthony Liguori
                                 strlen(vs->fidp->path.data))) {
2437 8cf89e00 Anthony Liguori
                        /* replace the name */
2438 8cf89e00 Anthony Liguori
                        v9fs_fix_path(&fidp->path, &vs->nname,
2439 8cf89e00 Anthony Liguori
                                      strlen(vs->fidp->path.data));
2440 8cf89e00 Anthony Liguori
                    }
2441 8cf89e00 Anthony Liguori
                }
2442 8cf89e00 Anthony Liguori
                v9fs_string_copy(&vs->fidp->path, &vs->nname);
2443 8cf89e00 Anthony Liguori
            }
2444 8cf89e00 Anthony Liguori
        }
2445 8cf89e00 Anthony Liguori
    }
2446 8cf89e00 Anthony Liguori
    v9fs_wstat_post_rename(s, vs, err);
2447 8cf89e00 Anthony Liguori
    return;
2448 8cf89e00 Anthony Liguori
2449 8cf89e00 Anthony Liguori
out:
2450 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2451 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2452 8cf89e00 Anthony Liguori
    qemu_free(vs);
2453 8cf89e00 Anthony Liguori
}
2454 8cf89e00 Anthony Liguori
2455 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2456 8cf89e00 Anthony Liguori
{
2457 8cf89e00 Anthony Liguori
    if (err < 0) {
2458 8cf89e00 Anthony Liguori
        goto out;
2459 8cf89e00 Anthony Liguori
    }
2460 8cf89e00 Anthony Liguori
2461 f7613bee Venkateswararao Jujjuri (JV)
    if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) {
2462 8cf89e00 Anthony Liguori
        if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2463 8cf89e00 Anthony Liguori
                    vs->v9stat.n_gid)) {
2464 8cf89e00 Anthony Liguori
            err = -errno;
2465 8cf89e00 Anthony Liguori
        }
2466 8cf89e00 Anthony Liguori
    }
2467 8cf89e00 Anthony Liguori
    v9fs_wstat_post_chown(s, vs, err);
2468 8cf89e00 Anthony Liguori
    return;
2469 8cf89e00 Anthony Liguori
2470 8cf89e00 Anthony Liguori
out:
2471 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2472 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2473 8cf89e00 Anthony Liguori
    qemu_free(vs);
2474 8cf89e00 Anthony Liguori
}
2475 8cf89e00 Anthony Liguori
2476 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2477 8cf89e00 Anthony Liguori
{
2478 8cf89e00 Anthony Liguori
    if (err < 0) {
2479 8cf89e00 Anthony Liguori
        goto out;
2480 8cf89e00 Anthony Liguori
    }
2481 8cf89e00 Anthony Liguori
2482 74bc02b2 M. Mohan Kumar
    if (vs->v9stat.mtime != -1 || vs->v9stat.atime != -1) {
2483 8fc39ae4 Sripathi Kodi
        struct timespec times[2];
2484 8fc39ae4 Sripathi Kodi
        if (vs->v9stat.atime != -1) {
2485 8fc39ae4 Sripathi Kodi
            times[0].tv_sec = vs->v9stat.atime;
2486 8fc39ae4 Sripathi Kodi
            times[0].tv_nsec = 0;
2487 8fc39ae4 Sripathi Kodi
        } else {
2488 8fc39ae4 Sripathi Kodi
            times[0].tv_nsec = UTIME_OMIT;
2489 8fc39ae4 Sripathi Kodi
        }
2490 8fc39ae4 Sripathi Kodi
        if (vs->v9stat.mtime != -1) {
2491 8fc39ae4 Sripathi Kodi
            times[1].tv_sec = vs->v9stat.mtime;
2492 8fc39ae4 Sripathi Kodi
            times[1].tv_nsec = 0;
2493 8fc39ae4 Sripathi Kodi
        } else {
2494 8fc39ae4 Sripathi Kodi
            times[1].tv_nsec = UTIME_OMIT;
2495 8fc39ae4 Sripathi Kodi
        }
2496 8fc39ae4 Sripathi Kodi
2497 8fc39ae4 Sripathi Kodi
        if (v9fs_do_utimensat(s, &vs->fidp->path, times)) {
2498 8cf89e00 Anthony Liguori
            err = -errno;
2499 8cf89e00 Anthony Liguori
        }
2500 8cf89e00 Anthony Liguori
    }
2501 8cf89e00 Anthony Liguori
2502 8cf89e00 Anthony Liguori
    v9fs_wstat_post_utime(s, vs, err);
2503 8cf89e00 Anthony Liguori
    return;
2504 8cf89e00 Anthony Liguori
2505 8cf89e00 Anthony Liguori
out:
2506 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2507 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2508 8cf89e00 Anthony Liguori
    qemu_free(vs);
2509 8cf89e00 Anthony Liguori
}
2510 8cf89e00 Anthony Liguori
2511 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2512 8cf89e00 Anthony Liguori
{
2513 8cf89e00 Anthony Liguori
    if (err == -1) {
2514 8cf89e00 Anthony Liguori
        err = -errno;
2515 8cf89e00 Anthony Liguori
    }
2516 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2517 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2518 8cf89e00 Anthony Liguori
    qemu_free(vs);
2519 8cf89e00 Anthony Liguori
}
2520 8cf89e00 Anthony Liguori
2521 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2522 8cf89e00 Anthony Liguori
{
2523 8cf89e00 Anthony Liguori
    uint32_t v9_mode;
2524 8cf89e00 Anthony Liguori
2525 8cf89e00 Anthony Liguori
    if (err == -1) {
2526 8cf89e00 Anthony Liguori
        err = -errno;
2527 8cf89e00 Anthony Liguori
        goto out;
2528 8cf89e00 Anthony Liguori
    }
2529 8cf89e00 Anthony Liguori
2530 8cf89e00 Anthony Liguori
    v9_mode = stat_to_v9mode(&vs->stbuf);
2531 8cf89e00 Anthony Liguori
2532 8cf89e00 Anthony Liguori
    if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2533 8cf89e00 Anthony Liguori
        (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2534 8cf89e00 Anthony Liguori
            /* Attempting to change the type */
2535 8cf89e00 Anthony Liguori
            err = -EIO;
2536 8cf89e00 Anthony Liguori
            goto out;
2537 8cf89e00 Anthony Liguori
    }
2538 8cf89e00 Anthony Liguori
2539 8cf89e00 Anthony Liguori
    if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2540 8cf89e00 Anthony Liguori
                    &vs->v9stat.extension))) {
2541 8cf89e00 Anthony Liguori
            err = -errno;
2542 8cf89e00 Anthony Liguori
     }
2543 8cf89e00 Anthony Liguori
    v9fs_wstat_post_chmod(s, vs, err);
2544 8cf89e00 Anthony Liguori
    return;
2545 8cf89e00 Anthony Liguori
2546 8cf89e00 Anthony Liguori
out:
2547 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2548 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2549 8cf89e00 Anthony Liguori
    qemu_free(vs);
2550 8cf89e00 Anthony Liguori
}
2551 8cf89e00 Anthony Liguori
2552 9f107513 Anthony Liguori
static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
2553 9f107513 Anthony Liguori
{
2554 8cf89e00 Anthony Liguori
    int32_t fid;
2555 8cf89e00 Anthony Liguori
    V9fsWstatState *vs;
2556 8cf89e00 Anthony Liguori
    int err = 0;
2557 8cf89e00 Anthony Liguori
2558 8cf89e00 Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
2559 8cf89e00 Anthony Liguori
    vs->pdu = pdu;
2560 8cf89e00 Anthony Liguori
    vs->offset = 7;
2561 8cf89e00 Anthony Liguori
2562 8cf89e00 Anthony Liguori
    pdu_unmarshal(pdu, vs->offset, "dwS", &fid, &vs->unused, &vs->v9stat);
2563 8cf89e00 Anthony Liguori
2564 8cf89e00 Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
2565 8cf89e00 Anthony Liguori
    if (vs->fidp == NULL) {
2566 8cf89e00 Anthony Liguori
        err = -EINVAL;
2567 8cf89e00 Anthony Liguori
        goto out;
2568 9f107513 Anthony Liguori
    }
2569 8cf89e00 Anthony Liguori
2570 8cf89e00 Anthony Liguori
    /* do we need to sync the file? */
2571 8cf89e00 Anthony Liguori
    if (donttouch_stat(&vs->v9stat)) {
2572 8cf89e00 Anthony Liguori
        err = v9fs_do_fsync(s, vs->fidp->fd);
2573 8cf89e00 Anthony Liguori
        v9fs_wstat_post_fsync(s, vs, err);
2574 8cf89e00 Anthony Liguori
        return;
2575 8cf89e00 Anthony Liguori
    }
2576 8cf89e00 Anthony Liguori
2577 8cf89e00 Anthony Liguori
    if (vs->v9stat.mode != -1) {
2578 8cf89e00 Anthony Liguori
        err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
2579 8cf89e00 Anthony Liguori
        v9fs_wstat_post_lstat(s, vs, err);
2580 8cf89e00 Anthony Liguori
        return;
2581 8cf89e00 Anthony Liguori
    }
2582 8cf89e00 Anthony Liguori
2583 8cf89e00 Anthony Liguori
    v9fs_wstat_post_chmod(s, vs, err);
2584 8cf89e00 Anthony Liguori
    return;
2585 8cf89e00 Anthony Liguori
2586 8cf89e00 Anthony Liguori
out:
2587 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2588 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2589 8cf89e00 Anthony Liguori
    qemu_free(vs);
2590 9f107513 Anthony Liguori
}
2591 9f107513 Anthony Liguori
2592 be940c87 M. Mohan Kumar
static void v9fs_statfs_post_statfs(V9fsState *s, V9fsStatfsState *vs, int err)
2593 be940c87 M. Mohan Kumar
{
2594 5e94c103 M. Mohan Kumar
    int32_t bsize_factor;
2595 5e94c103 M. Mohan Kumar
2596 be940c87 M. Mohan Kumar
    if (err) {
2597 be940c87 M. Mohan Kumar
        err = -errno;
2598 be940c87 M. Mohan Kumar
        goto out;
2599 be940c87 M. Mohan Kumar
    }
2600 be940c87 M. Mohan Kumar
2601 5e94c103 M. Mohan Kumar
    /*
2602 5e94c103 M. Mohan Kumar
     * compute bsize factor based on host file system block size
2603 5e94c103 M. Mohan Kumar
     * and client msize
2604 5e94c103 M. Mohan Kumar
     */
2605 5e94c103 M. Mohan Kumar
    bsize_factor = (s->msize - P9_IOHDRSZ)/vs->stbuf.f_bsize;
2606 5e94c103 M. Mohan Kumar
    if (!bsize_factor) {
2607 5e94c103 M. Mohan Kumar
        bsize_factor = 1;
2608 5e94c103 M. Mohan Kumar
    }
2609 be940c87 M. Mohan Kumar
    vs->v9statfs.f_type = vs->stbuf.f_type;
2610 be940c87 M. Mohan Kumar
    vs->v9statfs.f_bsize = vs->stbuf.f_bsize;
2611 5e94c103 M. Mohan Kumar
    vs->v9statfs.f_bsize *= bsize_factor;
2612 5e94c103 M. Mohan Kumar
    /*
2613 5e94c103 M. Mohan Kumar
     * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2614 5e94c103 M. Mohan Kumar
     * adjust(divide) the number of blocks, free blocks and available
2615 5e94c103 M. Mohan Kumar
     * blocks by bsize factor
2616 5e94c103 M. Mohan Kumar
     */
2617 5e94c103 M. Mohan Kumar
    vs->v9statfs.f_blocks = vs->stbuf.f_blocks/bsize_factor;
2618 5e94c103 M. Mohan Kumar
    vs->v9statfs.f_bfree = vs->stbuf.f_bfree/bsize_factor;
2619 5e94c103 M. Mohan Kumar
    vs->v9statfs.f_bavail = vs->stbuf.f_bavail/bsize_factor;
2620 be940c87 M. Mohan Kumar
    vs->v9statfs.f_files = vs->stbuf.f_files;
2621 be940c87 M. Mohan Kumar
    vs->v9statfs.f_ffree = vs->stbuf.f_ffree;
2622 be940c87 M. Mohan Kumar
    vs->v9statfs.fsid_val = (unsigned int) vs->stbuf.f_fsid.__val[0] |
2623 be940c87 M. Mohan Kumar
                        (unsigned long long)vs->stbuf.f_fsid.__val[1] << 32;
2624 be940c87 M. Mohan Kumar
    vs->v9statfs.f_namelen = vs->stbuf.f_namelen;
2625 be940c87 M. Mohan Kumar
2626 be940c87 M. Mohan Kumar
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "ddqqqqqqd",
2627 be940c87 M. Mohan Kumar
         vs->v9statfs.f_type, vs->v9statfs.f_bsize, vs->v9statfs.f_blocks,
2628 be940c87 M. Mohan Kumar
         vs->v9statfs.f_bfree, vs->v9statfs.f_bavail, vs->v9statfs.f_files,
2629 be940c87 M. Mohan Kumar
         vs->v9statfs.f_ffree, vs->v9statfs.fsid_val,
2630 be940c87 M. Mohan Kumar
         vs->v9statfs.f_namelen);
2631 be940c87 M. Mohan Kumar
2632 be940c87 M. Mohan Kumar
out:
2633 be940c87 M. Mohan Kumar
    complete_pdu(s, vs->pdu, vs->offset);
2634 be940c87 M. Mohan Kumar
    qemu_free(vs);
2635 be940c87 M. Mohan Kumar
}
2636 be940c87 M. Mohan Kumar
2637 be940c87 M. Mohan Kumar
static void v9fs_statfs(V9fsState *s, V9fsPDU *pdu)
2638 be940c87 M. Mohan Kumar
{
2639 be940c87 M. Mohan Kumar
    V9fsStatfsState *vs;
2640 be940c87 M. Mohan Kumar
    ssize_t err = 0;
2641 be940c87 M. Mohan Kumar
2642 be940c87 M. Mohan Kumar
    vs = qemu_malloc(sizeof(*vs));
2643 be940c87 M. Mohan Kumar
    vs->pdu = pdu;
2644 be940c87 M. Mohan Kumar
    vs->offset = 7;
2645 be940c87 M. Mohan Kumar
2646 be940c87 M. Mohan Kumar
    memset(&vs->v9statfs, 0, sizeof(vs->v9statfs));
2647 be940c87 M. Mohan Kumar
2648 be940c87 M. Mohan Kumar
    pdu_unmarshal(vs->pdu, vs->offset, "d", &vs->fid);
2649 be940c87 M. Mohan Kumar
2650 be940c87 M. Mohan Kumar
    vs->fidp = lookup_fid(s, vs->fid);
2651 be940c87 M. Mohan Kumar
    if (vs->fidp == NULL) {
2652 be940c87 M. Mohan Kumar
        err = -ENOENT;
2653 be940c87 M. Mohan Kumar
        goto out;
2654 be940c87 M. Mohan Kumar
    }
2655 be940c87 M. Mohan Kumar
2656 be940c87 M. Mohan Kumar
    err = v9fs_do_statfs(s, &vs->fidp->path, &vs->stbuf);
2657 be940c87 M. Mohan Kumar
    v9fs_statfs_post_statfs(s, vs, err);
2658 be940c87 M. Mohan Kumar
    return;
2659 be940c87 M. Mohan Kumar
2660 be940c87 M. Mohan Kumar
out:
2661 be940c87 M. Mohan Kumar
    complete_pdu(s, vs->pdu, err);
2662 be940c87 M. Mohan Kumar
    qemu_free(vs);
2663 be940c87 M. Mohan Kumar
}
2664 be940c87 M. Mohan Kumar
2665 9f107513 Anthony Liguori
typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
2666 9f107513 Anthony Liguori
2667 9f107513 Anthony Liguori
static pdu_handler_t *pdu_handlers[] = {
2668 c18e2f94 Sripathi Kodi
    [P9_TREADDIR] = v9fs_readdir,
2669 be940c87 M. Mohan Kumar
    [P9_TSTATFS] = v9fs_statfs,
2670 00ede4c2 Sripathi Kodi
    [P9_TGETATTR] = v9fs_getattr,
2671 c79ce737 Sripathi Kodi
    [P9_TSETATTR] = v9fs_setattr,
2672 9f107513 Anthony Liguori
    [P9_TVERSION] = v9fs_version,
2673 9f107513 Anthony Liguori
    [P9_TATTACH] = v9fs_attach,
2674 9f107513 Anthony Liguori
    [P9_TSTAT] = v9fs_stat,
2675 9f107513 Anthony Liguori
    [P9_TWALK] = v9fs_walk,
2676 9f107513 Anthony Liguori
    [P9_TCLUNK] = v9fs_clunk,
2677 9f107513 Anthony Liguori
    [P9_TOPEN] = v9fs_open,
2678 9f107513 Anthony Liguori
    [P9_TREAD] = v9fs_read,
2679 9f107513 Anthony Liguori
#if 0
2680 9f107513 Anthony Liguori
    [P9_TAUTH] = v9fs_auth,
2681 9f107513 Anthony Liguori
#endif
2682 9f107513 Anthony Liguori
    [P9_TFLUSH] = v9fs_flush,
2683 9f107513 Anthony Liguori
    [P9_TCREATE] = v9fs_create,
2684 9f107513 Anthony Liguori
    [P9_TWRITE] = v9fs_write,
2685 9f107513 Anthony Liguori
    [P9_TWSTAT] = v9fs_wstat,
2686 9f107513 Anthony Liguori
    [P9_TREMOVE] = v9fs_remove,
2687 9f107513 Anthony Liguori
};
2688 9f107513 Anthony Liguori
2689 9f107513 Anthony Liguori
static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
2690 9f107513 Anthony Liguori
{
2691 9f107513 Anthony Liguori
    pdu_handler_t *handler;
2692 9f107513 Anthony Liguori
2693 9f107513 Anthony Liguori
    if (debug_9p_pdu) {
2694 9f107513 Anthony Liguori
        pprint_pdu(pdu);
2695 9f107513 Anthony Liguori
    }
2696 9f107513 Anthony Liguori
2697 9f107513 Anthony Liguori
    BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
2698 9f107513 Anthony Liguori
2699 9f107513 Anthony Liguori
    handler = pdu_handlers[pdu->id];
2700 9f107513 Anthony Liguori
    BUG_ON(handler == NULL);
2701 9f107513 Anthony Liguori
2702 9f107513 Anthony Liguori
    handler(s, pdu);
2703 9f107513 Anthony Liguori
}
2704 9f107513 Anthony Liguori
2705 9f107513 Anthony Liguori
static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
2706 9f107513 Anthony Liguori
{
2707 9f107513 Anthony Liguori
    V9fsState *s = (V9fsState *)vdev;
2708 9f107513 Anthony Liguori
    V9fsPDU *pdu;
2709 9f107513 Anthony Liguori
    ssize_t len;
2710 9f107513 Anthony Liguori
2711 9f107513 Anthony Liguori
    while ((pdu = alloc_pdu(s)) &&
2712 9f107513 Anthony Liguori
            (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
2713 9f107513 Anthony Liguori
        uint8_t *ptr;
2714 9f107513 Anthony Liguori
2715 9f107513 Anthony Liguori
        BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
2716 9f107513 Anthony Liguori
        BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
2717 9f107513 Anthony Liguori
2718 9f107513 Anthony Liguori
        ptr = pdu->elem.out_sg[0].iov_base;
2719 9f107513 Anthony Liguori
2720 9f107513 Anthony Liguori
        memcpy(&pdu->size, ptr, 4);
2721 9f107513 Anthony Liguori
        pdu->id = ptr[4];
2722 9f107513 Anthony Liguori
        memcpy(&pdu->tag, ptr + 5, 2);
2723 9f107513 Anthony Liguori
2724 9f107513 Anthony Liguori
        submit_pdu(s, pdu);
2725 9f107513 Anthony Liguori
    }
2726 9f107513 Anthony Liguori
2727 9f107513 Anthony Liguori
    free_pdu(s, pdu);
2728 9f107513 Anthony Liguori
}
2729 9f107513 Anthony Liguori
2730 9f107513 Anthony Liguori
static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
2731 9f107513 Anthony Liguori
{
2732 9f107513 Anthony Liguori
    features |= 1 << VIRTIO_9P_MOUNT_TAG;
2733 9f107513 Anthony Liguori
    return features;
2734 9f107513 Anthony Liguori
}
2735 9f107513 Anthony Liguori
2736 9f107513 Anthony Liguori
static V9fsState *to_virtio_9p(VirtIODevice *vdev)
2737 9f107513 Anthony Liguori
{
2738 9f107513 Anthony Liguori
    return (V9fsState *)vdev;
2739 9f107513 Anthony Liguori
}
2740 9f107513 Anthony Liguori
2741 9f107513 Anthony Liguori
static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
2742 9f107513 Anthony Liguori
{
2743 9f107513 Anthony Liguori
    struct virtio_9p_config *cfg;
2744 9f107513 Anthony Liguori
    V9fsState *s = to_virtio_9p(vdev);
2745 9f107513 Anthony Liguori
2746 9f107513 Anthony Liguori
    cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
2747 9f107513 Anthony Liguori
                        s->tag_len);
2748 9f107513 Anthony Liguori
    stw_raw(&cfg->tag_len, s->tag_len);
2749 9f107513 Anthony Liguori
    memcpy(cfg->tag, s->tag, s->tag_len);
2750 9f107513 Anthony Liguori
    memcpy(config, cfg, s->config_size);
2751 9f107513 Anthony Liguori
    qemu_free(cfg);
2752 9f107513 Anthony Liguori
}
2753 9f107513 Anthony Liguori
2754 9f107513 Anthony Liguori
VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
2755 9f107513 Anthony Liguori
 {
2756 9f107513 Anthony Liguori
    V9fsState *s;
2757 9f107513 Anthony Liguori
    int i, len;
2758 9f107513 Anthony Liguori
    struct stat stat;
2759 9f107513 Anthony Liguori
    FsTypeEntry *fse;
2760 9f107513 Anthony Liguori
2761 9f107513 Anthony Liguori
2762 9f107513 Anthony Liguori
    s = (V9fsState *)virtio_common_init("virtio-9p",
2763 9f107513 Anthony Liguori
                                    VIRTIO_ID_9P,
2764 9f107513 Anthony Liguori
                                    sizeof(struct virtio_9p_config)+
2765 9f107513 Anthony Liguori
                                    MAX_TAG_LEN,
2766 9f107513 Anthony Liguori
                                    sizeof(V9fsState));
2767 9f107513 Anthony Liguori
2768 9f107513 Anthony Liguori
    /* initialize pdu allocator */
2769 9f107513 Anthony Liguori
    QLIST_INIT(&s->free_list);
2770 9f107513 Anthony Liguori
    for (i = 0; i < (MAX_REQ - 1); i++) {
2771 9f107513 Anthony Liguori
        QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
2772 9f107513 Anthony Liguori
    }
2773 9f107513 Anthony Liguori
2774 9f107513 Anthony Liguori
    s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
2775 9f107513 Anthony Liguori
2776 9f107513 Anthony Liguori
    fse = get_fsdev_fsentry(conf->fsdev_id);
2777 9f107513 Anthony Liguori
2778 9f107513 Anthony Liguori
    if (!fse) {
2779 9f107513 Anthony Liguori
        /* We don't have a fsdev identified by fsdev_id */
2780 9f107513 Anthony Liguori
        fprintf(stderr, "Virtio-9p device couldn't find fsdev "
2781 9f107513 Anthony Liguori
                    "with the id %s\n", conf->fsdev_id);
2782 9f107513 Anthony Liguori
        exit(1);
2783 9f107513 Anthony Liguori
    }
2784 9f107513 Anthony Liguori
2785 9f107513 Anthony Liguori
    if (!fse->path || !conf->tag) {
2786 9f107513 Anthony Liguori
        /* we haven't specified a mount_tag or the path */
2787 9f107513 Anthony Liguori
        fprintf(stderr, "fsdev with id %s needs path "
2788 9f107513 Anthony Liguori
                "and Virtio-9p device needs mount_tag arguments\n",
2789 9f107513 Anthony Liguori
                conf->fsdev_id);
2790 9f107513 Anthony Liguori
        exit(1);
2791 9f107513 Anthony Liguori
    }
2792 9f107513 Anthony Liguori
2793 758e8e38 Venkateswararao Jujjuri (JV)
    if (!strcmp(fse->security_model, "passthrough")) {
2794 758e8e38 Venkateswararao Jujjuri (JV)
        /* Files on the Fileserver set to client user credentials */
2795 758e8e38 Venkateswararao Jujjuri (JV)
        s->ctx.fs_sm = SM_PASSTHROUGH;
2796 758e8e38 Venkateswararao Jujjuri (JV)
    } else if (!strcmp(fse->security_model, "mapped")) {
2797 758e8e38 Venkateswararao Jujjuri (JV)
        /* Files on the fileserver are set to QEMU credentials.
2798 758e8e38 Venkateswararao Jujjuri (JV)
         * Client user credentials are saved in extended attributes.
2799 758e8e38 Venkateswararao Jujjuri (JV)
         */
2800 758e8e38 Venkateswararao Jujjuri (JV)
        s->ctx.fs_sm = SM_MAPPED;
2801 758e8e38 Venkateswararao Jujjuri (JV)
    } else {
2802 9ce56db6 Venkateswararao Jujjuri (JV)
        /* user haven't specified a correct security option */
2803 9ce56db6 Venkateswararao Jujjuri (JV)
        fprintf(stderr, "one of the following must be specified as the"
2804 9ce56db6 Venkateswararao Jujjuri (JV)
                "security option:\n\t security_model=passthrough \n\t "
2805 9ce56db6 Venkateswararao Jujjuri (JV)
                "security_model=mapped\n");
2806 9ce56db6 Venkateswararao Jujjuri (JV)
        return NULL;
2807 9ce56db6 Venkateswararao Jujjuri (JV)
    }
2808 9ce56db6 Venkateswararao Jujjuri (JV)
2809 9f107513 Anthony Liguori
    if (lstat(fse->path, &stat)) {
2810 9f107513 Anthony Liguori
        fprintf(stderr, "share path %s does not exist\n", fse->path);
2811 9f107513 Anthony Liguori
        exit(1);
2812 9f107513 Anthony Liguori
    } else if (!S_ISDIR(stat.st_mode)) {
2813 9f107513 Anthony Liguori
        fprintf(stderr, "share path %s is not a directory \n", fse->path);
2814 9f107513 Anthony Liguori
        exit(1);
2815 9f107513 Anthony Liguori
    }
2816 9f107513 Anthony Liguori
2817 9f107513 Anthony Liguori
    s->ctx.fs_root = qemu_strdup(fse->path);
2818 9f107513 Anthony Liguori
    len = strlen(conf->tag);
2819 9f107513 Anthony Liguori
    if (len > MAX_TAG_LEN) {
2820 9f107513 Anthony Liguori
        len = MAX_TAG_LEN;
2821 9f107513 Anthony Liguori
    }
2822 9f107513 Anthony Liguori
    /* s->tag is non-NULL terminated string */
2823 9f107513 Anthony Liguori
    s->tag = qemu_malloc(len);
2824 9f107513 Anthony Liguori
    memcpy(s->tag, conf->tag, len);
2825 9f107513 Anthony Liguori
    s->tag_len = len;
2826 9f107513 Anthony Liguori
    s->ctx.uid = -1;
2827 9f107513 Anthony Liguori
2828 9f107513 Anthony Liguori
    s->ops = fse->ops;
2829 9f107513 Anthony Liguori
    s->vdev.get_features = virtio_9p_get_features;
2830 9f107513 Anthony Liguori
    s->config_size = sizeof(struct virtio_9p_config) +
2831 9f107513 Anthony Liguori
                        s->tag_len;
2832 9f107513 Anthony Liguori
    s->vdev.get_config = virtio_9p_get_config;
2833 9f107513 Anthony Liguori
2834 9f107513 Anthony Liguori
    return &s->vdev;
2835 9f107513 Anthony Liguori
}