]>
git.proxmox.com Git - qemu.git/blob - hw/9pfs/virtio-9p.c
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
);
217 void v9fs_path_init(V9fsPath
*path
)
223 void v9fs_path_free(V9fsPath
*path
)
230 void v9fs_path_copy(V9fsPath
*lhs
, V9fsPath
*rhs
)
233 lhs
->data
= g_malloc(rhs
->size
);
234 memcpy(lhs
->data
, rhs
->data
, rhs
->size
);
235 lhs
->size
= rhs
->size
;
238 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
239 const char *name
, V9fsPath
*path
)
242 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
250 * Return TRUE if s1 is an ancestor of s2.
252 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
253 * As a special case, We treat s1 as ancestor of s2 if they are same!
255 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
257 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
258 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
265 static size_t v9fs_string_size(V9fsString
*str
)
271 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
272 static int v9fs_reopen_fid(V9fsState
*s
, V9fsFidState
*f
)
275 if (f
->fid_type
== P9_FID_FILE
) {
276 if (f
->fs
.fd
== -1) {
278 err
= v9fs_co_open(s
, f
, f
->open_flags
);
279 } while (err
== -EINTR
);
281 } else if (f
->fid_type
== P9_FID_DIR
) {
282 if (f
->fs
.dir
== NULL
) {
284 err
= v9fs_co_opendir(s
, f
);
285 } while (err
== -EINTR
);
291 static V9fsFidState
*get_fid(V9fsState
*s
, int32_t fid
)
296 for (f
= s
->fid_list
; f
; f
= f
->next
) {
300 * Update the fid ref upfront so that
301 * we don't get reclaimed when we yield
306 * check whether we need to reopen the
307 * file. We might have closed the fd
308 * while trying to free up some file
311 err
= v9fs_reopen_fid(s
, f
);
317 * Mark the fid as referenced so that the LRU
318 * reclaim won't close the file descriptor
320 f
->flags
|= FID_REFERENCED
;
327 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
331 for (f
= s
->fid_list
; f
; f
= f
->next
) {
332 /* If fid is already there return NULL */
338 f
= g_malloc0(sizeof(V9fsFidState
));
340 f
->fid_type
= P9_FID_NONE
;
343 * Mark the fid as referenced so that the LRU
344 * reclaim won't close the file descriptor
346 f
->flags
|= FID_REFERENCED
;
347 f
->next
= s
->fid_list
;
353 static int v9fs_xattr_fid_clunk(V9fsState
*s
, V9fsFidState
*fidp
)
357 if (fidp
->fs
.xattr
.copied_len
== -1) {
358 /* getxattr/listxattr fid */
362 * if this is fid for setxattr. clunk should
363 * result in setxattr localcall
365 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
366 /* clunk after partial write */
370 if (fidp
->fs
.xattr
.len
) {
371 retval
= v9fs_co_lsetxattr(s
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
372 fidp
->fs
.xattr
.value
,
374 fidp
->fs
.xattr
.flags
);
376 retval
= v9fs_co_lremovexattr(s
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
379 v9fs_string_free(&fidp
->fs
.xattr
.name
);
381 if (fidp
->fs
.xattr
.value
) {
382 g_free(fidp
->fs
.xattr
.value
);
387 static int free_fid(V9fsState
*s
, V9fsFidState
*fidp
)
391 if (fidp
->fid_type
== P9_FID_FILE
) {
392 /* If we reclaimed the fd no need to close */
393 if (fidp
->fs
.fd
!= -1) {
394 retval
= v9fs_co_close(s
, fidp
->fs
.fd
);
396 } else if (fidp
->fid_type
== P9_FID_DIR
) {
397 if (fidp
->fs
.dir
!= NULL
) {
398 retval
= v9fs_co_closedir(s
, fidp
->fs
.dir
);
400 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
401 retval
= v9fs_xattr_fid_clunk(s
, fidp
);
403 v9fs_path_free(&fidp
->path
);
408 static void put_fid(V9fsState
*s
, V9fsFidState
*fidp
)
413 * Don't free the fid if it is in reclaim list
415 if (!fidp
->ref
&& fidp
->clunked
) {
420 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
422 V9fsFidState
**fidpp
, *fidp
;
424 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
425 if ((*fidpp
)->fid
== fid
) {
429 if (*fidpp
== NULL
) {
438 void v9fs_reclaim_fd(V9fsState
*s
)
440 int reclaim_count
= 0;
441 V9fsFidState
*f
, *reclaim_list
= NULL
;
443 for (f
= s
->fid_list
; f
; f
= f
->next
) {
445 * Unlink fids cannot be reclaimed. Check
446 * for them and skip them. Also skip fids
447 * currently being operated on.
449 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
453 * if it is a recently referenced fid
454 * we leave the fid untouched and clear the
455 * reference bit. We come back to it later
456 * in the next iteration. (a simple LRU without
457 * moving list elements around)
459 if (f
->flags
& FID_REFERENCED
) {
460 f
->flags
&= ~FID_REFERENCED
;
464 * Add fids to reclaim list.
466 if (f
->fid_type
== P9_FID_FILE
) {
467 if (f
->fs
.fd
!= -1) {
469 * Up the reference count so that
470 * a clunk request won't free this fid
473 f
->rclm_lst
= reclaim_list
;
475 f
->fs_reclaim
.fd
= f
->fs
.fd
;
479 } else if (f
->fid_type
== P9_FID_DIR
) {
480 if (f
->fs
.dir
!= NULL
) {
482 * Up the reference count so that
483 * a clunk request won't free this fid
486 f
->rclm_lst
= reclaim_list
;
488 f
->fs_reclaim
.dir
= f
->fs
.dir
;
493 if (reclaim_count
>= open_fd_rc
) {
498 * Now close the fid in reclaim list. Free them if they
499 * are already clunked.
501 while (reclaim_list
) {
503 reclaim_list
= f
->rclm_lst
;
504 if (f
->fid_type
== P9_FID_FILE
) {
505 v9fs_co_close(s
, f
->fs_reclaim
.fd
);
506 } else if (f
->fid_type
== P9_FID_DIR
) {
507 v9fs_co_closedir(s
, f
->fs_reclaim
.dir
);
511 * Now drop the fid reference, free it
518 static int v9fs_mark_fids_unreclaim(V9fsState
*s
, V9fsPath
*path
)
521 V9fsFidState
*fidp
, head_fid
;
523 head_fid
.next
= s
->fid_list
;
524 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
525 if (fidp
->path
.size
!= path
->size
) {
528 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
529 /* Mark the fid non reclaimable. */
530 fidp
->flags
|= FID_NON_RECLAIMABLE
;
532 /* reopen the file/dir if already closed */
533 err
= v9fs_reopen_fid(s
, fidp
);
538 * Go back to head of fid list because
539 * the list could have got updated when
540 * switched to the worker thread
550 #define P9_QID_TYPE_DIR 0x80
551 #define P9_QID_TYPE_SYMLINK 0x02
553 #define P9_STAT_MODE_DIR 0x80000000
554 #define P9_STAT_MODE_APPEND 0x40000000
555 #define P9_STAT_MODE_EXCL 0x20000000
556 #define P9_STAT_MODE_MOUNT 0x10000000
557 #define P9_STAT_MODE_AUTH 0x08000000
558 #define P9_STAT_MODE_TMP 0x04000000
559 #define P9_STAT_MODE_SYMLINK 0x02000000
560 #define P9_STAT_MODE_LINK 0x01000000
561 #define P9_STAT_MODE_DEVICE 0x00800000
562 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
563 #define P9_STAT_MODE_SOCKET 0x00100000
564 #define P9_STAT_MODE_SETUID 0x00080000
565 #define P9_STAT_MODE_SETGID 0x00040000
566 #define P9_STAT_MODE_SETVTX 0x00010000
568 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
569 P9_STAT_MODE_SYMLINK | \
570 P9_STAT_MODE_LINK | \
571 P9_STAT_MODE_DEVICE | \
572 P9_STAT_MODE_NAMED_PIPE | \
575 /* This is the algorithm from ufs in spfs */
576 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
580 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
581 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
582 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
583 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
585 if (S_ISDIR(stbuf
->st_mode
)) {
586 qidp
->type
|= P9_QID_TYPE_DIR
;
588 if (S_ISLNK(stbuf
->st_mode
)) {
589 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
593 static int fid_to_qid(V9fsState
*s
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
598 err
= v9fs_co_lstat(s
, &fidp
->path
, &stbuf
);
602 stat_to_qid(&stbuf
, qidp
);
606 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
610 if (!QLIST_EMPTY(&s
->free_list
)) {
611 pdu
= QLIST_FIRST(&s
->free_list
);
612 QLIST_REMOVE(pdu
, next
);
617 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
623 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
627 size_t pdu_packunpack(void *addr
, struct iovec
*sg
, int sg_count
,
628 size_t offset
, size_t size
, int pack
)
633 for (i
= 0; size
&& i
< sg_count
; i
++) {
635 if (offset
>= sg
[i
].iov_len
) {
637 offset
-= sg
[i
].iov_len
;
640 len
= MIN(sg
[i
].iov_len
- offset
, size
);
642 memcpy(sg
[i
].iov_base
+ offset
, addr
, len
);
644 memcpy(addr
, sg
[i
].iov_base
+ offset
, len
);
659 static size_t pdu_unpack(void *dst
, V9fsPDU
*pdu
, size_t offset
, size_t size
)
661 return pdu_packunpack(dst
, pdu
->elem
.out_sg
, pdu
->elem
.out_num
,
665 static size_t pdu_pack(V9fsPDU
*pdu
, size_t offset
, const void *src
,
668 return pdu_packunpack((void *)src
, pdu
->elem
.in_sg
, pdu
->elem
.in_num
,
672 static int pdu_copy_sg(V9fsPDU
*pdu
, size_t offset
, int rx
, struct iovec
*sg
)
676 struct iovec
*src_sg
;
680 src_sg
= pdu
->elem
.in_sg
;
681 num
= pdu
->elem
.in_num
;
683 src_sg
= pdu
->elem
.out_sg
;
684 num
= pdu
->elem
.out_num
;
688 for (i
= 0; i
< num
; i
++) {
690 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
691 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
693 } else if (offset
< (src_sg
[i
].iov_len
+ pos
)) {
694 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
695 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
696 sg
[j
].iov_base
+= (offset
- pos
);
697 sg
[j
].iov_len
-= (offset
- pos
);
700 pos
+= src_sg
[i
].iov_len
;
706 static size_t pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
708 size_t old_offset
= offset
;
713 for (i
= 0; fmt
[i
]; i
++) {
716 uint8_t *valp
= va_arg(ap
, uint8_t *);
717 offset
+= pdu_unpack(valp
, pdu
, offset
, sizeof(*valp
));
722 valp
= va_arg(ap
, uint16_t *);
723 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
724 *valp
= le16_to_cpu(val
);
729 valp
= va_arg(ap
, uint32_t *);
730 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
731 *valp
= le32_to_cpu(val
);
736 valp
= va_arg(ap
, uint64_t *);
737 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
738 *valp
= le64_to_cpu(val
);
742 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
743 int *iovcnt
= va_arg(ap
, int *);
744 *iovcnt
= pdu_copy_sg(pdu
, offset
, 0, iov
);
748 V9fsString
*str
= va_arg(ap
, V9fsString
*);
749 offset
+= pdu_unmarshal(pdu
, offset
, "w", &str
->size
);
750 /* FIXME: sanity check str->size */
751 str
->data
= g_malloc(str
->size
+ 1);
752 offset
+= pdu_unpack(str
->data
, pdu
, offset
, str
->size
);
753 str
->data
[str
->size
] = 0;
757 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
758 offset
+= pdu_unmarshal(pdu
, offset
, "bdq",
759 &qidp
->type
, &qidp
->version
, &qidp
->path
);
763 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
764 offset
+= pdu_unmarshal(pdu
, offset
, "wwdQdddqsssssddd",
765 &statp
->size
, &statp
->type
, &statp
->dev
,
766 &statp
->qid
, &statp
->mode
, &statp
->atime
,
767 &statp
->mtime
, &statp
->length
,
768 &statp
->name
, &statp
->uid
, &statp
->gid
,
769 &statp
->muid
, &statp
->extension
,
770 &statp
->n_uid
, &statp
->n_gid
,
775 V9fsIattr
*iattr
= va_arg(ap
, V9fsIattr
*);
776 offset
+= pdu_unmarshal(pdu
, offset
, "ddddqqqqq",
777 &iattr
->valid
, &iattr
->mode
,
778 &iattr
->uid
, &iattr
->gid
, &iattr
->size
,
779 &iattr
->atime_sec
, &iattr
->atime_nsec
,
780 &iattr
->mtime_sec
, &iattr
->mtime_nsec
);
790 return offset
- old_offset
;
793 static size_t pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
795 size_t old_offset
= offset
;
800 for (i
= 0; fmt
[i
]; i
++) {
803 uint8_t val
= va_arg(ap
, int);
804 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
809 cpu_to_le16w(&val
, va_arg(ap
, int));
810 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
815 cpu_to_le32w(&val
, va_arg(ap
, uint32_t));
816 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
821 cpu_to_le64w(&val
, va_arg(ap
, uint64_t));
822 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
826 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
827 int *iovcnt
= va_arg(ap
, int *);
828 *iovcnt
= pdu_copy_sg(pdu
, offset
, 1, iov
);
832 V9fsString
*str
= va_arg(ap
, V9fsString
*);
833 offset
+= pdu_marshal(pdu
, offset
, "w", str
->size
);
834 offset
+= pdu_pack(pdu
, offset
, str
->data
, str
->size
);
838 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
839 offset
+= pdu_marshal(pdu
, offset
, "bdq",
840 qidp
->type
, qidp
->version
, qidp
->path
);
844 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
845 offset
+= pdu_marshal(pdu
, offset
, "wwdQdddqsssssddd",
846 statp
->size
, statp
->type
, statp
->dev
,
847 &statp
->qid
, statp
->mode
, statp
->atime
,
848 statp
->mtime
, statp
->length
, &statp
->name
,
849 &statp
->uid
, &statp
->gid
, &statp
->muid
,
850 &statp
->extension
, statp
->n_uid
,
851 statp
->n_gid
, statp
->n_muid
);
855 V9fsStatDotl
*statp
= va_arg(ap
, V9fsStatDotl
*);
856 offset
+= pdu_marshal(pdu
, offset
, "qQdddqqqqqqqqqqqqqqq",
857 statp
->st_result_mask
,
858 &statp
->qid
, statp
->st_mode
,
859 statp
->st_uid
, statp
->st_gid
,
860 statp
->st_nlink
, statp
->st_rdev
,
861 statp
->st_size
, statp
->st_blksize
, statp
->st_blocks
,
862 statp
->st_atime_sec
, statp
->st_atime_nsec
,
863 statp
->st_mtime_sec
, statp
->st_mtime_nsec
,
864 statp
->st_ctime_sec
, statp
->st_ctime_nsec
,
865 statp
->st_btime_sec
, statp
->st_btime_nsec
,
866 statp
->st_gen
, statp
->st_data_version
);
875 return offset
- old_offset
;
878 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
880 int8_t id
= pdu
->id
+ 1; /* Response */
886 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
889 str
.data
= strerror(err
);
890 str
.size
= strlen(str
.data
);
892 len
+= pdu_marshal(pdu
, len
, "s", &str
);
896 len
+= pdu_marshal(pdu
, len
, "d", err
);
898 if (s
->proto_version
== V9FS_PROTO_2000L
) {
903 /* fill out the header */
904 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
906 /* keep these in sync */
910 /* push onto queue and notify */
911 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
913 /* FIXME: we should batch these completions */
914 virtio_notify(&s
->vdev
, s
->vq
);
919 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
924 if (mode
& P9_STAT_MODE_DIR
) {
928 if (mode
& P9_STAT_MODE_SYMLINK
) {
931 if (mode
& P9_STAT_MODE_SOCKET
) {
934 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
937 if (mode
& P9_STAT_MODE_DEVICE
) {
938 if (extension
&& extension
->data
[0] == 'c') {
949 if (mode
& P9_STAT_MODE_SETUID
) {
952 if (mode
& P9_STAT_MODE_SETGID
) {
955 if (mode
& P9_STAT_MODE_SETVTX
) {
962 static int donttouch_stat(V9fsStat
*stat
)
964 if (stat
->type
== -1 &&
966 stat
->qid
.type
== -1 &&
967 stat
->qid
.version
== -1 &&
968 stat
->qid
.path
== -1 &&
972 stat
->length
== -1 &&
979 stat
->n_muid
== -1) {
986 static void v9fs_stat_free(V9fsStat
*stat
)
988 v9fs_string_free(&stat
->name
);
989 v9fs_string_free(&stat
->uid
);
990 v9fs_string_free(&stat
->gid
);
991 v9fs_string_free(&stat
->muid
);
992 v9fs_string_free(&stat
->extension
);
995 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
999 mode
= stbuf
->st_mode
& 0777;
1000 if (S_ISDIR(stbuf
->st_mode
)) {
1001 mode
|= P9_STAT_MODE_DIR
;
1004 if (S_ISLNK(stbuf
->st_mode
)) {
1005 mode
|= P9_STAT_MODE_SYMLINK
;
1008 if (S_ISSOCK(stbuf
->st_mode
)) {
1009 mode
|= P9_STAT_MODE_SOCKET
;
1012 if (S_ISFIFO(stbuf
->st_mode
)) {
1013 mode
|= P9_STAT_MODE_NAMED_PIPE
;
1016 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
1017 mode
|= P9_STAT_MODE_DEVICE
;
1020 if (stbuf
->st_mode
& S_ISUID
) {
1021 mode
|= P9_STAT_MODE_SETUID
;
1024 if (stbuf
->st_mode
& S_ISGID
) {
1025 mode
|= P9_STAT_MODE_SETGID
;
1028 if (stbuf
->st_mode
& S_ISVTX
) {
1029 mode
|= P9_STAT_MODE_SETVTX
;
1035 static int stat_to_v9stat(V9fsState
*s
, V9fsPath
*name
,
1036 const struct stat
*stbuf
,
1042 memset(v9stat
, 0, sizeof(*v9stat
));
1044 stat_to_qid(stbuf
, &v9stat
->qid
);
1045 v9stat
->mode
= stat_to_v9mode(stbuf
);
1046 v9stat
->atime
= stbuf
->st_atime
;
1047 v9stat
->mtime
= stbuf
->st_mtime
;
1048 v9stat
->length
= stbuf
->st_size
;
1050 v9fs_string_null(&v9stat
->uid
);
1051 v9fs_string_null(&v9stat
->gid
);
1052 v9fs_string_null(&v9stat
->muid
);
1054 v9stat
->n_uid
= stbuf
->st_uid
;
1055 v9stat
->n_gid
= stbuf
->st_gid
;
1058 v9fs_string_null(&v9stat
->extension
);
1060 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
1061 err
= v9fs_co_readlink(s
, name
, &v9stat
->extension
);
1065 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
1066 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
1067 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
1068 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
1069 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
1070 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
1071 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
1074 str
= strrchr(name
->data
, '/');
1081 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
1084 v9fs_string_size(&v9stat
->name
) +
1085 v9fs_string_size(&v9stat
->uid
) +
1086 v9fs_string_size(&v9stat
->gid
) +
1087 v9fs_string_size(&v9stat
->muid
) +
1088 v9fs_string_size(&v9stat
->extension
);
1092 #define P9_STATS_MODE 0x00000001ULL
1093 #define P9_STATS_NLINK 0x00000002ULL
1094 #define P9_STATS_UID 0x00000004ULL
1095 #define P9_STATS_GID 0x00000008ULL
1096 #define P9_STATS_RDEV 0x00000010ULL
1097 #define P9_STATS_ATIME 0x00000020ULL
1098 #define P9_STATS_MTIME 0x00000040ULL
1099 #define P9_STATS_CTIME 0x00000080ULL
1100 #define P9_STATS_INO 0x00000100ULL
1101 #define P9_STATS_SIZE 0x00000200ULL
1102 #define P9_STATS_BLOCKS 0x00000400ULL
1104 #define P9_STATS_BTIME 0x00000800ULL
1105 #define P9_STATS_GEN 0x00001000ULL
1106 #define P9_STATS_DATA_VERSION 0x00002000ULL
1108 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1109 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1112 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
1113 V9fsStatDotl
*v9lstat
)
1115 memset(v9lstat
, 0, sizeof(*v9lstat
));
1117 v9lstat
->st_mode
= stbuf
->st_mode
;
1118 v9lstat
->st_nlink
= stbuf
->st_nlink
;
1119 v9lstat
->st_uid
= stbuf
->st_uid
;
1120 v9lstat
->st_gid
= stbuf
->st_gid
;
1121 v9lstat
->st_rdev
= stbuf
->st_rdev
;
1122 v9lstat
->st_size
= stbuf
->st_size
;
1123 v9lstat
->st_blksize
= stbuf
->st_blksize
;
1124 v9lstat
->st_blocks
= stbuf
->st_blocks
;
1125 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
1126 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
1127 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
1128 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
1129 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
1130 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
1131 /* Currently we only support BASIC fields in stat */
1132 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
1134 stat_to_qid(stbuf
, &v9lstat
->qid
);
1137 static struct iovec
*adjust_sg(struct iovec
*sg
, int len
, int *iovcnt
)
1139 while (len
&& *iovcnt
) {
1140 if (len
< sg
->iov_len
) {
1142 sg
->iov_base
+= len
;
1154 static struct iovec
*cap_sg(struct iovec
*sg
, int cap
, int *cnt
)
1159 for (i
= 0; i
< *cnt
; i
++) {
1160 if ((total
+ sg
[i
].iov_len
) > cap
) {
1161 sg
[i
].iov_len
-= ((total
+ sg
[i
].iov_len
) - cap
);
1165 total
+= sg
[i
].iov_len
;
1173 static void print_sg(struct iovec
*sg
, int cnt
)
1177 printf("sg[%d]: {", cnt
);
1178 for (i
= 0; i
< cnt
; i
++) {
1182 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
1187 /* Will call this only for path name based fid */
1188 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
1191 v9fs_path_init(&str
);
1192 v9fs_path_copy(&str
, dst
);
1193 v9fs_string_sprintf((V9fsString
*)dst
, "%s%s", src
->data
, str
.data
+len
);
1194 v9fs_path_free(&str
);
1195 /* +1 to include terminating NULL */
1199 static void v9fs_version(void *opaque
)
1201 V9fsPDU
*pdu
= opaque
;
1202 V9fsState
*s
= pdu
->s
;
1206 pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
1208 if (!strcmp(version
.data
, "9P2000.u")) {
1209 s
->proto_version
= V9FS_PROTO_2000U
;
1210 } else if (!strcmp(version
.data
, "9P2000.L")) {
1211 s
->proto_version
= V9FS_PROTO_2000L
;
1213 v9fs_string_sprintf(&version
, "unknown");
1216 offset
+= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
1217 complete_pdu(s
, pdu
, offset
);
1219 v9fs_string_free(&version
);
1223 static void v9fs_attach(void *opaque
)
1225 V9fsPDU
*pdu
= opaque
;
1226 V9fsState
*s
= pdu
->s
;
1227 int32_t fid
, afid
, n_uname
;
1228 V9fsString uname
, aname
;
1234 pdu_unmarshal(pdu
, offset
, "ddssd", &fid
, &afid
, &uname
, &aname
, &n_uname
);
1236 fidp
= alloc_fid(s
, fid
);
1241 fidp
->uid
= n_uname
;
1242 err
= v9fs_co_name_to_path(s
, NULL
, "/", &fidp
->path
);
1248 err
= fid_to_qid(s
, fidp
, &qid
);
1254 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
1259 complete_pdu(s
, pdu
, err
);
1260 v9fs_string_free(&uname
);
1261 v9fs_string_free(&aname
);
1264 static void v9fs_stat(void *opaque
)
1272 V9fsPDU
*pdu
= opaque
;
1273 V9fsState
*s
= pdu
->s
;
1275 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1277 fidp
= get_fid(s
, fid
);
1282 err
= v9fs_co_lstat(s
, &fidp
->path
, &stbuf
);
1286 err
= stat_to_v9stat(s
, &fidp
->path
, &stbuf
, &v9stat
);
1290 offset
+= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1292 v9fs_stat_free(&v9stat
);
1296 complete_pdu(s
, pdu
, err
);
1299 static void v9fs_getattr(void *opaque
)
1306 uint64_t request_mask
;
1307 V9fsStatDotl v9stat_dotl
;
1308 V9fsPDU
*pdu
= opaque
;
1309 V9fsState
*s
= pdu
->s
;
1311 pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1313 fidp
= get_fid(s
, fid
);
1319 * Currently we only support BASIC fields in stat, so there is no
1320 * need to look at request_mask.
1322 retval
= v9fs_co_lstat(s
, &fidp
->path
, &stbuf
);
1326 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1328 retval
+= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1332 complete_pdu(s
, pdu
, retval
);
1335 /* From Linux kernel code */
1336 #define ATTR_MODE (1 << 0)
1337 #define ATTR_UID (1 << 1)
1338 #define ATTR_GID (1 << 2)
1339 #define ATTR_SIZE (1 << 3)
1340 #define ATTR_ATIME (1 << 4)
1341 #define ATTR_MTIME (1 << 5)
1342 #define ATTR_CTIME (1 << 6)
1343 #define ATTR_MASK 127
1344 #define ATTR_ATIME_SET (1 << 7)
1345 #define ATTR_MTIME_SET (1 << 8)
1347 static void v9fs_setattr(void *opaque
)
1354 V9fsPDU
*pdu
= opaque
;
1355 V9fsState
*s
= pdu
->s
;
1357 pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1359 fidp
= get_fid(s
, fid
);
1364 if (v9iattr
.valid
& ATTR_MODE
) {
1365 err
= v9fs_co_chmod(s
, &fidp
->path
, v9iattr
.mode
);
1370 if (v9iattr
.valid
& (ATTR_ATIME
| ATTR_MTIME
)) {
1371 struct timespec times
[2];
1372 if (v9iattr
.valid
& ATTR_ATIME
) {
1373 if (v9iattr
.valid
& ATTR_ATIME_SET
) {
1374 times
[0].tv_sec
= v9iattr
.atime_sec
;
1375 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1377 times
[0].tv_nsec
= UTIME_NOW
;
1380 times
[0].tv_nsec
= UTIME_OMIT
;
1382 if (v9iattr
.valid
& ATTR_MTIME
) {
1383 if (v9iattr
.valid
& ATTR_MTIME_SET
) {
1384 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1385 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1387 times
[1].tv_nsec
= UTIME_NOW
;
1390 times
[1].tv_nsec
= UTIME_OMIT
;
1392 err
= v9fs_co_utimensat(s
, &fidp
->path
, times
);
1398 * If the only valid entry in iattr is ctime we can call
1399 * chown(-1,-1) to update the ctime of the file
1401 if ((v9iattr
.valid
& (ATTR_UID
| ATTR_GID
)) ||
1402 ((v9iattr
.valid
& ATTR_CTIME
)
1403 && !((v9iattr
.valid
& ATTR_MASK
) & ~ATTR_CTIME
))) {
1404 if (!(v9iattr
.valid
& ATTR_UID
)) {
1407 if (!(v9iattr
.valid
& ATTR_GID
)) {
1410 err
= v9fs_co_chown(s
, &fidp
->path
, v9iattr
.uid
,
1416 if (v9iattr
.valid
& (ATTR_SIZE
)) {
1417 err
= v9fs_co_truncate(s
, &fidp
->path
, v9iattr
.size
);
1426 complete_pdu(s
, pdu
, err
);
1429 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1433 offset
+= pdu_marshal(pdu
, offset
, "w", nwnames
);
1434 for (i
= 0; i
< nwnames
; i
++) {
1435 offset
+= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1440 static void v9fs_walk(void *opaque
)
1443 V9fsQID
*qids
= NULL
;
1445 V9fsPath dpath
, path
;
1449 int32_t fid
, newfid
;
1450 V9fsString
*wnames
= NULL
;
1452 V9fsFidState
*newfidp
= NULL
;;
1453 V9fsPDU
*pdu
= opaque
;
1454 V9fsState
*s
= pdu
->s
;
1456 offset
+= pdu_unmarshal(pdu
, offset
, "ddw", &fid
,
1459 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1460 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1461 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1462 for (i
= 0; i
< nwnames
; i
++) {
1463 offset
+= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1465 } else if (nwnames
> P9_MAXWELEM
) {
1469 fidp
= get_fid(s
, fid
);
1474 v9fs_path_init(&dpath
);
1475 v9fs_path_init(&path
);
1477 * Both dpath and path initially poin to fidp.
1478 * Needed to handle request with nwnames == 0
1480 v9fs_path_copy(&dpath
, &fidp
->path
);
1481 v9fs_path_copy(&path
, &fidp
->path
);
1482 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1483 err
= v9fs_co_name_to_path(s
, &dpath
, wnames
[name_idx
].data
, &path
);
1487 err
= v9fs_co_lstat(s
, &path
, &stbuf
);
1491 stat_to_qid(&stbuf
, &qids
[name_idx
]);
1492 v9fs_path_copy(&dpath
, &path
);
1494 if (fid
== newfid
) {
1495 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1496 v9fs_path_copy(&fidp
->path
, &path
);
1498 newfidp
= alloc_fid(s
, newfid
);
1499 if (newfidp
== NULL
) {
1503 newfidp
->uid
= fidp
->uid
;
1504 v9fs_path_copy(&newfidp
->path
, &path
);
1506 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1510 put_fid(s
, newfidp
);
1512 v9fs_path_free(&dpath
);
1513 v9fs_path_free(&path
);
1515 complete_pdu(s
, pdu
, err
);
1516 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1517 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1518 v9fs_string_free(&wnames
[name_idx
]);
1526 static int32_t get_iounit(V9fsState
*s
, V9fsPath
*path
)
1528 struct statfs stbuf
;
1532 * iounit should be multiples of f_bsize (host filesystem block size
1533 * and as well as less than (client msize - P9_IOHDRSZ))
1535 if (!v9fs_co_statfs(s
, path
, &stbuf
)) {
1536 iounit
= stbuf
.f_bsize
;
1537 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1540 iounit
= s
->msize
- P9_IOHDRSZ
;
1545 static void v9fs_open(void *opaque
)
1556 V9fsPDU
*pdu
= opaque
;
1557 V9fsState
*s
= pdu
->s
;
1559 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1560 pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1562 pdu_unmarshal(pdu
, offset
, "db", &fid
, &mode
);
1564 fidp
= get_fid(s
, fid
);
1569 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1571 err
= v9fs_co_lstat(s
, &fidp
->path
, &stbuf
);
1575 stat_to_qid(&stbuf
, &qid
);
1576 if (S_ISDIR(stbuf
.st_mode
)) {
1577 err
= v9fs_co_opendir(s
, fidp
);
1581 fidp
->fid_type
= P9_FID_DIR
;
1582 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1585 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1587 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
1588 /* Ignore direct disk access hint until the server supports it. */
1591 flags
= omode_to_uflags(mode
);
1593 err
= v9fs_co_open(s
, fidp
, flags
);
1597 fidp
->fid_type
= P9_FID_FILE
;
1598 fidp
->open_flags
= flags
;
1599 if (flags
& O_EXCL
) {
1601 * We let the host file system do O_EXCL check
1602 * We should not reclaim such fd
1604 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1606 iounit
= get_iounit(s
, &fidp
->path
);
1607 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1613 complete_pdu(s
, pdu
, err
);
1616 static void v9fs_lcreate(void *opaque
)
1618 int32_t dfid
, flags
, mode
;
1627 V9fsPDU
*pdu
= opaque
;
1629 pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
, &name
, &flags
,
1632 fidp
= get_fid(pdu
->s
, dfid
);
1638 /* Ignore direct disk access hint until the server supports it. */
1641 err
= v9fs_co_open2(pdu
->s
, fidp
, &name
, gid
,
1642 flags
| O_CREAT
, mode
, &stbuf
);
1646 fidp
->fid_type
= P9_FID_FILE
;
1647 fidp
->open_flags
= flags
;
1648 if (flags
& O_EXCL
) {
1650 * We let the host file system do O_EXCL check
1651 * We should not reclaim such fd
1653 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1655 iounit
= get_iounit(pdu
->s
, &fidp
->path
);
1656 stat_to_qid(&stbuf
, &qid
);
1657 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1660 put_fid(pdu
->s
, fidp
);
1662 complete_pdu(pdu
->s
, pdu
, err
);
1663 v9fs_string_free(&name
);
1666 static void v9fs_fsync(void *opaque
)
1673 V9fsPDU
*pdu
= opaque
;
1674 V9fsState
*s
= pdu
->s
;
1676 pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1677 fidp
= get_fid(s
, fid
);
1682 err
= v9fs_co_fsync(s
, fidp
, datasync
);
1688 complete_pdu(s
, pdu
, err
);
1691 static void v9fs_clunk(void *opaque
)
1697 V9fsPDU
*pdu
= opaque
;
1698 V9fsState
*s
= pdu
->s
;
1700 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1702 fidp
= clunk_fid(s
, fid
);
1708 * Bump the ref so that put_fid will
1716 complete_pdu(s
, pdu
, err
);
1719 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
,
1720 V9fsFidState
*fidp
, int64_t off
, int32_t max_count
)
1726 xattr_len
= fidp
->fs
.xattr
.len
;
1727 read_count
= xattr_len
- off
;
1728 if (read_count
> max_count
) {
1729 read_count
= max_count
;
1730 } else if (read_count
< 0) {
1732 * read beyond XATTR value
1736 offset
+= pdu_marshal(pdu
, offset
, "d", read_count
);
1737 offset
+= pdu_pack(pdu
, offset
,
1738 ((char *)fidp
->fs
.xattr
.value
) + off
,
1743 static int v9fs_do_readdir_with_stat(V9fsState
*s
, V9fsPDU
*pdu
,
1744 V9fsFidState
*fidp
, int32_t max_count
)
1751 off_t saved_dir_pos
;
1752 struct dirent
*dent
, *result
;
1754 /* save the directory position */
1755 saved_dir_pos
= v9fs_co_telldir(s
, fidp
);
1756 if (saved_dir_pos
< 0) {
1757 return saved_dir_pos
;
1760 dent
= g_malloc(sizeof(struct dirent
));
1763 v9fs_path_init(&path
);
1764 err
= v9fs_co_readdir_r(s
, fidp
, dent
, &result
);
1765 if (err
|| !result
) {
1768 err
= v9fs_co_name_to_path(s
, &fidp
->path
, dent
->d_name
, &path
);
1772 err
= v9fs_co_lstat(s
, &path
, &stbuf
);
1776 err
= stat_to_v9stat(s
, &path
, &stbuf
, &v9stat
);
1780 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1781 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1782 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1783 /* Ran out of buffer. Set dir back to old position and return */
1784 v9fs_co_seekdir(s
, fidp
, saved_dir_pos
);
1785 v9fs_stat_free(&v9stat
);
1786 v9fs_path_free(&path
);
1791 v9fs_stat_free(&v9stat
);
1792 v9fs_path_free(&path
);
1793 saved_dir_pos
= dent
->d_off
;
1797 v9fs_path_free(&path
);
1804 static void v9fs_read(void *opaque
)
1813 V9fsPDU
*pdu
= opaque
;
1814 V9fsState
*s
= pdu
->s
;
1816 pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1818 fidp
= get_fid(s
, fid
);
1823 if (fidp
->fid_type
== P9_FID_DIR
) {
1826 v9fs_co_rewinddir(s
, fidp
);
1828 count
= v9fs_do_readdir_with_stat(s
, pdu
, fidp
, max_count
);
1834 err
+= pdu_marshal(pdu
, offset
, "d", count
);
1836 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1840 struct iovec iov
[128]; /* FIXME: bad, bad, bad */
1843 pdu_marshal(pdu
, offset
+ 4, "v", sg
, &cnt
);
1844 sg
= cap_sg(sg
, max_count
, &cnt
);
1849 /* Loop in case of EINTR */
1851 len
= v9fs_co_preadv(s
, fidp
, sg
, cnt
, off
);
1856 } while (len
== -EINTR
);
1858 /* IO error return the error */
1862 sg
= adjust_sg(sg
, len
, &cnt
);
1863 } while (count
< max_count
&& len
> 0);
1865 err
+= pdu_marshal(pdu
, offset
, "d", count
);
1867 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1868 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1875 complete_pdu(s
, pdu
, err
);
1878 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1881 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1882 * size of type (1) + size of name.size (2) + strlen(name.data)
1884 return 24 + v9fs_string_size(name
);
1887 static int v9fs_do_readdir(V9fsState
*s
, V9fsPDU
*pdu
,
1888 V9fsFidState
*fidp
, int32_t max_count
)
1895 off_t saved_dir_pos
;
1896 struct dirent
*dent
, *result
;
1898 /* save the directory position */
1899 saved_dir_pos
= v9fs_co_telldir(s
, fidp
);
1900 if (saved_dir_pos
< 0) {
1901 return saved_dir_pos
;
1904 dent
= g_malloc(sizeof(struct dirent
));
1907 err
= v9fs_co_readdir_r(s
, fidp
, dent
, &result
);
1908 if (err
|| !result
) {
1911 v9fs_string_init(&name
);
1912 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1913 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1914 /* Ran out of buffer. Set dir back to old position and return */
1915 v9fs_co_seekdir(s
, fidp
, saved_dir_pos
);
1916 v9fs_string_free(&name
);
1921 * Fill up just the path field of qid because the client uses
1922 * only that. To fill the entire qid structure we will have
1923 * to stat each dirent found, which is expensive
1925 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1926 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1927 /* Fill the other fields with dummy values */
1931 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1932 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1934 dent
->d_type
, &name
);
1936 v9fs_string_free(&name
);
1937 saved_dir_pos
= dent
->d_off
;
1946 static void v9fs_readdir(void *opaque
)
1952 int64_t initial_offset
;
1953 int32_t count
, max_count
;
1954 V9fsPDU
*pdu
= opaque
;
1955 V9fsState
*s
= pdu
->s
;
1957 pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &initial_offset
, &max_count
);
1959 fidp
= get_fid(s
, fid
);
1964 if (!fidp
->fs
.dir
) {
1968 if (initial_offset
== 0) {
1969 v9fs_co_rewinddir(s
, fidp
);
1971 v9fs_co_seekdir(s
, fidp
, initial_offset
);
1973 count
= v9fs_do_readdir(s
, pdu
, fidp
, max_count
);
1979 retval
+= pdu_marshal(pdu
, offset
, "d", count
);
1984 complete_pdu(s
, pdu
, retval
);
1987 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1988 int64_t off
, int32_t count
,
1989 struct iovec
*sg
, int cnt
)
1998 xattr_len
= fidp
->fs
.xattr
.len
;
1999 write_count
= xattr_len
- off
;
2000 if (write_count
> count
) {
2001 write_count
= count
;
2002 } else if (write_count
< 0) {
2004 * write beyond XATTR value len specified in
2010 offset
+= pdu_marshal(pdu
, offset
, "d", write_count
);
2012 fidp
->fs
.xattr
.copied_len
+= write_count
;
2014 * Now copy the content from sg list
2016 for (i
= 0; i
< cnt
; i
++) {
2017 if (write_count
> sg
[i
].iov_len
) {
2018 to_copy
= sg
[i
].iov_len
;
2020 to_copy
= write_count
;
2022 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
2023 /* updating vs->off since we are not using below */
2025 write_count
-= to_copy
;
2031 static void v9fs_write(void *opaque
)
2042 struct iovec iov
[128]; /* FIXME: bad, bad, bad */
2043 struct iovec
*sg
= iov
;
2044 V9fsPDU
*pdu
= opaque
;
2045 V9fsState
*s
= pdu
->s
;
2047 pdu_unmarshal(pdu
, offset
, "dqdv", &fid
, &off
, &count
, sg
, &cnt
);
2049 fidp
= get_fid(s
, fid
);
2054 if (fidp
->fid_type
== P9_FID_FILE
) {
2055 if (fidp
->fs
.fd
== -1) {
2059 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2061 * setxattr operation
2063 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
, sg
, cnt
);
2069 sg
= cap_sg(sg
, count
, &cnt
);
2074 /* Loop in case of EINTR */
2076 len
= v9fs_co_pwritev(s
, fidp
, sg
, cnt
, off
);
2081 } while (len
== -EINTR
);
2083 /* IO error return the error */
2087 sg
= adjust_sg(sg
, len
, &cnt
);
2088 } while (total
< count
&& len
> 0);
2089 offset
+= pdu_marshal(pdu
, offset
, "d", total
);
2094 complete_pdu(s
, pdu
, err
);
2097 static void v9fs_create(void *opaque
)
2109 V9fsString extension
;
2111 V9fsPDU
*pdu
= opaque
;
2113 v9fs_path_init(&path
);
2115 pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2116 &perm
, &mode
, &extension
);
2118 fidp
= get_fid(pdu
->s
, fid
);
2123 if (perm
& P9_STAT_MODE_DIR
) {
2124 err
= v9fs_co_mkdir(pdu
->s
, fidp
, &name
, perm
& 0777,
2125 fidp
->uid
, -1, &stbuf
);
2129 err
= v9fs_co_name_to_path(pdu
->s
, &fidp
->path
, name
.data
, &path
);
2133 v9fs_path_copy(&fidp
->path
, &path
);
2134 err
= v9fs_co_opendir(pdu
->s
, fidp
);
2138 fidp
->fid_type
= P9_FID_DIR
;
2139 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2140 err
= v9fs_co_symlink(pdu
->s
, fidp
, &name
,
2141 extension
.data
, -1 , &stbuf
);
2145 err
= v9fs_co_name_to_path(pdu
->s
, &fidp
->path
, name
.data
, &path
);
2149 v9fs_path_copy(&fidp
->path
, &path
);
2150 } else if (perm
& P9_STAT_MODE_LINK
) {
2151 int32_t ofid
= atoi(extension
.data
);
2152 V9fsFidState
*ofidp
= get_fid(pdu
->s
, ofid
);
2153 if (ofidp
== NULL
) {
2157 err
= v9fs_co_link(pdu
->s
, ofidp
, fidp
, &name
);
2158 put_fid(pdu
->s
, ofidp
);
2162 err
= v9fs_co_name_to_path(pdu
->s
, &fidp
->path
, name
.data
, &path
);
2164 fidp
->fid_type
= P9_FID_NONE
;
2167 v9fs_path_copy(&fidp
->path
, &path
);
2168 err
= v9fs_co_lstat(pdu
->s
, &fidp
->path
, &stbuf
);
2170 fidp
->fid_type
= P9_FID_NONE
;
2173 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2175 uint32_t major
, minor
;
2178 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2195 nmode
|= perm
& 0777;
2196 err
= v9fs_co_mknod(pdu
->s
, fidp
, &name
, fidp
->uid
, -1,
2197 makedev(major
, minor
), nmode
, &stbuf
);
2201 err
= v9fs_co_name_to_path(pdu
->s
, &fidp
->path
, name
.data
, &path
);
2205 v9fs_path_copy(&fidp
->path
, &path
);
2206 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2207 err
= v9fs_co_mknod(pdu
->s
, fidp
, &name
, fidp
->uid
, -1,
2208 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2212 err
= v9fs_co_name_to_path(pdu
->s
, &fidp
->path
, name
.data
, &path
);
2216 v9fs_path_copy(&fidp
->path
, &path
);
2217 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2218 err
= v9fs_co_mknod(pdu
->s
, fidp
, &name
, fidp
->uid
, -1,
2219 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2223 err
= v9fs_co_name_to_path(pdu
->s
, &fidp
->path
, name
.data
, &path
);
2227 v9fs_path_copy(&fidp
->path
, &path
);
2229 err
= v9fs_co_open2(pdu
->s
, fidp
, &name
, -1,
2230 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2234 fidp
->fid_type
= P9_FID_FILE
;
2235 fidp
->open_flags
= omode_to_uflags(mode
);
2236 if (fidp
->open_flags
& O_EXCL
) {
2238 * We let the host file system do O_EXCL check
2239 * We should not reclaim such fd
2241 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2244 iounit
= get_iounit(pdu
->s
, &fidp
->path
);
2245 stat_to_qid(&stbuf
, &qid
);
2246 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2249 put_fid(pdu
->s
, fidp
);
2251 complete_pdu(pdu
->s
, pdu
, err
);
2252 v9fs_string_free(&name
);
2253 v9fs_string_free(&extension
);
2254 v9fs_path_free(&path
);
2257 static void v9fs_symlink(void *opaque
)
2259 V9fsPDU
*pdu
= opaque
;
2262 V9fsFidState
*dfidp
;
2270 pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2272 dfidp
= get_fid(pdu
->s
, dfid
);
2273 if (dfidp
== NULL
) {
2277 err
= v9fs_co_symlink(pdu
->s
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2281 stat_to_qid(&stbuf
, &qid
);
2282 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
2285 put_fid(pdu
->s
, dfidp
);
2287 complete_pdu(pdu
->s
, pdu
, err
);
2288 v9fs_string_free(&name
);
2289 v9fs_string_free(&symname
);
2292 static void v9fs_flush(void *opaque
)
2294 V9fsPDU
*pdu
= opaque
;
2295 V9fsState
*s
= pdu
->s
;
2296 /* A nop call with no return */
2297 complete_pdu(s
, pdu
, 7);
2301 static void v9fs_link(void *opaque
)
2303 V9fsPDU
*pdu
= opaque
;
2304 V9fsState
*s
= pdu
->s
;
2305 int32_t dfid
, oldfid
;
2306 V9fsFidState
*dfidp
, *oldfidp
;
2311 pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2313 dfidp
= get_fid(s
, dfid
);
2314 if (dfidp
== NULL
) {
2319 oldfidp
= get_fid(s
, oldfid
);
2320 if (oldfidp
== NULL
) {
2324 err
= v9fs_co_link(s
, oldfidp
, dfidp
, &name
);
2331 v9fs_string_free(&name
);
2332 complete_pdu(s
, pdu
, err
);
2335 /* Only works with path name based fid */
2336 static void v9fs_remove(void *opaque
)
2342 V9fsPDU
*pdu
= opaque
;
2344 pdu_unmarshal(pdu
, offset
, "d", &fid
);
2346 fidp
= get_fid(pdu
->s
, fid
);
2351 /* if fs driver is not path based, return EOPNOTSUPP */
2352 if (!pdu
->s
->ctx
.flags
& PATHNAME_FSCONTEXT
) {
2357 * IF the file is unlinked, we cannot reopen
2358 * the file later. So don't reclaim fd
2360 err
= v9fs_mark_fids_unreclaim(pdu
->s
, &fidp
->path
);
2364 err
= v9fs_co_remove(pdu
->s
, &fidp
->path
);
2369 /* For TREMOVE we need to clunk the fid even on failed remove */
2370 clunk_fid(pdu
->s
, fidp
->fid
);
2371 put_fid(pdu
->s
, fidp
);
2373 complete_pdu(pdu
->s
, pdu
, err
);
2376 static void v9fs_unlinkat(void *opaque
)
2380 int32_t dfid
, flags
;
2383 V9fsFidState
*dfidp
;
2384 V9fsPDU
*pdu
= opaque
;
2386 pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2388 dfidp
= get_fid(pdu
->s
, dfid
);
2389 if (dfidp
== NULL
) {
2394 * IF the file is unlinked, we cannot reopen
2395 * the file later. So don't reclaim fd
2397 v9fs_path_init(&path
);
2398 err
= v9fs_co_name_to_path(pdu
->s
, &dfidp
->path
, name
.data
, &path
);
2402 err
= v9fs_mark_fids_unreclaim(pdu
->s
, &path
);
2406 err
= v9fs_co_unlinkat(pdu
->s
, &dfidp
->path
, &name
, flags
);
2411 put_fid(pdu
->s
, dfidp
);
2412 v9fs_path_free(&path
);
2414 complete_pdu(pdu
->s
, pdu
, err
);
2415 v9fs_string_free(&name
);
2419 /* Only works with path name based fid */
2420 static int v9fs_complete_rename(V9fsState
*s
, V9fsFidState
*fidp
,
2421 int32_t newdirfid
, V9fsString
*name
)
2426 V9fsFidState
*tfidp
;
2427 V9fsFidState
*dirfidp
= NULL
;
2428 char *old_name
, *new_name
;
2430 v9fs_path_init(&new_path
);
2431 if (newdirfid
!= -1) {
2432 dirfidp
= get_fid(s
, newdirfid
);
2433 if (dirfidp
== NULL
) {
2437 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2438 v9fs_co_name_to_path(s
, &dirfidp
->path
, name
->data
, &new_path
);
2440 old_name
= fidp
->path
.data
;
2441 end
= strrchr(old_name
, '/');
2447 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2448 strncat(new_name
, old_name
, end
- old_name
);
2449 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2450 v9fs_co_name_to_path(s
, NULL
, new_name
, &new_path
);
2453 err
= v9fs_co_rename(s
, &fidp
->path
, &new_path
);
2458 * Fixup fid's pointing to the old name to
2459 * start pointing to the new name
2461 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2462 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2463 /* replace the name */
2464 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2469 put_fid(s
, dirfidp
);
2471 v9fs_path_free(&new_path
);
2476 /* Only works with path name based fid */
2477 static void v9fs_rename(void *opaque
)
2485 V9fsPDU
*pdu
= opaque
;
2486 V9fsState
*s
= pdu
->s
;
2488 pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2490 fidp
= get_fid(s
, fid
);
2495 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
2496 /* if fs driver is not path based, return EOPNOTSUPP */
2497 if (!pdu
->s
->ctx
.flags
& PATHNAME_FSCONTEXT
) {
2501 v9fs_path_write_lock(s
);
2502 err
= v9fs_complete_rename(s
, fidp
, newdirfid
, &name
);
2503 v9fs_path_unlock(s
);
2510 complete_pdu(s
, pdu
, err
);
2511 v9fs_string_free(&name
);
2514 static void v9fs_fix_fid_paths(V9fsState
*s
, V9fsPath
*olddir
,
2515 V9fsString
*old_name
, V9fsPath
*newdir
,
2516 V9fsString
*new_name
)
2518 V9fsFidState
*tfidp
;
2519 V9fsPath oldpath
, newpath
;
2522 v9fs_path_init(&oldpath
);
2523 v9fs_path_init(&newpath
);
2524 v9fs_co_name_to_path(s
, olddir
, old_name
->data
, &oldpath
);
2525 v9fs_co_name_to_path(s
, newdir
, new_name
->data
, &newpath
);
2528 * Fixup fid's pointing to the old name to
2529 * start pointing to the new name
2531 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2532 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2533 /* replace the name */
2534 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2537 v9fs_path_free(&oldpath
);
2538 v9fs_path_free(&newpath
);
2541 static int v9fs_complete_renameat(V9fsState
*s
, int32_t olddirfid
,
2542 V9fsString
*old_name
, int32_t newdirfid
,
2543 V9fsString
*new_name
)
2546 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2548 olddirfidp
= get_fid(s
, olddirfid
);
2549 if (olddirfidp
== NULL
) {
2553 if (newdirfid
!= -1) {
2554 newdirfidp
= get_fid(s
, newdirfid
);
2555 if (newdirfidp
== NULL
) {
2560 newdirfidp
= get_fid(s
, olddirfid
);
2563 err
= v9fs_co_renameat(s
, &olddirfidp
->path
, old_name
,
2564 &newdirfidp
->path
, new_name
);
2568 if (s
->ctx
.flags
& PATHNAME_FSCONTEXT
) {
2569 /* Only for path based fid we need to do the below fixup */
2570 v9fs_fix_fid_paths(s
, &olddirfidp
->path
, old_name
,
2571 &newdirfidp
->path
, new_name
);
2575 put_fid(s
, olddirfidp
);
2578 put_fid(s
, newdirfidp
);
2583 static void v9fs_renameat(void *opaque
)
2587 V9fsPDU
*pdu
= opaque
;
2588 V9fsState
*s
= pdu
->s
;
2589 int32_t olddirfid
, newdirfid
;
2590 V9fsString old_name
, new_name
;
2592 pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2593 &old_name
, &newdirfid
, &new_name
);
2595 v9fs_path_write_lock(s
);
2596 err
= v9fs_complete_renameat(s
, olddirfid
, &old_name
, newdirfid
, &new_name
);
2597 v9fs_path_unlock(s
);
2601 complete_pdu(s
, pdu
, err
);
2602 v9fs_string_free(&old_name
);
2603 v9fs_string_free(&new_name
);
2606 static void v9fs_wstat(void *opaque
)
2615 V9fsPDU
*pdu
= opaque
;
2616 V9fsState
*s
= pdu
->s
;
2618 pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2620 fidp
= get_fid(s
, fid
);
2625 /* do we need to sync the file? */
2626 if (donttouch_stat(&v9stat
)) {
2627 err
= v9fs_co_fsync(s
, fidp
, 0);
2630 if (v9stat
.mode
!= -1) {
2632 err
= v9fs_co_lstat(s
, &fidp
->path
, &stbuf
);
2636 v9_mode
= stat_to_v9mode(&stbuf
);
2637 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2638 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2639 /* Attempting to change the type */
2643 err
= v9fs_co_chmod(s
, &fidp
->path
,
2644 v9mode_to_mode(v9stat
.mode
,
2645 &v9stat
.extension
));
2650 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2651 struct timespec times
[2];
2652 if (v9stat
.atime
!= -1) {
2653 times
[0].tv_sec
= v9stat
.atime
;
2654 times
[0].tv_nsec
= 0;
2656 times
[0].tv_nsec
= UTIME_OMIT
;
2658 if (v9stat
.mtime
!= -1) {
2659 times
[1].tv_sec
= v9stat
.mtime
;
2660 times
[1].tv_nsec
= 0;
2662 times
[1].tv_nsec
= UTIME_OMIT
;
2664 err
= v9fs_co_utimensat(s
, &fidp
->path
, times
);
2669 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2670 err
= v9fs_co_chown(s
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2675 if (v9stat
.name
.size
!= 0) {
2676 err
= v9fs_complete_rename(s
, fidp
, -1, &v9stat
.name
);
2681 if (v9stat
.length
!= -1) {
2682 err
= v9fs_co_truncate(s
, &fidp
->path
, v9stat
.length
);
2691 v9fs_stat_free(&v9stat
);
2692 complete_pdu(s
, pdu
, err
);
2695 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2707 int32_t bsize_factor
;
2710 * compute bsize factor based on host file system block size
2713 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2714 if (!bsize_factor
) {
2717 f_type
= stbuf
->f_type
;
2718 f_bsize
= stbuf
->f_bsize
;
2719 f_bsize
*= bsize_factor
;
2721 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2722 * adjust(divide) the number of blocks, free blocks and available
2723 * blocks by bsize factor
2725 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2726 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2727 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2728 f_files
= stbuf
->f_files
;
2729 f_ffree
= stbuf
->f_ffree
;
2730 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2731 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2732 f_namelen
= stbuf
->f_namelen
;
2734 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2735 f_type
, f_bsize
, f_blocks
, f_bfree
,
2736 f_bavail
, f_files
, f_ffree
,
2737 fsid_val
, f_namelen
);
2740 static void v9fs_statfs(void *opaque
)
2746 struct statfs stbuf
;
2747 V9fsPDU
*pdu
= opaque
;
2748 V9fsState
*s
= pdu
->s
;
2750 pdu_unmarshal(pdu
, offset
, "d", &fid
);
2751 fidp
= get_fid(s
, fid
);
2756 retval
= v9fs_co_statfs(s
, &fidp
->path
, &stbuf
);
2761 retval
+= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2765 complete_pdu(s
, pdu
, retval
);
2769 static void v9fs_mknod(void *opaque
)
2782 V9fsPDU
*pdu
= opaque
;
2783 V9fsState
*s
= pdu
->s
;
2785 pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2786 &major
, &minor
, &gid
);
2788 fidp
= get_fid(s
, fid
);
2793 err
= v9fs_co_mknod(s
, fidp
, &name
, fidp
->uid
, gid
,
2794 makedev(major
, minor
), mode
, &stbuf
);
2798 stat_to_qid(&stbuf
, &qid
);
2800 err
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
2804 complete_pdu(s
, pdu
, err
);
2805 v9fs_string_free(&name
);
2809 * Implement posix byte range locking code
2810 * Server side handling of locking code is very simple, because 9p server in
2811 * QEMU can handle only one client. And most of the lock handling
2812 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2813 * do any thing in * qemu 9p server side lock code path.
2814 * So when a TLOCK request comes, always return success
2816 static void v9fs_lock(void *opaque
)
2823 int32_t fid
, err
= 0;
2824 V9fsPDU
*pdu
= opaque
;
2825 V9fsState
*s
= pdu
->s
;
2827 flock
= g_malloc(sizeof(*flock
));
2828 pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
->type
,
2829 &flock
->flags
, &flock
->start
, &flock
->length
,
2830 &flock
->proc_id
, &flock
->client_id
);
2831 status
= P9_LOCK_ERROR
;
2833 /* We support only block flag now (that too ignored currently) */
2834 if (flock
->flags
& ~P9_LOCK_FLAGS_BLOCK
) {
2838 fidp
= get_fid(s
, fid
);
2843 err
= v9fs_co_fstat(s
, fidp
->fs
.fd
, &stbuf
);
2847 status
= P9_LOCK_SUCCESS
;
2852 err
+= pdu_marshal(pdu
, offset
, "b", status
);
2853 complete_pdu(s
, pdu
, err
);
2854 v9fs_string_free(&flock
->client_id
);
2859 * When a TGETLOCK request comes, always return success because all lock
2860 * handling is done by client's VFS layer.
2862 static void v9fs_getlock(void *opaque
)
2868 int32_t fid
, err
= 0;
2869 V9fsPDU
*pdu
= opaque
;
2870 V9fsState
*s
= pdu
->s
;
2872 glock
= g_malloc(sizeof(*glock
));
2873 pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
->type
,
2874 &glock
->start
, &glock
->length
, &glock
->proc_id
,
2877 fidp
= get_fid(s
, fid
);
2882 err
= v9fs_co_fstat(s
, fidp
->fs
.fd
, &stbuf
);
2886 glock
->type
= F_UNLCK
;
2887 offset
+= pdu_marshal(pdu
, offset
, "bqqds", glock
->type
,
2888 glock
->start
, glock
->length
, glock
->proc_id
,
2894 complete_pdu(s
, pdu
, err
);
2895 v9fs_string_free(&glock
->client_id
);
2899 static void v9fs_mkdir(void *opaque
)
2901 V9fsPDU
*pdu
= opaque
;
2912 pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
2914 fidp
= get_fid(pdu
->s
, fid
);
2919 err
= v9fs_co_mkdir(pdu
->s
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
2923 stat_to_qid(&stbuf
, &qid
);
2924 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
2927 put_fid(pdu
->s
, fidp
);
2929 complete_pdu(pdu
->s
, pdu
, err
);
2930 v9fs_string_free(&name
);
2933 static void v9fs_xattrwalk(void *opaque
)
2939 int32_t fid
, newfid
;
2940 V9fsFidState
*file_fidp
;
2941 V9fsFidState
*xattr_fidp
= NULL
;
2942 V9fsPDU
*pdu
= opaque
;
2943 V9fsState
*s
= pdu
->s
;
2945 pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
2946 file_fidp
= get_fid(s
, fid
);
2947 if (file_fidp
== NULL
) {
2951 xattr_fidp
= alloc_fid(s
, newfid
);
2952 if (xattr_fidp
== NULL
) {
2956 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
2957 if (name
.data
[0] == 0) {
2959 * listxattr request. Get the size first
2961 size
= v9fs_co_llistxattr(s
, &xattr_fidp
->path
, NULL
, 0);
2964 clunk_fid(s
, xattr_fidp
->fid
);
2968 * Read the xattr value
2970 xattr_fidp
->fs
.xattr
.len
= size
;
2971 xattr_fidp
->fid_type
= P9_FID_XATTR
;
2972 xattr_fidp
->fs
.xattr
.copied_len
= -1;
2974 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
2975 err
= v9fs_co_llistxattr(s
, &xattr_fidp
->path
,
2976 xattr_fidp
->fs
.xattr
.value
,
2977 xattr_fidp
->fs
.xattr
.len
);
2979 clunk_fid(s
, xattr_fidp
->fid
);
2983 offset
+= pdu_marshal(pdu
, offset
, "q", size
);
2987 * specific xattr fid. We check for xattr
2988 * presence also collect the xattr size
2990 size
= v9fs_co_lgetxattr(s
, &xattr_fidp
->path
,
2994 clunk_fid(s
, xattr_fidp
->fid
);
2998 * Read the xattr value
3000 xattr_fidp
->fs
.xattr
.len
= size
;
3001 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3002 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3004 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3005 err
= v9fs_co_lgetxattr(s
, &xattr_fidp
->path
,
3006 &name
, xattr_fidp
->fs
.xattr
.value
,
3007 xattr_fidp
->fs
.xattr
.len
);
3009 clunk_fid(s
, xattr_fidp
->fid
);
3013 offset
+= pdu_marshal(pdu
, offset
, "q", size
);
3017 put_fid(s
, file_fidp
);
3019 put_fid(s
, xattr_fidp
);
3022 complete_pdu(s
, pdu
, err
);
3023 v9fs_string_free(&name
);
3026 static void v9fs_xattrcreate(void *opaque
)
3034 V9fsFidState
*file_fidp
;
3035 V9fsFidState
*xattr_fidp
;
3036 V9fsPDU
*pdu
= opaque
;
3037 V9fsState
*s
= pdu
->s
;
3039 pdu_unmarshal(pdu
, offset
, "dsqd",
3040 &fid
, &name
, &size
, &flags
);
3042 file_fidp
= get_fid(s
, fid
);
3043 if (file_fidp
== NULL
) {
3047 /* Make the file fid point to xattr */
3048 xattr_fidp
= file_fidp
;
3049 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3050 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3051 xattr_fidp
->fs
.xattr
.len
= size
;
3052 xattr_fidp
->fs
.xattr
.flags
= flags
;
3053 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3054 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3056 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3058 xattr_fidp
->fs
.xattr
.value
= NULL
;
3061 put_fid(s
, file_fidp
);
3063 complete_pdu(s
, pdu
, err
);
3064 v9fs_string_free(&name
);
3067 static void v9fs_readlink(void *opaque
)
3069 V9fsPDU
*pdu
= opaque
;
3076 pdu_unmarshal(pdu
, offset
, "d", &fid
);
3077 fidp
= get_fid(pdu
->s
, fid
);
3083 v9fs_string_init(&target
);
3084 err
= v9fs_co_readlink(pdu
->s
, &fidp
->path
, &target
);
3088 offset
+= pdu_marshal(pdu
, offset
, "s", &target
);
3090 v9fs_string_free(&target
);
3092 put_fid(pdu
->s
, fidp
);
3094 complete_pdu(pdu
->s
, pdu
, err
);
3097 static CoroutineEntry
*pdu_co_handlers
[] = {
3098 [P9_TREADDIR
] = v9fs_readdir
,
3099 [P9_TSTATFS
] = v9fs_statfs
,
3100 [P9_TGETATTR
] = v9fs_getattr
,
3101 [P9_TSETATTR
] = v9fs_setattr
,
3102 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3103 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3104 [P9_TMKNOD
] = v9fs_mknod
,
3105 [P9_TRENAME
] = v9fs_rename
,
3106 [P9_TLOCK
] = v9fs_lock
,
3107 [P9_TGETLOCK
] = v9fs_getlock
,
3108 [P9_TRENAMEAT
] = v9fs_renameat
,
3109 [P9_TREADLINK
] = v9fs_readlink
,
3110 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3111 [P9_TMKDIR
] = v9fs_mkdir
,
3112 [P9_TVERSION
] = v9fs_version
,
3113 [P9_TLOPEN
] = v9fs_open
,
3114 [P9_TATTACH
] = v9fs_attach
,
3115 [P9_TSTAT
] = v9fs_stat
,
3116 [P9_TWALK
] = v9fs_walk
,
3117 [P9_TCLUNK
] = v9fs_clunk
,
3118 [P9_TFSYNC
] = v9fs_fsync
,
3119 [P9_TOPEN
] = v9fs_open
,
3120 [P9_TREAD
] = v9fs_read
,
3122 [P9_TAUTH
] = v9fs_auth
,
3124 [P9_TFLUSH
] = v9fs_flush
,
3125 [P9_TLINK
] = v9fs_link
,
3126 [P9_TSYMLINK
] = v9fs_symlink
,
3127 [P9_TCREATE
] = v9fs_create
,
3128 [P9_TLCREATE
] = v9fs_lcreate
,
3129 [P9_TWRITE
] = v9fs_write
,
3130 [P9_TWSTAT
] = v9fs_wstat
,
3131 [P9_TREMOVE
] = v9fs_remove
,
3134 static void v9fs_op_not_supp(void *opaque
)
3136 V9fsPDU
*pdu
= opaque
;
3137 complete_pdu(pdu
->s
, pdu
, -EOPNOTSUPP
);
3140 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
3143 CoroutineEntry
*handler
;
3148 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3149 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3150 handler
= v9fs_op_not_supp
;
3152 handler
= pdu_co_handlers
[pdu
->id
];
3154 co
= qemu_coroutine_create(handler
);
3155 qemu_coroutine_enter(co
, pdu
);
3158 void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
3160 V9fsState
*s
= (V9fsState
*)vdev
;
3164 while ((pdu
= alloc_pdu(s
)) &&
3165 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
3168 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
3169 BUG_ON(pdu
->elem
.out_sg
[0].iov_len
< 7);
3171 ptr
= pdu
->elem
.out_sg
[0].iov_base
;
3173 memcpy(&pdu
->size
, ptr
, 4);
3175 memcpy(&pdu
->tag
, ptr
+ 5, 2);
3181 void virtio_9p_set_fd_limit(void)
3184 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3185 fprintf(stderr
, "Failed to get the resource limit\n");
3188 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3189 open_fd_rc
= rlim
.rlim_cur
/2;