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.
14 #include "hw/virtio.h"
16 #include "qemu_socket.h"
17 #include "hw/virtio-pci.h"
18 #include "virtio-9p.h"
19 #include "fsdev/qemu-fsdev.h"
20 #include "virtio-9p-debug.h"
21 #include "virtio-9p-xattr.h"
37 static int omode_to_uflags(int8_t mode
)
71 void cred_init(FsCred
*credp
)
79 static int v9fs_do_lstat(V9fsState
*s
, V9fsString
*path
, struct stat
*stbuf
)
81 return s
->ops
->lstat(&s
->ctx
, path
->data
, stbuf
);
84 static ssize_t
v9fs_do_readlink(V9fsState
*s
, V9fsString
*path
, V9fsString
*buf
)
88 buf
->data
= qemu_malloc(1024);
90 len
= s
->ops
->readlink(&s
->ctx
, path
->data
, buf
->data
, 1024 - 1);
99 static int v9fs_do_close(V9fsState
*s
, int fd
)
101 return s
->ops
->close(&s
->ctx
, fd
);
104 static int v9fs_do_closedir(V9fsState
*s
, DIR *dir
)
106 return s
->ops
->closedir(&s
->ctx
, dir
);
109 static int v9fs_do_open(V9fsState
*s
, V9fsString
*path
, int flags
)
111 return s
->ops
->open(&s
->ctx
, path
->data
, flags
);
114 static DIR *v9fs_do_opendir(V9fsState
*s
, V9fsString
*path
)
116 return s
->ops
->opendir(&s
->ctx
, path
->data
);
119 static void v9fs_do_rewinddir(V9fsState
*s
, DIR *dir
)
121 return s
->ops
->rewinddir(&s
->ctx
, dir
);
124 static off_t
v9fs_do_telldir(V9fsState
*s
, DIR *dir
)
126 return s
->ops
->telldir(&s
->ctx
, dir
);
129 static struct dirent
*v9fs_do_readdir(V9fsState
*s
, DIR *dir
)
131 return s
->ops
->readdir(&s
->ctx
, dir
);
134 static void v9fs_do_seekdir(V9fsState
*s
, DIR *dir
, off_t off
)
136 return s
->ops
->seekdir(&s
->ctx
, dir
, off
);
139 static int v9fs_do_preadv(V9fsState
*s
, int fd
, const struct iovec
*iov
,
140 int iovcnt
, int64_t offset
)
142 return s
->ops
->preadv(&s
->ctx
, fd
, iov
, iovcnt
, offset
);
145 static int v9fs_do_pwritev(V9fsState
*s
, int fd
, const struct iovec
*iov
,
146 int iovcnt
, int64_t offset
)
148 return s
->ops
->pwritev(&s
->ctx
, fd
, iov
, iovcnt
, offset
);
151 static int v9fs_do_chmod(V9fsState
*s
, V9fsString
*path
, mode_t mode
)
156 return s
->ops
->chmod(&s
->ctx
, path
->data
, &cred
);
159 static int v9fs_do_mknod(V9fsState
*s
, char *name
,
160 mode_t mode
, dev_t dev
, uid_t uid
, gid_t gid
)
168 return s
->ops
->mknod(&s
->ctx
, name
, &cred
);
171 static int v9fs_do_mkdir(V9fsState
*s
, char *name
, mode_t mode
,
172 uid_t uid
, gid_t gid
)
181 return s
->ops
->mkdir(&s
->ctx
, name
, &cred
);
184 static int v9fs_do_fstat(V9fsState
*s
, int fd
, struct stat
*stbuf
)
186 return s
->ops
->fstat(&s
->ctx
, fd
, stbuf
);
189 static int v9fs_do_open2(V9fsState
*s
, char *fullname
, uid_t uid
, gid_t gid
,
197 cred
.fc_mode
= mode
& 07777;
199 return s
->ops
->open2(&s
->ctx
, fullname
, flags
, &cred
);
202 static int v9fs_do_symlink(V9fsState
*s
, V9fsFidState
*fidp
,
203 const char *oldpath
, const char *newpath
, gid_t gid
)
207 cred
.fc_uid
= fidp
->uid
;
211 return s
->ops
->symlink(&s
->ctx
, oldpath
, newpath
, &cred
);
214 static int v9fs_do_link(V9fsState
*s
, V9fsString
*oldpath
, V9fsString
*newpath
)
216 return s
->ops
->link(&s
->ctx
, oldpath
->data
, newpath
->data
);
219 static int v9fs_do_truncate(V9fsState
*s
, V9fsString
*path
, off_t size
)
221 return s
->ops
->truncate(&s
->ctx
, path
->data
, size
);
224 static int v9fs_do_rename(V9fsState
*s
, V9fsString
*oldpath
,
227 return s
->ops
->rename(&s
->ctx
, oldpath
->data
, newpath
->data
);
230 static int v9fs_do_chown(V9fsState
*s
, V9fsString
*path
, uid_t uid
, gid_t gid
)
237 return s
->ops
->chown(&s
->ctx
, path
->data
, &cred
);
240 static int v9fs_do_utimensat(V9fsState
*s
, V9fsString
*path
,
241 const struct timespec times
[2])
243 return s
->ops
->utimensat(&s
->ctx
, path
->data
, times
);
246 static int v9fs_do_remove(V9fsState
*s
, V9fsString
*path
)
248 return s
->ops
->remove(&s
->ctx
, path
->data
);
251 static int v9fs_do_fsync(V9fsState
*s
, int fd
, int datasync
)
253 return s
->ops
->fsync(&s
->ctx
, fd
, datasync
);
256 static int v9fs_do_statfs(V9fsState
*s
, V9fsString
*path
, struct statfs
*stbuf
)
258 return s
->ops
->statfs(&s
->ctx
, path
->data
, stbuf
);
261 static ssize_t
v9fs_do_lgetxattr(V9fsState
*s
, V9fsString
*path
,
262 V9fsString
*xattr_name
,
263 void *value
, size_t size
)
265 return s
->ops
->lgetxattr(&s
->ctx
, path
->data
,
266 xattr_name
->data
, value
, size
);
269 static ssize_t
v9fs_do_llistxattr(V9fsState
*s
, V9fsString
*path
,
270 void *value
, size_t size
)
272 return s
->ops
->llistxattr(&s
->ctx
, path
->data
,
276 static int v9fs_do_lsetxattr(V9fsState
*s
, V9fsString
*path
,
277 V9fsString
*xattr_name
,
278 void *value
, size_t size
, int flags
)
280 return s
->ops
->lsetxattr(&s
->ctx
, path
->data
,
281 xattr_name
->data
, value
, size
, flags
);
284 static int v9fs_do_lremovexattr(V9fsState
*s
, V9fsString
*path
,
285 V9fsString
*xattr_name
)
287 return s
->ops
->lremovexattr(&s
->ctx
, path
->data
,
292 static void v9fs_string_init(V9fsString
*str
)
298 static void v9fs_string_free(V9fsString
*str
)
300 qemu_free(str
->data
);
305 static void v9fs_string_null(V9fsString
*str
)
307 v9fs_string_free(str
);
310 static int number_to_string(void *arg
, char type
)
312 unsigned int ret
= 0;
316 unsigned int num
= *(unsigned int *)arg
;
325 unsigned long num
= *(unsigned long *)arg
;
333 printf("Number_to_string: Unknown number format\n");
340 static int GCC_FMT_ATTR(2, 0)
341 v9fs_string_alloc_printf(char **strp
, const char *fmt
, va_list ap
)
344 char *iter
= (char *)fmt
;
348 unsigned int arg_uint
;
349 unsigned long arg_ulong
;
351 /* Find the number of %'s that denotes an argument */
352 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
357 len
= strlen(fmt
) - 2*nr_args
;
367 /* Now parse the format string */
368 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
372 arg_uint
= va_arg(ap2
, unsigned int);
373 len
+= number_to_string((void *)&arg_uint
, 'u');
376 if (*++iter
== 'u') {
377 arg_ulong
= va_arg(ap2
, unsigned long);
378 len
+= number_to_string((void *)&arg_ulong
, 'U');
384 arg_char_ptr
= va_arg(ap2
, char *);
385 len
+= strlen(arg_char_ptr
);
392 "v9fs_string_alloc_printf:Incorrect format %c", *iter
);
399 *strp
= qemu_malloc((len
+ 1) * sizeof(**strp
));
401 return vsprintf(*strp
, fmt
, ap
);
404 static void GCC_FMT_ATTR(2, 3)
405 v9fs_string_sprintf(V9fsString
*str
, const char *fmt
, ...)
410 v9fs_string_free(str
);
413 err
= v9fs_string_alloc_printf(&str
->data
, fmt
, ap
);
420 static void v9fs_string_copy(V9fsString
*lhs
, V9fsString
*rhs
)
422 v9fs_string_free(lhs
);
423 v9fs_string_sprintf(lhs
, "%s", rhs
->data
);
427 * Return TRUE if s1 is an ancestor of s2.
429 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
430 * As a special case, We treat s1 as ancestor of s2 if they are same!
432 static int v9fs_path_is_ancestor(V9fsString
*s1
, V9fsString
*s2
)
434 if (!strncmp(s1
->data
, s2
->data
, s1
->size
)) {
435 if (s2
->data
[s1
->size
] == '\0' || s2
->data
[s1
->size
] == '/') {
442 static size_t v9fs_string_size(V9fsString
*str
)
447 static V9fsFidState
*lookup_fid(V9fsState
*s
, int32_t fid
)
451 for (f
= s
->fid_list
; f
; f
= f
->next
) {
460 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
464 f
= lookup_fid(s
, fid
);
469 f
= qemu_mallocz(sizeof(V9fsFidState
));
472 f
->fid_type
= P9_FID_NONE
;
474 f
->next
= s
->fid_list
;
480 static int v9fs_xattr_fid_clunk(V9fsState
*s
, V9fsFidState
*fidp
)
484 if (fidp
->fs
.xattr
.copied_len
== -1) {
485 /* getxattr/listxattr fid */
489 * if this is fid for setxattr. clunk should
490 * result in setxattr localcall
492 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
493 /* clunk after partial write */
497 if (fidp
->fs
.xattr
.len
) {
498 retval
= v9fs_do_lsetxattr(s
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
499 fidp
->fs
.xattr
.value
,
501 fidp
->fs
.xattr
.flags
);
503 retval
= v9fs_do_lremovexattr(s
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
506 v9fs_string_free(&fidp
->fs
.xattr
.name
);
508 if (fidp
->fs
.xattr
.value
) {
509 qemu_free(fidp
->fs
.xattr
.value
);
514 static int free_fid(V9fsState
*s
, int32_t fid
)
517 V9fsFidState
**fidpp
, *fidp
;
519 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
520 if ((*fidpp
)->fid
== fid
) {
525 if (*fidpp
== NULL
) {
532 if (fidp
->fid_type
== P9_FID_FILE
) {
533 v9fs_do_close(s
, fidp
->fs
.fd
);
534 } else if (fidp
->fid_type
== P9_FID_DIR
) {
535 v9fs_do_closedir(s
, fidp
->fs
.dir
);
536 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
537 retval
= v9fs_xattr_fid_clunk(s
, fidp
);
539 v9fs_string_free(&fidp
->path
);
545 #define P9_QID_TYPE_DIR 0x80
546 #define P9_QID_TYPE_SYMLINK 0x02
548 #define P9_STAT_MODE_DIR 0x80000000
549 #define P9_STAT_MODE_APPEND 0x40000000
550 #define P9_STAT_MODE_EXCL 0x20000000
551 #define P9_STAT_MODE_MOUNT 0x10000000
552 #define P9_STAT_MODE_AUTH 0x08000000
553 #define P9_STAT_MODE_TMP 0x04000000
554 #define P9_STAT_MODE_SYMLINK 0x02000000
555 #define P9_STAT_MODE_LINK 0x01000000
556 #define P9_STAT_MODE_DEVICE 0x00800000
557 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
558 #define P9_STAT_MODE_SOCKET 0x00100000
559 #define P9_STAT_MODE_SETUID 0x00080000
560 #define P9_STAT_MODE_SETGID 0x00040000
561 #define P9_STAT_MODE_SETVTX 0x00010000
563 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
564 P9_STAT_MODE_SYMLINK | \
565 P9_STAT_MODE_LINK | \
566 P9_STAT_MODE_DEVICE | \
567 P9_STAT_MODE_NAMED_PIPE | \
570 /* This is the algorithm from ufs in spfs */
571 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
575 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
576 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
577 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
579 if (S_ISDIR(stbuf
->st_mode
)) {
580 qidp
->type
|= P9_QID_TYPE_DIR
;
582 if (S_ISLNK(stbuf
->st_mode
)) {
583 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
587 static int fid_to_qid(V9fsState
*s
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
592 err
= v9fs_do_lstat(s
, &fidp
->path
, &stbuf
);
597 stat_to_qid(&stbuf
, qidp
);
601 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
605 if (!QLIST_EMPTY(&s
->free_list
)) {
606 pdu
= QLIST_FIRST(&s
->free_list
);
607 QLIST_REMOVE(pdu
, next
);
612 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
618 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
622 size_t pdu_packunpack(void *addr
, struct iovec
*sg
, int sg_count
,
623 size_t offset
, size_t size
, int pack
)
628 for (i
= 0; size
&& i
< sg_count
; i
++) {
630 if (offset
>= sg
[i
].iov_len
) {
632 offset
-= sg
[i
].iov_len
;
635 len
= MIN(sg
[i
].iov_len
- offset
, size
);
637 memcpy(sg
[i
].iov_base
+ offset
, addr
, len
);
639 memcpy(addr
, sg
[i
].iov_base
+ offset
, len
);
654 static size_t pdu_unpack(void *dst
, V9fsPDU
*pdu
, size_t offset
, size_t size
)
656 return pdu_packunpack(dst
, pdu
->elem
.out_sg
, pdu
->elem
.out_num
,
660 static size_t pdu_pack(V9fsPDU
*pdu
, size_t offset
, const void *src
,
663 return pdu_packunpack((void *)src
, pdu
->elem
.in_sg
, pdu
->elem
.in_num
,
667 static int pdu_copy_sg(V9fsPDU
*pdu
, size_t offset
, int rx
, struct iovec
*sg
)
671 struct iovec
*src_sg
;
675 src_sg
= pdu
->elem
.in_sg
;
676 num
= pdu
->elem
.in_num
;
678 src_sg
= pdu
->elem
.out_sg
;
679 num
= pdu
->elem
.out_num
;
683 for (i
= 0; i
< num
; i
++) {
685 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
686 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
688 } else if (offset
< (src_sg
[i
].iov_len
+ pos
)) {
689 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
690 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
691 sg
[j
].iov_base
+= (offset
- pos
);
692 sg
[j
].iov_len
-= (offset
- pos
);
695 pos
+= src_sg
[i
].iov_len
;
701 static size_t pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
703 size_t old_offset
= offset
;
708 for (i
= 0; fmt
[i
]; i
++) {
711 uint8_t *valp
= va_arg(ap
, uint8_t *);
712 offset
+= pdu_unpack(valp
, pdu
, offset
, sizeof(*valp
));
717 valp
= va_arg(ap
, uint16_t *);
718 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
719 *valp
= le16_to_cpu(val
);
724 valp
= va_arg(ap
, uint32_t *);
725 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
726 *valp
= le32_to_cpu(val
);
731 valp
= va_arg(ap
, uint64_t *);
732 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
733 *valp
= le64_to_cpu(val
);
737 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
738 int *iovcnt
= va_arg(ap
, int *);
739 *iovcnt
= pdu_copy_sg(pdu
, offset
, 0, iov
);
743 V9fsString
*str
= va_arg(ap
, V9fsString
*);
744 offset
+= pdu_unmarshal(pdu
, offset
, "w", &str
->size
);
745 /* FIXME: sanity check str->size */
746 str
->data
= qemu_malloc(str
->size
+ 1);
747 offset
+= pdu_unpack(str
->data
, pdu
, offset
, str
->size
);
748 str
->data
[str
->size
] = 0;
752 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
753 offset
+= pdu_unmarshal(pdu
, offset
, "bdq",
754 &qidp
->type
, &qidp
->version
, &qidp
->path
);
758 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
759 offset
+= pdu_unmarshal(pdu
, offset
, "wwdQdddqsssssddd",
760 &statp
->size
, &statp
->type
, &statp
->dev
,
761 &statp
->qid
, &statp
->mode
, &statp
->atime
,
762 &statp
->mtime
, &statp
->length
,
763 &statp
->name
, &statp
->uid
, &statp
->gid
,
764 &statp
->muid
, &statp
->extension
,
765 &statp
->n_uid
, &statp
->n_gid
,
770 V9fsIattr
*iattr
= va_arg(ap
, V9fsIattr
*);
771 offset
+= pdu_unmarshal(pdu
, offset
, "ddddqqqqq",
772 &iattr
->valid
, &iattr
->mode
,
773 &iattr
->uid
, &iattr
->gid
, &iattr
->size
,
774 &iattr
->atime_sec
, &iattr
->atime_nsec
,
775 &iattr
->mtime_sec
, &iattr
->mtime_nsec
);
785 return offset
- old_offset
;
788 static size_t pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
790 size_t old_offset
= offset
;
795 for (i
= 0; fmt
[i
]; i
++) {
798 uint8_t val
= va_arg(ap
, int);
799 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
804 cpu_to_le16w(&val
, va_arg(ap
, int));
805 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
810 cpu_to_le32w(&val
, va_arg(ap
, uint32_t));
811 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
816 cpu_to_le64w(&val
, va_arg(ap
, uint64_t));
817 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
821 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
822 int *iovcnt
= va_arg(ap
, int *);
823 *iovcnt
= pdu_copy_sg(pdu
, offset
, 1, iov
);
827 V9fsString
*str
= va_arg(ap
, V9fsString
*);
828 offset
+= pdu_marshal(pdu
, offset
, "w", str
->size
);
829 offset
+= pdu_pack(pdu
, offset
, str
->data
, str
->size
);
833 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
834 offset
+= pdu_marshal(pdu
, offset
, "bdq",
835 qidp
->type
, qidp
->version
, qidp
->path
);
839 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
840 offset
+= pdu_marshal(pdu
, offset
, "wwdQdddqsssssddd",
841 statp
->size
, statp
->type
, statp
->dev
,
842 &statp
->qid
, statp
->mode
, statp
->atime
,
843 statp
->mtime
, statp
->length
, &statp
->name
,
844 &statp
->uid
, &statp
->gid
, &statp
->muid
,
845 &statp
->extension
, statp
->n_uid
,
846 statp
->n_gid
, statp
->n_muid
);
850 V9fsStatDotl
*statp
= va_arg(ap
, V9fsStatDotl
*);
851 offset
+= pdu_marshal(pdu
, offset
, "qQdddqqqqqqqqqqqqqqq",
852 statp
->st_result_mask
,
853 &statp
->qid
, statp
->st_mode
,
854 statp
->st_uid
, statp
->st_gid
,
855 statp
->st_nlink
, statp
->st_rdev
,
856 statp
->st_size
, statp
->st_blksize
, statp
->st_blocks
,
857 statp
->st_atime_sec
, statp
->st_atime_nsec
,
858 statp
->st_mtime_sec
, statp
->st_mtime_nsec
,
859 statp
->st_ctime_sec
, statp
->st_ctime_nsec
,
860 statp
->st_btime_sec
, statp
->st_btime_nsec
,
861 statp
->st_gen
, statp
->st_data_version
);
870 return offset
- old_offset
;
873 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
875 int8_t id
= pdu
->id
+ 1; /* Response */
881 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
884 str
.data
= strerror(err
);
885 str
.size
= strlen(str
.data
);
887 len
+= pdu_marshal(pdu
, len
, "s", &str
);
891 len
+= pdu_marshal(pdu
, len
, "d", err
);
893 if (s
->proto_version
== V9FS_PROTO_2000L
) {
898 /* fill out the header */
899 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
901 /* keep these in sync */
905 /* push onto queue and notify */
906 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
908 /* FIXME: we should batch these completions */
909 virtio_notify(&s
->vdev
, s
->vq
);
914 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
919 if (mode
& P9_STAT_MODE_DIR
) {
923 if (mode
& P9_STAT_MODE_SYMLINK
) {
926 if (mode
& P9_STAT_MODE_SOCKET
) {
929 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
932 if (mode
& P9_STAT_MODE_DEVICE
) {
933 if (extension
&& extension
->data
[0] == 'c') {
944 if (mode
& P9_STAT_MODE_SETUID
) {
947 if (mode
& P9_STAT_MODE_SETGID
) {
950 if (mode
& P9_STAT_MODE_SETVTX
) {
957 static int donttouch_stat(V9fsStat
*stat
)
959 if (stat
->type
== -1 &&
961 stat
->qid
.type
== -1 &&
962 stat
->qid
.version
== -1 &&
963 stat
->qid
.path
== -1 &&
967 stat
->length
== -1 &&
974 stat
->n_muid
== -1) {
981 static void v9fs_stat_free(V9fsStat
*stat
)
983 v9fs_string_free(&stat
->name
);
984 v9fs_string_free(&stat
->uid
);
985 v9fs_string_free(&stat
->gid
);
986 v9fs_string_free(&stat
->muid
);
987 v9fs_string_free(&stat
->extension
);
990 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
994 mode
= stbuf
->st_mode
& 0777;
995 if (S_ISDIR(stbuf
->st_mode
)) {
996 mode
|= P9_STAT_MODE_DIR
;
999 if (S_ISLNK(stbuf
->st_mode
)) {
1000 mode
|= P9_STAT_MODE_SYMLINK
;
1003 if (S_ISSOCK(stbuf
->st_mode
)) {
1004 mode
|= P9_STAT_MODE_SOCKET
;
1007 if (S_ISFIFO(stbuf
->st_mode
)) {
1008 mode
|= P9_STAT_MODE_NAMED_PIPE
;
1011 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
1012 mode
|= P9_STAT_MODE_DEVICE
;
1015 if (stbuf
->st_mode
& S_ISUID
) {
1016 mode
|= P9_STAT_MODE_SETUID
;
1019 if (stbuf
->st_mode
& S_ISGID
) {
1020 mode
|= P9_STAT_MODE_SETGID
;
1023 if (stbuf
->st_mode
& S_ISVTX
) {
1024 mode
|= P9_STAT_MODE_SETVTX
;
1030 static int stat_to_v9stat(V9fsState
*s
, V9fsString
*name
,
1031 const struct stat
*stbuf
,
1037 memset(v9stat
, 0, sizeof(*v9stat
));
1039 stat_to_qid(stbuf
, &v9stat
->qid
);
1040 v9stat
->mode
= stat_to_v9mode(stbuf
);
1041 v9stat
->atime
= stbuf
->st_atime
;
1042 v9stat
->mtime
= stbuf
->st_mtime
;
1043 v9stat
->length
= stbuf
->st_size
;
1045 v9fs_string_null(&v9stat
->uid
);
1046 v9fs_string_null(&v9stat
->gid
);
1047 v9fs_string_null(&v9stat
->muid
);
1049 v9stat
->n_uid
= stbuf
->st_uid
;
1050 v9stat
->n_gid
= stbuf
->st_gid
;
1053 v9fs_string_null(&v9stat
->extension
);
1055 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
1056 err
= v9fs_do_readlink(s
, name
, &v9stat
->extension
);
1061 v9stat
->extension
.data
[err
] = 0;
1062 v9stat
->extension
.size
= err
;
1063 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
1064 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
1065 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
1066 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
1067 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
1068 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
1069 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
1072 str
= strrchr(name
->data
, '/');
1079 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
1082 v9fs_string_size(&v9stat
->name
) +
1083 v9fs_string_size(&v9stat
->uid
) +
1084 v9fs_string_size(&v9stat
->gid
) +
1085 v9fs_string_size(&v9stat
->muid
) +
1086 v9fs_string_size(&v9stat
->extension
);
1090 #define P9_STATS_MODE 0x00000001ULL
1091 #define P9_STATS_NLINK 0x00000002ULL
1092 #define P9_STATS_UID 0x00000004ULL
1093 #define P9_STATS_GID 0x00000008ULL
1094 #define P9_STATS_RDEV 0x00000010ULL
1095 #define P9_STATS_ATIME 0x00000020ULL
1096 #define P9_STATS_MTIME 0x00000040ULL
1097 #define P9_STATS_CTIME 0x00000080ULL
1098 #define P9_STATS_INO 0x00000100ULL
1099 #define P9_STATS_SIZE 0x00000200ULL
1100 #define P9_STATS_BLOCKS 0x00000400ULL
1102 #define P9_STATS_BTIME 0x00000800ULL
1103 #define P9_STATS_GEN 0x00001000ULL
1104 #define P9_STATS_DATA_VERSION 0x00002000ULL
1106 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1107 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1110 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
1111 V9fsStatDotl
*v9lstat
)
1113 memset(v9lstat
, 0, sizeof(*v9lstat
));
1115 v9lstat
->st_mode
= stbuf
->st_mode
;
1116 v9lstat
->st_nlink
= stbuf
->st_nlink
;
1117 v9lstat
->st_uid
= stbuf
->st_uid
;
1118 v9lstat
->st_gid
= stbuf
->st_gid
;
1119 v9lstat
->st_rdev
= stbuf
->st_rdev
;
1120 v9lstat
->st_size
= stbuf
->st_size
;
1121 v9lstat
->st_blksize
= stbuf
->st_blksize
;
1122 v9lstat
->st_blocks
= stbuf
->st_blocks
;
1123 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
1124 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
1125 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
1126 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
1127 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
1128 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
1129 /* Currently we only support BASIC fields in stat */
1130 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
1132 stat_to_qid(stbuf
, &v9lstat
->qid
);
1135 static struct iovec
*adjust_sg(struct iovec
*sg
, int len
, int *iovcnt
)
1137 while (len
&& *iovcnt
) {
1138 if (len
< sg
->iov_len
) {
1140 sg
->iov_base
+= len
;
1152 static struct iovec
*cap_sg(struct iovec
*sg
, int cap
, int *cnt
)
1157 for (i
= 0; i
< *cnt
; i
++) {
1158 if ((total
+ sg
[i
].iov_len
) > cap
) {
1159 sg
[i
].iov_len
-= ((total
+ sg
[i
].iov_len
) - cap
);
1163 total
+= sg
[i
].iov_len
;
1171 static void print_sg(struct iovec
*sg
, int cnt
)
1175 printf("sg[%d]: {", cnt
);
1176 for (i
= 0; i
< cnt
; i
++) {
1180 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
1185 static void v9fs_fix_path(V9fsString
*dst
, V9fsString
*src
, int len
)
1188 v9fs_string_init(&str
);
1189 v9fs_string_copy(&str
, dst
);
1190 v9fs_string_sprintf(dst
, "%s%s", src
->data
, str
.data
+len
);
1191 v9fs_string_free(&str
);
1194 static void v9fs_version(V9fsState
*s
, V9fsPDU
*pdu
)
1199 pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
1201 if (!strcmp(version
.data
, "9P2000.u")) {
1202 s
->proto_version
= V9FS_PROTO_2000U
;
1203 } else if (!strcmp(version
.data
, "9P2000.L")) {
1204 s
->proto_version
= V9FS_PROTO_2000L
;
1206 v9fs_string_sprintf(&version
, "unknown");
1209 offset
+= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
1210 complete_pdu(s
, pdu
, offset
);
1212 v9fs_string_free(&version
);
1215 static void v9fs_attach(V9fsState
*s
, V9fsPDU
*pdu
)
1217 int32_t fid
, afid
, n_uname
;
1218 V9fsString uname
, aname
;
1224 pdu_unmarshal(pdu
, offset
, "ddssd", &fid
, &afid
, &uname
, &aname
, &n_uname
);
1226 fidp
= alloc_fid(s
, fid
);
1232 fidp
->uid
= n_uname
;
1234 v9fs_string_sprintf(&fidp
->path
, "%s", "/");
1235 err
= fid_to_qid(s
, fidp
, &qid
);
1242 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
1246 complete_pdu(s
, pdu
, err
);
1247 v9fs_string_free(&uname
);
1248 v9fs_string_free(&aname
);
1251 static void v9fs_stat_post_lstat(V9fsState
*s
, V9fsStatState
*vs
, int err
)
1258 err
= stat_to_v9stat(s
, &vs
->fidp
->path
, &vs
->stbuf
, &vs
->v9stat
);
1262 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "wS", 0, &vs
->v9stat
);
1266 complete_pdu(s
, vs
->pdu
, err
);
1267 v9fs_stat_free(&vs
->v9stat
);
1271 static void v9fs_stat(V9fsState
*s
, V9fsPDU
*pdu
)
1277 vs
= qemu_malloc(sizeof(*vs
));
1281 memset(&vs
->v9stat
, 0, sizeof(vs
->v9stat
));
1283 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &fid
);
1285 vs
->fidp
= lookup_fid(s
, fid
);
1286 if (vs
->fidp
== NULL
) {
1291 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1292 v9fs_stat_post_lstat(s
, vs
, err
);
1296 complete_pdu(s
, vs
->pdu
, err
);
1297 v9fs_stat_free(&vs
->v9stat
);
1301 static void v9fs_getattr_post_lstat(V9fsState
*s
, V9fsStatStateDotl
*vs
,
1309 stat_to_v9stat_dotl(s
, &vs
->stbuf
, &vs
->v9stat_dotl
);
1310 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "A", &vs
->v9stat_dotl
);
1314 complete_pdu(s
, vs
->pdu
, err
);
1318 static void v9fs_getattr(V9fsState
*s
, V9fsPDU
*pdu
)
1321 V9fsStatStateDotl
*vs
;
1324 uint64_t request_mask
;
1326 vs
= qemu_malloc(sizeof(*vs
));
1330 memset(&vs
->v9stat_dotl
, 0, sizeof(vs
->v9stat_dotl
));
1332 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dq", &fid
, &request_mask
);
1334 fidp
= lookup_fid(s
, fid
);
1340 /* Currently we only support BASIC fields in stat, so there is no
1341 * need to look at request_mask.
1343 err
= v9fs_do_lstat(s
, &fidp
->path
, &vs
->stbuf
);
1344 v9fs_getattr_post_lstat(s
, vs
, err
);
1348 complete_pdu(s
, vs
->pdu
, err
);
1352 /* From Linux kernel code */
1353 #define ATTR_MODE (1 << 0)
1354 #define ATTR_UID (1 << 1)
1355 #define ATTR_GID (1 << 2)
1356 #define ATTR_SIZE (1 << 3)
1357 #define ATTR_ATIME (1 << 4)
1358 #define ATTR_MTIME (1 << 5)
1359 #define ATTR_CTIME (1 << 6)
1360 #define ATTR_MASK 127
1361 #define ATTR_ATIME_SET (1 << 7)
1362 #define ATTR_MTIME_SET (1 << 8)
1364 static void v9fs_setattr_post_truncate(V9fsState
*s
, V9fsSetattrState
*vs
,
1374 complete_pdu(s
, vs
->pdu
, err
);
1378 static void v9fs_setattr_post_chown(V9fsState
*s
, V9fsSetattrState
*vs
, int err
)
1385 if (vs
->v9iattr
.valid
& (ATTR_SIZE
)) {
1386 err
= v9fs_do_truncate(s
, &vs
->fidp
->path
, vs
->v9iattr
.size
);
1388 v9fs_setattr_post_truncate(s
, vs
, err
);
1392 complete_pdu(s
, vs
->pdu
, err
);
1396 static void v9fs_setattr_post_utimensat(V9fsState
*s
, V9fsSetattrState
*vs
,
1404 /* If the only valid entry in iattr is ctime we can call
1405 * chown(-1,-1) to update the ctime of the file
1407 if ((vs
->v9iattr
.valid
& (ATTR_UID
| ATTR_GID
)) ||
1408 ((vs
->v9iattr
.valid
& ATTR_CTIME
)
1409 && !((vs
->v9iattr
.valid
& ATTR_MASK
) & ~ATTR_CTIME
))) {
1410 if (!(vs
->v9iattr
.valid
& ATTR_UID
)) {
1411 vs
->v9iattr
.uid
= -1;
1413 if (!(vs
->v9iattr
.valid
& ATTR_GID
)) {
1414 vs
->v9iattr
.gid
= -1;
1416 err
= v9fs_do_chown(s
, &vs
->fidp
->path
, vs
->v9iattr
.uid
,
1419 v9fs_setattr_post_chown(s
, vs
, err
);
1423 complete_pdu(s
, vs
->pdu
, err
);
1427 static void v9fs_setattr_post_chmod(V9fsState
*s
, V9fsSetattrState
*vs
, int err
)
1434 if (vs
->v9iattr
.valid
& (ATTR_ATIME
| ATTR_MTIME
)) {
1435 struct timespec times
[2];
1436 if (vs
->v9iattr
.valid
& ATTR_ATIME
) {
1437 if (vs
->v9iattr
.valid
& ATTR_ATIME_SET
) {
1438 times
[0].tv_sec
= vs
->v9iattr
.atime_sec
;
1439 times
[0].tv_nsec
= vs
->v9iattr
.atime_nsec
;
1441 times
[0].tv_nsec
= UTIME_NOW
;
1444 times
[0].tv_nsec
= UTIME_OMIT
;
1447 if (vs
->v9iattr
.valid
& ATTR_MTIME
) {
1448 if (vs
->v9iattr
.valid
& ATTR_MTIME_SET
) {
1449 times
[1].tv_sec
= vs
->v9iattr
.mtime_sec
;
1450 times
[1].tv_nsec
= vs
->v9iattr
.mtime_nsec
;
1452 times
[1].tv_nsec
= UTIME_NOW
;
1455 times
[1].tv_nsec
= UTIME_OMIT
;
1457 err
= v9fs_do_utimensat(s
, &vs
->fidp
->path
, times
);
1459 v9fs_setattr_post_utimensat(s
, vs
, err
);
1463 complete_pdu(s
, vs
->pdu
, err
);
1467 static void v9fs_setattr(V9fsState
*s
, V9fsPDU
*pdu
)
1470 V9fsSetattrState
*vs
;
1473 vs
= qemu_malloc(sizeof(*vs
));
1477 pdu_unmarshal(pdu
, vs
->offset
, "dI", &fid
, &vs
->v9iattr
);
1479 vs
->fidp
= lookup_fid(s
, fid
);
1480 if (vs
->fidp
== NULL
) {
1485 if (vs
->v9iattr
.valid
& ATTR_MODE
) {
1486 err
= v9fs_do_chmod(s
, &vs
->fidp
->path
, vs
->v9iattr
.mode
);
1489 v9fs_setattr_post_chmod(s
, vs
, err
);
1493 complete_pdu(s
, vs
->pdu
, err
);
1497 static void v9fs_walk_complete(V9fsState
*s
, V9fsWalkState
*vs
, int err
)
1499 complete_pdu(s
, vs
->pdu
, err
);
1501 if (vs
->nwnames
&& vs
->nwnames
<= P9_MAXWELEM
) {
1502 for (vs
->name_idx
= 0; vs
->name_idx
< vs
->nwnames
; vs
->name_idx
++) {
1503 v9fs_string_free(&vs
->wnames
[vs
->name_idx
]);
1506 qemu_free(vs
->wnames
);
1507 qemu_free(vs
->qids
);
1511 static void v9fs_walk_marshal(V9fsWalkState
*vs
)
1515 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "w", vs
->nwnames
);
1517 for (i
= 0; i
< vs
->nwnames
; i
++) {
1518 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qids
[i
]);
1522 static void v9fs_walk_post_newfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
1526 free_fid(s
, vs
->newfidp
->fid
);
1527 v9fs_string_free(&vs
->path
);
1532 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1535 if (vs
->name_idx
< vs
->nwnames
) {
1536 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1537 vs
->wnames
[vs
->name_idx
].data
);
1538 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1540 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1541 v9fs_walk_post_newfid_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_post_oldfid_lstat(V9fsState
*s
, V9fsWalkState
*vs
,
1556 v9fs_string_free(&vs
->path
);
1561 stat_to_qid(&vs
->stbuf
, &vs
->qids
[vs
->name_idx
]);
1563 if (vs
->name_idx
< vs
->nwnames
) {
1565 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1566 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1567 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1569 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1570 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1574 v9fs_string_free(&vs
->path
);
1575 v9fs_walk_marshal(vs
);
1578 v9fs_walk_complete(s
, vs
, err
);
1581 static void v9fs_walk(V9fsState
*s
, V9fsPDU
*pdu
)
1583 int32_t fid
, newfid
;
1588 vs
= qemu_malloc(sizeof(*vs
));
1594 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "ddw", &fid
,
1595 &newfid
, &vs
->nwnames
);
1597 if (vs
->nwnames
&& vs
->nwnames
<= P9_MAXWELEM
) {
1598 vs
->wnames
= qemu_mallocz(sizeof(vs
->wnames
[0]) * vs
->nwnames
);
1600 vs
->qids
= qemu_mallocz(sizeof(vs
->qids
[0]) * vs
->nwnames
);
1602 for (i
= 0; i
< vs
->nwnames
; i
++) {
1603 vs
->offset
+= pdu_unmarshal(vs
->pdu
, vs
->offset
, "s",
1606 } else if (vs
->nwnames
> P9_MAXWELEM
) {
1611 vs
->fidp
= lookup_fid(s
, fid
);
1612 if (vs
->fidp
== NULL
) {
1617 /* FIXME: is this really valid? */
1618 if (fid
== newfid
) {
1620 BUG_ON(vs
->fidp
->fid_type
!= P9_FID_NONE
);
1621 v9fs_string_init(&vs
->path
);
1624 if (vs
->name_idx
< vs
->nwnames
) {
1625 v9fs_string_sprintf(&vs
->path
, "%s/%s",
1626 vs
->fidp
->path
.data
, vs
->wnames
[vs
->name_idx
].data
);
1627 v9fs_string_copy(&vs
->fidp
->path
, &vs
->path
);
1629 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1630 v9fs_walk_post_oldfid_lstat(s
, vs
, err
);
1634 vs
->newfidp
= alloc_fid(s
, newfid
);
1635 if (vs
->newfidp
== NULL
) {
1640 vs
->newfidp
->uid
= vs
->fidp
->uid
;
1641 v9fs_string_init(&vs
->path
);
1643 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->fidp
->path
);
1645 if (vs
->name_idx
< vs
->nwnames
) {
1646 v9fs_string_sprintf(&vs
->path
, "%s/%s", vs
->newfidp
->path
.data
,
1647 vs
->wnames
[vs
->name_idx
].data
);
1648 v9fs_string_copy(&vs
->newfidp
->path
, &vs
->path
);
1650 err
= v9fs_do_lstat(s
, &vs
->newfidp
->path
, &vs
->stbuf
);
1651 v9fs_walk_post_newfid_lstat(s
, vs
, err
);
1656 v9fs_walk_marshal(vs
);
1659 v9fs_walk_complete(s
, vs
, err
);
1662 static int32_t get_iounit(V9fsState
*s
, V9fsString
*name
)
1664 struct statfs stbuf
;
1668 * iounit should be multiples of f_bsize (host filesystem block size
1669 * and as well as less than (client msize - P9_IOHDRSZ))
1671 if (!v9fs_do_statfs(s
, name
, &stbuf
)) {
1672 iounit
= stbuf
.f_bsize
;
1673 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1677 iounit
= s
->msize
- P9_IOHDRSZ
;
1682 static void v9fs_open_post_opendir(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1684 if (vs
->fidp
->fs
.dir
== NULL
) {
1688 vs
->fidp
->fid_type
= P9_FID_DIR
;
1689 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, 0);
1692 complete_pdu(s
, vs
->pdu
, err
);
1697 static void v9fs_open_post_getiounit(V9fsState
*s
, V9fsOpenState
*vs
)
1700 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, vs
->iounit
);
1702 complete_pdu(s
, vs
->pdu
, err
);
1706 static void v9fs_open_post_open(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1708 if (vs
->fidp
->fs
.fd
== -1) {
1712 vs
->fidp
->fid_type
= P9_FID_FILE
;
1713 vs
->iounit
= get_iounit(s
, &vs
->fidp
->path
);
1714 v9fs_open_post_getiounit(s
, vs
);
1717 complete_pdu(s
, vs
->pdu
, err
);
1721 static void v9fs_open_post_lstat(V9fsState
*s
, V9fsOpenState
*vs
, int err
)
1730 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
1732 if (S_ISDIR(vs
->stbuf
.st_mode
)) {
1733 vs
->fidp
->fs
.dir
= v9fs_do_opendir(s
, &vs
->fidp
->path
);
1734 v9fs_open_post_opendir(s
, vs
, err
);
1736 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1738 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
1739 /* Ignore direct disk access hint until the server supports it. */
1742 flags
= omode_to_uflags(vs
->mode
);
1744 vs
->fidp
->fs
.fd
= v9fs_do_open(s
, &vs
->fidp
->path
, flags
);
1745 v9fs_open_post_open(s
, vs
, err
);
1749 complete_pdu(s
, vs
->pdu
, err
);
1753 static void v9fs_open(V9fsState
*s
, V9fsPDU
*pdu
)
1759 vs
= qemu_malloc(sizeof(*vs
));
1764 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1765 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dd", &fid
, &vs
->mode
);
1767 pdu_unmarshal(vs
->pdu
, vs
->offset
, "db", &fid
, &vs
->mode
);
1770 vs
->fidp
= lookup_fid(s
, fid
);
1771 if (vs
->fidp
== NULL
) {
1776 BUG_ON(vs
->fidp
->fid_type
!= P9_FID_NONE
);
1778 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
1780 v9fs_open_post_lstat(s
, vs
, err
);
1783 complete_pdu(s
, pdu
, err
);
1787 static void v9fs_post_lcreate(V9fsState
*s
, V9fsLcreateState
*vs
, int err
)
1790 v9fs_string_copy(&vs
->fidp
->path
, &vs
->fullname
);
1791 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
1792 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
,
1796 vs
->fidp
->fid_type
= P9_FID_NONE
;
1798 if (vs
->fidp
->fs
.fd
> 0) {
1799 close(vs
->fidp
->fs
.fd
);
1803 complete_pdu(s
, vs
->pdu
, err
);
1804 v9fs_string_free(&vs
->name
);
1805 v9fs_string_free(&vs
->fullname
);
1809 static void v9fs_lcreate_post_get_iounit(V9fsState
*s
, V9fsLcreateState
*vs
,
1816 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
1819 v9fs_post_lcreate(s
, vs
, err
);
1822 static void v9fs_lcreate_post_do_open2(V9fsState
*s
, V9fsLcreateState
*vs
,
1825 if (vs
->fidp
->fs
.fd
== -1) {
1829 vs
->fidp
->fid_type
= P9_FID_FILE
;
1830 vs
->iounit
= get_iounit(s
, &vs
->fullname
);
1831 v9fs_lcreate_post_get_iounit(s
, vs
, err
);
1835 v9fs_post_lcreate(s
, vs
, err
);
1838 static void v9fs_lcreate(V9fsState
*s
, V9fsPDU
*pdu
)
1840 int32_t dfid
, flags
, mode
;
1842 V9fsLcreateState
*vs
;
1845 vs
= qemu_malloc(sizeof(*vs
));
1849 v9fs_string_init(&vs
->fullname
);
1851 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsddd", &dfid
, &vs
->name
, &flags
,
1854 vs
->fidp
= lookup_fid(s
, dfid
);
1855 if (vs
->fidp
== NULL
) {
1860 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", vs
->fidp
->path
.data
,
1863 /* Ignore direct disk access hint until the server supports it. */
1866 vs
->fidp
->fs
.fd
= v9fs_do_open2(s
, vs
->fullname
.data
, vs
->fidp
->uid
,
1868 v9fs_lcreate_post_do_open2(s
, vs
, err
);
1872 complete_pdu(s
, vs
->pdu
, err
);
1873 v9fs_string_free(&vs
->name
);
1877 static void v9fs_post_do_fsync(V9fsState
*s
, V9fsPDU
*pdu
, int err
)
1882 complete_pdu(s
, pdu
, err
);
1885 static void v9fs_fsync(V9fsState
*s
, V9fsPDU
*pdu
)
1893 pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1894 fidp
= lookup_fid(s
, fid
);
1897 v9fs_post_do_fsync(s
, pdu
, err
);
1900 err
= v9fs_do_fsync(s
, fidp
->fs
.fd
, datasync
);
1901 v9fs_post_do_fsync(s
, pdu
, err
);
1904 static void v9fs_clunk(V9fsState
*s
, V9fsPDU
*pdu
)
1910 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1912 err
= free_fid(s
, fid
);
1920 complete_pdu(s
, pdu
, err
);
1923 static void v9fs_read_post_readdir(V9fsState
*, V9fsReadState
*, ssize_t
);
1925 static void v9fs_read_post_seekdir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1930 v9fs_stat_free(&vs
->v9stat
);
1931 v9fs_string_free(&vs
->name
);
1932 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
1933 vs
->offset
+= vs
->count
;
1936 complete_pdu(s
, vs
->pdu
, err
);
1941 static void v9fs_read_post_dir_lstat(V9fsState
*s
, V9fsReadState
*vs
,
1948 err
= stat_to_v9stat(s
, &vs
->name
, &vs
->stbuf
, &vs
->v9stat
);
1953 vs
->len
= pdu_marshal(vs
->pdu
, vs
->offset
+ 4 + vs
->count
, "S",
1955 if ((vs
->len
!= (vs
->v9stat
.size
+ 2)) ||
1956 ((vs
->count
+ vs
->len
) > vs
->max_count
)) {
1957 v9fs_do_seekdir(s
, vs
->fidp
->fs
.dir
, vs
->dir_pos
);
1958 v9fs_read_post_seekdir(s
, vs
, err
);
1961 vs
->count
+= vs
->len
;
1962 v9fs_stat_free(&vs
->v9stat
);
1963 v9fs_string_free(&vs
->name
);
1964 vs
->dir_pos
= vs
->dent
->d_off
;
1965 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->fs
.dir
);
1966 v9fs_read_post_readdir(s
, vs
, err
);
1969 v9fs_do_seekdir(s
, vs
->fidp
->fs
.dir
, vs
->dir_pos
);
1970 v9fs_read_post_seekdir(s
, vs
, err
);
1975 static void v9fs_read_post_readdir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1978 memset(&vs
->v9stat
, 0, sizeof(vs
->v9stat
));
1979 v9fs_string_init(&vs
->name
);
1980 v9fs_string_sprintf(&vs
->name
, "%s/%s", vs
->fidp
->path
.data
,
1982 err
= v9fs_do_lstat(s
, &vs
->name
, &vs
->stbuf
);
1983 v9fs_read_post_dir_lstat(s
, vs
, err
);
1987 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
1988 vs
->offset
+= vs
->count
;
1990 complete_pdu(s
, vs
->pdu
, err
);
1995 static void v9fs_read_post_telldir(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
1997 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->fs
.dir
);
1998 v9fs_read_post_readdir(s
, vs
, err
);
2002 static void v9fs_read_post_rewinddir(V9fsState
*s
, V9fsReadState
*vs
,
2005 vs
->dir_pos
= v9fs_do_telldir(s
, vs
->fidp
->fs
.dir
);
2006 v9fs_read_post_telldir(s
, vs
, err
);
2010 static void v9fs_read_post_preadv(V9fsState
*s
, V9fsReadState
*vs
, ssize_t err
)
2013 /* IO error return the error */
2017 vs
->total
+= vs
->len
;
2018 vs
->sg
= adjust_sg(vs
->sg
, vs
->len
, &vs
->cnt
);
2019 if (vs
->total
< vs
->count
&& vs
->len
> 0) {
2022 print_sg(vs
->sg
, vs
->cnt
);
2024 vs
->len
= v9fs_do_preadv(s
, vs
->fidp
->fs
.fd
, vs
->sg
, vs
->cnt
,
2029 } while (vs
->len
== -1 && errno
== EINTR
);
2030 if (vs
->len
== -1) {
2033 v9fs_read_post_preadv(s
, vs
, err
);
2036 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->total
);
2037 vs
->offset
+= vs
->count
;
2041 complete_pdu(s
, vs
->pdu
, err
);
2045 static void v9fs_xattr_read(V9fsState
*s
, V9fsReadState
*vs
)
2051 xattr_len
= vs
->fidp
->fs
.xattr
.len
;
2052 read_count
= xattr_len
- vs
->off
;
2053 if (read_count
> vs
->count
) {
2054 read_count
= vs
->count
;
2055 } else if (read_count
< 0) {
2057 * read beyond XATTR value
2061 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", read_count
);
2062 vs
->offset
+= pdu_pack(vs
->pdu
, vs
->offset
,
2063 ((char *)vs
->fidp
->fs
.xattr
.value
) + vs
->off
,
2066 complete_pdu(s
, vs
->pdu
, err
);
2070 static void v9fs_read(V9fsState
*s
, V9fsPDU
*pdu
)
2076 vs
= qemu_malloc(sizeof(*vs
));
2083 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dqd", &fid
, &vs
->off
, &vs
->count
);
2085 vs
->fidp
= lookup_fid(s
, fid
);
2086 if (vs
->fidp
== NULL
) {
2091 if (vs
->fidp
->fid_type
== P9_FID_DIR
) {
2092 vs
->max_count
= vs
->count
;
2095 v9fs_do_rewinddir(s
, vs
->fidp
->fs
.dir
);
2097 v9fs_read_post_rewinddir(s
, vs
, err
);
2099 } else if (vs
->fidp
->fid_type
== P9_FID_FILE
) {
2101 pdu_marshal(vs
->pdu
, vs
->offset
+ 4, "v", vs
->sg
, &vs
->cnt
);
2102 vs
->sg
= cap_sg(vs
->sg
, vs
->count
, &vs
->cnt
);
2103 if (vs
->total
<= vs
->count
) {
2104 vs
->len
= v9fs_do_preadv(s
, vs
->fidp
->fs
.fd
, vs
->sg
, vs
->cnt
,
2110 v9fs_read_post_preadv(s
, vs
, err
);
2113 } else if (vs
->fidp
->fid_type
== P9_FID_XATTR
) {
2114 v9fs_xattr_read(s
, vs
);
2120 complete_pdu(s
, pdu
, err
);
2124 typedef struct V9fsReadDirState
{
2128 off_t saved_dir_pos
;
2129 struct dirent
*dent
;
2133 int64_t initial_offset
;
2137 static void v9fs_readdir_post_seekdir(V9fsState
*s
, V9fsReadDirState
*vs
)
2139 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
2140 vs
->offset
+= vs
->count
;
2141 complete_pdu(s
, vs
->pdu
, vs
->offset
);
2146 /* Size of each dirent on the wire: size of qid (13) + size of offset (8)
2147 * size of type (1) + size of name.size (2) + strlen(name.data)
2149 #define V9_READDIR_DATA_SZ (24 + strlen(vs->name.data))
2151 static void v9fs_readdir_post_readdir(V9fsState
*s
, V9fsReadDirState
*vs
)
2157 v9fs_string_init(&vs
->name
);
2158 v9fs_string_sprintf(&vs
->name
, "%s", vs
->dent
->d_name
);
2160 if ((vs
->count
+ V9_READDIR_DATA_SZ
) > vs
->max_count
) {
2161 /* Ran out of buffer. Set dir back to old position and return */
2162 v9fs_do_seekdir(s
, vs
->fidp
->fs
.dir
, vs
->saved_dir_pos
);
2163 v9fs_readdir_post_seekdir(s
, vs
);
2167 /* Fill up just the path field of qid because the client uses
2168 * only that. To fill the entire qid structure we will have
2169 * to stat each dirent found, which is expensive
2171 size
= MIN(sizeof(vs
->dent
->d_ino
), sizeof(vs
->qid
.path
));
2172 memcpy(&vs
->qid
.path
, &vs
->dent
->d_ino
, size
);
2173 /* Fill the other fields with dummy values */
2175 vs
->qid
.version
= 0;
2177 len
= pdu_marshal(vs
->pdu
, vs
->offset
+4+vs
->count
, "Qqbs",
2178 &vs
->qid
, vs
->dent
->d_off
,
2179 vs
->dent
->d_type
, &vs
->name
);
2181 v9fs_string_free(&vs
->name
);
2182 vs
->saved_dir_pos
= vs
->dent
->d_off
;
2183 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->fs
.dir
);
2184 v9fs_readdir_post_readdir(s
, vs
);
2188 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->count
);
2189 vs
->offset
+= vs
->count
;
2190 complete_pdu(s
, vs
->pdu
, vs
->offset
);
2195 static void v9fs_readdir_post_telldir(V9fsState
*s
, V9fsReadDirState
*vs
)
2197 vs
->dent
= v9fs_do_readdir(s
, vs
->fidp
->fs
.dir
);
2198 v9fs_readdir_post_readdir(s
, vs
);
2202 static void v9fs_readdir_post_setdir(V9fsState
*s
, V9fsReadDirState
*vs
)
2204 vs
->saved_dir_pos
= v9fs_do_telldir(s
, vs
->fidp
->fs
.dir
);
2205 v9fs_readdir_post_telldir(s
, vs
);
2209 static void v9fs_readdir(V9fsState
*s
, V9fsPDU
*pdu
)
2212 V9fsReadDirState
*vs
;
2216 vs
= qemu_malloc(sizeof(*vs
));
2221 pdu_unmarshal(vs
->pdu
, offset
, "dqd", &fid
, &vs
->initial_offset
,
2224 vs
->fidp
= lookup_fid(s
, fid
);
2225 if (vs
->fidp
== NULL
|| !(vs
->fidp
->fs
.dir
)) {
2230 if (vs
->initial_offset
== 0) {
2231 v9fs_do_rewinddir(s
, vs
->fidp
->fs
.dir
);
2233 v9fs_do_seekdir(s
, vs
->fidp
->fs
.dir
, vs
->initial_offset
);
2236 v9fs_readdir_post_setdir(s
, vs
);
2240 complete_pdu(s
, pdu
, err
);
2245 static void v9fs_write_post_pwritev(V9fsState
*s
, V9fsWriteState
*vs
,
2249 /* IO error return the error */
2253 vs
->total
+= vs
->len
;
2254 vs
->sg
= adjust_sg(vs
->sg
, vs
->len
, &vs
->cnt
);
2255 if (vs
->total
< vs
->count
&& vs
->len
> 0) {
2258 print_sg(vs
->sg
, vs
->cnt
);
2260 vs
->len
= v9fs_do_pwritev(s
, vs
->fidp
->fs
.fd
, vs
->sg
, vs
->cnt
,
2265 } while (vs
->len
== -1 && errno
== EINTR
);
2266 if (vs
->len
== -1) {
2269 v9fs_write_post_pwritev(s
, vs
, err
);
2272 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "d", vs
->total
);
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 vs
->sg
= cap_sg(vs
->sg
, vs
->count
, &vs
->cnt
);
2360 if (vs
->total
<= vs
->count
) {
2361 vs
->len
= v9fs_do_pwritev(s
, vs
->fidp
->fs
.fd
, vs
->sg
, vs
->cnt
, vs
->off
);
2366 v9fs_write_post_pwritev(s
, vs
, err
);
2370 complete_pdu(s
, vs
->pdu
, err
);
2374 static void v9fs_create_post_getiounit(V9fsState
*s
, V9fsCreateState
*vs
)
2377 v9fs_string_copy(&vs
->fidp
->path
, &vs
->fullname
);
2378 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
2380 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Qd", &vs
->qid
, vs
->iounit
);
2383 complete_pdu(s
, vs
->pdu
, err
);
2384 v9fs_string_free(&vs
->name
);
2385 v9fs_string_free(&vs
->extension
);
2386 v9fs_string_free(&vs
->fullname
);
2390 static void v9fs_post_create(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2393 vs
->iounit
= get_iounit(s
, &vs
->fidp
->path
);
2394 v9fs_create_post_getiounit(s
, vs
);
2398 complete_pdu(s
, vs
->pdu
, err
);
2399 v9fs_string_free(&vs
->name
);
2400 v9fs_string_free(&vs
->extension
);
2401 v9fs_string_free(&vs
->fullname
);
2405 static void v9fs_create_post_perms(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2410 v9fs_post_create(s
, vs
, err
);
2413 static void v9fs_create_post_opendir(V9fsState
*s
, V9fsCreateState
*vs
,
2416 if (!vs
->fidp
->fs
.dir
) {
2419 vs
->fidp
->fid_type
= P9_FID_DIR
;
2420 v9fs_post_create(s
, vs
, err
);
2423 static void v9fs_create_post_dir_lstat(V9fsState
*s
, V9fsCreateState
*vs
,
2431 vs
->fidp
->fs
.dir
= v9fs_do_opendir(s
, &vs
->fullname
);
2432 v9fs_create_post_opendir(s
, vs
, err
);
2436 v9fs_post_create(s
, vs
, err
);
2439 static void v9fs_create_post_mkdir(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2446 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
2447 v9fs_create_post_dir_lstat(s
, vs
, err
);
2451 v9fs_post_create(s
, vs
, err
);
2454 static void v9fs_create_post_fstat(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2457 vs
->fidp
->fid_type
= P9_FID_NONE
;
2458 close(vs
->fidp
->fs
.fd
);
2461 v9fs_post_create(s
, vs
, err
);
2465 static void v9fs_create_post_open2(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2467 if (vs
->fidp
->fs
.fd
== -1) {
2471 vs
->fidp
->fid_type
= P9_FID_FILE
;
2472 err
= v9fs_do_fstat(s
, vs
->fidp
->fs
.fd
, &vs
->stbuf
);
2473 v9fs_create_post_fstat(s
, vs
, err
);
2478 v9fs_post_create(s
, vs
, err
);
2482 static void v9fs_create_post_lstat(V9fsState
*s
, V9fsCreateState
*vs
, int err
)
2485 if (err
== 0 || errno
!= ENOENT
) {
2490 if (vs
->perm
& P9_STAT_MODE_DIR
) {
2491 err
= v9fs_do_mkdir(s
, vs
->fullname
.data
, vs
->perm
& 0777,
2493 v9fs_create_post_mkdir(s
, vs
, err
);
2494 } else if (vs
->perm
& P9_STAT_MODE_SYMLINK
) {
2495 err
= v9fs_do_symlink(s
, vs
->fidp
, vs
->extension
.data
,
2496 vs
->fullname
.data
, -1);
2497 v9fs_create_post_perms(s
, vs
, err
);
2498 } else if (vs
->perm
& P9_STAT_MODE_LINK
) {
2499 int32_t nfid
= atoi(vs
->extension
.data
);
2500 V9fsFidState
*nfidp
= lookup_fid(s
, nfid
);
2501 if (nfidp
== NULL
) {
2503 v9fs_post_create(s
, vs
, err
);
2505 err
= v9fs_do_link(s
, &nfidp
->path
, &vs
->fullname
);
2506 v9fs_create_post_perms(s
, vs
, err
);
2507 } else if (vs
->perm
& P9_STAT_MODE_DEVICE
) {
2509 uint32_t major
, minor
;
2512 if (sscanf(vs
->extension
.data
, "%c %u %u", &ctype
, &major
,
2515 v9fs_post_create(s
, vs
, err
);
2527 v9fs_post_create(s
, vs
, err
);
2530 nmode
|= vs
->perm
& 0777;
2531 err
= v9fs_do_mknod(s
, vs
->fullname
.data
, nmode
,
2532 makedev(major
, minor
), vs
->fidp
->uid
, -1);
2533 v9fs_create_post_perms(s
, vs
, err
);
2534 } else if (vs
->perm
& P9_STAT_MODE_NAMED_PIPE
) {
2535 err
= v9fs_do_mknod(s
, vs
->fullname
.data
, S_IFIFO
| (vs
->perm
& 0777),
2536 0, vs
->fidp
->uid
, -1);
2537 v9fs_post_create(s
, vs
, err
);
2538 } else if (vs
->perm
& P9_STAT_MODE_SOCKET
) {
2539 err
= v9fs_do_mknod(s
, vs
->fullname
.data
, S_IFSOCK
| (vs
->perm
& 0777),
2540 0, vs
->fidp
->uid
, -1);
2541 v9fs_post_create(s
, vs
, err
);
2543 vs
->fidp
->fs
.fd
= v9fs_do_open2(s
, vs
->fullname
.data
, vs
->fidp
->uid
,
2544 -1, omode_to_uflags(vs
->mode
)|O_CREAT
, vs
->perm
);
2546 v9fs_create_post_open2(s
, vs
, err
);
2552 v9fs_post_create(s
, vs
, err
);
2555 static void v9fs_create(V9fsState
*s
, V9fsPDU
*pdu
)
2558 V9fsCreateState
*vs
;
2561 vs
= qemu_malloc(sizeof(*vs
));
2565 v9fs_string_init(&vs
->fullname
);
2567 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsdbs", &fid
, &vs
->name
,
2568 &vs
->perm
, &vs
->mode
, &vs
->extension
);
2570 vs
->fidp
= lookup_fid(s
, fid
);
2571 if (vs
->fidp
== NULL
) {
2576 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", vs
->fidp
->path
.data
,
2579 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
2580 v9fs_create_post_lstat(s
, vs
, err
);
2584 complete_pdu(s
, vs
->pdu
, err
);
2585 v9fs_string_free(&vs
->name
);
2586 v9fs_string_free(&vs
->extension
);
2590 static void v9fs_post_symlink(V9fsState
*s
, V9fsSymlinkState
*vs
, int err
)
2593 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
2594 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qid
);
2599 complete_pdu(s
, vs
->pdu
, err
);
2600 v9fs_string_free(&vs
->name
);
2601 v9fs_string_free(&vs
->symname
);
2602 v9fs_string_free(&vs
->fullname
);
2606 static void v9fs_symlink_post_do_symlink(V9fsState
*s
, V9fsSymlinkState
*vs
,
2612 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
2614 v9fs_post_symlink(s
, vs
, err
);
2617 static void v9fs_symlink(V9fsState
*s
, V9fsPDU
*pdu
)
2620 V9fsSymlinkState
*vs
;
2624 vs
= qemu_malloc(sizeof(*vs
));
2628 v9fs_string_init(&vs
->fullname
);
2630 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dssd", &dfid
, &vs
->name
,
2631 &vs
->symname
, &gid
);
2633 vs
->dfidp
= lookup_fid(s
, dfid
);
2634 if (vs
->dfidp
== NULL
) {
2639 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", vs
->dfidp
->path
.data
,
2641 err
= v9fs_do_symlink(s
, vs
->dfidp
, vs
->symname
.data
,
2642 vs
->fullname
.data
, gid
);
2643 v9fs_symlink_post_do_symlink(s
, vs
, err
);
2647 complete_pdu(s
, vs
->pdu
, err
);
2648 v9fs_string_free(&vs
->name
);
2649 v9fs_string_free(&vs
->symname
);
2653 static void v9fs_flush(V9fsState
*s
, V9fsPDU
*pdu
)
2655 /* A nop call with no return */
2656 complete_pdu(s
, pdu
, 7);
2659 static void v9fs_link(V9fsState
*s
, V9fsPDU
*pdu
)
2661 int32_t dfid
, oldfid
;
2662 V9fsFidState
*dfidp
, *oldfidp
;
2663 V9fsString name
, fullname
;
2667 v9fs_string_init(&fullname
);
2669 pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2671 dfidp
= lookup_fid(s
, dfid
);
2672 if (dfidp
== NULL
) {
2677 oldfidp
= lookup_fid(s
, oldfid
);
2678 if (oldfidp
== NULL
) {
2683 v9fs_string_sprintf(&fullname
, "%s/%s", dfidp
->path
.data
, name
.data
);
2685 err
= v9fs_do_link(s
, &oldfidp
->path
, &fullname
);
2689 v9fs_string_free(&fullname
);
2692 v9fs_string_free(&name
);
2693 complete_pdu(s
, pdu
, err
);
2696 static void v9fs_remove_post_remove(V9fsState
*s
, V9fsRemoveState
*vs
,
2705 /* For TREMOVE we need to clunk the fid even on failed remove */
2706 free_fid(s
, vs
->fidp
->fid
);
2708 complete_pdu(s
, vs
->pdu
, err
);
2712 static void v9fs_remove(V9fsState
*s
, V9fsPDU
*pdu
)
2715 V9fsRemoveState
*vs
;
2718 vs
= qemu_malloc(sizeof(*vs
));
2722 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &fid
);
2724 vs
->fidp
= lookup_fid(s
, fid
);
2725 if (vs
->fidp
== NULL
) {
2730 err
= v9fs_do_remove(s
, &vs
->fidp
->path
);
2731 v9fs_remove_post_remove(s
, vs
, err
);
2735 complete_pdu(s
, pdu
, err
);
2739 static void v9fs_wstat_post_truncate(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2748 v9fs_stat_free(&vs
->v9stat
);
2749 complete_pdu(s
, vs
->pdu
, err
);
2753 static void v9fs_wstat_post_rename(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2758 if (vs
->v9stat
.length
!= -1) {
2759 if (v9fs_do_truncate(s
, &vs
->fidp
->path
, vs
->v9stat
.length
) < 0) {
2763 v9fs_wstat_post_truncate(s
, vs
, err
);
2767 v9fs_stat_free(&vs
->v9stat
);
2768 complete_pdu(s
, vs
->pdu
, err
);
2772 static int v9fs_complete_rename(V9fsState
*s
, V9fsRenameState
*vs
)
2775 char *old_name
, *new_name
;
2778 if (vs
->newdirfid
!= -1) {
2779 V9fsFidState
*dirfidp
;
2780 dirfidp
= lookup_fid(s
, vs
->newdirfid
);
2782 if (dirfidp
== NULL
) {
2787 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2789 new_name
= qemu_mallocz(dirfidp
->path
.size
+ vs
->name
.size
+ 2);
2791 strcpy(new_name
, dirfidp
->path
.data
);
2792 strcat(new_name
, "/");
2793 strcat(new_name
+ dirfidp
->path
.size
, vs
->name
.data
);
2795 old_name
= vs
->fidp
->path
.data
;
2796 end
= strrchr(old_name
, '/');
2802 new_name
= qemu_mallocz(end
- old_name
+ vs
->name
.size
+ 1);
2804 strncat(new_name
, old_name
, end
- old_name
);
2805 strncat(new_name
+ (end
- old_name
), vs
->name
.data
, vs
->name
.size
);
2808 v9fs_string_free(&vs
->name
);
2809 vs
->name
.data
= qemu_strdup(new_name
);
2810 vs
->name
.size
= strlen(new_name
);
2812 if (strcmp(new_name
, vs
->fidp
->path
.data
) != 0) {
2813 if (v9fs_do_rename(s
, &vs
->fidp
->path
, &vs
->name
)) {
2818 * Fixup fid's pointing to the old name to
2819 * start pointing to the new name
2821 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
2822 if (vs
->fidp
== fidp
) {
2824 * we replace name of this fid towards the end so
2825 * that our below v9fs_path_is_ancestor check will
2830 if (v9fs_path_is_ancestor(&vs
->fidp
->path
, &fidp
->path
)) {
2831 /* replace the name */
2832 v9fs_fix_path(&fidp
->path
, &vs
->name
,
2833 strlen(vs
->fidp
->path
.data
));
2836 v9fs_string_copy(&vs
->fidp
->path
, &vs
->name
);
2840 v9fs_string_free(&vs
->name
);
2844 static void v9fs_rename_post_rename(V9fsState
*s
, V9fsRenameState
*vs
, int err
)
2846 complete_pdu(s
, vs
->pdu
, err
);
2850 static void v9fs_wstat_post_chown(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2856 if (vs
->v9stat
.name
.size
!= 0) {
2857 V9fsRenameState
*vr
;
2859 vr
= qemu_mallocz(sizeof(V9fsRenameState
));
2862 vr
->fidp
= vs
->fidp
;
2863 vr
->offset
= vs
->offset
;
2864 vr
->name
.size
= vs
->v9stat
.name
.size
;
2865 vr
->name
.data
= qemu_strdup(vs
->v9stat
.name
.data
);
2867 err
= v9fs_complete_rename(s
, vr
);
2870 v9fs_wstat_post_rename(s
, vs
, err
);
2874 v9fs_stat_free(&vs
->v9stat
);
2875 complete_pdu(s
, vs
->pdu
, err
);
2879 static void v9fs_rename(V9fsState
*s
, V9fsPDU
*pdu
)
2882 V9fsRenameState
*vs
;
2885 vs
= qemu_malloc(sizeof(*vs
));
2889 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dds", &fid
, &vs
->newdirfid
, &vs
->name
);
2891 vs
->fidp
= lookup_fid(s
, fid
);
2892 if (vs
->fidp
== NULL
) {
2897 BUG_ON(vs
->fidp
->fid_type
!= P9_FID_NONE
);
2899 err
= v9fs_complete_rename(s
, vs
);
2900 v9fs_rename_post_rename(s
, vs
, err
);
2903 complete_pdu(s
, vs
->pdu
, err
);
2907 static void v9fs_wstat_post_utime(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2913 if (vs
->v9stat
.n_gid
!= -1 || vs
->v9stat
.n_uid
!= -1) {
2914 if (v9fs_do_chown(s
, &vs
->fidp
->path
, vs
->v9stat
.n_uid
,
2915 vs
->v9stat
.n_gid
)) {
2919 v9fs_wstat_post_chown(s
, vs
, err
);
2923 v9fs_stat_free(&vs
->v9stat
);
2924 complete_pdu(s
, vs
->pdu
, err
);
2928 static void v9fs_wstat_post_chmod(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2934 if (vs
->v9stat
.mtime
!= -1 || vs
->v9stat
.atime
!= -1) {
2935 struct timespec times
[2];
2936 if (vs
->v9stat
.atime
!= -1) {
2937 times
[0].tv_sec
= vs
->v9stat
.atime
;
2938 times
[0].tv_nsec
= 0;
2940 times
[0].tv_nsec
= UTIME_OMIT
;
2942 if (vs
->v9stat
.mtime
!= -1) {
2943 times
[1].tv_sec
= vs
->v9stat
.mtime
;
2944 times
[1].tv_nsec
= 0;
2946 times
[1].tv_nsec
= UTIME_OMIT
;
2949 if (v9fs_do_utimensat(s
, &vs
->fidp
->path
, times
)) {
2954 v9fs_wstat_post_utime(s
, vs
, err
);
2958 v9fs_stat_free(&vs
->v9stat
);
2959 complete_pdu(s
, vs
->pdu
, err
);
2963 static void v9fs_wstat_post_fsync(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2968 v9fs_stat_free(&vs
->v9stat
);
2969 complete_pdu(s
, vs
->pdu
, err
);
2973 static void v9fs_wstat_post_lstat(V9fsState
*s
, V9fsWstatState
*vs
, int err
)
2982 v9_mode
= stat_to_v9mode(&vs
->stbuf
);
2984 if ((vs
->v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2985 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2986 /* Attempting to change the type */
2991 if (v9fs_do_chmod(s
, &vs
->fidp
->path
, v9mode_to_mode(vs
->v9stat
.mode
,
2992 &vs
->v9stat
.extension
))) {
2995 v9fs_wstat_post_chmod(s
, vs
, err
);
2999 v9fs_stat_free(&vs
->v9stat
);
3000 complete_pdu(s
, vs
->pdu
, err
);
3004 static void v9fs_wstat(V9fsState
*s
, V9fsPDU
*pdu
)
3010 vs
= qemu_malloc(sizeof(*vs
));
3014 pdu_unmarshal(pdu
, vs
->offset
, "dwS", &fid
, &vs
->unused
, &vs
->v9stat
);
3016 vs
->fidp
= lookup_fid(s
, fid
);
3017 if (vs
->fidp
== NULL
) {
3022 /* do we need to sync the file? */
3023 if (donttouch_stat(&vs
->v9stat
)) {
3024 err
= v9fs_do_fsync(s
, vs
->fidp
->fs
.fd
, 0);
3025 v9fs_wstat_post_fsync(s
, vs
, err
);
3029 if (vs
->v9stat
.mode
!= -1) {
3030 err
= v9fs_do_lstat(s
, &vs
->fidp
->path
, &vs
->stbuf
);
3031 v9fs_wstat_post_lstat(s
, vs
, err
);
3035 v9fs_wstat_post_chmod(s
, vs
, err
);
3039 v9fs_stat_free(&vs
->v9stat
);
3040 complete_pdu(s
, vs
->pdu
, err
);
3044 static void v9fs_statfs_post_statfs(V9fsState
*s
, V9fsStatfsState
*vs
, int err
)
3046 int32_t bsize_factor
;
3054 * compute bsize factor based on host file system block size
3057 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/vs
->stbuf
.f_bsize
;
3058 if (!bsize_factor
) {
3061 vs
->v9statfs
.f_type
= vs
->stbuf
.f_type
;
3062 vs
->v9statfs
.f_bsize
= vs
->stbuf
.f_bsize
;
3063 vs
->v9statfs
.f_bsize
*= bsize_factor
;
3065 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3066 * adjust(divide) the number of blocks, free blocks and available
3067 * blocks by bsize factor
3069 vs
->v9statfs
.f_blocks
= vs
->stbuf
.f_blocks
/bsize_factor
;
3070 vs
->v9statfs
.f_bfree
= vs
->stbuf
.f_bfree
/bsize_factor
;
3071 vs
->v9statfs
.f_bavail
= vs
->stbuf
.f_bavail
/bsize_factor
;
3072 vs
->v9statfs
.f_files
= vs
->stbuf
.f_files
;
3073 vs
->v9statfs
.f_ffree
= vs
->stbuf
.f_ffree
;
3074 vs
->v9statfs
.fsid_val
= (unsigned int) vs
->stbuf
.f_fsid
.__val
[0] |
3075 (unsigned long long)vs
->stbuf
.f_fsid
.__val
[1] << 32;
3076 vs
->v9statfs
.f_namelen
= vs
->stbuf
.f_namelen
;
3078 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "ddqqqqqqd",
3079 vs
->v9statfs
.f_type
, vs
->v9statfs
.f_bsize
, vs
->v9statfs
.f_blocks
,
3080 vs
->v9statfs
.f_bfree
, vs
->v9statfs
.f_bavail
, vs
->v9statfs
.f_files
,
3081 vs
->v9statfs
.f_ffree
, vs
->v9statfs
.fsid_val
,
3082 vs
->v9statfs
.f_namelen
);
3085 complete_pdu(s
, vs
->pdu
, vs
->offset
);
3089 static void v9fs_statfs(V9fsState
*s
, V9fsPDU
*pdu
)
3091 V9fsStatfsState
*vs
;
3094 vs
= qemu_malloc(sizeof(*vs
));
3098 memset(&vs
->v9statfs
, 0, sizeof(vs
->v9statfs
));
3100 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &vs
->fid
);
3102 vs
->fidp
= lookup_fid(s
, vs
->fid
);
3103 if (vs
->fidp
== NULL
) {
3108 err
= v9fs_do_statfs(s
, &vs
->fidp
->path
, &vs
->stbuf
);
3109 v9fs_statfs_post_statfs(s
, vs
, err
);
3113 complete_pdu(s
, vs
->pdu
, err
);
3117 static void v9fs_mknod_post_lstat(V9fsState
*s
, V9fsMkState
*vs
, int err
)
3124 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
3125 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qid
);
3128 complete_pdu(s
, vs
->pdu
, err
);
3129 v9fs_string_free(&vs
->fullname
);
3130 v9fs_string_free(&vs
->name
);
3134 static void v9fs_mknod_post_mknod(V9fsState
*s
, V9fsMkState
*vs
, int err
)
3141 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
3142 v9fs_mknod_post_lstat(s
, vs
, err
);
3145 complete_pdu(s
, vs
->pdu
, err
);
3146 v9fs_string_free(&vs
->fullname
);
3147 v9fs_string_free(&vs
->name
);
3151 static void v9fs_mknod(V9fsState
*s
, V9fsPDU
*pdu
)
3161 vs
= qemu_malloc(sizeof(*vs
));
3165 v9fs_string_init(&vs
->fullname
);
3166 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsdddd", &fid
, &vs
->name
, &mode
,
3167 &major
, &minor
, &gid
);
3169 fidp
= lookup_fid(s
, fid
);
3175 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", fidp
->path
.data
, vs
->name
.data
);
3176 err
= v9fs_do_mknod(s
, vs
->fullname
.data
, mode
, makedev(major
, minor
),
3178 v9fs_mknod_post_mknod(s
, vs
, err
);
3182 complete_pdu(s
, vs
->pdu
, err
);
3183 v9fs_string_free(&vs
->fullname
);
3184 v9fs_string_free(&vs
->name
);
3189 * Implement posix byte range locking code
3190 * Server side handling of locking code is very simple, because 9p server in
3191 * QEMU can handle only one client. And most of the lock handling
3192 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3193 * do any thing in * qemu 9p server side lock code path.
3194 * So when a TLOCK request comes, always return success
3197 static void v9fs_lock(V9fsState
*s
, V9fsPDU
*pdu
)
3199 int32_t fid
, err
= 0;
3202 vs
= qemu_mallocz(sizeof(*vs
));
3206 vs
->flock
= qemu_malloc(sizeof(*vs
->flock
));
3207 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dbdqqds", &fid
, &vs
->flock
->type
,
3208 &vs
->flock
->flags
, &vs
->flock
->start
, &vs
->flock
->length
,
3209 &vs
->flock
->proc_id
, &vs
->flock
->client_id
);
3211 vs
->status
= P9_LOCK_ERROR
;
3213 /* We support only block flag now (that too ignored currently) */
3214 if (vs
->flock
->flags
& ~P9_LOCK_FLAGS_BLOCK
) {
3218 vs
->fidp
= lookup_fid(s
, fid
);
3219 if (vs
->fidp
== NULL
) {
3224 err
= v9fs_do_fstat(s
, vs
->fidp
->fs
.fd
, &vs
->stbuf
);
3229 vs
->status
= P9_LOCK_SUCCESS
;
3231 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "b", vs
->status
);
3232 complete_pdu(s
, vs
->pdu
, err
);
3233 qemu_free(vs
->flock
);
3238 * When a TGETLOCK request comes, always return success because all lock
3239 * handling is done by client's VFS layer.
3242 static void v9fs_getlock(V9fsState
*s
, V9fsPDU
*pdu
)
3244 int32_t fid
, err
= 0;
3245 V9fsGetlockState
*vs
;
3247 vs
= qemu_mallocz(sizeof(*vs
));
3251 vs
->glock
= qemu_malloc(sizeof(*vs
->glock
));
3252 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dbqqds", &fid
, &vs
->glock
->type
,
3253 &vs
->glock
->start
, &vs
->glock
->length
, &vs
->glock
->proc_id
,
3254 &vs
->glock
->client_id
);
3256 vs
->fidp
= lookup_fid(s
, fid
);
3257 if (vs
->fidp
== NULL
) {
3262 err
= v9fs_do_fstat(s
, vs
->fidp
->fs
.fd
, &vs
->stbuf
);
3267 vs
->glock
->type
= F_UNLCK
;
3268 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "bqqds", vs
->glock
->type
,
3269 vs
->glock
->start
, vs
->glock
->length
, vs
->glock
->proc_id
,
3270 &vs
->glock
->client_id
);
3272 complete_pdu(s
, vs
->pdu
, err
);
3273 qemu_free(vs
->glock
);
3277 static void v9fs_mkdir_post_lstat(V9fsState
*s
, V9fsMkState
*vs
, int err
)
3284 stat_to_qid(&vs
->stbuf
, &vs
->qid
);
3285 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "Q", &vs
->qid
);
3288 complete_pdu(s
, vs
->pdu
, err
);
3289 v9fs_string_free(&vs
->fullname
);
3290 v9fs_string_free(&vs
->name
);
3294 static void v9fs_mkdir_post_mkdir(V9fsState
*s
, V9fsMkState
*vs
, int err
)
3301 err
= v9fs_do_lstat(s
, &vs
->fullname
, &vs
->stbuf
);
3302 v9fs_mkdir_post_lstat(s
, vs
, err
);
3305 complete_pdu(s
, vs
->pdu
, err
);
3306 v9fs_string_free(&vs
->fullname
);
3307 v9fs_string_free(&vs
->name
);
3311 static void v9fs_mkdir(V9fsState
*s
, V9fsPDU
*pdu
)
3320 vs
= qemu_malloc(sizeof(*vs
));
3324 v9fs_string_init(&vs
->fullname
);
3325 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsdd", &fid
, &vs
->name
, &mode
,
3328 fidp
= lookup_fid(s
, fid
);
3334 v9fs_string_sprintf(&vs
->fullname
, "%s/%s", fidp
->path
.data
, vs
->name
.data
);
3335 err
= v9fs_do_mkdir(s
, vs
->fullname
.data
, mode
, fidp
->uid
, gid
);
3336 v9fs_mkdir_post_mkdir(s
, vs
, err
);
3340 complete_pdu(s
, vs
->pdu
, err
);
3341 v9fs_string_free(&vs
->fullname
);
3342 v9fs_string_free(&vs
->name
);
3346 static void v9fs_post_xattr_getvalue(V9fsState
*s
, V9fsXattrState
*vs
, int err
)
3351 free_fid(s
, vs
->xattr_fidp
->fid
);
3354 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "q", vs
->size
);
3357 complete_pdu(s
, vs
->pdu
, err
);
3358 v9fs_string_free(&vs
->name
);
3363 static void v9fs_post_xattr_check(V9fsState
*s
, V9fsXattrState
*vs
, ssize_t err
)
3367 free_fid(s
, vs
->xattr_fidp
->fid
);
3371 * Read the xattr value
3373 vs
->xattr_fidp
->fs
.xattr
.len
= vs
->size
;
3374 vs
->xattr_fidp
->fid_type
= P9_FID_XATTR
;
3375 vs
->xattr_fidp
->fs
.xattr
.copied_len
= -1;
3377 vs
->xattr_fidp
->fs
.xattr
.value
= qemu_malloc(vs
->size
);
3378 err
= v9fs_do_lgetxattr(s
, &vs
->xattr_fidp
->path
,
3379 &vs
->name
, vs
->xattr_fidp
->fs
.xattr
.value
,
3380 vs
->xattr_fidp
->fs
.xattr
.len
);
3382 v9fs_post_xattr_getvalue(s
, vs
, err
);
3385 complete_pdu(s
, vs
->pdu
, err
);
3386 v9fs_string_free(&vs
->name
);
3390 static void v9fs_post_lxattr_getvalue(V9fsState
*s
,
3391 V9fsXattrState
*vs
, int err
)
3395 free_fid(s
, vs
->xattr_fidp
->fid
);
3398 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "q", vs
->size
);
3401 complete_pdu(s
, vs
->pdu
, err
);
3402 v9fs_string_free(&vs
->name
);
3407 static void v9fs_post_lxattr_check(V9fsState
*s
,
3408 V9fsXattrState
*vs
, ssize_t err
)
3412 free_fid(s
, vs
->xattr_fidp
->fid
);
3416 * Read the xattr value
3418 vs
->xattr_fidp
->fs
.xattr
.len
= vs
->size
;
3419 vs
->xattr_fidp
->fid_type
= P9_FID_XATTR
;
3420 vs
->xattr_fidp
->fs
.xattr
.copied_len
= -1;
3422 vs
->xattr_fidp
->fs
.xattr
.value
= qemu_malloc(vs
->size
);
3423 err
= v9fs_do_llistxattr(s
, &vs
->xattr_fidp
->path
,
3424 vs
->xattr_fidp
->fs
.xattr
.value
,
3425 vs
->xattr_fidp
->fs
.xattr
.len
);
3427 v9fs_post_lxattr_getvalue(s
, vs
, err
);
3430 complete_pdu(s
, vs
->pdu
, err
);
3431 v9fs_string_free(&vs
->name
);
3435 static void v9fs_xattrwalk(V9fsState
*s
, V9fsPDU
*pdu
)
3439 int32_t fid
, newfid
;
3441 vs
= qemu_malloc(sizeof(*vs
));
3445 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dds", &fid
, &newfid
, &vs
->name
);
3446 vs
->file_fidp
= lookup_fid(s
, fid
);
3447 if (vs
->file_fidp
== NULL
) {
3452 vs
->xattr_fidp
= alloc_fid(s
, newfid
);
3453 if (vs
->xattr_fidp
== NULL
) {
3458 v9fs_string_copy(&vs
->xattr_fidp
->path
, &vs
->file_fidp
->path
);
3459 if (vs
->name
.data
[0] == 0) {
3461 * listxattr request. Get the size first
3463 vs
->size
= v9fs_do_llistxattr(s
, &vs
->xattr_fidp
->path
,
3468 v9fs_post_lxattr_check(s
, vs
, err
);
3472 * specific xattr fid. We check for xattr
3473 * presence also collect the xattr size
3475 vs
->size
= v9fs_do_lgetxattr(s
, &vs
->xattr_fidp
->path
,
3476 &vs
->name
, NULL
, 0);
3480 v9fs_post_xattr_check(s
, vs
, err
);
3484 complete_pdu(s
, vs
->pdu
, err
);
3485 v9fs_string_free(&vs
->name
);
3489 static void v9fs_xattrcreate(V9fsState
*s
, V9fsPDU
*pdu
)
3496 vs
= qemu_malloc(sizeof(*vs
));
3500 pdu_unmarshal(vs
->pdu
, vs
->offset
, "dsqd",
3501 &fid
, &vs
->name
, &vs
->size
, &flags
);
3503 vs
->file_fidp
= lookup_fid(s
, fid
);
3504 if (vs
->file_fidp
== NULL
) {
3509 /* Make the file fid point to xattr */
3510 vs
->xattr_fidp
= vs
->file_fidp
;
3511 vs
->xattr_fidp
->fid_type
= P9_FID_XATTR
;
3512 vs
->xattr_fidp
->fs
.xattr
.copied_len
= 0;
3513 vs
->xattr_fidp
->fs
.xattr
.len
= vs
->size
;
3514 vs
->xattr_fidp
->fs
.xattr
.flags
= flags
;
3515 v9fs_string_init(&vs
->xattr_fidp
->fs
.xattr
.name
);
3516 v9fs_string_copy(&vs
->xattr_fidp
->fs
.xattr
.name
, &vs
->name
);
3518 vs
->xattr_fidp
->fs
.xattr
.value
= qemu_malloc(vs
->size
);
3520 vs
->xattr_fidp
->fs
.xattr
.value
= NULL
;
3523 complete_pdu(s
, vs
->pdu
, err
);
3524 v9fs_string_free(&vs
->name
);
3528 static void v9fs_readlink_post_readlink(V9fsState
*s
, V9fsReadLinkState
*vs
,
3535 vs
->offset
+= pdu_marshal(vs
->pdu
, vs
->offset
, "s", &vs
->target
);
3538 complete_pdu(s
, vs
->pdu
, err
);
3539 v9fs_string_free(&vs
->target
);
3543 static void v9fs_readlink(V9fsState
*s
, V9fsPDU
*pdu
)
3546 V9fsReadLinkState
*vs
;
3550 vs
= qemu_malloc(sizeof(*vs
));
3554 pdu_unmarshal(vs
->pdu
, vs
->offset
, "d", &fid
);
3556 fidp
= lookup_fid(s
, fid
);
3562 v9fs_string_init(&vs
->target
);
3563 err
= v9fs_do_readlink(s
, &fidp
->path
, &vs
->target
);
3564 v9fs_readlink_post_readlink(s
, vs
, err
);
3567 complete_pdu(s
, vs
->pdu
, err
);
3571 typedef void (pdu_handler_t
)(V9fsState
*s
, V9fsPDU
*pdu
);
3573 static pdu_handler_t
*pdu_handlers
[] = {
3574 [P9_TREADDIR
] = v9fs_readdir
,
3575 [P9_TSTATFS
] = v9fs_statfs
,
3576 [P9_TGETATTR
] = v9fs_getattr
,
3577 [P9_TSETATTR
] = v9fs_setattr
,
3578 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3579 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3580 [P9_TMKNOD
] = v9fs_mknod
,
3581 [P9_TRENAME
] = v9fs_rename
,
3582 [P9_TLOCK
] = v9fs_lock
,
3583 [P9_TGETLOCK
] = v9fs_getlock
,
3584 [P9_TREADLINK
] = v9fs_readlink
,
3585 [P9_TMKDIR
] = v9fs_mkdir
,
3586 [P9_TVERSION
] = v9fs_version
,
3587 [P9_TLOPEN
] = v9fs_open
,
3588 [P9_TATTACH
] = v9fs_attach
,
3589 [P9_TSTAT
] = v9fs_stat
,
3590 [P9_TWALK
] = v9fs_walk
,
3591 [P9_TCLUNK
] = v9fs_clunk
,
3592 [P9_TFSYNC
] = v9fs_fsync
,
3593 [P9_TOPEN
] = v9fs_open
,
3594 [P9_TREAD
] = v9fs_read
,
3596 [P9_TAUTH
] = v9fs_auth
,
3598 [P9_TFLUSH
] = v9fs_flush
,
3599 [P9_TLINK
] = v9fs_link
,
3600 [P9_TSYMLINK
] = v9fs_symlink
,
3601 [P9_TCREATE
] = v9fs_create
,
3602 [P9_TLCREATE
] = v9fs_lcreate
,
3603 [P9_TWRITE
] = v9fs_write
,
3604 [P9_TWSTAT
] = v9fs_wstat
,
3605 [P9_TREMOVE
] = v9fs_remove
,
3608 static void v9fs_op_not_supp(V9fsState
*s
, V9fsPDU
*pdu
)
3610 complete_pdu(s
, pdu
, -EOPNOTSUPP
);
3613 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
3615 pdu_handler_t
*handler
;
3620 if (pdu
->id
>= ARRAY_SIZE(pdu_handlers
) ||
3621 (pdu_handlers
[pdu
->id
] == NULL
)) {
3622 handler
= v9fs_op_not_supp
;
3624 handler
= pdu_handlers
[pdu
->id
];
3629 void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
3631 V9fsState
*s
= (V9fsState
*)vdev
;
3635 while ((pdu
= alloc_pdu(s
)) &&
3636 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
3639 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
3640 BUG_ON(pdu
->elem
.out_sg
[0].iov_len
< 7);
3642 ptr
= pdu
->elem
.out_sg
[0].iov_base
;
3644 memcpy(&pdu
->size
, ptr
, 4);
3646 memcpy(&pdu
->tag
, ptr
+ 5, 2);