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_string_init(V9fsString
*str
)
75 static void v9fs_string_free(V9fsString
*str
)
82 static void v9fs_string_null(V9fsString
*str
)
84 v9fs_string_free(str
);
87 static int number_to_string(void *arg
, char type
)
93 unsigned int num
= *(unsigned int *)arg
;
102 printf("Number_to_string: Unknown number format\n");
109 static int v9fs_string_alloc_printf(char **strp
, const char *fmt
, va_list ap
)
112 char *iter
= (char *)fmt
;
116 unsigned int arg_uint
;
118 /* Find the number of %'s that denotes an argument */
119 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
124 len
= strlen(fmt
) - 2*nr_args
;
134 /* Now parse the format string */
135 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
139 arg_uint
= va_arg(ap2
, unsigned int);
140 len
+= number_to_string((void *)&arg_uint
, 'u');
143 arg_char_ptr
= va_arg(ap2
, char *);
144 len
+= strlen(arg_char_ptr
);
151 "v9fs_string_alloc_printf:Incorrect format %c", *iter
);
158 *strp
= qemu_malloc((len
+ 1) * sizeof(**strp
));
160 return vsprintf(*strp
, fmt
, ap
);
163 static void v9fs_string_sprintf(V9fsString
*str
, const char *fmt
, ...)
168 v9fs_string_free(str
);
171 err
= v9fs_string_alloc_printf(&str
->data
, fmt
, ap
);
178 static void v9fs_string_copy(V9fsString
*lhs
, V9fsString
*rhs
)
180 v9fs_string_free(lhs
);
181 v9fs_string_sprintf(lhs
, "%s", rhs
->data
);
184 static size_t v9fs_string_size(V9fsString
*str
)
189 static V9fsFidState
*lookup_fid(V9fsState
*s
, int32_t fid
)
193 for (f
= s
->fid_list
; f
; f
= f
->next
) {
195 v9fs_do_setuid(s
, f
->uid
);
203 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
207 f
= lookup_fid(s
, fid
);
212 f
= qemu_mallocz(sizeof(V9fsFidState
));
218 f
->next
= s
->fid_list
;
224 static int free_fid(V9fsState
*s
, int32_t fid
)
226 V9fsFidState
**fidpp
, *fidp
;
228 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
229 if ((*fidpp
)->fid
== fid
) {
234 if (*fidpp
== NULL
) {
241 if (fidp
->fd
!= -1) {
242 v9fs_do_close(s
, fidp
->fd
);
245 v9fs_do_closedir(s
, fidp
->dir
);
247 v9fs_string_free(&fidp
->path
);
253 #define P9_QID_TYPE_DIR 0x80
254 #define P9_QID_TYPE_SYMLINK 0x02
256 #define P9_STAT_MODE_DIR 0x80000000
257 #define P9_STAT_MODE_APPEND 0x40000000
258 #define P9_STAT_MODE_EXCL 0x20000000
259 #define P9_STAT_MODE_MOUNT 0x10000000
260 #define P9_STAT_MODE_AUTH 0x08000000
261 #define P9_STAT_MODE_TMP 0x04000000
262 #define P9_STAT_MODE_SYMLINK 0x02000000
263 #define P9_STAT_MODE_LINK 0x01000000
264 #define P9_STAT_MODE_DEVICE 0x00800000
265 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
266 #define P9_STAT_MODE_SOCKET 0x00100000
267 #define P9_STAT_MODE_SETUID 0x00080000
268 #define P9_STAT_MODE_SETGID 0x00040000
269 #define P9_STAT_MODE_SETVTX 0x00010000
271 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
272 P9_STAT_MODE_SYMLINK | \
273 P9_STAT_MODE_LINK | \
274 P9_STAT_MODE_DEVICE | \
275 P9_STAT_MODE_NAMED_PIPE | \
278 /* This is the algorithm from ufs in spfs */
279 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
283 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
284 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
285 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
287 if (S_ISDIR(stbuf
->st_mode
)) {
288 qidp
->type
|= P9_QID_TYPE_DIR
;
290 if (S_ISLNK(stbuf
->st_mode
)) {
291 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
295 static int fid_to_qid(V9fsState
*s
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
300 err
= v9fs_do_lstat(s
, &fidp
->path
, &stbuf
);
305 stat_to_qid(&stbuf
, qidp
);
309 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
313 if (!QLIST_EMPTY(&s
->free_list
)) {
314 pdu
= QLIST_FIRST(&s
->free_list
);
315 QLIST_REMOVE(pdu
, next
);
320 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
323 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
327 size_t pdu_packunpack(void *addr
, struct iovec
*sg
, int sg_count
,
328 size_t offset
, size_t size
, int pack
)
333 for (i
= 0; size
&& i
< sg_count
; i
++) {
335 if (offset
>= sg
[i
].iov_len
) {
337 offset
-= sg
[i
].iov_len
;
340 len
= MIN(sg
[i
].iov_len
- offset
, size
);
342 memcpy(sg
[i
].iov_base
+ offset
, addr
, len
);
344 memcpy(addr
, sg
[i
].iov_base
+ offset
, len
);
359 static size_t pdu_unpack(void *dst
, V9fsPDU
*pdu
, size_t offset
, size_t size
)
361 return pdu_packunpack(dst
, pdu
->elem
.out_sg
, pdu
->elem
.out_num
,
365 static size_t pdu_pack(V9fsPDU
*pdu
, size_t offset
, const void *src
,
368 return pdu_packunpack((void *)src
, pdu
->elem
.in_sg
, pdu
->elem
.in_num
,
372 static int pdu_copy_sg(V9fsPDU
*pdu
, size_t offset
, int rx
, struct iovec
*sg
)
376 struct iovec
*src_sg
;
380 src_sg
= pdu
->elem
.in_sg
;
381 num
= pdu
->elem
.in_num
;
383 src_sg
= pdu
->elem
.out_sg
;
384 num
= pdu
->elem
.out_num
;
388 for (i
= 0; i
< num
; i
++) {
390 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
391 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
393 } else if (offset
< (src_sg
[i
].iov_len
+ pos
)) {
394 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
395 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
396 sg
[j
].iov_base
+= (offset
- pos
);
397 sg
[j
].iov_len
-= (offset
- pos
);
400 pos
+= src_sg
[i
].iov_len
;
406 static size_t pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
408 size_t old_offset
= offset
;
413 for (i
= 0; fmt
[i
]; i
++) {
416 uint8_t *valp
= va_arg(ap
, uint8_t *);
417 offset
+= pdu_unpack(valp
, pdu
, offset
, sizeof(*valp
));
422 valp
= va_arg(ap
, uint16_t *);
423 val
= le16_to_cpupu(valp
);
424 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
430 valp
= va_arg(ap
, uint32_t *);
431 val
= le32_to_cpupu(valp
);
432 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
438 valp
= va_arg(ap
, uint64_t *);
439 val
= le64_to_cpup(valp
);
440 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
445 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
446 int *iovcnt
= va_arg(ap
, int *);
447 *iovcnt
= pdu_copy_sg(pdu
, offset
, 0, iov
);
451 V9fsString
*str
= va_arg(ap
, V9fsString
*);
452 offset
+= pdu_unmarshal(pdu
, offset
, "w", &str
->size
);
453 /* FIXME: sanity check str->size */
454 str
->data
= qemu_malloc(str
->size
+ 1);
455 offset
+= pdu_unpack(str
->data
, pdu
, offset
, str
->size
);
456 str
->data
[str
->size
] = 0;
460 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
461 offset
+= pdu_unmarshal(pdu
, offset
, "bdq",
462 &qidp
->type
, &qidp
->version
, &qidp
->path
);
466 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
467 offset
+= pdu_unmarshal(pdu
, offset
, "wwdQdddqsssssddd",
468 &statp
->size
, &statp
->type
, &statp
->dev
,
469 &statp
->qid
, &statp
->mode
, &statp
->atime
,
470 &statp
->mtime
, &statp
->length
,
471 &statp
->name
, &statp
->uid
, &statp
->gid
,
472 &statp
->muid
, &statp
->extension
,
473 &statp
->n_uid
, &statp
->n_gid
,
484 return offset
- old_offset
;
487 static size_t pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
489 size_t old_offset
= offset
;
494 for (i
= 0; fmt
[i
]; i
++) {
497 uint8_t val
= va_arg(ap
, int);
498 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
503 cpu_to_le16w(&val
, va_arg(ap
, int));
504 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
509 cpu_to_le32w(&val
, va_arg(ap
, uint32_t));
510 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
515 cpu_to_le64w(&val
, va_arg(ap
, uint64_t));
516 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
520 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
521 int *iovcnt
= va_arg(ap
, int *);
522 *iovcnt
= pdu_copy_sg(pdu
, offset
, 1, iov
);
526 V9fsString
*str
= va_arg(ap
, V9fsString
*);
527 offset
+= pdu_marshal(pdu
, offset
, "w", str
->size
);
528 offset
+= pdu_pack(pdu
, offset
, str
->data
, str
->size
);
532 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
533 offset
+= pdu_marshal(pdu
, offset
, "bdq",
534 qidp
->type
, qidp
->version
, qidp
->path
);
538 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
539 offset
+= pdu_marshal(pdu
, offset
, "wwdQdddqsssssddd",
540 statp
->size
, statp
->type
, statp
->dev
,
541 &statp
->qid
, statp
->mode
, statp
->atime
,
542 statp
->mtime
, statp
->length
, &statp
->name
,
543 &statp
->uid
, &statp
->gid
, &statp
->muid
,
544 &statp
->extension
, statp
->n_uid
,
545 statp
->n_gid
, statp
->n_muid
);
554 return offset
- old_offset
;
557 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
559 int8_t id
= pdu
->id
+ 1; /* Response */
565 str
.data
= strerror(err
);
566 str
.size
= strlen(str
.data
);
569 len
+= pdu_marshal(pdu
, len
, "s", &str
);
571 len
+= pdu_marshal(pdu
, len
, "d", err
);
577 /* fill out the header */
578 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
580 /* keep these in sync */
584 /* push onto queue and notify */
585 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
587 /* FIXME: we should batch these completions */
588 virtio_notify(&s
->vdev
, s
->vq
);
593 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
598 if (mode
& P9_STAT_MODE_DIR
) {
603 if (mode
& P9_STAT_MODE_SYMLINK
) {
606 if (mode
& P9_STAT_MODE_SOCKET
) {
609 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
612 if (mode
& P9_STAT_MODE_DEVICE
) {
613 if (extension
&& extension
->data
[0] == 'c') {
625 if (mode
& P9_STAT_MODE_SETUID
) {
628 if (mode
& P9_STAT_MODE_SETGID
) {
631 if (mode
& P9_STAT_MODE_SETVTX
) {
638 static int donttouch_stat(V9fsStat
*stat
)
640 if (stat
->type
== -1 &&
642 stat
->qid
.type
== -1 &&
643 stat
->qid
.version
== -1 &&
644 stat
->qid
.path
== -1 &&
648 stat
->length
== -1 &&
655 stat
->n_muid
== -1) {
662 static void v9fs_stat_free(V9fsStat
*stat
)
664 v9fs_string_free(&stat
->name
);
665 v9fs_string_free(&stat
->uid
);
666 v9fs_string_free(&stat
->gid
);
667 v9fs_string_free(&stat
->muid
);
668 v9fs_string_free(&stat
->extension
);
671 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
675 mode
= stbuf
->st_mode
& 0777;
676 if (S_ISDIR(stbuf
->st_mode
)) {
677 mode
|= P9_STAT_MODE_DIR
;
681 if (S_ISLNK(stbuf
->st_mode
)) {
682 mode
|= P9_STAT_MODE_SYMLINK
;
685 if (S_ISSOCK(stbuf
->st_mode
)) {
686 mode
|= P9_STAT_MODE_SOCKET
;
689 if (S_ISFIFO(stbuf
->st_mode
)) {
690 mode
|= P9_STAT_MODE_NAMED_PIPE
;
693 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
694 mode
|= P9_STAT_MODE_DEVICE
;
697 if (stbuf
->st_mode
& S_ISUID
) {
698 mode
|= P9_STAT_MODE_SETUID
;
701 if (stbuf
->st_mode
& S_ISGID
) {
702 mode
|= P9_STAT_MODE_SETGID
;
705 if (stbuf
->st_mode
& S_ISVTX
) {
706 mode
|= P9_STAT_MODE_SETVTX
;
713 static int stat_to_v9stat(V9fsState
*s
, V9fsString
*name
,
714 const struct stat
*stbuf
,
720 memset(v9stat
, 0, sizeof(*v9stat
));
722 stat_to_qid(stbuf
, &v9stat
->qid
);
723 v9stat
->mode
= stat_to_v9mode(stbuf
);
724 v9stat
->atime
= stbuf
->st_atime
;
725 v9stat
->mtime
= stbuf
->st_mtime
;
726 v9stat
->length
= stbuf
->st_size
;
728 v9fs_string_null(&v9stat
->uid
);
729 v9fs_string_null(&v9stat
->gid
);
730 v9fs_string_null(&v9stat
->muid
);
733 v9stat
->n_uid
= stbuf
->st_uid
;
734 v9stat
->n_gid
= stbuf
->st_gid
;
737 v9fs_string_null(&v9stat
->extension
);
739 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
740 err
= v9fs_do_readlink(s
, name
, &v9stat
->extension
);
745 v9stat
->extension
.data
[err
] = 0;
746 v9stat
->extension
.size
= err
;
747 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
748 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
749 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
750 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
751 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
752 v9fs_string_sprintf(&v9stat
->extension
, "%s %u",
753 "HARDLINKCOUNT", stbuf
->st_nlink
);
757 str
= strrchr(name
->data
, '/');
764 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
767 v9fs_string_size(&v9stat
->name
) +
768 v9fs_string_size(&v9stat
->uid
) +
769 v9fs_string_size(&v9stat
->gid
) +
770 v9fs_string_size(&v9stat
->muid
) +
771 v9fs_string_size(&v9stat
->extension
);
775 static struct iovec
*adjust_sg(struct iovec
*sg
, int len
, int *iovcnt
)
777 while (len
&& *iovcnt
) {
778 if (len
< sg
->iov_len
) {
792 static struct iovec
*cap_sg(struct iovec
*sg
, int cap
, int *cnt
)
797 for (i
= 0; i
< *cnt
; i
++) {
798 if ((total
+ sg
[i
].iov_len
) > cap
) {
799 sg
[i
].iov_len
-= ((total
+ sg
[i
].iov_len
) - cap
);
803 total
+= sg
[i
].iov_len
;
811 static void print_sg(struct iovec
*sg
, int cnt
)
815 printf("sg[%d]: {", cnt
);
816 for (i
= 0; i
< cnt
; i
++) {
820 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
825 static void v9fs_dummy(V9fsState
*s
, V9fsPDU
*pdu
)
827 /* Note: The following have been added to prevent GCC from complaining
828 * They will be removed in the subsequent patches */
831 (void) v9fs_string_init
;
832 (void) v9fs_string_free
;
833 (void) v9fs_string_null
;
834 (void) v9fs_string_sprintf
;
835 (void) v9fs_string_copy
;
836 (void) v9fs_string_size
;
837 (void) v9fs_do_lstat
;
838 (void) v9fs_do_setuid
;
839 (void) v9fs_do_readlink
;
840 (void) v9fs_do_close
;
841 (void) v9fs_do_closedir
;
845 (void) v9mode_to_mode
;
846 (void) donttouch_stat
;
847 (void) v9fs_stat_free
;
848 (void) stat_to_v9stat
;
854 static void v9fs_version(V9fsState
*s
, V9fsPDU
*pdu
)
860 pdu_unmarshal(pdu
, offset
, "ds", &msize
, &version
);
862 if (strcmp(version
.data
, "9P2000.u")) {
863 v9fs_string_sprintf(&version
, "unknown");
866 offset
+= pdu_marshal(pdu
, offset
, "ds", msize
, &version
);
867 complete_pdu(s
, pdu
, offset
);
869 v9fs_string_free(&version
);
872 static void v9fs_attach(V9fsState
*s
, V9fsPDU
*pdu
)
874 int32_t fid
, afid
, n_uname
;
875 V9fsString uname
, aname
;
881 pdu_unmarshal(pdu
, offset
, "ddssd", &fid
, &afid
, &uname
, &aname
, &n_uname
);
883 fidp
= alloc_fid(s
, fid
);
891 v9fs_string_sprintf(&fidp
->path
, "%s", "/");
892 err
= fid_to_qid(s
, fidp
, &qid
);
899 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
903 complete_pdu(s
, pdu
, err
);
904 v9fs_string_free(&uname
);
905 v9fs_string_free(&aname
);
908 typedef struct V9fsStatState
{
916 static void v9fs_stat_post_lstat(V9fsState
*s
, V9fsStatState
*vs
, int err
)
923 err
= stat_to_v9stat(s
, &vs
->fidp
->path
, &vs
->stbuf
, &vs
->v9stat
);
927 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "wS", 0, &vs
->v9stat
);
931 complete_pdu(s
, vs
->pdu
, err
);
932 v9fs_stat_free(&vs
->v9stat
);
936 static void v9fs_stat(V9fsState
*s
, V9fsPDU
*pdu
)
942 vs
= qemu_malloc(sizeof(*vs
));
946 memset(&vs
->v9stat
, 0, sizeof(vs
->v9stat
));
948 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &fid
);
950 vs
->fidp
= lookup_fid(s
, fid
);
951 if (vs
->fidp
== NULL
) {
956 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
957 v9fs_stat_post_lstat(s
, vs
, err
);
961 complete_pdu(s
, vs
->pdu
, err
);
962 v9fs_stat_free(&vs
->v9stat
);
966 typedef struct V9fsWalkState
{
973 V9fsFidState
*newfidp
;
979 static void v9fs_walk_complete(V9fsState
*s
, V9fsWalkState
*vs
, int err
)
981 complete_pdu(s
, vs
->pdu
, err
);
984 for (vs
->name_idx
= 0; vs
->name_idx
< vs
->nwnames
; vs
->name_idx
++) {
985 v9fs_string_free(&vs
->wnames
[vs
->name_idx
]);
988 qemu_free(vs
->wnames
);
993 static void v9fs_walk_marshal(V9fsWalkState
*vs
)
997 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "w", vs
->nwnames
);
999 for (i
= 0; i
< vs
->nwnames
; i
++) {
1000 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qids
[i
]);
1004 static void v9fs_walk_post_newfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
1008 free_fid(s
, vs
->newfidp
->fid
);
1009 v9fs_string_free(&vs
->path
);
1014 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1017 if (vs
->name_idx
< vs
->nwnames
) {
1018 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1019 vs
->wnames
[vs
->name_idx
].data
);
1020 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1022 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1023 v9fs_walk_post_newfid_lstat(s
, vs
, err
);
1027 v9fs_string_free(&vs
->path
);
1028 v9fs_walk_marshal(vs
);
1031 v9fs_walk_complete(s
, vs
, err
);
1034 static void v9fs_walk_post_oldfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
1038 v9fs_string_free(&vs
->path
);
1043 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1045 if (vs
->name_idx
< vs
->nwnames
) {
1047 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1048 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1049 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1051 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1052 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1056 v9fs_string_free(&vs
->path
);
1057 v9fs_walk_marshal(vs
);
1060 v9fs_walk_complete(s
, vs
, err
);
1063 static void v9fs_walk(V9fsState
*s
, V9fsPDU
*pdu
)
1065 int32_t fid
, newfid
;
1070 vs
= qemu_malloc(sizeof(*vs
));
1076 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "ddw", &fid
,
1077 &newfid
, &vs
->nwnames
);
1080 vs
->wnames
= qemu_mallocz(sizeof(vs
->wnames
[0]) * vs
->nwnames
);
1082 vs
->qids
= qemu_mallocz(sizeof(vs
->qids
[0]) * vs
->nwnames
);
1084 for (i
= 0; i
< vs
->nwnames
; i
++) {
1085 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "s",
1090 vs
->fidp
= lookup_fid(s
, fid
);
1091 if (vs
->fidp
== NULL
) {
1096 /* FIXME: is this really valid? */
1097 if (fid
== newfid
) {
1099 BUG_ON(vs
->fidp
->fd
!= -1);
1100 BUG_ON(vs
->fidp
->dir
);
1101 v9fs_string_init(&vs
->path
);
1104 if (vs
->name_idx
< vs
->nwnames
) {
1105 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1106 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1107 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1109 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1110 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1114 vs
->newfidp
= alloc_fid(s
, newfid
);
1115 if (vs
->newfidp
== NULL
) {
1120 vs
->newfidp
->uid
= vs
->fidp
->uid
;
1121 v9fs_string_init(&vs
->path
);
1123 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->fidp
->path
);
1125 if (vs
->name_idx
< vs
->nwnames
) {
1126 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1127 vs
->wnames
[vs
->name_idx
].data
);
1128 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1130 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1131 v9fs_walk_post_newfid_lstat(s
, vs
, err
);
1136 v9fs_walk_marshal(vs
);
1139 v9fs_walk_complete(s
, vs
, err
);
1142 typedef struct V9fsOpenState
{
1164 static int omode_to_uflags(int8_t mode
)
1183 if (mode
& Otrunc
) {
1187 if (mode
& Oappend
) {
1198 static void v9fs_open_post_opendir(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1200 if (vs
->fidp
->dir
== NULL
) {
1205 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, 0);
1208 complete_pdu(s
, vs
->pdu
, err
);
1213 static void v9fs_open_post_open(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1215 if (vs
->fidp
->fd
== -1) {
1220 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, 0);
1223 complete_pdu(s
, vs
->pdu
, err
);
1227 static void v9fs_open_post_lstat(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1234 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
1236 if (S_ISDIR(vs
->stbuf
.st_mode
)) {
1237 vs
->fidp
->dir
= v9fs_do_opendir(s
, &vs
->fidp
->path
);
1238 v9fs_open_post_opendir(s
, vs
, err
);
1240 vs
->fidp
->fd
= v9fs_do_open(s
, &vs
->fidp
->path
,
1241 omode_to_uflags(vs
->mode
));
1242 v9fs_open_post_open(s
, vs
, err
);
1246 complete_pdu(s
, vs
->pdu
, err
);
1250 static void v9fs_open(V9fsState
*s
, V9fsPDU
*pdu
)
1257 vs
= qemu_malloc(sizeof(*vs
));
1261 pdu_unmarshal(vs
->pdu
, vs
->offset
, "db", &fid
, &vs
->mode
);
1263 vs
->fidp
= lookup_fid(s
, fid
);
1264 if (vs
->fidp
== NULL
) {
1269 BUG_ON(vs
->fidp
->fd
!= -1);
1270 BUG_ON(vs
->fidp
->dir
);
1272 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1274 v9fs_open_post_lstat(s
, vs
, err
);
1277 complete_pdu(s
, pdu
, err
);
1281 static void v9fs_clunk(V9fsState
*s
, V9fsPDU
*pdu
)
1288 static void v9fs_read(V9fsState
*s
, V9fsPDU
*pdu
)
1295 static void v9fs_write(V9fsState
*s
, V9fsPDU
*pdu
)
1302 static void v9fs_create(V9fsState
*s
, V9fsPDU
*pdu
)
1309 static void v9fs_flush(V9fsState
*s
, V9fsPDU
*pdu
)
1317 static void v9fs_remove(V9fsState
*s
, V9fsPDU
*pdu
)
1324 static void v9fs_wstat(V9fsState
*s
, V9fsPDU
*pdu
)
1331 typedef void (pdu_handler_t
)(V9fsState
*s
, V9fsPDU
*pdu
);
1333 static pdu_handler_t
*pdu_handlers
[] = {
1334 [P9_TVERSION
] = v9fs_version
,
1335 [P9_TATTACH
] = v9fs_attach
,
1336 [P9_TSTAT
] = v9fs_stat
,
1337 [P9_TWALK
] = v9fs_walk
,
1338 [P9_TCLUNK
] = v9fs_clunk
,
1339 [P9_TOPEN
] = v9fs_open
,
1340 [P9_TREAD
] = v9fs_read
,
1342 [P9_TAUTH
] = v9fs_auth
,
1344 [P9_TFLUSH
] = v9fs_flush
,
1345 [P9_TCREATE
] = v9fs_create
,
1346 [P9_TWRITE
] = v9fs_write
,
1347 [P9_TWSTAT
] = v9fs_wstat
,
1348 [P9_TREMOVE
] = v9fs_remove
,
1351 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
1353 pdu_handler_t
*handler
;
1359 BUG_ON(pdu
->id
>= ARRAY_SIZE(pdu_handlers
));
1361 handler
= pdu_handlers
[pdu
->id
];
1362 BUG_ON(handler
== NULL
);
1367 static void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
1369 V9fsState
*s
= (V9fsState
*)vdev
;
1373 while ((pdu
= alloc_pdu(s
)) &&
1374 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
1377 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
1378 BUG_ON(pdu
->elem
.out_sg
[0].iov_len
< 7);
1380 ptr
= pdu
->elem
.out_sg
[0].iov_base
;
1382 memcpy(&pdu
->size
, ptr
, 4);
1384 memcpy(&pdu
->tag
, ptr
+ 5, 2);
1392 static uint32_t virtio_9p_get_features(VirtIODevice
*vdev
, uint32_t features
)
1394 features
|= 1 << VIRTIO_9P_MOUNT_TAG
;
1398 static V9fsState
*to_virtio_9p(VirtIODevice
*vdev
)
1400 return (V9fsState
*)vdev
;
1403 static void virtio_9p_get_config(VirtIODevice
*vdev
, uint8_t *config
)
1405 struct virtio_9p_config
*cfg
;
1406 V9fsState
*s
= to_virtio_9p(vdev
);
1408 cfg
= qemu_mallocz(sizeof(struct virtio_9p_config
) +
1410 stw_raw(&cfg
->tag_len
, s
->tag_len
);
1411 memcpy(cfg
->tag
, s
->tag
, s
->tag_len
);
1412 memcpy(config
, cfg
, s
->config_size
);
1416 VirtIODevice
*virtio_9p_init(DeviceState
*dev
, V9fsConf
*conf
)
1424 s
= (V9fsState
*)virtio_common_init("virtio-9p",
1426 sizeof(struct virtio_9p_config
)+
1430 /* initialize pdu allocator */
1431 QLIST_INIT(&s
->free_list
);
1432 for (i
= 0; i
< (MAX_REQ
- 1); i
++) {
1433 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
1436 s
->vq
= virtio_add_queue(&s
->vdev
, MAX_REQ
, handle_9p_output
);
1438 fse
= get_fsdev_fsentry(conf
->fsdev_id
);
1441 /* We don't have a fsdev identified by fsdev_id */
1442 fprintf(stderr
, "Virtio-9p device couldn't find fsdev "
1443 "with the id %s\n", conf
->fsdev_id
);
1447 if (!fse
->path
|| !conf
->tag
) {
1448 /* we haven't specified a mount_tag or the path */
1449 fprintf(stderr
, "fsdev with id %s needs path "
1450 "and Virtio-9p device needs mount_tag arguments\n",
1455 if (lstat(fse
->path
, &stat
)) {
1456 fprintf(stderr
, "share path %s does not exist\n", fse
->path
);
1458 } else if (!S_ISDIR(stat
.st_mode
)) {
1459 fprintf(stderr
, "share path %s is not a directory \n", fse
->path
);
1463 s
->ctx
.fs_root
= qemu_strdup(fse
->path
);
1464 len
= strlen(conf
->tag
);
1465 if (len
> MAX_TAG_LEN
) {
1468 /* s->tag is non-NULL terminated string */
1469 s
->tag
= qemu_malloc(len
);
1470 memcpy(s
->tag
, conf
->tag
, len
);
1475 s
->vdev
.get_features
= virtio_9p_get_features
;
1476 s
->config_size
= sizeof(struct virtio_9p_config
) +
1478 s
->vdev
.get_config
= virtio_9p_get_config
;