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"
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
)
38 buf
->data
= qemu_malloc(1024);
40 len
= s
->ops
->readlink(&s
->ctx
, path
->data
, buf
->data
, 1024 - 1);
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
,
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
,
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
,
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
,
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_fsync(V9fsState
*s
, int fd
)
171 return s
->ops
->fsync(&s
->ctx
, fd
);
174 static void v9fs_string_init(V9fsString
*str
)
180 static void v9fs_string_free(V9fsString
*str
)
182 qemu_free(str
->data
);
187 static void v9fs_string_null(V9fsString
*str
)
189 v9fs_string_free(str
);
192 static int number_to_string(void *arg
, char type
)
194 unsigned int ret
= 0;
198 unsigned int num
= *(unsigned int *)arg
;
207 printf("Number_to_string: Unknown number format\n");
214 static int v9fs_string_alloc_printf(char **strp
, const char *fmt
, va_list ap
)
217 char *iter
= (char *)fmt
;
221 unsigned int arg_uint
;
223 /* Find the number of %'s that denotes an argument */
224 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
229 len
= strlen(fmt
) - 2*nr_args
;
239 /* Now parse the format string */
240 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
244 arg_uint
= va_arg(ap2
, unsigned int);
245 len
+= number_to_string((void *)&arg_uint
, 'u');
248 arg_char_ptr
= va_arg(ap2
, char *);
249 len
+= strlen(arg_char_ptr
);
256 "v9fs_string_alloc_printf:Incorrect format %c", *iter
);
263 *strp
= qemu_malloc((len
+ 1) * sizeof(**strp
));
265 return vsprintf(*strp
, fmt
, ap
);
268 static void v9fs_string_sprintf(V9fsString
*str
, const char *fmt
, ...)
273 v9fs_string_free(str
);
276 err
= v9fs_string_alloc_printf(&str
->data
, fmt
, ap
);
283 static void v9fs_string_copy(V9fsString
*lhs
, V9fsString
*rhs
)
285 v9fs_string_free(lhs
);
286 v9fs_string_sprintf(lhs
, "%s", rhs
->data
);
289 static size_t v9fs_string_size(V9fsString
*str
)
294 static V9fsFidState
*lookup_fid(V9fsState
*s
, int32_t fid
)
298 for (f
= s
->fid_list
; f
; f
= f
->next
) {
300 v9fs_do_setuid(s
, f
->uid
);
308 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
312 f
= lookup_fid(s
, fid
);
317 f
= qemu_mallocz(sizeof(V9fsFidState
));
323 f
->next
= s
->fid_list
;
329 static int free_fid(V9fsState
*s
, int32_t fid
)
331 V9fsFidState
**fidpp
, *fidp
;
333 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
334 if ((*fidpp
)->fid
== fid
) {
339 if (*fidpp
== NULL
) {
346 if (fidp
->fd
!= -1) {
347 v9fs_do_close(s
, fidp
->fd
);
350 v9fs_do_closedir(s
, fidp
->dir
);
352 v9fs_string_free(&fidp
->path
);
358 #define P9_QID_TYPE_DIR 0x80
359 #define P9_QID_TYPE_SYMLINK 0x02
361 #define P9_STAT_MODE_DIR 0x80000000
362 #define P9_STAT_MODE_APPEND 0x40000000
363 #define P9_STAT_MODE_EXCL 0x20000000
364 #define P9_STAT_MODE_MOUNT 0x10000000
365 #define P9_STAT_MODE_AUTH 0x08000000
366 #define P9_STAT_MODE_TMP 0x04000000
367 #define P9_STAT_MODE_SYMLINK 0x02000000
368 #define P9_STAT_MODE_LINK 0x01000000
369 #define P9_STAT_MODE_DEVICE 0x00800000
370 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
371 #define P9_STAT_MODE_SOCKET 0x00100000
372 #define P9_STAT_MODE_SETUID 0x00080000
373 #define P9_STAT_MODE_SETGID 0x00040000
374 #define P9_STAT_MODE_SETVTX 0x00010000
376 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
377 P9_STAT_MODE_SYMLINK | \
378 P9_STAT_MODE_LINK | \
379 P9_STAT_MODE_DEVICE | \
380 P9_STAT_MODE_NAMED_PIPE | \
383 /* This is the algorithm from ufs in spfs */
384 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
388 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
389 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
390 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
392 if (S_ISDIR(stbuf
->st_mode
)) {
393 qidp
->type
|= P9_QID_TYPE_DIR
;
395 if (S_ISLNK(stbuf
->st_mode
)) {
396 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
400 static int fid_to_qid(V9fsState
*s
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
405 err
= v9fs_do_lstat(s
, &fidp
->path
, &stbuf
);
410 stat_to_qid(&stbuf
, qidp
);
414 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
418 if (!QLIST_EMPTY(&s
->free_list
)) {
419 pdu
= QLIST_FIRST(&s
->free_list
);
420 QLIST_REMOVE(pdu
, next
);
425 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
428 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
432 size_t pdu_packunpack(void *addr
, struct iovec
*sg
, int sg_count
,
433 size_t offset
, size_t size
, int pack
)
438 for (i
= 0; size
&& i
< sg_count
; i
++) {
440 if (offset
>= sg
[i
].iov_len
) {
442 offset
-= sg
[i
].iov_len
;
445 len
= MIN(sg
[i
].iov_len
- offset
, size
);
447 memcpy(sg
[i
].iov_base
+ offset
, addr
, len
);
449 memcpy(addr
, sg
[i
].iov_base
+ offset
, len
);
464 static size_t pdu_unpack(void *dst
, V9fsPDU
*pdu
, size_t offset
, size_t size
)
466 return pdu_packunpack(dst
, pdu
->elem
.out_sg
, pdu
->elem
.out_num
,
470 static size_t pdu_pack(V9fsPDU
*pdu
, size_t offset
, const void *src
,
473 return pdu_packunpack((void *)src
, pdu
->elem
.in_sg
, pdu
->elem
.in_num
,
477 static int pdu_copy_sg(V9fsPDU
*pdu
, size_t offset
, int rx
, struct iovec
*sg
)
481 struct iovec
*src_sg
;
485 src_sg
= pdu
->elem
.in_sg
;
486 num
= pdu
->elem
.in_num
;
488 src_sg
= pdu
->elem
.out_sg
;
489 num
= pdu
->elem
.out_num
;
493 for (i
= 0; i
< num
; i
++) {
495 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
496 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
498 } else if (offset
< (src_sg
[i
].iov_len
+ pos
)) {
499 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
500 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
501 sg
[j
].iov_base
+= (offset
- pos
);
502 sg
[j
].iov_len
-= (offset
- pos
);
505 pos
+= src_sg
[i
].iov_len
;
511 static size_t pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
513 size_t old_offset
= offset
;
518 for (i
= 0; fmt
[i
]; i
++) {
521 uint8_t *valp
= va_arg(ap
, uint8_t *);
522 offset
+= pdu_unpack(valp
, pdu
, offset
, sizeof(*valp
));
527 valp
= va_arg(ap
, uint16_t *);
528 val
= le16_to_cpupu(valp
);
529 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
535 valp
= va_arg(ap
, uint32_t *);
536 val
= le32_to_cpupu(valp
);
537 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
543 valp
= va_arg(ap
, uint64_t *);
544 val
= le64_to_cpup(valp
);
545 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
550 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
551 int *iovcnt
= va_arg(ap
, int *);
552 *iovcnt
= pdu_copy_sg(pdu
, offset
, 0, iov
);
556 V9fsString
*str
= va_arg(ap
, V9fsString
*);
557 offset
+= pdu_unmarshal(pdu
, offset
, "w", &str
->size
);
558 /* FIXME: sanity check str->size */
559 str
->data
= qemu_malloc(str
->size
+ 1);
560 offset
+= pdu_unpack(str
->data
, pdu
, offset
, str
->size
);
561 str
->data
[str
->size
] = 0;
565 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
566 offset
+= pdu_unmarshal(pdu
, offset
, "bdq",
567 &qidp
->type
, &qidp
->version
, &qidp
->path
);
571 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
572 offset
+= pdu_unmarshal(pdu
, offset
, "wwdQdddqsssssddd",
573 &statp
->size
, &statp
->type
, &statp
->dev
,
574 &statp
->qid
, &statp
->mode
, &statp
->atime
,
575 &statp
->mtime
, &statp
->length
,
576 &statp
->name
, &statp
->uid
, &statp
->gid
,
577 &statp
->muid
, &statp
->extension
,
578 &statp
->n_uid
, &statp
->n_gid
,
589 return offset
- old_offset
;
592 static size_t pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
594 size_t old_offset
= offset
;
599 for (i
= 0; fmt
[i
]; i
++) {
602 uint8_t val
= va_arg(ap
, int);
603 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
608 cpu_to_le16w(&val
, va_arg(ap
, int));
609 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
614 cpu_to_le32w(&val
, va_arg(ap
, uint32_t));
615 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
620 cpu_to_le64w(&val
, va_arg(ap
, uint64_t));
621 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
625 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
626 int *iovcnt
= va_arg(ap
, int *);
627 *iovcnt
= pdu_copy_sg(pdu
, offset
, 1, iov
);
631 V9fsString
*str
= va_arg(ap
, V9fsString
*);
632 offset
+= pdu_marshal(pdu
, offset
, "w", str
->size
);
633 offset
+= pdu_pack(pdu
, offset
, str
->data
, str
->size
);
637 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
638 offset
+= pdu_marshal(pdu
, offset
, "bdq",
639 qidp
->type
, qidp
->version
, qidp
->path
);
643 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
644 offset
+= pdu_marshal(pdu
, offset
, "wwdQdddqsssssddd",
645 statp
->size
, statp
->type
, statp
->dev
,
646 &statp
->qid
, statp
->mode
, statp
->atime
,
647 statp
->mtime
, statp
->length
, &statp
->name
,
648 &statp
->uid
, &statp
->gid
, &statp
->muid
,
649 &statp
->extension
, statp
->n_uid
,
650 statp
->n_gid
, statp
->n_muid
);
659 return offset
- old_offset
;
662 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
664 int8_t id
= pdu
->id
+ 1; /* Response */
670 str
.data
= strerror(err
);
671 str
.size
= strlen(str
.data
);
674 len
+= pdu_marshal(pdu
, len
, "s", &str
);
676 len
+= pdu_marshal(pdu
, len
, "d", err
);
682 /* fill out the header */
683 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
685 /* keep these in sync */
689 /* push onto queue and notify */
690 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
692 /* FIXME: we should batch these completions */
693 virtio_notify(&s
->vdev
, s
->vq
);
698 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
703 if (mode
& P9_STAT_MODE_DIR
) {
708 if (mode
& P9_STAT_MODE_SYMLINK
) {
711 if (mode
& P9_STAT_MODE_SOCKET
) {
714 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
717 if (mode
& P9_STAT_MODE_DEVICE
) {
718 if (extension
&& extension
->data
[0] == 'c') {
730 if (mode
& P9_STAT_MODE_SETUID
) {
733 if (mode
& P9_STAT_MODE_SETGID
) {
736 if (mode
& P9_STAT_MODE_SETVTX
) {
743 static int donttouch_stat(V9fsStat
*stat
)
745 if (stat
->type
== -1 &&
747 stat
->qid
.type
== -1 &&
748 stat
->qid
.version
== -1 &&
749 stat
->qid
.path
== -1 &&
753 stat
->length
== -1 &&
760 stat
->n_muid
== -1) {
767 static void v9fs_stat_free(V9fsStat
*stat
)
769 v9fs_string_free(&stat
->name
);
770 v9fs_string_free(&stat
->uid
);
771 v9fs_string_free(&stat
->gid
);
772 v9fs_string_free(&stat
->muid
);
773 v9fs_string_free(&stat
->extension
);
776 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
780 mode
= stbuf
->st_mode
& 0777;
781 if (S_ISDIR(stbuf
->st_mode
)) {
782 mode
|= P9_STAT_MODE_DIR
;
786 if (S_ISLNK(stbuf
->st_mode
)) {
787 mode
|= P9_STAT_MODE_SYMLINK
;
790 if (S_ISSOCK(stbuf
->st_mode
)) {
791 mode
|= P9_STAT_MODE_SOCKET
;
794 if (S_ISFIFO(stbuf
->st_mode
)) {
795 mode
|= P9_STAT_MODE_NAMED_PIPE
;
798 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
799 mode
|= P9_STAT_MODE_DEVICE
;
802 if (stbuf
->st_mode
& S_ISUID
) {
803 mode
|= P9_STAT_MODE_SETUID
;
806 if (stbuf
->st_mode
& S_ISGID
) {
807 mode
|= P9_STAT_MODE_SETGID
;
810 if (stbuf
->st_mode
& S_ISVTX
) {
811 mode
|= P9_STAT_MODE_SETVTX
;
818 static int stat_to_v9stat(V9fsState
*s
, V9fsString
*name
,
819 const struct stat
*stbuf
,
825 memset(v9stat
, 0, sizeof(*v9stat
));
827 stat_to_qid(stbuf
, &v9stat
->qid
);
828 v9stat
->mode
= stat_to_v9mode(stbuf
);
829 v9stat
->atime
= stbuf
->st_atime
;
830 v9stat
->mtime
= stbuf
->st_mtime
;
831 v9stat
->length
= stbuf
->st_size
;
833 v9fs_string_null(&v9stat
->uid
);
834 v9fs_string_null(&v9stat
->gid
);
835 v9fs_string_null(&v9stat
->muid
);
838 v9stat
->n_uid
= stbuf
->st_uid
;
839 v9stat
->n_gid
= stbuf
->st_gid
;
842 v9fs_string_null(&v9stat
->extension
);
844 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
845 err
= v9fs_do_readlink(s
, name
, &v9stat
->extension
);
850 v9stat
->extension
.data
[err
] = 0;
851 v9stat
->extension
.size
= err
;
852 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
853 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
854 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
855 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
856 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
857 v9fs_string_sprintf(&v9stat
->extension
, "%s %u",
858 "HARDLINKCOUNT", stbuf
->st_nlink
);
862 str
= strrchr(name
->data
, '/');
869 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
872 v9fs_string_size(&v9stat
->name
) +
873 v9fs_string_size(&v9stat
->uid
) +
874 v9fs_string_size(&v9stat
->gid
) +
875 v9fs_string_size(&v9stat
->muid
) +
876 v9fs_string_size(&v9stat
->extension
);
880 static struct iovec
*adjust_sg(struct iovec
*sg
, int len
, int *iovcnt
)
882 while (len
&& *iovcnt
) {
883 if (len
< sg
->iov_len
) {
897 static struct iovec
*cap_sg(struct iovec
*sg
, int cap
, int *cnt
)
902 for (i
= 0; i
< *cnt
; i
++) {
903 if ((total
+ sg
[i
].iov_len
) > cap
) {
904 sg
[i
].iov_len
-= ((total
+ sg
[i
].iov_len
) - cap
);
908 total
+= sg
[i
].iov_len
;
916 static void print_sg(struct iovec
*sg
, int cnt
)
920 printf("sg[%d]: {", cnt
);
921 for (i
= 0; i
< cnt
; i
++) {
925 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
930 static void v9fs_dummy(V9fsState
*s
, V9fsPDU
*pdu
)
932 /* Note: The following have been added to prevent GCC from complaining
933 * They will be removed in the subsequent patches */
936 (void) v9fs_string_init
;
937 (void) v9fs_string_free
;
938 (void) v9fs_string_null
;
939 (void) v9fs_string_sprintf
;
940 (void) v9fs_string_copy
;
941 (void) v9fs_string_size
;
942 (void) v9fs_do_lstat
;
943 (void) v9fs_do_setuid
;
944 (void) v9fs_do_readlink
;
945 (void) v9fs_do_close
;
946 (void) v9fs_do_closedir
;
950 (void) v9mode_to_mode
;
951 (void) donttouch_stat
;
952 (void) v9fs_stat_free
;
953 (void) stat_to_v9stat
;
959 static void v9fs_fix_path(V9fsString
*dst
, V9fsString
*src
, int len
)
962 v9fs_string_init(&str
);
963 v9fs_string_copy(&str
, dst
);
964 v9fs_string_sprintf(dst
, "%s%s", src
->data
, str
.data
+len
);
965 v9fs_string_free(&str
);
968 static void v9fs_version(V9fsState
*s
, V9fsPDU
*pdu
)
974 pdu_unmarshal(pdu
, offset
, "ds", &msize
, &version
);
976 if (strcmp(version
.data
, "9P2000.u")) {
977 v9fs_string_sprintf(&version
, "unknown");
980 offset
+= pdu_marshal(pdu
, offset
, "ds", msize
, &version
);
981 complete_pdu(s
, pdu
, offset
);
983 v9fs_string_free(&version
);
986 static void v9fs_attach(V9fsState
*s
, V9fsPDU
*pdu
)
988 int32_t fid
, afid
, n_uname
;
989 V9fsString uname
, aname
;
995 pdu_unmarshal(pdu
, offset
, "ddssd", &fid
, &afid
, &uname
, &aname
, &n_uname
);
997 fidp
= alloc_fid(s
, fid
);
1003 fidp
->uid
= n_uname
;
1005 v9fs_string_sprintf(&fidp
->path
, "%s", "/");
1006 err
= fid_to_qid(s
, fidp
, &qid
);
1013 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
1017 complete_pdu(s
, pdu
, err
);
1018 v9fs_string_free(&uname
);
1019 v9fs_string_free(&aname
);
1022 typedef struct V9fsStatState
{
1030 static void v9fs_stat_post_lstat(V9fsState
*s
, V9fsStatState
*vs
, int err
)
1037 err
= stat_to_v9stat(s
, &vs
->fidp
->path
, &vs
->stbuf
, &vs
->v9stat
);
1041 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "wS", 0, &vs
->v9stat
);
1045 complete_pdu(s
, vs
->pdu
, err
);
1046 v9fs_stat_free(&vs
->v9stat
);
1050 static void v9fs_stat(V9fsState
*s
, V9fsPDU
*pdu
)
1056 vs
= qemu_malloc(sizeof(*vs
));
1060 memset(&vs
->v9stat
, 0, sizeof(vs
->v9stat
));
1062 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &fid
);
1064 vs
->fidp
= lookup_fid(s
, fid
);
1065 if (vs
->fidp
== NULL
) {
1070 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1071 v9fs_stat_post_lstat(s
, vs
, err
);
1075 complete_pdu(s
, vs
->pdu
, err
);
1076 v9fs_stat_free(&vs
->v9stat
);
1080 typedef struct V9fsWalkState
{
1087 V9fsFidState
*newfidp
;
1093 static void v9fs_walk_complete(V9fsState
*s
, V9fsWalkState
*vs
, int err
)
1095 complete_pdu(s
, vs
->pdu
, err
);
1098 for (vs
->name_idx
= 0; vs
->name_idx
< vs
->nwnames
; vs
->name_idx
++) {
1099 v9fs_string_free(&vs
->wnames
[vs
->name_idx
]);
1102 qemu_free(vs
->wnames
);
1103 qemu_free(vs
->qids
);
1107 static void v9fs_walk_marshal(V9fsWalkState
*vs
)
1111 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "w", vs
->nwnames
);
1113 for (i
= 0; i
< vs
->nwnames
; i
++) {
1114 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qids
[i
]);
1118 static void v9fs_walk_post_newfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
1122 free_fid(s
, vs
->newfidp
->fid
);
1123 v9fs_string_free(&vs
->path
);
1128 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1131 if (vs
->name_idx
< vs
->nwnames
) {
1132 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1133 vs
->wnames
[vs
->name_idx
].data
);
1134 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1136 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1137 v9fs_walk_post_newfid_lstat(s
, vs
, err
);
1141 v9fs_string_free(&vs
->path
);
1142 v9fs_walk_marshal(vs
);
1145 v9fs_walk_complete(s
, vs
, err
);
1148 static void v9fs_walk_post_oldfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
1152 v9fs_string_free(&vs
->path
);
1157 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1159 if (vs
->name_idx
< vs
->nwnames
) {
1161 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1162 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1163 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1165 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1166 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1170 v9fs_string_free(&vs
->path
);
1171 v9fs_walk_marshal(vs
);
1174 v9fs_walk_complete(s
, vs
, err
);
1177 static void v9fs_walk(V9fsState
*s
, V9fsPDU
*pdu
)
1179 int32_t fid
, newfid
;
1184 vs
= qemu_malloc(sizeof(*vs
));
1190 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "ddw", &fid
,
1191 &newfid
, &vs
->nwnames
);
1194 vs
->wnames
= qemu_mallocz(sizeof(vs
->wnames
[0]) * vs
->nwnames
);
1196 vs
->qids
= qemu_mallocz(sizeof(vs
->qids
[0]) * vs
->nwnames
);
1198 for (i
= 0; i
< vs
->nwnames
; i
++) {
1199 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "s",
1204 vs
->fidp
= lookup_fid(s
, fid
);
1205 if (vs
->fidp
== NULL
) {
1210 /* FIXME: is this really valid? */
1211 if (fid
== newfid
) {
1213 BUG_ON(vs
->fidp
->fd
!= -1);
1214 BUG_ON(vs
->fidp
->dir
);
1215 v9fs_string_init(&vs
->path
);
1218 if (vs
->name_idx
< vs
->nwnames
) {
1219 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1220 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1221 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1223 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1224 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1228 vs
->newfidp
= alloc_fid(s
, newfid
);
1229 if (vs
->newfidp
== NULL
) {
1234 vs
->newfidp
->uid
= vs
->fidp
->uid
;
1235 v9fs_string_init(&vs
->path
);
1237 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->fidp
->path
);
1239 if (vs
->name_idx
< vs
->nwnames
) {
1240 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1241 vs
->wnames
[vs
->name_idx
].data
);
1242 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1244 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1245 v9fs_walk_post_newfid_lstat(s
, vs
, err
);
1250 v9fs_walk_marshal(vs
);
1253 v9fs_walk_complete(s
, vs
, err
);
1256 typedef struct V9fsOpenState
{
1278 static int omode_to_uflags(int8_t mode
)
1297 if (mode
& Otrunc
) {
1301 if (mode
& Oappend
) {
1312 static void v9fs_open_post_opendir(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1314 if (vs
->fidp
->dir
== NULL
) {
1319 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, 0);
1322 complete_pdu(s
, vs
->pdu
, err
);
1327 static void v9fs_open_post_open(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1329 if (vs
->fidp
->fd
== -1) {
1334 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, 0);
1337 complete_pdu(s
, vs
->pdu
, err
);
1341 static void v9fs_open_post_lstat(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1348 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
1350 if (S_ISDIR(vs
->stbuf
.st_mode
)) {
1351 vs
->fidp
->dir
= v9fs_do_opendir(s
, &vs
->fidp
->path
);
1352 v9fs_open_post_opendir(s
, vs
, err
);
1354 vs
->fidp
->fd
= v9fs_do_open(s
, &vs
->fidp
->path
,
1355 omode_to_uflags(vs
->mode
));
1356 v9fs_open_post_open(s
, vs
, err
);
1360 complete_pdu(s
, vs
->pdu
, err
);
1364 static void v9fs_open(V9fsState
*s
, V9fsPDU
*pdu
)
1371 vs
= qemu_malloc(sizeof(*vs
));
1375 pdu_unmarshal(vs
->pdu
, vs
->offset
, "db", &fid
, &vs
->mode
);
1377 vs
->fidp
= lookup_fid(s
, fid
);
1378 if (vs
->fidp
== NULL
) {
1383 BUG_ON(vs
->fidp
->fd
!= -1);
1384 BUG_ON(vs
->fidp
->dir
);
1386 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1388 v9fs_open_post_lstat(s
, vs
, err
);
1391 complete_pdu(s
, pdu
, err
);
1395 static void v9fs_clunk(V9fsState
*s
, V9fsPDU
*pdu
)
1401 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1403 err
= free_fid(s
, fid
);
1411 complete_pdu(s
, pdu
, err
);
1414 typedef struct V9fsReadState
{
1421 struct iovec iov
[128]; /* FIXME: bad, bad, bad */
1424 struct dirent
*dent
;
1433 static void v9fs_read_post_readdir(V9fsState
*, V9fsReadState
*, ssize_t
);
1435 static void v9fs_read_post_seekdir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1440 v9fs_stat_free(&vs
->v9stat
);
1441 v9fs_string_free(&vs
->name
);
1442 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
1443 vs
->offset
+= vs
->count
;
1446 complete_pdu(s
, vs
->pdu
, err
);
1451 static void v9fs_read_post_dir_lstat(V9fsState
*s
, V9fsReadState
*vs
,
1458 err
= stat_to_v9stat(s
, &vs
->name
, &vs
->stbuf
, &vs
->v9stat
);
1463 vs
->len
= pdu_marshal(vs
->pdu
, vs
->offset
+ 4 + vs
->count
, "S",
1465 if ((vs
->len
!= (vs
->v9stat
.size
+ 2)) ||
1466 ((vs
->count
+ vs
->len
) > vs
->max_count
)) {
1467 v9fs_do_seekdir(s
, vs
->fidp
->dir
, vs
->dir_pos
);
1468 v9fs_read_post_seekdir(s
, vs
, err
);
1471 vs
->count
+= vs
->len
;
1472 v9fs_stat_free(&vs
->v9stat
);
1473 v9fs_string_free(&vs
->name
);
1474 vs
->dir_pos
= vs
->dent
->d_off
;
1475 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->dir
);
1476 v9fs_read_post_readdir(s
, vs
, err
);
1479 v9fs_do_seekdir(s
, vs
->fidp
->dir
, vs
->dir_pos
);
1480 v9fs_read_post_seekdir(s
, vs
, err
);
1485 static void v9fs_read_post_readdir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1488 memset(&vs
->v9stat
, 0, sizeof(vs
->v9stat
));
1489 v9fs_string_init(&vs
->name
);
1490 v9fs_string_sprintf(&vs
->name
, "%s/%s", vs
->fidp
->path
.data
,
1492 err
= v9fs_do_lstat(s
, &vs
->name
, &vs
->stbuf
);
1493 v9fs_read_post_dir_lstat(s
, vs
, err
);
1497 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
1498 vs
->offset
+= vs
->count
;
1500 complete_pdu(s
, vs
->pdu
, err
);
1505 static void v9fs_read_post_telldir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1507 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->dir
);
1508 v9fs_read_post_readdir(s
, vs
, err
);
1512 static void v9fs_read_post_rewinddir(V9fsState
*s
, V9fsReadState
*vs
,
1515 vs
->dir_pos
= v9fs_do_telldir(s
, vs
->fidp
->dir
);
1516 v9fs_read_post_telldir(s
, vs
, err
);
1520 static void v9fs_read_post_readv(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1523 /* IO error return the error */
1527 vs
->total
+= vs
->len
;
1528 vs
->sg
= adjust_sg(vs
->sg
, vs
->len
, &vs
->cnt
);
1529 if (vs
->total
< vs
->count
&& vs
->len
> 0) {
1532 print_sg(vs
->sg
, vs
->cnt
);
1534 vs
->len
= v9fs_do_readv(s
, vs
->fidp
->fd
, vs
->sg
, vs
->cnt
);
1535 } while (vs
->len
== -1 && errno
== EINTR
);
1536 if (vs
->len
== -1) {
1539 v9fs_read_post_readv(s
, vs
, err
);
1542 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->total
);
1543 vs
->offset
+= vs
->count
;
1547 complete_pdu(s
, vs
->pdu
, err
);
1551 static void v9fs_read_post_lseek(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1557 vs
->sg
= cap_sg(vs
->sg
, vs
->count
, &vs
->cnt
);
1559 if (vs
->total
< vs
->count
) {
1562 print_sg(vs
->sg
, vs
->cnt
);
1564 vs
->len
= v9fs_do_readv(s
, vs
->fidp
->fd
, vs
->sg
, vs
->cnt
);
1565 } while (vs
->len
== -1 && errno
== EINTR
);
1566 if (vs
->len
== -1) {
1569 v9fs_read_post_readv(s
, vs
, err
);
1573 complete_pdu(s
, vs
->pdu
, err
);
1577 static void v9fs_read(V9fsState
*s
, V9fsPDU
*pdu
)
1583 vs
= qemu_malloc(sizeof(*vs
));
1590 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dqd", &fid
, &vs
->off
, &vs
->count
);
1592 vs
->fidp
= lookup_fid(s
, fid
);
1593 if (vs
->fidp
== NULL
) {
1598 if (vs
->fidp
->dir
) {
1599 vs
->max_count
= vs
->count
;
1602 v9fs_do_rewinddir(s
, vs
->fidp
->dir
);
1604 v9fs_read_post_rewinddir(s
, vs
, err
);
1606 } else if (vs
->fidp
->fd
!= -1) {
1608 pdu_marshal(vs
->pdu
, vs
->offset
+ 4, "v", vs
->sg
, &vs
->cnt
);
1609 err
= v9fs_do_lseek(s
, vs
->fidp
->fd
, vs
->off
, SEEK_SET
);
1610 v9fs_read_post_lseek(s
, vs
, err
);
1616 complete_pdu(s
, pdu
, err
);
1620 typedef struct V9fsWriteState
{
1628 struct iovec iov
[128]; /* FIXME: bad, bad, bad */
1633 static void v9fs_write_post_writev(V9fsState
*s
, V9fsWriteState
*vs
,
1637 /* IO error return the error */
1641 vs
->total
+= vs
->len
;
1642 vs
->sg
= adjust_sg(vs
->sg
, vs
->len
, &vs
->cnt
);
1643 if (vs
->total
< vs
->count
&& vs
->len
> 0) {
1646 print_sg(vs
->sg
, vs
->cnt
);
1648 vs
->len
= v9fs_do_writev(s
, vs
->fidp
->fd
, vs
->sg
, vs
->cnt
);
1649 } while (vs
->len
== -1 && errno
== EINTR
);
1650 if (vs
->len
== -1) {
1653 v9fs_write_post_writev(s
, vs
, err
);
1656 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->total
);
1660 complete_pdu(s
, vs
->pdu
, err
);
1664 static void v9fs_write_post_lseek(V9fsState
*s
, V9fsWriteState
*vs
, ssize_t err
)
1670 vs
->sg
= cap_sg(vs
->sg
, vs
->count
, &vs
->cnt
);
1672 if (vs
->total
< vs
->count
) {
1675 print_sg(vs
->sg
, vs
->cnt
);
1677 vs
->len
= v9fs_do_writev(s
, vs
->fidp
->fd
, vs
->sg
, vs
->cnt
);
1678 } while (vs
->len
== -1 && errno
== EINTR
);
1679 if (vs
->len
== -1) {
1682 v9fs_write_post_writev(s
, vs
, err
);
1687 complete_pdu(s
, vs
->pdu
, err
);
1691 static void v9fs_write(V9fsState
*s
, V9fsPDU
*pdu
)
1697 vs
= qemu_malloc(sizeof(*vs
));
1705 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dqdv", &fid
, &vs
->off
, &vs
->count
,
1708 vs
->fidp
= lookup_fid(s
, fid
);
1709 if (vs
->fidp
== NULL
) {
1714 if (vs
->fidp
->fd
== -1) {
1719 err
= v9fs_do_lseek(s
, vs
->fidp
->fd
, vs
->off
, SEEK_SET
);
1721 v9fs_write_post_lseek(s
, vs
, err
);
1725 complete_pdu(s
, vs
->pdu
, err
);
1729 typedef struct V9fsCreateState
{
1738 V9fsString extension
;
1739 V9fsString fullname
;
1742 static void v9fs_post_create(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1745 v9fs_string_copy(&vs
->fidp
->path
, &vs
->fullname
);
1746 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
1748 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, 0);
1753 complete_pdu(s
, vs
->pdu
, err
);
1754 v9fs_string_free(&vs
->name
);
1755 v9fs_string_free(&vs
->extension
);
1756 v9fs_string_free(&vs
->fullname
);
1760 static void v9fs_create_post_perms(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1765 v9fs_post_create(s
, vs
, err
);
1768 static void v9fs_create_post_opendir(V9fsState
*s
, V9fsCreateState
*vs
,
1771 if (!vs
->fidp
->dir
) {
1774 v9fs_post_create(s
, vs
, err
);
1777 static void v9fs_create_post_dir_lstat(V9fsState
*s
, V9fsCreateState
*vs
,
1785 vs
->fidp
->dir
= v9fs_do_opendir(s
, &vs
->fullname
);
1786 v9fs_create_post_opendir(s
, vs
, err
);
1790 v9fs_post_create(s
, vs
, err
);
1793 static void v9fs_create_post_mkdir(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1800 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
1801 v9fs_create_post_dir_lstat(s
, vs
, err
);
1805 v9fs_post_create(s
, vs
, err
);
1808 static void v9fs_create_post_mksock(V9fsState
*s
, V9fsCreateState
*vs
,
1816 err
= v9fs_do_chmod(s
, &vs
->fullname
, vs
->perm
& 0777);
1817 v9fs_create_post_perms(s
, vs
, err
);
1821 v9fs_post_create(s
, vs
, err
);
1824 static void v9fs_create_post_fstat(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1831 v9fs_post_create(s
, vs
, err
);
1835 static void v9fs_create_post_open2(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1837 if (vs
->fidp
->fd
== -1) {
1842 err
= v9fs_do_fstat(s
, vs
->fidp
->fd
, &vs
->stbuf
);
1843 v9fs_create_post_fstat(s
, vs
, err
);
1848 v9fs_post_create(s
, vs
, err
);
1852 static void v9fs_create_post_lstat(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1855 if (err
== 0 || errno
!= ENOENT
) {
1860 if (vs
->perm
& P9_STAT_MODE_DIR
) {
1861 err
= v9fs_do_mkdir(s
, &vs
->fullname
, vs
->perm
& 0777);
1862 v9fs_create_post_mkdir(s
, vs
, err
);
1863 } else if (vs
->perm
& P9_STAT_MODE_SYMLINK
) {
1864 err
= v9fs_do_symlink(s
, &vs
->extension
, &vs
->fullname
);
1865 v9fs_create_post_perms(s
, vs
, err
);
1866 } else if (vs
->perm
& P9_STAT_MODE_LINK
) {
1867 int32_t nfid
= atoi(vs
->extension
.data
);
1868 V9fsFidState
*nfidp
= lookup_fid(s
, nfid
);
1869 if (nfidp
== NULL
) {
1871 v9fs_post_create(s
, vs
, err
);
1873 err
= v9fs_do_link(s
, &nfidp
->path
, &vs
->fullname
);
1874 v9fs_create_post_perms(s
, vs
, err
);
1875 } else if (vs
->perm
& P9_STAT_MODE_DEVICE
) {
1877 uint32_t major
, minor
;
1880 if (sscanf(vs
->extension
.data
, "%c %u %u", &ctype
, &major
,
1883 v9fs_post_create(s
, vs
, err
);
1895 v9fs_post_create(s
, vs
, err
);
1898 nmode
|= vs
->perm
& 0777;
1899 err
= v9fs_do_mknod(s
, &vs
->fullname
, nmode
, makedev(major
, minor
));
1900 v9fs_create_post_perms(s
, vs
, err
);
1901 } else if (vs
->perm
& P9_STAT_MODE_NAMED_PIPE
) {
1902 err
= v9fs_do_mknod(s
, &vs
->fullname
, S_IFIFO
| (vs
->mode
& 0777), 0);
1903 v9fs_post_create(s
, vs
, err
);
1904 } else if (vs
->perm
& P9_STAT_MODE_SOCKET
) {
1905 err
= v9fs_do_mksock(s
, &vs
->fullname
);
1906 v9fs_create_post_mksock(s
, vs
, err
);
1908 vs
->fidp
->fd
= v9fs_do_open2(s
, &vs
->fullname
,
1909 omode_to_uflags(vs
->mode
) | O_CREAT
,
1911 v9fs_create_post_open2(s
, vs
, err
);
1917 v9fs_post_create(s
, vs
, err
);
1920 static void v9fs_create(V9fsState
*s
, V9fsPDU
*pdu
)
1923 V9fsCreateState
*vs
;
1926 vs
= qemu_malloc(sizeof(*vs
));
1930 v9fs_string_init(&vs
->fullname
);
1932 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsdbs", &fid
, &vs
->name
,
1933 &vs
->perm
, &vs
->mode
, &vs
->extension
);
1935 vs
->fidp
= lookup_fid(s
, fid
);
1936 if (vs
->fidp
== NULL
) {
1941 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", vs
->fidp
->path
.data
,
1944 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
1945 v9fs_create_post_lstat(s
, vs
, err
);
1949 complete_pdu(s
, vs
->pdu
, err
);
1950 v9fs_string_free(&vs
->name
);
1951 v9fs_string_free(&vs
->extension
);
1955 static void v9fs_flush(V9fsState
*s
, V9fsPDU
*pdu
)
1963 static void v9fs_remove(V9fsState
*s
, V9fsPDU
*pdu
)
1970 typedef struct V9fsWstatState
1981 static void v9fs_wstat_post_truncate(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
1990 v9fs_stat_free(&vs
->v9stat
);
1991 complete_pdu(s
, vs
->pdu
, err
);
1995 static void v9fs_wstat_post_rename(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2001 if (vs
->v9stat
.name
.size
!= 0) {
2002 v9fs_string_free(&vs
->nname
);
2005 if (vs
->v9stat
.length
!= -1) {
2006 if (v9fs_do_truncate(s
, &vs
->fidp
->path
, vs
->v9stat
.length
) < 0) {
2010 v9fs_wstat_post_truncate(s
, vs
, err
);
2014 v9fs_stat_free(&vs
->v9stat
);
2015 complete_pdu(s
, vs
->pdu
, err
);
2019 static void v9fs_wstat_post_chown(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2026 if (vs
->v9stat
.name
.size
!= 0) {
2027 char *old_name
, *new_name
;
2030 old_name
= vs
->fidp
->path
.data
;
2031 end
= strrchr(old_name
, '/');
2038 new_name
= qemu_malloc(end
- old_name
+ vs
->v9stat
.name
.size
+ 1);
2040 memset(new_name
, 0, end
- old_name
+ vs
->v9stat
.name
.size
+ 1);
2041 memcpy(new_name
, old_name
, end
- old_name
);
2042 memcpy(new_name
+ (end
- old_name
), vs
->v9stat
.name
.data
,
2043 vs
->v9stat
.name
.size
);
2044 vs
->nname
.data
= new_name
;
2045 vs
->nname
.size
= strlen(new_name
);
2047 if (strcmp(new_name
, vs
->fidp
->path
.data
) != 0) {
2048 if (v9fs_do_rename(s
, &vs
->fidp
->path
, &vs
->nname
)) {
2052 * Fixup fid's pointing to the old name to
2053 * start pointing to the new name
2055 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
2057 if (vs
->fidp
== fidp
) {
2059 * we replace name of this fid towards the end
2060 * so that our below strcmp will work
2064 if (!strncmp(vs
->fidp
->path
.data
, fidp
->path
.data
,
2065 strlen(vs
->fidp
->path
.data
))) {
2066 /* replace the name */
2067 v9fs_fix_path(&fidp
->path
, &vs
->nname
,
2068 strlen(vs
->fidp
->path
.data
));
2071 v9fs_string_copy(&vs
->fidp
->path
, &vs
->nname
);
2075 v9fs_wstat_post_rename(s
, vs
, err
);
2079 v9fs_stat_free(&vs
->v9stat
);
2080 complete_pdu(s
, vs
->pdu
, err
);
2084 static void v9fs_wstat_post_utime(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2090 if (vs
->v9stat
.n_gid
!= -1) {
2091 if (v9fs_do_chown(s
, &vs
->fidp
->path
, vs
->v9stat
.n_uid
,
2092 vs
->v9stat
.n_gid
)) {
2096 v9fs_wstat_post_chown(s
, vs
, err
);
2100 v9fs_stat_free(&vs
->v9stat
);
2101 complete_pdu(s
, vs
->pdu
, err
);
2105 static void v9fs_wstat_post_chmod(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2111 if (vs
->v9stat
.mtime
!= -1) {
2114 tb
.modtime
= vs
->v9stat
.mtime
;
2115 if (v9fs_do_utime(s
, &vs
->fidp
->path
, &tb
)) {
2120 v9fs_wstat_post_utime(s
, vs
, err
);
2124 v9fs_stat_free(&vs
->v9stat
);
2125 complete_pdu(s
, vs
->pdu
, err
);
2129 static void v9fs_wstat_post_fsync(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2134 v9fs_stat_free(&vs
->v9stat
);
2135 complete_pdu(s
, vs
->pdu
, err
);
2139 static void v9fs_wstat_post_lstat(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2148 v9_mode
= stat_to_v9mode(&vs
->stbuf
);
2150 if ((vs
->v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2151 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2152 /* Attempting to change the type */
2157 if (v9fs_do_chmod(s
, &vs
->fidp
->path
, v9mode_to_mode(vs
->v9stat
.mode
,
2158 &vs
->v9stat
.extension
))) {
2161 v9fs_wstat_post_chmod(s
, vs
, err
);
2165 v9fs_stat_free(&vs
->v9stat
);
2166 complete_pdu(s
, vs
->pdu
, err
);
2170 static void v9fs_wstat(V9fsState
*s
, V9fsPDU
*pdu
)
2176 vs
= qemu_malloc(sizeof(*vs
));
2180 pdu_unmarshal(pdu
, vs
->offset
, "dwS", &fid
, &vs
->unused
, &vs
->v9stat
);
2182 vs
->fidp
= lookup_fid(s
, fid
);
2183 if (vs
->fidp
== NULL
) {
2188 /* do we need to sync the file? */
2189 if (donttouch_stat(&vs
->v9stat
)) {
2190 err
= v9fs_do_fsync(s
, vs
->fidp
->fd
);
2191 v9fs_wstat_post_fsync(s
, vs
, err
);
2195 if (vs
->v9stat
.mode
!= -1) {
2196 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
2197 v9fs_wstat_post_lstat(s
, vs
, err
);
2201 v9fs_wstat_post_chmod(s
, vs
, err
);
2205 v9fs_stat_free(&vs
->v9stat
);
2206 complete_pdu(s
, vs
->pdu
, err
);
2210 typedef void (pdu_handler_t
)(V9fsState
*s
, V9fsPDU
*pdu
);
2212 static pdu_handler_t
*pdu_handlers
[] = {
2213 [P9_TVERSION
] = v9fs_version
,
2214 [P9_TATTACH
] = v9fs_attach
,
2215 [P9_TSTAT
] = v9fs_stat
,
2216 [P9_TWALK
] = v9fs_walk
,
2217 [P9_TCLUNK
] = v9fs_clunk
,
2218 [P9_TOPEN
] = v9fs_open
,
2219 [P9_TREAD
] = v9fs_read
,
2221 [P9_TAUTH
] = v9fs_auth
,
2223 [P9_TFLUSH
] = v9fs_flush
,
2224 [P9_TCREATE
] = v9fs_create
,
2225 [P9_TWRITE
] = v9fs_write
,
2226 [P9_TWSTAT
] = v9fs_wstat
,
2227 [P9_TREMOVE
] = v9fs_remove
,
2230 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
2232 pdu_handler_t
*handler
;
2238 BUG_ON(pdu
->id
>= ARRAY_SIZE(pdu_handlers
));
2240 handler
= pdu_handlers
[pdu
->id
];
2241 BUG_ON(handler
== NULL
);
2246 static void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
2248 V9fsState
*s
= (V9fsState
*)vdev
;
2252 while ((pdu
= alloc_pdu(s
)) &&
2253 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
2256 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
2257 BUG_ON(pdu
->elem
.out_sg
[0].iov_len
< 7);
2259 ptr
= pdu
->elem
.out_sg
[0].iov_base
;
2261 memcpy(&pdu
->size
, ptr
, 4);
2263 memcpy(&pdu
->tag
, ptr
+ 5, 2);
2271 static uint32_t virtio_9p_get_features(VirtIODevice
*vdev
, uint32_t features
)
2273 features
|= 1 << VIRTIO_9P_MOUNT_TAG
;
2277 static V9fsState
*to_virtio_9p(VirtIODevice
*vdev
)
2279 return (V9fsState
*)vdev
;
2282 static void virtio_9p_get_config(VirtIODevice
*vdev
, uint8_t *config
)
2284 struct virtio_9p_config
*cfg
;
2285 V9fsState
*s
= to_virtio_9p(vdev
);
2287 cfg
= qemu_mallocz(sizeof(struct virtio_9p_config
) +
2289 stw_raw(&cfg
->tag_len
, s
->tag_len
);
2290 memcpy(cfg
->tag
, s
->tag
, s
->tag_len
);
2291 memcpy(config
, cfg
, s
->config_size
);
2295 VirtIODevice
*virtio_9p_init(DeviceState
*dev
, V9fsConf
*conf
)
2303 s
= (V9fsState
*)virtio_common_init("virtio-9p",
2305 sizeof(struct virtio_9p_config
)+
2309 /* initialize pdu allocator */
2310 QLIST_INIT(&s
->free_list
);
2311 for (i
= 0; i
< (MAX_REQ
- 1); i
++) {
2312 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
2315 s
->vq
= virtio_add_queue(&s
->vdev
, MAX_REQ
, handle_9p_output
);
2317 fse
= get_fsdev_fsentry(conf
->fsdev_id
);
2320 /* We don't have a fsdev identified by fsdev_id */
2321 fprintf(stderr
, "Virtio-9p device couldn't find fsdev "
2322 "with the id %s\n", conf
->fsdev_id
);
2326 if (!fse
->path
|| !conf
->tag
) {
2327 /* we haven't specified a mount_tag or the path */
2328 fprintf(stderr
, "fsdev with id %s needs path "
2329 "and Virtio-9p device needs mount_tag arguments\n",
2334 if (lstat(fse
->path
, &stat
)) {
2335 fprintf(stderr
, "share path %s does not exist\n", fse
->path
);
2337 } else if (!S_ISDIR(stat
.st_mode
)) {
2338 fprintf(stderr
, "share path %s is not a directory \n", fse
->path
);
2342 s
->ctx
.fs_root
= qemu_strdup(fse
->path
);
2343 len
= strlen(conf
->tag
);
2344 if (len
> MAX_TAG_LEN
) {
2347 /* s->tag is non-NULL terminated string */
2348 s
->tag
= qemu_malloc(len
);
2349 memcpy(s
->tag
, conf
->tag
, len
);
2354 s
->vdev
.get_features
= virtio_9p_get_features
;
2355 s
->config_size
= sizeof(struct virtio_9p_config
) +
2357 s
->vdev
.get_config
= virtio_9p_get_config
;