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 GCC_FMT_ATTR(2, 0)
337 v9fs_string_alloc_printf(char **strp
, const char *fmt
, va_list ap
)
340 char *iter
= (char *)fmt
;
344 unsigned int arg_uint
;
346 /* Find the number of %'s that denotes an argument */
347 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
352 len
= strlen(fmt
) - 2*nr_args
;
362 /* Now parse the format string */
363 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
367 arg_uint
= va_arg(ap2
, unsigned int);
368 len
+= number_to_string((void *)&arg_uint
, 'u');
371 arg_char_ptr
= va_arg(ap2
, char *);
372 len
+= strlen(arg_char_ptr
);
379 "v9fs_string_alloc_printf:Incorrect format %c", *iter
);
386 *strp
= qemu_malloc((len
+ 1) * sizeof(**strp
));
388 return vsprintf(*strp
, fmt
, ap
);
391 static void GCC_FMT_ATTR(2, 3)
392 v9fs_string_sprintf(V9fsString
*str
, const char *fmt
, ...)
397 v9fs_string_free(str
);
400 err
= v9fs_string_alloc_printf(&str
->data
, fmt
, ap
);
407 static void v9fs_string_copy(V9fsString
*lhs
, V9fsString
*rhs
)
409 v9fs_string_free(lhs
);
410 v9fs_string_sprintf(lhs
, "%s", rhs
->data
);
413 static size_t v9fs_string_size(V9fsString
*str
)
418 static V9fsFidState
*lookup_fid(V9fsState
*s
, int32_t fid
)
422 for (f
= s
->fid_list
; f
; f
= f
->next
) {
431 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
435 f
= lookup_fid(s
, fid
);
440 f
= qemu_mallocz(sizeof(V9fsFidState
));
443 f
->fid_type
= P9_FID_NONE
;
445 f
->next
= s
->fid_list
;
451 static int v9fs_xattr_fid_clunk(V9fsState
*s
, V9fsFidState
*fidp
)
455 if (fidp
->fs
.xattr
.copied_len
== -1) {
456 /* getxattr/listxattr fid */
460 * if this is fid for setxattr. clunk should
461 * result in setxattr localcall
463 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
464 /* clunk after partial write */
468 if (fidp
->fs
.xattr
.len
) {
469 retval
= v9fs_do_lsetxattr(s
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
470 fidp
->fs
.xattr
.value
,
472 fidp
->fs
.xattr
.flags
);
474 retval
= v9fs_do_lremovexattr(s
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
477 v9fs_string_free(&fidp
->fs
.xattr
.name
);
479 if (fidp
->fs
.xattr
.value
) {
480 qemu_free(fidp
->fs
.xattr
.value
);
485 static int free_fid(V9fsState
*s
, int32_t fid
)
488 V9fsFidState
**fidpp
, *fidp
;
490 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
491 if ((*fidpp
)->fid
== fid
) {
496 if (*fidpp
== NULL
) {
503 if (fidp
->fid_type
== P9_FID_FILE
) {
504 v9fs_do_close(s
, fidp
->fs
.fd
);
505 } else if (fidp
->fid_type
== P9_FID_DIR
) {
506 v9fs_do_closedir(s
, fidp
->fs
.dir
);
507 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
508 retval
= v9fs_xattr_fid_clunk(s
, fidp
);
510 v9fs_string_free(&fidp
->path
);
516 #define P9_QID_TYPE_DIR 0x80
517 #define P9_QID_TYPE_SYMLINK 0x02
519 #define P9_STAT_MODE_DIR 0x80000000
520 #define P9_STAT_MODE_APPEND 0x40000000
521 #define P9_STAT_MODE_EXCL 0x20000000
522 #define P9_STAT_MODE_MOUNT 0x10000000
523 #define P9_STAT_MODE_AUTH 0x08000000
524 #define P9_STAT_MODE_TMP 0x04000000
525 #define P9_STAT_MODE_SYMLINK 0x02000000
526 #define P9_STAT_MODE_LINK 0x01000000
527 #define P9_STAT_MODE_DEVICE 0x00800000
528 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
529 #define P9_STAT_MODE_SOCKET 0x00100000
530 #define P9_STAT_MODE_SETUID 0x00080000
531 #define P9_STAT_MODE_SETGID 0x00040000
532 #define P9_STAT_MODE_SETVTX 0x00010000
534 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
535 P9_STAT_MODE_SYMLINK | \
536 P9_STAT_MODE_LINK | \
537 P9_STAT_MODE_DEVICE | \
538 P9_STAT_MODE_NAMED_PIPE | \
541 /* This is the algorithm from ufs in spfs */
542 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
546 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
547 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
548 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
550 if (S_ISDIR(stbuf
->st_mode
)) {
551 qidp
->type
|= P9_QID_TYPE_DIR
;
553 if (S_ISLNK(stbuf
->st_mode
)) {
554 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
558 static int fid_to_qid(V9fsState
*s
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
563 err
= v9fs_do_lstat(s
, &fidp
->path
, &stbuf
);
568 stat_to_qid(&stbuf
, qidp
);
572 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
576 if (!QLIST_EMPTY(&s
->free_list
)) {
577 pdu
= QLIST_FIRST(&s
->free_list
);
578 QLIST_REMOVE(pdu
, next
);
583 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
586 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
590 size_t pdu_packunpack(void *addr
, struct iovec
*sg
, int sg_count
,
591 size_t offset
, size_t size
, int pack
)
596 for (i
= 0; size
&& i
< sg_count
; i
++) {
598 if (offset
>= sg
[i
].iov_len
) {
600 offset
-= sg
[i
].iov_len
;
603 len
= MIN(sg
[i
].iov_len
- offset
, size
);
605 memcpy(sg
[i
].iov_base
+ offset
, addr
, len
);
607 memcpy(addr
, sg
[i
].iov_base
+ offset
, len
);
622 static size_t pdu_unpack(void *dst
, V9fsPDU
*pdu
, size_t offset
, size_t size
)
624 return pdu_packunpack(dst
, pdu
->elem
.out_sg
, pdu
->elem
.out_num
,
628 static size_t pdu_pack(V9fsPDU
*pdu
, size_t offset
, const void *src
,
631 return pdu_packunpack((void *)src
, pdu
->elem
.in_sg
, pdu
->elem
.in_num
,
635 static int pdu_copy_sg(V9fsPDU
*pdu
, size_t offset
, int rx
, struct iovec
*sg
)
639 struct iovec
*src_sg
;
643 src_sg
= pdu
->elem
.in_sg
;
644 num
= pdu
->elem
.in_num
;
646 src_sg
= pdu
->elem
.out_sg
;
647 num
= pdu
->elem
.out_num
;
651 for (i
= 0; i
< num
; i
++) {
653 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
654 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
656 } else if (offset
< (src_sg
[i
].iov_len
+ pos
)) {
657 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
658 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
659 sg
[j
].iov_base
+= (offset
- pos
);
660 sg
[j
].iov_len
-= (offset
- pos
);
663 pos
+= src_sg
[i
].iov_len
;
669 static size_t pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
671 size_t old_offset
= offset
;
676 for (i
= 0; fmt
[i
]; i
++) {
679 uint8_t *valp
= va_arg(ap
, uint8_t *);
680 offset
+= pdu_unpack(valp
, pdu
, offset
, sizeof(*valp
));
685 valp
= va_arg(ap
, uint16_t *);
686 val
= le16_to_cpupu(valp
);
687 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
693 valp
= va_arg(ap
, uint32_t *);
694 val
= le32_to_cpupu(valp
);
695 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
701 valp
= va_arg(ap
, uint64_t *);
702 val
= le64_to_cpup(valp
);
703 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
708 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
709 int *iovcnt
= va_arg(ap
, int *);
710 *iovcnt
= pdu_copy_sg(pdu
, offset
, 0, iov
);
714 V9fsString
*str
= va_arg(ap
, V9fsString
*);
715 offset
+= pdu_unmarshal(pdu
, offset
, "w", &str
->size
);
716 /* FIXME: sanity check str->size */
717 str
->data
= qemu_malloc(str
->size
+ 1);
718 offset
+= pdu_unpack(str
->data
, pdu
, offset
, str
->size
);
719 str
->data
[str
->size
] = 0;
723 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
724 offset
+= pdu_unmarshal(pdu
, offset
, "bdq",
725 &qidp
->type
, &qidp
->version
, &qidp
->path
);
729 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
730 offset
+= pdu_unmarshal(pdu
, offset
, "wwdQdddqsssssddd",
731 &statp
->size
, &statp
->type
, &statp
->dev
,
732 &statp
->qid
, &statp
->mode
, &statp
->atime
,
733 &statp
->mtime
, &statp
->length
,
734 &statp
->name
, &statp
->uid
, &statp
->gid
,
735 &statp
->muid
, &statp
->extension
,
736 &statp
->n_uid
, &statp
->n_gid
,
741 V9fsIattr
*iattr
= va_arg(ap
, V9fsIattr
*);
742 offset
+= pdu_unmarshal(pdu
, offset
, "ddddqqqqq",
743 &iattr
->valid
, &iattr
->mode
,
744 &iattr
->uid
, &iattr
->gid
, &iattr
->size
,
745 &iattr
->atime_sec
, &iattr
->atime_nsec
,
746 &iattr
->mtime_sec
, &iattr
->mtime_nsec
);
756 return offset
- old_offset
;
759 static size_t pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
761 size_t old_offset
= offset
;
766 for (i
= 0; fmt
[i
]; i
++) {
769 uint8_t val
= va_arg(ap
, int);
770 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
775 cpu_to_le16w(&val
, va_arg(ap
, int));
776 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
781 cpu_to_le32w(&val
, va_arg(ap
, uint32_t));
782 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
787 cpu_to_le64w(&val
, va_arg(ap
, uint64_t));
788 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
792 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
793 int *iovcnt
= va_arg(ap
, int *);
794 *iovcnt
= pdu_copy_sg(pdu
, offset
, 1, iov
);
798 V9fsString
*str
= va_arg(ap
, V9fsString
*);
799 offset
+= pdu_marshal(pdu
, offset
, "w", str
->size
);
800 offset
+= pdu_pack(pdu
, offset
, str
->data
, str
->size
);
804 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
805 offset
+= pdu_marshal(pdu
, offset
, "bdq",
806 qidp
->type
, qidp
->version
, qidp
->path
);
810 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
811 offset
+= pdu_marshal(pdu
, offset
, "wwdQdddqsssssddd",
812 statp
->size
, statp
->type
, statp
->dev
,
813 &statp
->qid
, statp
->mode
, statp
->atime
,
814 statp
->mtime
, statp
->length
, &statp
->name
,
815 &statp
->uid
, &statp
->gid
, &statp
->muid
,
816 &statp
->extension
, statp
->n_uid
,
817 statp
->n_gid
, statp
->n_muid
);
821 V9fsStatDotl
*statp
= va_arg(ap
, V9fsStatDotl
*);
822 offset
+= pdu_marshal(pdu
, offset
, "qQdddqqqqqqqqqqqqqqq",
823 statp
->st_result_mask
,
824 &statp
->qid
, statp
->st_mode
,
825 statp
->st_uid
, statp
->st_gid
,
826 statp
->st_nlink
, statp
->st_rdev
,
827 statp
->st_size
, statp
->st_blksize
, statp
->st_blocks
,
828 statp
->st_atime_sec
, statp
->st_atime_nsec
,
829 statp
->st_mtime_sec
, statp
->st_mtime_nsec
,
830 statp
->st_ctime_sec
, statp
->st_ctime_nsec
,
831 statp
->st_btime_sec
, statp
->st_btime_nsec
,
832 statp
->st_gen
, statp
->st_data_version
);
841 return offset
- old_offset
;
844 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
846 int8_t id
= pdu
->id
+ 1; /* Response */
852 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
855 str
.data
= strerror(err
);
856 str
.size
= strlen(str
.data
);
858 len
+= pdu_marshal(pdu
, len
, "s", &str
);
862 len
+= pdu_marshal(pdu
, len
, "d", err
);
864 if (s
->proto_version
== V9FS_PROTO_2000L
) {
869 /* fill out the header */
870 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
872 /* keep these in sync */
876 /* push onto queue and notify */
877 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
879 /* FIXME: we should batch these completions */
880 virtio_notify(&s
->vdev
, s
->vq
);
885 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
890 if (mode
& P9_STAT_MODE_DIR
) {
894 if (mode
& P9_STAT_MODE_SYMLINK
) {
897 if (mode
& P9_STAT_MODE_SOCKET
) {
900 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
903 if (mode
& P9_STAT_MODE_DEVICE
) {
904 if (extension
&& extension
->data
[0] == 'c') {
915 if (mode
& P9_STAT_MODE_SETUID
) {
918 if (mode
& P9_STAT_MODE_SETGID
) {
921 if (mode
& P9_STAT_MODE_SETVTX
) {
928 static int donttouch_stat(V9fsStat
*stat
)
930 if (stat
->type
== -1 &&
932 stat
->qid
.type
== -1 &&
933 stat
->qid
.version
== -1 &&
934 stat
->qid
.path
== -1 &&
938 stat
->length
== -1 &&
945 stat
->n_muid
== -1) {
952 static void v9fs_stat_free(V9fsStat
*stat
)
954 v9fs_string_free(&stat
->name
);
955 v9fs_string_free(&stat
->uid
);
956 v9fs_string_free(&stat
->gid
);
957 v9fs_string_free(&stat
->muid
);
958 v9fs_string_free(&stat
->extension
);
961 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
965 mode
= stbuf
->st_mode
& 0777;
966 if (S_ISDIR(stbuf
->st_mode
)) {
967 mode
|= P9_STAT_MODE_DIR
;
970 if (S_ISLNK(stbuf
->st_mode
)) {
971 mode
|= P9_STAT_MODE_SYMLINK
;
974 if (S_ISSOCK(stbuf
->st_mode
)) {
975 mode
|= P9_STAT_MODE_SOCKET
;
978 if (S_ISFIFO(stbuf
->st_mode
)) {
979 mode
|= P9_STAT_MODE_NAMED_PIPE
;
982 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
983 mode
|= P9_STAT_MODE_DEVICE
;
986 if (stbuf
->st_mode
& S_ISUID
) {
987 mode
|= P9_STAT_MODE_SETUID
;
990 if (stbuf
->st_mode
& S_ISGID
) {
991 mode
|= P9_STAT_MODE_SETGID
;
994 if (stbuf
->st_mode
& S_ISVTX
) {
995 mode
|= P9_STAT_MODE_SETVTX
;
1001 static int stat_to_v9stat(V9fsState
*s
, V9fsString
*name
,
1002 const struct stat
*stbuf
,
1008 memset(v9stat
, 0, sizeof(*v9stat
));
1010 stat_to_qid(stbuf
, &v9stat
->qid
);
1011 v9stat
->mode
= stat_to_v9mode(stbuf
);
1012 v9stat
->atime
= stbuf
->st_atime
;
1013 v9stat
->mtime
= stbuf
->st_mtime
;
1014 v9stat
->length
= stbuf
->st_size
;
1016 v9fs_string_null(&v9stat
->uid
);
1017 v9fs_string_null(&v9stat
->gid
);
1018 v9fs_string_null(&v9stat
->muid
);
1020 v9stat
->n_uid
= stbuf
->st_uid
;
1021 v9stat
->n_gid
= stbuf
->st_gid
;
1024 v9fs_string_null(&v9stat
->extension
);
1026 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
1027 err
= v9fs_do_readlink(s
, name
, &v9stat
->extension
);
1032 v9stat
->extension
.data
[err
] = 0;
1033 v9stat
->extension
.size
= err
;
1034 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
1035 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
1036 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
1037 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
1038 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
1039 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
1040 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
1043 str
= strrchr(name
->data
, '/');
1050 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
1053 v9fs_string_size(&v9stat
->name
) +
1054 v9fs_string_size(&v9stat
->uid
) +
1055 v9fs_string_size(&v9stat
->gid
) +
1056 v9fs_string_size(&v9stat
->muid
) +
1057 v9fs_string_size(&v9stat
->extension
);
1061 #define P9_STATS_MODE 0x00000001ULL
1062 #define P9_STATS_NLINK 0x00000002ULL
1063 #define P9_STATS_UID 0x00000004ULL
1064 #define P9_STATS_GID 0x00000008ULL
1065 #define P9_STATS_RDEV 0x00000010ULL
1066 #define P9_STATS_ATIME 0x00000020ULL
1067 #define P9_STATS_MTIME 0x00000040ULL
1068 #define P9_STATS_CTIME 0x00000080ULL
1069 #define P9_STATS_INO 0x00000100ULL
1070 #define P9_STATS_SIZE 0x00000200ULL
1071 #define P9_STATS_BLOCKS 0x00000400ULL
1073 #define P9_STATS_BTIME 0x00000800ULL
1074 #define P9_STATS_GEN 0x00001000ULL
1075 #define P9_STATS_DATA_VERSION 0x00002000ULL
1077 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1078 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1081 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
1082 V9fsStatDotl
*v9lstat
)
1084 memset(v9lstat
, 0, sizeof(*v9lstat
));
1086 v9lstat
->st_mode
= stbuf
->st_mode
;
1087 v9lstat
->st_nlink
= stbuf
->st_nlink
;
1088 v9lstat
->st_uid
= stbuf
->st_uid
;
1089 v9lstat
->st_gid
= stbuf
->st_gid
;
1090 v9lstat
->st_rdev
= stbuf
->st_rdev
;
1091 v9lstat
->st_size
= stbuf
->st_size
;
1092 v9lstat
->st_blksize
= stbuf
->st_blksize
;
1093 v9lstat
->st_blocks
= stbuf
->st_blocks
;
1094 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
1095 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
1096 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
1097 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
1098 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
1099 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
1100 /* Currently we only support BASIC fields in stat */
1101 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
1103 stat_to_qid(stbuf
, &v9lstat
->qid
);
1106 static struct iovec
*adjust_sg(struct iovec
*sg
, int len
, int *iovcnt
)
1108 while (len
&& *iovcnt
) {
1109 if (len
< sg
->iov_len
) {
1111 sg
->iov_base
+= len
;
1123 static struct iovec
*cap_sg(struct iovec
*sg
, int cap
, int *cnt
)
1128 for (i
= 0; i
< *cnt
; i
++) {
1129 if ((total
+ sg
[i
].iov_len
) > cap
) {
1130 sg
[i
].iov_len
-= ((total
+ sg
[i
].iov_len
) - cap
);
1134 total
+= sg
[i
].iov_len
;
1142 static void print_sg(struct iovec
*sg
, int cnt
)
1146 printf("sg[%d]: {", cnt
);
1147 for (i
= 0; i
< cnt
; i
++) {
1151 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
1156 static void v9fs_fix_path(V9fsString
*dst
, V9fsString
*src
, int len
)
1159 v9fs_string_init(&str
);
1160 v9fs_string_copy(&str
, dst
);
1161 v9fs_string_sprintf(dst
, "%s%s", src
->data
, str
.data
+len
);
1162 v9fs_string_free(&str
);
1165 static void v9fs_version(V9fsState
*s
, V9fsPDU
*pdu
)
1170 pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
1172 if (!strcmp(version
.data
, "9P2000.u")) {
1173 s
->proto_version
= V9FS_PROTO_2000U
;
1174 } else if (!strcmp(version
.data
, "9P2000.L")) {
1175 s
->proto_version
= V9FS_PROTO_2000L
;
1177 v9fs_string_sprintf(&version
, "unknown");
1180 offset
+= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
1181 complete_pdu(s
, pdu
, offset
);
1183 v9fs_string_free(&version
);
1186 static void v9fs_attach(V9fsState
*s
, V9fsPDU
*pdu
)
1188 int32_t fid
, afid
, n_uname
;
1189 V9fsString uname
, aname
;
1195 pdu_unmarshal(pdu
, offset
, "ddssd", &fid
, &afid
, &uname
, &aname
, &n_uname
);
1197 fidp
= alloc_fid(s
, fid
);
1203 fidp
->uid
= n_uname
;
1205 v9fs_string_sprintf(&fidp
->path
, "%s", "/");
1206 err
= fid_to_qid(s
, fidp
, &qid
);
1213 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
1217 complete_pdu(s
, pdu
, err
);
1218 v9fs_string_free(&uname
);
1219 v9fs_string_free(&aname
);
1222 static void v9fs_stat_post_lstat(V9fsState
*s
, V9fsStatState
*vs
, int err
)
1229 err
= stat_to_v9stat(s
, &vs
->fidp
->path
, &vs
->stbuf
, &vs
->v9stat
);
1233 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "wS", 0, &vs
->v9stat
);
1237 complete_pdu(s
, vs
->pdu
, err
);
1238 v9fs_stat_free(&vs
->v9stat
);
1242 static void v9fs_stat(V9fsState
*s
, V9fsPDU
*pdu
)
1248 vs
= qemu_malloc(sizeof(*vs
));
1252 memset(&vs
->v9stat
, 0, sizeof(vs
->v9stat
));
1254 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &fid
);
1256 vs
->fidp
= lookup_fid(s
, fid
);
1257 if (vs
->fidp
== NULL
) {
1262 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1263 v9fs_stat_post_lstat(s
, vs
, err
);
1267 complete_pdu(s
, vs
->pdu
, err
);
1268 v9fs_stat_free(&vs
->v9stat
);
1272 static void v9fs_getattr_post_lstat(V9fsState
*s
, V9fsStatStateDotl
*vs
,
1280 stat_to_v9stat_dotl(s
, &vs
->stbuf
, &vs
->v9stat_dotl
);
1281 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "A", &vs
->v9stat_dotl
);
1285 complete_pdu(s
, vs
->pdu
, err
);
1289 static void v9fs_getattr(V9fsState
*s
, V9fsPDU
*pdu
)
1292 V9fsStatStateDotl
*vs
;
1295 uint64_t request_mask
;
1297 vs
= qemu_malloc(sizeof(*vs
));
1301 memset(&vs
->v9stat_dotl
, 0, sizeof(vs
->v9stat_dotl
));
1303 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dq", &fid
, &request_mask
);
1305 fidp
= lookup_fid(s
, fid
);
1311 /* Currently we only support BASIC fields in stat, so there is no
1312 * need to look at request_mask.
1314 err
= v9fs_do_lstat(s
, &fidp
->path
, &vs
->stbuf
);
1315 v9fs_getattr_post_lstat(s
, vs
, err
);
1319 complete_pdu(s
, vs
->pdu
, err
);
1323 /* From Linux kernel code */
1324 #define ATTR_MODE (1 << 0)
1325 #define ATTR_UID (1 << 1)
1326 #define ATTR_GID (1 << 2)
1327 #define ATTR_SIZE (1 << 3)
1328 #define ATTR_ATIME (1 << 4)
1329 #define ATTR_MTIME (1 << 5)
1330 #define ATTR_CTIME (1 << 6)
1331 #define ATTR_MASK 127
1332 #define ATTR_ATIME_SET (1 << 7)
1333 #define ATTR_MTIME_SET (1 << 8)
1335 static void v9fs_setattr_post_truncate(V9fsState
*s
, V9fsSetattrState
*vs
,
1345 complete_pdu(s
, vs
->pdu
, err
);
1349 static void v9fs_setattr_post_chown(V9fsState
*s
, V9fsSetattrState
*vs
, int err
)
1356 if (vs
->v9iattr
.valid
& (ATTR_SIZE
)) {
1357 err
= v9fs_do_truncate(s
, &vs
->fidp
->path
, vs
->v9iattr
.size
);
1359 v9fs_setattr_post_truncate(s
, vs
, err
);
1363 complete_pdu(s
, vs
->pdu
, err
);
1367 static void v9fs_setattr_post_utimensat(V9fsState
*s
, V9fsSetattrState
*vs
,
1375 /* If the only valid entry in iattr is ctime we can call
1376 * chown(-1,-1) to update the ctime of the file
1378 if ((vs
->v9iattr
.valid
& (ATTR_UID
| ATTR_GID
)) ||
1379 ((vs
->v9iattr
.valid
& ATTR_CTIME
)
1380 && !((vs
->v9iattr
.valid
& ATTR_MASK
) & ~ATTR_CTIME
))) {
1381 if (!(vs
->v9iattr
.valid
& ATTR_UID
)) {
1382 vs
->v9iattr
.uid
= -1;
1384 if (!(vs
->v9iattr
.valid
& ATTR_GID
)) {
1385 vs
->v9iattr
.gid
= -1;
1387 err
= v9fs_do_chown(s
, &vs
->fidp
->path
, vs
->v9iattr
.uid
,
1390 v9fs_setattr_post_chown(s
, vs
, err
);
1394 complete_pdu(s
, vs
->pdu
, err
);
1398 static void v9fs_setattr_post_chmod(V9fsState
*s
, V9fsSetattrState
*vs
, int err
)
1405 if (vs
->v9iattr
.valid
& (ATTR_ATIME
| ATTR_MTIME
)) {
1406 struct timespec times
[2];
1407 if (vs
->v9iattr
.valid
& ATTR_ATIME
) {
1408 if (vs
->v9iattr
.valid
& ATTR_ATIME_SET
) {
1409 times
[0].tv_sec
= vs
->v9iattr
.atime_sec
;
1410 times
[0].tv_nsec
= vs
->v9iattr
.atime_nsec
;
1412 times
[0].tv_nsec
= UTIME_NOW
;
1415 times
[0].tv_nsec
= UTIME_OMIT
;
1418 if (vs
->v9iattr
.valid
& ATTR_MTIME
) {
1419 if (vs
->v9iattr
.valid
& ATTR_MTIME_SET
) {
1420 times
[1].tv_sec
= vs
->v9iattr
.mtime_sec
;
1421 times
[1].tv_nsec
= vs
->v9iattr
.mtime_nsec
;
1423 times
[1].tv_nsec
= UTIME_NOW
;
1426 times
[1].tv_nsec
= UTIME_OMIT
;
1428 err
= v9fs_do_utimensat(s
, &vs
->fidp
->path
, times
);
1430 v9fs_setattr_post_utimensat(s
, vs
, err
);
1434 complete_pdu(s
, vs
->pdu
, err
);
1438 static void v9fs_setattr(V9fsState
*s
, V9fsPDU
*pdu
)
1441 V9fsSetattrState
*vs
;
1444 vs
= qemu_malloc(sizeof(*vs
));
1448 pdu_unmarshal(pdu
, vs
->offset
, "dI", &fid
, &vs
->v9iattr
);
1450 vs
->fidp
= lookup_fid(s
, fid
);
1451 if (vs
->fidp
== NULL
) {
1456 if (vs
->v9iattr
.valid
& ATTR_MODE
) {
1457 err
= v9fs_do_chmod(s
, &vs
->fidp
->path
, vs
->v9iattr
.mode
);
1460 v9fs_setattr_post_chmod(s
, vs
, err
);
1464 complete_pdu(s
, vs
->pdu
, err
);
1468 static void v9fs_walk_complete(V9fsState
*s
, V9fsWalkState
*vs
, int err
)
1470 complete_pdu(s
, vs
->pdu
, err
);
1473 for (vs
->name_idx
= 0; vs
->name_idx
< vs
->nwnames
; vs
->name_idx
++) {
1474 v9fs_string_free(&vs
->wnames
[vs
->name_idx
]);
1477 qemu_free(vs
->wnames
);
1478 qemu_free(vs
->qids
);
1482 static void v9fs_walk_marshal(V9fsWalkState
*vs
)
1486 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "w", vs
->nwnames
);
1488 for (i
= 0; i
< vs
->nwnames
; i
++) {
1489 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qids
[i
]);
1493 static void v9fs_walk_post_newfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
1497 free_fid(s
, vs
->newfidp
->fid
);
1498 v9fs_string_free(&vs
->path
);
1503 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1506 if (vs
->name_idx
< vs
->nwnames
) {
1507 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1508 vs
->wnames
[vs
->name_idx
].data
);
1509 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1511 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1512 v9fs_walk_post_newfid_lstat(s
, vs
, err
);
1516 v9fs_string_free(&vs
->path
);
1517 v9fs_walk_marshal(vs
);
1520 v9fs_walk_complete(s
, vs
, err
);
1523 static void v9fs_walk_post_oldfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
1527 v9fs_string_free(&vs
->path
);
1532 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1534 if (vs
->name_idx
< vs
->nwnames
) {
1536 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1537 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1538 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1540 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1541 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1545 v9fs_string_free(&vs
->path
);
1546 v9fs_walk_marshal(vs
);
1549 v9fs_walk_complete(s
, vs
, err
);
1552 static void v9fs_walk(V9fsState
*s
, V9fsPDU
*pdu
)
1554 int32_t fid
, newfid
;
1559 vs
= qemu_malloc(sizeof(*vs
));
1565 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "ddw", &fid
,
1566 &newfid
, &vs
->nwnames
);
1569 vs
->wnames
= qemu_mallocz(sizeof(vs
->wnames
[0]) * vs
->nwnames
);
1571 vs
->qids
= qemu_mallocz(sizeof(vs
->qids
[0]) * vs
->nwnames
);
1573 for (i
= 0; i
< vs
->nwnames
; i
++) {
1574 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "s",
1579 vs
->fidp
= lookup_fid(s
, fid
);
1580 if (vs
->fidp
== NULL
) {
1585 /* FIXME: is this really valid? */
1586 if (fid
== newfid
) {
1588 BUG_ON(vs
->fidp
->fid_type
!= P9_FID_NONE
);
1589 v9fs_string_init(&vs
->path
);
1592 if (vs
->name_idx
< vs
->nwnames
) {
1593 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1594 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1595 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1597 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1598 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1602 vs
->newfidp
= alloc_fid(s
, newfid
);
1603 if (vs
->newfidp
== NULL
) {
1608 vs
->newfidp
->uid
= vs
->fidp
->uid
;
1609 v9fs_string_init(&vs
->path
);
1611 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->fidp
->path
);
1613 if (vs
->name_idx
< vs
->nwnames
) {
1614 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1615 vs
->wnames
[vs
->name_idx
].data
);
1616 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1618 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1619 v9fs_walk_post_newfid_lstat(s
, vs
, err
);
1624 v9fs_walk_marshal(vs
);
1627 v9fs_walk_complete(s
, vs
, err
);
1630 static int32_t get_iounit(V9fsState
*s
, V9fsString
*name
)
1632 struct statfs stbuf
;
1636 * iounit should be multiples of f_bsize (host filesystem block size
1637 * and as well as less than (client msize - P9_IOHDRSZ))
1639 if (!v9fs_do_statfs(s
, name
, &stbuf
)) {
1640 iounit
= stbuf
.f_bsize
;
1641 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1645 iounit
= s
->msize
- P9_IOHDRSZ
;
1650 static void v9fs_open_post_opendir(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1652 if (vs
->fidp
->fs
.dir
== NULL
) {
1656 vs
->fidp
->fid_type
= P9_FID_DIR
;
1657 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, 0);
1660 complete_pdu(s
, vs
->pdu
, err
);
1665 static void v9fs_open_post_getiounit(V9fsState
*s
, V9fsOpenState
*vs
)
1668 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, vs
->iounit
);
1670 complete_pdu(s
, vs
->pdu
, err
);
1674 static void v9fs_open_post_open(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1676 if (vs
->fidp
->fs
.fd
== -1) {
1680 vs
->fidp
->fid_type
= P9_FID_FILE
;
1681 vs
->iounit
= get_iounit(s
, &vs
->fidp
->path
);
1682 v9fs_open_post_getiounit(s
, vs
);
1685 complete_pdu(s
, vs
->pdu
, err
);
1689 static void v9fs_open_post_lstat(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1698 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
1700 if (S_ISDIR(vs
->stbuf
.st_mode
)) {
1701 vs
->fidp
->fs
.dir
= v9fs_do_opendir(s
, &vs
->fidp
->path
);
1702 v9fs_open_post_opendir(s
, vs
, err
);
1704 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1706 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
1708 flags
= omode_to_uflags(vs
->mode
);
1710 vs
->fidp
->fs
.fd
= v9fs_do_open(s
, &vs
->fidp
->path
, flags
);
1711 v9fs_open_post_open(s
, vs
, err
);
1715 complete_pdu(s
, vs
->pdu
, err
);
1719 static void v9fs_open(V9fsState
*s
, V9fsPDU
*pdu
)
1725 vs
= qemu_malloc(sizeof(*vs
));
1730 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1731 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dd", &fid
, &vs
->mode
);
1733 pdu_unmarshal(vs
->pdu
, vs
->offset
, "db", &fid
, &vs
->mode
);
1736 vs
->fidp
= lookup_fid(s
, fid
);
1737 if (vs
->fidp
== NULL
) {
1742 BUG_ON(vs
->fidp
->fid_type
!= P9_FID_NONE
);
1744 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1746 v9fs_open_post_lstat(s
, vs
, err
);
1749 complete_pdu(s
, pdu
, err
);
1753 static void v9fs_post_lcreate(V9fsState
*s
, V9fsLcreateState
*vs
, int err
)
1756 v9fs_string_copy(&vs
->fidp
->path
, &vs
->fullname
);
1757 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
1758 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
,
1762 vs
->fidp
->fid_type
= P9_FID_NONE
;
1764 if (vs
->fidp
->fs
.fd
> 0) {
1765 close(vs
->fidp
->fs
.fd
);
1769 complete_pdu(s
, vs
->pdu
, err
);
1770 v9fs_string_free(&vs
->name
);
1771 v9fs_string_free(&vs
->fullname
);
1775 static void v9fs_lcreate_post_get_iounit(V9fsState
*s
, V9fsLcreateState
*vs
,
1782 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
1785 v9fs_post_lcreate(s
, vs
, err
);
1788 static void v9fs_lcreate_post_do_open2(V9fsState
*s
, V9fsLcreateState
*vs
,
1791 if (vs
->fidp
->fs
.fd
== -1) {
1795 vs
->fidp
->fid_type
= P9_FID_FILE
;
1796 vs
->iounit
= get_iounit(s
, &vs
->fullname
);
1797 v9fs_lcreate_post_get_iounit(s
, vs
, err
);
1801 v9fs_post_lcreate(s
, vs
, err
);
1804 static void v9fs_lcreate(V9fsState
*s
, V9fsPDU
*pdu
)
1806 int32_t dfid
, flags
, mode
;
1808 V9fsLcreateState
*vs
;
1811 vs
= qemu_malloc(sizeof(*vs
));
1815 v9fs_string_init(&vs
->fullname
);
1817 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsddd", &dfid
, &vs
->name
, &flags
,
1820 vs
->fidp
= lookup_fid(s
, dfid
);
1821 if (vs
->fidp
== NULL
) {
1826 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", vs
->fidp
->path
.data
,
1829 vs
->fidp
->fs
.fd
= v9fs_do_open2(s
, vs
->fullname
.data
, vs
->fidp
->uid
,
1831 v9fs_lcreate_post_do_open2(s
, vs
, err
);
1835 complete_pdu(s
, vs
->pdu
, err
);
1836 v9fs_string_free(&vs
->name
);
1840 static void v9fs_post_do_fsync(V9fsState
*s
, V9fsPDU
*pdu
, int err
)
1845 complete_pdu(s
, pdu
, err
);
1848 static void v9fs_fsync(V9fsState
*s
, V9fsPDU
*pdu
)
1855 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1856 fidp
= lookup_fid(s
, fid
);
1859 v9fs_post_do_fsync(s
, pdu
, err
);
1862 err
= v9fs_do_fsync(s
, fidp
->fs
.fd
);
1863 v9fs_post_do_fsync(s
, pdu
, err
);
1866 static void v9fs_clunk(V9fsState
*s
, V9fsPDU
*pdu
)
1872 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1874 err
= free_fid(s
, fid
);
1882 complete_pdu(s
, pdu
, err
);
1885 static void v9fs_read_post_readdir(V9fsState
*, V9fsReadState
*, ssize_t
);
1887 static void v9fs_read_post_seekdir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1892 v9fs_stat_free(&vs
->v9stat
);
1893 v9fs_string_free(&vs
->name
);
1894 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
1895 vs
->offset
+= vs
->count
;
1898 complete_pdu(s
, vs
->pdu
, err
);
1903 static void v9fs_read_post_dir_lstat(V9fsState
*s
, V9fsReadState
*vs
,
1910 err
= stat_to_v9stat(s
, &vs
->name
, &vs
->stbuf
, &vs
->v9stat
);
1915 vs
->len
= pdu_marshal(vs
->pdu
, vs
->offset
+ 4 + vs
->count
, "S",
1917 if ((vs
->len
!= (vs
->v9stat
.size
+ 2)) ||
1918 ((vs
->count
+ vs
->len
) > vs
->max_count
)) {
1919 v9fs_do_seekdir(s
, vs
->fidp
->fs
.dir
, vs
->dir_pos
);
1920 v9fs_read_post_seekdir(s
, vs
, err
);
1923 vs
->count
+= vs
->len
;
1924 v9fs_stat_free(&vs
->v9stat
);
1925 v9fs_string_free(&vs
->name
);
1926 vs
->dir_pos
= vs
->dent
->d_off
;
1927 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->fs
.dir
);
1928 v9fs_read_post_readdir(s
, vs
, err
);
1931 v9fs_do_seekdir(s
, vs
->fidp
->fs
.dir
, vs
->dir_pos
);
1932 v9fs_read_post_seekdir(s
, vs
, err
);
1937 static void v9fs_read_post_readdir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1940 memset(&vs
->v9stat
, 0, sizeof(vs
->v9stat
));
1941 v9fs_string_init(&vs
->name
);
1942 v9fs_string_sprintf(&vs
->name
, "%s/%s", vs
->fidp
->path
.data
,
1944 err
= v9fs_do_lstat(s
, &vs
->name
, &vs
->stbuf
);
1945 v9fs_read_post_dir_lstat(s
, vs
, err
);
1949 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
1950 vs
->offset
+= vs
->count
;
1952 complete_pdu(s
, vs
->pdu
, err
);
1957 static void v9fs_read_post_telldir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1959 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->fs
.dir
);
1960 v9fs_read_post_readdir(s
, vs
, err
);
1964 static void v9fs_read_post_rewinddir(V9fsState
*s
, V9fsReadState
*vs
,
1967 vs
->dir_pos
= v9fs_do_telldir(s
, vs
->fidp
->fs
.dir
);
1968 v9fs_read_post_telldir(s
, vs
, err
);
1972 static void v9fs_read_post_readv(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1975 /* IO error return the error */
1979 vs
->total
+= vs
->len
;
1980 vs
->sg
= adjust_sg(vs
->sg
, vs
->len
, &vs
->cnt
);
1981 if (vs
->total
< vs
->count
&& vs
->len
> 0) {
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
);
1994 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->total
);
1995 vs
->offset
+= vs
->count
;
1999 complete_pdu(s
, vs
->pdu
, err
);
2003 static void v9fs_read_post_lseek(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
2009 vs
->sg
= cap_sg(vs
->sg
, vs
->count
, &vs
->cnt
);
2011 if (vs
->total
< vs
->count
) {
2014 print_sg(vs
->sg
, vs
->cnt
);
2016 vs
->len
= v9fs_do_readv(s
, vs
->fidp
->fs
.fd
, vs
->sg
, vs
->cnt
);
2017 } while (vs
->len
== -1 && errno
== EINTR
);
2018 if (vs
->len
== -1) {
2021 v9fs_read_post_readv(s
, vs
, err
);
2025 complete_pdu(s
, vs
->pdu
, err
);
2029 static void v9fs_xattr_read(V9fsState
*s
, V9fsReadState
*vs
)
2035 xattr_len
= vs
->fidp
->fs
.xattr
.len
;
2036 read_count
= xattr_len
- vs
->off
;
2037 if (read_count
> vs
->count
) {
2038 read_count
= vs
->count
;
2039 } else if (read_count
< 0) {
2041 * read beyond XATTR value
2045 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", read_count
);
2046 vs
->offset
+= pdu_pack(vs
->pdu
, vs
->offset
,
2047 ((char *)vs
->fidp
->fs
.xattr
.value
) + vs
->off
,
2050 complete_pdu(s
, vs
->pdu
, err
);
2054 static void v9fs_read(V9fsState
*s
, V9fsPDU
*pdu
)
2060 vs
= qemu_malloc(sizeof(*vs
));
2067 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dqd", &fid
, &vs
->off
, &vs
->count
);
2069 vs
->fidp
= lookup_fid(s
, fid
);
2070 if (vs
->fidp
== NULL
) {
2075 if (vs
->fidp
->fid_type
== P9_FID_DIR
) {
2076 vs
->max_count
= vs
->count
;
2079 v9fs_do_rewinddir(s
, vs
->fidp
->fs
.dir
);
2081 v9fs_read_post_rewinddir(s
, vs
, err
);
2083 } else if (vs
->fidp
->fid_type
== P9_FID_FILE
) {
2085 pdu_marshal(vs
->pdu
, vs
->offset
+ 4, "v", vs
->sg
, &vs
->cnt
);
2086 err
= v9fs_do_lseek(s
, vs
->fidp
->fs
.fd
, vs
->off
, SEEK_SET
);
2087 v9fs_read_post_lseek(s
, vs
, err
);
2089 } else if (vs
->fidp
->fid_type
== P9_FID_XATTR
) {
2090 v9fs_xattr_read(s
, vs
);
2096 complete_pdu(s
, pdu
, err
);
2100 typedef struct V9fsReadDirState
{
2104 off_t saved_dir_pos
;
2105 struct dirent
*dent
;
2109 int64_t initial_offset
;
2113 static void v9fs_readdir_post_seekdir(V9fsState
*s
, V9fsReadDirState
*vs
)
2115 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
2116 vs
->offset
+= vs
->count
;
2117 complete_pdu(s
, vs
->pdu
, vs
->offset
);
2122 /* Size of each dirent on the wire: size of qid (13) + size of offset (8)
2123 * size of type (1) + size of name.size (2) + strlen(name.data)
2125 #define V9_READDIR_DATA_SZ (24 + strlen(vs->name.data))
2127 static void v9fs_readdir_post_readdir(V9fsState
*s
, V9fsReadDirState
*vs
)
2133 v9fs_string_init(&vs
->name
);
2134 v9fs_string_sprintf(&vs
->name
, "%s", vs
->dent
->d_name
);
2136 if ((vs
->count
+ V9_READDIR_DATA_SZ
) > vs
->max_count
) {
2137 /* Ran out of buffer. Set dir back to old position and return */
2138 v9fs_do_seekdir(s
, vs
->fidp
->fs
.dir
, vs
->saved_dir_pos
);
2139 v9fs_readdir_post_seekdir(s
, vs
);
2143 /* Fill up just the path field of qid because the client uses
2144 * only that. To fill the entire qid structure we will have
2145 * to stat each dirent found, which is expensive
2147 size
= MIN(sizeof(vs
->dent
->d_ino
), sizeof(vs
->qid
.path
));
2148 memcpy(&vs
->qid
.path
, &vs
->dent
->d_ino
, size
);
2149 /* Fill the other fields with dummy values */
2151 vs
->qid
.version
= 0;
2153 len
= pdu_marshal(vs
->pdu
, vs
->offset
+4+vs
->count
, "Qqbs",
2154 &vs
->qid
, vs
->dent
->d_off
,
2155 vs
->dent
->d_type
, &vs
->name
);
2157 v9fs_string_free(&vs
->name
);
2158 vs
->saved_dir_pos
= vs
->dent
->d_off
;
2159 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->fs
.dir
);
2160 v9fs_readdir_post_readdir(s
, vs
);
2164 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
2165 vs
->offset
+= vs
->count
;
2166 complete_pdu(s
, vs
->pdu
, vs
->offset
);
2171 static void v9fs_readdir_post_telldir(V9fsState
*s
, V9fsReadDirState
*vs
)
2173 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->fs
.dir
);
2174 v9fs_readdir_post_readdir(s
, vs
);
2178 static void v9fs_readdir_post_setdir(V9fsState
*s
, V9fsReadDirState
*vs
)
2180 vs
->saved_dir_pos
= v9fs_do_telldir(s
, vs
->fidp
->fs
.dir
);
2181 v9fs_readdir_post_telldir(s
, vs
);
2185 static void v9fs_readdir(V9fsState
*s
, V9fsPDU
*pdu
)
2188 V9fsReadDirState
*vs
;
2192 vs
= qemu_malloc(sizeof(*vs
));
2197 pdu_unmarshal(vs
->pdu
, offset
, "dqd", &fid
, &vs
->initial_offset
,
2200 vs
->fidp
= lookup_fid(s
, fid
);
2201 if (vs
->fidp
== NULL
|| !(vs
->fidp
->fs
.dir
)) {
2206 if (vs
->initial_offset
== 0) {
2207 v9fs_do_rewinddir(s
, vs
->fidp
->fs
.dir
);
2209 v9fs_do_seekdir(s
, vs
->fidp
->fs
.dir
, vs
->initial_offset
);
2212 v9fs_readdir_post_setdir(s
, vs
);
2216 complete_pdu(s
, pdu
, err
);
2221 static void v9fs_write_post_writev(V9fsState
*s
, V9fsWriteState
*vs
,
2225 /* IO error return the error */
2229 vs
->total
+= vs
->len
;
2230 vs
->sg
= adjust_sg(vs
->sg
, vs
->len
, &vs
->cnt
);
2231 if (vs
->total
< vs
->count
&& vs
->len
> 0) {
2234 print_sg(vs
->sg
, vs
->cnt
);
2236 vs
->len
= v9fs_do_writev(s
, vs
->fidp
->fs
.fd
, vs
->sg
, vs
->cnt
);
2237 } while (vs
->len
== -1 && errno
== EINTR
);
2238 if (vs
->len
== -1) {
2241 v9fs_write_post_writev(s
, vs
, err
);
2244 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->total
);
2248 complete_pdu(s
, vs
->pdu
, err
);
2252 static void v9fs_write_post_lseek(V9fsState
*s
, V9fsWriteState
*vs
, ssize_t err
)
2258 vs
->sg
= cap_sg(vs
->sg
, vs
->count
, &vs
->cnt
);
2260 if (vs
->total
< vs
->count
) {
2263 print_sg(vs
->sg
, vs
->cnt
);
2265 vs
->len
= v9fs_do_writev(s
, vs
->fidp
->fs
.fd
, vs
->sg
, vs
->cnt
);
2266 } while (vs
->len
== -1 && errno
== EINTR
);
2267 if (vs
->len
== -1) {
2270 v9fs_write_post_writev(s
, vs
, err
);
2275 complete_pdu(s
, vs
->pdu
, err
);
2279 static void v9fs_xattr_write(V9fsState
*s
, V9fsWriteState
*vs
)
2286 xattr_len
= vs
->fidp
->fs
.xattr
.len
;
2287 write_count
= xattr_len
- vs
->off
;
2288 if (write_count
> vs
->count
) {
2289 write_count
= vs
->count
;
2290 } else if (write_count
< 0) {
2292 * write beyond XATTR value len specified in
2298 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", write_count
);
2300 vs
->fidp
->fs
.xattr
.copied_len
+= write_count
;
2302 * Now copy the content from sg list
2304 for (i
= 0; i
< vs
->cnt
; i
++) {
2305 if (write_count
> vs
->sg
[i
].iov_len
) {
2306 to_copy
= vs
->sg
[i
].iov_len
;
2308 to_copy
= write_count
;
2310 memcpy((char *)vs
->fidp
->fs
.xattr
.value
+ vs
->off
,
2311 vs
->sg
[i
].iov_base
, to_copy
);
2312 /* updating vs->off since we are not using below */
2314 write_count
-= to_copy
;
2317 complete_pdu(s
, vs
->pdu
, err
);
2321 static void v9fs_write(V9fsState
*s
, V9fsPDU
*pdu
)
2327 vs
= qemu_malloc(sizeof(*vs
));
2335 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dqdv", &fid
, &vs
->off
, &vs
->count
,
2338 vs
->fidp
= lookup_fid(s
, fid
);
2339 if (vs
->fidp
== NULL
) {
2344 if (vs
->fidp
->fid_type
== P9_FID_FILE
) {
2345 if (vs
->fidp
->fs
.fd
== -1) {
2349 } else if (vs
->fidp
->fid_type
== P9_FID_XATTR
) {
2351 * setxattr operation
2353 v9fs_xattr_write(s
, vs
);
2359 err
= v9fs_do_lseek(s
, vs
->fidp
->fs
.fd
, vs
->off
, SEEK_SET
);
2361 v9fs_write_post_lseek(s
, vs
, err
);
2365 complete_pdu(s
, vs
->pdu
, err
);
2369 static void v9fs_create_post_getiounit(V9fsState
*s
, V9fsCreateState
*vs
)
2372 v9fs_string_copy(&vs
->fidp
->path
, &vs
->fullname
);
2373 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
2375 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, vs
->iounit
);
2378 complete_pdu(s
, vs
->pdu
, err
);
2379 v9fs_string_free(&vs
->name
);
2380 v9fs_string_free(&vs
->extension
);
2381 v9fs_string_free(&vs
->fullname
);
2385 static void v9fs_post_create(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2388 vs
->iounit
= get_iounit(s
, &vs
->fidp
->path
);
2389 v9fs_create_post_getiounit(s
, vs
);
2393 complete_pdu(s
, vs
->pdu
, err
);
2394 v9fs_string_free(&vs
->name
);
2395 v9fs_string_free(&vs
->extension
);
2396 v9fs_string_free(&vs
->fullname
);
2400 static void v9fs_create_post_perms(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2405 v9fs_post_create(s
, vs
, err
);
2408 static void v9fs_create_post_opendir(V9fsState
*s
, V9fsCreateState
*vs
,
2411 if (!vs
->fidp
->fs
.dir
) {
2414 vs
->fidp
->fid_type
= P9_FID_DIR
;
2415 v9fs_post_create(s
, vs
, err
);
2418 static void v9fs_create_post_dir_lstat(V9fsState
*s
, V9fsCreateState
*vs
,
2426 vs
->fidp
->fs
.dir
= v9fs_do_opendir(s
, &vs
->fullname
);
2427 v9fs_create_post_opendir(s
, vs
, err
);
2431 v9fs_post_create(s
, vs
, err
);
2434 static void v9fs_create_post_mkdir(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2441 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
2442 v9fs_create_post_dir_lstat(s
, vs
, err
);
2446 v9fs_post_create(s
, vs
, err
);
2449 static void v9fs_create_post_fstat(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2452 vs
->fidp
->fid_type
= P9_FID_NONE
;
2453 close(vs
->fidp
->fs
.fd
);
2456 v9fs_post_create(s
, vs
, err
);
2460 static void v9fs_create_post_open2(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2462 if (vs
->fidp
->fs
.fd
== -1) {
2466 vs
->fidp
->fid_type
= P9_FID_FILE
;
2467 err
= v9fs_do_fstat(s
, vs
->fidp
->fs
.fd
, &vs
->stbuf
);
2468 v9fs_create_post_fstat(s
, vs
, err
);
2473 v9fs_post_create(s
, vs
, err
);
2477 static void v9fs_create_post_lstat(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2480 if (err
== 0 || errno
!= ENOENT
) {
2485 if (vs
->perm
& P9_STAT_MODE_DIR
) {
2486 err
= v9fs_do_mkdir(s
, vs
->fullname
.data
, vs
->perm
& 0777,
2488 v9fs_create_post_mkdir(s
, vs
, err
);
2489 } else if (vs
->perm
& P9_STAT_MODE_SYMLINK
) {
2490 err
= v9fs_do_symlink(s
, vs
->fidp
, vs
->extension
.data
,
2491 vs
->fullname
.data
, -1);
2492 v9fs_create_post_perms(s
, vs
, err
);
2493 } else if (vs
->perm
& P9_STAT_MODE_LINK
) {
2494 int32_t nfid
= atoi(vs
->extension
.data
);
2495 V9fsFidState
*nfidp
= lookup_fid(s
, nfid
);
2496 if (nfidp
== NULL
) {
2498 v9fs_post_create(s
, vs
, err
);
2500 err
= v9fs_do_link(s
, &nfidp
->path
, &vs
->fullname
);
2501 v9fs_create_post_perms(s
, vs
, err
);
2502 } else if (vs
->perm
& P9_STAT_MODE_DEVICE
) {
2504 uint32_t major
, minor
;
2507 if (sscanf(vs
->extension
.data
, "%c %u %u", &ctype
, &major
,
2510 v9fs_post_create(s
, vs
, err
);
2522 v9fs_post_create(s
, vs
, err
);
2525 nmode
|= vs
->perm
& 0777;
2526 err
= v9fs_do_mknod(s
, vs
->fullname
.data
, nmode
,
2527 makedev(major
, minor
), vs
->fidp
->uid
, -1);
2528 v9fs_create_post_perms(s
, vs
, err
);
2529 } else if (vs
->perm
& P9_STAT_MODE_NAMED_PIPE
) {
2530 err
= v9fs_do_mknod(s
, vs
->fullname
.data
, S_IFIFO
| (vs
->perm
& 0777),
2531 0, vs
->fidp
->uid
, -1);
2532 v9fs_post_create(s
, vs
, err
);
2533 } else if (vs
->perm
& P9_STAT_MODE_SOCKET
) {
2534 err
= v9fs_do_mknod(s
, vs
->fullname
.data
, S_IFSOCK
| (vs
->perm
& 0777),
2535 0, vs
->fidp
->uid
, -1);
2536 v9fs_post_create(s
, vs
, err
);
2538 vs
->fidp
->fs
.fd
= v9fs_do_open2(s
, vs
->fullname
.data
, vs
->fidp
->uid
,
2539 -1, omode_to_uflags(vs
->mode
)|O_CREAT
, vs
->perm
);
2541 v9fs_create_post_open2(s
, vs
, err
);
2547 v9fs_post_create(s
, vs
, err
);
2550 static void v9fs_create(V9fsState
*s
, V9fsPDU
*pdu
)
2553 V9fsCreateState
*vs
;
2556 vs
= qemu_malloc(sizeof(*vs
));
2560 v9fs_string_init(&vs
->fullname
);
2562 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsdbs", &fid
, &vs
->name
,
2563 &vs
->perm
, &vs
->mode
, &vs
->extension
);
2565 vs
->fidp
= lookup_fid(s
, fid
);
2566 if (vs
->fidp
== NULL
) {
2571 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", vs
->fidp
->path
.data
,
2574 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
2575 v9fs_create_post_lstat(s
, vs
, err
);
2579 complete_pdu(s
, vs
->pdu
, err
);
2580 v9fs_string_free(&vs
->name
);
2581 v9fs_string_free(&vs
->extension
);
2585 static void v9fs_post_symlink(V9fsState
*s
, V9fsSymlinkState
*vs
, int err
)
2588 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
2589 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qid
);
2594 complete_pdu(s
, vs
->pdu
, err
);
2595 v9fs_string_free(&vs
->name
);
2596 v9fs_string_free(&vs
->symname
);
2597 v9fs_string_free(&vs
->fullname
);
2601 static void v9fs_symlink_post_do_symlink(V9fsState
*s
, V9fsSymlinkState
*vs
,
2607 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
2609 v9fs_post_symlink(s
, vs
, err
);
2612 static void v9fs_symlink(V9fsState
*s
, V9fsPDU
*pdu
)
2615 V9fsSymlinkState
*vs
;
2619 vs
= qemu_malloc(sizeof(*vs
));
2623 v9fs_string_init(&vs
->fullname
);
2625 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dssd", &dfid
, &vs
->name
,
2626 &vs
->symname
, &gid
);
2628 vs
->dfidp
= lookup_fid(s
, dfid
);
2629 if (vs
->dfidp
== NULL
) {
2634 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", vs
->dfidp
->path
.data
,
2636 err
= v9fs_do_symlink(s
, vs
->dfidp
, vs
->symname
.data
,
2637 vs
->fullname
.data
, gid
);
2638 v9fs_symlink_post_do_symlink(s
, vs
, err
);
2642 complete_pdu(s
, vs
->pdu
, err
);
2643 v9fs_string_free(&vs
->name
);
2644 v9fs_string_free(&vs
->symname
);
2648 static void v9fs_flush(V9fsState
*s
, V9fsPDU
*pdu
)
2650 /* A nop call with no return */
2651 complete_pdu(s
, pdu
, 7);
2654 static void v9fs_link(V9fsState
*s
, V9fsPDU
*pdu
)
2656 int32_t dfid
, oldfid
;
2657 V9fsFidState
*dfidp
, *oldfidp
;
2658 V9fsString name
, fullname
;
2662 v9fs_string_init(&fullname
);
2664 pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2666 dfidp
= lookup_fid(s
, dfid
);
2667 if (dfidp
== NULL
) {
2672 oldfidp
= lookup_fid(s
, oldfid
);
2673 if (oldfidp
== NULL
) {
2678 v9fs_string_sprintf(&fullname
, "%s/%s", dfidp
->path
.data
, name
.data
);
2680 err
= v9fs_do_link(s
, &oldfidp
->path
, &fullname
);
2684 v9fs_string_free(&fullname
);
2687 v9fs_string_free(&name
);
2688 complete_pdu(s
, pdu
, err
);
2691 static void v9fs_remove_post_remove(V9fsState
*s
, V9fsRemoveState
*vs
,
2700 /* For TREMOVE we need to clunk the fid even on failed remove */
2701 free_fid(s
, vs
->fidp
->fid
);
2703 complete_pdu(s
, vs
->pdu
, err
);
2707 static void v9fs_remove(V9fsState
*s
, V9fsPDU
*pdu
)
2710 V9fsRemoveState
*vs
;
2713 vs
= qemu_malloc(sizeof(*vs
));
2717 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &fid
);
2719 vs
->fidp
= lookup_fid(s
, fid
);
2720 if (vs
->fidp
== NULL
) {
2725 err
= v9fs_do_remove(s
, &vs
->fidp
->path
);
2726 v9fs_remove_post_remove(s
, vs
, err
);
2730 complete_pdu(s
, pdu
, err
);
2734 static void v9fs_wstat_post_truncate(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2743 v9fs_stat_free(&vs
->v9stat
);
2744 complete_pdu(s
, vs
->pdu
, err
);
2748 static void v9fs_wstat_post_rename(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2753 if (vs
->v9stat
.length
!= -1) {
2754 if (v9fs_do_truncate(s
, &vs
->fidp
->path
, vs
->v9stat
.length
) < 0) {
2758 v9fs_wstat_post_truncate(s
, vs
, err
);
2762 v9fs_stat_free(&vs
->v9stat
);
2763 complete_pdu(s
, vs
->pdu
, err
);
2767 static int v9fs_complete_rename(V9fsState
*s
, V9fsRenameState
*vs
)
2770 char *old_name
, *new_name
;
2773 if (vs
->newdirfid
!= -1) {
2774 V9fsFidState
*dirfidp
;
2775 dirfidp
= lookup_fid(s
, vs
->newdirfid
);
2777 if (dirfidp
== NULL
) {
2782 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2784 new_name
= qemu_mallocz(dirfidp
->path
.size
+ vs
->name
.size
+ 2);
2786 strcpy(new_name
, dirfidp
->path
.data
);
2787 strcat(new_name
, "/");
2788 strcat(new_name
+ dirfidp
->path
.size
, vs
->name
.data
);
2790 old_name
= vs
->fidp
->path
.data
;
2791 end
= strrchr(old_name
, '/');
2797 new_name
= qemu_mallocz(end
- old_name
+ vs
->name
.size
+ 1);
2799 strncat(new_name
, old_name
, end
- old_name
);
2800 strncat(new_name
+ (end
- old_name
), vs
->name
.data
, vs
->name
.size
);
2803 v9fs_string_free(&vs
->name
);
2804 vs
->name
.data
= qemu_strdup(new_name
);
2805 vs
->name
.size
= strlen(new_name
);
2807 if (strcmp(new_name
, vs
->fidp
->path
.data
) != 0) {
2808 if (v9fs_do_rename(s
, &vs
->fidp
->path
, &vs
->name
)) {
2813 * Fixup fid's pointing to the old name to
2814 * start pointing to the new name
2816 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
2817 if (vs
->fidp
== fidp
) {
2819 * we replace name of this fid towards the end
2820 * so that our below strcmp will work
2824 if (!strncmp(vs
->fidp
->path
.data
, fidp
->path
.data
,
2825 strlen(vs
->fidp
->path
.data
))) {
2826 /* replace the name */
2827 v9fs_fix_path(&fidp
->path
, &vs
->name
,
2828 strlen(vs
->fidp
->path
.data
));
2831 v9fs_string_copy(&vs
->fidp
->path
, &vs
->name
);
2835 v9fs_string_free(&vs
->name
);
2839 static void v9fs_rename_post_rename(V9fsState
*s
, V9fsRenameState
*vs
, int err
)
2841 complete_pdu(s
, vs
->pdu
, err
);
2845 static void v9fs_wstat_post_chown(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2851 if (vs
->v9stat
.name
.size
!= 0) {
2852 V9fsRenameState
*vr
;
2854 vr
= qemu_mallocz(sizeof(V9fsRenameState
));
2857 vr
->fidp
= vs
->fidp
;
2858 vr
->offset
= vs
->offset
;
2859 vr
->name
.size
= vs
->v9stat
.name
.size
;
2860 vr
->name
.data
= qemu_strdup(vs
->v9stat
.name
.data
);
2862 err
= v9fs_complete_rename(s
, vr
);
2865 v9fs_wstat_post_rename(s
, vs
, err
);
2869 v9fs_stat_free(&vs
->v9stat
);
2870 complete_pdu(s
, vs
->pdu
, err
);
2874 static void v9fs_rename(V9fsState
*s
, V9fsPDU
*pdu
)
2877 V9fsRenameState
*vs
;
2880 vs
= qemu_malloc(sizeof(*vs
));
2884 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dds", &fid
, &vs
->newdirfid
, &vs
->name
);
2886 vs
->fidp
= lookup_fid(s
, fid
);
2887 if (vs
->fidp
== NULL
) {
2892 BUG_ON(vs
->fidp
->fid_type
!= P9_FID_NONE
);
2894 err
= v9fs_complete_rename(s
, vs
);
2895 v9fs_rename_post_rename(s
, vs
, err
);
2898 complete_pdu(s
, vs
->pdu
, err
);
2902 static void v9fs_wstat_post_utime(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2908 if (vs
->v9stat
.n_gid
!= -1 || vs
->v9stat
.n_uid
!= -1) {
2909 if (v9fs_do_chown(s
, &vs
->fidp
->path
, vs
->v9stat
.n_uid
,
2910 vs
->v9stat
.n_gid
)) {
2914 v9fs_wstat_post_chown(s
, vs
, err
);
2918 v9fs_stat_free(&vs
->v9stat
);
2919 complete_pdu(s
, vs
->pdu
, err
);
2923 static void v9fs_wstat_post_chmod(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2929 if (vs
->v9stat
.mtime
!= -1 || vs
->v9stat
.atime
!= -1) {
2930 struct timespec times
[2];
2931 if (vs
->v9stat
.atime
!= -1) {
2932 times
[0].tv_sec
= vs
->v9stat
.atime
;
2933 times
[0].tv_nsec
= 0;
2935 times
[0].tv_nsec
= UTIME_OMIT
;
2937 if (vs
->v9stat
.mtime
!= -1) {
2938 times
[1].tv_sec
= vs
->v9stat
.mtime
;
2939 times
[1].tv_nsec
= 0;
2941 times
[1].tv_nsec
= UTIME_OMIT
;
2944 if (v9fs_do_utimensat(s
, &vs
->fidp
->path
, times
)) {
2949 v9fs_wstat_post_utime(s
, vs
, err
);
2953 v9fs_stat_free(&vs
->v9stat
);
2954 complete_pdu(s
, vs
->pdu
, err
);
2958 static void v9fs_wstat_post_fsync(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2963 v9fs_stat_free(&vs
->v9stat
);
2964 complete_pdu(s
, vs
->pdu
, err
);
2968 static void v9fs_wstat_post_lstat(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2977 v9_mode
= stat_to_v9mode(&vs
->stbuf
);
2979 if ((vs
->v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2980 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2981 /* Attempting to change the type */
2986 if (v9fs_do_chmod(s
, &vs
->fidp
->path
, v9mode_to_mode(vs
->v9stat
.mode
,
2987 &vs
->v9stat
.extension
))) {
2990 v9fs_wstat_post_chmod(s
, vs
, err
);
2994 v9fs_stat_free(&vs
->v9stat
);
2995 complete_pdu(s
, vs
->pdu
, err
);
2999 static void v9fs_wstat(V9fsState
*s
, V9fsPDU
*pdu
)
3005 vs
= qemu_malloc(sizeof(*vs
));
3009 pdu_unmarshal(pdu
, vs
->offset
, "dwS", &fid
, &vs
->unused
, &vs
->v9stat
);
3011 vs
->fidp
= lookup_fid(s
, fid
);
3012 if (vs
->fidp
== NULL
) {
3017 /* do we need to sync the file? */
3018 if (donttouch_stat(&vs
->v9stat
)) {
3019 err
= v9fs_do_fsync(s
, vs
->fidp
->fs
.fd
);
3020 v9fs_wstat_post_fsync(s
, vs
, err
);
3024 if (vs
->v9stat
.mode
!= -1) {
3025 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
3026 v9fs_wstat_post_lstat(s
, vs
, err
);
3030 v9fs_wstat_post_chmod(s
, vs
, err
);
3034 v9fs_stat_free(&vs
->v9stat
);
3035 complete_pdu(s
, vs
->pdu
, err
);
3039 static void v9fs_statfs_post_statfs(V9fsState
*s
, V9fsStatfsState
*vs
, int err
)
3041 int32_t bsize_factor
;
3049 * compute bsize factor based on host file system block size
3052 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/vs
->stbuf
.f_bsize
;
3053 if (!bsize_factor
) {
3056 vs
->v9statfs
.f_type
= vs
->stbuf
.f_type
;
3057 vs
->v9statfs
.f_bsize
= vs
->stbuf
.f_bsize
;
3058 vs
->v9statfs
.f_bsize
*= bsize_factor
;
3060 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3061 * adjust(divide) the number of blocks, free blocks and available
3062 * blocks by bsize factor
3064 vs
->v9statfs
.f_blocks
= vs
->stbuf
.f_blocks
/bsize_factor
;
3065 vs
->v9statfs
.f_bfree
= vs
->stbuf
.f_bfree
/bsize_factor
;
3066 vs
->v9statfs
.f_bavail
= vs
->stbuf
.f_bavail
/bsize_factor
;
3067 vs
->v9statfs
.f_files
= vs
->stbuf
.f_files
;
3068 vs
->v9statfs
.f_ffree
= vs
->stbuf
.f_ffree
;
3069 vs
->v9statfs
.fsid_val
= (unsigned int) vs
->stbuf
.f_fsid
.__val
[0] |
3070 (unsigned long long)vs
->stbuf
.f_fsid
.__val
[1] << 32;
3071 vs
->v9statfs
.f_namelen
= vs
->stbuf
.f_namelen
;
3073 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "ddqqqqqqd",
3074 vs
->v9statfs
.f_type
, vs
->v9statfs
.f_bsize
, vs
->v9statfs
.f_blocks
,
3075 vs
->v9statfs
.f_bfree
, vs
->v9statfs
.f_bavail
, vs
->v9statfs
.f_files
,
3076 vs
->v9statfs
.f_ffree
, vs
->v9statfs
.fsid_val
,
3077 vs
->v9statfs
.f_namelen
);
3080 complete_pdu(s
, vs
->pdu
, vs
->offset
);
3084 static void v9fs_statfs(V9fsState
*s
, V9fsPDU
*pdu
)
3086 V9fsStatfsState
*vs
;
3089 vs
= qemu_malloc(sizeof(*vs
));
3093 memset(&vs
->v9statfs
, 0, sizeof(vs
->v9statfs
));
3095 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &vs
->fid
);
3097 vs
->fidp
= lookup_fid(s
, vs
->fid
);
3098 if (vs
->fidp
== NULL
) {
3103 err
= v9fs_do_statfs(s
, &vs
->fidp
->path
, &vs
->stbuf
);
3104 v9fs_statfs_post_statfs(s
, vs
, err
);
3108 complete_pdu(s
, vs
->pdu
, err
);
3112 static void v9fs_mknod_post_lstat(V9fsState
*s
, V9fsMkState
*vs
, int err
)
3119 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
3120 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qid
);
3123 complete_pdu(s
, vs
->pdu
, err
);
3124 v9fs_string_free(&vs
->fullname
);
3125 v9fs_string_free(&vs
->name
);
3129 static void v9fs_mknod_post_mknod(V9fsState
*s
, V9fsMkState
*vs
, int err
)
3136 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
3137 v9fs_mknod_post_lstat(s
, vs
, err
);
3140 complete_pdu(s
, vs
->pdu
, err
);
3141 v9fs_string_free(&vs
->fullname
);
3142 v9fs_string_free(&vs
->name
);
3146 static void v9fs_mknod(V9fsState
*s
, V9fsPDU
*pdu
)
3156 vs
= qemu_malloc(sizeof(*vs
));
3160 v9fs_string_init(&vs
->fullname
);
3161 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsdddd", &fid
, &vs
->name
, &mode
,
3162 &major
, &minor
, &gid
);
3164 fidp
= lookup_fid(s
, fid
);
3170 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", fidp
->path
.data
, vs
->name
.data
);
3171 err
= v9fs_do_mknod(s
, vs
->fullname
.data
, mode
, makedev(major
, minor
),
3173 v9fs_mknod_post_mknod(s
, vs
, err
);
3177 complete_pdu(s
, vs
->pdu
, err
);
3178 v9fs_string_free(&vs
->fullname
);
3179 v9fs_string_free(&vs
->name
);
3184 * Implement posix byte range locking code
3185 * Server side handling of locking code is very simple, because 9p server in
3186 * QEMU can handle only one client. And most of the lock handling
3187 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3188 * do any thing in * qemu 9p server side lock code path.
3189 * So when a TLOCK request comes, always return success
3192 static void v9fs_lock(V9fsState
*s
, V9fsPDU
*pdu
)
3194 int32_t fid
, err
= 0;
3197 vs
= qemu_mallocz(sizeof(*vs
));
3201 vs
->flock
= qemu_malloc(sizeof(*vs
->flock
));
3202 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dbdqqds", &fid
, &vs
->flock
->type
,
3203 &vs
->flock
->flags
, &vs
->flock
->start
, &vs
->flock
->length
,
3204 &vs
->flock
->proc_id
, &vs
->flock
->client_id
);
3206 vs
->status
= P9_LOCK_ERROR
;
3208 /* We support only block flag now (that too ignored currently) */
3209 if (vs
->flock
->flags
& ~P9_LOCK_FLAGS_BLOCK
) {
3213 vs
->fidp
= lookup_fid(s
, fid
);
3214 if (vs
->fidp
== NULL
) {
3219 err
= v9fs_do_fstat(s
, vs
->fidp
->fs
.fd
, &vs
->stbuf
);
3224 vs
->status
= P9_LOCK_SUCCESS
;
3226 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "b", vs
->status
);
3227 complete_pdu(s
, vs
->pdu
, err
);
3228 qemu_free(vs
->flock
);
3233 * When a TGETLOCK request comes, always return success because all lock
3234 * handling is done by client's VFS layer.
3237 static void v9fs_getlock(V9fsState
*s
, V9fsPDU
*pdu
)
3239 int32_t fid
, err
= 0;
3240 V9fsGetlockState
*vs
;
3242 vs
= qemu_mallocz(sizeof(*vs
));
3246 vs
->glock
= qemu_malloc(sizeof(*vs
->glock
));
3247 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dbqqds", &fid
, &vs
->glock
->type
,
3248 &vs
->glock
->start
, &vs
->glock
->length
, &vs
->glock
->proc_id
,
3249 &vs
->glock
->client_id
);
3251 vs
->fidp
= lookup_fid(s
, fid
);
3252 if (vs
->fidp
== NULL
) {
3257 err
= v9fs_do_fstat(s
, vs
->fidp
->fs
.fd
, &vs
->stbuf
);
3262 vs
->glock
->type
= F_UNLCK
;
3263 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "bqqds", vs
->glock
->type
,
3264 vs
->glock
->start
, vs
->glock
->length
, vs
->glock
->proc_id
,
3265 &vs
->glock
->client_id
);
3267 complete_pdu(s
, vs
->pdu
, err
);
3268 qemu_free(vs
->glock
);
3272 static void v9fs_mkdir_post_lstat(V9fsState
*s
, V9fsMkState
*vs
, int err
)
3279 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
3280 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qid
);
3283 complete_pdu(s
, vs
->pdu
, err
);
3284 v9fs_string_free(&vs
->fullname
);
3285 v9fs_string_free(&vs
->name
);
3289 static void v9fs_mkdir_post_mkdir(V9fsState
*s
, V9fsMkState
*vs
, int err
)
3296 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
3297 v9fs_mkdir_post_lstat(s
, vs
, err
);
3300 complete_pdu(s
, vs
->pdu
, err
);
3301 v9fs_string_free(&vs
->fullname
);
3302 v9fs_string_free(&vs
->name
);
3306 static void v9fs_mkdir(V9fsState
*s
, V9fsPDU
*pdu
)
3315 vs
= qemu_malloc(sizeof(*vs
));
3319 v9fs_string_init(&vs
->fullname
);
3320 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsdd", &fid
, &vs
->name
, &mode
,
3323 fidp
= lookup_fid(s
, fid
);
3329 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", fidp
->path
.data
, vs
->name
.data
);
3330 err
= v9fs_do_mkdir(s
, vs
->fullname
.data
, mode
, fidp
->uid
, gid
);
3331 v9fs_mkdir_post_mkdir(s
, vs
, err
);
3335 complete_pdu(s
, vs
->pdu
, err
);
3336 v9fs_string_free(&vs
->fullname
);
3337 v9fs_string_free(&vs
->name
);
3341 static void v9fs_post_xattr_getvalue(V9fsState
*s
, V9fsXattrState
*vs
, int err
)
3346 free_fid(s
, vs
->xattr_fidp
->fid
);
3349 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "q", vs
->size
);
3352 complete_pdu(s
, vs
->pdu
, err
);
3353 v9fs_string_free(&vs
->name
);
3358 static void v9fs_post_xattr_check(V9fsState
*s
, V9fsXattrState
*vs
, ssize_t err
)
3362 free_fid(s
, vs
->xattr_fidp
->fid
);
3366 * Read the xattr value
3368 vs
->xattr_fidp
->fs
.xattr
.len
= vs
->size
;
3369 vs
->xattr_fidp
->fid_type
= P9_FID_XATTR
;
3370 vs
->xattr_fidp
->fs
.xattr
.copied_len
= -1;
3372 vs
->xattr_fidp
->fs
.xattr
.value
= qemu_malloc(vs
->size
);
3373 err
= v9fs_do_lgetxattr(s
, &vs
->xattr_fidp
->path
,
3374 &vs
->name
, vs
->xattr_fidp
->fs
.xattr
.value
,
3375 vs
->xattr_fidp
->fs
.xattr
.len
);
3377 v9fs_post_xattr_getvalue(s
, vs
, err
);
3380 complete_pdu(s
, vs
->pdu
, err
);
3381 v9fs_string_free(&vs
->name
);
3385 static void v9fs_post_lxattr_getvalue(V9fsState
*s
,
3386 V9fsXattrState
*vs
, int err
)
3390 free_fid(s
, vs
->xattr_fidp
->fid
);
3393 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "q", vs
->size
);
3396 complete_pdu(s
, vs
->pdu
, err
);
3397 v9fs_string_free(&vs
->name
);
3402 static void v9fs_post_lxattr_check(V9fsState
*s
,
3403 V9fsXattrState
*vs
, ssize_t err
)
3407 free_fid(s
, vs
->xattr_fidp
->fid
);
3411 * Read the xattr value
3413 vs
->xattr_fidp
->fs
.xattr
.len
= vs
->size
;
3414 vs
->xattr_fidp
->fid_type
= P9_FID_XATTR
;
3415 vs
->xattr_fidp
->fs
.xattr
.copied_len
= -1;
3417 vs
->xattr_fidp
->fs
.xattr
.value
= qemu_malloc(vs
->size
);
3418 err
= v9fs_do_llistxattr(s
, &vs
->xattr_fidp
->path
,
3419 vs
->xattr_fidp
->fs
.xattr
.value
,
3420 vs
->xattr_fidp
->fs
.xattr
.len
);
3422 v9fs_post_lxattr_getvalue(s
, vs
, err
);
3425 complete_pdu(s
, vs
->pdu
, err
);
3426 v9fs_string_free(&vs
->name
);
3430 static void v9fs_xattrwalk(V9fsState
*s
, V9fsPDU
*pdu
)
3434 int32_t fid
, newfid
;
3436 vs
= qemu_malloc(sizeof(*vs
));
3440 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dds", &fid
, &newfid
, &vs
->name
);
3441 vs
->file_fidp
= lookup_fid(s
, fid
);
3442 if (vs
->file_fidp
== NULL
) {
3447 vs
->xattr_fidp
= alloc_fid(s
, newfid
);
3448 if (vs
->xattr_fidp
== NULL
) {
3453 v9fs_string_copy(&vs
->xattr_fidp
->path
, &vs
->file_fidp
->path
);
3454 if (vs
->name
.data
[0] == 0) {
3456 * listxattr request. Get the size first
3458 vs
->size
= v9fs_do_llistxattr(s
, &vs
->xattr_fidp
->path
,
3463 v9fs_post_lxattr_check(s
, vs
, err
);
3467 * specific xattr fid. We check for xattr
3468 * presence also collect the xattr size
3470 vs
->size
= v9fs_do_lgetxattr(s
, &vs
->xattr_fidp
->path
,
3471 &vs
->name
, NULL
, 0);
3475 v9fs_post_xattr_check(s
, vs
, err
);
3479 complete_pdu(s
, vs
->pdu
, err
);
3480 v9fs_string_free(&vs
->name
);
3484 static void v9fs_xattrcreate(V9fsState
*s
, V9fsPDU
*pdu
)
3491 vs
= qemu_malloc(sizeof(*vs
));
3495 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsqd",
3496 &fid
, &vs
->name
, &vs
->size
, &flags
);
3498 vs
->file_fidp
= lookup_fid(s
, fid
);
3499 if (vs
->file_fidp
== NULL
) {
3504 /* Make the file fid point to xattr */
3505 vs
->xattr_fidp
= vs
->file_fidp
;
3506 vs
->xattr_fidp
->fid_type
= P9_FID_XATTR
;
3507 vs
->xattr_fidp
->fs
.xattr
.copied_len
= 0;
3508 vs
->xattr_fidp
->fs
.xattr
.len
= vs
->size
;
3509 vs
->xattr_fidp
->fs
.xattr
.flags
= flags
;
3510 v9fs_string_init(&vs
->xattr_fidp
->fs
.xattr
.name
);
3511 v9fs_string_copy(&vs
->xattr_fidp
->fs
.xattr
.name
, &vs
->name
);
3513 vs
->xattr_fidp
->fs
.xattr
.value
= qemu_malloc(vs
->size
);
3515 vs
->xattr_fidp
->fs
.xattr
.value
= NULL
;
3518 complete_pdu(s
, vs
->pdu
, err
);
3519 v9fs_string_free(&vs
->name
);
3523 static void v9fs_readlink_post_readlink(V9fsState
*s
, V9fsReadLinkState
*vs
,
3530 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "s", &vs
->target
);
3533 complete_pdu(s
, vs
->pdu
, err
);
3534 v9fs_string_free(&vs
->target
);
3538 static void v9fs_readlink(V9fsState
*s
, V9fsPDU
*pdu
)
3541 V9fsReadLinkState
*vs
;
3545 vs
= qemu_malloc(sizeof(*vs
));
3549 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &fid
);
3551 fidp
= lookup_fid(s
, fid
);
3557 v9fs_string_init(&vs
->target
);
3558 err
= v9fs_do_readlink(s
, &fidp
->path
, &vs
->target
);
3559 v9fs_readlink_post_readlink(s
, vs
, err
);
3562 complete_pdu(s
, vs
->pdu
, err
);
3566 typedef void (pdu_handler_t
)(V9fsState
*s
, V9fsPDU
*pdu
);
3568 static pdu_handler_t
*pdu_handlers
[] = {
3569 [P9_TREADDIR
] = v9fs_readdir
,
3570 [P9_TSTATFS
] = v9fs_statfs
,
3571 [P9_TGETATTR
] = v9fs_getattr
,
3572 [P9_TSETATTR
] = v9fs_setattr
,
3573 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3574 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3575 [P9_TMKNOD
] = v9fs_mknod
,
3576 [P9_TRENAME
] = v9fs_rename
,
3577 [P9_TLOCK
] = v9fs_lock
,
3578 [P9_TGETLOCK
] = v9fs_getlock
,
3579 [P9_TREADLINK
] = v9fs_readlink
,
3580 [P9_TMKDIR
] = v9fs_mkdir
,
3581 [P9_TVERSION
] = v9fs_version
,
3582 [P9_TLOPEN
] = v9fs_open
,
3583 [P9_TATTACH
] = v9fs_attach
,
3584 [P9_TSTAT
] = v9fs_stat
,
3585 [P9_TWALK
] = v9fs_walk
,
3586 [P9_TCLUNK
] = v9fs_clunk
,
3587 [P9_TFSYNC
] = v9fs_fsync
,
3588 [P9_TOPEN
] = v9fs_open
,
3589 [P9_TREAD
] = v9fs_read
,
3591 [P9_TAUTH
] = v9fs_auth
,
3593 [P9_TFLUSH
] = v9fs_flush
,
3594 [P9_TLINK
] = v9fs_link
,
3595 [P9_TSYMLINK
] = v9fs_symlink
,
3596 [P9_TCREATE
] = v9fs_create
,
3597 [P9_TLCREATE
] = v9fs_lcreate
,
3598 [P9_TWRITE
] = v9fs_write
,
3599 [P9_TWSTAT
] = v9fs_wstat
,
3600 [P9_TREMOVE
] = v9fs_remove
,
3603 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
3605 pdu_handler_t
*handler
;
3611 BUG_ON(pdu
->id
>= ARRAY_SIZE(pdu_handlers
));
3613 handler
= pdu_handlers
[pdu
->id
];
3614 BUG_ON(handler
== NULL
);
3619 static void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
3621 V9fsState
*s
= (V9fsState
*)vdev
;
3625 while ((pdu
= alloc_pdu(s
)) &&
3626 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
3629 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
3630 BUG_ON(pdu
->elem
.out_sg
[0].iov_len
< 7);
3632 ptr
= pdu
->elem
.out_sg
[0].iov_base
;
3634 memcpy(&pdu
->size
, ptr
, 4);
3636 memcpy(&pdu
->tag
, ptr
+ 5, 2);
3644 static uint32_t virtio_9p_get_features(VirtIODevice
*vdev
, uint32_t features
)
3646 features
|= 1 << VIRTIO_9P_MOUNT_TAG
;
3650 static V9fsState
*to_virtio_9p(VirtIODevice
*vdev
)
3652 return (V9fsState
*)vdev
;
3655 static void virtio_9p_get_config(VirtIODevice
*vdev
, uint8_t *config
)
3657 struct virtio_9p_config
*cfg
;
3658 V9fsState
*s
= to_virtio_9p(vdev
);
3660 cfg
= qemu_mallocz(sizeof(struct virtio_9p_config
) +
3662 stw_raw(&cfg
->tag_len
, s
->tag_len
);
3663 memcpy(cfg
->tag
, s
->tag
, s
->tag_len
);
3664 memcpy(config
, cfg
, s
->config_size
);
3668 VirtIODevice
*virtio_9p_init(DeviceState
*dev
, V9fsConf
*conf
)
3676 s
= (V9fsState
*)virtio_common_init("virtio-9p",
3678 sizeof(struct virtio_9p_config
)+
3682 /* initialize pdu allocator */
3683 QLIST_INIT(&s
->free_list
);
3684 for (i
= 0; i
< (MAX_REQ
- 1); i
++) {
3685 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
3688 s
->vq
= virtio_add_queue(&s
->vdev
, MAX_REQ
, handle_9p_output
);
3690 fse
= get_fsdev_fsentry(conf
->fsdev_id
);
3693 /* We don't have a fsdev identified by fsdev_id */
3694 fprintf(stderr
, "Virtio-9p device couldn't find fsdev "
3695 "with the id %s\n", conf
->fsdev_id
);
3699 if (!fse
->path
|| !conf
->tag
) {
3700 /* we haven't specified a mount_tag or the path */
3701 fprintf(stderr
, "fsdev with id %s needs path "
3702 "and Virtio-9p device needs mount_tag arguments\n",
3707 if (!strcmp(fse
->security_model
, "passthrough")) {
3708 /* Files on the Fileserver set to client user credentials */
3709 s
->ctx
.fs_sm
= SM_PASSTHROUGH
;
3710 } else if (!strcmp(fse
->security_model
, "mapped")) {
3711 /* Files on the fileserver are set to QEMU credentials.
3712 * Client user credentials are saved in extended attributes.
3714 s
->ctx
.fs_sm
= SM_MAPPED
;
3715 } else if (!strcmp(fse
->security_model
, "none")) {
3717 * Files on the fileserver are set to QEMU credentials.
3719 s
->ctx
.fs_sm
= SM_NONE
;
3722 fprintf(stderr
, "Default to security_model=none. You may want"
3723 " enable advanced security model using "
3724 "security option:\n\t security_model=passthrough \n\t "
3725 "security_model=mapped\n");
3726 s
->ctx
.fs_sm
= SM_NONE
;
3729 if (lstat(fse
->path
, &stat
)) {
3730 fprintf(stderr
, "share path %s does not exist\n", fse
->path
);
3732 } else if (!S_ISDIR(stat
.st_mode
)) {
3733 fprintf(stderr
, "share path %s is not a directory \n", fse
->path
);
3737 s
->ctx
.fs_root
= qemu_strdup(fse
->path
);
3738 len
= strlen(conf
->tag
);
3739 if (len
> MAX_TAG_LEN
) {
3742 /* s->tag is non-NULL terminated string */
3743 s
->tag
= qemu_malloc(len
);
3744 memcpy(s
->tag
, conf
->tag
, len
);
3749 s
->vdev
.get_features
= virtio_9p_get_features
;
3750 s
->config_size
= sizeof(struct virtio_9p_config
) +
3752 s
->vdev
.get_config
= virtio_9p_get_config
;