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"
35 static int omode_to_uflags(int8_t mode
)
69 void cred_init(FsCred
*credp
)
77 static int v9fs_do_lstat(V9fsState
*s
, V9fsString
*path
, struct stat
*stbuf
)
79 return s
->ops
->lstat(&s
->ctx
, path
->data
, stbuf
);
82 static ssize_t
v9fs_do_readlink(V9fsState
*s
, V9fsString
*path
, V9fsString
*buf
)
86 buf
->data
= qemu_malloc(1024);
88 len
= s
->ops
->readlink(&s
->ctx
, path
->data
, buf
->data
, 1024 - 1);
97 static int v9fs_do_close(V9fsState
*s
, int fd
)
99 return s
->ops
->close(&s
->ctx
, fd
);
102 static int v9fs_do_closedir(V9fsState
*s
, DIR *dir
)
104 return s
->ops
->closedir(&s
->ctx
, dir
);
107 static int v9fs_do_open(V9fsState
*s
, V9fsString
*path
, int flags
)
109 return s
->ops
->open(&s
->ctx
, path
->data
, flags
);
112 static DIR *v9fs_do_opendir(V9fsState
*s
, V9fsString
*path
)
114 return s
->ops
->opendir(&s
->ctx
, path
->data
);
117 static void v9fs_do_rewinddir(V9fsState
*s
, DIR *dir
)
119 return s
->ops
->rewinddir(&s
->ctx
, dir
);
122 static off_t
v9fs_do_telldir(V9fsState
*s
, DIR *dir
)
124 return s
->ops
->telldir(&s
->ctx
, dir
);
127 static struct dirent
*v9fs_do_readdir(V9fsState
*s
, DIR *dir
)
129 return s
->ops
->readdir(&s
->ctx
, dir
);
132 static void v9fs_do_seekdir(V9fsState
*s
, DIR *dir
, off_t off
)
134 return s
->ops
->seekdir(&s
->ctx
, dir
, off
);
137 static int v9fs_do_readv(V9fsState
*s
, int fd
, const struct iovec
*iov
,
140 return s
->ops
->readv(&s
->ctx
, fd
, iov
, iovcnt
);
143 static off_t
v9fs_do_lseek(V9fsState
*s
, int fd
, off_t offset
, int whence
)
145 return s
->ops
->lseek(&s
->ctx
, fd
, offset
, whence
);
148 static int v9fs_do_writev(V9fsState
*s
, int fd
, const struct iovec
*iov
,
151 return s
->ops
->writev(&s
->ctx
, fd
, iov
, iovcnt
);
154 static int v9fs_do_chmod(V9fsState
*s
, V9fsString
*path
, mode_t mode
)
159 return s
->ops
->chmod(&s
->ctx
, path
->data
, &cred
);
162 static int v9fs_do_mknod(V9fsState
*s
, char *name
,
163 mode_t mode
, dev_t dev
, uid_t uid
, gid_t gid
)
171 return s
->ops
->mknod(&s
->ctx
, name
, &cred
);
174 static int v9fs_do_mkdir(V9fsState
*s
, char *name
, mode_t mode
,
175 uid_t uid
, gid_t gid
)
184 return s
->ops
->mkdir(&s
->ctx
, name
, &cred
);
187 static int v9fs_do_fstat(V9fsState
*s
, int fd
, struct stat
*stbuf
)
189 return s
->ops
->fstat(&s
->ctx
, fd
, stbuf
);
192 static int v9fs_do_open2(V9fsState
*s
, char *fullname
, uid_t uid
, gid_t gid
,
200 cred
.fc_mode
= mode
& 07777;
203 return s
->ops
->open2(&s
->ctx
, fullname
, flags
, &cred
);
206 static int v9fs_do_symlink(V9fsState
*s
, V9fsFidState
*fidp
,
207 const char *oldpath
, const char *newpath
, gid_t gid
)
211 cred
.fc_uid
= fidp
->uid
;
215 return s
->ops
->symlink(&s
->ctx
, oldpath
, newpath
, &cred
);
218 static int v9fs_do_link(V9fsState
*s
, V9fsString
*oldpath
, V9fsString
*newpath
)
220 return s
->ops
->link(&s
->ctx
, oldpath
->data
, newpath
->data
);
223 static int v9fs_do_truncate(V9fsState
*s
, V9fsString
*path
, off_t size
)
225 return s
->ops
->truncate(&s
->ctx
, path
->data
, size
);
228 static int v9fs_do_rename(V9fsState
*s
, V9fsString
*oldpath
,
231 return s
->ops
->rename(&s
->ctx
, oldpath
->data
, newpath
->data
);
234 static int v9fs_do_chown(V9fsState
*s
, V9fsString
*path
, uid_t uid
, gid_t gid
)
241 return s
->ops
->chown(&s
->ctx
, path
->data
, &cred
);
244 static int v9fs_do_utimensat(V9fsState
*s
, V9fsString
*path
,
245 const struct timespec times
[2])
247 return s
->ops
->utimensat(&s
->ctx
, path
->data
, times
);
250 static int v9fs_do_remove(V9fsState
*s
, V9fsString
*path
)
252 return s
->ops
->remove(&s
->ctx
, path
->data
);
255 static int v9fs_do_fsync(V9fsState
*s
, int fd
)
257 return s
->ops
->fsync(&s
->ctx
, fd
);
260 static int v9fs_do_statfs(V9fsState
*s
, V9fsString
*path
, struct statfs
*stbuf
)
262 return s
->ops
->statfs(&s
->ctx
, path
->data
, stbuf
);
265 static ssize_t
v9fs_do_lgetxattr(V9fsState
*s
, V9fsString
*path
,
266 V9fsString
*xattr_name
,
267 void *value
, size_t size
)
269 return s
->ops
->lgetxattr(&s
->ctx
, path
->data
,
270 xattr_name
->data
, value
, size
);
273 static ssize_t
v9fs_do_llistxattr(V9fsState
*s
, V9fsString
*path
,
274 void *value
, size_t size
)
276 return s
->ops
->llistxattr(&s
->ctx
, path
->data
,
280 static int v9fs_do_lsetxattr(V9fsState
*s
, V9fsString
*path
,
281 V9fsString
*xattr_name
,
282 void *value
, size_t size
, int flags
)
284 return s
->ops
->lsetxattr(&s
->ctx
, path
->data
,
285 xattr_name
->data
, value
, size
, flags
);
288 static int v9fs_do_lremovexattr(V9fsState
*s
, V9fsString
*path
,
289 V9fsString
*xattr_name
)
291 return s
->ops
->lremovexattr(&s
->ctx
, path
->data
,
296 static void v9fs_string_init(V9fsString
*str
)
302 static void v9fs_string_free(V9fsString
*str
)
304 qemu_free(str
->data
);
309 static void v9fs_string_null(V9fsString
*str
)
311 v9fs_string_free(str
);
314 static int number_to_string(void *arg
, char type
)
316 unsigned int ret
= 0;
320 unsigned int num
= *(unsigned int *)arg
;
329 printf("Number_to_string: Unknown number format\n");
336 static int v9fs_string_alloc_printf(char **strp
, const char *fmt
, va_list ap
)
339 char *iter
= (char *)fmt
;
343 unsigned int arg_uint
;
345 /* Find the number of %'s that denotes an argument */
346 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
351 len
= strlen(fmt
) - 2*nr_args
;
361 /* Now parse the format string */
362 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
366 arg_uint
= va_arg(ap2
, unsigned int);
367 len
+= number_to_string((void *)&arg_uint
, 'u');
370 arg_char_ptr
= va_arg(ap2
, char *);
371 len
+= strlen(arg_char_ptr
);
378 "v9fs_string_alloc_printf:Incorrect format %c", *iter
);
385 *strp
= qemu_malloc((len
+ 1) * sizeof(**strp
));
387 return vsprintf(*strp
, fmt
, ap
);
390 static void v9fs_string_sprintf(V9fsString
*str
, const char *fmt
, ...)
395 v9fs_string_free(str
);
398 err
= v9fs_string_alloc_printf(&str
->data
, fmt
, ap
);
405 static void v9fs_string_copy(V9fsString
*lhs
, V9fsString
*rhs
)
407 v9fs_string_free(lhs
);
408 v9fs_string_sprintf(lhs
, "%s", rhs
->data
);
411 static size_t v9fs_string_size(V9fsString
*str
)
416 static V9fsFidState
*lookup_fid(V9fsState
*s
, int32_t fid
)
420 for (f
= s
->fid_list
; f
; f
= f
->next
) {
429 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
433 f
= lookup_fid(s
, fid
);
438 f
= qemu_mallocz(sizeof(V9fsFidState
));
441 f
->fid_type
= P9_FID_NONE
;
443 f
->next
= s
->fid_list
;
449 static int v9fs_xattr_fid_clunk(V9fsState
*s
, V9fsFidState
*fidp
)
453 if (fidp
->fs
.xattr
.copied_len
== -1) {
454 /* getxattr/listxattr fid */
458 * if this is fid for setxattr. clunk should
459 * result in setxattr localcall
461 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
462 /* clunk after partial write */
466 if (fidp
->fs
.xattr
.len
) {
467 retval
= v9fs_do_lsetxattr(s
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
468 fidp
->fs
.xattr
.value
,
470 fidp
->fs
.xattr
.flags
);
472 retval
= v9fs_do_lremovexattr(s
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
475 v9fs_string_free(&fidp
->fs
.xattr
.name
);
477 if (fidp
->fs
.xattr
.value
) {
478 qemu_free(fidp
->fs
.xattr
.value
);
483 static int free_fid(V9fsState
*s
, int32_t fid
)
486 V9fsFidState
**fidpp
, *fidp
;
488 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
489 if ((*fidpp
)->fid
== fid
) {
494 if (*fidpp
== NULL
) {
501 if (fidp
->fid_type
== P9_FID_FILE
) {
502 v9fs_do_close(s
, fidp
->fs
.fd
);
503 } else if (fidp
->fid_type
== P9_FID_DIR
) {
504 v9fs_do_closedir(s
, fidp
->fs
.dir
);
505 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
506 retval
= v9fs_xattr_fid_clunk(s
, fidp
);
508 v9fs_string_free(&fidp
->path
);
514 #define P9_QID_TYPE_DIR 0x80
515 #define P9_QID_TYPE_SYMLINK 0x02
517 #define P9_STAT_MODE_DIR 0x80000000
518 #define P9_STAT_MODE_APPEND 0x40000000
519 #define P9_STAT_MODE_EXCL 0x20000000
520 #define P9_STAT_MODE_MOUNT 0x10000000
521 #define P9_STAT_MODE_AUTH 0x08000000
522 #define P9_STAT_MODE_TMP 0x04000000
523 #define P9_STAT_MODE_SYMLINK 0x02000000
524 #define P9_STAT_MODE_LINK 0x01000000
525 #define P9_STAT_MODE_DEVICE 0x00800000
526 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
527 #define P9_STAT_MODE_SOCKET 0x00100000
528 #define P9_STAT_MODE_SETUID 0x00080000
529 #define P9_STAT_MODE_SETGID 0x00040000
530 #define P9_STAT_MODE_SETVTX 0x00010000
532 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
533 P9_STAT_MODE_SYMLINK | \
534 P9_STAT_MODE_LINK | \
535 P9_STAT_MODE_DEVICE | \
536 P9_STAT_MODE_NAMED_PIPE | \
539 /* This is the algorithm from ufs in spfs */
540 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
544 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
545 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
546 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
548 if (S_ISDIR(stbuf
->st_mode
)) {
549 qidp
->type
|= P9_QID_TYPE_DIR
;
551 if (S_ISLNK(stbuf
->st_mode
)) {
552 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
556 static int fid_to_qid(V9fsState
*s
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
561 err
= v9fs_do_lstat(s
, &fidp
->path
, &stbuf
);
566 stat_to_qid(&stbuf
, qidp
);
570 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
574 if (!QLIST_EMPTY(&s
->free_list
)) {
575 pdu
= QLIST_FIRST(&s
->free_list
);
576 QLIST_REMOVE(pdu
, next
);
581 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
584 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
588 size_t pdu_packunpack(void *addr
, struct iovec
*sg
, int sg_count
,
589 size_t offset
, size_t size
, int pack
)
594 for (i
= 0; size
&& i
< sg_count
; i
++) {
596 if (offset
>= sg
[i
].iov_len
) {
598 offset
-= sg
[i
].iov_len
;
601 len
= MIN(sg
[i
].iov_len
- offset
, size
);
603 memcpy(sg
[i
].iov_base
+ offset
, addr
, len
);
605 memcpy(addr
, sg
[i
].iov_base
+ offset
, len
);
620 static size_t pdu_unpack(void *dst
, V9fsPDU
*pdu
, size_t offset
, size_t size
)
622 return pdu_packunpack(dst
, pdu
->elem
.out_sg
, pdu
->elem
.out_num
,
626 static size_t pdu_pack(V9fsPDU
*pdu
, size_t offset
, const void *src
,
629 return pdu_packunpack((void *)src
, pdu
->elem
.in_sg
, pdu
->elem
.in_num
,
633 static int pdu_copy_sg(V9fsPDU
*pdu
, size_t offset
, int rx
, struct iovec
*sg
)
637 struct iovec
*src_sg
;
641 src_sg
= pdu
->elem
.in_sg
;
642 num
= pdu
->elem
.in_num
;
644 src_sg
= pdu
->elem
.out_sg
;
645 num
= pdu
->elem
.out_num
;
649 for (i
= 0; i
< num
; i
++) {
651 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
652 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
654 } else if (offset
< (src_sg
[i
].iov_len
+ pos
)) {
655 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
656 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
657 sg
[j
].iov_base
+= (offset
- pos
);
658 sg
[j
].iov_len
-= (offset
- pos
);
661 pos
+= src_sg
[i
].iov_len
;
667 static size_t pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
669 size_t old_offset
= offset
;
674 for (i
= 0; fmt
[i
]; i
++) {
677 uint8_t *valp
= va_arg(ap
, uint8_t *);
678 offset
+= pdu_unpack(valp
, pdu
, offset
, sizeof(*valp
));
683 valp
= va_arg(ap
, uint16_t *);
684 val
= le16_to_cpupu(valp
);
685 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
691 valp
= va_arg(ap
, uint32_t *);
692 val
= le32_to_cpupu(valp
);
693 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
699 valp
= va_arg(ap
, uint64_t *);
700 val
= le64_to_cpup(valp
);
701 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
706 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
707 int *iovcnt
= va_arg(ap
, int *);
708 *iovcnt
= pdu_copy_sg(pdu
, offset
, 0, iov
);
712 V9fsString
*str
= va_arg(ap
, V9fsString
*);
713 offset
+= pdu_unmarshal(pdu
, offset
, "w", &str
->size
);
714 /* FIXME: sanity check str->size */
715 str
->data
= qemu_malloc(str
->size
+ 1);
716 offset
+= pdu_unpack(str
->data
, pdu
, offset
, str
->size
);
717 str
->data
[str
->size
] = 0;
721 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
722 offset
+= pdu_unmarshal(pdu
, offset
, "bdq",
723 &qidp
->type
, &qidp
->version
, &qidp
->path
);
727 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
728 offset
+= pdu_unmarshal(pdu
, offset
, "wwdQdddqsssssddd",
729 &statp
->size
, &statp
->type
, &statp
->dev
,
730 &statp
->qid
, &statp
->mode
, &statp
->atime
,
731 &statp
->mtime
, &statp
->length
,
732 &statp
->name
, &statp
->uid
, &statp
->gid
,
733 &statp
->muid
, &statp
->extension
,
734 &statp
->n_uid
, &statp
->n_gid
,
739 V9fsIattr
*iattr
= va_arg(ap
, V9fsIattr
*);
740 offset
+= pdu_unmarshal(pdu
, offset
, "ddddqqqqq",
741 &iattr
->valid
, &iattr
->mode
,
742 &iattr
->uid
, &iattr
->gid
, &iattr
->size
,
743 &iattr
->atime_sec
, &iattr
->atime_nsec
,
744 &iattr
->mtime_sec
, &iattr
->mtime_nsec
);
754 return offset
- old_offset
;
757 static size_t pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
759 size_t old_offset
= offset
;
764 for (i
= 0; fmt
[i
]; i
++) {
767 uint8_t val
= va_arg(ap
, int);
768 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
773 cpu_to_le16w(&val
, va_arg(ap
, int));
774 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
779 cpu_to_le32w(&val
, va_arg(ap
, uint32_t));
780 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
785 cpu_to_le64w(&val
, va_arg(ap
, uint64_t));
786 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
790 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
791 int *iovcnt
= va_arg(ap
, int *);
792 *iovcnt
= pdu_copy_sg(pdu
, offset
, 1, iov
);
796 V9fsString
*str
= va_arg(ap
, V9fsString
*);
797 offset
+= pdu_marshal(pdu
, offset
, "w", str
->size
);
798 offset
+= pdu_pack(pdu
, offset
, str
->data
, str
->size
);
802 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
803 offset
+= pdu_marshal(pdu
, offset
, "bdq",
804 qidp
->type
, qidp
->version
, qidp
->path
);
808 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
809 offset
+= pdu_marshal(pdu
, offset
, "wwdQdddqsssssddd",
810 statp
->size
, statp
->type
, statp
->dev
,
811 &statp
->qid
, statp
->mode
, statp
->atime
,
812 statp
->mtime
, statp
->length
, &statp
->name
,
813 &statp
->uid
, &statp
->gid
, &statp
->muid
,
814 &statp
->extension
, statp
->n_uid
,
815 statp
->n_gid
, statp
->n_muid
);
819 V9fsStatDotl
*statp
= va_arg(ap
, V9fsStatDotl
*);
820 offset
+= pdu_marshal(pdu
, offset
, "qQdddqqqqqqqqqqqqqqq",
821 statp
->st_result_mask
,
822 &statp
->qid
, statp
->st_mode
,
823 statp
->st_uid
, statp
->st_gid
,
824 statp
->st_nlink
, statp
->st_rdev
,
825 statp
->st_size
, statp
->st_blksize
, statp
->st_blocks
,
826 statp
->st_atime_sec
, statp
->st_atime_nsec
,
827 statp
->st_mtime_sec
, statp
->st_mtime_nsec
,
828 statp
->st_ctime_sec
, statp
->st_ctime_nsec
,
829 statp
->st_btime_sec
, statp
->st_btime_nsec
,
830 statp
->st_gen
, statp
->st_data_version
);
839 return offset
- old_offset
;
842 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
844 int8_t id
= pdu
->id
+ 1; /* Response */
850 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
853 str
.data
= strerror(err
);
854 str
.size
= strlen(str
.data
);
856 len
+= pdu_marshal(pdu
, len
, "s", &str
);
860 len
+= pdu_marshal(pdu
, len
, "d", err
);
862 if (s
->proto_version
== V9FS_PROTO_2000L
) {
867 /* fill out the header */
868 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
870 /* keep these in sync */
874 /* push onto queue and notify */
875 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
877 /* FIXME: we should batch these completions */
878 virtio_notify(&s
->vdev
, s
->vq
);
883 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
888 if (mode
& P9_STAT_MODE_DIR
) {
892 if (mode
& P9_STAT_MODE_SYMLINK
) {
895 if (mode
& P9_STAT_MODE_SOCKET
) {
898 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
901 if (mode
& P9_STAT_MODE_DEVICE
) {
902 if (extension
&& extension
->data
[0] == 'c') {
913 if (mode
& P9_STAT_MODE_SETUID
) {
916 if (mode
& P9_STAT_MODE_SETGID
) {
919 if (mode
& P9_STAT_MODE_SETVTX
) {
926 static int donttouch_stat(V9fsStat
*stat
)
928 if (stat
->type
== -1 &&
930 stat
->qid
.type
== -1 &&
931 stat
->qid
.version
== -1 &&
932 stat
->qid
.path
== -1 &&
936 stat
->length
== -1 &&
943 stat
->n_muid
== -1) {
950 static void v9fs_stat_free(V9fsStat
*stat
)
952 v9fs_string_free(&stat
->name
);
953 v9fs_string_free(&stat
->uid
);
954 v9fs_string_free(&stat
->gid
);
955 v9fs_string_free(&stat
->muid
);
956 v9fs_string_free(&stat
->extension
);
959 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
963 mode
= stbuf
->st_mode
& 0777;
964 if (S_ISDIR(stbuf
->st_mode
)) {
965 mode
|= P9_STAT_MODE_DIR
;
968 if (S_ISLNK(stbuf
->st_mode
)) {
969 mode
|= P9_STAT_MODE_SYMLINK
;
972 if (S_ISSOCK(stbuf
->st_mode
)) {
973 mode
|= P9_STAT_MODE_SOCKET
;
976 if (S_ISFIFO(stbuf
->st_mode
)) {
977 mode
|= P9_STAT_MODE_NAMED_PIPE
;
980 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
981 mode
|= P9_STAT_MODE_DEVICE
;
984 if (stbuf
->st_mode
& S_ISUID
) {
985 mode
|= P9_STAT_MODE_SETUID
;
988 if (stbuf
->st_mode
& S_ISGID
) {
989 mode
|= P9_STAT_MODE_SETGID
;
992 if (stbuf
->st_mode
& S_ISVTX
) {
993 mode
|= P9_STAT_MODE_SETVTX
;
999 static int stat_to_v9stat(V9fsState
*s
, V9fsString
*name
,
1000 const struct stat
*stbuf
,
1006 memset(v9stat
, 0, sizeof(*v9stat
));
1008 stat_to_qid(stbuf
, &v9stat
->qid
);
1009 v9stat
->mode
= stat_to_v9mode(stbuf
);
1010 v9stat
->atime
= stbuf
->st_atime
;
1011 v9stat
->mtime
= stbuf
->st_mtime
;
1012 v9stat
->length
= stbuf
->st_size
;
1014 v9fs_string_null(&v9stat
->uid
);
1015 v9fs_string_null(&v9stat
->gid
);
1016 v9fs_string_null(&v9stat
->muid
);
1018 v9stat
->n_uid
= stbuf
->st_uid
;
1019 v9stat
->n_gid
= stbuf
->st_gid
;
1022 v9fs_string_null(&v9stat
->extension
);
1024 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
1025 err
= v9fs_do_readlink(s
, name
, &v9stat
->extension
);
1030 v9stat
->extension
.data
[err
] = 0;
1031 v9stat
->extension
.size
= err
;
1032 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
1033 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
1034 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
1035 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
1036 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
1037 v9fs_string_sprintf(&v9stat
->extension
, "%s %u",
1038 "HARDLINKCOUNT", stbuf
->st_nlink
);
1041 str
= strrchr(name
->data
, '/');
1048 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
1051 v9fs_string_size(&v9stat
->name
) +
1052 v9fs_string_size(&v9stat
->uid
) +
1053 v9fs_string_size(&v9stat
->gid
) +
1054 v9fs_string_size(&v9stat
->muid
) +
1055 v9fs_string_size(&v9stat
->extension
);
1059 #define P9_STATS_MODE 0x00000001ULL
1060 #define P9_STATS_NLINK 0x00000002ULL
1061 #define P9_STATS_UID 0x00000004ULL
1062 #define P9_STATS_GID 0x00000008ULL
1063 #define P9_STATS_RDEV 0x00000010ULL
1064 #define P9_STATS_ATIME 0x00000020ULL
1065 #define P9_STATS_MTIME 0x00000040ULL
1066 #define P9_STATS_CTIME 0x00000080ULL
1067 #define P9_STATS_INO 0x00000100ULL
1068 #define P9_STATS_SIZE 0x00000200ULL
1069 #define P9_STATS_BLOCKS 0x00000400ULL
1071 #define P9_STATS_BTIME 0x00000800ULL
1072 #define P9_STATS_GEN 0x00001000ULL
1073 #define P9_STATS_DATA_VERSION 0x00002000ULL
1075 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1076 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1079 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
1080 V9fsStatDotl
*v9lstat
)
1082 memset(v9lstat
, 0, sizeof(*v9lstat
));
1084 v9lstat
->st_mode
= stbuf
->st_mode
;
1085 v9lstat
->st_nlink
= stbuf
->st_nlink
;
1086 v9lstat
->st_uid
= stbuf
->st_uid
;
1087 v9lstat
->st_gid
= stbuf
->st_gid
;
1088 v9lstat
->st_rdev
= stbuf
->st_rdev
;
1089 v9lstat
->st_size
= stbuf
->st_size
;
1090 v9lstat
->st_blksize
= stbuf
->st_blksize
;
1091 v9lstat
->st_blocks
= stbuf
->st_blocks
;
1092 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
1093 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
1094 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
1095 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
1096 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
1097 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
1098 /* Currently we only support BASIC fields in stat */
1099 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
1101 stat_to_qid(stbuf
, &v9lstat
->qid
);
1104 static struct iovec
*adjust_sg(struct iovec
*sg
, int len
, int *iovcnt
)
1106 while (len
&& *iovcnt
) {
1107 if (len
< sg
->iov_len
) {
1109 sg
->iov_base
+= len
;
1121 static struct iovec
*cap_sg(struct iovec
*sg
, int cap
, int *cnt
)
1126 for (i
= 0; i
< *cnt
; i
++) {
1127 if ((total
+ sg
[i
].iov_len
) > cap
) {
1128 sg
[i
].iov_len
-= ((total
+ sg
[i
].iov_len
) - cap
);
1132 total
+= sg
[i
].iov_len
;
1140 static void print_sg(struct iovec
*sg
, int cnt
)
1144 printf("sg[%d]: {", cnt
);
1145 for (i
= 0; i
< cnt
; i
++) {
1149 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
1154 static void v9fs_fix_path(V9fsString
*dst
, V9fsString
*src
, int len
)
1157 v9fs_string_init(&str
);
1158 v9fs_string_copy(&str
, dst
);
1159 v9fs_string_sprintf(dst
, "%s%s", src
->data
, str
.data
+len
);
1160 v9fs_string_free(&str
);
1163 static void v9fs_version(V9fsState
*s
, V9fsPDU
*pdu
)
1168 pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
1170 if (!strcmp(version
.data
, "9P2000.u")) {
1171 s
->proto_version
= V9FS_PROTO_2000U
;
1172 } else if (!strcmp(version
.data
, "9P2000.L")) {
1173 s
->proto_version
= V9FS_PROTO_2000L
;
1175 v9fs_string_sprintf(&version
, "unknown");
1178 offset
+= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
1179 complete_pdu(s
, pdu
, offset
);
1181 v9fs_string_free(&version
);
1184 static void v9fs_attach(V9fsState
*s
, V9fsPDU
*pdu
)
1186 int32_t fid
, afid
, n_uname
;
1187 V9fsString uname
, aname
;
1193 pdu_unmarshal(pdu
, offset
, "ddssd", &fid
, &afid
, &uname
, &aname
, &n_uname
);
1195 fidp
= alloc_fid(s
, fid
);
1201 fidp
->uid
= n_uname
;
1203 v9fs_string_sprintf(&fidp
->path
, "%s", "/");
1204 err
= fid_to_qid(s
, fidp
, &qid
);
1211 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
1215 complete_pdu(s
, pdu
, err
);
1216 v9fs_string_free(&uname
);
1217 v9fs_string_free(&aname
);
1220 static void v9fs_stat_post_lstat(V9fsState
*s
, V9fsStatState
*vs
, int err
)
1227 err
= stat_to_v9stat(s
, &vs
->fidp
->path
, &vs
->stbuf
, &vs
->v9stat
);
1231 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "wS", 0, &vs
->v9stat
);
1235 complete_pdu(s
, vs
->pdu
, err
);
1236 v9fs_stat_free(&vs
->v9stat
);
1240 static void v9fs_stat(V9fsState
*s
, V9fsPDU
*pdu
)
1246 vs
= qemu_malloc(sizeof(*vs
));
1250 memset(&vs
->v9stat
, 0, sizeof(vs
->v9stat
));
1252 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &fid
);
1254 vs
->fidp
= lookup_fid(s
, fid
);
1255 if (vs
->fidp
== NULL
) {
1260 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1261 v9fs_stat_post_lstat(s
, vs
, err
);
1265 complete_pdu(s
, vs
->pdu
, err
);
1266 v9fs_stat_free(&vs
->v9stat
);
1270 static void v9fs_getattr_post_lstat(V9fsState
*s
, V9fsStatStateDotl
*vs
,
1278 stat_to_v9stat_dotl(s
, &vs
->stbuf
, &vs
->v9stat_dotl
);
1279 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "A", &vs
->v9stat_dotl
);
1283 complete_pdu(s
, vs
->pdu
, err
);
1287 static void v9fs_getattr(V9fsState
*s
, V9fsPDU
*pdu
)
1290 V9fsStatStateDotl
*vs
;
1293 uint64_t request_mask
;
1295 vs
= qemu_malloc(sizeof(*vs
));
1299 memset(&vs
->v9stat_dotl
, 0, sizeof(vs
->v9stat_dotl
));
1301 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dq", &fid
, &request_mask
);
1303 fidp
= lookup_fid(s
, fid
);
1309 /* Currently we only support BASIC fields in stat, so there is no
1310 * need to look at request_mask.
1312 err
= v9fs_do_lstat(s
, &fidp
->path
, &vs
->stbuf
);
1313 v9fs_getattr_post_lstat(s
, vs
, err
);
1317 complete_pdu(s
, vs
->pdu
, err
);
1321 /* From Linux kernel code */
1322 #define ATTR_MODE (1 << 0)
1323 #define ATTR_UID (1 << 1)
1324 #define ATTR_GID (1 << 2)
1325 #define ATTR_SIZE (1 << 3)
1326 #define ATTR_ATIME (1 << 4)
1327 #define ATTR_MTIME (1 << 5)
1328 #define ATTR_CTIME (1 << 6)
1329 #define ATTR_MASK 127
1330 #define ATTR_ATIME_SET (1 << 7)
1331 #define ATTR_MTIME_SET (1 << 8)
1333 static void v9fs_setattr_post_truncate(V9fsState
*s
, V9fsSetattrState
*vs
,
1343 complete_pdu(s
, vs
->pdu
, err
);
1347 static void v9fs_setattr_post_chown(V9fsState
*s
, V9fsSetattrState
*vs
, int err
)
1354 if (vs
->v9iattr
.valid
& (ATTR_SIZE
)) {
1355 err
= v9fs_do_truncate(s
, &vs
->fidp
->path
, vs
->v9iattr
.size
);
1357 v9fs_setattr_post_truncate(s
, vs
, err
);
1361 complete_pdu(s
, vs
->pdu
, err
);
1365 static void v9fs_setattr_post_utimensat(V9fsState
*s
, V9fsSetattrState
*vs
,
1373 /* If the only valid entry in iattr is ctime we can call
1374 * chown(-1,-1) to update the ctime of the file
1376 if ((vs
->v9iattr
.valid
& (ATTR_UID
| ATTR_GID
)) ||
1377 ((vs
->v9iattr
.valid
& ATTR_CTIME
)
1378 && !((vs
->v9iattr
.valid
& ATTR_MASK
) & ~ATTR_CTIME
))) {
1379 if (!(vs
->v9iattr
.valid
& ATTR_UID
)) {
1380 vs
->v9iattr
.uid
= -1;
1382 if (!(vs
->v9iattr
.valid
& ATTR_GID
)) {
1383 vs
->v9iattr
.gid
= -1;
1385 err
= v9fs_do_chown(s
, &vs
->fidp
->path
, vs
->v9iattr
.uid
,
1388 v9fs_setattr_post_chown(s
, vs
, err
);
1392 complete_pdu(s
, vs
->pdu
, err
);
1396 static void v9fs_setattr_post_chmod(V9fsState
*s
, V9fsSetattrState
*vs
, int err
)
1403 if (vs
->v9iattr
.valid
& (ATTR_ATIME
| ATTR_MTIME
)) {
1404 struct timespec times
[2];
1405 if (vs
->v9iattr
.valid
& ATTR_ATIME
) {
1406 if (vs
->v9iattr
.valid
& ATTR_ATIME_SET
) {
1407 times
[0].tv_sec
= vs
->v9iattr
.atime_sec
;
1408 times
[0].tv_nsec
= vs
->v9iattr
.atime_nsec
;
1410 times
[0].tv_nsec
= UTIME_NOW
;
1413 times
[0].tv_nsec
= UTIME_OMIT
;
1416 if (vs
->v9iattr
.valid
& ATTR_MTIME
) {
1417 if (vs
->v9iattr
.valid
& ATTR_MTIME_SET
) {
1418 times
[1].tv_sec
= vs
->v9iattr
.mtime_sec
;
1419 times
[1].tv_nsec
= vs
->v9iattr
.mtime_nsec
;
1421 times
[1].tv_nsec
= UTIME_NOW
;
1424 times
[1].tv_nsec
= UTIME_OMIT
;
1426 err
= v9fs_do_utimensat(s
, &vs
->fidp
->path
, times
);
1428 v9fs_setattr_post_utimensat(s
, vs
, err
);
1432 complete_pdu(s
, vs
->pdu
, err
);
1436 static void v9fs_setattr(V9fsState
*s
, V9fsPDU
*pdu
)
1439 V9fsSetattrState
*vs
;
1442 vs
= qemu_malloc(sizeof(*vs
));
1446 pdu_unmarshal(pdu
, vs
->offset
, "dI", &fid
, &vs
->v9iattr
);
1448 vs
->fidp
= lookup_fid(s
, fid
);
1449 if (vs
->fidp
== NULL
) {
1454 if (vs
->v9iattr
.valid
& ATTR_MODE
) {
1455 err
= v9fs_do_chmod(s
, &vs
->fidp
->path
, vs
->v9iattr
.mode
);
1458 v9fs_setattr_post_chmod(s
, vs
, err
);
1462 complete_pdu(s
, vs
->pdu
, err
);
1466 static void v9fs_walk_complete(V9fsState
*s
, V9fsWalkState
*vs
, int err
)
1468 complete_pdu(s
, vs
->pdu
, err
);
1471 for (vs
->name_idx
= 0; vs
->name_idx
< vs
->nwnames
; vs
->name_idx
++) {
1472 v9fs_string_free(&vs
->wnames
[vs
->name_idx
]);
1475 qemu_free(vs
->wnames
);
1476 qemu_free(vs
->qids
);
1480 static void v9fs_walk_marshal(V9fsWalkState
*vs
)
1484 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "w", vs
->nwnames
);
1486 for (i
= 0; i
< vs
->nwnames
; i
++) {
1487 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qids
[i
]);
1491 static void v9fs_walk_post_newfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
1495 free_fid(s
, vs
->newfidp
->fid
);
1496 v9fs_string_free(&vs
->path
);
1501 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1504 if (vs
->name_idx
< vs
->nwnames
) {
1505 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1506 vs
->wnames
[vs
->name_idx
].data
);
1507 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1509 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1510 v9fs_walk_post_newfid_lstat(s
, vs
, err
);
1514 v9fs_string_free(&vs
->path
);
1515 v9fs_walk_marshal(vs
);
1518 v9fs_walk_complete(s
, vs
, err
);
1521 static void v9fs_walk_post_oldfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
1525 v9fs_string_free(&vs
->path
);
1530 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1532 if (vs
->name_idx
< vs
->nwnames
) {
1534 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1535 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1536 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1538 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1539 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1543 v9fs_string_free(&vs
->path
);
1544 v9fs_walk_marshal(vs
);
1547 v9fs_walk_complete(s
, vs
, err
);
1550 static void v9fs_walk(V9fsState
*s
, V9fsPDU
*pdu
)
1552 int32_t fid
, newfid
;
1557 vs
= qemu_malloc(sizeof(*vs
));
1563 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "ddw", &fid
,
1564 &newfid
, &vs
->nwnames
);
1567 vs
->wnames
= qemu_mallocz(sizeof(vs
->wnames
[0]) * vs
->nwnames
);
1569 vs
->qids
= qemu_mallocz(sizeof(vs
->qids
[0]) * vs
->nwnames
);
1571 for (i
= 0; i
< vs
->nwnames
; i
++) {
1572 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "s",
1577 vs
->fidp
= lookup_fid(s
, fid
);
1578 if (vs
->fidp
== NULL
) {
1583 /* FIXME: is this really valid? */
1584 if (fid
== newfid
) {
1586 BUG_ON(vs
->fidp
->fid_type
!= P9_FID_NONE
);
1587 v9fs_string_init(&vs
->path
);
1590 if (vs
->name_idx
< vs
->nwnames
) {
1591 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1592 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1593 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1595 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1596 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1600 vs
->newfidp
= alloc_fid(s
, newfid
);
1601 if (vs
->newfidp
== NULL
) {
1606 vs
->newfidp
->uid
= vs
->fidp
->uid
;
1607 v9fs_string_init(&vs
->path
);
1609 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->fidp
->path
);
1611 if (vs
->name_idx
< vs
->nwnames
) {
1612 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1613 vs
->wnames
[vs
->name_idx
].data
);
1614 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1616 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1617 v9fs_walk_post_newfid_lstat(s
, vs
, err
);
1622 v9fs_walk_marshal(vs
);
1625 v9fs_walk_complete(s
, vs
, err
);
1628 static int32_t get_iounit(V9fsState
*s
, V9fsString
*name
)
1630 struct statfs stbuf
;
1634 * iounit should be multiples of f_bsize (host filesystem block size
1635 * and as well as less than (client msize - P9_IOHDRSZ))
1637 if (!v9fs_do_statfs(s
, name
, &stbuf
)) {
1638 iounit
= stbuf
.f_bsize
;
1639 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1643 iounit
= s
->msize
- P9_IOHDRSZ
;
1648 static void v9fs_open_post_opendir(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1650 if (vs
->fidp
->fs
.dir
== NULL
) {
1654 vs
->fidp
->fid_type
= P9_FID_DIR
;
1655 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, 0);
1658 complete_pdu(s
, vs
->pdu
, err
);
1663 static void v9fs_open_post_getiounit(V9fsState
*s
, V9fsOpenState
*vs
)
1666 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, vs
->iounit
);
1668 complete_pdu(s
, vs
->pdu
, err
);
1672 static void v9fs_open_post_open(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1674 if (vs
->fidp
->fs
.fd
== -1) {
1678 vs
->fidp
->fid_type
= P9_FID_FILE
;
1679 vs
->iounit
= get_iounit(s
, &vs
->fidp
->path
);
1680 v9fs_open_post_getiounit(s
, vs
);
1683 complete_pdu(s
, vs
->pdu
, err
);
1687 static void v9fs_open_post_lstat(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1696 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
1698 if (S_ISDIR(vs
->stbuf
.st_mode
)) {
1699 vs
->fidp
->fs
.dir
= v9fs_do_opendir(s
, &vs
->fidp
->path
);
1700 v9fs_open_post_opendir(s
, vs
, err
);
1702 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1704 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
1706 flags
= omode_to_uflags(vs
->mode
);
1708 vs
->fidp
->fs
.fd
= v9fs_do_open(s
, &vs
->fidp
->path
, flags
);
1709 v9fs_open_post_open(s
, vs
, err
);
1713 complete_pdu(s
, vs
->pdu
, err
);
1717 static void v9fs_open(V9fsState
*s
, V9fsPDU
*pdu
)
1723 vs
= qemu_malloc(sizeof(*vs
));
1728 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1729 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dd", &fid
, &vs
->mode
);
1731 pdu_unmarshal(vs
->pdu
, vs
->offset
, "db", &fid
, &vs
->mode
);
1734 vs
->fidp
= lookup_fid(s
, fid
);
1735 if (vs
->fidp
== NULL
) {
1740 BUG_ON(vs
->fidp
->fid_type
!= P9_FID_NONE
);
1742 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1744 v9fs_open_post_lstat(s
, vs
, err
);
1747 complete_pdu(s
, pdu
, err
);
1751 static void v9fs_post_lcreate(V9fsState
*s
, V9fsLcreateState
*vs
, int err
)
1754 v9fs_string_copy(&vs
->fidp
->path
, &vs
->fullname
);
1755 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
1756 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
,
1760 vs
->fidp
->fid_type
= P9_FID_NONE
;
1761 close(vs
->fidp
->fs
.fd
);
1765 complete_pdu(s
, vs
->pdu
, err
);
1766 v9fs_string_free(&vs
->name
);
1767 v9fs_string_free(&vs
->fullname
);
1771 static void v9fs_lcreate_post_get_iounit(V9fsState
*s
, V9fsLcreateState
*vs
,
1778 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
1781 v9fs_post_lcreate(s
, vs
, err
);
1784 static void v9fs_lcreate_post_do_open2(V9fsState
*s
, V9fsLcreateState
*vs
,
1787 if (vs
->fidp
->fs
.fd
== -1) {
1791 vs
->fidp
->fid_type
= P9_FID_FILE
;
1792 vs
->iounit
= get_iounit(s
, &vs
->fullname
);
1793 v9fs_lcreate_post_get_iounit(s
, vs
, err
);
1797 v9fs_post_lcreate(s
, vs
, err
);
1800 static void v9fs_lcreate(V9fsState
*s
, V9fsPDU
*pdu
)
1802 int32_t dfid
, flags
, mode
;
1804 V9fsLcreateState
*vs
;
1807 vs
= qemu_malloc(sizeof(*vs
));
1811 v9fs_string_init(&vs
->fullname
);
1813 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsddd", &dfid
, &vs
->name
, &flags
,
1816 vs
->fidp
= lookup_fid(s
, dfid
);
1817 if (vs
->fidp
== NULL
) {
1822 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", vs
->fidp
->path
.data
,
1825 vs
->fidp
->fs
.fd
= v9fs_do_open2(s
, vs
->fullname
.data
, vs
->fidp
->uid
,
1827 v9fs_lcreate_post_do_open2(s
, vs
, err
);
1831 complete_pdu(s
, vs
->pdu
, err
);
1832 v9fs_string_free(&vs
->name
);
1836 static void v9fs_clunk(V9fsState
*s
, V9fsPDU
*pdu
)
1842 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1844 err
= free_fid(s
, fid
);
1852 complete_pdu(s
, pdu
, err
);
1855 static void v9fs_read_post_readdir(V9fsState
*, V9fsReadState
*, ssize_t
);
1857 static void v9fs_read_post_seekdir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1862 v9fs_stat_free(&vs
->v9stat
);
1863 v9fs_string_free(&vs
->name
);
1864 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
1865 vs
->offset
+= vs
->count
;
1868 complete_pdu(s
, vs
->pdu
, err
);
1873 static void v9fs_read_post_dir_lstat(V9fsState
*s
, V9fsReadState
*vs
,
1880 err
= stat_to_v9stat(s
, &vs
->name
, &vs
->stbuf
, &vs
->v9stat
);
1885 vs
->len
= pdu_marshal(vs
->pdu
, vs
->offset
+ 4 + vs
->count
, "S",
1887 if ((vs
->len
!= (vs
->v9stat
.size
+ 2)) ||
1888 ((vs
->count
+ vs
->len
) > vs
->max_count
)) {
1889 v9fs_do_seekdir(s
, vs
->fidp
->fs
.dir
, vs
->dir_pos
);
1890 v9fs_read_post_seekdir(s
, vs
, err
);
1893 vs
->count
+= vs
->len
;
1894 v9fs_stat_free(&vs
->v9stat
);
1895 v9fs_string_free(&vs
->name
);
1896 vs
->dir_pos
= vs
->dent
->d_off
;
1897 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->fs
.dir
);
1898 v9fs_read_post_readdir(s
, vs
, err
);
1901 v9fs_do_seekdir(s
, vs
->fidp
->fs
.dir
, vs
->dir_pos
);
1902 v9fs_read_post_seekdir(s
, vs
, err
);
1907 static void v9fs_read_post_readdir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1910 memset(&vs
->v9stat
, 0, sizeof(vs
->v9stat
));
1911 v9fs_string_init(&vs
->name
);
1912 v9fs_string_sprintf(&vs
->name
, "%s/%s", vs
->fidp
->path
.data
,
1914 err
= v9fs_do_lstat(s
, &vs
->name
, &vs
->stbuf
);
1915 v9fs_read_post_dir_lstat(s
, vs
, err
);
1919 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
1920 vs
->offset
+= vs
->count
;
1922 complete_pdu(s
, vs
->pdu
, err
);
1927 static void v9fs_read_post_telldir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1929 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->fs
.dir
);
1930 v9fs_read_post_readdir(s
, vs
, err
);
1934 static void v9fs_read_post_rewinddir(V9fsState
*s
, V9fsReadState
*vs
,
1937 vs
->dir_pos
= v9fs_do_telldir(s
, vs
->fidp
->fs
.dir
);
1938 v9fs_read_post_telldir(s
, vs
, err
);
1942 static void v9fs_read_post_readv(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1945 /* IO error return the error */
1949 vs
->total
+= vs
->len
;
1950 vs
->sg
= adjust_sg(vs
->sg
, vs
->len
, &vs
->cnt
);
1951 if (vs
->total
< vs
->count
&& vs
->len
> 0) {
1954 print_sg(vs
->sg
, vs
->cnt
);
1956 vs
->len
= v9fs_do_readv(s
, vs
->fidp
->fs
.fd
, vs
->sg
, vs
->cnt
);
1957 } while (vs
->len
== -1 && errno
== EINTR
);
1958 if (vs
->len
== -1) {
1961 v9fs_read_post_readv(s
, vs
, err
);
1964 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->total
);
1965 vs
->offset
+= vs
->count
;
1969 complete_pdu(s
, vs
->pdu
, err
);
1973 static void v9fs_read_post_lseek(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1979 vs
->sg
= cap_sg(vs
->sg
, vs
->count
, &vs
->cnt
);
1981 if (vs
->total
< vs
->count
) {
1984 print_sg(vs
->sg
, vs
->cnt
);
1986 vs
->len
= v9fs_do_readv(s
, vs
->fidp
->fs
.fd
, vs
->sg
, vs
->cnt
);
1987 } while (vs
->len
== -1 && errno
== EINTR
);
1988 if (vs
->len
== -1) {
1991 v9fs_read_post_readv(s
, vs
, err
);
1995 complete_pdu(s
, vs
->pdu
, err
);
1999 static void v9fs_xattr_read(V9fsState
*s
, V9fsReadState
*vs
)
2005 xattr_len
= vs
->fidp
->fs
.xattr
.len
;
2006 read_count
= xattr_len
- vs
->off
;
2007 if (read_count
> vs
->count
) {
2008 read_count
= vs
->count
;
2009 } else if (read_count
< 0) {
2011 * read beyond XATTR value
2015 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", read_count
);
2016 vs
->offset
+= pdu_pack(vs
->pdu
, vs
->offset
,
2017 ((char *)vs
->fidp
->fs
.xattr
.value
) + vs
->off
,
2020 complete_pdu(s
, vs
->pdu
, err
);
2024 static void v9fs_read(V9fsState
*s
, V9fsPDU
*pdu
)
2030 vs
= qemu_malloc(sizeof(*vs
));
2037 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dqd", &fid
, &vs
->off
, &vs
->count
);
2039 vs
->fidp
= lookup_fid(s
, fid
);
2040 if (vs
->fidp
== NULL
) {
2045 if (vs
->fidp
->fid_type
== P9_FID_DIR
) {
2046 vs
->max_count
= vs
->count
;
2049 v9fs_do_rewinddir(s
, vs
->fidp
->fs
.dir
);
2051 v9fs_read_post_rewinddir(s
, vs
, err
);
2053 } else if (vs
->fidp
->fid_type
== P9_FID_FILE
) {
2055 pdu_marshal(vs
->pdu
, vs
->offset
+ 4, "v", vs
->sg
, &vs
->cnt
);
2056 err
= v9fs_do_lseek(s
, vs
->fidp
->fs
.fd
, vs
->off
, SEEK_SET
);
2057 v9fs_read_post_lseek(s
, vs
, err
);
2059 } else if (vs
->fidp
->fid_type
== P9_FID_XATTR
) {
2060 v9fs_xattr_read(s
, vs
);
2066 complete_pdu(s
, pdu
, err
);
2070 typedef struct V9fsReadDirState
{
2074 off_t saved_dir_pos
;
2075 struct dirent
*dent
;
2079 int64_t initial_offset
;
2083 static void v9fs_readdir_post_seekdir(V9fsState
*s
, V9fsReadDirState
*vs
)
2085 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
2086 vs
->offset
+= vs
->count
;
2087 complete_pdu(s
, vs
->pdu
, vs
->offset
);
2092 /* Size of each dirent on the wire: size of qid (13) + size of offset (8)
2093 * size of type (1) + size of name.size (2) + strlen(name.data)
2095 #define V9_READDIR_DATA_SZ (24 + strlen(vs->name.data))
2097 static void v9fs_readdir_post_readdir(V9fsState
*s
, V9fsReadDirState
*vs
)
2103 v9fs_string_init(&vs
->name
);
2104 v9fs_string_sprintf(&vs
->name
, "%s", vs
->dent
->d_name
);
2106 if ((vs
->count
+ V9_READDIR_DATA_SZ
) > vs
->max_count
) {
2107 /* Ran out of buffer. Set dir back to old position and return */
2108 v9fs_do_seekdir(s
, vs
->fidp
->fs
.dir
, vs
->saved_dir_pos
);
2109 v9fs_readdir_post_seekdir(s
, vs
);
2113 /* Fill up just the path field of qid because the client uses
2114 * only that. To fill the entire qid structure we will have
2115 * to stat each dirent found, which is expensive
2117 size
= MIN(sizeof(vs
->dent
->d_ino
), sizeof(vs
->qid
.path
));
2118 memcpy(&vs
->qid
.path
, &vs
->dent
->d_ino
, size
);
2119 /* Fill the other fields with dummy values */
2121 vs
->qid
.version
= 0;
2123 len
= pdu_marshal(vs
->pdu
, vs
->offset
+4+vs
->count
, "Qqbs",
2124 &vs
->qid
, vs
->dent
->d_off
,
2125 vs
->dent
->d_type
, &vs
->name
);
2127 v9fs_string_free(&vs
->name
);
2128 vs
->saved_dir_pos
= vs
->dent
->d_off
;
2129 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->fs
.dir
);
2130 v9fs_readdir_post_readdir(s
, vs
);
2134 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
2135 vs
->offset
+= vs
->count
;
2136 complete_pdu(s
, vs
->pdu
, vs
->offset
);
2141 static void v9fs_readdir_post_telldir(V9fsState
*s
, V9fsReadDirState
*vs
)
2143 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->fs
.dir
);
2144 v9fs_readdir_post_readdir(s
, vs
);
2148 static void v9fs_readdir_post_setdir(V9fsState
*s
, V9fsReadDirState
*vs
)
2150 vs
->saved_dir_pos
= v9fs_do_telldir(s
, vs
->fidp
->fs
.dir
);
2151 v9fs_readdir_post_telldir(s
, vs
);
2155 static void v9fs_readdir(V9fsState
*s
, V9fsPDU
*pdu
)
2158 V9fsReadDirState
*vs
;
2162 vs
= qemu_malloc(sizeof(*vs
));
2167 pdu_unmarshal(vs
->pdu
, offset
, "dqd", &fid
, &vs
->initial_offset
,
2170 vs
->fidp
= lookup_fid(s
, fid
);
2171 if (vs
->fidp
== NULL
|| !(vs
->fidp
->fs
.dir
)) {
2176 if (vs
->initial_offset
== 0) {
2177 v9fs_do_rewinddir(s
, vs
->fidp
->fs
.dir
);
2179 v9fs_do_seekdir(s
, vs
->fidp
->fs
.dir
, vs
->initial_offset
);
2182 v9fs_readdir_post_setdir(s
, vs
);
2186 complete_pdu(s
, pdu
, err
);
2191 static void v9fs_write_post_writev(V9fsState
*s
, V9fsWriteState
*vs
,
2195 /* IO error return the error */
2199 vs
->total
+= vs
->len
;
2200 vs
->sg
= adjust_sg(vs
->sg
, vs
->len
, &vs
->cnt
);
2201 if (vs
->total
< vs
->count
&& vs
->len
> 0) {
2204 print_sg(vs
->sg
, vs
->cnt
);
2206 vs
->len
= v9fs_do_writev(s
, vs
->fidp
->fs
.fd
, vs
->sg
, vs
->cnt
);
2207 } while (vs
->len
== -1 && errno
== EINTR
);
2208 if (vs
->len
== -1) {
2211 v9fs_write_post_writev(s
, vs
, err
);
2214 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->total
);
2218 complete_pdu(s
, vs
->pdu
, err
);
2222 static void v9fs_write_post_lseek(V9fsState
*s
, V9fsWriteState
*vs
, ssize_t err
)
2228 vs
->sg
= cap_sg(vs
->sg
, vs
->count
, &vs
->cnt
);
2230 if (vs
->total
< vs
->count
) {
2233 print_sg(vs
->sg
, vs
->cnt
);
2235 vs
->len
= v9fs_do_writev(s
, vs
->fidp
->fs
.fd
, vs
->sg
, vs
->cnt
);
2236 } while (vs
->len
== -1 && errno
== EINTR
);
2237 if (vs
->len
== -1) {
2240 v9fs_write_post_writev(s
, vs
, err
);
2245 complete_pdu(s
, vs
->pdu
, err
);
2249 static void v9fs_xattr_write(V9fsState
*s
, V9fsWriteState
*vs
)
2256 xattr_len
= vs
->fidp
->fs
.xattr
.len
;
2257 write_count
= xattr_len
- vs
->off
;
2258 if (write_count
> vs
->count
) {
2259 write_count
= vs
->count
;
2260 } else if (write_count
< 0) {
2262 * write beyond XATTR value len specified in
2268 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", write_count
);
2270 vs
->fidp
->fs
.xattr
.copied_len
+= write_count
;
2272 * Now copy the content from sg list
2274 for (i
= 0; i
< vs
->cnt
; i
++) {
2275 if (write_count
> vs
->sg
[i
].iov_len
) {
2276 to_copy
= vs
->sg
[i
].iov_len
;
2278 to_copy
= write_count
;
2280 memcpy((char *)vs
->fidp
->fs
.xattr
.value
+ vs
->off
,
2281 vs
->sg
[i
].iov_base
, to_copy
);
2282 /* updating vs->off since we are not using below */
2284 write_count
-= to_copy
;
2287 complete_pdu(s
, vs
->pdu
, err
);
2291 static void v9fs_write(V9fsState
*s
, V9fsPDU
*pdu
)
2297 vs
= qemu_malloc(sizeof(*vs
));
2305 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dqdv", &fid
, &vs
->off
, &vs
->count
,
2308 vs
->fidp
= lookup_fid(s
, fid
);
2309 if (vs
->fidp
== NULL
) {
2314 if (vs
->fidp
->fid_type
== P9_FID_FILE
) {
2315 if (vs
->fidp
->fs
.fd
== -1) {
2319 } else if (vs
->fidp
->fid_type
== P9_FID_XATTR
) {
2321 * setxattr operation
2323 v9fs_xattr_write(s
, vs
);
2329 err
= v9fs_do_lseek(s
, vs
->fidp
->fs
.fd
, vs
->off
, SEEK_SET
);
2331 v9fs_write_post_lseek(s
, vs
, err
);
2335 complete_pdu(s
, vs
->pdu
, err
);
2339 static void v9fs_create_post_getiounit(V9fsState
*s
, V9fsCreateState
*vs
)
2342 v9fs_string_copy(&vs
->fidp
->path
, &vs
->fullname
);
2343 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
2345 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, vs
->iounit
);
2348 complete_pdu(s
, vs
->pdu
, err
);
2349 v9fs_string_free(&vs
->name
);
2350 v9fs_string_free(&vs
->extension
);
2351 v9fs_string_free(&vs
->fullname
);
2355 static void v9fs_post_create(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2358 vs
->iounit
= get_iounit(s
, &vs
->fidp
->path
);
2359 v9fs_create_post_getiounit(s
, vs
);
2363 complete_pdu(s
, vs
->pdu
, err
);
2364 v9fs_string_free(&vs
->name
);
2365 v9fs_string_free(&vs
->extension
);
2366 v9fs_string_free(&vs
->fullname
);
2370 static void v9fs_create_post_perms(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2375 v9fs_post_create(s
, vs
, err
);
2378 static void v9fs_create_post_opendir(V9fsState
*s
, V9fsCreateState
*vs
,
2381 if (!vs
->fidp
->fs
.dir
) {
2384 vs
->fidp
->fid_type
= P9_FID_DIR
;
2385 v9fs_post_create(s
, vs
, err
);
2388 static void v9fs_create_post_dir_lstat(V9fsState
*s
, V9fsCreateState
*vs
,
2396 vs
->fidp
->fs
.dir
= v9fs_do_opendir(s
, &vs
->fullname
);
2397 v9fs_create_post_opendir(s
, vs
, err
);
2401 v9fs_post_create(s
, vs
, err
);
2404 static void v9fs_create_post_mkdir(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2411 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
2412 v9fs_create_post_dir_lstat(s
, vs
, err
);
2416 v9fs_post_create(s
, vs
, err
);
2419 static void v9fs_create_post_fstat(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2422 vs
->fidp
->fid_type
= P9_FID_NONE
;
2423 close(vs
->fidp
->fs
.fd
);
2426 v9fs_post_create(s
, vs
, err
);
2430 static void v9fs_create_post_open2(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2432 if (vs
->fidp
->fs
.fd
== -1) {
2436 vs
->fidp
->fid_type
= P9_FID_FILE
;
2437 err
= v9fs_do_fstat(s
, vs
->fidp
->fs
.fd
, &vs
->stbuf
);
2438 v9fs_create_post_fstat(s
, vs
, err
);
2443 v9fs_post_create(s
, vs
, err
);
2447 static void v9fs_create_post_lstat(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2450 if (err
== 0 || errno
!= ENOENT
) {
2455 if (vs
->perm
& P9_STAT_MODE_DIR
) {
2456 err
= v9fs_do_mkdir(s
, vs
->fullname
.data
, vs
->perm
& 0777,
2458 v9fs_create_post_mkdir(s
, vs
, err
);
2459 } else if (vs
->perm
& P9_STAT_MODE_SYMLINK
) {
2460 err
= v9fs_do_symlink(s
, vs
->fidp
, vs
->extension
.data
,
2461 vs
->fullname
.data
, -1);
2462 v9fs_create_post_perms(s
, vs
, err
);
2463 } else if (vs
->perm
& P9_STAT_MODE_LINK
) {
2464 int32_t nfid
= atoi(vs
->extension
.data
);
2465 V9fsFidState
*nfidp
= lookup_fid(s
, nfid
);
2466 if (nfidp
== NULL
) {
2468 v9fs_post_create(s
, vs
, err
);
2470 err
= v9fs_do_link(s
, &nfidp
->path
, &vs
->fullname
);
2471 v9fs_create_post_perms(s
, vs
, err
);
2472 } else if (vs
->perm
& P9_STAT_MODE_DEVICE
) {
2474 uint32_t major
, minor
;
2477 if (sscanf(vs
->extension
.data
, "%c %u %u", &ctype
, &major
,
2480 v9fs_post_create(s
, vs
, err
);
2492 v9fs_post_create(s
, vs
, err
);
2495 nmode
|= vs
->perm
& 0777;
2496 err
= v9fs_do_mknod(s
, vs
->fullname
.data
, nmode
,
2497 makedev(major
, minor
), vs
->fidp
->uid
, -1);
2498 v9fs_create_post_perms(s
, vs
, err
);
2499 } else if (vs
->perm
& P9_STAT_MODE_NAMED_PIPE
) {
2500 err
= v9fs_do_mknod(s
, vs
->fullname
.data
, S_IFIFO
| (vs
->perm
& 0777),
2501 0, vs
->fidp
->uid
, -1);
2502 v9fs_post_create(s
, vs
, err
);
2503 } else if (vs
->perm
& P9_STAT_MODE_SOCKET
) {
2504 err
= v9fs_do_mknod(s
, vs
->fullname
.data
, S_IFSOCK
| (vs
->perm
& 0777),
2505 0, vs
->fidp
->uid
, -1);
2506 v9fs_post_create(s
, vs
, err
);
2508 vs
->fidp
->fs
.fd
= v9fs_do_open2(s
, vs
->fullname
.data
, vs
->fidp
->uid
,
2509 -1, omode_to_uflags(vs
->mode
)|O_CREAT
, vs
->perm
);
2511 v9fs_create_post_open2(s
, vs
, err
);
2517 v9fs_post_create(s
, vs
, err
);
2520 static void v9fs_create(V9fsState
*s
, V9fsPDU
*pdu
)
2523 V9fsCreateState
*vs
;
2526 vs
= qemu_malloc(sizeof(*vs
));
2530 v9fs_string_init(&vs
->fullname
);
2532 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsdbs", &fid
, &vs
->name
,
2533 &vs
->perm
, &vs
->mode
, &vs
->extension
);
2535 vs
->fidp
= lookup_fid(s
, fid
);
2536 if (vs
->fidp
== NULL
) {
2541 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", vs
->fidp
->path
.data
,
2544 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
2545 v9fs_create_post_lstat(s
, vs
, err
);
2549 complete_pdu(s
, vs
->pdu
, err
);
2550 v9fs_string_free(&vs
->name
);
2551 v9fs_string_free(&vs
->extension
);
2555 static void v9fs_post_symlink(V9fsState
*s
, V9fsSymlinkState
*vs
, int err
)
2558 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
2559 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qid
);
2564 complete_pdu(s
, vs
->pdu
, err
);
2565 v9fs_string_free(&vs
->name
);
2566 v9fs_string_free(&vs
->symname
);
2567 v9fs_string_free(&vs
->fullname
);
2571 static void v9fs_symlink_post_do_symlink(V9fsState
*s
, V9fsSymlinkState
*vs
,
2577 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
2579 v9fs_post_symlink(s
, vs
, err
);
2582 static void v9fs_symlink(V9fsState
*s
, V9fsPDU
*pdu
)
2585 V9fsSymlinkState
*vs
;
2589 vs
= qemu_malloc(sizeof(*vs
));
2593 v9fs_string_init(&vs
->fullname
);
2595 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dssd", &dfid
, &vs
->name
,
2596 &vs
->symname
, &gid
);
2598 vs
->dfidp
= lookup_fid(s
, dfid
);
2599 if (vs
->dfidp
== NULL
) {
2604 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", vs
->dfidp
->path
.data
,
2606 err
= v9fs_do_symlink(s
, vs
->dfidp
, vs
->symname
.data
,
2607 vs
->fullname
.data
, gid
);
2608 v9fs_symlink_post_do_symlink(s
, vs
, err
);
2612 complete_pdu(s
, vs
->pdu
, err
);
2613 v9fs_string_free(&vs
->name
);
2614 v9fs_string_free(&vs
->symname
);
2618 static void v9fs_flush(V9fsState
*s
, V9fsPDU
*pdu
)
2620 /* A nop call with no return */
2621 complete_pdu(s
, pdu
, 7);
2624 static void v9fs_link(V9fsState
*s
, V9fsPDU
*pdu
)
2626 int32_t dfid
, oldfid
;
2627 V9fsFidState
*dfidp
, *oldfidp
;
2628 V9fsString name
, fullname
;
2632 v9fs_string_init(&fullname
);
2634 pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2636 dfidp
= lookup_fid(s
, dfid
);
2637 if (dfidp
== NULL
) {
2642 oldfidp
= lookup_fid(s
, oldfid
);
2643 if (oldfidp
== NULL
) {
2648 v9fs_string_sprintf(&fullname
, "%s/%s", dfidp
->path
.data
, name
.data
);
2650 err
= v9fs_do_link(s
, &oldfidp
->path
, &fullname
);
2654 v9fs_string_free(&fullname
);
2657 v9fs_string_free(&name
);
2658 complete_pdu(s
, pdu
, err
);
2661 static void v9fs_remove_post_remove(V9fsState
*s
, V9fsRemoveState
*vs
,
2670 /* For TREMOVE we need to clunk the fid even on failed remove */
2671 free_fid(s
, vs
->fidp
->fid
);
2673 complete_pdu(s
, vs
->pdu
, err
);
2677 static void v9fs_remove(V9fsState
*s
, V9fsPDU
*pdu
)
2680 V9fsRemoveState
*vs
;
2683 vs
= qemu_malloc(sizeof(*vs
));
2687 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &fid
);
2689 vs
->fidp
= lookup_fid(s
, fid
);
2690 if (vs
->fidp
== NULL
) {
2695 err
= v9fs_do_remove(s
, &vs
->fidp
->path
);
2696 v9fs_remove_post_remove(s
, vs
, err
);
2700 complete_pdu(s
, pdu
, err
);
2704 static void v9fs_wstat_post_truncate(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2713 v9fs_stat_free(&vs
->v9stat
);
2714 complete_pdu(s
, vs
->pdu
, err
);
2718 static void v9fs_wstat_post_rename(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2723 if (vs
->v9stat
.length
!= -1) {
2724 if (v9fs_do_truncate(s
, &vs
->fidp
->path
, vs
->v9stat
.length
) < 0) {
2728 v9fs_wstat_post_truncate(s
, vs
, err
);
2732 v9fs_stat_free(&vs
->v9stat
);
2733 complete_pdu(s
, vs
->pdu
, err
);
2737 static int v9fs_complete_rename(V9fsState
*s
, V9fsRenameState
*vs
)
2740 char *old_name
, *new_name
;
2743 if (vs
->newdirfid
!= -1) {
2744 V9fsFidState
*dirfidp
;
2745 dirfidp
= lookup_fid(s
, vs
->newdirfid
);
2747 if (dirfidp
== NULL
) {
2752 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2754 new_name
= qemu_mallocz(dirfidp
->path
.size
+ vs
->name
.size
+ 2);
2756 strcpy(new_name
, dirfidp
->path
.data
);
2757 strcat(new_name
, "/");
2758 strcat(new_name
+ dirfidp
->path
.size
, vs
->name
.data
);
2760 old_name
= vs
->fidp
->path
.data
;
2761 end
= strrchr(old_name
, '/');
2767 new_name
= qemu_mallocz(end
- old_name
+ vs
->name
.size
+ 1);
2769 strncat(new_name
, old_name
, end
- old_name
);
2770 strncat(new_name
+ (end
- old_name
), vs
->name
.data
, vs
->name
.size
);
2773 v9fs_string_free(&vs
->name
);
2774 vs
->name
.data
= qemu_strdup(new_name
);
2775 vs
->name
.size
= strlen(new_name
);
2777 if (strcmp(new_name
, vs
->fidp
->path
.data
) != 0) {
2778 if (v9fs_do_rename(s
, &vs
->fidp
->path
, &vs
->name
)) {
2783 * Fixup fid's pointing to the old name to
2784 * start pointing to the new name
2786 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
2787 if (vs
->fidp
== fidp
) {
2789 * we replace name of this fid towards the end
2790 * so that our below strcmp will work
2794 if (!strncmp(vs
->fidp
->path
.data
, fidp
->path
.data
,
2795 strlen(vs
->fidp
->path
.data
))) {
2796 /* replace the name */
2797 v9fs_fix_path(&fidp
->path
, &vs
->name
,
2798 strlen(vs
->fidp
->path
.data
));
2801 v9fs_string_copy(&vs
->fidp
->path
, &vs
->name
);
2805 v9fs_string_free(&vs
->name
);
2809 static void v9fs_rename_post_rename(V9fsState
*s
, V9fsRenameState
*vs
, int err
)
2811 complete_pdu(s
, vs
->pdu
, err
);
2815 static void v9fs_wstat_post_chown(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2821 if (vs
->v9stat
.name
.size
!= 0) {
2822 V9fsRenameState
*vr
;
2824 vr
= qemu_mallocz(sizeof(V9fsRenameState
));
2827 vr
->fidp
= vs
->fidp
;
2828 vr
->offset
= vs
->offset
;
2829 vr
->name
.size
= vs
->v9stat
.name
.size
;
2830 vr
->name
.data
= qemu_strdup(vs
->v9stat
.name
.data
);
2832 err
= v9fs_complete_rename(s
, vr
);
2835 v9fs_wstat_post_rename(s
, vs
, err
);
2839 v9fs_stat_free(&vs
->v9stat
);
2840 complete_pdu(s
, vs
->pdu
, err
);
2844 static void v9fs_rename(V9fsState
*s
, V9fsPDU
*pdu
)
2847 V9fsRenameState
*vs
;
2850 vs
= qemu_malloc(sizeof(*vs
));
2854 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dds", &fid
, &vs
->newdirfid
, &vs
->name
);
2856 vs
->fidp
= lookup_fid(s
, fid
);
2857 if (vs
->fidp
== NULL
) {
2862 BUG_ON(vs
->fidp
->fid_type
!= P9_FID_NONE
);
2864 err
= v9fs_complete_rename(s
, vs
);
2865 v9fs_rename_post_rename(s
, vs
, err
);
2868 complete_pdu(s
, vs
->pdu
, err
);
2872 static void v9fs_wstat_post_utime(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2878 if (vs
->v9stat
.n_gid
!= -1 || vs
->v9stat
.n_uid
!= -1) {
2879 if (v9fs_do_chown(s
, &vs
->fidp
->path
, vs
->v9stat
.n_uid
,
2880 vs
->v9stat
.n_gid
)) {
2884 v9fs_wstat_post_chown(s
, vs
, err
);
2888 v9fs_stat_free(&vs
->v9stat
);
2889 complete_pdu(s
, vs
->pdu
, err
);
2893 static void v9fs_wstat_post_chmod(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2899 if (vs
->v9stat
.mtime
!= -1 || vs
->v9stat
.atime
!= -1) {
2900 struct timespec times
[2];
2901 if (vs
->v9stat
.atime
!= -1) {
2902 times
[0].tv_sec
= vs
->v9stat
.atime
;
2903 times
[0].tv_nsec
= 0;
2905 times
[0].tv_nsec
= UTIME_OMIT
;
2907 if (vs
->v9stat
.mtime
!= -1) {
2908 times
[1].tv_sec
= vs
->v9stat
.mtime
;
2909 times
[1].tv_nsec
= 0;
2911 times
[1].tv_nsec
= UTIME_OMIT
;
2914 if (v9fs_do_utimensat(s
, &vs
->fidp
->path
, times
)) {
2919 v9fs_wstat_post_utime(s
, vs
, err
);
2923 v9fs_stat_free(&vs
->v9stat
);
2924 complete_pdu(s
, vs
->pdu
, err
);
2928 static void v9fs_wstat_post_fsync(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2933 v9fs_stat_free(&vs
->v9stat
);
2934 complete_pdu(s
, vs
->pdu
, err
);
2938 static void v9fs_wstat_post_lstat(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2947 v9_mode
= stat_to_v9mode(&vs
->stbuf
);
2949 if ((vs
->v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2950 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2951 /* Attempting to change the type */
2956 if (v9fs_do_chmod(s
, &vs
->fidp
->path
, v9mode_to_mode(vs
->v9stat
.mode
,
2957 &vs
->v9stat
.extension
))) {
2960 v9fs_wstat_post_chmod(s
, vs
, err
);
2964 v9fs_stat_free(&vs
->v9stat
);
2965 complete_pdu(s
, vs
->pdu
, err
);
2969 static void v9fs_wstat(V9fsState
*s
, V9fsPDU
*pdu
)
2975 vs
= qemu_malloc(sizeof(*vs
));
2979 pdu_unmarshal(pdu
, vs
->offset
, "dwS", &fid
, &vs
->unused
, &vs
->v9stat
);
2981 vs
->fidp
= lookup_fid(s
, fid
);
2982 if (vs
->fidp
== NULL
) {
2987 /* do we need to sync the file? */
2988 if (donttouch_stat(&vs
->v9stat
)) {
2989 err
= v9fs_do_fsync(s
, vs
->fidp
->fs
.fd
);
2990 v9fs_wstat_post_fsync(s
, vs
, err
);
2994 if (vs
->v9stat
.mode
!= -1) {
2995 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
2996 v9fs_wstat_post_lstat(s
, vs
, err
);
3000 v9fs_wstat_post_chmod(s
, vs
, err
);
3004 v9fs_stat_free(&vs
->v9stat
);
3005 complete_pdu(s
, vs
->pdu
, err
);
3009 static void v9fs_statfs_post_statfs(V9fsState
*s
, V9fsStatfsState
*vs
, int err
)
3011 int32_t bsize_factor
;
3019 * compute bsize factor based on host file system block size
3022 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/vs
->stbuf
.f_bsize
;
3023 if (!bsize_factor
) {
3026 vs
->v9statfs
.f_type
= vs
->stbuf
.f_type
;
3027 vs
->v9statfs
.f_bsize
= vs
->stbuf
.f_bsize
;
3028 vs
->v9statfs
.f_bsize
*= bsize_factor
;
3030 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3031 * adjust(divide) the number of blocks, free blocks and available
3032 * blocks by bsize factor
3034 vs
->v9statfs
.f_blocks
= vs
->stbuf
.f_blocks
/bsize_factor
;
3035 vs
->v9statfs
.f_bfree
= vs
->stbuf
.f_bfree
/bsize_factor
;
3036 vs
->v9statfs
.f_bavail
= vs
->stbuf
.f_bavail
/bsize_factor
;
3037 vs
->v9statfs
.f_files
= vs
->stbuf
.f_files
;
3038 vs
->v9statfs
.f_ffree
= vs
->stbuf
.f_ffree
;
3039 vs
->v9statfs
.fsid_val
= (unsigned int) vs
->stbuf
.f_fsid
.__val
[0] |
3040 (unsigned long long)vs
->stbuf
.f_fsid
.__val
[1] << 32;
3041 vs
->v9statfs
.f_namelen
= vs
->stbuf
.f_namelen
;
3043 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "ddqqqqqqd",
3044 vs
->v9statfs
.f_type
, vs
->v9statfs
.f_bsize
, vs
->v9statfs
.f_blocks
,
3045 vs
->v9statfs
.f_bfree
, vs
->v9statfs
.f_bavail
, vs
->v9statfs
.f_files
,
3046 vs
->v9statfs
.f_ffree
, vs
->v9statfs
.fsid_val
,
3047 vs
->v9statfs
.f_namelen
);
3050 complete_pdu(s
, vs
->pdu
, vs
->offset
);
3054 static void v9fs_statfs(V9fsState
*s
, V9fsPDU
*pdu
)
3056 V9fsStatfsState
*vs
;
3059 vs
= qemu_malloc(sizeof(*vs
));
3063 memset(&vs
->v9statfs
, 0, sizeof(vs
->v9statfs
));
3065 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &vs
->fid
);
3067 vs
->fidp
= lookup_fid(s
, vs
->fid
);
3068 if (vs
->fidp
== NULL
) {
3073 err
= v9fs_do_statfs(s
, &vs
->fidp
->path
, &vs
->stbuf
);
3074 v9fs_statfs_post_statfs(s
, vs
, err
);
3078 complete_pdu(s
, vs
->pdu
, err
);
3082 static void v9fs_mknod_post_lstat(V9fsState
*s
, V9fsMkState
*vs
, int err
)
3089 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
3090 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qid
);
3093 complete_pdu(s
, vs
->pdu
, err
);
3094 v9fs_string_free(&vs
->fullname
);
3095 v9fs_string_free(&vs
->name
);
3099 static void v9fs_mknod_post_mknod(V9fsState
*s
, V9fsMkState
*vs
, int err
)
3106 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
3107 v9fs_mknod_post_lstat(s
, vs
, err
);
3110 complete_pdu(s
, vs
->pdu
, err
);
3111 v9fs_string_free(&vs
->fullname
);
3112 v9fs_string_free(&vs
->name
);
3116 static void v9fs_mknod(V9fsState
*s
, V9fsPDU
*pdu
)
3126 vs
= qemu_malloc(sizeof(*vs
));
3130 v9fs_string_init(&vs
->fullname
);
3131 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsdddd", &fid
, &vs
->name
, &mode
,
3132 &major
, &minor
, &gid
);
3134 fidp
= lookup_fid(s
, fid
);
3140 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", fidp
->path
.data
, vs
->name
.data
);
3141 err
= v9fs_do_mknod(s
, vs
->fullname
.data
, mode
, makedev(major
, minor
),
3143 v9fs_mknod_post_mknod(s
, vs
, err
);
3147 complete_pdu(s
, vs
->pdu
, err
);
3148 v9fs_string_free(&vs
->fullname
);
3149 v9fs_string_free(&vs
->name
);
3153 static void v9fs_mkdir_post_lstat(V9fsState
*s
, V9fsMkState
*vs
, int err
)
3160 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
3161 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qid
);
3164 complete_pdu(s
, vs
->pdu
, err
);
3165 v9fs_string_free(&vs
->fullname
);
3166 v9fs_string_free(&vs
->name
);
3170 static void v9fs_mkdir_post_mkdir(V9fsState
*s
, V9fsMkState
*vs
, int err
)
3177 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
3178 v9fs_mkdir_post_lstat(s
, vs
, err
);
3181 complete_pdu(s
, vs
->pdu
, err
);
3182 v9fs_string_free(&vs
->fullname
);
3183 v9fs_string_free(&vs
->name
);
3187 static void v9fs_mkdir(V9fsState
*s
, V9fsPDU
*pdu
)
3196 vs
= qemu_malloc(sizeof(*vs
));
3200 v9fs_string_init(&vs
->fullname
);
3201 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsdd", &fid
, &vs
->name
, &mode
,
3204 fidp
= lookup_fid(s
, fid
);
3210 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", fidp
->path
.data
, vs
->name
.data
);
3211 err
= v9fs_do_mkdir(s
, vs
->fullname
.data
, mode
, fidp
->uid
, gid
);
3212 v9fs_mkdir_post_mkdir(s
, vs
, err
);
3216 complete_pdu(s
, vs
->pdu
, err
);
3217 v9fs_string_free(&vs
->fullname
);
3218 v9fs_string_free(&vs
->name
);
3222 static void v9fs_post_xattr_getvalue(V9fsState
*s
, V9fsXattrState
*vs
, int err
)
3227 free_fid(s
, vs
->xattr_fidp
->fid
);
3230 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "q", vs
->size
);
3233 complete_pdu(s
, vs
->pdu
, err
);
3234 v9fs_string_free(&vs
->name
);
3239 static void v9fs_post_xattr_check(V9fsState
*s
, V9fsXattrState
*vs
, ssize_t err
)
3243 free_fid(s
, vs
->xattr_fidp
->fid
);
3247 * Read the xattr value
3249 vs
->xattr_fidp
->fs
.xattr
.len
= vs
->size
;
3250 vs
->xattr_fidp
->fid_type
= P9_FID_XATTR
;
3251 vs
->xattr_fidp
->fs
.xattr
.copied_len
= -1;
3253 vs
->xattr_fidp
->fs
.xattr
.value
= qemu_malloc(vs
->size
);
3254 err
= v9fs_do_lgetxattr(s
, &vs
->xattr_fidp
->path
,
3255 &vs
->name
, vs
->xattr_fidp
->fs
.xattr
.value
,
3256 vs
->xattr_fidp
->fs
.xattr
.len
);
3258 v9fs_post_xattr_getvalue(s
, vs
, err
);
3261 complete_pdu(s
, vs
->pdu
, err
);
3262 v9fs_string_free(&vs
->name
);
3266 static void v9fs_post_lxattr_getvalue(V9fsState
*s
,
3267 V9fsXattrState
*vs
, int err
)
3271 free_fid(s
, vs
->xattr_fidp
->fid
);
3274 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "q", vs
->size
);
3277 complete_pdu(s
, vs
->pdu
, err
);
3278 v9fs_string_free(&vs
->name
);
3283 static void v9fs_post_lxattr_check(V9fsState
*s
,
3284 V9fsXattrState
*vs
, ssize_t err
)
3288 free_fid(s
, vs
->xattr_fidp
->fid
);
3292 * Read the xattr value
3294 vs
->xattr_fidp
->fs
.xattr
.len
= vs
->size
;
3295 vs
->xattr_fidp
->fid_type
= P9_FID_XATTR
;
3296 vs
->xattr_fidp
->fs
.xattr
.copied_len
= -1;
3298 vs
->xattr_fidp
->fs
.xattr
.value
= qemu_malloc(vs
->size
);
3299 err
= v9fs_do_llistxattr(s
, &vs
->xattr_fidp
->path
,
3300 vs
->xattr_fidp
->fs
.xattr
.value
,
3301 vs
->xattr_fidp
->fs
.xattr
.len
);
3303 v9fs_post_lxattr_getvalue(s
, vs
, err
);
3306 complete_pdu(s
, vs
->pdu
, err
);
3307 v9fs_string_free(&vs
->name
);
3311 static void v9fs_xattrwalk(V9fsState
*s
, V9fsPDU
*pdu
)
3315 int32_t fid
, newfid
;
3317 vs
= qemu_malloc(sizeof(*vs
));
3321 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dds", &fid
, &newfid
, &vs
->name
);
3322 vs
->file_fidp
= lookup_fid(s
, fid
);
3323 if (vs
->file_fidp
== NULL
) {
3328 vs
->xattr_fidp
= alloc_fid(s
, newfid
);
3329 if (vs
->xattr_fidp
== NULL
) {
3334 v9fs_string_copy(&vs
->xattr_fidp
->path
, &vs
->file_fidp
->path
);
3335 if (vs
->name
.data
[0] == 0) {
3337 * listxattr request. Get the size first
3339 vs
->size
= v9fs_do_llistxattr(s
, &vs
->xattr_fidp
->path
,
3344 v9fs_post_lxattr_check(s
, vs
, err
);
3348 * specific xattr fid. We check for xattr
3349 * presence also collect the xattr size
3351 vs
->size
= v9fs_do_lgetxattr(s
, &vs
->xattr_fidp
->path
,
3352 &vs
->name
, NULL
, 0);
3356 v9fs_post_xattr_check(s
, vs
, err
);
3360 complete_pdu(s
, vs
->pdu
, err
);
3361 v9fs_string_free(&vs
->name
);
3365 static void v9fs_xattrcreate(V9fsState
*s
, V9fsPDU
*pdu
)
3372 vs
= qemu_malloc(sizeof(*vs
));
3376 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsqd",
3377 &fid
, &vs
->name
, &vs
->size
, &flags
);
3379 vs
->file_fidp
= lookup_fid(s
, fid
);
3380 if (vs
->file_fidp
== NULL
) {
3385 /* Make the file fid point to xattr */
3386 vs
->xattr_fidp
= vs
->file_fidp
;
3387 vs
->xattr_fidp
->fid_type
= P9_FID_XATTR
;
3388 vs
->xattr_fidp
->fs
.xattr
.copied_len
= 0;
3389 vs
->xattr_fidp
->fs
.xattr
.len
= vs
->size
;
3390 vs
->xattr_fidp
->fs
.xattr
.flags
= flags
;
3391 v9fs_string_init(&vs
->xattr_fidp
->fs
.xattr
.name
);
3392 v9fs_string_copy(&vs
->xattr_fidp
->fs
.xattr
.name
, &vs
->name
);
3394 vs
->xattr_fidp
->fs
.xattr
.value
= qemu_malloc(vs
->size
);
3396 vs
->xattr_fidp
->fs
.xattr
.value
= NULL
;
3399 complete_pdu(s
, vs
->pdu
, err
);
3400 v9fs_string_free(&vs
->name
);
3404 typedef void (pdu_handler_t
)(V9fsState
*s
, V9fsPDU
*pdu
);
3406 static pdu_handler_t
*pdu_handlers
[] = {
3407 [P9_TREADDIR
] = v9fs_readdir
,
3408 [P9_TSTATFS
] = v9fs_statfs
,
3409 [P9_TGETATTR
] = v9fs_getattr
,
3410 [P9_TSETATTR
] = v9fs_setattr
,
3411 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3412 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3413 [P9_TMKNOD
] = v9fs_mknod
,
3414 [P9_TRENAME
] = v9fs_rename
,
3415 [P9_TMKDIR
] = v9fs_mkdir
,
3416 [P9_TVERSION
] = v9fs_version
,
3417 [P9_TLOPEN
] = v9fs_open
,
3418 [P9_TATTACH
] = v9fs_attach
,
3419 [P9_TSTAT
] = v9fs_stat
,
3420 [P9_TWALK
] = v9fs_walk
,
3421 [P9_TCLUNK
] = v9fs_clunk
,
3422 [P9_TOPEN
] = v9fs_open
,
3423 [P9_TREAD
] = v9fs_read
,
3425 [P9_TAUTH
] = v9fs_auth
,
3427 [P9_TFLUSH
] = v9fs_flush
,
3428 [P9_TLINK
] = v9fs_link
,
3429 [P9_TSYMLINK
] = v9fs_symlink
,
3430 [P9_TCREATE
] = v9fs_create
,
3431 [P9_TLCREATE
] = v9fs_lcreate
,
3432 [P9_TWRITE
] = v9fs_write
,
3433 [P9_TWSTAT
] = v9fs_wstat
,
3434 [P9_TREMOVE
] = v9fs_remove
,
3437 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
3439 pdu_handler_t
*handler
;
3445 BUG_ON(pdu
->id
>= ARRAY_SIZE(pdu_handlers
));
3447 handler
= pdu_handlers
[pdu
->id
];
3448 BUG_ON(handler
== NULL
);
3453 static void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
3455 V9fsState
*s
= (V9fsState
*)vdev
;
3459 while ((pdu
= alloc_pdu(s
)) &&
3460 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
3463 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
3464 BUG_ON(pdu
->elem
.out_sg
[0].iov_len
< 7);
3466 ptr
= pdu
->elem
.out_sg
[0].iov_base
;
3468 memcpy(&pdu
->size
, ptr
, 4);
3470 memcpy(&pdu
->tag
, ptr
+ 5, 2);
3478 static uint32_t virtio_9p_get_features(VirtIODevice
*vdev
, uint32_t features
)
3480 features
|= 1 << VIRTIO_9P_MOUNT_TAG
;
3484 static V9fsState
*to_virtio_9p(VirtIODevice
*vdev
)
3486 return (V9fsState
*)vdev
;
3489 static void virtio_9p_get_config(VirtIODevice
*vdev
, uint8_t *config
)
3491 struct virtio_9p_config
*cfg
;
3492 V9fsState
*s
= to_virtio_9p(vdev
);
3494 cfg
= qemu_mallocz(sizeof(struct virtio_9p_config
) +
3496 stw_raw(&cfg
->tag_len
, s
->tag_len
);
3497 memcpy(cfg
->tag
, s
->tag
, s
->tag_len
);
3498 memcpy(config
, cfg
, s
->config_size
);
3502 VirtIODevice
*virtio_9p_init(DeviceState
*dev
, V9fsConf
*conf
)
3510 s
= (V9fsState
*)virtio_common_init("virtio-9p",
3512 sizeof(struct virtio_9p_config
)+
3516 /* initialize pdu allocator */
3517 QLIST_INIT(&s
->free_list
);
3518 for (i
= 0; i
< (MAX_REQ
- 1); i
++) {
3519 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
3522 s
->vq
= virtio_add_queue(&s
->vdev
, MAX_REQ
, handle_9p_output
);
3524 fse
= get_fsdev_fsentry(conf
->fsdev_id
);
3527 /* We don't have a fsdev identified by fsdev_id */
3528 fprintf(stderr
, "Virtio-9p device couldn't find fsdev "
3529 "with the id %s\n", conf
->fsdev_id
);
3533 if (!fse
->path
|| !conf
->tag
) {
3534 /* we haven't specified a mount_tag or the path */
3535 fprintf(stderr
, "fsdev with id %s needs path "
3536 "and Virtio-9p device needs mount_tag arguments\n",
3541 if (!strcmp(fse
->security_model
, "passthrough")) {
3542 /* Files on the Fileserver set to client user credentials */
3543 s
->ctx
.fs_sm
= SM_PASSTHROUGH
;
3544 } else if (!strcmp(fse
->security_model
, "mapped")) {
3545 /* Files on the fileserver are set to QEMU credentials.
3546 * Client user credentials are saved in extended attributes.
3548 s
->ctx
.fs_sm
= SM_MAPPED
;
3549 } else if (!strcmp(fse
->security_model
, "none")) {
3551 * Files on the fileserver are set to QEMU credentials.
3553 s
->ctx
.fs_sm
= SM_NONE
;
3556 fprintf(stderr
, "Default to security_model=none. You may want"
3557 " enable advanced security model using "
3558 "security option:\n\t security_model=passthrough \n\t "
3559 "security_model=mapped\n");
3560 s
->ctx
.fs_sm
= SM_NONE
;
3563 if (lstat(fse
->path
, &stat
)) {
3564 fprintf(stderr
, "share path %s does not exist\n", fse
->path
);
3566 } else if (!S_ISDIR(stat
.st_mode
)) {
3567 fprintf(stderr
, "share path %s is not a directory \n", fse
->path
);
3571 s
->ctx
.fs_root
= qemu_strdup(fse
->path
);
3572 len
= strlen(conf
->tag
);
3573 if (len
> MAX_TAG_LEN
) {
3576 /* s->tag is non-NULL terminated string */
3577 s
->tag
= qemu_malloc(len
);
3578 memcpy(s
->tag
, conf
->tag
, len
);
3583 s
->vdev
.get_features
= virtio_9p_get_features
;
3584 s
->config_size
= sizeof(struct virtio_9p_config
) +
3586 s
->vdev
.get_config
= virtio_9p_get_config
;