]>
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 void cred_init(FsCred
*credp
)
78 static int v9fs_do_lstat(V9fsState
*s
, V9fsString
*path
, struct stat
*stbuf
)
80 return s
->ops
->lstat(&s
->ctx
, path
->data
, stbuf
);
83 static ssize_t
v9fs_do_readlink(V9fsState
*s
, V9fsString
*path
, V9fsString
*buf
)
87 buf
->data
= qemu_malloc(1024);
89 len
= s
->ops
->readlink(&s
->ctx
, path
->data
, buf
->data
, 1024 - 1);
98 static int v9fs_do_close(V9fsState
*s
, int fd
)
100 return s
->ops
->close(&s
->ctx
, fd
);
103 static int v9fs_do_closedir(V9fsState
*s
, DIR *dir
)
105 return s
->ops
->closedir(&s
->ctx
, dir
);
108 static int v9fs_do_open(V9fsState
*s
, V9fsString
*path
, int flags
)
110 return s
->ops
->open(&s
->ctx
, path
->data
, flags
);
113 static DIR *v9fs_do_opendir(V9fsState
*s
, V9fsString
*path
)
115 return s
->ops
->opendir(&s
->ctx
, path
->data
);
118 static void v9fs_do_rewinddir(V9fsState
*s
, DIR *dir
)
120 return s
->ops
->rewinddir(&s
->ctx
, dir
);
123 static off_t
v9fs_do_telldir(V9fsState
*s
, DIR *dir
)
125 return s
->ops
->telldir(&s
->ctx
, dir
);
128 static struct dirent
*v9fs_do_readdir(V9fsState
*s
, DIR *dir
)
130 return s
->ops
->readdir(&s
->ctx
, dir
);
133 static void v9fs_do_seekdir(V9fsState
*s
, DIR *dir
, off_t off
)
135 return s
->ops
->seekdir(&s
->ctx
, dir
, off
);
138 static int v9fs_do_readv(V9fsState
*s
, int fd
, const struct iovec
*iov
,
141 return s
->ops
->readv(&s
->ctx
, fd
, iov
, iovcnt
);
144 static off_t
v9fs_do_lseek(V9fsState
*s
, int fd
, off_t offset
, int whence
)
146 return s
->ops
->lseek(&s
->ctx
, fd
, offset
, whence
);
149 static int v9fs_do_writev(V9fsState
*s
, int fd
, const struct iovec
*iov
,
152 return s
->ops
->writev(&s
->ctx
, fd
, iov
, iovcnt
);
155 static int v9fs_do_chmod(V9fsState
*s
, V9fsString
*path
, mode_t mode
)
157 return s
->ops
->chmod(&s
->ctx
, path
->data
, mode
);
160 static int v9fs_do_mknod(V9fsState
*s
, V9fsString
*path
, mode_t mode
, dev_t dev
)
162 return s
->ops
->mknod(&s
->ctx
, path
->data
, mode
, dev
);
165 static int v9fs_do_mksock(V9fsState
*s
, V9fsString
*path
)
167 return s
->ops
->mksock(&s
->ctx
, path
->data
);
170 static int v9fs_do_mkdir(V9fsState
*s
, V9fsString
*path
, mode_t mode
)
172 return s
->ops
->mkdir(&s
->ctx
, path
->data
, mode
);
175 static int v9fs_do_fstat(V9fsState
*s
, int fd
, struct stat
*stbuf
)
177 return s
->ops
->fstat(&s
->ctx
, fd
, stbuf
);
180 static int v9fs_do_open2(V9fsState
*s
, V9fsString
*path
, int flags
, mode_t mode
)
182 return s
->ops
->open2(&s
->ctx
, path
->data
, flags
, mode
);
185 static int v9fs_do_symlink(V9fsState
*s
, V9fsString
*oldpath
,
188 return s
->ops
->symlink(&s
->ctx
, oldpath
->data
, newpath
->data
);
191 static int v9fs_do_link(V9fsState
*s
, V9fsString
*oldpath
, V9fsString
*newpath
)
193 return s
->ops
->link(&s
->ctx
, oldpath
->data
, newpath
->data
);
196 static int v9fs_do_truncate(V9fsState
*s
, V9fsString
*path
, off_t size
)
198 return s
->ops
->truncate(&s
->ctx
, path
->data
, size
);
201 static int v9fs_do_rename(V9fsState
*s
, V9fsString
*oldpath
,
204 return s
->ops
->rename(&s
->ctx
, oldpath
->data
, newpath
->data
);
207 static int v9fs_do_chown(V9fsState
*s
, V9fsString
*path
, uid_t uid
, gid_t gid
)
209 return s
->ops
->chown(&s
->ctx
, path
->data
, uid
, gid
);
212 static int v9fs_do_utime(V9fsState
*s
, V9fsString
*path
,
213 const struct utimbuf
*buf
)
215 return s
->ops
->utime(&s
->ctx
, path
->data
, buf
);
218 static int v9fs_do_remove(V9fsState
*s
, V9fsString
*path
)
220 return s
->ops
->remove(&s
->ctx
, path
->data
);
223 static int v9fs_do_fsync(V9fsState
*s
, int fd
)
225 return s
->ops
->fsync(&s
->ctx
, fd
);
228 static void v9fs_string_init(V9fsString
*str
)
234 static void v9fs_string_free(V9fsString
*str
)
236 qemu_free(str
->data
);
241 static void v9fs_string_null(V9fsString
*str
)
243 v9fs_string_free(str
);
246 static int number_to_string(void *arg
, char type
)
248 unsigned int ret
= 0;
252 unsigned int num
= *(unsigned int *)arg
;
261 printf("Number_to_string: Unknown number format\n");
268 static int v9fs_string_alloc_printf(char **strp
, const char *fmt
, va_list ap
)
271 char *iter
= (char *)fmt
;
275 unsigned int arg_uint
;
277 /* Find the number of %'s that denotes an argument */
278 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
283 len
= strlen(fmt
) - 2*nr_args
;
293 /* Now parse the format string */
294 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
298 arg_uint
= va_arg(ap2
, unsigned int);
299 len
+= number_to_string((void *)&arg_uint
, 'u');
302 arg_char_ptr
= va_arg(ap2
, char *);
303 len
+= strlen(arg_char_ptr
);
310 "v9fs_string_alloc_printf:Incorrect format %c", *iter
);
317 *strp
= qemu_malloc((len
+ 1) * sizeof(**strp
));
319 return vsprintf(*strp
, fmt
, ap
);
322 static void v9fs_string_sprintf(V9fsString
*str
, const char *fmt
, ...)
327 v9fs_string_free(str
);
330 err
= v9fs_string_alloc_printf(&str
->data
, fmt
, ap
);
337 static void v9fs_string_copy(V9fsString
*lhs
, V9fsString
*rhs
)
339 v9fs_string_free(lhs
);
340 v9fs_string_sprintf(lhs
, "%s", rhs
->data
);
343 static size_t v9fs_string_size(V9fsString
*str
)
348 static V9fsFidState
*lookup_fid(V9fsState
*s
, int32_t fid
)
352 for (f
= s
->fid_list
; f
; f
= f
->next
) {
361 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
365 f
= lookup_fid(s
, fid
);
370 f
= qemu_mallocz(sizeof(V9fsFidState
));
376 f
->next
= s
->fid_list
;
382 static int free_fid(V9fsState
*s
, int32_t fid
)
384 V9fsFidState
**fidpp
, *fidp
;
386 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
387 if ((*fidpp
)->fid
== fid
) {
392 if (*fidpp
== NULL
) {
399 if (fidp
->fd
!= -1) {
400 v9fs_do_close(s
, fidp
->fd
);
403 v9fs_do_closedir(s
, fidp
->dir
);
405 v9fs_string_free(&fidp
->path
);
411 #define P9_QID_TYPE_DIR 0x80
412 #define P9_QID_TYPE_SYMLINK 0x02
414 #define P9_STAT_MODE_DIR 0x80000000
415 #define P9_STAT_MODE_APPEND 0x40000000
416 #define P9_STAT_MODE_EXCL 0x20000000
417 #define P9_STAT_MODE_MOUNT 0x10000000
418 #define P9_STAT_MODE_AUTH 0x08000000
419 #define P9_STAT_MODE_TMP 0x04000000
420 #define P9_STAT_MODE_SYMLINK 0x02000000
421 #define P9_STAT_MODE_LINK 0x01000000
422 #define P9_STAT_MODE_DEVICE 0x00800000
423 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
424 #define P9_STAT_MODE_SOCKET 0x00100000
425 #define P9_STAT_MODE_SETUID 0x00080000
426 #define P9_STAT_MODE_SETGID 0x00040000
427 #define P9_STAT_MODE_SETVTX 0x00010000
429 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
430 P9_STAT_MODE_SYMLINK | \
431 P9_STAT_MODE_LINK | \
432 P9_STAT_MODE_DEVICE | \
433 P9_STAT_MODE_NAMED_PIPE | \
436 /* This is the algorithm from ufs in spfs */
437 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
441 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
442 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
443 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
445 if (S_ISDIR(stbuf
->st_mode
)) {
446 qidp
->type
|= P9_QID_TYPE_DIR
;
448 if (S_ISLNK(stbuf
->st_mode
)) {
449 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
453 static int fid_to_qid(V9fsState
*s
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
458 err
= v9fs_do_lstat(s
, &fidp
->path
, &stbuf
);
463 stat_to_qid(&stbuf
, qidp
);
467 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
471 if (!QLIST_EMPTY(&s
->free_list
)) {
472 pdu
= QLIST_FIRST(&s
->free_list
);
473 QLIST_REMOVE(pdu
, next
);
478 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
481 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
485 size_t pdu_packunpack(void *addr
, struct iovec
*sg
, int sg_count
,
486 size_t offset
, size_t size
, int pack
)
491 for (i
= 0; size
&& i
< sg_count
; i
++) {
493 if (offset
>= sg
[i
].iov_len
) {
495 offset
-= sg
[i
].iov_len
;
498 len
= MIN(sg
[i
].iov_len
- offset
, size
);
500 memcpy(sg
[i
].iov_base
+ offset
, addr
, len
);
502 memcpy(addr
, sg
[i
].iov_base
+ offset
, len
);
517 static size_t pdu_unpack(void *dst
, V9fsPDU
*pdu
, size_t offset
, size_t size
)
519 return pdu_packunpack(dst
, pdu
->elem
.out_sg
, pdu
->elem
.out_num
,
523 static size_t pdu_pack(V9fsPDU
*pdu
, size_t offset
, const void *src
,
526 return pdu_packunpack((void *)src
, pdu
->elem
.in_sg
, pdu
->elem
.in_num
,
530 static int pdu_copy_sg(V9fsPDU
*pdu
, size_t offset
, int rx
, struct iovec
*sg
)
534 struct iovec
*src_sg
;
538 src_sg
= pdu
->elem
.in_sg
;
539 num
= pdu
->elem
.in_num
;
541 src_sg
= pdu
->elem
.out_sg
;
542 num
= pdu
->elem
.out_num
;
546 for (i
= 0; i
< num
; i
++) {
548 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
549 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
551 } else if (offset
< (src_sg
[i
].iov_len
+ pos
)) {
552 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
553 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
554 sg
[j
].iov_base
+= (offset
- pos
);
555 sg
[j
].iov_len
-= (offset
- pos
);
558 pos
+= src_sg
[i
].iov_len
;
564 static size_t pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
566 size_t old_offset
= offset
;
571 for (i
= 0; fmt
[i
]; i
++) {
574 uint8_t *valp
= va_arg(ap
, uint8_t *);
575 offset
+= pdu_unpack(valp
, pdu
, offset
, sizeof(*valp
));
580 valp
= va_arg(ap
, uint16_t *);
581 val
= le16_to_cpupu(valp
);
582 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
588 valp
= va_arg(ap
, uint32_t *);
589 val
= le32_to_cpupu(valp
);
590 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
596 valp
= va_arg(ap
, uint64_t *);
597 val
= le64_to_cpup(valp
);
598 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
603 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
604 int *iovcnt
= va_arg(ap
, int *);
605 *iovcnt
= pdu_copy_sg(pdu
, offset
, 0, iov
);
609 V9fsString
*str
= va_arg(ap
, V9fsString
*);
610 offset
+= pdu_unmarshal(pdu
, offset
, "w", &str
->size
);
611 /* FIXME: sanity check str->size */
612 str
->data
= qemu_malloc(str
->size
+ 1);
613 offset
+= pdu_unpack(str
->data
, pdu
, offset
, str
->size
);
614 str
->data
[str
->size
] = 0;
618 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
619 offset
+= pdu_unmarshal(pdu
, offset
, "bdq",
620 &qidp
->type
, &qidp
->version
, &qidp
->path
);
624 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
625 offset
+= pdu_unmarshal(pdu
, offset
, "wwdQdddqsssssddd",
626 &statp
->size
, &statp
->type
, &statp
->dev
,
627 &statp
->qid
, &statp
->mode
, &statp
->atime
,
628 &statp
->mtime
, &statp
->length
,
629 &statp
->name
, &statp
->uid
, &statp
->gid
,
630 &statp
->muid
, &statp
->extension
,
631 &statp
->n_uid
, &statp
->n_gid
,
642 return offset
- old_offset
;
645 static size_t pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
647 size_t old_offset
= offset
;
652 for (i
= 0; fmt
[i
]; i
++) {
655 uint8_t val
= va_arg(ap
, int);
656 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
661 cpu_to_le16w(&val
, va_arg(ap
, int));
662 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
667 cpu_to_le32w(&val
, va_arg(ap
, uint32_t));
668 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
673 cpu_to_le64w(&val
, va_arg(ap
, uint64_t));
674 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
678 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
679 int *iovcnt
= va_arg(ap
, int *);
680 *iovcnt
= pdu_copy_sg(pdu
, offset
, 1, iov
);
684 V9fsString
*str
= va_arg(ap
, V9fsString
*);
685 offset
+= pdu_marshal(pdu
, offset
, "w", str
->size
);
686 offset
+= pdu_pack(pdu
, offset
, str
->data
, str
->size
);
690 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
691 offset
+= pdu_marshal(pdu
, offset
, "bdq",
692 qidp
->type
, qidp
->version
, qidp
->path
);
696 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
697 offset
+= pdu_marshal(pdu
, offset
, "wwdQdddqsssssddd",
698 statp
->size
, statp
->type
, statp
->dev
,
699 &statp
->qid
, statp
->mode
, statp
->atime
,
700 statp
->mtime
, statp
->length
, &statp
->name
,
701 &statp
->uid
, &statp
->gid
, &statp
->muid
,
702 &statp
->extension
, statp
->n_uid
,
703 statp
->n_gid
, statp
->n_muid
);
712 return offset
- old_offset
;
715 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
717 int8_t id
= pdu
->id
+ 1; /* Response */
723 str
.data
= strerror(err
);
724 str
.size
= strlen(str
.data
);
727 len
+= pdu_marshal(pdu
, len
, "s", &str
);
729 len
+= pdu_marshal(pdu
, len
, "d", err
);
735 /* fill out the header */
736 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
738 /* keep these in sync */
742 /* push onto queue and notify */
743 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
745 /* FIXME: we should batch these completions */
746 virtio_notify(&s
->vdev
, s
->vq
);
751 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
756 if (mode
& P9_STAT_MODE_DIR
) {
761 if (mode
& P9_STAT_MODE_SYMLINK
) {
764 if (mode
& P9_STAT_MODE_SOCKET
) {
767 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
770 if (mode
& P9_STAT_MODE_DEVICE
) {
771 if (extension
&& extension
->data
[0] == 'c') {
783 if (mode
& P9_STAT_MODE_SETUID
) {
786 if (mode
& P9_STAT_MODE_SETGID
) {
789 if (mode
& P9_STAT_MODE_SETVTX
) {
796 static int donttouch_stat(V9fsStat
*stat
)
798 if (stat
->type
== -1 &&
800 stat
->qid
.type
== -1 &&
801 stat
->qid
.version
== -1 &&
802 stat
->qid
.path
== -1 &&
806 stat
->length
== -1 &&
813 stat
->n_muid
== -1) {
820 static void v9fs_stat_free(V9fsStat
*stat
)
822 v9fs_string_free(&stat
->name
);
823 v9fs_string_free(&stat
->uid
);
824 v9fs_string_free(&stat
->gid
);
825 v9fs_string_free(&stat
->muid
);
826 v9fs_string_free(&stat
->extension
);
829 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
833 mode
= stbuf
->st_mode
& 0777;
834 if (S_ISDIR(stbuf
->st_mode
)) {
835 mode
|= P9_STAT_MODE_DIR
;
839 if (S_ISLNK(stbuf
->st_mode
)) {
840 mode
|= P9_STAT_MODE_SYMLINK
;
843 if (S_ISSOCK(stbuf
->st_mode
)) {
844 mode
|= P9_STAT_MODE_SOCKET
;
847 if (S_ISFIFO(stbuf
->st_mode
)) {
848 mode
|= P9_STAT_MODE_NAMED_PIPE
;
851 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
852 mode
|= P9_STAT_MODE_DEVICE
;
855 if (stbuf
->st_mode
& S_ISUID
) {
856 mode
|= P9_STAT_MODE_SETUID
;
859 if (stbuf
->st_mode
& S_ISGID
) {
860 mode
|= P9_STAT_MODE_SETGID
;
863 if (stbuf
->st_mode
& S_ISVTX
) {
864 mode
|= P9_STAT_MODE_SETVTX
;
871 static int stat_to_v9stat(V9fsState
*s
, V9fsString
*name
,
872 const struct stat
*stbuf
,
878 memset(v9stat
, 0, sizeof(*v9stat
));
880 stat_to_qid(stbuf
, &v9stat
->qid
);
881 v9stat
->mode
= stat_to_v9mode(stbuf
);
882 v9stat
->atime
= stbuf
->st_atime
;
883 v9stat
->mtime
= stbuf
->st_mtime
;
884 v9stat
->length
= stbuf
->st_size
;
886 v9fs_string_null(&v9stat
->uid
);
887 v9fs_string_null(&v9stat
->gid
);
888 v9fs_string_null(&v9stat
->muid
);
891 v9stat
->n_uid
= stbuf
->st_uid
;
892 v9stat
->n_gid
= stbuf
->st_gid
;
895 v9fs_string_null(&v9stat
->extension
);
897 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
898 err
= v9fs_do_readlink(s
, name
, &v9stat
->extension
);
903 v9stat
->extension
.data
[err
] = 0;
904 v9stat
->extension
.size
= err
;
905 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
906 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
907 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
908 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
909 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
910 v9fs_string_sprintf(&v9stat
->extension
, "%s %u",
911 "HARDLINKCOUNT", stbuf
->st_nlink
);
915 str
= strrchr(name
->data
, '/');
922 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
925 v9fs_string_size(&v9stat
->name
) +
926 v9fs_string_size(&v9stat
->uid
) +
927 v9fs_string_size(&v9stat
->gid
) +
928 v9fs_string_size(&v9stat
->muid
) +
929 v9fs_string_size(&v9stat
->extension
);
933 static struct iovec
*adjust_sg(struct iovec
*sg
, int len
, int *iovcnt
)
935 while (len
&& *iovcnt
) {
936 if (len
< sg
->iov_len
) {
950 static struct iovec
*cap_sg(struct iovec
*sg
, int cap
, int *cnt
)
955 for (i
= 0; i
< *cnt
; i
++) {
956 if ((total
+ sg
[i
].iov_len
) > cap
) {
957 sg
[i
].iov_len
-= ((total
+ sg
[i
].iov_len
) - cap
);
961 total
+= sg
[i
].iov_len
;
969 static void print_sg(struct iovec
*sg
, int cnt
)
973 printf("sg[%d]: {", cnt
);
974 for (i
= 0; i
< cnt
; i
++) {
978 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
983 static void v9fs_fix_path(V9fsString
*dst
, V9fsString
*src
, int len
)
986 v9fs_string_init(&str
);
987 v9fs_string_copy(&str
, dst
);
988 v9fs_string_sprintf(dst
, "%s%s", src
->data
, str
.data
+len
);
989 v9fs_string_free(&str
);
992 static void v9fs_version(V9fsState
*s
, V9fsPDU
*pdu
)
998 pdu_unmarshal(pdu
, offset
, "ds", &msize
, &version
);
1000 if (strcmp(version
.data
, "9P2000.u")) {
1001 v9fs_string_sprintf(&version
, "unknown");
1004 offset
+= pdu_marshal(pdu
, offset
, "ds", msize
, &version
);
1005 complete_pdu(s
, pdu
, offset
);
1007 v9fs_string_free(&version
);
1010 static void v9fs_attach(V9fsState
*s
, V9fsPDU
*pdu
)
1012 int32_t fid
, afid
, n_uname
;
1013 V9fsString uname
, aname
;
1019 pdu_unmarshal(pdu
, offset
, "ddssd", &fid
, &afid
, &uname
, &aname
, &n_uname
);
1021 fidp
= alloc_fid(s
, fid
);
1027 fidp
->uid
= n_uname
;
1029 v9fs_string_sprintf(&fidp
->path
, "%s", "/");
1030 err
= fid_to_qid(s
, fidp
, &qid
);
1037 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
1041 complete_pdu(s
, pdu
, err
);
1042 v9fs_string_free(&uname
);
1043 v9fs_string_free(&aname
);
1046 static void v9fs_stat_post_lstat(V9fsState
*s
, V9fsStatState
*vs
, int err
)
1053 err
= stat_to_v9stat(s
, &vs
->fidp
->path
, &vs
->stbuf
, &vs
->v9stat
);
1057 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "wS", 0, &vs
->v9stat
);
1061 complete_pdu(s
, vs
->pdu
, err
);
1062 v9fs_stat_free(&vs
->v9stat
);
1066 static void v9fs_stat(V9fsState
*s
, V9fsPDU
*pdu
)
1072 vs
= qemu_malloc(sizeof(*vs
));
1076 memset(&vs
->v9stat
, 0, sizeof(vs
->v9stat
));
1078 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &fid
);
1080 vs
->fidp
= lookup_fid(s
, fid
);
1081 if (vs
->fidp
== NULL
) {
1086 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1087 v9fs_stat_post_lstat(s
, vs
, err
);
1091 complete_pdu(s
, vs
->pdu
, err
);
1092 v9fs_stat_free(&vs
->v9stat
);
1096 static void v9fs_walk_complete(V9fsState
*s
, V9fsWalkState
*vs
, int err
)
1098 complete_pdu(s
, vs
->pdu
, err
);
1101 for (vs
->name_idx
= 0; vs
->name_idx
< vs
->nwnames
; vs
->name_idx
++) {
1102 v9fs_string_free(&vs
->wnames
[vs
->name_idx
]);
1105 qemu_free(vs
->wnames
);
1106 qemu_free(vs
->qids
);
1110 static void v9fs_walk_marshal(V9fsWalkState
*vs
)
1114 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "w", vs
->nwnames
);
1116 for (i
= 0; i
< vs
->nwnames
; i
++) {
1117 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qids
[i
]);
1121 static void v9fs_walk_post_newfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
1125 free_fid(s
, vs
->newfidp
->fid
);
1126 v9fs_string_free(&vs
->path
);
1131 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1134 if (vs
->name_idx
< vs
->nwnames
) {
1135 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1136 vs
->wnames
[vs
->name_idx
].data
);
1137 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1139 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1140 v9fs_walk_post_newfid_lstat(s
, vs
, err
);
1144 v9fs_string_free(&vs
->path
);
1145 v9fs_walk_marshal(vs
);
1148 v9fs_walk_complete(s
, vs
, err
);
1151 static void v9fs_walk_post_oldfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
1155 v9fs_string_free(&vs
->path
);
1160 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1162 if (vs
->name_idx
< vs
->nwnames
) {
1164 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1165 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1166 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1168 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1169 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1173 v9fs_string_free(&vs
->path
);
1174 v9fs_walk_marshal(vs
);
1177 v9fs_walk_complete(s
, vs
, err
);
1180 static void v9fs_walk(V9fsState
*s
, V9fsPDU
*pdu
)
1182 int32_t fid
, newfid
;
1187 vs
= qemu_malloc(sizeof(*vs
));
1193 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "ddw", &fid
,
1194 &newfid
, &vs
->nwnames
);
1197 vs
->wnames
= qemu_mallocz(sizeof(vs
->wnames
[0]) * vs
->nwnames
);
1199 vs
->qids
= qemu_mallocz(sizeof(vs
->qids
[0]) * vs
->nwnames
);
1201 for (i
= 0; i
< vs
->nwnames
; i
++) {
1202 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "s",
1207 vs
->fidp
= lookup_fid(s
, fid
);
1208 if (vs
->fidp
== NULL
) {
1213 /* FIXME: is this really valid? */
1214 if (fid
== newfid
) {
1216 BUG_ON(vs
->fidp
->fd
!= -1);
1217 BUG_ON(vs
->fidp
->dir
);
1218 v9fs_string_init(&vs
->path
);
1221 if (vs
->name_idx
< vs
->nwnames
) {
1222 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1223 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1224 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1226 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1227 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1231 vs
->newfidp
= alloc_fid(s
, newfid
);
1232 if (vs
->newfidp
== NULL
) {
1237 vs
->newfidp
->uid
= vs
->fidp
->uid
;
1238 v9fs_string_init(&vs
->path
);
1240 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->fidp
->path
);
1242 if (vs
->name_idx
< vs
->nwnames
) {
1243 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1244 vs
->wnames
[vs
->name_idx
].data
);
1245 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1247 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1248 v9fs_walk_post_newfid_lstat(s
, vs
, err
);
1253 v9fs_walk_marshal(vs
);
1256 v9fs_walk_complete(s
, vs
, err
);
1259 static void v9fs_open_post_opendir(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1261 if (vs
->fidp
->dir
== NULL
) {
1266 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, 0);
1269 complete_pdu(s
, vs
->pdu
, err
);
1274 static void v9fs_open_post_open(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1276 if (vs
->fidp
->fd
== -1) {
1281 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, 0);
1284 complete_pdu(s
, vs
->pdu
, err
);
1288 static void v9fs_open_post_lstat(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1295 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
1297 if (S_ISDIR(vs
->stbuf
.st_mode
)) {
1298 vs
->fidp
->dir
= v9fs_do_opendir(s
, &vs
->fidp
->path
);
1299 v9fs_open_post_opendir(s
, vs
, err
);
1301 vs
->fidp
->fd
= v9fs_do_open(s
, &vs
->fidp
->path
,
1302 omode_to_uflags(vs
->mode
));
1303 v9fs_open_post_open(s
, vs
, err
);
1307 complete_pdu(s
, vs
->pdu
, err
);
1311 static void v9fs_open(V9fsState
*s
, V9fsPDU
*pdu
)
1318 vs
= qemu_malloc(sizeof(*vs
));
1322 pdu_unmarshal(vs
->pdu
, vs
->offset
, "db", &fid
, &vs
->mode
);
1324 vs
->fidp
= lookup_fid(s
, fid
);
1325 if (vs
->fidp
== NULL
) {
1330 BUG_ON(vs
->fidp
->fd
!= -1);
1331 BUG_ON(vs
->fidp
->dir
);
1333 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1335 v9fs_open_post_lstat(s
, vs
, err
);
1338 complete_pdu(s
, pdu
, err
);
1342 static void v9fs_clunk(V9fsState
*s
, V9fsPDU
*pdu
)
1348 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1350 err
= free_fid(s
, fid
);
1358 complete_pdu(s
, pdu
, err
);
1361 static void v9fs_read_post_readdir(V9fsState
*, V9fsReadState
*, ssize_t
);
1363 static void v9fs_read_post_seekdir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1368 v9fs_stat_free(&vs
->v9stat
);
1369 v9fs_string_free(&vs
->name
);
1370 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
1371 vs
->offset
+= vs
->count
;
1374 complete_pdu(s
, vs
->pdu
, err
);
1379 static void v9fs_read_post_dir_lstat(V9fsState
*s
, V9fsReadState
*vs
,
1386 err
= stat_to_v9stat(s
, &vs
->name
, &vs
->stbuf
, &vs
->v9stat
);
1391 vs
->len
= pdu_marshal(vs
->pdu
, vs
->offset
+ 4 + vs
->count
, "S",
1393 if ((vs
->len
!= (vs
->v9stat
.size
+ 2)) ||
1394 ((vs
->count
+ vs
->len
) > vs
->max_count
)) {
1395 v9fs_do_seekdir(s
, vs
->fidp
->dir
, vs
->dir_pos
);
1396 v9fs_read_post_seekdir(s
, vs
, err
);
1399 vs
->count
+= vs
->len
;
1400 v9fs_stat_free(&vs
->v9stat
);
1401 v9fs_string_free(&vs
->name
);
1402 vs
->dir_pos
= vs
->dent
->d_off
;
1403 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->dir
);
1404 v9fs_read_post_readdir(s
, vs
, err
);
1407 v9fs_do_seekdir(s
, vs
->fidp
->dir
, vs
->dir_pos
);
1408 v9fs_read_post_seekdir(s
, vs
, err
);
1413 static void v9fs_read_post_readdir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1416 memset(&vs
->v9stat
, 0, sizeof(vs
->v9stat
));
1417 v9fs_string_init(&vs
->name
);
1418 v9fs_string_sprintf(&vs
->name
, "%s/%s", vs
->fidp
->path
.data
,
1420 err
= v9fs_do_lstat(s
, &vs
->name
, &vs
->stbuf
);
1421 v9fs_read_post_dir_lstat(s
, vs
, err
);
1425 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
1426 vs
->offset
+= vs
->count
;
1428 complete_pdu(s
, vs
->pdu
, err
);
1433 static void v9fs_read_post_telldir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1435 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->dir
);
1436 v9fs_read_post_readdir(s
, vs
, err
);
1440 static void v9fs_read_post_rewinddir(V9fsState
*s
, V9fsReadState
*vs
,
1443 vs
->dir_pos
= v9fs_do_telldir(s
, vs
->fidp
->dir
);
1444 v9fs_read_post_telldir(s
, vs
, err
);
1448 static void v9fs_read_post_readv(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1451 /* IO error return the error */
1455 vs
->total
+= vs
->len
;
1456 vs
->sg
= adjust_sg(vs
->sg
, vs
->len
, &vs
->cnt
);
1457 if (vs
->total
< vs
->count
&& vs
->len
> 0) {
1460 print_sg(vs
->sg
, vs
->cnt
);
1462 vs
->len
= v9fs_do_readv(s
, vs
->fidp
->fd
, vs
->sg
, vs
->cnt
);
1463 } while (vs
->len
== -1 && errno
== EINTR
);
1464 if (vs
->len
== -1) {
1467 v9fs_read_post_readv(s
, vs
, err
);
1470 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->total
);
1471 vs
->offset
+= vs
->count
;
1475 complete_pdu(s
, vs
->pdu
, err
);
1479 static void v9fs_read_post_lseek(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1485 vs
->sg
= cap_sg(vs
->sg
, vs
->count
, &vs
->cnt
);
1487 if (vs
->total
< vs
->count
) {
1490 print_sg(vs
->sg
, vs
->cnt
);
1492 vs
->len
= v9fs_do_readv(s
, vs
->fidp
->fd
, vs
->sg
, vs
->cnt
);
1493 } while (vs
->len
== -1 && errno
== EINTR
);
1494 if (vs
->len
== -1) {
1497 v9fs_read_post_readv(s
, vs
, err
);
1501 complete_pdu(s
, vs
->pdu
, err
);
1505 static void v9fs_read(V9fsState
*s
, V9fsPDU
*pdu
)
1511 vs
= qemu_malloc(sizeof(*vs
));
1518 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dqd", &fid
, &vs
->off
, &vs
->count
);
1520 vs
->fidp
= lookup_fid(s
, fid
);
1521 if (vs
->fidp
== NULL
) {
1526 if (vs
->fidp
->dir
) {
1527 vs
->max_count
= vs
->count
;
1530 v9fs_do_rewinddir(s
, vs
->fidp
->dir
);
1532 v9fs_read_post_rewinddir(s
, vs
, err
);
1534 } else if (vs
->fidp
->fd
!= -1) {
1536 pdu_marshal(vs
->pdu
, vs
->offset
+ 4, "v", vs
->sg
, &vs
->cnt
);
1537 err
= v9fs_do_lseek(s
, vs
->fidp
->fd
, vs
->off
, SEEK_SET
);
1538 v9fs_read_post_lseek(s
, vs
, err
);
1544 complete_pdu(s
, pdu
, err
);
1548 static void v9fs_write_post_writev(V9fsState
*s
, V9fsWriteState
*vs
,
1552 /* IO error return the error */
1556 vs
->total
+= vs
->len
;
1557 vs
->sg
= adjust_sg(vs
->sg
, vs
->len
, &vs
->cnt
);
1558 if (vs
->total
< vs
->count
&& vs
->len
> 0) {
1561 print_sg(vs
->sg
, vs
->cnt
);
1563 vs
->len
= v9fs_do_writev(s
, vs
->fidp
->fd
, vs
->sg
, vs
->cnt
);
1564 } while (vs
->len
== -1 && errno
== EINTR
);
1565 if (vs
->len
== -1) {
1568 v9fs_write_post_writev(s
, vs
, err
);
1571 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->total
);
1575 complete_pdu(s
, vs
->pdu
, err
);
1579 static void v9fs_write_post_lseek(V9fsState
*s
, V9fsWriteState
*vs
, ssize_t err
)
1585 vs
->sg
= cap_sg(vs
->sg
, vs
->count
, &vs
->cnt
);
1587 if (vs
->total
< vs
->count
) {
1590 print_sg(vs
->sg
, vs
->cnt
);
1592 vs
->len
= v9fs_do_writev(s
, vs
->fidp
->fd
, vs
->sg
, vs
->cnt
);
1593 } while (vs
->len
== -1 && errno
== EINTR
);
1594 if (vs
->len
== -1) {
1597 v9fs_write_post_writev(s
, vs
, err
);
1602 complete_pdu(s
, vs
->pdu
, err
);
1606 static void v9fs_write(V9fsState
*s
, V9fsPDU
*pdu
)
1612 vs
= qemu_malloc(sizeof(*vs
));
1620 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dqdv", &fid
, &vs
->off
, &vs
->count
,
1623 vs
->fidp
= lookup_fid(s
, fid
);
1624 if (vs
->fidp
== NULL
) {
1629 if (vs
->fidp
->fd
== -1) {
1634 err
= v9fs_do_lseek(s
, vs
->fidp
->fd
, vs
->off
, SEEK_SET
);
1636 v9fs_write_post_lseek(s
, vs
, err
);
1640 complete_pdu(s
, vs
->pdu
, err
);
1644 static void v9fs_post_create(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1647 v9fs_string_copy(&vs
->fidp
->path
, &vs
->fullname
);
1648 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
1650 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, 0);
1655 complete_pdu(s
, vs
->pdu
, err
);
1656 v9fs_string_free(&vs
->name
);
1657 v9fs_string_free(&vs
->extension
);
1658 v9fs_string_free(&vs
->fullname
);
1662 static void v9fs_create_post_perms(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1667 v9fs_post_create(s
, vs
, err
);
1670 static void v9fs_create_post_opendir(V9fsState
*s
, V9fsCreateState
*vs
,
1673 if (!vs
->fidp
->dir
) {
1676 v9fs_post_create(s
, vs
, err
);
1679 static void v9fs_create_post_dir_lstat(V9fsState
*s
, V9fsCreateState
*vs
,
1687 vs
->fidp
->dir
= v9fs_do_opendir(s
, &vs
->fullname
);
1688 v9fs_create_post_opendir(s
, vs
, err
);
1692 v9fs_post_create(s
, vs
, err
);
1695 static void v9fs_create_post_mkdir(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1702 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
1703 v9fs_create_post_dir_lstat(s
, vs
, err
);
1707 v9fs_post_create(s
, vs
, err
);
1710 static void v9fs_create_post_mksock(V9fsState
*s
, V9fsCreateState
*vs
,
1718 err
= v9fs_do_chmod(s
, &vs
->fullname
, vs
->perm
& 0777);
1719 v9fs_create_post_perms(s
, vs
, err
);
1723 v9fs_post_create(s
, vs
, err
);
1726 static void v9fs_create_post_fstat(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1733 v9fs_post_create(s
, vs
, err
);
1737 static void v9fs_create_post_open2(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1739 if (vs
->fidp
->fd
== -1) {
1744 err
= v9fs_do_fstat(s
, vs
->fidp
->fd
, &vs
->stbuf
);
1745 v9fs_create_post_fstat(s
, vs
, err
);
1750 v9fs_post_create(s
, vs
, err
);
1754 static void v9fs_create_post_lstat(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1757 if (err
== 0 || errno
!= ENOENT
) {
1762 if (vs
->perm
& P9_STAT_MODE_DIR
) {
1763 err
= v9fs_do_mkdir(s
, &vs
->fullname
, vs
->perm
& 0777);
1764 v9fs_create_post_mkdir(s
, vs
, err
);
1765 } else if (vs
->perm
& P9_STAT_MODE_SYMLINK
) {
1766 err
= v9fs_do_symlink(s
, &vs
->extension
, &vs
->fullname
);
1767 v9fs_create_post_perms(s
, vs
, err
);
1768 } else if (vs
->perm
& P9_STAT_MODE_LINK
) {
1769 int32_t nfid
= atoi(vs
->extension
.data
);
1770 V9fsFidState
*nfidp
= lookup_fid(s
, nfid
);
1771 if (nfidp
== NULL
) {
1773 v9fs_post_create(s
, vs
, err
);
1775 err
= v9fs_do_link(s
, &nfidp
->path
, &vs
->fullname
);
1776 v9fs_create_post_perms(s
, vs
, err
);
1777 } else if (vs
->perm
& P9_STAT_MODE_DEVICE
) {
1779 uint32_t major
, minor
;
1782 if (sscanf(vs
->extension
.data
, "%c %u %u", &ctype
, &major
,
1785 v9fs_post_create(s
, vs
, err
);
1797 v9fs_post_create(s
, vs
, err
);
1800 nmode
|= vs
->perm
& 0777;
1801 err
= v9fs_do_mknod(s
, &vs
->fullname
, nmode
, makedev(major
, minor
));
1802 v9fs_create_post_perms(s
, vs
, err
);
1803 } else if (vs
->perm
& P9_STAT_MODE_NAMED_PIPE
) {
1804 err
= v9fs_do_mknod(s
, &vs
->fullname
, S_IFIFO
| (vs
->mode
& 0777), 0);
1805 v9fs_post_create(s
, vs
, err
);
1806 } else if (vs
->perm
& P9_STAT_MODE_SOCKET
) {
1807 err
= v9fs_do_mksock(s
, &vs
->fullname
);
1808 v9fs_create_post_mksock(s
, vs
, err
);
1810 vs
->fidp
->fd
= v9fs_do_open2(s
, &vs
->fullname
,
1811 omode_to_uflags(vs
->mode
) | O_CREAT
,
1813 v9fs_create_post_open2(s
, vs
, err
);
1819 v9fs_post_create(s
, vs
, err
);
1822 static void v9fs_create(V9fsState
*s
, V9fsPDU
*pdu
)
1825 V9fsCreateState
*vs
;
1828 vs
= qemu_malloc(sizeof(*vs
));
1832 v9fs_string_init(&vs
->fullname
);
1834 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsdbs", &fid
, &vs
->name
,
1835 &vs
->perm
, &vs
->mode
, &vs
->extension
);
1837 vs
->fidp
= lookup_fid(s
, fid
);
1838 if (vs
->fidp
== NULL
) {
1843 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", vs
->fidp
->path
.data
,
1846 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
1847 v9fs_create_post_lstat(s
, vs
, err
);
1851 complete_pdu(s
, vs
->pdu
, err
);
1852 v9fs_string_free(&vs
->name
);
1853 v9fs_string_free(&vs
->extension
);
1857 static void v9fs_flush(V9fsState
*s
, V9fsPDU
*pdu
)
1859 /* A nop call with no return */
1860 complete_pdu(s
, pdu
, 7);
1863 static void v9fs_remove_post_remove(V9fsState
*s
, V9fsRemoveState
*vs
,
1866 /* For TREMOVE we need to clunk the fid even on failed remove */
1867 err
= free_fid(s
, vs
->fidp
->fid
);
1874 complete_pdu(s
, vs
->pdu
, err
);
1878 static void v9fs_remove(V9fsState
*s
, V9fsPDU
*pdu
)
1881 V9fsRemoveState
*vs
;
1884 vs
= qemu_malloc(sizeof(*vs
));
1888 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &fid
);
1890 vs
->fidp
= lookup_fid(s
, fid
);
1891 if (vs
->fidp
== NULL
) {
1896 err
= v9fs_do_remove(s
, &vs
->fidp
->path
);
1897 v9fs_remove_post_remove(s
, vs
, err
);
1901 complete_pdu(s
, pdu
, err
);
1905 static void v9fs_wstat_post_truncate(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
1914 v9fs_stat_free(&vs
->v9stat
);
1915 complete_pdu(s
, vs
->pdu
, err
);
1919 static void v9fs_wstat_post_rename(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
1925 if (vs
->v9stat
.name
.size
!= 0) {
1926 v9fs_string_free(&vs
->nname
);
1929 if (vs
->v9stat
.length
!= -1) {
1930 if (v9fs_do_truncate(s
, &vs
->fidp
->path
, vs
->v9stat
.length
) < 0) {
1934 v9fs_wstat_post_truncate(s
, vs
, err
);
1938 v9fs_stat_free(&vs
->v9stat
);
1939 complete_pdu(s
, vs
->pdu
, err
);
1943 static void v9fs_wstat_post_chown(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
1950 if (vs
->v9stat
.name
.size
!= 0) {
1951 char *old_name
, *new_name
;
1954 old_name
= vs
->fidp
->path
.data
;
1955 end
= strrchr(old_name
, '/');
1962 new_name
= qemu_malloc(end
- old_name
+ vs
->v9stat
.name
.size
+ 1);
1964 memset(new_name
, 0, end
- old_name
+ vs
->v9stat
.name
.size
+ 1);
1965 memcpy(new_name
, old_name
, end
- old_name
);
1966 memcpy(new_name
+ (end
- old_name
), vs
->v9stat
.name
.data
,
1967 vs
->v9stat
.name
.size
);
1968 vs
->nname
.data
= new_name
;
1969 vs
->nname
.size
= strlen(new_name
);
1971 if (strcmp(new_name
, vs
->fidp
->path
.data
) != 0) {
1972 if (v9fs_do_rename(s
, &vs
->fidp
->path
, &vs
->nname
)) {
1976 * Fixup fid's pointing to the old name to
1977 * start pointing to the new name
1979 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
1981 if (vs
->fidp
== fidp
) {
1983 * we replace name of this fid towards the end
1984 * so that our below strcmp will work
1988 if (!strncmp(vs
->fidp
->path
.data
, fidp
->path
.data
,
1989 strlen(vs
->fidp
->path
.data
))) {
1990 /* replace the name */
1991 v9fs_fix_path(&fidp
->path
, &vs
->nname
,
1992 strlen(vs
->fidp
->path
.data
));
1995 v9fs_string_copy(&vs
->fidp
->path
, &vs
->nname
);
1999 v9fs_wstat_post_rename(s
, vs
, err
);
2003 v9fs_stat_free(&vs
->v9stat
);
2004 complete_pdu(s
, vs
->pdu
, err
);
2008 static void v9fs_wstat_post_utime(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2014 if (vs
->v9stat
.n_gid
!= -1) {
2015 if (v9fs_do_chown(s
, &vs
->fidp
->path
, vs
->v9stat
.n_uid
,
2016 vs
->v9stat
.n_gid
)) {
2020 v9fs_wstat_post_chown(s
, vs
, err
);
2024 v9fs_stat_free(&vs
->v9stat
);
2025 complete_pdu(s
, vs
->pdu
, err
);
2029 static void v9fs_wstat_post_chmod(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2035 if (vs
->v9stat
.mtime
!= -1) {
2038 tb
.modtime
= vs
->v9stat
.mtime
;
2039 if (v9fs_do_utime(s
, &vs
->fidp
->path
, &tb
)) {
2044 v9fs_wstat_post_utime(s
, vs
, err
);
2048 v9fs_stat_free(&vs
->v9stat
);
2049 complete_pdu(s
, vs
->pdu
, err
);
2053 static void v9fs_wstat_post_fsync(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2058 v9fs_stat_free(&vs
->v9stat
);
2059 complete_pdu(s
, vs
->pdu
, err
);
2063 static void v9fs_wstat_post_lstat(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2072 v9_mode
= stat_to_v9mode(&vs
->stbuf
);
2074 if ((vs
->v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2075 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2076 /* Attempting to change the type */
2081 if (v9fs_do_chmod(s
, &vs
->fidp
->path
, v9mode_to_mode(vs
->v9stat
.mode
,
2082 &vs
->v9stat
.extension
))) {
2085 v9fs_wstat_post_chmod(s
, vs
, err
);
2089 v9fs_stat_free(&vs
->v9stat
);
2090 complete_pdu(s
, vs
->pdu
, err
);
2094 static void v9fs_wstat(V9fsState
*s
, V9fsPDU
*pdu
)
2100 vs
= qemu_malloc(sizeof(*vs
));
2104 pdu_unmarshal(pdu
, vs
->offset
, "dwS", &fid
, &vs
->unused
, &vs
->v9stat
);
2106 vs
->fidp
= lookup_fid(s
, fid
);
2107 if (vs
->fidp
== NULL
) {
2112 /* do we need to sync the file? */
2113 if (donttouch_stat(&vs
->v9stat
)) {
2114 err
= v9fs_do_fsync(s
, vs
->fidp
->fd
);
2115 v9fs_wstat_post_fsync(s
, vs
, err
);
2119 if (vs
->v9stat
.mode
!= -1) {
2120 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
2121 v9fs_wstat_post_lstat(s
, vs
, err
);
2125 v9fs_wstat_post_chmod(s
, vs
, err
);
2129 v9fs_stat_free(&vs
->v9stat
);
2130 complete_pdu(s
, vs
->pdu
, err
);
2134 typedef void (pdu_handler_t
)(V9fsState
*s
, V9fsPDU
*pdu
);
2136 static pdu_handler_t
*pdu_handlers
[] = {
2137 [P9_TVERSION
] = v9fs_version
,
2138 [P9_TATTACH
] = v9fs_attach
,
2139 [P9_TSTAT
] = v9fs_stat
,
2140 [P9_TWALK
] = v9fs_walk
,
2141 [P9_TCLUNK
] = v9fs_clunk
,
2142 [P9_TOPEN
] = v9fs_open
,
2143 [P9_TREAD
] = v9fs_read
,
2145 [P9_TAUTH
] = v9fs_auth
,
2147 [P9_TFLUSH
] = v9fs_flush
,
2148 [P9_TCREATE
] = v9fs_create
,
2149 [P9_TWRITE
] = v9fs_write
,
2150 [P9_TWSTAT
] = v9fs_wstat
,
2151 [P9_TREMOVE
] = v9fs_remove
,
2154 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
2156 pdu_handler_t
*handler
;
2162 BUG_ON(pdu
->id
>= ARRAY_SIZE(pdu_handlers
));
2164 handler
= pdu_handlers
[pdu
->id
];
2165 BUG_ON(handler
== NULL
);
2170 static void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
2172 V9fsState
*s
= (V9fsState
*)vdev
;
2176 while ((pdu
= alloc_pdu(s
)) &&
2177 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
2180 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
2181 BUG_ON(pdu
->elem
.out_sg
[0].iov_len
< 7);
2183 ptr
= pdu
->elem
.out_sg
[0].iov_base
;
2185 memcpy(&pdu
->size
, ptr
, 4);
2187 memcpy(&pdu
->tag
, ptr
+ 5, 2);
2195 static uint32_t virtio_9p_get_features(VirtIODevice
*vdev
, uint32_t features
)
2197 features
|= 1 << VIRTIO_9P_MOUNT_TAG
;
2201 static V9fsState
*to_virtio_9p(VirtIODevice
*vdev
)
2203 return (V9fsState
*)vdev
;
2206 static void virtio_9p_get_config(VirtIODevice
*vdev
, uint8_t *config
)
2208 struct virtio_9p_config
*cfg
;
2209 V9fsState
*s
= to_virtio_9p(vdev
);
2211 cfg
= qemu_mallocz(sizeof(struct virtio_9p_config
) +
2213 stw_raw(&cfg
->tag_len
, s
->tag_len
);
2214 memcpy(cfg
->tag
, s
->tag
, s
->tag_len
);
2215 memcpy(config
, cfg
, s
->config_size
);
2219 VirtIODevice
*virtio_9p_init(DeviceState
*dev
, V9fsConf
*conf
)
2227 s
= (V9fsState
*)virtio_common_init("virtio-9p",
2229 sizeof(struct virtio_9p_config
)+
2233 /* initialize pdu allocator */
2234 QLIST_INIT(&s
->free_list
);
2235 for (i
= 0; i
< (MAX_REQ
- 1); i
++) {
2236 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
2239 s
->vq
= virtio_add_queue(&s
->vdev
, MAX_REQ
, handle_9p_output
);
2241 fse
= get_fsdev_fsentry(conf
->fsdev_id
);
2244 /* We don't have a fsdev identified by fsdev_id */
2245 fprintf(stderr
, "Virtio-9p device couldn't find fsdev "
2246 "with the id %s\n", conf
->fsdev_id
);
2250 if (!fse
->path
|| !conf
->tag
) {
2251 /* we haven't specified a mount_tag or the path */
2252 fprintf(stderr
, "fsdev with id %s needs path "
2253 "and Virtio-9p device needs mount_tag arguments\n",
2258 if (!strcmp(fse
->security_model
, "passthrough")) {
2259 /* Files on the Fileserver set to client user credentials */
2260 s
->ctx
.fs_sm
= SM_PASSTHROUGH
;
2261 } else if (!strcmp(fse
->security_model
, "mapped")) {
2262 /* Files on the fileserver are set to QEMU credentials.
2263 * Client user credentials are saved in extended attributes.
2265 s
->ctx
.fs_sm
= SM_MAPPED
;
2267 /* user haven't specified a correct security option */
2268 fprintf(stderr
, "one of the following must be specified as the"
2269 "security option:\n\t security_model=passthrough \n\t "
2270 "security_model=mapped\n");
2274 if (lstat(fse
->path
, &stat
)) {
2275 fprintf(stderr
, "share path %s does not exist\n", fse
->path
);
2277 } else if (!S_ISDIR(stat
.st_mode
)) {
2278 fprintf(stderr
, "share path %s is not a directory \n", fse
->path
);
2282 s
->ctx
.fs_root
= qemu_strdup(fse
->path
);
2283 len
= strlen(conf
->tag
);
2284 if (len
> MAX_TAG_LEN
) {
2287 /* s->tag is non-NULL terminated string */
2288 s
->tag
= qemu_malloc(len
);
2289 memcpy(s
->tag
, conf
->tag
, len
);
2294 s
->vdev
.get_features
= virtio_9p_get_features
;
2295 s
->config_size
= sizeof(struct virtio_9p_config
) +
2297 s
->vdev
.get_config
= virtio_9p_get_config
;