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-xattr.h"
21 #include "virtio-9p-coth.h"
26 static int open_fd_rc
;
40 static int omode_to_uflags(int8_t mode
)
74 static int dotl_to_at_flags(int flags
)
77 if (flags
& P9_DOTL_AT_REMOVEDIR
) {
78 rflags
|= AT_REMOVEDIR
;
83 struct dotl_openflag_map
{
88 static int dotl_to_open_flags(int flags
)
92 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
93 * and P9_DOTL_NOACCESS
95 int oflags
= flags
& O_ACCMODE
;
97 struct dotl_openflag_map dotl_oflag_map
[] = {
98 { P9_DOTL_CREATE
, O_CREAT
},
99 { P9_DOTL_EXCL
, O_EXCL
},
100 { P9_DOTL_NOCTTY
, O_NOCTTY
},
101 { P9_DOTL_TRUNC
, O_TRUNC
},
102 { P9_DOTL_APPEND
, O_APPEND
},
103 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
104 { P9_DOTL_DSYNC
, O_DSYNC
},
105 { P9_DOTL_FASYNC
, FASYNC
},
106 { P9_DOTL_DIRECT
, O_DIRECT
},
107 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
108 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
109 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
110 { P9_DOTL_NOATIME
, O_NOATIME
},
111 { P9_DOTL_SYNC
, O_SYNC
},
114 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
115 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
116 oflags
|= dotl_oflag_map
[i
].open_flag
;
123 void cred_init(FsCred
*credp
)
131 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
135 * Filter the client open flags
137 flags
= dotl_to_open_flags(oflags
);
138 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
140 * Ignore direct disk access hint until the server supports it.
146 void v9fs_string_init(V9fsString
*str
)
152 void v9fs_string_free(V9fsString
*str
)
159 void v9fs_string_null(V9fsString
*str
)
161 v9fs_string_free(str
);
164 static int number_to_string(void *arg
, char type
)
166 unsigned int ret
= 0;
170 unsigned int num
= *(unsigned int *)arg
;
179 unsigned long num
= *(unsigned long *)arg
;
187 printf("Number_to_string: Unknown number format\n");
194 static int GCC_FMT_ATTR(2, 0)
195 v9fs_string_alloc_printf(char **strp
, const char *fmt
, va_list ap
)
198 char *iter
= (char *)fmt
;
202 unsigned int arg_uint
;
203 unsigned long arg_ulong
;
205 /* Find the number of %'s that denotes an argument */
206 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
211 len
= strlen(fmt
) - 2*nr_args
;
221 /* Now parse the format string */
222 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
226 arg_uint
= va_arg(ap2
, unsigned int);
227 len
+= number_to_string((void *)&arg_uint
, 'u');
230 if (*++iter
== 'u') {
231 arg_ulong
= va_arg(ap2
, unsigned long);
232 len
+= number_to_string((void *)&arg_ulong
, 'U');
238 arg_char_ptr
= va_arg(ap2
, char *);
239 len
+= strlen(arg_char_ptr
);
246 "v9fs_string_alloc_printf:Incorrect format %c", *iter
);
253 *strp
= g_malloc((len
+ 1) * sizeof(**strp
));
255 return vsprintf(*strp
, fmt
, ap
);
258 void GCC_FMT_ATTR(2, 3)
259 v9fs_string_sprintf(V9fsString
*str
, const char *fmt
, ...)
264 v9fs_string_free(str
);
267 err
= v9fs_string_alloc_printf(&str
->data
, fmt
, ap
);
274 void v9fs_string_copy(V9fsString
*lhs
, V9fsString
*rhs
)
276 v9fs_string_free(lhs
);
277 v9fs_string_sprintf(lhs
, "%s", rhs
->data
);
280 void v9fs_path_init(V9fsPath
*path
)
286 void v9fs_path_free(V9fsPath
*path
)
293 void v9fs_path_copy(V9fsPath
*lhs
, V9fsPath
*rhs
)
296 lhs
->data
= g_malloc(rhs
->size
);
297 memcpy(lhs
->data
, rhs
->data
, rhs
->size
);
298 lhs
->size
= rhs
->size
;
301 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
302 const char *name
, V9fsPath
*path
)
305 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
313 * Return TRUE if s1 is an ancestor of s2.
315 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
316 * As a special case, We treat s1 as ancestor of s2 if they are same!
318 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
320 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
321 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
328 static size_t v9fs_string_size(V9fsString
*str
)
334 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
335 static int v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
338 if (f
->fid_type
== P9_FID_FILE
) {
339 if (f
->fs
.fd
== -1) {
341 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
342 } while (err
== -EINTR
&& !pdu
->cancelled
);
344 } else if (f
->fid_type
== P9_FID_DIR
) {
345 if (f
->fs
.dir
== NULL
) {
347 err
= v9fs_co_opendir(pdu
, f
);
348 } while (err
== -EINTR
&& !pdu
->cancelled
);
354 static V9fsFidState
*get_fid(V9fsPDU
*pdu
, int32_t fid
)
358 V9fsState
*s
= pdu
->s
;
360 for (f
= s
->fid_list
; f
; f
= f
->next
) {
364 * Update the fid ref upfront so that
365 * we don't get reclaimed when we yield
370 * check whether we need to reopen the
371 * file. We might have closed the fd
372 * while trying to free up some file
375 err
= v9fs_reopen_fid(pdu
, f
);
381 * Mark the fid as referenced so that the LRU
382 * reclaim won't close the file descriptor
384 f
->flags
|= FID_REFERENCED
;
391 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
395 for (f
= s
->fid_list
; f
; f
= f
->next
) {
396 /* If fid is already there return NULL */
402 f
= g_malloc0(sizeof(V9fsFidState
));
404 f
->fid_type
= P9_FID_NONE
;
407 * Mark the fid as referenced so that the LRU
408 * reclaim won't close the file descriptor
410 f
->flags
|= FID_REFERENCED
;
411 f
->next
= s
->fid_list
;
417 static int v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
421 if (fidp
->fs
.xattr
.copied_len
== -1) {
422 /* getxattr/listxattr fid */
426 * if this is fid for setxattr. clunk should
427 * result in setxattr localcall
429 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
430 /* clunk after partial write */
434 if (fidp
->fs
.xattr
.len
) {
435 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
436 fidp
->fs
.xattr
.value
,
438 fidp
->fs
.xattr
.flags
);
440 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
443 v9fs_string_free(&fidp
->fs
.xattr
.name
);
445 if (fidp
->fs
.xattr
.value
) {
446 g_free(fidp
->fs
.xattr
.value
);
451 static int free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
455 if (fidp
->fid_type
== P9_FID_FILE
) {
456 /* If we reclaimed the fd no need to close */
457 if (fidp
->fs
.fd
!= -1) {
458 retval
= v9fs_co_close(pdu
, fidp
->fs
.fd
);
460 } else if (fidp
->fid_type
== P9_FID_DIR
) {
461 if (fidp
->fs
.dir
!= NULL
) {
462 retval
= v9fs_co_closedir(pdu
, fidp
->fs
.dir
);
464 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
465 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
467 v9fs_path_free(&fidp
->path
);
472 static void put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
477 * Don't free the fid if it is in reclaim list
479 if (!fidp
->ref
&& fidp
->clunked
) {
484 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
486 V9fsFidState
**fidpp
, *fidp
;
488 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
489 if ((*fidpp
)->fid
== fid
) {
493 if (*fidpp
== NULL
) {
502 void v9fs_reclaim_fd(V9fsPDU
*pdu
)
504 int reclaim_count
= 0;
505 V9fsState
*s
= pdu
->s
;
506 V9fsFidState
*f
, *reclaim_list
= NULL
;
508 for (f
= s
->fid_list
; f
; f
= f
->next
) {
510 * Unlink fids cannot be reclaimed. Check
511 * for them and skip them. Also skip fids
512 * currently being operated on.
514 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
518 * if it is a recently referenced fid
519 * we leave the fid untouched and clear the
520 * reference bit. We come back to it later
521 * in the next iteration. (a simple LRU without
522 * moving list elements around)
524 if (f
->flags
& FID_REFERENCED
) {
525 f
->flags
&= ~FID_REFERENCED
;
529 * Add fids to reclaim list.
531 if (f
->fid_type
== P9_FID_FILE
) {
532 if (f
->fs
.fd
!= -1) {
534 * Up the reference count so that
535 * a clunk request won't free this fid
538 f
->rclm_lst
= reclaim_list
;
540 f
->fs_reclaim
.fd
= f
->fs
.fd
;
544 } else if (f
->fid_type
== P9_FID_DIR
) {
545 if (f
->fs
.dir
!= NULL
) {
547 * Up the reference count so that
548 * a clunk request won't free this fid
551 f
->rclm_lst
= reclaim_list
;
553 f
->fs_reclaim
.dir
= f
->fs
.dir
;
558 if (reclaim_count
>= open_fd_rc
) {
563 * Now close the fid in reclaim list. Free them if they
564 * are already clunked.
566 while (reclaim_list
) {
568 reclaim_list
= f
->rclm_lst
;
569 if (f
->fid_type
== P9_FID_FILE
) {
570 v9fs_co_close(pdu
, f
->fs_reclaim
.fd
);
571 } else if (f
->fid_type
== P9_FID_DIR
) {
572 v9fs_co_closedir(pdu
, f
->fs_reclaim
.dir
);
576 * Now drop the fid reference, free it
583 static int v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
586 V9fsState
*s
= pdu
->s
;
587 V9fsFidState
*fidp
, head_fid
;
589 head_fid
.next
= s
->fid_list
;
590 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
591 if (fidp
->path
.size
!= path
->size
) {
594 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
595 /* Mark the fid non reclaimable. */
596 fidp
->flags
|= FID_NON_RECLAIMABLE
;
598 /* reopen the file/dir if already closed */
599 err
= v9fs_reopen_fid(pdu
, fidp
);
604 * Go back to head of fid list because
605 * the list could have got updated when
606 * switched to the worker thread
616 #define P9_QID_TYPE_DIR 0x80
617 #define P9_QID_TYPE_SYMLINK 0x02
619 #define P9_STAT_MODE_DIR 0x80000000
620 #define P9_STAT_MODE_APPEND 0x40000000
621 #define P9_STAT_MODE_EXCL 0x20000000
622 #define P9_STAT_MODE_MOUNT 0x10000000
623 #define P9_STAT_MODE_AUTH 0x08000000
624 #define P9_STAT_MODE_TMP 0x04000000
625 #define P9_STAT_MODE_SYMLINK 0x02000000
626 #define P9_STAT_MODE_LINK 0x01000000
627 #define P9_STAT_MODE_DEVICE 0x00800000
628 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
629 #define P9_STAT_MODE_SOCKET 0x00100000
630 #define P9_STAT_MODE_SETUID 0x00080000
631 #define P9_STAT_MODE_SETGID 0x00040000
632 #define P9_STAT_MODE_SETVTX 0x00010000
634 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
635 P9_STAT_MODE_SYMLINK | \
636 P9_STAT_MODE_LINK | \
637 P9_STAT_MODE_DEVICE | \
638 P9_STAT_MODE_NAMED_PIPE | \
641 /* This is the algorithm from ufs in spfs */
642 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
646 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
647 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
648 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
649 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
651 if (S_ISDIR(stbuf
->st_mode
)) {
652 qidp
->type
|= P9_QID_TYPE_DIR
;
654 if (S_ISLNK(stbuf
->st_mode
)) {
655 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
659 static int fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
664 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
668 stat_to_qid(&stbuf
, qidp
);
672 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
676 if (!QLIST_EMPTY(&s
->free_list
)) {
677 pdu
= QLIST_FIRST(&s
->free_list
);
678 QLIST_REMOVE(pdu
, next
);
679 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
684 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
688 * Cancelled pdu are added back to the freelist
691 if (!pdu
->cancelled
) {
692 QLIST_REMOVE(pdu
, next
);
693 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
698 size_t pdu_packunpack(void *addr
, struct iovec
*sg
, int sg_count
,
699 size_t offset
, size_t size
, int pack
)
704 for (i
= 0; size
&& i
< sg_count
; i
++) {
706 if (offset
>= sg
[i
].iov_len
) {
708 offset
-= sg
[i
].iov_len
;
711 len
= MIN(sg
[i
].iov_len
- offset
, size
);
713 memcpy(sg
[i
].iov_base
+ offset
, addr
, len
);
715 memcpy(addr
, sg
[i
].iov_base
+ offset
, len
);
730 static size_t pdu_unpack(void *dst
, V9fsPDU
*pdu
, size_t offset
, size_t size
)
732 return pdu_packunpack(dst
, pdu
->elem
.out_sg
, pdu
->elem
.out_num
,
736 static size_t pdu_pack(V9fsPDU
*pdu
, size_t offset
, const void *src
,
739 return pdu_packunpack((void *)src
, pdu
->elem
.in_sg
, pdu
->elem
.in_num
,
743 static int pdu_copy_sg(V9fsPDU
*pdu
, size_t offset
, int rx
, struct iovec
*sg
)
747 struct iovec
*src_sg
;
751 src_sg
= pdu
->elem
.in_sg
;
752 num
= pdu
->elem
.in_num
;
754 src_sg
= pdu
->elem
.out_sg
;
755 num
= pdu
->elem
.out_num
;
759 for (i
= 0; i
< num
; i
++) {
761 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
762 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
764 } else if (offset
< (src_sg
[i
].iov_len
+ pos
)) {
765 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
766 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
767 sg
[j
].iov_base
+= (offset
- pos
);
768 sg
[j
].iov_len
-= (offset
- pos
);
771 pos
+= src_sg
[i
].iov_len
;
777 static size_t pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
779 size_t old_offset
= offset
;
784 for (i
= 0; fmt
[i
]; i
++) {
787 uint8_t *valp
= va_arg(ap
, uint8_t *);
788 offset
+= pdu_unpack(valp
, pdu
, offset
, sizeof(*valp
));
793 valp
= va_arg(ap
, uint16_t *);
794 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
795 *valp
= le16_to_cpu(val
);
800 valp
= va_arg(ap
, uint32_t *);
801 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
802 *valp
= le32_to_cpu(val
);
807 valp
= va_arg(ap
, uint64_t *);
808 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
809 *valp
= le64_to_cpu(val
);
813 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
814 int *iovcnt
= va_arg(ap
, int *);
815 *iovcnt
= pdu_copy_sg(pdu
, offset
, 0, iov
);
819 V9fsString
*str
= va_arg(ap
, V9fsString
*);
820 offset
+= pdu_unmarshal(pdu
, offset
, "w", &str
->size
);
821 /* FIXME: sanity check str->size */
822 str
->data
= g_malloc(str
->size
+ 1);
823 offset
+= pdu_unpack(str
->data
, pdu
, offset
, str
->size
);
824 str
->data
[str
->size
] = 0;
828 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
829 offset
+= pdu_unmarshal(pdu
, offset
, "bdq",
830 &qidp
->type
, &qidp
->version
, &qidp
->path
);
834 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
835 offset
+= pdu_unmarshal(pdu
, offset
, "wwdQdddqsssssddd",
836 &statp
->size
, &statp
->type
, &statp
->dev
,
837 &statp
->qid
, &statp
->mode
, &statp
->atime
,
838 &statp
->mtime
, &statp
->length
,
839 &statp
->name
, &statp
->uid
, &statp
->gid
,
840 &statp
->muid
, &statp
->extension
,
841 &statp
->n_uid
, &statp
->n_gid
,
846 V9fsIattr
*iattr
= va_arg(ap
, V9fsIattr
*);
847 offset
+= pdu_unmarshal(pdu
, offset
, "ddddqqqqq",
848 &iattr
->valid
, &iattr
->mode
,
849 &iattr
->uid
, &iattr
->gid
, &iattr
->size
,
850 &iattr
->atime_sec
, &iattr
->atime_nsec
,
851 &iattr
->mtime_sec
, &iattr
->mtime_nsec
);
861 return offset
- old_offset
;
864 static size_t pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
866 size_t old_offset
= offset
;
871 for (i
= 0; fmt
[i
]; i
++) {
874 uint8_t val
= va_arg(ap
, int);
875 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
880 cpu_to_le16w(&val
, va_arg(ap
, int));
881 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
886 cpu_to_le32w(&val
, va_arg(ap
, uint32_t));
887 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
892 cpu_to_le64w(&val
, va_arg(ap
, uint64_t));
893 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
897 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
898 int *iovcnt
= va_arg(ap
, int *);
899 *iovcnt
= pdu_copy_sg(pdu
, offset
, 1, iov
);
903 V9fsString
*str
= va_arg(ap
, V9fsString
*);
904 offset
+= pdu_marshal(pdu
, offset
, "w", str
->size
);
905 offset
+= pdu_pack(pdu
, offset
, str
->data
, str
->size
);
909 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
910 offset
+= pdu_marshal(pdu
, offset
, "bdq",
911 qidp
->type
, qidp
->version
, qidp
->path
);
915 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
916 offset
+= pdu_marshal(pdu
, offset
, "wwdQdddqsssssddd",
917 statp
->size
, statp
->type
, statp
->dev
,
918 &statp
->qid
, statp
->mode
, statp
->atime
,
919 statp
->mtime
, statp
->length
, &statp
->name
,
920 &statp
->uid
, &statp
->gid
, &statp
->muid
,
921 &statp
->extension
, statp
->n_uid
,
922 statp
->n_gid
, statp
->n_muid
);
926 V9fsStatDotl
*statp
= va_arg(ap
, V9fsStatDotl
*);
927 offset
+= pdu_marshal(pdu
, offset
, "qQdddqqqqqqqqqqqqqqq",
928 statp
->st_result_mask
,
929 &statp
->qid
, statp
->st_mode
,
930 statp
->st_uid
, statp
->st_gid
,
931 statp
->st_nlink
, statp
->st_rdev
,
932 statp
->st_size
, statp
->st_blksize
, statp
->st_blocks
,
933 statp
->st_atime_sec
, statp
->st_atime_nsec
,
934 statp
->st_mtime_sec
, statp
->st_mtime_nsec
,
935 statp
->st_ctime_sec
, statp
->st_ctime_nsec
,
936 statp
->st_btime_sec
, statp
->st_btime_nsec
,
937 statp
->st_gen
, statp
->st_data_version
);
946 return offset
- old_offset
;
949 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
951 int8_t id
= pdu
->id
+ 1; /* Response */
957 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
960 str
.data
= strerror(err
);
961 str
.size
= strlen(str
.data
);
963 len
+= pdu_marshal(pdu
, len
, "s", &str
);
967 len
+= pdu_marshal(pdu
, len
, "d", err
);
969 if (s
->proto_version
== V9FS_PROTO_2000L
) {
972 trace_complete_pdu(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
975 /* fill out the header */
976 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
978 /* keep these in sync */
982 /* push onto queue and notify */
983 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
985 /* FIXME: we should batch these completions */
986 virtio_notify(&s
->vdev
, s
->vq
);
988 /* Now wakeup anybody waiting in flush for this request */
989 qemu_co_queue_next(&pdu
->complete
);
994 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
999 if (mode
& P9_STAT_MODE_DIR
) {
1003 if (mode
& P9_STAT_MODE_SYMLINK
) {
1006 if (mode
& P9_STAT_MODE_SOCKET
) {
1009 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
1012 if (mode
& P9_STAT_MODE_DEVICE
) {
1013 if (extension
&& extension
->data
[0] == 'c') {
1024 if (mode
& P9_STAT_MODE_SETUID
) {
1027 if (mode
& P9_STAT_MODE_SETGID
) {
1030 if (mode
& P9_STAT_MODE_SETVTX
) {
1037 static int donttouch_stat(V9fsStat
*stat
)
1039 if (stat
->type
== -1 &&
1041 stat
->qid
.type
== -1 &&
1042 stat
->qid
.version
== -1 &&
1043 stat
->qid
.path
== -1 &&
1045 stat
->atime
== -1 &&
1046 stat
->mtime
== -1 &&
1047 stat
->length
== -1 &&
1052 stat
->n_uid
== -1 &&
1053 stat
->n_gid
== -1 &&
1054 stat
->n_muid
== -1) {
1061 static void v9fs_stat_free(V9fsStat
*stat
)
1063 v9fs_string_free(&stat
->name
);
1064 v9fs_string_free(&stat
->uid
);
1065 v9fs_string_free(&stat
->gid
);
1066 v9fs_string_free(&stat
->muid
);
1067 v9fs_string_free(&stat
->extension
);
1070 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
1074 mode
= stbuf
->st_mode
& 0777;
1075 if (S_ISDIR(stbuf
->st_mode
)) {
1076 mode
|= P9_STAT_MODE_DIR
;
1079 if (S_ISLNK(stbuf
->st_mode
)) {
1080 mode
|= P9_STAT_MODE_SYMLINK
;
1083 if (S_ISSOCK(stbuf
->st_mode
)) {
1084 mode
|= P9_STAT_MODE_SOCKET
;
1087 if (S_ISFIFO(stbuf
->st_mode
)) {
1088 mode
|= P9_STAT_MODE_NAMED_PIPE
;
1091 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
1092 mode
|= P9_STAT_MODE_DEVICE
;
1095 if (stbuf
->st_mode
& S_ISUID
) {
1096 mode
|= P9_STAT_MODE_SETUID
;
1099 if (stbuf
->st_mode
& S_ISGID
) {
1100 mode
|= P9_STAT_MODE_SETGID
;
1103 if (stbuf
->st_mode
& S_ISVTX
) {
1104 mode
|= P9_STAT_MODE_SETVTX
;
1110 static int stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*name
,
1111 const struct stat
*stbuf
,
1117 memset(v9stat
, 0, sizeof(*v9stat
));
1119 stat_to_qid(stbuf
, &v9stat
->qid
);
1120 v9stat
->mode
= stat_to_v9mode(stbuf
);
1121 v9stat
->atime
= stbuf
->st_atime
;
1122 v9stat
->mtime
= stbuf
->st_mtime
;
1123 v9stat
->length
= stbuf
->st_size
;
1125 v9fs_string_null(&v9stat
->uid
);
1126 v9fs_string_null(&v9stat
->gid
);
1127 v9fs_string_null(&v9stat
->muid
);
1129 v9stat
->n_uid
= stbuf
->st_uid
;
1130 v9stat
->n_gid
= stbuf
->st_gid
;
1133 v9fs_string_null(&v9stat
->extension
);
1135 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
1136 err
= v9fs_co_readlink(pdu
, name
, &v9stat
->extension
);
1140 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
1141 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
1142 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
1143 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
1144 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
1145 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
1146 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
1149 str
= strrchr(name
->data
, '/');
1156 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
1159 v9fs_string_size(&v9stat
->name
) +
1160 v9fs_string_size(&v9stat
->uid
) +
1161 v9fs_string_size(&v9stat
->gid
) +
1162 v9fs_string_size(&v9stat
->muid
) +
1163 v9fs_string_size(&v9stat
->extension
);
1167 #define P9_STATS_MODE 0x00000001ULL
1168 #define P9_STATS_NLINK 0x00000002ULL
1169 #define P9_STATS_UID 0x00000004ULL
1170 #define P9_STATS_GID 0x00000008ULL
1171 #define P9_STATS_RDEV 0x00000010ULL
1172 #define P9_STATS_ATIME 0x00000020ULL
1173 #define P9_STATS_MTIME 0x00000040ULL
1174 #define P9_STATS_CTIME 0x00000080ULL
1175 #define P9_STATS_INO 0x00000100ULL
1176 #define P9_STATS_SIZE 0x00000200ULL
1177 #define P9_STATS_BLOCKS 0x00000400ULL
1179 #define P9_STATS_BTIME 0x00000800ULL
1180 #define P9_STATS_GEN 0x00001000ULL
1181 #define P9_STATS_DATA_VERSION 0x00002000ULL
1183 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1184 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1187 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
1188 V9fsStatDotl
*v9lstat
)
1190 memset(v9lstat
, 0, sizeof(*v9lstat
));
1192 v9lstat
->st_mode
= stbuf
->st_mode
;
1193 v9lstat
->st_nlink
= stbuf
->st_nlink
;
1194 v9lstat
->st_uid
= stbuf
->st_uid
;
1195 v9lstat
->st_gid
= stbuf
->st_gid
;
1196 v9lstat
->st_rdev
= stbuf
->st_rdev
;
1197 v9lstat
->st_size
= stbuf
->st_size
;
1198 v9lstat
->st_blksize
= stbuf
->st_blksize
;
1199 v9lstat
->st_blocks
= stbuf
->st_blocks
;
1200 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
1201 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
1202 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
1203 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
1204 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
1205 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
1206 /* Currently we only support BASIC fields in stat */
1207 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
1209 stat_to_qid(stbuf
, &v9lstat
->qid
);
1212 static struct iovec
*adjust_sg(struct iovec
*sg
, int len
, int *iovcnt
)
1214 while (len
&& *iovcnt
) {
1215 if (len
< sg
->iov_len
) {
1217 sg
->iov_base
+= len
;
1229 static struct iovec
*cap_sg(struct iovec
*sg
, int cap
, int *cnt
)
1234 for (i
= 0; i
< *cnt
; i
++) {
1235 if ((total
+ sg
[i
].iov_len
) > cap
) {
1236 sg
[i
].iov_len
-= ((total
+ sg
[i
].iov_len
) - cap
);
1240 total
+= sg
[i
].iov_len
;
1248 static void print_sg(struct iovec
*sg
, int cnt
)
1252 printf("sg[%d]: {", cnt
);
1253 for (i
= 0; i
< cnt
; i
++) {
1257 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
1262 /* Will call this only for path name based fid */
1263 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
1266 v9fs_path_init(&str
);
1267 v9fs_path_copy(&str
, dst
);
1268 v9fs_string_sprintf((V9fsString
*)dst
, "%s%s", src
->data
, str
.data
+len
);
1269 v9fs_path_free(&str
);
1270 /* +1 to include terminating NULL */
1274 static void v9fs_version(void *opaque
)
1276 V9fsPDU
*pdu
= opaque
;
1277 V9fsState
*s
= pdu
->s
;
1281 pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
1282 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
1284 if (!strcmp(version
.data
, "9P2000.u")) {
1285 s
->proto_version
= V9FS_PROTO_2000U
;
1286 } else if (!strcmp(version
.data
, "9P2000.L")) {
1287 s
->proto_version
= V9FS_PROTO_2000L
;
1289 v9fs_string_sprintf(&version
, "unknown");
1292 offset
+= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
1293 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
1295 complete_pdu(s
, pdu
, offset
);
1297 v9fs_string_free(&version
);
1301 static void v9fs_attach(void *opaque
)
1303 V9fsPDU
*pdu
= opaque
;
1304 V9fsState
*s
= pdu
->s
;
1305 int32_t fid
, afid
, n_uname
;
1306 V9fsString uname
, aname
;
1312 pdu_unmarshal(pdu
, offset
, "ddssd", &fid
, &afid
, &uname
, &aname
, &n_uname
);
1313 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
1315 fidp
= alloc_fid(s
, fid
);
1320 fidp
->uid
= n_uname
;
1321 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
1327 err
= fid_to_qid(pdu
, fidp
, &qid
);
1333 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
1338 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1339 qid
.type
, qid
.version
, qid
.path
);
1340 complete_pdu(s
, pdu
, err
);
1341 v9fs_string_free(&uname
);
1342 v9fs_string_free(&aname
);
1345 static void v9fs_stat(void *opaque
)
1353 V9fsPDU
*pdu
= opaque
;
1354 V9fsState
*s
= pdu
->s
;
1356 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1357 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1359 fidp
= get_fid(pdu
, fid
);
1364 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1368 err
= stat_to_v9stat(pdu
, &fidp
->path
, &stbuf
, &v9stat
);
1372 offset
+= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1374 v9fs_stat_free(&v9stat
);
1378 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1379 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1381 complete_pdu(s
, pdu
, err
);
1384 static void v9fs_getattr(void *opaque
)
1391 uint64_t request_mask
;
1392 V9fsStatDotl v9stat_dotl
;
1393 V9fsPDU
*pdu
= opaque
;
1394 V9fsState
*s
= pdu
->s
;
1396 pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1397 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1399 fidp
= get_fid(pdu
, fid
);
1405 * Currently we only support BASIC fields in stat, so there is no
1406 * need to look at request_mask.
1408 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1412 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1414 /* fill st_gen if requested and supported by underlying fs */
1415 if (request_mask
& P9_STATS_GEN
) {
1416 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1420 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1423 retval
+= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1427 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1428 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1429 v9stat_dotl
.st_gid
);
1431 complete_pdu(s
, pdu
, retval
);
1434 /* From Linux kernel code */
1435 #define ATTR_MODE (1 << 0)
1436 #define ATTR_UID (1 << 1)
1437 #define ATTR_GID (1 << 2)
1438 #define ATTR_SIZE (1 << 3)
1439 #define ATTR_ATIME (1 << 4)
1440 #define ATTR_MTIME (1 << 5)
1441 #define ATTR_CTIME (1 << 6)
1442 #define ATTR_MASK 127
1443 #define ATTR_ATIME_SET (1 << 7)
1444 #define ATTR_MTIME_SET (1 << 8)
1446 static void v9fs_setattr(void *opaque
)
1453 V9fsPDU
*pdu
= opaque
;
1454 V9fsState
*s
= pdu
->s
;
1456 pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1458 fidp
= get_fid(pdu
, fid
);
1463 if (v9iattr
.valid
& ATTR_MODE
) {
1464 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1469 if (v9iattr
.valid
& (ATTR_ATIME
| ATTR_MTIME
)) {
1470 struct timespec times
[2];
1471 if (v9iattr
.valid
& ATTR_ATIME
) {
1472 if (v9iattr
.valid
& ATTR_ATIME_SET
) {
1473 times
[0].tv_sec
= v9iattr
.atime_sec
;
1474 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1476 times
[0].tv_nsec
= UTIME_NOW
;
1479 times
[0].tv_nsec
= UTIME_OMIT
;
1481 if (v9iattr
.valid
& ATTR_MTIME
) {
1482 if (v9iattr
.valid
& ATTR_MTIME_SET
) {
1483 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1484 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1486 times
[1].tv_nsec
= UTIME_NOW
;
1489 times
[1].tv_nsec
= UTIME_OMIT
;
1491 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1497 * If the only valid entry in iattr is ctime we can call
1498 * chown(-1,-1) to update the ctime of the file
1500 if ((v9iattr
.valid
& (ATTR_UID
| ATTR_GID
)) ||
1501 ((v9iattr
.valid
& ATTR_CTIME
)
1502 && !((v9iattr
.valid
& ATTR_MASK
) & ~ATTR_CTIME
))) {
1503 if (!(v9iattr
.valid
& ATTR_UID
)) {
1506 if (!(v9iattr
.valid
& ATTR_GID
)) {
1509 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1515 if (v9iattr
.valid
& (ATTR_SIZE
)) {
1516 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1525 complete_pdu(s
, pdu
, err
);
1528 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1532 offset
+= pdu_marshal(pdu
, offset
, "w", nwnames
);
1533 for (i
= 0; i
< nwnames
; i
++) {
1534 offset
+= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1539 static void v9fs_walk(void *opaque
)
1542 V9fsQID
*qids
= NULL
;
1544 V9fsPath dpath
, path
;
1548 int32_t fid
, newfid
;
1549 V9fsString
*wnames
= NULL
;
1551 V9fsFidState
*newfidp
= NULL
;;
1552 V9fsPDU
*pdu
= opaque
;
1553 V9fsState
*s
= pdu
->s
;
1555 offset
+= pdu_unmarshal(pdu
, offset
, "ddw", &fid
,
1558 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1560 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1561 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1562 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1563 for (i
= 0; i
< nwnames
; i
++) {
1564 offset
+= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1566 } else if (nwnames
> P9_MAXWELEM
) {
1570 fidp
= get_fid(pdu
, fid
);
1575 v9fs_path_init(&dpath
);
1576 v9fs_path_init(&path
);
1578 * Both dpath and path initially poin to fidp.
1579 * Needed to handle request with nwnames == 0
1581 v9fs_path_copy(&dpath
, &fidp
->path
);
1582 v9fs_path_copy(&path
, &fidp
->path
);
1583 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1584 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
, &path
);
1588 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1592 stat_to_qid(&stbuf
, &qids
[name_idx
]);
1593 v9fs_path_copy(&dpath
, &path
);
1595 if (fid
== newfid
) {
1596 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1597 v9fs_path_copy(&fidp
->path
, &path
);
1599 newfidp
= alloc_fid(s
, newfid
);
1600 if (newfidp
== NULL
) {
1604 newfidp
->uid
= fidp
->uid
;
1605 v9fs_path_copy(&newfidp
->path
, &path
);
1607 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1611 put_fid(pdu
, newfidp
);
1613 v9fs_path_free(&dpath
);
1614 v9fs_path_free(&path
);
1616 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1617 complete_pdu(s
, pdu
, err
);
1618 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1619 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1620 v9fs_string_free(&wnames
[name_idx
]);
1628 static int32_t get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1630 struct statfs stbuf
;
1632 V9fsState
*s
= pdu
->s
;
1635 * iounit should be multiples of f_bsize (host filesystem block size
1636 * and as well as less than (client msize - P9_IOHDRSZ))
1638 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1639 iounit
= stbuf
.f_bsize
;
1640 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1643 iounit
= s
->msize
- P9_IOHDRSZ
;
1648 static void v9fs_open(void *opaque
)
1659 V9fsPDU
*pdu
= opaque
;
1660 V9fsState
*s
= pdu
->s
;
1662 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1663 pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1665 pdu_unmarshal(pdu
, offset
, "db", &fid
, &mode
);
1667 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1669 fidp
= get_fid(pdu
, fid
);
1674 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1676 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1680 stat_to_qid(&stbuf
, &qid
);
1681 if (S_ISDIR(stbuf
.st_mode
)) {
1682 err
= v9fs_co_opendir(pdu
, fidp
);
1686 fidp
->fid_type
= P9_FID_DIR
;
1687 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1690 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1691 flags
= get_dotl_openflags(s
, mode
);
1693 flags
= omode_to_uflags(mode
);
1695 err
= v9fs_co_open(pdu
, fidp
, flags
);
1699 fidp
->fid_type
= P9_FID_FILE
;
1700 fidp
->open_flags
= flags
;
1701 if (flags
& O_EXCL
) {
1703 * We let the host file system do O_EXCL check
1704 * We should not reclaim such fd
1706 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1708 iounit
= get_iounit(pdu
, &fidp
->path
);
1709 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1715 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1716 qid
.type
, qid
.version
, qid
.path
, iounit
);
1717 complete_pdu(s
, pdu
, err
);
1720 static void v9fs_lcreate(void *opaque
)
1722 int32_t dfid
, flags
, mode
;
1731 V9fsPDU
*pdu
= opaque
;
1733 pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
, &name
, &flags
,
1735 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1737 fidp
= get_fid(pdu
, dfid
);
1743 flags
= get_dotl_openflags(pdu
->s
, flags
);
1744 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1745 flags
| O_CREAT
, mode
, &stbuf
);
1749 fidp
->fid_type
= P9_FID_FILE
;
1750 fidp
->open_flags
= flags
;
1751 if (flags
& O_EXCL
) {
1753 * We let the host file system do O_EXCL check
1754 * We should not reclaim such fd
1756 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1758 iounit
= get_iounit(pdu
, &fidp
->path
);
1759 stat_to_qid(&stbuf
, &qid
);
1760 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1765 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1766 qid
.type
, qid
.version
, qid
.path
, iounit
);
1767 complete_pdu(pdu
->s
, pdu
, err
);
1768 v9fs_string_free(&name
);
1771 static void v9fs_fsync(void *opaque
)
1778 V9fsPDU
*pdu
= opaque
;
1779 V9fsState
*s
= pdu
->s
;
1781 pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1782 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1784 fidp
= get_fid(pdu
, fid
);
1789 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1795 complete_pdu(s
, pdu
, err
);
1798 static void v9fs_clunk(void *opaque
)
1804 V9fsPDU
*pdu
= opaque
;
1805 V9fsState
*s
= pdu
->s
;
1807 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1808 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1810 fidp
= clunk_fid(s
, fid
);
1816 * Bump the ref so that put_fid will
1824 complete_pdu(s
, pdu
, err
);
1827 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
,
1828 V9fsFidState
*fidp
, int64_t off
, int32_t max_count
)
1834 xattr_len
= fidp
->fs
.xattr
.len
;
1835 read_count
= xattr_len
- off
;
1836 if (read_count
> max_count
) {
1837 read_count
= max_count
;
1838 } else if (read_count
< 0) {
1840 * read beyond XATTR value
1844 offset
+= pdu_marshal(pdu
, offset
, "d", read_count
);
1845 offset
+= pdu_pack(pdu
, offset
,
1846 ((char *)fidp
->fs
.xattr
.value
) + off
,
1851 static int v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1852 V9fsFidState
*fidp
, int32_t max_count
)
1859 off_t saved_dir_pos
;
1860 struct dirent
*dent
, *result
;
1862 /* save the directory position */
1863 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1864 if (saved_dir_pos
< 0) {
1865 return saved_dir_pos
;
1868 dent
= g_malloc(sizeof(struct dirent
));
1871 v9fs_path_init(&path
);
1872 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
1873 if (err
|| !result
) {
1876 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1880 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1884 err
= stat_to_v9stat(pdu
, &path
, &stbuf
, &v9stat
);
1888 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1889 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1890 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1891 /* Ran out of buffer. Set dir back to old position and return */
1892 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1893 v9fs_stat_free(&v9stat
);
1894 v9fs_path_free(&path
);
1899 v9fs_stat_free(&v9stat
);
1900 v9fs_path_free(&path
);
1901 saved_dir_pos
= dent
->d_off
;
1905 v9fs_path_free(&path
);
1912 static void v9fs_read(void *opaque
)
1921 V9fsPDU
*pdu
= opaque
;
1922 V9fsState
*s
= pdu
->s
;
1924 pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1925 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1927 fidp
= get_fid(pdu
, fid
);
1932 if (fidp
->fid_type
== P9_FID_DIR
) {
1935 v9fs_co_rewinddir(pdu
, fidp
);
1937 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1943 err
+= pdu_marshal(pdu
, offset
, "d", count
);
1945 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1949 struct iovec iov
[128]; /* FIXME: bad, bad, bad */
1952 pdu_marshal(pdu
, offset
+ 4, "v", sg
, &cnt
);
1953 sg
= cap_sg(sg
, max_count
, &cnt
);
1958 /* Loop in case of EINTR */
1960 len
= v9fs_co_preadv(pdu
, fidp
, sg
, cnt
, off
);
1965 } while (len
== -EINTR
&& !pdu
->cancelled
);
1967 /* IO error return the error */
1971 sg
= adjust_sg(sg
, len
, &cnt
);
1972 } while (count
< max_count
&& len
> 0);
1974 err
+= pdu_marshal(pdu
, offset
, "d", count
);
1976 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1977 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1984 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1985 complete_pdu(s
, pdu
, err
);
1988 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1991 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1992 * size of type (1) + size of name.size (2) + strlen(name.data)
1994 return 24 + v9fs_string_size(name
);
1997 static int v9fs_do_readdir(V9fsPDU
*pdu
,
1998 V9fsFidState
*fidp
, int32_t max_count
)
2005 off_t saved_dir_pos
;
2006 struct dirent
*dent
, *result
;
2008 /* save the directory position */
2009 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
2010 if (saved_dir_pos
< 0) {
2011 return saved_dir_pos
;
2014 dent
= g_malloc(sizeof(struct dirent
));
2017 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
2018 if (err
|| !result
) {
2021 v9fs_string_init(&name
);
2022 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
2023 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
2024 /* Ran out of buffer. Set dir back to old position and return */
2025 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
2026 v9fs_string_free(&name
);
2031 * Fill up just the path field of qid because the client uses
2032 * only that. To fill the entire qid structure we will have
2033 * to stat each dirent found, which is expensive
2035 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
2036 memcpy(&qid
.path
, &dent
->d_ino
, size
);
2037 /* Fill the other fields with dummy values */
2041 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2042 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
2044 dent
->d_type
, &name
);
2046 v9fs_string_free(&name
);
2047 saved_dir_pos
= dent
->d_off
;
2056 static void v9fs_readdir(void *opaque
)
2062 int64_t initial_offset
;
2063 int32_t count
, max_count
;
2064 V9fsPDU
*pdu
= opaque
;
2065 V9fsState
*s
= pdu
->s
;
2067 pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &initial_offset
, &max_count
);
2069 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
2071 fidp
= get_fid(pdu
, fid
);
2076 if (!fidp
->fs
.dir
) {
2080 if (initial_offset
== 0) {
2081 v9fs_co_rewinddir(pdu
, fidp
);
2083 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
2085 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
2091 retval
+= pdu_marshal(pdu
, offset
, "d", count
);
2096 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
2097 complete_pdu(s
, pdu
, retval
);
2100 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2101 int64_t off
, int32_t count
,
2102 struct iovec
*sg
, int cnt
)
2111 xattr_len
= fidp
->fs
.xattr
.len
;
2112 write_count
= xattr_len
- off
;
2113 if (write_count
> count
) {
2114 write_count
= count
;
2115 } else if (write_count
< 0) {
2117 * write beyond XATTR value len specified in
2123 offset
+= pdu_marshal(pdu
, offset
, "d", write_count
);
2125 fidp
->fs
.xattr
.copied_len
+= write_count
;
2127 * Now copy the content from sg list
2129 for (i
= 0; i
< cnt
; i
++) {
2130 if (write_count
> sg
[i
].iov_len
) {
2131 to_copy
= sg
[i
].iov_len
;
2133 to_copy
= write_count
;
2135 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
2136 /* updating vs->off since we are not using below */
2138 write_count
-= to_copy
;
2144 static void v9fs_write(void *opaque
)
2155 struct iovec iov
[128]; /* FIXME: bad, bad, bad */
2156 struct iovec
*sg
= iov
;
2157 V9fsPDU
*pdu
= opaque
;
2158 V9fsState
*s
= pdu
->s
;
2160 pdu_unmarshal(pdu
, offset
, "dqdv", &fid
, &off
, &count
, sg
, &cnt
);
2161 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, cnt
);
2163 fidp
= get_fid(pdu
, fid
);
2168 if (fidp
->fid_type
== P9_FID_FILE
) {
2169 if (fidp
->fs
.fd
== -1) {
2173 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2175 * setxattr operation
2177 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
, sg
, cnt
);
2183 sg
= cap_sg(sg
, count
, &cnt
);
2188 /* Loop in case of EINTR */
2190 len
= v9fs_co_pwritev(pdu
, fidp
, sg
, cnt
, off
);
2195 } while (len
== -EINTR
&& !pdu
->cancelled
);
2197 /* IO error return the error */
2201 sg
= adjust_sg(sg
, len
, &cnt
);
2202 } while (total
< count
&& len
> 0);
2203 offset
+= pdu_marshal(pdu
, offset
, "d", total
);
2208 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2209 complete_pdu(s
, pdu
, err
);
2212 static void v9fs_create(void *opaque
)
2224 V9fsString extension
;
2226 V9fsPDU
*pdu
= opaque
;
2228 v9fs_path_init(&path
);
2230 pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2231 &perm
, &mode
, &extension
);
2233 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2235 fidp
= get_fid(pdu
, fid
);
2240 if (perm
& P9_STAT_MODE_DIR
) {
2241 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2242 fidp
->uid
, -1, &stbuf
);
2246 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2250 v9fs_path_copy(&fidp
->path
, &path
);
2251 err
= v9fs_co_opendir(pdu
, fidp
);
2255 fidp
->fid_type
= P9_FID_DIR
;
2256 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2257 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2258 extension
.data
, -1 , &stbuf
);
2262 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2266 v9fs_path_copy(&fidp
->path
, &path
);
2267 } else if (perm
& P9_STAT_MODE_LINK
) {
2268 int32_t ofid
= atoi(extension
.data
);
2269 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2270 if (ofidp
== NULL
) {
2274 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2275 put_fid(pdu
, ofidp
);
2279 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2281 fidp
->fid_type
= P9_FID_NONE
;
2284 v9fs_path_copy(&fidp
->path
, &path
);
2285 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2287 fidp
->fid_type
= P9_FID_NONE
;
2290 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2292 uint32_t major
, minor
;
2295 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2312 nmode
|= perm
& 0777;
2313 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2314 makedev(major
, minor
), nmode
, &stbuf
);
2318 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2322 v9fs_path_copy(&fidp
->path
, &path
);
2323 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2324 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2325 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2329 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2333 v9fs_path_copy(&fidp
->path
, &path
);
2334 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2335 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2336 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2340 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2344 v9fs_path_copy(&fidp
->path
, &path
);
2346 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2347 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2351 fidp
->fid_type
= P9_FID_FILE
;
2352 fidp
->open_flags
= omode_to_uflags(mode
);
2353 if (fidp
->open_flags
& O_EXCL
) {
2355 * We let the host file system do O_EXCL check
2356 * We should not reclaim such fd
2358 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2361 iounit
= get_iounit(pdu
, &fidp
->path
);
2362 stat_to_qid(&stbuf
, &qid
);
2363 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2368 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2369 qid
.type
, qid
.version
, qid
.path
, iounit
);
2370 complete_pdu(pdu
->s
, pdu
, err
);
2371 v9fs_string_free(&name
);
2372 v9fs_string_free(&extension
);
2373 v9fs_path_free(&path
);
2376 static void v9fs_symlink(void *opaque
)
2378 V9fsPDU
*pdu
= opaque
;
2381 V9fsFidState
*dfidp
;
2389 pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2390 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2392 dfidp
= get_fid(pdu
, dfid
);
2393 if (dfidp
== NULL
) {
2397 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2401 stat_to_qid(&stbuf
, &qid
);
2402 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
2405 put_fid(pdu
, dfidp
);
2407 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2408 qid
.type
, qid
.version
, qid
.path
);
2409 complete_pdu(pdu
->s
, pdu
, err
);
2410 v9fs_string_free(&name
);
2411 v9fs_string_free(&symname
);
2414 static void v9fs_flush(void *opaque
)
2418 V9fsPDU
*cancel_pdu
;
2419 V9fsPDU
*pdu
= opaque
;
2420 V9fsState
*s
= pdu
->s
;
2422 pdu_unmarshal(pdu
, offset
, "w", &tag
);
2423 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2425 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2426 if (cancel_pdu
->tag
== tag
) {
2431 cancel_pdu
->cancelled
= 1;
2433 * Wait for pdu to complete.
2435 qemu_co_queue_wait(&cancel_pdu
->complete
);
2436 cancel_pdu
->cancelled
= 0;
2437 free_pdu(pdu
->s
, cancel_pdu
);
2439 complete_pdu(s
, pdu
, 7);
2443 static void v9fs_link(void *opaque
)
2445 V9fsPDU
*pdu
= opaque
;
2446 V9fsState
*s
= pdu
->s
;
2447 int32_t dfid
, oldfid
;
2448 V9fsFidState
*dfidp
, *oldfidp
;
2453 pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2454 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2456 dfidp
= get_fid(pdu
, dfid
);
2457 if (dfidp
== NULL
) {
2462 oldfidp
= get_fid(pdu
, oldfid
);
2463 if (oldfidp
== NULL
) {
2467 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2472 put_fid(pdu
, dfidp
);
2474 v9fs_string_free(&name
);
2475 complete_pdu(s
, pdu
, err
);
2478 /* Only works with path name based fid */
2479 static void v9fs_remove(void *opaque
)
2485 V9fsPDU
*pdu
= opaque
;
2487 pdu_unmarshal(pdu
, offset
, "d", &fid
);
2488 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2490 fidp
= get_fid(pdu
, fid
);
2495 /* if fs driver is not path based, return EOPNOTSUPP */
2496 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2501 * IF the file is unlinked, we cannot reopen
2502 * the file later. So don't reclaim fd
2504 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2508 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2513 /* For TREMOVE we need to clunk the fid even on failed remove */
2514 clunk_fid(pdu
->s
, fidp
->fid
);
2517 complete_pdu(pdu
->s
, pdu
, err
);
2520 static void v9fs_unlinkat(void *opaque
)
2524 int32_t dfid
, flags
;
2527 V9fsFidState
*dfidp
;
2528 V9fsPDU
*pdu
= opaque
;
2530 pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2531 flags
= dotl_to_at_flags(flags
);
2533 dfidp
= get_fid(pdu
, dfid
);
2534 if (dfidp
== NULL
) {
2539 * IF the file is unlinked, we cannot reopen
2540 * the file later. So don't reclaim fd
2542 v9fs_path_init(&path
);
2543 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2547 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2551 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2556 put_fid(pdu
, dfidp
);
2557 v9fs_path_free(&path
);
2559 complete_pdu(pdu
->s
, pdu
, err
);
2560 v9fs_string_free(&name
);
2564 /* Only works with path name based fid */
2565 static int v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2566 int32_t newdirfid
, V9fsString
*name
)
2571 V9fsFidState
*tfidp
;
2572 V9fsState
*s
= pdu
->s
;
2573 V9fsFidState
*dirfidp
= NULL
;
2574 char *old_name
, *new_name
;
2576 v9fs_path_init(&new_path
);
2577 if (newdirfid
!= -1) {
2578 dirfidp
= get_fid(pdu
, newdirfid
);
2579 if (dirfidp
== NULL
) {
2583 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2584 v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2586 old_name
= fidp
->path
.data
;
2587 end
= strrchr(old_name
, '/');
2593 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2594 strncat(new_name
, old_name
, end
- old_name
);
2595 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2596 v9fs_co_name_to_path(pdu
, NULL
, new_name
, &new_path
);
2599 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2604 * Fixup fid's pointing to the old name to
2605 * start pointing to the new name
2607 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2608 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2609 /* replace the name */
2610 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2615 put_fid(pdu
, dirfidp
);
2617 v9fs_path_free(&new_path
);
2622 /* Only works with path name based fid */
2623 static void v9fs_rename(void *opaque
)
2631 V9fsPDU
*pdu
= opaque
;
2632 V9fsState
*s
= pdu
->s
;
2634 pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2636 fidp
= get_fid(pdu
, fid
);
2641 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
2642 /* if fs driver is not path based, return EOPNOTSUPP */
2643 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2647 v9fs_path_write_lock(s
);
2648 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2649 v9fs_path_unlock(s
);
2656 complete_pdu(s
, pdu
, err
);
2657 v9fs_string_free(&name
);
2660 static void v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2661 V9fsString
*old_name
, V9fsPath
*newdir
,
2662 V9fsString
*new_name
)
2664 V9fsFidState
*tfidp
;
2665 V9fsPath oldpath
, newpath
;
2666 V9fsState
*s
= pdu
->s
;
2669 v9fs_path_init(&oldpath
);
2670 v9fs_path_init(&newpath
);
2671 v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2672 v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2675 * Fixup fid's pointing to the old name to
2676 * start pointing to the new name
2678 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2679 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2680 /* replace the name */
2681 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2684 v9fs_path_free(&oldpath
);
2685 v9fs_path_free(&newpath
);
2688 static int v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2689 V9fsString
*old_name
, int32_t newdirfid
,
2690 V9fsString
*new_name
)
2693 V9fsState
*s
= pdu
->s
;
2694 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2696 olddirfidp
= get_fid(pdu
, olddirfid
);
2697 if (olddirfidp
== NULL
) {
2701 if (newdirfid
!= -1) {
2702 newdirfidp
= get_fid(pdu
, newdirfid
);
2703 if (newdirfidp
== NULL
) {
2708 newdirfidp
= get_fid(pdu
, olddirfid
);
2711 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2712 &newdirfidp
->path
, new_name
);
2716 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2717 /* Only for path based fid we need to do the below fixup */
2718 v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2719 &newdirfidp
->path
, new_name
);
2723 put_fid(pdu
, olddirfidp
);
2726 put_fid(pdu
, newdirfidp
);
2731 static void v9fs_renameat(void *opaque
)
2735 V9fsPDU
*pdu
= opaque
;
2736 V9fsState
*s
= pdu
->s
;
2737 int32_t olddirfid
, newdirfid
;
2738 V9fsString old_name
, new_name
;
2740 pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2741 &old_name
, &newdirfid
, &new_name
);
2743 v9fs_path_write_lock(s
);
2744 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2745 &old_name
, newdirfid
, &new_name
);
2746 v9fs_path_unlock(s
);
2750 complete_pdu(s
, pdu
, err
);
2751 v9fs_string_free(&old_name
);
2752 v9fs_string_free(&new_name
);
2755 static void v9fs_wstat(void *opaque
)
2764 V9fsPDU
*pdu
= opaque
;
2765 V9fsState
*s
= pdu
->s
;
2767 pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2768 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2769 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2771 fidp
= get_fid(pdu
, fid
);
2776 /* do we need to sync the file? */
2777 if (donttouch_stat(&v9stat
)) {
2778 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2781 if (v9stat
.mode
!= -1) {
2783 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2787 v9_mode
= stat_to_v9mode(&stbuf
);
2788 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2789 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2790 /* Attempting to change the type */
2794 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2795 v9mode_to_mode(v9stat
.mode
,
2796 &v9stat
.extension
));
2801 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2802 struct timespec times
[2];
2803 if (v9stat
.atime
!= -1) {
2804 times
[0].tv_sec
= v9stat
.atime
;
2805 times
[0].tv_nsec
= 0;
2807 times
[0].tv_nsec
= UTIME_OMIT
;
2809 if (v9stat
.mtime
!= -1) {
2810 times
[1].tv_sec
= v9stat
.mtime
;
2811 times
[1].tv_nsec
= 0;
2813 times
[1].tv_nsec
= UTIME_OMIT
;
2815 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2820 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2821 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2826 if (v9stat
.name
.size
!= 0) {
2827 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2832 if (v9stat
.length
!= -1) {
2833 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2842 v9fs_stat_free(&v9stat
);
2843 complete_pdu(s
, pdu
, err
);
2846 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2858 int32_t bsize_factor
;
2861 * compute bsize factor based on host file system block size
2864 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2865 if (!bsize_factor
) {
2868 f_type
= stbuf
->f_type
;
2869 f_bsize
= stbuf
->f_bsize
;
2870 f_bsize
*= bsize_factor
;
2872 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2873 * adjust(divide) the number of blocks, free blocks and available
2874 * blocks by bsize factor
2876 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2877 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2878 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2879 f_files
= stbuf
->f_files
;
2880 f_ffree
= stbuf
->f_ffree
;
2881 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2882 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2883 f_namelen
= stbuf
->f_namelen
;
2885 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2886 f_type
, f_bsize
, f_blocks
, f_bfree
,
2887 f_bavail
, f_files
, f_ffree
,
2888 fsid_val
, f_namelen
);
2891 static void v9fs_statfs(void *opaque
)
2897 struct statfs stbuf
;
2898 V9fsPDU
*pdu
= opaque
;
2899 V9fsState
*s
= pdu
->s
;
2901 pdu_unmarshal(pdu
, offset
, "d", &fid
);
2902 fidp
= get_fid(pdu
, fid
);
2907 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2912 retval
+= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2916 complete_pdu(s
, pdu
, retval
);
2920 static void v9fs_mknod(void *opaque
)
2933 V9fsPDU
*pdu
= opaque
;
2934 V9fsState
*s
= pdu
->s
;
2936 pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2937 &major
, &minor
, &gid
);
2938 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
2940 fidp
= get_fid(pdu
, fid
);
2945 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
2946 makedev(major
, minor
), mode
, &stbuf
);
2950 stat_to_qid(&stbuf
, &qid
);
2952 err
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
2956 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
, qid
.type
, qid
.version
, qid
.path
);
2957 complete_pdu(s
, pdu
, err
);
2958 v9fs_string_free(&name
);
2962 * Implement posix byte range locking code
2963 * Server side handling of locking code is very simple, because 9p server in
2964 * QEMU can handle only one client. And most of the lock handling
2965 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2966 * do any thing in * qemu 9p server side lock code path.
2967 * So when a TLOCK request comes, always return success
2969 static void v9fs_lock(void *opaque
)
2976 int32_t fid
, err
= 0;
2977 V9fsPDU
*pdu
= opaque
;
2978 V9fsState
*s
= pdu
->s
;
2980 flock
= g_malloc(sizeof(*flock
));
2981 pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
->type
,
2982 &flock
->flags
, &flock
->start
, &flock
->length
,
2983 &flock
->proc_id
, &flock
->client_id
);
2985 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
2986 flock
->type
, flock
->start
, flock
->length
);
2988 status
= P9_LOCK_ERROR
;
2990 /* We support only block flag now (that too ignored currently) */
2991 if (flock
->flags
& ~P9_LOCK_FLAGS_BLOCK
) {
2995 fidp
= get_fid(pdu
, fid
);
3000 err
= v9fs_co_fstat(pdu
, fidp
->fs
.fd
, &stbuf
);
3004 status
= P9_LOCK_SUCCESS
;
3009 err
+= pdu_marshal(pdu
, offset
, "b", status
);
3010 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, status
);
3011 complete_pdu(s
, pdu
, err
);
3012 v9fs_string_free(&flock
->client_id
);
3017 * When a TGETLOCK request comes, always return success because all lock
3018 * handling is done by client's VFS layer.
3020 static void v9fs_getlock(void *opaque
)
3026 int32_t fid
, err
= 0;
3027 V9fsPDU
*pdu
= opaque
;
3028 V9fsState
*s
= pdu
->s
;
3030 glock
= g_malloc(sizeof(*glock
));
3031 pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
->type
,
3032 &glock
->start
, &glock
->length
, &glock
->proc_id
,
3035 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
3036 glock
->type
, glock
->start
, glock
->length
);
3038 fidp
= get_fid(pdu
, fid
);
3043 err
= v9fs_co_fstat(pdu
, fidp
->fs
.fd
, &stbuf
);
3047 glock
->type
= P9_LOCK_TYPE_UNLCK
;
3048 offset
+= pdu_marshal(pdu
, offset
, "bqqds", glock
->type
,
3049 glock
->start
, glock
->length
, glock
->proc_id
,
3055 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
->type
, glock
->start
,
3056 glock
->length
, glock
->proc_id
);
3058 complete_pdu(s
, pdu
, err
);
3059 v9fs_string_free(&glock
->client_id
);
3063 static void v9fs_mkdir(void *opaque
)
3065 V9fsPDU
*pdu
= opaque
;
3076 pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
3078 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
3080 fidp
= get_fid(pdu
, fid
);
3085 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
3089 stat_to_qid(&stbuf
, &qid
);
3090 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
3095 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
3096 qid
.type
, qid
.version
, qid
.path
, err
);
3097 complete_pdu(pdu
->s
, pdu
, err
);
3098 v9fs_string_free(&name
);
3101 static void v9fs_xattrwalk(void *opaque
)
3107 int32_t fid
, newfid
;
3108 V9fsFidState
*file_fidp
;
3109 V9fsFidState
*xattr_fidp
= NULL
;
3110 V9fsPDU
*pdu
= opaque
;
3111 V9fsState
*s
= pdu
->s
;
3113 pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3114 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3116 file_fidp
= get_fid(pdu
, fid
);
3117 if (file_fidp
== NULL
) {
3121 xattr_fidp
= alloc_fid(s
, newfid
);
3122 if (xattr_fidp
== NULL
) {
3126 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3127 if (name
.data
[0] == 0) {
3129 * listxattr request. Get the size first
3131 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3134 clunk_fid(s
, xattr_fidp
->fid
);
3138 * Read the xattr value
3140 xattr_fidp
->fs
.xattr
.len
= size
;
3141 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3142 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3144 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3145 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3146 xattr_fidp
->fs
.xattr
.value
,
3147 xattr_fidp
->fs
.xattr
.len
);
3149 clunk_fid(s
, xattr_fidp
->fid
);
3153 offset
+= pdu_marshal(pdu
, offset
, "q", size
);
3157 * specific xattr fid. We check for xattr
3158 * presence also collect the xattr size
3160 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3164 clunk_fid(s
, xattr_fidp
->fid
);
3168 * Read the xattr value
3170 xattr_fidp
->fs
.xattr
.len
= size
;
3171 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3172 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3174 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3175 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3176 &name
, xattr_fidp
->fs
.xattr
.value
,
3177 xattr_fidp
->fs
.xattr
.len
);
3179 clunk_fid(s
, xattr_fidp
->fid
);
3183 offset
+= pdu_marshal(pdu
, offset
, "q", size
);
3187 put_fid(pdu
, file_fidp
);
3189 put_fid(pdu
, xattr_fidp
);
3192 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3193 complete_pdu(s
, pdu
, err
);
3194 v9fs_string_free(&name
);
3197 static void v9fs_xattrcreate(void *opaque
)
3205 V9fsFidState
*file_fidp
;
3206 V9fsFidState
*xattr_fidp
;
3207 V9fsPDU
*pdu
= opaque
;
3208 V9fsState
*s
= pdu
->s
;
3210 pdu_unmarshal(pdu
, offset
, "dsqd",
3211 &fid
, &name
, &size
, &flags
);
3212 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3214 file_fidp
= get_fid(pdu
, fid
);
3215 if (file_fidp
== NULL
) {
3219 /* Make the file fid point to xattr */
3220 xattr_fidp
= file_fidp
;
3221 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3222 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3223 xattr_fidp
->fs
.xattr
.len
= size
;
3224 xattr_fidp
->fs
.xattr
.flags
= flags
;
3225 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3226 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3228 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3230 xattr_fidp
->fs
.xattr
.value
= NULL
;
3233 put_fid(pdu
, file_fidp
);
3235 complete_pdu(s
, pdu
, err
);
3236 v9fs_string_free(&name
);
3239 static void v9fs_readlink(void *opaque
)
3241 V9fsPDU
*pdu
= opaque
;
3248 pdu_unmarshal(pdu
, offset
, "d", &fid
);
3249 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3250 fidp
= get_fid(pdu
, fid
);
3256 v9fs_string_init(&target
);
3257 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3261 offset
+= pdu_marshal(pdu
, offset
, "s", &target
);
3263 v9fs_string_free(&target
);
3267 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3268 complete_pdu(pdu
->s
, pdu
, err
);
3271 static CoroutineEntry
*pdu_co_handlers
[] = {
3272 [P9_TREADDIR
] = v9fs_readdir
,
3273 [P9_TSTATFS
] = v9fs_statfs
,
3274 [P9_TGETATTR
] = v9fs_getattr
,
3275 [P9_TSETATTR
] = v9fs_setattr
,
3276 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3277 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3278 [P9_TMKNOD
] = v9fs_mknod
,
3279 [P9_TRENAME
] = v9fs_rename
,
3280 [P9_TLOCK
] = v9fs_lock
,
3281 [P9_TGETLOCK
] = v9fs_getlock
,
3282 [P9_TRENAMEAT
] = v9fs_renameat
,
3283 [P9_TREADLINK
] = v9fs_readlink
,
3284 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3285 [P9_TMKDIR
] = v9fs_mkdir
,
3286 [P9_TVERSION
] = v9fs_version
,
3287 [P9_TLOPEN
] = v9fs_open
,
3288 [P9_TATTACH
] = v9fs_attach
,
3289 [P9_TSTAT
] = v9fs_stat
,
3290 [P9_TWALK
] = v9fs_walk
,
3291 [P9_TCLUNK
] = v9fs_clunk
,
3292 [P9_TFSYNC
] = v9fs_fsync
,
3293 [P9_TOPEN
] = v9fs_open
,
3294 [P9_TREAD
] = v9fs_read
,
3296 [P9_TAUTH
] = v9fs_auth
,
3298 [P9_TFLUSH
] = v9fs_flush
,
3299 [P9_TLINK
] = v9fs_link
,
3300 [P9_TSYMLINK
] = v9fs_symlink
,
3301 [P9_TCREATE
] = v9fs_create
,
3302 [P9_TLCREATE
] = v9fs_lcreate
,
3303 [P9_TWRITE
] = v9fs_write
,
3304 [P9_TWSTAT
] = v9fs_wstat
,
3305 [P9_TREMOVE
] = v9fs_remove
,
3308 static void v9fs_op_not_supp(void *opaque
)
3310 V9fsPDU
*pdu
= opaque
;
3311 complete_pdu(pdu
->s
, pdu
, -EOPNOTSUPP
);
3314 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
3317 CoroutineEntry
*handler
;
3319 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3320 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3321 handler
= v9fs_op_not_supp
;
3323 handler
= pdu_co_handlers
[pdu
->id
];
3325 co
= qemu_coroutine_create(handler
);
3326 qemu_coroutine_enter(co
, pdu
);
3329 void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
3331 V9fsState
*s
= (V9fsState
*)vdev
;
3335 while ((pdu
= alloc_pdu(s
)) &&
3336 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
3339 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
3340 BUG_ON(pdu
->elem
.out_sg
[0].iov_len
< 7);
3342 ptr
= pdu
->elem
.out_sg
[0].iov_base
;
3344 memcpy(&pdu
->size
, ptr
, 4);
3346 memcpy(&pdu
->tag
, ptr
+ 5, 2);
3347 qemu_co_queue_init(&pdu
->complete
);
3353 void virtio_9p_set_fd_limit(void)
3356 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3357 fprintf(stderr
, "Failed to get the resource limit\n");
3360 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3361 open_fd_rc
= rlim
.rlim_cur
/2;