Statistics
| Branch: | Revision:

root / hw / 9pfs / virtio-9p.c @ 5c3234c6

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