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