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 void v9fs_string_init(V9fsString
*str
)
65 static void v9fs_string_free(V9fsString
*str
)
72 static void v9fs_string_null(V9fsString
*str
)
74 v9fs_string_free(str
);
77 static int number_to_string(void *arg
, char type
)
83 unsigned int num
= *(unsigned int *)arg
;
92 printf("Number_to_string: Unknown number format\n");
99 static int v9fs_string_alloc_printf(char **strp
, const char *fmt
, va_list ap
)
102 char *iter
= (char *)fmt
;
106 unsigned int arg_uint
;
108 /* Find the number of %'s that denotes an argument */
109 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
114 len
= strlen(fmt
) - 2*nr_args
;
124 /* Now parse the format string */
125 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
129 arg_uint
= va_arg(ap2
, unsigned int);
130 len
+= number_to_string((void *)&arg_uint
, 'u');
133 arg_char_ptr
= va_arg(ap2
, char *);
134 len
+= strlen(arg_char_ptr
);
141 "v9fs_string_alloc_printf:Incorrect format %c", *iter
);
148 *strp
= qemu_malloc((len
+ 1) * sizeof(**strp
));
150 return vsprintf(*strp
, fmt
, ap
);
153 static void v9fs_string_sprintf(V9fsString
*str
, const char *fmt
, ...)
158 v9fs_string_free(str
);
161 err
= v9fs_string_alloc_printf(&str
->data
, fmt
, ap
);
168 static void v9fs_string_copy(V9fsString
*lhs
, V9fsString
*rhs
)
170 v9fs_string_free(lhs
);
171 v9fs_string_sprintf(lhs
, "%s", rhs
->data
);
174 static size_t v9fs_string_size(V9fsString
*str
)
179 static V9fsFidState
*lookup_fid(V9fsState
*s
, int32_t fid
)
183 for (f
= s
->fid_list
; f
; f
= f
->next
) {
185 v9fs_do_setuid(s
, f
->uid
);
193 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
197 f
= lookup_fid(s
, fid
);
202 f
= qemu_mallocz(sizeof(V9fsFidState
));
208 f
->next
= s
->fid_list
;
214 static int free_fid(V9fsState
*s
, int32_t fid
)
216 V9fsFidState
**fidpp
, *fidp
;
218 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
219 if ((*fidpp
)->fid
== fid
) {
224 if (*fidpp
== NULL
) {
231 if (fidp
->fd
!= -1) {
232 v9fs_do_close(s
, fidp
->fd
);
235 v9fs_do_closedir(s
, fidp
->dir
);
237 v9fs_string_free(&fidp
->path
);
243 #define P9_QID_TYPE_DIR 0x80
244 #define P9_QID_TYPE_SYMLINK 0x02
246 #define P9_STAT_MODE_DIR 0x80000000
247 #define P9_STAT_MODE_APPEND 0x40000000
248 #define P9_STAT_MODE_EXCL 0x20000000
249 #define P9_STAT_MODE_MOUNT 0x10000000
250 #define P9_STAT_MODE_AUTH 0x08000000
251 #define P9_STAT_MODE_TMP 0x04000000
252 #define P9_STAT_MODE_SYMLINK 0x02000000
253 #define P9_STAT_MODE_LINK 0x01000000
254 #define P9_STAT_MODE_DEVICE 0x00800000
255 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
256 #define P9_STAT_MODE_SOCKET 0x00100000
257 #define P9_STAT_MODE_SETUID 0x00080000
258 #define P9_STAT_MODE_SETGID 0x00040000
259 #define P9_STAT_MODE_SETVTX 0x00010000
261 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
262 P9_STAT_MODE_SYMLINK | \
263 P9_STAT_MODE_LINK | \
264 P9_STAT_MODE_DEVICE | \
265 P9_STAT_MODE_NAMED_PIPE | \
268 /* This is the algorithm from ufs in spfs */
269 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
273 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
274 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
275 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
277 if (S_ISDIR(stbuf
->st_mode
)) {
278 qidp
->type
|= P9_QID_TYPE_DIR
;
280 if (S_ISLNK(stbuf
->st_mode
)) {
281 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
285 static int fid_to_qid(V9fsState
*s
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
290 err
= v9fs_do_lstat(s
, &fidp
->path
, &stbuf
);
295 stat_to_qid(&stbuf
, qidp
);
299 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
303 if (!QLIST_EMPTY(&s
->free_list
)) {
304 pdu
= QLIST_FIRST(&s
->free_list
);
305 QLIST_REMOVE(pdu
, next
);
310 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
313 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
317 size_t pdu_packunpack(void *addr
, struct iovec
*sg
, int sg_count
,
318 size_t offset
, size_t size
, int pack
)
323 for (i
= 0; size
&& i
< sg_count
; i
++) {
325 if (offset
>= sg
[i
].iov_len
) {
327 offset
-= sg
[i
].iov_len
;
330 len
= MIN(sg
[i
].iov_len
- offset
, size
);
332 memcpy(sg
[i
].iov_base
+ offset
, addr
, len
);
334 memcpy(addr
, sg
[i
].iov_base
+ offset
, len
);
349 static size_t pdu_unpack(void *dst
, V9fsPDU
*pdu
, size_t offset
, size_t size
)
351 return pdu_packunpack(dst
, pdu
->elem
.out_sg
, pdu
->elem
.out_num
,
355 static size_t pdu_pack(V9fsPDU
*pdu
, size_t offset
, const void *src
,
358 return pdu_packunpack((void *)src
, pdu
->elem
.in_sg
, pdu
->elem
.in_num
,
362 static int pdu_copy_sg(V9fsPDU
*pdu
, size_t offset
, int rx
, struct iovec
*sg
)
366 struct iovec
*src_sg
;
370 src_sg
= pdu
->elem
.in_sg
;
371 num
= pdu
->elem
.in_num
;
373 src_sg
= pdu
->elem
.out_sg
;
374 num
= pdu
->elem
.out_num
;
378 for (i
= 0; i
< num
; i
++) {
380 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
381 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
383 } else if (offset
< (src_sg
[i
].iov_len
+ pos
)) {
384 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
385 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
386 sg
[j
].iov_base
+= (offset
- pos
);
387 sg
[j
].iov_len
-= (offset
- pos
);
390 pos
+= src_sg
[i
].iov_len
;
396 static size_t pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
398 size_t old_offset
= offset
;
403 for (i
= 0; fmt
[i
]; i
++) {
406 uint8_t *valp
= va_arg(ap
, uint8_t *);
407 offset
+= pdu_unpack(valp
, pdu
, offset
, sizeof(*valp
));
412 valp
= va_arg(ap
, uint16_t *);
413 val
= le16_to_cpupu(valp
);
414 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
420 valp
= va_arg(ap
, uint32_t *);
421 val
= le32_to_cpupu(valp
);
422 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
428 valp
= va_arg(ap
, uint64_t *);
429 val
= le64_to_cpup(valp
);
430 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
435 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
436 int *iovcnt
= va_arg(ap
, int *);
437 *iovcnt
= pdu_copy_sg(pdu
, offset
, 0, iov
);
441 V9fsString
*str
= va_arg(ap
, V9fsString
*);
442 offset
+= pdu_unmarshal(pdu
, offset
, "w", &str
->size
);
443 /* FIXME: sanity check str->size */
444 str
->data
= qemu_malloc(str
->size
+ 1);
445 offset
+= pdu_unpack(str
->data
, pdu
, offset
, str
->size
);
446 str
->data
[str
->size
] = 0;
450 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
451 offset
+= pdu_unmarshal(pdu
, offset
, "bdq",
452 &qidp
->type
, &qidp
->version
, &qidp
->path
);
456 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
457 offset
+= pdu_unmarshal(pdu
, offset
, "wwdQdddqsssssddd",
458 &statp
->size
, &statp
->type
, &statp
->dev
,
459 &statp
->qid
, &statp
->mode
, &statp
->atime
,
460 &statp
->mtime
, &statp
->length
,
461 &statp
->name
, &statp
->uid
, &statp
->gid
,
462 &statp
->muid
, &statp
->extension
,
463 &statp
->n_uid
, &statp
->n_gid
,
474 return offset
- old_offset
;
477 static size_t pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
479 size_t old_offset
= offset
;
484 for (i
= 0; fmt
[i
]; i
++) {
487 uint8_t val
= va_arg(ap
, int);
488 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
493 cpu_to_le16w(&val
, va_arg(ap
, int));
494 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
499 cpu_to_le32w(&val
, va_arg(ap
, uint32_t));
500 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
505 cpu_to_le64w(&val
, va_arg(ap
, uint64_t));
506 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
510 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
511 int *iovcnt
= va_arg(ap
, int *);
512 *iovcnt
= pdu_copy_sg(pdu
, offset
, 1, iov
);
516 V9fsString
*str
= va_arg(ap
, V9fsString
*);
517 offset
+= pdu_marshal(pdu
, offset
, "w", str
->size
);
518 offset
+= pdu_pack(pdu
, offset
, str
->data
, str
->size
);
522 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
523 offset
+= pdu_marshal(pdu
, offset
, "bdq",
524 qidp
->type
, qidp
->version
, qidp
->path
);
528 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
529 offset
+= pdu_marshal(pdu
, offset
, "wwdQdddqsssssddd",
530 statp
->size
, statp
->type
, statp
->dev
,
531 &statp
->qid
, statp
->mode
, statp
->atime
,
532 statp
->mtime
, statp
->length
, &statp
->name
,
533 &statp
->uid
, &statp
->gid
, &statp
->muid
,
534 &statp
->extension
, statp
->n_uid
,
535 statp
->n_gid
, statp
->n_muid
);
544 return offset
- old_offset
;
547 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
549 int8_t id
= pdu
->id
+ 1; /* Response */
555 str
.data
= strerror(err
);
556 str
.size
= strlen(str
.data
);
559 len
+= pdu_marshal(pdu
, len
, "s", &str
);
561 len
+= pdu_marshal(pdu
, len
, "d", err
);
567 /* fill out the header */
568 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
570 /* keep these in sync */
574 /* push onto queue and notify */
575 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
577 /* FIXME: we should batch these completions */
578 virtio_notify(&s
->vdev
, s
->vq
);
583 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
588 if (mode
& P9_STAT_MODE_DIR
) {
593 if (mode
& P9_STAT_MODE_SYMLINK
) {
596 if (mode
& P9_STAT_MODE_SOCKET
) {
599 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
602 if (mode
& P9_STAT_MODE_DEVICE
) {
603 if (extension
&& extension
->data
[0] == 'c') {
615 if (mode
& P9_STAT_MODE_SETUID
) {
618 if (mode
& P9_STAT_MODE_SETGID
) {
621 if (mode
& P9_STAT_MODE_SETVTX
) {
628 static int donttouch_stat(V9fsStat
*stat
)
630 if (stat
->type
== -1 &&
632 stat
->qid
.type
== -1 &&
633 stat
->qid
.version
== -1 &&
634 stat
->qid
.path
== -1 &&
638 stat
->length
== -1 &&
645 stat
->n_muid
== -1) {
652 static void v9fs_stat_free(V9fsStat
*stat
)
654 v9fs_string_free(&stat
->name
);
655 v9fs_string_free(&stat
->uid
);
656 v9fs_string_free(&stat
->gid
);
657 v9fs_string_free(&stat
->muid
);
658 v9fs_string_free(&stat
->extension
);
661 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
665 mode
= stbuf
->st_mode
& 0777;
666 if (S_ISDIR(stbuf
->st_mode
)) {
667 mode
|= P9_STAT_MODE_DIR
;
671 if (S_ISLNK(stbuf
->st_mode
)) {
672 mode
|= P9_STAT_MODE_SYMLINK
;
675 if (S_ISSOCK(stbuf
->st_mode
)) {
676 mode
|= P9_STAT_MODE_SOCKET
;
679 if (S_ISFIFO(stbuf
->st_mode
)) {
680 mode
|= P9_STAT_MODE_NAMED_PIPE
;
683 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
684 mode
|= P9_STAT_MODE_DEVICE
;
687 if (stbuf
->st_mode
& S_ISUID
) {
688 mode
|= P9_STAT_MODE_SETUID
;
691 if (stbuf
->st_mode
& S_ISGID
) {
692 mode
|= P9_STAT_MODE_SETGID
;
695 if (stbuf
->st_mode
& S_ISVTX
) {
696 mode
|= P9_STAT_MODE_SETVTX
;
703 static int stat_to_v9stat(V9fsState
*s
, V9fsString
*name
,
704 const struct stat
*stbuf
,
710 memset(v9stat
, 0, sizeof(*v9stat
));
712 stat_to_qid(stbuf
, &v9stat
->qid
);
713 v9stat
->mode
= stat_to_v9mode(stbuf
);
714 v9stat
->atime
= stbuf
->st_atime
;
715 v9stat
->mtime
= stbuf
->st_mtime
;
716 v9stat
->length
= stbuf
->st_size
;
718 v9fs_string_null(&v9stat
->uid
);
719 v9fs_string_null(&v9stat
->gid
);
720 v9fs_string_null(&v9stat
->muid
);
723 v9stat
->n_uid
= stbuf
->st_uid
;
724 v9stat
->n_gid
= stbuf
->st_gid
;
727 v9fs_string_null(&v9stat
->extension
);
729 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
730 err
= v9fs_do_readlink(s
, name
, &v9stat
->extension
);
735 v9stat
->extension
.data
[err
] = 0;
736 v9stat
->extension
.size
= err
;
737 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
738 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
739 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
740 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
741 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
742 v9fs_string_sprintf(&v9stat
->extension
, "%s %u",
743 "HARDLINKCOUNT", stbuf
->st_nlink
);
747 str
= strrchr(name
->data
, '/');
754 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
757 v9fs_string_size(&v9stat
->name
) +
758 v9fs_string_size(&v9stat
->uid
) +
759 v9fs_string_size(&v9stat
->gid
) +
760 v9fs_string_size(&v9stat
->muid
) +
761 v9fs_string_size(&v9stat
->extension
);
765 static struct iovec
*adjust_sg(struct iovec
*sg
, int len
, int *iovcnt
)
767 while (len
&& *iovcnt
) {
768 if (len
< sg
->iov_len
) {
782 static struct iovec
*cap_sg(struct iovec
*sg
, int cap
, int *cnt
)
787 for (i
= 0; i
< *cnt
; i
++) {
788 if ((total
+ sg
[i
].iov_len
) > cap
) {
789 sg
[i
].iov_len
-= ((total
+ sg
[i
].iov_len
) - cap
);
793 total
+= sg
[i
].iov_len
;
801 static void print_sg(struct iovec
*sg
, int cnt
)
805 printf("sg[%d]: {", cnt
);
806 for (i
= 0; i
< cnt
; i
++) {
810 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
815 static void v9fs_dummy(V9fsState
*s
, V9fsPDU
*pdu
)
817 /* Note: The following have been added to prevent GCC from complaining
818 * They will be removed in the subsequent patches */
821 (void) v9fs_string_init
;
822 (void) v9fs_string_free
;
823 (void) v9fs_string_null
;
824 (void) v9fs_string_sprintf
;
825 (void) v9fs_string_copy
;
826 (void) v9fs_string_size
;
827 (void) v9fs_do_lstat
;
828 (void) v9fs_do_setuid
;
829 (void) v9fs_do_readlink
;
830 (void) v9fs_do_close
;
831 (void) v9fs_do_closedir
;
835 (void) v9mode_to_mode
;
836 (void) donttouch_stat
;
837 (void) v9fs_stat_free
;
838 (void) stat_to_v9stat
;
844 static void v9fs_version(V9fsState
*s
, V9fsPDU
*pdu
)
850 pdu_unmarshal(pdu
, offset
, "ds", &msize
, &version
);
852 if (strcmp(version
.data
, "9P2000.u")) {
853 v9fs_string_sprintf(&version
, "unknown");
856 offset
+= pdu_marshal(pdu
, offset
, "ds", msize
, &version
);
857 complete_pdu(s
, pdu
, offset
);
859 v9fs_string_free(&version
);
862 static void v9fs_attach(V9fsState
*s
, V9fsPDU
*pdu
)
864 int32_t fid
, afid
, n_uname
;
865 V9fsString uname
, aname
;
871 pdu_unmarshal(pdu
, offset
, "ddssd", &fid
, &afid
, &uname
, &aname
, &n_uname
);
873 fidp
= alloc_fid(s
, fid
);
881 v9fs_string_sprintf(&fidp
->path
, "%s", "/");
882 err
= fid_to_qid(s
, fidp
, &qid
);
889 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
893 complete_pdu(s
, pdu
, err
);
894 v9fs_string_free(&uname
);
895 v9fs_string_free(&aname
);
898 typedef struct V9fsStatState
{
906 static void v9fs_stat_post_lstat(V9fsState
*s
, V9fsStatState
*vs
, int err
)
913 err
= stat_to_v9stat(s
, &vs
->fidp
->path
, &vs
->stbuf
, &vs
->v9stat
);
917 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "wS", 0, &vs
->v9stat
);
921 complete_pdu(s
, vs
->pdu
, err
);
922 v9fs_stat_free(&vs
->v9stat
);
926 static void v9fs_stat(V9fsState
*s
, V9fsPDU
*pdu
)
932 vs
= qemu_malloc(sizeof(*vs
));
936 memset(&vs
->v9stat
, 0, sizeof(vs
->v9stat
));
938 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &fid
);
940 vs
->fidp
= lookup_fid(s
, fid
);
941 if (vs
->fidp
== NULL
) {
946 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
947 v9fs_stat_post_lstat(s
, vs
, err
);
951 complete_pdu(s
, vs
->pdu
, err
);
952 v9fs_stat_free(&vs
->v9stat
);
956 typedef struct V9fsWalkState
{
963 V9fsFidState
*newfidp
;
969 static void v9fs_walk_complete(V9fsState
*s
, V9fsWalkState
*vs
, int err
)
971 complete_pdu(s
, vs
->pdu
, err
);
974 for (vs
->name_idx
= 0; vs
->name_idx
< vs
->nwnames
; vs
->name_idx
++) {
975 v9fs_string_free(&vs
->wnames
[vs
->name_idx
]);
978 qemu_free(vs
->wnames
);
983 static void v9fs_walk_marshal(V9fsWalkState
*vs
)
987 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "w", vs
->nwnames
);
989 for (i
= 0; i
< vs
->nwnames
; i
++) {
990 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qids
[i
]);
994 static void v9fs_walk_post_newfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
998 free_fid(s
, vs
->newfidp
->fid
);
999 v9fs_string_free(&vs
->path
);
1004 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1007 if (vs
->name_idx
< vs
->nwnames
) {
1008 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1009 vs
->wnames
[vs
->name_idx
].data
);
1010 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1012 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1013 v9fs_walk_post_newfid_lstat(s
, vs
, err
);
1017 v9fs_string_free(&vs
->path
);
1018 v9fs_walk_marshal(vs
);
1021 v9fs_walk_complete(s
, vs
, err
);
1024 static void v9fs_walk_post_oldfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
1028 v9fs_string_free(&vs
->path
);
1033 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1035 if (vs
->name_idx
< vs
->nwnames
) {
1037 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1038 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1039 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1041 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1042 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1046 v9fs_string_free(&vs
->path
);
1047 v9fs_walk_marshal(vs
);
1050 v9fs_walk_complete(s
, vs
, err
);
1053 static void v9fs_walk(V9fsState
*s
, V9fsPDU
*pdu
)
1055 int32_t fid
, newfid
;
1060 vs
= qemu_malloc(sizeof(*vs
));
1066 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "ddw", &fid
,
1067 &newfid
, &vs
->nwnames
);
1070 vs
->wnames
= qemu_mallocz(sizeof(vs
->wnames
[0]) * vs
->nwnames
);
1072 vs
->qids
= qemu_mallocz(sizeof(vs
->qids
[0]) * vs
->nwnames
);
1074 for (i
= 0; i
< vs
->nwnames
; i
++) {
1075 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "s",
1080 vs
->fidp
= lookup_fid(s
, fid
);
1081 if (vs
->fidp
== NULL
) {
1086 /* FIXME: is this really valid? */
1087 if (fid
== newfid
) {
1089 BUG_ON(vs
->fidp
->fd
!= -1);
1090 BUG_ON(vs
->fidp
->dir
);
1091 v9fs_string_init(&vs
->path
);
1094 if (vs
->name_idx
< vs
->nwnames
) {
1095 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1096 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1097 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1099 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1100 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1104 vs
->newfidp
= alloc_fid(s
, newfid
);
1105 if (vs
->newfidp
== NULL
) {
1110 vs
->newfidp
->uid
= vs
->fidp
->uid
;
1111 v9fs_string_init(&vs
->path
);
1113 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->fidp
->path
);
1115 if (vs
->name_idx
< vs
->nwnames
) {
1116 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1117 vs
->wnames
[vs
->name_idx
].data
);
1118 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1120 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1121 v9fs_walk_post_newfid_lstat(s
, vs
, err
);
1126 v9fs_walk_marshal(vs
);
1129 v9fs_walk_complete(s
, vs
, err
);
1132 static void v9fs_clunk(V9fsState
*s
, V9fsPDU
*pdu
)
1139 static void v9fs_open(V9fsState
*s
, V9fsPDU
*pdu
)
1140 { if (debug_9p_pdu
) {
1145 static void v9fs_read(V9fsState
*s
, V9fsPDU
*pdu
)
1152 static void v9fs_write(V9fsState
*s
, V9fsPDU
*pdu
)
1159 static void v9fs_create(V9fsState
*s
, V9fsPDU
*pdu
)
1166 static void v9fs_flush(V9fsState
*s
, V9fsPDU
*pdu
)
1174 static void v9fs_remove(V9fsState
*s
, V9fsPDU
*pdu
)
1181 static void v9fs_wstat(V9fsState
*s
, V9fsPDU
*pdu
)
1188 typedef void (pdu_handler_t
)(V9fsState
*s
, V9fsPDU
*pdu
);
1190 static pdu_handler_t
*pdu_handlers
[] = {
1191 [P9_TVERSION
] = v9fs_version
,
1192 [P9_TATTACH
] = v9fs_attach
,
1193 [P9_TSTAT
] = v9fs_stat
,
1194 [P9_TWALK
] = v9fs_walk
,
1195 [P9_TCLUNK
] = v9fs_clunk
,
1196 [P9_TOPEN
] = v9fs_open
,
1197 [P9_TREAD
] = v9fs_read
,
1199 [P9_TAUTH
] = v9fs_auth
,
1201 [P9_TFLUSH
] = v9fs_flush
,
1202 [P9_TCREATE
] = v9fs_create
,
1203 [P9_TWRITE
] = v9fs_write
,
1204 [P9_TWSTAT
] = v9fs_wstat
,
1205 [P9_TREMOVE
] = v9fs_remove
,
1208 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
1210 pdu_handler_t
*handler
;
1216 BUG_ON(pdu
->id
>= ARRAY_SIZE(pdu_handlers
));
1218 handler
= pdu_handlers
[pdu
->id
];
1219 BUG_ON(handler
== NULL
);
1224 static void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
1226 V9fsState
*s
= (V9fsState
*)vdev
;
1230 while ((pdu
= alloc_pdu(s
)) &&
1231 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
1234 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
1235 BUG_ON(pdu
->elem
.out_sg
[0].iov_len
< 7);
1237 ptr
= pdu
->elem
.out_sg
[0].iov_base
;
1239 memcpy(&pdu
->size
, ptr
, 4);
1241 memcpy(&pdu
->tag
, ptr
+ 5, 2);
1249 static uint32_t virtio_9p_get_features(VirtIODevice
*vdev
, uint32_t features
)
1251 features
|= 1 << VIRTIO_9P_MOUNT_TAG
;
1255 static V9fsState
*to_virtio_9p(VirtIODevice
*vdev
)
1257 return (V9fsState
*)vdev
;
1260 static void virtio_9p_get_config(VirtIODevice
*vdev
, uint8_t *config
)
1262 struct virtio_9p_config
*cfg
;
1263 V9fsState
*s
= to_virtio_9p(vdev
);
1265 cfg
= qemu_mallocz(sizeof(struct virtio_9p_config
) +
1267 stw_raw(&cfg
->tag_len
, s
->tag_len
);
1268 memcpy(cfg
->tag
, s
->tag
, s
->tag_len
);
1269 memcpy(config
, cfg
, s
->config_size
);
1273 VirtIODevice
*virtio_9p_init(DeviceState
*dev
, V9fsConf
*conf
)
1281 s
= (V9fsState
*)virtio_common_init("virtio-9p",
1283 sizeof(struct virtio_9p_config
)+
1287 /* initialize pdu allocator */
1288 QLIST_INIT(&s
->free_list
);
1289 for (i
= 0; i
< (MAX_REQ
- 1); i
++) {
1290 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
1293 s
->vq
= virtio_add_queue(&s
->vdev
, MAX_REQ
, handle_9p_output
);
1295 fse
= get_fsdev_fsentry(conf
->fsdev_id
);
1298 /* We don't have a fsdev identified by fsdev_id */
1299 fprintf(stderr
, "Virtio-9p device couldn't find fsdev "
1300 "with the id %s\n", conf
->fsdev_id
);
1304 if (!fse
->path
|| !conf
->tag
) {
1305 /* we haven't specified a mount_tag or the path */
1306 fprintf(stderr
, "fsdev with id %s needs path "
1307 "and Virtio-9p device needs mount_tag arguments\n",
1312 if (lstat(fse
->path
, &stat
)) {
1313 fprintf(stderr
, "share path %s does not exist\n", fse
->path
);
1315 } else if (!S_ISDIR(stat
.st_mode
)) {
1316 fprintf(stderr
, "share path %s is not a directory \n", fse
->path
);
1320 s
->ctx
.fs_root
= qemu_strdup(fse
->path
);
1321 len
= strlen(conf
->tag
);
1322 if (len
> MAX_TAG_LEN
) {
1325 /* s->tag is non-NULL terminated string */
1326 s
->tag
= qemu_malloc(len
);
1327 memcpy(s
->tag
, conf
->tag
, len
);
1332 s
->vdev
.get_features
= virtio_9p_get_features
;
1333 s
->config_size
= sizeof(struct virtio_9p_config
) +
1335 s
->vdev
.get_config
= virtio_9p_get_config
;