]> git.proxmox.com Git - qemu.git/blame - hw/virtio-9p.c
virtio-blk: Avoid zeroing every request structure
[qemu.git] / hw / 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"
17#include "virtio-9p.h"
18#include "fsdev/qemu-fsdev.h"
19#include "virtio-9p-debug.h"
20
21int dotu = 1;
22int debug_9p_pdu;
23
131dcb25
AL
24static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf)
25{
26 return s->ops->lstat(&s->ctx, path->data, stbuf);
27}
28
29static int v9fs_do_setuid(V9fsState *s, uid_t uid)
30{
31 return s->ops->setuid(&s->ctx, uid);
32}
33
34static ssize_t v9fs_do_readlink(V9fsState *s, V9fsString *path, V9fsString *buf)
35{
36 ssize_t len;
37
38 buf->data = qemu_malloc(1024);
39
40 len = s->ops->readlink(&s->ctx, path->data, buf->data, 1024 - 1);
41 if (len > -1) {
42 buf->size = len;
43 buf->data[len] = 0;
44 }
45
46 return len;
47}
48
49static int v9fs_do_close(V9fsState *s, int fd)
50{
51 return s->ops->close(&s->ctx, fd);
52}
53
54static int v9fs_do_closedir(V9fsState *s, DIR *dir)
55{
56 return s->ops->closedir(&s->ctx, dir);
57}
58
a6568fe2
AL
59static int v9fs_do_open(V9fsState *s, V9fsString *path, int flags)
60{
61 return s->ops->open(&s->ctx, path->data, flags);
62}
63
64static DIR *v9fs_do_opendir(V9fsState *s, V9fsString *path)
65{
66 return s->ops->opendir(&s->ctx, path->data);
67}
68
a9231555
AL
69static void v9fs_do_rewinddir(V9fsState *s, DIR *dir)
70{
71 return s->ops->rewinddir(&s->ctx, dir);
72}
73
74static off_t v9fs_do_telldir(V9fsState *s, DIR *dir)
75{
76 return s->ops->telldir(&s->ctx, dir);
77}
78
79static struct dirent *v9fs_do_readdir(V9fsState *s, DIR *dir)
80{
81 return s->ops->readdir(&s->ctx, dir);
82}
83
84static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off)
85{
86 return s->ops->seekdir(&s->ctx, dir, off);
87}
88
89static int v9fs_do_readv(V9fsState *s, int fd, const struct iovec *iov,
90 int iovcnt)
91{
92 return s->ops->readv(&s->ctx, fd, iov, iovcnt);
93}
94
95static off_t v9fs_do_lseek(V9fsState *s, int fd, off_t offset, int whence)
96{
97 return s->ops->lseek(&s->ctx, fd, offset, whence);
98}
99
8449360c
AL
100static int v9fs_do_writev(V9fsState *s, int fd, const struct iovec *iov,
101 int iovcnt)
102{
103 return s->ops->writev(&s->ctx, fd, iov, iovcnt);
104}
105
c494dd6f
AL
106static int v9fs_do_chmod(V9fsState *s, V9fsString *path, mode_t mode)
107{
108 return s->ops->chmod(&s->ctx, path->data, mode);
109}
110
111static int v9fs_do_mknod(V9fsState *s, V9fsString *path, mode_t mode, dev_t dev)
112{
113 return s->ops->mknod(&s->ctx, path->data, mode, dev);
114}
115
116static int v9fs_do_mksock(V9fsState *s, V9fsString *path)
117{
118 return s->ops->mksock(&s->ctx, path->data);
119}
120
121static int v9fs_do_mkdir(V9fsState *s, V9fsString *path, mode_t mode)
122{
123 return s->ops->mkdir(&s->ctx, path->data, mode);
124}
125
126static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
127{
128 return s->ops->fstat(&s->ctx, fd, stbuf);
129}
130
131static int v9fs_do_open2(V9fsState *s, V9fsString *path, int flags, mode_t mode)
132{
133 return s->ops->open2(&s->ctx, path->data, flags, mode);
134}
135
136static int v9fs_do_symlink(V9fsState *s, V9fsString *oldpath,
137 V9fsString *newpath)
138{
139 return s->ops->symlink(&s->ctx, oldpath->data, newpath->data);
140}
141
142static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
143{
144 return s->ops->link(&s->ctx, oldpath->data, newpath->data);
145}
146
8cf89e00
AL
147static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
148{
149 return s->ops->truncate(&s->ctx, path->data, size);
150}
151
152static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath,
153 V9fsString *newpath)
154{
155 return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
156}
157
158static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
159{
160 return s->ops->chown(&s->ctx, path->data, uid, gid);
161}
162
163static int v9fs_do_utime(V9fsState *s, V9fsString *path,
164 const struct utimbuf *buf)
165{
166 return s->ops->utime(&s->ctx, path->data, buf);
167}
168
5bae1900
AL
169static int v9fs_do_remove(V9fsState *s, V9fsString *path)
170{
171 return s->ops->remove(&s->ctx, path->data);
172}
173
8cf89e00
AL
174static int v9fs_do_fsync(V9fsState *s, int fd)
175{
176 return s->ops->fsync(&s->ctx, fd);
177}
178
a03f7874
AL
179static void v9fs_string_init(V9fsString *str)
180{
181 str->data = NULL;
182 str->size = 0;
183}
184
185static void v9fs_string_free(V9fsString *str)
186{
187 qemu_free(str->data);
188 str->data = NULL;
189 str->size = 0;
190}
191
192static void v9fs_string_null(V9fsString *str)
193{
194 v9fs_string_free(str);
195}
196
197static int number_to_string(void *arg, char type)
198{
199 unsigned int ret = 0;
200
201 switch (type) {
202 case 'u': {
203 unsigned int num = *(unsigned int *)arg;
204
205 do {
206 ret++;
207 num = num/10;
208 } while (num);
209 break;
210 }
211 default:
212 printf("Number_to_string: Unknown number format\n");
213 return -1;
214 }
215
216 return ret;
217}
218
219static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
220{
221 va_list ap2;
222 char *iter = (char *)fmt;
223 int len = 0;
224 int nr_args = 0;
225 char *arg_char_ptr;
226 unsigned int arg_uint;
227
228 /* Find the number of %'s that denotes an argument */
229 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
230 nr_args++;
231 iter++;
232 }
233
234 len = strlen(fmt) - 2*nr_args;
235
236 if (!nr_args) {
237 goto alloc_print;
238 }
239
240 va_copy(ap2, ap);
241
242 iter = (char *)fmt;
243
244 /* Now parse the format string */
245 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
246 iter++;
247 switch (*iter) {
248 case 'u':
249 arg_uint = va_arg(ap2, unsigned int);
250 len += number_to_string((void *)&arg_uint, 'u');
251 break;
252 case 's':
253 arg_char_ptr = va_arg(ap2, char *);
254 len += strlen(arg_char_ptr);
255 break;
256 case 'c':
257 len += 1;
258 break;
259 default:
260 fprintf(stderr,
261 "v9fs_string_alloc_printf:Incorrect format %c", *iter);
262 return -1;
263 }
264 iter++;
265 }
266
267alloc_print:
268 *strp = qemu_malloc((len + 1) * sizeof(**strp));
269
270 return vsprintf(*strp, fmt, ap);
271}
272
273static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
274{
275 va_list ap;
276 int err;
277
278 v9fs_string_free(str);
279
280 va_start(ap, fmt);
281 err = v9fs_string_alloc_printf(&str->data, fmt, ap);
282 BUG_ON(err == -1);
283 va_end(ap);
284
285 str->size = err;
286}
287
288static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
289{
290 v9fs_string_free(lhs);
291 v9fs_string_sprintf(lhs, "%s", rhs->data);
292}
293
294static size_t v9fs_string_size(V9fsString *str)
295{
296 return str->size;
297}
298
286d5652
AL
299static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
300{
301 V9fsFidState *f;
302
303 for (f = s->fid_list; f; f = f->next) {
304 if (f->fid == fid) {
305 v9fs_do_setuid(s, f->uid);
306 return f;
307 }
308 }
309
310 return NULL;
311}
312
313static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
314{
315 V9fsFidState *f;
316
317 f = lookup_fid(s, fid);
318 if (f) {
319 return NULL;
320 }
321
322 f = qemu_mallocz(sizeof(V9fsFidState));
323
324 f->fid = fid;
325 f->fd = -1;
326 f->dir = NULL;
327
328 f->next = s->fid_list;
329 s->fid_list = f;
330
331 return f;
332}
333
334static int free_fid(V9fsState *s, int32_t fid)
335{
336 V9fsFidState **fidpp, *fidp;
337
338 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
339 if ((*fidpp)->fid == fid) {
340 break;
341 }
342 }
343
344 if (*fidpp == NULL) {
345 return -ENOENT;
346 }
347
348 fidp = *fidpp;
349 *fidpp = fidp->next;
350
351 if (fidp->fd != -1) {
352 v9fs_do_close(s, fidp->fd);
353 }
354 if (fidp->dir) {
355 v9fs_do_closedir(s, fidp->dir);
356 }
357 v9fs_string_free(&fidp->path);
358 qemu_free(fidp);
359
360 return 0;
361}
362
363#define P9_QID_TYPE_DIR 0x80
364#define P9_QID_TYPE_SYMLINK 0x02
365
366#define P9_STAT_MODE_DIR 0x80000000
367#define P9_STAT_MODE_APPEND 0x40000000
368#define P9_STAT_MODE_EXCL 0x20000000
369#define P9_STAT_MODE_MOUNT 0x10000000
370#define P9_STAT_MODE_AUTH 0x08000000
371#define P9_STAT_MODE_TMP 0x04000000
372#define P9_STAT_MODE_SYMLINK 0x02000000
373#define P9_STAT_MODE_LINK 0x01000000
374#define P9_STAT_MODE_DEVICE 0x00800000
375#define P9_STAT_MODE_NAMED_PIPE 0x00200000
376#define P9_STAT_MODE_SOCKET 0x00100000
377#define P9_STAT_MODE_SETUID 0x00080000
378#define P9_STAT_MODE_SETGID 0x00040000
379#define P9_STAT_MODE_SETVTX 0x00010000
380
381#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
382 P9_STAT_MODE_SYMLINK | \
383 P9_STAT_MODE_LINK | \
384 P9_STAT_MODE_DEVICE | \
385 P9_STAT_MODE_NAMED_PIPE | \
386 P9_STAT_MODE_SOCKET)
387
388/* This is the algorithm from ufs in spfs */
389static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
390{
391 size_t size;
392
393 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
394 memcpy(&qidp->path, &stbuf->st_ino, size);
395 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
396 qidp->type = 0;
397 if (S_ISDIR(stbuf->st_mode)) {
398 qidp->type |= P9_QID_TYPE_DIR;
399 }
400 if (S_ISLNK(stbuf->st_mode)) {
401 qidp->type |= P9_QID_TYPE_SYMLINK;
402 }
403}
404
405static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
406{
407 struct stat stbuf;
408 int err;
409
410 err = v9fs_do_lstat(s, &fidp->path, &stbuf);
411 if (err) {
412 return err;
413 }
414
415 stat_to_qid(&stbuf, qidp);
416 return 0;
417}
418
9f107513
AL
419static V9fsPDU *alloc_pdu(V9fsState *s)
420{
421 V9fsPDU *pdu = NULL;
422
423 if (!QLIST_EMPTY(&s->free_list)) {
424 pdu = QLIST_FIRST(&s->free_list);
425 QLIST_REMOVE(pdu, next);
426 }
427 return pdu;
428}
429
430static void free_pdu(V9fsState *s, V9fsPDU *pdu)
431{
432 if (pdu) {
433 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
434 }
435}
436
437size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
438 size_t offset, size_t size, int pack)
439{
440 int i = 0;
441 size_t copied = 0;
442
443 for (i = 0; size && i < sg_count; i++) {
444 size_t len;
445 if (offset >= sg[i].iov_len) {
446 /* skip this sg */
447 offset -= sg[i].iov_len;
448 continue;
449 } else {
450 len = MIN(sg[i].iov_len - offset, size);
451 if (pack) {
452 memcpy(sg[i].iov_base + offset, addr, len);
453 } else {
454 memcpy(addr, sg[i].iov_base + offset, len);
455 }
456 size -= len;
457 copied += len;
458 addr += len;
459 if (size) {
460 offset = 0;
461 continue;
462 }
463 }
464 }
465
466 return copied;
467}
468
405a549a
AL
469static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
470{
471 return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
472 offset, size, 0);
473}
474
475static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
476 size_t size)
477{
478 return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
479 offset, size, 1);
480}
481
482static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
483{
484 size_t pos = 0;
485 int i, j;
486 struct iovec *src_sg;
487 unsigned int num;
488
489 if (rx) {
490 src_sg = pdu->elem.in_sg;
491 num = pdu->elem.in_num;
492 } else {
493 src_sg = pdu->elem.out_sg;
494 num = pdu->elem.out_num;
495 }
496
497 j = 0;
498 for (i = 0; i < num; i++) {
499 if (offset <= pos) {
500 sg[j].iov_base = src_sg[i].iov_base;
501 sg[j].iov_len = src_sg[i].iov_len;
502 j++;
503 } else if (offset < (src_sg[i].iov_len + pos)) {
504 sg[j].iov_base = src_sg[i].iov_base;
505 sg[j].iov_len = src_sg[i].iov_len;
506 sg[j].iov_base += (offset - pos);
507 sg[j].iov_len -= (offset - pos);
508 j++;
509 }
510 pos += src_sg[i].iov_len;
511 }
512
513 return j;
514}
515
516static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
517{
518 size_t old_offset = offset;
519 va_list ap;
520 int i;
521
522 va_start(ap, fmt);
523 for (i = 0; fmt[i]; i++) {
524 switch (fmt[i]) {
525 case 'b': {
526 uint8_t *valp = va_arg(ap, uint8_t *);
527 offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
528 break;
529 }
530 case 'w': {
531 uint16_t val, *valp;
532 valp = va_arg(ap, uint16_t *);
533 val = le16_to_cpupu(valp);
534 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
535 *valp = val;
536 break;
537 }
538 case 'd': {
539 uint32_t val, *valp;
540 valp = va_arg(ap, uint32_t *);
541 val = le32_to_cpupu(valp);
542 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
543 *valp = val;
544 break;
545 }
546 case 'q': {
547 uint64_t val, *valp;
548 valp = va_arg(ap, uint64_t *);
549 val = le64_to_cpup(valp);
550 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
551 *valp = val;
552 break;
553 }
554 case 'v': {
555 struct iovec *iov = va_arg(ap, struct iovec *);
556 int *iovcnt = va_arg(ap, int *);
557 *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
558 break;
559 }
560 case 's': {
561 V9fsString *str = va_arg(ap, V9fsString *);
562 offset += pdu_unmarshal(pdu, offset, "w", &str->size);
563 /* FIXME: sanity check str->size */
564 str->data = qemu_malloc(str->size + 1);
565 offset += pdu_unpack(str->data, pdu, offset, str->size);
566 str->data[str->size] = 0;
567 break;
568 }
569 case 'Q': {
570 V9fsQID *qidp = va_arg(ap, V9fsQID *);
571 offset += pdu_unmarshal(pdu, offset, "bdq",
572 &qidp->type, &qidp->version, &qidp->path);
573 break;
574 }
575 case 'S': {
576 V9fsStat *statp = va_arg(ap, V9fsStat *);
577 offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
578 &statp->size, &statp->type, &statp->dev,
579 &statp->qid, &statp->mode, &statp->atime,
580 &statp->mtime, &statp->length,
581 &statp->name, &statp->uid, &statp->gid,
582 &statp->muid, &statp->extension,
583 &statp->n_uid, &statp->n_gid,
584 &statp->n_muid);
585 break;
586 }
587 default:
588 break;
589 }
590 }
591
592 va_end(ap);
593
594 return offset - old_offset;
595}
596
597static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
598{
599 size_t old_offset = offset;
600 va_list ap;
601 int i;
602
603 va_start(ap, fmt);
604 for (i = 0; fmt[i]; i++) {
605 switch (fmt[i]) {
606 case 'b': {
607 uint8_t val = va_arg(ap, int);
608 offset += pdu_pack(pdu, offset, &val, sizeof(val));
609 break;
610 }
611 case 'w': {
612 uint16_t val;
613 cpu_to_le16w(&val, va_arg(ap, int));
614 offset += pdu_pack(pdu, offset, &val, sizeof(val));
615 break;
616 }
617 case 'd': {
618 uint32_t val;
619 cpu_to_le32w(&val, va_arg(ap, uint32_t));
620 offset += pdu_pack(pdu, offset, &val, sizeof(val));
621 break;
622 }
623 case 'q': {
624 uint64_t val;
625 cpu_to_le64w(&val, va_arg(ap, uint64_t));
626 offset += pdu_pack(pdu, offset, &val, sizeof(val));
627 break;
628 }
629 case 'v': {
630 struct iovec *iov = va_arg(ap, struct iovec *);
631 int *iovcnt = va_arg(ap, int *);
632 *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
633 break;
634 }
635 case 's': {
636 V9fsString *str = va_arg(ap, V9fsString *);
637 offset += pdu_marshal(pdu, offset, "w", str->size);
638 offset += pdu_pack(pdu, offset, str->data, str->size);
639 break;
640 }
641 case 'Q': {
642 V9fsQID *qidp = va_arg(ap, V9fsQID *);
643 offset += pdu_marshal(pdu, offset, "bdq",
644 qidp->type, qidp->version, qidp->path);
645 break;
646 }
647 case 'S': {
648 V9fsStat *statp = va_arg(ap, V9fsStat *);
649 offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
650 statp->size, statp->type, statp->dev,
651 &statp->qid, statp->mode, statp->atime,
652 statp->mtime, statp->length, &statp->name,
653 &statp->uid, &statp->gid, &statp->muid,
654 &statp->extension, statp->n_uid,
655 statp->n_gid, statp->n_muid);
656 break;
657 }
658 default:
659 break;
660 }
661 }
662 va_end(ap);
663
664 return offset - old_offset;
665}
666
667static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
668{
669 int8_t id = pdu->id + 1; /* Response */
670
671 if (len < 0) {
672 V9fsString str;
673 int err = -len;
674
675 str.data = strerror(err);
676 str.size = strlen(str.data);
677
678 len = 7;
679 len += pdu_marshal(pdu, len, "s", &str);
680 if (dotu) {
681 len += pdu_marshal(pdu, len, "d", err);
682 }
683
684 id = P9_RERROR;
685 }
686
687 /* fill out the header */
688 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
689
690 /* keep these in sync */
691 pdu->size = len;
692 pdu->id = id;
693
694 /* push onto queue and notify */
695 virtqueue_push(s->vq, &pdu->elem, len);
696
697 /* FIXME: we should batch these completions */
698 virtio_notify(&s->vdev, s->vq);
699
700 free_pdu(s, pdu);
701}
702
bb9e3216
AL
703static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
704{
705 mode_t ret;
706
707 ret = mode & 0777;
708 if (mode & P9_STAT_MODE_DIR) {
709 ret |= S_IFDIR;
710 }
711
712 if (dotu) {
713 if (mode & P9_STAT_MODE_SYMLINK) {
714 ret |= S_IFLNK;
715 }
716 if (mode & P9_STAT_MODE_SOCKET) {
717 ret |= S_IFSOCK;
718 }
719 if (mode & P9_STAT_MODE_NAMED_PIPE) {
720 ret |= S_IFIFO;
721 }
722 if (mode & P9_STAT_MODE_DEVICE) {
723 if (extension && extension->data[0] == 'c') {
724 ret |= S_IFCHR;
725 } else {
726 ret |= S_IFBLK;
727 }
728 }
729 }
730
731 if (!(ret&~0777)) {
732 ret |= S_IFREG;
733 }
734
735 if (mode & P9_STAT_MODE_SETUID) {
736 ret |= S_ISUID;
737 }
738 if (mode & P9_STAT_MODE_SETGID) {
739 ret |= S_ISGID;
740 }
741 if (mode & P9_STAT_MODE_SETVTX) {
742 ret |= S_ISVTX;
743 }
744
745 return ret;
746}
747
748static int donttouch_stat(V9fsStat *stat)
749{
750 if (stat->type == -1 &&
751 stat->dev == -1 &&
752 stat->qid.type == -1 &&
753 stat->qid.version == -1 &&
754 stat->qid.path == -1 &&
755 stat->mode == -1 &&
756 stat->atime == -1 &&
757 stat->mtime == -1 &&
758 stat->length == -1 &&
759 !stat->name.size &&
760 !stat->uid.size &&
761 !stat->gid.size &&
762 !stat->muid.size &&
763 stat->n_uid == -1 &&
764 stat->n_gid == -1 &&
765 stat->n_muid == -1) {
766 return 1;
767 }
768
769 return 0;
770}
771
772static void v9fs_stat_free(V9fsStat *stat)
773{
774 v9fs_string_free(&stat->name);
775 v9fs_string_free(&stat->uid);
776 v9fs_string_free(&stat->gid);
777 v9fs_string_free(&stat->muid);
778 v9fs_string_free(&stat->extension);
779}
780
781static uint32_t stat_to_v9mode(const struct stat *stbuf)
782{
783 uint32_t mode;
784
785 mode = stbuf->st_mode & 0777;
786 if (S_ISDIR(stbuf->st_mode)) {
787 mode |= P9_STAT_MODE_DIR;
788 }
789
790 if (dotu) {
791 if (S_ISLNK(stbuf->st_mode)) {
792 mode |= P9_STAT_MODE_SYMLINK;
793 }
794
795 if (S_ISSOCK(stbuf->st_mode)) {
796 mode |= P9_STAT_MODE_SOCKET;
797 }
798
799 if (S_ISFIFO(stbuf->st_mode)) {
800 mode |= P9_STAT_MODE_NAMED_PIPE;
801 }
802
803 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
804 mode |= P9_STAT_MODE_DEVICE;
805 }
806
807 if (stbuf->st_mode & S_ISUID) {
808 mode |= P9_STAT_MODE_SETUID;
809 }
810
811 if (stbuf->st_mode & S_ISGID) {
812 mode |= P9_STAT_MODE_SETGID;
813 }
814
815 if (stbuf->st_mode & S_ISVTX) {
816 mode |= P9_STAT_MODE_SETVTX;
817 }
818 }
819
820 return mode;
821}
822
823static int stat_to_v9stat(V9fsState *s, V9fsString *name,
824 const struct stat *stbuf,
825 V9fsStat *v9stat)
826{
827 int err;
828 const char *str;
829
830 memset(v9stat, 0, sizeof(*v9stat));
831
832 stat_to_qid(stbuf, &v9stat->qid);
833 v9stat->mode = stat_to_v9mode(stbuf);
834 v9stat->atime = stbuf->st_atime;
835 v9stat->mtime = stbuf->st_mtime;
836 v9stat->length = stbuf->st_size;
837
838 v9fs_string_null(&v9stat->uid);
839 v9fs_string_null(&v9stat->gid);
840 v9fs_string_null(&v9stat->muid);
841
842 if (dotu) {
843 v9stat->n_uid = stbuf->st_uid;
844 v9stat->n_gid = stbuf->st_gid;
845 v9stat->n_muid = 0;
846
847 v9fs_string_null(&v9stat->extension);
848
849 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
850 err = v9fs_do_readlink(s, name, &v9stat->extension);
851 if (err == -1) {
852 err = -errno;
853 return err;
854 }
855 v9stat->extension.data[err] = 0;
856 v9stat->extension.size = err;
857 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
858 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
859 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
860 major(stbuf->st_rdev), minor(stbuf->st_rdev));
861 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
862 v9fs_string_sprintf(&v9stat->extension, "%s %u",
863 "HARDLINKCOUNT", stbuf->st_nlink);
864 }
865 }
866
867 str = strrchr(name->data, '/');
868 if (str) {
869 str += 1;
870 } else {
871 str = name->data;
872 }
873
874 v9fs_string_sprintf(&v9stat->name, "%s", str);
875
876 v9stat->size = 61 +
877 v9fs_string_size(&v9stat->name) +
878 v9fs_string_size(&v9stat->uid) +
879 v9fs_string_size(&v9stat->gid) +
880 v9fs_string_size(&v9stat->muid) +
881 v9fs_string_size(&v9stat->extension);
882 return 0;
883}
884
1f5a89bf
AL
885static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
886{
887 while (len && *iovcnt) {
888 if (len < sg->iov_len) {
889 sg->iov_len -= len;
890 sg->iov_base += len;
891 len = 0;
892 } else {
893 len -= sg->iov_len;
894 sg++;
895 *iovcnt -= 1;
896 }
897 }
898
899 return sg;
900}
901
902static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
903{
904 int i;
905 int total = 0;
906
907 for (i = 0; i < *cnt; i++) {
908 if ((total + sg[i].iov_len) > cap) {
909 sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
910 i++;
911 break;
912 }
913 total += sg[i].iov_len;
914 }
915
916 *cnt = i;
917
918 return sg;
919}
920
921static void print_sg(struct iovec *sg, int cnt)
922{
923 int i;
924
925 printf("sg[%d]: {", cnt);
926 for (i = 0; i < cnt; i++) {
927 if (i) {
928 printf(", ");
929 }
930 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
931 }
932 printf("}\n");
933}
934
8cf89e00
AL
935static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
936{
937 V9fsString str;
938 v9fs_string_init(&str);
939 v9fs_string_copy(&str, dst);
940 v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
941 v9fs_string_free(&str);
942}
943
9f107513
AL
944static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
945{
92c1ad03
AL
946 int32_t msize;
947 V9fsString version;
948 size_t offset = 7;
949
950 pdu_unmarshal(pdu, offset, "ds", &msize, &version);
951
952 if (strcmp(version.data, "9P2000.u")) {
953 v9fs_string_sprintf(&version, "unknown");
9f107513 954 }
92c1ad03
AL
955
956 offset += pdu_marshal(pdu, offset, "ds", msize, &version);
957 complete_pdu(s, pdu, offset);
958
959 v9fs_string_free(&version);
9f107513
AL
960}
961
962static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
963{
955efc47
AL
964 int32_t fid, afid, n_uname;
965 V9fsString uname, aname;
966 V9fsFidState *fidp;
967 V9fsQID qid;
968 size_t offset = 7;
969 ssize_t err;
970
971 pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
972
973 fidp = alloc_fid(s, fid);
974 if (fidp == NULL) {
975 err = -EINVAL;
976 goto out;
9f107513 977 }
955efc47
AL
978
979 fidp->uid = n_uname;
980
981 v9fs_string_sprintf(&fidp->path, "%s", "/");
982 err = fid_to_qid(s, fidp, &qid);
983 if (err) {
984 err = -EINVAL;
985 free_fid(s, fid);
986 goto out;
987 }
988
989 offset += pdu_marshal(pdu, offset, "Q", &qid);
990
991 err = offset;
992out:
993 complete_pdu(s, pdu, err);
994 v9fs_string_free(&uname);
995 v9fs_string_free(&aname);
9f107513
AL
996}
997
4da7d3fa
AL
998typedef struct V9fsStatState {
999 V9fsPDU *pdu;
1000 size_t offset;
1001 V9fsStat v9stat;
1002 V9fsFidState *fidp;
1003 struct stat stbuf;
1004} V9fsStatState;
1005
1006static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1007{
1008 if (err == -1) {
1009 err = -errno;
1010 goto out;
1011 }
1012
1013 err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1014 if (err) {
1015 goto out;
1016 }
1017 vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1018 err = vs->offset;
1019
1020out:
1021 complete_pdu(s, vs->pdu, err);
1022 v9fs_stat_free(&vs->v9stat);
1023 qemu_free(vs);
1024}
1025
9f107513
AL
1026static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
1027{
4da7d3fa
AL
1028 int32_t fid;
1029 V9fsStatState *vs;
1030 ssize_t err = 0;
1031
1032 vs = qemu_malloc(sizeof(*vs));
1033 vs->pdu = pdu;
1034 vs->offset = 7;
1035
1036 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1037
1038 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1039
1040 vs->fidp = lookup_fid(s, fid);
1041 if (vs->fidp == NULL) {
1042 err = -ENOENT;
1043 goto out;
9f107513 1044 }
4da7d3fa
AL
1045
1046 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1047 v9fs_stat_post_lstat(s, vs, err);
1048 return;
1049
1050out:
1051 complete_pdu(s, vs->pdu, err);
1052 v9fs_stat_free(&vs->v9stat);
1053 qemu_free(vs);
9f107513
AL
1054}
1055
ff5e54c9
AL
1056typedef struct V9fsWalkState {
1057 V9fsPDU *pdu;
1058 size_t offset;
1059 int16_t nwnames;
1060 int name_idx;
1061 V9fsQID *qids;
1062 V9fsFidState *fidp;
1063 V9fsFidState *newfidp;
1064 V9fsString path;
1065 V9fsString *wnames;
1066 struct stat stbuf;
1067} V9fsWalkState;
1068
1069static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1070{
1071 complete_pdu(s, vs->pdu, err);
1072
1073 if (vs->nwnames) {
1074 for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1075 v9fs_string_free(&vs->wnames[vs->name_idx]);
1076 }
1077
1078 qemu_free(vs->wnames);
1079 qemu_free(vs->qids);
1080 }
1081}
1082
1083static void v9fs_walk_marshal(V9fsWalkState *vs)
1084{
1085 int i;
1086 vs->offset = 7;
1087 vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1088
1089 for (i = 0; i < vs->nwnames; i++) {
1090 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1091 }
1092}
1093
1094static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1095 int err)
1096{
1097 if (err == -1) {
1098 free_fid(s, vs->newfidp->fid);
1099 v9fs_string_free(&vs->path);
1100 err = -ENOENT;
1101 goto out;
1102 }
1103
1104 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1105
1106 vs->name_idx++;
1107 if (vs->name_idx < vs->nwnames) {
1108 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1109 vs->wnames[vs->name_idx].data);
1110 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1111
1112 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1113 v9fs_walk_post_newfid_lstat(s, vs, err);
1114 return;
1115 }
1116
1117 v9fs_string_free(&vs->path);
1118 v9fs_walk_marshal(vs);
1119 err = vs->offset;
1120out:
1121 v9fs_walk_complete(s, vs, err);
1122}
1123
1124static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1125 int err)
1126{
1127 if (err == -1) {
1128 v9fs_string_free(&vs->path);
1129 err = -ENOENT;
1130 goto out;
1131 }
1132
1133 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1134 vs->name_idx++;
1135 if (vs->name_idx < vs->nwnames) {
1136
1137 v9fs_string_sprintf(&vs->path, "%s/%s",
1138 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1139 v9fs_string_copy(&vs->fidp->path, &vs->path);
1140
1141 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1142 v9fs_walk_post_oldfid_lstat(s, vs, err);
1143 return;
1144 }
1145
1146 v9fs_string_free(&vs->path);
1147 v9fs_walk_marshal(vs);
1148 err = vs->offset;
1149out:
1150 v9fs_walk_complete(s, vs, err);
1151}
1152
9f107513
AL
1153static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1154{
ff5e54c9
AL
1155 int32_t fid, newfid;
1156 V9fsWalkState *vs;
1157 int err = 0;
1158 int i;
1159
1160 vs = qemu_malloc(sizeof(*vs));
1161 vs->pdu = pdu;
1162 vs->wnames = NULL;
1163 vs->qids = NULL;
1164 vs->offset = 7;
1165
1166 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1167 &newfid, &vs->nwnames);
1168
1169 if (vs->nwnames) {
1170 vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1171
1172 vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1173
1174 for (i = 0; i < vs->nwnames; i++) {
1175 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1176 &vs->wnames[i]);
1177 }
1178 }
1179
1180 vs->fidp = lookup_fid(s, fid);
1181 if (vs->fidp == NULL) {
1182 err = -ENOENT;
1183 goto out;
1184 }
1185
1186 /* FIXME: is this really valid? */
1187 if (fid == newfid) {
1188
1189 BUG_ON(vs->fidp->fd != -1);
1190 BUG_ON(vs->fidp->dir);
1191 v9fs_string_init(&vs->path);
1192 vs->name_idx = 0;
1193
1194 if (vs->name_idx < vs->nwnames) {
1195 v9fs_string_sprintf(&vs->path, "%s/%s",
1196 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1197 v9fs_string_copy(&vs->fidp->path, &vs->path);
1198
1199 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1200 v9fs_walk_post_oldfid_lstat(s, vs, err);
1201 return;
1202 }
1203 } else {
1204 vs->newfidp = alloc_fid(s, newfid);
1205 if (vs->newfidp == NULL) {
1206 err = -EINVAL;
1207 goto out;
1208 }
1209
1210 vs->newfidp->uid = vs->fidp->uid;
1211 v9fs_string_init(&vs->path);
1212 vs->name_idx = 0;
1213 v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1214
1215 if (vs->name_idx < vs->nwnames) {
1216 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1217 vs->wnames[vs->name_idx].data);
1218 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1219
1220 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1221 v9fs_walk_post_newfid_lstat(s, vs, err);
1222 return;
1223 }
9f107513 1224 }
ff5e54c9
AL
1225
1226 v9fs_walk_marshal(vs);
1227 err = vs->offset;
1228out:
1229 v9fs_walk_complete(s, vs, err);
9f107513
AL
1230}
1231
a6568fe2
AL
1232typedef struct V9fsOpenState {
1233 V9fsPDU *pdu;
1234 size_t offset;
1235 int8_t mode;
1236 V9fsFidState *fidp;
1237 V9fsQID qid;
1238 struct stat stbuf;
1239
1240} V9fsOpenState;
1241
1242enum {
1243 Oread = 0x00,
1244 Owrite = 0x01,
1245 Ordwr = 0x02,
1246 Oexec = 0x03,
1247 Oexcl = 0x04,
1248 Otrunc = 0x10,
1249 Orexec = 0x20,
1250 Orclose = 0x40,
1251 Oappend = 0x80,
1252};
1253
1254static int omode_to_uflags(int8_t mode)
9f107513 1255{
a6568fe2
AL
1256 int ret = 0;
1257
1258 switch (mode & 3) {
1259 case Oread:
1260 ret = O_RDONLY;
1261 break;
1262 case Ordwr:
1263 ret = O_RDWR;
1264 break;
1265 case Owrite:
1266 ret = O_WRONLY;
1267 break;
1268 case Oexec:
1269 ret = O_RDONLY;
1270 break;
1271 }
1272
1273 if (mode & Otrunc) {
1274 ret |= O_TRUNC;
1275 }
1276
1277 if (mode & Oappend) {
1278 ret |= O_APPEND;
9f107513 1279 }
a6568fe2
AL
1280
1281 if (mode & Oexcl) {
1282 ret |= O_EXCL;
1283 }
1284
1285 return ret;
1286}
1287
1288static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1289{
1290 if (vs->fidp->dir == NULL) {
1291 err = -errno;
1292 goto out;
1293 }
1294
1295 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1296 err = vs->offset;
1297out:
1298 complete_pdu(s, vs->pdu, err);
1299 qemu_free(vs);
1300
1301}
1302
1303static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1304{
1305 if (vs->fidp->fd == -1) {
1306 err = -errno;
1307 goto out;
1308 }
1309
1310 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1311 err = vs->offset;
1312out:
1313 complete_pdu(s, vs->pdu, err);
1314 qemu_free(vs);
1315}
1316
1317static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1318{
1319 if (err) {
1320 err = -errno;
1321 goto out;
1322 }
1323
1324 stat_to_qid(&vs->stbuf, &vs->qid);
1325
1326 if (S_ISDIR(vs->stbuf.st_mode)) {
1327 vs->fidp->dir = v9fs_do_opendir(s, &vs->fidp->path);
1328 v9fs_open_post_opendir(s, vs, err);
1329 } else {
1330 vs->fidp->fd = v9fs_do_open(s, &vs->fidp->path,
1331 omode_to_uflags(vs->mode));
1332 v9fs_open_post_open(s, vs, err);
1333 }
1334 return;
1335out:
1336 complete_pdu(s, vs->pdu, err);
1337 qemu_free(vs);
9f107513
AL
1338}
1339
1340static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
a6568fe2
AL
1341{
1342 int32_t fid;
1343 V9fsOpenState *vs;
1344 ssize_t err = 0;
1345
1346
1347 vs = qemu_malloc(sizeof(*vs));
1348 vs->pdu = pdu;
1349 vs->offset = 7;
1350
1351 pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1352
1353 vs->fidp = lookup_fid(s, fid);
1354 if (vs->fidp == NULL) {
1355 err = -ENOENT;
1356 goto out;
1357 }
1358
1359 BUG_ON(vs->fidp->fd != -1);
1360 BUG_ON(vs->fidp->dir);
1361
1362 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1363
1364 v9fs_open_post_lstat(s, vs, err);
1365 return;
1366out:
1367 complete_pdu(s, pdu, err);
1368 qemu_free(vs);
1369}
1370
1371static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1372{
bbd5697b
AL
1373 int32_t fid;
1374 size_t offset = 7;
1375 int err;
1376
1377 pdu_unmarshal(pdu, offset, "d", &fid);
1378
1379 err = free_fid(s, fid);
1380 if (err < 0) {
1381 goto out;
a6568fe2 1382 }
bbd5697b
AL
1383
1384 offset = 7;
1385 err = offset;
1386out:
1387 complete_pdu(s, pdu, err);
9f107513
AL
1388}
1389
a9231555
AL
1390typedef struct V9fsReadState {
1391 V9fsPDU *pdu;
1392 size_t offset;
1393 int32_t count;
1394 int32_t total;
1395 int64_t off;
1396 V9fsFidState *fidp;
1397 struct iovec iov[128]; /* FIXME: bad, bad, bad */
1398 struct iovec *sg;
1399 off_t dir_pos;
1400 struct dirent *dent;
1401 struct stat stbuf;
1402 V9fsString name;
1403 V9fsStat v9stat;
1404 int32_t len;
1405 int32_t cnt;
1406 int32_t max_count;
1407} V9fsReadState;
1408
1409static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1410
1411static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1412{
1413 if (err) {
1414 goto out;
1415 }
1416 v9fs_stat_free(&vs->v9stat);
1417 v9fs_string_free(&vs->name);
1418 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1419 vs->offset += vs->count;
1420 err = vs->offset;
1421out:
1422 complete_pdu(s, vs->pdu, err);
1423 qemu_free(vs);
1424 return;
1425}
1426
1427static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1428 ssize_t err)
1429{
1430 if (err) {
1431 err = -errno;
1432 goto out;
1433 }
1434 err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1435 if (err) {
1436 goto out;
1437 }
1438
1439 vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1440 &vs->v9stat);
1441 if ((vs->len != (vs->v9stat.size + 2)) ||
1442 ((vs->count + vs->len) > vs->max_count)) {
1443 v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1444 v9fs_read_post_seekdir(s, vs, err);
1445 return;
1446 }
1447 vs->count += vs->len;
1448 v9fs_stat_free(&vs->v9stat);
1449 v9fs_string_free(&vs->name);
1450 vs->dir_pos = vs->dent->d_off;
1451 vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1452 v9fs_read_post_readdir(s, vs, err);
1453 return;
1454out:
1455 v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1456 v9fs_read_post_seekdir(s, vs, err);
1457 return;
1458
1459}
1460
1461static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1462{
1463 if (vs->dent) {
1464 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1465 v9fs_string_init(&vs->name);
1466 v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1467 vs->dent->d_name);
1468 err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1469 v9fs_read_post_dir_lstat(s, vs, err);
1470 return;
1471 }
1472
1473 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1474 vs->offset += vs->count;
1475 err = vs->offset;
1476 complete_pdu(s, vs->pdu, err);
1477 qemu_free(vs);
1478 return;
1479}
1480
1481static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1482{
1483 vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1484 v9fs_read_post_readdir(s, vs, err);
1485 return;
1486}
1487
1488static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
1489 ssize_t err)
1490{
1491 vs->dir_pos = v9fs_do_telldir(s, vs->fidp->dir);
1492 v9fs_read_post_telldir(s, vs, err);
1493 return;
1494}
1495
1496static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *vs, ssize_t err)
1497{
1498 if (err < 0) {
1499 /* IO error return the error */
1500 err = -errno;
1501 goto out;
1502 }
1503 vs->total += vs->len;
1504 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1505 if (vs->total < vs->count && vs->len > 0) {
1506 do {
1507 if (0) {
1508 print_sg(vs->sg, vs->cnt);
1509 }
1510 vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1511 } while (vs->len == -1 && errno == EINTR);
1512 if (vs->len == -1) {
1513 err = -errno;
1514 }
1515 v9fs_read_post_readv(s, vs, err);
1516 return;
1517 }
1518 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1519 vs->offset += vs->count;
1520 err = vs->offset;
1521
1522out:
1523 complete_pdu(s, vs->pdu, err);
1524 qemu_free(vs);
1525}
1526
1527static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err)
1528{
1529 if (err == -1) {
1530 err = -errno;
1531 goto out;
1532 }
1533 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1534
1535 if (vs->total < vs->count) {
1536 do {
1537 if (0) {
1538 print_sg(vs->sg, vs->cnt);
1539 }
1540 vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1541 } while (vs->len == -1 && errno == EINTR);
1542 if (vs->len == -1) {
1543 err = -errno;
1544 }
1545 v9fs_read_post_readv(s, vs, err);
1546 return;
1547 }
1548out:
1549 complete_pdu(s, vs->pdu, err);
1550 qemu_free(vs);
1551}
1552
9f107513
AL
1553static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
1554{
a9231555
AL
1555 int32_t fid;
1556 V9fsReadState *vs;
1557 ssize_t err = 0;
1558
1559 vs = qemu_malloc(sizeof(*vs));
1560 vs->pdu = pdu;
1561 vs->offset = 7;
1562 vs->total = 0;
1563 vs->len = 0;
1564 vs->count = 0;
1565
1566 pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count);
1567
1568 vs->fidp = lookup_fid(s, fid);
1569 if (vs->fidp == NULL) {
1570 err = -EINVAL;
1571 goto out;
1572 }
1573
1574 if (vs->fidp->dir) {
1575 vs->max_count = vs->count;
1576 vs->count = 0;
1577 if (vs->off == 0) {
1578 v9fs_do_rewinddir(s, vs->fidp->dir);
1579 }
1580 v9fs_read_post_rewinddir(s, vs, err);
1581 return;
1582 } else if (vs->fidp->fd != -1) {
1583 vs->sg = vs->iov;
1584 pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
1585 err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1586 v9fs_read_post_lseek(s, vs, err);
1587 return;
1588 } else {
1589 err = -EINVAL;
9f107513 1590 }
a9231555
AL
1591out:
1592 complete_pdu(s, pdu, err);
1593 qemu_free(vs);
9f107513
AL
1594}
1595
8449360c
AL
1596typedef struct V9fsWriteState {
1597 V9fsPDU *pdu;
1598 size_t offset;
1599 int32_t len;
1600 int32_t count;
1601 int32_t total;
1602 int64_t off;
1603 V9fsFidState *fidp;
1604 struct iovec iov[128]; /* FIXME: bad, bad, bad */
1605 struct iovec *sg;
1606 int cnt;
1607} V9fsWriteState;
1608
1609static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs,
1610 ssize_t err)
1611{
1612 if (err < 0) {
1613 /* IO error return the error */
1614 err = -errno;
1615 goto out;
1616 }
1617 vs->total += vs->len;
1618 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1619 if (vs->total < vs->count && vs->len > 0) {
1620 do {
1621 if (0) {
1622 print_sg(vs->sg, vs->cnt);
1623 }
1624 vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
1625 } while (vs->len == -1 && errno == EINTR);
1626 if (vs->len == -1) {
1627 err = -errno;
1628 }
1629 v9fs_write_post_writev(s, vs, err);
1630 return;
1631 }
1632 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1633
1634 err = vs->offset;
1635out:
1636 complete_pdu(s, vs->pdu, err);
1637 qemu_free(vs);
1638}
1639
1640static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err)
1641{
1642 if (err == -1) {
1643 err = -errno;
1644 goto out;
1645 }
1646 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1647
1648 if (vs->total < vs->count) {
1649 do {
1650 if (0) {
1651 print_sg(vs->sg, vs->cnt);
1652 }
1653 vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
1654 } while (vs->len == -1 && errno == EINTR);
1655 if (vs->len == -1) {
1656 err = -errno;
1657 }
1658 v9fs_write_post_writev(s, vs, err);
1659 return;
1660 }
1661
1662out:
1663 complete_pdu(s, vs->pdu, err);
1664 qemu_free(vs);
1665}
1666
9f107513
AL
1667static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
1668{
8449360c
AL
1669 int32_t fid;
1670 V9fsWriteState *vs;
1671 ssize_t err;
1672
1673 vs = qemu_malloc(sizeof(*vs));
1674
1675 vs->pdu = pdu;
1676 vs->offset = 7;
1677 vs->sg = vs->iov;
1678 vs->total = 0;
1679 vs->len = 0;
1680
1681 pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
1682 vs->sg, &vs->cnt);
1683
1684 vs->fidp = lookup_fid(s, fid);
1685 if (vs->fidp == NULL) {
1686 err = -EINVAL;
1687 goto out;
9f107513 1688 }
8449360c
AL
1689
1690 if (vs->fidp->fd == -1) {
1691 err = -EINVAL;
1692 goto out;
1693 }
1694
1695 err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1696
1697 v9fs_write_post_lseek(s, vs, err);
1698 return;
1699
1700out:
1701 complete_pdu(s, vs->pdu, err);
1702 qemu_free(vs);
9f107513
AL
1703}
1704
c494dd6f
AL
1705typedef struct V9fsCreateState {
1706 V9fsPDU *pdu;
1707 size_t offset;
1708 V9fsFidState *fidp;
1709 V9fsQID qid;
1710 int32_t perm;
1711 int8_t mode;
1712 struct stat stbuf;
1713 V9fsString name;
1714 V9fsString extension;
1715 V9fsString fullname;
1716} V9fsCreateState;
1717
1718static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
1719{
1720 if (err == 0) {
1721 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1722 stat_to_qid(&vs->stbuf, &vs->qid);
1723
1724 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1725
1726 err = vs->offset;
1727 }
1728
1729 complete_pdu(s, vs->pdu, err);
1730 v9fs_string_free(&vs->name);
1731 v9fs_string_free(&vs->extension);
1732 v9fs_string_free(&vs->fullname);
1733 qemu_free(vs);
1734}
1735
1736static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
1737{
1738 if (err) {
1739 err = -errno;
1740 }
1741 v9fs_post_create(s, vs, err);
1742}
1743
1744static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
1745 int err)
1746{
1747 if (!vs->fidp->dir) {
1748 err = -errno;
1749 }
1750 v9fs_post_create(s, vs, err);
1751}
1752
1753static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
1754 int err)
1755{
1756 if (err) {
1757 err = -errno;
1758 goto out;
1759 }
1760
1761 vs->fidp->dir = v9fs_do_opendir(s, &vs->fullname);
1762 v9fs_create_post_opendir(s, vs, err);
1763 return;
1764
1765out:
1766 v9fs_post_create(s, vs, err);
1767}
1768
1769static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
1770{
1771 if (err) {
1772 err = -errno;
1773 goto out;
1774 }
1775
1776 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1777 v9fs_create_post_dir_lstat(s, vs, err);
1778 return;
1779
1780out:
1781 v9fs_post_create(s, vs, err);
1782}
1783
1784static void v9fs_create_post_mksock(V9fsState *s, V9fsCreateState *vs,
1785 int err)
1786{
1787 if (err) {
1788 err = -errno;
1789 goto out;
1790 }
1791
1792 err = v9fs_do_chmod(s, &vs->fullname, vs->perm & 0777);
1793 v9fs_create_post_perms(s, vs, err);
1794 return;
1795
1796out:
1797 v9fs_post_create(s, vs, err);
1798}
1799
1800static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
1801{
1802 if (err) {
1803 vs->fidp->fd = -1;
1804 err = -errno;
1805 }
1806
1807 v9fs_post_create(s, vs, err);
1808 return;
1809}
1810
1811static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
1812{
1813 if (vs->fidp->fd == -1) {
1814 err = -errno;
1815 goto out;
1816 }
1817
1818 err = v9fs_do_fstat(s, vs->fidp->fd, &vs->stbuf);
1819 v9fs_create_post_fstat(s, vs, err);
1820
1821 return;
1822
1823out:
1824 v9fs_post_create(s, vs, err);
1825
1826}
1827
1828static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
1829{
1830
1831 if (err == 0 || errno != ENOENT) {
1832 err = -errno;
1833 goto out;
1834 }
1835
1836 if (vs->perm & P9_STAT_MODE_DIR) {
1837 err = v9fs_do_mkdir(s, &vs->fullname, vs->perm & 0777);
1838 v9fs_create_post_mkdir(s, vs, err);
1839 } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
1840 err = v9fs_do_symlink(s, &vs->extension, &vs->fullname);
1841 v9fs_create_post_perms(s, vs, err);
1842 } else if (vs->perm & P9_STAT_MODE_LINK) {
1843 int32_t nfid = atoi(vs->extension.data);
1844 V9fsFidState *nfidp = lookup_fid(s, nfid);
1845 if (nfidp == NULL) {
1846 err = -errno;
1847 v9fs_post_create(s, vs, err);
1848 }
1849 err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
1850 v9fs_create_post_perms(s, vs, err);
1851 } else if (vs->perm & P9_STAT_MODE_DEVICE) {
1852 char ctype;
1853 uint32_t major, minor;
1854 mode_t nmode = 0;
1855
1856 if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
1857 &minor) != 3) {
1858 err = -errno;
1859 v9fs_post_create(s, vs, err);
1860 }
1861
1862 switch (ctype) {
1863 case 'c':
1864 nmode = S_IFCHR;
1865 break;
1866 case 'b':
1867 nmode = S_IFBLK;
1868 break;
1869 default:
1870 err = -EIO;
1871 v9fs_post_create(s, vs, err);
1872 }
1873
1874 nmode |= vs->perm & 0777;
1875 err = v9fs_do_mknod(s, &vs->fullname, nmode, makedev(major, minor));
1876 v9fs_create_post_perms(s, vs, err);
1877 } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
1878 err = v9fs_do_mknod(s, &vs->fullname, S_IFIFO | (vs->mode & 0777), 0);
1879 v9fs_post_create(s, vs, err);
1880 } else if (vs->perm & P9_STAT_MODE_SOCKET) {
1881 err = v9fs_do_mksock(s, &vs->fullname);
1882 v9fs_create_post_mksock(s, vs, err);
1883 } else {
1884 vs->fidp->fd = v9fs_do_open2(s, &vs->fullname,
1885 omode_to_uflags(vs->mode) | O_CREAT,
1886 vs->perm & 0777);
1887 v9fs_create_post_open2(s, vs, err);
1888 }
1889
1890 return;
1891
1892out:
1893 v9fs_post_create(s, vs, err);
1894}
1895
9f107513
AL
1896static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
1897{
c494dd6f
AL
1898 int32_t fid;
1899 V9fsCreateState *vs;
1900 int err = 0;
1901
1902 vs = qemu_malloc(sizeof(*vs));
1903 vs->pdu = pdu;
1904 vs->offset = 7;
1905
1906 v9fs_string_init(&vs->fullname);
1907
1908 pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
1909 &vs->perm, &vs->mode, &vs->extension);
1910
1911 vs->fidp = lookup_fid(s, fid);
1912 if (vs->fidp == NULL) {
1913 err = -EINVAL;
1914 goto out;
9f107513 1915 }
c494dd6f
AL
1916
1917 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
1918 vs->name.data);
1919
1920 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1921 v9fs_create_post_lstat(s, vs, err);
1922 return;
1923
1924out:
1925 complete_pdu(s, vs->pdu, err);
1926 v9fs_string_free(&vs->name);
1927 v9fs_string_free(&vs->extension);
1928 qemu_free(vs);
9f107513
AL
1929}
1930
1931static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
1932{
9c5e9d89
AL
1933 /* A nop call with no return */
1934 complete_pdu(s, pdu, 7);
9f107513
AL
1935}
1936
5bae1900
AL
1937typedef struct V9fsRemoveState {
1938 V9fsPDU *pdu;
1939 size_t offset;
1940 V9fsFidState *fidp;
1941} V9fsRemoveState;
1942
1943static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
1944 int err)
1945{
1946 /* For TREMOVE we need to clunk the fid even on failed remove */
1947 err = free_fid(s, vs->fidp->fid);
1948 if (err < 0) {
1949 goto out;
1950 }
1951
1952 err = vs->offset;
1953out:
1954 complete_pdu(s, vs->pdu, err);
1955 qemu_free(vs);
1956}
1957
9f107513
AL
1958static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
1959{
5bae1900
AL
1960 int32_t fid;
1961 V9fsRemoveState *vs;
1962 int err = 0;
1963
1964 vs = qemu_malloc(sizeof(*vs));
1965 vs->pdu = pdu;
1966 vs->offset = 7;
1967
1968 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1969
1970 vs->fidp = lookup_fid(s, fid);
1971 if (vs->fidp == NULL) {
1972 err = -EINVAL;
1973 goto out;
9f107513 1974 }
5bae1900
AL
1975
1976 err = v9fs_do_remove(s, &vs->fidp->path);
1977 v9fs_remove_post_remove(s, vs, err);
1978 return;
1979
1980out:
1981 complete_pdu(s, pdu, err);
1982 qemu_free(vs);
9f107513
AL
1983}
1984
8cf89e00
AL
1985typedef struct V9fsWstatState
1986{
1987 V9fsPDU *pdu;
1988 size_t offset;
1989 int16_t unused;
1990 V9fsStat v9stat;
1991 V9fsFidState *fidp;
1992 struct stat stbuf;
1993 V9fsString nname;
1994} V9fsWstatState;
1995
1996static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
1997{
1998 if (err < 0) {
1999 goto out;
2000 }
2001
2002 err = vs->offset;
2003
2004out:
2005 v9fs_stat_free(&vs->v9stat);
2006 complete_pdu(s, vs->pdu, err);
2007 qemu_free(vs);
2008}
2009
2010static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
2011{
2012 if (err < 0) {
2013 goto out;
2014 }
2015
2016 if (vs->v9stat.name.size != 0) {
2017 v9fs_string_free(&vs->nname);
2018 }
2019
2020 if (vs->v9stat.length != -1) {
2021 if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
2022 err = -errno;
2023 }
2024 }
2025 v9fs_wstat_post_truncate(s, vs, err);
2026 return;
2027
2028out:
2029 v9fs_stat_free(&vs->v9stat);
2030 complete_pdu(s, vs->pdu, err);
2031 qemu_free(vs);
2032}
2033
2034static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
2035{
2036 V9fsFidState *fidp;
2037 if (err < 0) {
2038 goto out;
2039 }
2040
2041 if (vs->v9stat.name.size != 0) {
2042 char *old_name, *new_name;
2043 char *end;
2044
2045 old_name = vs->fidp->path.data;
2046 end = strrchr(old_name, '/');
2047 if (end) {
2048 end++;
2049 } else {
2050 end = old_name;
2051 }
2052
2053 new_name = qemu_malloc(end - old_name + vs->v9stat.name.size + 1);
2054
2055 memset(new_name, 0, end - old_name + vs->v9stat.name.size + 1);
2056 memcpy(new_name, old_name, end - old_name);
2057 memcpy(new_name + (end - old_name), vs->v9stat.name.data,
2058 vs->v9stat.name.size);
2059 vs->nname.data = new_name;
2060 vs->nname.size = strlen(new_name);
2061
2062 if (strcmp(new_name, vs->fidp->path.data) != 0) {
2063 if (v9fs_do_rename(s, &vs->fidp->path, &vs->nname)) {
2064 err = -errno;
2065 } else {
2066 /*
2067 * Fixup fid's pointing to the old name to
2068 * start pointing to the new name
2069 */
2070 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2071
2072 if (vs->fidp == fidp) {
2073 /*
2074 * we replace name of this fid towards the end
2075 * so that our below strcmp will work
2076 */
2077 continue;
2078 }
2079 if (!strncmp(vs->fidp->path.data, fidp->path.data,
2080 strlen(vs->fidp->path.data))) {
2081 /* replace the name */
2082 v9fs_fix_path(&fidp->path, &vs->nname,
2083 strlen(vs->fidp->path.data));
2084 }
2085 }
2086 v9fs_string_copy(&vs->fidp->path, &vs->nname);
2087 }
2088 }
2089 }
2090 v9fs_wstat_post_rename(s, vs, err);
2091 return;
2092
2093out:
2094 v9fs_stat_free(&vs->v9stat);
2095 complete_pdu(s, vs->pdu, err);
2096 qemu_free(vs);
2097}
2098
2099static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2100{
2101 if (err < 0) {
2102 goto out;
2103 }
2104
2105 if (vs->v9stat.n_gid != -1) {
2106 if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2107 vs->v9stat.n_gid)) {
2108 err = -errno;
2109 }
2110 }
2111 v9fs_wstat_post_chown(s, vs, err);
2112 return;
2113
2114out:
2115 v9fs_stat_free(&vs->v9stat);
2116 complete_pdu(s, vs->pdu, err);
2117 qemu_free(vs);
2118}
2119
2120static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2121{
2122 if (err < 0) {
2123 goto out;
2124 }
2125
2126 if (vs->v9stat.mtime != -1) {
2127 struct utimbuf tb;
2128 tb.actime = 0;
2129 tb.modtime = vs->v9stat.mtime;
2130 if (v9fs_do_utime(s, &vs->fidp->path, &tb)) {
2131 err = -errno;
2132 }
2133 }
2134
2135 v9fs_wstat_post_utime(s, vs, err);
2136 return;
2137
2138out:
2139 v9fs_stat_free(&vs->v9stat);
2140 complete_pdu(s, vs->pdu, err);
2141 qemu_free(vs);
2142}
2143
2144static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2145{
2146 if (err == -1) {
2147 err = -errno;
2148 }
2149 v9fs_stat_free(&vs->v9stat);
2150 complete_pdu(s, vs->pdu, err);
2151 qemu_free(vs);
2152}
2153
2154static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2155{
2156 uint32_t v9_mode;
2157
2158 if (err == -1) {
2159 err = -errno;
2160 goto out;
2161 }
2162
2163 v9_mode = stat_to_v9mode(&vs->stbuf);
2164
2165 if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2166 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2167 /* Attempting to change the type */
2168 err = -EIO;
2169 goto out;
2170 }
2171
2172 if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2173 &vs->v9stat.extension))) {
2174 err = -errno;
2175 }
2176 v9fs_wstat_post_chmod(s, vs, err);
2177 return;
2178
2179out:
2180 v9fs_stat_free(&vs->v9stat);
2181 complete_pdu(s, vs->pdu, err);
2182 qemu_free(vs);
2183}
2184
9f107513
AL
2185static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
2186{
8cf89e00
AL
2187 int32_t fid;
2188 V9fsWstatState *vs;
2189 int err = 0;
2190
2191 vs = qemu_malloc(sizeof(*vs));
2192 vs->pdu = pdu;
2193 vs->offset = 7;
2194
2195 pdu_unmarshal(pdu, vs->offset, "dwS", &fid, &vs->unused, &vs->v9stat);
2196
2197 vs->fidp = lookup_fid(s, fid);
2198 if (vs->fidp == NULL) {
2199 err = -EINVAL;
2200 goto out;
9f107513 2201 }
8cf89e00
AL
2202
2203 /* do we need to sync the file? */
2204 if (donttouch_stat(&vs->v9stat)) {
2205 err = v9fs_do_fsync(s, vs->fidp->fd);
2206 v9fs_wstat_post_fsync(s, vs, err);
2207 return;
2208 }
2209
2210 if (vs->v9stat.mode != -1) {
2211 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
2212 v9fs_wstat_post_lstat(s, vs, err);
2213 return;
2214 }
2215
2216 v9fs_wstat_post_chmod(s, vs, err);
2217 return;
2218
2219out:
2220 v9fs_stat_free(&vs->v9stat);
2221 complete_pdu(s, vs->pdu, err);
2222 qemu_free(vs);
9f107513
AL
2223}
2224
2225typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
2226
2227static pdu_handler_t *pdu_handlers[] = {
2228 [P9_TVERSION] = v9fs_version,
2229 [P9_TATTACH] = v9fs_attach,
2230 [P9_TSTAT] = v9fs_stat,
2231 [P9_TWALK] = v9fs_walk,
2232 [P9_TCLUNK] = v9fs_clunk,
2233 [P9_TOPEN] = v9fs_open,
2234 [P9_TREAD] = v9fs_read,
2235#if 0
2236 [P9_TAUTH] = v9fs_auth,
2237#endif
2238 [P9_TFLUSH] = v9fs_flush,
2239 [P9_TCREATE] = v9fs_create,
2240 [P9_TWRITE] = v9fs_write,
2241 [P9_TWSTAT] = v9fs_wstat,
2242 [P9_TREMOVE] = v9fs_remove,
2243};
2244
2245static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
2246{
2247 pdu_handler_t *handler;
2248
2249 if (debug_9p_pdu) {
2250 pprint_pdu(pdu);
2251 }
2252
2253 BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
2254
2255 handler = pdu_handlers[pdu->id];
2256 BUG_ON(handler == NULL);
2257
2258 handler(s, pdu);
2259}
2260
2261static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
2262{
2263 V9fsState *s = (V9fsState *)vdev;
2264 V9fsPDU *pdu;
2265 ssize_t len;
2266
2267 while ((pdu = alloc_pdu(s)) &&
2268 (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
2269 uint8_t *ptr;
2270
2271 BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
2272 BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
2273
2274 ptr = pdu->elem.out_sg[0].iov_base;
2275
2276 memcpy(&pdu->size, ptr, 4);
2277 pdu->id = ptr[4];
2278 memcpy(&pdu->tag, ptr + 5, 2);
2279
2280 submit_pdu(s, pdu);
2281 }
2282
2283 free_pdu(s, pdu);
2284}
2285
2286static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
2287{
2288 features |= 1 << VIRTIO_9P_MOUNT_TAG;
2289 return features;
2290}
2291
2292static V9fsState *to_virtio_9p(VirtIODevice *vdev)
2293{
2294 return (V9fsState *)vdev;
2295}
2296
2297static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
2298{
2299 struct virtio_9p_config *cfg;
2300 V9fsState *s = to_virtio_9p(vdev);
2301
2302 cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
2303 s->tag_len);
2304 stw_raw(&cfg->tag_len, s->tag_len);
2305 memcpy(cfg->tag, s->tag, s->tag_len);
2306 memcpy(config, cfg, s->config_size);
2307 qemu_free(cfg);
2308}
2309
2310VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
2311 {
2312 V9fsState *s;
2313 int i, len;
2314 struct stat stat;
2315 FsTypeEntry *fse;
2316
2317
2318 s = (V9fsState *)virtio_common_init("virtio-9p",
2319 VIRTIO_ID_9P,
2320 sizeof(struct virtio_9p_config)+
2321 MAX_TAG_LEN,
2322 sizeof(V9fsState));
2323
2324 /* initialize pdu allocator */
2325 QLIST_INIT(&s->free_list);
2326 for (i = 0; i < (MAX_REQ - 1); i++) {
2327 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
2328 }
2329
2330 s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
2331
2332 fse = get_fsdev_fsentry(conf->fsdev_id);
2333
2334 if (!fse) {
2335 /* We don't have a fsdev identified by fsdev_id */
2336 fprintf(stderr, "Virtio-9p device couldn't find fsdev "
2337 "with the id %s\n", conf->fsdev_id);
2338 exit(1);
2339 }
2340
2341 if (!fse->path || !conf->tag) {
2342 /* we haven't specified a mount_tag or the path */
2343 fprintf(stderr, "fsdev with id %s needs path "
2344 "and Virtio-9p device needs mount_tag arguments\n",
2345 conf->fsdev_id);
2346 exit(1);
2347 }
2348
2349 if (lstat(fse->path, &stat)) {
2350 fprintf(stderr, "share path %s does not exist\n", fse->path);
2351 exit(1);
2352 } else if (!S_ISDIR(stat.st_mode)) {
2353 fprintf(stderr, "share path %s is not a directory \n", fse->path);
2354 exit(1);
2355 }
2356
2357 s->ctx.fs_root = qemu_strdup(fse->path);
2358 len = strlen(conf->tag);
2359 if (len > MAX_TAG_LEN) {
2360 len = MAX_TAG_LEN;
2361 }
2362 /* s->tag is non-NULL terminated string */
2363 s->tag = qemu_malloc(len);
2364 memcpy(s->tag, conf->tag, len);
2365 s->tag_len = len;
2366 s->ctx.uid = -1;
2367
2368 s->ops = fse->ops;
2369 s->vdev.get_features = virtio_9p_get_features;
2370 s->config_size = sizeof(struct virtio_9p_config) +
2371 s->tag_len;
2372 s->vdev.get_config = virtio_9p_get_config;
2373
2374 return &s->vdev;
2375}