]>
git.proxmox.com Git - qemu.git/blob - hw/9pfs/virtio-9p.c
c01c31aa2535cab17d53fd6179a3da66a654666c
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(V9fsPDU
*pdu
, V9fsFidState
*f
)
275 if (f
->fid_type
== P9_FID_FILE
) {
276 if (f
->fs
.fd
== -1) {
278 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
279 } while (err
== -EINTR
&& !pdu
->cancelled
);
281 } else if (f
->fid_type
== P9_FID_DIR
) {
282 if (f
->fs
.dir
== NULL
) {
284 err
= v9fs_co_opendir(pdu
, f
);
285 } while (err
== -EINTR
&& !pdu
->cancelled
);
291 static V9fsFidState
*get_fid(V9fsPDU
*pdu
, int32_t fid
)
295 V9fsState
*s
= pdu
->s
;
297 for (f
= s
->fid_list
; f
; f
= f
->next
) {
301 * Update the fid ref upfront so that
302 * we don't get reclaimed when we yield
307 * check whether we need to reopen the
308 * file. We might have closed the fd
309 * while trying to free up some file
312 err
= v9fs_reopen_fid(pdu
, f
);
318 * Mark the fid as referenced so that the LRU
319 * reclaim won't close the file descriptor
321 f
->flags
|= FID_REFERENCED
;
328 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
332 for (f
= s
->fid_list
; f
; f
= f
->next
) {
333 /* If fid is already there return NULL */
339 f
= g_malloc0(sizeof(V9fsFidState
));
341 f
->fid_type
= P9_FID_NONE
;
344 * Mark the fid as referenced so that the LRU
345 * reclaim won't close the file descriptor
347 f
->flags
|= FID_REFERENCED
;
348 f
->next
= s
->fid_list
;
354 static int v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
358 if (fidp
->fs
.xattr
.copied_len
== -1) {
359 /* getxattr/listxattr fid */
363 * if this is fid for setxattr. clunk should
364 * result in setxattr localcall
366 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
367 /* clunk after partial write */
371 if (fidp
->fs
.xattr
.len
) {
372 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
373 fidp
->fs
.xattr
.value
,
375 fidp
->fs
.xattr
.flags
);
377 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
380 v9fs_string_free(&fidp
->fs
.xattr
.name
);
382 if (fidp
->fs
.xattr
.value
) {
383 g_free(fidp
->fs
.xattr
.value
);
388 static int free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
392 if (fidp
->fid_type
== P9_FID_FILE
) {
393 /* If we reclaimed the fd no need to close */
394 if (fidp
->fs
.fd
!= -1) {
395 retval
= v9fs_co_close(pdu
, fidp
->fs
.fd
);
397 } else if (fidp
->fid_type
== P9_FID_DIR
) {
398 if (fidp
->fs
.dir
!= NULL
) {
399 retval
= v9fs_co_closedir(pdu
, fidp
->fs
.dir
);
401 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
402 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
404 v9fs_path_free(&fidp
->path
);
409 static void put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
414 * Don't free the fid if it is in reclaim list
416 if (!fidp
->ref
&& fidp
->clunked
) {
421 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
423 V9fsFidState
**fidpp
, *fidp
;
425 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
426 if ((*fidpp
)->fid
== fid
) {
430 if (*fidpp
== NULL
) {
439 void v9fs_reclaim_fd(V9fsPDU
*pdu
)
441 int reclaim_count
= 0;
442 V9fsState
*s
= pdu
->s
;
443 V9fsFidState
*f
, *reclaim_list
= NULL
;
445 for (f
= s
->fid_list
; f
; f
= f
->next
) {
447 * Unlink fids cannot be reclaimed. Check
448 * for them and skip them. Also skip fids
449 * currently being operated on.
451 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
455 * if it is a recently referenced fid
456 * we leave the fid untouched and clear the
457 * reference bit. We come back to it later
458 * in the next iteration. (a simple LRU without
459 * moving list elements around)
461 if (f
->flags
& FID_REFERENCED
) {
462 f
->flags
&= ~FID_REFERENCED
;
466 * Add fids to reclaim list.
468 if (f
->fid_type
== P9_FID_FILE
) {
469 if (f
->fs
.fd
!= -1) {
471 * Up the reference count so that
472 * a clunk request won't free this fid
475 f
->rclm_lst
= reclaim_list
;
477 f
->fs_reclaim
.fd
= f
->fs
.fd
;
481 } else if (f
->fid_type
== P9_FID_DIR
) {
482 if (f
->fs
.dir
!= NULL
) {
484 * Up the reference count so that
485 * a clunk request won't free this fid
488 f
->rclm_lst
= reclaim_list
;
490 f
->fs_reclaim
.dir
= f
->fs
.dir
;
495 if (reclaim_count
>= open_fd_rc
) {
500 * Now close the fid in reclaim list. Free them if they
501 * are already clunked.
503 while (reclaim_list
) {
505 reclaim_list
= f
->rclm_lst
;
506 if (f
->fid_type
== P9_FID_FILE
) {
507 v9fs_co_close(pdu
, f
->fs_reclaim
.fd
);
508 } else if (f
->fid_type
== P9_FID_DIR
) {
509 v9fs_co_closedir(pdu
, f
->fs_reclaim
.dir
);
513 * Now drop the fid reference, free it
520 static int v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
523 V9fsState
*s
= pdu
->s
;
524 V9fsFidState
*fidp
, head_fid
;
526 head_fid
.next
= s
->fid_list
;
527 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
528 if (fidp
->path
.size
!= path
->size
) {
531 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
532 /* Mark the fid non reclaimable. */
533 fidp
->flags
|= FID_NON_RECLAIMABLE
;
535 /* reopen the file/dir if already closed */
536 err
= v9fs_reopen_fid(pdu
, fidp
);
541 * Go back to head of fid list because
542 * the list could have got updated when
543 * switched to the worker thread
553 #define P9_QID_TYPE_DIR 0x80
554 #define P9_QID_TYPE_SYMLINK 0x02
556 #define P9_STAT_MODE_DIR 0x80000000
557 #define P9_STAT_MODE_APPEND 0x40000000
558 #define P9_STAT_MODE_EXCL 0x20000000
559 #define P9_STAT_MODE_MOUNT 0x10000000
560 #define P9_STAT_MODE_AUTH 0x08000000
561 #define P9_STAT_MODE_TMP 0x04000000
562 #define P9_STAT_MODE_SYMLINK 0x02000000
563 #define P9_STAT_MODE_LINK 0x01000000
564 #define P9_STAT_MODE_DEVICE 0x00800000
565 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
566 #define P9_STAT_MODE_SOCKET 0x00100000
567 #define P9_STAT_MODE_SETUID 0x00080000
568 #define P9_STAT_MODE_SETGID 0x00040000
569 #define P9_STAT_MODE_SETVTX 0x00010000
571 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
572 P9_STAT_MODE_SYMLINK | \
573 P9_STAT_MODE_LINK | \
574 P9_STAT_MODE_DEVICE | \
575 P9_STAT_MODE_NAMED_PIPE | \
578 /* This is the algorithm from ufs in spfs */
579 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
583 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
584 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
585 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
586 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
588 if (S_ISDIR(stbuf
->st_mode
)) {
589 qidp
->type
|= P9_QID_TYPE_DIR
;
591 if (S_ISLNK(stbuf
->st_mode
)) {
592 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
596 static int fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
601 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
605 stat_to_qid(&stbuf
, qidp
);
609 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
613 if (!QLIST_EMPTY(&s
->free_list
)) {
614 pdu
= QLIST_FIRST(&s
->free_list
);
615 QLIST_REMOVE(pdu
, next
);
616 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
621 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
628 * Cancelled pdu are added back to the freelist
631 if (!pdu
->cancelled
) {
632 QLIST_REMOVE(pdu
, next
);
633 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
638 size_t pdu_packunpack(void *addr
, struct iovec
*sg
, int sg_count
,
639 size_t offset
, size_t size
, int pack
)
644 for (i
= 0; size
&& i
< sg_count
; i
++) {
646 if (offset
>= sg
[i
].iov_len
) {
648 offset
-= sg
[i
].iov_len
;
651 len
= MIN(sg
[i
].iov_len
- offset
, size
);
653 memcpy(sg
[i
].iov_base
+ offset
, addr
, len
);
655 memcpy(addr
, sg
[i
].iov_base
+ offset
, len
);
670 static size_t pdu_unpack(void *dst
, V9fsPDU
*pdu
, size_t offset
, size_t size
)
672 return pdu_packunpack(dst
, pdu
->elem
.out_sg
, pdu
->elem
.out_num
,
676 static size_t pdu_pack(V9fsPDU
*pdu
, size_t offset
, const void *src
,
679 return pdu_packunpack((void *)src
, pdu
->elem
.in_sg
, pdu
->elem
.in_num
,
683 static int pdu_copy_sg(V9fsPDU
*pdu
, size_t offset
, int rx
, struct iovec
*sg
)
687 struct iovec
*src_sg
;
691 src_sg
= pdu
->elem
.in_sg
;
692 num
= pdu
->elem
.in_num
;
694 src_sg
= pdu
->elem
.out_sg
;
695 num
= pdu
->elem
.out_num
;
699 for (i
= 0; i
< num
; i
++) {
701 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
702 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
704 } else if (offset
< (src_sg
[i
].iov_len
+ pos
)) {
705 sg
[j
].iov_base
= src_sg
[i
].iov_base
;
706 sg
[j
].iov_len
= src_sg
[i
].iov_len
;
707 sg
[j
].iov_base
+= (offset
- pos
);
708 sg
[j
].iov_len
-= (offset
- pos
);
711 pos
+= src_sg
[i
].iov_len
;
717 static size_t pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
719 size_t old_offset
= offset
;
724 for (i
= 0; fmt
[i
]; i
++) {
727 uint8_t *valp
= va_arg(ap
, uint8_t *);
728 offset
+= pdu_unpack(valp
, pdu
, offset
, sizeof(*valp
));
733 valp
= va_arg(ap
, uint16_t *);
734 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
735 *valp
= le16_to_cpu(val
);
740 valp
= va_arg(ap
, uint32_t *);
741 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
742 *valp
= le32_to_cpu(val
);
747 valp
= va_arg(ap
, uint64_t *);
748 offset
+= pdu_unpack(&val
, pdu
, offset
, sizeof(val
));
749 *valp
= le64_to_cpu(val
);
753 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
754 int *iovcnt
= va_arg(ap
, int *);
755 *iovcnt
= pdu_copy_sg(pdu
, offset
, 0, iov
);
759 V9fsString
*str
= va_arg(ap
, V9fsString
*);
760 offset
+= pdu_unmarshal(pdu
, offset
, "w", &str
->size
);
761 /* FIXME: sanity check str->size */
762 str
->data
= g_malloc(str
->size
+ 1);
763 offset
+= pdu_unpack(str
->data
, pdu
, offset
, str
->size
);
764 str
->data
[str
->size
] = 0;
768 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
769 offset
+= pdu_unmarshal(pdu
, offset
, "bdq",
770 &qidp
->type
, &qidp
->version
, &qidp
->path
);
774 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
775 offset
+= pdu_unmarshal(pdu
, offset
, "wwdQdddqsssssddd",
776 &statp
->size
, &statp
->type
, &statp
->dev
,
777 &statp
->qid
, &statp
->mode
, &statp
->atime
,
778 &statp
->mtime
, &statp
->length
,
779 &statp
->name
, &statp
->uid
, &statp
->gid
,
780 &statp
->muid
, &statp
->extension
,
781 &statp
->n_uid
, &statp
->n_gid
,
786 V9fsIattr
*iattr
= va_arg(ap
, V9fsIattr
*);
787 offset
+= pdu_unmarshal(pdu
, offset
, "ddddqqqqq",
788 &iattr
->valid
, &iattr
->mode
,
789 &iattr
->uid
, &iattr
->gid
, &iattr
->size
,
790 &iattr
->atime_sec
, &iattr
->atime_nsec
,
791 &iattr
->mtime_sec
, &iattr
->mtime_nsec
);
801 return offset
- old_offset
;
804 static size_t pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
806 size_t old_offset
= offset
;
811 for (i
= 0; fmt
[i
]; i
++) {
814 uint8_t val
= va_arg(ap
, int);
815 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
820 cpu_to_le16w(&val
, va_arg(ap
, int));
821 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
826 cpu_to_le32w(&val
, va_arg(ap
, uint32_t));
827 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
832 cpu_to_le64w(&val
, va_arg(ap
, uint64_t));
833 offset
+= pdu_pack(pdu
, offset
, &val
, sizeof(val
));
837 struct iovec
*iov
= va_arg(ap
, struct iovec
*);
838 int *iovcnt
= va_arg(ap
, int *);
839 *iovcnt
= pdu_copy_sg(pdu
, offset
, 1, iov
);
843 V9fsString
*str
= va_arg(ap
, V9fsString
*);
844 offset
+= pdu_marshal(pdu
, offset
, "w", str
->size
);
845 offset
+= pdu_pack(pdu
, offset
, str
->data
, str
->size
);
849 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
850 offset
+= pdu_marshal(pdu
, offset
, "bdq",
851 qidp
->type
, qidp
->version
, qidp
->path
);
855 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
856 offset
+= pdu_marshal(pdu
, offset
, "wwdQdddqsssssddd",
857 statp
->size
, statp
->type
, statp
->dev
,
858 &statp
->qid
, statp
->mode
, statp
->atime
,
859 statp
->mtime
, statp
->length
, &statp
->name
,
860 &statp
->uid
, &statp
->gid
, &statp
->muid
,
861 &statp
->extension
, statp
->n_uid
,
862 statp
->n_gid
, statp
->n_muid
);
866 V9fsStatDotl
*statp
= va_arg(ap
, V9fsStatDotl
*);
867 offset
+= pdu_marshal(pdu
, offset
, "qQdddqqqqqqqqqqqqqqq",
868 statp
->st_result_mask
,
869 &statp
->qid
, statp
->st_mode
,
870 statp
->st_uid
, statp
->st_gid
,
871 statp
->st_nlink
, statp
->st_rdev
,
872 statp
->st_size
, statp
->st_blksize
, statp
->st_blocks
,
873 statp
->st_atime_sec
, statp
->st_atime_nsec
,
874 statp
->st_mtime_sec
, statp
->st_mtime_nsec
,
875 statp
->st_ctime_sec
, statp
->st_ctime_nsec
,
876 statp
->st_btime_sec
, statp
->st_btime_nsec
,
877 statp
->st_gen
, statp
->st_data_version
);
886 return offset
- old_offset
;
889 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
891 int8_t id
= pdu
->id
+ 1; /* Response */
897 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
900 str
.data
= strerror(err
);
901 str
.size
= strlen(str
.data
);
903 len
+= pdu_marshal(pdu
, len
, "s", &str
);
907 len
+= pdu_marshal(pdu
, len
, "d", err
);
909 if (s
->proto_version
== V9FS_PROTO_2000L
) {
914 /* fill out the header */
915 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
917 /* keep these in sync */
921 /* push onto queue and notify */
922 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
924 /* FIXME: we should batch these completions */
925 virtio_notify(&s
->vdev
, s
->vq
);
927 /* Now wakeup anybody waiting in flush for this request */
928 qemu_co_queue_next(&pdu
->complete
);
933 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
938 if (mode
& P9_STAT_MODE_DIR
) {
942 if (mode
& P9_STAT_MODE_SYMLINK
) {
945 if (mode
& P9_STAT_MODE_SOCKET
) {
948 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
951 if (mode
& P9_STAT_MODE_DEVICE
) {
952 if (extension
&& extension
->data
[0] == 'c') {
963 if (mode
& P9_STAT_MODE_SETUID
) {
966 if (mode
& P9_STAT_MODE_SETGID
) {
969 if (mode
& P9_STAT_MODE_SETVTX
) {
976 static int donttouch_stat(V9fsStat
*stat
)
978 if (stat
->type
== -1 &&
980 stat
->qid
.type
== -1 &&
981 stat
->qid
.version
== -1 &&
982 stat
->qid
.path
== -1 &&
986 stat
->length
== -1 &&
993 stat
->n_muid
== -1) {
1000 static void v9fs_stat_free(V9fsStat
*stat
)
1002 v9fs_string_free(&stat
->name
);
1003 v9fs_string_free(&stat
->uid
);
1004 v9fs_string_free(&stat
->gid
);
1005 v9fs_string_free(&stat
->muid
);
1006 v9fs_string_free(&stat
->extension
);
1009 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
1013 mode
= stbuf
->st_mode
& 0777;
1014 if (S_ISDIR(stbuf
->st_mode
)) {
1015 mode
|= P9_STAT_MODE_DIR
;
1018 if (S_ISLNK(stbuf
->st_mode
)) {
1019 mode
|= P9_STAT_MODE_SYMLINK
;
1022 if (S_ISSOCK(stbuf
->st_mode
)) {
1023 mode
|= P9_STAT_MODE_SOCKET
;
1026 if (S_ISFIFO(stbuf
->st_mode
)) {
1027 mode
|= P9_STAT_MODE_NAMED_PIPE
;
1030 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
1031 mode
|= P9_STAT_MODE_DEVICE
;
1034 if (stbuf
->st_mode
& S_ISUID
) {
1035 mode
|= P9_STAT_MODE_SETUID
;
1038 if (stbuf
->st_mode
& S_ISGID
) {
1039 mode
|= P9_STAT_MODE_SETGID
;
1042 if (stbuf
->st_mode
& S_ISVTX
) {
1043 mode
|= P9_STAT_MODE_SETVTX
;
1049 static int stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*name
,
1050 const struct stat
*stbuf
,
1056 memset(v9stat
, 0, sizeof(*v9stat
));
1058 stat_to_qid(stbuf
, &v9stat
->qid
);
1059 v9stat
->mode
= stat_to_v9mode(stbuf
);
1060 v9stat
->atime
= stbuf
->st_atime
;
1061 v9stat
->mtime
= stbuf
->st_mtime
;
1062 v9stat
->length
= stbuf
->st_size
;
1064 v9fs_string_null(&v9stat
->uid
);
1065 v9fs_string_null(&v9stat
->gid
);
1066 v9fs_string_null(&v9stat
->muid
);
1068 v9stat
->n_uid
= stbuf
->st_uid
;
1069 v9stat
->n_gid
= stbuf
->st_gid
;
1072 v9fs_string_null(&v9stat
->extension
);
1074 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
1075 err
= v9fs_co_readlink(pdu
, name
, &v9stat
->extension
);
1079 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
1080 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
1081 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
1082 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
1083 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
1084 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
1085 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
1088 str
= strrchr(name
->data
, '/');
1095 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
1098 v9fs_string_size(&v9stat
->name
) +
1099 v9fs_string_size(&v9stat
->uid
) +
1100 v9fs_string_size(&v9stat
->gid
) +
1101 v9fs_string_size(&v9stat
->muid
) +
1102 v9fs_string_size(&v9stat
->extension
);
1106 #define P9_STATS_MODE 0x00000001ULL
1107 #define P9_STATS_NLINK 0x00000002ULL
1108 #define P9_STATS_UID 0x00000004ULL
1109 #define P9_STATS_GID 0x00000008ULL
1110 #define P9_STATS_RDEV 0x00000010ULL
1111 #define P9_STATS_ATIME 0x00000020ULL
1112 #define P9_STATS_MTIME 0x00000040ULL
1113 #define P9_STATS_CTIME 0x00000080ULL
1114 #define P9_STATS_INO 0x00000100ULL
1115 #define P9_STATS_SIZE 0x00000200ULL
1116 #define P9_STATS_BLOCKS 0x00000400ULL
1118 #define P9_STATS_BTIME 0x00000800ULL
1119 #define P9_STATS_GEN 0x00001000ULL
1120 #define P9_STATS_DATA_VERSION 0x00002000ULL
1122 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1123 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1126 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
1127 V9fsStatDotl
*v9lstat
)
1129 memset(v9lstat
, 0, sizeof(*v9lstat
));
1131 v9lstat
->st_mode
= stbuf
->st_mode
;
1132 v9lstat
->st_nlink
= stbuf
->st_nlink
;
1133 v9lstat
->st_uid
= stbuf
->st_uid
;
1134 v9lstat
->st_gid
= stbuf
->st_gid
;
1135 v9lstat
->st_rdev
= stbuf
->st_rdev
;
1136 v9lstat
->st_size
= stbuf
->st_size
;
1137 v9lstat
->st_blksize
= stbuf
->st_blksize
;
1138 v9lstat
->st_blocks
= stbuf
->st_blocks
;
1139 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
1140 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
1141 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
1142 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
1143 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
1144 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
1145 /* Currently we only support BASIC fields in stat */
1146 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
1148 stat_to_qid(stbuf
, &v9lstat
->qid
);
1151 static struct iovec
*adjust_sg(struct iovec
*sg
, int len
, int *iovcnt
)
1153 while (len
&& *iovcnt
) {
1154 if (len
< sg
->iov_len
) {
1156 sg
->iov_base
+= len
;
1168 static struct iovec
*cap_sg(struct iovec
*sg
, int cap
, int *cnt
)
1173 for (i
= 0; i
< *cnt
; i
++) {
1174 if ((total
+ sg
[i
].iov_len
) > cap
) {
1175 sg
[i
].iov_len
-= ((total
+ sg
[i
].iov_len
) - cap
);
1179 total
+= sg
[i
].iov_len
;
1187 static void print_sg(struct iovec
*sg
, int cnt
)
1191 printf("sg[%d]: {", cnt
);
1192 for (i
= 0; i
< cnt
; i
++) {
1196 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
1201 /* Will call this only for path name based fid */
1202 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
1205 v9fs_path_init(&str
);
1206 v9fs_path_copy(&str
, dst
);
1207 v9fs_string_sprintf((V9fsString
*)dst
, "%s%s", src
->data
, str
.data
+len
);
1208 v9fs_path_free(&str
);
1209 /* +1 to include terminating NULL */
1213 static void v9fs_version(void *opaque
)
1215 V9fsPDU
*pdu
= opaque
;
1216 V9fsState
*s
= pdu
->s
;
1220 pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
1222 if (!strcmp(version
.data
, "9P2000.u")) {
1223 s
->proto_version
= V9FS_PROTO_2000U
;
1224 } else if (!strcmp(version
.data
, "9P2000.L")) {
1225 s
->proto_version
= V9FS_PROTO_2000L
;
1227 v9fs_string_sprintf(&version
, "unknown");
1230 offset
+= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
1231 complete_pdu(s
, pdu
, offset
);
1233 v9fs_string_free(&version
);
1237 static void v9fs_attach(void *opaque
)
1239 V9fsPDU
*pdu
= opaque
;
1240 V9fsState
*s
= pdu
->s
;
1241 int32_t fid
, afid
, n_uname
;
1242 V9fsString uname
, aname
;
1248 pdu_unmarshal(pdu
, offset
, "ddssd", &fid
, &afid
, &uname
, &aname
, &n_uname
);
1250 fidp
= alloc_fid(s
, fid
);
1255 fidp
->uid
= n_uname
;
1256 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
1262 err
= fid_to_qid(pdu
, fidp
, &qid
);
1268 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
1273 complete_pdu(s
, pdu
, err
);
1274 v9fs_string_free(&uname
);
1275 v9fs_string_free(&aname
);
1278 static void v9fs_stat(void *opaque
)
1286 V9fsPDU
*pdu
= opaque
;
1287 V9fsState
*s
= pdu
->s
;
1289 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1291 fidp
= get_fid(pdu
, fid
);
1296 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1300 err
= stat_to_v9stat(pdu
, &fidp
->path
, &stbuf
, &v9stat
);
1304 offset
+= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1306 v9fs_stat_free(&v9stat
);
1310 complete_pdu(s
, pdu
, err
);
1313 static void v9fs_getattr(void *opaque
)
1320 uint64_t request_mask
;
1321 V9fsStatDotl v9stat_dotl
;
1322 V9fsPDU
*pdu
= opaque
;
1323 V9fsState
*s
= pdu
->s
;
1325 pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1327 fidp
= get_fid(pdu
, fid
);
1333 * Currently we only support BASIC fields in stat, so there is no
1334 * need to look at request_mask.
1336 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1340 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1342 retval
+= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1346 complete_pdu(s
, pdu
, retval
);
1349 /* From Linux kernel code */
1350 #define ATTR_MODE (1 << 0)
1351 #define ATTR_UID (1 << 1)
1352 #define ATTR_GID (1 << 2)
1353 #define ATTR_SIZE (1 << 3)
1354 #define ATTR_ATIME (1 << 4)
1355 #define ATTR_MTIME (1 << 5)
1356 #define ATTR_CTIME (1 << 6)
1357 #define ATTR_MASK 127
1358 #define ATTR_ATIME_SET (1 << 7)
1359 #define ATTR_MTIME_SET (1 << 8)
1361 static void v9fs_setattr(void *opaque
)
1368 V9fsPDU
*pdu
= opaque
;
1369 V9fsState
*s
= pdu
->s
;
1371 pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1373 fidp
= get_fid(pdu
, fid
);
1378 if (v9iattr
.valid
& ATTR_MODE
) {
1379 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1384 if (v9iattr
.valid
& (ATTR_ATIME
| ATTR_MTIME
)) {
1385 struct timespec times
[2];
1386 if (v9iattr
.valid
& ATTR_ATIME
) {
1387 if (v9iattr
.valid
& ATTR_ATIME_SET
) {
1388 times
[0].tv_sec
= v9iattr
.atime_sec
;
1389 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1391 times
[0].tv_nsec
= UTIME_NOW
;
1394 times
[0].tv_nsec
= UTIME_OMIT
;
1396 if (v9iattr
.valid
& ATTR_MTIME
) {
1397 if (v9iattr
.valid
& ATTR_MTIME_SET
) {
1398 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1399 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1401 times
[1].tv_nsec
= UTIME_NOW
;
1404 times
[1].tv_nsec
= UTIME_OMIT
;
1406 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1412 * If the only valid entry in iattr is ctime we can call
1413 * chown(-1,-1) to update the ctime of the file
1415 if ((v9iattr
.valid
& (ATTR_UID
| ATTR_GID
)) ||
1416 ((v9iattr
.valid
& ATTR_CTIME
)
1417 && !((v9iattr
.valid
& ATTR_MASK
) & ~ATTR_CTIME
))) {
1418 if (!(v9iattr
.valid
& ATTR_UID
)) {
1421 if (!(v9iattr
.valid
& ATTR_GID
)) {
1424 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1430 if (v9iattr
.valid
& (ATTR_SIZE
)) {
1431 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1440 complete_pdu(s
, pdu
, err
);
1443 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1447 offset
+= pdu_marshal(pdu
, offset
, "w", nwnames
);
1448 for (i
= 0; i
< nwnames
; i
++) {
1449 offset
+= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1454 static void v9fs_walk(void *opaque
)
1457 V9fsQID
*qids
= NULL
;
1459 V9fsPath dpath
, path
;
1463 int32_t fid
, newfid
;
1464 V9fsString
*wnames
= NULL
;
1466 V9fsFidState
*newfidp
= NULL
;;
1467 V9fsPDU
*pdu
= opaque
;
1468 V9fsState
*s
= pdu
->s
;
1470 offset
+= pdu_unmarshal(pdu
, offset
, "ddw", &fid
,
1473 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1474 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1475 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1476 for (i
= 0; i
< nwnames
; i
++) {
1477 offset
+= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1479 } else if (nwnames
> P9_MAXWELEM
) {
1483 fidp
= get_fid(pdu
, fid
);
1488 v9fs_path_init(&dpath
);
1489 v9fs_path_init(&path
);
1491 * Both dpath and path initially poin to fidp.
1492 * Needed to handle request with nwnames == 0
1494 v9fs_path_copy(&dpath
, &fidp
->path
);
1495 v9fs_path_copy(&path
, &fidp
->path
);
1496 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1497 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
, &path
);
1501 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1505 stat_to_qid(&stbuf
, &qids
[name_idx
]);
1506 v9fs_path_copy(&dpath
, &path
);
1508 if (fid
== newfid
) {
1509 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1510 v9fs_path_copy(&fidp
->path
, &path
);
1512 newfidp
= alloc_fid(s
, newfid
);
1513 if (newfidp
== NULL
) {
1517 newfidp
->uid
= fidp
->uid
;
1518 v9fs_path_copy(&newfidp
->path
, &path
);
1520 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1524 put_fid(pdu
, newfidp
);
1526 v9fs_path_free(&dpath
);
1527 v9fs_path_free(&path
);
1529 complete_pdu(s
, pdu
, err
);
1530 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1531 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1532 v9fs_string_free(&wnames
[name_idx
]);
1540 static int32_t get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1542 struct statfs stbuf
;
1544 V9fsState
*s
= pdu
->s
;
1547 * iounit should be multiples of f_bsize (host filesystem block size
1548 * and as well as less than (client msize - P9_IOHDRSZ))
1550 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1551 iounit
= stbuf
.f_bsize
;
1552 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1555 iounit
= s
->msize
- P9_IOHDRSZ
;
1560 static void v9fs_open(void *opaque
)
1571 V9fsPDU
*pdu
= opaque
;
1572 V9fsState
*s
= pdu
->s
;
1574 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1575 pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1577 pdu_unmarshal(pdu
, offset
, "db", &fid
, &mode
);
1579 fidp
= get_fid(pdu
, fid
);
1584 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1586 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1590 stat_to_qid(&stbuf
, &qid
);
1591 if (S_ISDIR(stbuf
.st_mode
)) {
1592 err
= v9fs_co_opendir(pdu
, fidp
);
1596 fidp
->fid_type
= P9_FID_DIR
;
1597 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1600 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1602 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
1603 /* Ignore direct disk access hint until the server supports it. */
1606 flags
= omode_to_uflags(mode
);
1608 err
= v9fs_co_open(pdu
, fidp
, flags
);
1612 fidp
->fid_type
= P9_FID_FILE
;
1613 fidp
->open_flags
= flags
;
1614 if (flags
& O_EXCL
) {
1616 * We let the host file system do O_EXCL check
1617 * We should not reclaim such fd
1619 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1621 iounit
= get_iounit(pdu
, &fidp
->path
);
1622 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1628 complete_pdu(s
, pdu
, err
);
1631 static void v9fs_lcreate(void *opaque
)
1633 int32_t dfid
, flags
, mode
;
1642 V9fsPDU
*pdu
= opaque
;
1644 pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
, &name
, &flags
,
1647 fidp
= get_fid(pdu
, dfid
);
1653 /* Ignore direct disk access hint until the server supports it. */
1655 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1656 flags
| O_CREAT
, mode
, &stbuf
);
1660 fidp
->fid_type
= P9_FID_FILE
;
1661 fidp
->open_flags
= flags
;
1662 if (flags
& O_EXCL
) {
1664 * We let the host file system do O_EXCL check
1665 * We should not reclaim such fd
1667 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1669 iounit
= get_iounit(pdu
, &fidp
->path
);
1670 stat_to_qid(&stbuf
, &qid
);
1671 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1676 complete_pdu(pdu
->s
, pdu
, err
);
1677 v9fs_string_free(&name
);
1680 static void v9fs_fsync(void *opaque
)
1687 V9fsPDU
*pdu
= opaque
;
1688 V9fsState
*s
= pdu
->s
;
1690 pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1691 fidp
= get_fid(pdu
, fid
);
1696 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1702 complete_pdu(s
, pdu
, err
);
1705 static void v9fs_clunk(void *opaque
)
1711 V9fsPDU
*pdu
= opaque
;
1712 V9fsState
*s
= pdu
->s
;
1714 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1716 fidp
= clunk_fid(s
, fid
);
1722 * Bump the ref so that put_fid will
1730 complete_pdu(s
, pdu
, err
);
1733 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
,
1734 V9fsFidState
*fidp
, int64_t off
, int32_t max_count
)
1740 xattr_len
= fidp
->fs
.xattr
.len
;
1741 read_count
= xattr_len
- off
;
1742 if (read_count
> max_count
) {
1743 read_count
= max_count
;
1744 } else if (read_count
< 0) {
1746 * read beyond XATTR value
1750 offset
+= pdu_marshal(pdu
, offset
, "d", read_count
);
1751 offset
+= pdu_pack(pdu
, offset
,
1752 ((char *)fidp
->fs
.xattr
.value
) + off
,
1757 static int v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1758 V9fsFidState
*fidp
, int32_t max_count
)
1765 off_t saved_dir_pos
;
1766 struct dirent
*dent
, *result
;
1768 /* save the directory position */
1769 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1770 if (saved_dir_pos
< 0) {
1771 return saved_dir_pos
;
1774 dent
= g_malloc(sizeof(struct dirent
));
1777 v9fs_path_init(&path
);
1778 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
1779 if (err
|| !result
) {
1782 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1786 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1790 err
= stat_to_v9stat(pdu
, &path
, &stbuf
, &v9stat
);
1794 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1795 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1796 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1797 /* Ran out of buffer. Set dir back to old position and return */
1798 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1799 v9fs_stat_free(&v9stat
);
1800 v9fs_path_free(&path
);
1805 v9fs_stat_free(&v9stat
);
1806 v9fs_path_free(&path
);
1807 saved_dir_pos
= dent
->d_off
;
1811 v9fs_path_free(&path
);
1818 static void v9fs_read(void *opaque
)
1827 V9fsPDU
*pdu
= opaque
;
1828 V9fsState
*s
= pdu
->s
;
1830 pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1832 fidp
= get_fid(pdu
, fid
);
1837 if (fidp
->fid_type
== P9_FID_DIR
) {
1840 v9fs_co_rewinddir(pdu
, fidp
);
1842 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1848 err
+= pdu_marshal(pdu
, offset
, "d", count
);
1850 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1854 struct iovec iov
[128]; /* FIXME: bad, bad, bad */
1857 pdu_marshal(pdu
, offset
+ 4, "v", sg
, &cnt
);
1858 sg
= cap_sg(sg
, max_count
, &cnt
);
1863 /* Loop in case of EINTR */
1865 len
= v9fs_co_preadv(pdu
, fidp
, sg
, cnt
, off
);
1870 } while (len
== -EINTR
&& !pdu
->cancelled
);
1872 /* IO error return the error */
1876 sg
= adjust_sg(sg
, len
, &cnt
);
1877 } while (count
< max_count
&& len
> 0);
1879 err
+= pdu_marshal(pdu
, offset
, "d", count
);
1881 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1882 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1889 complete_pdu(s
, pdu
, err
);
1892 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1895 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1896 * size of type (1) + size of name.size (2) + strlen(name.data)
1898 return 24 + v9fs_string_size(name
);
1901 static int v9fs_do_readdir(V9fsPDU
*pdu
,
1902 V9fsFidState
*fidp
, int32_t max_count
)
1909 off_t saved_dir_pos
;
1910 struct dirent
*dent
, *result
;
1912 /* save the directory position */
1913 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1914 if (saved_dir_pos
< 0) {
1915 return saved_dir_pos
;
1918 dent
= g_malloc(sizeof(struct dirent
));
1921 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
1922 if (err
|| !result
) {
1925 v9fs_string_init(&name
);
1926 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1927 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1928 /* Ran out of buffer. Set dir back to old position and return */
1929 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1930 v9fs_string_free(&name
);
1935 * Fill up just the path field of qid because the client uses
1936 * only that. To fill the entire qid structure we will have
1937 * to stat each dirent found, which is expensive
1939 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1940 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1941 /* Fill the other fields with dummy values */
1945 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1946 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1948 dent
->d_type
, &name
);
1950 v9fs_string_free(&name
);
1951 saved_dir_pos
= dent
->d_off
;
1960 static void v9fs_readdir(void *opaque
)
1966 int64_t initial_offset
;
1967 int32_t count
, max_count
;
1968 V9fsPDU
*pdu
= opaque
;
1969 V9fsState
*s
= pdu
->s
;
1971 pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &initial_offset
, &max_count
);
1973 fidp
= get_fid(pdu
, fid
);
1978 if (!fidp
->fs
.dir
) {
1982 if (initial_offset
== 0) {
1983 v9fs_co_rewinddir(pdu
, fidp
);
1985 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
1987 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
1993 retval
+= pdu_marshal(pdu
, offset
, "d", count
);
1998 complete_pdu(s
, pdu
, retval
);
2001 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2002 int64_t off
, int32_t count
,
2003 struct iovec
*sg
, int cnt
)
2012 xattr_len
= fidp
->fs
.xattr
.len
;
2013 write_count
= xattr_len
- off
;
2014 if (write_count
> count
) {
2015 write_count
= count
;
2016 } else if (write_count
< 0) {
2018 * write beyond XATTR value len specified in
2024 offset
+= pdu_marshal(pdu
, offset
, "d", write_count
);
2026 fidp
->fs
.xattr
.copied_len
+= write_count
;
2028 * Now copy the content from sg list
2030 for (i
= 0; i
< cnt
; i
++) {
2031 if (write_count
> sg
[i
].iov_len
) {
2032 to_copy
= sg
[i
].iov_len
;
2034 to_copy
= write_count
;
2036 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
2037 /* updating vs->off since we are not using below */
2039 write_count
-= to_copy
;
2045 static void v9fs_write(void *opaque
)
2056 struct iovec iov
[128]; /* FIXME: bad, bad, bad */
2057 struct iovec
*sg
= iov
;
2058 V9fsPDU
*pdu
= opaque
;
2059 V9fsState
*s
= pdu
->s
;
2061 pdu_unmarshal(pdu
, offset
, "dqdv", &fid
, &off
, &count
, sg
, &cnt
);
2063 fidp
= get_fid(pdu
, fid
);
2068 if (fidp
->fid_type
== P9_FID_FILE
) {
2069 if (fidp
->fs
.fd
== -1) {
2073 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2075 * setxattr operation
2077 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
, sg
, cnt
);
2083 sg
= cap_sg(sg
, count
, &cnt
);
2088 /* Loop in case of EINTR */
2090 len
= v9fs_co_pwritev(pdu
, fidp
, sg
, cnt
, off
);
2095 } while (len
== -EINTR
&& !pdu
->cancelled
);
2097 /* IO error return the error */
2101 sg
= adjust_sg(sg
, len
, &cnt
);
2102 } while (total
< count
&& len
> 0);
2103 offset
+= pdu_marshal(pdu
, offset
, "d", total
);
2108 complete_pdu(s
, pdu
, err
);
2111 static void v9fs_create(void *opaque
)
2123 V9fsString extension
;
2125 V9fsPDU
*pdu
= opaque
;
2127 v9fs_path_init(&path
);
2129 pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2130 &perm
, &mode
, &extension
);
2132 fidp
= get_fid(pdu
, fid
);
2137 if (perm
& P9_STAT_MODE_DIR
) {
2138 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2139 fidp
->uid
, -1, &stbuf
);
2143 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2147 v9fs_path_copy(&fidp
->path
, &path
);
2148 err
= v9fs_co_opendir(pdu
, fidp
);
2152 fidp
->fid_type
= P9_FID_DIR
;
2153 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2154 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2155 extension
.data
, -1 , &stbuf
);
2159 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2163 v9fs_path_copy(&fidp
->path
, &path
);
2164 } else if (perm
& P9_STAT_MODE_LINK
) {
2165 int32_t ofid
= atoi(extension
.data
);
2166 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2167 if (ofidp
== NULL
) {
2171 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2172 put_fid(pdu
, ofidp
);
2176 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2178 fidp
->fid_type
= P9_FID_NONE
;
2181 v9fs_path_copy(&fidp
->path
, &path
);
2182 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2184 fidp
->fid_type
= P9_FID_NONE
;
2187 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2189 uint32_t major
, minor
;
2192 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2209 nmode
|= perm
& 0777;
2210 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2211 makedev(major
, minor
), nmode
, &stbuf
);
2215 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2219 v9fs_path_copy(&fidp
->path
, &path
);
2220 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2221 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2222 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2226 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2230 v9fs_path_copy(&fidp
->path
, &path
);
2231 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2232 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2233 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2237 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2241 v9fs_path_copy(&fidp
->path
, &path
);
2243 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2244 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2248 fidp
->fid_type
= P9_FID_FILE
;
2249 fidp
->open_flags
= omode_to_uflags(mode
);
2250 if (fidp
->open_flags
& O_EXCL
) {
2252 * We let the host file system do O_EXCL check
2253 * We should not reclaim such fd
2255 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2258 iounit
= get_iounit(pdu
, &fidp
->path
);
2259 stat_to_qid(&stbuf
, &qid
);
2260 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2265 complete_pdu(pdu
->s
, pdu
, err
);
2266 v9fs_string_free(&name
);
2267 v9fs_string_free(&extension
);
2268 v9fs_path_free(&path
);
2271 static void v9fs_symlink(void *opaque
)
2273 V9fsPDU
*pdu
= opaque
;
2276 V9fsFidState
*dfidp
;
2284 pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2286 dfidp
= get_fid(pdu
, dfid
);
2287 if (dfidp
== NULL
) {
2291 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2295 stat_to_qid(&stbuf
, &qid
);
2296 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
2299 put_fid(pdu
, dfidp
);
2301 complete_pdu(pdu
->s
, pdu
, err
);
2302 v9fs_string_free(&name
);
2303 v9fs_string_free(&symname
);
2306 static void v9fs_flush(void *opaque
)
2310 V9fsPDU
*cancel_pdu
;
2311 V9fsPDU
*pdu
= opaque
;
2312 V9fsState
*s
= pdu
->s
;
2314 pdu_unmarshal(pdu
, offset
, "w", &tag
);
2316 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2317 if (cancel_pdu
->tag
== tag
) {
2322 cancel_pdu
->cancelled
= 1;
2324 * Wait for pdu to complete.
2326 qemu_co_queue_wait(&cancel_pdu
->complete
);
2327 cancel_pdu
->cancelled
= 0;
2328 free_pdu(pdu
->s
, cancel_pdu
);
2330 complete_pdu(s
, pdu
, 7);
2334 static void v9fs_link(void *opaque
)
2336 V9fsPDU
*pdu
= opaque
;
2337 V9fsState
*s
= pdu
->s
;
2338 int32_t dfid
, oldfid
;
2339 V9fsFidState
*dfidp
, *oldfidp
;
2344 pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2346 dfidp
= get_fid(pdu
, dfid
);
2347 if (dfidp
== NULL
) {
2352 oldfidp
= get_fid(pdu
, oldfid
);
2353 if (oldfidp
== NULL
) {
2357 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2362 put_fid(pdu
, dfidp
);
2364 v9fs_string_free(&name
);
2365 complete_pdu(s
, pdu
, err
);
2368 /* Only works with path name based fid */
2369 static void v9fs_remove(void *opaque
)
2375 V9fsPDU
*pdu
= opaque
;
2377 pdu_unmarshal(pdu
, offset
, "d", &fid
);
2379 fidp
= get_fid(pdu
, fid
);
2384 /* if fs driver is not path based, return EOPNOTSUPP */
2385 if (!pdu
->s
->ctx
.flags
& PATHNAME_FSCONTEXT
) {
2390 * IF the file is unlinked, we cannot reopen
2391 * the file later. So don't reclaim fd
2393 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2397 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2402 /* For TREMOVE we need to clunk the fid even on failed remove */
2403 clunk_fid(pdu
->s
, fidp
->fid
);
2406 complete_pdu(pdu
->s
, pdu
, err
);
2409 static void v9fs_unlinkat(void *opaque
)
2413 int32_t dfid
, flags
;
2416 V9fsFidState
*dfidp
;
2417 V9fsPDU
*pdu
= opaque
;
2419 pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2421 dfidp
= get_fid(pdu
, dfid
);
2422 if (dfidp
== NULL
) {
2427 * IF the file is unlinked, we cannot reopen
2428 * the file later. So don't reclaim fd
2430 v9fs_path_init(&path
);
2431 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2435 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2439 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2444 put_fid(pdu
, dfidp
);
2445 v9fs_path_free(&path
);
2447 complete_pdu(pdu
->s
, pdu
, err
);
2448 v9fs_string_free(&name
);
2452 /* Only works with path name based fid */
2453 static int v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2454 int32_t newdirfid
, V9fsString
*name
)
2459 V9fsFidState
*tfidp
;
2460 V9fsState
*s
= pdu
->s
;
2461 V9fsFidState
*dirfidp
= NULL
;
2462 char *old_name
, *new_name
;
2464 v9fs_path_init(&new_path
);
2465 if (newdirfid
!= -1) {
2466 dirfidp
= get_fid(pdu
, newdirfid
);
2467 if (dirfidp
== NULL
) {
2471 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2472 v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2474 old_name
= fidp
->path
.data
;
2475 end
= strrchr(old_name
, '/');
2481 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2482 strncat(new_name
, old_name
, end
- old_name
);
2483 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2484 v9fs_co_name_to_path(pdu
, NULL
, new_name
, &new_path
);
2487 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2492 * Fixup fid's pointing to the old name to
2493 * start pointing to the new name
2495 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2496 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2497 /* replace the name */
2498 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2503 put_fid(pdu
, dirfidp
);
2505 v9fs_path_free(&new_path
);
2510 /* Only works with path name based fid */
2511 static void v9fs_rename(void *opaque
)
2519 V9fsPDU
*pdu
= opaque
;
2520 V9fsState
*s
= pdu
->s
;
2522 pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2524 fidp
= get_fid(pdu
, fid
);
2529 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
2530 /* if fs driver is not path based, return EOPNOTSUPP */
2531 if (!pdu
->s
->ctx
.flags
& PATHNAME_FSCONTEXT
) {
2535 v9fs_path_write_lock(s
);
2536 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2537 v9fs_path_unlock(s
);
2544 complete_pdu(s
, pdu
, err
);
2545 v9fs_string_free(&name
);
2548 static void v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2549 V9fsString
*old_name
, V9fsPath
*newdir
,
2550 V9fsString
*new_name
)
2552 V9fsFidState
*tfidp
;
2553 V9fsPath oldpath
, newpath
;
2554 V9fsState
*s
= pdu
->s
;
2557 v9fs_path_init(&oldpath
);
2558 v9fs_path_init(&newpath
);
2559 v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2560 v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2563 * Fixup fid's pointing to the old name to
2564 * start pointing to the new name
2566 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2567 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2568 /* replace the name */
2569 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2572 v9fs_path_free(&oldpath
);
2573 v9fs_path_free(&newpath
);
2576 static int v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2577 V9fsString
*old_name
, int32_t newdirfid
,
2578 V9fsString
*new_name
)
2581 V9fsState
*s
= pdu
->s
;
2582 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2584 olddirfidp
= get_fid(pdu
, olddirfid
);
2585 if (olddirfidp
== NULL
) {
2589 if (newdirfid
!= -1) {
2590 newdirfidp
= get_fid(pdu
, newdirfid
);
2591 if (newdirfidp
== NULL
) {
2596 newdirfidp
= get_fid(pdu
, olddirfid
);
2599 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2600 &newdirfidp
->path
, new_name
);
2604 if (s
->ctx
.flags
& PATHNAME_FSCONTEXT
) {
2605 /* Only for path based fid we need to do the below fixup */
2606 v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2607 &newdirfidp
->path
, new_name
);
2611 put_fid(pdu
, olddirfidp
);
2614 put_fid(pdu
, newdirfidp
);
2619 static void v9fs_renameat(void *opaque
)
2623 V9fsPDU
*pdu
= opaque
;
2624 V9fsState
*s
= pdu
->s
;
2625 int32_t olddirfid
, newdirfid
;
2626 V9fsString old_name
, new_name
;
2628 pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2629 &old_name
, &newdirfid
, &new_name
);
2631 v9fs_path_write_lock(s
);
2632 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2633 &old_name
, newdirfid
, &new_name
);
2634 v9fs_path_unlock(s
);
2638 complete_pdu(s
, pdu
, err
);
2639 v9fs_string_free(&old_name
);
2640 v9fs_string_free(&new_name
);
2643 static void v9fs_wstat(void *opaque
)
2652 V9fsPDU
*pdu
= opaque
;
2653 V9fsState
*s
= pdu
->s
;
2655 pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2657 fidp
= get_fid(pdu
, fid
);
2662 /* do we need to sync the file? */
2663 if (donttouch_stat(&v9stat
)) {
2664 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2667 if (v9stat
.mode
!= -1) {
2669 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2673 v9_mode
= stat_to_v9mode(&stbuf
);
2674 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2675 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2676 /* Attempting to change the type */
2680 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2681 v9mode_to_mode(v9stat
.mode
,
2682 &v9stat
.extension
));
2687 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2688 struct timespec times
[2];
2689 if (v9stat
.atime
!= -1) {
2690 times
[0].tv_sec
= v9stat
.atime
;
2691 times
[0].tv_nsec
= 0;
2693 times
[0].tv_nsec
= UTIME_OMIT
;
2695 if (v9stat
.mtime
!= -1) {
2696 times
[1].tv_sec
= v9stat
.mtime
;
2697 times
[1].tv_nsec
= 0;
2699 times
[1].tv_nsec
= UTIME_OMIT
;
2701 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2706 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2707 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2712 if (v9stat
.name
.size
!= 0) {
2713 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2718 if (v9stat
.length
!= -1) {
2719 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2728 v9fs_stat_free(&v9stat
);
2729 complete_pdu(s
, pdu
, err
);
2732 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2744 int32_t bsize_factor
;
2747 * compute bsize factor based on host file system block size
2750 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2751 if (!bsize_factor
) {
2754 f_type
= stbuf
->f_type
;
2755 f_bsize
= stbuf
->f_bsize
;
2756 f_bsize
*= bsize_factor
;
2758 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2759 * adjust(divide) the number of blocks, free blocks and available
2760 * blocks by bsize factor
2762 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2763 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2764 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2765 f_files
= stbuf
->f_files
;
2766 f_ffree
= stbuf
->f_ffree
;
2767 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2768 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2769 f_namelen
= stbuf
->f_namelen
;
2771 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2772 f_type
, f_bsize
, f_blocks
, f_bfree
,
2773 f_bavail
, f_files
, f_ffree
,
2774 fsid_val
, f_namelen
);
2777 static void v9fs_statfs(void *opaque
)
2783 struct statfs stbuf
;
2784 V9fsPDU
*pdu
= opaque
;
2785 V9fsState
*s
= pdu
->s
;
2787 pdu_unmarshal(pdu
, offset
, "d", &fid
);
2788 fidp
= get_fid(pdu
, fid
);
2793 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2798 retval
+= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2802 complete_pdu(s
, pdu
, retval
);
2806 static void v9fs_mknod(void *opaque
)
2819 V9fsPDU
*pdu
= opaque
;
2820 V9fsState
*s
= pdu
->s
;
2822 pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2823 &major
, &minor
, &gid
);
2825 fidp
= get_fid(pdu
, fid
);
2830 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
2831 makedev(major
, minor
), mode
, &stbuf
);
2835 stat_to_qid(&stbuf
, &qid
);
2837 err
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
2841 complete_pdu(s
, pdu
, err
);
2842 v9fs_string_free(&name
);
2846 * Implement posix byte range locking code
2847 * Server side handling of locking code is very simple, because 9p server in
2848 * QEMU can handle only one client. And most of the lock handling
2849 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2850 * do any thing in * qemu 9p server side lock code path.
2851 * So when a TLOCK request comes, always return success
2853 static void v9fs_lock(void *opaque
)
2860 int32_t fid
, err
= 0;
2861 V9fsPDU
*pdu
= opaque
;
2862 V9fsState
*s
= pdu
->s
;
2864 flock
= g_malloc(sizeof(*flock
));
2865 pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
->type
,
2866 &flock
->flags
, &flock
->start
, &flock
->length
,
2867 &flock
->proc_id
, &flock
->client_id
);
2868 status
= P9_LOCK_ERROR
;
2870 /* We support only block flag now (that too ignored currently) */
2871 if (flock
->flags
& ~P9_LOCK_FLAGS_BLOCK
) {
2875 fidp
= get_fid(pdu
, fid
);
2880 err
= v9fs_co_fstat(pdu
, fidp
->fs
.fd
, &stbuf
);
2884 status
= P9_LOCK_SUCCESS
;
2889 err
+= pdu_marshal(pdu
, offset
, "b", status
);
2890 complete_pdu(s
, pdu
, err
);
2891 v9fs_string_free(&flock
->client_id
);
2896 * When a TGETLOCK request comes, always return success because all lock
2897 * handling is done by client's VFS layer.
2899 static void v9fs_getlock(void *opaque
)
2905 int32_t fid
, err
= 0;
2906 V9fsPDU
*pdu
= opaque
;
2907 V9fsState
*s
= pdu
->s
;
2909 glock
= g_malloc(sizeof(*glock
));
2910 pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
->type
,
2911 &glock
->start
, &glock
->length
, &glock
->proc_id
,
2914 fidp
= get_fid(pdu
, fid
);
2919 err
= v9fs_co_fstat(pdu
, fidp
->fs
.fd
, &stbuf
);
2923 glock
->type
= F_UNLCK
;
2924 offset
+= pdu_marshal(pdu
, offset
, "bqqds", glock
->type
,
2925 glock
->start
, glock
->length
, glock
->proc_id
,
2931 complete_pdu(s
, pdu
, err
);
2932 v9fs_string_free(&glock
->client_id
);
2936 static void v9fs_mkdir(void *opaque
)
2938 V9fsPDU
*pdu
= opaque
;
2949 pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
2951 fidp
= get_fid(pdu
, fid
);
2956 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
2960 stat_to_qid(&stbuf
, &qid
);
2961 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
2966 complete_pdu(pdu
->s
, pdu
, err
);
2967 v9fs_string_free(&name
);
2970 static void v9fs_xattrwalk(void *opaque
)
2976 int32_t fid
, newfid
;
2977 V9fsFidState
*file_fidp
;
2978 V9fsFidState
*xattr_fidp
= NULL
;
2979 V9fsPDU
*pdu
= opaque
;
2980 V9fsState
*s
= pdu
->s
;
2982 pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
2983 file_fidp
= get_fid(pdu
, fid
);
2984 if (file_fidp
== NULL
) {
2988 xattr_fidp
= alloc_fid(s
, newfid
);
2989 if (xattr_fidp
== NULL
) {
2993 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
2994 if (name
.data
[0] == 0) {
2996 * listxattr request. Get the size first
2998 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3001 clunk_fid(s
, xattr_fidp
->fid
);
3005 * Read the xattr value
3007 xattr_fidp
->fs
.xattr
.len
= size
;
3008 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3009 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3011 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3012 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3013 xattr_fidp
->fs
.xattr
.value
,
3014 xattr_fidp
->fs
.xattr
.len
);
3016 clunk_fid(s
, xattr_fidp
->fid
);
3020 offset
+= pdu_marshal(pdu
, offset
, "q", size
);
3024 * specific xattr fid. We check for xattr
3025 * presence also collect the xattr size
3027 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3031 clunk_fid(s
, xattr_fidp
->fid
);
3035 * Read the xattr value
3037 xattr_fidp
->fs
.xattr
.len
= size
;
3038 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3039 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3041 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3042 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3043 &name
, xattr_fidp
->fs
.xattr
.value
,
3044 xattr_fidp
->fs
.xattr
.len
);
3046 clunk_fid(s
, xattr_fidp
->fid
);
3050 offset
+= pdu_marshal(pdu
, offset
, "q", size
);
3054 put_fid(pdu
, file_fidp
);
3056 put_fid(pdu
, xattr_fidp
);
3059 complete_pdu(s
, pdu
, err
);
3060 v9fs_string_free(&name
);
3063 static void v9fs_xattrcreate(void *opaque
)
3071 V9fsFidState
*file_fidp
;
3072 V9fsFidState
*xattr_fidp
;
3073 V9fsPDU
*pdu
= opaque
;
3074 V9fsState
*s
= pdu
->s
;
3076 pdu_unmarshal(pdu
, offset
, "dsqd",
3077 &fid
, &name
, &size
, &flags
);
3079 file_fidp
= get_fid(pdu
, fid
);
3080 if (file_fidp
== NULL
) {
3084 /* Make the file fid point to xattr */
3085 xattr_fidp
= file_fidp
;
3086 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3087 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3088 xattr_fidp
->fs
.xattr
.len
= size
;
3089 xattr_fidp
->fs
.xattr
.flags
= flags
;
3090 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3091 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3093 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3095 xattr_fidp
->fs
.xattr
.value
= NULL
;
3098 put_fid(pdu
, file_fidp
);
3100 complete_pdu(s
, pdu
, err
);
3101 v9fs_string_free(&name
);
3104 static void v9fs_readlink(void *opaque
)
3106 V9fsPDU
*pdu
= opaque
;
3113 pdu_unmarshal(pdu
, offset
, "d", &fid
);
3114 fidp
= get_fid(pdu
, fid
);
3120 v9fs_string_init(&target
);
3121 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3125 offset
+= pdu_marshal(pdu
, offset
, "s", &target
);
3127 v9fs_string_free(&target
);
3131 complete_pdu(pdu
->s
, pdu
, err
);
3134 static CoroutineEntry
*pdu_co_handlers
[] = {
3135 [P9_TREADDIR
] = v9fs_readdir
,
3136 [P9_TSTATFS
] = v9fs_statfs
,
3137 [P9_TGETATTR
] = v9fs_getattr
,
3138 [P9_TSETATTR
] = v9fs_setattr
,
3139 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3140 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3141 [P9_TMKNOD
] = v9fs_mknod
,
3142 [P9_TRENAME
] = v9fs_rename
,
3143 [P9_TLOCK
] = v9fs_lock
,
3144 [P9_TGETLOCK
] = v9fs_getlock
,
3145 [P9_TRENAMEAT
] = v9fs_renameat
,
3146 [P9_TREADLINK
] = v9fs_readlink
,
3147 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3148 [P9_TMKDIR
] = v9fs_mkdir
,
3149 [P9_TVERSION
] = v9fs_version
,
3150 [P9_TLOPEN
] = v9fs_open
,
3151 [P9_TATTACH
] = v9fs_attach
,
3152 [P9_TSTAT
] = v9fs_stat
,
3153 [P9_TWALK
] = v9fs_walk
,
3154 [P9_TCLUNK
] = v9fs_clunk
,
3155 [P9_TFSYNC
] = v9fs_fsync
,
3156 [P9_TOPEN
] = v9fs_open
,
3157 [P9_TREAD
] = v9fs_read
,
3159 [P9_TAUTH
] = v9fs_auth
,
3161 [P9_TFLUSH
] = v9fs_flush
,
3162 [P9_TLINK
] = v9fs_link
,
3163 [P9_TSYMLINK
] = v9fs_symlink
,
3164 [P9_TCREATE
] = v9fs_create
,
3165 [P9_TLCREATE
] = v9fs_lcreate
,
3166 [P9_TWRITE
] = v9fs_write
,
3167 [P9_TWSTAT
] = v9fs_wstat
,
3168 [P9_TREMOVE
] = v9fs_remove
,
3171 static void v9fs_op_not_supp(void *opaque
)
3173 V9fsPDU
*pdu
= opaque
;
3174 complete_pdu(pdu
->s
, pdu
, -EOPNOTSUPP
);
3177 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
3180 CoroutineEntry
*handler
;
3185 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3186 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3187 handler
= v9fs_op_not_supp
;
3189 handler
= pdu_co_handlers
[pdu
->id
];
3191 co
= qemu_coroutine_create(handler
);
3192 qemu_coroutine_enter(co
, pdu
);
3195 void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
3197 V9fsState
*s
= (V9fsState
*)vdev
;
3201 while ((pdu
= alloc_pdu(s
)) &&
3202 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
3205 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
3206 BUG_ON(pdu
->elem
.out_sg
[0].iov_len
< 7);
3208 ptr
= pdu
->elem
.out_sg
[0].iov_base
;
3210 memcpy(&pdu
->size
, ptr
, 4);
3212 memcpy(&pdu
->tag
, ptr
+ 5, 2);
3213 qemu_co_queue_init(&pdu
->complete
);
3219 void virtio_9p_set_fd_limit(void)
3222 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3223 fprintf(stderr
, "Failed to get the resource limit\n");
3226 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3227 open_fd_rc
= rlim
.rlim_cur
/2;