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_remove(V9fsState
*s
, V9fsString
*path
)
171 return s
->ops
->remove(&s
->ctx
, path
->data
);
174 static int v9fs_do_fsync(V9fsState
*s
, int fd
)
176 return s
->ops
->fsync(&s
->ctx
, fd
);
179 static void v9fs_string_init(V9fsString
*str
)
185 static void v9fs_string_free(V9fsString
*str
)
187 qemu_free(str
->data
);
192 static void v9fs_string_null(V9fsString
*str
)
194 v9fs_string_free(str
);
197 static int number_to_string(void *arg
, char type
)
199 unsigned int ret
= 0;
203 unsigned int num
= *(unsigned int *)arg
;
212 printf("Number_to_string: Unknown number format\n");
219 static int v9fs_string_alloc_printf(char **strp
, const char *fmt
, va_list ap
)
222 char *iter
= (char *)fmt
;
226 unsigned int arg_uint
;
228 /* Find the number of %'s that denotes an argument */
229 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
234 len
= strlen(fmt
) - 2*nr_args
;
244 /* Now parse the format string */
245 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
249 arg_uint
= va_arg(ap2
, unsigned int);
250 len
+= number_to_string((void *)&arg_uint
, 'u');
253 arg_char_ptr
= va_arg(ap2
, char *);
254 len
+= strlen(arg_char_ptr
);
261 "v9fs_string_alloc_printf:Incorrect format %c", *iter
);
268 *strp
= qemu_malloc((len
+ 1) * sizeof(**strp
));
270 return vsprintf(*strp
, fmt
, ap
);
273 static void v9fs_string_sprintf(V9fsString
*str
, const char *fmt
, ...)
278 v9fs_string_free(str
);
281 err
= v9fs_string_alloc_printf(&str
->data
, fmt
, ap
);
288 static void v9fs_string_copy(V9fsString
*lhs
, V9fsString
*rhs
)
290 v9fs_string_free(lhs
);
291 v9fs_string_sprintf(lhs
, "%s", rhs
->data
);
294 static size_t v9fs_string_size(V9fsString
*str
)
299 static V9fsFidState
*lookup_fid(V9fsState
*s
, int32_t fid
)
303 for (f
= s
->fid_list
; f
; f
= f
->next
) {
305 v9fs_do_setuid(s
, f
->uid
);
313 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
317 f
= lookup_fid(s
, fid
);
322 f
= qemu_mallocz(sizeof(V9fsFidState
));
328 f
->next
= s
->fid_list
;
334 static int free_fid(V9fsState
*s
, int32_t fid
)
336 V9fsFidState
**fidpp
, *fidp
;
338 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
339 if ((*fidpp
)->fid
== fid
) {
344 if (*fidpp
== NULL
) {
351 if (fidp
->fd
!= -1) {
352 v9fs_do_close(s
, fidp
->fd
);
355 v9fs_do_closedir(s
, fidp
->dir
);
357 v9fs_string_free(&fidp
->path
);
363 #define P9_QID_TYPE_DIR 0x80
364 #define P9_QID_TYPE_SYMLINK 0x02
366 #define P9_STAT_MODE_DIR 0x80000000
367 #define P9_STAT_MODE_APPEND 0x40000000
368 #define P9_STAT_MODE_EXCL 0x20000000
369 #define P9_STAT_MODE_MOUNT 0x10000000
370 #define P9_STAT_MODE_AUTH 0x08000000
371 #define P9_STAT_MODE_TMP 0x04000000
372 #define P9_STAT_MODE_SYMLINK 0x02000000
373 #define P9_STAT_MODE_LINK 0x01000000
374 #define P9_STAT_MODE_DEVICE 0x00800000
375 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
376 #define P9_STAT_MODE_SOCKET 0x00100000
377 #define P9_STAT_MODE_SETUID 0x00080000
378 #define P9_STAT_MODE_SETGID 0x00040000
379 #define P9_STAT_MODE_SETVTX 0x00010000
381 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
382 P9_STAT_MODE_SYMLINK | \
383 P9_STAT_MODE_LINK | \
384 P9_STAT_MODE_DEVICE | \
385 P9_STAT_MODE_NAMED_PIPE | \
388 /* This is the algorithm from ufs in spfs */
389 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
393 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
394 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
395 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
397 if (S_ISDIR(stbuf
->st_mode
)) {
398 qidp
->type
|= P9_QID_TYPE_DIR
;
400 if (S_ISLNK(stbuf
->st_mode
)) {
401 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
405 static int fid_to_qid(V9fsState
*s
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
410 err
= v9fs_do_lstat(s
, &fidp
->path
, &stbuf
);
415 stat_to_qid(&stbuf
, qidp
);
419 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
423 if (!QLIST_EMPTY(&s
->free_list
)) {
424 pdu
= QLIST_FIRST(&s
->free_list
);
425 QLIST_REMOVE(pdu
, next
);
430 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
433 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
437 size_t pdu_packunpack(void *addr
, struct iovec
*sg
, int sg_count
,
438 size_t offset
, size_t size
, int pack
)
443 for (i
= 0; size
&& i
< sg_count
; i
++) {
445 if (offset
>= sg
[i
].iov_len
) {
447 offset
-= sg
[i
].iov_len
;
450 len
= MIN(sg
[i
].iov_len
- offset
, size
);
452 memcpy(sg
[i
].iov_base
+ offset
, addr
, len
);
454 memcpy(addr
, sg
[i
].iov_base
+ offset
, len
);
469 static size_t pdu_unpack(void *dst
, V9fsPDU
*pdu
, size_t offset
, size_t size
)
471 return pdu_packunpack(dst
, pdu
->elem
.out_sg
, pdu
->elem
.out_num
,
475 static size_t pdu_pack(V9fsPDU
*pdu
, size_t offset
, const void *src
,
478 return pdu_packunpack((void *)src
, pdu
->elem
.in_sg
, pdu
->elem
.in_num
,
482 static int pdu_copy_sg(V9fsPDU
*pdu
, size_t offset
, int rx
, struct iovec
*sg
)
486 struct iovec
*src_sg
;
490 src_sg
= pdu
->elem
.in_sg
;
491 num
= pdu
->elem
.in_num
;
493 src_sg
= pdu
->elem
.out_sg
;
494 num
= pdu
->elem
.out_num
;
498 for (i
= 0; i
< num
; i
++) {
500 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
501 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
503 } else if (offset
< (src_sg
[i
].iov_len
+ pos
)) {
504 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
505 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
506 sg
[j
].iov_base
+= (offset
- pos
);
507 sg
[j
].iov_len
-= (offset
- pos
);
510 pos
+= src_sg
[i
].iov_len
;
516 static size_t pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
518 size_t old_offset
= offset
;
523 for (i
= 0; fmt
[i
]; i
++) {
526 uint8_t *valp
= va_arg(ap
, uint8_t *);
527 offset
+= pdu_unpack(valp
, pdu
, offset
, sizeof(*valp
));
532 valp
= va_arg(ap
, uint16_t *);
533 val
= le16_to_cpupu(valp
);
534 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
540 valp
= va_arg(ap
, uint32_t *);
541 val
= le32_to_cpupu(valp
);
542 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
548 valp
= va_arg(ap
, uint64_t *);
549 val
= le64_to_cpup(valp
);
550 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
555 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
556 int *iovcnt
= va_arg(ap
, int *);
557 *iovcnt
= pdu_copy_sg(pdu
, offset
, 0, iov
);
561 V9fsString
*str
= va_arg(ap
, V9fsString
*);
562 offset
+= pdu_unmarshal(pdu
, offset
, "w", &str
->size
);
563 /* FIXME: sanity check str->size */
564 str
->data
= qemu_malloc(str
->size
+ 1);
565 offset
+= pdu_unpack(str
->data
, pdu
, offset
, str
->size
);
566 str
->data
[str
->size
] = 0;
570 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
571 offset
+= pdu_unmarshal(pdu
, offset
, "bdq",
572 &qidp
->type
, &qidp
->version
, &qidp
->path
);
576 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
577 offset
+= pdu_unmarshal(pdu
, offset
, "wwdQdddqsssssddd",
578 &statp
->size
, &statp
->type
, &statp
->dev
,
579 &statp
->qid
, &statp
->mode
, &statp
->atime
,
580 &statp
->mtime
, &statp
->length
,
581 &statp
->name
, &statp
->uid
, &statp
->gid
,
582 &statp
->muid
, &statp
->extension
,
583 &statp
->n_uid
, &statp
->n_gid
,
594 return offset
- old_offset
;
597 static size_t pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
599 size_t old_offset
= offset
;
604 for (i
= 0; fmt
[i
]; i
++) {
607 uint8_t val
= va_arg(ap
, int);
608 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
613 cpu_to_le16w(&val
, va_arg(ap
, int));
614 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
619 cpu_to_le32w(&val
, va_arg(ap
, uint32_t));
620 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
625 cpu_to_le64w(&val
, va_arg(ap
, uint64_t));
626 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
630 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
631 int *iovcnt
= va_arg(ap
, int *);
632 *iovcnt
= pdu_copy_sg(pdu
, offset
, 1, iov
);
636 V9fsString
*str
= va_arg(ap
, V9fsString
*);
637 offset
+= pdu_marshal(pdu
, offset
, "w", str
->size
);
638 offset
+= pdu_pack(pdu
, offset
, str
->data
, str
->size
);
642 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
643 offset
+= pdu_marshal(pdu
, offset
, "bdq",
644 qidp
->type
, qidp
->version
, qidp
->path
);
648 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
649 offset
+= pdu_marshal(pdu
, offset
, "wwdQdddqsssssddd",
650 statp
->size
, statp
->type
, statp
->dev
,
651 &statp
->qid
, statp
->mode
, statp
->atime
,
652 statp
->mtime
, statp
->length
, &statp
->name
,
653 &statp
->uid
, &statp
->gid
, &statp
->muid
,
654 &statp
->extension
, statp
->n_uid
,
655 statp
->n_gid
, statp
->n_muid
);
664 return offset
- old_offset
;
667 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
669 int8_t id
= pdu
->id
+ 1; /* Response */
675 str
.data
= strerror(err
);
676 str
.size
= strlen(str
.data
);
679 len
+= pdu_marshal(pdu
, len
, "s", &str
);
681 len
+= pdu_marshal(pdu
, len
, "d", err
);
687 /* fill out the header */
688 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
690 /* keep these in sync */
694 /* push onto queue and notify */
695 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
697 /* FIXME: we should batch these completions */
698 virtio_notify(&s
->vdev
, s
->vq
);
703 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
708 if (mode
& P9_STAT_MODE_DIR
) {
713 if (mode
& P9_STAT_MODE_SYMLINK
) {
716 if (mode
& P9_STAT_MODE_SOCKET
) {
719 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
722 if (mode
& P9_STAT_MODE_DEVICE
) {
723 if (extension
&& extension
->data
[0] == 'c') {
735 if (mode
& P9_STAT_MODE_SETUID
) {
738 if (mode
& P9_STAT_MODE_SETGID
) {
741 if (mode
& P9_STAT_MODE_SETVTX
) {
748 static int donttouch_stat(V9fsStat
*stat
)
750 if (stat
->type
== -1 &&
752 stat
->qid
.type
== -1 &&
753 stat
->qid
.version
== -1 &&
754 stat
->qid
.path
== -1 &&
758 stat
->length
== -1 &&
765 stat
->n_muid
== -1) {
772 static void v9fs_stat_free(V9fsStat
*stat
)
774 v9fs_string_free(&stat
->name
);
775 v9fs_string_free(&stat
->uid
);
776 v9fs_string_free(&stat
->gid
);
777 v9fs_string_free(&stat
->muid
);
778 v9fs_string_free(&stat
->extension
);
781 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
785 mode
= stbuf
->st_mode
& 0777;
786 if (S_ISDIR(stbuf
->st_mode
)) {
787 mode
|= P9_STAT_MODE_DIR
;
791 if (S_ISLNK(stbuf
->st_mode
)) {
792 mode
|= P9_STAT_MODE_SYMLINK
;
795 if (S_ISSOCK(stbuf
->st_mode
)) {
796 mode
|= P9_STAT_MODE_SOCKET
;
799 if (S_ISFIFO(stbuf
->st_mode
)) {
800 mode
|= P9_STAT_MODE_NAMED_PIPE
;
803 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
804 mode
|= P9_STAT_MODE_DEVICE
;
807 if (stbuf
->st_mode
& S_ISUID
) {
808 mode
|= P9_STAT_MODE_SETUID
;
811 if (stbuf
->st_mode
& S_ISGID
) {
812 mode
|= P9_STAT_MODE_SETGID
;
815 if (stbuf
->st_mode
& S_ISVTX
) {
816 mode
|= P9_STAT_MODE_SETVTX
;
823 static int stat_to_v9stat(V9fsState
*s
, V9fsString
*name
,
824 const struct stat
*stbuf
,
830 memset(v9stat
, 0, sizeof(*v9stat
));
832 stat_to_qid(stbuf
, &v9stat
->qid
);
833 v9stat
->mode
= stat_to_v9mode(stbuf
);
834 v9stat
->atime
= stbuf
->st_atime
;
835 v9stat
->mtime
= stbuf
->st_mtime
;
836 v9stat
->length
= stbuf
->st_size
;
838 v9fs_string_null(&v9stat
->uid
);
839 v9fs_string_null(&v9stat
->gid
);
840 v9fs_string_null(&v9stat
->muid
);
843 v9stat
->n_uid
= stbuf
->st_uid
;
844 v9stat
->n_gid
= stbuf
->st_gid
;
847 v9fs_string_null(&v9stat
->extension
);
849 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
850 err
= v9fs_do_readlink(s
, name
, &v9stat
->extension
);
855 v9stat
->extension
.data
[err
] = 0;
856 v9stat
->extension
.size
= err
;
857 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
858 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
859 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
860 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
861 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
862 v9fs_string_sprintf(&v9stat
->extension
, "%s %u",
863 "HARDLINKCOUNT", stbuf
->st_nlink
);
867 str
= strrchr(name
->data
, '/');
874 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
877 v9fs_string_size(&v9stat
->name
) +
878 v9fs_string_size(&v9stat
->uid
) +
879 v9fs_string_size(&v9stat
->gid
) +
880 v9fs_string_size(&v9stat
->muid
) +
881 v9fs_string_size(&v9stat
->extension
);
885 static struct iovec
*adjust_sg(struct iovec
*sg
, int len
, int *iovcnt
)
887 while (len
&& *iovcnt
) {
888 if (len
< sg
->iov_len
) {
902 static struct iovec
*cap_sg(struct iovec
*sg
, int cap
, int *cnt
)
907 for (i
= 0; i
< *cnt
; i
++) {
908 if ((total
+ sg
[i
].iov_len
) > cap
) {
909 sg
[i
].iov_len
-= ((total
+ sg
[i
].iov_len
) - cap
);
913 total
+= sg
[i
].iov_len
;
921 static void print_sg(struct iovec
*sg
, int cnt
)
925 printf("sg[%d]: {", cnt
);
926 for (i
= 0; i
< cnt
; i
++) {
930 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
935 static void v9fs_fix_path(V9fsString
*dst
, V9fsString
*src
, int len
)
938 v9fs_string_init(&str
);
939 v9fs_string_copy(&str
, dst
);
940 v9fs_string_sprintf(dst
, "%s%s", src
->data
, str
.data
+len
);
941 v9fs_string_free(&str
);
944 static void v9fs_version(V9fsState
*s
, V9fsPDU
*pdu
)
950 pdu_unmarshal(pdu
, offset
, "ds", &msize
, &version
);
952 if (strcmp(version
.data
, "9P2000.u")) {
953 v9fs_string_sprintf(&version
, "unknown");
956 offset
+= pdu_marshal(pdu
, offset
, "ds", msize
, &version
);
957 complete_pdu(s
, pdu
, offset
);
959 v9fs_string_free(&version
);
962 static void v9fs_attach(V9fsState
*s
, V9fsPDU
*pdu
)
964 int32_t fid
, afid
, n_uname
;
965 V9fsString uname
, aname
;
971 pdu_unmarshal(pdu
, offset
, "ddssd", &fid
, &afid
, &uname
, &aname
, &n_uname
);
973 fidp
= alloc_fid(s
, fid
);
981 v9fs_string_sprintf(&fidp
->path
, "%s", "/");
982 err
= fid_to_qid(s
, fidp
, &qid
);
989 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
993 complete_pdu(s
, pdu
, err
);
994 v9fs_string_free(&uname
);
995 v9fs_string_free(&aname
);
998 typedef struct V9fsStatState
{
1006 static void v9fs_stat_post_lstat(V9fsState
*s
, V9fsStatState
*vs
, int err
)
1013 err
= stat_to_v9stat(s
, &vs
->fidp
->path
, &vs
->stbuf
, &vs
->v9stat
);
1017 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "wS", 0, &vs
->v9stat
);
1021 complete_pdu(s
, vs
->pdu
, err
);
1022 v9fs_stat_free(&vs
->v9stat
);
1026 static void v9fs_stat(V9fsState
*s
, V9fsPDU
*pdu
)
1032 vs
= qemu_malloc(sizeof(*vs
));
1036 memset(&vs
->v9stat
, 0, sizeof(vs
->v9stat
));
1038 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &fid
);
1040 vs
->fidp
= lookup_fid(s
, fid
);
1041 if (vs
->fidp
== NULL
) {
1046 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1047 v9fs_stat_post_lstat(s
, vs
, err
);
1051 complete_pdu(s
, vs
->pdu
, err
);
1052 v9fs_stat_free(&vs
->v9stat
);
1056 typedef struct V9fsWalkState
{
1063 V9fsFidState
*newfidp
;
1069 static void v9fs_walk_complete(V9fsState
*s
, V9fsWalkState
*vs
, int err
)
1071 complete_pdu(s
, vs
->pdu
, err
);
1074 for (vs
->name_idx
= 0; vs
->name_idx
< vs
->nwnames
; vs
->name_idx
++) {
1075 v9fs_string_free(&vs
->wnames
[vs
->name_idx
]);
1078 qemu_free(vs
->wnames
);
1079 qemu_free(vs
->qids
);
1083 static void v9fs_walk_marshal(V9fsWalkState
*vs
)
1087 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "w", vs
->nwnames
);
1089 for (i
= 0; i
< vs
->nwnames
; i
++) {
1090 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qids
[i
]);
1094 static void v9fs_walk_post_newfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
1098 free_fid(s
, vs
->newfidp
->fid
);
1099 v9fs_string_free(&vs
->path
);
1104 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1107 if (vs
->name_idx
< vs
->nwnames
) {
1108 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1109 vs
->wnames
[vs
->name_idx
].data
);
1110 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1112 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1113 v9fs_walk_post_newfid_lstat(s
, vs
, err
);
1117 v9fs_string_free(&vs
->path
);
1118 v9fs_walk_marshal(vs
);
1121 v9fs_walk_complete(s
, vs
, err
);
1124 static void v9fs_walk_post_oldfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
1128 v9fs_string_free(&vs
->path
);
1133 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1135 if (vs
->name_idx
< vs
->nwnames
) {
1137 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1138 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1139 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1141 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1142 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1146 v9fs_string_free(&vs
->path
);
1147 v9fs_walk_marshal(vs
);
1150 v9fs_walk_complete(s
, vs
, err
);
1153 static void v9fs_walk(V9fsState
*s
, V9fsPDU
*pdu
)
1155 int32_t fid
, newfid
;
1160 vs
= qemu_malloc(sizeof(*vs
));
1166 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "ddw", &fid
,
1167 &newfid
, &vs
->nwnames
);
1170 vs
->wnames
= qemu_mallocz(sizeof(vs
->wnames
[0]) * vs
->nwnames
);
1172 vs
->qids
= qemu_mallocz(sizeof(vs
->qids
[0]) * vs
->nwnames
);
1174 for (i
= 0; i
< vs
->nwnames
; i
++) {
1175 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "s",
1180 vs
->fidp
= lookup_fid(s
, fid
);
1181 if (vs
->fidp
== NULL
) {
1186 /* FIXME: is this really valid? */
1187 if (fid
== newfid
) {
1189 BUG_ON(vs
->fidp
->fd
!= -1);
1190 BUG_ON(vs
->fidp
->dir
);
1191 v9fs_string_init(&vs
->path
);
1194 if (vs
->name_idx
< vs
->nwnames
) {
1195 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1196 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1197 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1199 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1200 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1204 vs
->newfidp
= alloc_fid(s
, newfid
);
1205 if (vs
->newfidp
== NULL
) {
1210 vs
->newfidp
->uid
= vs
->fidp
->uid
;
1211 v9fs_string_init(&vs
->path
);
1213 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->fidp
->path
);
1215 if (vs
->name_idx
< vs
->nwnames
) {
1216 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1217 vs
->wnames
[vs
->name_idx
].data
);
1218 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1220 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1221 v9fs_walk_post_newfid_lstat(s
, vs
, err
);
1226 v9fs_walk_marshal(vs
);
1229 v9fs_walk_complete(s
, vs
, err
);
1232 typedef struct V9fsOpenState
{
1254 static int omode_to_uflags(int8_t mode
)
1273 if (mode
& Otrunc
) {
1277 if (mode
& Oappend
) {
1288 static void v9fs_open_post_opendir(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1290 if (vs
->fidp
->dir
== NULL
) {
1295 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, 0);
1298 complete_pdu(s
, vs
->pdu
, err
);
1303 static void v9fs_open_post_open(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1305 if (vs
->fidp
->fd
== -1) {
1310 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, 0);
1313 complete_pdu(s
, vs
->pdu
, err
);
1317 static void v9fs_open_post_lstat(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1324 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
1326 if (S_ISDIR(vs
->stbuf
.st_mode
)) {
1327 vs
->fidp
->dir
= v9fs_do_opendir(s
, &vs
->fidp
->path
);
1328 v9fs_open_post_opendir(s
, vs
, err
);
1330 vs
->fidp
->fd
= v9fs_do_open(s
, &vs
->fidp
->path
,
1331 omode_to_uflags(vs
->mode
));
1332 v9fs_open_post_open(s
, vs
, err
);
1336 complete_pdu(s
, vs
->pdu
, err
);
1340 static void v9fs_open(V9fsState
*s
, V9fsPDU
*pdu
)
1347 vs
= qemu_malloc(sizeof(*vs
));
1351 pdu_unmarshal(vs
->pdu
, vs
->offset
, "db", &fid
, &vs
->mode
);
1353 vs
->fidp
= lookup_fid(s
, fid
);
1354 if (vs
->fidp
== NULL
) {
1359 BUG_ON(vs
->fidp
->fd
!= -1);
1360 BUG_ON(vs
->fidp
->dir
);
1362 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1364 v9fs_open_post_lstat(s
, vs
, err
);
1367 complete_pdu(s
, pdu
, err
);
1371 static void v9fs_clunk(V9fsState
*s
, V9fsPDU
*pdu
)
1377 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1379 err
= free_fid(s
, fid
);
1387 complete_pdu(s
, pdu
, err
);
1390 typedef struct V9fsReadState
{
1397 struct iovec iov
[128]; /* FIXME: bad, bad, bad */
1400 struct dirent
*dent
;
1409 static void v9fs_read_post_readdir(V9fsState
*, V9fsReadState
*, ssize_t
);
1411 static void v9fs_read_post_seekdir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1416 v9fs_stat_free(&vs
->v9stat
);
1417 v9fs_string_free(&vs
->name
);
1418 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
1419 vs
->offset
+= vs
->count
;
1422 complete_pdu(s
, vs
->pdu
, err
);
1427 static void v9fs_read_post_dir_lstat(V9fsState
*s
, V9fsReadState
*vs
,
1434 err
= stat_to_v9stat(s
, &vs
->name
, &vs
->stbuf
, &vs
->v9stat
);
1439 vs
->len
= pdu_marshal(vs
->pdu
, vs
->offset
+ 4 + vs
->count
, "S",
1441 if ((vs
->len
!= (vs
->v9stat
.size
+ 2)) ||
1442 ((vs
->count
+ vs
->len
) > vs
->max_count
)) {
1443 v9fs_do_seekdir(s
, vs
->fidp
->dir
, vs
->dir_pos
);
1444 v9fs_read_post_seekdir(s
, vs
, err
);
1447 vs
->count
+= vs
->len
;
1448 v9fs_stat_free(&vs
->v9stat
);
1449 v9fs_string_free(&vs
->name
);
1450 vs
->dir_pos
= vs
->dent
->d_off
;
1451 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->dir
);
1452 v9fs_read_post_readdir(s
, vs
, err
);
1455 v9fs_do_seekdir(s
, vs
->fidp
->dir
, vs
->dir_pos
);
1456 v9fs_read_post_seekdir(s
, vs
, err
);
1461 static void v9fs_read_post_readdir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1464 memset(&vs
->v9stat
, 0, sizeof(vs
->v9stat
));
1465 v9fs_string_init(&vs
->name
);
1466 v9fs_string_sprintf(&vs
->name
, "%s/%s", vs
->fidp
->path
.data
,
1468 err
= v9fs_do_lstat(s
, &vs
->name
, &vs
->stbuf
);
1469 v9fs_read_post_dir_lstat(s
, vs
, err
);
1473 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
1474 vs
->offset
+= vs
->count
;
1476 complete_pdu(s
, vs
->pdu
, err
);
1481 static void v9fs_read_post_telldir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1483 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->dir
);
1484 v9fs_read_post_readdir(s
, vs
, err
);
1488 static void v9fs_read_post_rewinddir(V9fsState
*s
, V9fsReadState
*vs
,
1491 vs
->dir_pos
= v9fs_do_telldir(s
, vs
->fidp
->dir
);
1492 v9fs_read_post_telldir(s
, vs
, err
);
1496 static void v9fs_read_post_readv(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1499 /* IO error return the error */
1503 vs
->total
+= vs
->len
;
1504 vs
->sg
= adjust_sg(vs
->sg
, vs
->len
, &vs
->cnt
);
1505 if (vs
->total
< vs
->count
&& vs
->len
> 0) {
1508 print_sg(vs
->sg
, vs
->cnt
);
1510 vs
->len
= v9fs_do_readv(s
, vs
->fidp
->fd
, vs
->sg
, vs
->cnt
);
1511 } while (vs
->len
== -1 && errno
== EINTR
);
1512 if (vs
->len
== -1) {
1515 v9fs_read_post_readv(s
, vs
, err
);
1518 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->total
);
1519 vs
->offset
+= vs
->count
;
1523 complete_pdu(s
, vs
->pdu
, err
);
1527 static void v9fs_read_post_lseek(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1533 vs
->sg
= cap_sg(vs
->sg
, vs
->count
, &vs
->cnt
);
1535 if (vs
->total
< vs
->count
) {
1538 print_sg(vs
->sg
, vs
->cnt
);
1540 vs
->len
= v9fs_do_readv(s
, vs
->fidp
->fd
, vs
->sg
, vs
->cnt
);
1541 } while (vs
->len
== -1 && errno
== EINTR
);
1542 if (vs
->len
== -1) {
1545 v9fs_read_post_readv(s
, vs
, err
);
1549 complete_pdu(s
, vs
->pdu
, err
);
1553 static void v9fs_read(V9fsState
*s
, V9fsPDU
*pdu
)
1559 vs
= qemu_malloc(sizeof(*vs
));
1566 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dqd", &fid
, &vs
->off
, &vs
->count
);
1568 vs
->fidp
= lookup_fid(s
, fid
);
1569 if (vs
->fidp
== NULL
) {
1574 if (vs
->fidp
->dir
) {
1575 vs
->max_count
= vs
->count
;
1578 v9fs_do_rewinddir(s
, vs
->fidp
->dir
);
1580 v9fs_read_post_rewinddir(s
, vs
, err
);
1582 } else if (vs
->fidp
->fd
!= -1) {
1584 pdu_marshal(vs
->pdu
, vs
->offset
+ 4, "v", vs
->sg
, &vs
->cnt
);
1585 err
= v9fs_do_lseek(s
, vs
->fidp
->fd
, vs
->off
, SEEK_SET
);
1586 v9fs_read_post_lseek(s
, vs
, err
);
1592 complete_pdu(s
, pdu
, err
);
1596 typedef struct V9fsWriteState
{
1604 struct iovec iov
[128]; /* FIXME: bad, bad, bad */
1609 static void v9fs_write_post_writev(V9fsState
*s
, V9fsWriteState
*vs
,
1613 /* IO error return the error */
1617 vs
->total
+= vs
->len
;
1618 vs
->sg
= adjust_sg(vs
->sg
, vs
->len
, &vs
->cnt
);
1619 if (vs
->total
< vs
->count
&& vs
->len
> 0) {
1622 print_sg(vs
->sg
, vs
->cnt
);
1624 vs
->len
= v9fs_do_writev(s
, vs
->fidp
->fd
, vs
->sg
, vs
->cnt
);
1625 } while (vs
->len
== -1 && errno
== EINTR
);
1626 if (vs
->len
== -1) {
1629 v9fs_write_post_writev(s
, vs
, err
);
1632 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->total
);
1636 complete_pdu(s
, vs
->pdu
, err
);
1640 static void v9fs_write_post_lseek(V9fsState
*s
, V9fsWriteState
*vs
, ssize_t err
)
1646 vs
->sg
= cap_sg(vs
->sg
, vs
->count
, &vs
->cnt
);
1648 if (vs
->total
< vs
->count
) {
1651 print_sg(vs
->sg
, vs
->cnt
);
1653 vs
->len
= v9fs_do_writev(s
, vs
->fidp
->fd
, vs
->sg
, vs
->cnt
);
1654 } while (vs
->len
== -1 && errno
== EINTR
);
1655 if (vs
->len
== -1) {
1658 v9fs_write_post_writev(s
, vs
, err
);
1663 complete_pdu(s
, vs
->pdu
, err
);
1667 static void v9fs_write(V9fsState
*s
, V9fsPDU
*pdu
)
1673 vs
= qemu_malloc(sizeof(*vs
));
1681 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dqdv", &fid
, &vs
->off
, &vs
->count
,
1684 vs
->fidp
= lookup_fid(s
, fid
);
1685 if (vs
->fidp
== NULL
) {
1690 if (vs
->fidp
->fd
== -1) {
1695 err
= v9fs_do_lseek(s
, vs
->fidp
->fd
, vs
->off
, SEEK_SET
);
1697 v9fs_write_post_lseek(s
, vs
, err
);
1701 complete_pdu(s
, vs
->pdu
, err
);
1705 typedef struct V9fsCreateState
{
1714 V9fsString extension
;
1715 V9fsString fullname
;
1718 static void v9fs_post_create(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1721 v9fs_string_copy(&vs
->fidp
->path
, &vs
->fullname
);
1722 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
1724 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, 0);
1729 complete_pdu(s
, vs
->pdu
, err
);
1730 v9fs_string_free(&vs
->name
);
1731 v9fs_string_free(&vs
->extension
);
1732 v9fs_string_free(&vs
->fullname
);
1736 static void v9fs_create_post_perms(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1741 v9fs_post_create(s
, vs
, err
);
1744 static void v9fs_create_post_opendir(V9fsState
*s
, V9fsCreateState
*vs
,
1747 if (!vs
->fidp
->dir
) {
1750 v9fs_post_create(s
, vs
, err
);
1753 static void v9fs_create_post_dir_lstat(V9fsState
*s
, V9fsCreateState
*vs
,
1761 vs
->fidp
->dir
= v9fs_do_opendir(s
, &vs
->fullname
);
1762 v9fs_create_post_opendir(s
, vs
, err
);
1766 v9fs_post_create(s
, vs
, err
);
1769 static void v9fs_create_post_mkdir(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1776 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
1777 v9fs_create_post_dir_lstat(s
, vs
, err
);
1781 v9fs_post_create(s
, vs
, err
);
1784 static void v9fs_create_post_mksock(V9fsState
*s
, V9fsCreateState
*vs
,
1792 err
= v9fs_do_chmod(s
, &vs
->fullname
, vs
->perm
& 0777);
1793 v9fs_create_post_perms(s
, vs
, err
);
1797 v9fs_post_create(s
, vs
, err
);
1800 static void v9fs_create_post_fstat(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1807 v9fs_post_create(s
, vs
, err
);
1811 static void v9fs_create_post_open2(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1813 if (vs
->fidp
->fd
== -1) {
1818 err
= v9fs_do_fstat(s
, vs
->fidp
->fd
, &vs
->stbuf
);
1819 v9fs_create_post_fstat(s
, vs
, err
);
1824 v9fs_post_create(s
, vs
, err
);
1828 static void v9fs_create_post_lstat(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
1831 if (err
== 0 || errno
!= ENOENT
) {
1836 if (vs
->perm
& P9_STAT_MODE_DIR
) {
1837 err
= v9fs_do_mkdir(s
, &vs
->fullname
, vs
->perm
& 0777);
1838 v9fs_create_post_mkdir(s
, vs
, err
);
1839 } else if (vs
->perm
& P9_STAT_MODE_SYMLINK
) {
1840 err
= v9fs_do_symlink(s
, &vs
->extension
, &vs
->fullname
);
1841 v9fs_create_post_perms(s
, vs
, err
);
1842 } else if (vs
->perm
& P9_STAT_MODE_LINK
) {
1843 int32_t nfid
= atoi(vs
->extension
.data
);
1844 V9fsFidState
*nfidp
= lookup_fid(s
, nfid
);
1845 if (nfidp
== NULL
) {
1847 v9fs_post_create(s
, vs
, err
);
1849 err
= v9fs_do_link(s
, &nfidp
->path
, &vs
->fullname
);
1850 v9fs_create_post_perms(s
, vs
, err
);
1851 } else if (vs
->perm
& P9_STAT_MODE_DEVICE
) {
1853 uint32_t major
, minor
;
1856 if (sscanf(vs
->extension
.data
, "%c %u %u", &ctype
, &major
,
1859 v9fs_post_create(s
, vs
, err
);
1871 v9fs_post_create(s
, vs
, err
);
1874 nmode
|= vs
->perm
& 0777;
1875 err
= v9fs_do_mknod(s
, &vs
->fullname
, nmode
, makedev(major
, minor
));
1876 v9fs_create_post_perms(s
, vs
, err
);
1877 } else if (vs
->perm
& P9_STAT_MODE_NAMED_PIPE
) {
1878 err
= v9fs_do_mknod(s
, &vs
->fullname
, S_IFIFO
| (vs
->mode
& 0777), 0);
1879 v9fs_post_create(s
, vs
, err
);
1880 } else if (vs
->perm
& P9_STAT_MODE_SOCKET
) {
1881 err
= v9fs_do_mksock(s
, &vs
->fullname
);
1882 v9fs_create_post_mksock(s
, vs
, err
);
1884 vs
->fidp
->fd
= v9fs_do_open2(s
, &vs
->fullname
,
1885 omode_to_uflags(vs
->mode
) | O_CREAT
,
1887 v9fs_create_post_open2(s
, vs
, err
);
1893 v9fs_post_create(s
, vs
, err
);
1896 static void v9fs_create(V9fsState
*s
, V9fsPDU
*pdu
)
1899 V9fsCreateState
*vs
;
1902 vs
= qemu_malloc(sizeof(*vs
));
1906 v9fs_string_init(&vs
->fullname
);
1908 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsdbs", &fid
, &vs
->name
,
1909 &vs
->perm
, &vs
->mode
, &vs
->extension
);
1911 vs
->fidp
= lookup_fid(s
, fid
);
1912 if (vs
->fidp
== NULL
) {
1917 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", vs
->fidp
->path
.data
,
1920 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
1921 v9fs_create_post_lstat(s
, vs
, err
);
1925 complete_pdu(s
, vs
->pdu
, err
);
1926 v9fs_string_free(&vs
->name
);
1927 v9fs_string_free(&vs
->extension
);
1931 static void v9fs_flush(V9fsState
*s
, V9fsPDU
*pdu
)
1933 /* A nop call with no return */
1934 complete_pdu(s
, pdu
, 7);
1937 typedef struct V9fsRemoveState
{
1943 static void v9fs_remove_post_remove(V9fsState
*s
, V9fsRemoveState
*vs
,
1946 /* For TREMOVE we need to clunk the fid even on failed remove */
1947 err
= free_fid(s
, vs
->fidp
->fid
);
1954 complete_pdu(s
, vs
->pdu
, err
);
1958 static void v9fs_remove(V9fsState
*s
, V9fsPDU
*pdu
)
1961 V9fsRemoveState
*vs
;
1964 vs
= qemu_malloc(sizeof(*vs
));
1968 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &fid
);
1970 vs
->fidp
= lookup_fid(s
, fid
);
1971 if (vs
->fidp
== NULL
) {
1976 err
= v9fs_do_remove(s
, &vs
->fidp
->path
);
1977 v9fs_remove_post_remove(s
, vs
, err
);
1981 complete_pdu(s
, pdu
, err
);
1985 typedef struct V9fsWstatState
1996 static void v9fs_wstat_post_truncate(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2005 v9fs_stat_free(&vs
->v9stat
);
2006 complete_pdu(s
, vs
->pdu
, err
);
2010 static void v9fs_wstat_post_rename(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2016 if (vs
->v9stat
.name
.size
!= 0) {
2017 v9fs_string_free(&vs
->nname
);
2020 if (vs
->v9stat
.length
!= -1) {
2021 if (v9fs_do_truncate(s
, &vs
->fidp
->path
, vs
->v9stat
.length
) < 0) {
2025 v9fs_wstat_post_truncate(s
, vs
, err
);
2029 v9fs_stat_free(&vs
->v9stat
);
2030 complete_pdu(s
, vs
->pdu
, err
);
2034 static void v9fs_wstat_post_chown(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2041 if (vs
->v9stat
.name
.size
!= 0) {
2042 char *old_name
, *new_name
;
2045 old_name
= vs
->fidp
->path
.data
;
2046 end
= strrchr(old_name
, '/');
2053 new_name
= qemu_malloc(end
- old_name
+ vs
->v9stat
.name
.size
+ 1);
2055 memset(new_name
, 0, end
- old_name
+ vs
->v9stat
.name
.size
+ 1);
2056 memcpy(new_name
, old_name
, end
- old_name
);
2057 memcpy(new_name
+ (end
- old_name
), vs
->v9stat
.name
.data
,
2058 vs
->v9stat
.name
.size
);
2059 vs
->nname
.data
= new_name
;
2060 vs
->nname
.size
= strlen(new_name
);
2062 if (strcmp(new_name
, vs
->fidp
->path
.data
) != 0) {
2063 if (v9fs_do_rename(s
, &vs
->fidp
->path
, &vs
->nname
)) {
2067 * Fixup fid's pointing to the old name to
2068 * start pointing to the new name
2070 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
2072 if (vs
->fidp
== fidp
) {
2074 * we replace name of this fid towards the end
2075 * so that our below strcmp will work
2079 if (!strncmp(vs
->fidp
->path
.data
, fidp
->path
.data
,
2080 strlen(vs
->fidp
->path
.data
))) {
2081 /* replace the name */
2082 v9fs_fix_path(&fidp
->path
, &vs
->nname
,
2083 strlen(vs
->fidp
->path
.data
));
2086 v9fs_string_copy(&vs
->fidp
->path
, &vs
->nname
);
2090 v9fs_wstat_post_rename(s
, vs
, err
);
2094 v9fs_stat_free(&vs
->v9stat
);
2095 complete_pdu(s
, vs
->pdu
, err
);
2099 static void v9fs_wstat_post_utime(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2105 if (vs
->v9stat
.n_gid
!= -1) {
2106 if (v9fs_do_chown(s
, &vs
->fidp
->path
, vs
->v9stat
.n_uid
,
2107 vs
->v9stat
.n_gid
)) {
2111 v9fs_wstat_post_chown(s
, vs
, err
);
2115 v9fs_stat_free(&vs
->v9stat
);
2116 complete_pdu(s
, vs
->pdu
, err
);
2120 static void v9fs_wstat_post_chmod(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2126 if (vs
->v9stat
.mtime
!= -1) {
2129 tb
.modtime
= vs
->v9stat
.mtime
;
2130 if (v9fs_do_utime(s
, &vs
->fidp
->path
, &tb
)) {
2135 v9fs_wstat_post_utime(s
, vs
, err
);
2139 v9fs_stat_free(&vs
->v9stat
);
2140 complete_pdu(s
, vs
->pdu
, err
);
2144 static void v9fs_wstat_post_fsync(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2149 v9fs_stat_free(&vs
->v9stat
);
2150 complete_pdu(s
, vs
->pdu
, err
);
2154 static void v9fs_wstat_post_lstat(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2163 v9_mode
= stat_to_v9mode(&vs
->stbuf
);
2165 if ((vs
->v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2166 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2167 /* Attempting to change the type */
2172 if (v9fs_do_chmod(s
, &vs
->fidp
->path
, v9mode_to_mode(vs
->v9stat
.mode
,
2173 &vs
->v9stat
.extension
))) {
2176 v9fs_wstat_post_chmod(s
, vs
, err
);
2180 v9fs_stat_free(&vs
->v9stat
);
2181 complete_pdu(s
, vs
->pdu
, err
);
2185 static void v9fs_wstat(V9fsState
*s
, V9fsPDU
*pdu
)
2191 vs
= qemu_malloc(sizeof(*vs
));
2195 pdu_unmarshal(pdu
, vs
->offset
, "dwS", &fid
, &vs
->unused
, &vs
->v9stat
);
2197 vs
->fidp
= lookup_fid(s
, fid
);
2198 if (vs
->fidp
== NULL
) {
2203 /* do we need to sync the file? */
2204 if (donttouch_stat(&vs
->v9stat
)) {
2205 err
= v9fs_do_fsync(s
, vs
->fidp
->fd
);
2206 v9fs_wstat_post_fsync(s
, vs
, err
);
2210 if (vs
->v9stat
.mode
!= -1) {
2211 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
2212 v9fs_wstat_post_lstat(s
, vs
, err
);
2216 v9fs_wstat_post_chmod(s
, vs
, err
);
2220 v9fs_stat_free(&vs
->v9stat
);
2221 complete_pdu(s
, vs
->pdu
, err
);
2225 typedef void (pdu_handler_t
)(V9fsState
*s
, V9fsPDU
*pdu
);
2227 static pdu_handler_t
*pdu_handlers
[] = {
2228 [P9_TVERSION
] = v9fs_version
,
2229 [P9_TATTACH
] = v9fs_attach
,
2230 [P9_TSTAT
] = v9fs_stat
,
2231 [P9_TWALK
] = v9fs_walk
,
2232 [P9_TCLUNK
] = v9fs_clunk
,
2233 [P9_TOPEN
] = v9fs_open
,
2234 [P9_TREAD
] = v9fs_read
,
2236 [P9_TAUTH
] = v9fs_auth
,
2238 [P9_TFLUSH
] = v9fs_flush
,
2239 [P9_TCREATE
] = v9fs_create
,
2240 [P9_TWRITE
] = v9fs_write
,
2241 [P9_TWSTAT
] = v9fs_wstat
,
2242 [P9_TREMOVE
] = v9fs_remove
,
2245 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
2247 pdu_handler_t
*handler
;
2253 BUG_ON(pdu
->id
>= ARRAY_SIZE(pdu_handlers
));
2255 handler
= pdu_handlers
[pdu
->id
];
2256 BUG_ON(handler
== NULL
);
2261 static void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
2263 V9fsState
*s
= (V9fsState
*)vdev
;
2267 while ((pdu
= alloc_pdu(s
)) &&
2268 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
2271 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
2272 BUG_ON(pdu
->elem
.out_sg
[0].iov_len
< 7);
2274 ptr
= pdu
->elem
.out_sg
[0].iov_base
;
2276 memcpy(&pdu
->size
, ptr
, 4);
2278 memcpy(&pdu
->tag
, ptr
+ 5, 2);
2286 static uint32_t virtio_9p_get_features(VirtIODevice
*vdev
, uint32_t features
)
2288 features
|= 1 << VIRTIO_9P_MOUNT_TAG
;
2292 static V9fsState
*to_virtio_9p(VirtIODevice
*vdev
)
2294 return (V9fsState
*)vdev
;
2297 static void virtio_9p_get_config(VirtIODevice
*vdev
, uint8_t *config
)
2299 struct virtio_9p_config
*cfg
;
2300 V9fsState
*s
= to_virtio_9p(vdev
);
2302 cfg
= qemu_mallocz(sizeof(struct virtio_9p_config
) +
2304 stw_raw(&cfg
->tag_len
, s
->tag_len
);
2305 memcpy(cfg
->tag
, s
->tag
, s
->tag_len
);
2306 memcpy(config
, cfg
, s
->config_size
);
2310 VirtIODevice
*virtio_9p_init(DeviceState
*dev
, V9fsConf
*conf
)
2318 s
= (V9fsState
*)virtio_common_init("virtio-9p",
2320 sizeof(struct virtio_9p_config
)+
2324 /* initialize pdu allocator */
2325 QLIST_INIT(&s
->free_list
);
2326 for (i
= 0; i
< (MAX_REQ
- 1); i
++) {
2327 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
2330 s
->vq
= virtio_add_queue(&s
->vdev
, MAX_REQ
, handle_9p_output
);
2332 fse
= get_fsdev_fsentry(conf
->fsdev_id
);
2335 /* We don't have a fsdev identified by fsdev_id */
2336 fprintf(stderr
, "Virtio-9p device couldn't find fsdev "
2337 "with the id %s\n", conf
->fsdev_id
);
2341 if (!fse
->path
|| !conf
->tag
) {
2342 /* we haven't specified a mount_tag or the path */
2343 fprintf(stderr
, "fsdev with id %s needs path "
2344 "and Virtio-9p device needs mount_tag arguments\n",
2349 if (lstat(fse
->path
, &stat
)) {
2350 fprintf(stderr
, "share path %s does not exist\n", fse
->path
);
2352 } else if (!S_ISDIR(stat
.st_mode
)) {
2353 fprintf(stderr
, "share path %s is not a directory \n", fse
->path
);
2357 s
->ctx
.fs_root
= qemu_strdup(fse
->path
);
2358 len
= strlen(conf
->tag
);
2359 if (len
> MAX_TAG_LEN
) {
2362 /* s->tag is non-NULL terminated string */
2363 s
->tag
= qemu_malloc(len
);
2364 memcpy(s
->tag
, conf
->tag
, len
);
2369 s
->vdev
.get_features
= virtio_9p_get_features
;
2370 s
->config_size
= sizeof(struct virtio_9p_config
) +
2372 s
->vdev
.get_config
= virtio_9p_get_config
;