virtio-9p: Add P9_TOPEN support.
[qemu/aliguori-queue.git] / hw / virtio-9p.c
blobe98da1b4414fa72f11db5febc3ccfe7e363c12ad
1 /*
2 * Virtio 9p backend
4 * Copyright IBM, Corp. 2010
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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"
21 int dotu = 1;
22 int debug_9p_pdu;
24 static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf)
26 return s->ops->lstat(&s->ctx, path->data, stbuf);
29 static int v9fs_do_setuid(V9fsState *s, uid_t uid)
31 return s->ops->setuid(&s->ctx, uid);
34 static ssize_t v9fs_do_readlink(V9fsState *s, V9fsString *path, V9fsString *buf)
36 ssize_t len;
38 buf->data = qemu_malloc(1024);
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;
46 return len;
49 static int v9fs_do_close(V9fsState *s, int fd)
51 return s->ops->close(&s->ctx, fd);
54 static int v9fs_do_closedir(V9fsState *s, DIR *dir)
56 return s->ops->closedir(&s->ctx, dir);
59 static int v9fs_do_open(V9fsState *s, V9fsString *path, int flags)
61 return s->ops->open(&s->ctx, path->data, flags);
64 static DIR *v9fs_do_opendir(V9fsState *s, V9fsString *path)
66 return s->ops->opendir(&s->ctx, path->data);
69 static void v9fs_string_init(V9fsString *str)
71 str->data = NULL;
72 str->size = 0;
75 static void v9fs_string_free(V9fsString *str)
77 qemu_free(str->data);
78 str->data = NULL;
79 str->size = 0;
82 static void v9fs_string_null(V9fsString *str)
84 v9fs_string_free(str);
87 static int number_to_string(void *arg, char type)
89 unsigned int ret = 0;
91 switch (type) {
92 case 'u': {
93 unsigned int num = *(unsigned int *)arg;
95 do {
96 ret++;
97 num = num/10;
98 } while (num);
99 break;
101 default:
102 printf("Number_to_string: Unknown number format\n");
103 return -1;
106 return ret;
109 static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
111 va_list ap2;
112 char *iter = (char *)fmt;
113 int len = 0;
114 int nr_args = 0;
115 char *arg_char_ptr;
116 unsigned int arg_uint;
118 /* Find the number of %'s that denotes an argument */
119 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
120 nr_args++;
121 iter++;
124 len = strlen(fmt) - 2*nr_args;
126 if (!nr_args) {
127 goto alloc_print;
130 va_copy(ap2, ap);
132 iter = (char *)fmt;
134 /* Now parse the format string */
135 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
136 iter++;
137 switch (*iter) {
138 case 'u':
139 arg_uint = va_arg(ap2, unsigned int);
140 len += number_to_string((void *)&arg_uint, 'u');
141 break;
142 case 's':
143 arg_char_ptr = va_arg(ap2, char *);
144 len += strlen(arg_char_ptr);
145 break;
146 case 'c':
147 len += 1;
148 break;
149 default:
150 fprintf(stderr,
151 "v9fs_string_alloc_printf:Incorrect format %c", *iter);
152 return -1;
154 iter++;
157 alloc_print:
158 *strp = qemu_malloc((len + 1) * sizeof(**strp));
160 return vsprintf(*strp, fmt, ap);
163 static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
165 va_list ap;
166 int err;
168 v9fs_string_free(str);
170 va_start(ap, fmt);
171 err = v9fs_string_alloc_printf(&str->data, fmt, ap);
172 BUG_ON(err == -1);
173 va_end(ap);
175 str->size = err;
178 static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
180 v9fs_string_free(lhs);
181 v9fs_string_sprintf(lhs, "%s", rhs->data);
184 static size_t v9fs_string_size(V9fsString *str)
186 return str->size;
189 static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
191 V9fsFidState *f;
193 for (f = s->fid_list; f; f = f->next) {
194 if (f->fid == fid) {
195 v9fs_do_setuid(s, f->uid);
196 return f;
200 return NULL;
203 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
205 V9fsFidState *f;
207 f = lookup_fid(s, fid);
208 if (f) {
209 return NULL;
212 f = qemu_mallocz(sizeof(V9fsFidState));
214 f->fid = fid;
215 f->fd = -1;
216 f->dir = NULL;
218 f->next = s->fid_list;
219 s->fid_list = f;
221 return f;
224 static int free_fid(V9fsState *s, int32_t fid)
226 V9fsFidState **fidpp, *fidp;
228 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
229 if ((*fidpp)->fid == fid) {
230 break;
234 if (*fidpp == NULL) {
235 return -ENOENT;
238 fidp = *fidpp;
239 *fidpp = fidp->next;
241 if (fidp->fd != -1) {
242 v9fs_do_close(s, fidp->fd);
244 if (fidp->dir) {
245 v9fs_do_closedir(s, fidp->dir);
247 v9fs_string_free(&fidp->path);
248 qemu_free(fidp);
250 return 0;
253 #define P9_QID_TYPE_DIR 0x80
254 #define P9_QID_TYPE_SYMLINK 0x02
256 #define P9_STAT_MODE_DIR 0x80000000
257 #define P9_STAT_MODE_APPEND 0x40000000
258 #define P9_STAT_MODE_EXCL 0x20000000
259 #define P9_STAT_MODE_MOUNT 0x10000000
260 #define P9_STAT_MODE_AUTH 0x08000000
261 #define P9_STAT_MODE_TMP 0x04000000
262 #define P9_STAT_MODE_SYMLINK 0x02000000
263 #define P9_STAT_MODE_LINK 0x01000000
264 #define P9_STAT_MODE_DEVICE 0x00800000
265 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
266 #define P9_STAT_MODE_SOCKET 0x00100000
267 #define P9_STAT_MODE_SETUID 0x00080000
268 #define P9_STAT_MODE_SETGID 0x00040000
269 #define P9_STAT_MODE_SETVTX 0x00010000
271 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
272 P9_STAT_MODE_SYMLINK | \
273 P9_STAT_MODE_LINK | \
274 P9_STAT_MODE_DEVICE | \
275 P9_STAT_MODE_NAMED_PIPE | \
276 P9_STAT_MODE_SOCKET)
278 /* This is the algorithm from ufs in spfs */
279 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
281 size_t size;
283 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
284 memcpy(&qidp->path, &stbuf->st_ino, size);
285 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
286 qidp->type = 0;
287 if (S_ISDIR(stbuf->st_mode)) {
288 qidp->type |= P9_QID_TYPE_DIR;
290 if (S_ISLNK(stbuf->st_mode)) {
291 qidp->type |= P9_QID_TYPE_SYMLINK;
295 static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
297 struct stat stbuf;
298 int err;
300 err = v9fs_do_lstat(s, &fidp->path, &stbuf);
301 if (err) {
302 return err;
305 stat_to_qid(&stbuf, qidp);
306 return 0;
309 static V9fsPDU *alloc_pdu(V9fsState *s)
311 V9fsPDU *pdu = NULL;
313 if (!QLIST_EMPTY(&s->free_list)) {
314 pdu = QLIST_FIRST(&s->free_list);
315 QLIST_REMOVE(pdu, next);
317 return pdu;
320 static void free_pdu(V9fsState *s, V9fsPDU *pdu)
322 if (pdu) {
323 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
327 size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
328 size_t offset, size_t size, int pack)
330 int i = 0;
331 size_t copied = 0;
333 for (i = 0; size && i < sg_count; i++) {
334 size_t len;
335 if (offset >= sg[i].iov_len) {
336 /* skip this sg */
337 offset -= sg[i].iov_len;
338 continue;
339 } else {
340 len = MIN(sg[i].iov_len - offset, size);
341 if (pack) {
342 memcpy(sg[i].iov_base + offset, addr, len);
343 } else {
344 memcpy(addr, sg[i].iov_base + offset, len);
346 size -= len;
347 copied += len;
348 addr += len;
349 if (size) {
350 offset = 0;
351 continue;
356 return copied;
359 static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
361 return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
362 offset, size, 0);
365 static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
366 size_t size)
368 return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
369 offset, size, 1);
372 static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
374 size_t pos = 0;
375 int i, j;
376 struct iovec *src_sg;
377 unsigned int num;
379 if (rx) {
380 src_sg = pdu->elem.in_sg;
381 num = pdu->elem.in_num;
382 } else {
383 src_sg = pdu->elem.out_sg;
384 num = pdu->elem.out_num;
387 j = 0;
388 for (i = 0; i < num; i++) {
389 if (offset <= pos) {
390 sg[j].iov_base = src_sg[i].iov_base;
391 sg[j].iov_len = src_sg[i].iov_len;
392 j++;
393 } else if (offset < (src_sg[i].iov_len + pos)) {
394 sg[j].iov_base = src_sg[i].iov_base;
395 sg[j].iov_len = src_sg[i].iov_len;
396 sg[j].iov_base += (offset - pos);
397 sg[j].iov_len -= (offset - pos);
398 j++;
400 pos += src_sg[i].iov_len;
403 return j;
406 static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
408 size_t old_offset = offset;
409 va_list ap;
410 int i;
412 va_start(ap, fmt);
413 for (i = 0; fmt[i]; i++) {
414 switch (fmt[i]) {
415 case 'b': {
416 uint8_t *valp = va_arg(ap, uint8_t *);
417 offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
418 break;
420 case 'w': {
421 uint16_t val, *valp;
422 valp = va_arg(ap, uint16_t *);
423 val = le16_to_cpupu(valp);
424 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
425 *valp = val;
426 break;
428 case 'd': {
429 uint32_t val, *valp;
430 valp = va_arg(ap, uint32_t *);
431 val = le32_to_cpupu(valp);
432 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
433 *valp = val;
434 break;
436 case 'q': {
437 uint64_t val, *valp;
438 valp = va_arg(ap, uint64_t *);
439 val = le64_to_cpup(valp);
440 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
441 *valp = val;
442 break;
444 case 'v': {
445 struct iovec *iov = va_arg(ap, struct iovec *);
446 int *iovcnt = va_arg(ap, int *);
447 *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
448 break;
450 case 's': {
451 V9fsString *str = va_arg(ap, V9fsString *);
452 offset += pdu_unmarshal(pdu, offset, "w", &str->size);
453 /* FIXME: sanity check str->size */
454 str->data = qemu_malloc(str->size + 1);
455 offset += pdu_unpack(str->data, pdu, offset, str->size);
456 str->data[str->size] = 0;
457 break;
459 case 'Q': {
460 V9fsQID *qidp = va_arg(ap, V9fsQID *);
461 offset += pdu_unmarshal(pdu, offset, "bdq",
462 &qidp->type, &qidp->version, &qidp->path);
463 break;
465 case 'S': {
466 V9fsStat *statp = va_arg(ap, V9fsStat *);
467 offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
468 &statp->size, &statp->type, &statp->dev,
469 &statp->qid, &statp->mode, &statp->atime,
470 &statp->mtime, &statp->length,
471 &statp->name, &statp->uid, &statp->gid,
472 &statp->muid, &statp->extension,
473 &statp->n_uid, &statp->n_gid,
474 &statp->n_muid);
475 break;
477 default:
478 break;
482 va_end(ap);
484 return offset - old_offset;
487 static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
489 size_t old_offset = offset;
490 va_list ap;
491 int i;
493 va_start(ap, fmt);
494 for (i = 0; fmt[i]; i++) {
495 switch (fmt[i]) {
496 case 'b': {
497 uint8_t val = va_arg(ap, int);
498 offset += pdu_pack(pdu, offset, &val, sizeof(val));
499 break;
501 case 'w': {
502 uint16_t val;
503 cpu_to_le16w(&val, va_arg(ap, int));
504 offset += pdu_pack(pdu, offset, &val, sizeof(val));
505 break;
507 case 'd': {
508 uint32_t val;
509 cpu_to_le32w(&val, va_arg(ap, uint32_t));
510 offset += pdu_pack(pdu, offset, &val, sizeof(val));
511 break;
513 case 'q': {
514 uint64_t val;
515 cpu_to_le64w(&val, va_arg(ap, uint64_t));
516 offset += pdu_pack(pdu, offset, &val, sizeof(val));
517 break;
519 case 'v': {
520 struct iovec *iov = va_arg(ap, struct iovec *);
521 int *iovcnt = va_arg(ap, int *);
522 *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
523 break;
525 case 's': {
526 V9fsString *str = va_arg(ap, V9fsString *);
527 offset += pdu_marshal(pdu, offset, "w", str->size);
528 offset += pdu_pack(pdu, offset, str->data, str->size);
529 break;
531 case 'Q': {
532 V9fsQID *qidp = va_arg(ap, V9fsQID *);
533 offset += pdu_marshal(pdu, offset, "bdq",
534 qidp->type, qidp->version, qidp->path);
535 break;
537 case 'S': {
538 V9fsStat *statp = va_arg(ap, V9fsStat *);
539 offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
540 statp->size, statp->type, statp->dev,
541 &statp->qid, statp->mode, statp->atime,
542 statp->mtime, statp->length, &statp->name,
543 &statp->uid, &statp->gid, &statp->muid,
544 &statp->extension, statp->n_uid,
545 statp->n_gid, statp->n_muid);
546 break;
548 default:
549 break;
552 va_end(ap);
554 return offset - old_offset;
557 static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
559 int8_t id = pdu->id + 1; /* Response */
561 if (len < 0) {
562 V9fsString str;
563 int err = -len;
565 str.data = strerror(err);
566 str.size = strlen(str.data);
568 len = 7;
569 len += pdu_marshal(pdu, len, "s", &str);
570 if (dotu) {
571 len += pdu_marshal(pdu, len, "d", err);
574 id = P9_RERROR;
577 /* fill out the header */
578 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
580 /* keep these in sync */
581 pdu->size = len;
582 pdu->id = id;
584 /* push onto queue and notify */
585 virtqueue_push(s->vq, &pdu->elem, len);
587 /* FIXME: we should batch these completions */
588 virtio_notify(&s->vdev, s->vq);
590 free_pdu(s, pdu);
593 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
595 mode_t ret;
597 ret = mode & 0777;
598 if (mode & P9_STAT_MODE_DIR) {
599 ret |= S_IFDIR;
602 if (dotu) {
603 if (mode & P9_STAT_MODE_SYMLINK) {
604 ret |= S_IFLNK;
606 if (mode & P9_STAT_MODE_SOCKET) {
607 ret |= S_IFSOCK;
609 if (mode & P9_STAT_MODE_NAMED_PIPE) {
610 ret |= S_IFIFO;
612 if (mode & P9_STAT_MODE_DEVICE) {
613 if (extension && extension->data[0] == 'c') {
614 ret |= S_IFCHR;
615 } else {
616 ret |= S_IFBLK;
621 if (!(ret&~0777)) {
622 ret |= S_IFREG;
625 if (mode & P9_STAT_MODE_SETUID) {
626 ret |= S_ISUID;
628 if (mode & P9_STAT_MODE_SETGID) {
629 ret |= S_ISGID;
631 if (mode & P9_STAT_MODE_SETVTX) {
632 ret |= S_ISVTX;
635 return ret;
638 static int donttouch_stat(V9fsStat *stat)
640 if (stat->type == -1 &&
641 stat->dev == -1 &&
642 stat->qid.type == -1 &&
643 stat->qid.version == -1 &&
644 stat->qid.path == -1 &&
645 stat->mode == -1 &&
646 stat->atime == -1 &&
647 stat->mtime == -1 &&
648 stat->length == -1 &&
649 !stat->name.size &&
650 !stat->uid.size &&
651 !stat->gid.size &&
652 !stat->muid.size &&
653 stat->n_uid == -1 &&
654 stat->n_gid == -1 &&
655 stat->n_muid == -1) {
656 return 1;
659 return 0;
662 static void v9fs_stat_free(V9fsStat *stat)
664 v9fs_string_free(&stat->name);
665 v9fs_string_free(&stat->uid);
666 v9fs_string_free(&stat->gid);
667 v9fs_string_free(&stat->muid);
668 v9fs_string_free(&stat->extension);
671 static uint32_t stat_to_v9mode(const struct stat *stbuf)
673 uint32_t mode;
675 mode = stbuf->st_mode & 0777;
676 if (S_ISDIR(stbuf->st_mode)) {
677 mode |= P9_STAT_MODE_DIR;
680 if (dotu) {
681 if (S_ISLNK(stbuf->st_mode)) {
682 mode |= P9_STAT_MODE_SYMLINK;
685 if (S_ISSOCK(stbuf->st_mode)) {
686 mode |= P9_STAT_MODE_SOCKET;
689 if (S_ISFIFO(stbuf->st_mode)) {
690 mode |= P9_STAT_MODE_NAMED_PIPE;
693 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
694 mode |= P9_STAT_MODE_DEVICE;
697 if (stbuf->st_mode & S_ISUID) {
698 mode |= P9_STAT_MODE_SETUID;
701 if (stbuf->st_mode & S_ISGID) {
702 mode |= P9_STAT_MODE_SETGID;
705 if (stbuf->st_mode & S_ISVTX) {
706 mode |= P9_STAT_MODE_SETVTX;
710 return mode;
713 static int stat_to_v9stat(V9fsState *s, V9fsString *name,
714 const struct stat *stbuf,
715 V9fsStat *v9stat)
717 int err;
718 const char *str;
720 memset(v9stat, 0, sizeof(*v9stat));
722 stat_to_qid(stbuf, &v9stat->qid);
723 v9stat->mode = stat_to_v9mode(stbuf);
724 v9stat->atime = stbuf->st_atime;
725 v9stat->mtime = stbuf->st_mtime;
726 v9stat->length = stbuf->st_size;
728 v9fs_string_null(&v9stat->uid);
729 v9fs_string_null(&v9stat->gid);
730 v9fs_string_null(&v9stat->muid);
732 if (dotu) {
733 v9stat->n_uid = stbuf->st_uid;
734 v9stat->n_gid = stbuf->st_gid;
735 v9stat->n_muid = 0;
737 v9fs_string_null(&v9stat->extension);
739 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
740 err = v9fs_do_readlink(s, name, &v9stat->extension);
741 if (err == -1) {
742 err = -errno;
743 return err;
745 v9stat->extension.data[err] = 0;
746 v9stat->extension.size = err;
747 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
748 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
749 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
750 major(stbuf->st_rdev), minor(stbuf->st_rdev));
751 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
752 v9fs_string_sprintf(&v9stat->extension, "%s %u",
753 "HARDLINKCOUNT", stbuf->st_nlink);
757 str = strrchr(name->data, '/');
758 if (str) {
759 str += 1;
760 } else {
761 str = name->data;
764 v9fs_string_sprintf(&v9stat->name, "%s", str);
766 v9stat->size = 61 +
767 v9fs_string_size(&v9stat->name) +
768 v9fs_string_size(&v9stat->uid) +
769 v9fs_string_size(&v9stat->gid) +
770 v9fs_string_size(&v9stat->muid) +
771 v9fs_string_size(&v9stat->extension);
772 return 0;
775 static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
777 while (len && *iovcnt) {
778 if (len < sg->iov_len) {
779 sg->iov_len -= len;
780 sg->iov_base += len;
781 len = 0;
782 } else {
783 len -= sg->iov_len;
784 sg++;
785 *iovcnt -= 1;
789 return sg;
792 static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
794 int i;
795 int total = 0;
797 for (i = 0; i < *cnt; i++) {
798 if ((total + sg[i].iov_len) > cap) {
799 sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
800 i++;
801 break;
803 total += sg[i].iov_len;
806 *cnt = i;
808 return sg;
811 static void print_sg(struct iovec *sg, int cnt)
813 int i;
815 printf("sg[%d]: {", cnt);
816 for (i = 0; i < cnt; i++) {
817 if (i) {
818 printf(", ");
820 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
822 printf("}\n");
825 static void v9fs_dummy(V9fsState *s, V9fsPDU *pdu)
827 /* Note: The following have been added to prevent GCC from complaining
828 * They will be removed in the subsequent patches */
829 (void)pdu_unmarshal;
830 (void) complete_pdu;
831 (void) v9fs_string_init;
832 (void) v9fs_string_free;
833 (void) v9fs_string_null;
834 (void) v9fs_string_sprintf;
835 (void) v9fs_string_copy;
836 (void) v9fs_string_size;
837 (void) v9fs_do_lstat;
838 (void) v9fs_do_setuid;
839 (void) v9fs_do_readlink;
840 (void) v9fs_do_close;
841 (void) v9fs_do_closedir;
842 (void) alloc_fid;
843 (void) free_fid;
844 (void) fid_to_qid;
845 (void) v9mode_to_mode;
846 (void) donttouch_stat;
847 (void) v9fs_stat_free;
848 (void) stat_to_v9stat;
849 (void) adjust_sg;
850 (void) cap_sg;
851 (void) print_sg;
854 static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
856 int32_t msize;
857 V9fsString version;
858 size_t offset = 7;
860 pdu_unmarshal(pdu, offset, "ds", &msize, &version);
862 if (strcmp(version.data, "9P2000.u")) {
863 v9fs_string_sprintf(&version, "unknown");
866 offset += pdu_marshal(pdu, offset, "ds", msize, &version);
867 complete_pdu(s, pdu, offset);
869 v9fs_string_free(&version);
872 static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
874 int32_t fid, afid, n_uname;
875 V9fsString uname, aname;
876 V9fsFidState *fidp;
877 V9fsQID qid;
878 size_t offset = 7;
879 ssize_t err;
881 pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
883 fidp = alloc_fid(s, fid);
884 if (fidp == NULL) {
885 err = -EINVAL;
886 goto out;
889 fidp->uid = n_uname;
891 v9fs_string_sprintf(&fidp->path, "%s", "/");
892 err = fid_to_qid(s, fidp, &qid);
893 if (err) {
894 err = -EINVAL;
895 free_fid(s, fid);
896 goto out;
899 offset += pdu_marshal(pdu, offset, "Q", &qid);
901 err = offset;
902 out:
903 complete_pdu(s, pdu, err);
904 v9fs_string_free(&uname);
905 v9fs_string_free(&aname);
908 typedef struct V9fsStatState {
909 V9fsPDU *pdu;
910 size_t offset;
911 V9fsStat v9stat;
912 V9fsFidState *fidp;
913 struct stat stbuf;
914 } V9fsStatState;
916 static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
918 if (err == -1) {
919 err = -errno;
920 goto out;
923 err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
924 if (err) {
925 goto out;
927 vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
928 err = vs->offset;
930 out:
931 complete_pdu(s, vs->pdu, err);
932 v9fs_stat_free(&vs->v9stat);
933 qemu_free(vs);
936 static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
938 int32_t fid;
939 V9fsStatState *vs;
940 ssize_t err = 0;
942 vs = qemu_malloc(sizeof(*vs));
943 vs->pdu = pdu;
944 vs->offset = 7;
946 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
948 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
950 vs->fidp = lookup_fid(s, fid);
951 if (vs->fidp == NULL) {
952 err = -ENOENT;
953 goto out;
956 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
957 v9fs_stat_post_lstat(s, vs, err);
958 return;
960 out:
961 complete_pdu(s, vs->pdu, err);
962 v9fs_stat_free(&vs->v9stat);
963 qemu_free(vs);
966 typedef struct V9fsWalkState {
967 V9fsPDU *pdu;
968 size_t offset;
969 int16_t nwnames;
970 int name_idx;
971 V9fsQID *qids;
972 V9fsFidState *fidp;
973 V9fsFidState *newfidp;
974 V9fsString path;
975 V9fsString *wnames;
976 struct stat stbuf;
977 } V9fsWalkState;
979 static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
981 complete_pdu(s, vs->pdu, err);
983 if (vs->nwnames) {
984 for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
985 v9fs_string_free(&vs->wnames[vs->name_idx]);
988 qemu_free(vs->wnames);
989 qemu_free(vs->qids);
993 static void v9fs_walk_marshal(V9fsWalkState *vs)
995 int i;
996 vs->offset = 7;
997 vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
999 for (i = 0; i < vs->nwnames; i++) {
1000 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1004 static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1005 int err)
1007 if (err == -1) {
1008 free_fid(s, vs->newfidp->fid);
1009 v9fs_string_free(&vs->path);
1010 err = -ENOENT;
1011 goto out;
1014 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1016 vs->name_idx++;
1017 if (vs->name_idx < vs->nwnames) {
1018 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1019 vs->wnames[vs->name_idx].data);
1020 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1022 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1023 v9fs_walk_post_newfid_lstat(s, vs, err);
1024 return;
1027 v9fs_string_free(&vs->path);
1028 v9fs_walk_marshal(vs);
1029 err = vs->offset;
1030 out:
1031 v9fs_walk_complete(s, vs, err);
1034 static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1035 int err)
1037 if (err == -1) {
1038 v9fs_string_free(&vs->path);
1039 err = -ENOENT;
1040 goto out;
1043 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1044 vs->name_idx++;
1045 if (vs->name_idx < vs->nwnames) {
1047 v9fs_string_sprintf(&vs->path, "%s/%s",
1048 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1049 v9fs_string_copy(&vs->fidp->path, &vs->path);
1051 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1052 v9fs_walk_post_oldfid_lstat(s, vs, err);
1053 return;
1056 v9fs_string_free(&vs->path);
1057 v9fs_walk_marshal(vs);
1058 err = vs->offset;
1059 out:
1060 v9fs_walk_complete(s, vs, err);
1063 static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1065 int32_t fid, newfid;
1066 V9fsWalkState *vs;
1067 int err = 0;
1068 int i;
1070 vs = qemu_malloc(sizeof(*vs));
1071 vs->pdu = pdu;
1072 vs->wnames = NULL;
1073 vs->qids = NULL;
1074 vs->offset = 7;
1076 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1077 &newfid, &vs->nwnames);
1079 if (vs->nwnames) {
1080 vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1082 vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1084 for (i = 0; i < vs->nwnames; i++) {
1085 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1086 &vs->wnames[i]);
1090 vs->fidp = lookup_fid(s, fid);
1091 if (vs->fidp == NULL) {
1092 err = -ENOENT;
1093 goto out;
1096 /* FIXME: is this really valid? */
1097 if (fid == newfid) {
1099 BUG_ON(vs->fidp->fd != -1);
1100 BUG_ON(vs->fidp->dir);
1101 v9fs_string_init(&vs->path);
1102 vs->name_idx = 0;
1104 if (vs->name_idx < vs->nwnames) {
1105 v9fs_string_sprintf(&vs->path, "%s/%s",
1106 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1107 v9fs_string_copy(&vs->fidp->path, &vs->path);
1109 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1110 v9fs_walk_post_oldfid_lstat(s, vs, err);
1111 return;
1113 } else {
1114 vs->newfidp = alloc_fid(s, newfid);
1115 if (vs->newfidp == NULL) {
1116 err = -EINVAL;
1117 goto out;
1120 vs->newfidp->uid = vs->fidp->uid;
1121 v9fs_string_init(&vs->path);
1122 vs->name_idx = 0;
1123 v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1125 if (vs->name_idx < vs->nwnames) {
1126 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1127 vs->wnames[vs->name_idx].data);
1128 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1130 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1131 v9fs_walk_post_newfid_lstat(s, vs, err);
1132 return;
1136 v9fs_walk_marshal(vs);
1137 err = vs->offset;
1138 out:
1139 v9fs_walk_complete(s, vs, err);
1142 typedef struct V9fsOpenState {
1143 V9fsPDU *pdu;
1144 size_t offset;
1145 int8_t mode;
1146 V9fsFidState *fidp;
1147 V9fsQID qid;
1148 struct stat stbuf;
1150 } V9fsOpenState;
1152 enum {
1153 Oread = 0x00,
1154 Owrite = 0x01,
1155 Ordwr = 0x02,
1156 Oexec = 0x03,
1157 Oexcl = 0x04,
1158 Otrunc = 0x10,
1159 Orexec = 0x20,
1160 Orclose = 0x40,
1161 Oappend = 0x80,
1164 static int omode_to_uflags(int8_t mode)
1166 int ret = 0;
1168 switch (mode & 3) {
1169 case Oread:
1170 ret = O_RDONLY;
1171 break;
1172 case Ordwr:
1173 ret = O_RDWR;
1174 break;
1175 case Owrite:
1176 ret = O_WRONLY;
1177 break;
1178 case Oexec:
1179 ret = O_RDONLY;
1180 break;
1183 if (mode & Otrunc) {
1184 ret |= O_TRUNC;
1187 if (mode & Oappend) {
1188 ret |= O_APPEND;
1191 if (mode & Oexcl) {
1192 ret |= O_EXCL;
1195 return ret;
1198 static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1200 if (vs->fidp->dir == NULL) {
1201 err = -errno;
1202 goto out;
1205 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1206 err = vs->offset;
1207 out:
1208 complete_pdu(s, vs->pdu, err);
1209 qemu_free(vs);
1213 static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1215 if (vs->fidp->fd == -1) {
1216 err = -errno;
1217 goto out;
1220 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1221 err = vs->offset;
1222 out:
1223 complete_pdu(s, vs->pdu, err);
1224 qemu_free(vs);
1227 static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1229 if (err) {
1230 err = -errno;
1231 goto out;
1234 stat_to_qid(&vs->stbuf, &vs->qid);
1236 if (S_ISDIR(vs->stbuf.st_mode)) {
1237 vs->fidp->dir = v9fs_do_opendir(s, &vs->fidp->path);
1238 v9fs_open_post_opendir(s, vs, err);
1239 } else {
1240 vs->fidp->fd = v9fs_do_open(s, &vs->fidp->path,
1241 omode_to_uflags(vs->mode));
1242 v9fs_open_post_open(s, vs, err);
1244 return;
1245 out:
1246 complete_pdu(s, vs->pdu, err);
1247 qemu_free(vs);
1250 static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
1252 int32_t fid;
1253 V9fsOpenState *vs;
1254 ssize_t err = 0;
1257 vs = qemu_malloc(sizeof(*vs));
1258 vs->pdu = pdu;
1259 vs->offset = 7;
1261 pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1263 vs->fidp = lookup_fid(s, fid);
1264 if (vs->fidp == NULL) {
1265 err = -ENOENT;
1266 goto out;
1269 BUG_ON(vs->fidp->fd != -1);
1270 BUG_ON(vs->fidp->dir);
1272 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1274 v9fs_open_post_lstat(s, vs, err);
1275 return;
1276 out:
1277 complete_pdu(s, pdu, err);
1278 qemu_free(vs);
1281 static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1283 if (debug_9p_pdu) {
1284 pprint_pdu(pdu);
1288 static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
1290 if (debug_9p_pdu) {
1291 pprint_pdu(pdu);
1295 static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
1297 if (debug_9p_pdu) {
1298 pprint_pdu(pdu);
1302 static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
1304 if (debug_9p_pdu) {
1305 pprint_pdu(pdu);
1309 static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
1311 v9fs_dummy(s, pdu);
1312 if (debug_9p_pdu) {
1313 pprint_pdu(pdu);
1317 static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
1319 if (debug_9p_pdu) {
1320 pprint_pdu(pdu);
1324 static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
1326 if (debug_9p_pdu) {
1327 pprint_pdu(pdu);
1331 typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
1333 static pdu_handler_t *pdu_handlers[] = {
1334 [P9_TVERSION] = v9fs_version,
1335 [P9_TATTACH] = v9fs_attach,
1336 [P9_TSTAT] = v9fs_stat,
1337 [P9_TWALK] = v9fs_walk,
1338 [P9_TCLUNK] = v9fs_clunk,
1339 [P9_TOPEN] = v9fs_open,
1340 [P9_TREAD] = v9fs_read,
1341 #if 0
1342 [P9_TAUTH] = v9fs_auth,
1343 #endif
1344 [P9_TFLUSH] = v9fs_flush,
1345 [P9_TCREATE] = v9fs_create,
1346 [P9_TWRITE] = v9fs_write,
1347 [P9_TWSTAT] = v9fs_wstat,
1348 [P9_TREMOVE] = v9fs_remove,
1351 static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
1353 pdu_handler_t *handler;
1355 if (debug_9p_pdu) {
1356 pprint_pdu(pdu);
1359 BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
1361 handler = pdu_handlers[pdu->id];
1362 BUG_ON(handler == NULL);
1364 handler(s, pdu);
1367 static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
1369 V9fsState *s = (V9fsState *)vdev;
1370 V9fsPDU *pdu;
1371 ssize_t len;
1373 while ((pdu = alloc_pdu(s)) &&
1374 (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
1375 uint8_t *ptr;
1377 BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
1378 BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
1380 ptr = pdu->elem.out_sg[0].iov_base;
1382 memcpy(&pdu->size, ptr, 4);
1383 pdu->id = ptr[4];
1384 memcpy(&pdu->tag, ptr + 5, 2);
1386 submit_pdu(s, pdu);
1389 free_pdu(s, pdu);
1392 static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
1394 features |= 1 << VIRTIO_9P_MOUNT_TAG;
1395 return features;
1398 static V9fsState *to_virtio_9p(VirtIODevice *vdev)
1400 return (V9fsState *)vdev;
1403 static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
1405 struct virtio_9p_config *cfg;
1406 V9fsState *s = to_virtio_9p(vdev);
1408 cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
1409 s->tag_len);
1410 stw_raw(&cfg->tag_len, s->tag_len);
1411 memcpy(cfg->tag, s->tag, s->tag_len);
1412 memcpy(config, cfg, s->config_size);
1413 qemu_free(cfg);
1416 VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
1418 V9fsState *s;
1419 int i, len;
1420 struct stat stat;
1421 FsTypeEntry *fse;
1424 s = (V9fsState *)virtio_common_init("virtio-9p",
1425 VIRTIO_ID_9P,
1426 sizeof(struct virtio_9p_config)+
1427 MAX_TAG_LEN,
1428 sizeof(V9fsState));
1430 /* initialize pdu allocator */
1431 QLIST_INIT(&s->free_list);
1432 for (i = 0; i < (MAX_REQ - 1); i++) {
1433 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
1436 s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
1438 fse = get_fsdev_fsentry(conf->fsdev_id);
1440 if (!fse) {
1441 /* We don't have a fsdev identified by fsdev_id */
1442 fprintf(stderr, "Virtio-9p device couldn't find fsdev "
1443 "with the id %s\n", conf->fsdev_id);
1444 exit(1);
1447 if (!fse->path || !conf->tag) {
1448 /* we haven't specified a mount_tag or the path */
1449 fprintf(stderr, "fsdev with id %s needs path "
1450 "and Virtio-9p device needs mount_tag arguments\n",
1451 conf->fsdev_id);
1452 exit(1);
1455 if (lstat(fse->path, &stat)) {
1456 fprintf(stderr, "share path %s does not exist\n", fse->path);
1457 exit(1);
1458 } else if (!S_ISDIR(stat.st_mode)) {
1459 fprintf(stderr, "share path %s is not a directory \n", fse->path);
1460 exit(1);
1463 s->ctx.fs_root = qemu_strdup(fse->path);
1464 len = strlen(conf->tag);
1465 if (len > MAX_TAG_LEN) {
1466 len = MAX_TAG_LEN;
1468 /* s->tag is non-NULL terminated string */
1469 s->tag = qemu_malloc(len);
1470 memcpy(s->tag, conf->tag, len);
1471 s->tag_len = len;
1472 s->ctx.uid = -1;
1474 s->ops = fse->ops;
1475 s->vdev.get_features = virtio_9p_get_features;
1476 s->config_size = sizeof(struct virtio_9p_config) +
1477 s->tag_len;
1478 s->vdev.get_config = virtio_9p_get_config;
1480 return &s->vdev;