]> git.proxmox.com Git - qemu.git/blame - hw/9pfs/virtio-9p.c
virtio-9p: Move 9p device registration into virtio-9p.c
[qemu.git] / hw / 9pfs / virtio-9p.c
CommitLineData
9f107513
AL
1/*
2 * Virtio 9p backend
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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"
9fe1ebeb 17#include "virtio-pci.h"
9f107513
AL
18#include "virtio-9p.h"
19#include "fsdev/qemu-fsdev.h"
20#include "virtio-9p-debug.h"
fc22118d 21#include "virtio-9p-xattr.h"
9f107513 22
9f107513
AL
23int debug_9p_pdu;
24
fac4f111
VJJ
25enum {
26 Oread = 0x00,
27 Owrite = 0x01,
28 Ordwr = 0x02,
29 Oexec = 0x03,
30 Oexcl = 0x04,
31 Otrunc = 0x10,
32 Orexec = 0x20,
33 Orclose = 0x40,
34 Oappend = 0x80,
35};
36
37static int omode_to_uflags(int8_t mode)
38{
39 int ret = 0;
40
41 switch (mode & 3) {
42 case Oread:
43 ret = O_RDONLY;
44 break;
45 case Ordwr:
46 ret = O_RDWR;
47 break;
48 case Owrite:
49 ret = O_WRONLY;
50 break;
51 case Oexec:
52 ret = O_RDONLY;
53 break;
54 }
55
56 if (mode & Otrunc) {
57 ret |= O_TRUNC;
58 }
59
60 if (mode & Oappend) {
61 ret |= O_APPEND;
62 }
63
64 if (mode & Oexcl) {
65 ret |= O_EXCL;
66 }
67
68 return ret;
69}
70
758e8e38 71void cred_init(FsCred *credp)
131dcb25 72{
758e8e38
VJJ
73 credp->fc_uid = -1;
74 credp->fc_gid = -1;
75 credp->fc_mode = -1;
76 credp->fc_rdev = -1;
131dcb25
AL
77}
78
758e8e38 79static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf)
131dcb25 80{
758e8e38 81 return s->ops->lstat(&s->ctx, path->data, stbuf);
131dcb25
AL
82}
83
84static ssize_t v9fs_do_readlink(V9fsState *s, V9fsString *path, V9fsString *buf)
85{
86 ssize_t len;
87
88 buf->data = qemu_malloc(1024);
89
90 len = s->ops->readlink(&s->ctx, path->data, buf->data, 1024 - 1);
91 if (len > -1) {
92 buf->size = len;
93 buf->data[len] = 0;
94 }
95
96 return len;
97}
98
99static int v9fs_do_close(V9fsState *s, int fd)
100{
101 return s->ops->close(&s->ctx, fd);
102}
103
104static int v9fs_do_closedir(V9fsState *s, DIR *dir)
105{
106 return s->ops->closedir(&s->ctx, dir);
107}
108
a6568fe2
AL
109static int v9fs_do_open(V9fsState *s, V9fsString *path, int flags)
110{
111 return s->ops->open(&s->ctx, path->data, flags);
112}
113
114static DIR *v9fs_do_opendir(V9fsState *s, V9fsString *path)
115{
116 return s->ops->opendir(&s->ctx, path->data);
117}
118
a9231555
AL
119static void v9fs_do_rewinddir(V9fsState *s, DIR *dir)
120{
121 return s->ops->rewinddir(&s->ctx, dir);
122}
123
124static off_t v9fs_do_telldir(V9fsState *s, DIR *dir)
125{
126 return s->ops->telldir(&s->ctx, dir);
127}
128
129static struct dirent *v9fs_do_readdir(V9fsState *s, DIR *dir)
130{
131 return s->ops->readdir(&s->ctx, dir);
132}
133
134static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off)
135{
136 return s->ops->seekdir(&s->ctx, dir, off);
137}
138
56d15a53
SG
139static int v9fs_do_preadv(V9fsState *s, int fd, const struct iovec *iov,
140 int iovcnt, int64_t offset)
a9231555 141{
56d15a53 142 return s->ops->preadv(&s->ctx, fd, iov, iovcnt, offset);
a9231555
AL
143}
144
56d15a53
SG
145static int v9fs_do_pwritev(V9fsState *s, int fd, const struct iovec *iov,
146 int iovcnt, int64_t offset)
a9231555 147{
56d15a53 148 return s->ops->pwritev(&s->ctx, fd, iov, iovcnt, offset);
8449360c
AL
149}
150
c494dd6f
AL
151static int v9fs_do_chmod(V9fsState *s, V9fsString *path, mode_t mode)
152{
e95ead32
VJJ
153 FsCred cred;
154 cred_init(&cred);
155 cred.fc_mode = mode;
156 return s->ops->chmod(&s->ctx, path->data, &cred);
c494dd6f
AL
157}
158
5268cecc
MK
159static int v9fs_do_mknod(V9fsState *s, char *name,
160 mode_t mode, dev_t dev, uid_t uid, gid_t gid)
c494dd6f 161{
1c293312
VJJ
162 FsCred cred;
163 cred_init(&cred);
5268cecc
MK
164 cred.fc_uid = uid;
165 cred.fc_gid = gid;
1c293312
VJJ
166 cred.fc_mode = mode;
167 cred.fc_rdev = dev;
5268cecc 168 return s->ops->mknod(&s->ctx, name, &cred);
c494dd6f
AL
169}
170
b67592ea
MK
171static int v9fs_do_mkdir(V9fsState *s, char *name, mode_t mode,
172 uid_t uid, gid_t gid)
c494dd6f 173{
00ec5c37
VJJ
174 FsCred cred;
175
176 cred_init(&cred);
b67592ea
MK
177 cred.fc_uid = uid;
178 cred.fc_gid = gid;
179 cred.fc_mode = mode;
00ec5c37 180
b67592ea 181 return s->ops->mkdir(&s->ctx, name, &cred);
c494dd6f
AL
182}
183
184static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
185{
186 return s->ops->fstat(&s->ctx, fd, stbuf);
187}
188
c1568af5
VJJ
189static int v9fs_do_open2(V9fsState *s, char *fullname, uid_t uid, gid_t gid,
190 int flags, int mode)
c494dd6f 191{
4750a96f 192 FsCred cred;
4750a96f
VJJ
193
194 cred_init(&cred);
c1568af5
VJJ
195 cred.fc_uid = uid;
196 cred.fc_gid = gid;
197 cred.fc_mode = mode & 07777;
198 flags = flags;
4750a96f 199
c1568af5 200 return s->ops->open2(&s->ctx, fullname, flags, &cred);
c494dd6f
AL
201}
202
08c60fc9
VJJ
203static int v9fs_do_symlink(V9fsState *s, V9fsFidState *fidp,
204 const char *oldpath, const char *newpath, gid_t gid)
c494dd6f 205{
879c2813
VJJ
206 FsCred cred;
207 cred_init(&cred);
08c60fc9
VJJ
208 cred.fc_uid = fidp->uid;
209 cred.fc_gid = gid;
210 cred.fc_mode = 0777;
879c2813 211
08c60fc9 212 return s->ops->symlink(&s->ctx, oldpath, newpath, &cred);
c494dd6f
AL
213}
214
215static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
216{
217 return s->ops->link(&s->ctx, oldpath->data, newpath->data);
218}
219
8cf89e00
AL
220static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
221{
222 return s->ops->truncate(&s->ctx, path->data, size);
223}
224
225static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath,
226 V9fsString *newpath)
227{
228 return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
229}
230
231static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
232{
f7613bee
VJJ
233 FsCred cred;
234 cred_init(&cred);
235 cred.fc_uid = uid;
236 cred.fc_gid = gid;
237
238 return s->ops->chown(&s->ctx, path->data, &cred);
8cf89e00
AL
239}
240
8fc39ae4
SK
241static int v9fs_do_utimensat(V9fsState *s, V9fsString *path,
242 const struct timespec times[2])
8cf89e00 243{
8fc39ae4 244 return s->ops->utimensat(&s->ctx, path->data, times);
8cf89e00
AL
245}
246
5bae1900
AL
247static int v9fs_do_remove(V9fsState *s, V9fsString *path)
248{
249 return s->ops->remove(&s->ctx, path->data);
250}
251
49594973 252static int v9fs_do_fsync(V9fsState *s, int fd, int datasync)
8cf89e00 253{
49594973 254 return s->ops->fsync(&s->ctx, fd, datasync);
8cf89e00
AL
255}
256
5e94c103
MK
257static int v9fs_do_statfs(V9fsState *s, V9fsString *path, struct statfs *stbuf)
258{
259 return s->ops->statfs(&s->ctx, path->data, stbuf);
260}
261
fa32ef88
AK
262static ssize_t v9fs_do_lgetxattr(V9fsState *s, V9fsString *path,
263 V9fsString *xattr_name,
264 void *value, size_t size)
265{
266 return s->ops->lgetxattr(&s->ctx, path->data,
267 xattr_name->data, value, size);
268}
269
270static ssize_t v9fs_do_llistxattr(V9fsState *s, V9fsString *path,
271 void *value, size_t size)
272{
273 return s->ops->llistxattr(&s->ctx, path->data,
274 value, size);
275}
276
10b468bd
AK
277static int v9fs_do_lsetxattr(V9fsState *s, V9fsString *path,
278 V9fsString *xattr_name,
279 void *value, size_t size, int flags)
280{
281 return s->ops->lsetxattr(&s->ctx, path->data,
282 xattr_name->data, value, size, flags);
283}
284
9ed3ef26
AK
285static int v9fs_do_lremovexattr(V9fsState *s, V9fsString *path,
286 V9fsString *xattr_name)
287{
288 return s->ops->lremovexattr(&s->ctx, path->data,
289 xattr_name->data);
290}
291
292
a03f7874
AL
293static void v9fs_string_init(V9fsString *str)
294{
295 str->data = NULL;
296 str->size = 0;
297}
298
299static void v9fs_string_free(V9fsString *str)
300{
301 qemu_free(str->data);
302 str->data = NULL;
303 str->size = 0;
304}
305
306static void v9fs_string_null(V9fsString *str)
307{
308 v9fs_string_free(str);
309}
310
311static int number_to_string(void *arg, char type)
312{
313 unsigned int ret = 0;
314
315 switch (type) {
316 case 'u': {
317 unsigned int num = *(unsigned int *)arg;
318
319 do {
320 ret++;
321 num = num/10;
322 } while (num);
323 break;
324 }
45b23ff8
VJJ
325 case 'U': {
326 unsigned long num = *(unsigned long *)arg;
327 do {
328 ret++;
329 num = num/10;
330 } while (num);
331 break;
332 }
a03f7874
AL
333 default:
334 printf("Number_to_string: Unknown number format\n");
335 return -1;
336 }
337
338 return ret;
339}
340
c9ba47dc
SW
341static int GCC_FMT_ATTR(2, 0)
342v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
a03f7874
AL
343{
344 va_list ap2;
345 char *iter = (char *)fmt;
346 int len = 0;
347 int nr_args = 0;
348 char *arg_char_ptr;
349 unsigned int arg_uint;
45b23ff8 350 unsigned long arg_ulong;
a03f7874
AL
351
352 /* Find the number of %'s that denotes an argument */
353 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
354 nr_args++;
355 iter++;
356 }
357
358 len = strlen(fmt) - 2*nr_args;
359
360 if (!nr_args) {
361 goto alloc_print;
362 }
363
364 va_copy(ap2, ap);
365
366 iter = (char *)fmt;
367
368 /* Now parse the format string */
369 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
370 iter++;
371 switch (*iter) {
372 case 'u':
373 arg_uint = va_arg(ap2, unsigned int);
374 len += number_to_string((void *)&arg_uint, 'u');
375 break;
45b23ff8
VJJ
376 case 'l':
377 if (*++iter == 'u') {
378 arg_ulong = va_arg(ap2, unsigned long);
379 len += number_to_string((void *)&arg_ulong, 'U');
380 } else {
381 return -1;
382 }
383 break;
a03f7874
AL
384 case 's':
385 arg_char_ptr = va_arg(ap2, char *);
386 len += strlen(arg_char_ptr);
387 break;
388 case 'c':
389 len += 1;
390 break;
391 default:
392 fprintf(stderr,
393 "v9fs_string_alloc_printf:Incorrect format %c", *iter);
394 return -1;
395 }
396 iter++;
397 }
398
399alloc_print:
400 *strp = qemu_malloc((len + 1) * sizeof(**strp));
401
402 return vsprintf(*strp, fmt, ap);
403}
404
c9ba47dc
SW
405static void GCC_FMT_ATTR(2, 3)
406v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
a03f7874
AL
407{
408 va_list ap;
409 int err;
410
411 v9fs_string_free(str);
412
413 va_start(ap, fmt);
414 err = v9fs_string_alloc_printf(&str->data, fmt, ap);
415 BUG_ON(err == -1);
416 va_end(ap);
417
418 str->size = err;
419}
420
421static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
422{
423 v9fs_string_free(lhs);
424 v9fs_string_sprintf(lhs, "%s", rhs->data);
425}
426
936532a4
MN
427/*
428 * Return TRUE if s1 is an ancestor of s2.
429 *
430 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
431 * As a special case, We treat s1 as ancestor of s2 if they are same!
432 */
433static int v9fs_path_is_ancestor(V9fsString *s1, V9fsString *s2)
434{
435 if (!strncmp(s1->data, s2->data, s1->size)) {
436 if (s2->data[s1->size] == '\0' || s2->data[s1->size] == '/') {
437 return 1;
438 }
439 }
440 return 0;
441}
442
a03f7874
AL
443static size_t v9fs_string_size(V9fsString *str)
444{
445 return str->size;
446}
447
286d5652
AL
448static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
449{
450 V9fsFidState *f;
451
452 for (f = s->fid_list; f; f = f->next) {
453 if (f->fid == fid) {
286d5652
AL
454 return f;
455 }
456 }
457
458 return NULL;
459}
460
461static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
462{
463 V9fsFidState *f;
464
465 f = lookup_fid(s, fid);
466 if (f) {
467 return NULL;
468 }
469
470 f = qemu_mallocz(sizeof(V9fsFidState));
471
472 f->fid = fid;
d62dbb51 473 f->fid_type = P9_FID_NONE;
286d5652
AL
474
475 f->next = s->fid_list;
476 s->fid_list = f;
477
478 return f;
479}
480
10b468bd
AK
481static int v9fs_xattr_fid_clunk(V9fsState *s, V9fsFidState *fidp)
482{
483 int retval = 0;
484
485 if (fidp->fs.xattr.copied_len == -1) {
486 /* getxattr/listxattr fid */
487 goto free_value;
488 }
489 /*
490 * if this is fid for setxattr. clunk should
491 * result in setxattr localcall
492 */
493 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
494 /* clunk after partial write */
495 retval = -EINVAL;
496 goto free_out;
497 }
9ed3ef26
AK
498 if (fidp->fs.xattr.len) {
499 retval = v9fs_do_lsetxattr(s, &fidp->path, &fidp->fs.xattr.name,
500 fidp->fs.xattr.value,
501 fidp->fs.xattr.len,
502 fidp->fs.xattr.flags);
503 } else {
504 retval = v9fs_do_lremovexattr(s, &fidp->path, &fidp->fs.xattr.name);
505 }
10b468bd
AK
506free_out:
507 v9fs_string_free(&fidp->fs.xattr.name);
508free_value:
509 if (fidp->fs.xattr.value) {
510 qemu_free(fidp->fs.xattr.value);
511 }
512 return retval;
513}
514
286d5652
AL
515static int free_fid(V9fsState *s, int32_t fid)
516{
10b468bd 517 int retval = 0;
286d5652
AL
518 V9fsFidState **fidpp, *fidp;
519
520 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
521 if ((*fidpp)->fid == fid) {
522 break;
523 }
524 }
525
526 if (*fidpp == NULL) {
527 return -ENOENT;
528 }
529
530 fidp = *fidpp;
531 *fidpp = fidp->next;
532
d62dbb51
AK
533 if (fidp->fid_type == P9_FID_FILE) {
534 v9fs_do_close(s, fidp->fs.fd);
535 } else if (fidp->fid_type == P9_FID_DIR) {
536 v9fs_do_closedir(s, fidp->fs.dir);
537 } else if (fidp->fid_type == P9_FID_XATTR) {
10b468bd 538 retval = v9fs_xattr_fid_clunk(s, fidp);
286d5652
AL
539 }
540 v9fs_string_free(&fidp->path);
541 qemu_free(fidp);
542
10b468bd 543 return retval;
286d5652
AL
544}
545
546#define P9_QID_TYPE_DIR 0x80
547#define P9_QID_TYPE_SYMLINK 0x02
548
549#define P9_STAT_MODE_DIR 0x80000000
550#define P9_STAT_MODE_APPEND 0x40000000
551#define P9_STAT_MODE_EXCL 0x20000000
552#define P9_STAT_MODE_MOUNT 0x10000000
553#define P9_STAT_MODE_AUTH 0x08000000
554#define P9_STAT_MODE_TMP 0x04000000
555#define P9_STAT_MODE_SYMLINK 0x02000000
556#define P9_STAT_MODE_LINK 0x01000000
557#define P9_STAT_MODE_DEVICE 0x00800000
558#define P9_STAT_MODE_NAMED_PIPE 0x00200000
559#define P9_STAT_MODE_SOCKET 0x00100000
560#define P9_STAT_MODE_SETUID 0x00080000
561#define P9_STAT_MODE_SETGID 0x00040000
562#define P9_STAT_MODE_SETVTX 0x00010000
563
564#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
565 P9_STAT_MODE_SYMLINK | \
566 P9_STAT_MODE_LINK | \
567 P9_STAT_MODE_DEVICE | \
568 P9_STAT_MODE_NAMED_PIPE | \
569 P9_STAT_MODE_SOCKET)
570
571/* This is the algorithm from ufs in spfs */
572static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
573{
574 size_t size;
575
576 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
577 memcpy(&qidp->path, &stbuf->st_ino, size);
578 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
579 qidp->type = 0;
580 if (S_ISDIR(stbuf->st_mode)) {
581 qidp->type |= P9_QID_TYPE_DIR;
582 }
583 if (S_ISLNK(stbuf->st_mode)) {
584 qidp->type |= P9_QID_TYPE_SYMLINK;
585 }
586}
587
588static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
589{
590 struct stat stbuf;
591 int err;
592
593 err = v9fs_do_lstat(s, &fidp->path, &stbuf);
594 if (err) {
595 return err;
596 }
597
598 stat_to_qid(&stbuf, qidp);
599 return 0;
600}
601
9f107513
AL
602static V9fsPDU *alloc_pdu(V9fsState *s)
603{
604 V9fsPDU *pdu = NULL;
605
606 if (!QLIST_EMPTY(&s->free_list)) {
607 pdu = QLIST_FIRST(&s->free_list);
608 QLIST_REMOVE(pdu, next);
609 }
610 return pdu;
611}
612
613static void free_pdu(V9fsState *s, V9fsPDU *pdu)
614{
615 if (pdu) {
39792515
AK
616 if (debug_9p_pdu) {
617 pprint_pdu(pdu);
618 }
619 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
9f107513
AL
620 }
621}
622
623size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
624 size_t offset, size_t size, int pack)
625{
626 int i = 0;
627 size_t copied = 0;
628
629 for (i = 0; size && i < sg_count; i++) {
630 size_t len;
631 if (offset >= sg[i].iov_len) {
632 /* skip this sg */
633 offset -= sg[i].iov_len;
634 continue;
635 } else {
636 len = MIN(sg[i].iov_len - offset, size);
637 if (pack) {
638 memcpy(sg[i].iov_base + offset, addr, len);
639 } else {
640 memcpy(addr, sg[i].iov_base + offset, len);
641 }
642 size -= len;
643 copied += len;
644 addr += len;
645 if (size) {
646 offset = 0;
647 continue;
648 }
649 }
650 }
651
652 return copied;
653}
654
405a549a
AL
655static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
656{
657 return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
658 offset, size, 0);
659}
660
661static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
662 size_t size)
663{
664 return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
665 offset, size, 1);
666}
667
668static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
669{
670 size_t pos = 0;
671 int i, j;
672 struct iovec *src_sg;
673 unsigned int num;
674
675 if (rx) {
676 src_sg = pdu->elem.in_sg;
677 num = pdu->elem.in_num;
678 } else {
679 src_sg = pdu->elem.out_sg;
680 num = pdu->elem.out_num;
681 }
682
683 j = 0;
684 for (i = 0; i < num; i++) {
685 if (offset <= pos) {
686 sg[j].iov_base = src_sg[i].iov_base;
687 sg[j].iov_len = src_sg[i].iov_len;
688 j++;
689 } else if (offset < (src_sg[i].iov_len + pos)) {
690 sg[j].iov_base = src_sg[i].iov_base;
691 sg[j].iov_len = src_sg[i].iov_len;
692 sg[j].iov_base += (offset - pos);
693 sg[j].iov_len -= (offset - pos);
694 j++;
695 }
696 pos += src_sg[i].iov_len;
697 }
698
699 return j;
700}
701
702static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
703{
704 size_t old_offset = offset;
705 va_list ap;
706 int i;
707
708 va_start(ap, fmt);
709 for (i = 0; fmt[i]; i++) {
710 switch (fmt[i]) {
711 case 'b': {
712 uint8_t *valp = va_arg(ap, uint8_t *);
713 offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
714 break;
715 }
716 case 'w': {
717 uint16_t val, *valp;
718 valp = va_arg(ap, uint16_t *);
405a549a 719 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
4e37bfc1 720 *valp = le16_to_cpu(val);
405a549a
AL
721 break;
722 }
723 case 'd': {
724 uint32_t val, *valp;
725 valp = va_arg(ap, uint32_t *);
405a549a 726 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
4e37bfc1 727 *valp = le32_to_cpu(val);
405a549a
AL
728 break;
729 }
730 case 'q': {
731 uint64_t val, *valp;
732 valp = va_arg(ap, uint64_t *);
405a549a 733 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
4e37bfc1 734 *valp = le64_to_cpu(val);
405a549a
AL
735 break;
736 }
737 case 'v': {
738 struct iovec *iov = va_arg(ap, struct iovec *);
739 int *iovcnt = va_arg(ap, int *);
740 *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
741 break;
742 }
743 case 's': {
744 V9fsString *str = va_arg(ap, V9fsString *);
745 offset += pdu_unmarshal(pdu, offset, "w", &str->size);
746 /* FIXME: sanity check str->size */
747 str->data = qemu_malloc(str->size + 1);
748 offset += pdu_unpack(str->data, pdu, offset, str->size);
749 str->data[str->size] = 0;
750 break;
751 }
752 case 'Q': {
753 V9fsQID *qidp = va_arg(ap, V9fsQID *);
754 offset += pdu_unmarshal(pdu, offset, "bdq",
755 &qidp->type, &qidp->version, &qidp->path);
756 break;
757 }
758 case 'S': {
759 V9fsStat *statp = va_arg(ap, V9fsStat *);
760 offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
761 &statp->size, &statp->type, &statp->dev,
762 &statp->qid, &statp->mode, &statp->atime,
763 &statp->mtime, &statp->length,
764 &statp->name, &statp->uid, &statp->gid,
765 &statp->muid, &statp->extension,
766 &statp->n_uid, &statp->n_gid,
767 &statp->n_muid);
768 break;
769 }
c79ce737
SK
770 case 'I': {
771 V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
772 offset += pdu_unmarshal(pdu, offset, "ddddqqqqq",
773 &iattr->valid, &iattr->mode,
774 &iattr->uid, &iattr->gid, &iattr->size,
775 &iattr->atime_sec, &iattr->atime_nsec,
776 &iattr->mtime_sec, &iattr->mtime_nsec);
777 break;
778 }
405a549a
AL
779 default:
780 break;
781 }
782 }
783
784 va_end(ap);
785
786 return offset - old_offset;
787}
788
789static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
790{
791 size_t old_offset = offset;
792 va_list ap;
793 int i;
794
795 va_start(ap, fmt);
796 for (i = 0; fmt[i]; i++) {
797 switch (fmt[i]) {
798 case 'b': {
799 uint8_t val = va_arg(ap, int);
800 offset += pdu_pack(pdu, offset, &val, sizeof(val));
801 break;
802 }
803 case 'w': {
804 uint16_t val;
805 cpu_to_le16w(&val, va_arg(ap, int));
806 offset += pdu_pack(pdu, offset, &val, sizeof(val));
807 break;
808 }
809 case 'd': {
810 uint32_t val;
811 cpu_to_le32w(&val, va_arg(ap, uint32_t));
812 offset += pdu_pack(pdu, offset, &val, sizeof(val));
813 break;
814 }
815 case 'q': {
816 uint64_t val;
817 cpu_to_le64w(&val, va_arg(ap, uint64_t));
818 offset += pdu_pack(pdu, offset, &val, sizeof(val));
819 break;
820 }
821 case 'v': {
822 struct iovec *iov = va_arg(ap, struct iovec *);
823 int *iovcnt = va_arg(ap, int *);
824 *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
825 break;
826 }
827 case 's': {
828 V9fsString *str = va_arg(ap, V9fsString *);
829 offset += pdu_marshal(pdu, offset, "w", str->size);
830 offset += pdu_pack(pdu, offset, str->data, str->size);
831 break;
832 }
833 case 'Q': {
834 V9fsQID *qidp = va_arg(ap, V9fsQID *);
835 offset += pdu_marshal(pdu, offset, "bdq",
836 qidp->type, qidp->version, qidp->path);
837 break;
838 }
839 case 'S': {
840 V9fsStat *statp = va_arg(ap, V9fsStat *);
841 offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
842 statp->size, statp->type, statp->dev,
843 &statp->qid, statp->mode, statp->atime,
844 statp->mtime, statp->length, &statp->name,
845 &statp->uid, &statp->gid, &statp->muid,
846 &statp->extension, statp->n_uid,
847 statp->n_gid, statp->n_muid);
848 break;
849 }
00ede4c2
SK
850 case 'A': {
851 V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
852 offset += pdu_marshal(pdu, offset, "qQdddqqqqqqqqqqqqqqq",
853 statp->st_result_mask,
854 &statp->qid, statp->st_mode,
855 statp->st_uid, statp->st_gid,
856 statp->st_nlink, statp->st_rdev,
857 statp->st_size, statp->st_blksize, statp->st_blocks,
858 statp->st_atime_sec, statp->st_atime_nsec,
859 statp->st_mtime_sec, statp->st_mtime_nsec,
860 statp->st_ctime_sec, statp->st_ctime_nsec,
861 statp->st_btime_sec, statp->st_btime_nsec,
862 statp->st_gen, statp->st_data_version);
863 break;
864 }
405a549a
AL
865 default:
866 break;
867 }
868 }
869 va_end(ap);
870
871 return offset - old_offset;
872}
873
874static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
875{
876 int8_t id = pdu->id + 1; /* Response */
877
878 if (len < 0) {
405a549a 879 int err = -len;
8f4d1ca5 880 len = 7;
405a549a 881
8f4d1ca5
AB
882 if (s->proto_version != V9FS_PROTO_2000L) {
883 V9fsString str;
884
885 str.data = strerror(err);
886 str.size = strlen(str.data);
887
888 len += pdu_marshal(pdu, len, "s", &str);
889 id = P9_RERROR;
890 }
405a549a 891
cf03eb2c 892 len += pdu_marshal(pdu, len, "d", err);
405a549a 893
8f4d1ca5
AB
894 if (s->proto_version == V9FS_PROTO_2000L) {
895 id = P9_RLERROR;
896 }
405a549a
AL
897 }
898
899 /* fill out the header */
900 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
901
902 /* keep these in sync */
903 pdu->size = len;
904 pdu->id = id;
905
906 /* push onto queue and notify */
907 virtqueue_push(s->vq, &pdu->elem, len);
908
909 /* FIXME: we should batch these completions */
910 virtio_notify(&s->vdev, s->vq);
911
912 free_pdu(s, pdu);
913}
914
bb9e3216
AL
915static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
916{
917 mode_t ret;
918
919 ret = mode & 0777;
920 if (mode & P9_STAT_MODE_DIR) {
921 ret |= S_IFDIR;
922 }
923
cf03eb2c
AB
924 if (mode & P9_STAT_MODE_SYMLINK) {
925 ret |= S_IFLNK;
926 }
927 if (mode & P9_STAT_MODE_SOCKET) {
928 ret |= S_IFSOCK;
929 }
930 if (mode & P9_STAT_MODE_NAMED_PIPE) {
931 ret |= S_IFIFO;
932 }
933 if (mode & P9_STAT_MODE_DEVICE) {
934 if (extension && extension->data[0] == 'c') {
935 ret |= S_IFCHR;
936 } else {
937 ret |= S_IFBLK;
bb9e3216
AL
938 }
939 }
940
941 if (!(ret&~0777)) {
942 ret |= S_IFREG;
943 }
944
945 if (mode & P9_STAT_MODE_SETUID) {
946 ret |= S_ISUID;
947 }
948 if (mode & P9_STAT_MODE_SETGID) {
949 ret |= S_ISGID;
950 }
951 if (mode & P9_STAT_MODE_SETVTX) {
952 ret |= S_ISVTX;
953 }
954
955 return ret;
956}
957
958static int donttouch_stat(V9fsStat *stat)
959{
960 if (stat->type == -1 &&
961 stat->dev == -1 &&
962 stat->qid.type == -1 &&
963 stat->qid.version == -1 &&
964 stat->qid.path == -1 &&
965 stat->mode == -1 &&
966 stat->atime == -1 &&
967 stat->mtime == -1 &&
968 stat->length == -1 &&
969 !stat->name.size &&
970 !stat->uid.size &&
971 !stat->gid.size &&
972 !stat->muid.size &&
973 stat->n_uid == -1 &&
974 stat->n_gid == -1 &&
975 stat->n_muid == -1) {
976 return 1;
977 }
978
979 return 0;
980}
981
982static void v9fs_stat_free(V9fsStat *stat)
983{
984 v9fs_string_free(&stat->name);
985 v9fs_string_free(&stat->uid);
986 v9fs_string_free(&stat->gid);
987 v9fs_string_free(&stat->muid);
988 v9fs_string_free(&stat->extension);
989}
990
991static uint32_t stat_to_v9mode(const struct stat *stbuf)
992{
993 uint32_t mode;
994
995 mode = stbuf->st_mode & 0777;
996 if (S_ISDIR(stbuf->st_mode)) {
997 mode |= P9_STAT_MODE_DIR;
998 }
999
cf03eb2c
AB
1000 if (S_ISLNK(stbuf->st_mode)) {
1001 mode |= P9_STAT_MODE_SYMLINK;
1002 }
bb9e3216 1003
cf03eb2c
AB
1004 if (S_ISSOCK(stbuf->st_mode)) {
1005 mode |= P9_STAT_MODE_SOCKET;
1006 }
bb9e3216 1007
cf03eb2c
AB
1008 if (S_ISFIFO(stbuf->st_mode)) {
1009 mode |= P9_STAT_MODE_NAMED_PIPE;
1010 }
bb9e3216 1011
cf03eb2c
AB
1012 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
1013 mode |= P9_STAT_MODE_DEVICE;
1014 }
bb9e3216 1015
cf03eb2c
AB
1016 if (stbuf->st_mode & S_ISUID) {
1017 mode |= P9_STAT_MODE_SETUID;
1018 }
bb9e3216 1019
cf03eb2c
AB
1020 if (stbuf->st_mode & S_ISGID) {
1021 mode |= P9_STAT_MODE_SETGID;
1022 }
bb9e3216 1023
cf03eb2c
AB
1024 if (stbuf->st_mode & S_ISVTX) {
1025 mode |= P9_STAT_MODE_SETVTX;
bb9e3216
AL
1026 }
1027
1028 return mode;
1029}
1030
1031static int stat_to_v9stat(V9fsState *s, V9fsString *name,
1032 const struct stat *stbuf,
1033 V9fsStat *v9stat)
1034{
1035 int err;
1036 const char *str;
1037
1038 memset(v9stat, 0, sizeof(*v9stat));
1039
1040 stat_to_qid(stbuf, &v9stat->qid);
1041 v9stat->mode = stat_to_v9mode(stbuf);
1042 v9stat->atime = stbuf->st_atime;
1043 v9stat->mtime = stbuf->st_mtime;
1044 v9stat->length = stbuf->st_size;
1045
1046 v9fs_string_null(&v9stat->uid);
1047 v9fs_string_null(&v9stat->gid);
1048 v9fs_string_null(&v9stat->muid);
1049
cf03eb2c
AB
1050 v9stat->n_uid = stbuf->st_uid;
1051 v9stat->n_gid = stbuf->st_gid;
1052 v9stat->n_muid = 0;
bb9e3216 1053
cf03eb2c 1054 v9fs_string_null(&v9stat->extension);
bb9e3216 1055
cf03eb2c
AB
1056 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
1057 err = v9fs_do_readlink(s, name, &v9stat->extension);
1058 if (err == -1) {
1059 err = -errno;
1060 return err;
bb9e3216 1061 }
cf03eb2c
AB
1062 v9stat->extension.data[err] = 0;
1063 v9stat->extension.size = err;
1064 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
1065 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
1066 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
1067 major(stbuf->st_rdev), minor(stbuf->st_rdev));
1068 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
c9ba47dc
SW
1069 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
1070 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
bb9e3216
AL
1071 }
1072
1073 str = strrchr(name->data, '/');
1074 if (str) {
1075 str += 1;
1076 } else {
1077 str = name->data;
1078 }
1079
1080 v9fs_string_sprintf(&v9stat->name, "%s", str);
1081
1082 v9stat->size = 61 +
1083 v9fs_string_size(&v9stat->name) +
1084 v9fs_string_size(&v9stat->uid) +
1085 v9fs_string_size(&v9stat->gid) +
1086 v9fs_string_size(&v9stat->muid) +
1087 v9fs_string_size(&v9stat->extension);
1088 return 0;
1089}
1090
00ede4c2
SK
1091#define P9_STATS_MODE 0x00000001ULL
1092#define P9_STATS_NLINK 0x00000002ULL
1093#define P9_STATS_UID 0x00000004ULL
1094#define P9_STATS_GID 0x00000008ULL
1095#define P9_STATS_RDEV 0x00000010ULL
1096#define P9_STATS_ATIME 0x00000020ULL
1097#define P9_STATS_MTIME 0x00000040ULL
1098#define P9_STATS_CTIME 0x00000080ULL
1099#define P9_STATS_INO 0x00000100ULL
1100#define P9_STATS_SIZE 0x00000200ULL
1101#define P9_STATS_BLOCKS 0x00000400ULL
1102
1103#define P9_STATS_BTIME 0x00000800ULL
1104#define P9_STATS_GEN 0x00001000ULL
1105#define P9_STATS_DATA_VERSION 0x00002000ULL
1106
1107#define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1108#define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1109
1110
1111static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
1112 V9fsStatDotl *v9lstat)
1113{
1114 memset(v9lstat, 0, sizeof(*v9lstat));
1115
1116 v9lstat->st_mode = stbuf->st_mode;
1117 v9lstat->st_nlink = stbuf->st_nlink;
1118 v9lstat->st_uid = stbuf->st_uid;
1119 v9lstat->st_gid = stbuf->st_gid;
1120 v9lstat->st_rdev = stbuf->st_rdev;
1121 v9lstat->st_size = stbuf->st_size;
1122 v9lstat->st_blksize = stbuf->st_blksize;
1123 v9lstat->st_blocks = stbuf->st_blocks;
1124 v9lstat->st_atime_sec = stbuf->st_atime;
1125 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1126 v9lstat->st_mtime_sec = stbuf->st_mtime;
1127 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
1128 v9lstat->st_ctime_sec = stbuf->st_ctime;
1129 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1130 /* Currently we only support BASIC fields in stat */
1131 v9lstat->st_result_mask = P9_STATS_BASIC;
1132
1133 stat_to_qid(stbuf, &v9lstat->qid);
1134}
1135
1f5a89bf
AL
1136static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
1137{
1138 while (len && *iovcnt) {
1139 if (len < sg->iov_len) {
1140 sg->iov_len -= len;
1141 sg->iov_base += len;
1142 len = 0;
1143 } else {
1144 len -= sg->iov_len;
1145 sg++;
1146 *iovcnt -= 1;
1147 }
1148 }
1149
1150 return sg;
1151}
1152
1153static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
1154{
1155 int i;
1156 int total = 0;
1157
1158 for (i = 0; i < *cnt; i++) {
1159 if ((total + sg[i].iov_len) > cap) {
1160 sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
1161 i++;
1162 break;
1163 }
1164 total += sg[i].iov_len;
1165 }
1166
1167 *cnt = i;
1168
1169 return sg;
1170}
1171
1172static void print_sg(struct iovec *sg, int cnt)
1173{
1174 int i;
1175
1176 printf("sg[%d]: {", cnt);
1177 for (i = 0; i < cnt; i++) {
1178 if (i) {
1179 printf(", ");
1180 }
1181 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1182 }
1183 printf("}\n");
1184}
1185
8cf89e00
AL
1186static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
1187{
1188 V9fsString str;
1189 v9fs_string_init(&str);
1190 v9fs_string_copy(&str, dst);
1191 v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
1192 v9fs_string_free(&str);
1193}
1194
9f107513
AL
1195static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
1196{
92c1ad03
AL
1197 V9fsString version;
1198 size_t offset = 7;
1199
5e94c103 1200 pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
92c1ad03 1201
84151514
MK
1202 if (!strcmp(version.data, "9P2000.u")) {
1203 s->proto_version = V9FS_PROTO_2000U;
1204 } else if (!strcmp(version.data, "9P2000.L")) {
1205 s->proto_version = V9FS_PROTO_2000L;
1206 } else {
92c1ad03 1207 v9fs_string_sprintf(&version, "unknown");
9f107513 1208 }
92c1ad03 1209
5e94c103 1210 offset += pdu_marshal(pdu, offset, "ds", s->msize, &version);
92c1ad03
AL
1211 complete_pdu(s, pdu, offset);
1212
1213 v9fs_string_free(&version);
9f107513
AL
1214}
1215
1216static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
1217{
955efc47
AL
1218 int32_t fid, afid, n_uname;
1219 V9fsString uname, aname;
1220 V9fsFidState *fidp;
1221 V9fsQID qid;
1222 size_t offset = 7;
1223 ssize_t err;
1224
1225 pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1226
1227 fidp = alloc_fid(s, fid);
1228 if (fidp == NULL) {
1229 err = -EINVAL;
1230 goto out;
9f107513 1231 }
955efc47
AL
1232
1233 fidp->uid = n_uname;
1234
1235 v9fs_string_sprintf(&fidp->path, "%s", "/");
1236 err = fid_to_qid(s, fidp, &qid);
1237 if (err) {
1238 err = -EINVAL;
1239 free_fid(s, fid);
1240 goto out;
1241 }
1242
1243 offset += pdu_marshal(pdu, offset, "Q", &qid);
1244
1245 err = offset;
1246out:
1247 complete_pdu(s, pdu, err);
1248 v9fs_string_free(&uname);
1249 v9fs_string_free(&aname);
9f107513
AL
1250}
1251
4da7d3fa
AL
1252static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1253{
1254 if (err == -1) {
1255 err = -errno;
1256 goto out;
1257 }
1258
1259 err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1260 if (err) {
1261 goto out;
1262 }
1263 vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1264 err = vs->offset;
1265
1266out:
1267 complete_pdu(s, vs->pdu, err);
1268 v9fs_stat_free(&vs->v9stat);
1269 qemu_free(vs);
1270}
1271
9f107513
AL
1272static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
1273{
4da7d3fa
AL
1274 int32_t fid;
1275 V9fsStatState *vs;
1276 ssize_t err = 0;
1277
1278 vs = qemu_malloc(sizeof(*vs));
1279 vs->pdu = pdu;
1280 vs->offset = 7;
1281
1282 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1283
1284 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1285
1286 vs->fidp = lookup_fid(s, fid);
1287 if (vs->fidp == NULL) {
1288 err = -ENOENT;
1289 goto out;
9f107513 1290 }
4da7d3fa
AL
1291
1292 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1293 v9fs_stat_post_lstat(s, vs, err);
1294 return;
1295
1296out:
1297 complete_pdu(s, vs->pdu, err);
1298 v9fs_stat_free(&vs->v9stat);
1299 qemu_free(vs);
9f107513
AL
1300}
1301
00ede4c2
SK
1302static void v9fs_getattr_post_lstat(V9fsState *s, V9fsStatStateDotl *vs,
1303 int err)
1304{
1305 if (err == -1) {
1306 err = -errno;
1307 goto out;
1308 }
1309
1310 stat_to_v9stat_dotl(s, &vs->stbuf, &vs->v9stat_dotl);
1311 vs->offset += pdu_marshal(vs->pdu, vs->offset, "A", &vs->v9stat_dotl);
1312 err = vs->offset;
1313
1314out:
1315 complete_pdu(s, vs->pdu, err);
1316 qemu_free(vs);
1317}
1318
1319static void v9fs_getattr(V9fsState *s, V9fsPDU *pdu)
1320{
1321 int32_t fid;
1322 V9fsStatStateDotl *vs;
1323 ssize_t err = 0;
1324 V9fsFidState *fidp;
1325 uint64_t request_mask;
1326
1327 vs = qemu_malloc(sizeof(*vs));
1328 vs->pdu = pdu;
1329 vs->offset = 7;
1330
1331 memset(&vs->v9stat_dotl, 0, sizeof(vs->v9stat_dotl));
1332
1333 pdu_unmarshal(vs->pdu, vs->offset, "dq", &fid, &request_mask);
1334
1335 fidp = lookup_fid(s, fid);
1336 if (fidp == NULL) {
1337 err = -ENOENT;
1338 goto out;
1339 }
1340
1341 /* Currently we only support BASIC fields in stat, so there is no
1342 * need to look at request_mask.
1343 */
1344 err = v9fs_do_lstat(s, &fidp->path, &vs->stbuf);
1345 v9fs_getattr_post_lstat(s, vs, err);
1346 return;
1347
1348out:
1349 complete_pdu(s, vs->pdu, err);
1350 qemu_free(vs);
1351}
1352
c79ce737
SK
1353/* From Linux kernel code */
1354#define ATTR_MODE (1 << 0)
1355#define ATTR_UID (1 << 1)
1356#define ATTR_GID (1 << 2)
1357#define ATTR_SIZE (1 << 3)
1358#define ATTR_ATIME (1 << 4)
1359#define ATTR_MTIME (1 << 5)
1360#define ATTR_CTIME (1 << 6)
1361#define ATTR_MASK 127
1362#define ATTR_ATIME_SET (1 << 7)
1363#define ATTR_MTIME_SET (1 << 8)
1364
1365static void v9fs_setattr_post_truncate(V9fsState *s, V9fsSetattrState *vs,
1366 int err)
1367{
1368 if (err == -1) {
1369 err = -errno;
1370 goto out;
1371 }
1372 err = vs->offset;
1373
1374out:
1375 complete_pdu(s, vs->pdu, err);
1376 qemu_free(vs);
1377}
1378
1379static void v9fs_setattr_post_chown(V9fsState *s, V9fsSetattrState *vs, int err)
1380{
1381 if (err == -1) {
1382 err = -errno;
1383 goto out;
1384 }
1385
1386 if (vs->v9iattr.valid & (ATTR_SIZE)) {
1387 err = v9fs_do_truncate(s, &vs->fidp->path, vs->v9iattr.size);
1388 }
1389 v9fs_setattr_post_truncate(s, vs, err);
1390 return;
1391
1392out:
1393 complete_pdu(s, vs->pdu, err);
1394 qemu_free(vs);
1395}
1396
1397static void v9fs_setattr_post_utimensat(V9fsState *s, V9fsSetattrState *vs,
1398 int err)
1399{
1400 if (err == -1) {
1401 err = -errno;
1402 goto out;
1403 }
1404
1405 /* If the only valid entry in iattr is ctime we can call
1406 * chown(-1,-1) to update the ctime of the file
1407 */
1408 if ((vs->v9iattr.valid & (ATTR_UID | ATTR_GID)) ||
1409 ((vs->v9iattr.valid & ATTR_CTIME)
1410 && !((vs->v9iattr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
1411 if (!(vs->v9iattr.valid & ATTR_UID)) {
1412 vs->v9iattr.uid = -1;
1413 }
1414 if (!(vs->v9iattr.valid & ATTR_GID)) {
1415 vs->v9iattr.gid = -1;
1416 }
1417 err = v9fs_do_chown(s, &vs->fidp->path, vs->v9iattr.uid,
1418 vs->v9iattr.gid);
1419 }
1420 v9fs_setattr_post_chown(s, vs, err);
1421 return;
1422
1423out:
1424 complete_pdu(s, vs->pdu, err);
1425 qemu_free(vs);
1426}
1427
1428static void v9fs_setattr_post_chmod(V9fsState *s, V9fsSetattrState *vs, int err)
1429{
1430 if (err == -1) {
1431 err = -errno;
1432 goto out;
1433 }
1434
1435 if (vs->v9iattr.valid & (ATTR_ATIME | ATTR_MTIME)) {
1436 struct timespec times[2];
1437 if (vs->v9iattr.valid & ATTR_ATIME) {
1438 if (vs->v9iattr.valid & ATTR_ATIME_SET) {
1439 times[0].tv_sec = vs->v9iattr.atime_sec;
1440 times[0].tv_nsec = vs->v9iattr.atime_nsec;
1441 } else {
1442 times[0].tv_nsec = UTIME_NOW;
1443 }
1444 } else {
1445 times[0].tv_nsec = UTIME_OMIT;
1446 }
1447
1448 if (vs->v9iattr.valid & ATTR_MTIME) {
1449 if (vs->v9iattr.valid & ATTR_MTIME_SET) {
1450 times[1].tv_sec = vs->v9iattr.mtime_sec;
1451 times[1].tv_nsec = vs->v9iattr.mtime_nsec;
1452 } else {
1453 times[1].tv_nsec = UTIME_NOW;
1454 }
1455 } else {
1456 times[1].tv_nsec = UTIME_OMIT;
1457 }
1458 err = v9fs_do_utimensat(s, &vs->fidp->path, times);
1459 }
1460 v9fs_setattr_post_utimensat(s, vs, err);
1461 return;
1462
1463out:
1464 complete_pdu(s, vs->pdu, err);
1465 qemu_free(vs);
1466}
1467
1468static void v9fs_setattr(V9fsState *s, V9fsPDU *pdu)
1469{
1470 int32_t fid;
1471 V9fsSetattrState *vs;
1472 int err = 0;
1473
1474 vs = qemu_malloc(sizeof(*vs));
1475 vs->pdu = pdu;
1476 vs->offset = 7;
1477
1478 pdu_unmarshal(pdu, vs->offset, "dI", &fid, &vs->v9iattr);
1479
1480 vs->fidp = lookup_fid(s, fid);
1481 if (vs->fidp == NULL) {
1482 err = -EINVAL;
1483 goto out;
1484 }
1485
1486 if (vs->v9iattr.valid & ATTR_MODE) {
1487 err = v9fs_do_chmod(s, &vs->fidp->path, vs->v9iattr.mode);
1488 }
1489
1490 v9fs_setattr_post_chmod(s, vs, err);
1491 return;
1492
1493out:
1494 complete_pdu(s, vs->pdu, err);
1495 qemu_free(vs);
1496}
1497
ff5e54c9
AL
1498static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1499{
1500 complete_pdu(s, vs->pdu, err);
1501
4f8dee2d 1502 if (vs->nwnames && vs->nwnames <= P9_MAXWELEM) {
ff5e54c9
AL
1503 for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1504 v9fs_string_free(&vs->wnames[vs->name_idx]);
1505 }
1506
1507 qemu_free(vs->wnames);
1508 qemu_free(vs->qids);
1509 }
1510}
1511
1512static void v9fs_walk_marshal(V9fsWalkState *vs)
1513{
1514 int i;
1515 vs->offset = 7;
1516 vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1517
1518 for (i = 0; i < vs->nwnames; i++) {
1519 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1520 }
1521}
1522
1523static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1524 int err)
1525{
1526 if (err == -1) {
1527 free_fid(s, vs->newfidp->fid);
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
1535 vs->name_idx++;
1536 if (vs->name_idx < vs->nwnames) {
1537 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1538 vs->wnames[vs->name_idx].data);
1539 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1540
1541 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1542 v9fs_walk_post_newfid_lstat(s, vs, err);
1543 return;
1544 }
1545
1546 v9fs_string_free(&vs->path);
1547 v9fs_walk_marshal(vs);
1548 err = vs->offset;
1549out:
1550 v9fs_walk_complete(s, vs, err);
1551}
1552
1553static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1554 int err)
1555{
1556 if (err == -1) {
1557 v9fs_string_free(&vs->path);
1558 err = -ENOENT;
1559 goto out;
1560 }
1561
1562 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1563 vs->name_idx++;
1564 if (vs->name_idx < vs->nwnames) {
1565
1566 v9fs_string_sprintf(&vs->path, "%s/%s",
1567 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1568 v9fs_string_copy(&vs->fidp->path, &vs->path);
1569
1570 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1571 v9fs_walk_post_oldfid_lstat(s, vs, err);
1572 return;
1573 }
1574
1575 v9fs_string_free(&vs->path);
1576 v9fs_walk_marshal(vs);
1577 err = vs->offset;
1578out:
1579 v9fs_walk_complete(s, vs, err);
1580}
1581
9f107513
AL
1582static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1583{
ff5e54c9
AL
1584 int32_t fid, newfid;
1585 V9fsWalkState *vs;
1586 int err = 0;
1587 int i;
1588
1589 vs = qemu_malloc(sizeof(*vs));
1590 vs->pdu = pdu;
1591 vs->wnames = NULL;
1592 vs->qids = NULL;
1593 vs->offset = 7;
1594
1595 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1596 &newfid, &vs->nwnames);
1597
4f8dee2d 1598 if (vs->nwnames && vs->nwnames <= P9_MAXWELEM) {
ff5e54c9
AL
1599 vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1600
1601 vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1602
1603 for (i = 0; i < vs->nwnames; i++) {
1604 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1605 &vs->wnames[i]);
1606 }
4f8dee2d
HPB
1607 } else if (vs->nwnames > P9_MAXWELEM) {
1608 err = -EINVAL;
1609 goto out;
ff5e54c9
AL
1610 }
1611
1612 vs->fidp = lookup_fid(s, fid);
1613 if (vs->fidp == NULL) {
1614 err = -ENOENT;
1615 goto out;
1616 }
1617
1618 /* FIXME: is this really valid? */
1619 if (fid == newfid) {
1620
d62dbb51 1621 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
ff5e54c9
AL
1622 v9fs_string_init(&vs->path);
1623 vs->name_idx = 0;
1624
1625 if (vs->name_idx < vs->nwnames) {
1626 v9fs_string_sprintf(&vs->path, "%s/%s",
1627 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1628 v9fs_string_copy(&vs->fidp->path, &vs->path);
1629
1630 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1631 v9fs_walk_post_oldfid_lstat(s, vs, err);
1632 return;
1633 }
1634 } else {
1635 vs->newfidp = alloc_fid(s, newfid);
1636 if (vs->newfidp == NULL) {
1637 err = -EINVAL;
1638 goto out;
1639 }
1640
1641 vs->newfidp->uid = vs->fidp->uid;
1642 v9fs_string_init(&vs->path);
1643 vs->name_idx = 0;
1644 v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1645
1646 if (vs->name_idx < vs->nwnames) {
1647 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1648 vs->wnames[vs->name_idx].data);
1649 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1650
1651 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1652 v9fs_walk_post_newfid_lstat(s, vs, err);
1653 return;
1654 }
9f107513 1655 }
ff5e54c9
AL
1656
1657 v9fs_walk_marshal(vs);
1658 err = vs->offset;
1659out:
1660 v9fs_walk_complete(s, vs, err);
9f107513
AL
1661}
1662
5e94c103
MK
1663static int32_t get_iounit(V9fsState *s, V9fsString *name)
1664{
1665 struct statfs stbuf;
1666 int32_t iounit = 0;
1667
1668 /*
1669 * iounit should be multiples of f_bsize (host filesystem block size
1670 * and as well as less than (client msize - P9_IOHDRSZ))
1671 */
1672 if (!v9fs_do_statfs(s, name, &stbuf)) {
1673 iounit = stbuf.f_bsize;
1674 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1675 }
1676
1677 if (!iounit) {
1678 iounit = s->msize - P9_IOHDRSZ;
1679 }
1680 return iounit;
1681}
1682
a6568fe2
AL
1683static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1684{
d62dbb51 1685 if (vs->fidp->fs.dir == NULL) {
a6568fe2
AL
1686 err = -errno;
1687 goto out;
1688 }
d62dbb51 1689 vs->fidp->fid_type = P9_FID_DIR;
a6568fe2
AL
1690 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1691 err = vs->offset;
1692out:
1693 complete_pdu(s, vs->pdu, err);
1694 qemu_free(vs);
1695
1696}
1697
5e94c103
MK
1698static void v9fs_open_post_getiounit(V9fsState *s, V9fsOpenState *vs)
1699{
1700 int err;
1701 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
1702 err = vs->offset;
1703 complete_pdu(s, vs->pdu, err);
1704 qemu_free(vs);
1705}
1706
a6568fe2
AL
1707static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1708{
d62dbb51 1709 if (vs->fidp->fs.fd == -1) {
a6568fe2
AL
1710 err = -errno;
1711 goto out;
1712 }
d62dbb51 1713 vs->fidp->fid_type = P9_FID_FILE;
5e94c103
MK
1714 vs->iounit = get_iounit(s, &vs->fidp->path);
1715 v9fs_open_post_getiounit(s, vs);
1716 return;
a6568fe2
AL
1717out:
1718 complete_pdu(s, vs->pdu, err);
1719 qemu_free(vs);
1720}
1721
1722static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1723{
771e9d4c
MK
1724 int flags;
1725
a6568fe2
AL
1726 if (err) {
1727 err = -errno;
1728 goto out;
1729 }
1730
1731 stat_to_qid(&vs->stbuf, &vs->qid);
1732
1733 if (S_ISDIR(vs->stbuf.st_mode)) {
d62dbb51 1734 vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fidp->path);
a6568fe2
AL
1735 v9fs_open_post_opendir(s, vs, err);
1736 } else {
771e9d4c 1737 if (s->proto_version == V9FS_PROTO_2000L) {
771e9d4c 1738 flags = vs->mode;
630c2689 1739 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
0f8151cb
VJJ
1740 /* Ignore direct disk access hint until the server supports it. */
1741 flags &= ~O_DIRECT;
771e9d4c
MK
1742 } else {
1743 flags = omode_to_uflags(vs->mode);
1744 }
d62dbb51 1745 vs->fidp->fs.fd = v9fs_do_open(s, &vs->fidp->path, flags);
a6568fe2
AL
1746 v9fs_open_post_open(s, vs, err);
1747 }
1748 return;
1749out:
1750 complete_pdu(s, vs->pdu, err);
1751 qemu_free(vs);
9f107513
AL
1752}
1753
1754static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
a6568fe2
AL
1755{
1756 int32_t fid;
1757 V9fsOpenState *vs;
1758 ssize_t err = 0;
1759
a6568fe2
AL
1760 vs = qemu_malloc(sizeof(*vs));
1761 vs->pdu = pdu;
1762 vs->offset = 7;
771e9d4c 1763 vs->mode = 0;
a6568fe2 1764
771e9d4c
MK
1765 if (s->proto_version == V9FS_PROTO_2000L) {
1766 pdu_unmarshal(vs->pdu, vs->offset, "dd", &fid, &vs->mode);
1767 } else {
1768 pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1769 }
a6568fe2
AL
1770
1771 vs->fidp = lookup_fid(s, fid);
1772 if (vs->fidp == NULL) {
1773 err = -ENOENT;
1774 goto out;
1775 }
1776
d62dbb51 1777 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
a6568fe2
AL
1778
1779 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1780
1781 v9fs_open_post_lstat(s, vs, err);
1782 return;
1783out:
1784 complete_pdu(s, pdu, err);
1785 qemu_free(vs);
1786}
1787
c1568af5
VJJ
1788static void v9fs_post_lcreate(V9fsState *s, V9fsLcreateState *vs, int err)
1789{
1790 if (err == 0) {
1791 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1792 stat_to_qid(&vs->stbuf, &vs->qid);
1793 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid,
1d810aeb 1794 vs->iounit);
c1568af5
VJJ
1795 err = vs->offset;
1796 } else {
d62dbb51 1797 vs->fidp->fid_type = P9_FID_NONE;
c1568af5 1798 err = -errno;
ab03b63d
SK
1799 if (vs->fidp->fs.fd > 0) {
1800 close(vs->fidp->fs.fd);
1801 }
c1568af5
VJJ
1802 }
1803
1804 complete_pdu(s, vs->pdu, err);
1805 v9fs_string_free(&vs->name);
1806 v9fs_string_free(&vs->fullname);
1807 qemu_free(vs);
1808}
1809
1810static void v9fs_lcreate_post_get_iounit(V9fsState *s, V9fsLcreateState *vs,
1811 int err)
1812{
1813 if (err) {
1814 err = -errno;
1815 goto out;
1816 }
1817 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1818
1819out:
1820 v9fs_post_lcreate(s, vs, err);
1821}
1822
1823static void v9fs_lcreate_post_do_open2(V9fsState *s, V9fsLcreateState *vs,
1824 int err)
1825{
d62dbb51 1826 if (vs->fidp->fs.fd == -1) {
c1568af5
VJJ
1827 err = -errno;
1828 goto out;
1829 }
d62dbb51 1830 vs->fidp->fid_type = P9_FID_FILE;
c1568af5
VJJ
1831 vs->iounit = get_iounit(s, &vs->fullname);
1832 v9fs_lcreate_post_get_iounit(s, vs, err);
1833 return;
1834
1835out:
1836 v9fs_post_lcreate(s, vs, err);
1837}
1838
1839static void v9fs_lcreate(V9fsState *s, V9fsPDU *pdu)
1840{
1841 int32_t dfid, flags, mode;
1842 gid_t gid;
1843 V9fsLcreateState *vs;
1844 ssize_t err = 0;
1845
1846 vs = qemu_malloc(sizeof(*vs));
1847 vs->pdu = pdu;
1848 vs->offset = 7;
1849
1850 v9fs_string_init(&vs->fullname);
1851
1852 pdu_unmarshal(vs->pdu, vs->offset, "dsddd", &dfid, &vs->name, &flags,
1853 &mode, &gid);
1854
1855 vs->fidp = lookup_fid(s, dfid);
1856 if (vs->fidp == NULL) {
1857 err = -ENOENT;
1858 goto out;
1859 }
1860
1861 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
1862 vs->name.data);
1863
0f8151cb
VJJ
1864 /* Ignore direct disk access hint until the server supports it. */
1865 flags &= ~O_DIRECT;
1866
d62dbb51 1867 vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
c1568af5
VJJ
1868 gid, flags, mode);
1869 v9fs_lcreate_post_do_open2(s, vs, err);
1870 return;
1871
1872out:
1873 complete_pdu(s, vs->pdu, err);
1874 v9fs_string_free(&vs->name);
1875 qemu_free(vs);
1876}
1877
b41e95d3
VJJ
1878static void v9fs_post_do_fsync(V9fsState *s, V9fsPDU *pdu, int err)
1879{
1880 if (err == -1) {
1881 err = -errno;
1882 }
1883 complete_pdu(s, pdu, err);
1884}
1885
1886static void v9fs_fsync(V9fsState *s, V9fsPDU *pdu)
1887{
1888 int32_t fid;
1889 size_t offset = 7;
1890 V9fsFidState *fidp;
49594973 1891 int datasync;
b41e95d3
VJJ
1892 int err;
1893
49594973 1894 pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
b41e95d3
VJJ
1895 fidp = lookup_fid(s, fid);
1896 if (fidp == NULL) {
1897 err = -ENOENT;
1898 v9fs_post_do_fsync(s, pdu, err);
1899 return;
1900 }
49594973 1901 err = v9fs_do_fsync(s, fidp->fs.fd, datasync);
b41e95d3
VJJ
1902 v9fs_post_do_fsync(s, pdu, err);
1903}
1904
a6568fe2
AL
1905static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1906{
bbd5697b
AL
1907 int32_t fid;
1908 size_t offset = 7;
1909 int err;
1910
1911 pdu_unmarshal(pdu, offset, "d", &fid);
1912
1913 err = free_fid(s, fid);
1914 if (err < 0) {
1915 goto out;
a6568fe2 1916 }
bbd5697b
AL
1917
1918 offset = 7;
1919 err = offset;
1920out:
1921 complete_pdu(s, pdu, err);
9f107513
AL
1922}
1923
a9231555
AL
1924static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1925
1926static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1927{
1928 if (err) {
1929 goto out;
1930 }
1931 v9fs_stat_free(&vs->v9stat);
1932 v9fs_string_free(&vs->name);
1933 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1934 vs->offset += vs->count;
1935 err = vs->offset;
1936out:
1937 complete_pdu(s, vs->pdu, err);
1938 qemu_free(vs);
1939 return;
1940}
1941
1942static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1943 ssize_t err)
1944{
1945 if (err) {
1946 err = -errno;
1947 goto out;
1948 }
1949 err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1950 if (err) {
1951 goto out;
1952 }
1953
1954 vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1955 &vs->v9stat);
1956 if ((vs->len != (vs->v9stat.size + 2)) ||
1957 ((vs->count + vs->len) > vs->max_count)) {
d62dbb51 1958 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
a9231555
AL
1959 v9fs_read_post_seekdir(s, vs, err);
1960 return;
1961 }
1962 vs->count += vs->len;
1963 v9fs_stat_free(&vs->v9stat);
1964 v9fs_string_free(&vs->name);
1965 vs->dir_pos = vs->dent->d_off;
d62dbb51 1966 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
a9231555
AL
1967 v9fs_read_post_readdir(s, vs, err);
1968 return;
1969out:
d62dbb51 1970 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
a9231555
AL
1971 v9fs_read_post_seekdir(s, vs, err);
1972 return;
1973
1974}
1975
1976static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1977{
1978 if (vs->dent) {
1979 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1980 v9fs_string_init(&vs->name);
1981 v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1982 vs->dent->d_name);
1983 err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1984 v9fs_read_post_dir_lstat(s, vs, err);
1985 return;
1986 }
1987
1988 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1989 vs->offset += vs->count;
1990 err = vs->offset;
1991 complete_pdu(s, vs->pdu, err);
1992 qemu_free(vs);
1993 return;
1994}
1995
1996static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1997{
d62dbb51 1998 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
a9231555
AL
1999 v9fs_read_post_readdir(s, vs, err);
2000 return;
2001}
2002
2003static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
2004 ssize_t err)
2005{
d62dbb51 2006 vs->dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
a9231555
AL
2007 v9fs_read_post_telldir(s, vs, err);
2008 return;
2009}
2010
56d15a53 2011static void v9fs_read_post_preadv(V9fsState *s, V9fsReadState *vs, ssize_t err)
a9231555
AL
2012{
2013 if (err < 0) {
2014 /* IO error return the error */
2015 err = -errno;
2016 goto out;
2017 }
2018 vs->total += vs->len;
2019 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
2020 if (vs->total < vs->count && vs->len > 0) {
2021 do {
2022 if (0) {
2023 print_sg(vs->sg, vs->cnt);
2024 }
56d15a53
SG
2025 vs->len = v9fs_do_preadv(s, vs->fidp->fs.fd, vs->sg, vs->cnt,
2026 vs->off);
2027 if (vs->len > 0) {
2028 vs->off += vs->len;
2029 }
a9231555
AL
2030 } while (vs->len == -1 && errno == EINTR);
2031 if (vs->len == -1) {
2032 err = -errno;
2033 }
56d15a53 2034 v9fs_read_post_preadv(s, vs, err);
a9231555
AL
2035 return;
2036 }
2037 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
2038 vs->offset += vs->count;
2039 err = vs->offset;
2040
2041out:
2042 complete_pdu(s, vs->pdu, err);
2043 qemu_free(vs);
2044}
2045
fa32ef88
AK
2046static void v9fs_xattr_read(V9fsState *s, V9fsReadState *vs)
2047{
2048 ssize_t err = 0;
2049 int read_count;
2050 int64_t xattr_len;
2051
2052 xattr_len = vs->fidp->fs.xattr.len;
2053 read_count = xattr_len - vs->off;
2054 if (read_count > vs->count) {
2055 read_count = vs->count;
2056 } else if (read_count < 0) {
2057 /*
2058 * read beyond XATTR value
2059 */
2060 read_count = 0;
2061 }
2062 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", read_count);
2063 vs->offset += pdu_pack(vs->pdu, vs->offset,
2064 ((char *)vs->fidp->fs.xattr.value) + vs->off,
2065 read_count);
2066 err = vs->offset;
2067 complete_pdu(s, vs->pdu, err);
2068 qemu_free(vs);
2069}
2070
9f107513
AL
2071static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
2072{
a9231555
AL
2073 int32_t fid;
2074 V9fsReadState *vs;
2075 ssize_t err = 0;
2076
2077 vs = qemu_malloc(sizeof(*vs));
2078 vs->pdu = pdu;
2079 vs->offset = 7;
2080 vs->total = 0;
2081 vs->len = 0;
2082 vs->count = 0;
2083
2084 pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count);
2085
2086 vs->fidp = lookup_fid(s, fid);
2087 if (vs->fidp == NULL) {
2088 err = -EINVAL;
2089 goto out;
2090 }
2091
fa32ef88 2092 if (vs->fidp->fid_type == P9_FID_DIR) {
a9231555
AL
2093 vs->max_count = vs->count;
2094 vs->count = 0;
2095 if (vs->off == 0) {
d62dbb51 2096 v9fs_do_rewinddir(s, vs->fidp->fs.dir);
a9231555
AL
2097 }
2098 v9fs_read_post_rewinddir(s, vs, err);
2099 return;
fa32ef88 2100 } else if (vs->fidp->fid_type == P9_FID_FILE) {
a9231555
AL
2101 vs->sg = vs->iov;
2102 pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
56d15a53
SG
2103 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
2104 if (vs->total <= vs->count) {
2105 vs->len = v9fs_do_preadv(s, vs->fidp->fs.fd, vs->sg, vs->cnt,
2106 vs->off);
2107 if (vs->len > 0) {
2108 vs->off += vs->len;
2109 }
2110 err = vs->len;
2111 v9fs_read_post_preadv(s, vs, err);
2112 }
a9231555 2113 return;
fa32ef88
AK
2114 } else if (vs->fidp->fid_type == P9_FID_XATTR) {
2115 v9fs_xattr_read(s, vs);
2116 return;
a9231555
AL
2117 } else {
2118 err = -EINVAL;
9f107513 2119 }
a9231555
AL
2120out:
2121 complete_pdu(s, pdu, err);
2122 qemu_free(vs);
9f107513
AL
2123}
2124
c18e2f94
SK
2125typedef struct V9fsReadDirState {
2126 V9fsPDU *pdu;
2127 V9fsFidState *fidp;
2128 V9fsQID qid;
2129 off_t saved_dir_pos;
2130 struct dirent *dent;
2131 int32_t count;
2132 int32_t max_count;
2133 size_t offset;
2134 int64_t initial_offset;
2135 V9fsString name;
2136} V9fsReadDirState;
2137
2138static void v9fs_readdir_post_seekdir(V9fsState *s, V9fsReadDirState *vs)
2139{
2140 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
2141 vs->offset += vs->count;
2142 complete_pdu(s, vs->pdu, vs->offset);
2143 qemu_free(vs);
2144 return;
2145}
2146
2147/* Size of each dirent on the wire: size of qid (13) + size of offset (8)
2148 * size of type (1) + size of name.size (2) + strlen(name.data)
2149 */
2150#define V9_READDIR_DATA_SZ (24 + strlen(vs->name.data))
2151
2152static void v9fs_readdir_post_readdir(V9fsState *s, V9fsReadDirState *vs)
2153{
2154 int len;
2155 size_t size;
2156
2157 if (vs->dent) {
2158 v9fs_string_init(&vs->name);
2159 v9fs_string_sprintf(&vs->name, "%s", vs->dent->d_name);
2160
2161 if ((vs->count + V9_READDIR_DATA_SZ) > vs->max_count) {
2162 /* Ran out of buffer. Set dir back to old position and return */
d62dbb51 2163 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->saved_dir_pos);
c18e2f94
SK
2164 v9fs_readdir_post_seekdir(s, vs);
2165 return;
2166 }
2167
2168 /* Fill up just the path field of qid because the client uses
2169 * only that. To fill the entire qid structure we will have
2170 * to stat each dirent found, which is expensive
2171 */
2172 size = MIN(sizeof(vs->dent->d_ino), sizeof(vs->qid.path));
2173 memcpy(&vs->qid.path, &vs->dent->d_ino, size);
2174 /* Fill the other fields with dummy values */
2175 vs->qid.type = 0;
2176 vs->qid.version = 0;
2177
2178 len = pdu_marshal(vs->pdu, vs->offset+4+vs->count, "Qqbs",
2179 &vs->qid, vs->dent->d_off,
2180 vs->dent->d_type, &vs->name);
2181 vs->count += len;
2182 v9fs_string_free(&vs->name);
2183 vs->saved_dir_pos = vs->dent->d_off;
d62dbb51 2184 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
c18e2f94
SK
2185 v9fs_readdir_post_readdir(s, vs);
2186 return;
2187 }
2188
2189 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
2190 vs->offset += vs->count;
2191 complete_pdu(s, vs->pdu, vs->offset);
2192 qemu_free(vs);
2193 return;
2194}
2195
2196static void v9fs_readdir_post_telldir(V9fsState *s, V9fsReadDirState *vs)
2197{
d62dbb51 2198 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
c18e2f94
SK
2199 v9fs_readdir_post_readdir(s, vs);
2200 return;
2201}
2202
2203static void v9fs_readdir_post_setdir(V9fsState *s, V9fsReadDirState *vs)
2204{
d62dbb51 2205 vs->saved_dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
c18e2f94
SK
2206 v9fs_readdir_post_telldir(s, vs);
2207 return;
2208}
2209
2210static void v9fs_readdir(V9fsState *s, V9fsPDU *pdu)
2211{
2212 int32_t fid;
2213 V9fsReadDirState *vs;
2214 ssize_t err = 0;
2215 size_t offset = 7;
2216
2217 vs = qemu_malloc(sizeof(*vs));
2218 vs->pdu = pdu;
2219 vs->offset = 7;
2220 vs->count = 0;
2221
2222 pdu_unmarshal(vs->pdu, offset, "dqd", &fid, &vs->initial_offset,
2223 &vs->max_count);
2224
2225 vs->fidp = lookup_fid(s, fid);
d62dbb51 2226 if (vs->fidp == NULL || !(vs->fidp->fs.dir)) {
c18e2f94
SK
2227 err = -EINVAL;
2228 goto out;
2229 }
2230
2231 if (vs->initial_offset == 0) {
d62dbb51 2232 v9fs_do_rewinddir(s, vs->fidp->fs.dir);
c18e2f94 2233 } else {
d62dbb51 2234 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->initial_offset);
c18e2f94
SK
2235 }
2236
2237 v9fs_readdir_post_setdir(s, vs);
2238 return;
2239
2240out:
2241 complete_pdu(s, pdu, err);
2242 qemu_free(vs);
2243 return;
2244}
2245
56d15a53 2246static void v9fs_write_post_pwritev(V9fsState *s, V9fsWriteState *vs,
8449360c
AL
2247 ssize_t err)
2248{
2249 if (err < 0) {
2250 /* IO error return the error */
2251 err = -errno;
2252 goto out;
2253 }
2254 vs->total += vs->len;
2255 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
2256 if (vs->total < vs->count && vs->len > 0) {
2257 do {
2258 if (0) {
2259 print_sg(vs->sg, vs->cnt);
2260 }
56d15a53
SG
2261 vs->len = v9fs_do_pwritev(s, vs->fidp->fs.fd, vs->sg, vs->cnt,
2262 vs->off);
2263 if (vs->len > 0) {
2264 vs->off += vs->len;
2265 }
8449360c
AL
2266 } while (vs->len == -1 && errno == EINTR);
2267 if (vs->len == -1) {
2268 err = -errno;
2269 }
56d15a53 2270 v9fs_write_post_pwritev(s, vs, err);
8449360c
AL
2271 return;
2272 }
2273 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
8449360c
AL
2274 err = vs->offset;
2275out:
2276 complete_pdu(s, vs->pdu, err);
2277 qemu_free(vs);
2278}
2279
10b468bd
AK
2280static void v9fs_xattr_write(V9fsState *s, V9fsWriteState *vs)
2281{
2282 int i, to_copy;
2283 ssize_t err = 0;
2284 int write_count;
2285 int64_t xattr_len;
2286
2287 xattr_len = vs->fidp->fs.xattr.len;
2288 write_count = xattr_len - vs->off;
2289 if (write_count > vs->count) {
2290 write_count = vs->count;
2291 } else if (write_count < 0) {
2292 /*
2293 * write beyond XATTR value len specified in
2294 * xattrcreate
2295 */
2296 err = -ENOSPC;
2297 goto out;
2298 }
2299 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", write_count);
2300 err = vs->offset;
2301 vs->fidp->fs.xattr.copied_len += write_count;
2302 /*
2303 * Now copy the content from sg list
2304 */
2305 for (i = 0; i < vs->cnt; i++) {
2306 if (write_count > vs->sg[i].iov_len) {
2307 to_copy = vs->sg[i].iov_len;
2308 } else {
2309 to_copy = write_count;
2310 }
2311 memcpy((char *)vs->fidp->fs.xattr.value + vs->off,
2312 vs->sg[i].iov_base, to_copy);
2313 /* updating vs->off since we are not using below */
2314 vs->off += to_copy;
2315 write_count -= to_copy;
2316 }
2317out:
2318 complete_pdu(s, vs->pdu, err);
2319 qemu_free(vs);
2320}
2321
9f107513
AL
2322static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
2323{
8449360c
AL
2324 int32_t fid;
2325 V9fsWriteState *vs;
2326 ssize_t err;
2327
2328 vs = qemu_malloc(sizeof(*vs));
2329
2330 vs->pdu = pdu;
2331 vs->offset = 7;
2332 vs->sg = vs->iov;
2333 vs->total = 0;
2334 vs->len = 0;
2335
2336 pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
10b468bd 2337 vs->sg, &vs->cnt);
8449360c
AL
2338
2339 vs->fidp = lookup_fid(s, fid);
2340 if (vs->fidp == NULL) {
2341 err = -EINVAL;
2342 goto out;
9f107513 2343 }
8449360c 2344
10b468bd
AK
2345 if (vs->fidp->fid_type == P9_FID_FILE) {
2346 if (vs->fidp->fs.fd == -1) {
2347 err = -EINVAL;
2348 goto out;
2349 }
2350 } else if (vs->fidp->fid_type == P9_FID_XATTR) {
2351 /*
2352 * setxattr operation
2353 */
2354 v9fs_xattr_write(s, vs);
2355 return;
2356 } else {
8449360c
AL
2357 err = -EINVAL;
2358 goto out;
2359 }
56d15a53
SG
2360 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
2361 if (vs->total <= vs->count) {
2362 vs->len = v9fs_do_pwritev(s, vs->fidp->fs.fd, vs->sg, vs->cnt, vs->off);
2363 if (vs->len > 0) {
2364 vs->off += vs->len;
2365 }
2366 err = vs->len;
2367 v9fs_write_post_pwritev(s, vs, err);
2368 }
8449360c 2369 return;
8449360c
AL
2370out:
2371 complete_pdu(s, vs->pdu, err);
2372 qemu_free(vs);
9f107513
AL
2373}
2374
5e94c103 2375static 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
2391static 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
2406static 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
2414static 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
2424static 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
2436out:
2437 v9fs_post_create(s, vs, err);
2438}
2439
2440static 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
2451out:
2452 v9fs_post_create(s, vs, err);
2453}
2454
c494dd6f
AL
2455static 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
2466static 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
2478out:
2479 v9fs_post_create(s, vs, err);
2480
2481}
2482
2483static 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
VJJ
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
VJJ
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
2552out:
2553 v9fs_post_create(s, vs, err);
2554}
2555
9f107513
AL
2556static 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
2584out:
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
VJJ
2591static 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
2607static 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);
2614out:
2615 v9fs_post_symlink(s, vs, err);
2616}
2617
2618static 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
2647out:
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
2654static 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
VJJ
2660static 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
2692out:
2693 v9fs_string_free(&name);
2694 complete_pdu(s, pdu, err);
2695}
2696
5bae1900
AL
2697static 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
2713static 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
2735out:
2736 complete_pdu(s, pdu, err);
2737 qemu_free(vs);
9f107513
AL
2738}
2739
8cf89e00
AL
2740static 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
2748out:
2749 v9fs_stat_free(&vs->v9stat);
2750 complete_pdu(s, vs->pdu, err);
2751 qemu_free(vs);
2752}
2753
2754static 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
2767out:
2768 v9fs_stat_free(&vs->v9stat);
2769 complete_pdu(s, vs->pdu, err);
2770 qemu_free(vs);
2771}
2772
c7b4b0b3 2773static 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 /*
936532a4
MN
2825 * we replace name of this fid towards the end so
2826 * that our below v9fs_path_is_ancestor check will
2827 * work
c7b4b0b3
MK
2828 */
2829 continue;
2830 }
936532a4 2831 if (v9fs_path_is_ancestor(&vs->fidp->path, &fidp->path)) {
c7b4b0b3
MK
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
2840out:
2841 v9fs_string_free(&vs->name);
2842 return err;
2843}
2844
2845static 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
2851static 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
2874out:
2875 v9fs_stat_free(&vs->v9stat);
2876 complete_pdu(s, vs->pdu, err);
2877 qemu_free(vs);
2878}
2879
c7b4b0b3
MK
2880static 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;
2903out:
2904 complete_pdu(s, vs->pdu, err);
2905 qemu_free(vs);
2906}
2907
8cf89e00
AL
2908static 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
2923out:
2924 v9fs_stat_free(&vs->v9stat);
2925 complete_pdu(s, vs->pdu, err);
2926 qemu_free(vs);
2927}
2928
2929static 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
2958out:
2959 v9fs_stat_free(&vs->v9stat);
2960 complete_pdu(s, vs->pdu, err);
2961 qemu_free(vs);
2962}
2963
2964static 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
2974static 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
2999out:
3000 v9fs_stat_free(&vs->v9stat);
3001 complete_pdu(s, vs->pdu, err);
3002 qemu_free(vs);
3003}
3004
9f107513
AL
3005static 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)) {
49594973 3025 err = v9fs_do_fsync(s, vs->fidp->fs.fd, 0);
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
3039out:
3040 v9fs_stat_free(&vs->v9stat);
3041 complete_pdu(s, vs->pdu, err);
3042 qemu_free(vs);
9f107513
AL
3043}
3044
be940c87
MK
3045static 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
3085out:
3086 complete_pdu(s, vs->pdu, vs->offset);
3087 qemu_free(vs);
3088}
3089
3090static 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
3113out:
3114 complete_pdu(s, vs->pdu, err);
3115 qemu_free(vs);
3116}
3117
5268cecc
MK
3118static 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;
3128out:
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
3135static 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;
3145out:
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
3152static 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
3182out:
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
3198static 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;
3231out:
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
3243static 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);
3272out:
3273 complete_pdu(s, vs->pdu, err);
3274 qemu_free(vs->glock);
3275 qemu_free(vs);
3276}
3277
b67592ea
MK
3278static 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;
3288out:
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
3295static 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;
3305out:
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
3312static 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
3340out:
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
3347static 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;
3357out:
3358 complete_pdu(s, vs->pdu, err);
3359 v9fs_string_free(&vs->name);
3360 qemu_free(vs);
3361 return;
3362}
3363
3364static 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;
3385out:
3386 complete_pdu(s, vs->pdu, err);
3387 v9fs_string_free(&vs->name);
3388 qemu_free(vs);
3389}
3390
3391static 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;
3401out:
3402 complete_pdu(s, vs->pdu, err);
3403 v9fs_string_free(&vs->name);
3404 qemu_free(vs);
3405 return;
3406}
3407
3408static 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;
3430out:
3431 complete_pdu(s, vs->pdu, err);
3432 v9fs_string_free(&vs->name);
3433 qemu_free(vs);
3434}
3435
3436static 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 }
3484out:
3485 complete_pdu(s, vs->pdu, err);
3486 v9fs_string_free(&vs->name);
3487 qemu_free(vs);
3488}
3489
10b468bd
AK
3490static 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
3523out:
3524 complete_pdu(s, vs->pdu, err);
3525 v9fs_string_free(&vs->name);
3526 qemu_free(vs);
3527}
fa32ef88 3528
df0973a4
MK
3529static 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;
3538out:
3539 complete_pdu(s, vs->pdu, err);
3540 v9fs_string_free(&vs->target);
3541 qemu_free(vs);
3542}
3543
3544static 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;
3567out:
3568 complete_pdu(s, vs->pdu, err);
3569 qemu_free(vs);
3570}
3571
9f107513
AL
3572typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
3573
3574static 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
3609static 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
3625static 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
3650static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
3651{
3652 features |= 1 << VIRTIO_9P_MOUNT_TAG;
3653 return features;
3654}
3655
3656static V9fsState *to_virtio_9p(VirtIODevice *vdev)
3657{
3658 return (V9fsState *)vdev;
3659}
3660
3661static 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
3674VirtIODevice *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
VJJ
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
VJJ
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
VJJ
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
VJJ
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}
9fe1ebeb
AK
3765
3766static int virtio_9p_init_pci(PCIDevice *pci_dev)
3767{
3768 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
3769 VirtIODevice *vdev;
3770
3771 vdev = virtio_9p_init(&pci_dev->qdev, &proxy->fsconf);
3772 vdev->nvectors = proxy->nvectors;
3773 virtio_init_pci(proxy, vdev,
3774 PCI_VENDOR_ID_REDHAT_QUMRANET,
3775 0x1009,
3776 0x2,
3777 0x00);
3778 /* make the actual value visible */
3779 proxy->nvectors = vdev->nvectors;
3780 return 0;
3781}
3782
3783static PCIDeviceInfo virtio_9p_info = {
3784 .qdev.name = "virtio-9p-pci",
3785 .qdev.size = sizeof(VirtIOPCIProxy),
3786 .init = virtio_9p_init_pci,
3787 .qdev.props = (Property[]) {
3788 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
3789 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
3790 DEFINE_PROP_STRING("mount_tag", VirtIOPCIProxy, fsconf.tag),
3791 DEFINE_PROP_STRING("fsdev", VirtIOPCIProxy, fsconf.fsdev_id),
3792 DEFINE_PROP_END_OF_LIST(),
3793 }
3794};
3795
3796static void virtio_9p_register_devices(void)
3797{
3798 pci_qdev_register(&virtio_9p_info);
3799}
3800
3801device_init(virtio_9p_register_devices)