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/main-loop.h"
21 #include "qemu/sockets.h"
22 #include "virtio-9p.h"
23 #include "fsdev/qemu-fsdev.h"
27 #include "migration/blocker.h"
28 #include "sysemu/qtest.h"
32 static int open_fd_rc
;
46 static ssize_t
pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
52 ret
= pdu
->s
->transport
->pdu_vmarshal(pdu
, offset
, fmt
, ap
);
58 static ssize_t
pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
64 ret
= pdu
->s
->transport
->pdu_vunmarshal(pdu
, offset
, fmt
, ap
);
70 static int omode_to_uflags(int8_t mode
)
104 typedef struct DotlOpenflagMap
{
109 static int dotl_to_open_flags(int flags
)
113 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
114 * and P9_DOTL_NOACCESS
116 int oflags
= flags
& O_ACCMODE
;
118 DotlOpenflagMap dotl_oflag_map
[] = {
119 { P9_DOTL_CREATE
, O_CREAT
},
120 { P9_DOTL_EXCL
, O_EXCL
},
121 { P9_DOTL_NOCTTY
, O_NOCTTY
},
122 { P9_DOTL_TRUNC
, O_TRUNC
},
123 { P9_DOTL_APPEND
, O_APPEND
},
124 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
125 { P9_DOTL_DSYNC
, O_DSYNC
},
126 { P9_DOTL_FASYNC
, FASYNC
},
127 { P9_DOTL_DIRECT
, O_DIRECT
},
128 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
129 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
130 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
131 { P9_DOTL_NOATIME
, O_NOATIME
},
132 { P9_DOTL_SYNC
, O_SYNC
},
135 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
136 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
137 oflags
|= dotl_oflag_map
[i
].open_flag
;
144 void cred_init(FsCred
*credp
)
152 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
156 * Filter the client open flags
158 flags
= dotl_to_open_flags(oflags
);
159 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
161 * Ignore direct disk access hint until the server supports it.
167 void v9fs_path_init(V9fsPath
*path
)
173 void v9fs_path_free(V9fsPath
*path
)
181 void GCC_FMT_ATTR(2, 3)
182 v9fs_path_sprintf(V9fsPath
*path
, const char *fmt
, ...)
186 v9fs_path_free(path
);
189 /* Bump the size for including terminating NULL */
190 path
->size
= g_vasprintf(&path
->data
, fmt
, ap
) + 1;
194 void v9fs_path_copy(V9fsPath
*dst
, const V9fsPath
*src
)
197 dst
->size
= src
->size
;
198 dst
->data
= g_memdup(src
->data
, src
->size
);
201 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
202 const char *name
, V9fsPath
*path
)
205 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
213 * Return TRUE if s1 is an ancestor of s2.
215 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
216 * As a special case, We treat s1 as ancestor of s2 if they are same!
218 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
220 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
221 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
228 static size_t v9fs_string_size(V9fsString
*str
)
234 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
235 static int coroutine_fn
v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
238 if (f
->fid_type
== P9_FID_FILE
) {
239 if (f
->fs
.fd
== -1) {
241 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
242 } while (err
== -EINTR
&& !pdu
->cancelled
);
244 } else if (f
->fid_type
== P9_FID_DIR
) {
245 if (f
->fs
.dir
.stream
== NULL
) {
247 err
= v9fs_co_opendir(pdu
, f
);
248 } while (err
== -EINTR
&& !pdu
->cancelled
);
254 static V9fsFidState
*coroutine_fn
get_fid(V9fsPDU
*pdu
, int32_t fid
)
258 V9fsState
*s
= pdu
->s
;
260 for (f
= s
->fid_list
; f
; f
= f
->next
) {
264 * Update the fid ref upfront so that
265 * we don't get reclaimed when we yield
270 * check whether we need to reopen the
271 * file. We might have closed the fd
272 * while trying to free up some file
275 err
= v9fs_reopen_fid(pdu
, f
);
281 * Mark the fid as referenced so that the LRU
282 * reclaim won't close the file descriptor
284 f
->flags
|= FID_REFERENCED
;
291 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
295 for (f
= s
->fid_list
; f
; f
= f
->next
) {
296 /* If fid is already there return NULL */
302 f
= g_malloc0(sizeof(V9fsFidState
));
304 f
->fid_type
= P9_FID_NONE
;
307 * Mark the fid as referenced so that the LRU
308 * reclaim won't close the file descriptor
310 f
->flags
|= FID_REFERENCED
;
311 f
->next
= s
->fid_list
;
314 v9fs_readdir_init(&f
->fs
.dir
);
315 v9fs_readdir_init(&f
->fs_reclaim
.dir
);
320 static int coroutine_fn
v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
324 if (fidp
->fs
.xattr
.xattrwalk_fid
) {
325 /* getxattr/listxattr fid */
329 * if this is fid for setxattr. clunk should
330 * result in setxattr localcall
332 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
333 /* clunk after partial write */
337 if (fidp
->fs
.xattr
.len
) {
338 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
339 fidp
->fs
.xattr
.value
,
341 fidp
->fs
.xattr
.flags
);
343 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
346 v9fs_string_free(&fidp
->fs
.xattr
.name
);
348 g_free(fidp
->fs
.xattr
.value
);
352 static int coroutine_fn
free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
356 if (fidp
->fid_type
== P9_FID_FILE
) {
357 /* If we reclaimed the fd no need to close */
358 if (fidp
->fs
.fd
!= -1) {
359 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
361 } else if (fidp
->fid_type
== P9_FID_DIR
) {
362 if (fidp
->fs
.dir
.stream
!= NULL
) {
363 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
365 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
366 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
368 v9fs_path_free(&fidp
->path
);
373 static int coroutine_fn
put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
378 * Don't free the fid if it is in reclaim list
380 if (!fidp
->ref
&& fidp
->clunked
) {
381 if (fidp
->fid
== pdu
->s
->root_fid
) {
383 * if the clunked fid is root fid then we
384 * have unmounted the fs on the client side.
385 * delete the migration blocker. Ideally, this
386 * should be hooked to transport close notification
388 if (pdu
->s
->migration_blocker
) {
389 migrate_del_blocker(pdu
->s
->migration_blocker
);
390 error_free(pdu
->s
->migration_blocker
);
391 pdu
->s
->migration_blocker
= NULL
;
394 return free_fid(pdu
, fidp
);
399 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
401 V9fsFidState
**fidpp
, *fidp
;
403 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
404 if ((*fidpp
)->fid
== fid
) {
408 if (*fidpp
== NULL
) {
417 void coroutine_fn
v9fs_reclaim_fd(V9fsPDU
*pdu
)
419 int reclaim_count
= 0;
420 V9fsState
*s
= pdu
->s
;
421 V9fsFidState
*f
, *reclaim_list
= NULL
;
423 for (f
= s
->fid_list
; f
; f
= f
->next
) {
425 * Unlink fids cannot be reclaimed. Check
426 * for them and skip them. Also skip fids
427 * currently being operated on.
429 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
433 * if it is a recently referenced fid
434 * we leave the fid untouched and clear the
435 * reference bit. We come back to it later
436 * in the next iteration. (a simple LRU without
437 * moving list elements around)
439 if (f
->flags
& FID_REFERENCED
) {
440 f
->flags
&= ~FID_REFERENCED
;
444 * Add fids to reclaim list.
446 if (f
->fid_type
== P9_FID_FILE
) {
447 if (f
->fs
.fd
!= -1) {
449 * Up the reference count so that
450 * a clunk request won't free this fid
453 f
->rclm_lst
= reclaim_list
;
455 f
->fs_reclaim
.fd
= f
->fs
.fd
;
459 } else if (f
->fid_type
== P9_FID_DIR
) {
460 if (f
->fs
.dir
.stream
!= NULL
) {
462 * Up the reference count so that
463 * a clunk request won't free this fid
466 f
->rclm_lst
= reclaim_list
;
468 f
->fs_reclaim
.dir
.stream
= f
->fs
.dir
.stream
;
469 f
->fs
.dir
.stream
= NULL
;
473 if (reclaim_count
>= open_fd_rc
) {
478 * Now close the fid in reclaim list. Free them if they
479 * are already clunked.
481 while (reclaim_list
) {
483 reclaim_list
= f
->rclm_lst
;
484 if (f
->fid_type
== P9_FID_FILE
) {
485 v9fs_co_close(pdu
, &f
->fs_reclaim
);
486 } else if (f
->fid_type
== P9_FID_DIR
) {
487 v9fs_co_closedir(pdu
, &f
->fs_reclaim
);
491 * Now drop the fid reference, free it
498 static int coroutine_fn
v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
501 V9fsState
*s
= pdu
->s
;
502 V9fsFidState
*fidp
, head_fid
;
504 head_fid
.next
= s
->fid_list
;
505 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
506 if (fidp
->path
.size
!= path
->size
) {
509 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
510 /* Mark the fid non reclaimable. */
511 fidp
->flags
|= FID_NON_RECLAIMABLE
;
513 /* reopen the file/dir if already closed */
514 err
= v9fs_reopen_fid(pdu
, fidp
);
519 * Go back to head of fid list because
520 * the list could have got updated when
521 * switched to the worker thread
531 static void coroutine_fn
virtfs_reset(V9fsPDU
*pdu
)
533 V9fsState
*s
= pdu
->s
;
537 while (s
->fid_list
) {
543 s
->fid_list
= fidp
->next
;
550 #define P9_QID_TYPE_DIR 0x80
551 #define P9_QID_TYPE_SYMLINK 0x02
553 #define P9_STAT_MODE_DIR 0x80000000
554 #define P9_STAT_MODE_APPEND 0x40000000
555 #define P9_STAT_MODE_EXCL 0x20000000
556 #define P9_STAT_MODE_MOUNT 0x10000000
557 #define P9_STAT_MODE_AUTH 0x08000000
558 #define P9_STAT_MODE_TMP 0x04000000
559 #define P9_STAT_MODE_SYMLINK 0x02000000
560 #define P9_STAT_MODE_LINK 0x01000000
561 #define P9_STAT_MODE_DEVICE 0x00800000
562 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
563 #define P9_STAT_MODE_SOCKET 0x00100000
564 #define P9_STAT_MODE_SETUID 0x00080000
565 #define P9_STAT_MODE_SETGID 0x00040000
566 #define P9_STAT_MODE_SETVTX 0x00010000
568 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
569 P9_STAT_MODE_SYMLINK | \
570 P9_STAT_MODE_LINK | \
571 P9_STAT_MODE_DEVICE | \
572 P9_STAT_MODE_NAMED_PIPE | \
575 /* This is the algorithm from ufs in spfs */
576 static int stat_to_qid(V9fsPDU
*pdu
, const struct stat
*stbuf
, V9fsQID
*qidp
)
580 if (pdu
->s
->dev_id
!= stbuf
->st_dev
) {
582 "9p: Multiple devices detected in same VirtFS export, "
583 "which might lead to file ID collisions and severe "
584 "misbehaviours on guest! You should use a separate "
585 "export for each device shared from host."
589 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
590 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
591 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
592 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
594 if (S_ISDIR(stbuf
->st_mode
)) {
595 qidp
->type
|= P9_QID_TYPE_DIR
;
597 if (S_ISLNK(stbuf
->st_mode
)) {
598 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
604 static int coroutine_fn
fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
610 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
614 err
= stat_to_qid(pdu
, &stbuf
, qidp
);
621 V9fsPDU
*pdu_alloc(V9fsState
*s
)
625 if (!QLIST_EMPTY(&s
->free_list
)) {
626 pdu
= QLIST_FIRST(&s
->free_list
);
627 QLIST_REMOVE(pdu
, next
);
628 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
633 void pdu_free(V9fsPDU
*pdu
)
635 V9fsState
*s
= pdu
->s
;
637 g_assert(!pdu
->cancelled
);
638 QLIST_REMOVE(pdu
, next
);
639 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
642 static void coroutine_fn
pdu_complete(V9fsPDU
*pdu
, ssize_t len
)
644 int8_t id
= pdu
->id
+ 1; /* Response */
645 V9fsState
*s
= pdu
->s
;
649 * The 9p spec requires that successfully cancelled pdus receive no reply.
650 * Sending a reply would confuse clients because they would
651 * assume that any EINTR is the actual result of the operation,
652 * rather than a consequence of the cancellation. However, if
653 * the operation completed (succesfully or with an error other
654 * than caused be cancellation), we do send out that reply, both
655 * for efficiency and to avoid confusing the rest of the state machine
656 * that assumes passing a non-error here will mean a successful
657 * transmission of the reply.
659 bool discard
= pdu
->cancelled
&& len
== -EINTR
;
661 trace_v9fs_rcancel(pdu
->tag
, pdu
->id
);
670 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
673 str
.data
= strerror(err
);
674 str
.size
= strlen(str
.data
);
676 ret
= pdu_marshal(pdu
, len
, "s", &str
);
684 ret
= pdu_marshal(pdu
, len
, "d", err
);
690 if (s
->proto_version
== V9FS_PROTO_2000L
) {
693 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
696 /* fill out the header */
697 if (pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
) < 0) {
701 /* keep these in sync */
706 pdu
->s
->transport
->push_and_notify(pdu
);
708 /* Now wakeup anybody waiting in flush for this request */
709 if (!qemu_co_queue_next(&pdu
->complete
)) {
714 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
719 if (mode
& P9_STAT_MODE_DIR
) {
723 if (mode
& P9_STAT_MODE_SYMLINK
) {
726 if (mode
& P9_STAT_MODE_SOCKET
) {
729 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
732 if (mode
& P9_STAT_MODE_DEVICE
) {
733 if (extension
->size
&& extension
->data
[0] == 'c') {
744 if (mode
& P9_STAT_MODE_SETUID
) {
747 if (mode
& P9_STAT_MODE_SETGID
) {
750 if (mode
& P9_STAT_MODE_SETVTX
) {
757 static int donttouch_stat(V9fsStat
*stat
)
759 if (stat
->type
== -1 &&
761 stat
->qid
.type
== 0xff &&
762 stat
->qid
.version
== (uint32_t) -1 &&
763 stat
->qid
.path
== (uint64_t) -1 &&
767 stat
->length
== -1 &&
774 stat
->n_muid
== -1) {
781 static void v9fs_stat_init(V9fsStat
*stat
)
783 v9fs_string_init(&stat
->name
);
784 v9fs_string_init(&stat
->uid
);
785 v9fs_string_init(&stat
->gid
);
786 v9fs_string_init(&stat
->muid
);
787 v9fs_string_init(&stat
->extension
);
790 static void v9fs_stat_free(V9fsStat
*stat
)
792 v9fs_string_free(&stat
->name
);
793 v9fs_string_free(&stat
->uid
);
794 v9fs_string_free(&stat
->gid
);
795 v9fs_string_free(&stat
->muid
);
796 v9fs_string_free(&stat
->extension
);
799 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
803 mode
= stbuf
->st_mode
& 0777;
804 if (S_ISDIR(stbuf
->st_mode
)) {
805 mode
|= P9_STAT_MODE_DIR
;
808 if (S_ISLNK(stbuf
->st_mode
)) {
809 mode
|= P9_STAT_MODE_SYMLINK
;
812 if (S_ISSOCK(stbuf
->st_mode
)) {
813 mode
|= P9_STAT_MODE_SOCKET
;
816 if (S_ISFIFO(stbuf
->st_mode
)) {
817 mode
|= P9_STAT_MODE_NAMED_PIPE
;
820 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
821 mode
|= P9_STAT_MODE_DEVICE
;
824 if (stbuf
->st_mode
& S_ISUID
) {
825 mode
|= P9_STAT_MODE_SETUID
;
828 if (stbuf
->st_mode
& S_ISGID
) {
829 mode
|= P9_STAT_MODE_SETGID
;
832 if (stbuf
->st_mode
& S_ISVTX
) {
833 mode
|= P9_STAT_MODE_SETVTX
;
839 static int coroutine_fn
stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*path
,
840 const char *basename
,
841 const struct stat
*stbuf
,
846 memset(v9stat
, 0, sizeof(*v9stat
));
848 err
= stat_to_qid(pdu
, stbuf
, &v9stat
->qid
);
852 v9stat
->mode
= stat_to_v9mode(stbuf
);
853 v9stat
->atime
= stbuf
->st_atime
;
854 v9stat
->mtime
= stbuf
->st_mtime
;
855 v9stat
->length
= stbuf
->st_size
;
857 v9fs_string_free(&v9stat
->uid
);
858 v9fs_string_free(&v9stat
->gid
);
859 v9fs_string_free(&v9stat
->muid
);
861 v9stat
->n_uid
= stbuf
->st_uid
;
862 v9stat
->n_gid
= stbuf
->st_gid
;
865 v9fs_string_free(&v9stat
->extension
);
867 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
868 err
= v9fs_co_readlink(pdu
, path
, &v9stat
->extension
);
872 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
873 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
874 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
875 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
876 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
877 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
878 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
881 v9fs_string_sprintf(&v9stat
->name
, "%s", basename
);
884 v9fs_string_size(&v9stat
->name
) +
885 v9fs_string_size(&v9stat
->uid
) +
886 v9fs_string_size(&v9stat
->gid
) +
887 v9fs_string_size(&v9stat
->muid
) +
888 v9fs_string_size(&v9stat
->extension
);
892 #define P9_STATS_MODE 0x00000001ULL
893 #define P9_STATS_NLINK 0x00000002ULL
894 #define P9_STATS_UID 0x00000004ULL
895 #define P9_STATS_GID 0x00000008ULL
896 #define P9_STATS_RDEV 0x00000010ULL
897 #define P9_STATS_ATIME 0x00000020ULL
898 #define P9_STATS_MTIME 0x00000040ULL
899 #define P9_STATS_CTIME 0x00000080ULL
900 #define P9_STATS_INO 0x00000100ULL
901 #define P9_STATS_SIZE 0x00000200ULL
902 #define P9_STATS_BLOCKS 0x00000400ULL
904 #define P9_STATS_BTIME 0x00000800ULL
905 #define P9_STATS_GEN 0x00001000ULL
906 #define P9_STATS_DATA_VERSION 0x00002000ULL
908 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
909 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
912 static int stat_to_v9stat_dotl(V9fsPDU
*pdu
, const struct stat
*stbuf
,
913 V9fsStatDotl
*v9lstat
)
915 memset(v9lstat
, 0, sizeof(*v9lstat
));
917 v9lstat
->st_mode
= stbuf
->st_mode
;
918 v9lstat
->st_nlink
= stbuf
->st_nlink
;
919 v9lstat
->st_uid
= stbuf
->st_uid
;
920 v9lstat
->st_gid
= stbuf
->st_gid
;
921 v9lstat
->st_rdev
= stbuf
->st_rdev
;
922 v9lstat
->st_size
= stbuf
->st_size
;
923 v9lstat
->st_blksize
= stbuf
->st_blksize
;
924 v9lstat
->st_blocks
= stbuf
->st_blocks
;
925 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
926 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
927 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
928 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
929 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
930 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
931 /* Currently we only support BASIC fields in stat */
932 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
934 return stat_to_qid(pdu
, stbuf
, &v9lstat
->qid
);
937 static void print_sg(struct iovec
*sg
, int cnt
)
941 printf("sg[%d]: {", cnt
);
942 for (i
= 0; i
< cnt
; i
++) {
946 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
951 /* Will call this only for path name based fid */
952 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
955 v9fs_path_init(&str
);
956 v9fs_path_copy(&str
, dst
);
957 v9fs_path_sprintf(dst
, "%s%s", src
->data
, str
.data
+ len
);
958 v9fs_path_free(&str
);
961 static inline bool is_ro_export(FsContext
*ctx
)
963 return ctx
->export_flags
& V9FS_RDONLY
;
966 static void coroutine_fn
v9fs_version(void *opaque
)
969 V9fsPDU
*pdu
= opaque
;
970 V9fsState
*s
= pdu
->s
;
974 v9fs_string_init(&version
);
975 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
979 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
983 if (!strcmp(version
.data
, "9P2000.u")) {
984 s
->proto_version
= V9FS_PROTO_2000U
;
985 } else if (!strcmp(version
.data
, "9P2000.L")) {
986 s
->proto_version
= V9FS_PROTO_2000L
;
988 v9fs_string_sprintf(&version
, "unknown");
991 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
996 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
998 pdu_complete(pdu
, err
);
999 v9fs_string_free(&version
);
1002 static void coroutine_fn
v9fs_attach(void *opaque
)
1004 V9fsPDU
*pdu
= opaque
;
1005 V9fsState
*s
= pdu
->s
;
1006 int32_t fid
, afid
, n_uname
;
1007 V9fsString uname
, aname
;
1012 Error
*local_err
= NULL
;
1014 v9fs_string_init(&uname
);
1015 v9fs_string_init(&aname
);
1016 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
1017 &afid
, &uname
, &aname
, &n_uname
);
1021 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
1023 fidp
= alloc_fid(s
, fid
);
1028 fidp
->uid
= n_uname
;
1029 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
1035 err
= fid_to_qid(pdu
, fidp
, &qid
);
1043 * disable migration if we haven't done already.
1044 * attach could get called multiple times for the same export.
1046 if (!s
->migration_blocker
) {
1047 error_setg(&s
->migration_blocker
,
1048 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1049 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
1050 err
= migrate_add_blocker(s
->migration_blocker
, &local_err
);
1052 error_free(local_err
);
1053 error_free(s
->migration_blocker
);
1054 s
->migration_blocker
= NULL
;
1061 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
1068 memcpy(&s
->root_qid
, &qid
, sizeof(qid
));
1069 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1070 qid
.type
, qid
.version
, qid
.path
);
1074 pdu_complete(pdu
, err
);
1075 v9fs_string_free(&uname
);
1076 v9fs_string_free(&aname
);
1079 static void coroutine_fn
v9fs_stat(void *opaque
)
1087 V9fsPDU
*pdu
= opaque
;
1090 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1094 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1096 fidp
= get_fid(pdu
, fid
);
1101 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1105 basename
= g_path_get_basename(fidp
->path
.data
);
1106 err
= stat_to_v9stat(pdu
, &fidp
->path
, basename
, &stbuf
, &v9stat
);
1111 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1113 v9fs_stat_free(&v9stat
);
1116 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1117 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1119 v9fs_stat_free(&v9stat
);
1123 pdu_complete(pdu
, err
);
1126 static void coroutine_fn
v9fs_getattr(void *opaque
)
1133 uint64_t request_mask
;
1134 V9fsStatDotl v9stat_dotl
;
1135 V9fsPDU
*pdu
= opaque
;
1137 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1141 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1143 fidp
= get_fid(pdu
, fid
);
1149 * Currently we only support BASIC fields in stat, so there is no
1150 * need to look at request_mask.
1152 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1156 retval
= stat_to_v9stat_dotl(pdu
, &stbuf
, &v9stat_dotl
);
1161 /* fill st_gen if requested and supported by underlying fs */
1162 if (request_mask
& P9_STATS_GEN
) {
1163 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1166 /* we have valid st_gen: update result mask */
1167 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1170 /* request cancelled, e.g. by Tflush */
1173 /* failed to get st_gen: not fatal, ignore */
1177 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1182 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1183 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1184 v9stat_dotl
.st_gid
);
1188 pdu_complete(pdu
, retval
);
1191 /* Attribute flags */
1192 #define P9_ATTR_MODE (1 << 0)
1193 #define P9_ATTR_UID (1 << 1)
1194 #define P9_ATTR_GID (1 << 2)
1195 #define P9_ATTR_SIZE (1 << 3)
1196 #define P9_ATTR_ATIME (1 << 4)
1197 #define P9_ATTR_MTIME (1 << 5)
1198 #define P9_ATTR_CTIME (1 << 6)
1199 #define P9_ATTR_ATIME_SET (1 << 7)
1200 #define P9_ATTR_MTIME_SET (1 << 8)
1202 #define P9_ATTR_MASK 127
1204 static void coroutine_fn
v9fs_setattr(void *opaque
)
1211 V9fsPDU
*pdu
= opaque
;
1213 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1218 trace_v9fs_setattr(pdu
->tag
, pdu
->id
, fid
,
1219 v9iattr
.valid
, v9iattr
.mode
, v9iattr
.uid
, v9iattr
.gid
,
1220 v9iattr
.size
, v9iattr
.atime_sec
, v9iattr
.mtime_sec
);
1222 fidp
= get_fid(pdu
, fid
);
1227 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1228 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1233 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1234 struct timespec times
[2];
1235 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1236 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1237 times
[0].tv_sec
= v9iattr
.atime_sec
;
1238 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1240 times
[0].tv_nsec
= UTIME_NOW
;
1243 times
[0].tv_nsec
= UTIME_OMIT
;
1245 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1246 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1247 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1248 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1250 times
[1].tv_nsec
= UTIME_NOW
;
1253 times
[1].tv_nsec
= UTIME_OMIT
;
1255 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1261 * If the only valid entry in iattr is ctime we can call
1262 * chown(-1,-1) to update the ctime of the file
1264 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1265 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1266 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1267 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1270 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1273 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1279 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1280 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1286 trace_v9fs_setattr_return(pdu
->tag
, pdu
->id
);
1290 pdu_complete(pdu
, err
);
1293 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1299 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1304 for (i
= 0; i
< nwnames
; i
++) {
1305 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1314 static bool name_is_illegal(const char *name
)
1316 return !*name
|| strchr(name
, '/') != NULL
;
1319 static bool not_same_qid(const V9fsQID
*qid1
, const V9fsQID
*qid2
)
1322 qid1
->type
!= qid2
->type
||
1323 qid1
->version
!= qid2
->version
||
1324 qid1
->path
!= qid2
->path
;
1327 static void coroutine_fn
v9fs_walk(void *opaque
)
1330 V9fsQID
*qids
= NULL
;
1332 V9fsPath dpath
, path
;
1336 int32_t fid
, newfid
;
1337 V9fsString
*wnames
= NULL
;
1339 V9fsFidState
*newfidp
= NULL
;
1340 V9fsPDU
*pdu
= opaque
;
1341 V9fsState
*s
= pdu
->s
;
1344 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1346 pdu_complete(pdu
, err
);
1351 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1353 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1354 wnames
= g_new0(V9fsString
, nwnames
);
1355 qids
= g_new0(V9fsQID
, nwnames
);
1356 for (i
= 0; i
< nwnames
; i
++) {
1357 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1361 if (name_is_illegal(wnames
[i
].data
)) {
1367 } else if (nwnames
> P9_MAXWELEM
) {
1371 fidp
= get_fid(pdu
, fid
);
1377 v9fs_path_init(&dpath
);
1378 v9fs_path_init(&path
);
1380 err
= fid_to_qid(pdu
, fidp
, &qid
);
1386 * Both dpath and path initially poin to fidp.
1387 * Needed to handle request with nwnames == 0
1389 v9fs_path_copy(&dpath
, &fidp
->path
);
1390 v9fs_path_copy(&path
, &fidp
->path
);
1391 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1392 if (not_same_qid(&pdu
->s
->root_qid
, &qid
) ||
1393 strcmp("..", wnames
[name_idx
].data
)) {
1394 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
,
1400 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1404 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1408 v9fs_path_copy(&dpath
, &path
);
1410 memcpy(&qids
[name_idx
], &qid
, sizeof(qid
));
1412 if (fid
== newfid
) {
1413 if (fidp
->fid_type
!= P9_FID_NONE
) {
1417 v9fs_path_write_lock(s
);
1418 v9fs_path_copy(&fidp
->path
, &path
);
1419 v9fs_path_unlock(s
);
1421 newfidp
= alloc_fid(s
, newfid
);
1422 if (newfidp
== NULL
) {
1426 newfidp
->uid
= fidp
->uid
;
1427 v9fs_path_copy(&newfidp
->path
, &path
);
1429 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1430 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1434 put_fid(pdu
, newfidp
);
1436 v9fs_path_free(&dpath
);
1437 v9fs_path_free(&path
);
1439 pdu_complete(pdu
, err
);
1440 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1441 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1442 v9fs_string_free(&wnames
[name_idx
]);
1449 static int32_t coroutine_fn
get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1451 struct statfs stbuf
;
1453 V9fsState
*s
= pdu
->s
;
1456 * iounit should be multiples of f_bsize (host filesystem block size
1457 * and as well as less than (client msize - P9_IOHDRSZ))
1459 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1460 iounit
= stbuf
.f_bsize
;
1461 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1464 iounit
= s
->msize
- P9_IOHDRSZ
;
1469 static void coroutine_fn
v9fs_open(void *opaque
)
1480 V9fsPDU
*pdu
= opaque
;
1481 V9fsState
*s
= pdu
->s
;
1483 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1484 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1487 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1493 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1495 fidp
= get_fid(pdu
, fid
);
1500 if (fidp
->fid_type
!= P9_FID_NONE
) {
1505 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1509 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1513 if (S_ISDIR(stbuf
.st_mode
)) {
1514 err
= v9fs_co_opendir(pdu
, fidp
);
1518 fidp
->fid_type
= P9_FID_DIR
;
1519 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1525 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1526 flags
= get_dotl_openflags(s
, mode
);
1528 flags
= omode_to_uflags(mode
);
1530 if (is_ro_export(&s
->ctx
)) {
1531 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1532 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1537 err
= v9fs_co_open(pdu
, fidp
, flags
);
1541 fidp
->fid_type
= P9_FID_FILE
;
1542 fidp
->open_flags
= flags
;
1543 if (flags
& O_EXCL
) {
1545 * We let the host file system do O_EXCL check
1546 * We should not reclaim such fd
1548 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1550 iounit
= get_iounit(pdu
, &fidp
->path
);
1551 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1557 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1558 qid
.type
, qid
.version
, qid
.path
, iounit
);
1562 pdu_complete(pdu
, err
);
1565 static void coroutine_fn
v9fs_lcreate(void *opaque
)
1567 int32_t dfid
, flags
, mode
;
1576 V9fsPDU
*pdu
= opaque
;
1578 v9fs_string_init(&name
);
1579 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1580 &name
, &flags
, &mode
, &gid
);
1584 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1586 if (name_is_illegal(name
.data
)) {
1591 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
1596 fidp
= get_fid(pdu
, dfid
);
1601 if (fidp
->fid_type
!= P9_FID_NONE
) {
1606 flags
= get_dotl_openflags(pdu
->s
, flags
);
1607 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1608 flags
| O_CREAT
, mode
, &stbuf
);
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 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
1626 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1631 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1632 qid
.type
, qid
.version
, qid
.path
, iounit
);
1636 pdu_complete(pdu
, err
);
1637 v9fs_string_free(&name
);
1640 static void coroutine_fn
v9fs_fsync(void *opaque
)
1647 V9fsPDU
*pdu
= opaque
;
1649 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1653 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1655 fidp
= get_fid(pdu
, fid
);
1660 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1666 pdu_complete(pdu
, err
);
1669 static void coroutine_fn
v9fs_clunk(void *opaque
)
1675 V9fsPDU
*pdu
= opaque
;
1676 V9fsState
*s
= pdu
->s
;
1678 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1682 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1684 fidp
= clunk_fid(s
, fid
);
1690 * Bump the ref so that put_fid will
1694 err
= put_fid(pdu
, fidp
);
1699 pdu_complete(pdu
, err
);
1703 * Create a QEMUIOVector for a sub-region of PDU iovecs
1705 * @qiov: uninitialized QEMUIOVector
1706 * @skip: number of bytes to skip from beginning of PDU
1707 * @size: number of bytes to include
1708 * @is_write: true - write, false - read
1710 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1711 * with qemu_iovec_destroy().
1713 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1714 size_t skip
, size_t size
,
1722 pdu
->s
->transport
->init_out_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
1724 pdu
->s
->transport
->init_in_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
1727 qemu_iovec_init_external(&elem
, iov
, niov
);
1728 qemu_iovec_init(qiov
, niov
);
1729 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
1732 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1733 uint64_t off
, uint32_t max_count
)
1737 uint64_t read_count
;
1738 QEMUIOVector qiov_full
;
1740 if (fidp
->fs
.xattr
.len
< off
) {
1743 read_count
= fidp
->fs
.xattr
.len
- off
;
1745 if (read_count
> max_count
) {
1746 read_count
= max_count
;
1748 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
1754 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, read_count
, false);
1755 err
= v9fs_pack(qiov_full
.iov
, qiov_full
.niov
, 0,
1756 ((char *)fidp
->fs
.xattr
.value
) + off
,
1758 qemu_iovec_destroy(&qiov_full
);
1766 static int coroutine_fn
v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1775 off_t saved_dir_pos
;
1776 struct dirent
*dent
;
1778 /* save the directory position */
1779 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1780 if (saved_dir_pos
< 0) {
1781 return saved_dir_pos
;
1785 v9fs_path_init(&path
);
1787 v9fs_readdir_lock(&fidp
->fs
.dir
);
1789 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1793 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1797 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1801 err
= stat_to_v9stat(pdu
, &path
, dent
->d_name
, &stbuf
, &v9stat
);
1805 if ((count
+ v9stat
.size
+ 2) > max_count
) {
1806 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1808 /* Ran out of buffer. Set dir back to old position and return */
1809 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1810 v9fs_stat_free(&v9stat
);
1811 v9fs_path_free(&path
);
1815 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1816 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1818 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1821 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1822 v9fs_stat_free(&v9stat
);
1823 v9fs_path_free(&path
);
1827 v9fs_stat_free(&v9stat
);
1828 v9fs_path_free(&path
);
1829 saved_dir_pos
= dent
->d_off
;
1832 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1834 v9fs_path_free(&path
);
1841 static void coroutine_fn
v9fs_read(void *opaque
)
1850 V9fsPDU
*pdu
= opaque
;
1851 V9fsState
*s
= pdu
->s
;
1853 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1857 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1859 fidp
= get_fid(pdu
, fid
);
1864 if (fidp
->fid_type
== P9_FID_DIR
) {
1867 v9fs_co_rewinddir(pdu
, fidp
);
1869 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1874 err
= pdu_marshal(pdu
, offset
, "d", count
);
1878 err
+= offset
+ count
;
1879 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1880 QEMUIOVector qiov_full
;
1884 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
1885 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1887 qemu_iovec_reset(&qiov
);
1888 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
1890 print_sg(qiov
.iov
, qiov
.niov
);
1892 /* Loop in case of EINTR */
1894 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1899 } while (len
== -EINTR
&& !pdu
->cancelled
);
1901 /* IO error return the error */
1903 goto out_free_iovec
;
1905 } while (count
< max_count
&& len
> 0);
1906 err
= pdu_marshal(pdu
, offset
, "d", count
);
1908 goto out_free_iovec
;
1910 err
+= offset
+ count
;
1912 qemu_iovec_destroy(&qiov
);
1913 qemu_iovec_destroy(&qiov_full
);
1914 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1915 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1919 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1923 pdu_complete(pdu
, err
);
1926 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1929 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1930 * size of type (1) + size of name.size (2) + strlen(name.data)
1932 return 24 + v9fs_string_size(name
);
1935 static int coroutine_fn
v9fs_do_readdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1943 off_t saved_dir_pos
;
1944 struct dirent
*dent
;
1946 /* save the directory position */
1947 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1948 if (saved_dir_pos
< 0) {
1949 return saved_dir_pos
;
1953 v9fs_readdir_lock(&fidp
->fs
.dir
);
1955 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1959 v9fs_string_init(&name
);
1960 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1961 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1962 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1964 /* Ran out of buffer. Set dir back to old position and return */
1965 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1966 v9fs_string_free(&name
);
1970 * Fill up just the path field of qid because the client uses
1971 * only that. To fill the entire qid structure we will have
1972 * to stat each dirent found, which is expensive
1974 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1975 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1976 /* Fill the other fields with dummy values */
1980 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1981 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1983 dent
->d_type
, &name
);
1985 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1988 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1989 v9fs_string_free(&name
);
1993 v9fs_string_free(&name
);
1994 saved_dir_pos
= dent
->d_off
;
1997 v9fs_readdir_unlock(&fidp
->fs
.dir
);
2005 static void coroutine_fn
v9fs_readdir(void *opaque
)
2011 uint64_t initial_offset
;
2014 V9fsPDU
*pdu
= opaque
;
2016 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
2017 &initial_offset
, &max_count
);
2021 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
2023 fidp
= get_fid(pdu
, fid
);
2028 if (!fidp
->fs
.dir
.stream
) {
2032 if (initial_offset
== 0) {
2033 v9fs_co_rewinddir(pdu
, fidp
);
2035 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
2037 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
2042 retval
= pdu_marshal(pdu
, offset
, "d", count
);
2046 retval
+= count
+ offset
;
2047 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
2051 pdu_complete(pdu
, retval
);
2054 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2055 uint64_t off
, uint32_t count
,
2056 struct iovec
*sg
, int cnt
)
2060 uint64_t write_count
;
2064 if (fidp
->fs
.xattr
.len
< off
) {
2068 write_count
= fidp
->fs
.xattr
.len
- off
;
2069 if (write_count
> count
) {
2070 write_count
= count
;
2072 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
2077 fidp
->fs
.xattr
.copied_len
+= write_count
;
2079 * Now copy the content from sg list
2081 for (i
= 0; i
< cnt
; i
++) {
2082 if (write_count
> sg
[i
].iov_len
) {
2083 to_copy
= sg
[i
].iov_len
;
2085 to_copy
= write_count
;
2087 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
2088 /* updating vs->off since we are not using below */
2090 write_count
-= to_copy
;
2096 static void coroutine_fn
v9fs_write(void *opaque
)
2106 V9fsPDU
*pdu
= opaque
;
2107 V9fsState
*s
= pdu
->s
;
2108 QEMUIOVector qiov_full
;
2111 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
2113 pdu_complete(pdu
, err
);
2117 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
2118 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
2120 fidp
= get_fid(pdu
, fid
);
2125 if (fidp
->fid_type
== P9_FID_FILE
) {
2126 if (fidp
->fs
.fd
== -1) {
2130 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2132 * setxattr operation
2134 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
2135 qiov_full
.iov
, qiov_full
.niov
);
2141 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2143 qemu_iovec_reset(&qiov
);
2144 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2146 print_sg(qiov
.iov
, qiov
.niov
);
2148 /* Loop in case of EINTR */
2150 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2155 } while (len
== -EINTR
&& !pdu
->cancelled
);
2157 /* IO error return the error */
2161 } while (total
< count
&& len
> 0);
2164 err
= pdu_marshal(pdu
, offset
, "d", total
);
2169 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2171 qemu_iovec_destroy(&qiov
);
2175 qemu_iovec_destroy(&qiov_full
);
2176 pdu_complete(pdu
, err
);
2179 static void coroutine_fn
v9fs_create(void *opaque
)
2191 V9fsString extension
;
2193 V9fsPDU
*pdu
= opaque
;
2194 V9fsState
*s
= pdu
->s
;
2196 v9fs_path_init(&path
);
2197 v9fs_string_init(&name
);
2198 v9fs_string_init(&extension
);
2199 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2200 &perm
, &mode
, &extension
);
2204 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2206 if (name_is_illegal(name
.data
)) {
2211 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2216 fidp
= get_fid(pdu
, fid
);
2221 if (fidp
->fid_type
!= P9_FID_NONE
) {
2225 if (perm
& P9_STAT_MODE_DIR
) {
2226 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2227 fidp
->uid
, -1, &stbuf
);
2231 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2235 v9fs_path_write_lock(s
);
2236 v9fs_path_copy(&fidp
->path
, &path
);
2237 v9fs_path_unlock(s
);
2238 err
= v9fs_co_opendir(pdu
, fidp
);
2242 fidp
->fid_type
= P9_FID_DIR
;
2243 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2244 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2245 extension
.data
, -1 , &stbuf
);
2249 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2253 v9fs_path_write_lock(s
);
2254 v9fs_path_copy(&fidp
->path
, &path
);
2255 v9fs_path_unlock(s
);
2256 } else if (perm
& P9_STAT_MODE_LINK
) {
2257 int32_t ofid
= atoi(extension
.data
);
2258 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2259 if (ofidp
== NULL
) {
2263 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2264 put_fid(pdu
, ofidp
);
2268 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2270 fidp
->fid_type
= P9_FID_NONE
;
2273 v9fs_path_write_lock(s
);
2274 v9fs_path_copy(&fidp
->path
, &path
);
2275 v9fs_path_unlock(s
);
2276 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2278 fidp
->fid_type
= P9_FID_NONE
;
2281 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2283 uint32_t major
, minor
;
2286 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2303 nmode
|= perm
& 0777;
2304 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2305 makedev(major
, minor
), nmode
, &stbuf
);
2309 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2313 v9fs_path_write_lock(s
);
2314 v9fs_path_copy(&fidp
->path
, &path
);
2315 v9fs_path_unlock(s
);
2316 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2317 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2318 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2322 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2326 v9fs_path_write_lock(s
);
2327 v9fs_path_copy(&fidp
->path
, &path
);
2328 v9fs_path_unlock(s
);
2329 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2330 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2331 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2335 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2339 v9fs_path_write_lock(s
);
2340 v9fs_path_copy(&fidp
->path
, &path
);
2341 v9fs_path_unlock(s
);
2343 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2344 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2348 fidp
->fid_type
= P9_FID_FILE
;
2349 fidp
->open_flags
= omode_to_uflags(mode
);
2350 if (fidp
->open_flags
& O_EXCL
) {
2352 * We let the host file system do O_EXCL check
2353 * We should not reclaim such fd
2355 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2358 iounit
= get_iounit(pdu
, &fidp
->path
);
2359 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2363 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2368 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2369 qid
.type
, qid
.version
, qid
.path
, iounit
);
2373 pdu_complete(pdu
, err
);
2374 v9fs_string_free(&name
);
2375 v9fs_string_free(&extension
);
2376 v9fs_path_free(&path
);
2379 static void coroutine_fn
v9fs_symlink(void *opaque
)
2381 V9fsPDU
*pdu
= opaque
;
2384 V9fsFidState
*dfidp
;
2392 v9fs_string_init(&name
);
2393 v9fs_string_init(&symname
);
2394 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2398 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2400 if (name_is_illegal(name
.data
)) {
2405 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2410 dfidp
= get_fid(pdu
, dfid
);
2411 if (dfidp
== NULL
) {
2415 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2419 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
2423 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2428 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2429 qid
.type
, qid
.version
, qid
.path
);
2431 put_fid(pdu
, dfidp
);
2433 pdu_complete(pdu
, err
);
2434 v9fs_string_free(&name
);
2435 v9fs_string_free(&symname
);
2438 static void coroutine_fn
v9fs_flush(void *opaque
)
2443 V9fsPDU
*cancel_pdu
= NULL
;
2444 V9fsPDU
*pdu
= opaque
;
2445 V9fsState
*s
= pdu
->s
;
2447 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2449 pdu_complete(pdu
, err
);
2452 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2454 if (pdu
->tag
== tag
) {
2455 warn_report("the guest sent a self-referencing 9P flush request");
2457 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2458 if (cancel_pdu
->tag
== tag
) {
2464 cancel_pdu
->cancelled
= 1;
2466 * Wait for pdu to complete.
2468 qemu_co_queue_wait(&cancel_pdu
->complete
, NULL
);
2469 if (!qemu_co_queue_next(&cancel_pdu
->complete
)) {
2470 cancel_pdu
->cancelled
= 0;
2471 pdu_free(cancel_pdu
);
2474 pdu_complete(pdu
, 7);
2477 static void coroutine_fn
v9fs_link(void *opaque
)
2479 V9fsPDU
*pdu
= opaque
;
2480 int32_t dfid
, oldfid
;
2481 V9fsFidState
*dfidp
, *oldfidp
;
2486 v9fs_string_init(&name
);
2487 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2491 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2493 if (name_is_illegal(name
.data
)) {
2498 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2503 dfidp
= get_fid(pdu
, dfid
);
2504 if (dfidp
== NULL
) {
2509 oldfidp
= get_fid(pdu
, oldfid
);
2510 if (oldfidp
== NULL
) {
2514 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2518 put_fid(pdu
, oldfidp
);
2520 put_fid(pdu
, dfidp
);
2522 v9fs_string_free(&name
);
2523 pdu_complete(pdu
, err
);
2526 /* Only works with path name based fid */
2527 static void coroutine_fn
v9fs_remove(void *opaque
)
2533 V9fsPDU
*pdu
= opaque
;
2535 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2539 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2541 fidp
= get_fid(pdu
, fid
);
2546 /* if fs driver is not path based, return EOPNOTSUPP */
2547 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2552 * IF the file is unlinked, we cannot reopen
2553 * the file later. So don't reclaim fd
2555 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2559 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2564 /* For TREMOVE we need to clunk the fid even on failed remove */
2565 clunk_fid(pdu
->s
, fidp
->fid
);
2568 pdu_complete(pdu
, err
);
2571 static void coroutine_fn
v9fs_unlinkat(void *opaque
)
2575 int32_t dfid
, flags
, rflags
= 0;
2578 V9fsFidState
*dfidp
;
2579 V9fsPDU
*pdu
= opaque
;
2581 v9fs_string_init(&name
);
2582 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2587 if (name_is_illegal(name
.data
)) {
2592 if (!strcmp(".", name
.data
)) {
2597 if (!strcmp("..", name
.data
)) {
2602 if (flags
& ~P9_DOTL_AT_REMOVEDIR
) {
2607 if (flags
& P9_DOTL_AT_REMOVEDIR
) {
2608 rflags
|= AT_REMOVEDIR
;
2611 dfidp
= get_fid(pdu
, dfid
);
2612 if (dfidp
== NULL
) {
2617 * IF the file is unlinked, we cannot reopen
2618 * the file later. So don't reclaim fd
2620 v9fs_path_init(&path
);
2621 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2625 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2629 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, rflags
);
2634 put_fid(pdu
, dfidp
);
2635 v9fs_path_free(&path
);
2637 pdu_complete(pdu
, err
);
2638 v9fs_string_free(&name
);
2642 /* Only works with path name based fid */
2643 static int coroutine_fn
v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2649 V9fsFidState
*tfidp
;
2650 V9fsState
*s
= pdu
->s
;
2651 V9fsFidState
*dirfidp
= NULL
;
2653 v9fs_path_init(&new_path
);
2654 if (newdirfid
!= -1) {
2655 dirfidp
= get_fid(pdu
, newdirfid
);
2656 if (dirfidp
== NULL
) {
2660 if (fidp
->fid_type
!= P9_FID_NONE
) {
2664 err
= v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2669 char *dir_name
= g_path_get_dirname(fidp
->path
.data
);
2672 v9fs_path_init(&dir_path
);
2673 v9fs_path_sprintf(&dir_path
, "%s", dir_name
);
2676 err
= v9fs_co_name_to_path(pdu
, &dir_path
, name
->data
, &new_path
);
2677 v9fs_path_free(&dir_path
);
2682 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2687 * Fixup fid's pointing to the old name to
2688 * start pointing to the new name
2690 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2691 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2692 /* replace the name */
2693 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2698 put_fid(pdu
, dirfidp
);
2700 v9fs_path_free(&new_path
);
2705 /* Only works with path name based fid */
2706 static void coroutine_fn
v9fs_rename(void *opaque
)
2714 V9fsPDU
*pdu
= opaque
;
2715 V9fsState
*s
= pdu
->s
;
2717 v9fs_string_init(&name
);
2718 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2723 if (name_is_illegal(name
.data
)) {
2728 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2733 fidp
= get_fid(pdu
, fid
);
2738 if (fidp
->fid_type
!= P9_FID_NONE
) {
2742 /* if fs driver is not path based, return EOPNOTSUPP */
2743 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2747 v9fs_path_write_lock(s
);
2748 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2749 v9fs_path_unlock(s
);
2756 pdu_complete(pdu
, err
);
2757 v9fs_string_free(&name
);
2760 static int coroutine_fn
v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2761 V9fsString
*old_name
,
2763 V9fsString
*new_name
)
2765 V9fsFidState
*tfidp
;
2766 V9fsPath oldpath
, newpath
;
2767 V9fsState
*s
= pdu
->s
;
2770 v9fs_path_init(&oldpath
);
2771 v9fs_path_init(&newpath
);
2772 err
= v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2776 err
= v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2782 * Fixup fid's pointing to the old name to
2783 * start pointing to the new name
2785 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2786 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2787 /* replace the name */
2788 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2792 v9fs_path_free(&oldpath
);
2793 v9fs_path_free(&newpath
);
2797 static int coroutine_fn
v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2798 V9fsString
*old_name
,
2800 V9fsString
*new_name
)
2803 V9fsState
*s
= pdu
->s
;
2804 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2806 olddirfidp
= get_fid(pdu
, olddirfid
);
2807 if (olddirfidp
== NULL
) {
2811 if (newdirfid
!= -1) {
2812 newdirfidp
= get_fid(pdu
, newdirfid
);
2813 if (newdirfidp
== NULL
) {
2818 newdirfidp
= get_fid(pdu
, olddirfid
);
2821 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2822 &newdirfidp
->path
, new_name
);
2826 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2827 /* Only for path based fid we need to do the below fixup */
2828 err
= v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2829 &newdirfidp
->path
, new_name
);
2833 put_fid(pdu
, olddirfidp
);
2836 put_fid(pdu
, newdirfidp
);
2841 static void coroutine_fn
v9fs_renameat(void *opaque
)
2845 V9fsPDU
*pdu
= opaque
;
2846 V9fsState
*s
= pdu
->s
;
2847 int32_t olddirfid
, newdirfid
;
2848 V9fsString old_name
, new_name
;
2850 v9fs_string_init(&old_name
);
2851 v9fs_string_init(&new_name
);
2852 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2853 &old_name
, &newdirfid
, &new_name
);
2858 if (name_is_illegal(old_name
.data
) || name_is_illegal(new_name
.data
)) {
2863 if (!strcmp(".", old_name
.data
) || !strcmp("..", old_name
.data
) ||
2864 !strcmp(".", new_name
.data
) || !strcmp("..", new_name
.data
)) {
2869 v9fs_path_write_lock(s
);
2870 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2871 &old_name
, newdirfid
, &new_name
);
2872 v9fs_path_unlock(s
);
2878 pdu_complete(pdu
, err
);
2879 v9fs_string_free(&old_name
);
2880 v9fs_string_free(&new_name
);
2883 static void coroutine_fn
v9fs_wstat(void *opaque
)
2892 V9fsPDU
*pdu
= opaque
;
2893 V9fsState
*s
= pdu
->s
;
2895 v9fs_stat_init(&v9stat
);
2896 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2900 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2901 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2903 fidp
= get_fid(pdu
, fid
);
2908 /* do we need to sync the file? */
2909 if (donttouch_stat(&v9stat
)) {
2910 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2913 if (v9stat
.mode
!= -1) {
2915 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2919 v9_mode
= stat_to_v9mode(&stbuf
);
2920 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2921 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2922 /* Attempting to change the type */
2926 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2927 v9mode_to_mode(v9stat
.mode
,
2928 &v9stat
.extension
));
2933 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2934 struct timespec times
[2];
2935 if (v9stat
.atime
!= -1) {
2936 times
[0].tv_sec
= v9stat
.atime
;
2937 times
[0].tv_nsec
= 0;
2939 times
[0].tv_nsec
= UTIME_OMIT
;
2941 if (v9stat
.mtime
!= -1) {
2942 times
[1].tv_sec
= v9stat
.mtime
;
2943 times
[1].tv_nsec
= 0;
2945 times
[1].tv_nsec
= UTIME_OMIT
;
2947 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2952 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2953 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2958 if (v9stat
.name
.size
!= 0) {
2959 v9fs_path_write_lock(s
);
2960 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2961 v9fs_path_unlock(s
);
2966 if (v9stat
.length
!= -1) {
2967 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2976 v9fs_stat_free(&v9stat
);
2977 pdu_complete(pdu
, err
);
2980 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2992 int32_t bsize_factor
;
2995 * compute bsize factor based on host file system block size
2998 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2999 if (!bsize_factor
) {
3002 f_type
= stbuf
->f_type
;
3003 f_bsize
= stbuf
->f_bsize
;
3004 f_bsize
*= bsize_factor
;
3006 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3007 * adjust(divide) the number of blocks, free blocks and available
3008 * blocks by bsize factor
3010 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
3011 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
3012 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
3013 f_files
= stbuf
->f_files
;
3014 f_ffree
= stbuf
->f_ffree
;
3015 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
3016 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
3017 f_namelen
= stbuf
->f_namelen
;
3019 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
3020 f_type
, f_bsize
, f_blocks
, f_bfree
,
3021 f_bavail
, f_files
, f_ffree
,
3022 fsid_val
, f_namelen
);
3025 static void coroutine_fn
v9fs_statfs(void *opaque
)
3031 struct statfs stbuf
;
3032 V9fsPDU
*pdu
= opaque
;
3033 V9fsState
*s
= pdu
->s
;
3035 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3039 fidp
= get_fid(pdu
, fid
);
3044 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
3048 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
3056 pdu_complete(pdu
, retval
);
3059 static void coroutine_fn
v9fs_mknod(void *opaque
)
3072 V9fsPDU
*pdu
= opaque
;
3074 v9fs_string_init(&name
);
3075 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
3076 &major
, &minor
, &gid
);
3080 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
3082 if (name_is_illegal(name
.data
)) {
3087 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3092 fidp
= get_fid(pdu
, fid
);
3097 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
3098 makedev(major
, minor
), mode
, &stbuf
);
3102 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
3106 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3111 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
3112 qid
.type
, qid
.version
, qid
.path
);
3116 pdu_complete(pdu
, err
);
3117 v9fs_string_free(&name
);
3121 * Implement posix byte range locking code
3122 * Server side handling of locking code is very simple, because 9p server in
3123 * QEMU can handle only one client. And most of the lock handling
3124 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3125 * do any thing in * qemu 9p server side lock code path.
3126 * So when a TLOCK request comes, always return success
3128 static void coroutine_fn
v9fs_lock(void *opaque
)
3134 int32_t fid
, err
= 0;
3135 V9fsPDU
*pdu
= opaque
;
3137 v9fs_string_init(&flock
.client_id
);
3138 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
3139 &flock
.flags
, &flock
.start
, &flock
.length
,
3140 &flock
.proc_id
, &flock
.client_id
);
3144 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
3145 flock
.type
, flock
.start
, flock
.length
);
3148 /* We support only block flag now (that too ignored currently) */
3149 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
3153 fidp
= get_fid(pdu
, fid
);
3158 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3162 err
= pdu_marshal(pdu
, offset
, "b", P9_LOCK_SUCCESS
);
3167 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, P9_LOCK_SUCCESS
);
3171 pdu_complete(pdu
, err
);
3172 v9fs_string_free(&flock
.client_id
);
3176 * When a TGETLOCK request comes, always return success because all lock
3177 * handling is done by client's VFS layer.
3179 static void coroutine_fn
v9fs_getlock(void *opaque
)
3185 int32_t fid
, err
= 0;
3186 V9fsPDU
*pdu
= opaque
;
3188 v9fs_string_init(&glock
.client_id
);
3189 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
3190 &glock
.start
, &glock
.length
, &glock
.proc_id
,
3195 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
3196 glock
.type
, glock
.start
, glock
.length
);
3198 fidp
= get_fid(pdu
, fid
);
3203 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3207 glock
.type
= P9_LOCK_TYPE_UNLCK
;
3208 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
3209 glock
.start
, glock
.length
, glock
.proc_id
,
3215 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
3216 glock
.length
, glock
.proc_id
);
3220 pdu_complete(pdu
, err
);
3221 v9fs_string_free(&glock
.client_id
);
3224 static void coroutine_fn
v9fs_mkdir(void *opaque
)
3226 V9fsPDU
*pdu
= opaque
;
3237 v9fs_string_init(&name
);
3238 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
3242 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
3244 if (name_is_illegal(name
.data
)) {
3249 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3254 fidp
= get_fid(pdu
, fid
);
3259 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
3263 err
= stat_to_qid(pdu
, &stbuf
, &qid
);
3267 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3272 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
3273 qid
.type
, qid
.version
, qid
.path
, err
);
3277 pdu_complete(pdu
, err
);
3278 v9fs_string_free(&name
);
3281 static void coroutine_fn
v9fs_xattrwalk(void *opaque
)
3287 int32_t fid
, newfid
;
3288 V9fsFidState
*file_fidp
;
3289 V9fsFidState
*xattr_fidp
= NULL
;
3290 V9fsPDU
*pdu
= opaque
;
3291 V9fsState
*s
= pdu
->s
;
3293 v9fs_string_init(&name
);
3294 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3298 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3300 file_fidp
= get_fid(pdu
, fid
);
3301 if (file_fidp
== NULL
) {
3305 xattr_fidp
= alloc_fid(s
, newfid
);
3306 if (xattr_fidp
== NULL
) {
3310 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3311 if (!v9fs_string_size(&name
)) {
3313 * listxattr request. Get the size first
3315 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3318 clunk_fid(s
, xattr_fidp
->fid
);
3322 * Read the xattr value
3324 xattr_fidp
->fs
.xattr
.len
= size
;
3325 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3326 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3327 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3329 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3330 xattr_fidp
->fs
.xattr
.value
,
3331 xattr_fidp
->fs
.xattr
.len
);
3333 clunk_fid(s
, xattr_fidp
->fid
);
3337 err
= pdu_marshal(pdu
, offset
, "q", size
);
3344 * specific xattr fid. We check for xattr
3345 * presence also collect the xattr size
3347 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3351 clunk_fid(s
, xattr_fidp
->fid
);
3355 * Read the xattr value
3357 xattr_fidp
->fs
.xattr
.len
= size
;
3358 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3359 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3360 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3362 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3363 &name
, xattr_fidp
->fs
.xattr
.value
,
3364 xattr_fidp
->fs
.xattr
.len
);
3366 clunk_fid(s
, xattr_fidp
->fid
);
3370 err
= pdu_marshal(pdu
, offset
, "q", size
);
3376 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3378 put_fid(pdu
, file_fidp
);
3380 put_fid(pdu
, xattr_fidp
);
3383 pdu_complete(pdu
, err
);
3384 v9fs_string_free(&name
);
3387 static void coroutine_fn
v9fs_xattrcreate(void *opaque
)
3389 int flags
, rflags
= 0;
3395 V9fsFidState
*file_fidp
;
3396 V9fsFidState
*xattr_fidp
;
3397 V9fsPDU
*pdu
= opaque
;
3399 v9fs_string_init(&name
);
3400 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3404 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3406 if (flags
& ~(P9_XATTR_CREATE
| P9_XATTR_REPLACE
)) {
3411 if (flags
& P9_XATTR_CREATE
) {
3412 rflags
|= XATTR_CREATE
;
3415 if (flags
& P9_XATTR_REPLACE
) {
3416 rflags
|= XATTR_REPLACE
;
3419 if (size
> XATTR_SIZE_MAX
) {
3424 file_fidp
= get_fid(pdu
, fid
);
3425 if (file_fidp
== NULL
) {
3429 if (file_fidp
->fid_type
!= P9_FID_NONE
) {
3434 /* Make the file fid point to xattr */
3435 xattr_fidp
= file_fidp
;
3436 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3437 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3438 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= false;
3439 xattr_fidp
->fs
.xattr
.len
= size
;
3440 xattr_fidp
->fs
.xattr
.flags
= rflags
;
3441 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3442 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3443 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3446 put_fid(pdu
, file_fidp
);
3448 pdu_complete(pdu
, err
);
3449 v9fs_string_free(&name
);
3452 static void coroutine_fn
v9fs_readlink(void *opaque
)
3454 V9fsPDU
*pdu
= opaque
;
3461 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3465 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3466 fidp
= get_fid(pdu
, fid
);
3472 v9fs_string_init(&target
);
3473 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3477 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3479 v9fs_string_free(&target
);
3483 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3484 v9fs_string_free(&target
);
3488 pdu_complete(pdu
, err
);
3491 static CoroutineEntry
*pdu_co_handlers
[] = {
3492 [P9_TREADDIR
] = v9fs_readdir
,
3493 [P9_TSTATFS
] = v9fs_statfs
,
3494 [P9_TGETATTR
] = v9fs_getattr
,
3495 [P9_TSETATTR
] = v9fs_setattr
,
3496 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3497 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3498 [P9_TMKNOD
] = v9fs_mknod
,
3499 [P9_TRENAME
] = v9fs_rename
,
3500 [P9_TLOCK
] = v9fs_lock
,
3501 [P9_TGETLOCK
] = v9fs_getlock
,
3502 [P9_TRENAMEAT
] = v9fs_renameat
,
3503 [P9_TREADLINK
] = v9fs_readlink
,
3504 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3505 [P9_TMKDIR
] = v9fs_mkdir
,
3506 [P9_TVERSION
] = v9fs_version
,
3507 [P9_TLOPEN
] = v9fs_open
,
3508 [P9_TATTACH
] = v9fs_attach
,
3509 [P9_TSTAT
] = v9fs_stat
,
3510 [P9_TWALK
] = v9fs_walk
,
3511 [P9_TCLUNK
] = v9fs_clunk
,
3512 [P9_TFSYNC
] = v9fs_fsync
,
3513 [P9_TOPEN
] = v9fs_open
,
3514 [P9_TREAD
] = v9fs_read
,
3516 [P9_TAUTH
] = v9fs_auth
,
3518 [P9_TFLUSH
] = v9fs_flush
,
3519 [P9_TLINK
] = v9fs_link
,
3520 [P9_TSYMLINK
] = v9fs_symlink
,
3521 [P9_TCREATE
] = v9fs_create
,
3522 [P9_TLCREATE
] = v9fs_lcreate
,
3523 [P9_TWRITE
] = v9fs_write
,
3524 [P9_TWSTAT
] = v9fs_wstat
,
3525 [P9_TREMOVE
] = v9fs_remove
,
3528 static void coroutine_fn
v9fs_op_not_supp(void *opaque
)
3530 V9fsPDU
*pdu
= opaque
;
3531 pdu_complete(pdu
, -EOPNOTSUPP
);
3534 static void coroutine_fn
v9fs_fs_ro(void *opaque
)
3536 V9fsPDU
*pdu
= opaque
;
3537 pdu_complete(pdu
, -EROFS
);
3540 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3567 void pdu_submit(V9fsPDU
*pdu
, P9MsgHeader
*hdr
)
3570 CoroutineEntry
*handler
;
3571 V9fsState
*s
= pdu
->s
;
3573 pdu
->size
= le32_to_cpu(hdr
->size_le
);
3575 pdu
->tag
= le16_to_cpu(hdr
->tag_le
);
3577 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3578 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3579 handler
= v9fs_op_not_supp
;
3580 } else if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3581 handler
= v9fs_fs_ro
;
3583 handler
= pdu_co_handlers
[pdu
->id
];
3586 qemu_co_queue_init(&pdu
->complete
);
3587 co
= qemu_coroutine_create(handler
, pdu
);
3588 qemu_coroutine_enter(co
);
3591 /* Returns 0 on success, 1 on failure. */
3592 int v9fs_device_realize_common(V9fsState
*s
, const V9fsTransport
*t
,
3601 assert(!s
->transport
);
3604 /* initialize pdu allocator */
3605 QLIST_INIT(&s
->free_list
);
3606 QLIST_INIT(&s
->active_list
);
3607 for (i
= 0; i
< MAX_REQ
; i
++) {
3608 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
3613 v9fs_path_init(&path
);
3615 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
3618 /* We don't have a fsdev identified by fsdev_id */
3619 error_setg(errp
, "9pfs device couldn't find fsdev with the "
3621 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
3625 if (!s
->fsconf
.tag
) {
3626 /* we haven't specified a mount_tag */
3627 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
3628 s
->fsconf
.fsdev_id
);
3632 s
->ctx
.export_flags
= fse
->export_flags
;
3633 s
->ctx
.fs_root
= g_strdup(fse
->path
);
3634 s
->ctx
.exops
.get_st_gen
= NULL
;
3635 len
= strlen(s
->fsconf
.tag
);
3636 if (len
> MAX_TAG_LEN
- 1) {
3637 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
3638 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
3642 s
->tag
= g_strdup(s
->fsconf
.tag
);
3647 s
->ctx
.fmode
= fse
->fmode
;
3648 s
->ctx
.dmode
= fse
->dmode
;
3651 qemu_co_rwlock_init(&s
->rename_lock
);
3653 if (s
->ops
->init(&s
->ctx
, errp
) < 0) {
3654 error_prepend(errp
, "cannot initialize fsdev '%s': ",
3655 s
->fsconf
.fsdev_id
);
3660 * Check details of export path, We need to use fs driver
3661 * call back to do that. Since we are in the init path, we don't
3662 * use co-routines here.
3664 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
3666 "error in converting name to path %s", strerror(errno
));
3669 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
3670 error_setg(errp
, "share path %s does not exist", fse
->path
);
3672 } else if (!S_ISDIR(stat
.st_mode
)) {
3673 error_setg(errp
, "share path %s is not a directory", fse
->path
);
3677 s
->dev_id
= stat
.st_dev
;
3679 s
->ctx
.fst
= &fse
->fst
;
3680 fsdev_throttle_init(s
->ctx
.fst
);
3685 v9fs_device_unrealize_common(s
, NULL
);
3687 v9fs_path_free(&path
);
3691 void v9fs_device_unrealize_common(V9fsState
*s
, Error
**errp
)
3693 if (s
->ops
&& s
->ops
->cleanup
) {
3694 s
->ops
->cleanup(&s
->ctx
);
3697 fsdev_throttle_cleanup(s
->ctx
.fst
);
3700 g_free(s
->ctx
.fs_root
);
3703 typedef struct VirtfsCoResetData
{
3706 } VirtfsCoResetData
;
3708 static void coroutine_fn
virtfs_co_reset(void *opaque
)
3710 VirtfsCoResetData
*data
= opaque
;
3712 virtfs_reset(&data
->pdu
);
3716 void v9fs_reset(V9fsState
*s
)
3718 VirtfsCoResetData data
= { .pdu
= { .s
= s
}, .done
= false };
3721 while (!QLIST_EMPTY(&s
->active_list
)) {
3722 aio_poll(qemu_get_aio_context(), true);
3725 co
= qemu_coroutine_create(virtfs_co_reset
, &data
);
3726 qemu_coroutine_enter(co
);
3728 while (!data
.done
) {
3729 aio_poll(qemu_get_aio_context(), true);
3733 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
3736 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3737 error_report("Failed to get the resource limit");
3740 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3741 open_fd_rc
= rlim
.rlim_cur
/2;