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"
22 #include "virtio-9p-coth.h"
27 static int open_fd_rc
;
41 static int omode_to_uflags(int8_t mode
)
75 void cred_init(FsCred
*credp
)
83 void v9fs_string_init(V9fsString
*str
)
89 void v9fs_string_free(V9fsString
*str
)
96 void v9fs_string_null(V9fsString
*str
)
98 v9fs_string_free(str
);
101 static int number_to_string(void *arg
, char type
)
103 unsigned int ret
= 0;
107 unsigned int num
= *(unsigned int *)arg
;
116 unsigned long num
= *(unsigned long *)arg
;
124 printf("Number_to_string: Unknown number format\n");
131 static int GCC_FMT_ATTR(2, 0)
132 v9fs_string_alloc_printf(char **strp
, const char *fmt
, va_list ap
)
135 char *iter
= (char *)fmt
;
139 unsigned int arg_uint
;
140 unsigned long arg_ulong
;
142 /* Find the number of %'s that denotes an argument */
143 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
148 len
= strlen(fmt
) - 2*nr_args
;
158 /* Now parse the format string */
159 for (iter
= strstr(iter
, "%"); iter
; iter
= strstr(iter
, "%")) {
163 arg_uint
= va_arg(ap2
, unsigned int);
164 len
+= number_to_string((void *)&arg_uint
, 'u');
167 if (*++iter
== 'u') {
168 arg_ulong
= va_arg(ap2
, unsigned long);
169 len
+= number_to_string((void *)&arg_ulong
, 'U');
175 arg_char_ptr
= va_arg(ap2
, char *);
176 len
+= strlen(arg_char_ptr
);
183 "v9fs_string_alloc_printf:Incorrect format %c", *iter
);
190 *strp
= g_malloc((len
+ 1) * sizeof(**strp
));
192 return vsprintf(*strp
, fmt
, ap
);
195 void GCC_FMT_ATTR(2, 3)
196 v9fs_string_sprintf(V9fsString
*str
, const char *fmt
, ...)
201 v9fs_string_free(str
);
204 err
= v9fs_string_alloc_printf(&str
->data
, fmt
, ap
);
211 void v9fs_string_copy(V9fsString
*lhs
, V9fsString
*rhs
)
213 v9fs_string_free(lhs
);
214 v9fs_string_sprintf(lhs
, "%s", rhs
->data
);
218 * Return TRUE if s1 is an ancestor of s2.
220 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
221 * As a special case, We treat s1 as ancestor of s2 if they are same!
223 static int v9fs_path_is_ancestor(V9fsString
*s1
, V9fsString
*s2
)
225 if (!strncmp(s1
->data
, s2
->data
, s1
->size
)) {
226 if (s2
->data
[s1
->size
] == '\0' || s2
->data
[s1
->size
] == '/') {
233 static size_t v9fs_string_size(V9fsString
*str
)
239 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
240 static int v9fs_reopen_fid(V9fsState
*s
, V9fsFidState
*f
)
243 if (f
->fid_type
== P9_FID_FILE
) {
244 if (f
->fs
.fd
== -1) {
246 err
= v9fs_co_open(s
, f
, f
->open_flags
);
247 } while (err
== -EINTR
);
249 } else if (f
->fid_type
== P9_FID_DIR
) {
250 if (f
->fs
.dir
== NULL
) {
252 err
= v9fs_co_opendir(s
, f
);
253 } while (err
== -EINTR
);
259 static V9fsFidState
*get_fid(V9fsState
*s
, int32_t fid
)
264 for (f
= s
->fid_list
; f
; f
= f
->next
) {
268 * Update the fid ref upfront so that
269 * we don't get reclaimed when we yield
274 * check whether we need to reopen the
275 * file. We might have closed the fd
276 * while trying to free up some file
279 err
= v9fs_reopen_fid(s
, f
);
285 * Mark the fid as referenced so that the LRU
286 * reclaim won't close the file descriptor
288 f
->flags
|= FID_REFERENCED
;
295 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
299 for (f
= s
->fid_list
; f
; f
= f
->next
) {
300 /* If fid is already there return NULL */
306 f
= g_malloc0(sizeof(V9fsFidState
));
308 f
->fid_type
= P9_FID_NONE
;
311 * Mark the fid as referenced so that the LRU
312 * reclaim won't close the file descriptor
314 f
->flags
|= FID_REFERENCED
;
315 f
->next
= s
->fid_list
;
321 static int v9fs_xattr_fid_clunk(V9fsState
*s
, V9fsFidState
*fidp
)
325 if (fidp
->fs
.xattr
.copied_len
== -1) {
326 /* getxattr/listxattr fid */
330 * if this is fid for setxattr. clunk should
331 * result in setxattr localcall
333 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
334 /* clunk after partial write */
338 if (fidp
->fs
.xattr
.len
) {
339 retval
= v9fs_co_lsetxattr(s
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
340 fidp
->fs
.xattr
.value
,
342 fidp
->fs
.xattr
.flags
);
344 retval
= v9fs_co_lremovexattr(s
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
347 v9fs_string_free(&fidp
->fs
.xattr
.name
);
349 if (fidp
->fs
.xattr
.value
) {
350 g_free(fidp
->fs
.xattr
.value
);
355 static int free_fid(V9fsState
*s
, V9fsFidState
*fidp
)
359 if (fidp
->fid_type
== P9_FID_FILE
) {
360 /* If we reclaimed the fd no need to close */
361 if (fidp
->fs
.fd
!= -1) {
362 retval
= v9fs_co_close(s
, fidp
->fs
.fd
);
364 } else if (fidp
->fid_type
== P9_FID_DIR
) {
365 if (fidp
->fs
.dir
!= NULL
) {
366 retval
= v9fs_co_closedir(s
, fidp
->fs
.dir
);
368 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
369 retval
= v9fs_xattr_fid_clunk(s
, fidp
);
371 v9fs_string_free(&fidp
->path
);
376 static void put_fid(V9fsState
*s
, V9fsFidState
*fidp
)
381 * Don't free the fid if it is in reclaim list
383 if (!fidp
->ref
&& fidp
->clunked
) {
388 static int clunk_fid(V9fsState
*s
, int32_t fid
)
390 V9fsFidState
**fidpp
, *fidp
;
392 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
393 if ((*fidpp
)->fid
== fid
) {
398 if (*fidpp
== NULL
) {
407 void v9fs_reclaim_fd(V9fsState
*s
)
409 int reclaim_count
= 0;
410 V9fsFidState
*f
, *reclaim_list
= NULL
;
412 for (f
= s
->fid_list
; f
; f
= f
->next
) {
414 * Unlink fids cannot be reclaimed. Check
415 * for them and skip them. Also skip fids
416 * currently being operated on.
418 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
422 * if it is a recently referenced fid
423 * we leave the fid untouched and clear the
424 * reference bit. We come back to it later
425 * in the next iteration. (a simple LRU without
426 * moving list elements around)
428 if (f
->flags
& FID_REFERENCED
) {
429 f
->flags
&= ~FID_REFERENCED
;
433 * Add fids to reclaim list.
435 if (f
->fid_type
== P9_FID_FILE
) {
436 if (f
->fs
.fd
!= -1) {
438 * Up the reference count so that
439 * a clunk request won't free this fid
442 f
->rclm_lst
= reclaim_list
;
444 f
->fs_reclaim
.fd
= f
->fs
.fd
;
448 } else if (f
->fid_type
== P9_FID_DIR
) {
449 if (f
->fs
.dir
!= NULL
) {
451 * Up the reference count so that
452 * a clunk request won't free this fid
455 f
->rclm_lst
= reclaim_list
;
457 f
->fs_reclaim
.dir
= f
->fs
.dir
;
462 if (reclaim_count
>= open_fd_rc
) {
467 * Now close the fid in reclaim list. Free them if they
468 * are already clunked.
470 while (reclaim_list
) {
472 reclaim_list
= f
->rclm_lst
;
473 if (f
->fid_type
== P9_FID_FILE
) {
474 v9fs_co_close(s
, f
->fs_reclaim
.fd
);
475 } else if (f
->fid_type
== P9_FID_DIR
) {
476 v9fs_co_closedir(s
, f
->fs_reclaim
.dir
);
480 * Now drop the fid reference, free it
487 static int v9fs_mark_fids_unreclaim(V9fsState
*s
, V9fsString
*str
)
490 V9fsFidState
*fidp
, head_fid
;
492 head_fid
.next
= s
->fid_list
;
493 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
494 if (!strcmp(fidp
->path
.data
, str
->data
)) {
495 /* Mark the fid non reclaimable. */
496 fidp
->flags
|= FID_NON_RECLAIMABLE
;
498 /* reopen the file/dir if already closed */
499 err
= v9fs_reopen_fid(s
, fidp
);
504 * Go back to head of fid list because
505 * the list could have got updated when
506 * switched to the worker thread
516 #define P9_QID_TYPE_DIR 0x80
517 #define P9_QID_TYPE_SYMLINK 0x02
519 #define P9_STAT_MODE_DIR 0x80000000
520 #define P9_STAT_MODE_APPEND 0x40000000
521 #define P9_STAT_MODE_EXCL 0x20000000
522 #define P9_STAT_MODE_MOUNT 0x10000000
523 #define P9_STAT_MODE_AUTH 0x08000000
524 #define P9_STAT_MODE_TMP 0x04000000
525 #define P9_STAT_MODE_SYMLINK 0x02000000
526 #define P9_STAT_MODE_LINK 0x01000000
527 #define P9_STAT_MODE_DEVICE 0x00800000
528 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
529 #define P9_STAT_MODE_SOCKET 0x00100000
530 #define P9_STAT_MODE_SETUID 0x00080000
531 #define P9_STAT_MODE_SETGID 0x00040000
532 #define P9_STAT_MODE_SETVTX 0x00010000
534 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
535 P9_STAT_MODE_SYMLINK | \
536 P9_STAT_MODE_LINK | \
537 P9_STAT_MODE_DEVICE | \
538 P9_STAT_MODE_NAMED_PIPE | \
541 /* This is the algorithm from ufs in spfs */
542 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
546 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
547 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
548 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
549 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
551 if (S_ISDIR(stbuf
->st_mode
)) {
552 qidp
->type
|= P9_QID_TYPE_DIR
;
554 if (S_ISLNK(stbuf
->st_mode
)) {
555 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
559 static int fid_to_qid(V9fsState
*s
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
564 err
= v9fs_co_lstat(s
, &fidp
->path
, &stbuf
);
568 stat_to_qid(&stbuf
, qidp
);
572 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
576 if (!QLIST_EMPTY(&s
->free_list
)) {
577 pdu
= QLIST_FIRST(&s
->free_list
);
578 QLIST_REMOVE(pdu
, next
);
583 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
589 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
593 size_t pdu_packunpack(void *addr
, struct iovec
*sg
, int sg_count
,
594 size_t offset
, size_t size
, int pack
)
599 for (i
= 0; size
&& i
< sg_count
; i
++) {
601 if (offset
>= sg
[i
].iov_len
) {
603 offset
-= sg
[i
].iov_len
;
606 len
= MIN(sg
[i
].iov_len
- offset
, size
);
608 memcpy(sg
[i
].iov_base
+ offset
, addr
, len
);
610 memcpy(addr
, sg
[i
].iov_base
+ offset
, len
);
625 static size_t pdu_unpack(void *dst
, V9fsPDU
*pdu
, size_t offset
, size_t size
)
627 return pdu_packunpack(dst
, pdu
->elem
.out_sg
, pdu
->elem
.out_num
,
631 static size_t pdu_pack(V9fsPDU
*pdu
, size_t offset
, const void *src
,
634 return pdu_packunpack((void *)src
, pdu
->elem
.in_sg
, pdu
->elem
.in_num
,
638 static int pdu_copy_sg(V9fsPDU
*pdu
, size_t offset
, int rx
, struct iovec
*sg
)
642 struct iovec
*src_sg
;
646 src_sg
= pdu
->elem
.in_sg
;
647 num
= pdu
->elem
.in_num
;
649 src_sg
= pdu
->elem
.out_sg
;
650 num
= pdu
->elem
.out_num
;
654 for (i
= 0; i
< num
; i
++) {
656 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
657 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
659 } else if (offset
< (src_sg
[i
].iov_len
+ pos
)) {
660 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
661 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
662 sg
[j
].iov_base
+= (offset
- pos
);
663 sg
[j
].iov_len
-= (offset
- pos
);
666 pos
+= src_sg
[i
].iov_len
;
672 static size_t pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
674 size_t old_offset
= offset
;
679 for (i
= 0; fmt
[i
]; i
++) {
682 uint8_t *valp
= va_arg(ap
, uint8_t *);
683 offset
+= pdu_unpack(valp
, pdu
, offset
, sizeof(*valp
));
688 valp
= va_arg(ap
, uint16_t *);
689 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
690 *valp
= le16_to_cpu(val
);
695 valp
= va_arg(ap
, uint32_t *);
696 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
697 *valp
= le32_to_cpu(val
);
702 valp
= va_arg(ap
, uint64_t *);
703 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
704 *valp
= le64_to_cpu(val
);
708 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
709 int *iovcnt
= va_arg(ap
, int *);
710 *iovcnt
= pdu_copy_sg(pdu
, offset
, 0, iov
);
714 V9fsString
*str
= va_arg(ap
, V9fsString
*);
715 offset
+= pdu_unmarshal(pdu
, offset
, "w", &str
->size
);
716 /* FIXME: sanity check str->size */
717 str
->data
= g_malloc(str
->size
+ 1);
718 offset
+= pdu_unpack(str
->data
, pdu
, offset
, str
->size
);
719 str
->data
[str
->size
] = 0;
723 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
724 offset
+= pdu_unmarshal(pdu
, offset
, "bdq",
725 &qidp
->type
, &qidp
->version
, &qidp
->path
);
729 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
730 offset
+= pdu_unmarshal(pdu
, offset
, "wwdQdddqsssssddd",
731 &statp
->size
, &statp
->type
, &statp
->dev
,
732 &statp
->qid
, &statp
->mode
, &statp
->atime
,
733 &statp
->mtime
, &statp
->length
,
734 &statp
->name
, &statp
->uid
, &statp
->gid
,
735 &statp
->muid
, &statp
->extension
,
736 &statp
->n_uid
, &statp
->n_gid
,
741 V9fsIattr
*iattr
= va_arg(ap
, V9fsIattr
*);
742 offset
+= pdu_unmarshal(pdu
, offset
, "ddddqqqqq",
743 &iattr
->valid
, &iattr
->mode
,
744 &iattr
->uid
, &iattr
->gid
, &iattr
->size
,
745 &iattr
->atime_sec
, &iattr
->atime_nsec
,
746 &iattr
->mtime_sec
, &iattr
->mtime_nsec
);
756 return offset
- old_offset
;
759 static size_t pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
761 size_t old_offset
= offset
;
766 for (i
= 0; fmt
[i
]; i
++) {
769 uint8_t val
= va_arg(ap
, int);
770 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
775 cpu_to_le16w(&val
, va_arg(ap
, int));
776 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
781 cpu_to_le32w(&val
, va_arg(ap
, uint32_t));
782 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
787 cpu_to_le64w(&val
, va_arg(ap
, uint64_t));
788 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
792 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
793 int *iovcnt
= va_arg(ap
, int *);
794 *iovcnt
= pdu_copy_sg(pdu
, offset
, 1, iov
);
798 V9fsString
*str
= va_arg(ap
, V9fsString
*);
799 offset
+= pdu_marshal(pdu
, offset
, "w", str
->size
);
800 offset
+= pdu_pack(pdu
, offset
, str
->data
, str
->size
);
804 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
805 offset
+= pdu_marshal(pdu
, offset
, "bdq",
806 qidp
->type
, qidp
->version
, qidp
->path
);
810 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
811 offset
+= pdu_marshal(pdu
, offset
, "wwdQdddqsssssddd",
812 statp
->size
, statp
->type
, statp
->dev
,
813 &statp
->qid
, statp
->mode
, statp
->atime
,
814 statp
->mtime
, statp
->length
, &statp
->name
,
815 &statp
->uid
, &statp
->gid
, &statp
->muid
,
816 &statp
->extension
, statp
->n_uid
,
817 statp
->n_gid
, statp
->n_muid
);
821 V9fsStatDotl
*statp
= va_arg(ap
, V9fsStatDotl
*);
822 offset
+= pdu_marshal(pdu
, offset
, "qQdddqqqqqqqqqqqqqqq",
823 statp
->st_result_mask
,
824 &statp
->qid
, statp
->st_mode
,
825 statp
->st_uid
, statp
->st_gid
,
826 statp
->st_nlink
, statp
->st_rdev
,
827 statp
->st_size
, statp
->st_blksize
, statp
->st_blocks
,
828 statp
->st_atime_sec
, statp
->st_atime_nsec
,
829 statp
->st_mtime_sec
, statp
->st_mtime_nsec
,
830 statp
->st_ctime_sec
, statp
->st_ctime_nsec
,
831 statp
->st_btime_sec
, statp
->st_btime_nsec
,
832 statp
->st_gen
, statp
->st_data_version
);
841 return offset
- old_offset
;
844 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
846 int8_t id
= pdu
->id
+ 1; /* Response */
852 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
855 str
.data
= strerror(err
);
856 str
.size
= strlen(str
.data
);
858 len
+= pdu_marshal(pdu
, len
, "s", &str
);
862 len
+= pdu_marshal(pdu
, len
, "d", err
);
864 if (s
->proto_version
== V9FS_PROTO_2000L
) {
869 /* fill out the header */
870 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
872 /* keep these in sync */
876 /* push onto queue and notify */
877 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
879 /* FIXME: we should batch these completions */
880 virtio_notify(&s
->vdev
, s
->vq
);
885 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
890 if (mode
& P9_STAT_MODE_DIR
) {
894 if (mode
& P9_STAT_MODE_SYMLINK
) {
897 if (mode
& P9_STAT_MODE_SOCKET
) {
900 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
903 if (mode
& P9_STAT_MODE_DEVICE
) {
904 if (extension
&& extension
->data
[0] == 'c') {
915 if (mode
& P9_STAT_MODE_SETUID
) {
918 if (mode
& P9_STAT_MODE_SETGID
) {
921 if (mode
& P9_STAT_MODE_SETVTX
) {
928 static int donttouch_stat(V9fsStat
*stat
)
930 if (stat
->type
== -1 &&
932 stat
->qid
.type
== -1 &&
933 stat
->qid
.version
== -1 &&
934 stat
->qid
.path
== -1 &&
938 stat
->length
== -1 &&
945 stat
->n_muid
== -1) {
952 static void v9fs_stat_free(V9fsStat
*stat
)
954 v9fs_string_free(&stat
->name
);
955 v9fs_string_free(&stat
->uid
);
956 v9fs_string_free(&stat
->gid
);
957 v9fs_string_free(&stat
->muid
);
958 v9fs_string_free(&stat
->extension
);
961 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
965 mode
= stbuf
->st_mode
& 0777;
966 if (S_ISDIR(stbuf
->st_mode
)) {
967 mode
|= P9_STAT_MODE_DIR
;
970 if (S_ISLNK(stbuf
->st_mode
)) {
971 mode
|= P9_STAT_MODE_SYMLINK
;
974 if (S_ISSOCK(stbuf
->st_mode
)) {
975 mode
|= P9_STAT_MODE_SOCKET
;
978 if (S_ISFIFO(stbuf
->st_mode
)) {
979 mode
|= P9_STAT_MODE_NAMED_PIPE
;
982 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
983 mode
|= P9_STAT_MODE_DEVICE
;
986 if (stbuf
->st_mode
& S_ISUID
) {
987 mode
|= P9_STAT_MODE_SETUID
;
990 if (stbuf
->st_mode
& S_ISGID
) {
991 mode
|= P9_STAT_MODE_SETGID
;
994 if (stbuf
->st_mode
& S_ISVTX
) {
995 mode
|= P9_STAT_MODE_SETVTX
;
1001 static int stat_to_v9stat(V9fsState
*s
, V9fsString
*name
,
1002 const struct stat
*stbuf
,
1008 memset(v9stat
, 0, sizeof(*v9stat
));
1010 stat_to_qid(stbuf
, &v9stat
->qid
);
1011 v9stat
->mode
= stat_to_v9mode(stbuf
);
1012 v9stat
->atime
= stbuf
->st_atime
;
1013 v9stat
->mtime
= stbuf
->st_mtime
;
1014 v9stat
->length
= stbuf
->st_size
;
1016 v9fs_string_null(&v9stat
->uid
);
1017 v9fs_string_null(&v9stat
->gid
);
1018 v9fs_string_null(&v9stat
->muid
);
1020 v9stat
->n_uid
= stbuf
->st_uid
;
1021 v9stat
->n_gid
= stbuf
->st_gid
;
1024 v9fs_string_null(&v9stat
->extension
);
1026 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
1027 err
= v9fs_co_readlink(s
, name
, &v9stat
->extension
);
1031 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
1032 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
1033 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
1034 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
1035 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
1036 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
1037 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
1040 str
= strrchr(name
->data
, '/');
1047 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
1050 v9fs_string_size(&v9stat
->name
) +
1051 v9fs_string_size(&v9stat
->uid
) +
1052 v9fs_string_size(&v9stat
->gid
) +
1053 v9fs_string_size(&v9stat
->muid
) +
1054 v9fs_string_size(&v9stat
->extension
);
1058 #define P9_STATS_MODE 0x00000001ULL
1059 #define P9_STATS_NLINK 0x00000002ULL
1060 #define P9_STATS_UID 0x00000004ULL
1061 #define P9_STATS_GID 0x00000008ULL
1062 #define P9_STATS_RDEV 0x00000010ULL
1063 #define P9_STATS_ATIME 0x00000020ULL
1064 #define P9_STATS_MTIME 0x00000040ULL
1065 #define P9_STATS_CTIME 0x00000080ULL
1066 #define P9_STATS_INO 0x00000100ULL
1067 #define P9_STATS_SIZE 0x00000200ULL
1068 #define P9_STATS_BLOCKS 0x00000400ULL
1070 #define P9_STATS_BTIME 0x00000800ULL
1071 #define P9_STATS_GEN 0x00001000ULL
1072 #define P9_STATS_DATA_VERSION 0x00002000ULL
1074 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1075 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1078 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
1079 V9fsStatDotl
*v9lstat
)
1081 memset(v9lstat
, 0, sizeof(*v9lstat
));
1083 v9lstat
->st_mode
= stbuf
->st_mode
;
1084 v9lstat
->st_nlink
= stbuf
->st_nlink
;
1085 v9lstat
->st_uid
= stbuf
->st_uid
;
1086 v9lstat
->st_gid
= stbuf
->st_gid
;
1087 v9lstat
->st_rdev
= stbuf
->st_rdev
;
1088 v9lstat
->st_size
= stbuf
->st_size
;
1089 v9lstat
->st_blksize
= stbuf
->st_blksize
;
1090 v9lstat
->st_blocks
= stbuf
->st_blocks
;
1091 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
1092 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
1093 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
1094 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
1095 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
1096 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
1097 /* Currently we only support BASIC fields in stat */
1098 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
1100 stat_to_qid(stbuf
, &v9lstat
->qid
);
1103 static struct iovec
*adjust_sg(struct iovec
*sg
, int len
, int *iovcnt
)
1105 while (len
&& *iovcnt
) {
1106 if (len
< sg
->iov_len
) {
1108 sg
->iov_base
+= len
;
1120 static struct iovec
*cap_sg(struct iovec
*sg
, int cap
, int *cnt
)
1125 for (i
= 0; i
< *cnt
; i
++) {
1126 if ((total
+ sg
[i
].iov_len
) > cap
) {
1127 sg
[i
].iov_len
-= ((total
+ sg
[i
].iov_len
) - cap
);
1131 total
+= sg
[i
].iov_len
;
1139 static void print_sg(struct iovec
*sg
, int cnt
)
1143 printf("sg[%d]: {", cnt
);
1144 for (i
= 0; i
< cnt
; i
++) {
1148 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
1153 static void v9fs_fix_path(V9fsString
*dst
, V9fsString
*src
, int len
)
1156 v9fs_string_init(&str
);
1157 v9fs_string_copy(&str
, dst
);
1158 v9fs_string_sprintf(dst
, "%s%s", src
->data
, str
.data
+len
);
1159 v9fs_string_free(&str
);
1162 static void v9fs_version(void *opaque
)
1164 V9fsPDU
*pdu
= opaque
;
1165 V9fsState
*s
= pdu
->s
;
1169 pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
1171 if (!strcmp(version
.data
, "9P2000.u")) {
1172 s
->proto_version
= V9FS_PROTO_2000U
;
1173 } else if (!strcmp(version
.data
, "9P2000.L")) {
1174 s
->proto_version
= V9FS_PROTO_2000L
;
1176 v9fs_string_sprintf(&version
, "unknown");
1179 offset
+= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
1180 complete_pdu(s
, pdu
, offset
);
1182 v9fs_string_free(&version
);
1186 static void v9fs_attach(void *opaque
)
1188 V9fsPDU
*pdu
= opaque
;
1189 V9fsState
*s
= pdu
->s
;
1190 int32_t fid
, afid
, n_uname
;
1191 V9fsString uname
, aname
;
1197 pdu_unmarshal(pdu
, offset
, "ddssd", &fid
, &afid
, &uname
, &aname
, &n_uname
);
1199 fidp
= alloc_fid(s
, fid
);
1204 fidp
->uid
= n_uname
;
1205 v9fs_string_sprintf(&fidp
->path
, "%s", "/");
1206 err
= fid_to_qid(s
, fidp
, &qid
);
1212 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
1217 complete_pdu(s
, pdu
, err
);
1218 v9fs_string_free(&uname
);
1219 v9fs_string_free(&aname
);
1222 static void v9fs_stat(void *opaque
)
1230 V9fsPDU
*pdu
= opaque
;
1231 V9fsState
*s
= pdu
->s
;
1233 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1235 fidp
= get_fid(s
, fid
);
1240 err
= v9fs_co_lstat(s
, &fidp
->path
, &stbuf
);
1244 err
= stat_to_v9stat(s
, &fidp
->path
, &stbuf
, &v9stat
);
1248 offset
+= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1250 v9fs_stat_free(&v9stat
);
1254 complete_pdu(s
, pdu
, err
);
1257 static void v9fs_getattr(void *opaque
)
1264 uint64_t request_mask
;
1265 V9fsStatDotl v9stat_dotl
;
1266 V9fsPDU
*pdu
= opaque
;
1267 V9fsState
*s
= pdu
->s
;
1269 pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1271 fidp
= get_fid(s
, fid
);
1277 * Currently we only support BASIC fields in stat, so there is no
1278 * need to look at request_mask.
1280 retval
= v9fs_co_lstat(s
, &fidp
->path
, &stbuf
);
1284 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1286 retval
+= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1290 complete_pdu(s
, pdu
, retval
);
1293 /* From Linux kernel code */
1294 #define ATTR_MODE (1 << 0)
1295 #define ATTR_UID (1 << 1)
1296 #define ATTR_GID (1 << 2)
1297 #define ATTR_SIZE (1 << 3)
1298 #define ATTR_ATIME (1 << 4)
1299 #define ATTR_MTIME (1 << 5)
1300 #define ATTR_CTIME (1 << 6)
1301 #define ATTR_MASK 127
1302 #define ATTR_ATIME_SET (1 << 7)
1303 #define ATTR_MTIME_SET (1 << 8)
1305 static void v9fs_setattr(void *opaque
)
1312 V9fsPDU
*pdu
= opaque
;
1313 V9fsState
*s
= pdu
->s
;
1315 pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1317 fidp
= get_fid(s
, fid
);
1322 if (v9iattr
.valid
& ATTR_MODE
) {
1323 err
= v9fs_co_chmod(s
, &fidp
->path
, v9iattr
.mode
);
1328 if (v9iattr
.valid
& (ATTR_ATIME
| ATTR_MTIME
)) {
1329 struct timespec times
[2];
1330 if (v9iattr
.valid
& ATTR_ATIME
) {
1331 if (v9iattr
.valid
& ATTR_ATIME_SET
) {
1332 times
[0].tv_sec
= v9iattr
.atime_sec
;
1333 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1335 times
[0].tv_nsec
= UTIME_NOW
;
1338 times
[0].tv_nsec
= UTIME_OMIT
;
1340 if (v9iattr
.valid
& ATTR_MTIME
) {
1341 if (v9iattr
.valid
& ATTR_MTIME_SET
) {
1342 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1343 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1345 times
[1].tv_nsec
= UTIME_NOW
;
1348 times
[1].tv_nsec
= UTIME_OMIT
;
1350 err
= v9fs_co_utimensat(s
, &fidp
->path
, times
);
1356 * If the only valid entry in iattr is ctime we can call
1357 * chown(-1,-1) to update the ctime of the file
1359 if ((v9iattr
.valid
& (ATTR_UID
| ATTR_GID
)) ||
1360 ((v9iattr
.valid
& ATTR_CTIME
)
1361 && !((v9iattr
.valid
& ATTR_MASK
) & ~ATTR_CTIME
))) {
1362 if (!(v9iattr
.valid
& ATTR_UID
)) {
1365 if (!(v9iattr
.valid
& ATTR_GID
)) {
1368 err
= v9fs_co_chown(s
, &fidp
->path
, v9iattr
.uid
,
1374 if (v9iattr
.valid
& (ATTR_SIZE
)) {
1375 err
= v9fs_co_truncate(s
, &fidp
->path
, v9iattr
.size
);
1384 complete_pdu(s
, pdu
, err
);
1387 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1391 offset
+= pdu_marshal(pdu
, offset
, "w", nwnames
);
1392 for (i
= 0; i
< nwnames
; i
++) {
1393 offset
+= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1398 static void v9fs_walk(void *opaque
)
1401 V9fsQID
*qids
= NULL
;
1407 int32_t fid
, newfid
;
1408 V9fsString
*wnames
= NULL
;
1410 V9fsFidState
*newfidp
= NULL
;;
1411 V9fsPDU
*pdu
= opaque
;
1412 V9fsState
*s
= pdu
->s
;
1414 offset
+= pdu_unmarshal(pdu
, offset
, "ddw", &fid
,
1417 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1418 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1419 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1420 for (i
= 0; i
< nwnames
; i
++) {
1421 offset
+= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1424 } else if (nwnames
> P9_MAXWELEM
) {
1428 fidp
= get_fid(s
, fid
);
1433 if (fid
== newfid
) {
1434 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1435 v9fs_string_init(&path
);
1436 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1437 v9fs_string_sprintf(&path
, "%s/%s",
1438 fidp
->path
.data
, wnames
[name_idx
].data
);
1439 v9fs_string_copy(&fidp
->path
, &path
);
1441 err
= v9fs_co_lstat(s
, &fidp
->path
, &stbuf
);
1443 v9fs_string_free(&path
);
1446 stat_to_qid(&stbuf
, &qids
[name_idx
]);
1448 v9fs_string_free(&path
);
1450 newfidp
= alloc_fid(s
, newfid
);
1451 if (newfidp
== NULL
) {
1455 newfidp
->uid
= fidp
->uid
;
1456 v9fs_string_init(&path
);
1457 v9fs_string_copy(&newfidp
->path
, &fidp
->path
);
1458 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1459 v9fs_string_sprintf(&path
, "%s/%s", newfidp
->path
.data
,
1460 wnames
[name_idx
].data
);
1461 v9fs_string_copy(&newfidp
->path
, &path
);
1462 err
= v9fs_co_lstat(s
, &newfidp
->path
, &stbuf
);
1464 clunk_fid(s
, newfidp
->fid
);
1465 v9fs_string_free(&path
);
1468 stat_to_qid(&stbuf
, &qids
[name_idx
]);
1470 v9fs_string_free(&path
);
1472 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1476 put_fid(s
, newfidp
);
1479 complete_pdu(s
, pdu
, err
);
1480 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1481 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1482 v9fs_string_free(&wnames
[name_idx
]);
1489 static int32_t get_iounit(V9fsState
*s
, V9fsString
*name
)
1491 struct statfs stbuf
;
1495 * iounit should be multiples of f_bsize (host filesystem block size
1496 * and as well as less than (client msize - P9_IOHDRSZ))
1498 if (!v9fs_co_statfs(s
, name
, &stbuf
)) {
1499 iounit
= stbuf
.f_bsize
;
1500 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1503 iounit
= s
->msize
- P9_IOHDRSZ
;
1508 static void v9fs_open(void *opaque
)
1519 V9fsPDU
*pdu
= opaque
;
1520 V9fsState
*s
= pdu
->s
;
1522 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1523 pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1525 pdu_unmarshal(pdu
, offset
, "db", &fid
, &mode
);
1527 fidp
= get_fid(s
, fid
);
1532 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1534 err
= v9fs_co_lstat(s
, &fidp
->path
, &stbuf
);
1538 stat_to_qid(&stbuf
, &qid
);
1539 if (S_ISDIR(stbuf
.st_mode
)) {
1540 err
= v9fs_co_opendir(s
, fidp
);
1544 fidp
->fid_type
= P9_FID_DIR
;
1545 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1548 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1550 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
1551 /* Ignore direct disk access hint until the server supports it. */
1554 flags
= omode_to_uflags(mode
);
1556 err
= v9fs_co_open(s
, fidp
, flags
);
1560 fidp
->fid_type
= P9_FID_FILE
;
1561 fidp
->open_flags
= flags
;
1562 if (flags
& O_EXCL
) {
1564 * We let the host file system do O_EXCL check
1565 * We should not reclaim such fd
1567 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1569 iounit
= get_iounit(s
, &fidp
->path
);
1570 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1576 complete_pdu(s
, pdu
, err
);
1579 static void v9fs_lcreate(void *opaque
)
1581 int32_t dfid
, flags
, mode
;
1590 V9fsPDU
*pdu
= opaque
;
1592 pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
, &name
, &flags
,
1595 fidp
= get_fid(pdu
->s
, dfid
);
1601 /* Ignore direct disk access hint until the server supports it. */
1604 err
= v9fs_co_open2(pdu
->s
, fidp
, &name
, gid
,
1605 flags
| O_CREAT
, mode
, &stbuf
);
1609 fidp
->fid_type
= P9_FID_FILE
;
1610 fidp
->open_flags
= flags
;
1611 if (flags
& O_EXCL
) {
1613 * We let the host file system do O_EXCL check
1614 * We should not reclaim such fd
1616 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1618 iounit
= get_iounit(pdu
->s
, &fidp
->path
);
1619 stat_to_qid(&stbuf
, &qid
);
1620 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1623 put_fid(pdu
->s
, fidp
);
1625 complete_pdu(pdu
->s
, pdu
, err
);
1626 v9fs_string_free(&name
);
1629 static void v9fs_fsync(void *opaque
)
1636 V9fsPDU
*pdu
= opaque
;
1637 V9fsState
*s
= pdu
->s
;
1639 pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1640 fidp
= get_fid(s
, fid
);
1645 err
= v9fs_co_fsync(s
, fidp
, datasync
);
1651 complete_pdu(s
, pdu
, err
);
1654 static void v9fs_clunk(void *opaque
)
1660 V9fsPDU
*pdu
= opaque
;
1661 V9fsState
*s
= pdu
->s
;
1663 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1665 fidp
= get_fid(s
, fid
);
1670 err
= clunk_fid(s
, fidp
->fid
);
1678 complete_pdu(s
, pdu
, err
);
1681 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
,
1682 V9fsFidState
*fidp
, int64_t off
, int32_t max_count
)
1688 xattr_len
= fidp
->fs
.xattr
.len
;
1689 read_count
= xattr_len
- off
;
1690 if (read_count
> max_count
) {
1691 read_count
= max_count
;
1692 } else if (read_count
< 0) {
1694 * read beyond XATTR value
1698 offset
+= pdu_marshal(pdu
, offset
, "d", read_count
);
1699 offset
+= pdu_pack(pdu
, offset
,
1700 ((char *)fidp
->fs
.xattr
.value
) + off
,
1705 static int v9fs_do_readdir_with_stat(V9fsState
*s
, V9fsPDU
*pdu
,
1706 V9fsFidState
*fidp
, int32_t max_count
)
1713 off_t saved_dir_pos
;
1714 struct dirent
*dent
, *result
;
1716 /* save the directory position */
1717 saved_dir_pos
= v9fs_co_telldir(s
, fidp
);
1718 if (saved_dir_pos
< 0) {
1719 return saved_dir_pos
;
1722 dent
= g_malloc(sizeof(struct dirent
));
1725 v9fs_string_init(&name
);
1726 err
= v9fs_co_readdir_r(s
, fidp
, dent
, &result
);
1727 if (err
|| !result
) {
1730 v9fs_string_sprintf(&name
, "%s/%s", fidp
->path
.data
, dent
->d_name
);
1731 err
= v9fs_co_lstat(s
, &name
, &stbuf
);
1735 err
= stat_to_v9stat(s
, &name
, &stbuf
, &v9stat
);
1739 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1740 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1741 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1742 /* Ran out of buffer. Set dir back to old position and return */
1743 v9fs_co_seekdir(s
, fidp
, saved_dir_pos
);
1744 v9fs_stat_free(&v9stat
);
1745 v9fs_string_free(&name
);
1750 v9fs_stat_free(&v9stat
);
1751 v9fs_string_free(&name
);
1752 saved_dir_pos
= dent
->d_off
;
1756 v9fs_string_free(&name
);
1763 static void v9fs_read(void *opaque
)
1772 V9fsPDU
*pdu
= opaque
;
1773 V9fsState
*s
= pdu
->s
;
1775 pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1777 fidp
= get_fid(s
, fid
);
1782 if (fidp
->fid_type
== P9_FID_DIR
) {
1785 v9fs_co_rewinddir(s
, fidp
);
1787 count
= v9fs_do_readdir_with_stat(s
, pdu
, fidp
, max_count
);
1793 err
+= pdu_marshal(pdu
, offset
, "d", count
);
1795 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1799 struct iovec iov
[128]; /* FIXME: bad, bad, bad */
1802 pdu_marshal(pdu
, offset
+ 4, "v", sg
, &cnt
);
1803 sg
= cap_sg(sg
, max_count
, &cnt
);
1808 /* Loop in case of EINTR */
1810 len
= v9fs_co_preadv(s
, fidp
, sg
, cnt
, off
);
1815 } while (len
== -EINTR
);
1817 /* IO error return the error */
1821 sg
= adjust_sg(sg
, len
, &cnt
);
1822 } while (count
< max_count
&& len
> 0);
1824 err
+= pdu_marshal(pdu
, offset
, "d", count
);
1826 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1827 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1834 complete_pdu(s
, pdu
, err
);
1837 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1840 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1841 * size of type (1) + size of name.size (2) + strlen(name.data)
1843 return 24 + v9fs_string_size(name
);
1846 static int v9fs_do_readdir(V9fsState
*s
, V9fsPDU
*pdu
,
1847 V9fsFidState
*fidp
, int32_t max_count
)
1854 off_t saved_dir_pos
;
1855 struct dirent
*dent
, *result
;
1857 /* save the directory position */
1858 saved_dir_pos
= v9fs_co_telldir(s
, fidp
);
1859 if (saved_dir_pos
< 0) {
1860 return saved_dir_pos
;
1863 dent
= g_malloc(sizeof(struct dirent
));
1866 err
= v9fs_co_readdir_r(s
, fidp
, dent
, &result
);
1867 if (err
|| !result
) {
1870 v9fs_string_init(&name
);
1871 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1872 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1873 /* Ran out of buffer. Set dir back to old position and return */
1874 v9fs_co_seekdir(s
, fidp
, saved_dir_pos
);
1875 v9fs_string_free(&name
);
1880 * Fill up just the path field of qid because the client uses
1881 * only that. To fill the entire qid structure we will have
1882 * to stat each dirent found, which is expensive
1884 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1885 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1886 /* Fill the other fields with dummy values */
1890 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1891 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1893 dent
->d_type
, &name
);
1895 v9fs_string_free(&name
);
1896 saved_dir_pos
= dent
->d_off
;
1905 static void v9fs_readdir(void *opaque
)
1911 int64_t initial_offset
;
1912 int32_t count
, max_count
;
1913 V9fsPDU
*pdu
= opaque
;
1914 V9fsState
*s
= pdu
->s
;
1916 pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &initial_offset
, &max_count
);
1918 fidp
= get_fid(s
, fid
);
1923 if (!fidp
->fs
.dir
) {
1927 if (initial_offset
== 0) {
1928 v9fs_co_rewinddir(s
, fidp
);
1930 v9fs_co_seekdir(s
, fidp
, initial_offset
);
1932 count
= v9fs_do_readdir(s
, pdu
, fidp
, max_count
);
1938 retval
+= pdu_marshal(pdu
, offset
, "d", count
);
1943 complete_pdu(s
, pdu
, retval
);
1946 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1947 int64_t off
, int32_t count
,
1948 struct iovec
*sg
, int cnt
)
1957 xattr_len
= fidp
->fs
.xattr
.len
;
1958 write_count
= xattr_len
- off
;
1959 if (write_count
> count
) {
1960 write_count
= count
;
1961 } else if (write_count
< 0) {
1963 * write beyond XATTR value len specified in
1969 offset
+= pdu_marshal(pdu
, offset
, "d", write_count
);
1971 fidp
->fs
.xattr
.copied_len
+= write_count
;
1973 * Now copy the content from sg list
1975 for (i
= 0; i
< cnt
; i
++) {
1976 if (write_count
> sg
[i
].iov_len
) {
1977 to_copy
= sg
[i
].iov_len
;
1979 to_copy
= write_count
;
1981 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
1982 /* updating vs->off since we are not using below */
1984 write_count
-= to_copy
;
1990 static void v9fs_write(void *opaque
)
2001 struct iovec iov
[128]; /* FIXME: bad, bad, bad */
2002 struct iovec
*sg
= iov
;
2003 V9fsPDU
*pdu
= opaque
;
2004 V9fsState
*s
= pdu
->s
;
2006 pdu_unmarshal(pdu
, offset
, "dqdv", &fid
, &off
, &count
, sg
, &cnt
);
2008 fidp
= get_fid(s
, fid
);
2013 if (fidp
->fid_type
== P9_FID_FILE
) {
2014 if (fidp
->fs
.fd
== -1) {
2018 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2020 * setxattr operation
2022 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
, sg
, cnt
);
2028 sg
= cap_sg(sg
, count
, &cnt
);
2033 /* Loop in case of EINTR */
2035 len
= v9fs_co_pwritev(s
, fidp
, sg
, cnt
, off
);
2040 } while (len
== -EINTR
);
2042 /* IO error return the error */
2046 sg
= adjust_sg(sg
, len
, &cnt
);
2047 } while (total
< count
&& len
> 0);
2048 offset
+= pdu_marshal(pdu
, offset
, "d", total
);
2053 complete_pdu(s
, pdu
, err
);
2056 static void v9fs_create(void *opaque
)
2067 V9fsString extension
;
2068 V9fsString fullname
;
2070 V9fsPDU
*pdu
= opaque
;
2072 v9fs_string_init(&fullname
);
2074 pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2075 &perm
, &mode
, &extension
);
2077 fidp
= get_fid(pdu
->s
, fid
);
2083 v9fs_string_sprintf(&fullname
, "%s/%s", fidp
->path
.data
, name
.data
);
2084 err
= v9fs_co_lstat(pdu
->s
, &fullname
, &stbuf
);
2088 } else if (err
!= -ENOENT
) {
2091 if (perm
& P9_STAT_MODE_DIR
) {
2092 err
= v9fs_co_mkdir(pdu
->s
, fidp
, &name
, perm
& 0777,
2093 fidp
->uid
, -1, &stbuf
);
2097 v9fs_string_sprintf(&fullname
, "%s/%s", fidp
->path
.data
, name
.data
);
2098 v9fs_string_copy(&fidp
->path
, &fullname
);
2099 err
= v9fs_co_opendir(pdu
->s
, fidp
);
2103 fidp
->fid_type
= P9_FID_DIR
;
2104 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2105 err
= v9fs_co_symlink(pdu
->s
, fidp
, &name
,
2106 extension
.data
, -1 , &stbuf
);
2110 v9fs_string_sprintf(&fullname
, "%s/%s", fidp
->path
.data
, name
.data
);
2111 v9fs_string_copy(&fidp
->path
, &fullname
);
2112 } else if (perm
& P9_STAT_MODE_LINK
) {
2113 int32_t nfid
= atoi(extension
.data
);
2114 V9fsFidState
*nfidp
= get_fid(pdu
->s
, nfid
);
2115 if (nfidp
== NULL
) {
2119 v9fs_string_sprintf(&fullname
, "%s/%s", fidp
->path
.data
, name
.data
);
2120 err
= v9fs_co_link(pdu
->s
, &nfidp
->path
, &fullname
);
2121 put_fid(pdu
->s
, nfidp
);
2125 v9fs_string_sprintf(&fullname
, "%s/%s", fidp
->path
.data
, name
.data
);
2126 v9fs_string_copy(&fidp
->path
, &fullname
);
2127 err
= v9fs_co_lstat(pdu
->s
, &fidp
->path
, &stbuf
);
2129 fidp
->fid_type
= P9_FID_NONE
;
2132 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2134 uint32_t major
, minor
;
2137 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2154 nmode
|= perm
& 0777;
2155 err
= v9fs_co_mknod(pdu
->s
, fidp
, &name
, fidp
->uid
, -1,
2156 makedev(major
, minor
), nmode
, &stbuf
);
2160 v9fs_string_sprintf(&fullname
, "%s/%s", fidp
->path
.data
, name
.data
);
2161 v9fs_string_copy(&fidp
->path
, &fullname
);
2162 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2163 err
= v9fs_co_mknod(pdu
->s
, fidp
, &name
, fidp
->uid
, -1,
2164 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2168 v9fs_string_sprintf(&fullname
, "%s/%s", fidp
->path
.data
, name
.data
);
2169 v9fs_string_copy(&fidp
->path
, &fullname
);
2170 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2171 err
= v9fs_co_mknod(pdu
->s
, fidp
, &name
, fidp
->uid
, -1,
2172 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2176 v9fs_string_sprintf(&fullname
, "%s/%s", fidp
->path
.data
, name
.data
);
2177 v9fs_string_copy(&fidp
->path
, &fullname
);
2179 err
= v9fs_co_open2(pdu
->s
, fidp
, &name
, -1,
2180 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2184 fidp
->fid_type
= P9_FID_FILE
;
2185 fidp
->open_flags
= omode_to_uflags(mode
);
2186 if (fidp
->open_flags
& O_EXCL
) {
2188 * We let the host file system do O_EXCL check
2189 * We should not reclaim such fd
2191 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2194 iounit
= get_iounit(pdu
->s
, &fidp
->path
);
2195 stat_to_qid(&stbuf
, &qid
);
2196 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2199 put_fid(pdu
->s
, fidp
);
2201 complete_pdu(pdu
->s
, pdu
, err
);
2202 v9fs_string_free(&name
);
2203 v9fs_string_free(&extension
);
2204 v9fs_string_free(&fullname
);
2207 static void v9fs_symlink(void *opaque
)
2209 V9fsPDU
*pdu
= opaque
;
2212 V9fsFidState
*dfidp
;
2220 pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2222 dfidp
= get_fid(pdu
->s
, dfid
);
2223 if (dfidp
== NULL
) {
2227 err
= v9fs_co_symlink(pdu
->s
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2231 stat_to_qid(&stbuf
, &qid
);
2232 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
2235 put_fid(pdu
->s
, dfidp
);
2237 complete_pdu(pdu
->s
, pdu
, err
);
2238 v9fs_string_free(&name
);
2239 v9fs_string_free(&symname
);
2242 static void v9fs_flush(void *opaque
)
2244 V9fsPDU
*pdu
= opaque
;
2245 V9fsState
*s
= pdu
->s
;
2246 /* A nop call with no return */
2247 complete_pdu(s
, pdu
, 7);
2251 static void v9fs_link(void *opaque
)
2253 V9fsPDU
*pdu
= opaque
;
2254 V9fsState
*s
= pdu
->s
;
2255 int32_t dfid
, oldfid
;
2256 V9fsFidState
*dfidp
, *oldfidp
;
2257 V9fsString name
, fullname
;
2261 v9fs_string_init(&fullname
);
2263 pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2265 dfidp
= get_fid(s
, dfid
);
2266 if (dfidp
== NULL
) {
2271 oldfidp
= get_fid(s
, oldfid
);
2272 if (oldfidp
== NULL
) {
2277 v9fs_string_sprintf(&fullname
, "%s/%s", dfidp
->path
.data
, name
.data
);
2278 err
= v9fs_co_link(s
, &oldfidp
->path
, &fullname
);
2282 v9fs_string_free(&fullname
);
2287 v9fs_string_free(&name
);
2288 complete_pdu(s
, pdu
, err
);
2291 static void v9fs_remove(void *opaque
)
2297 V9fsPDU
*pdu
= opaque
;
2299 pdu_unmarshal(pdu
, offset
, "d", &fid
);
2301 fidp
= get_fid(pdu
->s
, fid
);
2307 * IF the file is unlinked, we cannot reopen
2308 * the file later. So don't reclaim fd
2310 err
= v9fs_mark_fids_unreclaim(pdu
->s
, &fidp
->path
);
2314 err
= v9fs_co_remove(pdu
->s
, &fidp
->path
);
2319 /* For TREMOVE we need to clunk the fid even on failed remove */
2320 clunk_fid(pdu
->s
, fidp
->fid
);
2321 put_fid(pdu
->s
, fidp
);
2323 complete_pdu(pdu
->s
, pdu
, err
);
2326 static void v9fs_unlinkat(void *opaque
)
2330 int32_t dfid
, flags
;
2332 V9fsFidState
*dfidp
;
2333 V9fsPDU
*pdu
= opaque
;
2334 V9fsString full_name
;
2336 pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2338 dfidp
= get_fid(pdu
->s
, dfid
);
2339 if (dfidp
== NULL
) {
2343 v9fs_string_init(&full_name
);
2344 v9fs_string_sprintf(&full_name
, "%s/%s", dfidp
->path
.data
, name
.data
);
2346 * IF the file is unlinked, we cannot reopen
2347 * the file later. So don't reclaim fd
2349 err
= v9fs_mark_fids_unreclaim(pdu
->s
, &full_name
);
2353 err
= v9fs_co_remove(pdu
->s
, &full_name
);
2358 put_fid(pdu
->s
, dfidp
);
2359 v9fs_string_free(&full_name
);
2361 complete_pdu(pdu
->s
, pdu
, err
);
2362 v9fs_string_free(&name
);
2365 static int v9fs_complete_rename(V9fsState
*s
, V9fsFidState
*fidp
,
2366 int32_t newdirfid
, V9fsString
*name
)
2370 V9fsFidState
*dirfidp
= NULL
;
2371 char *old_name
, *new_name
;
2373 if (newdirfid
!= -1) {
2374 dirfidp
= get_fid(s
, newdirfid
);
2375 if (dirfidp
== NULL
) {
2379 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2381 new_name
= g_malloc0(dirfidp
->path
.size
+ name
->size
+ 2);
2383 strcpy(new_name
, dirfidp
->path
.data
);
2384 strcat(new_name
, "/");
2385 strcat(new_name
+ dirfidp
->path
.size
, name
->data
);
2387 old_name
= fidp
->path
.data
;
2388 end
= strrchr(old_name
, '/');
2394 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2396 strncat(new_name
, old_name
, end
- old_name
);
2397 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2400 v9fs_string_free(name
);
2401 name
->data
= new_name
;
2402 name
->size
= strlen(new_name
);
2404 if (strcmp(new_name
, fidp
->path
.data
) != 0) {
2405 err
= v9fs_co_rename(s
, &fidp
->path
, name
);
2409 V9fsFidState
*tfidp
;
2411 * Fixup fid's pointing to the old name to
2412 * start pointing to the new name
2414 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2415 if (fidp
== tfidp
) {
2417 * we replace name of this fid towards the end
2418 * so that our below strcmp will work
2422 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2423 /* replace the name */
2424 v9fs_fix_path(&tfidp
->path
, name
, strlen(fidp
->path
.data
));
2427 v9fs_string_copy(&fidp
->path
, name
);
2431 put_fid(s
, dirfidp
);
2437 static void v9fs_rename(void *opaque
)
2445 V9fsPDU
*pdu
= opaque
;
2446 V9fsState
*s
= pdu
->s
;
2448 pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2450 fidp
= get_fid(s
, fid
);
2455 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
2457 qemu_co_rwlock_wrlock(&s
->rename_lock
);
2458 err
= v9fs_complete_rename(s
, fidp
, newdirfid
, &name
);
2459 qemu_co_rwlock_unlock(&s
->rename_lock
);
2465 complete_pdu(s
, pdu
, err
);
2466 v9fs_string_free(&name
);
2469 static int v9fs_complete_renameat(V9fsState
*s
, int32_t olddirfid
,
2470 V9fsString
*old_name
, int32_t newdirfid
,
2471 V9fsString
*new_name
)
2474 V9fsString old_full_name
, new_full_name
;
2475 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2477 olddirfidp
= get_fid(s
, olddirfid
);
2478 if (olddirfidp
== NULL
) {
2482 v9fs_string_init(&old_full_name
);
2483 v9fs_string_init(&new_full_name
);
2485 v9fs_string_sprintf(&old_full_name
, "%s/%s",
2486 olddirfidp
->path
.data
, old_name
->data
);
2487 if (newdirfid
!= -1) {
2488 newdirfidp
= get_fid(s
, newdirfid
);
2489 if (newdirfidp
== NULL
) {
2493 v9fs_string_sprintf(&new_full_name
, "%s/%s",
2494 newdirfidp
->path
.data
, new_name
->data
);
2496 v9fs_string_sprintf(&new_full_name
, "%s/%s",
2497 olddirfidp
->path
.data
, new_name
->data
);
2500 if (strcmp(old_full_name
.data
, new_full_name
.data
) != 0) {
2501 V9fsFidState
*tfidp
;
2502 err
= v9fs_co_rename(s
, &old_full_name
, &new_full_name
);
2507 * Fixup fid's pointing to the old name to
2508 * start pointing to the new name
2510 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2511 if (v9fs_path_is_ancestor(&old_full_name
, &tfidp
->path
)) {
2512 /* replace the name */
2513 v9fs_fix_path(&tfidp
->path
, &new_full_name
, old_full_name
.size
);
2519 put_fid(s
, olddirfidp
);
2522 put_fid(s
, newdirfidp
);
2524 v9fs_string_free(&old_full_name
);
2525 v9fs_string_free(&new_full_name
);
2529 static void v9fs_renameat(void *opaque
)
2533 V9fsPDU
*pdu
= opaque
;
2534 V9fsState
*s
= pdu
->s
;
2535 int32_t olddirfid
, newdirfid
;
2536 V9fsString old_name
, new_name
;
2538 pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2539 &old_name
, &newdirfid
, &new_name
);
2541 qemu_co_rwlock_wrlock(&s
->rename_lock
);
2542 err
= v9fs_complete_renameat(s
, olddirfid
, &old_name
, newdirfid
, &new_name
);
2543 qemu_co_rwlock_unlock(&s
->rename_lock
);
2547 complete_pdu(s
, pdu
, err
);
2548 v9fs_string_free(&old_name
);
2549 v9fs_string_free(&new_name
);
2552 static void v9fs_wstat(void *opaque
)
2561 V9fsPDU
*pdu
= opaque
;
2562 V9fsState
*s
= pdu
->s
;
2564 pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2566 fidp
= get_fid(s
, fid
);
2571 /* do we need to sync the file? */
2572 if (donttouch_stat(&v9stat
)) {
2573 err
= v9fs_co_fsync(s
, fidp
, 0);
2576 if (v9stat
.mode
!= -1) {
2578 err
= v9fs_co_lstat(s
, &fidp
->path
, &stbuf
);
2582 v9_mode
= stat_to_v9mode(&stbuf
);
2583 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2584 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2585 /* Attempting to change the type */
2589 err
= v9fs_co_chmod(s
, &fidp
->path
,
2590 v9mode_to_mode(v9stat
.mode
,
2591 &v9stat
.extension
));
2596 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2597 struct timespec times
[2];
2598 if (v9stat
.atime
!= -1) {
2599 times
[0].tv_sec
= v9stat
.atime
;
2600 times
[0].tv_nsec
= 0;
2602 times
[0].tv_nsec
= UTIME_OMIT
;
2604 if (v9stat
.mtime
!= -1) {
2605 times
[1].tv_sec
= v9stat
.mtime
;
2606 times
[1].tv_nsec
= 0;
2608 times
[1].tv_nsec
= UTIME_OMIT
;
2610 err
= v9fs_co_utimensat(s
, &fidp
->path
, times
);
2615 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2616 err
= v9fs_co_chown(s
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2621 if (v9stat
.name
.size
!= 0) {
2622 err
= v9fs_complete_rename(s
, fidp
, -1, &v9stat
.name
);
2627 if (v9stat
.length
!= -1) {
2628 err
= v9fs_co_truncate(s
, &fidp
->path
, v9stat
.length
);
2637 v9fs_stat_free(&v9stat
);
2638 complete_pdu(s
, pdu
, err
);
2641 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2653 int32_t bsize_factor
;
2656 * compute bsize factor based on host file system block size
2659 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2660 if (!bsize_factor
) {
2663 f_type
= stbuf
->f_type
;
2664 f_bsize
= stbuf
->f_bsize
;
2665 f_bsize
*= bsize_factor
;
2667 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2668 * adjust(divide) the number of blocks, free blocks and available
2669 * blocks by bsize factor
2671 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2672 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2673 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2674 f_files
= stbuf
->f_files
;
2675 f_ffree
= stbuf
->f_ffree
;
2676 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2677 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2678 f_namelen
= stbuf
->f_namelen
;
2680 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2681 f_type
, f_bsize
, f_blocks
, f_bfree
,
2682 f_bavail
, f_files
, f_ffree
,
2683 fsid_val
, f_namelen
);
2686 static void v9fs_statfs(void *opaque
)
2692 struct statfs stbuf
;
2693 V9fsPDU
*pdu
= opaque
;
2694 V9fsState
*s
= pdu
->s
;
2696 pdu_unmarshal(pdu
, offset
, "d", &fid
);
2697 fidp
= get_fid(s
, fid
);
2702 retval
= v9fs_co_statfs(s
, &fidp
->path
, &stbuf
);
2707 retval
+= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2711 complete_pdu(s
, pdu
, retval
);
2715 static void v9fs_mknod(void *opaque
)
2728 V9fsPDU
*pdu
= opaque
;
2729 V9fsState
*s
= pdu
->s
;
2731 pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2732 &major
, &minor
, &gid
);
2734 fidp
= get_fid(s
, fid
);
2739 err
= v9fs_co_mknod(s
, fidp
, &name
, fidp
->uid
, gid
,
2740 makedev(major
, minor
), mode
, &stbuf
);
2744 stat_to_qid(&stbuf
, &qid
);
2746 err
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
2750 complete_pdu(s
, pdu
, err
);
2751 v9fs_string_free(&name
);
2755 * Implement posix byte range locking code
2756 * Server side handling of locking code is very simple, because 9p server in
2757 * QEMU can handle only one client. And most of the lock handling
2758 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2759 * do any thing in * qemu 9p server side lock code path.
2760 * So when a TLOCK request comes, always return success
2762 static void v9fs_lock(void *opaque
)
2769 int32_t fid
, err
= 0;
2770 V9fsPDU
*pdu
= opaque
;
2771 V9fsState
*s
= pdu
->s
;
2773 flock
= g_malloc(sizeof(*flock
));
2774 pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
->type
,
2775 &flock
->flags
, &flock
->start
, &flock
->length
,
2776 &flock
->proc_id
, &flock
->client_id
);
2777 status
= P9_LOCK_ERROR
;
2779 /* We support only block flag now (that too ignored currently) */
2780 if (flock
->flags
& ~P9_LOCK_FLAGS_BLOCK
) {
2784 fidp
= get_fid(s
, fid
);
2789 err
= v9fs_co_fstat(s
, fidp
->fs
.fd
, &stbuf
);
2793 status
= P9_LOCK_SUCCESS
;
2798 err
+= pdu_marshal(pdu
, offset
, "b", status
);
2799 complete_pdu(s
, pdu
, err
);
2800 v9fs_string_free(&flock
->client_id
);
2805 * When a TGETLOCK request comes, always return success because all lock
2806 * handling is done by client's VFS layer.
2808 static void v9fs_getlock(void *opaque
)
2814 int32_t fid
, err
= 0;
2815 V9fsPDU
*pdu
= opaque
;
2816 V9fsState
*s
= pdu
->s
;
2818 glock
= g_malloc(sizeof(*glock
));
2819 pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
->type
,
2820 &glock
->start
, &glock
->length
, &glock
->proc_id
,
2823 fidp
= get_fid(s
, fid
);
2828 err
= v9fs_co_fstat(s
, fidp
->fs
.fd
, &stbuf
);
2832 glock
->type
= F_UNLCK
;
2833 offset
+= pdu_marshal(pdu
, offset
, "bqqds", glock
->type
,
2834 glock
->start
, glock
->length
, glock
->proc_id
,
2840 complete_pdu(s
, pdu
, err
);
2841 v9fs_string_free(&glock
->client_id
);
2845 static void v9fs_mkdir(void *opaque
)
2847 V9fsPDU
*pdu
= opaque
;
2858 pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
2860 fidp
= get_fid(pdu
->s
, fid
);
2865 err
= v9fs_co_mkdir(pdu
->s
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
2869 stat_to_qid(&stbuf
, &qid
);
2870 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
2873 put_fid(pdu
->s
, fidp
);
2875 complete_pdu(pdu
->s
, pdu
, err
);
2876 v9fs_string_free(&name
);
2879 static void v9fs_xattrwalk(void *opaque
)
2885 int32_t fid
, newfid
;
2886 V9fsFidState
*file_fidp
;
2887 V9fsFidState
*xattr_fidp
= NULL
;
2888 V9fsPDU
*pdu
= opaque
;
2889 V9fsState
*s
= pdu
->s
;
2891 pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
2892 file_fidp
= get_fid(s
, fid
);
2893 if (file_fidp
== NULL
) {
2897 xattr_fidp
= alloc_fid(s
, newfid
);
2898 if (xattr_fidp
== NULL
) {
2902 v9fs_string_copy(&xattr_fidp
->path
, &file_fidp
->path
);
2903 if (name
.data
[0] == 0) {
2905 * listxattr request. Get the size first
2907 size
= v9fs_co_llistxattr(s
, &xattr_fidp
->path
, NULL
, 0);
2910 clunk_fid(s
, xattr_fidp
->fid
);
2914 * Read the xattr value
2916 xattr_fidp
->fs
.xattr
.len
= size
;
2917 xattr_fidp
->fid_type
= P9_FID_XATTR
;
2918 xattr_fidp
->fs
.xattr
.copied_len
= -1;
2920 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
2921 err
= v9fs_co_llistxattr(s
, &xattr_fidp
->path
,
2922 xattr_fidp
->fs
.xattr
.value
,
2923 xattr_fidp
->fs
.xattr
.len
);
2925 clunk_fid(s
, xattr_fidp
->fid
);
2929 offset
+= pdu_marshal(pdu
, offset
, "q", size
);
2933 * specific xattr fid. We check for xattr
2934 * presence also collect the xattr size
2936 size
= v9fs_co_lgetxattr(s
, &xattr_fidp
->path
,
2940 clunk_fid(s
, xattr_fidp
->fid
);
2944 * Read the xattr value
2946 xattr_fidp
->fs
.xattr
.len
= size
;
2947 xattr_fidp
->fid_type
= P9_FID_XATTR
;
2948 xattr_fidp
->fs
.xattr
.copied_len
= -1;
2950 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
2951 err
= v9fs_co_lgetxattr(s
, &xattr_fidp
->path
,
2952 &name
, xattr_fidp
->fs
.xattr
.value
,
2953 xattr_fidp
->fs
.xattr
.len
);
2955 clunk_fid(s
, xattr_fidp
->fid
);
2959 offset
+= pdu_marshal(pdu
, offset
, "q", size
);
2963 put_fid(s
, file_fidp
);
2965 put_fid(s
, xattr_fidp
);
2968 complete_pdu(s
, pdu
, err
);
2969 v9fs_string_free(&name
);
2972 static void v9fs_xattrcreate(void *opaque
)
2980 V9fsFidState
*file_fidp
;
2981 V9fsFidState
*xattr_fidp
;
2982 V9fsPDU
*pdu
= opaque
;
2983 V9fsState
*s
= pdu
->s
;
2985 pdu_unmarshal(pdu
, offset
, "dsqd",
2986 &fid
, &name
, &size
, &flags
);
2988 file_fidp
= get_fid(s
, fid
);
2989 if (file_fidp
== NULL
) {
2993 /* Make the file fid point to xattr */
2994 xattr_fidp
= file_fidp
;
2995 xattr_fidp
->fid_type
= P9_FID_XATTR
;
2996 xattr_fidp
->fs
.xattr
.copied_len
= 0;
2997 xattr_fidp
->fs
.xattr
.len
= size
;
2998 xattr_fidp
->fs
.xattr
.flags
= flags
;
2999 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3000 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3002 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3004 xattr_fidp
->fs
.xattr
.value
= NULL
;
3007 put_fid(s
, file_fidp
);
3009 complete_pdu(s
, pdu
, err
);
3010 v9fs_string_free(&name
);
3013 static void v9fs_readlink(void *opaque
)
3015 V9fsPDU
*pdu
= opaque
;
3022 pdu_unmarshal(pdu
, offset
, "d", &fid
);
3023 fidp
= get_fid(pdu
->s
, fid
);
3029 v9fs_string_init(&target
);
3030 err
= v9fs_co_readlink(pdu
->s
, &fidp
->path
, &target
);
3034 offset
+= pdu_marshal(pdu
, offset
, "s", &target
);
3036 v9fs_string_free(&target
);
3038 put_fid(pdu
->s
, fidp
);
3040 complete_pdu(pdu
->s
, pdu
, err
);
3043 static CoroutineEntry
*pdu_co_handlers
[] = {
3044 [P9_TREADDIR
] = v9fs_readdir
,
3045 [P9_TSTATFS
] = v9fs_statfs
,
3046 [P9_TGETATTR
] = v9fs_getattr
,
3047 [P9_TSETATTR
] = v9fs_setattr
,
3048 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3049 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3050 [P9_TMKNOD
] = v9fs_mknod
,
3051 [P9_TRENAME
] = v9fs_rename
,
3052 [P9_TLOCK
] = v9fs_lock
,
3053 [P9_TGETLOCK
] = v9fs_getlock
,
3054 [P9_TRENAMEAT
] = v9fs_renameat
,
3055 [P9_TREADLINK
] = v9fs_readlink
,
3056 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3057 [P9_TMKDIR
] = v9fs_mkdir
,
3058 [P9_TVERSION
] = v9fs_version
,
3059 [P9_TLOPEN
] = v9fs_open
,
3060 [P9_TATTACH
] = v9fs_attach
,
3061 [P9_TSTAT
] = v9fs_stat
,
3062 [P9_TWALK
] = v9fs_walk
,
3063 [P9_TCLUNK
] = v9fs_clunk
,
3064 [P9_TFSYNC
] = v9fs_fsync
,
3065 [P9_TOPEN
] = v9fs_open
,
3066 [P9_TREAD
] = v9fs_read
,
3068 [P9_TAUTH
] = v9fs_auth
,
3070 [P9_TFLUSH
] = v9fs_flush
,
3071 [P9_TLINK
] = v9fs_link
,
3072 [P9_TSYMLINK
] = v9fs_symlink
,
3073 [P9_TCREATE
] = v9fs_create
,
3074 [P9_TLCREATE
] = v9fs_lcreate
,
3075 [P9_TWRITE
] = v9fs_write
,
3076 [P9_TWSTAT
] = v9fs_wstat
,
3077 [P9_TREMOVE
] = v9fs_remove
,
3080 static void v9fs_op_not_supp(void *opaque
)
3082 V9fsPDU
*pdu
= opaque
;
3083 complete_pdu(pdu
->s
, pdu
, -EOPNOTSUPP
);
3086 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
3089 CoroutineEntry
*handler
;
3094 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3095 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3096 handler
= v9fs_op_not_supp
;
3098 handler
= pdu_co_handlers
[pdu
->id
];
3100 co
= qemu_coroutine_create(handler
);
3101 qemu_coroutine_enter(co
, pdu
);
3104 void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
3106 V9fsState
*s
= (V9fsState
*)vdev
;
3110 while ((pdu
= alloc_pdu(s
)) &&
3111 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
3114 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
3115 BUG_ON(pdu
->elem
.out_sg
[0].iov_len
< 7);
3117 ptr
= pdu
->elem
.out_sg
[0].iov_base
;
3119 memcpy(&pdu
->size
, ptr
, 4);
3121 memcpy(&pdu
->tag
, ptr
+ 5, 2);
3127 void virtio_9p_set_fd_limit(void)
3130 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3131 fprintf(stderr
, "Failed to get the resource limit\n");
3134 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3135 open_fd_rc
= rlim
.rlim_cur
/2;