Statistics
| Branch: | Revision:

root / hw / virtio-9p.c @ 10b468bd

History | View | Annotate | Download (89.3 kB)

1 9f107513 Anthony Liguori
/*
2 9f107513 Anthony Liguori
 * Virtio 9p backend
3 9f107513 Anthony Liguori
 *
4 9f107513 Anthony Liguori
 * Copyright IBM, Corp. 2010
5 9f107513 Anthony Liguori
 *
6 9f107513 Anthony Liguori
 * Authors:
7 9f107513 Anthony Liguori
 *  Anthony Liguori   <aliguori@us.ibm.com>
8 9f107513 Anthony Liguori
 *
9 9f107513 Anthony Liguori
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10 9f107513 Anthony Liguori
 * the COPYING file in the top-level directory.
11 9f107513 Anthony Liguori
 *
12 9f107513 Anthony Liguori
 */
13 9f107513 Anthony Liguori
14 9f107513 Anthony Liguori
#include "virtio.h"
15 9f107513 Anthony Liguori
#include "pc.h"
16 9f107513 Anthony Liguori
#include "qemu_socket.h"
17 9f107513 Anthony Liguori
#include "virtio-9p.h"
18 9f107513 Anthony Liguori
#include "fsdev/qemu-fsdev.h"
19 9f107513 Anthony Liguori
#include "virtio-9p-debug.h"
20 9f107513 Anthony Liguori
21 9f107513 Anthony Liguori
int dotu = 1;
22 9f107513 Anthony Liguori
int debug_9p_pdu;
23 9f107513 Anthony Liguori
24 fac4f111 Venkateswararao Jujjuri (JV)
enum {
25 fac4f111 Venkateswararao Jujjuri (JV)
    Oread   = 0x00,
26 fac4f111 Venkateswararao Jujjuri (JV)
    Owrite  = 0x01,
27 fac4f111 Venkateswararao Jujjuri (JV)
    Ordwr   = 0x02,
28 fac4f111 Venkateswararao Jujjuri (JV)
    Oexec   = 0x03,
29 fac4f111 Venkateswararao Jujjuri (JV)
    Oexcl   = 0x04,
30 fac4f111 Venkateswararao Jujjuri (JV)
    Otrunc  = 0x10,
31 fac4f111 Venkateswararao Jujjuri (JV)
    Orexec  = 0x20,
32 fac4f111 Venkateswararao Jujjuri (JV)
    Orclose = 0x40,
33 fac4f111 Venkateswararao Jujjuri (JV)
    Oappend = 0x80,
34 fac4f111 Venkateswararao Jujjuri (JV)
};
35 fac4f111 Venkateswararao Jujjuri (JV)
36 fac4f111 Venkateswararao Jujjuri (JV)
static int omode_to_uflags(int8_t mode)
37 fac4f111 Venkateswararao Jujjuri (JV)
{
38 fac4f111 Venkateswararao Jujjuri (JV)
    int ret = 0;
39 fac4f111 Venkateswararao Jujjuri (JV)
40 fac4f111 Venkateswararao Jujjuri (JV)
    switch (mode & 3) {
41 fac4f111 Venkateswararao Jujjuri (JV)
    case Oread:
42 fac4f111 Venkateswararao Jujjuri (JV)
        ret = O_RDONLY;
43 fac4f111 Venkateswararao Jujjuri (JV)
        break;
44 fac4f111 Venkateswararao Jujjuri (JV)
    case Ordwr:
45 fac4f111 Venkateswararao Jujjuri (JV)
        ret = O_RDWR;
46 fac4f111 Venkateswararao Jujjuri (JV)
        break;
47 fac4f111 Venkateswararao Jujjuri (JV)
    case Owrite:
48 fac4f111 Venkateswararao Jujjuri (JV)
        ret = O_WRONLY;
49 fac4f111 Venkateswararao Jujjuri (JV)
        break;
50 fac4f111 Venkateswararao Jujjuri (JV)
    case Oexec:
51 fac4f111 Venkateswararao Jujjuri (JV)
        ret = O_RDONLY;
52 fac4f111 Venkateswararao Jujjuri (JV)
        break;
53 fac4f111 Venkateswararao Jujjuri (JV)
    }
54 fac4f111 Venkateswararao Jujjuri (JV)
55 fac4f111 Venkateswararao Jujjuri (JV)
    if (mode & Otrunc) {
56 fac4f111 Venkateswararao Jujjuri (JV)
        ret |= O_TRUNC;
57 fac4f111 Venkateswararao Jujjuri (JV)
    }
58 fac4f111 Venkateswararao Jujjuri (JV)
59 fac4f111 Venkateswararao Jujjuri (JV)
    if (mode & Oappend) {
60 fac4f111 Venkateswararao Jujjuri (JV)
        ret |= O_APPEND;
61 fac4f111 Venkateswararao Jujjuri (JV)
    }
62 fac4f111 Venkateswararao Jujjuri (JV)
63 fac4f111 Venkateswararao Jujjuri (JV)
    if (mode & Oexcl) {
64 fac4f111 Venkateswararao Jujjuri (JV)
        ret |= O_EXCL;
65 fac4f111 Venkateswararao Jujjuri (JV)
    }
66 fac4f111 Venkateswararao Jujjuri (JV)
67 fac4f111 Venkateswararao Jujjuri (JV)
    return ret;
68 fac4f111 Venkateswararao Jujjuri (JV)
}
69 fac4f111 Venkateswararao Jujjuri (JV)
70 758e8e38 Venkateswararao Jujjuri (JV)
void cred_init(FsCred *credp)
71 131dcb25 Anthony Liguori
{
72 758e8e38 Venkateswararao Jujjuri (JV)
    credp->fc_uid = -1;
73 758e8e38 Venkateswararao Jujjuri (JV)
    credp->fc_gid = -1;
74 758e8e38 Venkateswararao Jujjuri (JV)
    credp->fc_mode = -1;
75 758e8e38 Venkateswararao Jujjuri (JV)
    credp->fc_rdev = -1;
76 131dcb25 Anthony Liguori
}
77 131dcb25 Anthony Liguori
78 758e8e38 Venkateswararao Jujjuri (JV)
static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf)
79 131dcb25 Anthony Liguori
{
80 758e8e38 Venkateswararao Jujjuri (JV)
    return s->ops->lstat(&s->ctx, path->data, stbuf);
81 131dcb25 Anthony Liguori
}
82 131dcb25 Anthony Liguori
83 131dcb25 Anthony Liguori
static ssize_t v9fs_do_readlink(V9fsState *s, V9fsString *path, V9fsString *buf)
84 131dcb25 Anthony Liguori
{
85 131dcb25 Anthony Liguori
    ssize_t len;
86 131dcb25 Anthony Liguori
87 131dcb25 Anthony Liguori
    buf->data = qemu_malloc(1024);
88 131dcb25 Anthony Liguori
89 131dcb25 Anthony Liguori
    len = s->ops->readlink(&s->ctx, path->data, buf->data, 1024 - 1);
90 131dcb25 Anthony Liguori
    if (len > -1) {
91 131dcb25 Anthony Liguori
        buf->size = len;
92 131dcb25 Anthony Liguori
        buf->data[len] = 0;
93 131dcb25 Anthony Liguori
    }
94 131dcb25 Anthony Liguori
95 131dcb25 Anthony Liguori
    return len;
96 131dcb25 Anthony Liguori
}
97 131dcb25 Anthony Liguori
98 131dcb25 Anthony Liguori
static int v9fs_do_close(V9fsState *s, int fd)
99 131dcb25 Anthony Liguori
{
100 131dcb25 Anthony Liguori
    return s->ops->close(&s->ctx, fd);
101 131dcb25 Anthony Liguori
}
102 131dcb25 Anthony Liguori
103 131dcb25 Anthony Liguori
static int v9fs_do_closedir(V9fsState *s, DIR *dir)
104 131dcb25 Anthony Liguori
{
105 131dcb25 Anthony Liguori
    return s->ops->closedir(&s->ctx, dir);
106 131dcb25 Anthony Liguori
}
107 131dcb25 Anthony Liguori
108 a6568fe2 Anthony Liguori
static int v9fs_do_open(V9fsState *s, V9fsString *path, int flags)
109 a6568fe2 Anthony Liguori
{
110 a6568fe2 Anthony Liguori
    return s->ops->open(&s->ctx, path->data, flags);
111 a6568fe2 Anthony Liguori
}
112 a6568fe2 Anthony Liguori
113 a6568fe2 Anthony Liguori
static DIR *v9fs_do_opendir(V9fsState *s, V9fsString *path)
114 a6568fe2 Anthony Liguori
{
115 a6568fe2 Anthony Liguori
    return s->ops->opendir(&s->ctx, path->data);
116 a6568fe2 Anthony Liguori
}
117 a6568fe2 Anthony Liguori
118 a9231555 Anthony Liguori
static void v9fs_do_rewinddir(V9fsState *s, DIR *dir)
119 a9231555 Anthony Liguori
{
120 a9231555 Anthony Liguori
    return s->ops->rewinddir(&s->ctx, dir);
121 a9231555 Anthony Liguori
}
122 a9231555 Anthony Liguori
123 a9231555 Anthony Liguori
static off_t v9fs_do_telldir(V9fsState *s, DIR *dir)
124 a9231555 Anthony Liguori
{
125 a9231555 Anthony Liguori
    return s->ops->telldir(&s->ctx, dir);
126 a9231555 Anthony Liguori
}
127 a9231555 Anthony Liguori
128 a9231555 Anthony Liguori
static struct dirent *v9fs_do_readdir(V9fsState *s, DIR *dir)
129 a9231555 Anthony Liguori
{
130 a9231555 Anthony Liguori
    return s->ops->readdir(&s->ctx, dir);
131 a9231555 Anthony Liguori
}
132 a9231555 Anthony Liguori
133 a9231555 Anthony Liguori
static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off)
134 a9231555 Anthony Liguori
{
135 a9231555 Anthony Liguori
    return s->ops->seekdir(&s->ctx, dir, off);
136 a9231555 Anthony Liguori
}
137 a9231555 Anthony Liguori
138 a9231555 Anthony Liguori
static int v9fs_do_readv(V9fsState *s, int fd, const struct iovec *iov,
139 a9231555 Anthony Liguori
                            int iovcnt)
140 a9231555 Anthony Liguori
{
141 a9231555 Anthony Liguori
    return s->ops->readv(&s->ctx, fd, iov, iovcnt);
142 a9231555 Anthony Liguori
}
143 a9231555 Anthony Liguori
144 a9231555 Anthony Liguori
static off_t v9fs_do_lseek(V9fsState *s, int fd, off_t offset, int whence)
145 a9231555 Anthony Liguori
{
146 a9231555 Anthony Liguori
    return s->ops->lseek(&s->ctx, fd, offset, whence);
147 a9231555 Anthony Liguori
}
148 a9231555 Anthony Liguori
149 8449360c Anthony Liguori
static int v9fs_do_writev(V9fsState *s, int fd, const struct iovec *iov,
150 8449360c Anthony Liguori
                       int iovcnt)
151 8449360c Anthony Liguori
{
152 8449360c Anthony Liguori
    return s->ops->writev(&s->ctx, fd, iov, iovcnt);
153 8449360c Anthony Liguori
}
154 8449360c Anthony Liguori
155 c494dd6f Anthony Liguori
static int v9fs_do_chmod(V9fsState *s, V9fsString *path, mode_t mode)
156 c494dd6f Anthony Liguori
{
157 e95ead32 Venkateswararao Jujjuri (JV)
    FsCred cred;
158 e95ead32 Venkateswararao Jujjuri (JV)
    cred_init(&cred);
159 e95ead32 Venkateswararao Jujjuri (JV)
    cred.fc_mode = mode;
160 e95ead32 Venkateswararao Jujjuri (JV)
    return s->ops->chmod(&s->ctx, path->data, &cred);
161 c494dd6f Anthony Liguori
}
162 c494dd6f Anthony Liguori
163 5268cecc M. Mohan Kumar
static int v9fs_do_mknod(V9fsState *s, char *name,
164 5268cecc M. Mohan Kumar
        mode_t mode, dev_t dev, uid_t uid, gid_t gid)
165 c494dd6f Anthony Liguori
{
166 1c293312 Venkateswararao Jujjuri (JV)
    FsCred cred;
167 1c293312 Venkateswararao Jujjuri (JV)
    cred_init(&cred);
168 5268cecc M. Mohan Kumar
    cred.fc_uid = uid;
169 5268cecc M. Mohan Kumar
    cred.fc_gid = gid;
170 1c293312 Venkateswararao Jujjuri (JV)
    cred.fc_mode = mode;
171 1c293312 Venkateswararao Jujjuri (JV)
    cred.fc_rdev = dev;
172 5268cecc M. Mohan Kumar
    return s->ops->mknod(&s->ctx, name, &cred);
173 c494dd6f Anthony Liguori
}
174 c494dd6f Anthony Liguori
175 b67592ea M. Mohan Kumar
static int v9fs_do_mkdir(V9fsState *s, char *name, mode_t mode,
176 b67592ea M. Mohan Kumar
                uid_t uid, gid_t gid)
177 c494dd6f Anthony Liguori
{
178 00ec5c37 Venkateswararao Jujjuri (JV)
    FsCred cred;
179 00ec5c37 Venkateswararao Jujjuri (JV)
180 00ec5c37 Venkateswararao Jujjuri (JV)
    cred_init(&cred);
181 b67592ea M. Mohan Kumar
    cred.fc_uid = uid;
182 b67592ea M. Mohan Kumar
    cred.fc_gid = gid;
183 b67592ea M. Mohan Kumar
    cred.fc_mode = mode;
184 00ec5c37 Venkateswararao Jujjuri (JV)
185 b67592ea M. Mohan Kumar
    return s->ops->mkdir(&s->ctx, name, &cred);
186 c494dd6f Anthony Liguori
}
187 c494dd6f Anthony Liguori
188 c494dd6f Anthony Liguori
static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
189 c494dd6f Anthony Liguori
{
190 c494dd6f Anthony Liguori
    return s->ops->fstat(&s->ctx, fd, stbuf);
191 c494dd6f Anthony Liguori
}
192 c494dd6f Anthony Liguori
193 c1568af5 Venkateswararao Jujjuri (JV)
static int v9fs_do_open2(V9fsState *s, char *fullname, uid_t uid, gid_t gid,
194 c1568af5 Venkateswararao Jujjuri (JV)
        int flags, int mode)
195 c494dd6f Anthony Liguori
{
196 4750a96f Venkateswararao Jujjuri (JV)
    FsCred cred;
197 4750a96f Venkateswararao Jujjuri (JV)
198 4750a96f Venkateswararao Jujjuri (JV)
    cred_init(&cred);
199 c1568af5 Venkateswararao Jujjuri (JV)
    cred.fc_uid = uid;
200 c1568af5 Venkateswararao Jujjuri (JV)
    cred.fc_gid = gid;
201 c1568af5 Venkateswararao Jujjuri (JV)
    cred.fc_mode = mode & 07777;
202 c1568af5 Venkateswararao Jujjuri (JV)
    flags = flags;
203 4750a96f Venkateswararao Jujjuri (JV)
204 c1568af5 Venkateswararao Jujjuri (JV)
    return s->ops->open2(&s->ctx, fullname, flags, &cred);
205 c494dd6f Anthony Liguori
}
206 c494dd6f Anthony Liguori
207 08c60fc9 Venkateswararao Jujjuri (JV)
static int v9fs_do_symlink(V9fsState *s, V9fsFidState *fidp,
208 08c60fc9 Venkateswararao Jujjuri (JV)
        const char *oldpath, const char *newpath, gid_t gid)
209 c494dd6f Anthony Liguori
{
210 879c2813 Venkateswararao Jujjuri (JV)
    FsCred cred;
211 879c2813 Venkateswararao Jujjuri (JV)
    cred_init(&cred);
212 08c60fc9 Venkateswararao Jujjuri (JV)
    cred.fc_uid = fidp->uid;
213 08c60fc9 Venkateswararao Jujjuri (JV)
    cred.fc_gid = gid;
214 08c60fc9 Venkateswararao Jujjuri (JV)
    cred.fc_mode = 0777;
215 879c2813 Venkateswararao Jujjuri (JV)
216 08c60fc9 Venkateswararao Jujjuri (JV)
    return s->ops->symlink(&s->ctx, oldpath, newpath, &cred);
217 c494dd6f Anthony Liguori
}
218 c494dd6f Anthony Liguori
219 c494dd6f Anthony Liguori
static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
220 c494dd6f Anthony Liguori
{
221 c494dd6f Anthony Liguori
    return s->ops->link(&s->ctx, oldpath->data, newpath->data);
222 c494dd6f Anthony Liguori
}
223 c494dd6f Anthony Liguori
224 8cf89e00 Anthony Liguori
static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
225 8cf89e00 Anthony Liguori
{
226 8cf89e00 Anthony Liguori
    return s->ops->truncate(&s->ctx, path->data, size);
227 8cf89e00 Anthony Liguori
}
228 8cf89e00 Anthony Liguori
229 8cf89e00 Anthony Liguori
static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath,
230 8cf89e00 Anthony Liguori
                            V9fsString *newpath)
231 8cf89e00 Anthony Liguori
{
232 8cf89e00 Anthony Liguori
    return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
233 8cf89e00 Anthony Liguori
}
234 8cf89e00 Anthony Liguori
235 8cf89e00 Anthony Liguori
static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
236 8cf89e00 Anthony Liguori
{
237 f7613bee Venkateswararao Jujjuri (JV)
    FsCred cred;
238 f7613bee Venkateswararao Jujjuri (JV)
    cred_init(&cred);
239 f7613bee Venkateswararao Jujjuri (JV)
    cred.fc_uid = uid;
240 f7613bee Venkateswararao Jujjuri (JV)
    cred.fc_gid = gid;
241 f7613bee Venkateswararao Jujjuri (JV)
242 f7613bee Venkateswararao Jujjuri (JV)
    return s->ops->chown(&s->ctx, path->data, &cred);
243 8cf89e00 Anthony Liguori
}
244 8cf89e00 Anthony Liguori
245 8fc39ae4 Sripathi Kodi
static int v9fs_do_utimensat(V9fsState *s, V9fsString *path,
246 8fc39ae4 Sripathi Kodi
                                           const struct timespec times[2])
247 8cf89e00 Anthony Liguori
{
248 8fc39ae4 Sripathi Kodi
    return s->ops->utimensat(&s->ctx, path->data, times);
249 8cf89e00 Anthony Liguori
}
250 8cf89e00 Anthony Liguori
251 5bae1900 Anthony Liguori
static int v9fs_do_remove(V9fsState *s, V9fsString *path)
252 5bae1900 Anthony Liguori
{
253 5bae1900 Anthony Liguori
    return s->ops->remove(&s->ctx, path->data);
254 5bae1900 Anthony Liguori
}
255 5bae1900 Anthony Liguori
256 8cf89e00 Anthony Liguori
static int v9fs_do_fsync(V9fsState *s, int fd)
257 8cf89e00 Anthony Liguori
{
258 8cf89e00 Anthony Liguori
    return s->ops->fsync(&s->ctx, fd);
259 8cf89e00 Anthony Liguori
}
260 8cf89e00 Anthony Liguori
261 5e94c103 M. Mohan Kumar
static int v9fs_do_statfs(V9fsState *s, V9fsString *path, struct statfs *stbuf)
262 5e94c103 M. Mohan Kumar
{
263 5e94c103 M. Mohan Kumar
    return s->ops->statfs(&s->ctx, path->data, stbuf);
264 5e94c103 M. Mohan Kumar
}
265 5e94c103 M. Mohan Kumar
266 fa32ef88 Aneesh Kumar K.V
static ssize_t v9fs_do_lgetxattr(V9fsState *s, V9fsString *path,
267 fa32ef88 Aneesh Kumar K.V
                             V9fsString *xattr_name,
268 fa32ef88 Aneesh Kumar K.V
                             void *value, size_t size)
269 fa32ef88 Aneesh Kumar K.V
{
270 fa32ef88 Aneesh Kumar K.V
    return s->ops->lgetxattr(&s->ctx, path->data,
271 fa32ef88 Aneesh Kumar K.V
                             xattr_name->data, value, size);
272 fa32ef88 Aneesh Kumar K.V
}
273 fa32ef88 Aneesh Kumar K.V
274 fa32ef88 Aneesh Kumar K.V
static ssize_t v9fs_do_llistxattr(V9fsState *s, V9fsString *path,
275 fa32ef88 Aneesh Kumar K.V
                              void *value, size_t size)
276 fa32ef88 Aneesh Kumar K.V
{
277 fa32ef88 Aneesh Kumar K.V
    return s->ops->llistxattr(&s->ctx, path->data,
278 fa32ef88 Aneesh Kumar K.V
                              value, size);
279 fa32ef88 Aneesh Kumar K.V
}
280 fa32ef88 Aneesh Kumar K.V
281 10b468bd Aneesh Kumar K.V
static int v9fs_do_lsetxattr(V9fsState *s, V9fsString *path,
282 10b468bd Aneesh Kumar K.V
                             V9fsString *xattr_name,
283 10b468bd Aneesh Kumar K.V
                             void *value, size_t size, int flags)
284 10b468bd Aneesh Kumar K.V
{
285 10b468bd Aneesh Kumar K.V
    return s->ops->lsetxattr(&s->ctx, path->data,
286 10b468bd Aneesh Kumar K.V
                             xattr_name->data, value, size, flags);
287 10b468bd Aneesh Kumar K.V
}
288 10b468bd Aneesh Kumar K.V
289 a03f7874 Anthony Liguori
static void v9fs_string_init(V9fsString *str)
290 a03f7874 Anthony Liguori
{
291 a03f7874 Anthony Liguori
    str->data = NULL;
292 a03f7874 Anthony Liguori
    str->size = 0;
293 a03f7874 Anthony Liguori
}
294 a03f7874 Anthony Liguori
295 a03f7874 Anthony Liguori
static void v9fs_string_free(V9fsString *str)
296 a03f7874 Anthony Liguori
{
297 a03f7874 Anthony Liguori
    qemu_free(str->data);
298 a03f7874 Anthony Liguori
    str->data = NULL;
299 a03f7874 Anthony Liguori
    str->size = 0;
300 a03f7874 Anthony Liguori
}
301 a03f7874 Anthony Liguori
302 a03f7874 Anthony Liguori
static void v9fs_string_null(V9fsString *str)
303 a03f7874 Anthony Liguori
{
304 a03f7874 Anthony Liguori
    v9fs_string_free(str);
305 a03f7874 Anthony Liguori
}
306 a03f7874 Anthony Liguori
307 a03f7874 Anthony Liguori
static int number_to_string(void *arg, char type)
308 a03f7874 Anthony Liguori
{
309 a03f7874 Anthony Liguori
    unsigned int ret = 0;
310 a03f7874 Anthony Liguori
311 a03f7874 Anthony Liguori
    switch (type) {
312 a03f7874 Anthony Liguori
    case 'u': {
313 a03f7874 Anthony Liguori
        unsigned int num = *(unsigned int *)arg;
314 a03f7874 Anthony Liguori
315 a03f7874 Anthony Liguori
        do {
316 a03f7874 Anthony Liguori
            ret++;
317 a03f7874 Anthony Liguori
            num = num/10;
318 a03f7874 Anthony Liguori
        } while (num);
319 a03f7874 Anthony Liguori
        break;
320 a03f7874 Anthony Liguori
    }
321 a03f7874 Anthony Liguori
    default:
322 a03f7874 Anthony Liguori
        printf("Number_to_string: Unknown number format\n");
323 a03f7874 Anthony Liguori
        return -1;
324 a03f7874 Anthony Liguori
    }
325 a03f7874 Anthony Liguori
326 a03f7874 Anthony Liguori
    return ret;
327 a03f7874 Anthony Liguori
}
328 a03f7874 Anthony Liguori
329 a03f7874 Anthony Liguori
static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
330 a03f7874 Anthony Liguori
{
331 a03f7874 Anthony Liguori
    va_list ap2;
332 a03f7874 Anthony Liguori
    char *iter = (char *)fmt;
333 a03f7874 Anthony Liguori
    int len = 0;
334 a03f7874 Anthony Liguori
    int nr_args = 0;
335 a03f7874 Anthony Liguori
    char *arg_char_ptr;
336 a03f7874 Anthony Liguori
    unsigned int arg_uint;
337 a03f7874 Anthony Liguori
338 a03f7874 Anthony Liguori
    /* Find the number of %'s that denotes an argument */
339 a03f7874 Anthony Liguori
    for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
340 a03f7874 Anthony Liguori
        nr_args++;
341 a03f7874 Anthony Liguori
        iter++;
342 a03f7874 Anthony Liguori
    }
343 a03f7874 Anthony Liguori
344 a03f7874 Anthony Liguori
    len = strlen(fmt) - 2*nr_args;
345 a03f7874 Anthony Liguori
346 a03f7874 Anthony Liguori
    if (!nr_args) {
347 a03f7874 Anthony Liguori
        goto alloc_print;
348 a03f7874 Anthony Liguori
    }
349 a03f7874 Anthony Liguori
350 a03f7874 Anthony Liguori
    va_copy(ap2, ap);
351 a03f7874 Anthony Liguori
352 a03f7874 Anthony Liguori
    iter = (char *)fmt;
353 a03f7874 Anthony Liguori
354 a03f7874 Anthony Liguori
    /* Now parse the format string */
355 a03f7874 Anthony Liguori
    for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
356 a03f7874 Anthony Liguori
        iter++;
357 a03f7874 Anthony Liguori
        switch (*iter) {
358 a03f7874 Anthony Liguori
        case 'u':
359 a03f7874 Anthony Liguori
            arg_uint = va_arg(ap2, unsigned int);
360 a03f7874 Anthony Liguori
            len += number_to_string((void *)&arg_uint, 'u');
361 a03f7874 Anthony Liguori
            break;
362 a03f7874 Anthony Liguori
        case 's':
363 a03f7874 Anthony Liguori
            arg_char_ptr = va_arg(ap2, char *);
364 a03f7874 Anthony Liguori
            len += strlen(arg_char_ptr);
365 a03f7874 Anthony Liguori
            break;
366 a03f7874 Anthony Liguori
        case 'c':
367 a03f7874 Anthony Liguori
            len += 1;
368 a03f7874 Anthony Liguori
            break;
369 a03f7874 Anthony Liguori
        default:
370 a03f7874 Anthony Liguori
            fprintf(stderr,
371 a03f7874 Anthony Liguori
                    "v9fs_string_alloc_printf:Incorrect format %c", *iter);
372 a03f7874 Anthony Liguori
            return -1;
373 a03f7874 Anthony Liguori
        }
374 a03f7874 Anthony Liguori
        iter++;
375 a03f7874 Anthony Liguori
    }
376 a03f7874 Anthony Liguori
377 a03f7874 Anthony Liguori
alloc_print:
378 a03f7874 Anthony Liguori
    *strp = qemu_malloc((len + 1) * sizeof(**strp));
379 a03f7874 Anthony Liguori
380 a03f7874 Anthony Liguori
    return vsprintf(*strp, fmt, ap);
381 a03f7874 Anthony Liguori
}
382 a03f7874 Anthony Liguori
383 a03f7874 Anthony Liguori
static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
384 a03f7874 Anthony Liguori
{
385 a03f7874 Anthony Liguori
    va_list ap;
386 a03f7874 Anthony Liguori
    int err;
387 a03f7874 Anthony Liguori
388 a03f7874 Anthony Liguori
    v9fs_string_free(str);
389 a03f7874 Anthony Liguori
390 a03f7874 Anthony Liguori
    va_start(ap, fmt);
391 a03f7874 Anthony Liguori
    err = v9fs_string_alloc_printf(&str->data, fmt, ap);
392 a03f7874 Anthony Liguori
    BUG_ON(err == -1);
393 a03f7874 Anthony Liguori
    va_end(ap);
394 a03f7874 Anthony Liguori
395 a03f7874 Anthony Liguori
    str->size = err;
396 a03f7874 Anthony Liguori
}
397 a03f7874 Anthony Liguori
398 a03f7874 Anthony Liguori
static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
399 a03f7874 Anthony Liguori
{
400 a03f7874 Anthony Liguori
    v9fs_string_free(lhs);
401 a03f7874 Anthony Liguori
    v9fs_string_sprintf(lhs, "%s", rhs->data);
402 a03f7874 Anthony Liguori
}
403 a03f7874 Anthony Liguori
404 a03f7874 Anthony Liguori
static size_t v9fs_string_size(V9fsString *str)
405 a03f7874 Anthony Liguori
{
406 a03f7874 Anthony Liguori
    return str->size;
407 a03f7874 Anthony Liguori
}
408 a03f7874 Anthony Liguori
409 286d5652 Anthony Liguori
static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
410 286d5652 Anthony Liguori
{
411 286d5652 Anthony Liguori
    V9fsFidState *f;
412 286d5652 Anthony Liguori
413 286d5652 Anthony Liguori
    for (f = s->fid_list; f; f = f->next) {
414 286d5652 Anthony Liguori
        if (f->fid == fid) {
415 286d5652 Anthony Liguori
            return f;
416 286d5652 Anthony Liguori
        }
417 286d5652 Anthony Liguori
    }
418 286d5652 Anthony Liguori
419 286d5652 Anthony Liguori
    return NULL;
420 286d5652 Anthony Liguori
}
421 286d5652 Anthony Liguori
422 286d5652 Anthony Liguori
static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
423 286d5652 Anthony Liguori
{
424 286d5652 Anthony Liguori
    V9fsFidState *f;
425 286d5652 Anthony Liguori
426 286d5652 Anthony Liguori
    f = lookup_fid(s, fid);
427 286d5652 Anthony Liguori
    if (f) {
428 286d5652 Anthony Liguori
        return NULL;
429 286d5652 Anthony Liguori
    }
430 286d5652 Anthony Liguori
431 286d5652 Anthony Liguori
    f = qemu_mallocz(sizeof(V9fsFidState));
432 286d5652 Anthony Liguori
433 286d5652 Anthony Liguori
    f->fid = fid;
434 d62dbb51 Aneesh Kumar K.V
    f->fid_type = P9_FID_NONE;
435 286d5652 Anthony Liguori
436 286d5652 Anthony Liguori
    f->next = s->fid_list;
437 286d5652 Anthony Liguori
    s->fid_list = f;
438 286d5652 Anthony Liguori
439 286d5652 Anthony Liguori
    return f;
440 286d5652 Anthony Liguori
}
441 286d5652 Anthony Liguori
442 10b468bd Aneesh Kumar K.V
static int v9fs_xattr_fid_clunk(V9fsState *s, V9fsFidState *fidp)
443 10b468bd Aneesh Kumar K.V
{
444 10b468bd Aneesh Kumar K.V
    int retval = 0;
445 10b468bd Aneesh Kumar K.V
446 10b468bd Aneesh Kumar K.V
    if (fidp->fs.xattr.copied_len == -1) {
447 10b468bd Aneesh Kumar K.V
        /* getxattr/listxattr fid */
448 10b468bd Aneesh Kumar K.V
        goto free_value;
449 10b468bd Aneesh Kumar K.V
    }
450 10b468bd Aneesh Kumar K.V
    /*
451 10b468bd Aneesh Kumar K.V
     * if this is fid for setxattr. clunk should
452 10b468bd Aneesh Kumar K.V
     * result in setxattr localcall
453 10b468bd Aneesh Kumar K.V
     */
454 10b468bd Aneesh Kumar K.V
    if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
455 10b468bd Aneesh Kumar K.V
        /* clunk after partial write */
456 10b468bd Aneesh Kumar K.V
        retval = -EINVAL;
457 10b468bd Aneesh Kumar K.V
        goto free_out;
458 10b468bd Aneesh Kumar K.V
    }
459 10b468bd Aneesh Kumar K.V
    retval = v9fs_do_lsetxattr(s, &fidp->path, &fidp->fs.xattr.name,
460 10b468bd Aneesh Kumar K.V
                               fidp->fs.xattr.value,
461 10b468bd Aneesh Kumar K.V
                               fidp->fs.xattr.len,
462 10b468bd Aneesh Kumar K.V
                               fidp->fs.xattr.flags);
463 10b468bd Aneesh Kumar K.V
free_out:
464 10b468bd Aneesh Kumar K.V
    v9fs_string_free(&fidp->fs.xattr.name);
465 10b468bd Aneesh Kumar K.V
free_value:
466 10b468bd Aneesh Kumar K.V
    if (fidp->fs.xattr.value) {
467 10b468bd Aneesh Kumar K.V
        qemu_free(fidp->fs.xattr.value);
468 10b468bd Aneesh Kumar K.V
    }
469 10b468bd Aneesh Kumar K.V
    return retval;
470 10b468bd Aneesh Kumar K.V
}
471 10b468bd Aneesh Kumar K.V
472 286d5652 Anthony Liguori
static int free_fid(V9fsState *s, int32_t fid)
473 286d5652 Anthony Liguori
{
474 10b468bd Aneesh Kumar K.V
    int retval = 0;
475 286d5652 Anthony Liguori
    V9fsFidState **fidpp, *fidp;
476 286d5652 Anthony Liguori
477 286d5652 Anthony Liguori
    for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
478 286d5652 Anthony Liguori
        if ((*fidpp)->fid == fid) {
479 286d5652 Anthony Liguori
            break;
480 286d5652 Anthony Liguori
        }
481 286d5652 Anthony Liguori
    }
482 286d5652 Anthony Liguori
483 286d5652 Anthony Liguori
    if (*fidpp == NULL) {
484 286d5652 Anthony Liguori
        return -ENOENT;
485 286d5652 Anthony Liguori
    }
486 286d5652 Anthony Liguori
487 286d5652 Anthony Liguori
    fidp = *fidpp;
488 286d5652 Anthony Liguori
    *fidpp = fidp->next;
489 286d5652 Anthony Liguori
490 d62dbb51 Aneesh Kumar K.V
    if (fidp->fid_type == P9_FID_FILE) {
491 d62dbb51 Aneesh Kumar K.V
        v9fs_do_close(s, fidp->fs.fd);
492 d62dbb51 Aneesh Kumar K.V
    } else if (fidp->fid_type == P9_FID_DIR) {
493 d62dbb51 Aneesh Kumar K.V
        v9fs_do_closedir(s, fidp->fs.dir);
494 d62dbb51 Aneesh Kumar K.V
    } else if (fidp->fid_type == P9_FID_XATTR) {
495 10b468bd Aneesh Kumar K.V
        retval = v9fs_xattr_fid_clunk(s, fidp);
496 286d5652 Anthony Liguori
    }
497 286d5652 Anthony Liguori
    v9fs_string_free(&fidp->path);
498 286d5652 Anthony Liguori
    qemu_free(fidp);
499 286d5652 Anthony Liguori
500 10b468bd Aneesh Kumar K.V
    return retval;
501 286d5652 Anthony Liguori
}
502 286d5652 Anthony Liguori
503 286d5652 Anthony Liguori
#define P9_QID_TYPE_DIR         0x80
504 286d5652 Anthony Liguori
#define P9_QID_TYPE_SYMLINK     0x02
505 286d5652 Anthony Liguori
506 286d5652 Anthony Liguori
#define P9_STAT_MODE_DIR        0x80000000
507 286d5652 Anthony Liguori
#define P9_STAT_MODE_APPEND     0x40000000
508 286d5652 Anthony Liguori
#define P9_STAT_MODE_EXCL       0x20000000
509 286d5652 Anthony Liguori
#define P9_STAT_MODE_MOUNT      0x10000000
510 286d5652 Anthony Liguori
#define P9_STAT_MODE_AUTH       0x08000000
511 286d5652 Anthony Liguori
#define P9_STAT_MODE_TMP        0x04000000
512 286d5652 Anthony Liguori
#define P9_STAT_MODE_SYMLINK    0x02000000
513 286d5652 Anthony Liguori
#define P9_STAT_MODE_LINK       0x01000000
514 286d5652 Anthony Liguori
#define P9_STAT_MODE_DEVICE     0x00800000
515 286d5652 Anthony Liguori
#define P9_STAT_MODE_NAMED_PIPE 0x00200000
516 286d5652 Anthony Liguori
#define P9_STAT_MODE_SOCKET     0x00100000
517 286d5652 Anthony Liguori
#define P9_STAT_MODE_SETUID     0x00080000
518 286d5652 Anthony Liguori
#define P9_STAT_MODE_SETGID     0x00040000
519 286d5652 Anthony Liguori
#define P9_STAT_MODE_SETVTX     0x00010000
520 286d5652 Anthony Liguori
521 286d5652 Anthony Liguori
#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR |          \
522 286d5652 Anthony Liguori
                                P9_STAT_MODE_SYMLINK |      \
523 286d5652 Anthony Liguori
                                P9_STAT_MODE_LINK |         \
524 286d5652 Anthony Liguori
                                P9_STAT_MODE_DEVICE |       \
525 286d5652 Anthony Liguori
                                P9_STAT_MODE_NAMED_PIPE |   \
526 286d5652 Anthony Liguori
                                P9_STAT_MODE_SOCKET)
527 286d5652 Anthony Liguori
528 286d5652 Anthony Liguori
/* This is the algorithm from ufs in spfs */
529 286d5652 Anthony Liguori
static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
530 286d5652 Anthony Liguori
{
531 286d5652 Anthony Liguori
    size_t size;
532 286d5652 Anthony Liguori
533 286d5652 Anthony Liguori
    size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
534 286d5652 Anthony Liguori
    memcpy(&qidp->path, &stbuf->st_ino, size);
535 286d5652 Anthony Liguori
    qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
536 286d5652 Anthony Liguori
    qidp->type = 0;
537 286d5652 Anthony Liguori
    if (S_ISDIR(stbuf->st_mode)) {
538 286d5652 Anthony Liguori
        qidp->type |= P9_QID_TYPE_DIR;
539 286d5652 Anthony Liguori
    }
540 286d5652 Anthony Liguori
    if (S_ISLNK(stbuf->st_mode)) {
541 286d5652 Anthony Liguori
        qidp->type |= P9_QID_TYPE_SYMLINK;
542 286d5652 Anthony Liguori
    }
543 286d5652 Anthony Liguori
}
544 286d5652 Anthony Liguori
545 286d5652 Anthony Liguori
static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
546 286d5652 Anthony Liguori
{
547 286d5652 Anthony Liguori
    struct stat stbuf;
548 286d5652 Anthony Liguori
    int err;
549 286d5652 Anthony Liguori
550 286d5652 Anthony Liguori
    err = v9fs_do_lstat(s, &fidp->path, &stbuf);
551 286d5652 Anthony Liguori
    if (err) {
552 286d5652 Anthony Liguori
        return err;
553 286d5652 Anthony Liguori
    }
554 286d5652 Anthony Liguori
555 286d5652 Anthony Liguori
    stat_to_qid(&stbuf, qidp);
556 286d5652 Anthony Liguori
    return 0;
557 286d5652 Anthony Liguori
}
558 286d5652 Anthony Liguori
559 9f107513 Anthony Liguori
static V9fsPDU *alloc_pdu(V9fsState *s)
560 9f107513 Anthony Liguori
{
561 9f107513 Anthony Liguori
    V9fsPDU *pdu = NULL;
562 9f107513 Anthony Liguori
563 9f107513 Anthony Liguori
    if (!QLIST_EMPTY(&s->free_list)) {
564 9f107513 Anthony Liguori
        pdu = QLIST_FIRST(&s->free_list);
565 9f107513 Anthony Liguori
        QLIST_REMOVE(pdu, next);
566 9f107513 Anthony Liguori
    }
567 9f107513 Anthony Liguori
    return pdu;
568 9f107513 Anthony Liguori
}
569 9f107513 Anthony Liguori
570 9f107513 Anthony Liguori
static void free_pdu(V9fsState *s, V9fsPDU *pdu)
571 9f107513 Anthony Liguori
{
572 9f107513 Anthony Liguori
    if (pdu) {
573 9f107513 Anthony Liguori
        QLIST_INSERT_HEAD(&s->free_list, pdu, next);
574 9f107513 Anthony Liguori
    }
575 9f107513 Anthony Liguori
}
576 9f107513 Anthony Liguori
577 9f107513 Anthony Liguori
size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
578 9f107513 Anthony Liguori
                        size_t offset, size_t size, int pack)
579 9f107513 Anthony Liguori
{
580 9f107513 Anthony Liguori
    int i = 0;
581 9f107513 Anthony Liguori
    size_t copied = 0;
582 9f107513 Anthony Liguori
583 9f107513 Anthony Liguori
    for (i = 0; size && i < sg_count; i++) {
584 9f107513 Anthony Liguori
        size_t len;
585 9f107513 Anthony Liguori
        if (offset >= sg[i].iov_len) {
586 9f107513 Anthony Liguori
            /* skip this sg */
587 9f107513 Anthony Liguori
            offset -= sg[i].iov_len;
588 9f107513 Anthony Liguori
            continue;
589 9f107513 Anthony Liguori
        } else {
590 9f107513 Anthony Liguori
            len = MIN(sg[i].iov_len - offset, size);
591 9f107513 Anthony Liguori
            if (pack) {
592 9f107513 Anthony Liguori
                memcpy(sg[i].iov_base + offset, addr, len);
593 9f107513 Anthony Liguori
            } else {
594 9f107513 Anthony Liguori
                memcpy(addr, sg[i].iov_base + offset, len);
595 9f107513 Anthony Liguori
            }
596 9f107513 Anthony Liguori
            size -= len;
597 9f107513 Anthony Liguori
            copied += len;
598 9f107513 Anthony Liguori
            addr += len;
599 9f107513 Anthony Liguori
            if (size) {
600 9f107513 Anthony Liguori
                offset = 0;
601 9f107513 Anthony Liguori
                continue;
602 9f107513 Anthony Liguori
            }
603 9f107513 Anthony Liguori
        }
604 9f107513 Anthony Liguori
    }
605 9f107513 Anthony Liguori
606 9f107513 Anthony Liguori
    return copied;
607 9f107513 Anthony Liguori
}
608 9f107513 Anthony Liguori
609 405a549a Anthony Liguori
static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
610 405a549a Anthony Liguori
{
611 405a549a Anthony Liguori
    return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
612 405a549a Anthony Liguori
                         offset, size, 0);
613 405a549a Anthony Liguori
}
614 405a549a Anthony Liguori
615 405a549a Anthony Liguori
static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
616 405a549a Anthony Liguori
                        size_t size)
617 405a549a Anthony Liguori
{
618 405a549a Anthony Liguori
    return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
619 405a549a Anthony Liguori
                             offset, size, 1);
620 405a549a Anthony Liguori
}
621 405a549a Anthony Liguori
622 405a549a Anthony Liguori
static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
623 405a549a Anthony Liguori
{
624 405a549a Anthony Liguori
    size_t pos = 0;
625 405a549a Anthony Liguori
    int i, j;
626 405a549a Anthony Liguori
    struct iovec *src_sg;
627 405a549a Anthony Liguori
    unsigned int num;
628 405a549a Anthony Liguori
629 405a549a Anthony Liguori
    if (rx) {
630 405a549a Anthony Liguori
        src_sg = pdu->elem.in_sg;
631 405a549a Anthony Liguori
        num = pdu->elem.in_num;
632 405a549a Anthony Liguori
    } else {
633 405a549a Anthony Liguori
        src_sg = pdu->elem.out_sg;
634 405a549a Anthony Liguori
        num = pdu->elem.out_num;
635 405a549a Anthony Liguori
    }
636 405a549a Anthony Liguori
637 405a549a Anthony Liguori
    j = 0;
638 405a549a Anthony Liguori
    for (i = 0; i < num; i++) {
639 405a549a Anthony Liguori
        if (offset <= pos) {
640 405a549a Anthony Liguori
            sg[j].iov_base = src_sg[i].iov_base;
641 405a549a Anthony Liguori
            sg[j].iov_len = src_sg[i].iov_len;
642 405a549a Anthony Liguori
            j++;
643 405a549a Anthony Liguori
        } else if (offset < (src_sg[i].iov_len + pos)) {
644 405a549a Anthony Liguori
            sg[j].iov_base = src_sg[i].iov_base;
645 405a549a Anthony Liguori
            sg[j].iov_len = src_sg[i].iov_len;
646 405a549a Anthony Liguori
            sg[j].iov_base += (offset - pos);
647 405a549a Anthony Liguori
            sg[j].iov_len -= (offset - pos);
648 405a549a Anthony Liguori
            j++;
649 405a549a Anthony Liguori
        }
650 405a549a Anthony Liguori
        pos += src_sg[i].iov_len;
651 405a549a Anthony Liguori
    }
652 405a549a Anthony Liguori
653 405a549a Anthony Liguori
    return j;
654 405a549a Anthony Liguori
}
655 405a549a Anthony Liguori
656 405a549a Anthony Liguori
static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
657 405a549a Anthony Liguori
{
658 405a549a Anthony Liguori
    size_t old_offset = offset;
659 405a549a Anthony Liguori
    va_list ap;
660 405a549a Anthony Liguori
    int i;
661 405a549a Anthony Liguori
662 405a549a Anthony Liguori
    va_start(ap, fmt);
663 405a549a Anthony Liguori
    for (i = 0; fmt[i]; i++) {
664 405a549a Anthony Liguori
        switch (fmt[i]) {
665 405a549a Anthony Liguori
        case 'b': {
666 405a549a Anthony Liguori
            uint8_t *valp = va_arg(ap, uint8_t *);
667 405a549a Anthony Liguori
            offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
668 405a549a Anthony Liguori
            break;
669 405a549a Anthony Liguori
        }
670 405a549a Anthony Liguori
        case 'w': {
671 405a549a Anthony Liguori
            uint16_t val, *valp;
672 405a549a Anthony Liguori
            valp = va_arg(ap, uint16_t *);
673 405a549a Anthony Liguori
            val = le16_to_cpupu(valp);
674 405a549a Anthony Liguori
            offset += pdu_unpack(&val, pdu, offset, sizeof(val));
675 405a549a Anthony Liguori
            *valp = val;
676 405a549a Anthony Liguori
            break;
677 405a549a Anthony Liguori
        }
678 405a549a Anthony Liguori
        case 'd': {
679 405a549a Anthony Liguori
            uint32_t val, *valp;
680 405a549a Anthony Liguori
            valp = va_arg(ap, uint32_t *);
681 405a549a Anthony Liguori
            val = le32_to_cpupu(valp);
682 405a549a Anthony Liguori
            offset += pdu_unpack(&val, pdu, offset, sizeof(val));
683 405a549a Anthony Liguori
            *valp = val;
684 405a549a Anthony Liguori
            break;
685 405a549a Anthony Liguori
        }
686 405a549a Anthony Liguori
        case 'q': {
687 405a549a Anthony Liguori
            uint64_t val, *valp;
688 405a549a Anthony Liguori
            valp = va_arg(ap, uint64_t *);
689 405a549a Anthony Liguori
            val = le64_to_cpup(valp);
690 405a549a Anthony Liguori
            offset += pdu_unpack(&val, pdu, offset, sizeof(val));
691 405a549a Anthony Liguori
            *valp = val;
692 405a549a Anthony Liguori
            break;
693 405a549a Anthony Liguori
        }
694 405a549a Anthony Liguori
        case 'v': {
695 405a549a Anthony Liguori
            struct iovec *iov = va_arg(ap, struct iovec *);
696 405a549a Anthony Liguori
            int *iovcnt = va_arg(ap, int *);
697 405a549a Anthony Liguori
            *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
698 405a549a Anthony Liguori
            break;
699 405a549a Anthony Liguori
        }
700 405a549a Anthony Liguori
        case 's': {
701 405a549a Anthony Liguori
            V9fsString *str = va_arg(ap, V9fsString *);
702 405a549a Anthony Liguori
            offset += pdu_unmarshal(pdu, offset, "w", &str->size);
703 405a549a Anthony Liguori
            /* FIXME: sanity check str->size */
704 405a549a Anthony Liguori
            str->data = qemu_malloc(str->size + 1);
705 405a549a Anthony Liguori
            offset += pdu_unpack(str->data, pdu, offset, str->size);
706 405a549a Anthony Liguori
            str->data[str->size] = 0;
707 405a549a Anthony Liguori
            break;
708 405a549a Anthony Liguori
        }
709 405a549a Anthony Liguori
        case 'Q': {
710 405a549a Anthony Liguori
            V9fsQID *qidp = va_arg(ap, V9fsQID *);
711 405a549a Anthony Liguori
            offset += pdu_unmarshal(pdu, offset, "bdq",
712 405a549a Anthony Liguori
                        &qidp->type, &qidp->version, &qidp->path);
713 405a549a Anthony Liguori
            break;
714 405a549a Anthony Liguori
        }
715 405a549a Anthony Liguori
        case 'S': {
716 405a549a Anthony Liguori
            V9fsStat *statp = va_arg(ap, V9fsStat *);
717 405a549a Anthony Liguori
            offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
718 405a549a Anthony Liguori
                        &statp->size, &statp->type, &statp->dev,
719 405a549a Anthony Liguori
                        &statp->qid, &statp->mode, &statp->atime,
720 405a549a Anthony Liguori
                        &statp->mtime, &statp->length,
721 405a549a Anthony Liguori
                        &statp->name, &statp->uid, &statp->gid,
722 405a549a Anthony Liguori
                        &statp->muid, &statp->extension,
723 405a549a Anthony Liguori
                        &statp->n_uid, &statp->n_gid,
724 405a549a Anthony Liguori
                        &statp->n_muid);
725 405a549a Anthony Liguori
            break;
726 405a549a Anthony Liguori
        }
727 c79ce737 Sripathi Kodi
        case 'I': {
728 c79ce737 Sripathi Kodi
            V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
729 c79ce737 Sripathi Kodi
            offset += pdu_unmarshal(pdu, offset, "ddddqqqqq",
730 c79ce737 Sripathi Kodi
                        &iattr->valid, &iattr->mode,
731 c79ce737 Sripathi Kodi
                        &iattr->uid, &iattr->gid, &iattr->size,
732 c79ce737 Sripathi Kodi
                        &iattr->atime_sec, &iattr->atime_nsec,
733 c79ce737 Sripathi Kodi
                        &iattr->mtime_sec, &iattr->mtime_nsec);
734 c79ce737 Sripathi Kodi
            break;
735 c79ce737 Sripathi Kodi
        }
736 405a549a Anthony Liguori
        default:
737 405a549a Anthony Liguori
            break;
738 405a549a Anthony Liguori
        }
739 405a549a Anthony Liguori
    }
740 405a549a Anthony Liguori
741 405a549a Anthony Liguori
    va_end(ap);
742 405a549a Anthony Liguori
743 405a549a Anthony Liguori
    return offset - old_offset;
744 405a549a Anthony Liguori
}
745 405a549a Anthony Liguori
746 405a549a Anthony Liguori
static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
747 405a549a Anthony Liguori
{
748 405a549a Anthony Liguori
    size_t old_offset = offset;
749 405a549a Anthony Liguori
    va_list ap;
750 405a549a Anthony Liguori
    int i;
751 405a549a Anthony Liguori
752 405a549a Anthony Liguori
    va_start(ap, fmt);
753 405a549a Anthony Liguori
    for (i = 0; fmt[i]; i++) {
754 405a549a Anthony Liguori
        switch (fmt[i]) {
755 405a549a Anthony Liguori
        case 'b': {
756 405a549a Anthony Liguori
            uint8_t val = va_arg(ap, int);
757 405a549a Anthony Liguori
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
758 405a549a Anthony Liguori
            break;
759 405a549a Anthony Liguori
        }
760 405a549a Anthony Liguori
        case 'w': {
761 405a549a Anthony Liguori
            uint16_t val;
762 405a549a Anthony Liguori
            cpu_to_le16w(&val, va_arg(ap, int));
763 405a549a Anthony Liguori
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
764 405a549a Anthony Liguori
            break;
765 405a549a Anthony Liguori
        }
766 405a549a Anthony Liguori
        case 'd': {
767 405a549a Anthony Liguori
            uint32_t val;
768 405a549a Anthony Liguori
            cpu_to_le32w(&val, va_arg(ap, uint32_t));
769 405a549a Anthony Liguori
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
770 405a549a Anthony Liguori
            break;
771 405a549a Anthony Liguori
        }
772 405a549a Anthony Liguori
        case 'q': {
773 405a549a Anthony Liguori
            uint64_t val;
774 405a549a Anthony Liguori
            cpu_to_le64w(&val, va_arg(ap, uint64_t));
775 405a549a Anthony Liguori
            offset += pdu_pack(pdu, offset, &val, sizeof(val));
776 405a549a Anthony Liguori
            break;
777 405a549a Anthony Liguori
        }
778 405a549a Anthony Liguori
        case 'v': {
779 405a549a Anthony Liguori
            struct iovec *iov = va_arg(ap, struct iovec *);
780 405a549a Anthony Liguori
            int *iovcnt = va_arg(ap, int *);
781 405a549a Anthony Liguori
            *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
782 405a549a Anthony Liguori
            break;
783 405a549a Anthony Liguori
        }
784 405a549a Anthony Liguori
        case 's': {
785 405a549a Anthony Liguori
            V9fsString *str = va_arg(ap, V9fsString *);
786 405a549a Anthony Liguori
            offset += pdu_marshal(pdu, offset, "w", str->size);
787 405a549a Anthony Liguori
            offset += pdu_pack(pdu, offset, str->data, str->size);
788 405a549a Anthony Liguori
            break;
789 405a549a Anthony Liguori
        }
790 405a549a Anthony Liguori
        case 'Q': {
791 405a549a Anthony Liguori
            V9fsQID *qidp = va_arg(ap, V9fsQID *);
792 405a549a Anthony Liguori
            offset += pdu_marshal(pdu, offset, "bdq",
793 405a549a Anthony Liguori
                        qidp->type, qidp->version, qidp->path);
794 405a549a Anthony Liguori
            break;
795 405a549a Anthony Liguori
        }
796 405a549a Anthony Liguori
        case 'S': {
797 405a549a Anthony Liguori
            V9fsStat *statp = va_arg(ap, V9fsStat *);
798 405a549a Anthony Liguori
            offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
799 405a549a Anthony Liguori
                        statp->size, statp->type, statp->dev,
800 405a549a Anthony Liguori
                        &statp->qid, statp->mode, statp->atime,
801 405a549a Anthony Liguori
                        statp->mtime, statp->length, &statp->name,
802 405a549a Anthony Liguori
                        &statp->uid, &statp->gid, &statp->muid,
803 405a549a Anthony Liguori
                        &statp->extension, statp->n_uid,
804 405a549a Anthony Liguori
                        statp->n_gid, statp->n_muid);
805 405a549a Anthony Liguori
            break;
806 405a549a Anthony Liguori
        }
807 00ede4c2 Sripathi Kodi
        case 'A': {
808 00ede4c2 Sripathi Kodi
            V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
809 00ede4c2 Sripathi Kodi
            offset += pdu_marshal(pdu, offset, "qQdddqqqqqqqqqqqqqqq",
810 00ede4c2 Sripathi Kodi
                        statp->st_result_mask,
811 00ede4c2 Sripathi Kodi
                        &statp->qid, statp->st_mode,
812 00ede4c2 Sripathi Kodi
                        statp->st_uid, statp->st_gid,
813 00ede4c2 Sripathi Kodi
                        statp->st_nlink, statp->st_rdev,
814 00ede4c2 Sripathi Kodi
                        statp->st_size, statp->st_blksize, statp->st_blocks,
815 00ede4c2 Sripathi Kodi
                        statp->st_atime_sec, statp->st_atime_nsec,
816 00ede4c2 Sripathi Kodi
                        statp->st_mtime_sec, statp->st_mtime_nsec,
817 00ede4c2 Sripathi Kodi
                        statp->st_ctime_sec, statp->st_ctime_nsec,
818 00ede4c2 Sripathi Kodi
                        statp->st_btime_sec, statp->st_btime_nsec,
819 00ede4c2 Sripathi Kodi
                        statp->st_gen, statp->st_data_version);
820 00ede4c2 Sripathi Kodi
            break;
821 00ede4c2 Sripathi Kodi
        }
822 405a549a Anthony Liguori
        default:
823 405a549a Anthony Liguori
            break;
824 405a549a Anthony Liguori
        }
825 405a549a Anthony Liguori
    }
826 405a549a Anthony Liguori
    va_end(ap);
827 405a549a Anthony Liguori
828 405a549a Anthony Liguori
    return offset - old_offset;
829 405a549a Anthony Liguori
}
830 405a549a Anthony Liguori
831 405a549a Anthony Liguori
static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
832 405a549a Anthony Liguori
{
833 405a549a Anthony Liguori
    int8_t id = pdu->id + 1; /* Response */
834 405a549a Anthony Liguori
835 405a549a Anthony Liguori
    if (len < 0) {
836 405a549a Anthony Liguori
        V9fsString str;
837 405a549a Anthony Liguori
        int err = -len;
838 405a549a Anthony Liguori
839 405a549a Anthony Liguori
        str.data = strerror(err);
840 405a549a Anthony Liguori
        str.size = strlen(str.data);
841 405a549a Anthony Liguori
842 405a549a Anthony Liguori
        len = 7;
843 405a549a Anthony Liguori
        len += pdu_marshal(pdu, len, "s", &str);
844 405a549a Anthony Liguori
        if (dotu) {
845 405a549a Anthony Liguori
            len += pdu_marshal(pdu, len, "d", err);
846 405a549a Anthony Liguori
        }
847 405a549a Anthony Liguori
848 405a549a Anthony Liguori
        id = P9_RERROR;
849 405a549a Anthony Liguori
    }
850 405a549a Anthony Liguori
851 405a549a Anthony Liguori
    /* fill out the header */
852 405a549a Anthony Liguori
    pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
853 405a549a Anthony Liguori
854 405a549a Anthony Liguori
    /* keep these in sync */
855 405a549a Anthony Liguori
    pdu->size = len;
856 405a549a Anthony Liguori
    pdu->id = id;
857 405a549a Anthony Liguori
858 405a549a Anthony Liguori
    /* push onto queue and notify */
859 405a549a Anthony Liguori
    virtqueue_push(s->vq, &pdu->elem, len);
860 405a549a Anthony Liguori
861 405a549a Anthony Liguori
    /* FIXME: we should batch these completions */
862 405a549a Anthony Liguori
    virtio_notify(&s->vdev, s->vq);
863 405a549a Anthony Liguori
864 405a549a Anthony Liguori
    free_pdu(s, pdu);
865 405a549a Anthony Liguori
}
866 405a549a Anthony Liguori
867 bb9e3216 Anthony Liguori
static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
868 bb9e3216 Anthony Liguori
{
869 bb9e3216 Anthony Liguori
    mode_t ret;
870 bb9e3216 Anthony Liguori
871 bb9e3216 Anthony Liguori
    ret = mode & 0777;
872 bb9e3216 Anthony Liguori
    if (mode & P9_STAT_MODE_DIR) {
873 bb9e3216 Anthony Liguori
        ret |= S_IFDIR;
874 bb9e3216 Anthony Liguori
    }
875 bb9e3216 Anthony Liguori
876 bb9e3216 Anthony Liguori
    if (dotu) {
877 bb9e3216 Anthony Liguori
        if (mode & P9_STAT_MODE_SYMLINK) {
878 bb9e3216 Anthony Liguori
            ret |= S_IFLNK;
879 bb9e3216 Anthony Liguori
        }
880 bb9e3216 Anthony Liguori
        if (mode & P9_STAT_MODE_SOCKET) {
881 bb9e3216 Anthony Liguori
            ret |= S_IFSOCK;
882 bb9e3216 Anthony Liguori
        }
883 bb9e3216 Anthony Liguori
        if (mode & P9_STAT_MODE_NAMED_PIPE) {
884 bb9e3216 Anthony Liguori
            ret |= S_IFIFO;
885 bb9e3216 Anthony Liguori
        }
886 bb9e3216 Anthony Liguori
        if (mode & P9_STAT_MODE_DEVICE) {
887 bb9e3216 Anthony Liguori
            if (extension && extension->data[0] == 'c') {
888 bb9e3216 Anthony Liguori
                ret |= S_IFCHR;
889 bb9e3216 Anthony Liguori
            } else {
890 bb9e3216 Anthony Liguori
                ret |= S_IFBLK;
891 bb9e3216 Anthony Liguori
            }
892 bb9e3216 Anthony Liguori
        }
893 bb9e3216 Anthony Liguori
    }
894 bb9e3216 Anthony Liguori
895 bb9e3216 Anthony Liguori
    if (!(ret&~0777)) {
896 bb9e3216 Anthony Liguori
        ret |= S_IFREG;
897 bb9e3216 Anthony Liguori
    }
898 bb9e3216 Anthony Liguori
899 bb9e3216 Anthony Liguori
    if (mode & P9_STAT_MODE_SETUID) {
900 bb9e3216 Anthony Liguori
        ret |= S_ISUID;
901 bb9e3216 Anthony Liguori
    }
902 bb9e3216 Anthony Liguori
    if (mode & P9_STAT_MODE_SETGID) {
903 bb9e3216 Anthony Liguori
        ret |= S_ISGID;
904 bb9e3216 Anthony Liguori
    }
905 bb9e3216 Anthony Liguori
    if (mode & P9_STAT_MODE_SETVTX) {
906 bb9e3216 Anthony Liguori
        ret |= S_ISVTX;
907 bb9e3216 Anthony Liguori
    }
908 bb9e3216 Anthony Liguori
909 bb9e3216 Anthony Liguori
    return ret;
910 bb9e3216 Anthony Liguori
}
911 bb9e3216 Anthony Liguori
912 bb9e3216 Anthony Liguori
static int donttouch_stat(V9fsStat *stat)
913 bb9e3216 Anthony Liguori
{
914 bb9e3216 Anthony Liguori
    if (stat->type == -1 &&
915 bb9e3216 Anthony Liguori
        stat->dev == -1 &&
916 bb9e3216 Anthony Liguori
        stat->qid.type == -1 &&
917 bb9e3216 Anthony Liguori
        stat->qid.version == -1 &&
918 bb9e3216 Anthony Liguori
        stat->qid.path == -1 &&
919 bb9e3216 Anthony Liguori
        stat->mode == -1 &&
920 bb9e3216 Anthony Liguori
        stat->atime == -1 &&
921 bb9e3216 Anthony Liguori
        stat->mtime == -1 &&
922 bb9e3216 Anthony Liguori
        stat->length == -1 &&
923 bb9e3216 Anthony Liguori
        !stat->name.size &&
924 bb9e3216 Anthony Liguori
        !stat->uid.size &&
925 bb9e3216 Anthony Liguori
        !stat->gid.size &&
926 bb9e3216 Anthony Liguori
        !stat->muid.size &&
927 bb9e3216 Anthony Liguori
        stat->n_uid == -1 &&
928 bb9e3216 Anthony Liguori
        stat->n_gid == -1 &&
929 bb9e3216 Anthony Liguori
        stat->n_muid == -1) {
930 bb9e3216 Anthony Liguori
        return 1;
931 bb9e3216 Anthony Liguori
    }
932 bb9e3216 Anthony Liguori
933 bb9e3216 Anthony Liguori
    return 0;
934 bb9e3216 Anthony Liguori
}
935 bb9e3216 Anthony Liguori
936 bb9e3216 Anthony Liguori
static void v9fs_stat_free(V9fsStat *stat)
937 bb9e3216 Anthony Liguori
{
938 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->name);
939 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->uid);
940 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->gid);
941 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->muid);
942 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->extension);
943 bb9e3216 Anthony Liguori
}
944 bb9e3216 Anthony Liguori
945 bb9e3216 Anthony Liguori
static uint32_t stat_to_v9mode(const struct stat *stbuf)
946 bb9e3216 Anthony Liguori
{
947 bb9e3216 Anthony Liguori
    uint32_t mode;
948 bb9e3216 Anthony Liguori
949 bb9e3216 Anthony Liguori
    mode = stbuf->st_mode & 0777;
950 bb9e3216 Anthony Liguori
    if (S_ISDIR(stbuf->st_mode)) {
951 bb9e3216 Anthony Liguori
        mode |= P9_STAT_MODE_DIR;
952 bb9e3216 Anthony Liguori
    }
953 bb9e3216 Anthony Liguori
954 bb9e3216 Anthony Liguori
    if (dotu) {
955 bb9e3216 Anthony Liguori
        if (S_ISLNK(stbuf->st_mode)) {
956 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_SYMLINK;
957 bb9e3216 Anthony Liguori
        }
958 bb9e3216 Anthony Liguori
959 bb9e3216 Anthony Liguori
        if (S_ISSOCK(stbuf->st_mode)) {
960 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_SOCKET;
961 bb9e3216 Anthony Liguori
        }
962 bb9e3216 Anthony Liguori
963 bb9e3216 Anthony Liguori
        if (S_ISFIFO(stbuf->st_mode)) {
964 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_NAMED_PIPE;
965 bb9e3216 Anthony Liguori
        }
966 bb9e3216 Anthony Liguori
967 bb9e3216 Anthony Liguori
        if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
968 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_DEVICE;
969 bb9e3216 Anthony Liguori
        }
970 bb9e3216 Anthony Liguori
971 bb9e3216 Anthony Liguori
        if (stbuf->st_mode & S_ISUID) {
972 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_SETUID;
973 bb9e3216 Anthony Liguori
        }
974 bb9e3216 Anthony Liguori
975 bb9e3216 Anthony Liguori
        if (stbuf->st_mode & S_ISGID) {
976 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_SETGID;
977 bb9e3216 Anthony Liguori
        }
978 bb9e3216 Anthony Liguori
979 bb9e3216 Anthony Liguori
        if (stbuf->st_mode & S_ISVTX) {
980 bb9e3216 Anthony Liguori
            mode |= P9_STAT_MODE_SETVTX;
981 bb9e3216 Anthony Liguori
        }
982 bb9e3216 Anthony Liguori
    }
983 bb9e3216 Anthony Liguori
984 bb9e3216 Anthony Liguori
    return mode;
985 bb9e3216 Anthony Liguori
}
986 bb9e3216 Anthony Liguori
987 bb9e3216 Anthony Liguori
static int stat_to_v9stat(V9fsState *s, V9fsString *name,
988 bb9e3216 Anthony Liguori
                            const struct stat *stbuf,
989 bb9e3216 Anthony Liguori
                            V9fsStat *v9stat)
990 bb9e3216 Anthony Liguori
{
991 bb9e3216 Anthony Liguori
    int err;
992 bb9e3216 Anthony Liguori
    const char *str;
993 bb9e3216 Anthony Liguori
994 bb9e3216 Anthony Liguori
    memset(v9stat, 0, sizeof(*v9stat));
995 bb9e3216 Anthony Liguori
996 bb9e3216 Anthony Liguori
    stat_to_qid(stbuf, &v9stat->qid);
997 bb9e3216 Anthony Liguori
    v9stat->mode = stat_to_v9mode(stbuf);
998 bb9e3216 Anthony Liguori
    v9stat->atime = stbuf->st_atime;
999 bb9e3216 Anthony Liguori
    v9stat->mtime = stbuf->st_mtime;
1000 bb9e3216 Anthony Liguori
    v9stat->length = stbuf->st_size;
1001 bb9e3216 Anthony Liguori
1002 bb9e3216 Anthony Liguori
    v9fs_string_null(&v9stat->uid);
1003 bb9e3216 Anthony Liguori
    v9fs_string_null(&v9stat->gid);
1004 bb9e3216 Anthony Liguori
    v9fs_string_null(&v9stat->muid);
1005 bb9e3216 Anthony Liguori
1006 bb9e3216 Anthony Liguori
    if (dotu) {
1007 bb9e3216 Anthony Liguori
        v9stat->n_uid = stbuf->st_uid;
1008 bb9e3216 Anthony Liguori
        v9stat->n_gid = stbuf->st_gid;
1009 bb9e3216 Anthony Liguori
        v9stat->n_muid = 0;
1010 bb9e3216 Anthony Liguori
1011 bb9e3216 Anthony Liguori
        v9fs_string_null(&v9stat->extension);
1012 bb9e3216 Anthony Liguori
1013 bb9e3216 Anthony Liguori
        if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
1014 bb9e3216 Anthony Liguori
            err = v9fs_do_readlink(s, name, &v9stat->extension);
1015 bb9e3216 Anthony Liguori
            if (err == -1) {
1016 bb9e3216 Anthony Liguori
                err = -errno;
1017 bb9e3216 Anthony Liguori
                return err;
1018 bb9e3216 Anthony Liguori
            }
1019 bb9e3216 Anthony Liguori
            v9stat->extension.data[err] = 0;
1020 bb9e3216 Anthony Liguori
            v9stat->extension.size = err;
1021 bb9e3216 Anthony Liguori
        } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
1022 bb9e3216 Anthony Liguori
            v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
1023 bb9e3216 Anthony Liguori
                    S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
1024 bb9e3216 Anthony Liguori
                    major(stbuf->st_rdev), minor(stbuf->st_rdev));
1025 bb9e3216 Anthony Liguori
        } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
1026 bb9e3216 Anthony Liguori
            v9fs_string_sprintf(&v9stat->extension, "%s %u",
1027 bb9e3216 Anthony Liguori
                    "HARDLINKCOUNT", stbuf->st_nlink);
1028 bb9e3216 Anthony Liguori
        }
1029 bb9e3216 Anthony Liguori
    }
1030 bb9e3216 Anthony Liguori
1031 bb9e3216 Anthony Liguori
    str = strrchr(name->data, '/');
1032 bb9e3216 Anthony Liguori
    if (str) {
1033 bb9e3216 Anthony Liguori
        str += 1;
1034 bb9e3216 Anthony Liguori
    } else {
1035 bb9e3216 Anthony Liguori
        str = name->data;
1036 bb9e3216 Anthony Liguori
    }
1037 bb9e3216 Anthony Liguori
1038 bb9e3216 Anthony Liguori
    v9fs_string_sprintf(&v9stat->name, "%s", str);
1039 bb9e3216 Anthony Liguori
1040 bb9e3216 Anthony Liguori
    v9stat->size = 61 +
1041 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->name) +
1042 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->uid) +
1043 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->gid) +
1044 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->muid) +
1045 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->extension);
1046 bb9e3216 Anthony Liguori
    return 0;
1047 bb9e3216 Anthony Liguori
}
1048 bb9e3216 Anthony Liguori
1049 00ede4c2 Sripathi Kodi
#define P9_STATS_MODE          0x00000001ULL
1050 00ede4c2 Sripathi Kodi
#define P9_STATS_NLINK         0x00000002ULL
1051 00ede4c2 Sripathi Kodi
#define P9_STATS_UID           0x00000004ULL
1052 00ede4c2 Sripathi Kodi
#define P9_STATS_GID           0x00000008ULL
1053 00ede4c2 Sripathi Kodi
#define P9_STATS_RDEV          0x00000010ULL
1054 00ede4c2 Sripathi Kodi
#define P9_STATS_ATIME         0x00000020ULL
1055 00ede4c2 Sripathi Kodi
#define P9_STATS_MTIME         0x00000040ULL
1056 00ede4c2 Sripathi Kodi
#define P9_STATS_CTIME         0x00000080ULL
1057 00ede4c2 Sripathi Kodi
#define P9_STATS_INO           0x00000100ULL
1058 00ede4c2 Sripathi Kodi
#define P9_STATS_SIZE          0x00000200ULL
1059 00ede4c2 Sripathi Kodi
#define P9_STATS_BLOCKS        0x00000400ULL
1060 00ede4c2 Sripathi Kodi
1061 00ede4c2 Sripathi Kodi
#define P9_STATS_BTIME         0x00000800ULL
1062 00ede4c2 Sripathi Kodi
#define P9_STATS_GEN           0x00001000ULL
1063 00ede4c2 Sripathi Kodi
#define P9_STATS_DATA_VERSION  0x00002000ULL
1064 00ede4c2 Sripathi Kodi
1065 00ede4c2 Sripathi Kodi
#define P9_STATS_BASIC         0x000007ffULL /* Mask for fields up to BLOCKS */
1066 00ede4c2 Sripathi Kodi
#define P9_STATS_ALL           0x00003fffULL /* Mask for All fields above */
1067 00ede4c2 Sripathi Kodi
1068 00ede4c2 Sripathi Kodi
1069 00ede4c2 Sripathi Kodi
static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
1070 00ede4c2 Sripathi Kodi
                            V9fsStatDotl *v9lstat)
1071 00ede4c2 Sripathi Kodi
{
1072 00ede4c2 Sripathi Kodi
    memset(v9lstat, 0, sizeof(*v9lstat));
1073 00ede4c2 Sripathi Kodi
1074 00ede4c2 Sripathi Kodi
    v9lstat->st_mode = stbuf->st_mode;
1075 00ede4c2 Sripathi Kodi
    v9lstat->st_nlink = stbuf->st_nlink;
1076 00ede4c2 Sripathi Kodi
    v9lstat->st_uid = stbuf->st_uid;
1077 00ede4c2 Sripathi Kodi
    v9lstat->st_gid = stbuf->st_gid;
1078 00ede4c2 Sripathi Kodi
    v9lstat->st_rdev = stbuf->st_rdev;
1079 00ede4c2 Sripathi Kodi
    v9lstat->st_size = stbuf->st_size;
1080 00ede4c2 Sripathi Kodi
    v9lstat->st_blksize = stbuf->st_blksize;
1081 00ede4c2 Sripathi Kodi
    v9lstat->st_blocks = stbuf->st_blocks;
1082 00ede4c2 Sripathi Kodi
    v9lstat->st_atime_sec = stbuf->st_atime;
1083 00ede4c2 Sripathi Kodi
    v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1084 00ede4c2 Sripathi Kodi
    v9lstat->st_mtime_sec = stbuf->st_mtime;
1085 00ede4c2 Sripathi Kodi
    v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
1086 00ede4c2 Sripathi Kodi
    v9lstat->st_ctime_sec = stbuf->st_ctime;
1087 00ede4c2 Sripathi Kodi
    v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1088 00ede4c2 Sripathi Kodi
    /* Currently we only support BASIC fields in stat */
1089 00ede4c2 Sripathi Kodi
    v9lstat->st_result_mask = P9_STATS_BASIC;
1090 00ede4c2 Sripathi Kodi
1091 00ede4c2 Sripathi Kodi
    stat_to_qid(stbuf, &v9lstat->qid);
1092 00ede4c2 Sripathi Kodi
}
1093 00ede4c2 Sripathi Kodi
1094 1f5a89bf Anthony Liguori
static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
1095 1f5a89bf Anthony Liguori
{
1096 1f5a89bf Anthony Liguori
    while (len && *iovcnt) {
1097 1f5a89bf Anthony Liguori
        if (len < sg->iov_len) {
1098 1f5a89bf Anthony Liguori
            sg->iov_len -= len;
1099 1f5a89bf Anthony Liguori
            sg->iov_base += len;
1100 1f5a89bf Anthony Liguori
            len = 0;
1101 1f5a89bf Anthony Liguori
        } else {
1102 1f5a89bf Anthony Liguori
            len -= sg->iov_len;
1103 1f5a89bf Anthony Liguori
            sg++;
1104 1f5a89bf Anthony Liguori
            *iovcnt -= 1;
1105 1f5a89bf Anthony Liguori
        }
1106 1f5a89bf Anthony Liguori
    }
1107 1f5a89bf Anthony Liguori
1108 1f5a89bf Anthony Liguori
    return sg;
1109 1f5a89bf Anthony Liguori
}
1110 1f5a89bf Anthony Liguori
1111 1f5a89bf Anthony Liguori
static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
1112 1f5a89bf Anthony Liguori
{
1113 1f5a89bf Anthony Liguori
    int i;
1114 1f5a89bf Anthony Liguori
    int total = 0;
1115 1f5a89bf Anthony Liguori
1116 1f5a89bf Anthony Liguori
    for (i = 0; i < *cnt; i++) {
1117 1f5a89bf Anthony Liguori
        if ((total + sg[i].iov_len) > cap) {
1118 1f5a89bf Anthony Liguori
            sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
1119 1f5a89bf Anthony Liguori
            i++;
1120 1f5a89bf Anthony Liguori
            break;
1121 1f5a89bf Anthony Liguori
        }
1122 1f5a89bf Anthony Liguori
        total += sg[i].iov_len;
1123 1f5a89bf Anthony Liguori
    }
1124 1f5a89bf Anthony Liguori
1125 1f5a89bf Anthony Liguori
    *cnt = i;
1126 1f5a89bf Anthony Liguori
1127 1f5a89bf Anthony Liguori
    return sg;
1128 1f5a89bf Anthony Liguori
}
1129 1f5a89bf Anthony Liguori
1130 1f5a89bf Anthony Liguori
static void print_sg(struct iovec *sg, int cnt)
1131 1f5a89bf Anthony Liguori
{
1132 1f5a89bf Anthony Liguori
    int i;
1133 1f5a89bf Anthony Liguori
1134 1f5a89bf Anthony Liguori
    printf("sg[%d]: {", cnt);
1135 1f5a89bf Anthony Liguori
    for (i = 0; i < cnt; i++) {
1136 1f5a89bf Anthony Liguori
        if (i) {
1137 1f5a89bf Anthony Liguori
            printf(", ");
1138 1f5a89bf Anthony Liguori
        }
1139 1f5a89bf Anthony Liguori
        printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1140 1f5a89bf Anthony Liguori
    }
1141 1f5a89bf Anthony Liguori
    printf("}\n");
1142 1f5a89bf Anthony Liguori
}
1143 1f5a89bf Anthony Liguori
1144 8cf89e00 Anthony Liguori
static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
1145 8cf89e00 Anthony Liguori
{
1146 8cf89e00 Anthony Liguori
    V9fsString str;
1147 8cf89e00 Anthony Liguori
    v9fs_string_init(&str);
1148 8cf89e00 Anthony Liguori
    v9fs_string_copy(&str, dst);
1149 8cf89e00 Anthony Liguori
    v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
1150 8cf89e00 Anthony Liguori
    v9fs_string_free(&str);
1151 8cf89e00 Anthony Liguori
}
1152 8cf89e00 Anthony Liguori
1153 9f107513 Anthony Liguori
static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
1154 9f107513 Anthony Liguori
{
1155 92c1ad03 Anthony Liguori
    V9fsString version;
1156 92c1ad03 Anthony Liguori
    size_t offset = 7;
1157 92c1ad03 Anthony Liguori
1158 5e94c103 M. Mohan Kumar
    pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1159 92c1ad03 Anthony Liguori
1160 84151514 M. Mohan Kumar
    if (!strcmp(version.data, "9P2000.u")) {
1161 84151514 M. Mohan Kumar
        s->proto_version = V9FS_PROTO_2000U;
1162 84151514 M. Mohan Kumar
    } else if (!strcmp(version.data, "9P2000.L")) {
1163 84151514 M. Mohan Kumar
        s->proto_version = V9FS_PROTO_2000L;
1164 84151514 M. Mohan Kumar
    } else {
1165 92c1ad03 Anthony Liguori
        v9fs_string_sprintf(&version, "unknown");
1166 9f107513 Anthony Liguori
    }
1167 92c1ad03 Anthony Liguori
1168 5e94c103 M. Mohan Kumar
    offset += pdu_marshal(pdu, offset, "ds", s->msize, &version);
1169 92c1ad03 Anthony Liguori
    complete_pdu(s, pdu, offset);
1170 92c1ad03 Anthony Liguori
1171 92c1ad03 Anthony Liguori
    v9fs_string_free(&version);
1172 9f107513 Anthony Liguori
}
1173 9f107513 Anthony Liguori
1174 9f107513 Anthony Liguori
static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
1175 9f107513 Anthony Liguori
{
1176 955efc47 Anthony Liguori
    int32_t fid, afid, n_uname;
1177 955efc47 Anthony Liguori
    V9fsString uname, aname;
1178 955efc47 Anthony Liguori
    V9fsFidState *fidp;
1179 955efc47 Anthony Liguori
    V9fsQID qid;
1180 955efc47 Anthony Liguori
    size_t offset = 7;
1181 955efc47 Anthony Liguori
    ssize_t err;
1182 955efc47 Anthony Liguori
1183 955efc47 Anthony Liguori
    pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1184 955efc47 Anthony Liguori
1185 955efc47 Anthony Liguori
    fidp = alloc_fid(s, fid);
1186 955efc47 Anthony Liguori
    if (fidp == NULL) {
1187 955efc47 Anthony Liguori
        err = -EINVAL;
1188 955efc47 Anthony Liguori
        goto out;
1189 9f107513 Anthony Liguori
    }
1190 955efc47 Anthony Liguori
1191 955efc47 Anthony Liguori
    fidp->uid = n_uname;
1192 955efc47 Anthony Liguori
1193 955efc47 Anthony Liguori
    v9fs_string_sprintf(&fidp->path, "%s", "/");
1194 955efc47 Anthony Liguori
    err = fid_to_qid(s, fidp, &qid);
1195 955efc47 Anthony Liguori
    if (err) {
1196 955efc47 Anthony Liguori
        err = -EINVAL;
1197 955efc47 Anthony Liguori
        free_fid(s, fid);
1198 955efc47 Anthony Liguori
        goto out;
1199 955efc47 Anthony Liguori
    }
1200 955efc47 Anthony Liguori
1201 955efc47 Anthony Liguori
    offset += pdu_marshal(pdu, offset, "Q", &qid);
1202 955efc47 Anthony Liguori
1203 955efc47 Anthony Liguori
    err = offset;
1204 955efc47 Anthony Liguori
out:
1205 955efc47 Anthony Liguori
    complete_pdu(s, pdu, err);
1206 955efc47 Anthony Liguori
    v9fs_string_free(&uname);
1207 955efc47 Anthony Liguori
    v9fs_string_free(&aname);
1208 9f107513 Anthony Liguori
}
1209 9f107513 Anthony Liguori
1210 4da7d3fa Anthony Liguori
static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1211 4da7d3fa Anthony Liguori
{
1212 4da7d3fa Anthony Liguori
    if (err == -1) {
1213 4da7d3fa Anthony Liguori
        err = -errno;
1214 4da7d3fa Anthony Liguori
        goto out;
1215 4da7d3fa Anthony Liguori
    }
1216 4da7d3fa Anthony Liguori
1217 4da7d3fa Anthony Liguori
    err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1218 4da7d3fa Anthony Liguori
    if (err) {
1219 4da7d3fa Anthony Liguori
        goto out;
1220 4da7d3fa Anthony Liguori
    }
1221 4da7d3fa Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1222 4da7d3fa Anthony Liguori
    err = vs->offset;
1223 4da7d3fa Anthony Liguori
1224 4da7d3fa Anthony Liguori
out:
1225 4da7d3fa Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1226 4da7d3fa Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
1227 4da7d3fa Anthony Liguori
    qemu_free(vs);
1228 4da7d3fa Anthony Liguori
}
1229 4da7d3fa Anthony Liguori
1230 9f107513 Anthony Liguori
static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
1231 9f107513 Anthony Liguori
{
1232 4da7d3fa Anthony Liguori
    int32_t fid;
1233 4da7d3fa Anthony Liguori
    V9fsStatState *vs;
1234 4da7d3fa Anthony Liguori
    ssize_t err = 0;
1235 4da7d3fa Anthony Liguori
1236 4da7d3fa Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
1237 4da7d3fa Anthony Liguori
    vs->pdu = pdu;
1238 4da7d3fa Anthony Liguori
    vs->offset = 7;
1239 4da7d3fa Anthony Liguori
1240 4da7d3fa Anthony Liguori
    memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1241 4da7d3fa Anthony Liguori
1242 4da7d3fa Anthony Liguori
    pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1243 4da7d3fa Anthony Liguori
1244 4da7d3fa Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
1245 4da7d3fa Anthony Liguori
    if (vs->fidp == NULL) {
1246 4da7d3fa Anthony Liguori
        err = -ENOENT;
1247 4da7d3fa Anthony Liguori
        goto out;
1248 9f107513 Anthony Liguori
    }
1249 4da7d3fa Anthony Liguori
1250 4da7d3fa Anthony Liguori
    err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1251 4da7d3fa Anthony Liguori
    v9fs_stat_post_lstat(s, vs, err);
1252 4da7d3fa Anthony Liguori
    return;
1253 4da7d3fa Anthony Liguori
1254 4da7d3fa Anthony Liguori
out:
1255 4da7d3fa Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1256 4da7d3fa Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
1257 4da7d3fa Anthony Liguori
    qemu_free(vs);
1258 9f107513 Anthony Liguori
}
1259 9f107513 Anthony Liguori
1260 00ede4c2 Sripathi Kodi
static void v9fs_getattr_post_lstat(V9fsState *s, V9fsStatStateDotl *vs,
1261 00ede4c2 Sripathi Kodi
                                                                int err)
1262 00ede4c2 Sripathi Kodi
{
1263 00ede4c2 Sripathi Kodi
    if (err == -1) {
1264 00ede4c2 Sripathi Kodi
        err = -errno;
1265 00ede4c2 Sripathi Kodi
        goto out;
1266 00ede4c2 Sripathi Kodi
    }
1267 00ede4c2 Sripathi Kodi
1268 00ede4c2 Sripathi Kodi
    stat_to_v9stat_dotl(s, &vs->stbuf, &vs->v9stat_dotl);
1269 00ede4c2 Sripathi Kodi
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "A", &vs->v9stat_dotl);
1270 00ede4c2 Sripathi Kodi
    err = vs->offset;
1271 00ede4c2 Sripathi Kodi
1272 00ede4c2 Sripathi Kodi
out:
1273 00ede4c2 Sripathi Kodi
    complete_pdu(s, vs->pdu, err);
1274 00ede4c2 Sripathi Kodi
    qemu_free(vs);
1275 00ede4c2 Sripathi Kodi
}
1276 00ede4c2 Sripathi Kodi
1277 00ede4c2 Sripathi Kodi
static void v9fs_getattr(V9fsState *s, V9fsPDU *pdu)
1278 00ede4c2 Sripathi Kodi
{
1279 00ede4c2 Sripathi Kodi
    int32_t fid;
1280 00ede4c2 Sripathi Kodi
    V9fsStatStateDotl *vs;
1281 00ede4c2 Sripathi Kodi
    ssize_t err = 0;
1282 00ede4c2 Sripathi Kodi
    V9fsFidState *fidp;
1283 00ede4c2 Sripathi Kodi
    uint64_t request_mask;
1284 00ede4c2 Sripathi Kodi
1285 00ede4c2 Sripathi Kodi
    vs = qemu_malloc(sizeof(*vs));
1286 00ede4c2 Sripathi Kodi
    vs->pdu = pdu;
1287 00ede4c2 Sripathi Kodi
    vs->offset = 7;
1288 00ede4c2 Sripathi Kodi
1289 00ede4c2 Sripathi Kodi
    memset(&vs->v9stat_dotl, 0, sizeof(vs->v9stat_dotl));
1290 00ede4c2 Sripathi Kodi
1291 00ede4c2 Sripathi Kodi
    pdu_unmarshal(vs->pdu, vs->offset, "dq", &fid, &request_mask);
1292 00ede4c2 Sripathi Kodi
1293 00ede4c2 Sripathi Kodi
    fidp = lookup_fid(s, fid);
1294 00ede4c2 Sripathi Kodi
    if (fidp == NULL) {
1295 00ede4c2 Sripathi Kodi
        err = -ENOENT;
1296 00ede4c2 Sripathi Kodi
        goto out;
1297 00ede4c2 Sripathi Kodi
    }
1298 00ede4c2 Sripathi Kodi
1299 00ede4c2 Sripathi Kodi
    /* Currently we only support BASIC fields in stat, so there is no
1300 00ede4c2 Sripathi Kodi
     * need to look at request_mask.
1301 00ede4c2 Sripathi Kodi
     */
1302 00ede4c2 Sripathi Kodi
    err = v9fs_do_lstat(s, &fidp->path, &vs->stbuf);
1303 00ede4c2 Sripathi Kodi
    v9fs_getattr_post_lstat(s, vs, err);
1304 00ede4c2 Sripathi Kodi
    return;
1305 00ede4c2 Sripathi Kodi
1306 00ede4c2 Sripathi Kodi
out:
1307 00ede4c2 Sripathi Kodi
    complete_pdu(s, vs->pdu, err);
1308 00ede4c2 Sripathi Kodi
    qemu_free(vs);
1309 00ede4c2 Sripathi Kodi
}
1310 00ede4c2 Sripathi Kodi
1311 c79ce737 Sripathi Kodi
/* From Linux kernel code */
1312 c79ce737 Sripathi Kodi
#define ATTR_MODE    (1 << 0)
1313 c79ce737 Sripathi Kodi
#define ATTR_UID     (1 << 1)
1314 c79ce737 Sripathi Kodi
#define ATTR_GID     (1 << 2)
1315 c79ce737 Sripathi Kodi
#define ATTR_SIZE    (1 << 3)
1316 c79ce737 Sripathi Kodi
#define ATTR_ATIME   (1 << 4)
1317 c79ce737 Sripathi Kodi
#define ATTR_MTIME   (1 << 5)
1318 c79ce737 Sripathi Kodi
#define ATTR_CTIME   (1 << 6)
1319 c79ce737 Sripathi Kodi
#define ATTR_MASK    127
1320 c79ce737 Sripathi Kodi
#define ATTR_ATIME_SET  (1 << 7)
1321 c79ce737 Sripathi Kodi
#define ATTR_MTIME_SET  (1 << 8)
1322 c79ce737 Sripathi Kodi
1323 c79ce737 Sripathi Kodi
static void v9fs_setattr_post_truncate(V9fsState *s, V9fsSetattrState *vs,
1324 c79ce737 Sripathi Kodi
                                                                  int err)
1325 c79ce737 Sripathi Kodi
{
1326 c79ce737 Sripathi Kodi
    if (err == -1) {
1327 c79ce737 Sripathi Kodi
        err = -errno;
1328 c79ce737 Sripathi Kodi
        goto out;
1329 c79ce737 Sripathi Kodi
    }
1330 c79ce737 Sripathi Kodi
    err = vs->offset;
1331 c79ce737 Sripathi Kodi
1332 c79ce737 Sripathi Kodi
out:
1333 c79ce737 Sripathi Kodi
    complete_pdu(s, vs->pdu, err);
1334 c79ce737 Sripathi Kodi
    qemu_free(vs);
1335 c79ce737 Sripathi Kodi
}
1336 c79ce737 Sripathi Kodi
1337 c79ce737 Sripathi Kodi
static void v9fs_setattr_post_chown(V9fsState *s, V9fsSetattrState *vs, int err)
1338 c79ce737 Sripathi Kodi
{
1339 c79ce737 Sripathi Kodi
    if (err == -1) {
1340 c79ce737 Sripathi Kodi
        err = -errno;
1341 c79ce737 Sripathi Kodi
        goto out;
1342 c79ce737 Sripathi Kodi
    }
1343 c79ce737 Sripathi Kodi
1344 c79ce737 Sripathi Kodi
    if (vs->v9iattr.valid & (ATTR_SIZE)) {
1345 c79ce737 Sripathi Kodi
        err = v9fs_do_truncate(s, &vs->fidp->path, vs->v9iattr.size);
1346 c79ce737 Sripathi Kodi
    }
1347 c79ce737 Sripathi Kodi
    v9fs_setattr_post_truncate(s, vs, err);
1348 c79ce737 Sripathi Kodi
    return;
1349 c79ce737 Sripathi Kodi
1350 c79ce737 Sripathi Kodi
out:
1351 c79ce737 Sripathi Kodi
    complete_pdu(s, vs->pdu, err);
1352 c79ce737 Sripathi Kodi
    qemu_free(vs);
1353 c79ce737 Sripathi Kodi
}
1354 c79ce737 Sripathi Kodi
1355 c79ce737 Sripathi Kodi
static void v9fs_setattr_post_utimensat(V9fsState *s, V9fsSetattrState *vs,
1356 c79ce737 Sripathi Kodi
                                                                   int err)
1357 c79ce737 Sripathi Kodi
{
1358 c79ce737 Sripathi Kodi
    if (err == -1) {
1359 c79ce737 Sripathi Kodi
        err = -errno;
1360 c79ce737 Sripathi Kodi
        goto out;
1361 c79ce737 Sripathi Kodi
    }
1362 c79ce737 Sripathi Kodi
1363 c79ce737 Sripathi Kodi
    /* If the only valid entry in iattr is ctime we can call
1364 c79ce737 Sripathi Kodi
     * chown(-1,-1) to update the ctime of the file
1365 c79ce737 Sripathi Kodi
     */
1366 c79ce737 Sripathi Kodi
    if ((vs->v9iattr.valid & (ATTR_UID | ATTR_GID)) ||
1367 c79ce737 Sripathi Kodi
            ((vs->v9iattr.valid & ATTR_CTIME)
1368 c79ce737 Sripathi Kodi
            && !((vs->v9iattr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
1369 c79ce737 Sripathi Kodi
        if (!(vs->v9iattr.valid & ATTR_UID)) {
1370 c79ce737 Sripathi Kodi
            vs->v9iattr.uid = -1;
1371 c79ce737 Sripathi Kodi
        }
1372 c79ce737 Sripathi Kodi
        if (!(vs->v9iattr.valid & ATTR_GID)) {
1373 c79ce737 Sripathi Kodi
            vs->v9iattr.gid = -1;
1374 c79ce737 Sripathi Kodi
        }
1375 c79ce737 Sripathi Kodi
        err = v9fs_do_chown(s, &vs->fidp->path, vs->v9iattr.uid,
1376 c79ce737 Sripathi Kodi
                                                vs->v9iattr.gid);
1377 c79ce737 Sripathi Kodi
    }
1378 c79ce737 Sripathi Kodi
    v9fs_setattr_post_chown(s, vs, err);
1379 c79ce737 Sripathi Kodi
    return;
1380 c79ce737 Sripathi Kodi
1381 c79ce737 Sripathi Kodi
out:
1382 c79ce737 Sripathi Kodi
    complete_pdu(s, vs->pdu, err);
1383 c79ce737 Sripathi Kodi
    qemu_free(vs);
1384 c79ce737 Sripathi Kodi
}
1385 c79ce737 Sripathi Kodi
1386 c79ce737 Sripathi Kodi
static void v9fs_setattr_post_chmod(V9fsState *s, V9fsSetattrState *vs, int err)
1387 c79ce737 Sripathi Kodi
{
1388 c79ce737 Sripathi Kodi
    if (err == -1) {
1389 c79ce737 Sripathi Kodi
        err = -errno;
1390 c79ce737 Sripathi Kodi
        goto out;
1391 c79ce737 Sripathi Kodi
    }
1392 c79ce737 Sripathi Kodi
1393 c79ce737 Sripathi Kodi
    if (vs->v9iattr.valid & (ATTR_ATIME | ATTR_MTIME)) {
1394 c79ce737 Sripathi Kodi
        struct timespec times[2];
1395 c79ce737 Sripathi Kodi
        if (vs->v9iattr.valid & ATTR_ATIME) {
1396 c79ce737 Sripathi Kodi
            if (vs->v9iattr.valid & ATTR_ATIME_SET) {
1397 c79ce737 Sripathi Kodi
                times[0].tv_sec = vs->v9iattr.atime_sec;
1398 c79ce737 Sripathi Kodi
                times[0].tv_nsec = vs->v9iattr.atime_nsec;
1399 c79ce737 Sripathi Kodi
            } else {
1400 c79ce737 Sripathi Kodi
                times[0].tv_nsec = UTIME_NOW;
1401 c79ce737 Sripathi Kodi
            }
1402 c79ce737 Sripathi Kodi
        } else {
1403 c79ce737 Sripathi Kodi
            times[0].tv_nsec = UTIME_OMIT;
1404 c79ce737 Sripathi Kodi
        }
1405 c79ce737 Sripathi Kodi
1406 c79ce737 Sripathi Kodi
        if (vs->v9iattr.valid & ATTR_MTIME) {
1407 c79ce737 Sripathi Kodi
            if (vs->v9iattr.valid & ATTR_MTIME_SET) {
1408 c79ce737 Sripathi Kodi
                times[1].tv_sec = vs->v9iattr.mtime_sec;
1409 c79ce737 Sripathi Kodi
                times[1].tv_nsec = vs->v9iattr.mtime_nsec;
1410 c79ce737 Sripathi Kodi
            } else {
1411 c79ce737 Sripathi Kodi
                times[1].tv_nsec = UTIME_NOW;
1412 c79ce737 Sripathi Kodi
            }
1413 c79ce737 Sripathi Kodi
        } else {
1414 c79ce737 Sripathi Kodi
            times[1].tv_nsec = UTIME_OMIT;
1415 c79ce737 Sripathi Kodi
        }
1416 c79ce737 Sripathi Kodi
        err = v9fs_do_utimensat(s, &vs->fidp->path, times);
1417 c79ce737 Sripathi Kodi
    }
1418 c79ce737 Sripathi Kodi
    v9fs_setattr_post_utimensat(s, vs, err);
1419 c79ce737 Sripathi Kodi
    return;
1420 c79ce737 Sripathi Kodi
1421 c79ce737 Sripathi Kodi
out:
1422 c79ce737 Sripathi Kodi
    complete_pdu(s, vs->pdu, err);
1423 c79ce737 Sripathi Kodi
    qemu_free(vs);
1424 c79ce737 Sripathi Kodi
}
1425 c79ce737 Sripathi Kodi
1426 c79ce737 Sripathi Kodi
static void v9fs_setattr(V9fsState *s, V9fsPDU *pdu)
1427 c79ce737 Sripathi Kodi
{
1428 c79ce737 Sripathi Kodi
    int32_t fid;
1429 c79ce737 Sripathi Kodi
    V9fsSetattrState *vs;
1430 c79ce737 Sripathi Kodi
    int err = 0;
1431 c79ce737 Sripathi Kodi
1432 c79ce737 Sripathi Kodi
    vs = qemu_malloc(sizeof(*vs));
1433 c79ce737 Sripathi Kodi
    vs->pdu = pdu;
1434 c79ce737 Sripathi Kodi
    vs->offset = 7;
1435 c79ce737 Sripathi Kodi
1436 c79ce737 Sripathi Kodi
    pdu_unmarshal(pdu, vs->offset, "dI", &fid, &vs->v9iattr);
1437 c79ce737 Sripathi Kodi
1438 c79ce737 Sripathi Kodi
    vs->fidp = lookup_fid(s, fid);
1439 c79ce737 Sripathi Kodi
    if (vs->fidp == NULL) {
1440 c79ce737 Sripathi Kodi
        err = -EINVAL;
1441 c79ce737 Sripathi Kodi
        goto out;
1442 c79ce737 Sripathi Kodi
    }
1443 c79ce737 Sripathi Kodi
1444 c79ce737 Sripathi Kodi
    if (vs->v9iattr.valid & ATTR_MODE) {
1445 c79ce737 Sripathi Kodi
        err = v9fs_do_chmod(s, &vs->fidp->path, vs->v9iattr.mode);
1446 c79ce737 Sripathi Kodi
    }
1447 c79ce737 Sripathi Kodi
1448 c79ce737 Sripathi Kodi
    v9fs_setattr_post_chmod(s, vs, err);
1449 c79ce737 Sripathi Kodi
    return;
1450 c79ce737 Sripathi Kodi
1451 c79ce737 Sripathi Kodi
out:
1452 c79ce737 Sripathi Kodi
    complete_pdu(s, vs->pdu, err);
1453 c79ce737 Sripathi Kodi
    qemu_free(vs);
1454 c79ce737 Sripathi Kodi
}
1455 c79ce737 Sripathi Kodi
1456 ff5e54c9 Anthony Liguori
static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1457 ff5e54c9 Anthony Liguori
{
1458 ff5e54c9 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1459 ff5e54c9 Anthony Liguori
1460 ff5e54c9 Anthony Liguori
    if (vs->nwnames) {
1461 ff5e54c9 Anthony Liguori
        for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1462 ff5e54c9 Anthony Liguori
            v9fs_string_free(&vs->wnames[vs->name_idx]);
1463 ff5e54c9 Anthony Liguori
        }
1464 ff5e54c9 Anthony Liguori
1465 ff5e54c9 Anthony Liguori
        qemu_free(vs->wnames);
1466 ff5e54c9 Anthony Liguori
        qemu_free(vs->qids);
1467 ff5e54c9 Anthony Liguori
    }
1468 ff5e54c9 Anthony Liguori
}
1469 ff5e54c9 Anthony Liguori
1470 ff5e54c9 Anthony Liguori
static void v9fs_walk_marshal(V9fsWalkState *vs)
1471 ff5e54c9 Anthony Liguori
{
1472 ff5e54c9 Anthony Liguori
    int i;
1473 ff5e54c9 Anthony Liguori
    vs->offset = 7;
1474 ff5e54c9 Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1475 ff5e54c9 Anthony Liguori
1476 ff5e54c9 Anthony Liguori
    for (i = 0; i < vs->nwnames; i++) {
1477 ff5e54c9 Anthony Liguori
        vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1478 ff5e54c9 Anthony Liguori
    }
1479 ff5e54c9 Anthony Liguori
}
1480 ff5e54c9 Anthony Liguori
1481 ff5e54c9 Anthony Liguori
static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1482 ff5e54c9 Anthony Liguori
                                                                int err)
1483 ff5e54c9 Anthony Liguori
{
1484 ff5e54c9 Anthony Liguori
    if (err == -1) {
1485 ff5e54c9 Anthony Liguori
        free_fid(s, vs->newfidp->fid);
1486 ff5e54c9 Anthony Liguori
        v9fs_string_free(&vs->path);
1487 ff5e54c9 Anthony Liguori
        err = -ENOENT;
1488 ff5e54c9 Anthony Liguori
        goto out;
1489 ff5e54c9 Anthony Liguori
    }
1490 ff5e54c9 Anthony Liguori
1491 ff5e54c9 Anthony Liguori
    stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1492 ff5e54c9 Anthony Liguori
1493 ff5e54c9 Anthony Liguori
    vs->name_idx++;
1494 ff5e54c9 Anthony Liguori
    if (vs->name_idx < vs->nwnames) {
1495 ff5e54c9 Anthony Liguori
        v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1496 ff5e54c9 Anthony Liguori
                                            vs->wnames[vs->name_idx].data);
1497 ff5e54c9 Anthony Liguori
        v9fs_string_copy(&vs->newfidp->path, &vs->path);
1498 ff5e54c9 Anthony Liguori
1499 ff5e54c9 Anthony Liguori
        err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1500 ff5e54c9 Anthony Liguori
        v9fs_walk_post_newfid_lstat(s, vs, err);
1501 ff5e54c9 Anthony Liguori
        return;
1502 ff5e54c9 Anthony Liguori
    }
1503 ff5e54c9 Anthony Liguori
1504 ff5e54c9 Anthony Liguori
    v9fs_string_free(&vs->path);
1505 ff5e54c9 Anthony Liguori
    v9fs_walk_marshal(vs);
1506 ff5e54c9 Anthony Liguori
    err = vs->offset;
1507 ff5e54c9 Anthony Liguori
out:
1508 ff5e54c9 Anthony Liguori
    v9fs_walk_complete(s, vs, err);
1509 ff5e54c9 Anthony Liguori
}
1510 ff5e54c9 Anthony Liguori
1511 ff5e54c9 Anthony Liguori
static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1512 ff5e54c9 Anthony Liguori
        int err)
1513 ff5e54c9 Anthony Liguori
{
1514 ff5e54c9 Anthony Liguori
    if (err == -1) {
1515 ff5e54c9 Anthony Liguori
        v9fs_string_free(&vs->path);
1516 ff5e54c9 Anthony Liguori
        err = -ENOENT;
1517 ff5e54c9 Anthony Liguori
        goto out;
1518 ff5e54c9 Anthony Liguori
    }
1519 ff5e54c9 Anthony Liguori
1520 ff5e54c9 Anthony Liguori
    stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1521 ff5e54c9 Anthony Liguori
    vs->name_idx++;
1522 ff5e54c9 Anthony Liguori
    if (vs->name_idx < vs->nwnames) {
1523 ff5e54c9 Anthony Liguori
1524 ff5e54c9 Anthony Liguori
        v9fs_string_sprintf(&vs->path, "%s/%s",
1525 ff5e54c9 Anthony Liguori
                vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1526 ff5e54c9 Anthony Liguori
        v9fs_string_copy(&vs->fidp->path, &vs->path);
1527 ff5e54c9 Anthony Liguori
1528 ff5e54c9 Anthony Liguori
        err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1529 ff5e54c9 Anthony Liguori
        v9fs_walk_post_oldfid_lstat(s, vs, err);
1530 ff5e54c9 Anthony Liguori
        return;
1531 ff5e54c9 Anthony Liguori
    }
1532 ff5e54c9 Anthony Liguori
1533 ff5e54c9 Anthony Liguori
    v9fs_string_free(&vs->path);
1534 ff5e54c9 Anthony Liguori
    v9fs_walk_marshal(vs);
1535 ff5e54c9 Anthony Liguori
    err = vs->offset;
1536 ff5e54c9 Anthony Liguori
out:
1537 ff5e54c9 Anthony Liguori
    v9fs_walk_complete(s, vs, err);
1538 ff5e54c9 Anthony Liguori
}
1539 ff5e54c9 Anthony Liguori
1540 9f107513 Anthony Liguori
static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1541 9f107513 Anthony Liguori
{
1542 ff5e54c9 Anthony Liguori
    int32_t fid, newfid;
1543 ff5e54c9 Anthony Liguori
    V9fsWalkState *vs;
1544 ff5e54c9 Anthony Liguori
    int err = 0;
1545 ff5e54c9 Anthony Liguori
    int i;
1546 ff5e54c9 Anthony Liguori
1547 ff5e54c9 Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
1548 ff5e54c9 Anthony Liguori
    vs->pdu = pdu;
1549 ff5e54c9 Anthony Liguori
    vs->wnames = NULL;
1550 ff5e54c9 Anthony Liguori
    vs->qids = NULL;
1551 ff5e54c9 Anthony Liguori
    vs->offset = 7;
1552 ff5e54c9 Anthony Liguori
1553 ff5e54c9 Anthony Liguori
    vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1554 ff5e54c9 Anthony Liguori
                                            &newfid, &vs->nwnames);
1555 ff5e54c9 Anthony Liguori
1556 ff5e54c9 Anthony Liguori
    if (vs->nwnames) {
1557 ff5e54c9 Anthony Liguori
        vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1558 ff5e54c9 Anthony Liguori
1559 ff5e54c9 Anthony Liguori
        vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1560 ff5e54c9 Anthony Liguori
1561 ff5e54c9 Anthony Liguori
        for (i = 0; i < vs->nwnames; i++) {
1562 ff5e54c9 Anthony Liguori
            vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1563 ff5e54c9 Anthony Liguori
                                            &vs->wnames[i]);
1564 ff5e54c9 Anthony Liguori
        }
1565 ff5e54c9 Anthony Liguori
    }
1566 ff5e54c9 Anthony Liguori
1567 ff5e54c9 Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
1568 ff5e54c9 Anthony Liguori
    if (vs->fidp == NULL) {
1569 ff5e54c9 Anthony Liguori
        err = -ENOENT;
1570 ff5e54c9 Anthony Liguori
        goto out;
1571 ff5e54c9 Anthony Liguori
    }
1572 ff5e54c9 Anthony Liguori
1573 ff5e54c9 Anthony Liguori
    /* FIXME: is this really valid? */
1574 ff5e54c9 Anthony Liguori
    if (fid == newfid) {
1575 ff5e54c9 Anthony Liguori
1576 d62dbb51 Aneesh Kumar K.V
        BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
1577 ff5e54c9 Anthony Liguori
        v9fs_string_init(&vs->path);
1578 ff5e54c9 Anthony Liguori
        vs->name_idx = 0;
1579 ff5e54c9 Anthony Liguori
1580 ff5e54c9 Anthony Liguori
        if (vs->name_idx < vs->nwnames) {
1581 ff5e54c9 Anthony Liguori
            v9fs_string_sprintf(&vs->path, "%s/%s",
1582 ff5e54c9 Anthony Liguori
                vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1583 ff5e54c9 Anthony Liguori
            v9fs_string_copy(&vs->fidp->path, &vs->path);
1584 ff5e54c9 Anthony Liguori
1585 ff5e54c9 Anthony Liguori
            err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1586 ff5e54c9 Anthony Liguori
            v9fs_walk_post_oldfid_lstat(s, vs, err);
1587 ff5e54c9 Anthony Liguori
            return;
1588 ff5e54c9 Anthony Liguori
        }
1589 ff5e54c9 Anthony Liguori
    } else {
1590 ff5e54c9 Anthony Liguori
        vs->newfidp = alloc_fid(s, newfid);
1591 ff5e54c9 Anthony Liguori
        if (vs->newfidp == NULL) {
1592 ff5e54c9 Anthony Liguori
            err = -EINVAL;
1593 ff5e54c9 Anthony Liguori
            goto out;
1594 ff5e54c9 Anthony Liguori
        }
1595 ff5e54c9 Anthony Liguori
1596 ff5e54c9 Anthony Liguori
        vs->newfidp->uid = vs->fidp->uid;
1597 ff5e54c9 Anthony Liguori
        v9fs_string_init(&vs->path);
1598 ff5e54c9 Anthony Liguori
        vs->name_idx = 0;
1599 ff5e54c9 Anthony Liguori
        v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1600 ff5e54c9 Anthony Liguori
1601 ff5e54c9 Anthony Liguori
        if (vs->name_idx < vs->nwnames) {
1602 ff5e54c9 Anthony Liguori
            v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1603 ff5e54c9 Anthony Liguori
                                vs->wnames[vs->name_idx].data);
1604 ff5e54c9 Anthony Liguori
            v9fs_string_copy(&vs->newfidp->path, &vs->path);
1605 ff5e54c9 Anthony Liguori
1606 ff5e54c9 Anthony Liguori
            err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1607 ff5e54c9 Anthony Liguori
            v9fs_walk_post_newfid_lstat(s, vs, err);
1608 ff5e54c9 Anthony Liguori
            return;
1609 ff5e54c9 Anthony Liguori
        }
1610 9f107513 Anthony Liguori
    }
1611 ff5e54c9 Anthony Liguori
1612 ff5e54c9 Anthony Liguori
    v9fs_walk_marshal(vs);
1613 ff5e54c9 Anthony Liguori
    err = vs->offset;
1614 ff5e54c9 Anthony Liguori
out:
1615 ff5e54c9 Anthony Liguori
    v9fs_walk_complete(s, vs, err);
1616 9f107513 Anthony Liguori
}
1617 9f107513 Anthony Liguori
1618 5e94c103 M. Mohan Kumar
static int32_t get_iounit(V9fsState *s, V9fsString *name)
1619 5e94c103 M. Mohan Kumar
{
1620 5e94c103 M. Mohan Kumar
    struct statfs stbuf;
1621 5e94c103 M. Mohan Kumar
    int32_t iounit = 0;
1622 5e94c103 M. Mohan Kumar
1623 5e94c103 M. Mohan Kumar
    /*
1624 5e94c103 M. Mohan Kumar
     * iounit should be multiples of f_bsize (host filesystem block size
1625 5e94c103 M. Mohan Kumar
     * and as well as less than (client msize - P9_IOHDRSZ))
1626 5e94c103 M. Mohan Kumar
     */
1627 5e94c103 M. Mohan Kumar
    if (!v9fs_do_statfs(s, name, &stbuf)) {
1628 5e94c103 M. Mohan Kumar
        iounit = stbuf.f_bsize;
1629 5e94c103 M. Mohan Kumar
        iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1630 5e94c103 M. Mohan Kumar
    }
1631 5e94c103 M. Mohan Kumar
1632 5e94c103 M. Mohan Kumar
    if (!iounit) {
1633 5e94c103 M. Mohan Kumar
        iounit = s->msize - P9_IOHDRSZ;
1634 5e94c103 M. Mohan Kumar
    }
1635 5e94c103 M. Mohan Kumar
    return iounit;
1636 5e94c103 M. Mohan Kumar
}
1637 5e94c103 M. Mohan Kumar
1638 a6568fe2 Anthony Liguori
static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1639 a6568fe2 Anthony Liguori
{
1640 d62dbb51 Aneesh Kumar K.V
    if (vs->fidp->fs.dir == NULL) {
1641 a6568fe2 Anthony Liguori
        err = -errno;
1642 a6568fe2 Anthony Liguori
        goto out;
1643 a6568fe2 Anthony Liguori
    }
1644 d62dbb51 Aneesh Kumar K.V
    vs->fidp->fid_type = P9_FID_DIR;
1645 a6568fe2 Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1646 a6568fe2 Anthony Liguori
    err = vs->offset;
1647 a6568fe2 Anthony Liguori
out:
1648 a6568fe2 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1649 a6568fe2 Anthony Liguori
    qemu_free(vs);
1650 a6568fe2 Anthony Liguori
1651 a6568fe2 Anthony Liguori
}
1652 a6568fe2 Anthony Liguori
1653 5e94c103 M. Mohan Kumar
static void v9fs_open_post_getiounit(V9fsState *s, V9fsOpenState *vs)
1654 5e94c103 M. Mohan Kumar
{
1655 5e94c103 M. Mohan Kumar
    int err;
1656 5e94c103 M. Mohan Kumar
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
1657 5e94c103 M. Mohan Kumar
    err = vs->offset;
1658 5e94c103 M. Mohan Kumar
    complete_pdu(s, vs->pdu, err);
1659 5e94c103 M. Mohan Kumar
    qemu_free(vs);
1660 5e94c103 M. Mohan Kumar
}
1661 5e94c103 M. Mohan Kumar
1662 a6568fe2 Anthony Liguori
static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1663 a6568fe2 Anthony Liguori
{
1664 d62dbb51 Aneesh Kumar K.V
    if (vs->fidp->fs.fd == -1) {
1665 a6568fe2 Anthony Liguori
        err = -errno;
1666 a6568fe2 Anthony Liguori
        goto out;
1667 a6568fe2 Anthony Liguori
    }
1668 d62dbb51 Aneesh Kumar K.V
    vs->fidp->fid_type = P9_FID_FILE;
1669 5e94c103 M. Mohan Kumar
    vs->iounit = get_iounit(s, &vs->fidp->path);
1670 5e94c103 M. Mohan Kumar
    v9fs_open_post_getiounit(s, vs);
1671 5e94c103 M. Mohan Kumar
    return;
1672 a6568fe2 Anthony Liguori
out:
1673 a6568fe2 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1674 a6568fe2 Anthony Liguori
    qemu_free(vs);
1675 a6568fe2 Anthony Liguori
}
1676 a6568fe2 Anthony Liguori
1677 771e9d4c M. Mohan Kumar
static inline int valid_flags(int flag)
1678 771e9d4c M. Mohan Kumar
{
1679 771e9d4c M. Mohan Kumar
    if (flag & O_NOCTTY || flag & O_NONBLOCK || flag & O_ASYNC ||
1680 771e9d4c M. Mohan Kumar
            flag & O_CLOEXEC)
1681 771e9d4c M. Mohan Kumar
        return 0;
1682 771e9d4c M. Mohan Kumar
    else
1683 771e9d4c M. Mohan Kumar
        return 1;
1684 771e9d4c M. Mohan Kumar
}
1685 771e9d4c M. Mohan Kumar
1686 a6568fe2 Anthony Liguori
static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1687 a6568fe2 Anthony Liguori
{
1688 771e9d4c M. Mohan Kumar
    int flags;
1689 771e9d4c M. Mohan Kumar
1690 a6568fe2 Anthony Liguori
    if (err) {
1691 a6568fe2 Anthony Liguori
        err = -errno;
1692 a6568fe2 Anthony Liguori
        goto out;
1693 a6568fe2 Anthony Liguori
    }
1694 a6568fe2 Anthony Liguori
1695 a6568fe2 Anthony Liguori
    stat_to_qid(&vs->stbuf, &vs->qid);
1696 a6568fe2 Anthony Liguori
1697 a6568fe2 Anthony Liguori
    if (S_ISDIR(vs->stbuf.st_mode)) {
1698 d62dbb51 Aneesh Kumar K.V
        vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fidp->path);
1699 a6568fe2 Anthony Liguori
        v9fs_open_post_opendir(s, vs, err);
1700 a6568fe2 Anthony Liguori
    } else {
1701 771e9d4c M. Mohan Kumar
        if (s->proto_version == V9FS_PROTO_2000L) {
1702 771e9d4c M. Mohan Kumar
            if (!valid_flags(vs->mode)) {
1703 771e9d4c M. Mohan Kumar
                err = -EINVAL;
1704 771e9d4c M. Mohan Kumar
                goto out;
1705 771e9d4c M. Mohan Kumar
            }
1706 771e9d4c M. Mohan Kumar
            flags = vs->mode;
1707 771e9d4c M. Mohan Kumar
        } else {
1708 771e9d4c M. Mohan Kumar
            flags = omode_to_uflags(vs->mode);
1709 771e9d4c M. Mohan Kumar
        }
1710 d62dbb51 Aneesh Kumar K.V
        vs->fidp->fs.fd = v9fs_do_open(s, &vs->fidp->path, flags);
1711 a6568fe2 Anthony Liguori
        v9fs_open_post_open(s, vs, err);
1712 a6568fe2 Anthony Liguori
    }
1713 a6568fe2 Anthony Liguori
    return;
1714 a6568fe2 Anthony Liguori
out:
1715 a6568fe2 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1716 a6568fe2 Anthony Liguori
    qemu_free(vs);
1717 9f107513 Anthony Liguori
}
1718 9f107513 Anthony Liguori
1719 9f107513 Anthony Liguori
static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
1720 a6568fe2 Anthony Liguori
{
1721 a6568fe2 Anthony Liguori
    int32_t fid;
1722 a6568fe2 Anthony Liguori
    V9fsOpenState *vs;
1723 a6568fe2 Anthony Liguori
    ssize_t err = 0;
1724 a6568fe2 Anthony Liguori
1725 a6568fe2 Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
1726 a6568fe2 Anthony Liguori
    vs->pdu = pdu;
1727 a6568fe2 Anthony Liguori
    vs->offset = 7;
1728 771e9d4c M. Mohan Kumar
    vs->mode = 0;
1729 a6568fe2 Anthony Liguori
1730 771e9d4c M. Mohan Kumar
    if (s->proto_version == V9FS_PROTO_2000L) {
1731 771e9d4c M. Mohan Kumar
        pdu_unmarshal(vs->pdu, vs->offset, "dd", &fid, &vs->mode);
1732 771e9d4c M. Mohan Kumar
    } else {
1733 771e9d4c M. Mohan Kumar
        pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1734 771e9d4c M. Mohan Kumar
    }
1735 a6568fe2 Anthony Liguori
1736 a6568fe2 Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
1737 a6568fe2 Anthony Liguori
    if (vs->fidp == NULL) {
1738 a6568fe2 Anthony Liguori
        err = -ENOENT;
1739 a6568fe2 Anthony Liguori
        goto out;
1740 a6568fe2 Anthony Liguori
    }
1741 a6568fe2 Anthony Liguori
1742 d62dbb51 Aneesh Kumar K.V
    BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
1743 a6568fe2 Anthony Liguori
1744 a6568fe2 Anthony Liguori
    err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1745 a6568fe2 Anthony Liguori
1746 a6568fe2 Anthony Liguori
    v9fs_open_post_lstat(s, vs, err);
1747 a6568fe2 Anthony Liguori
    return;
1748 a6568fe2 Anthony Liguori
out:
1749 a6568fe2 Anthony Liguori
    complete_pdu(s, pdu, err);
1750 a6568fe2 Anthony Liguori
    qemu_free(vs);
1751 a6568fe2 Anthony Liguori
}
1752 a6568fe2 Anthony Liguori
1753 c1568af5 Venkateswararao Jujjuri (JV)
static void v9fs_post_lcreate(V9fsState *s, V9fsLcreateState *vs, int err)
1754 c1568af5 Venkateswararao Jujjuri (JV)
{
1755 c1568af5 Venkateswararao Jujjuri (JV)
    if (err == 0) {
1756 c1568af5 Venkateswararao Jujjuri (JV)
        v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1757 c1568af5 Venkateswararao Jujjuri (JV)
        stat_to_qid(&vs->stbuf, &vs->qid);
1758 c1568af5 Venkateswararao Jujjuri (JV)
        vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid,
1759 c1568af5 Venkateswararao Jujjuri (JV)
                &vs->iounit);
1760 c1568af5 Venkateswararao Jujjuri (JV)
        err = vs->offset;
1761 c1568af5 Venkateswararao Jujjuri (JV)
    } else {
1762 d62dbb51 Aneesh Kumar K.V
        vs->fidp->fid_type = P9_FID_NONE;
1763 d62dbb51 Aneesh Kumar K.V
        close(vs->fidp->fs.fd);
1764 c1568af5 Venkateswararao Jujjuri (JV)
        err = -errno;
1765 c1568af5 Venkateswararao Jujjuri (JV)
    }
1766 c1568af5 Venkateswararao Jujjuri (JV)
1767 c1568af5 Venkateswararao Jujjuri (JV)
    complete_pdu(s, vs->pdu, err);
1768 c1568af5 Venkateswararao Jujjuri (JV)
    v9fs_string_free(&vs->name);
1769 c1568af5 Venkateswararao Jujjuri (JV)
    v9fs_string_free(&vs->fullname);
1770 c1568af5 Venkateswararao Jujjuri (JV)
    qemu_free(vs);
1771 c1568af5 Venkateswararao Jujjuri (JV)
}
1772 c1568af5 Venkateswararao Jujjuri (JV)
1773 c1568af5 Venkateswararao Jujjuri (JV)
static void v9fs_lcreate_post_get_iounit(V9fsState *s, V9fsLcreateState *vs,
1774 c1568af5 Venkateswararao Jujjuri (JV)
        int err)
1775 c1568af5 Venkateswararao Jujjuri (JV)
{
1776 c1568af5 Venkateswararao Jujjuri (JV)
    if (err) {
1777 c1568af5 Venkateswararao Jujjuri (JV)
        err = -errno;
1778 c1568af5 Venkateswararao Jujjuri (JV)
        goto out;
1779 c1568af5 Venkateswararao Jujjuri (JV)
    }
1780 c1568af5 Venkateswararao Jujjuri (JV)
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1781 c1568af5 Venkateswararao Jujjuri (JV)
1782 c1568af5 Venkateswararao Jujjuri (JV)
out:
1783 c1568af5 Venkateswararao Jujjuri (JV)
    v9fs_post_lcreate(s, vs, err);
1784 c1568af5 Venkateswararao Jujjuri (JV)
}
1785 c1568af5 Venkateswararao Jujjuri (JV)
1786 c1568af5 Venkateswararao Jujjuri (JV)
static void v9fs_lcreate_post_do_open2(V9fsState *s, V9fsLcreateState *vs,
1787 c1568af5 Venkateswararao Jujjuri (JV)
        int err)
1788 c1568af5 Venkateswararao Jujjuri (JV)
{
1789 d62dbb51 Aneesh Kumar K.V
    if (vs->fidp->fs.fd == -1) {
1790 c1568af5 Venkateswararao Jujjuri (JV)
        err = -errno;
1791 c1568af5 Venkateswararao Jujjuri (JV)
        goto out;
1792 c1568af5 Venkateswararao Jujjuri (JV)
    }
1793 d62dbb51 Aneesh Kumar K.V
    vs->fidp->fid_type = P9_FID_FILE;
1794 c1568af5 Venkateswararao Jujjuri (JV)
    vs->iounit =  get_iounit(s, &vs->fullname);
1795 c1568af5 Venkateswararao Jujjuri (JV)
    v9fs_lcreate_post_get_iounit(s, vs, err);
1796 c1568af5 Venkateswararao Jujjuri (JV)
    return;
1797 c1568af5 Venkateswararao Jujjuri (JV)
1798 c1568af5 Venkateswararao Jujjuri (JV)
out:
1799 c1568af5 Venkateswararao Jujjuri (JV)
    v9fs_post_lcreate(s, vs, err);
1800 c1568af5 Venkateswararao Jujjuri (JV)
}
1801 c1568af5 Venkateswararao Jujjuri (JV)
1802 c1568af5 Venkateswararao Jujjuri (JV)
static void v9fs_lcreate(V9fsState *s, V9fsPDU *pdu)
1803 c1568af5 Venkateswararao Jujjuri (JV)
{
1804 c1568af5 Venkateswararao Jujjuri (JV)
    int32_t dfid, flags, mode;
1805 c1568af5 Venkateswararao Jujjuri (JV)
    gid_t gid;
1806 c1568af5 Venkateswararao Jujjuri (JV)
    V9fsLcreateState *vs;
1807 c1568af5 Venkateswararao Jujjuri (JV)
    ssize_t err = 0;
1808 c1568af5 Venkateswararao Jujjuri (JV)
1809 c1568af5 Venkateswararao Jujjuri (JV)
    vs = qemu_malloc(sizeof(*vs));
1810 c1568af5 Venkateswararao Jujjuri (JV)
    vs->pdu = pdu;
1811 c1568af5 Venkateswararao Jujjuri (JV)
    vs->offset = 7;
1812 c1568af5 Venkateswararao Jujjuri (JV)
1813 c1568af5 Venkateswararao Jujjuri (JV)
    v9fs_string_init(&vs->fullname);
1814 c1568af5 Venkateswararao Jujjuri (JV)
1815 c1568af5 Venkateswararao Jujjuri (JV)
    pdu_unmarshal(vs->pdu, vs->offset, "dsddd", &dfid, &vs->name, &flags,
1816 c1568af5 Venkateswararao Jujjuri (JV)
            &mode, &gid);
1817 c1568af5 Venkateswararao Jujjuri (JV)
1818 c1568af5 Venkateswararao Jujjuri (JV)
    vs->fidp = lookup_fid(s, dfid);
1819 c1568af5 Venkateswararao Jujjuri (JV)
    if (vs->fidp == NULL) {
1820 c1568af5 Venkateswararao Jujjuri (JV)
        err = -ENOENT;
1821 c1568af5 Venkateswararao Jujjuri (JV)
        goto out;
1822 c1568af5 Venkateswararao Jujjuri (JV)
    }
1823 c1568af5 Venkateswararao Jujjuri (JV)
1824 c1568af5 Venkateswararao Jujjuri (JV)
    v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
1825 c1568af5 Venkateswararao Jujjuri (JV)
             vs->name.data);
1826 c1568af5 Venkateswararao Jujjuri (JV)
1827 d62dbb51 Aneesh Kumar K.V
    vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
1828 c1568af5 Venkateswararao Jujjuri (JV)
            gid, flags, mode);
1829 c1568af5 Venkateswararao Jujjuri (JV)
    v9fs_lcreate_post_do_open2(s, vs, err);
1830 c1568af5 Venkateswararao Jujjuri (JV)
    return;
1831 c1568af5 Venkateswararao Jujjuri (JV)
1832 c1568af5 Venkateswararao Jujjuri (JV)
out:
1833 c1568af5 Venkateswararao Jujjuri (JV)
    complete_pdu(s, vs->pdu, err);
1834 c1568af5 Venkateswararao Jujjuri (JV)
    v9fs_string_free(&vs->name);
1835 c1568af5 Venkateswararao Jujjuri (JV)
    qemu_free(vs);
1836 c1568af5 Venkateswararao Jujjuri (JV)
}
1837 c1568af5 Venkateswararao Jujjuri (JV)
1838 a6568fe2 Anthony Liguori
static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1839 a6568fe2 Anthony Liguori
{
1840 bbd5697b Anthony Liguori
    int32_t fid;
1841 bbd5697b Anthony Liguori
    size_t offset = 7;
1842 bbd5697b Anthony Liguori
    int err;
1843 bbd5697b Anthony Liguori
1844 bbd5697b Anthony Liguori
    pdu_unmarshal(pdu, offset, "d", &fid);
1845 bbd5697b Anthony Liguori
1846 bbd5697b Anthony Liguori
    err = free_fid(s, fid);
1847 bbd5697b Anthony Liguori
    if (err < 0) {
1848 bbd5697b Anthony Liguori
        goto out;
1849 a6568fe2 Anthony Liguori
    }
1850 bbd5697b Anthony Liguori
1851 bbd5697b Anthony Liguori
    offset = 7;
1852 bbd5697b Anthony Liguori
    err = offset;
1853 bbd5697b Anthony Liguori
out:
1854 bbd5697b Anthony Liguori
    complete_pdu(s, pdu, err);
1855 9f107513 Anthony Liguori
}
1856 9f107513 Anthony Liguori
1857 a9231555 Anthony Liguori
static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1858 a9231555 Anthony Liguori
1859 a9231555 Anthony Liguori
static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1860 a9231555 Anthony Liguori
{
1861 a9231555 Anthony Liguori
    if (err) {
1862 a9231555 Anthony Liguori
        goto out;
1863 a9231555 Anthony Liguori
    }
1864 a9231555 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
1865 a9231555 Anthony Liguori
    v9fs_string_free(&vs->name);
1866 a9231555 Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1867 a9231555 Anthony Liguori
    vs->offset += vs->count;
1868 a9231555 Anthony Liguori
    err = vs->offset;
1869 a9231555 Anthony Liguori
out:
1870 a9231555 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1871 a9231555 Anthony Liguori
    qemu_free(vs);
1872 a9231555 Anthony Liguori
    return;
1873 a9231555 Anthony Liguori
}
1874 a9231555 Anthony Liguori
1875 a9231555 Anthony Liguori
static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1876 a9231555 Anthony Liguori
                                    ssize_t err)
1877 a9231555 Anthony Liguori
{
1878 a9231555 Anthony Liguori
    if (err) {
1879 a9231555 Anthony Liguori
        err = -errno;
1880 a9231555 Anthony Liguori
        goto out;
1881 a9231555 Anthony Liguori
    }
1882 a9231555 Anthony Liguori
    err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1883 a9231555 Anthony Liguori
    if (err) {
1884 a9231555 Anthony Liguori
        goto out;
1885 a9231555 Anthony Liguori
    }
1886 a9231555 Anthony Liguori
1887 a9231555 Anthony Liguori
    vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1888 a9231555 Anthony Liguori
                            &vs->v9stat);
1889 a9231555 Anthony Liguori
    if ((vs->len != (vs->v9stat.size + 2)) ||
1890 a9231555 Anthony Liguori
            ((vs->count + vs->len) > vs->max_count)) {
1891 d62dbb51 Aneesh Kumar K.V
        v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1892 a9231555 Anthony Liguori
        v9fs_read_post_seekdir(s, vs, err);
1893 a9231555 Anthony Liguori
        return;
1894 a9231555 Anthony Liguori
    }
1895 a9231555 Anthony Liguori
    vs->count += vs->len;
1896 a9231555 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
1897 a9231555 Anthony Liguori
    v9fs_string_free(&vs->name);
1898 a9231555 Anthony Liguori
    vs->dir_pos = vs->dent->d_off;
1899 d62dbb51 Aneesh Kumar K.V
    vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
1900 a9231555 Anthony Liguori
    v9fs_read_post_readdir(s, vs, err);
1901 a9231555 Anthony Liguori
    return;
1902 a9231555 Anthony Liguori
out:
1903 d62dbb51 Aneesh Kumar K.V
    v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1904 a9231555 Anthony Liguori
    v9fs_read_post_seekdir(s, vs, err);
1905 a9231555 Anthony Liguori
    return;
1906 a9231555 Anthony Liguori
1907 a9231555 Anthony Liguori
}
1908 a9231555 Anthony Liguori
1909 a9231555 Anthony Liguori
static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1910 a9231555 Anthony Liguori
{
1911 a9231555 Anthony Liguori
    if (vs->dent) {
1912 a9231555 Anthony Liguori
        memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1913 a9231555 Anthony Liguori
        v9fs_string_init(&vs->name);
1914 a9231555 Anthony Liguori
        v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1915 a9231555 Anthony Liguori
                            vs->dent->d_name);
1916 a9231555 Anthony Liguori
        err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1917 a9231555 Anthony Liguori
        v9fs_read_post_dir_lstat(s, vs, err);
1918 a9231555 Anthony Liguori
        return;
1919 a9231555 Anthony Liguori
    }
1920 a9231555 Anthony Liguori
1921 a9231555 Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1922 a9231555 Anthony Liguori
    vs->offset += vs->count;
1923 a9231555 Anthony Liguori
    err = vs->offset;
1924 a9231555 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1925 a9231555 Anthony Liguori
    qemu_free(vs);
1926 a9231555 Anthony Liguori
    return;
1927 a9231555 Anthony Liguori
}
1928 a9231555 Anthony Liguori
1929 a9231555 Anthony Liguori
static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1930 a9231555 Anthony Liguori
{
1931 d62dbb51 Aneesh Kumar K.V
    vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
1932 a9231555 Anthony Liguori
    v9fs_read_post_readdir(s, vs, err);
1933 a9231555 Anthony Liguori
    return;
1934 a9231555 Anthony Liguori
}
1935 a9231555 Anthony Liguori
1936 a9231555 Anthony Liguori
static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
1937 a9231555 Anthony Liguori
                                       ssize_t err)
1938 a9231555 Anthony Liguori
{
1939 d62dbb51 Aneesh Kumar K.V
    vs->dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
1940 a9231555 Anthony Liguori
    v9fs_read_post_telldir(s, vs, err);
1941 a9231555 Anthony Liguori
    return;
1942 a9231555 Anthony Liguori
}
1943 a9231555 Anthony Liguori
1944 a9231555 Anthony Liguori
static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *vs, ssize_t err)
1945 a9231555 Anthony Liguori
{
1946 a9231555 Anthony Liguori
    if (err  < 0) {
1947 a9231555 Anthony Liguori
        /* IO error return the error */
1948 a9231555 Anthony Liguori
        err = -errno;
1949 a9231555 Anthony Liguori
        goto out;
1950 a9231555 Anthony Liguori
    }
1951 a9231555 Anthony Liguori
    vs->total += vs->len;
1952 a9231555 Anthony Liguori
    vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1953 a9231555 Anthony Liguori
    if (vs->total < vs->count && vs->len > 0) {
1954 a9231555 Anthony Liguori
        do {
1955 a9231555 Anthony Liguori
            if (0) {
1956 a9231555 Anthony Liguori
                print_sg(vs->sg, vs->cnt);
1957 a9231555 Anthony Liguori
            }
1958 d62dbb51 Aneesh Kumar K.V
            vs->len = v9fs_do_readv(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
1959 a9231555 Anthony Liguori
        } while (vs->len == -1 && errno == EINTR);
1960 a9231555 Anthony Liguori
        if (vs->len == -1) {
1961 a9231555 Anthony Liguori
            err  = -errno;
1962 a9231555 Anthony Liguori
        }
1963 a9231555 Anthony Liguori
        v9fs_read_post_readv(s, vs, err);
1964 a9231555 Anthony Liguori
        return;
1965 a9231555 Anthony Liguori
    }
1966 a9231555 Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1967 a9231555 Anthony Liguori
    vs->offset += vs->count;
1968 a9231555 Anthony Liguori
    err = vs->offset;
1969 a9231555 Anthony Liguori
1970 a9231555 Anthony Liguori
out:
1971 a9231555 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1972 a9231555 Anthony Liguori
    qemu_free(vs);
1973 a9231555 Anthony Liguori
}
1974 a9231555 Anthony Liguori
1975 a9231555 Anthony Liguori
static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err)
1976 a9231555 Anthony Liguori
{
1977 a9231555 Anthony Liguori
    if (err == -1) {
1978 a9231555 Anthony Liguori
        err = -errno;
1979 a9231555 Anthony Liguori
        goto out;
1980 a9231555 Anthony Liguori
    }
1981 a9231555 Anthony Liguori
    vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1982 a9231555 Anthony Liguori
1983 a9231555 Anthony Liguori
    if (vs->total < vs->count) {
1984 a9231555 Anthony Liguori
        do {
1985 a9231555 Anthony Liguori
            if (0) {
1986 a9231555 Anthony Liguori
                print_sg(vs->sg, vs->cnt);
1987 a9231555 Anthony Liguori
            }
1988 d62dbb51 Aneesh Kumar K.V
            vs->len = v9fs_do_readv(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
1989 a9231555 Anthony Liguori
        } while (vs->len == -1 && errno == EINTR);
1990 a9231555 Anthony Liguori
        if (vs->len == -1) {
1991 a9231555 Anthony Liguori
            err  = -errno;
1992 a9231555 Anthony Liguori
        }
1993 a9231555 Anthony Liguori
        v9fs_read_post_readv(s, vs, err);
1994 a9231555 Anthony Liguori
        return;
1995 a9231555 Anthony Liguori
    }
1996 a9231555 Anthony Liguori
out:
1997 a9231555 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
1998 a9231555 Anthony Liguori
    qemu_free(vs);
1999 a9231555 Anthony Liguori
}
2000 a9231555 Anthony Liguori
2001 fa32ef88 Aneesh Kumar K.V
static void v9fs_xattr_read(V9fsState *s, V9fsReadState *vs)
2002 fa32ef88 Aneesh Kumar K.V
{
2003 fa32ef88 Aneesh Kumar K.V
    ssize_t err = 0;
2004 fa32ef88 Aneesh Kumar K.V
    int read_count;
2005 fa32ef88 Aneesh Kumar K.V
    int64_t xattr_len;
2006 fa32ef88 Aneesh Kumar K.V
2007 fa32ef88 Aneesh Kumar K.V
    xattr_len = vs->fidp->fs.xattr.len;
2008 fa32ef88 Aneesh Kumar K.V
    read_count = xattr_len - vs->off;
2009 fa32ef88 Aneesh Kumar K.V
    if (read_count > vs->count) {
2010 fa32ef88 Aneesh Kumar K.V
        read_count = vs->count;
2011 fa32ef88 Aneesh Kumar K.V
    } else if (read_count < 0) {
2012 fa32ef88 Aneesh Kumar K.V
        /*
2013 fa32ef88 Aneesh Kumar K.V
         * read beyond XATTR value
2014 fa32ef88 Aneesh Kumar K.V
         */
2015 fa32ef88 Aneesh Kumar K.V
        read_count = 0;
2016 fa32ef88 Aneesh Kumar K.V
    }
2017 fa32ef88 Aneesh Kumar K.V
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", read_count);
2018 fa32ef88 Aneesh Kumar K.V
    vs->offset += pdu_pack(vs->pdu, vs->offset,
2019 fa32ef88 Aneesh Kumar K.V
                           ((char *)vs->fidp->fs.xattr.value) + vs->off,
2020 fa32ef88 Aneesh Kumar K.V
                           read_count);
2021 fa32ef88 Aneesh Kumar K.V
    err = vs->offset;
2022 fa32ef88 Aneesh Kumar K.V
    complete_pdu(s, vs->pdu, err);
2023 fa32ef88 Aneesh Kumar K.V
    qemu_free(vs);
2024 fa32ef88 Aneesh Kumar K.V
}
2025 fa32ef88 Aneesh Kumar K.V
2026 9f107513 Anthony Liguori
static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
2027 9f107513 Anthony Liguori
{
2028 a9231555 Anthony Liguori
    int32_t fid;
2029 a9231555 Anthony Liguori
    V9fsReadState *vs;
2030 a9231555 Anthony Liguori
    ssize_t err = 0;
2031 a9231555 Anthony Liguori
2032 a9231555 Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
2033 a9231555 Anthony Liguori
    vs->pdu = pdu;
2034 a9231555 Anthony Liguori
    vs->offset = 7;
2035 a9231555 Anthony Liguori
    vs->total = 0;
2036 a9231555 Anthony Liguori
    vs->len = 0;
2037 a9231555 Anthony Liguori
    vs->count = 0;
2038 a9231555 Anthony Liguori
2039 a9231555 Anthony Liguori
    pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count);
2040 a9231555 Anthony Liguori
2041 a9231555 Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
2042 a9231555 Anthony Liguori
    if (vs->fidp == NULL) {
2043 a9231555 Anthony Liguori
        err = -EINVAL;
2044 a9231555 Anthony Liguori
        goto out;
2045 a9231555 Anthony Liguori
    }
2046 a9231555 Anthony Liguori
2047 fa32ef88 Aneesh Kumar K.V
    if (vs->fidp->fid_type == P9_FID_DIR) {
2048 a9231555 Anthony Liguori
        vs->max_count = vs->count;
2049 a9231555 Anthony Liguori
        vs->count = 0;
2050 a9231555 Anthony Liguori
        if (vs->off == 0) {
2051 d62dbb51 Aneesh Kumar K.V
            v9fs_do_rewinddir(s, vs->fidp->fs.dir);
2052 a9231555 Anthony Liguori
        }
2053 a9231555 Anthony Liguori
        v9fs_read_post_rewinddir(s, vs, err);
2054 a9231555 Anthony Liguori
        return;
2055 fa32ef88 Aneesh Kumar K.V
    } else if (vs->fidp->fid_type == P9_FID_FILE) {
2056 a9231555 Anthony Liguori
        vs->sg = vs->iov;
2057 a9231555 Anthony Liguori
        pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
2058 d62dbb51 Aneesh Kumar K.V
        err = v9fs_do_lseek(s, vs->fidp->fs.fd, vs->off, SEEK_SET);
2059 a9231555 Anthony Liguori
        v9fs_read_post_lseek(s, vs, err);
2060 a9231555 Anthony Liguori
        return;
2061 fa32ef88 Aneesh Kumar K.V
    } else if (vs->fidp->fid_type == P9_FID_XATTR) {
2062 fa32ef88 Aneesh Kumar K.V
        v9fs_xattr_read(s, vs);
2063 fa32ef88 Aneesh Kumar K.V
        return;
2064 a9231555 Anthony Liguori
    } else {
2065 a9231555 Anthony Liguori
        err = -EINVAL;
2066 9f107513 Anthony Liguori
    }
2067 a9231555 Anthony Liguori
out:
2068 a9231555 Anthony Liguori
    complete_pdu(s, pdu, err);
2069 a9231555 Anthony Liguori
    qemu_free(vs);
2070 9f107513 Anthony Liguori
}
2071 9f107513 Anthony Liguori
2072 c18e2f94 Sripathi Kodi
typedef struct V9fsReadDirState {
2073 c18e2f94 Sripathi Kodi
    V9fsPDU *pdu;
2074 c18e2f94 Sripathi Kodi
    V9fsFidState *fidp;
2075 c18e2f94 Sripathi Kodi
    V9fsQID qid;
2076 c18e2f94 Sripathi Kodi
    off_t saved_dir_pos;
2077 c18e2f94 Sripathi Kodi
    struct dirent *dent;
2078 c18e2f94 Sripathi Kodi
    int32_t count;
2079 c18e2f94 Sripathi Kodi
    int32_t max_count;
2080 c18e2f94 Sripathi Kodi
    size_t offset;
2081 c18e2f94 Sripathi Kodi
    int64_t initial_offset;
2082 c18e2f94 Sripathi Kodi
    V9fsString name;
2083 c18e2f94 Sripathi Kodi
} V9fsReadDirState;
2084 c18e2f94 Sripathi Kodi
2085 c18e2f94 Sripathi Kodi
static void v9fs_readdir_post_seekdir(V9fsState *s, V9fsReadDirState *vs)
2086 c18e2f94 Sripathi Kodi
{
2087 c18e2f94 Sripathi Kodi
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
2088 c18e2f94 Sripathi Kodi
    vs->offset += vs->count;
2089 c18e2f94 Sripathi Kodi
    complete_pdu(s, vs->pdu, vs->offset);
2090 c18e2f94 Sripathi Kodi
    qemu_free(vs);
2091 c18e2f94 Sripathi Kodi
    return;
2092 c18e2f94 Sripathi Kodi
}
2093 c18e2f94 Sripathi Kodi
2094 c18e2f94 Sripathi Kodi
/* Size of each dirent on the wire: size of qid (13) + size of offset (8)
2095 c18e2f94 Sripathi Kodi
 * size of type (1) + size of name.size (2) + strlen(name.data)
2096 c18e2f94 Sripathi Kodi
 */
2097 c18e2f94 Sripathi Kodi
#define V9_READDIR_DATA_SZ (24 + strlen(vs->name.data))
2098 c18e2f94 Sripathi Kodi
2099 c18e2f94 Sripathi Kodi
static void v9fs_readdir_post_readdir(V9fsState *s, V9fsReadDirState *vs)
2100 c18e2f94 Sripathi Kodi
{
2101 c18e2f94 Sripathi Kodi
    int len;
2102 c18e2f94 Sripathi Kodi
    size_t size;
2103 c18e2f94 Sripathi Kodi
2104 c18e2f94 Sripathi Kodi
    if (vs->dent) {
2105 c18e2f94 Sripathi Kodi
        v9fs_string_init(&vs->name);
2106 c18e2f94 Sripathi Kodi
        v9fs_string_sprintf(&vs->name, "%s", vs->dent->d_name);
2107 c18e2f94 Sripathi Kodi
2108 c18e2f94 Sripathi Kodi
        if ((vs->count + V9_READDIR_DATA_SZ) > vs->max_count) {
2109 c18e2f94 Sripathi Kodi
            /* Ran out of buffer. Set dir back to old position and return */
2110 d62dbb51 Aneesh Kumar K.V
            v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->saved_dir_pos);
2111 c18e2f94 Sripathi Kodi
            v9fs_readdir_post_seekdir(s, vs);
2112 c18e2f94 Sripathi Kodi
            return;
2113 c18e2f94 Sripathi Kodi
        }
2114 c18e2f94 Sripathi Kodi
2115 c18e2f94 Sripathi Kodi
        /* Fill up just the path field of qid because the client uses
2116 c18e2f94 Sripathi Kodi
         * only that. To fill the entire qid structure we will have
2117 c18e2f94 Sripathi Kodi
         * to stat each dirent found, which is expensive
2118 c18e2f94 Sripathi Kodi
         */
2119 c18e2f94 Sripathi Kodi
        size = MIN(sizeof(vs->dent->d_ino), sizeof(vs->qid.path));
2120 c18e2f94 Sripathi Kodi
        memcpy(&vs->qid.path, &vs->dent->d_ino, size);
2121 c18e2f94 Sripathi Kodi
        /* Fill the other fields with dummy values */
2122 c18e2f94 Sripathi Kodi
        vs->qid.type = 0;
2123 c18e2f94 Sripathi Kodi
        vs->qid.version = 0;
2124 c18e2f94 Sripathi Kodi
2125 c18e2f94 Sripathi Kodi
        len = pdu_marshal(vs->pdu, vs->offset+4+vs->count, "Qqbs",
2126 c18e2f94 Sripathi Kodi
                              &vs->qid, vs->dent->d_off,
2127 c18e2f94 Sripathi Kodi
                              vs->dent->d_type, &vs->name);
2128 c18e2f94 Sripathi Kodi
        vs->count += len;
2129 c18e2f94 Sripathi Kodi
        v9fs_string_free(&vs->name);
2130 c18e2f94 Sripathi Kodi
        vs->saved_dir_pos = vs->dent->d_off;
2131 d62dbb51 Aneesh Kumar K.V
        vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
2132 c18e2f94 Sripathi Kodi
        v9fs_readdir_post_readdir(s, vs);
2133 c18e2f94 Sripathi Kodi
        return;
2134 c18e2f94 Sripathi Kodi
    }
2135 c18e2f94 Sripathi Kodi
2136 c18e2f94 Sripathi Kodi
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
2137 c18e2f94 Sripathi Kodi
    vs->offset += vs->count;
2138 c18e2f94 Sripathi Kodi
    complete_pdu(s, vs->pdu, vs->offset);
2139 c18e2f94 Sripathi Kodi
    qemu_free(vs);
2140 c18e2f94 Sripathi Kodi
    return;
2141 c18e2f94 Sripathi Kodi
}
2142 c18e2f94 Sripathi Kodi
2143 c18e2f94 Sripathi Kodi
static void v9fs_readdir_post_telldir(V9fsState *s, V9fsReadDirState *vs)
2144 c18e2f94 Sripathi Kodi
{
2145 d62dbb51 Aneesh Kumar K.V
    vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
2146 c18e2f94 Sripathi Kodi
    v9fs_readdir_post_readdir(s, vs);
2147 c18e2f94 Sripathi Kodi
    return;
2148 c18e2f94 Sripathi Kodi
}
2149 c18e2f94 Sripathi Kodi
2150 c18e2f94 Sripathi Kodi
static void v9fs_readdir_post_setdir(V9fsState *s, V9fsReadDirState *vs)
2151 c18e2f94 Sripathi Kodi
{
2152 d62dbb51 Aneesh Kumar K.V
    vs->saved_dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
2153 c18e2f94 Sripathi Kodi
    v9fs_readdir_post_telldir(s, vs);
2154 c18e2f94 Sripathi Kodi
    return;
2155 c18e2f94 Sripathi Kodi
}
2156 c18e2f94 Sripathi Kodi
2157 c18e2f94 Sripathi Kodi
static void v9fs_readdir(V9fsState *s, V9fsPDU *pdu)
2158 c18e2f94 Sripathi Kodi
{
2159 c18e2f94 Sripathi Kodi
    int32_t fid;
2160 c18e2f94 Sripathi Kodi
    V9fsReadDirState *vs;
2161 c18e2f94 Sripathi Kodi
    ssize_t err = 0;
2162 c18e2f94 Sripathi Kodi
    size_t offset = 7;
2163 c18e2f94 Sripathi Kodi
2164 c18e2f94 Sripathi Kodi
    vs = qemu_malloc(sizeof(*vs));
2165 c18e2f94 Sripathi Kodi
    vs->pdu = pdu;
2166 c18e2f94 Sripathi Kodi
    vs->offset = 7;
2167 c18e2f94 Sripathi Kodi
    vs->count = 0;
2168 c18e2f94 Sripathi Kodi
2169 c18e2f94 Sripathi Kodi
    pdu_unmarshal(vs->pdu, offset, "dqd", &fid, &vs->initial_offset,
2170 c18e2f94 Sripathi Kodi
                                                        &vs->max_count);
2171 c18e2f94 Sripathi Kodi
2172 c18e2f94 Sripathi Kodi
    vs->fidp = lookup_fid(s, fid);
2173 d62dbb51 Aneesh Kumar K.V
    if (vs->fidp == NULL || !(vs->fidp->fs.dir)) {
2174 c18e2f94 Sripathi Kodi
        err = -EINVAL;
2175 c18e2f94 Sripathi Kodi
        goto out;
2176 c18e2f94 Sripathi Kodi
    }
2177 c18e2f94 Sripathi Kodi
2178 c18e2f94 Sripathi Kodi
    if (vs->initial_offset == 0) {
2179 d62dbb51 Aneesh Kumar K.V
        v9fs_do_rewinddir(s, vs->fidp->fs.dir);
2180 c18e2f94 Sripathi Kodi
    } else {
2181 d62dbb51 Aneesh Kumar K.V
        v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->initial_offset);
2182 c18e2f94 Sripathi Kodi
    }
2183 c18e2f94 Sripathi Kodi
2184 c18e2f94 Sripathi Kodi
    v9fs_readdir_post_setdir(s, vs);
2185 c18e2f94 Sripathi Kodi
    return;
2186 c18e2f94 Sripathi Kodi
2187 c18e2f94 Sripathi Kodi
out:
2188 c18e2f94 Sripathi Kodi
    complete_pdu(s, pdu, err);
2189 c18e2f94 Sripathi Kodi
    qemu_free(vs);
2190 c18e2f94 Sripathi Kodi
    return;
2191 c18e2f94 Sripathi Kodi
}
2192 c18e2f94 Sripathi Kodi
2193 8449360c Anthony Liguori
static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs,
2194 8449360c Anthony Liguori
                                   ssize_t err)
2195 8449360c Anthony Liguori
{
2196 8449360c Anthony Liguori
    if (err  < 0) {
2197 8449360c Anthony Liguori
        /* IO error return the error */
2198 8449360c Anthony Liguori
        err = -errno;
2199 8449360c Anthony Liguori
        goto out;
2200 8449360c Anthony Liguori
    }
2201 8449360c Anthony Liguori
    vs->total += vs->len;
2202 8449360c Anthony Liguori
    vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
2203 8449360c Anthony Liguori
    if (vs->total < vs->count && vs->len > 0) {
2204 8449360c Anthony Liguori
        do {
2205 8449360c Anthony Liguori
            if (0) {
2206 8449360c Anthony Liguori
                print_sg(vs->sg, vs->cnt);
2207 8449360c Anthony Liguori
            }
2208 d62dbb51 Aneesh Kumar K.V
            vs->len =  v9fs_do_writev(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
2209 8449360c Anthony Liguori
        } while (vs->len == -1 && errno == EINTR);
2210 8449360c Anthony Liguori
        if (vs->len == -1) {
2211 8449360c Anthony Liguori
            err  = -errno;
2212 8449360c Anthony Liguori
        }
2213 8449360c Anthony Liguori
        v9fs_write_post_writev(s, vs, err);
2214 8449360c Anthony Liguori
        return;
2215 8449360c Anthony Liguori
    }
2216 8449360c Anthony Liguori
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
2217 8449360c Anthony Liguori
2218 8449360c Anthony Liguori
    err = vs->offset;
2219 8449360c Anthony Liguori
out:
2220 8449360c Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2221 8449360c Anthony Liguori
    qemu_free(vs);
2222 8449360c Anthony Liguori
}
2223 8449360c Anthony Liguori
2224 8449360c Anthony Liguori
static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err)
2225 8449360c Anthony Liguori
{
2226 8449360c Anthony Liguori
    if (err == -1) {
2227 8449360c Anthony Liguori
        err = -errno;
2228 8449360c Anthony Liguori
        goto out;
2229 8449360c Anthony Liguori
    }
2230 8449360c Anthony Liguori
    vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
2231 8449360c Anthony Liguori
2232 8449360c Anthony Liguori
    if (vs->total < vs->count) {
2233 8449360c Anthony Liguori
        do {
2234 8449360c Anthony Liguori
            if (0) {
2235 8449360c Anthony Liguori
                print_sg(vs->sg, vs->cnt);
2236 8449360c Anthony Liguori
            }
2237 d62dbb51 Aneesh Kumar K.V
            vs->len = v9fs_do_writev(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
2238 8449360c Anthony Liguori
        } while (vs->len == -1 && errno == EINTR);
2239 8449360c Anthony Liguori
        if (vs->len == -1) {
2240 8449360c Anthony Liguori
            err  = -errno;
2241 8449360c Anthony Liguori
        }
2242 8449360c Anthony Liguori
        v9fs_write_post_writev(s, vs, err);
2243 8449360c Anthony Liguori
        return;
2244 8449360c Anthony Liguori
    }
2245 8449360c Anthony Liguori
2246 8449360c Anthony Liguori
out:
2247 8449360c Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2248 8449360c Anthony Liguori
    qemu_free(vs);
2249 8449360c Anthony Liguori
}
2250 8449360c Anthony Liguori
2251 10b468bd Aneesh Kumar K.V
static void v9fs_xattr_write(V9fsState *s, V9fsWriteState *vs)
2252 10b468bd Aneesh Kumar K.V
{
2253 10b468bd Aneesh Kumar K.V
    int i, to_copy;
2254 10b468bd Aneesh Kumar K.V
    ssize_t err = 0;
2255 10b468bd Aneesh Kumar K.V
    int write_count;
2256 10b468bd Aneesh Kumar K.V
    int64_t xattr_len;
2257 10b468bd Aneesh Kumar K.V
2258 10b468bd Aneesh Kumar K.V
    xattr_len = vs->fidp->fs.xattr.len;
2259 10b468bd Aneesh Kumar K.V
    write_count = xattr_len - vs->off;
2260 10b468bd Aneesh Kumar K.V
    if (write_count > vs->count) {
2261 10b468bd Aneesh Kumar K.V
        write_count = vs->count;
2262 10b468bd Aneesh Kumar K.V
    } else if (write_count < 0) {
2263 10b468bd Aneesh Kumar K.V
        /*
2264 10b468bd Aneesh Kumar K.V
         * write beyond XATTR value len specified in
2265 10b468bd Aneesh Kumar K.V
         * xattrcreate
2266 10b468bd Aneesh Kumar K.V
         */
2267 10b468bd Aneesh Kumar K.V
        err = -ENOSPC;
2268 10b468bd Aneesh Kumar K.V
        goto out;
2269 10b468bd Aneesh Kumar K.V
    }
2270 10b468bd Aneesh Kumar K.V
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", write_count);
2271 10b468bd Aneesh Kumar K.V
    err = vs->offset;
2272 10b468bd Aneesh Kumar K.V
    vs->fidp->fs.xattr.copied_len += write_count;
2273 10b468bd Aneesh Kumar K.V
    /*
2274 10b468bd Aneesh Kumar K.V
     * Now copy the content from sg list
2275 10b468bd Aneesh Kumar K.V
     */
2276 10b468bd Aneesh Kumar K.V
    for (i = 0; i < vs->cnt; i++) {
2277 10b468bd Aneesh Kumar K.V
        if (write_count > vs->sg[i].iov_len) {
2278 10b468bd Aneesh Kumar K.V
            to_copy = vs->sg[i].iov_len;
2279 10b468bd Aneesh Kumar K.V
        } else {
2280 10b468bd Aneesh Kumar K.V
            to_copy = write_count;
2281 10b468bd Aneesh Kumar K.V
        }
2282 10b468bd Aneesh Kumar K.V
        memcpy((char *)vs->fidp->fs.xattr.value + vs->off,
2283 10b468bd Aneesh Kumar K.V
               vs->sg[i].iov_base, to_copy);
2284 10b468bd Aneesh Kumar K.V
        /* updating vs->off since we are not using below */
2285 10b468bd Aneesh Kumar K.V
        vs->off += to_copy;
2286 10b468bd Aneesh Kumar K.V
        write_count -= to_copy;
2287 10b468bd Aneesh Kumar K.V
    }
2288 10b468bd Aneesh Kumar K.V
out:
2289 10b468bd Aneesh Kumar K.V
    complete_pdu(s, vs->pdu, err);
2290 10b468bd Aneesh Kumar K.V
    qemu_free(vs);
2291 10b468bd Aneesh Kumar K.V
}
2292 10b468bd Aneesh Kumar K.V
2293 9f107513 Anthony Liguori
static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
2294 9f107513 Anthony Liguori
{
2295 8449360c Anthony Liguori
    int32_t fid;
2296 8449360c Anthony Liguori
    V9fsWriteState *vs;
2297 8449360c Anthony Liguori
    ssize_t err;
2298 8449360c Anthony Liguori
2299 8449360c Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
2300 8449360c Anthony Liguori
2301 8449360c Anthony Liguori
    vs->pdu = pdu;
2302 8449360c Anthony Liguori
    vs->offset = 7;
2303 8449360c Anthony Liguori
    vs->sg = vs->iov;
2304 8449360c Anthony Liguori
    vs->total = 0;
2305 8449360c Anthony Liguori
    vs->len = 0;
2306 8449360c Anthony Liguori
2307 8449360c Anthony Liguori
    pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
2308 10b468bd Aneesh Kumar K.V
                  vs->sg, &vs->cnt);
2309 8449360c Anthony Liguori
2310 8449360c Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
2311 8449360c Anthony Liguori
    if (vs->fidp == NULL) {
2312 8449360c Anthony Liguori
        err = -EINVAL;
2313 8449360c Anthony Liguori
        goto out;
2314 9f107513 Anthony Liguori
    }
2315 8449360c Anthony Liguori
2316 10b468bd Aneesh Kumar K.V
    if (vs->fidp->fid_type == P9_FID_FILE) {
2317 10b468bd Aneesh Kumar K.V
        if (vs->fidp->fs.fd == -1) {
2318 10b468bd Aneesh Kumar K.V
            err = -EINVAL;
2319 10b468bd Aneesh Kumar K.V
            goto out;
2320 10b468bd Aneesh Kumar K.V
        }
2321 10b468bd Aneesh Kumar K.V
    } else if (vs->fidp->fid_type == P9_FID_XATTR) {
2322 10b468bd Aneesh Kumar K.V
        /*
2323 10b468bd Aneesh Kumar K.V
         * setxattr operation
2324 10b468bd Aneesh Kumar K.V
         */
2325 10b468bd Aneesh Kumar K.V
        v9fs_xattr_write(s, vs);
2326 10b468bd Aneesh Kumar K.V
        return;
2327 10b468bd Aneesh Kumar K.V
    } else {
2328 8449360c Anthony Liguori
        err = -EINVAL;
2329 8449360c Anthony Liguori
        goto out;
2330 8449360c Anthony Liguori
    }
2331 d62dbb51 Aneesh Kumar K.V
    err = v9fs_do_lseek(s, vs->fidp->fs.fd, vs->off, SEEK_SET);
2332 8449360c Anthony Liguori
2333 8449360c Anthony Liguori
    v9fs_write_post_lseek(s, vs, err);
2334 8449360c Anthony Liguori
    return;
2335 8449360c Anthony Liguori
2336 8449360c Anthony Liguori
out:
2337 8449360c Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2338 8449360c Anthony Liguori
    qemu_free(vs);
2339 9f107513 Anthony Liguori
}
2340 9f107513 Anthony Liguori
2341 5e94c103 M. Mohan Kumar
static void v9fs_create_post_getiounit(V9fsState *s, V9fsCreateState *vs)
2342 c494dd6f Anthony Liguori
{
2343 5e94c103 M. Mohan Kumar
    int err;
2344 5e94c103 M. Mohan Kumar
    v9fs_string_copy(&vs->fidp->path, &vs->fullname);
2345 5e94c103 M. Mohan Kumar
    stat_to_qid(&vs->stbuf, &vs->qid);
2346 c494dd6f Anthony Liguori
2347 5e94c103 M. Mohan Kumar
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
2348 5e94c103 M. Mohan Kumar
    err = vs->offset;
2349 c494dd6f Anthony Liguori
2350 5e94c103 M. Mohan Kumar
    complete_pdu(s, vs->pdu, err);
2351 5e94c103 M. Mohan Kumar
    v9fs_string_free(&vs->name);
2352 5e94c103 M. Mohan Kumar
    v9fs_string_free(&vs->extension);
2353 5e94c103 M. Mohan Kumar
    v9fs_string_free(&vs->fullname);
2354 5e94c103 M. Mohan Kumar
    qemu_free(vs);
2355 5e94c103 M. Mohan Kumar
}
2356 5e94c103 M. Mohan Kumar
2357 5e94c103 M. Mohan Kumar
static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
2358 5e94c103 M. Mohan Kumar
{
2359 5e94c103 M. Mohan Kumar
    if (err == 0) {
2360 5e94c103 M. Mohan Kumar
        vs->iounit = get_iounit(s, &vs->fidp->path);
2361 5e94c103 M. Mohan Kumar
        v9fs_create_post_getiounit(s, vs);
2362 5e94c103 M. Mohan Kumar
        return;
2363 c494dd6f Anthony Liguori
    }
2364 c494dd6f Anthony Liguori
2365 c494dd6f Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2366 c494dd6f Anthony Liguori
    v9fs_string_free(&vs->name);
2367 c494dd6f Anthony Liguori
    v9fs_string_free(&vs->extension);
2368 c494dd6f Anthony Liguori
    v9fs_string_free(&vs->fullname);
2369 c494dd6f Anthony Liguori
    qemu_free(vs);
2370 c494dd6f Anthony Liguori
}
2371 c494dd6f Anthony Liguori
2372 c494dd6f Anthony Liguori
static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
2373 c494dd6f Anthony Liguori
{
2374 c494dd6f Anthony Liguori
    if (err) {
2375 c494dd6f Anthony Liguori
        err = -errno;
2376 c494dd6f Anthony Liguori
    }
2377 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
2378 c494dd6f Anthony Liguori
}
2379 c494dd6f Anthony Liguori
2380 c494dd6f Anthony Liguori
static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
2381 c494dd6f Anthony Liguori
                                                                    int err)
2382 c494dd6f Anthony Liguori
{
2383 d62dbb51 Aneesh Kumar K.V
    if (!vs->fidp->fs.dir) {
2384 c494dd6f Anthony Liguori
        err = -errno;
2385 c494dd6f Anthony Liguori
    }
2386 d62dbb51 Aneesh Kumar K.V
    vs->fidp->fid_type = P9_FID_DIR;
2387 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
2388 c494dd6f Anthony Liguori
}
2389 c494dd6f Anthony Liguori
2390 c494dd6f Anthony Liguori
static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
2391 c494dd6f Anthony Liguori
                                                                    int err)
2392 c494dd6f Anthony Liguori
{
2393 c494dd6f Anthony Liguori
    if (err) {
2394 c494dd6f Anthony Liguori
        err = -errno;
2395 c494dd6f Anthony Liguori
        goto out;
2396 c494dd6f Anthony Liguori
    }
2397 c494dd6f Anthony Liguori
2398 d62dbb51 Aneesh Kumar K.V
    vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fullname);
2399 c494dd6f Anthony Liguori
    v9fs_create_post_opendir(s, vs, err);
2400 c494dd6f Anthony Liguori
    return;
2401 c494dd6f Anthony Liguori
2402 c494dd6f Anthony Liguori
out:
2403 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
2404 c494dd6f Anthony Liguori
}
2405 c494dd6f Anthony Liguori
2406 c494dd6f Anthony Liguori
static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
2407 c494dd6f Anthony Liguori
{
2408 c494dd6f Anthony Liguori
    if (err) {
2409 c494dd6f Anthony Liguori
        err = -errno;
2410 c494dd6f Anthony Liguori
        goto out;
2411 c494dd6f Anthony Liguori
    }
2412 c494dd6f Anthony Liguori
2413 c494dd6f Anthony Liguori
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2414 c494dd6f Anthony Liguori
    v9fs_create_post_dir_lstat(s, vs, err);
2415 c494dd6f Anthony Liguori
    return;
2416 c494dd6f Anthony Liguori
2417 c494dd6f Anthony Liguori
out:
2418 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
2419 c494dd6f Anthony Liguori
}
2420 c494dd6f Anthony Liguori
2421 c494dd6f Anthony Liguori
static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
2422 c494dd6f Anthony Liguori
{
2423 c494dd6f Anthony Liguori
    if (err) {
2424 d62dbb51 Aneesh Kumar K.V
        vs->fidp->fid_type = P9_FID_NONE;
2425 d62dbb51 Aneesh Kumar K.V
        close(vs->fidp->fs.fd);
2426 c494dd6f Anthony Liguori
        err = -errno;
2427 c494dd6f Anthony Liguori
    }
2428 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
2429 c494dd6f Anthony Liguori
    return;
2430 c494dd6f Anthony Liguori
}
2431 c494dd6f Anthony Liguori
2432 c494dd6f Anthony Liguori
static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
2433 c494dd6f Anthony Liguori
{
2434 d62dbb51 Aneesh Kumar K.V
    if (vs->fidp->fs.fd == -1) {
2435 c494dd6f Anthony Liguori
        err = -errno;
2436 c494dd6f Anthony Liguori
        goto out;
2437 c494dd6f Anthony Liguori
    }
2438 d62dbb51 Aneesh Kumar K.V
    vs->fidp->fid_type = P9_FID_FILE;
2439 d62dbb51 Aneesh Kumar K.V
    err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
2440 c494dd6f Anthony Liguori
    v9fs_create_post_fstat(s, vs, err);
2441 c494dd6f Anthony Liguori
2442 c494dd6f Anthony Liguori
    return;
2443 c494dd6f Anthony Liguori
2444 c494dd6f Anthony Liguori
out:
2445 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
2446 c494dd6f Anthony Liguori
2447 c494dd6f Anthony Liguori
}
2448 c494dd6f Anthony Liguori
2449 c494dd6f Anthony Liguori
static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
2450 c494dd6f Anthony Liguori
{
2451 c494dd6f Anthony Liguori
2452 c494dd6f Anthony Liguori
    if (err == 0 || errno != ENOENT) {
2453 c494dd6f Anthony Liguori
        err = -errno;
2454 c494dd6f Anthony Liguori
        goto out;
2455 c494dd6f Anthony Liguori
    }
2456 c494dd6f Anthony Liguori
2457 c494dd6f Anthony Liguori
    if (vs->perm & P9_STAT_MODE_DIR) {
2458 b67592ea M. Mohan Kumar
        err = v9fs_do_mkdir(s, vs->fullname.data, vs->perm & 0777,
2459 b67592ea M. Mohan Kumar
                vs->fidp->uid, -1);
2460 c494dd6f Anthony Liguori
        v9fs_create_post_mkdir(s, vs, err);
2461 c494dd6f Anthony Liguori
    } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
2462 08c60fc9 Venkateswararao Jujjuri (JV)
        err = v9fs_do_symlink(s, vs->fidp, vs->extension.data,
2463 08c60fc9 Venkateswararao Jujjuri (JV)
                vs->fullname.data, -1);
2464 c494dd6f Anthony Liguori
        v9fs_create_post_perms(s, vs, err);
2465 c494dd6f Anthony Liguori
    } else if (vs->perm & P9_STAT_MODE_LINK) {
2466 c494dd6f Anthony Liguori
        int32_t nfid = atoi(vs->extension.data);
2467 c494dd6f Anthony Liguori
        V9fsFidState *nfidp = lookup_fid(s, nfid);
2468 c494dd6f Anthony Liguori
        if (nfidp == NULL) {
2469 c494dd6f Anthony Liguori
            err = -errno;
2470 c494dd6f Anthony Liguori
            v9fs_post_create(s, vs, err);
2471 c494dd6f Anthony Liguori
        }
2472 c494dd6f Anthony Liguori
        err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
2473 c494dd6f Anthony Liguori
        v9fs_create_post_perms(s, vs, err);
2474 c494dd6f Anthony Liguori
    } else if (vs->perm & P9_STAT_MODE_DEVICE) {
2475 c494dd6f Anthony Liguori
        char ctype;
2476 c494dd6f Anthony Liguori
        uint32_t major, minor;
2477 c494dd6f Anthony Liguori
        mode_t nmode = 0;
2478 c494dd6f Anthony Liguori
2479 c494dd6f Anthony Liguori
        if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
2480 c494dd6f Anthony Liguori
                                        &minor) != 3) {
2481 c494dd6f Anthony Liguori
            err = -errno;
2482 c494dd6f Anthony Liguori
            v9fs_post_create(s, vs, err);
2483 c494dd6f Anthony Liguori
        }
2484 c494dd6f Anthony Liguori
2485 c494dd6f Anthony Liguori
        switch (ctype) {
2486 c494dd6f Anthony Liguori
        case 'c':
2487 c494dd6f Anthony Liguori
            nmode = S_IFCHR;
2488 c494dd6f Anthony Liguori
            break;
2489 c494dd6f Anthony Liguori
        case 'b':
2490 c494dd6f Anthony Liguori
            nmode = S_IFBLK;
2491 c494dd6f Anthony Liguori
            break;
2492 c494dd6f Anthony Liguori
        default:
2493 c494dd6f Anthony Liguori
            err = -EIO;
2494 c494dd6f Anthony Liguori
            v9fs_post_create(s, vs, err);
2495 c494dd6f Anthony Liguori
        }
2496 c494dd6f Anthony Liguori
2497 c494dd6f Anthony Liguori
        nmode |= vs->perm & 0777;
2498 5268cecc M. Mohan Kumar
        err = v9fs_do_mknod(s, vs->fullname.data, nmode,
2499 5268cecc M. Mohan Kumar
                makedev(major, minor), vs->fidp->uid, -1);
2500 c494dd6f Anthony Liguori
        v9fs_create_post_perms(s, vs, err);
2501 c494dd6f Anthony Liguori
    } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
2502 5268cecc M. Mohan Kumar
        err = v9fs_do_mknod(s, vs->fullname.data, S_IFIFO | (vs->perm & 0777),
2503 5268cecc M. Mohan Kumar
                0, vs->fidp->uid, -1);
2504 c494dd6f Anthony Liguori
        v9fs_post_create(s, vs, err);
2505 c494dd6f Anthony Liguori
    } else if (vs->perm & P9_STAT_MODE_SOCKET) {
2506 5268cecc M. Mohan Kumar
        err = v9fs_do_mknod(s, vs->fullname.data, S_IFSOCK | (vs->perm & 0777),
2507 5268cecc M. Mohan Kumar
                0, vs->fidp->uid, -1);
2508 63729c36 Venkateswararao Jujjuri (JV)
        v9fs_post_create(s, vs, err);
2509 c494dd6f Anthony Liguori
    } else {
2510 d62dbb51 Aneesh Kumar K.V
        vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
2511 c1568af5 Venkateswararao Jujjuri (JV)
                -1, omode_to_uflags(vs->mode)|O_CREAT, vs->perm);
2512 c1568af5 Venkateswararao Jujjuri (JV)
2513 c494dd6f Anthony Liguori
        v9fs_create_post_open2(s, vs, err);
2514 c494dd6f Anthony Liguori
    }
2515 c494dd6f Anthony Liguori
2516 c494dd6f Anthony Liguori
    return;
2517 c494dd6f Anthony Liguori
2518 c494dd6f Anthony Liguori
out:
2519 c494dd6f Anthony Liguori
    v9fs_post_create(s, vs, err);
2520 c494dd6f Anthony Liguori
}
2521 c494dd6f Anthony Liguori
2522 9f107513 Anthony Liguori
static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
2523 9f107513 Anthony Liguori
{
2524 c494dd6f Anthony Liguori
    int32_t fid;
2525 c494dd6f Anthony Liguori
    V9fsCreateState *vs;
2526 c494dd6f Anthony Liguori
    int err = 0;
2527 c494dd6f Anthony Liguori
2528 c494dd6f Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
2529 c494dd6f Anthony Liguori
    vs->pdu = pdu;
2530 c494dd6f Anthony Liguori
    vs->offset = 7;
2531 c494dd6f Anthony Liguori
2532 c494dd6f Anthony Liguori
    v9fs_string_init(&vs->fullname);
2533 c494dd6f Anthony Liguori
2534 c494dd6f Anthony Liguori
    pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
2535 c494dd6f Anthony Liguori
                                &vs->perm, &vs->mode, &vs->extension);
2536 c494dd6f Anthony Liguori
2537 c494dd6f Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
2538 c494dd6f Anthony Liguori
    if (vs->fidp == NULL) {
2539 c494dd6f Anthony Liguori
        err = -EINVAL;
2540 c494dd6f Anthony Liguori
        goto out;
2541 9f107513 Anthony Liguori
    }
2542 c494dd6f Anthony Liguori
2543 c494dd6f Anthony Liguori
    v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
2544 c494dd6f Anthony Liguori
                                                        vs->name.data);
2545 c494dd6f Anthony Liguori
2546 c494dd6f Anthony Liguori
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2547 c494dd6f Anthony Liguori
    v9fs_create_post_lstat(s, vs, err);
2548 c494dd6f Anthony Liguori
    return;
2549 c494dd6f Anthony Liguori
2550 c494dd6f Anthony Liguori
out:
2551 c494dd6f Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2552 c494dd6f Anthony Liguori
    v9fs_string_free(&vs->name);
2553 c494dd6f Anthony Liguori
    v9fs_string_free(&vs->extension);
2554 c494dd6f Anthony Liguori
    qemu_free(vs);
2555 9f107513 Anthony Liguori
}
2556 9f107513 Anthony Liguori
2557 08c60fc9 Venkateswararao Jujjuri (JV)
static void v9fs_post_symlink(V9fsState *s, V9fsSymlinkState *vs, int err)
2558 08c60fc9 Venkateswararao Jujjuri (JV)
{
2559 08c60fc9 Venkateswararao Jujjuri (JV)
    if (err == 0) {
2560 08c60fc9 Venkateswararao Jujjuri (JV)
        stat_to_qid(&vs->stbuf, &vs->qid);
2561 08c60fc9 Venkateswararao Jujjuri (JV)
        vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
2562 08c60fc9 Venkateswararao Jujjuri (JV)
        err = vs->offset;
2563 08c60fc9 Venkateswararao Jujjuri (JV)
    } else {
2564 08c60fc9 Venkateswararao Jujjuri (JV)
        err = -errno;
2565 08c60fc9 Venkateswararao Jujjuri (JV)
    }
2566 08c60fc9 Venkateswararao Jujjuri (JV)
    complete_pdu(s, vs->pdu, err);
2567 08c60fc9 Venkateswararao Jujjuri (JV)
    v9fs_string_free(&vs->name);
2568 08c60fc9 Venkateswararao Jujjuri (JV)
    v9fs_string_free(&vs->symname);
2569 08c60fc9 Venkateswararao Jujjuri (JV)
    v9fs_string_free(&vs->fullname);
2570 08c60fc9 Venkateswararao Jujjuri (JV)
    qemu_free(vs);
2571 08c60fc9 Venkateswararao Jujjuri (JV)
}
2572 08c60fc9 Venkateswararao Jujjuri (JV)
2573 08c60fc9 Venkateswararao Jujjuri (JV)
static void v9fs_symlink_post_do_symlink(V9fsState *s, V9fsSymlinkState *vs,
2574 08c60fc9 Venkateswararao Jujjuri (JV)
        int err)
2575 08c60fc9 Venkateswararao Jujjuri (JV)
{
2576 08c60fc9 Venkateswararao Jujjuri (JV)
    if (err) {
2577 08c60fc9 Venkateswararao Jujjuri (JV)
        goto out;
2578 08c60fc9 Venkateswararao Jujjuri (JV)
    }
2579 08c60fc9 Venkateswararao Jujjuri (JV)
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2580 08c60fc9 Venkateswararao Jujjuri (JV)
out:
2581 08c60fc9 Venkateswararao Jujjuri (JV)
    v9fs_post_symlink(s, vs, err);
2582 08c60fc9 Venkateswararao Jujjuri (JV)
}
2583 08c60fc9 Venkateswararao Jujjuri (JV)
2584 08c60fc9 Venkateswararao Jujjuri (JV)
static void v9fs_symlink(V9fsState *s, V9fsPDU *pdu)
2585 08c60fc9 Venkateswararao Jujjuri (JV)
{
2586 08c60fc9 Venkateswararao Jujjuri (JV)
    int32_t dfid;
2587 08c60fc9 Venkateswararao Jujjuri (JV)
    V9fsSymlinkState *vs;
2588 08c60fc9 Venkateswararao Jujjuri (JV)
    int err = 0;
2589 08c60fc9 Venkateswararao Jujjuri (JV)
    gid_t gid;
2590 08c60fc9 Venkateswararao Jujjuri (JV)
2591 08c60fc9 Venkateswararao Jujjuri (JV)
    vs = qemu_malloc(sizeof(*vs));
2592 08c60fc9 Venkateswararao Jujjuri (JV)
    vs->pdu = pdu;
2593 08c60fc9 Venkateswararao Jujjuri (JV)
    vs->offset = 7;
2594 08c60fc9 Venkateswararao Jujjuri (JV)
2595 08c60fc9 Venkateswararao Jujjuri (JV)
    v9fs_string_init(&vs->fullname);
2596 08c60fc9 Venkateswararao Jujjuri (JV)
2597 08c60fc9 Venkateswararao Jujjuri (JV)
    pdu_unmarshal(vs->pdu, vs->offset, "dssd", &dfid, &vs->name,
2598 08c60fc9 Venkateswararao Jujjuri (JV)
            &vs->symname, &gid);
2599 08c60fc9 Venkateswararao Jujjuri (JV)
2600 08c60fc9 Venkateswararao Jujjuri (JV)
    vs->dfidp = lookup_fid(s, dfid);
2601 08c60fc9 Venkateswararao Jujjuri (JV)
    if (vs->dfidp == NULL) {
2602 08c60fc9 Venkateswararao Jujjuri (JV)
        err = -EINVAL;
2603 08c60fc9 Venkateswararao Jujjuri (JV)
        goto out;
2604 08c60fc9 Venkateswararao Jujjuri (JV)
    }
2605 08c60fc9 Venkateswararao Jujjuri (JV)
2606 08c60fc9 Venkateswararao Jujjuri (JV)
    v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->dfidp->path.data,
2607 08c60fc9 Venkateswararao Jujjuri (JV)
            vs->name.data);
2608 08c60fc9 Venkateswararao Jujjuri (JV)
    err = v9fs_do_symlink(s, vs->dfidp, vs->symname.data,
2609 08c60fc9 Venkateswararao Jujjuri (JV)
            vs->fullname.data, gid);
2610 08c60fc9 Venkateswararao Jujjuri (JV)
    v9fs_symlink_post_do_symlink(s, vs, err);
2611 08c60fc9 Venkateswararao Jujjuri (JV)
    return;
2612 08c60fc9 Venkateswararao Jujjuri (JV)
2613 08c60fc9 Venkateswararao Jujjuri (JV)
out:
2614 08c60fc9 Venkateswararao Jujjuri (JV)
    complete_pdu(s, vs->pdu, err);
2615 08c60fc9 Venkateswararao Jujjuri (JV)
    v9fs_string_free(&vs->name);
2616 08c60fc9 Venkateswararao Jujjuri (JV)
    v9fs_string_free(&vs->symname);
2617 08c60fc9 Venkateswararao Jujjuri (JV)
    qemu_free(vs);
2618 08c60fc9 Venkateswararao Jujjuri (JV)
}
2619 08c60fc9 Venkateswararao Jujjuri (JV)
2620 9f107513 Anthony Liguori
static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
2621 9f107513 Anthony Liguori
{
2622 9c5e9d89 Anthony Liguori
    /* A nop call with no return */
2623 9c5e9d89 Anthony Liguori
    complete_pdu(s, pdu, 7);
2624 9f107513 Anthony Liguori
}
2625 9f107513 Anthony Liguori
2626 b2c224be Venkateswararao Jujjuri (JV)
static void v9fs_link(V9fsState *s, V9fsPDU *pdu)
2627 b2c224be Venkateswararao Jujjuri (JV)
{
2628 b2c224be Venkateswararao Jujjuri (JV)
    int32_t dfid, oldfid;
2629 b2c224be Venkateswararao Jujjuri (JV)
    V9fsFidState *dfidp, *oldfidp;
2630 b2c224be Venkateswararao Jujjuri (JV)
    V9fsString name, fullname;
2631 b2c224be Venkateswararao Jujjuri (JV)
    size_t offset = 7;
2632 b2c224be Venkateswararao Jujjuri (JV)
    int err = 0;
2633 b2c224be Venkateswararao Jujjuri (JV)
2634 b2c224be Venkateswararao Jujjuri (JV)
    v9fs_string_init(&fullname);
2635 b2c224be Venkateswararao Jujjuri (JV)
2636 b2c224be Venkateswararao Jujjuri (JV)
    pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2637 b2c224be Venkateswararao Jujjuri (JV)
2638 b2c224be Venkateswararao Jujjuri (JV)
    dfidp = lookup_fid(s, dfid);
2639 b2c224be Venkateswararao Jujjuri (JV)
    if (dfidp == NULL) {
2640 b2c224be Venkateswararao Jujjuri (JV)
        err = -errno;
2641 b2c224be Venkateswararao Jujjuri (JV)
        goto out;
2642 b2c224be Venkateswararao Jujjuri (JV)
    }
2643 b2c224be Venkateswararao Jujjuri (JV)
2644 b2c224be Venkateswararao Jujjuri (JV)
    oldfidp = lookup_fid(s, oldfid);
2645 b2c224be Venkateswararao Jujjuri (JV)
    if (oldfidp == NULL) {
2646 b2c224be Venkateswararao Jujjuri (JV)
        err = -errno;
2647 b2c224be Venkateswararao Jujjuri (JV)
        goto out;
2648 b2c224be Venkateswararao Jujjuri (JV)
    }
2649 b2c224be Venkateswararao Jujjuri (JV)
2650 b2c224be Venkateswararao Jujjuri (JV)
    v9fs_string_sprintf(&fullname, "%s/%s", dfidp->path.data, name.data);
2651 b2c224be Venkateswararao Jujjuri (JV)
    err = offset;
2652 b2c224be Venkateswararao Jujjuri (JV)
    err = v9fs_do_link(s, &oldfidp->path, &fullname);
2653 b2c224be Venkateswararao Jujjuri (JV)
    if (err) {
2654 b2c224be Venkateswararao Jujjuri (JV)
        err = -errno;
2655 b2c224be Venkateswararao Jujjuri (JV)
    }
2656 b2c224be Venkateswararao Jujjuri (JV)
    v9fs_string_free(&fullname);
2657 b2c224be Venkateswararao Jujjuri (JV)
2658 b2c224be Venkateswararao Jujjuri (JV)
out:
2659 b2c224be Venkateswararao Jujjuri (JV)
    v9fs_string_free(&name);
2660 b2c224be Venkateswararao Jujjuri (JV)
    complete_pdu(s, pdu, err);
2661 b2c224be Venkateswararao Jujjuri (JV)
}
2662 b2c224be Venkateswararao Jujjuri (JV)
2663 5bae1900 Anthony Liguori
static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
2664 5bae1900 Anthony Liguori
                                                                int err)
2665 5bae1900 Anthony Liguori
{
2666 5bae1900 Anthony Liguori
    if (err < 0) {
2667 926487b7 Sripathi Kodi
        err = -errno;
2668 926487b7 Sripathi Kodi
    } else {
2669 926487b7 Sripathi Kodi
        err = vs->offset;
2670 5bae1900 Anthony Liguori
    }
2671 5bae1900 Anthony Liguori
2672 926487b7 Sripathi Kodi
    /* For TREMOVE we need to clunk the fid even on failed remove */
2673 926487b7 Sripathi Kodi
    free_fid(s, vs->fidp->fid);
2674 926487b7 Sripathi Kodi
2675 5bae1900 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2676 5bae1900 Anthony Liguori
    qemu_free(vs);
2677 5bae1900 Anthony Liguori
}
2678 5bae1900 Anthony Liguori
2679 9f107513 Anthony Liguori
static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
2680 9f107513 Anthony Liguori
{
2681 5bae1900 Anthony Liguori
    int32_t fid;
2682 5bae1900 Anthony Liguori
    V9fsRemoveState *vs;
2683 5bae1900 Anthony Liguori
    int err = 0;
2684 5bae1900 Anthony Liguori
2685 5bae1900 Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
2686 5bae1900 Anthony Liguori
    vs->pdu = pdu;
2687 5bae1900 Anthony Liguori
    vs->offset = 7;
2688 5bae1900 Anthony Liguori
2689 5bae1900 Anthony Liguori
    pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
2690 5bae1900 Anthony Liguori
2691 5bae1900 Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
2692 5bae1900 Anthony Liguori
    if (vs->fidp == NULL) {
2693 5bae1900 Anthony Liguori
        err = -EINVAL;
2694 5bae1900 Anthony Liguori
        goto out;
2695 9f107513 Anthony Liguori
    }
2696 5bae1900 Anthony Liguori
2697 5bae1900 Anthony Liguori
    err = v9fs_do_remove(s, &vs->fidp->path);
2698 5bae1900 Anthony Liguori
    v9fs_remove_post_remove(s, vs, err);
2699 5bae1900 Anthony Liguori
    return;
2700 5bae1900 Anthony Liguori
2701 5bae1900 Anthony Liguori
out:
2702 5bae1900 Anthony Liguori
    complete_pdu(s, pdu, err);
2703 5bae1900 Anthony Liguori
    qemu_free(vs);
2704 9f107513 Anthony Liguori
}
2705 9f107513 Anthony Liguori
2706 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
2707 8cf89e00 Anthony Liguori
{
2708 8cf89e00 Anthony Liguori
    if (err < 0) {
2709 8cf89e00 Anthony Liguori
        goto out;
2710 8cf89e00 Anthony Liguori
    }
2711 8cf89e00 Anthony Liguori
2712 8cf89e00 Anthony Liguori
    err = vs->offset;
2713 8cf89e00 Anthony Liguori
2714 8cf89e00 Anthony Liguori
out:
2715 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2716 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2717 8cf89e00 Anthony Liguori
    qemu_free(vs);
2718 8cf89e00 Anthony Liguori
}
2719 8cf89e00 Anthony Liguori
2720 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
2721 8cf89e00 Anthony Liguori
{
2722 8cf89e00 Anthony Liguori
    if (err < 0) {
2723 8cf89e00 Anthony Liguori
        goto out;
2724 8cf89e00 Anthony Liguori
    }
2725 8cf89e00 Anthony Liguori
    if (vs->v9stat.length != -1) {
2726 8cf89e00 Anthony Liguori
        if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
2727 8cf89e00 Anthony Liguori
            err = -errno;
2728 8cf89e00 Anthony Liguori
        }
2729 8cf89e00 Anthony Liguori
    }
2730 8cf89e00 Anthony Liguori
    v9fs_wstat_post_truncate(s, vs, err);
2731 8cf89e00 Anthony Liguori
    return;
2732 8cf89e00 Anthony Liguori
2733 8cf89e00 Anthony Liguori
out:
2734 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2735 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2736 8cf89e00 Anthony Liguori
    qemu_free(vs);
2737 8cf89e00 Anthony Liguori
}
2738 8cf89e00 Anthony Liguori
2739 c7b4b0b3 M. Mohan Kumar
static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs)
2740 8cf89e00 Anthony Liguori
{
2741 c7b4b0b3 M. Mohan Kumar
    int err = 0;
2742 c7b4b0b3 M. Mohan Kumar
    char *old_name, *new_name;
2743 c7b4b0b3 M. Mohan Kumar
    char *end;
2744 8cf89e00 Anthony Liguori
2745 c7b4b0b3 M. Mohan Kumar
    if (vs->newdirfid != -1) {
2746 c7b4b0b3 M. Mohan Kumar
        V9fsFidState *dirfidp;
2747 c7b4b0b3 M. Mohan Kumar
        dirfidp = lookup_fid(s, vs->newdirfid);
2748 c7b4b0b3 M. Mohan Kumar
2749 c7b4b0b3 M. Mohan Kumar
        if (dirfidp == NULL) {
2750 c7b4b0b3 M. Mohan Kumar
            err = -ENOENT;
2751 c7b4b0b3 M. Mohan Kumar
            goto out;
2752 c7b4b0b3 M. Mohan Kumar
        }
2753 c7b4b0b3 M. Mohan Kumar
2754 d62dbb51 Aneesh Kumar K.V
        BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2755 8cf89e00 Anthony Liguori
2756 c7b4b0b3 M. Mohan Kumar
        new_name = qemu_mallocz(dirfidp->path.size + vs->name.size + 2);
2757 c7b4b0b3 M. Mohan Kumar
2758 c7b4b0b3 M. Mohan Kumar
        strcpy(new_name, dirfidp->path.data);
2759 c7b4b0b3 M. Mohan Kumar
        strcat(new_name, "/");
2760 c7b4b0b3 M. Mohan Kumar
        strcat(new_name + dirfidp->path.size, vs->name.data);
2761 c7b4b0b3 M. Mohan Kumar
    } else {
2762 8cf89e00 Anthony Liguori
        old_name = vs->fidp->path.data;
2763 8cf89e00 Anthony Liguori
        end = strrchr(old_name, '/');
2764 8cf89e00 Anthony Liguori
        if (end) {
2765 8cf89e00 Anthony Liguori
            end++;
2766 8cf89e00 Anthony Liguori
        } else {
2767 8cf89e00 Anthony Liguori
            end = old_name;
2768 8cf89e00 Anthony Liguori
        }
2769 c7b4b0b3 M. Mohan Kumar
        new_name = qemu_mallocz(end - old_name + vs->name.size + 1);
2770 8cf89e00 Anthony Liguori
2771 c7b4b0b3 M. Mohan Kumar
        strncat(new_name, old_name, end - old_name);
2772 c7b4b0b3 M. Mohan Kumar
        strncat(new_name + (end - old_name), vs->name.data, vs->name.size);
2773 c7b4b0b3 M. Mohan Kumar
    }
2774 8cf89e00 Anthony Liguori
2775 c7b4b0b3 M. Mohan Kumar
    v9fs_string_free(&vs->name);
2776 c7b4b0b3 M. Mohan Kumar
    vs->name.data = qemu_strdup(new_name);
2777 c7b4b0b3 M. Mohan Kumar
    vs->name.size = strlen(new_name);
2778 8cf89e00 Anthony Liguori
2779 c7b4b0b3 M. Mohan Kumar
    if (strcmp(new_name, vs->fidp->path.data) != 0) {
2780 c7b4b0b3 M. Mohan Kumar
        if (v9fs_do_rename(s, &vs->fidp->path, &vs->name)) {
2781 c7b4b0b3 M. Mohan Kumar
            err = -errno;
2782 c7b4b0b3 M. Mohan Kumar
        } else {
2783 c7b4b0b3 M. Mohan Kumar
            V9fsFidState *fidp;
2784 c7b4b0b3 M. Mohan Kumar
            /*
2785 c7b4b0b3 M. Mohan Kumar
            * Fixup fid's pointing to the old name to
2786 c7b4b0b3 M. Mohan Kumar
            * start pointing to the new name
2787 c7b4b0b3 M. Mohan Kumar
            */
2788 c7b4b0b3 M. Mohan Kumar
            for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2789 c7b4b0b3 M. Mohan Kumar
                if (vs->fidp == fidp) {
2790 c7b4b0b3 M. Mohan Kumar
                    /*
2791 c7b4b0b3 M. Mohan Kumar
                    * we replace name of this fid towards the end
2792 c7b4b0b3 M. Mohan Kumar
                    * so that our below strcmp will work
2793 c7b4b0b3 M. Mohan Kumar
                    */
2794 c7b4b0b3 M. Mohan Kumar
                    continue;
2795 c7b4b0b3 M. Mohan Kumar
                }
2796 c7b4b0b3 M. Mohan Kumar
                if (!strncmp(vs->fidp->path.data, fidp->path.data,
2797 c7b4b0b3 M. Mohan Kumar
                    strlen(vs->fidp->path.data))) {
2798 c7b4b0b3 M. Mohan Kumar
                    /* replace the name */
2799 c7b4b0b3 M. Mohan Kumar
                    v9fs_fix_path(&fidp->path, &vs->name,
2800 c7b4b0b3 M. Mohan Kumar
                                  strlen(vs->fidp->path.data));
2801 8cf89e00 Anthony Liguori
                }
2802 8cf89e00 Anthony Liguori
            }
2803 c7b4b0b3 M. Mohan Kumar
            v9fs_string_copy(&vs->fidp->path, &vs->name);
2804 8cf89e00 Anthony Liguori
        }
2805 8cf89e00 Anthony Liguori
    }
2806 c7b4b0b3 M. Mohan Kumar
out:
2807 c7b4b0b3 M. Mohan Kumar
    v9fs_string_free(&vs->name);
2808 c7b4b0b3 M. Mohan Kumar
    return err;
2809 c7b4b0b3 M. Mohan Kumar
}
2810 c7b4b0b3 M. Mohan Kumar
2811 c7b4b0b3 M. Mohan Kumar
static void v9fs_rename_post_rename(V9fsState *s, V9fsRenameState *vs, int err)
2812 c7b4b0b3 M. Mohan Kumar
{
2813 c7b4b0b3 M. Mohan Kumar
    complete_pdu(s, vs->pdu, err);
2814 c7b4b0b3 M. Mohan Kumar
    qemu_free(vs);
2815 c7b4b0b3 M. Mohan Kumar
}
2816 c7b4b0b3 M. Mohan Kumar
2817 c7b4b0b3 M. Mohan Kumar
static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
2818 c7b4b0b3 M. Mohan Kumar
{
2819 c7b4b0b3 M. Mohan Kumar
    if (err < 0) {
2820 c7b4b0b3 M. Mohan Kumar
        goto out;
2821 c7b4b0b3 M. Mohan Kumar
    }
2822 c7b4b0b3 M. Mohan Kumar
2823 c7b4b0b3 M. Mohan Kumar
    if (vs->v9stat.name.size != 0) {
2824 c7b4b0b3 M. Mohan Kumar
        V9fsRenameState *vr;
2825 c7b4b0b3 M. Mohan Kumar
2826 c7b4b0b3 M. Mohan Kumar
        vr = qemu_malloc(sizeof(V9fsRenameState));
2827 c7b4b0b3 M. Mohan Kumar
        memset(vr, sizeof(*vr), 0);
2828 c7b4b0b3 M. Mohan Kumar
        vr->newdirfid = -1;
2829 c7b4b0b3 M. Mohan Kumar
        vr->pdu = vs->pdu;
2830 c7b4b0b3 M. Mohan Kumar
        vr->fidp = vs->fidp;
2831 c7b4b0b3 M. Mohan Kumar
        vr->offset = vs->offset;
2832 c7b4b0b3 M. Mohan Kumar
        vr->name.size = vs->v9stat.name.size;
2833 c7b4b0b3 M. Mohan Kumar
        vr->name.data = qemu_strdup(vs->v9stat.name.data);
2834 c7b4b0b3 M. Mohan Kumar
2835 c7b4b0b3 M. Mohan Kumar
        err = v9fs_complete_rename(s, vr);
2836 c7b4b0b3 M. Mohan Kumar
        qemu_free(vr);
2837 c7b4b0b3 M. Mohan Kumar
    }
2838 8cf89e00 Anthony Liguori
    v9fs_wstat_post_rename(s, vs, err);
2839 8cf89e00 Anthony Liguori
    return;
2840 8cf89e00 Anthony Liguori
2841 8cf89e00 Anthony Liguori
out:
2842 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2843 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2844 8cf89e00 Anthony Liguori
    qemu_free(vs);
2845 8cf89e00 Anthony Liguori
}
2846 8cf89e00 Anthony Liguori
2847 c7b4b0b3 M. Mohan Kumar
static void v9fs_rename(V9fsState *s, V9fsPDU *pdu)
2848 c7b4b0b3 M. Mohan Kumar
{
2849 c7b4b0b3 M. Mohan Kumar
    int32_t fid;
2850 c7b4b0b3 M. Mohan Kumar
    V9fsRenameState *vs;
2851 c7b4b0b3 M. Mohan Kumar
    ssize_t err = 0;
2852 c7b4b0b3 M. Mohan Kumar
2853 c7b4b0b3 M. Mohan Kumar
    vs = qemu_malloc(sizeof(*vs));
2854 c7b4b0b3 M. Mohan Kumar
    vs->pdu = pdu;
2855 c7b4b0b3 M. Mohan Kumar
    vs->offset = 7;
2856 c7b4b0b3 M. Mohan Kumar
2857 c7b4b0b3 M. Mohan Kumar
    pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &vs->newdirfid, &vs->name);
2858 c7b4b0b3 M. Mohan Kumar
2859 c7b4b0b3 M. Mohan Kumar
    vs->fidp = lookup_fid(s, fid);
2860 c7b4b0b3 M. Mohan Kumar
    if (vs->fidp == NULL) {
2861 c7b4b0b3 M. Mohan Kumar
        err = -ENOENT;
2862 c7b4b0b3 M. Mohan Kumar
        goto out;
2863 c7b4b0b3 M. Mohan Kumar
    }
2864 c7b4b0b3 M. Mohan Kumar
2865 d62dbb51 Aneesh Kumar K.V
    BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
2866 c7b4b0b3 M. Mohan Kumar
2867 c7b4b0b3 M. Mohan Kumar
    err = v9fs_complete_rename(s, vs);
2868 c7b4b0b3 M. Mohan Kumar
    v9fs_rename_post_rename(s, vs, err);
2869 c7b4b0b3 M. Mohan Kumar
    return;
2870 c7b4b0b3 M. Mohan Kumar
out:
2871 c7b4b0b3 M. Mohan Kumar
    complete_pdu(s, vs->pdu, err);
2872 c7b4b0b3 M. Mohan Kumar
    qemu_free(vs);
2873 c7b4b0b3 M. Mohan Kumar
}
2874 c7b4b0b3 M. Mohan Kumar
2875 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2876 8cf89e00 Anthony Liguori
{
2877 8cf89e00 Anthony Liguori
    if (err < 0) {
2878 8cf89e00 Anthony Liguori
        goto out;
2879 8cf89e00 Anthony Liguori
    }
2880 8cf89e00 Anthony Liguori
2881 f7613bee Venkateswararao Jujjuri (JV)
    if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) {
2882 8cf89e00 Anthony Liguori
        if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2883 8cf89e00 Anthony Liguori
                    vs->v9stat.n_gid)) {
2884 8cf89e00 Anthony Liguori
            err = -errno;
2885 8cf89e00 Anthony Liguori
        }
2886 8cf89e00 Anthony Liguori
    }
2887 8cf89e00 Anthony Liguori
    v9fs_wstat_post_chown(s, vs, err);
2888 8cf89e00 Anthony Liguori
    return;
2889 8cf89e00 Anthony Liguori
2890 8cf89e00 Anthony Liguori
out:
2891 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2892 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2893 8cf89e00 Anthony Liguori
    qemu_free(vs);
2894 8cf89e00 Anthony Liguori
}
2895 8cf89e00 Anthony Liguori
2896 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2897 8cf89e00 Anthony Liguori
{
2898 8cf89e00 Anthony Liguori
    if (err < 0) {
2899 8cf89e00 Anthony Liguori
        goto out;
2900 8cf89e00 Anthony Liguori
    }
2901 8cf89e00 Anthony Liguori
2902 74bc02b2 M. Mohan Kumar
    if (vs->v9stat.mtime != -1 || vs->v9stat.atime != -1) {
2903 8fc39ae4 Sripathi Kodi
        struct timespec times[2];
2904 8fc39ae4 Sripathi Kodi
        if (vs->v9stat.atime != -1) {
2905 8fc39ae4 Sripathi Kodi
            times[0].tv_sec = vs->v9stat.atime;
2906 8fc39ae4 Sripathi Kodi
            times[0].tv_nsec = 0;
2907 8fc39ae4 Sripathi Kodi
        } else {
2908 8fc39ae4 Sripathi Kodi
            times[0].tv_nsec = UTIME_OMIT;
2909 8fc39ae4 Sripathi Kodi
        }
2910 8fc39ae4 Sripathi Kodi
        if (vs->v9stat.mtime != -1) {
2911 8fc39ae4 Sripathi Kodi
            times[1].tv_sec = vs->v9stat.mtime;
2912 8fc39ae4 Sripathi Kodi
            times[1].tv_nsec = 0;
2913 8fc39ae4 Sripathi Kodi
        } else {
2914 8fc39ae4 Sripathi Kodi
            times[1].tv_nsec = UTIME_OMIT;
2915 8fc39ae4 Sripathi Kodi
        }
2916 8fc39ae4 Sripathi Kodi
2917 8fc39ae4 Sripathi Kodi
        if (v9fs_do_utimensat(s, &vs->fidp->path, times)) {
2918 8cf89e00 Anthony Liguori
            err = -errno;
2919 8cf89e00 Anthony Liguori
        }
2920 8cf89e00 Anthony Liguori
    }
2921 8cf89e00 Anthony Liguori
2922 8cf89e00 Anthony Liguori
    v9fs_wstat_post_utime(s, vs, err);
2923 8cf89e00 Anthony Liguori
    return;
2924 8cf89e00 Anthony Liguori
2925 8cf89e00 Anthony Liguori
out:
2926 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2927 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2928 8cf89e00 Anthony Liguori
    qemu_free(vs);
2929 8cf89e00 Anthony Liguori
}
2930 8cf89e00 Anthony Liguori
2931 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2932 8cf89e00 Anthony Liguori
{
2933 8cf89e00 Anthony Liguori
    if (err == -1) {
2934 8cf89e00 Anthony Liguori
        err = -errno;
2935 8cf89e00 Anthony Liguori
    }
2936 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2937 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2938 8cf89e00 Anthony Liguori
    qemu_free(vs);
2939 8cf89e00 Anthony Liguori
}
2940 8cf89e00 Anthony Liguori
2941 8cf89e00 Anthony Liguori
static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2942 8cf89e00 Anthony Liguori
{
2943 8cf89e00 Anthony Liguori
    uint32_t v9_mode;
2944 8cf89e00 Anthony Liguori
2945 8cf89e00 Anthony Liguori
    if (err == -1) {
2946 8cf89e00 Anthony Liguori
        err = -errno;
2947 8cf89e00 Anthony Liguori
        goto out;
2948 8cf89e00 Anthony Liguori
    }
2949 8cf89e00 Anthony Liguori
2950 8cf89e00 Anthony Liguori
    v9_mode = stat_to_v9mode(&vs->stbuf);
2951 8cf89e00 Anthony Liguori
2952 8cf89e00 Anthony Liguori
    if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2953 8cf89e00 Anthony Liguori
        (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2954 8cf89e00 Anthony Liguori
            /* Attempting to change the type */
2955 8cf89e00 Anthony Liguori
            err = -EIO;
2956 8cf89e00 Anthony Liguori
            goto out;
2957 8cf89e00 Anthony Liguori
    }
2958 8cf89e00 Anthony Liguori
2959 8cf89e00 Anthony Liguori
    if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2960 8cf89e00 Anthony Liguori
                    &vs->v9stat.extension))) {
2961 8cf89e00 Anthony Liguori
            err = -errno;
2962 8cf89e00 Anthony Liguori
     }
2963 8cf89e00 Anthony Liguori
    v9fs_wstat_post_chmod(s, vs, err);
2964 8cf89e00 Anthony Liguori
    return;
2965 8cf89e00 Anthony Liguori
2966 8cf89e00 Anthony Liguori
out:
2967 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
2968 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
2969 8cf89e00 Anthony Liguori
    qemu_free(vs);
2970 8cf89e00 Anthony Liguori
}
2971 8cf89e00 Anthony Liguori
2972 9f107513 Anthony Liguori
static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
2973 9f107513 Anthony Liguori
{
2974 8cf89e00 Anthony Liguori
    int32_t fid;
2975 8cf89e00 Anthony Liguori
    V9fsWstatState *vs;
2976 8cf89e00 Anthony Liguori
    int err = 0;
2977 8cf89e00 Anthony Liguori
2978 8cf89e00 Anthony Liguori
    vs = qemu_malloc(sizeof(*vs));
2979 8cf89e00 Anthony Liguori
    vs->pdu = pdu;
2980 8cf89e00 Anthony Liguori
    vs->offset = 7;
2981 8cf89e00 Anthony Liguori
2982 8cf89e00 Anthony Liguori
    pdu_unmarshal(pdu, vs->offset, "dwS", &fid, &vs->unused, &vs->v9stat);
2983 8cf89e00 Anthony Liguori
2984 8cf89e00 Anthony Liguori
    vs->fidp = lookup_fid(s, fid);
2985 8cf89e00 Anthony Liguori
    if (vs->fidp == NULL) {
2986 8cf89e00 Anthony Liguori
        err = -EINVAL;
2987 8cf89e00 Anthony Liguori
        goto out;
2988 9f107513 Anthony Liguori
    }
2989 8cf89e00 Anthony Liguori
2990 8cf89e00 Anthony Liguori
    /* do we need to sync the file? */
2991 8cf89e00 Anthony Liguori
    if (donttouch_stat(&vs->v9stat)) {
2992 d62dbb51 Aneesh Kumar K.V
        err = v9fs_do_fsync(s, vs->fidp->fs.fd);
2993 8cf89e00 Anthony Liguori
        v9fs_wstat_post_fsync(s, vs, err);
2994 8cf89e00 Anthony Liguori
        return;
2995 8cf89e00 Anthony Liguori
    }
2996 8cf89e00 Anthony Liguori
2997 8cf89e00 Anthony Liguori
    if (vs->v9stat.mode != -1) {
2998 8cf89e00 Anthony Liguori
        err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
2999 8cf89e00 Anthony Liguori
        v9fs_wstat_post_lstat(s, vs, err);
3000 8cf89e00 Anthony Liguori
        return;
3001 8cf89e00 Anthony Liguori
    }
3002 8cf89e00 Anthony Liguori
3003 8cf89e00 Anthony Liguori
    v9fs_wstat_post_chmod(s, vs, err);
3004 8cf89e00 Anthony Liguori
    return;
3005 8cf89e00 Anthony Liguori
3006 8cf89e00 Anthony Liguori
out:
3007 8cf89e00 Anthony Liguori
    v9fs_stat_free(&vs->v9stat);
3008 8cf89e00 Anthony Liguori
    complete_pdu(s, vs->pdu, err);
3009 8cf89e00 Anthony Liguori
    qemu_free(vs);
3010 9f107513 Anthony Liguori
}
3011 9f107513 Anthony Liguori
3012 be940c87 M. Mohan Kumar
static void v9fs_statfs_post_statfs(V9fsState *s, V9fsStatfsState *vs, int err)
3013 be940c87 M. Mohan Kumar
{
3014 5e94c103 M. Mohan Kumar
    int32_t bsize_factor;
3015 5e94c103 M. Mohan Kumar
3016 be940c87 M. Mohan Kumar
    if (err) {
3017 be940c87 M. Mohan Kumar
        err = -errno;
3018 be940c87 M. Mohan Kumar
        goto out;
3019 be940c87 M. Mohan Kumar
    }
3020 be940c87 M. Mohan Kumar
3021 5e94c103 M. Mohan Kumar
    /*
3022 5e94c103 M. Mohan Kumar
     * compute bsize factor based on host file system block size
3023 5e94c103 M. Mohan Kumar
     * and client msize
3024 5e94c103 M. Mohan Kumar
     */
3025 5e94c103 M. Mohan Kumar
    bsize_factor = (s->msize - P9_IOHDRSZ)/vs->stbuf.f_bsize;
3026 5e94c103 M. Mohan Kumar
    if (!bsize_factor) {
3027 5e94c103 M. Mohan Kumar
        bsize_factor = 1;
3028 5e94c103 M. Mohan Kumar
    }
3029 be940c87 M. Mohan Kumar
    vs->v9statfs.f_type = vs->stbuf.f_type;
3030 be940c87 M. Mohan Kumar
    vs->v9statfs.f_bsize = vs->stbuf.f_bsize;
3031 5e94c103 M. Mohan Kumar
    vs->v9statfs.f_bsize *= bsize_factor;
3032 5e94c103 M. Mohan Kumar
    /*
3033 5e94c103 M. Mohan Kumar
     * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3034 5e94c103 M. Mohan Kumar
     * adjust(divide) the number of blocks, free blocks and available
3035 5e94c103 M. Mohan Kumar
     * blocks by bsize factor
3036 5e94c103 M. Mohan Kumar
     */
3037 5e94c103 M. Mohan Kumar
    vs->v9statfs.f_blocks = vs->stbuf.f_blocks/bsize_factor;
3038 5e94c103 M. Mohan Kumar
    vs->v9statfs.f_bfree = vs->stbuf.f_bfree/bsize_factor;
3039 5e94c103 M. Mohan Kumar
    vs->v9statfs.f_bavail = vs->stbuf.f_bavail/bsize_factor;
3040 be940c87 M. Mohan Kumar
    vs->v9statfs.f_files = vs->stbuf.f_files;
3041 be940c87 M. Mohan Kumar
    vs->v9statfs.f_ffree = vs->stbuf.f_ffree;
3042 be940c87 M. Mohan Kumar
    vs->v9statfs.fsid_val = (unsigned int) vs->stbuf.f_fsid.__val[0] |
3043 be940c87 M. Mohan Kumar
                        (unsigned long long)vs->stbuf.f_fsid.__val[1] << 32;
3044 be940c87 M. Mohan Kumar
    vs->v9statfs.f_namelen = vs->stbuf.f_namelen;
3045 be940c87 M. Mohan Kumar
3046 be940c87 M. Mohan Kumar
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "ddqqqqqqd",
3047 be940c87 M. Mohan Kumar
         vs->v9statfs.f_type, vs->v9statfs.f_bsize, vs->v9statfs.f_blocks,
3048 be940c87 M. Mohan Kumar
         vs->v9statfs.f_bfree, vs->v9statfs.f_bavail, vs->v9statfs.f_files,
3049 be940c87 M. Mohan Kumar
         vs->v9statfs.f_ffree, vs->v9statfs.fsid_val,
3050 be940c87 M. Mohan Kumar
         vs->v9statfs.f_namelen);
3051 be940c87 M. Mohan Kumar
3052 be940c87 M. Mohan Kumar
out:
3053 be940c87 M. Mohan Kumar
    complete_pdu(s, vs->pdu, vs->offset);
3054 be940c87 M. Mohan Kumar
    qemu_free(vs);
3055 be940c87 M. Mohan Kumar
}
3056 be940c87 M. Mohan Kumar
3057 be940c87 M. Mohan Kumar
static void v9fs_statfs(V9fsState *s, V9fsPDU *pdu)
3058 be940c87 M. Mohan Kumar
{
3059 be940c87 M. Mohan Kumar
    V9fsStatfsState *vs;
3060 be940c87 M. Mohan Kumar
    ssize_t err = 0;
3061 be940c87 M. Mohan Kumar
3062 be940c87 M. Mohan Kumar
    vs = qemu_malloc(sizeof(*vs));
3063 be940c87 M. Mohan Kumar
    vs->pdu = pdu;
3064 be940c87 M. Mohan Kumar
    vs->offset = 7;
3065 be940c87 M. Mohan Kumar
3066 be940c87 M. Mohan Kumar
    memset(&vs->v9statfs, 0, sizeof(vs->v9statfs));
3067 be940c87 M. Mohan Kumar
3068 be940c87 M. Mohan Kumar
    pdu_unmarshal(vs->pdu, vs->offset, "d", &vs->fid);
3069 be940c87 M. Mohan Kumar
3070 be940c87 M. Mohan Kumar
    vs->fidp = lookup_fid(s, vs->fid);
3071 be940c87 M. Mohan Kumar
    if (vs->fidp == NULL) {
3072 be940c87 M. Mohan Kumar
        err = -ENOENT;
3073 be940c87 M. Mohan Kumar
        goto out;
3074 be940c87 M. Mohan Kumar
    }
3075 be940c87 M. Mohan Kumar
3076 be940c87 M. Mohan Kumar
    err = v9fs_do_statfs(s, &vs->fidp->path, &vs->stbuf);
3077 be940c87 M. Mohan Kumar
    v9fs_statfs_post_statfs(s, vs, err);
3078 be940c87 M. Mohan Kumar
    return;
3079 be940c87 M. Mohan Kumar
3080 be940c87 M. Mohan Kumar
out:
3081 be940c87 M. Mohan Kumar
    complete_pdu(s, vs->pdu, err);
3082 be940c87 M. Mohan Kumar
    qemu_free(vs);
3083 be940c87 M. Mohan Kumar
}
3084 be940c87 M. Mohan Kumar
3085 5268cecc M. Mohan Kumar
static void v9fs_mknod_post_lstat(V9fsState *s, V9fsMkState *vs, int err)
3086 5268cecc M. Mohan Kumar
{
3087 5268cecc M. Mohan Kumar
    if (err == -1) {
3088 5268cecc M. Mohan Kumar
        err = -errno;
3089 5268cecc M. Mohan Kumar
        goto out;
3090 5268cecc M. Mohan Kumar
    }
3091 5268cecc M. Mohan Kumar
3092 5268cecc M. Mohan Kumar
    stat_to_qid(&vs->stbuf, &vs->qid);
3093 5268cecc M. Mohan Kumar
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
3094 5268cecc M. Mohan Kumar
    err = vs->offset;
3095 5268cecc M. Mohan Kumar
out:
3096 5268cecc M. Mohan Kumar
    complete_pdu(s, vs->pdu, err);
3097 5268cecc M. Mohan Kumar
    v9fs_string_free(&vs->fullname);
3098 5268cecc M. Mohan Kumar
    v9fs_string_free(&vs->name);
3099 5268cecc M. Mohan Kumar
    qemu_free(vs);
3100 5268cecc M. Mohan Kumar
}
3101 5268cecc M. Mohan Kumar
3102 5268cecc M. Mohan Kumar
static void v9fs_mknod_post_mknod(V9fsState *s, V9fsMkState *vs, int err)
3103 5268cecc M. Mohan Kumar
{
3104 5268cecc M. Mohan Kumar
    if (err == -1) {
3105 5268cecc M. Mohan Kumar
        err = -errno;
3106 5268cecc M. Mohan Kumar
        goto out;
3107 5268cecc M. Mohan Kumar
    }
3108 5268cecc M. Mohan Kumar
3109 5268cecc M. Mohan Kumar
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
3110 5268cecc M. Mohan Kumar
    v9fs_mknod_post_lstat(s, vs, err);
3111 5268cecc M. Mohan Kumar
    return;
3112 5268cecc M. Mohan Kumar
out:
3113 5268cecc M. Mohan Kumar
    complete_pdu(s, vs->pdu, err);
3114 5268cecc M. Mohan Kumar
    v9fs_string_free(&vs->fullname);
3115 5268cecc M. Mohan Kumar
    v9fs_string_free(&vs->name);
3116 5268cecc M. Mohan Kumar
    qemu_free(vs);
3117 5268cecc M. Mohan Kumar
}
3118 5268cecc M. Mohan Kumar
3119 5268cecc M. Mohan Kumar
static void v9fs_mknod(V9fsState *s, V9fsPDU *pdu)
3120 5268cecc M. Mohan Kumar
{
3121 5268cecc M. Mohan Kumar
    int32_t fid;
3122 5268cecc M. Mohan Kumar
    V9fsMkState *vs;
3123 5268cecc M. Mohan Kumar
    int err = 0;
3124 5268cecc M. Mohan Kumar
    V9fsFidState *fidp;
3125 5268cecc M. Mohan Kumar
    gid_t gid;
3126 5268cecc M. Mohan Kumar
    int mode;
3127 5268cecc M. Mohan Kumar
    int major, minor;
3128 5268cecc M. Mohan Kumar
3129 5268cecc M. Mohan Kumar
    vs = qemu_malloc(sizeof(*vs));
3130 5268cecc M. Mohan Kumar
    vs->pdu = pdu;
3131 5268cecc M. Mohan Kumar
    vs->offset = 7;
3132 5268cecc M. Mohan Kumar
3133 5268cecc M. Mohan Kumar
    v9fs_string_init(&vs->fullname);
3134 5268cecc M. Mohan Kumar
    pdu_unmarshal(vs->pdu, vs->offset, "dsdddd", &fid, &vs->name, &mode,
3135 5268cecc M. Mohan Kumar
        &major, &minor, &gid);
3136 5268cecc M. Mohan Kumar
3137 5268cecc M. Mohan Kumar
    fidp = lookup_fid(s, fid);
3138 5268cecc M. Mohan Kumar
    if (fidp == NULL) {
3139 5268cecc M. Mohan Kumar
        err = -ENOENT;
3140 5268cecc M. Mohan Kumar
        goto out;
3141 5268cecc M. Mohan Kumar
    }
3142 5268cecc M. Mohan Kumar
3143 5268cecc M. Mohan Kumar
    v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data);
3144 5268cecc M. Mohan Kumar
    err = v9fs_do_mknod(s, vs->fullname.data, mode, makedev(major, minor),
3145 5268cecc M. Mohan Kumar
        fidp->uid, gid);
3146 5268cecc M. Mohan Kumar
    v9fs_mknod_post_mknod(s, vs, err);
3147 5268cecc M. Mohan Kumar
    return;
3148 5268cecc M. Mohan Kumar
3149 5268cecc M. Mohan Kumar
out:
3150 5268cecc M. Mohan Kumar
    complete_pdu(s, vs->pdu, err);
3151 5268cecc M. Mohan Kumar
    v9fs_string_free(&vs->fullname);
3152 5268cecc M. Mohan Kumar
    v9fs_string_free(&vs->name);
3153 5268cecc M. Mohan Kumar
    qemu_free(vs);
3154 5268cecc M. Mohan Kumar
}
3155 5268cecc M. Mohan Kumar
3156 b67592ea M. Mohan Kumar
static void v9fs_mkdir_post_lstat(V9fsState *s, V9fsMkState *vs, int err)
3157 b67592ea M. Mohan Kumar
{
3158 b67592ea M. Mohan Kumar
    if (err == -1) {
3159 b67592ea M. Mohan Kumar
        err = -errno;
3160 b67592ea M. Mohan Kumar
        goto out;
3161 b67592ea M. Mohan Kumar
    }
3162 b67592ea M. Mohan Kumar
3163 b67592ea M. Mohan Kumar
    stat_to_qid(&vs->stbuf, &vs->qid);
3164 b67592ea M. Mohan Kumar
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
3165 b67592ea M. Mohan Kumar
    err = vs->offset;
3166 b67592ea M. Mohan Kumar
out:
3167 b67592ea M. Mohan Kumar
    complete_pdu(s, vs->pdu, err);
3168 b67592ea M. Mohan Kumar
    v9fs_string_free(&vs->fullname);
3169 b67592ea M. Mohan Kumar
    v9fs_string_free(&vs->name);
3170 b67592ea M. Mohan Kumar
    qemu_free(vs);
3171 b67592ea M. Mohan Kumar
}
3172 b67592ea M. Mohan Kumar
3173 b67592ea M. Mohan Kumar
static void v9fs_mkdir_post_mkdir(V9fsState *s, V9fsMkState *vs, int err)
3174 b67592ea M. Mohan Kumar
{
3175 b67592ea M. Mohan Kumar
    if (err == -1) {
3176 b67592ea M. Mohan Kumar
        err = -errno;
3177 b67592ea M. Mohan Kumar
        goto out;
3178 b67592ea M. Mohan Kumar
    }
3179 b67592ea M. Mohan Kumar
3180 b67592ea M. Mohan Kumar
    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
3181 b67592ea M. Mohan Kumar
    v9fs_mkdir_post_lstat(s, vs, err);
3182 b67592ea M. Mohan Kumar
    return;
3183 b67592ea M. Mohan Kumar
out:
3184 b67592ea M. Mohan Kumar
    complete_pdu(s, vs->pdu, err);
3185 b67592ea M. Mohan Kumar
    v9fs_string_free(&vs->fullname);
3186 b67592ea M. Mohan Kumar
    v9fs_string_free(&vs->name);
3187 b67592ea M. Mohan Kumar
    qemu_free(vs);
3188 b67592ea M. Mohan Kumar
}
3189 b67592ea M. Mohan Kumar
3190 b67592ea M. Mohan Kumar
static void v9fs_mkdir(V9fsState *s, V9fsPDU *pdu)
3191 b67592ea M. Mohan Kumar
{
3192 b67592ea M. Mohan Kumar
    int32_t fid;
3193 b67592ea M. Mohan Kumar
    V9fsMkState *vs;
3194 b67592ea M. Mohan Kumar
    int err = 0;
3195 b67592ea M. Mohan Kumar
    V9fsFidState *fidp;
3196 b67592ea M. Mohan Kumar
    gid_t gid;
3197 b67592ea M. Mohan Kumar
    int mode;
3198 b67592ea M. Mohan Kumar
3199 b67592ea M. Mohan Kumar
    vs = qemu_malloc(sizeof(*vs));
3200 b67592ea M. Mohan Kumar
    vs->pdu = pdu;
3201 b67592ea M. Mohan Kumar
    vs->offset = 7;
3202 b67592ea M. Mohan Kumar
3203 b67592ea M. Mohan Kumar
    v9fs_string_init(&vs->fullname);
3204 b67592ea M. Mohan Kumar
    pdu_unmarshal(vs->pdu, vs->offset, "dsdd", &fid, &vs->name, &mode,
3205 b67592ea M. Mohan Kumar
        &gid);
3206 b67592ea M. Mohan Kumar
3207 b67592ea M. Mohan Kumar
    fidp = lookup_fid(s, fid);
3208 b67592ea M. Mohan Kumar
    if (fidp == NULL) {
3209 b67592ea M. Mohan Kumar
        err = -ENOENT;
3210 b67592ea M. Mohan Kumar
        goto out;
3211 b67592ea M. Mohan Kumar
    }
3212 b67592ea M. Mohan Kumar
3213 b67592ea M. Mohan Kumar
    v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data);
3214 b67592ea M. Mohan Kumar
    err = v9fs_do_mkdir(s, vs->fullname.data, mode, fidp->uid, gid);
3215 b67592ea M. Mohan Kumar
    v9fs_mkdir_post_mkdir(s, vs, err);
3216 b67592ea M. Mohan Kumar
    return;
3217 b67592ea M. Mohan Kumar
3218 b67592ea M. Mohan Kumar
out:
3219 b67592ea M. Mohan Kumar
    complete_pdu(s, vs->pdu, err);
3220 b67592ea M. Mohan Kumar
    v9fs_string_free(&vs->fullname);
3221 b67592ea M. Mohan Kumar
    v9fs_string_free(&vs->name);
3222 b67592ea M. Mohan Kumar
    qemu_free(vs);
3223 b67592ea M. Mohan Kumar
}
3224 b67592ea M. Mohan Kumar
3225 fa32ef88 Aneesh Kumar K.V
static void v9fs_post_xattr_getvalue(V9fsState *s, V9fsXattrState *vs, int err)
3226 fa32ef88 Aneesh Kumar K.V
{
3227 fa32ef88 Aneesh Kumar K.V
3228 fa32ef88 Aneesh Kumar K.V
    if (err < 0) {
3229 fa32ef88 Aneesh Kumar K.V
        err = -errno;
3230 fa32ef88 Aneesh Kumar K.V
        free_fid(s, vs->xattr_fidp->fid);
3231 fa32ef88 Aneesh Kumar K.V
        goto out;
3232 fa32ef88 Aneesh Kumar K.V
    }
3233 fa32ef88 Aneesh Kumar K.V
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size);
3234 fa32ef88 Aneesh Kumar K.V
    err = vs->offset;
3235 fa32ef88 Aneesh Kumar K.V
out:
3236 fa32ef88 Aneesh Kumar K.V
    complete_pdu(s, vs->pdu, err);
3237 fa32ef88 Aneesh Kumar K.V
    v9fs_string_free(&vs->name);
3238 fa32ef88 Aneesh Kumar K.V
    qemu_free(vs);
3239 fa32ef88 Aneesh Kumar K.V
    return;
3240 fa32ef88 Aneesh Kumar K.V
}
3241 fa32ef88 Aneesh Kumar K.V
3242 fa32ef88 Aneesh Kumar K.V
static void v9fs_post_xattr_check(V9fsState *s, V9fsXattrState *vs, ssize_t err)
3243 fa32ef88 Aneesh Kumar K.V
{
3244 fa32ef88 Aneesh Kumar K.V
    if (err < 0) {
3245 fa32ef88 Aneesh Kumar K.V
        err = -errno;
3246 fa32ef88 Aneesh Kumar K.V
        free_fid(s, vs->xattr_fidp->fid);
3247 fa32ef88 Aneesh Kumar K.V
        goto out;
3248 fa32ef88 Aneesh Kumar K.V
    }
3249 fa32ef88 Aneesh Kumar K.V
    /*
3250 fa32ef88 Aneesh Kumar K.V
     * Read the xattr value
3251 fa32ef88 Aneesh Kumar K.V
     */
3252 fa32ef88 Aneesh Kumar K.V
    vs->xattr_fidp->fs.xattr.len = vs->size;
3253 fa32ef88 Aneesh Kumar K.V
    vs->xattr_fidp->fid_type = P9_FID_XATTR;
3254 fa32ef88 Aneesh Kumar K.V
    vs->xattr_fidp->fs.xattr.copied_len = -1;
3255 fa32ef88 Aneesh Kumar K.V
    if (vs->size) {
3256 fa32ef88 Aneesh Kumar K.V
        vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3257 fa32ef88 Aneesh Kumar K.V
        err = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path,
3258 fa32ef88 Aneesh Kumar K.V
                                &vs->name, vs->xattr_fidp->fs.xattr.value,
3259 fa32ef88 Aneesh Kumar K.V
                                vs->xattr_fidp->fs.xattr.len);
3260 fa32ef88 Aneesh Kumar K.V
    }
3261 fa32ef88 Aneesh Kumar K.V
    v9fs_post_xattr_getvalue(s, vs, err);
3262 fa32ef88 Aneesh Kumar K.V
    return;
3263 fa32ef88 Aneesh Kumar K.V
out:
3264 fa32ef88 Aneesh Kumar K.V
    complete_pdu(s, vs->pdu, err);
3265 fa32ef88 Aneesh Kumar K.V
    v9fs_string_free(&vs->name);
3266 fa32ef88 Aneesh Kumar K.V
    qemu_free(vs);
3267 fa32ef88 Aneesh Kumar K.V
}
3268 fa32ef88 Aneesh Kumar K.V
3269 fa32ef88 Aneesh Kumar K.V
static void v9fs_post_lxattr_getvalue(V9fsState *s,
3270 fa32ef88 Aneesh Kumar K.V
                                      V9fsXattrState *vs, int err)
3271 fa32ef88 Aneesh Kumar K.V
{
3272 fa32ef88 Aneesh Kumar K.V
    if (err < 0) {
3273 fa32ef88 Aneesh Kumar K.V
        err = -errno;
3274 fa32ef88 Aneesh Kumar K.V
        free_fid(s, vs->xattr_fidp->fid);
3275 fa32ef88 Aneesh Kumar K.V
        goto out;
3276 fa32ef88 Aneesh Kumar K.V
    }
3277 fa32ef88 Aneesh Kumar K.V
    vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size);
3278 fa32ef88 Aneesh Kumar K.V
    err = vs->offset;
3279 fa32ef88 Aneesh Kumar K.V
out:
3280 fa32ef88 Aneesh Kumar K.V
    complete_pdu(s, vs->pdu, err);
3281 fa32ef88 Aneesh Kumar K.V
    v9fs_string_free(&vs->name);
3282 fa32ef88 Aneesh Kumar K.V
    qemu_free(vs);
3283 fa32ef88 Aneesh Kumar K.V
    return;
3284 fa32ef88 Aneesh Kumar K.V
}
3285 fa32ef88 Aneesh Kumar K.V
3286 fa32ef88 Aneesh Kumar K.V
static void v9fs_post_lxattr_check(V9fsState *s,
3287 fa32ef88 Aneesh Kumar K.V
                                   V9fsXattrState *vs, ssize_t err)
3288 fa32ef88 Aneesh Kumar K.V
{
3289 fa32ef88 Aneesh Kumar K.V
    if (err < 0) {
3290 fa32ef88 Aneesh Kumar K.V
        err = -errno;
3291 fa32ef88 Aneesh Kumar K.V
        free_fid(s, vs->xattr_fidp->fid);
3292 fa32ef88 Aneesh Kumar K.V
        goto out;
3293 fa32ef88 Aneesh Kumar K.V
    }
3294 fa32ef88 Aneesh Kumar K.V
    /*
3295 fa32ef88 Aneesh Kumar K.V
     * Read the xattr value
3296 fa32ef88 Aneesh Kumar K.V
     */
3297 fa32ef88 Aneesh Kumar K.V
    vs->xattr_fidp->fs.xattr.len = vs->size;
3298 fa32ef88 Aneesh Kumar K.V
    vs->xattr_fidp->fid_type = P9_FID_XATTR;
3299 fa32ef88 Aneesh Kumar K.V
    vs->xattr_fidp->fs.xattr.copied_len = -1;
3300 fa32ef88 Aneesh Kumar K.V
    if (vs->size) {
3301 fa32ef88 Aneesh Kumar K.V
        vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3302 fa32ef88 Aneesh Kumar K.V
        err = v9fs_do_llistxattr(s, &vs->xattr_fidp->path,
3303 fa32ef88 Aneesh Kumar K.V
                                 vs->xattr_fidp->fs.xattr.value,
3304 fa32ef88 Aneesh Kumar K.V
                                 vs->xattr_fidp->fs.xattr.len);
3305 fa32ef88 Aneesh Kumar K.V
    }
3306 fa32ef88 Aneesh Kumar K.V
    v9fs_post_lxattr_getvalue(s, vs, err);
3307 fa32ef88 Aneesh Kumar K.V
    return;
3308 fa32ef88 Aneesh Kumar K.V
out:
3309 fa32ef88 Aneesh Kumar K.V
    complete_pdu(s, vs->pdu, err);
3310 fa32ef88 Aneesh Kumar K.V
    v9fs_string_free(&vs->name);
3311 fa32ef88 Aneesh Kumar K.V
    qemu_free(vs);
3312 fa32ef88 Aneesh Kumar K.V
}
3313 fa32ef88 Aneesh Kumar K.V
3314 fa32ef88 Aneesh Kumar K.V
static void v9fs_xattrwalk(V9fsState *s, V9fsPDU *pdu)
3315 fa32ef88 Aneesh Kumar K.V
{
3316 fa32ef88 Aneesh Kumar K.V
    ssize_t err = 0;
3317 fa32ef88 Aneesh Kumar K.V
    V9fsXattrState *vs;
3318 fa32ef88 Aneesh Kumar K.V
    int32_t fid, newfid;
3319 fa32ef88 Aneesh Kumar K.V
3320 fa32ef88 Aneesh Kumar K.V
    vs = qemu_malloc(sizeof(*vs));
3321 fa32ef88 Aneesh Kumar K.V
    vs->pdu = pdu;
3322 fa32ef88 Aneesh Kumar K.V
    vs->offset = 7;
3323 fa32ef88 Aneesh Kumar K.V
3324 fa32ef88 Aneesh Kumar K.V
    pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &newfid, &vs->name);
3325 fa32ef88 Aneesh Kumar K.V
    vs->file_fidp = lookup_fid(s, fid);
3326 fa32ef88 Aneesh Kumar K.V
    if (vs->file_fidp == NULL) {
3327 fa32ef88 Aneesh Kumar K.V
        err = -ENOENT;
3328 fa32ef88 Aneesh Kumar K.V
        goto out;
3329 fa32ef88 Aneesh Kumar K.V
    }
3330 fa32ef88 Aneesh Kumar K.V
3331 fa32ef88 Aneesh Kumar K.V
    vs->xattr_fidp = alloc_fid(s, newfid);
3332 fa32ef88 Aneesh Kumar K.V
    if (vs->xattr_fidp == NULL) {
3333 fa32ef88 Aneesh Kumar K.V
        err = -EINVAL;
3334 fa32ef88 Aneesh Kumar K.V
        goto out;
3335 fa32ef88 Aneesh Kumar K.V
    }
3336 fa32ef88 Aneesh Kumar K.V
3337 fa32ef88 Aneesh Kumar K.V
    v9fs_string_copy(&vs->xattr_fidp->path, &vs->file_fidp->path);
3338 fa32ef88 Aneesh Kumar K.V
    if (vs->name.data[0] == 0) {
3339 fa32ef88 Aneesh Kumar K.V
        /*
3340 fa32ef88 Aneesh Kumar K.V
         * listxattr request. Get the size first
3341 fa32ef88 Aneesh Kumar K.V
         */
3342 fa32ef88 Aneesh Kumar K.V
        vs->size = v9fs_do_llistxattr(s, &vs->xattr_fidp->path,
3343 fa32ef88 Aneesh Kumar K.V
                                      NULL, 0);
3344 fa32ef88 Aneesh Kumar K.V
        if (vs->size < 0) {
3345 fa32ef88 Aneesh Kumar K.V
            err = vs->size;
3346 fa32ef88 Aneesh Kumar K.V
        }
3347 fa32ef88 Aneesh Kumar K.V
        v9fs_post_lxattr_check(s, vs, err);
3348 fa32ef88 Aneesh Kumar K.V
        return;
3349 fa32ef88 Aneesh Kumar K.V
    } else {
3350 fa32ef88 Aneesh Kumar K.V
        /*
3351 fa32ef88 Aneesh Kumar K.V
         * specific xattr fid. We check for xattr
3352 fa32ef88 Aneesh Kumar K.V
         * presence also collect the xattr size
3353 fa32ef88 Aneesh Kumar K.V
         */
3354 fa32ef88 Aneesh Kumar K.V
        vs->size = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path,
3355 fa32ef88 Aneesh Kumar K.V
                                     &vs->name, NULL, 0);
3356 fa32ef88 Aneesh Kumar K.V
        if (vs->size < 0) {
3357 fa32ef88 Aneesh Kumar K.V
            err = vs->size;
3358 fa32ef88 Aneesh Kumar K.V
        }
3359 fa32ef88 Aneesh Kumar K.V
        v9fs_post_xattr_check(s, vs, err);
3360 fa32ef88 Aneesh Kumar K.V
        return;
3361 fa32ef88 Aneesh Kumar K.V
    }
3362 fa32ef88 Aneesh Kumar K.V
out:
3363 fa32ef88 Aneesh Kumar K.V
    complete_pdu(s, vs->pdu, err);
3364 fa32ef88 Aneesh Kumar K.V
    v9fs_string_free(&vs->name);
3365 fa32ef88 Aneesh Kumar K.V
    qemu_free(vs);
3366 fa32ef88 Aneesh Kumar K.V
}
3367 fa32ef88 Aneesh Kumar K.V
3368 10b468bd Aneesh Kumar K.V
static void v9fs_xattrcreate(V9fsState *s, V9fsPDU *pdu)
3369 10b468bd Aneesh Kumar K.V
{
3370 10b468bd Aneesh Kumar K.V
    int flags;
3371 10b468bd Aneesh Kumar K.V
    int32_t fid;
3372 10b468bd Aneesh Kumar K.V
    ssize_t err = 0;
3373 10b468bd Aneesh Kumar K.V
    V9fsXattrState *vs;
3374 10b468bd Aneesh Kumar K.V
3375 10b468bd Aneesh Kumar K.V
    vs = qemu_malloc(sizeof(*vs));
3376 10b468bd Aneesh Kumar K.V
    vs->pdu = pdu;
3377 10b468bd Aneesh Kumar K.V
    vs->offset = 7;
3378 10b468bd Aneesh Kumar K.V
3379 10b468bd Aneesh Kumar K.V
    pdu_unmarshal(vs->pdu, vs->offset, "dsqd",
3380 10b468bd Aneesh Kumar K.V
                  &fid, &vs->name, &vs->size, &flags);
3381 10b468bd Aneesh Kumar K.V
3382 10b468bd Aneesh Kumar K.V
    vs->file_fidp = lookup_fid(s, fid);
3383 10b468bd Aneesh Kumar K.V
    if (vs->file_fidp == NULL) {
3384 10b468bd Aneesh Kumar K.V
        err = -EINVAL;
3385 10b468bd Aneesh Kumar K.V
        goto out;
3386 10b468bd Aneesh Kumar K.V
    }
3387 10b468bd Aneesh Kumar K.V
3388 10b468bd Aneesh Kumar K.V
    /* Make the file fid point to xattr */
3389 10b468bd Aneesh Kumar K.V
    vs->xattr_fidp = vs->file_fidp;
3390 10b468bd Aneesh Kumar K.V
    vs->xattr_fidp->fid_type = P9_FID_XATTR;
3391 10b468bd Aneesh Kumar K.V
    vs->xattr_fidp->fs.xattr.copied_len = 0;
3392 10b468bd Aneesh Kumar K.V
    vs->xattr_fidp->fs.xattr.len = vs->size;
3393 10b468bd Aneesh Kumar K.V
    vs->xattr_fidp->fs.xattr.flags = flags;
3394 10b468bd Aneesh Kumar K.V
    v9fs_string_init(&vs->xattr_fidp->fs.xattr.name);
3395 10b468bd Aneesh Kumar K.V
    v9fs_string_copy(&vs->xattr_fidp->fs.xattr.name, &vs->name);
3396 10b468bd Aneesh Kumar K.V
    vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3397 10b468bd Aneesh Kumar K.V
3398 10b468bd Aneesh Kumar K.V
out:
3399 10b468bd Aneesh Kumar K.V
    complete_pdu(s, vs->pdu, err);
3400 10b468bd Aneesh Kumar K.V
    v9fs_string_free(&vs->name);
3401 10b468bd Aneesh Kumar K.V
    qemu_free(vs);
3402 10b468bd Aneesh Kumar K.V
}
3403 fa32ef88 Aneesh Kumar K.V
3404 9f107513 Anthony Liguori
typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
3405 9f107513 Anthony Liguori
3406 9f107513 Anthony Liguori
static pdu_handler_t *pdu_handlers[] = {
3407 c18e2f94 Sripathi Kodi
    [P9_TREADDIR] = v9fs_readdir,
3408 be940c87 M. Mohan Kumar
    [P9_TSTATFS] = v9fs_statfs,
3409 00ede4c2 Sripathi Kodi
    [P9_TGETATTR] = v9fs_getattr,
3410 c79ce737 Sripathi Kodi
    [P9_TSETATTR] = v9fs_setattr,
3411 fa32ef88 Aneesh Kumar K.V
    [P9_TXATTRWALK] = v9fs_xattrwalk,
3412 10b468bd Aneesh Kumar K.V
    [P9_TXATTRCREATE] = v9fs_xattrcreate,
3413 5268cecc M. Mohan Kumar
    [P9_TMKNOD] = v9fs_mknod,
3414 c7b4b0b3 M. Mohan Kumar
    [P9_TRENAME] = v9fs_rename,
3415 b67592ea M. Mohan Kumar
    [P9_TMKDIR] = v9fs_mkdir,
3416 9f107513 Anthony Liguori
    [P9_TVERSION] = v9fs_version,
3417 771e9d4c M. Mohan Kumar
    [P9_TLOPEN] = v9fs_open,
3418 9f107513 Anthony Liguori
    [P9_TATTACH] = v9fs_attach,
3419 9f107513 Anthony Liguori
    [P9_TSTAT] = v9fs_stat,
3420 9f107513 Anthony Liguori
    [P9_TWALK] = v9fs_walk,
3421 9f107513 Anthony Liguori
    [P9_TCLUNK] = v9fs_clunk,
3422 9f107513 Anthony Liguori
    [P9_TOPEN] = v9fs_open,
3423 9f107513 Anthony Liguori
    [P9_TREAD] = v9fs_read,
3424 9f107513 Anthony Liguori
#if 0
3425 9f107513 Anthony Liguori
    [P9_TAUTH] = v9fs_auth,
3426 9f107513 Anthony Liguori
#endif
3427 9f107513 Anthony Liguori
    [P9_TFLUSH] = v9fs_flush,
3428 b2c224be Venkateswararao Jujjuri (JV)
    [P9_TLINK] = v9fs_link,
3429 08c60fc9 Venkateswararao Jujjuri (JV)
    [P9_TSYMLINK] = v9fs_symlink,
3430 9f107513 Anthony Liguori
    [P9_TCREATE] = v9fs_create,
3431 c1568af5 Venkateswararao Jujjuri (JV)
    [P9_TLCREATE] = v9fs_lcreate,
3432 9f107513 Anthony Liguori
    [P9_TWRITE] = v9fs_write,
3433 9f107513 Anthony Liguori
    [P9_TWSTAT] = v9fs_wstat,
3434 9f107513 Anthony Liguori
    [P9_TREMOVE] = v9fs_remove,
3435 9f107513 Anthony Liguori
};
3436 9f107513 Anthony Liguori
3437 9f107513 Anthony Liguori
static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
3438 9f107513 Anthony Liguori
{
3439 9f107513 Anthony Liguori
    pdu_handler_t *handler;
3440 9f107513 Anthony Liguori
3441 9f107513 Anthony Liguori
    if (debug_9p_pdu) {
3442 9f107513 Anthony Liguori
        pprint_pdu(pdu);
3443 9f107513 Anthony Liguori
    }
3444 9f107513 Anthony Liguori
3445 9f107513 Anthony Liguori
    BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
3446 9f107513 Anthony Liguori
3447 9f107513 Anthony Liguori
    handler = pdu_handlers[pdu->id];
3448 9f107513 Anthony Liguori
    BUG_ON(handler == NULL);
3449 9f107513 Anthony Liguori
3450 9f107513 Anthony Liguori
    handler(s, pdu);
3451 9f107513 Anthony Liguori
}
3452 9f107513 Anthony Liguori
3453 9f107513 Anthony Liguori
static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
3454 9f107513 Anthony Liguori
{
3455 9f107513 Anthony Liguori
    V9fsState *s = (V9fsState *)vdev;
3456 9f107513 Anthony Liguori
    V9fsPDU *pdu;
3457 9f107513 Anthony Liguori
    ssize_t len;
3458 9f107513 Anthony Liguori
3459 9f107513 Anthony Liguori
    while ((pdu = alloc_pdu(s)) &&
3460 9f107513 Anthony Liguori
            (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
3461 9f107513 Anthony Liguori
        uint8_t *ptr;
3462 9f107513 Anthony Liguori
3463 9f107513 Anthony Liguori
        BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
3464 9f107513 Anthony Liguori
        BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
3465 9f107513 Anthony Liguori
3466 9f107513 Anthony Liguori
        ptr = pdu->elem.out_sg[0].iov_base;
3467 9f107513 Anthony Liguori
3468 9f107513 Anthony Liguori
        memcpy(&pdu->size, ptr, 4);
3469 9f107513 Anthony Liguori
        pdu->id = ptr[4];
3470 9f107513 Anthony Liguori
        memcpy(&pdu->tag, ptr + 5, 2);
3471 9f107513 Anthony Liguori
3472 9f107513 Anthony Liguori
        submit_pdu(s, pdu);
3473 9f107513 Anthony Liguori
    }
3474 9f107513 Anthony Liguori
3475 9f107513 Anthony Liguori
    free_pdu(s, pdu);
3476 9f107513 Anthony Liguori
}
3477 9f107513 Anthony Liguori
3478 9f107513 Anthony Liguori
static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
3479 9f107513 Anthony Liguori
{
3480 9f107513 Anthony Liguori
    features |= 1 << VIRTIO_9P_MOUNT_TAG;
3481 9f107513 Anthony Liguori
    return features;
3482 9f107513 Anthony Liguori
}
3483 9f107513 Anthony Liguori
3484 9f107513 Anthony Liguori
static V9fsState *to_virtio_9p(VirtIODevice *vdev)
3485 9f107513 Anthony Liguori
{
3486 9f107513 Anthony Liguori
    return (V9fsState *)vdev;
3487 9f107513 Anthony Liguori
}
3488 9f107513 Anthony Liguori
3489 9f107513 Anthony Liguori
static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
3490 9f107513 Anthony Liguori
{
3491 9f107513 Anthony Liguori
    struct virtio_9p_config *cfg;
3492 9f107513 Anthony Liguori
    V9fsState *s = to_virtio_9p(vdev);
3493 9f107513 Anthony Liguori
3494 9f107513 Anthony Liguori
    cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
3495 9f107513 Anthony Liguori
                        s->tag_len);
3496 9f107513 Anthony Liguori
    stw_raw(&cfg->tag_len, s->tag_len);
3497 9f107513 Anthony Liguori
    memcpy(cfg->tag, s->tag, s->tag_len);
3498 9f107513 Anthony Liguori
    memcpy(config, cfg, s->config_size);
3499 9f107513 Anthony Liguori
    qemu_free(cfg);
3500 9f107513 Anthony Liguori
}
3501 9f107513 Anthony Liguori
3502 9f107513 Anthony Liguori
VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
3503 9f107513 Anthony Liguori
 {
3504 9f107513 Anthony Liguori
    V9fsState *s;
3505 9f107513 Anthony Liguori
    int i, len;
3506 9f107513 Anthony Liguori
    struct stat stat;
3507 9f107513 Anthony Liguori
    FsTypeEntry *fse;
3508 9f107513 Anthony Liguori
3509 9f107513 Anthony Liguori
3510 9f107513 Anthony Liguori
    s = (V9fsState *)virtio_common_init("virtio-9p",
3511 9f107513 Anthony Liguori
                                    VIRTIO_ID_9P,
3512 9f107513 Anthony Liguori
                                    sizeof(struct virtio_9p_config)+
3513 9f107513 Anthony Liguori
                                    MAX_TAG_LEN,
3514 9f107513 Anthony Liguori
                                    sizeof(V9fsState));
3515 9f107513 Anthony Liguori
3516 9f107513 Anthony Liguori
    /* initialize pdu allocator */
3517 9f107513 Anthony Liguori
    QLIST_INIT(&s->free_list);
3518 9f107513 Anthony Liguori
    for (i = 0; i < (MAX_REQ - 1); i++) {
3519 9f107513 Anthony Liguori
        QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
3520 9f107513 Anthony Liguori
    }
3521 9f107513 Anthony Liguori
3522 9f107513 Anthony Liguori
    s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
3523 9f107513 Anthony Liguori
3524 9f107513 Anthony Liguori
    fse = get_fsdev_fsentry(conf->fsdev_id);
3525 9f107513 Anthony Liguori
3526 9f107513 Anthony Liguori
    if (!fse) {
3527 9f107513 Anthony Liguori
        /* We don't have a fsdev identified by fsdev_id */
3528 9f107513 Anthony Liguori
        fprintf(stderr, "Virtio-9p device couldn't find fsdev "
3529 9f107513 Anthony Liguori
                    "with the id %s\n", conf->fsdev_id);
3530 9f107513 Anthony Liguori
        exit(1);
3531 9f107513 Anthony Liguori
    }
3532 9f107513 Anthony Liguori
3533 9f107513 Anthony Liguori
    if (!fse->path || !conf->tag) {
3534 9f107513 Anthony Liguori
        /* we haven't specified a mount_tag or the path */
3535 9f107513 Anthony Liguori
        fprintf(stderr, "fsdev with id %s needs path "
3536 9f107513 Anthony Liguori
                "and Virtio-9p device needs mount_tag arguments\n",
3537 9f107513 Anthony Liguori
                conf->fsdev_id);
3538 9f107513 Anthony Liguori
        exit(1);
3539 9f107513 Anthony Liguori
    }
3540 9f107513 Anthony Liguori
3541 758e8e38 Venkateswararao Jujjuri (JV)
    if (!strcmp(fse->security_model, "passthrough")) {
3542 758e8e38 Venkateswararao Jujjuri (JV)
        /* Files on the Fileserver set to client user credentials */
3543 758e8e38 Venkateswararao Jujjuri (JV)
        s->ctx.fs_sm = SM_PASSTHROUGH;
3544 758e8e38 Venkateswararao Jujjuri (JV)
    } else if (!strcmp(fse->security_model, "mapped")) {
3545 758e8e38 Venkateswararao Jujjuri (JV)
        /* Files on the fileserver are set to QEMU credentials.
3546 758e8e38 Venkateswararao Jujjuri (JV)
         * Client user credentials are saved in extended attributes.
3547 758e8e38 Venkateswararao Jujjuri (JV)
         */
3548 758e8e38 Venkateswararao Jujjuri (JV)
        s->ctx.fs_sm = SM_MAPPED;
3549 758e8e38 Venkateswararao Jujjuri (JV)
    } else {
3550 9ce56db6 Venkateswararao Jujjuri (JV)
        /* user haven't specified a correct security option */
3551 9ce56db6 Venkateswararao Jujjuri (JV)
        fprintf(stderr, "one of the following must be specified as the"
3552 9ce56db6 Venkateswararao Jujjuri (JV)
                "security option:\n\t security_model=passthrough \n\t "
3553 9ce56db6 Venkateswararao Jujjuri (JV)
                "security_model=mapped\n");
3554 9ce56db6 Venkateswararao Jujjuri (JV)
        return NULL;
3555 9ce56db6 Venkateswararao Jujjuri (JV)
    }
3556 9ce56db6 Venkateswararao Jujjuri (JV)
3557 9f107513 Anthony Liguori
    if (lstat(fse->path, &stat)) {
3558 9f107513 Anthony Liguori
        fprintf(stderr, "share path %s does not exist\n", fse->path);
3559 9f107513 Anthony Liguori
        exit(1);
3560 9f107513 Anthony Liguori
    } else if (!S_ISDIR(stat.st_mode)) {
3561 9f107513 Anthony Liguori
        fprintf(stderr, "share path %s is not a directory \n", fse->path);
3562 9f107513 Anthony Liguori
        exit(1);
3563 9f107513 Anthony Liguori
    }
3564 9f107513 Anthony Liguori
3565 9f107513 Anthony Liguori
    s->ctx.fs_root = qemu_strdup(fse->path);
3566 9f107513 Anthony Liguori
    len = strlen(conf->tag);
3567 9f107513 Anthony Liguori
    if (len > MAX_TAG_LEN) {
3568 9f107513 Anthony Liguori
        len = MAX_TAG_LEN;
3569 9f107513 Anthony Liguori
    }
3570 9f107513 Anthony Liguori
    /* s->tag is non-NULL terminated string */
3571 9f107513 Anthony Liguori
    s->tag = qemu_malloc(len);
3572 9f107513 Anthony Liguori
    memcpy(s->tag, conf->tag, len);
3573 9f107513 Anthony Liguori
    s->tag_len = len;
3574 9f107513 Anthony Liguori
    s->ctx.uid = -1;
3575 9f107513 Anthony Liguori
3576 9f107513 Anthony Liguori
    s->ops = fse->ops;
3577 9f107513 Anthony Liguori
    s->vdev.get_features = virtio_9p_get_features;
3578 9f107513 Anthony Liguori
    s->config_size = sizeof(struct virtio_9p_config) +
3579 9f107513 Anthony Liguori
                        s->tag_len;
3580 9f107513 Anthony Liguori
    s->vdev.get_config = virtio_9p_get_config;
3581 9f107513 Anthony Liguori
3582 9f107513 Anthony Liguori
    return &s->vdev;
3583 9f107513 Anthony Liguori
}