]>
git.proxmox.com Git - qemu.git/blob - hw/9pfs/virtio-9p.c
2a6895396d2dacddf4fca3a5454ad1901e4ac73f
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 int 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
) {
430 if (*fidpp
== NULL
) {
439 void v9fs_reclaim_fd(V9fsState
*s
)
441 int reclaim_count
= 0;
442 V9fsFidState
*f
, *reclaim_list
= NULL
;
444 for (f
= s
->fid_list
; f
; f
= f
->next
) {
446 * Unlink fids cannot be reclaimed. Check
447 * for them and skip them. Also skip fids
448 * currently being operated on.
450 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
454 * if it is a recently referenced fid
455 * we leave the fid untouched and clear the
456 * reference bit. We come back to it later
457 * in the next iteration. (a simple LRU without
458 * moving list elements around)
460 if (f
->flags
& FID_REFERENCED
) {
461 f
->flags
&= ~FID_REFERENCED
;
465 * Add fids to reclaim list.
467 if (f
->fid_type
== P9_FID_FILE
) {
468 if (f
->fs
.fd
!= -1) {
470 * Up the reference count so that
471 * a clunk request won't free this fid
474 f
->rclm_lst
= reclaim_list
;
476 f
->fs_reclaim
.fd
= f
->fs
.fd
;
480 } else if (f
->fid_type
== P9_FID_DIR
) {
481 if (f
->fs
.dir
!= NULL
) {
483 * Up the reference count so that
484 * a clunk request won't free this fid
487 f
->rclm_lst
= reclaim_list
;
489 f
->fs_reclaim
.dir
= f
->fs
.dir
;
494 if (reclaim_count
>= open_fd_rc
) {
499 * Now close the fid in reclaim list. Free them if they
500 * are already clunked.
502 while (reclaim_list
) {
504 reclaim_list
= f
->rclm_lst
;
505 if (f
->fid_type
== P9_FID_FILE
) {
506 v9fs_co_close(s
, f
->fs_reclaim
.fd
);
507 } else if (f
->fid_type
== P9_FID_DIR
) {
508 v9fs_co_closedir(s
, f
->fs_reclaim
.dir
);
512 * Now drop the fid reference, free it
519 static int v9fs_mark_fids_unreclaim(V9fsState
*s
, V9fsPath
*path
)
522 V9fsFidState
*fidp
, head_fid
;
524 head_fid
.next
= s
->fid_list
;
525 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
526 if (fidp
->path
.size
!= path
->size
) {
529 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
530 /* Mark the fid non reclaimable. */
531 fidp
->flags
|= FID_NON_RECLAIMABLE
;
533 /* reopen the file/dir if already closed */
534 err
= v9fs_reopen_fid(s
, fidp
);
539 * Go back to head of fid list because
540 * the list could have got updated when
541 * switched to the worker thread
551 #define P9_QID_TYPE_DIR 0x80
552 #define P9_QID_TYPE_SYMLINK 0x02
554 #define P9_STAT_MODE_DIR 0x80000000
555 #define P9_STAT_MODE_APPEND 0x40000000
556 #define P9_STAT_MODE_EXCL 0x20000000
557 #define P9_STAT_MODE_MOUNT 0x10000000
558 #define P9_STAT_MODE_AUTH 0x08000000
559 #define P9_STAT_MODE_TMP 0x04000000
560 #define P9_STAT_MODE_SYMLINK 0x02000000
561 #define P9_STAT_MODE_LINK 0x01000000
562 #define P9_STAT_MODE_DEVICE 0x00800000
563 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
564 #define P9_STAT_MODE_SOCKET 0x00100000
565 #define P9_STAT_MODE_SETUID 0x00080000
566 #define P9_STAT_MODE_SETGID 0x00040000
567 #define P9_STAT_MODE_SETVTX 0x00010000
569 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
570 P9_STAT_MODE_SYMLINK | \
571 P9_STAT_MODE_LINK | \
572 P9_STAT_MODE_DEVICE | \
573 P9_STAT_MODE_NAMED_PIPE | \
576 /* This is the algorithm from ufs in spfs */
577 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
581 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
582 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
583 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
584 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
586 if (S_ISDIR(stbuf
->st_mode
)) {
587 qidp
->type
|= P9_QID_TYPE_DIR
;
589 if (S_ISLNK(stbuf
->st_mode
)) {
590 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
594 static int fid_to_qid(V9fsState
*s
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
599 err
= v9fs_co_lstat(s
, &fidp
->path
, &stbuf
);
603 stat_to_qid(&stbuf
, qidp
);
607 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
611 if (!QLIST_EMPTY(&s
->free_list
)) {
612 pdu
= QLIST_FIRST(&s
->free_list
);
613 QLIST_REMOVE(pdu
, next
);
618 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
624 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
628 size_t pdu_packunpack(void *addr
, struct iovec
*sg
, int sg_count
,
629 size_t offset
, size_t size
, int pack
)
634 for (i
= 0; size
&& i
< sg_count
; i
++) {
636 if (offset
>= sg
[i
].iov_len
) {
638 offset
-= sg
[i
].iov_len
;
641 len
= MIN(sg
[i
].iov_len
- offset
, size
);
643 memcpy(sg
[i
].iov_base
+ offset
, addr
, len
);
645 memcpy(addr
, sg
[i
].iov_base
+ offset
, len
);
660 static size_t pdu_unpack(void *dst
, V9fsPDU
*pdu
, size_t offset
, size_t size
)
662 return pdu_packunpack(dst
, pdu
->elem
.out_sg
, pdu
->elem
.out_num
,
666 static size_t pdu_pack(V9fsPDU
*pdu
, size_t offset
, const void *src
,
669 return pdu_packunpack((void *)src
, pdu
->elem
.in_sg
, pdu
->elem
.in_num
,
673 static int pdu_copy_sg(V9fsPDU
*pdu
, size_t offset
, int rx
, struct iovec
*sg
)
677 struct iovec
*src_sg
;
681 src_sg
= pdu
->elem
.in_sg
;
682 num
= pdu
->elem
.in_num
;
684 src_sg
= pdu
->elem
.out_sg
;
685 num
= pdu
->elem
.out_num
;
689 for (i
= 0; i
< num
; i
++) {
691 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
692 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
694 } else if (offset
< (src_sg
[i
].iov_len
+ pos
)) {
695 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
696 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
697 sg
[j
].iov_base
+= (offset
- pos
);
698 sg
[j
].iov_len
-= (offset
- pos
);
701 pos
+= src_sg
[i
].iov_len
;
707 static size_t pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
709 size_t old_offset
= offset
;
714 for (i
= 0; fmt
[i
]; i
++) {
717 uint8_t *valp
= va_arg(ap
, uint8_t *);
718 offset
+= pdu_unpack(valp
, pdu
, offset
, sizeof(*valp
));
723 valp
= va_arg(ap
, uint16_t *);
724 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
725 *valp
= le16_to_cpu(val
);
730 valp
= va_arg(ap
, uint32_t *);
731 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
732 *valp
= le32_to_cpu(val
);
737 valp
= va_arg(ap
, uint64_t *);
738 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
739 *valp
= le64_to_cpu(val
);
743 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
744 int *iovcnt
= va_arg(ap
, int *);
745 *iovcnt
= pdu_copy_sg(pdu
, offset
, 0, iov
);
749 V9fsString
*str
= va_arg(ap
, V9fsString
*);
750 offset
+= pdu_unmarshal(pdu
, offset
, "w", &str
->size
);
751 /* FIXME: sanity check str->size */
752 str
->data
= g_malloc(str
->size
+ 1);
753 offset
+= pdu_unpack(str
->data
, pdu
, offset
, str
->size
);
754 str
->data
[str
->size
] = 0;
758 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
759 offset
+= pdu_unmarshal(pdu
, offset
, "bdq",
760 &qidp
->type
, &qidp
->version
, &qidp
->path
);
764 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
765 offset
+= pdu_unmarshal(pdu
, offset
, "wwdQdddqsssssddd",
766 &statp
->size
, &statp
->type
, &statp
->dev
,
767 &statp
->qid
, &statp
->mode
, &statp
->atime
,
768 &statp
->mtime
, &statp
->length
,
769 &statp
->name
, &statp
->uid
, &statp
->gid
,
770 &statp
->muid
, &statp
->extension
,
771 &statp
->n_uid
, &statp
->n_gid
,
776 V9fsIattr
*iattr
= va_arg(ap
, V9fsIattr
*);
777 offset
+= pdu_unmarshal(pdu
, offset
, "ddddqqqqq",
778 &iattr
->valid
, &iattr
->mode
,
779 &iattr
->uid
, &iattr
->gid
, &iattr
->size
,
780 &iattr
->atime_sec
, &iattr
->atime_nsec
,
781 &iattr
->mtime_sec
, &iattr
->mtime_nsec
);
791 return offset
- old_offset
;
794 static size_t pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
796 size_t old_offset
= offset
;
801 for (i
= 0; fmt
[i
]; i
++) {
804 uint8_t val
= va_arg(ap
, int);
805 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
810 cpu_to_le16w(&val
, va_arg(ap
, int));
811 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
816 cpu_to_le32w(&val
, va_arg(ap
, uint32_t));
817 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
822 cpu_to_le64w(&val
, va_arg(ap
, uint64_t));
823 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
827 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
828 int *iovcnt
= va_arg(ap
, int *);
829 *iovcnt
= pdu_copy_sg(pdu
, offset
, 1, iov
);
833 V9fsString
*str
= va_arg(ap
, V9fsString
*);
834 offset
+= pdu_marshal(pdu
, offset
, "w", str
->size
);
835 offset
+= pdu_pack(pdu
, offset
, str
->data
, str
->size
);
839 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
840 offset
+= pdu_marshal(pdu
, offset
, "bdq",
841 qidp
->type
, qidp
->version
, qidp
->path
);
845 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
846 offset
+= pdu_marshal(pdu
, offset
, "wwdQdddqsssssddd",
847 statp
->size
, statp
->type
, statp
->dev
,
848 &statp
->qid
, statp
->mode
, statp
->atime
,
849 statp
->mtime
, statp
->length
, &statp
->name
,
850 &statp
->uid
, &statp
->gid
, &statp
->muid
,
851 &statp
->extension
, statp
->n_uid
,
852 statp
->n_gid
, statp
->n_muid
);
856 V9fsStatDotl
*statp
= va_arg(ap
, V9fsStatDotl
*);
857 offset
+= pdu_marshal(pdu
, offset
, "qQdddqqqqqqqqqqqqqqq",
858 statp
->st_result_mask
,
859 &statp
->qid
, statp
->st_mode
,
860 statp
->st_uid
, statp
->st_gid
,
861 statp
->st_nlink
, statp
->st_rdev
,
862 statp
->st_size
, statp
->st_blksize
, statp
->st_blocks
,
863 statp
->st_atime_sec
, statp
->st_atime_nsec
,
864 statp
->st_mtime_sec
, statp
->st_mtime_nsec
,
865 statp
->st_ctime_sec
, statp
->st_ctime_nsec
,
866 statp
->st_btime_sec
, statp
->st_btime_nsec
,
867 statp
->st_gen
, statp
->st_data_version
);
876 return offset
- old_offset
;
879 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
881 int8_t id
= pdu
->id
+ 1; /* Response */
887 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
890 str
.data
= strerror(err
);
891 str
.size
= strlen(str
.data
);
893 len
+= pdu_marshal(pdu
, len
, "s", &str
);
897 len
+= pdu_marshal(pdu
, len
, "d", err
);
899 if (s
->proto_version
== V9FS_PROTO_2000L
) {
904 /* fill out the header */
905 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
907 /* keep these in sync */
911 /* push onto queue and notify */
912 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
914 /* FIXME: we should batch these completions */
915 virtio_notify(&s
->vdev
, s
->vq
);
920 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
925 if (mode
& P9_STAT_MODE_DIR
) {
929 if (mode
& P9_STAT_MODE_SYMLINK
) {
932 if (mode
& P9_STAT_MODE_SOCKET
) {
935 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
938 if (mode
& P9_STAT_MODE_DEVICE
) {
939 if (extension
&& extension
->data
[0] == 'c') {
950 if (mode
& P9_STAT_MODE_SETUID
) {
953 if (mode
& P9_STAT_MODE_SETGID
) {
956 if (mode
& P9_STAT_MODE_SETVTX
) {
963 static int donttouch_stat(V9fsStat
*stat
)
965 if (stat
->type
== -1 &&
967 stat
->qid
.type
== -1 &&
968 stat
->qid
.version
== -1 &&
969 stat
->qid
.path
== -1 &&
973 stat
->length
== -1 &&
980 stat
->n_muid
== -1) {
987 static void v9fs_stat_free(V9fsStat
*stat
)
989 v9fs_string_free(&stat
->name
);
990 v9fs_string_free(&stat
->uid
);
991 v9fs_string_free(&stat
->gid
);
992 v9fs_string_free(&stat
->muid
);
993 v9fs_string_free(&stat
->extension
);
996 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
1000 mode
= stbuf
->st_mode
& 0777;
1001 if (S_ISDIR(stbuf
->st_mode
)) {
1002 mode
|= P9_STAT_MODE_DIR
;
1005 if (S_ISLNK(stbuf
->st_mode
)) {
1006 mode
|= P9_STAT_MODE_SYMLINK
;
1009 if (S_ISSOCK(stbuf
->st_mode
)) {
1010 mode
|= P9_STAT_MODE_SOCKET
;
1013 if (S_ISFIFO(stbuf
->st_mode
)) {
1014 mode
|= P9_STAT_MODE_NAMED_PIPE
;
1017 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
1018 mode
|= P9_STAT_MODE_DEVICE
;
1021 if (stbuf
->st_mode
& S_ISUID
) {
1022 mode
|= P9_STAT_MODE_SETUID
;
1025 if (stbuf
->st_mode
& S_ISGID
) {
1026 mode
|= P9_STAT_MODE_SETGID
;
1029 if (stbuf
->st_mode
& S_ISVTX
) {
1030 mode
|= P9_STAT_MODE_SETVTX
;
1036 static int stat_to_v9stat(V9fsState
*s
, V9fsPath
*name
,
1037 const struct stat
*stbuf
,
1043 memset(v9stat
, 0, sizeof(*v9stat
));
1045 stat_to_qid(stbuf
, &v9stat
->qid
);
1046 v9stat
->mode
= stat_to_v9mode(stbuf
);
1047 v9stat
->atime
= stbuf
->st_atime
;
1048 v9stat
->mtime
= stbuf
->st_mtime
;
1049 v9stat
->length
= stbuf
->st_size
;
1051 v9fs_string_null(&v9stat
->uid
);
1052 v9fs_string_null(&v9stat
->gid
);
1053 v9fs_string_null(&v9stat
->muid
);
1055 v9stat
->n_uid
= stbuf
->st_uid
;
1056 v9stat
->n_gid
= stbuf
->st_gid
;
1059 v9fs_string_null(&v9stat
->extension
);
1061 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
1062 err
= v9fs_co_readlink(s
, name
, &v9stat
->extension
);
1066 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
1067 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
1068 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
1069 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
1070 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
1071 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
1072 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
1075 str
= strrchr(name
->data
, '/');
1082 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
1085 v9fs_string_size(&v9stat
->name
) +
1086 v9fs_string_size(&v9stat
->uid
) +
1087 v9fs_string_size(&v9stat
->gid
) +
1088 v9fs_string_size(&v9stat
->muid
) +
1089 v9fs_string_size(&v9stat
->extension
);
1093 #define P9_STATS_MODE 0x00000001ULL
1094 #define P9_STATS_NLINK 0x00000002ULL
1095 #define P9_STATS_UID 0x00000004ULL
1096 #define P9_STATS_GID 0x00000008ULL
1097 #define P9_STATS_RDEV 0x00000010ULL
1098 #define P9_STATS_ATIME 0x00000020ULL
1099 #define P9_STATS_MTIME 0x00000040ULL
1100 #define P9_STATS_CTIME 0x00000080ULL
1101 #define P9_STATS_INO 0x00000100ULL
1102 #define P9_STATS_SIZE 0x00000200ULL
1103 #define P9_STATS_BLOCKS 0x00000400ULL
1105 #define P9_STATS_BTIME 0x00000800ULL
1106 #define P9_STATS_GEN 0x00001000ULL
1107 #define P9_STATS_DATA_VERSION 0x00002000ULL
1109 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1110 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1113 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
1114 V9fsStatDotl
*v9lstat
)
1116 memset(v9lstat
, 0, sizeof(*v9lstat
));
1118 v9lstat
->st_mode
= stbuf
->st_mode
;
1119 v9lstat
->st_nlink
= stbuf
->st_nlink
;
1120 v9lstat
->st_uid
= stbuf
->st_uid
;
1121 v9lstat
->st_gid
= stbuf
->st_gid
;
1122 v9lstat
->st_rdev
= stbuf
->st_rdev
;
1123 v9lstat
->st_size
= stbuf
->st_size
;
1124 v9lstat
->st_blksize
= stbuf
->st_blksize
;
1125 v9lstat
->st_blocks
= stbuf
->st_blocks
;
1126 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
1127 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
1128 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
1129 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
1130 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
1131 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
1132 /* Currently we only support BASIC fields in stat */
1133 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
1135 stat_to_qid(stbuf
, &v9lstat
->qid
);
1138 static struct iovec
*adjust_sg(struct iovec
*sg
, int len
, int *iovcnt
)
1140 while (len
&& *iovcnt
) {
1141 if (len
< sg
->iov_len
) {
1143 sg
->iov_base
+= len
;
1155 static struct iovec
*cap_sg(struct iovec
*sg
, int cap
, int *cnt
)
1160 for (i
= 0; i
< *cnt
; i
++) {
1161 if ((total
+ sg
[i
].iov_len
) > cap
) {
1162 sg
[i
].iov_len
-= ((total
+ sg
[i
].iov_len
) - cap
);
1166 total
+= sg
[i
].iov_len
;
1174 static void print_sg(struct iovec
*sg
, int cnt
)
1178 printf("sg[%d]: {", cnt
);
1179 for (i
= 0; i
< cnt
; i
++) {
1183 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
1188 /* Will call this only for path name based fid */
1189 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
1192 v9fs_path_init(&str
);
1193 v9fs_path_copy(&str
, dst
);
1194 v9fs_string_sprintf((V9fsString
*)dst
, "%s%s", src
->data
, str
.data
+len
);
1195 v9fs_path_free(&str
);
1196 /* +1 to include terminating NULL */
1200 static void v9fs_version(void *opaque
)
1202 V9fsPDU
*pdu
= opaque
;
1203 V9fsState
*s
= pdu
->s
;
1207 pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
1209 if (!strcmp(version
.data
, "9P2000.u")) {
1210 s
->proto_version
= V9FS_PROTO_2000U
;
1211 } else if (!strcmp(version
.data
, "9P2000.L")) {
1212 s
->proto_version
= V9FS_PROTO_2000L
;
1214 v9fs_string_sprintf(&version
, "unknown");
1217 offset
+= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
1218 complete_pdu(s
, pdu
, offset
);
1220 v9fs_string_free(&version
);
1224 static void v9fs_attach(void *opaque
)
1226 V9fsPDU
*pdu
= opaque
;
1227 V9fsState
*s
= pdu
->s
;
1228 int32_t fid
, afid
, n_uname
;
1229 V9fsString uname
, aname
;
1235 pdu_unmarshal(pdu
, offset
, "ddssd", &fid
, &afid
, &uname
, &aname
, &n_uname
);
1237 fidp
= alloc_fid(s
, fid
);
1242 fidp
->uid
= n_uname
;
1243 err
= v9fs_co_name_to_path(s
, NULL
, "/", &fidp
->path
);
1249 err
= fid_to_qid(s
, fidp
, &qid
);
1255 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
1260 complete_pdu(s
, pdu
, err
);
1261 v9fs_string_free(&uname
);
1262 v9fs_string_free(&aname
);
1265 static void v9fs_stat(void *opaque
)
1273 V9fsPDU
*pdu
= opaque
;
1274 V9fsState
*s
= pdu
->s
;
1276 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1278 fidp
= get_fid(s
, fid
);
1283 err
= v9fs_co_lstat(s
, &fidp
->path
, &stbuf
);
1287 err
= stat_to_v9stat(s
, &fidp
->path
, &stbuf
, &v9stat
);
1291 offset
+= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1293 v9fs_stat_free(&v9stat
);
1297 complete_pdu(s
, pdu
, err
);
1300 static void v9fs_getattr(void *opaque
)
1307 uint64_t request_mask
;
1308 V9fsStatDotl v9stat_dotl
;
1309 V9fsPDU
*pdu
= opaque
;
1310 V9fsState
*s
= pdu
->s
;
1312 pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1314 fidp
= get_fid(s
, fid
);
1320 * Currently we only support BASIC fields in stat, so there is no
1321 * need to look at request_mask.
1323 retval
= v9fs_co_lstat(s
, &fidp
->path
, &stbuf
);
1327 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1329 retval
+= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1333 complete_pdu(s
, pdu
, retval
);
1336 /* From Linux kernel code */
1337 #define ATTR_MODE (1 << 0)
1338 #define ATTR_UID (1 << 1)
1339 #define ATTR_GID (1 << 2)
1340 #define ATTR_SIZE (1 << 3)
1341 #define ATTR_ATIME (1 << 4)
1342 #define ATTR_MTIME (1 << 5)
1343 #define ATTR_CTIME (1 << 6)
1344 #define ATTR_MASK 127
1345 #define ATTR_ATIME_SET (1 << 7)
1346 #define ATTR_MTIME_SET (1 << 8)
1348 static void v9fs_setattr(void *opaque
)
1355 V9fsPDU
*pdu
= opaque
;
1356 V9fsState
*s
= pdu
->s
;
1358 pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1360 fidp
= get_fid(s
, fid
);
1365 if (v9iattr
.valid
& ATTR_MODE
) {
1366 err
= v9fs_co_chmod(s
, &fidp
->path
, v9iattr
.mode
);
1371 if (v9iattr
.valid
& (ATTR_ATIME
| ATTR_MTIME
)) {
1372 struct timespec times
[2];
1373 if (v9iattr
.valid
& ATTR_ATIME
) {
1374 if (v9iattr
.valid
& ATTR_ATIME_SET
) {
1375 times
[0].tv_sec
= v9iattr
.atime_sec
;
1376 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1378 times
[0].tv_nsec
= UTIME_NOW
;
1381 times
[0].tv_nsec
= UTIME_OMIT
;
1383 if (v9iattr
.valid
& ATTR_MTIME
) {
1384 if (v9iattr
.valid
& ATTR_MTIME_SET
) {
1385 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1386 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1388 times
[1].tv_nsec
= UTIME_NOW
;
1391 times
[1].tv_nsec
= UTIME_OMIT
;
1393 err
= v9fs_co_utimensat(s
, &fidp
->path
, times
);
1399 * If the only valid entry in iattr is ctime we can call
1400 * chown(-1,-1) to update the ctime of the file
1402 if ((v9iattr
.valid
& (ATTR_UID
| ATTR_GID
)) ||
1403 ((v9iattr
.valid
& ATTR_CTIME
)
1404 && !((v9iattr
.valid
& ATTR_MASK
) & ~ATTR_CTIME
))) {
1405 if (!(v9iattr
.valid
& ATTR_UID
)) {
1408 if (!(v9iattr
.valid
& ATTR_GID
)) {
1411 err
= v9fs_co_chown(s
, &fidp
->path
, v9iattr
.uid
,
1417 if (v9iattr
.valid
& (ATTR_SIZE
)) {
1418 err
= v9fs_co_truncate(s
, &fidp
->path
, v9iattr
.size
);
1427 complete_pdu(s
, pdu
, err
);
1430 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1434 offset
+= pdu_marshal(pdu
, offset
, "w", nwnames
);
1435 for (i
= 0; i
< nwnames
; i
++) {
1436 offset
+= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1441 static void v9fs_walk(void *opaque
)
1444 V9fsQID
*qids
= NULL
;
1446 V9fsPath dpath
, path
;
1450 int32_t fid
, newfid
;
1451 V9fsString
*wnames
= NULL
;
1453 V9fsFidState
*newfidp
= NULL
;;
1454 V9fsPDU
*pdu
= opaque
;
1455 V9fsState
*s
= pdu
->s
;
1457 offset
+= pdu_unmarshal(pdu
, offset
, "ddw", &fid
,
1460 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1461 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1462 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1463 for (i
= 0; i
< nwnames
; i
++) {
1464 offset
+= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1466 } else if (nwnames
> P9_MAXWELEM
) {
1470 fidp
= get_fid(s
, fid
);
1475 v9fs_path_init(&dpath
);
1476 v9fs_path_init(&path
);
1478 * Both dpath and path initially poin to fidp.
1479 * Needed to handle request with nwnames == 0
1481 v9fs_path_copy(&dpath
, &fidp
->path
);
1482 v9fs_path_copy(&path
, &fidp
->path
);
1483 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1484 err
= v9fs_co_name_to_path(s
, &dpath
, wnames
[name_idx
].data
, &path
);
1488 err
= v9fs_co_lstat(s
, &path
, &stbuf
);
1492 stat_to_qid(&stbuf
, &qids
[name_idx
]);
1493 v9fs_path_copy(&dpath
, &path
);
1495 if (fid
== newfid
) {
1496 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1497 v9fs_path_copy(&fidp
->path
, &path
);
1499 newfidp
= alloc_fid(s
, newfid
);
1500 if (newfidp
== NULL
) {
1504 newfidp
->uid
= fidp
->uid
;
1505 v9fs_path_copy(&newfidp
->path
, &path
);
1507 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1511 put_fid(s
, newfidp
);
1513 v9fs_path_free(&dpath
);
1514 v9fs_path_free(&path
);
1516 complete_pdu(s
, pdu
, err
);
1517 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1518 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1519 v9fs_string_free(&wnames
[name_idx
]);
1527 static int32_t get_iounit(V9fsState
*s
, V9fsPath
*path
)
1529 struct statfs stbuf
;
1533 * iounit should be multiples of f_bsize (host filesystem block size
1534 * and as well as less than (client msize - P9_IOHDRSZ))
1536 if (!v9fs_co_statfs(s
, path
, &stbuf
)) {
1537 iounit
= stbuf
.f_bsize
;
1538 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1541 iounit
= s
->msize
- P9_IOHDRSZ
;
1546 static void v9fs_open(void *opaque
)
1557 V9fsPDU
*pdu
= opaque
;
1558 V9fsState
*s
= pdu
->s
;
1560 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1561 pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1563 pdu_unmarshal(pdu
, offset
, "db", &fid
, &mode
);
1565 fidp
= get_fid(s
, fid
);
1570 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1572 err
= v9fs_co_lstat(s
, &fidp
->path
, &stbuf
);
1576 stat_to_qid(&stbuf
, &qid
);
1577 if (S_ISDIR(stbuf
.st_mode
)) {
1578 err
= v9fs_co_opendir(s
, fidp
);
1582 fidp
->fid_type
= P9_FID_DIR
;
1583 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1586 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1588 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
1589 /* Ignore direct disk access hint until the server supports it. */
1592 flags
= omode_to_uflags(mode
);
1594 err
= v9fs_co_open(s
, fidp
, flags
);
1598 fidp
->fid_type
= P9_FID_FILE
;
1599 fidp
->open_flags
= flags
;
1600 if (flags
& O_EXCL
) {
1602 * We let the host file system do O_EXCL check
1603 * We should not reclaim such fd
1605 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1607 iounit
= get_iounit(s
, &fidp
->path
);
1608 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1614 complete_pdu(s
, pdu
, err
);
1617 static void v9fs_lcreate(void *opaque
)
1619 int32_t dfid
, flags
, mode
;
1628 V9fsPDU
*pdu
= opaque
;
1630 pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
, &name
, &flags
,
1633 fidp
= get_fid(pdu
->s
, dfid
);
1639 /* Ignore direct disk access hint until the server supports it. */
1642 err
= v9fs_co_open2(pdu
->s
, fidp
, &name
, gid
,
1643 flags
| O_CREAT
, mode
, &stbuf
);
1647 fidp
->fid_type
= P9_FID_FILE
;
1648 fidp
->open_flags
= flags
;
1649 if (flags
& O_EXCL
) {
1651 * We let the host file system do O_EXCL check
1652 * We should not reclaim such fd
1654 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1656 iounit
= get_iounit(pdu
->s
, &fidp
->path
);
1657 stat_to_qid(&stbuf
, &qid
);
1658 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1661 put_fid(pdu
->s
, fidp
);
1663 complete_pdu(pdu
->s
, pdu
, err
);
1664 v9fs_string_free(&name
);
1667 static void v9fs_fsync(void *opaque
)
1674 V9fsPDU
*pdu
= opaque
;
1675 V9fsState
*s
= pdu
->s
;
1677 pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1678 fidp
= get_fid(s
, fid
);
1683 err
= v9fs_co_fsync(s
, fidp
, datasync
);
1689 complete_pdu(s
, pdu
, err
);
1692 static void v9fs_clunk(void *opaque
)
1698 V9fsPDU
*pdu
= opaque
;
1699 V9fsState
*s
= pdu
->s
;
1701 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1703 fidp
= get_fid(s
, fid
);
1708 err
= clunk_fid(s
, fidp
->fid
);
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;