]>
git.proxmox.com Git - qemu.git/blob - hw/virtio-9p.c
4 * Copyright IBM, Corp. 2010
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.
16 #include "qemu_socket.h"
17 #include "virtio-9p.h"
18 #include "fsdev/qemu-fsdev.h"
19 #include "virtio-9p-debug.h"
36 static int omode_to_uflags(int8_t mode
)
70 static int v9fs_do_lstat(V9fsState
*s
, V9fsString
*path
, struct stat
*stbuf
)
72 return s
->ops
->lstat(&s
->ctx
, path
->data
, stbuf
);
75 static int v9fs_do_setuid(V9fsState
*s
, uid_t uid
)
77 return s
->ops
->setuid(&s
->ctx
, uid
);
80 static ssize_t
v9fs_do_readlink(V9fsState
*s
, V9fsString
*path
, V9fsString
*buf
)
84 buf
->data
= qemu_malloc(1024);
86 len
= s
->ops
->readlink(&s
->ctx
, path
->data
, buf
->data
, 1024 - 1);
95 static int v9fs_do_close(V9fsState
*s
, int fd
)
97 return s
->ops
->close(&s
->ctx
, fd
);
100 static int v9fs_do_closedir(V9fsState
*s
, DIR *dir
)
102 return s
->ops
->closedir(&s
->ctx
, dir
);
105 static int v9fs_do_open(V9fsState
*s
, V9fsString
*path
, int flags
)
107 return s
->ops
->open(&s
->ctx
, path
->data
, flags
);
110 static DIR *v9fs_do_opendir(V9fsState
*s
, V9fsString
*path
)
112 return s
->ops
->opendir(&s
->ctx
, path
->data
);
115 static void v9fs_do_rewinddir(V9fsState
*s
, DIR *dir
)
117 return s
->ops
->rewinddir(&s
->ctx
, dir
);
120 static off_t
v9fs_do_telldir(V9fsState
*s
, DIR *dir
)
122 return s
->ops
->telldir(&s
->ctx
, dir
);
125 static struct dirent
*v9fs_do_readdir(V9fsState
*s
, DIR *dir
)
127 return s
->ops
->readdir(&s
->ctx
, dir
);
130 static void v9fs_do_seekdir(V9fsState
*s
, DIR *dir
, off_t off
)
132 return s
->ops
->seekdir(&s
->ctx
, dir
, off
);
135 static int v9fs_do_readv(V9fsState
*s
, int fd
, const struct iovec
*iov
,
138 return s
->ops
->readv(&s
->ctx
, fd
, iov
, iovcnt
);
141 static off_t
v9fs_do_lseek(V9fsState
*s
, int fd
, off_t offset
, int whence
)
143 return s
->ops
->lseek(&s
->ctx
, fd
, offset
, whence
);
146 static int v9fs_do_writev(V9fsState
*s
, int fd
, const struct iovec
*iov
,
149 return s
->ops
->writev(&s
->ctx
, fd
, iov
, iovcnt
);
152 static int v9fs_do_chmod(V9fsState
*s
, V9fsString
*path
, mode_t mode
)
154 return s
->ops
->chmod(&s
->ctx
, path
->data
, mode
);
157 static int v9fs_do_mknod(V9fsState
*s
, V9fsString
*path
, mode_t mode
, dev_t dev
)
159 return s
->ops
->mknod(&s
->ctx
, path
->data
, mode
, dev
);
162 static int v9fs_do_mksock(V9fsState
*s
, V9fsString
*path
)
164 return s
->ops
->mksock(&s
->ctx
, path
->data
);
167 static int v9fs_do_mkdir(V9fsState
*s
, V9fsString
*path
, mode_t mode
)
169 return s
->ops
->mkdir(&s
->ctx
, path
->data
, mode
);
172 static int v9fs_do_fstat(V9fsState
*s
, int fd
, struct stat
*stbuf
)
174 return s
->ops
->fstat(&s
->ctx
, fd
, stbuf
);
177 static int v9fs_do_open2(V9fsState
*s
, V9fsString
*path
, int flags
, mode_t mode
)
179 return s
->ops
->open2(&s
->ctx
, path
->data
, flags
, mode
);
182 static int v9fs_do_symlink(V9fsState
*s
, V9fsString
*oldpath
,
185 return s
->ops
->symlink(&s
->ctx
, oldpath
->data
, newpath
->data
);
188 static int v9fs_do_link(V9fsState
*s
, V9fsString
*oldpath
, V9fsString
*newpath
)
190 return s
->ops
->link(&s
->ctx
, oldpath
->data
, newpath
->data
);
193 static int v9fs_do_truncate(V9fsState
*s
, V9fsString
*path
, off_t size
)
195 return s
->ops
->truncate(&s
->ctx
, path
->data
, size
);
198 static int v9fs_do_rename(V9fsState
*s
, V9fsString
*oldpath
,
201 return s
->ops
->rename(&s
->ctx
, oldpath
->data
, newpath
->data
);
204 static int v9fs_do_chown(V9fsState
*s
, V9fsString
*path
, uid_t uid
, gid_t gid
)
206 return s
->ops
->chown(&s
->ctx
, path
->data
, uid
, gid
);
209 static int v9fs_do_utime(V9fsState
*s
, V9fsString
*path
,
210 const struct utimbuf
*buf
)
212 return s
->ops
->utime(&s
->ctx
, path
->data
, buf
);
215 static int v9fs_do_remove(V9fsState
*s
, V9fsString
*path
)
217 return s
->ops
->remove(&s
->ctx
, path
->data
);
220 static int v9fs_do_fsync(V9fsState
*s
, int fd
)
222 return s
->ops
->fsync(&s
->ctx
, fd
);
225 static void v9fs_string_init(V9fsString
*str
)
231 static void v9fs_string_free(V9fsString
*str
)
233 qemu_free(str
->data
);
238 static void v9fs_string_null(V9fsString
*str
)
240 v9fs_string_free(str
);
243 static int number_to_string(void *arg
, char type
)
245 unsigned int ret
= 0;
249 unsigned int num
= *(unsigned int *)arg
;
258 printf("Number_to_string: Unknown number format\n");
265 static int v9fs_string_alloc_printf(char **strp
, const char *fmt
, va_list ap
)
268 char *iter
= (char *)fmt
;
272 unsigned int arg_uint
;
274 /* Find the number of %'s that denotes an argument */
275 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
280 len
= strlen(fmt
) - 2*nr_args
;
290 /* Now parse the format string */
291 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
295 arg_uint
= va_arg(ap2
, unsigned int);
296 len
+= number_to_string((void *)&arg_uint
, 'u');
299 arg_char_ptr
= va_arg(ap2
, char *);
300 len
+= strlen(arg_char_ptr
);
307 "v9fs_string_alloc_printf:Incorrect format %c", *iter
);
314 *strp
= qemu_malloc((len
+ 1) * sizeof(**strp
));
316 return vsprintf(*strp
, fmt
, ap
);
319 static void v9fs_string_sprintf(V9fsString
*str
, const char *fmt
, ...)
324 v9fs_string_free(str
);
327 err
= v9fs_string_alloc_printf(&str
->data
, fmt
, ap
);
334 static void v9fs_string_copy(V9fsString
*lhs
, V9fsString
*rhs
)
336 v9fs_string_free(lhs
);
337 v9fs_string_sprintf(lhs
, "%s", rhs
->data
);
340 static size_t v9fs_string_size(V9fsString
*str
)
345 static V9fsFidState
*lookup_fid(V9fsState
*s
, int32_t fid
)
349 for (f
= s
->fid_list
; f
; f
= f
->next
) {
351 v9fs_do_setuid(s
, f
->uid
);
359 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
363 f
= lookup_fid(s
, fid
);
368 f
= qemu_mallocz(sizeof(V9fsFidState
));
374 f
->next
= s
->fid_list
;
380 static int free_fid(V9fsState
*s
, int32_t fid
)
382 V9fsFidState
**fidpp
, *fidp
;
384 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
385 if ((*fidpp
)->fid
== fid
) {
390 if (*fidpp
== NULL
) {
397 if (fidp
->fd
!= -1) {
398 v9fs_do_close(s
, fidp
->fd
);
401 v9fs_do_closedir(s
, fidp
->dir
);
403 v9fs_string_free(&fidp
->path
);
409 #define P9_QID_TYPE_DIR 0x80
410 #define P9_QID_TYPE_SYMLINK 0x02
412 #define P9_STAT_MODE_DIR 0x80000000
413 #define P9_STAT_MODE_APPEND 0x40000000
414 #define P9_STAT_MODE_EXCL 0x20000000
415 #define P9_STAT_MODE_MOUNT 0x10000000
416 #define P9_STAT_MODE_AUTH 0x08000000
417 #define P9_STAT_MODE_TMP 0x04000000
418 #define P9_STAT_MODE_SYMLINK 0x02000000
419 #define P9_STAT_MODE_LINK 0x01000000
420 #define P9_STAT_MODE_DEVICE 0x00800000
421 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
422 #define P9_STAT_MODE_SOCKET 0x00100000
423 #define P9_STAT_MODE_SETUID 0x00080000
424 #define P9_STAT_MODE_SETGID 0x00040000
425 #define P9_STAT_MODE_SETVTX 0x00010000
427 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
428 P9_STAT_MODE_SYMLINK | \
429 P9_STAT_MODE_LINK | \
430 P9_STAT_MODE_DEVICE | \
431 P9_STAT_MODE_NAMED_PIPE | \
434 /* This is the algorithm from ufs in spfs */
435 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
439 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
440 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
441 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
443 if (S_ISDIR(stbuf
->st_mode
)) {
444 qidp
->type
|= P9_QID_TYPE_DIR
;
446 if (S_ISLNK(stbuf
->st_mode
)) {
447 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
451 static int fid_to_qid(V9fsState
*s
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
456 err
= v9fs_do_lstat(s
, &fidp
->path
, &stbuf
);
461 stat_to_qid(&stbuf
, qidp
);
465 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
469 if (!QLIST_EMPTY(&s
->free_list
)) {
470 pdu
= QLIST_FIRST(&s
->free_list
);
471 QLIST_REMOVE(pdu
, next
);
476 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
479 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
483 size_t pdu_packunpack(void *addr
, struct iovec
*sg
, int sg_count
,
484 size_t offset
, size_t size
, int pack
)
489 for (i
= 0; size
&& i
< sg_count
; i
++) {
491 if (offset
>= sg
[i
].iov_len
) {
493 offset
-= sg
[i
].iov_len
;
496 len
= MIN(sg
[i
].iov_len
- offset
, size
);
498 memcpy(sg
[i
].iov_base
+ offset
, addr
, len
);
500 memcpy(addr
, sg
[i
].iov_base
+ offset
, len
);
515 static size_t pdu_unpack(void *dst
, V9fsPDU
*pdu
, size_t offset
, size_t size
)
517 return pdu_packunpack(dst
, pdu
->elem
.out_sg
, pdu
->elem
.out_num
,
521 static size_t pdu_pack(V9fsPDU
*pdu
, size_t offset
, const void *src
,
524 return pdu_packunpack((void *)src
, pdu
->elem
.in_sg
, pdu
->elem
.in_num
,
528 static int pdu_copy_sg(V9fsPDU
*pdu
, size_t offset
, int rx
, struct iovec
*sg
)
532 struct iovec
*src_sg
;
536 src_sg
= pdu
->elem
.in_sg
;
537 num
= pdu
->elem
.in_num
;
539 src_sg
= pdu
->elem
.out_sg
;
540 num
= pdu
->elem
.out_num
;
544 for (i
= 0; i
< num
; i
++) {
546 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
547 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
549 } else if (offset
< (src_sg
[i
].iov_len
+ pos
)) {
550 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
551 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
552 sg
[j
].iov_base
+= (offset
- pos
);
553 sg
[j
].iov_len
-= (offset
- pos
);
556 pos
+= src_sg
[i
].iov_len
;
562 static size_t pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
564 size_t old_offset
= offset
;
569 for (i
= 0; fmt
[i
]; i
++) {
572 uint8_t *valp
= va_arg(ap
, uint8_t *);
573 offset
+= pdu_unpack(valp
, pdu
, offset
, sizeof(*valp
));
578 valp
= va_arg(ap
, uint16_t *);
579 val
= le16_to_cpupu(valp
);
580 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
586 valp
= va_arg(ap
, uint32_t *);
587 val
= le32_to_cpupu(valp
);
588 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
594 valp
= va_arg(ap
, uint64_t *);
595 val
= le64_to_cpup(valp
);
596 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
601 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
602 int *iovcnt
= va_arg(ap
, int *);
603 *iovcnt
= pdu_copy_sg(pdu
, offset
, 0, iov
);
607 V9fsString
*str
= va_arg(ap
, V9fsString
*);
608 offset
+= pdu_unmarshal(pdu
, offset
, "w", &str
->size
);
609 /* FIXME: sanity check str->size */
610 str
->data
= qemu_malloc(str
->size
+ 1);
611 offset
+= pdu_unpack(str
->data
, pdu
, offset
, str
->size
);
612 str
->data
[str
->size
] = 0;
616 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
617 offset
+= pdu_unmarshal(pdu
, offset
, "bdq",
618 &qidp
->type
, &qidp
->version
, &qidp
->path
);
622 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
623 offset
+= pdu_unmarshal(pdu
, offset
, "wwdQdddqsssssddd",
624 &statp
->size
, &statp
->type
, &statp
->dev
,
625 &statp
->qid
, &statp
->mode
, &statp
->atime
,
626 &statp
->mtime
, &statp
->length
,
627 &statp
->name
, &statp
->uid
, &statp
->gid
,
628 &statp
->muid
, &statp
->extension
,
629 &statp
->n_uid
, &statp
->n_gid
,
640 return offset
- old_offset
;
643 static size_t pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
645 size_t old_offset
= offset
;
650 for (i
= 0; fmt
[i
]; i
++) {
653 uint8_t val
= va_arg(ap
, int);
654 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
659 cpu_to_le16w(&val
, va_arg(ap
, int));
660 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
665 cpu_to_le32w(&val
, va_arg(ap
, uint32_t));
666 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
671 cpu_to_le64w(&val
, va_arg(ap
, uint64_t));
672 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
676 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
677 int *iovcnt
= va_arg(ap
, int *);
678 *iovcnt
= pdu_copy_sg(pdu
, offset
, 1, iov
);
682 V9fsString
*str
= va_arg(ap
, V9fsString
*);
683 offset
+= pdu_marshal(pdu
, offset
, "w", str
->size
);
684 offset
+= pdu_pack(pdu
, offset
, str
->data
, str
->size
);
688 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
689 offset
+= pdu_marshal(pdu
, offset
, "bdq",
690 qidp
->type
, qidp
->version
, qidp
->path
);
694 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
695 offset
+= pdu_marshal(pdu
, offset
, "wwdQdddqsssssddd",
696 statp
->size
, statp
->type
, statp
->dev
,
697 &statp
->qid
, statp
->mode
, statp
->atime
,
698 statp
->mtime
, statp
->length
, &statp
->name
,
699 &statp
->uid
, &statp
->gid
, &statp
->muid
,
700 &statp
->extension
, statp
->n_uid
,
701 statp
->n_gid
, statp
->n_muid
);
710 return offset
- old_offset
;
713 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
715 int8_t id
= pdu
->id
+ 1; /* Response */
721 str
.data
= strerror(err
);
722 str
.size
= strlen(str
.data
);
725 len
+= pdu_marshal(pdu
, len
, "s", &str
);
727 len
+= pdu_marshal(pdu
, len
, "d", err
);
733 /* fill out the header */
734 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
736 /* keep these in sync */
740 /* push onto queue and notify */
741 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
743 /* FIXME: we should batch these completions */
744 virtio_notify(&s
->vdev
, s
->vq
);
749 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
754 if (mode
& P9_STAT_MODE_DIR
) {
759 if (mode
& P9_STAT_MODE_SYMLINK
) {
762 if (mode
& P9_STAT_MODE_SOCKET
) {
765 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
768 if (mode
& P9_STAT_MODE_DEVICE
) {
769 if (extension
&& extension
->data
[0] == 'c') {
781 if (mode
& P9_STAT_MODE_SETUID
) {
784 if (mode
& P9_STAT_MODE_SETGID
) {
787 if (mode
& P9_STAT_MODE_SETVTX
) {
794 static int donttouch_stat(V9fsStat
*stat
)
796 if (stat
->type
== -1 &&
798 stat
->qid
.type
== -1 &&
799 stat
->qid
.version
== -1 &&
800 stat
->qid
.path
== -1 &&
804 stat
->length
== -1 &&
811 stat
->n_muid
== -1) {
818 static void v9fs_stat_free(V9fsStat
*stat
)
820 v9fs_string_free(&stat
->name
);
821 v9fs_string_free(&stat
->uid
);
822 v9fs_string_free(&stat
->gid
);
823 v9fs_string_free(&stat
->muid
);
824 v9fs_string_free(&stat
->extension
);
827 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
831 mode
= stbuf
->st_mode
& 0777;
832 if (S_ISDIR(stbuf
->st_mode
)) {
833 mode
|= P9_STAT_MODE_DIR
;
837 if (S_ISLNK(stbuf
->st_mode
)) {
838 mode
|= P9_STAT_MODE_SYMLINK
;
841 if (S_ISSOCK(stbuf
->st_mode
)) {
842 mode
|= P9_STAT_MODE_SOCKET
;
845 if (S_ISFIFO(stbuf
->st_mode
)) {
846 mode
|= P9_STAT_MODE_NAMED_PIPE
;
849 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
850 mode
|= P9_STAT_MODE_DEVICE
;
853 if (stbuf
->st_mode
& S_ISUID
) {
854 mode
|= P9_STAT_MODE_SETUID
;
857 if (stbuf
->st_mode
& S_ISGID
) {
858 mode
|= P9_STAT_MODE_SETGID
;
861 if (stbuf
->st_mode
& S_ISVTX
) {
862 mode
|= P9_STAT_MODE_SETVTX
;
869 static int stat_to_v9stat(V9fsState
*s
, V9fsString
*name
,
870 const struct stat
*stbuf
,
876 memset(v9stat
, 0, sizeof(*v9stat
));
878 stat_to_qid(stbuf
, &v9stat
->qid
);
879 v9stat
->mode
= stat_to_v9mode(stbuf
);
880 v9stat
->atime
= stbuf
->st_atime
;
881 v9stat
->mtime
= stbuf
->st_mtime
;
882 v9stat
->length
= stbuf
->st_size
;
884 v9fs_string_null(&v9stat
->uid
);
885 v9fs_string_null(&v9stat
->gid
);
886 v9fs_string_null(&v9stat
->muid
);
889 v9stat
->n_uid
= stbuf
->st_uid
;
890 v9stat
->n_gid
= stbuf
->st_gid
;
893 v9fs_string_null(&v9stat
->extension
);
895 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
896 err
= v9fs_do_readlink(s
, name
, &v9stat
->extension
);
901 v9stat
->extension
.data
[err
] = 0;
902 v9stat
->extension
.size
= err
;
903 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
904 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
905 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
906 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
907 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
908 v9fs_string_sprintf(&v9stat
->extension
, "%s %u",
909 "HARDLINKCOUNT", stbuf
->st_nlink
);
913 str
= strrchr(name
->data
, '/');
920 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
923 v9fs_string_size(&v9stat
->name
) +
924 v9fs_string_size(&v9stat
->uid
) +
925 v9fs_string_size(&v9stat
->gid
) +
926 v9fs_string_size(&v9stat
->muid
) +
927 v9fs_string_size(&v9stat
->extension
);
931 static struct iovec
*adjust_sg(struct iovec
*sg
, int len
, int *iovcnt
)
933 while (len
&& *iovcnt
) {
934 if (len
< sg
->iov_len
) {
948 static struct iovec
*cap_sg(struct iovec
*sg
, int cap
, int *cnt
)
953 for (i
= 0; i
< *cnt
; i
++) {
954 if ((total
+ sg
[i
].iov_len
) > cap
) {
955 sg
[i
].iov_len
-= ((total
+ sg
[i
].iov_len
) - cap
);
959 total
+= sg
[i
].iov_len
;
967 static void print_sg(struct iovec
*sg
, int cnt
)
971 printf("sg[%d]: {", cnt
);
972 for (i
= 0; i
< cnt
; i
++) {
976 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
981 static void v9fs_fix_path(V9fsString
*dst
, V9fsString
*src
, int len
)
984 v9fs_string_init(&str
);
985 v9fs_string_copy(&str
, dst
);
986 v9fs_string_sprintf(dst
, "%s%s", src
->data
, str
.data
+len
);
987 v9fs_string_free(&str
);
990 static void v9fs_version(V9fsState
*s
, V9fsPDU
*pdu
)
996 pdu_unmarshal(pdu
, offset
, "ds", &msize
, &version
);
998 if (strcmp(version
.data
, "9P2000.u")) {
999 v9fs_string_sprintf(&version
, "unknown");
1002 offset
+= pdu_marshal(pdu
, offset
, "ds", msize
, &version
);
1003 complete_pdu(s
, pdu
, offset
);
1005 v9fs_string_free(&version
);
1008 static void v9fs_attach(V9fsState
*s
, V9fsPDU
*pdu
)
1010 int32_t fid
, afid
, n_uname
;
1011 V9fsString uname
, aname
;
1017 pdu_unmarshal(pdu
, offset
, "ddssd", &fid
, &afid
, &uname
, &aname
, &n_uname
);
1019 fidp
= alloc_fid(s
, fid
);
1025 fidp
->uid
= n_uname
;
1027 v9fs_string_sprintf(&fidp
->path
, "%s", "/");
1028 err
= fid_to_qid(s
, fidp
, &qid
);
1035 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
1039 complete_pdu(s
, pdu
, err
);
1040 v9fs_string_free(&uname
);
1041 v9fs_string_free(&aname
);
1044 static void v9fs_stat_post_lstat(V9fsState
*s
, V9fsStatState
*vs
, int err
)
1051 err
= stat_to_v9stat(s
, &vs
->fidp
->path
, &vs
->stbuf
, &vs
->v9stat
);
1055 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "wS", 0, &vs
->v9stat
);
1059 complete_pdu(s
, vs
->pdu
, err
);
1060 v9fs_stat_free(&vs
->v9stat
);
1064 static void v9fs_stat(V9fsState
*s
, V9fsPDU
*pdu
)
1070 vs
= qemu_malloc(sizeof(*vs
));
1074 memset(&vs
->v9stat
, 0, sizeof(vs
->v9stat
));
1076 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &fid
);
1078 vs
->fidp
= lookup_fid(s
, fid
);
1079 if (vs
->fidp
== NULL
) {
1084 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1085 v9fs_stat_post_lstat(s
, vs
, err
);
1089 complete_pdu(s
, vs
->pdu
, err
);
1090 v9fs_stat_free(&vs
->v9stat
);
1094 static void v9fs_walk_complete(V9fsState
*s
, V9fsWalkState
*vs
, int err
)
1096 complete_pdu(s
, vs
->pdu
, err
);
1099 for (vs
->name_idx
= 0; vs
->name_idx
< vs
->nwnames
; vs
->name_idx
++) {
1100 v9fs_string_free(&vs
->wnames
[vs
->name_idx
]);
1103 qemu_free(vs
->wnames
);
1104 qemu_free(vs
->qids
);
1108 static void v9fs_walk_marshal(V9fsWalkState
*vs
)
1112 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "w", vs
->nwnames
);
1114 for (i
= 0; i
< vs
->nwnames
; i
++) {
1115 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qids
[i
]);
1119 static void v9fs_walk_post_newfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
1123 free_fid(s
, vs
->newfidp
->fid
);
1124 v9fs_string_free(&vs
->path
);
1129 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1132 if (vs
->name_idx
< vs
->nwnames
) {
1133 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1134 vs
->wnames
[vs
->name_idx
].data
);
1135 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1137 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1138 v9fs_walk_post_newfid_lstat(s
, vs
, err
);
1142 v9fs_string_free(&vs
->path
);
1143 v9fs_walk_marshal(vs
);
1146 v9fs_walk_complete(s
, vs
, err
);
1149 static void v9fs_walk_post_oldfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
1153 v9fs_string_free(&vs
->path
);
1158 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1160 if (vs
->name_idx
< vs
->nwnames
) {
1162 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1163 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1164 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1166 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1167 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1171 v9fs_string_free(&vs
->path
);
1172 v9fs_walk_marshal(vs
);
1175 v9fs_walk_complete(s
, vs
, err
);
1178 static void v9fs_walk(V9fsState
*s
, V9fsPDU
*pdu
)
1180 int32_t fid
, newfid
;
1185 vs
= qemu_malloc(sizeof(*vs
));
1191 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "ddw", &fid
,
1192 &newfid
, &vs
->nwnames
);
1195 vs
->wnames
= qemu_mallocz(sizeof(vs
->wnames
[0]) * vs
->nwnames
);
1197 vs
->qids
= qemu_mallocz(sizeof(vs
->qids
[0]) * vs
->nwnames
);
1199 for (i
= 0; i
< vs
->nwnames
; i
++) {
1200 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "s",
1205 vs
->fidp
= lookup_fid(s
, fid
);
1206 if (vs
->fidp
== NULL
) {
1211 /* FIXME: is this really valid? */
1212 if (fid
== newfid
) {
1214 BUG_ON(vs
->fidp
->fd
!= -1);
1215 BUG_ON(vs
->fidp
->dir
);
1216 v9fs_string_init(&vs
->path
);
1219 if (vs
->name_idx
< vs
->nwnames
) {
1220 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1221 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1222 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1224 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1225 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1229 vs
->newfidp
= alloc_fid(s
, newfid
);
1230 if (vs
->newfidp
== NULL
) {
1235 vs
->newfidp
->uid
= vs
->fidp
->uid
;
1236 v9fs_string_init(&vs
->path
);
1238 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->fidp
->path
);
1240 if (vs
->name_idx
< vs
->nwnames
) {
1241 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1242 vs
->wnames
[vs
->name_idx
].data
);
1243 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1245 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1246 v9fs_walk_post_newfid_lstat(s
, vs
, err
);
1251 v9fs_walk_marshal(vs
);
1254 v9fs_walk_complete(s
, vs
, err
);
1257 static void v9fs_open_post_opendir(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1259 if (vs
->fidp
->dir
== NULL
) {
1264 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, 0);
1267 complete_pdu(s
, vs
->pdu
, err
);
1272 static void v9fs_open_post_open(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1274 if (vs
->fidp
->fd
== -1) {
1279 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, 0);
1282 complete_pdu(s
, vs
->pdu
, err
);
1286 static void v9fs_open_post_lstat(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1293 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
1295 if (S_ISDIR(vs
->stbuf
.st_mode
)) {
1296 vs
->fidp
->dir
= v9fs_do_opendir(s
, &vs
->fidp
->path
);
1297 v9fs_open_post_opendir(s
, vs
, err
);
1299 vs
->fidp
->fd
= v9fs_do_open(s
, &vs
->fidp
->path
,
1300 omode_to_uflags(vs
->mode
));
1301 v9fs_open_post_open(s
, vs
, err
);
1305 complete_pdu(s
, vs
->pdu
, err
);
1309 static void v9fs_open(V9fsState
*s
, V9fsPDU
*pdu
)
1316 vs
= qemu_malloc(sizeof(*vs
));
1320 pdu_unmarshal(vs
->pdu
, vs
->offset
, "db", &fid
, &vs
->mode
);
1322 vs
->fidp
= lookup_fid(s
, fid
);
1323 if (vs
->fidp
== NULL
) {
1328 BUG_ON(vs
->fidp
->fd
!= -1);
1329 BUG_ON(vs
->fidp
->dir
);
1331 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1333 v9fs_open_post_lstat(s
, vs
, err
);
1336 complete_pdu(s
, pdu
, err
);
1340 static void v9fs_clunk(V9fsState
*s
, V9fsPDU
*pdu
)
1346 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1348 err
= free_fid(s
, fid
);
1356 complete_pdu(s
, pdu
, err
);
1359 static void v9fs_read_post_readdir(V9fsState
*, V9fsReadState
*, ssize_t
);
1361 static void v9fs_read_post_seekdir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1366 v9fs_stat_free(&vs
->v9stat
);
1367 v9fs_string_free(&vs
->name
);
1368 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
1369 vs
->offset
+= vs
->count
;
1372 complete_pdu(s
, vs
->pdu
, err
);
1377 static void v9fs_read_post_dir_lstat(V9fsState
*s
, V9fsReadState
*vs
,
1384 err
= stat_to_v9stat(s
, &vs
->name
, &vs
->stbuf
, &vs
->v9stat
);
1389 vs
->len
= pdu_marshal(vs
->pdu
, vs
->offset
+ 4 + vs
->count
, "S",
1391 if ((vs
->len
!= (vs
->v9stat
.size
+ 2)) ||
1392 ((vs
->count
+ vs
->len
) > vs
->max_count
)) {
1393 v9fs_do_seekdir(s
, vs
->fidp
->dir
, vs
->dir_pos
);
1394 v9fs_read_post_seekdir(s
, vs
, err
);
1397 vs
->count
+= vs
->len
;
1398 v9fs_stat_free(&vs
->v9stat
);
1399 v9fs_string_free(&vs
->name
);
1400 vs
->dir_pos
= vs
->dent
->d_off
;
1401 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->dir
);
1402 v9fs_read_post_readdir(s
, vs
, err
);
1405 v9fs_do_seekdir(s
, vs
->fidp
->dir
, vs
->dir_pos
);
1406 v9fs_read_post_seekdir(s
, vs
, err
);
1411 static void v9fs_read_post_readdir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1414 memset(&vs
->v9stat
, 0, sizeof(vs
->v9stat
));
1415 v9fs_string_init(&vs
->name
);
1416 v9fs_string_sprintf(&vs
->name
, "%s/%s", vs
->fidp
->path
.data
,
1418 err
= v9fs_do_lstat(s
, &vs
->name
, &vs
->stbuf
);
1419 v9fs_read_post_dir_lstat(s
, vs
, err
);
1423 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
1424 vs
->offset
+= vs
->count
;
1426 complete_pdu(s
, vs
->pdu
, err
);
1431 static void v9fs_read_post_telldir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1433 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->dir
);
1434 v9fs_read_post_readdir(s
, vs
, err
);
1438 static void v9fs_read_post_rewinddir(V9fsState
*s
, V9fsReadState
*vs
,
1441 vs
->dir_pos
= v9fs_do_telldir(s
, vs
->fidp
->dir
);
1442 v9fs_read_post_telldir(s
, vs
, err
);
1446 static void v9fs_read_post_readv(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1449 /* IO error return the error */
1453 vs
->total
+= vs
->len
;
1454 vs
->sg
= adjust_sg(vs
->sg
, vs
->len
, &vs
->cnt
);
1455 if (vs
->total
< vs
->count
&& vs
->len
> 0) {
1458 print_sg(vs
->sg
, vs
->cnt
);
1460 vs
->len
= v9fs_do_readv(s
, vs
->fidp
->fd
, vs
->sg
, vs
->cnt
);
1461 } while (vs
->len
== -1 && errno
== EINTR
);
1462 if (vs
->len
== -1) {
1465 v9fs_read_post_readv(s
, vs
, err
);
1468 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->total
);
1469 vs
->offset
+= vs
->count
;
1473 complete_pdu(s
, vs
->pdu
, err
);
1477 static void v9fs_read_post_lseek(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1483 vs
->sg
= cap_sg(vs
->sg
, vs
->count
, &vs
->cnt
);
1485 if (vs
->total
< vs
->count
) {
1488 print_sg(vs
->sg
, vs
->cnt
);
1490 vs
->len
= v9fs_do_readv(s
, vs
->fidp
->fd
, vs
->sg
, vs
->cnt
);
1491 } while (vs
->len
== -1 && errno
== EINTR
);
1492 if (vs
->len
== -1) {
1495 v9fs_read_post_readv(s
, vs
, err
);
1499 complete_pdu(s
, vs
->pdu
, err
);
1503 static void v9fs_read(V9fsState
*s
, V9fsPDU
*pdu
)
1509 vs
= qemu_malloc(sizeof(*vs
));
1516 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dqd", &fid
, &vs
->off
, &vs
->count
);
1518 vs
->fidp
= lookup_fid(s
, fid
);
1519 if (vs
->fidp
== NULL
) {
1524 if (vs
->fidp
->dir
) {
1525 vs
->max_count
= vs
->count
;
1528 v9fs_do_rewinddir(s
, vs
->fidp
->dir
);
1530 v9fs_read_post_rewinddir(s
, vs
, err
);
1532 } else if (vs
->fidp
->fd
!= -1) {
1534 pdu_marshal(vs
->pdu
, vs
->offset
+ 4, "v", vs
->sg
, &vs
->cnt
);
1535 err
= v9fs_do_lseek(s
, vs
->fidp
->fd
, vs
->off
, SEEK_SET
);
1536 v9fs_read_post_lseek(s
, vs
, err
);
1542 complete_pdu(s
, pdu
, err
);
1546 static void v9fs_write_post_writev(V9fsState
*s
, V9fsWriteState
*vs
,
1550 /* IO error return the error */
1554 vs
->total
+= vs
->len
;
1555 vs
->sg
= adjust_sg(vs
->sg
, vs
->len
, &vs
->cnt
);
1556 if (vs
->total
< vs
->count
&& vs
->len
> 0) {
1559 print_sg(vs
->sg
, vs
->cnt
);
1561 vs
->len
= v9fs_do_writev(s
, vs
->fidp
->fd
, vs
->sg
, vs
->cnt
);
1562 } while (vs
->len
== -1 && errno
== EINTR
);
1563 if (vs
->len
== -1) {
1566 v9fs_write_post_writev(s
, vs
, err
);
1569 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->total
);
1573 complete_pdu(s
, vs
->pdu
, err
);
1577 static void v9fs_write_post_lseek(V9fsState
*s
, V9fsWriteState
*vs
, ssize_t err
)
1583 vs
->sg
= cap_sg(vs
->sg
, vs
->count
, &vs
->cnt
);
1585 if (vs
->total
< vs
->count
) {
1588 print_sg(vs
->sg
, vs
->cnt
);
1590 vs
->len
= v9fs_do_writev(s
, vs
->fidp
->fd
, vs
->sg
, vs
->cnt
);
1591 } while (vs
->len
== -1 && errno
== EINTR
);
1592 if (vs
->len
== -1) {
1595 v9fs_write_post_writev(s
, vs
, err
);
1600 complete_pdu(s
, vs
->pdu
, err
);
1604 static void v9fs_write(V9fsState
*s
, V9fsPDU
*pdu
)
1610 vs
= qemu_malloc(sizeof(*vs
));
1618 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dqdv", &fid
, &vs
->off
, &vs
->count
,
1621 vs
->fidp
= lookup_fid(s
, fid
);
1622 if (vs
->fidp
== NULL
) {
1627 if (vs
->fidp
->fd
== -1) {
1632 err
= v9fs_do_lseek(s
, vs
->fidp
->fd
, vs
->off
, SEEK_SET
);
1634 v9fs_write_post_lseek(s
, vs
, err
);
1638 complete_pdu(s
, vs
->pdu
, err
);
1642 static void v9fs_post_create(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1645 v9fs_string_copy(&vs
->fidp
->path
, &vs
->fullname
);
1646 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
1648 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, 0);
1653 complete_pdu(s
, vs
->pdu
, err
);
1654 v9fs_string_free(&vs
->name
);
1655 v9fs_string_free(&vs
->extension
);
1656 v9fs_string_free(&vs
->fullname
);
1660 static void v9fs_create_post_perms(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1665 v9fs_post_create(s
, vs
, err
);
1668 static void v9fs_create_post_opendir(V9fsState
*s
, V9fsCreateState
*vs
,
1671 if (!vs
->fidp
->dir
) {
1674 v9fs_post_create(s
, vs
, err
);
1677 static void v9fs_create_post_dir_lstat(V9fsState
*s
, V9fsCreateState
*vs
,
1685 vs
->fidp
->dir
= v9fs_do_opendir(s
, &vs
->fullname
);
1686 v9fs_create_post_opendir(s
, vs
, err
);
1690 v9fs_post_create(s
, vs
, err
);
1693 static void v9fs_create_post_mkdir(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1700 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
1701 v9fs_create_post_dir_lstat(s
, vs
, err
);
1705 v9fs_post_create(s
, vs
, err
);
1708 static void v9fs_create_post_mksock(V9fsState
*s
, V9fsCreateState
*vs
,
1716 err
= v9fs_do_chmod(s
, &vs
->fullname
, vs
->perm
& 0777);
1717 v9fs_create_post_perms(s
, vs
, err
);
1721 v9fs_post_create(s
, vs
, err
);
1724 static void v9fs_create_post_fstat(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1731 v9fs_post_create(s
, vs
, err
);
1735 static void v9fs_create_post_open2(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1737 if (vs
->fidp
->fd
== -1) {
1742 err
= v9fs_do_fstat(s
, vs
->fidp
->fd
, &vs
->stbuf
);
1743 v9fs_create_post_fstat(s
, vs
, err
);
1748 v9fs_post_create(s
, vs
, err
);
1752 static void v9fs_create_post_lstat(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1755 if (err
== 0 || errno
!= ENOENT
) {
1760 if (vs
->perm
& P9_STAT_MODE_DIR
) {
1761 err
= v9fs_do_mkdir(s
, &vs
->fullname
, vs
->perm
& 0777);
1762 v9fs_create_post_mkdir(s
, vs
, err
);
1763 } else if (vs
->perm
& P9_STAT_MODE_SYMLINK
) {
1764 err
= v9fs_do_symlink(s
, &vs
->extension
, &vs
->fullname
);
1765 v9fs_create_post_perms(s
, vs
, err
);
1766 } else if (vs
->perm
& P9_STAT_MODE_LINK
) {
1767 int32_t nfid
= atoi(vs
->extension
.data
);
1768 V9fsFidState
*nfidp
= lookup_fid(s
, nfid
);
1769 if (nfidp
== NULL
) {
1771 v9fs_post_create(s
, vs
, err
);
1773 err
= v9fs_do_link(s
, &nfidp
->path
, &vs
->fullname
);
1774 v9fs_create_post_perms(s
, vs
, err
);
1775 } else if (vs
->perm
& P9_STAT_MODE_DEVICE
) {
1777 uint32_t major
, minor
;
1780 if (sscanf(vs
->extension
.data
, "%c %u %u", &ctype
, &major
,
1783 v9fs_post_create(s
, vs
, err
);
1795 v9fs_post_create(s
, vs
, err
);
1798 nmode
|= vs
->perm
& 0777;
1799 err
= v9fs_do_mknod(s
, &vs
->fullname
, nmode
, makedev(major
, minor
));
1800 v9fs_create_post_perms(s
, vs
, err
);
1801 } else if (vs
->perm
& P9_STAT_MODE_NAMED_PIPE
) {
1802 err
= v9fs_do_mknod(s
, &vs
->fullname
, S_IFIFO
| (vs
->mode
& 0777), 0);
1803 v9fs_post_create(s
, vs
, err
);
1804 } else if (vs
->perm
& P9_STAT_MODE_SOCKET
) {
1805 err
= v9fs_do_mksock(s
, &vs
->fullname
);
1806 v9fs_create_post_mksock(s
, vs
, err
);
1808 vs
->fidp
->fd
= v9fs_do_open2(s
, &vs
->fullname
,
1809 omode_to_uflags(vs
->mode
) | O_CREAT
,
1811 v9fs_create_post_open2(s
, vs
, err
);
1817 v9fs_post_create(s
, vs
, err
);
1820 static void v9fs_create(V9fsState
*s
, V9fsPDU
*pdu
)
1823 V9fsCreateState
*vs
;
1826 vs
= qemu_malloc(sizeof(*vs
));
1830 v9fs_string_init(&vs
->fullname
);
1832 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsdbs", &fid
, &vs
->name
,
1833 &vs
->perm
, &vs
->mode
, &vs
->extension
);
1835 vs
->fidp
= lookup_fid(s
, fid
);
1836 if (vs
->fidp
== NULL
) {
1841 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", vs
->fidp
->path
.data
,
1844 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
1845 v9fs_create_post_lstat(s
, vs
, err
);
1849 complete_pdu(s
, vs
->pdu
, err
);
1850 v9fs_string_free(&vs
->name
);
1851 v9fs_string_free(&vs
->extension
);
1855 static void v9fs_flush(V9fsState
*s
, V9fsPDU
*pdu
)
1857 /* A nop call with no return */
1858 complete_pdu(s
, pdu
, 7);
1861 static void v9fs_remove_post_remove(V9fsState
*s
, V9fsRemoveState
*vs
,
1864 /* For TREMOVE we need to clunk the fid even on failed remove */
1865 err
= free_fid(s
, vs
->fidp
->fid
);
1872 complete_pdu(s
, vs
->pdu
, err
);
1876 static void v9fs_remove(V9fsState
*s
, V9fsPDU
*pdu
)
1879 V9fsRemoveState
*vs
;
1882 vs
= qemu_malloc(sizeof(*vs
));
1886 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &fid
);
1888 vs
->fidp
= lookup_fid(s
, fid
);
1889 if (vs
->fidp
== NULL
) {
1894 err
= v9fs_do_remove(s
, &vs
->fidp
->path
);
1895 v9fs_remove_post_remove(s
, vs
, err
);
1899 complete_pdu(s
, pdu
, err
);
1903 static void v9fs_wstat_post_truncate(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
1912 v9fs_stat_free(&vs
->v9stat
);
1913 complete_pdu(s
, vs
->pdu
, err
);
1917 static void v9fs_wstat_post_rename(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
1923 if (vs
->v9stat
.name
.size
!= 0) {
1924 v9fs_string_free(&vs
->nname
);
1927 if (vs
->v9stat
.length
!= -1) {
1928 if (v9fs_do_truncate(s
, &vs
->fidp
->path
, vs
->v9stat
.length
) < 0) {
1932 v9fs_wstat_post_truncate(s
, vs
, err
);
1936 v9fs_stat_free(&vs
->v9stat
);
1937 complete_pdu(s
, vs
->pdu
, err
);
1941 static void v9fs_wstat_post_chown(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
1948 if (vs
->v9stat
.name
.size
!= 0) {
1949 char *old_name
, *new_name
;
1952 old_name
= vs
->fidp
->path
.data
;
1953 end
= strrchr(old_name
, '/');
1960 new_name
= qemu_malloc(end
- old_name
+ vs
->v9stat
.name
.size
+ 1);
1962 memset(new_name
, 0, end
- old_name
+ vs
->v9stat
.name
.size
+ 1);
1963 memcpy(new_name
, old_name
, end
- old_name
);
1964 memcpy(new_name
+ (end
- old_name
), vs
->v9stat
.name
.data
,
1965 vs
->v9stat
.name
.size
);
1966 vs
->nname
.data
= new_name
;
1967 vs
->nname
.size
= strlen(new_name
);
1969 if (strcmp(new_name
, vs
->fidp
->path
.data
) != 0) {
1970 if (v9fs_do_rename(s
, &vs
->fidp
->path
, &vs
->nname
)) {
1974 * Fixup fid's pointing to the old name to
1975 * start pointing to the new name
1977 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
1979 if (vs
->fidp
== fidp
) {
1981 * we replace name of this fid towards the end
1982 * so that our below strcmp will work
1986 if (!strncmp(vs
->fidp
->path
.data
, fidp
->path
.data
,
1987 strlen(vs
->fidp
->path
.data
))) {
1988 /* replace the name */
1989 v9fs_fix_path(&fidp
->path
, &vs
->nname
,
1990 strlen(vs
->fidp
->path
.data
));
1993 v9fs_string_copy(&vs
->fidp
->path
, &vs
->nname
);
1997 v9fs_wstat_post_rename(s
, vs
, err
);
2001 v9fs_stat_free(&vs
->v9stat
);
2002 complete_pdu(s
, vs
->pdu
, err
);
2006 static void v9fs_wstat_post_utime(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2012 if (vs
->v9stat
.n_gid
!= -1) {
2013 if (v9fs_do_chown(s
, &vs
->fidp
->path
, vs
->v9stat
.n_uid
,
2014 vs
->v9stat
.n_gid
)) {
2018 v9fs_wstat_post_chown(s
, vs
, err
);
2022 v9fs_stat_free(&vs
->v9stat
);
2023 complete_pdu(s
, vs
->pdu
, err
);
2027 static void v9fs_wstat_post_chmod(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2033 if (vs
->v9stat
.mtime
!= -1) {
2036 tb
.modtime
= vs
->v9stat
.mtime
;
2037 if (v9fs_do_utime(s
, &vs
->fidp
->path
, &tb
)) {
2042 v9fs_wstat_post_utime(s
, vs
, err
);
2046 v9fs_stat_free(&vs
->v9stat
);
2047 complete_pdu(s
, vs
->pdu
, err
);
2051 static void v9fs_wstat_post_fsync(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2056 v9fs_stat_free(&vs
->v9stat
);
2057 complete_pdu(s
, vs
->pdu
, err
);
2061 static void v9fs_wstat_post_lstat(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2070 v9_mode
= stat_to_v9mode(&vs
->stbuf
);
2072 if ((vs
->v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2073 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2074 /* Attempting to change the type */
2079 if (v9fs_do_chmod(s
, &vs
->fidp
->path
, v9mode_to_mode(vs
->v9stat
.mode
,
2080 &vs
->v9stat
.extension
))) {
2083 v9fs_wstat_post_chmod(s
, vs
, err
);
2087 v9fs_stat_free(&vs
->v9stat
);
2088 complete_pdu(s
, vs
->pdu
, err
);
2092 static void v9fs_wstat(V9fsState
*s
, V9fsPDU
*pdu
)
2098 vs
= qemu_malloc(sizeof(*vs
));
2102 pdu_unmarshal(pdu
, vs
->offset
, "dwS", &fid
, &vs
->unused
, &vs
->v9stat
);
2104 vs
->fidp
= lookup_fid(s
, fid
);
2105 if (vs
->fidp
== NULL
) {
2110 /* do we need to sync the file? */
2111 if (donttouch_stat(&vs
->v9stat
)) {
2112 err
= v9fs_do_fsync(s
, vs
->fidp
->fd
);
2113 v9fs_wstat_post_fsync(s
, vs
, err
);
2117 if (vs
->v9stat
.mode
!= -1) {
2118 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
2119 v9fs_wstat_post_lstat(s
, vs
, err
);
2123 v9fs_wstat_post_chmod(s
, vs
, err
);
2127 v9fs_stat_free(&vs
->v9stat
);
2128 complete_pdu(s
, vs
->pdu
, err
);
2132 typedef void (pdu_handler_t
)(V9fsState
*s
, V9fsPDU
*pdu
);
2134 static pdu_handler_t
*pdu_handlers
[] = {
2135 [P9_TVERSION
] = v9fs_version
,
2136 [P9_TATTACH
] = v9fs_attach
,
2137 [P9_TSTAT
] = v9fs_stat
,
2138 [P9_TWALK
] = v9fs_walk
,
2139 [P9_TCLUNK
] = v9fs_clunk
,
2140 [P9_TOPEN
] = v9fs_open
,
2141 [P9_TREAD
] = v9fs_read
,
2143 [P9_TAUTH
] = v9fs_auth
,
2145 [P9_TFLUSH
] = v9fs_flush
,
2146 [P9_TCREATE
] = v9fs_create
,
2147 [P9_TWRITE
] = v9fs_write
,
2148 [P9_TWSTAT
] = v9fs_wstat
,
2149 [P9_TREMOVE
] = v9fs_remove
,
2152 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
2154 pdu_handler_t
*handler
;
2160 BUG_ON(pdu
->id
>= ARRAY_SIZE(pdu_handlers
));
2162 handler
= pdu_handlers
[pdu
->id
];
2163 BUG_ON(handler
== NULL
);
2168 static void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
2170 V9fsState
*s
= (V9fsState
*)vdev
;
2174 while ((pdu
= alloc_pdu(s
)) &&
2175 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
2178 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
2179 BUG_ON(pdu
->elem
.out_sg
[0].iov_len
< 7);
2181 ptr
= pdu
->elem
.out_sg
[0].iov_base
;
2183 memcpy(&pdu
->size
, ptr
, 4);
2185 memcpy(&pdu
->tag
, ptr
+ 5, 2);
2193 static uint32_t virtio_9p_get_features(VirtIODevice
*vdev
, uint32_t features
)
2195 features
|= 1 << VIRTIO_9P_MOUNT_TAG
;
2199 static V9fsState
*to_virtio_9p(VirtIODevice
*vdev
)
2201 return (V9fsState
*)vdev
;
2204 static void virtio_9p_get_config(VirtIODevice
*vdev
, uint8_t *config
)
2206 struct virtio_9p_config
*cfg
;
2207 V9fsState
*s
= to_virtio_9p(vdev
);
2209 cfg
= qemu_mallocz(sizeof(struct virtio_9p_config
) +
2211 stw_raw(&cfg
->tag_len
, s
->tag_len
);
2212 memcpy(cfg
->tag
, s
->tag
, s
->tag_len
);
2213 memcpy(config
, cfg
, s
->config_size
);
2217 VirtIODevice
*virtio_9p_init(DeviceState
*dev
, V9fsConf
*conf
)
2225 s
= (V9fsState
*)virtio_common_init("virtio-9p",
2227 sizeof(struct virtio_9p_config
)+
2231 /* initialize pdu allocator */
2232 QLIST_INIT(&s
->free_list
);
2233 for (i
= 0; i
< (MAX_REQ
- 1); i
++) {
2234 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
2237 s
->vq
= virtio_add_queue(&s
->vdev
, MAX_REQ
, handle_9p_output
);
2239 fse
= get_fsdev_fsentry(conf
->fsdev_id
);
2242 /* We don't have a fsdev identified by fsdev_id */
2243 fprintf(stderr
, "Virtio-9p device couldn't find fsdev "
2244 "with the id %s\n", conf
->fsdev_id
);
2248 if (!fse
->path
|| !conf
->tag
) {
2249 /* we haven't specified a mount_tag or the path */
2250 fprintf(stderr
, "fsdev with id %s needs path "
2251 "and Virtio-9p device needs mount_tag arguments\n",
2256 if (!strcmp(fse
->security_model
, "passthrough") &&
2257 !strcmp(fse
->security_model
, "mapped")) {
2258 /* user haven't specified a correct security option */
2259 fprintf(stderr
, "one of the following must be specified as the"
2260 "security option:\n\t security_model=passthrough \n\t "
2261 "security_model=mapped\n");
2265 if (lstat(fse
->path
, &stat
)) {
2266 fprintf(stderr
, "share path %s does not exist\n", fse
->path
);
2268 } else if (!S_ISDIR(stat
.st_mode
)) {
2269 fprintf(stderr
, "share path %s is not a directory \n", fse
->path
);
2273 s
->ctx
.fs_root
= qemu_strdup(fse
->path
);
2274 len
= strlen(conf
->tag
);
2275 if (len
> MAX_TAG_LEN
) {
2278 /* s->tag is non-NULL terminated string */
2279 s
->tag
= qemu_malloc(len
);
2280 memcpy(s
->tag
, conf
->tag
, len
);
2285 s
->vdev
.get_features
= virtio_9p_get_features
;
2286 s
->config_size
= sizeof(struct virtio_9p_config
) +
2288 s
->vdev
.get_config
= virtio_9p_get_config
;