configure: dyngen is long time gone
[qemu.git] / hw / virtio-9p.c
blobe5d0112611cbe0418e10bf5e02ca02b1a61fa996
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_do_rewinddir(V9fsState *s, DIR *dir)
71 return s->ops->rewinddir(&s->ctx, dir);
74 static off_t v9fs_do_telldir(V9fsState *s, DIR *dir)
76 return s->ops->telldir(&s->ctx, dir);
79 static struct dirent *v9fs_do_readdir(V9fsState *s, DIR *dir)
81 return s->ops->readdir(&s->ctx, dir);
84 static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off)
86 return s->ops->seekdir(&s->ctx, dir, off);
89 static int v9fs_do_readv(V9fsState *s, int fd, const struct iovec *iov,
90 int iovcnt)
92 return s->ops->readv(&s->ctx, fd, iov, iovcnt);
95 static off_t v9fs_do_lseek(V9fsState *s, int fd, off_t offset, int whence)
97 return s->ops->lseek(&s->ctx, fd, offset, whence);
100 static int v9fs_do_writev(V9fsState *s, int fd, const struct iovec *iov,
101 int iovcnt)
103 return s->ops->writev(&s->ctx, fd, iov, iovcnt);
106 static int v9fs_do_chmod(V9fsState *s, V9fsString *path, mode_t mode)
108 return s->ops->chmod(&s->ctx, path->data, mode);
111 static int v9fs_do_mknod(V9fsState *s, V9fsString *path, mode_t mode, dev_t dev)
113 return s->ops->mknod(&s->ctx, path->data, mode, dev);
116 static int v9fs_do_mksock(V9fsState *s, V9fsString *path)
118 return s->ops->mksock(&s->ctx, path->data);
121 static int v9fs_do_mkdir(V9fsState *s, V9fsString *path, mode_t mode)
123 return s->ops->mkdir(&s->ctx, path->data, mode);
126 static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
128 return s->ops->fstat(&s->ctx, fd, stbuf);
131 static int v9fs_do_open2(V9fsState *s, V9fsString *path, int flags, mode_t mode)
133 return s->ops->open2(&s->ctx, path->data, flags, mode);
136 static int v9fs_do_symlink(V9fsState *s, V9fsString *oldpath,
137 V9fsString *newpath)
139 return s->ops->symlink(&s->ctx, oldpath->data, newpath->data);
142 static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
144 return s->ops->link(&s->ctx, oldpath->data, newpath->data);
147 static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
149 return s->ops->truncate(&s->ctx, path->data, size);
152 static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath,
153 V9fsString *newpath)
155 return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
158 static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
160 return s->ops->chown(&s->ctx, path->data, uid, gid);
163 static int v9fs_do_utime(V9fsState *s, V9fsString *path,
164 const struct utimbuf *buf)
166 return s->ops->utime(&s->ctx, path->data, buf);
169 static int v9fs_do_remove(V9fsState *s, V9fsString *path)
171 return s->ops->remove(&s->ctx, path->data);
174 static int v9fs_do_fsync(V9fsState *s, int fd)
176 return s->ops->fsync(&s->ctx, fd);
179 static void v9fs_string_init(V9fsString *str)
181 str->data = NULL;
182 str->size = 0;
185 static void v9fs_string_free(V9fsString *str)
187 qemu_free(str->data);
188 str->data = NULL;
189 str->size = 0;
192 static void v9fs_string_null(V9fsString *str)
194 v9fs_string_free(str);
197 static int number_to_string(void *arg, char type)
199 unsigned int ret = 0;
201 switch (type) {
202 case 'u': {
203 unsigned int num = *(unsigned int *)arg;
205 do {
206 ret++;
207 num = num/10;
208 } while (num);
209 break;
211 default:
212 printf("Number_to_string: Unknown number format\n");
213 return -1;
216 return ret;
219 static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
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;
228 /* Find the number of %'s that denotes an argument */
229 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
230 nr_args++;
231 iter++;
234 len = strlen(fmt) - 2*nr_args;
236 if (!nr_args) {
237 goto alloc_print;
240 va_copy(ap2, ap);
242 iter = (char *)fmt;
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;
264 iter++;
267 alloc_print:
268 *strp = qemu_malloc((len + 1) * sizeof(**strp));
270 return vsprintf(*strp, fmt, ap);
273 static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
275 va_list ap;
276 int err;
278 v9fs_string_free(str);
280 va_start(ap, fmt);
281 err = v9fs_string_alloc_printf(&str->data, fmt, ap);
282 BUG_ON(err == -1);
283 va_end(ap);
285 str->size = err;
288 static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
290 v9fs_string_free(lhs);
291 v9fs_string_sprintf(lhs, "%s", rhs->data);
294 static size_t v9fs_string_size(V9fsString *str)
296 return str->size;
299 static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
301 V9fsFidState *f;
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;
310 return NULL;
313 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
315 V9fsFidState *f;
317 f = lookup_fid(s, fid);
318 if (f) {
319 return NULL;
322 f = qemu_mallocz(sizeof(V9fsFidState));
324 f->fid = fid;
325 f->fd = -1;
326 f->dir = NULL;
328 f->next = s->fid_list;
329 s->fid_list = f;
331 return f;
334 static int free_fid(V9fsState *s, int32_t fid)
336 V9fsFidState **fidpp, *fidp;
338 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
339 if ((*fidpp)->fid == fid) {
340 break;
344 if (*fidpp == NULL) {
345 return -ENOENT;
348 fidp = *fidpp;
349 *fidpp = fidp->next;
351 if (fidp->fd != -1) {
352 v9fs_do_close(s, fidp->fd);
354 if (fidp->dir) {
355 v9fs_do_closedir(s, fidp->dir);
357 v9fs_string_free(&fidp->path);
358 qemu_free(fidp);
360 return 0;
363 #define P9_QID_TYPE_DIR 0x80
364 #define P9_QID_TYPE_SYMLINK 0x02
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
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)
388 /* This is the algorithm from ufs in spfs */
389 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
391 size_t size;
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;
400 if (S_ISLNK(stbuf->st_mode)) {
401 qidp->type |= P9_QID_TYPE_SYMLINK;
405 static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
407 struct stat stbuf;
408 int err;
410 err = v9fs_do_lstat(s, &fidp->path, &stbuf);
411 if (err) {
412 return err;
415 stat_to_qid(&stbuf, qidp);
416 return 0;
419 static V9fsPDU *alloc_pdu(V9fsState *s)
421 V9fsPDU *pdu = NULL;
423 if (!QLIST_EMPTY(&s->free_list)) {
424 pdu = QLIST_FIRST(&s->free_list);
425 QLIST_REMOVE(pdu, next);
427 return pdu;
430 static void free_pdu(V9fsState *s, V9fsPDU *pdu)
432 if (pdu) {
433 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
437 size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
438 size_t offset, size_t size, int pack)
440 int i = 0;
441 size_t copied = 0;
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);
456 size -= len;
457 copied += len;
458 addr += len;
459 if (size) {
460 offset = 0;
461 continue;
466 return copied;
469 static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
471 return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
472 offset, size, 0);
475 static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
476 size_t size)
478 return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
479 offset, size, 1);
482 static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
484 size_t pos = 0;
485 int i, j;
486 struct iovec *src_sg;
487 unsigned int num;
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;
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++;
510 pos += src_sg[i].iov_len;
513 return j;
516 static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
518 size_t old_offset = offset;
519 va_list ap;
520 int i;
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;
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;
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;
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;
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;
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;
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;
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;
587 default:
588 break;
592 va_end(ap);
594 return offset - old_offset;
597 static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
599 size_t old_offset = offset;
600 va_list ap;
601 int i;
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;
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;
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;
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;
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;
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;
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;
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;
658 default:
659 break;
662 va_end(ap);
664 return offset - old_offset;
667 static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
669 int8_t id = pdu->id + 1; /* Response */
671 if (len < 0) {
672 V9fsString str;
673 int err = -len;
675 str.data = strerror(err);
676 str.size = strlen(str.data);
678 len = 7;
679 len += pdu_marshal(pdu, len, "s", &str);
680 if (dotu) {
681 len += pdu_marshal(pdu, len, "d", err);
684 id = P9_RERROR;
687 /* fill out the header */
688 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
690 /* keep these in sync */
691 pdu->size = len;
692 pdu->id = id;
694 /* push onto queue and notify */
695 virtqueue_push(s->vq, &pdu->elem, len);
697 /* FIXME: we should batch these completions */
698 virtio_notify(&s->vdev, s->vq);
700 free_pdu(s, pdu);
703 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
705 mode_t ret;
707 ret = mode & 0777;
708 if (mode & P9_STAT_MODE_DIR) {
709 ret |= S_IFDIR;
712 if (dotu) {
713 if (mode & P9_STAT_MODE_SYMLINK) {
714 ret |= S_IFLNK;
716 if (mode & P9_STAT_MODE_SOCKET) {
717 ret |= S_IFSOCK;
719 if (mode & P9_STAT_MODE_NAMED_PIPE) {
720 ret |= S_IFIFO;
722 if (mode & P9_STAT_MODE_DEVICE) {
723 if (extension && extension->data[0] == 'c') {
724 ret |= S_IFCHR;
725 } else {
726 ret |= S_IFBLK;
731 if (!(ret&~0777)) {
732 ret |= S_IFREG;
735 if (mode & P9_STAT_MODE_SETUID) {
736 ret |= S_ISUID;
738 if (mode & P9_STAT_MODE_SETGID) {
739 ret |= S_ISGID;
741 if (mode & P9_STAT_MODE_SETVTX) {
742 ret |= S_ISVTX;
745 return ret;
748 static int donttouch_stat(V9fsStat *stat)
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;
769 return 0;
772 static void v9fs_stat_free(V9fsStat *stat)
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);
781 static uint32_t stat_to_v9mode(const struct stat *stbuf)
783 uint32_t mode;
785 mode = stbuf->st_mode & 0777;
786 if (S_ISDIR(stbuf->st_mode)) {
787 mode |= P9_STAT_MODE_DIR;
790 if (dotu) {
791 if (S_ISLNK(stbuf->st_mode)) {
792 mode |= P9_STAT_MODE_SYMLINK;
795 if (S_ISSOCK(stbuf->st_mode)) {
796 mode |= P9_STAT_MODE_SOCKET;
799 if (S_ISFIFO(stbuf->st_mode)) {
800 mode |= P9_STAT_MODE_NAMED_PIPE;
803 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
804 mode |= P9_STAT_MODE_DEVICE;
807 if (stbuf->st_mode & S_ISUID) {
808 mode |= P9_STAT_MODE_SETUID;
811 if (stbuf->st_mode & S_ISGID) {
812 mode |= P9_STAT_MODE_SETGID;
815 if (stbuf->st_mode & S_ISVTX) {
816 mode |= P9_STAT_MODE_SETVTX;
820 return mode;
823 static int stat_to_v9stat(V9fsState *s, V9fsString *name,
824 const struct stat *stbuf,
825 V9fsStat *v9stat)
827 int err;
828 const char *str;
830 memset(v9stat, 0, sizeof(*v9stat));
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;
838 v9fs_string_null(&v9stat->uid);
839 v9fs_string_null(&v9stat->gid);
840 v9fs_string_null(&v9stat->muid);
842 if (dotu) {
843 v9stat->n_uid = stbuf->st_uid;
844 v9stat->n_gid = stbuf->st_gid;
845 v9stat->n_muid = 0;
847 v9fs_string_null(&v9stat->extension);
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;
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);
867 str = strrchr(name->data, '/');
868 if (str) {
869 str += 1;
870 } else {
871 str = name->data;
874 v9fs_string_sprintf(&v9stat->name, "%s", str);
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;
885 static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
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;
899 return sg;
902 static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
904 int i;
905 int total = 0;
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;
913 total += sg[i].iov_len;
916 *cnt = i;
918 return sg;
921 static void print_sg(struct iovec *sg, int cnt)
923 int i;
925 printf("sg[%d]: {", cnt);
926 for (i = 0; i < cnt; i++) {
927 if (i) {
928 printf(", ");
930 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
932 printf("}\n");
935 static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
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);
944 static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
946 int32_t msize;
947 V9fsString version;
948 size_t offset = 7;
950 pdu_unmarshal(pdu, offset, "ds", &msize, &version);
952 if (strcmp(version.data, "9P2000.u")) {
953 v9fs_string_sprintf(&version, "unknown");
956 offset += pdu_marshal(pdu, offset, "ds", msize, &version);
957 complete_pdu(s, pdu, offset);
959 v9fs_string_free(&version);
962 static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
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;
971 pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
973 fidp = alloc_fid(s, fid);
974 if (fidp == NULL) {
975 err = -EINVAL;
976 goto out;
979 fidp->uid = n_uname;
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;
989 offset += pdu_marshal(pdu, offset, "Q", &qid);
991 err = offset;
992 out:
993 complete_pdu(s, pdu, err);
994 v9fs_string_free(&uname);
995 v9fs_string_free(&aname);
998 typedef struct V9fsStatState {
999 V9fsPDU *pdu;
1000 size_t offset;
1001 V9fsStat v9stat;
1002 V9fsFidState *fidp;
1003 struct stat stbuf;
1004 } V9fsStatState;
1006 static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1008 if (err == -1) {
1009 err = -errno;
1010 goto out;
1013 err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1014 if (err) {
1015 goto out;
1017 vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1018 err = vs->offset;
1020 out:
1021 complete_pdu(s, vs->pdu, err);
1022 v9fs_stat_free(&vs->v9stat);
1023 qemu_free(vs);
1026 static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
1028 int32_t fid;
1029 V9fsStatState *vs;
1030 ssize_t err = 0;
1032 vs = qemu_malloc(sizeof(*vs));
1033 vs->pdu = pdu;
1034 vs->offset = 7;
1036 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1038 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1040 vs->fidp = lookup_fid(s, fid);
1041 if (vs->fidp == NULL) {
1042 err = -ENOENT;
1043 goto out;
1046 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1047 v9fs_stat_post_lstat(s, vs, err);
1048 return;
1050 out:
1051 complete_pdu(s, vs->pdu, err);
1052 v9fs_stat_free(&vs->v9stat);
1053 qemu_free(vs);
1056 typedef 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;
1069 static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1071 complete_pdu(s, vs->pdu, err);
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]);
1078 qemu_free(vs->wnames);
1079 qemu_free(vs->qids);
1083 static void v9fs_walk_marshal(V9fsWalkState *vs)
1085 int i;
1086 vs->offset = 7;
1087 vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1089 for (i = 0; i < vs->nwnames; i++) {
1090 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1094 static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1095 int err)
1097 if (err == -1) {
1098 free_fid(s, vs->newfidp->fid);
1099 v9fs_string_free(&vs->path);
1100 err = -ENOENT;
1101 goto out;
1104 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
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);
1112 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1113 v9fs_walk_post_newfid_lstat(s, vs, err);
1114 return;
1117 v9fs_string_free(&vs->path);
1118 v9fs_walk_marshal(vs);
1119 err = vs->offset;
1120 out:
1121 v9fs_walk_complete(s, vs, err);
1124 static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1125 int err)
1127 if (err == -1) {
1128 v9fs_string_free(&vs->path);
1129 err = -ENOENT;
1130 goto out;
1133 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1134 vs->name_idx++;
1135 if (vs->name_idx < vs->nwnames) {
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);
1141 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1142 v9fs_walk_post_oldfid_lstat(s, vs, err);
1143 return;
1146 v9fs_string_free(&vs->path);
1147 v9fs_walk_marshal(vs);
1148 err = vs->offset;
1149 out:
1150 v9fs_walk_complete(s, vs, err);
1153 static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1155 int32_t fid, newfid;
1156 V9fsWalkState *vs;
1157 int err = 0;
1158 int i;
1160 vs = qemu_malloc(sizeof(*vs));
1161 vs->pdu = pdu;
1162 vs->wnames = NULL;
1163 vs->qids = NULL;
1164 vs->offset = 7;
1166 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1167 &newfid, &vs->nwnames);
1169 if (vs->nwnames) {
1170 vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1172 vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1174 for (i = 0; i < vs->nwnames; i++) {
1175 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1176 &vs->wnames[i]);
1180 vs->fidp = lookup_fid(s, fid);
1181 if (vs->fidp == NULL) {
1182 err = -ENOENT;
1183 goto out;
1186 /* FIXME: is this really valid? */
1187 if (fid == newfid) {
1189 BUG_ON(vs->fidp->fd != -1);
1190 BUG_ON(vs->fidp->dir);
1191 v9fs_string_init(&vs->path);
1192 vs->name_idx = 0;
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);
1199 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1200 v9fs_walk_post_oldfid_lstat(s, vs, err);
1201 return;
1203 } else {
1204 vs->newfidp = alloc_fid(s, newfid);
1205 if (vs->newfidp == NULL) {
1206 err = -EINVAL;
1207 goto out;
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);
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);
1220 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1221 v9fs_walk_post_newfid_lstat(s, vs, err);
1222 return;
1226 v9fs_walk_marshal(vs);
1227 err = vs->offset;
1228 out:
1229 v9fs_walk_complete(s, vs, err);
1232 typedef struct V9fsOpenState {
1233 V9fsPDU *pdu;
1234 size_t offset;
1235 int8_t mode;
1236 V9fsFidState *fidp;
1237 V9fsQID qid;
1238 struct stat stbuf;
1240 } V9fsOpenState;
1242 enum {
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,
1254 static int omode_to_uflags(int8_t mode)
1256 int ret = 0;
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;
1273 if (mode & Otrunc) {
1274 ret |= O_TRUNC;
1277 if (mode & Oappend) {
1278 ret |= O_APPEND;
1281 if (mode & Oexcl) {
1282 ret |= O_EXCL;
1285 return ret;
1288 static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1290 if (vs->fidp->dir == NULL) {
1291 err = -errno;
1292 goto out;
1295 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1296 err = vs->offset;
1297 out:
1298 complete_pdu(s, vs->pdu, err);
1299 qemu_free(vs);
1303 static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1305 if (vs->fidp->fd == -1) {
1306 err = -errno;
1307 goto out;
1310 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1311 err = vs->offset;
1312 out:
1313 complete_pdu(s, vs->pdu, err);
1314 qemu_free(vs);
1317 static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1319 if (err) {
1320 err = -errno;
1321 goto out;
1324 stat_to_qid(&vs->stbuf, &vs->qid);
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);
1334 return;
1335 out:
1336 complete_pdu(s, vs->pdu, err);
1337 qemu_free(vs);
1340 static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
1342 int32_t fid;
1343 V9fsOpenState *vs;
1344 ssize_t err = 0;
1347 vs = qemu_malloc(sizeof(*vs));
1348 vs->pdu = pdu;
1349 vs->offset = 7;
1351 pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1353 vs->fidp = lookup_fid(s, fid);
1354 if (vs->fidp == NULL) {
1355 err = -ENOENT;
1356 goto out;
1359 BUG_ON(vs->fidp->fd != -1);
1360 BUG_ON(vs->fidp->dir);
1362 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1364 v9fs_open_post_lstat(s, vs, err);
1365 return;
1366 out:
1367 complete_pdu(s, pdu, err);
1368 qemu_free(vs);
1371 static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1373 int32_t fid;
1374 size_t offset = 7;
1375 int err;
1377 pdu_unmarshal(pdu, offset, "d", &fid);
1379 err = free_fid(s, fid);
1380 if (err < 0) {
1381 goto out;
1384 offset = 7;
1385 err = offset;
1386 out:
1387 complete_pdu(s, pdu, err);
1390 typedef 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;
1409 static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1411 static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1413 if (err) {
1414 goto out;
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;
1421 out:
1422 complete_pdu(s, vs->pdu, err);
1423 qemu_free(vs);
1424 return;
1427 static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1428 ssize_t err)
1430 if (err) {
1431 err = -errno;
1432 goto out;
1434 err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1435 if (err) {
1436 goto out;
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;
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;
1454 out:
1455 v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1456 v9fs_read_post_seekdir(s, vs, err);
1457 return;
1461 static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
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;
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;
1481 static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1483 vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1484 v9fs_read_post_readdir(s, vs, err);
1485 return;
1488 static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
1489 ssize_t err)
1491 vs->dir_pos = v9fs_do_telldir(s, vs->fidp->dir);
1492 v9fs_read_post_telldir(s, vs, err);
1493 return;
1496 static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *vs, ssize_t err)
1498 if (err < 0) {
1499 /* IO error return the error */
1500 err = -errno;
1501 goto out;
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);
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;
1515 v9fs_read_post_readv(s, vs, err);
1516 return;
1518 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1519 vs->offset += vs->count;
1520 err = vs->offset;
1522 out:
1523 complete_pdu(s, vs->pdu, err);
1524 qemu_free(vs);
1527 static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err)
1529 if (err == -1) {
1530 err = -errno;
1531 goto out;
1533 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1535 if (vs->total < vs->count) {
1536 do {
1537 if (0) {
1538 print_sg(vs->sg, vs->cnt);
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;
1545 v9fs_read_post_readv(s, vs, err);
1546 return;
1548 out:
1549 complete_pdu(s, vs->pdu, err);
1550 qemu_free(vs);
1553 static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
1555 int32_t fid;
1556 V9fsReadState *vs;
1557 ssize_t err = 0;
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;
1566 pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count);
1568 vs->fidp = lookup_fid(s, fid);
1569 if (vs->fidp == NULL) {
1570 err = -EINVAL;
1571 goto out;
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);
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;
1591 out:
1592 complete_pdu(s, pdu, err);
1593 qemu_free(vs);
1596 typedef 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;
1609 static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs,
1610 ssize_t err)
1612 if (err < 0) {
1613 /* IO error return the error */
1614 err = -errno;
1615 goto out;
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);
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;
1629 v9fs_write_post_writev(s, vs, err);
1630 return;
1632 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1634 err = vs->offset;
1635 out:
1636 complete_pdu(s, vs->pdu, err);
1637 qemu_free(vs);
1640 static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err)
1642 if (err == -1) {
1643 err = -errno;
1644 goto out;
1646 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1648 if (vs->total < vs->count) {
1649 do {
1650 if (0) {
1651 print_sg(vs->sg, vs->cnt);
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;
1658 v9fs_write_post_writev(s, vs, err);
1659 return;
1662 out:
1663 complete_pdu(s, vs->pdu, err);
1664 qemu_free(vs);
1667 static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
1669 int32_t fid;
1670 V9fsWriteState *vs;
1671 ssize_t err;
1673 vs = qemu_malloc(sizeof(*vs));
1675 vs->pdu = pdu;
1676 vs->offset = 7;
1677 vs->sg = vs->iov;
1678 vs->total = 0;
1679 vs->len = 0;
1681 pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
1682 vs->sg, &vs->cnt);
1684 vs->fidp = lookup_fid(s, fid);
1685 if (vs->fidp == NULL) {
1686 err = -EINVAL;
1687 goto out;
1690 if (vs->fidp->fd == -1) {
1691 err = -EINVAL;
1692 goto out;
1695 err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1697 v9fs_write_post_lseek(s, vs, err);
1698 return;
1700 out:
1701 complete_pdu(s, vs->pdu, err);
1702 qemu_free(vs);
1705 typedef 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;
1718 static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
1720 if (err == 0) {
1721 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1722 stat_to_qid(&vs->stbuf, &vs->qid);
1724 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1726 err = vs->offset;
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);
1736 static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
1738 if (err) {
1739 err = -errno;
1741 v9fs_post_create(s, vs, err);
1744 static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
1745 int err)
1747 if (!vs->fidp->dir) {
1748 err = -errno;
1750 v9fs_post_create(s, vs, err);
1753 static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
1754 int err)
1756 if (err) {
1757 err = -errno;
1758 goto out;
1761 vs->fidp->dir = v9fs_do_opendir(s, &vs->fullname);
1762 v9fs_create_post_opendir(s, vs, err);
1763 return;
1765 out:
1766 v9fs_post_create(s, vs, err);
1769 static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
1771 if (err) {
1772 err = -errno;
1773 goto out;
1776 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1777 v9fs_create_post_dir_lstat(s, vs, err);
1778 return;
1780 out:
1781 v9fs_post_create(s, vs, err);
1784 static void v9fs_create_post_mksock(V9fsState *s, V9fsCreateState *vs,
1785 int err)
1787 if (err) {
1788 err = -errno;
1789 goto out;
1792 err = v9fs_do_chmod(s, &vs->fullname, vs->perm & 0777);
1793 v9fs_create_post_perms(s, vs, err);
1794 return;
1796 out:
1797 v9fs_post_create(s, vs, err);
1800 static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
1802 if (err) {
1803 vs->fidp->fd = -1;
1804 err = -errno;
1807 v9fs_post_create(s, vs, err);
1808 return;
1811 static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
1813 if (vs->fidp->fd == -1) {
1814 err = -errno;
1815 goto out;
1818 err = v9fs_do_fstat(s, vs->fidp->fd, &vs->stbuf);
1819 v9fs_create_post_fstat(s, vs, err);
1821 return;
1823 out:
1824 v9fs_post_create(s, vs, err);
1828 static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
1831 if (err == 0 || errno != ENOENT) {
1832 err = -errno;
1833 goto out;
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);
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;
1856 if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
1857 &minor) != 3) {
1858 err = -errno;
1859 v9fs_post_create(s, vs, err);
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);
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);
1890 return;
1892 out:
1893 v9fs_post_create(s, vs, err);
1896 static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
1898 int32_t fid;
1899 V9fsCreateState *vs;
1900 int err = 0;
1902 vs = qemu_malloc(sizeof(*vs));
1903 vs->pdu = pdu;
1904 vs->offset = 7;
1906 v9fs_string_init(&vs->fullname);
1908 pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
1909 &vs->perm, &vs->mode, &vs->extension);
1911 vs->fidp = lookup_fid(s, fid);
1912 if (vs->fidp == NULL) {
1913 err = -EINVAL;
1914 goto out;
1917 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
1918 vs->name.data);
1920 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1921 v9fs_create_post_lstat(s, vs, err);
1922 return;
1924 out:
1925 complete_pdu(s, vs->pdu, err);
1926 v9fs_string_free(&vs->name);
1927 v9fs_string_free(&vs->extension);
1928 qemu_free(vs);
1931 static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
1933 /* A nop call with no return */
1934 complete_pdu(s, pdu, 7);
1937 typedef struct V9fsRemoveState {
1938 V9fsPDU *pdu;
1939 size_t offset;
1940 V9fsFidState *fidp;
1941 } V9fsRemoveState;
1943 static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
1944 int err)
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;
1952 err = vs->offset;
1953 out:
1954 complete_pdu(s, vs->pdu, err);
1955 qemu_free(vs);
1958 static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
1960 int32_t fid;
1961 V9fsRemoveState *vs;
1962 int err = 0;
1964 vs = qemu_malloc(sizeof(*vs));
1965 vs->pdu = pdu;
1966 vs->offset = 7;
1968 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1970 vs->fidp = lookup_fid(s, fid);
1971 if (vs->fidp == NULL) {
1972 err = -EINVAL;
1973 goto out;
1976 err = v9fs_do_remove(s, &vs->fidp->path);
1977 v9fs_remove_post_remove(s, vs, err);
1978 return;
1980 out:
1981 complete_pdu(s, pdu, err);
1982 qemu_free(vs);
1985 typedef struct V9fsWstatState
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;
1996 static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
1998 if (err < 0) {
1999 goto out;
2002 err = vs->offset;
2004 out:
2005 v9fs_stat_free(&vs->v9stat);
2006 complete_pdu(s, vs->pdu, err);
2007 qemu_free(vs);
2010 static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
2012 if (err < 0) {
2013 goto out;
2016 if (vs->v9stat.name.size != 0) {
2017 v9fs_string_free(&vs->nname);
2020 if (vs->v9stat.length != -1) {
2021 if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
2022 err = -errno;
2025 v9fs_wstat_post_truncate(s, vs, err);
2026 return;
2028 out:
2029 v9fs_stat_free(&vs->v9stat);
2030 complete_pdu(s, vs->pdu, err);
2031 qemu_free(vs);
2034 static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
2036 V9fsFidState *fidp;
2037 if (err < 0) {
2038 goto out;
2041 if (vs->v9stat.name.size != 0) {
2042 char *old_name, *new_name;
2043 char *end;
2045 old_name = vs->fidp->path.data;
2046 end = strrchr(old_name, '/');
2047 if (end) {
2048 end++;
2049 } else {
2050 end = old_name;
2053 new_name = qemu_malloc(end - old_name + vs->v9stat.name.size + 1);
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);
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 {
2067 * Fixup fid's pointing to the old name to
2068 * start pointing to the new name
2070 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2072 if (vs->fidp == fidp) {
2074 * we replace name of this fid towards the end
2075 * so that our below strcmp will work
2077 continue;
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));
2086 v9fs_string_copy(&vs->fidp->path, &vs->nname);
2090 v9fs_wstat_post_rename(s, vs, err);
2091 return;
2093 out:
2094 v9fs_stat_free(&vs->v9stat);
2095 complete_pdu(s, vs->pdu, err);
2096 qemu_free(vs);
2099 static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2101 if (err < 0) {
2102 goto out;
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;
2111 v9fs_wstat_post_chown(s, vs, err);
2112 return;
2114 out:
2115 v9fs_stat_free(&vs->v9stat);
2116 complete_pdu(s, vs->pdu, err);
2117 qemu_free(vs);
2120 static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2122 if (err < 0) {
2123 goto out;
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;
2135 v9fs_wstat_post_utime(s, vs, err);
2136 return;
2138 out:
2139 v9fs_stat_free(&vs->v9stat);
2140 complete_pdu(s, vs->pdu, err);
2141 qemu_free(vs);
2144 static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2146 if (err == -1) {
2147 err = -errno;
2149 v9fs_stat_free(&vs->v9stat);
2150 complete_pdu(s, vs->pdu, err);
2151 qemu_free(vs);
2154 static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2156 uint32_t v9_mode;
2158 if (err == -1) {
2159 err = -errno;
2160 goto out;
2163 v9_mode = stat_to_v9mode(&vs->stbuf);
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;
2172 if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2173 &vs->v9stat.extension))) {
2174 err = -errno;
2176 v9fs_wstat_post_chmod(s, vs, err);
2177 return;
2179 out:
2180 v9fs_stat_free(&vs->v9stat);
2181 complete_pdu(s, vs->pdu, err);
2182 qemu_free(vs);
2185 static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
2187 int32_t fid;
2188 V9fsWstatState *vs;
2189 int err = 0;
2191 vs = qemu_malloc(sizeof(*vs));
2192 vs->pdu = pdu;
2193 vs->offset = 7;
2195 pdu_unmarshal(pdu, vs->offset, "dwS", &fid, &vs->unused, &vs->v9stat);
2197 vs->fidp = lookup_fid(s, fid);
2198 if (vs->fidp == NULL) {
2199 err = -EINVAL;
2200 goto out;
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;
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;
2216 v9fs_wstat_post_chmod(s, vs, err);
2217 return;
2219 out:
2220 v9fs_stat_free(&vs->v9stat);
2221 complete_pdu(s, vs->pdu, err);
2222 qemu_free(vs);
2225 typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
2227 static 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,
2245 static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
2247 pdu_handler_t *handler;
2249 if (debug_9p_pdu) {
2250 pprint_pdu(pdu);
2253 BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
2255 handler = pdu_handlers[pdu->id];
2256 BUG_ON(handler == NULL);
2258 handler(s, pdu);
2261 static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
2263 V9fsState *s = (V9fsState *)vdev;
2264 V9fsPDU *pdu;
2265 ssize_t len;
2267 while ((pdu = alloc_pdu(s)) &&
2268 (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
2269 uint8_t *ptr;
2271 BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
2272 BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
2274 ptr = pdu->elem.out_sg[0].iov_base;
2276 memcpy(&pdu->size, ptr, 4);
2277 pdu->id = ptr[4];
2278 memcpy(&pdu->tag, ptr + 5, 2);
2280 submit_pdu(s, pdu);
2283 free_pdu(s, pdu);
2286 static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
2288 features |= 1 << VIRTIO_9P_MOUNT_TAG;
2289 return features;
2292 static V9fsState *to_virtio_9p(VirtIODevice *vdev)
2294 return (V9fsState *)vdev;
2297 static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
2299 struct virtio_9p_config *cfg;
2300 V9fsState *s = to_virtio_9p(vdev);
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);
2310 VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
2312 V9fsState *s;
2313 int i, len;
2314 struct stat stat;
2315 FsTypeEntry *fse;
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));
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);
2330 s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
2332 fse = get_fsdev_fsentry(conf->fsdev_id);
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);
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);
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);
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;
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;
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;
2374 return &s->vdev;