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 "qemu/osdep.h"
15 #include <glib/gprintf.h>
16 #include "hw/virtio/virtio.h"
17 #include "qapi/error.h"
18 #include "qemu/error-report.h"
20 #include "qemu/sockets.h"
21 #include "virtio-9p.h"
22 #include "fsdev/qemu-fsdev.h"
26 #include "migration/blocker.h"
27 #include "sysemu/qtest.h"
31 static int open_fd_rc
;
45 static ssize_t
pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
51 ret
= pdu
->s
->transport
->pdu_vmarshal(pdu
, offset
, fmt
, ap
);
57 static ssize_t
pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
63 ret
= pdu
->s
->transport
->pdu_vunmarshal(pdu
, offset
, fmt
, ap
);
69 static int omode_to_uflags(int8_t mode
)
103 typedef struct DotlOpenflagMap
{
108 static int dotl_to_open_flags(int flags
)
112 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
113 * and P9_DOTL_NOACCESS
115 int oflags
= flags
& O_ACCMODE
;
117 DotlOpenflagMap dotl_oflag_map
[] = {
118 { P9_DOTL_CREATE
, O_CREAT
},
119 { P9_DOTL_EXCL
, O_EXCL
},
120 { P9_DOTL_NOCTTY
, O_NOCTTY
},
121 { P9_DOTL_TRUNC
, O_TRUNC
},
122 { P9_DOTL_APPEND
, O_APPEND
},
123 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
124 { P9_DOTL_DSYNC
, O_DSYNC
},
125 { P9_DOTL_FASYNC
, FASYNC
},
126 { P9_DOTL_DIRECT
, O_DIRECT
},
127 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
128 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
129 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
130 { P9_DOTL_NOATIME
, O_NOATIME
},
131 { P9_DOTL_SYNC
, O_SYNC
},
134 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
135 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
136 oflags
|= dotl_oflag_map
[i
].open_flag
;
143 void cred_init(FsCred
*credp
)
151 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
155 * Filter the client open flags
157 flags
= dotl_to_open_flags(oflags
);
158 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
160 * Ignore direct disk access hint until the server supports it.
166 void v9fs_path_init(V9fsPath
*path
)
172 void v9fs_path_free(V9fsPath
*path
)
180 void GCC_FMT_ATTR(2, 3)
181 v9fs_path_sprintf(V9fsPath
*path
, const char *fmt
, ...)
185 v9fs_path_free(path
);
188 /* Bump the size for including terminating NULL */
189 path
->size
= g_vasprintf(&path
->data
, fmt
, ap
) + 1;
193 void v9fs_path_copy(V9fsPath
*dst
, const V9fsPath
*src
)
196 dst
->size
= src
->size
;
197 dst
->data
= g_memdup(src
->data
, src
->size
);
200 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
201 const char *name
, V9fsPath
*path
)
204 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
212 * Return TRUE if s1 is an ancestor of s2.
214 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
215 * As a special case, We treat s1 as ancestor of s2 if they are same!
217 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
219 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
220 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
227 static size_t v9fs_string_size(V9fsString
*str
)
233 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
234 static int coroutine_fn
v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
237 if (f
->fid_type
== P9_FID_FILE
) {
238 if (f
->fs
.fd
== -1) {
240 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
241 } while (err
== -EINTR
&& !pdu
->cancelled
);
243 } else if (f
->fid_type
== P9_FID_DIR
) {
244 if (f
->fs
.dir
.stream
== NULL
) {
246 err
= v9fs_co_opendir(pdu
, f
);
247 } while (err
== -EINTR
&& !pdu
->cancelled
);
253 static V9fsFidState
*coroutine_fn
get_fid(V9fsPDU
*pdu
, int32_t fid
)
257 V9fsState
*s
= pdu
->s
;
259 for (f
= s
->fid_list
; f
; f
= f
->next
) {
263 * Update the fid ref upfront so that
264 * we don't get reclaimed when we yield
269 * check whether we need to reopen the
270 * file. We might have closed the fd
271 * while trying to free up some file
274 err
= v9fs_reopen_fid(pdu
, f
);
280 * Mark the fid as referenced so that the LRU
281 * reclaim won't close the file descriptor
283 f
->flags
|= FID_REFERENCED
;
290 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
294 for (f
= s
->fid_list
; f
; f
= f
->next
) {
295 /* If fid is already there return NULL */
301 f
= g_malloc0(sizeof(V9fsFidState
));
303 f
->fid_type
= P9_FID_NONE
;
306 * Mark the fid as referenced so that the LRU
307 * reclaim won't close the file descriptor
309 f
->flags
|= FID_REFERENCED
;
310 f
->next
= s
->fid_list
;
313 v9fs_readdir_init(&f
->fs
.dir
);
314 v9fs_readdir_init(&f
->fs_reclaim
.dir
);
319 static int coroutine_fn
v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
323 if (fidp
->fs
.xattr
.xattrwalk_fid
) {
324 /* getxattr/listxattr fid */
328 * if this is fid for setxattr. clunk should
329 * result in setxattr localcall
331 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
332 /* clunk after partial write */
336 if (fidp
->fs
.xattr
.len
) {
337 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
338 fidp
->fs
.xattr
.value
,
340 fidp
->fs
.xattr
.flags
);
342 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
345 v9fs_string_free(&fidp
->fs
.xattr
.name
);
347 g_free(fidp
->fs
.xattr
.value
);
351 static int coroutine_fn
free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
355 if (fidp
->fid_type
== P9_FID_FILE
) {
356 /* If we reclaimed the fd no need to close */
357 if (fidp
->fs
.fd
!= -1) {
358 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
360 } else if (fidp
->fid_type
== P9_FID_DIR
) {
361 if (fidp
->fs
.dir
.stream
!= NULL
) {
362 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
364 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
365 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
367 v9fs_path_free(&fidp
->path
);
372 static int coroutine_fn
put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
377 * Don't free the fid if it is in reclaim list
379 if (!fidp
->ref
&& fidp
->clunked
) {
380 if (fidp
->fid
== pdu
->s
->root_fid
) {
382 * if the clunked fid is root fid then we
383 * have unmounted the fs on the client side.
384 * delete the migration blocker. Ideally, this
385 * should be hooked to transport close notification
387 if (pdu
->s
->migration_blocker
) {
388 migrate_del_blocker(pdu
->s
->migration_blocker
);
389 error_free(pdu
->s
->migration_blocker
);
390 pdu
->s
->migration_blocker
= NULL
;
393 return free_fid(pdu
, fidp
);
398 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
400 V9fsFidState
**fidpp
, *fidp
;
402 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
403 if ((*fidpp
)->fid
== fid
) {
407 if (*fidpp
== NULL
) {
416 void coroutine_fn
v9fs_reclaim_fd(V9fsPDU
*pdu
)
418 int reclaim_count
= 0;
419 V9fsState
*s
= pdu
->s
;
420 V9fsFidState
*f
, *reclaim_list
= NULL
;
422 for (f
= s
->fid_list
; f
; f
= f
->next
) {
424 * Unlink fids cannot be reclaimed. Check
425 * for them and skip them. Also skip fids
426 * currently being operated on.
428 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
432 * if it is a recently referenced fid
433 * we leave the fid untouched and clear the
434 * reference bit. We come back to it later
435 * in the next iteration. (a simple LRU without
436 * moving list elements around)
438 if (f
->flags
& FID_REFERENCED
) {
439 f
->flags
&= ~FID_REFERENCED
;
443 * Add fids to reclaim list.
445 if (f
->fid_type
== P9_FID_FILE
) {
446 if (f
->fs
.fd
!= -1) {
448 * Up the reference count so that
449 * a clunk request won't free this fid
452 f
->rclm_lst
= reclaim_list
;
454 f
->fs_reclaim
.fd
= f
->fs
.fd
;
458 } else if (f
->fid_type
== P9_FID_DIR
) {
459 if (f
->fs
.dir
.stream
!= NULL
) {
461 * Up the reference count so that
462 * a clunk request won't free this fid
465 f
->rclm_lst
= reclaim_list
;
467 f
->fs_reclaim
.dir
.stream
= f
->fs
.dir
.stream
;
468 f
->fs
.dir
.stream
= NULL
;
472 if (reclaim_count
>= open_fd_rc
) {
477 * Now close the fid in reclaim list. Free them if they
478 * are already clunked.
480 while (reclaim_list
) {
482 reclaim_list
= f
->rclm_lst
;
483 if (f
->fid_type
== P9_FID_FILE
) {
484 v9fs_co_close(pdu
, &f
->fs_reclaim
);
485 } else if (f
->fid_type
== P9_FID_DIR
) {
486 v9fs_co_closedir(pdu
, &f
->fs_reclaim
);
490 * Now drop the fid reference, free it
497 static int coroutine_fn
v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
500 V9fsState
*s
= pdu
->s
;
501 V9fsFidState
*fidp
, head_fid
;
503 head_fid
.next
= s
->fid_list
;
504 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
505 if (fidp
->path
.size
!= path
->size
) {
508 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
509 /* Mark the fid non reclaimable. */
510 fidp
->flags
|= FID_NON_RECLAIMABLE
;
512 /* reopen the file/dir if already closed */
513 err
= v9fs_reopen_fid(pdu
, fidp
);
518 * Go back to head of fid list because
519 * the list could have got updated when
520 * switched to the worker thread
530 static void coroutine_fn
virtfs_reset(V9fsPDU
*pdu
)
532 V9fsState
*s
= pdu
->s
;
536 while (s
->fid_list
) {
542 s
->fid_list
= fidp
->next
;
549 #define P9_QID_TYPE_DIR 0x80
550 #define P9_QID_TYPE_SYMLINK 0x02
552 #define P9_STAT_MODE_DIR 0x80000000
553 #define P9_STAT_MODE_APPEND 0x40000000
554 #define P9_STAT_MODE_EXCL 0x20000000
555 #define P9_STAT_MODE_MOUNT 0x10000000
556 #define P9_STAT_MODE_AUTH 0x08000000
557 #define P9_STAT_MODE_TMP 0x04000000
558 #define P9_STAT_MODE_SYMLINK 0x02000000
559 #define P9_STAT_MODE_LINK 0x01000000
560 #define P9_STAT_MODE_DEVICE 0x00800000
561 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
562 #define P9_STAT_MODE_SOCKET 0x00100000
563 #define P9_STAT_MODE_SETUID 0x00080000
564 #define P9_STAT_MODE_SETGID 0x00040000
565 #define P9_STAT_MODE_SETVTX 0x00010000
567 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
568 P9_STAT_MODE_SYMLINK | \
569 P9_STAT_MODE_LINK | \
570 P9_STAT_MODE_DEVICE | \
571 P9_STAT_MODE_NAMED_PIPE | \
574 /* This is the algorithm from ufs in spfs */
575 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
579 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
580 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
581 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
582 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
584 if (S_ISDIR(stbuf
->st_mode
)) {
585 qidp
->type
|= P9_QID_TYPE_DIR
;
587 if (S_ISLNK(stbuf
->st_mode
)) {
588 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
592 static int coroutine_fn
fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
598 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
602 stat_to_qid(&stbuf
, qidp
);
606 V9fsPDU
*pdu_alloc(V9fsState
*s
)
610 if (!QLIST_EMPTY(&s
->free_list
)) {
611 pdu
= QLIST_FIRST(&s
->free_list
);
612 QLIST_REMOVE(pdu
, next
);
613 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
618 void pdu_free(V9fsPDU
*pdu
)
620 V9fsState
*s
= pdu
->s
;
622 g_assert(!pdu
->cancelled
);
623 QLIST_REMOVE(pdu
, next
);
624 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
627 static void coroutine_fn
pdu_complete(V9fsPDU
*pdu
, ssize_t len
)
629 int8_t id
= pdu
->id
+ 1; /* Response */
630 V9fsState
*s
= pdu
->s
;
634 * The 9p spec requires that successfully cancelled pdus receive no reply.
635 * Sending a reply would confuse clients because they would
636 * assume that any EINTR is the actual result of the operation,
637 * rather than a consequence of the cancellation. However, if
638 * the operation completed (succesfully or with an error other
639 * than caused be cancellation), we do send out that reply, both
640 * for efficiency and to avoid confusing the rest of the state machine
641 * that assumes passing a non-error here will mean a successful
642 * transmission of the reply.
644 bool discard
= pdu
->cancelled
&& len
== -EINTR
;
646 trace_v9fs_rcancel(pdu
->tag
, pdu
->id
);
655 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
658 str
.data
= strerror(err
);
659 str
.size
= strlen(str
.data
);
661 ret
= pdu_marshal(pdu
, len
, "s", &str
);
669 ret
= pdu_marshal(pdu
, len
, "d", err
);
675 if (s
->proto_version
== V9FS_PROTO_2000L
) {
678 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
681 /* fill out the header */
682 if (pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
) < 0) {
686 /* keep these in sync */
691 pdu
->s
->transport
->push_and_notify(pdu
);
693 /* Now wakeup anybody waiting in flush for this request */
694 if (!qemu_co_queue_next(&pdu
->complete
)) {
699 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
704 if (mode
& P9_STAT_MODE_DIR
) {
708 if (mode
& P9_STAT_MODE_SYMLINK
) {
711 if (mode
& P9_STAT_MODE_SOCKET
) {
714 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
717 if (mode
& P9_STAT_MODE_DEVICE
) {
718 if (extension
->size
&& extension
->data
[0] == 'c') {
729 if (mode
& P9_STAT_MODE_SETUID
) {
732 if (mode
& P9_STAT_MODE_SETGID
) {
735 if (mode
& P9_STAT_MODE_SETVTX
) {
742 static int donttouch_stat(V9fsStat
*stat
)
744 if (stat
->type
== -1 &&
746 stat
->qid
.type
== -1 &&
747 stat
->qid
.version
== -1 &&
748 stat
->qid
.path
== -1 &&
752 stat
->length
== -1 &&
759 stat
->n_muid
== -1) {
766 static void v9fs_stat_init(V9fsStat
*stat
)
768 v9fs_string_init(&stat
->name
);
769 v9fs_string_init(&stat
->uid
);
770 v9fs_string_init(&stat
->gid
);
771 v9fs_string_init(&stat
->muid
);
772 v9fs_string_init(&stat
->extension
);
775 static void v9fs_stat_free(V9fsStat
*stat
)
777 v9fs_string_free(&stat
->name
);
778 v9fs_string_free(&stat
->uid
);
779 v9fs_string_free(&stat
->gid
);
780 v9fs_string_free(&stat
->muid
);
781 v9fs_string_free(&stat
->extension
);
784 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
788 mode
= stbuf
->st_mode
& 0777;
789 if (S_ISDIR(stbuf
->st_mode
)) {
790 mode
|= P9_STAT_MODE_DIR
;
793 if (S_ISLNK(stbuf
->st_mode
)) {
794 mode
|= P9_STAT_MODE_SYMLINK
;
797 if (S_ISSOCK(stbuf
->st_mode
)) {
798 mode
|= P9_STAT_MODE_SOCKET
;
801 if (S_ISFIFO(stbuf
->st_mode
)) {
802 mode
|= P9_STAT_MODE_NAMED_PIPE
;
805 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
806 mode
|= P9_STAT_MODE_DEVICE
;
809 if (stbuf
->st_mode
& S_ISUID
) {
810 mode
|= P9_STAT_MODE_SETUID
;
813 if (stbuf
->st_mode
& S_ISGID
) {
814 mode
|= P9_STAT_MODE_SETGID
;
817 if (stbuf
->st_mode
& S_ISVTX
) {
818 mode
|= P9_STAT_MODE_SETVTX
;
824 static int coroutine_fn
stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*path
,
825 const char *basename
,
826 const struct stat
*stbuf
,
831 memset(v9stat
, 0, sizeof(*v9stat
));
833 stat_to_qid(stbuf
, &v9stat
->qid
);
834 v9stat
->mode
= stat_to_v9mode(stbuf
);
835 v9stat
->atime
= stbuf
->st_atime
;
836 v9stat
->mtime
= stbuf
->st_mtime
;
837 v9stat
->length
= stbuf
->st_size
;
839 v9fs_string_free(&v9stat
->uid
);
840 v9fs_string_free(&v9stat
->gid
);
841 v9fs_string_free(&v9stat
->muid
);
843 v9stat
->n_uid
= stbuf
->st_uid
;
844 v9stat
->n_gid
= stbuf
->st_gid
;
847 v9fs_string_free(&v9stat
->extension
);
849 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
850 err
= v9fs_co_readlink(pdu
, path
, &v9stat
->extension
);
854 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
855 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
856 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
857 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
858 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
859 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
860 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
863 v9fs_string_sprintf(&v9stat
->name
, "%s", basename
);
866 v9fs_string_size(&v9stat
->name
) +
867 v9fs_string_size(&v9stat
->uid
) +
868 v9fs_string_size(&v9stat
->gid
) +
869 v9fs_string_size(&v9stat
->muid
) +
870 v9fs_string_size(&v9stat
->extension
);
874 #define P9_STATS_MODE 0x00000001ULL
875 #define P9_STATS_NLINK 0x00000002ULL
876 #define P9_STATS_UID 0x00000004ULL
877 #define P9_STATS_GID 0x00000008ULL
878 #define P9_STATS_RDEV 0x00000010ULL
879 #define P9_STATS_ATIME 0x00000020ULL
880 #define P9_STATS_MTIME 0x00000040ULL
881 #define P9_STATS_CTIME 0x00000080ULL
882 #define P9_STATS_INO 0x00000100ULL
883 #define P9_STATS_SIZE 0x00000200ULL
884 #define P9_STATS_BLOCKS 0x00000400ULL
886 #define P9_STATS_BTIME 0x00000800ULL
887 #define P9_STATS_GEN 0x00001000ULL
888 #define P9_STATS_DATA_VERSION 0x00002000ULL
890 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
891 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
894 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
895 V9fsStatDotl
*v9lstat
)
897 memset(v9lstat
, 0, sizeof(*v9lstat
));
899 v9lstat
->st_mode
= stbuf
->st_mode
;
900 v9lstat
->st_nlink
= stbuf
->st_nlink
;
901 v9lstat
->st_uid
= stbuf
->st_uid
;
902 v9lstat
->st_gid
= stbuf
->st_gid
;
903 v9lstat
->st_rdev
= stbuf
->st_rdev
;
904 v9lstat
->st_size
= stbuf
->st_size
;
905 v9lstat
->st_blksize
= stbuf
->st_blksize
;
906 v9lstat
->st_blocks
= stbuf
->st_blocks
;
907 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
908 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
909 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
910 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
911 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
912 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
913 /* Currently we only support BASIC fields in stat */
914 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
916 stat_to_qid(stbuf
, &v9lstat
->qid
);
919 static void print_sg(struct iovec
*sg
, int cnt
)
923 printf("sg[%d]: {", cnt
);
924 for (i
= 0; i
< cnt
; i
++) {
928 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
933 /* Will call this only for path name based fid */
934 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
937 v9fs_path_init(&str
);
938 v9fs_path_copy(&str
, dst
);
939 v9fs_path_sprintf(dst
, "%s%s", src
->data
, str
.data
+ len
);
940 v9fs_path_free(&str
);
943 static inline bool is_ro_export(FsContext
*ctx
)
945 return ctx
->export_flags
& V9FS_RDONLY
;
948 static void coroutine_fn
v9fs_version(void *opaque
)
951 V9fsPDU
*pdu
= opaque
;
952 V9fsState
*s
= pdu
->s
;
956 v9fs_string_init(&version
);
957 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
961 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
965 if (!strcmp(version
.data
, "9P2000.u")) {
966 s
->proto_version
= V9FS_PROTO_2000U
;
967 } else if (!strcmp(version
.data
, "9P2000.L")) {
968 s
->proto_version
= V9FS_PROTO_2000L
;
970 v9fs_string_sprintf(&version
, "unknown");
973 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
978 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
980 pdu_complete(pdu
, err
);
981 v9fs_string_free(&version
);
984 static void coroutine_fn
v9fs_attach(void *opaque
)
986 V9fsPDU
*pdu
= opaque
;
987 V9fsState
*s
= pdu
->s
;
988 int32_t fid
, afid
, n_uname
;
989 V9fsString uname
, aname
;
994 Error
*local_err
= NULL
;
996 v9fs_string_init(&uname
);
997 v9fs_string_init(&aname
);
998 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
999 &afid
, &uname
, &aname
, &n_uname
);
1003 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
1005 fidp
= alloc_fid(s
, fid
);
1010 fidp
->uid
= n_uname
;
1011 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
1017 err
= fid_to_qid(pdu
, fidp
, &qid
);
1025 * disable migration if we haven't done already.
1026 * attach could get called multiple times for the same export.
1028 if (!s
->migration_blocker
) {
1029 error_setg(&s
->migration_blocker
,
1030 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1031 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
1032 err
= migrate_add_blocker(s
->migration_blocker
, &local_err
);
1034 error_free(local_err
);
1035 error_free(s
->migration_blocker
);
1036 s
->migration_blocker
= NULL
;
1043 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
1050 memcpy(&s
->root_qid
, &qid
, sizeof(qid
));
1051 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1052 qid
.type
, qid
.version
, qid
.path
);
1056 pdu_complete(pdu
, err
);
1057 v9fs_string_free(&uname
);
1058 v9fs_string_free(&aname
);
1061 static void coroutine_fn
v9fs_stat(void *opaque
)
1069 V9fsPDU
*pdu
= opaque
;
1072 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1076 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1078 fidp
= get_fid(pdu
, fid
);
1083 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1087 basename
= g_path_get_basename(fidp
->path
.data
);
1088 err
= stat_to_v9stat(pdu
, &fidp
->path
, basename
, &stbuf
, &v9stat
);
1093 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1095 v9fs_stat_free(&v9stat
);
1098 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1099 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1101 v9fs_stat_free(&v9stat
);
1105 pdu_complete(pdu
, err
);
1108 static void coroutine_fn
v9fs_getattr(void *opaque
)
1115 uint64_t request_mask
;
1116 V9fsStatDotl v9stat_dotl
;
1117 V9fsPDU
*pdu
= opaque
;
1118 V9fsState
*s
= pdu
->s
;
1120 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1124 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1126 fidp
= get_fid(pdu
, fid
);
1132 * Currently we only support BASIC fields in stat, so there is no
1133 * need to look at request_mask.
1135 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1139 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1141 /* fill st_gen if requested and supported by underlying fs */
1142 if (request_mask
& P9_STATS_GEN
) {
1143 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1146 /* we have valid st_gen: update result mask */
1147 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1150 /* request cancelled, e.g. by Tflush */
1153 /* failed to get st_gen: not fatal, ignore */
1157 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1162 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1163 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1164 v9stat_dotl
.st_gid
);
1168 pdu_complete(pdu
, retval
);
1171 /* Attribute flags */
1172 #define P9_ATTR_MODE (1 << 0)
1173 #define P9_ATTR_UID (1 << 1)
1174 #define P9_ATTR_GID (1 << 2)
1175 #define P9_ATTR_SIZE (1 << 3)
1176 #define P9_ATTR_ATIME (1 << 4)
1177 #define P9_ATTR_MTIME (1 << 5)
1178 #define P9_ATTR_CTIME (1 << 6)
1179 #define P9_ATTR_ATIME_SET (1 << 7)
1180 #define P9_ATTR_MTIME_SET (1 << 8)
1182 #define P9_ATTR_MASK 127
1184 static void coroutine_fn
v9fs_setattr(void *opaque
)
1191 V9fsPDU
*pdu
= opaque
;
1193 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1198 fidp
= get_fid(pdu
, fid
);
1203 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1204 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1209 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1210 struct timespec times
[2];
1211 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1212 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1213 times
[0].tv_sec
= v9iattr
.atime_sec
;
1214 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1216 times
[0].tv_nsec
= UTIME_NOW
;
1219 times
[0].tv_nsec
= UTIME_OMIT
;
1221 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1222 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1223 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1224 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1226 times
[1].tv_nsec
= UTIME_NOW
;
1229 times
[1].tv_nsec
= UTIME_OMIT
;
1231 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1237 * If the only valid entry in iattr is ctime we can call
1238 * chown(-1,-1) to update the ctime of the file
1240 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1241 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1242 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1243 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1246 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1249 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1255 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1256 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1265 pdu_complete(pdu
, err
);
1268 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1274 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1279 for (i
= 0; i
< nwnames
; i
++) {
1280 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1289 static bool name_is_illegal(const char *name
)
1291 return !*name
|| strchr(name
, '/') != NULL
;
1294 static bool not_same_qid(const V9fsQID
*qid1
, const V9fsQID
*qid2
)
1297 qid1
->type
!= qid2
->type
||
1298 qid1
->version
!= qid2
->version
||
1299 qid1
->path
!= qid2
->path
;
1302 static void coroutine_fn
v9fs_walk(void *opaque
)
1305 V9fsQID
*qids
= NULL
;
1307 V9fsPath dpath
, path
;
1311 int32_t fid
, newfid
;
1312 V9fsString
*wnames
= NULL
;
1314 V9fsFidState
*newfidp
= NULL
;
1315 V9fsPDU
*pdu
= opaque
;
1316 V9fsState
*s
= pdu
->s
;
1319 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1321 pdu_complete(pdu
, err
);
1326 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1328 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1329 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1330 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1331 for (i
= 0; i
< nwnames
; i
++) {
1332 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1336 if (name_is_illegal(wnames
[i
].data
)) {
1342 } else if (nwnames
> P9_MAXWELEM
) {
1346 fidp
= get_fid(pdu
, fid
);
1352 v9fs_path_init(&dpath
);
1353 v9fs_path_init(&path
);
1355 err
= fid_to_qid(pdu
, fidp
, &qid
);
1361 * Both dpath and path initially poin to fidp.
1362 * Needed to handle request with nwnames == 0
1364 v9fs_path_copy(&dpath
, &fidp
->path
);
1365 v9fs_path_copy(&path
, &fidp
->path
);
1366 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1367 if (not_same_qid(&pdu
->s
->root_qid
, &qid
) ||
1368 strcmp("..", wnames
[name_idx
].data
)) {
1369 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
,
1375 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1379 stat_to_qid(&stbuf
, &qid
);
1380 v9fs_path_copy(&dpath
, &path
);
1382 memcpy(&qids
[name_idx
], &qid
, sizeof(qid
));
1384 if (fid
== newfid
) {
1385 if (fidp
->fid_type
!= P9_FID_NONE
) {
1389 v9fs_path_copy(&fidp
->path
, &path
);
1391 newfidp
= alloc_fid(s
, newfid
);
1392 if (newfidp
== NULL
) {
1396 newfidp
->uid
= fidp
->uid
;
1397 v9fs_path_copy(&newfidp
->path
, &path
);
1399 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1400 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1404 put_fid(pdu
, newfidp
);
1406 v9fs_path_free(&dpath
);
1407 v9fs_path_free(&path
);
1409 pdu_complete(pdu
, err
);
1410 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1411 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1412 v9fs_string_free(&wnames
[name_idx
]);
1419 static int32_t coroutine_fn
get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1421 struct statfs stbuf
;
1423 V9fsState
*s
= pdu
->s
;
1426 * iounit should be multiples of f_bsize (host filesystem block size
1427 * and as well as less than (client msize - P9_IOHDRSZ))
1429 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1430 iounit
= stbuf
.f_bsize
;
1431 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1434 iounit
= s
->msize
- P9_IOHDRSZ
;
1439 static void coroutine_fn
v9fs_open(void *opaque
)
1450 V9fsPDU
*pdu
= opaque
;
1451 V9fsState
*s
= pdu
->s
;
1453 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1454 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1457 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1463 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1465 fidp
= get_fid(pdu
, fid
);
1470 if (fidp
->fid_type
!= P9_FID_NONE
) {
1475 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1479 stat_to_qid(&stbuf
, &qid
);
1480 if (S_ISDIR(stbuf
.st_mode
)) {
1481 err
= v9fs_co_opendir(pdu
, fidp
);
1485 fidp
->fid_type
= P9_FID_DIR
;
1486 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1492 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1493 flags
= get_dotl_openflags(s
, mode
);
1495 flags
= omode_to_uflags(mode
);
1497 if (is_ro_export(&s
->ctx
)) {
1498 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1499 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1504 err
= v9fs_co_open(pdu
, fidp
, flags
);
1508 fidp
->fid_type
= P9_FID_FILE
;
1509 fidp
->open_flags
= flags
;
1510 if (flags
& O_EXCL
) {
1512 * We let the host file system do O_EXCL check
1513 * We should not reclaim such fd
1515 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1517 iounit
= get_iounit(pdu
, &fidp
->path
);
1518 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1524 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1525 qid
.type
, qid
.version
, qid
.path
, iounit
);
1529 pdu_complete(pdu
, err
);
1532 static void coroutine_fn
v9fs_lcreate(void *opaque
)
1534 int32_t dfid
, flags
, mode
;
1543 V9fsPDU
*pdu
= opaque
;
1545 v9fs_string_init(&name
);
1546 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1547 &name
, &flags
, &mode
, &gid
);
1551 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1553 if (name_is_illegal(name
.data
)) {
1558 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
1563 fidp
= get_fid(pdu
, dfid
);
1568 if (fidp
->fid_type
!= P9_FID_NONE
) {
1573 flags
= get_dotl_openflags(pdu
->s
, flags
);
1574 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1575 flags
| O_CREAT
, mode
, &stbuf
);
1579 fidp
->fid_type
= P9_FID_FILE
;
1580 fidp
->open_flags
= flags
;
1581 if (flags
& O_EXCL
) {
1583 * We let the host file system do O_EXCL check
1584 * We should not reclaim such fd
1586 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1588 iounit
= get_iounit(pdu
, &fidp
->path
);
1589 stat_to_qid(&stbuf
, &qid
);
1590 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1595 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1596 qid
.type
, qid
.version
, qid
.path
, iounit
);
1600 pdu_complete(pdu
, err
);
1601 v9fs_string_free(&name
);
1604 static void coroutine_fn
v9fs_fsync(void *opaque
)
1611 V9fsPDU
*pdu
= opaque
;
1613 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1617 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1619 fidp
= get_fid(pdu
, fid
);
1624 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1630 pdu_complete(pdu
, err
);
1633 static void coroutine_fn
v9fs_clunk(void *opaque
)
1639 V9fsPDU
*pdu
= opaque
;
1640 V9fsState
*s
= pdu
->s
;
1642 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1646 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1648 fidp
= clunk_fid(s
, fid
);
1654 * Bump the ref so that put_fid will
1658 err
= put_fid(pdu
, fidp
);
1663 pdu_complete(pdu
, err
);
1667 * Create a QEMUIOVector for a sub-region of PDU iovecs
1669 * @qiov: uninitialized QEMUIOVector
1670 * @skip: number of bytes to skip from beginning of PDU
1671 * @size: number of bytes to include
1672 * @is_write: true - write, false - read
1674 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1675 * with qemu_iovec_destroy().
1677 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1678 size_t skip
, size_t size
,
1686 pdu
->s
->transport
->init_out_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
1688 pdu
->s
->transport
->init_in_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
1691 qemu_iovec_init_external(&elem
, iov
, niov
);
1692 qemu_iovec_init(qiov
, niov
);
1693 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
1696 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1697 uint64_t off
, uint32_t max_count
)
1701 uint64_t read_count
;
1702 QEMUIOVector qiov_full
;
1704 if (fidp
->fs
.xattr
.len
< off
) {
1707 read_count
= fidp
->fs
.xattr
.len
- off
;
1709 if (read_count
> max_count
) {
1710 read_count
= max_count
;
1712 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
1718 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, read_count
, false);
1719 err
= v9fs_pack(qiov_full
.iov
, qiov_full
.niov
, 0,
1720 ((char *)fidp
->fs
.xattr
.value
) + off
,
1722 qemu_iovec_destroy(&qiov_full
);
1730 static int coroutine_fn
v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1739 off_t saved_dir_pos
;
1740 struct dirent
*dent
;
1742 /* save the directory position */
1743 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1744 if (saved_dir_pos
< 0) {
1745 return saved_dir_pos
;
1749 v9fs_path_init(&path
);
1751 v9fs_readdir_lock(&fidp
->fs
.dir
);
1753 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1757 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1761 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1765 err
= stat_to_v9stat(pdu
, &path
, dent
->d_name
, &stbuf
, &v9stat
);
1769 if ((count
+ v9stat
.size
+ 2) > max_count
) {
1770 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1772 /* Ran out of buffer. Set dir back to old position and return */
1773 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1774 v9fs_stat_free(&v9stat
);
1775 v9fs_path_free(&path
);
1779 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1780 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1782 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1785 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1786 v9fs_stat_free(&v9stat
);
1787 v9fs_path_free(&path
);
1791 v9fs_stat_free(&v9stat
);
1792 v9fs_path_free(&path
);
1793 saved_dir_pos
= dent
->d_off
;
1796 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1798 v9fs_path_free(&path
);
1805 static void coroutine_fn
v9fs_read(void *opaque
)
1814 V9fsPDU
*pdu
= opaque
;
1815 V9fsState
*s
= pdu
->s
;
1817 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1821 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1823 fidp
= get_fid(pdu
, fid
);
1828 if (fidp
->fid_type
== P9_FID_DIR
) {
1831 v9fs_co_rewinddir(pdu
, fidp
);
1833 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1838 err
= pdu_marshal(pdu
, offset
, "d", count
);
1842 err
+= offset
+ count
;
1843 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1844 QEMUIOVector qiov_full
;
1848 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
1849 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1851 qemu_iovec_reset(&qiov
);
1852 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
1854 print_sg(qiov
.iov
, qiov
.niov
);
1856 /* Loop in case of EINTR */
1858 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1863 } while (len
== -EINTR
&& !pdu
->cancelled
);
1865 /* IO error return the error */
1867 goto out_free_iovec
;
1869 } while (count
< max_count
&& len
> 0);
1870 err
= pdu_marshal(pdu
, offset
, "d", count
);
1872 goto out_free_iovec
;
1874 err
+= offset
+ count
;
1876 qemu_iovec_destroy(&qiov
);
1877 qemu_iovec_destroy(&qiov_full
);
1878 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1879 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1883 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1887 pdu_complete(pdu
, err
);
1890 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1893 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1894 * size of type (1) + size of name.size (2) + strlen(name.data)
1896 return 24 + v9fs_string_size(name
);
1899 static int coroutine_fn
v9fs_do_readdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1907 off_t saved_dir_pos
;
1908 struct dirent
*dent
;
1910 /* save the directory position */
1911 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1912 if (saved_dir_pos
< 0) {
1913 return saved_dir_pos
;
1917 v9fs_readdir_lock(&fidp
->fs
.dir
);
1919 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1923 v9fs_string_init(&name
);
1924 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1925 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1926 v9fs_readdir_unlock(&fidp
->fs
.dir
);
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
);
1934 * Fill up just the path field of qid because the client uses
1935 * only that. To fill the entire qid structure we will have
1936 * to stat each dirent found, which is expensive
1938 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1939 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1940 /* Fill the other fields with dummy values */
1944 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1945 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1947 dent
->d_type
, &name
);
1949 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1952 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1953 v9fs_string_free(&name
);
1957 v9fs_string_free(&name
);
1958 saved_dir_pos
= dent
->d_off
;
1961 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1969 static void coroutine_fn
v9fs_readdir(void *opaque
)
1975 uint64_t initial_offset
;
1978 V9fsPDU
*pdu
= opaque
;
1980 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
1981 &initial_offset
, &max_count
);
1985 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
1987 fidp
= get_fid(pdu
, fid
);
1992 if (!fidp
->fs
.dir
.stream
) {
1996 if (initial_offset
== 0) {
1997 v9fs_co_rewinddir(pdu
, fidp
);
1999 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
2001 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
2006 retval
= pdu_marshal(pdu
, offset
, "d", count
);
2010 retval
+= count
+ offset
;
2011 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
2015 pdu_complete(pdu
, retval
);
2018 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2019 uint64_t off
, uint32_t count
,
2020 struct iovec
*sg
, int cnt
)
2024 uint64_t write_count
;
2028 if (fidp
->fs
.xattr
.len
< off
) {
2032 write_count
= fidp
->fs
.xattr
.len
- off
;
2033 if (write_count
> count
) {
2034 write_count
= count
;
2036 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
2041 fidp
->fs
.xattr
.copied_len
+= write_count
;
2043 * Now copy the content from sg list
2045 for (i
= 0; i
< cnt
; i
++) {
2046 if (write_count
> sg
[i
].iov_len
) {
2047 to_copy
= sg
[i
].iov_len
;
2049 to_copy
= write_count
;
2051 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
2052 /* updating vs->off since we are not using below */
2054 write_count
-= to_copy
;
2060 static void coroutine_fn
v9fs_write(void *opaque
)
2070 V9fsPDU
*pdu
= opaque
;
2071 V9fsState
*s
= pdu
->s
;
2072 QEMUIOVector qiov_full
;
2075 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
2077 pdu_complete(pdu
, err
);
2081 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
2082 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
2084 fidp
= get_fid(pdu
, fid
);
2089 if (fidp
->fid_type
== P9_FID_FILE
) {
2090 if (fidp
->fs
.fd
== -1) {
2094 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2096 * setxattr operation
2098 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
2099 qiov_full
.iov
, qiov_full
.niov
);
2105 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2107 qemu_iovec_reset(&qiov
);
2108 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2110 print_sg(qiov
.iov
, qiov
.niov
);
2112 /* Loop in case of EINTR */
2114 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2119 } while (len
== -EINTR
&& !pdu
->cancelled
);
2121 /* IO error return the error */
2125 } while (total
< count
&& len
> 0);
2128 err
= pdu_marshal(pdu
, offset
, "d", total
);
2133 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2135 qemu_iovec_destroy(&qiov
);
2139 qemu_iovec_destroy(&qiov_full
);
2140 pdu_complete(pdu
, err
);
2143 static void coroutine_fn
v9fs_create(void *opaque
)
2155 V9fsString extension
;
2157 V9fsPDU
*pdu
= opaque
;
2159 v9fs_path_init(&path
);
2160 v9fs_string_init(&name
);
2161 v9fs_string_init(&extension
);
2162 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2163 &perm
, &mode
, &extension
);
2167 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2169 if (name_is_illegal(name
.data
)) {
2174 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2179 fidp
= get_fid(pdu
, fid
);
2184 if (fidp
->fid_type
!= P9_FID_NONE
) {
2188 if (perm
& P9_STAT_MODE_DIR
) {
2189 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2190 fidp
->uid
, -1, &stbuf
);
2194 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2198 v9fs_path_copy(&fidp
->path
, &path
);
2199 err
= v9fs_co_opendir(pdu
, fidp
);
2203 fidp
->fid_type
= P9_FID_DIR
;
2204 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2205 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2206 extension
.data
, -1 , &stbuf
);
2210 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2214 v9fs_path_copy(&fidp
->path
, &path
);
2215 } else if (perm
& P9_STAT_MODE_LINK
) {
2216 int32_t ofid
= atoi(extension
.data
);
2217 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2218 if (ofidp
== NULL
) {
2222 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2223 put_fid(pdu
, ofidp
);
2227 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2229 fidp
->fid_type
= P9_FID_NONE
;
2232 v9fs_path_copy(&fidp
->path
, &path
);
2233 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2235 fidp
->fid_type
= P9_FID_NONE
;
2238 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2240 uint32_t major
, minor
;
2243 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2260 nmode
|= perm
& 0777;
2261 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2262 makedev(major
, minor
), nmode
, &stbuf
);
2266 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2270 v9fs_path_copy(&fidp
->path
, &path
);
2271 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2272 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2273 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2277 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2281 v9fs_path_copy(&fidp
->path
, &path
);
2282 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2283 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2284 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2288 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2292 v9fs_path_copy(&fidp
->path
, &path
);
2294 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2295 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2299 fidp
->fid_type
= P9_FID_FILE
;
2300 fidp
->open_flags
= omode_to_uflags(mode
);
2301 if (fidp
->open_flags
& O_EXCL
) {
2303 * We let the host file system do O_EXCL check
2304 * We should not reclaim such fd
2306 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2309 iounit
= get_iounit(pdu
, &fidp
->path
);
2310 stat_to_qid(&stbuf
, &qid
);
2311 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2316 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2317 qid
.type
, qid
.version
, qid
.path
, iounit
);
2321 pdu_complete(pdu
, err
);
2322 v9fs_string_free(&name
);
2323 v9fs_string_free(&extension
);
2324 v9fs_path_free(&path
);
2327 static void coroutine_fn
v9fs_symlink(void *opaque
)
2329 V9fsPDU
*pdu
= opaque
;
2332 V9fsFidState
*dfidp
;
2340 v9fs_string_init(&name
);
2341 v9fs_string_init(&symname
);
2342 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2346 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2348 if (name_is_illegal(name
.data
)) {
2353 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2358 dfidp
= get_fid(pdu
, dfid
);
2359 if (dfidp
== NULL
) {
2363 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2367 stat_to_qid(&stbuf
, &qid
);
2368 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2373 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2374 qid
.type
, qid
.version
, qid
.path
);
2376 put_fid(pdu
, dfidp
);
2378 pdu_complete(pdu
, err
);
2379 v9fs_string_free(&name
);
2380 v9fs_string_free(&symname
);
2383 static void coroutine_fn
v9fs_flush(void *opaque
)
2388 V9fsPDU
*cancel_pdu
= NULL
;
2389 V9fsPDU
*pdu
= opaque
;
2390 V9fsState
*s
= pdu
->s
;
2392 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2394 pdu_complete(pdu
, err
);
2397 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2399 if (pdu
->tag
== tag
) {
2400 warn_report("the guest sent a self-referencing 9P flush request");
2402 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2403 if (cancel_pdu
->tag
== tag
) {
2409 cancel_pdu
->cancelled
= 1;
2411 * Wait for pdu to complete.
2413 qemu_co_queue_wait(&cancel_pdu
->complete
, NULL
);
2414 if (!qemu_co_queue_next(&cancel_pdu
->complete
)) {
2415 cancel_pdu
->cancelled
= 0;
2416 pdu_free(cancel_pdu
);
2419 pdu_complete(pdu
, 7);
2422 static void coroutine_fn
v9fs_link(void *opaque
)
2424 V9fsPDU
*pdu
= opaque
;
2425 int32_t dfid
, oldfid
;
2426 V9fsFidState
*dfidp
, *oldfidp
;
2431 v9fs_string_init(&name
);
2432 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2436 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2438 if (name_is_illegal(name
.data
)) {
2443 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2448 dfidp
= get_fid(pdu
, dfid
);
2449 if (dfidp
== NULL
) {
2454 oldfidp
= get_fid(pdu
, oldfid
);
2455 if (oldfidp
== NULL
) {
2459 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2463 put_fid(pdu
, oldfidp
);
2465 put_fid(pdu
, dfidp
);
2467 v9fs_string_free(&name
);
2468 pdu_complete(pdu
, err
);
2471 /* Only works with path name based fid */
2472 static void coroutine_fn
v9fs_remove(void *opaque
)
2478 V9fsPDU
*pdu
= opaque
;
2480 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2484 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2486 fidp
= get_fid(pdu
, fid
);
2491 /* if fs driver is not path based, return EOPNOTSUPP */
2492 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2497 * IF the file is unlinked, we cannot reopen
2498 * the file later. So don't reclaim fd
2500 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2504 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2509 /* For TREMOVE we need to clunk the fid even on failed remove */
2510 clunk_fid(pdu
->s
, fidp
->fid
);
2513 pdu_complete(pdu
, err
);
2516 static void coroutine_fn
v9fs_unlinkat(void *opaque
)
2520 int32_t dfid
, flags
;
2523 V9fsFidState
*dfidp
;
2524 V9fsPDU
*pdu
= opaque
;
2526 v9fs_string_init(&name
);
2527 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2532 if (name_is_illegal(name
.data
)) {
2537 if (!strcmp(".", name
.data
)) {
2542 if (!strcmp("..", name
.data
)) {
2547 dfidp
= get_fid(pdu
, dfid
);
2548 if (dfidp
== NULL
) {
2553 * IF the file is unlinked, we cannot reopen
2554 * the file later. So don't reclaim fd
2556 v9fs_path_init(&path
);
2557 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2561 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2565 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2570 put_fid(pdu
, dfidp
);
2571 v9fs_path_free(&path
);
2573 pdu_complete(pdu
, err
);
2574 v9fs_string_free(&name
);
2578 /* Only works with path name based fid */
2579 static int coroutine_fn
v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2585 V9fsFidState
*tfidp
;
2586 V9fsState
*s
= pdu
->s
;
2587 V9fsFidState
*dirfidp
= NULL
;
2589 v9fs_path_init(&new_path
);
2590 if (newdirfid
!= -1) {
2591 dirfidp
= get_fid(pdu
, newdirfid
);
2592 if (dirfidp
== NULL
) {
2596 if (fidp
->fid_type
!= P9_FID_NONE
) {
2600 err
= v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2605 char *dir_name
= g_path_get_dirname(fidp
->path
.data
);
2608 v9fs_path_init(&dir_path
);
2609 v9fs_path_sprintf(&dir_path
, "%s", dir_name
);
2612 err
= v9fs_co_name_to_path(pdu
, &dir_path
, name
->data
, &new_path
);
2613 v9fs_path_free(&dir_path
);
2618 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2623 * Fixup fid's pointing to the old name to
2624 * start pointing to the new name
2626 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2627 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2628 /* replace the name */
2629 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2634 put_fid(pdu
, dirfidp
);
2636 v9fs_path_free(&new_path
);
2641 /* Only works with path name based fid */
2642 static void coroutine_fn
v9fs_rename(void *opaque
)
2650 V9fsPDU
*pdu
= opaque
;
2651 V9fsState
*s
= pdu
->s
;
2653 v9fs_string_init(&name
);
2654 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2659 if (name_is_illegal(name
.data
)) {
2664 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2669 fidp
= get_fid(pdu
, fid
);
2674 if (fidp
->fid_type
!= P9_FID_NONE
) {
2678 /* if fs driver is not path based, return EOPNOTSUPP */
2679 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2683 v9fs_path_write_lock(s
);
2684 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2685 v9fs_path_unlock(s
);
2692 pdu_complete(pdu
, err
);
2693 v9fs_string_free(&name
);
2696 static int coroutine_fn
v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2697 V9fsString
*old_name
,
2699 V9fsString
*new_name
)
2701 V9fsFidState
*tfidp
;
2702 V9fsPath oldpath
, newpath
;
2703 V9fsState
*s
= pdu
->s
;
2706 v9fs_path_init(&oldpath
);
2707 v9fs_path_init(&newpath
);
2708 err
= v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2712 err
= v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2718 * Fixup fid's pointing to the old name to
2719 * start pointing to the new name
2721 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2722 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2723 /* replace the name */
2724 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2728 v9fs_path_free(&oldpath
);
2729 v9fs_path_free(&newpath
);
2733 static int coroutine_fn
v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2734 V9fsString
*old_name
,
2736 V9fsString
*new_name
)
2739 V9fsState
*s
= pdu
->s
;
2740 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2742 olddirfidp
= get_fid(pdu
, olddirfid
);
2743 if (olddirfidp
== NULL
) {
2747 if (newdirfid
!= -1) {
2748 newdirfidp
= get_fid(pdu
, newdirfid
);
2749 if (newdirfidp
== NULL
) {
2754 newdirfidp
= get_fid(pdu
, olddirfid
);
2757 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2758 &newdirfidp
->path
, new_name
);
2762 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2763 /* Only for path based fid we need to do the below fixup */
2764 err
= v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2765 &newdirfidp
->path
, new_name
);
2769 put_fid(pdu
, olddirfidp
);
2772 put_fid(pdu
, newdirfidp
);
2777 static void coroutine_fn
v9fs_renameat(void *opaque
)
2781 V9fsPDU
*pdu
= opaque
;
2782 V9fsState
*s
= pdu
->s
;
2783 int32_t olddirfid
, newdirfid
;
2784 V9fsString old_name
, new_name
;
2786 v9fs_string_init(&old_name
);
2787 v9fs_string_init(&new_name
);
2788 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2789 &old_name
, &newdirfid
, &new_name
);
2794 if (name_is_illegal(old_name
.data
) || name_is_illegal(new_name
.data
)) {
2799 if (!strcmp(".", old_name
.data
) || !strcmp("..", old_name
.data
) ||
2800 !strcmp(".", new_name
.data
) || !strcmp("..", new_name
.data
)) {
2805 v9fs_path_write_lock(s
);
2806 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2807 &old_name
, newdirfid
, &new_name
);
2808 v9fs_path_unlock(s
);
2814 pdu_complete(pdu
, err
);
2815 v9fs_string_free(&old_name
);
2816 v9fs_string_free(&new_name
);
2819 static void coroutine_fn
v9fs_wstat(void *opaque
)
2828 V9fsPDU
*pdu
= opaque
;
2830 v9fs_stat_init(&v9stat
);
2831 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2835 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2836 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2838 fidp
= get_fid(pdu
, fid
);
2843 /* do we need to sync the file? */
2844 if (donttouch_stat(&v9stat
)) {
2845 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2848 if (v9stat
.mode
!= -1) {
2850 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2854 v9_mode
= stat_to_v9mode(&stbuf
);
2855 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2856 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2857 /* Attempting to change the type */
2861 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2862 v9mode_to_mode(v9stat
.mode
,
2863 &v9stat
.extension
));
2868 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2869 struct timespec times
[2];
2870 if (v9stat
.atime
!= -1) {
2871 times
[0].tv_sec
= v9stat
.atime
;
2872 times
[0].tv_nsec
= 0;
2874 times
[0].tv_nsec
= UTIME_OMIT
;
2876 if (v9stat
.mtime
!= -1) {
2877 times
[1].tv_sec
= v9stat
.mtime
;
2878 times
[1].tv_nsec
= 0;
2880 times
[1].tv_nsec
= UTIME_OMIT
;
2882 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2887 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2888 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2893 if (v9stat
.name
.size
!= 0) {
2894 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2899 if (v9stat
.length
!= -1) {
2900 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2909 v9fs_stat_free(&v9stat
);
2910 pdu_complete(pdu
, err
);
2913 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2925 int32_t bsize_factor
;
2928 * compute bsize factor based on host file system block size
2931 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2932 if (!bsize_factor
) {
2935 f_type
= stbuf
->f_type
;
2936 f_bsize
= stbuf
->f_bsize
;
2937 f_bsize
*= bsize_factor
;
2939 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2940 * adjust(divide) the number of blocks, free blocks and available
2941 * blocks by bsize factor
2943 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2944 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2945 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2946 f_files
= stbuf
->f_files
;
2947 f_ffree
= stbuf
->f_ffree
;
2948 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2949 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2950 f_namelen
= stbuf
->f_namelen
;
2952 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2953 f_type
, f_bsize
, f_blocks
, f_bfree
,
2954 f_bavail
, f_files
, f_ffree
,
2955 fsid_val
, f_namelen
);
2958 static void coroutine_fn
v9fs_statfs(void *opaque
)
2964 struct statfs stbuf
;
2965 V9fsPDU
*pdu
= opaque
;
2966 V9fsState
*s
= pdu
->s
;
2968 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2972 fidp
= get_fid(pdu
, fid
);
2977 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2981 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2989 pdu_complete(pdu
, retval
);
2992 static void coroutine_fn
v9fs_mknod(void *opaque
)
3005 V9fsPDU
*pdu
= opaque
;
3007 v9fs_string_init(&name
);
3008 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
3009 &major
, &minor
, &gid
);
3013 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
3015 if (name_is_illegal(name
.data
)) {
3020 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3025 fidp
= get_fid(pdu
, fid
);
3030 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
3031 makedev(major
, minor
), mode
, &stbuf
);
3035 stat_to_qid(&stbuf
, &qid
);
3036 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3041 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
3042 qid
.type
, qid
.version
, qid
.path
);
3046 pdu_complete(pdu
, err
);
3047 v9fs_string_free(&name
);
3051 * Implement posix byte range locking code
3052 * Server side handling of locking code is very simple, because 9p server in
3053 * QEMU can handle only one client. And most of the lock handling
3054 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3055 * do any thing in * qemu 9p server side lock code path.
3056 * So when a TLOCK request comes, always return success
3058 static void coroutine_fn
v9fs_lock(void *opaque
)
3064 int32_t fid
, err
= 0;
3065 V9fsPDU
*pdu
= opaque
;
3067 v9fs_string_init(&flock
.client_id
);
3068 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
3069 &flock
.flags
, &flock
.start
, &flock
.length
,
3070 &flock
.proc_id
, &flock
.client_id
);
3074 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
3075 flock
.type
, flock
.start
, flock
.length
);
3078 /* We support only block flag now (that too ignored currently) */
3079 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
3083 fidp
= get_fid(pdu
, fid
);
3088 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3092 err
= pdu_marshal(pdu
, offset
, "b", P9_LOCK_SUCCESS
);
3097 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, P9_LOCK_SUCCESS
);
3101 pdu_complete(pdu
, err
);
3102 v9fs_string_free(&flock
.client_id
);
3106 * When a TGETLOCK request comes, always return success because all lock
3107 * handling is done by client's VFS layer.
3109 static void coroutine_fn
v9fs_getlock(void *opaque
)
3115 int32_t fid
, err
= 0;
3116 V9fsPDU
*pdu
= opaque
;
3118 v9fs_string_init(&glock
.client_id
);
3119 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
3120 &glock
.start
, &glock
.length
, &glock
.proc_id
,
3125 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
3126 glock
.type
, glock
.start
, glock
.length
);
3128 fidp
= get_fid(pdu
, fid
);
3133 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3137 glock
.type
= P9_LOCK_TYPE_UNLCK
;
3138 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
3139 glock
.start
, glock
.length
, glock
.proc_id
,
3145 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
3146 glock
.length
, glock
.proc_id
);
3150 pdu_complete(pdu
, err
);
3151 v9fs_string_free(&glock
.client_id
);
3154 static void coroutine_fn
v9fs_mkdir(void *opaque
)
3156 V9fsPDU
*pdu
= opaque
;
3167 v9fs_string_init(&name
);
3168 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
3172 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
3174 if (name_is_illegal(name
.data
)) {
3179 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3184 fidp
= get_fid(pdu
, fid
);
3189 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
3193 stat_to_qid(&stbuf
, &qid
);
3194 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3199 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
3200 qid
.type
, qid
.version
, qid
.path
, err
);
3204 pdu_complete(pdu
, err
);
3205 v9fs_string_free(&name
);
3208 static void coroutine_fn
v9fs_xattrwalk(void *opaque
)
3214 int32_t fid
, newfid
;
3215 V9fsFidState
*file_fidp
;
3216 V9fsFidState
*xattr_fidp
= NULL
;
3217 V9fsPDU
*pdu
= opaque
;
3218 V9fsState
*s
= pdu
->s
;
3220 v9fs_string_init(&name
);
3221 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3225 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3227 file_fidp
= get_fid(pdu
, fid
);
3228 if (file_fidp
== NULL
) {
3232 xattr_fidp
= alloc_fid(s
, newfid
);
3233 if (xattr_fidp
== NULL
) {
3237 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3238 if (!v9fs_string_size(&name
)) {
3240 * listxattr request. Get the size first
3242 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3245 clunk_fid(s
, xattr_fidp
->fid
);
3249 * Read the xattr value
3251 xattr_fidp
->fs
.xattr
.len
= size
;
3252 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3253 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3255 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3256 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3257 xattr_fidp
->fs
.xattr
.value
,
3258 xattr_fidp
->fs
.xattr
.len
);
3260 clunk_fid(s
, xattr_fidp
->fid
);
3264 err
= pdu_marshal(pdu
, offset
, "q", size
);
3271 * specific xattr fid. We check for xattr
3272 * presence also collect the xattr size
3274 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3278 clunk_fid(s
, xattr_fidp
->fid
);
3282 * Read the xattr value
3284 xattr_fidp
->fs
.xattr
.len
= size
;
3285 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3286 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3288 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3289 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3290 &name
, xattr_fidp
->fs
.xattr
.value
,
3291 xattr_fidp
->fs
.xattr
.len
);
3293 clunk_fid(s
, xattr_fidp
->fid
);
3297 err
= pdu_marshal(pdu
, offset
, "q", size
);
3303 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3305 put_fid(pdu
, file_fidp
);
3307 put_fid(pdu
, xattr_fidp
);
3310 pdu_complete(pdu
, err
);
3311 v9fs_string_free(&name
);
3314 static void coroutine_fn
v9fs_xattrcreate(void *opaque
)
3322 V9fsFidState
*file_fidp
;
3323 V9fsFidState
*xattr_fidp
;
3324 V9fsPDU
*pdu
= opaque
;
3326 v9fs_string_init(&name
);
3327 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3331 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3333 if (size
> XATTR_SIZE_MAX
) {
3338 file_fidp
= get_fid(pdu
, fid
);
3339 if (file_fidp
== NULL
) {
3343 if (file_fidp
->fid_type
!= P9_FID_NONE
) {
3348 /* Make the file fid point to xattr */
3349 xattr_fidp
= file_fidp
;
3350 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3351 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3352 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= false;
3353 xattr_fidp
->fs
.xattr
.len
= size
;
3354 xattr_fidp
->fs
.xattr
.flags
= flags
;
3355 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3356 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3357 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3360 put_fid(pdu
, file_fidp
);
3362 pdu_complete(pdu
, err
);
3363 v9fs_string_free(&name
);
3366 static void coroutine_fn
v9fs_readlink(void *opaque
)
3368 V9fsPDU
*pdu
= opaque
;
3375 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3379 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3380 fidp
= get_fid(pdu
, fid
);
3386 v9fs_string_init(&target
);
3387 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3391 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3393 v9fs_string_free(&target
);
3397 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3398 v9fs_string_free(&target
);
3402 pdu_complete(pdu
, err
);
3405 static CoroutineEntry
*pdu_co_handlers
[] = {
3406 [P9_TREADDIR
] = v9fs_readdir
,
3407 [P9_TSTATFS
] = v9fs_statfs
,
3408 [P9_TGETATTR
] = v9fs_getattr
,
3409 [P9_TSETATTR
] = v9fs_setattr
,
3410 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3411 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3412 [P9_TMKNOD
] = v9fs_mknod
,
3413 [P9_TRENAME
] = v9fs_rename
,
3414 [P9_TLOCK
] = v9fs_lock
,
3415 [P9_TGETLOCK
] = v9fs_getlock
,
3416 [P9_TRENAMEAT
] = v9fs_renameat
,
3417 [P9_TREADLINK
] = v9fs_readlink
,
3418 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3419 [P9_TMKDIR
] = v9fs_mkdir
,
3420 [P9_TVERSION
] = v9fs_version
,
3421 [P9_TLOPEN
] = v9fs_open
,
3422 [P9_TATTACH
] = v9fs_attach
,
3423 [P9_TSTAT
] = v9fs_stat
,
3424 [P9_TWALK
] = v9fs_walk
,
3425 [P9_TCLUNK
] = v9fs_clunk
,
3426 [P9_TFSYNC
] = v9fs_fsync
,
3427 [P9_TOPEN
] = v9fs_open
,
3428 [P9_TREAD
] = v9fs_read
,
3430 [P9_TAUTH
] = v9fs_auth
,
3432 [P9_TFLUSH
] = v9fs_flush
,
3433 [P9_TLINK
] = v9fs_link
,
3434 [P9_TSYMLINK
] = v9fs_symlink
,
3435 [P9_TCREATE
] = v9fs_create
,
3436 [P9_TLCREATE
] = v9fs_lcreate
,
3437 [P9_TWRITE
] = v9fs_write
,
3438 [P9_TWSTAT
] = v9fs_wstat
,
3439 [P9_TREMOVE
] = v9fs_remove
,
3442 static void coroutine_fn
v9fs_op_not_supp(void *opaque
)
3444 V9fsPDU
*pdu
= opaque
;
3445 pdu_complete(pdu
, -EOPNOTSUPP
);
3448 static void coroutine_fn
v9fs_fs_ro(void *opaque
)
3450 V9fsPDU
*pdu
= opaque
;
3451 pdu_complete(pdu
, -EROFS
);
3454 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3481 void pdu_submit(V9fsPDU
*pdu
, P9MsgHeader
*hdr
)
3484 CoroutineEntry
*handler
;
3485 V9fsState
*s
= pdu
->s
;
3487 pdu
->size
= le32_to_cpu(hdr
->size_le
);
3489 pdu
->tag
= le16_to_cpu(hdr
->tag_le
);
3491 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3492 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3493 handler
= v9fs_op_not_supp
;
3494 } else if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3495 handler
= v9fs_fs_ro
;
3497 handler
= pdu_co_handlers
[pdu
->id
];
3500 qemu_co_queue_init(&pdu
->complete
);
3501 co
= qemu_coroutine_create(handler
, pdu
);
3502 qemu_coroutine_enter(co
);
3505 /* Returns 0 on success, 1 on failure. */
3506 int v9fs_device_realize_common(V9fsState
*s
, const V9fsTransport
*t
,
3515 assert(!s
->transport
);
3518 /* initialize pdu allocator */
3519 QLIST_INIT(&s
->free_list
);
3520 QLIST_INIT(&s
->active_list
);
3521 for (i
= 0; i
< MAX_REQ
; i
++) {
3522 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
3527 v9fs_path_init(&path
);
3529 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
3532 /* We don't have a fsdev identified by fsdev_id */
3533 error_setg(errp
, "9pfs device couldn't find fsdev with the "
3535 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
3539 if (!s
->fsconf
.tag
) {
3540 /* we haven't specified a mount_tag */
3541 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
3542 s
->fsconf
.fsdev_id
);
3546 s
->ctx
.export_flags
= fse
->export_flags
;
3547 s
->ctx
.fs_root
= g_strdup(fse
->path
);
3548 s
->ctx
.exops
.get_st_gen
= NULL
;
3549 len
= strlen(s
->fsconf
.tag
);
3550 if (len
> MAX_TAG_LEN
- 1) {
3551 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
3552 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
3556 s
->tag
= g_strdup(s
->fsconf
.tag
);
3561 s
->ctx
.fmode
= fse
->fmode
;
3562 s
->ctx
.dmode
= fse
->dmode
;
3565 qemu_co_rwlock_init(&s
->rename_lock
);
3567 if (s
->ops
->init(&s
->ctx
, errp
) < 0) {
3568 error_prepend(errp
, "cannot initialize fsdev '%s': ",
3569 s
->fsconf
.fsdev_id
);
3574 * Check details of export path, We need to use fs driver
3575 * call back to do that. Since we are in the init path, we don't
3576 * use co-routines here.
3578 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
3580 "error in converting name to path %s", strerror(errno
));
3583 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
3584 error_setg(errp
, "share path %s does not exist", fse
->path
);
3586 } else if (!S_ISDIR(stat
.st_mode
)) {
3587 error_setg(errp
, "share path %s is not a directory", fse
->path
);
3591 s
->ctx
.fst
= &fse
->fst
;
3592 fsdev_throttle_init(s
->ctx
.fst
);
3594 v9fs_path_free(&path
);
3599 if (s
->ops
&& s
->ops
->cleanup
&& s
->ctx
.private) {
3600 s
->ops
->cleanup(&s
->ctx
);
3603 g_free(s
->ctx
.fs_root
);
3604 v9fs_path_free(&path
);
3609 void v9fs_device_unrealize_common(V9fsState
*s
, Error
**errp
)
3611 if (s
->ops
->cleanup
) {
3612 s
->ops
->cleanup(&s
->ctx
);
3614 fsdev_throttle_cleanup(s
->ctx
.fst
);
3616 g_free(s
->ctx
.fs_root
);
3619 typedef struct VirtfsCoResetData
{
3622 } VirtfsCoResetData
;
3624 static void coroutine_fn
virtfs_co_reset(void *opaque
)
3626 VirtfsCoResetData
*data
= opaque
;
3628 virtfs_reset(&data
->pdu
);
3632 void v9fs_reset(V9fsState
*s
)
3634 VirtfsCoResetData data
= { .pdu
= { .s
= s
}, .done
= false };
3637 while (!QLIST_EMPTY(&s
->active_list
)) {
3638 aio_poll(qemu_get_aio_context(), true);
3641 co
= qemu_coroutine_create(virtfs_co_reset
, &data
);
3642 qemu_coroutine_enter(co
);
3644 while (!data
.done
) {
3645 aio_poll(qemu_get_aio_context(), true);
3649 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
3652 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3653 error_report("Failed to get the resource limit");
3656 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3657 open_fd_rc
= rlim
.rlim_cur
/2;