2 * Virtio 9p backend for Maru
3 * Based on hw/9pfs/9p.c:
5 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
8 * Sooyoung Ha <yoosah.ha@samsung.com>
9 * SeokYeon Hwang <syeon.hwang@samsung.com>
10 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 #include "qemu/osdep.h"
32 #include <glib/gprintf.h>
33 #include "hw/virtio/virtio.h"
34 #include "qapi/error.h"
35 #include "qemu/error-report.h"
37 #include "qemu/sockets.h"
38 #include "virtio-9p.h"
39 #include "fsdev/qemu-fsdev.h"
43 #include "migration/migration.h"
46 #include "tizen/src/resources_win32.h"
48 /* On windows, there's no mknod function. The device number is meaningless */
49 #define makedev(x,y) 0
53 /* Old MinGW's struct dirent doesn't support d_type member */
54 #define WIN32_D_TYPE 0
56 extern uint64_t hostBytesPerSector;
58 #include <sys/sysmacros.h>
62 #define O_DIRECT 040000 /* Direct disk access */
63 #define O_NOATIME 01000000 /* Do not set atime */
64 #ifndef XATTR_SIZE_MAX
65 #define XATTR_SIZE_MAX 65536
69 #include "../../tizen/src/debug_ch.h"
70 MULTI_DEBUG_CHANNEL(tizen, 9pfs);
74 static int open_fd_rc;
88 ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
94 ret = virtio_pdu_vmarshal(pdu, offset, fmt, ap);
100 ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
106 ret = virtio_pdu_vunmarshal(pdu, offset, fmt, ap);
112 static void pdu_push_and_notify(V9fsPDU *pdu)
114 virtio_9p_push_and_notify(pdu);
117 static int omode_to_uflags(int8_t mode)
140 if (mode & Oappend) {
151 struct dotl_openflag_map {
156 static int dotl_to_open_flags(int flags)
160 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
161 * and P9_DOTL_NOACCESS
163 int oflags = flags & O_ACCMODE;
165 struct dotl_openflag_map dotl_oflag_map[] = {
166 { P9_DOTL_CREATE, O_CREAT },
167 { P9_DOTL_EXCL, O_EXCL },
168 { P9_DOTL_NOCTTY , O_NOCTTY },
169 { P9_DOTL_TRUNC, O_TRUNC },
170 { P9_DOTL_APPEND, O_APPEND },
171 { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
172 { P9_DOTL_DSYNC, O_DSYNC },
173 { P9_DOTL_FASYNC, FASYNC },
174 { P9_DOTL_DIRECT, O_DIRECT },
175 { P9_DOTL_LARGEFILE, O_LARGEFILE },
176 { P9_DOTL_DIRECTORY, O_DIRECTORY },
177 { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
178 { P9_DOTL_NOATIME, O_NOATIME },
179 { P9_DOTL_SYNC, O_SYNC },
182 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
183 if (flags & dotl_oflag_map[i].dotl_flag) {
184 oflags |= dotl_oflag_map[i].open_flag;
191 void cred_init(FsCred *credp)
199 static int get_dotl_openflags(V9fsState *s, int oflags)
203 * Filter the client open flags
205 flags = dotl_to_open_flags(oflags);
206 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
208 * Ignore direct disk access hint until the server supports it.
214 void v9fs_path_init(V9fsPath *path)
220 void v9fs_path_free(V9fsPath *path)
228 void GCC_FMT_ATTR(2, 3)
229 v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
233 v9fs_path_free(path);
236 /* Bump the size for including terminating NULL */
237 path->size = g_vasprintf(&path->data, fmt, ap) + 1;
241 void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs)
244 lhs->data = g_malloc(rhs->size);
245 memcpy(lhs->data, rhs->data, rhs->size);
246 lhs->size = rhs->size;
249 int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
250 const char *name, V9fsPath *path)
252 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
254 err = s->ops->name_to_path(&s->ctx, dirpath, name, path); // local_name_to_path
262 * Return TRUE if s1 is an ancestor of s2.
264 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
265 * As a special case, We treat s1 as ancestor of s2 if they are same!
267 static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
269 if (!strncmp(s1->data, s2->data, s1->size - 1)) {
271 if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
273 if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '\\') {
281 static size_t v9fs_string_size(V9fsString *str)
287 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
288 static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
290 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
292 if (f->fid_type == P9_FID_FILE) {
293 if (f->fs.fd == -1) {
295 err = v9fs_co_open(pdu, f, f->open_flags);
296 } while (err == -EINTR && !pdu->cancelled);
298 } else if (f->fid_type == P9_FID_DIR) {
299 if (f->fs.dir.stream == NULL) {
301 err = v9fs_co_opendir(pdu, f);
302 } while (err == -EINTR && !pdu->cancelled);
308 static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid)
310 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
313 V9fsState *s = pdu->s;
315 for (f = s->fid_list; f; f = f->next) {
319 * Update the fid ref upfront so that
320 * we don't get reclaimed when we yield
325 * check whether we need to reopen the
326 * file. We might have closed the fd
327 * while trying to free up some file
330 err = v9fs_reopen_fid(pdu, f);
332 ERR("[%d][ >> %s]\n", __LINE__, __func__);
337 * Mark the fid as referenced so that the LRU
338 * reclaim won't close the file descriptor
340 f->flags |= FID_REFERENCED;
347 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
351 for (f = s->fid_list; f; f = f->next) {
352 /* If fid is already there return NULL */
355 ERR("[%d][ >> %s]\n", __LINE__, __func__);
359 f = g_malloc0(sizeof(V9fsFidState));
361 f->fid_type = P9_FID_NONE;
364 * Mark the fid as referenced so that the LRU
365 * reclaim won't close the file descriptor
367 f->flags |= FID_REFERENCED;
368 f->next = s->fid_list;
371 v9fs_readdir_init(&f->fs.dir);
372 v9fs_readdir_init(&f->fs_reclaim.dir);
378 static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
380 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
383 if (fidp->fs.xattr.xattrwalk_fid) {
384 /* getxattr/listxattr fid */
388 * if this is fid for setxattr. clunk should
389 * result in setxattr localcall
391 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
392 /* clunk after partial write */
396 if (fidp->fs.xattr.len) {
397 retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
398 fidp->fs.xattr.value,
400 fidp->fs.xattr.flags);
402 retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
405 v9fs_string_free(&fidp->fs.xattr.name);
407 g_free(fidp->fs.xattr.value);
412 static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
414 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
417 if (fidp->fid_type == P9_FID_FILE) {
418 /* If we reclaimed the fd no need to close */
419 if (fidp->fs.fd != -1) {
420 retval = v9fs_co_close(pdu, &fidp->fs);
422 } else if (fidp->fid_type == P9_FID_DIR) {
423 if (fidp->fs.dir.stream != NULL) {
424 retval = v9fs_co_closedir(pdu, &fidp->fs);
426 } else if (fidp->fid_type == P9_FID_XATTR) {
428 retval = v9fs_xattr_fid_clunk(pdu, fidp);
433 v9fs_path_free(&fidp->path);
438 static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
443 * Don't free the fid if it is in reclaim list
445 if (!fidp->ref && fidp->clunked) {
446 if (fidp->fid == pdu->s->root_fid) {
448 * if the clunked fid is root fid then we
449 * have unmounted the fs on the client side.
450 * delete the migration blocker. Ideally, this
451 * should be hooked to transport close notification
453 if (pdu->s->migration_blocker) {
454 migrate_del_blocker(pdu->s->migration_blocker);
455 error_free(pdu->s->migration_blocker);
456 pdu->s->migration_blocker = NULL;
459 return free_fid(pdu, fidp);
464 static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
466 V9fsFidState **fidpp, *fidp;
468 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
469 if ((*fidpp)->fid == fid) {
473 if (*fidpp == NULL) {
482 void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)
484 int reclaim_count = 0;
485 V9fsState *s = pdu->s;
486 V9fsFidState *f, *reclaim_list = NULL;
488 for (f = s->fid_list; f; f = f->next) {
490 * Unlink fids cannot be reclaimed. Check
491 * for them and skip them. Also skip fids
492 * currently being operated on.
494 if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
498 * if it is a recently referenced fid
499 * we leave the fid untouched and clear the
500 * reference bit. We come back to it later
501 * in the next iteration. (a simple LRU without
502 * moving list elements around)
504 if (f->flags & FID_REFERENCED) {
505 f->flags &= ~FID_REFERENCED;
509 * Add fids to reclaim list.
511 if (f->fid_type == P9_FID_FILE) {
512 if (f->fs.fd != -1) {
514 * Up the reference count so that
515 * a clunk request won't free this fid
518 f->rclm_lst = reclaim_list;
520 f->fs_reclaim.fd = f->fs.fd;
524 } else if (f->fid_type == P9_FID_DIR) {
525 if (f->fs.dir.stream != NULL) {
527 * Up the reference count so that
528 * a clunk request won't free this fid
531 f->rclm_lst = reclaim_list;
533 f->fs_reclaim.dir.stream = f->fs.dir.stream;
534 f->fs.dir.stream = NULL;
538 if (reclaim_count >= open_fd_rc) {
543 * Now close the fid in reclaim list. Free them if they
544 * are already clunked.
546 while (reclaim_list) {
548 reclaim_list = f->rclm_lst;
549 if (f->fid_type == P9_FID_FILE) {
550 v9fs_co_close(pdu, &f->fs_reclaim);
551 } else if (f->fid_type == P9_FID_DIR) {
552 v9fs_co_closedir(pdu, &f->fs_reclaim);
556 * Now drop the fid reference, free it
563 static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
565 TRACE("[%d][ >> %s]\n", __LINE__, __func__);
567 V9fsState *s = pdu->s;
568 V9fsFidState *fidp, head_fid;
570 head_fid.next = s->fid_list;
571 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
572 if (fidp->path.size != path->size) {
575 if (!memcmp(fidp->path.data, path->data, path->size)) {
576 /* Mark the fid non reclaimable. */
577 fidp->flags |= FID_NON_RECLAIMABLE;
579 /* reopen the file/dir if already closed */
580 err = v9fs_reopen_fid(pdu, fidp);
585 * Go back to head of fid list because
586 * the list could have got updated when
587 * switched to the worker thread
597 static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
599 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
600 V9fsState *s = pdu->s;
604 while (s->fid_list) {
606 s->fid_list = fidp->next;
616 #define P9_QID_TYPE_DIR 0x80
617 #define P9_QID_TYPE_SYMLINK 0x02
619 #define P9_STAT_MODE_DIR 0x80000000
620 #define P9_STAT_MODE_APPEND 0x40000000
621 #define P9_STAT_MODE_EXCL 0x20000000
622 #define P9_STAT_MODE_MOUNT 0x10000000
623 #define P9_STAT_MODE_AUTH 0x08000000
624 #define P9_STAT_MODE_TMP 0x04000000
625 #define P9_STAT_MODE_SYMLINK 0x02000000
626 #define P9_STAT_MODE_LINK 0x01000000
627 #define P9_STAT_MODE_DEVICE 0x00800000
628 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
629 #define P9_STAT_MODE_SOCKET 0x00100000
630 #define P9_STAT_MODE_SETUID 0x00080000
631 #define P9_STAT_MODE_SETGID 0x00040000
632 #define P9_STAT_MODE_SETVTX 0x00010000
634 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
635 P9_STAT_MODE_SYMLINK | \
636 P9_STAT_MODE_LINK | \
637 P9_STAT_MODE_DEVICE | \
638 P9_STAT_MODE_NAMED_PIPE | \
641 /* This is the algorithm from ufs in spfs */
642 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
646 memset(&qidp->path, 0, sizeof(qidp->path));
647 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
648 memcpy(&qidp->path, &stbuf->st_ino, size);
649 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
651 if (S_ISDIR(stbuf->st_mode)) {
652 qidp->type |= P9_QID_TYPE_DIR;
655 if (S_ISLNK(stbuf->st_mode)) {
657 if (((stbuf->st_mode) & S_IFMT) == 0xA000) {
659 qidp->type |= P9_QID_TYPE_SYMLINK;
663 static int coroutine_fn fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp,
666 TRACE("[%d][ >> %s]\n", __LINE__, __func__);
670 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
674 stat_to_qid(&stbuf, qidp);
678 V9fsPDU *pdu_alloc(V9fsState *s)
682 if (!QLIST_EMPTY(&s->free_list)) {
683 pdu = QLIST_FIRST(&s->free_list);
684 QLIST_REMOVE(pdu, next);
685 QLIST_INSERT_HEAD(&s->active_list, pdu, next);
690 void pdu_free(V9fsPDU *pdu)
692 V9fsState *s = pdu->s;
694 g_assert(!pdu->cancelled);
695 QLIST_REMOVE(pdu, next);
696 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
700 * We don't do error checking for pdu_marshal/unmarshal here
701 * because we always expect to have enough space to encode
704 static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
706 int8_t id = pdu->id + 1; /* Response */
707 V9fsState *s = pdu->s;
710 WARN("[%d][ >> %s]\n", __LINE__, __func__);
714 if (s->proto_version != V9FS_PROTO_2000L) {
717 str.data = strerror(err);
718 str.size = strlen(str.data);
720 len += pdu_marshal(pdu, len, "s", &str);
724 len += pdu_marshal(pdu, len, "d", err);
726 if (s->proto_version == V9FS_PROTO_2000L) {
729 trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
732 /* fill out the header */
733 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
735 /* keep these in sync */
739 pdu_push_and_notify(pdu);
741 /* Now wakeup anybody waiting in flush for this request */
742 if (!qemu_co_queue_next(&pdu->complete)) {
748 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
753 if (mode & P9_STAT_MODE_DIR) {
757 if (mode & P9_STAT_MODE_SYMLINK) {
760 if (mode & P9_STAT_MODE_SOCKET) {
763 if (mode & P9_STAT_MODE_NAMED_PIPE) {
766 if (mode & P9_STAT_MODE_DEVICE) {
767 if (extension->size && extension->data[0] == 'c') {
778 if (mode & P9_STAT_MODE_SETUID) {
781 if (mode & P9_STAT_MODE_SETGID) {
784 if (mode & P9_STAT_MODE_SETVTX) {
792 static int donttouch_stat(V9fsStat *stat)
794 if (stat->type == -1 &&
796 stat->qid.type == -1 &&
797 stat->qid.version == -1 &&
798 stat->qid.path == -1 &&
802 stat->length == -1 &&
809 stat->n_muid == -1) {
816 static void v9fs_stat_init(V9fsStat *stat)
818 v9fs_string_init(&stat->name);
819 v9fs_string_init(&stat->uid);
820 v9fs_string_init(&stat->gid);
821 v9fs_string_init(&stat->muid);
822 v9fs_string_init(&stat->extension);
825 static void v9fs_stat_free(V9fsStat *stat)
827 v9fs_string_free(&stat->name);
828 v9fs_string_free(&stat->uid);
829 v9fs_string_free(&stat->gid);
830 v9fs_string_free(&stat->muid);
831 v9fs_string_free(&stat->extension);
834 static uint32_t stat_to_v9mode(const struct stat *stbuf)
838 mode = stbuf->st_mode & 0777;
840 if (S_ISDIR(stbuf->st_mode)) {
841 mode |= P9_STAT_MODE_DIR;
844 if (S_ISLNK(stbuf->st_mode)) {
845 mode |= P9_STAT_MODE_SYMLINK;
848 if (S_ISSOCK(stbuf->st_mode)) {
849 mode |= P9_STAT_MODE_SOCKET;
852 if (S_ISFIFO(stbuf->st_mode)) {
853 mode |= P9_STAT_MODE_NAMED_PIPE;
856 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
857 mode |= P9_STAT_MODE_DEVICE;
860 if (stbuf->st_mode & S_ISUID) {
861 mode |= P9_STAT_MODE_SETUID;
864 if (stbuf->st_mode & S_ISGID) {
865 mode |= P9_STAT_MODE_SETGID;
868 if (stbuf->st_mode & S_ISVTX) {
869 mode |= P9_STAT_MODE_SETVTX;
876 static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *name,
877 const struct stat *stbuf,
880 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
884 memset(v9stat, 0, sizeof(*v9stat));
886 stat_to_qid(stbuf, &v9stat->qid);
887 v9stat->mode = stat_to_v9mode(stbuf);
888 v9stat->atime = stbuf->st_atime;
889 v9stat->mtime = stbuf->st_mtime;
890 v9stat->length = stbuf->st_size;
892 v9fs_string_free(&v9stat->uid);
893 v9fs_string_free(&v9stat->gid);
894 v9fs_string_free(&v9stat->muid);
896 v9stat->n_uid = stbuf->st_uid;
897 v9stat->n_gid = stbuf->st_gid;
900 v9fs_string_free(&v9stat->extension);
902 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
903 err = v9fs_co_readlink(pdu, name, &v9stat->extension);
907 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
908 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
909 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
910 major(stbuf->st_rdev), minor(stbuf->st_rdev));
911 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
912 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
913 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
916 str = strrchr(name->data, '/');
923 v9fs_string_sprintf(&v9stat->name, "%s", str);
926 v9fs_string_size(&v9stat->name) +
927 v9fs_string_size(&v9stat->uid) +
928 v9fs_string_size(&v9stat->gid) +
929 v9fs_string_size(&v9stat->muid) +
930 v9fs_string_size(&v9stat->extension);
934 #define P9_STATS_MODE 0x00000001ULL
935 #define P9_STATS_NLINK 0x00000002ULL
936 #define P9_STATS_UID 0x00000004ULL
937 #define P9_STATS_GID 0x00000008ULL
938 #define P9_STATS_RDEV 0x00000010ULL
939 #define P9_STATS_ATIME 0x00000020ULL
940 #define P9_STATS_MTIME 0x00000040ULL
941 #define P9_STATS_CTIME 0x00000080ULL
942 #define P9_STATS_INO 0x00000100ULL
943 #define P9_STATS_SIZE 0x00000200ULL
944 #define P9_STATS_BLOCKS 0x00000400ULL
946 #define P9_STATS_BTIME 0x00000800ULL
947 #define P9_STATS_GEN 0x00001000ULL
948 #define P9_STATS_DATA_VERSION 0x00002000ULL
950 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
951 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
954 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
955 V9fsStatDotl *v9lstat)
957 memset(v9lstat, 0, sizeof(*v9lstat));
959 v9lstat->st_mode = stbuf->st_mode;
960 v9lstat->st_nlink = stbuf->st_nlink;
961 v9lstat->st_uid = stbuf->st_uid;
962 v9lstat->st_gid = stbuf->st_gid;
963 v9lstat->st_rdev = stbuf->st_rdev;
964 v9lstat->st_size = stbuf->st_size;
966 v9lstat->st_blksize = stbuf->st_blksize;
967 v9lstat->st_blocks = stbuf->st_blocks;
969 v9lstat->st_blksize = hostBytesPerSector;
970 v9lstat->st_blocks = (uint64_t)(v9lstat->st_size / 512
971 + (v9lstat->st_size % 512 ? 1 : 0)); //round up
974 v9lstat->st_atime_sec = stbuf->st_atime;
975 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
976 v9lstat->st_mtime_sec = stbuf->st_mtime;
977 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
978 v9lstat->st_ctime_sec = stbuf->st_ctime;
979 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
982 v9lstat->st_atime_sec = stbuf->st_atimespec.tv_sec;
983 v9lstat->st_atime_nsec = stbuf->st_atimespec.tv_nsec;
984 v9lstat->st_mtime_sec = stbuf->st_mtimespec.tv_sec;
985 v9lstat->st_mtime_nsec = stbuf->st_mtimespec.tv_nsec;
986 v9lstat->st_ctime_sec = stbuf->st_ctimespec.tv_sec;
987 v9lstat->st_ctime_nsec = stbuf->st_ctimespec.tv_nsec;
989 v9lstat->st_atime_sec = stbuf->st_atime;
990 v9lstat->st_atime_nsec = 0;
991 v9lstat->st_mtime_sec = stbuf->st_mtime;
992 v9lstat->st_mtime_nsec = 0;
993 v9lstat->st_ctime_sec = stbuf->st_ctime;
994 v9lstat->st_ctime_nsec = 0;
998 /* Currently we only support BASIC fields in stat */
999 v9lstat->st_result_mask = P9_STATS_BASIC;
1001 stat_to_qid(stbuf, &v9lstat->qid);
1004 static void print_sg(struct iovec *sg, int cnt)
1008 printf("sg[%d]: {", cnt);
1009 for (i = 0; i < cnt; i++) {
1013 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1018 /* Will call this only for path name based fid */
1019 static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
1022 v9fs_path_init(&str);
1023 v9fs_path_copy(&str, dst);
1024 v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
1025 v9fs_path_free(&str);
1028 static inline bool is_ro_export(FsContext *ctx)
1030 return ctx->export_flags & V9FS_RDONLY;
1033 static void coroutine_fn v9fs_version(void *opaque)
1035 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1037 V9fsPDU *pdu = opaque;
1038 V9fsState *s = pdu->s;
1042 v9fs_string_init(&version);
1043 err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1048 trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
1052 if (!strcmp(version.data, "9P2000.u")) {
1053 s->proto_version = V9FS_PROTO_2000U;
1054 } else if (!strcmp(version.data, "9P2000.L")) {
1055 s->proto_version = V9FS_PROTO_2000L;
1057 v9fs_string_sprintf(&version, "unknown");
1060 err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
1066 trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
1068 pdu_complete(pdu, offset);
1069 v9fs_string_free(&version);
1072 static void coroutine_fn v9fs_attach(void *opaque)
1074 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1075 V9fsPDU *pdu = opaque;
1076 V9fsState *s = pdu->s;
1077 int32_t fid, afid, n_uname;
1078 V9fsString uname, aname;
1084 v9fs_string_init(&uname);
1085 v9fs_string_init(&aname);
1086 err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
1087 &afid, &uname, &aname, &n_uname);
1091 trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
1093 fidp = alloc_fid(s, fid);
1098 fidp->uid = n_uname;
1099 #ifndef CONFIG_WIN32
1100 err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
1102 err = v9fs_co_name_to_path(pdu, NULL, "\\", &fidp->path);
1109 err = fid_to_qid(pdu, fidp, &qid);
1115 err = pdu_marshal(pdu, offset, "Q", &qid);
1121 memcpy(&s->root_qid, &qid, sizeof(qid));
1122 trace_v9fs_attach_return(pdu->tag, pdu->id,
1123 qid.type, qid.version, qid.path);
1125 * disable migration if we haven't done already.
1126 * attach could get called multiple times for the same export.
1128 if (!s->migration_blocker) {
1130 error_setg(&s->migration_blocker,
1131 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1132 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
1133 migrate_add_blocker(s->migration_blocker);
1138 pdu_complete(pdu, err);
1139 v9fs_string_free(&uname);
1140 v9fs_string_free(&aname);
1143 static void coroutine_fn v9fs_stat(void *opaque)
1145 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1152 V9fsPDU *pdu = opaque;
1154 err = pdu_unmarshal(pdu, offset, "d", &fid);
1158 trace_v9fs_stat(pdu->tag, pdu->id, fid);
1160 fidp = get_fid(pdu, fid);
1165 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1169 err = stat_to_v9stat(pdu, &fidp->path, &stbuf, &v9stat);
1173 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1175 v9fs_stat_free(&v9stat);
1178 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1179 v9stat.atime, v9stat.mtime, v9stat.length);
1181 v9fs_stat_free(&v9stat);
1185 pdu_complete(pdu, err);
1188 static void coroutine_fn v9fs_getattr(void *opaque)
1190 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1196 uint64_t request_mask;
1197 V9fsStatDotl v9stat_dotl;
1198 V9fsPDU *pdu = opaque;
1199 V9fsState *s = pdu->s;
1201 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1205 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
1207 fidp = get_fid(pdu, fid);
1213 * Currently we only support BASIC fields in stat, so there is no
1214 * need to look at request_mask.
1216 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1220 stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
1222 /* fill st_gen if requested and supported by underlying fs */
1223 if (request_mask & P9_STATS_GEN) {
1224 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
1227 /* we have valid st_gen: update result mask */
1228 v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1231 /* request cancelled, e.g. by Tflush */
1234 /* failed to get st_gen: not fatal, ignore */
1238 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1243 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1244 v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1245 v9stat_dotl.st_gid);
1249 pdu_complete(pdu, retval);
1252 /* Attribute flags */
1253 #define P9_ATTR_MODE (1 << 0)
1254 #define P9_ATTR_UID (1 << 1)
1255 #define P9_ATTR_GID (1 << 2)
1256 #define P9_ATTR_SIZE (1 << 3)
1257 #define P9_ATTR_ATIME (1 << 4)
1258 #define P9_ATTR_MTIME (1 << 5)
1259 #define P9_ATTR_CTIME (1 << 6)
1260 #define P9_ATTR_ATIME_SET (1 << 7)
1261 #define P9_ATTR_MTIME_SET (1 << 8)
1263 #define P9_ATTR_MASK 127
1265 static void coroutine_fn v9fs_setattr(void *opaque)
1267 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1273 V9fsPDU *pdu = opaque;
1275 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1280 fidp = get_fid(pdu, fid);
1285 if (v9iattr.valid & P9_ATTR_MODE) {
1286 #ifndef CONFIG_WIN32
1287 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1295 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1296 struct timespec times[2];
1297 if (v9iattr.valid & P9_ATTR_ATIME) {
1298 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1299 times[0].tv_sec = v9iattr.atime_sec;
1300 times[0].tv_nsec = v9iattr.atime_nsec;
1302 times[0].tv_nsec = UTIME_NOW;
1305 times[0].tv_nsec = UTIME_OMIT;
1307 if (v9iattr.valid & P9_ATTR_MTIME) {
1308 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1309 times[1].tv_sec = v9iattr.mtime_sec;
1310 times[1].tv_nsec = v9iattr.mtime_nsec;
1312 times[1].tv_nsec = UTIME_NOW;
1315 times[1].tv_nsec = UTIME_OMIT;
1317 err = v9fs_co_utimensat(pdu, &fidp->path, times);
1323 * If the only valid entry in iattr is ctime we can call
1324 * chown(-1,-1) to update the ctime of the file
1326 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1327 ((v9iattr.valid & P9_ATTR_CTIME)
1328 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1329 if (!(v9iattr.valid & P9_ATTR_UID)) {
1332 if (!(v9iattr.valid & P9_ATTR_GID)) {
1335 #ifndef CONFIG_WIN32
1336 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1345 if (v9iattr.valid & (P9_ATTR_SIZE)) {
1346 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1355 pdu_complete(pdu, err);
1358 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1364 err = pdu_marshal(pdu, offset, "w", nwnames);
1366 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1370 for (i = 0; i < nwnames; i++) {
1371 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1373 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1381 static bool name_is_illegal(const char *name)
1383 #ifndef CONFIG_WIN32
1384 return !*name || strchr(name, '/') != NULL;
1386 return !*name || strchr(name, '\\') != NULL;
1390 static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
1393 qid1->type != qid2->type ||
1394 qid1->version != qid2->version ||
1395 qid1->path != qid2->path;
1398 static void coroutine_fn v9fs_walk(void *opaque)
1400 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1402 V9fsQID *qids = NULL;
1404 V9fsPath dpath, path;
1408 int32_t fid, newfid;
1409 V9fsString *wnames = NULL;
1411 V9fsFidState *newfidp = NULL;
1412 V9fsPDU *pdu = opaque;
1413 V9fsState *s = pdu->s;
1416 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1418 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1419 pdu_complete(pdu, err);
1424 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1426 if (nwnames && nwnames <= P9_MAXWELEM) {
1427 wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
1428 qids = g_malloc0(sizeof(qids[0]) * nwnames);
1429 for (i = 0; i < nwnames; i++) {
1430 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1432 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1435 if (name_is_illegal(wnames[i].data)) {
1441 } else if (nwnames > P9_MAXWELEM) {
1442 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1446 fidp = get_fid(pdu, fid);
1448 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1453 v9fs_path_init(&dpath);
1454 v9fs_path_init(&path);
1456 err = fid_to_qid(pdu, fidp, &qid);
1462 * Both dpath and path initially poin to fidp.
1463 * Needed to handle request with nwnames == 0
1465 v9fs_path_copy(&dpath, &fidp->path);
1466 v9fs_path_copy(&path, &fidp->path);
1467 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1468 if (not_same_qid(&pdu->s->root_qid, &qid) ||
1469 strcmp("..", wnames[name_idx].data)) {
1470 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
1473 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1477 err = v9fs_co_lstat(pdu, &path, &stbuf);
1481 stat_to_qid(&stbuf, &qid);
1482 v9fs_path_copy(&dpath, &path);
1484 memcpy(&qids[name_idx], &qid, sizeof(qid));
1486 if (fid == newfid) {
1487 if (fidp->fid_type != P9_FID_NONE) {
1491 WARN("[%d][ >> %s]\n", __LINE__, __func__);
1492 v9fs_path_copy(&fidp->path, &path);
1494 newfidp = alloc_fid(s, newfid);
1495 if (newfidp == NULL) {
1496 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1500 newfidp->uid = fidp->uid;
1501 v9fs_path_copy(&newfidp->path, &path);
1503 err = v9fs_walk_marshal(pdu, nwnames, qids);
1504 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1508 put_fid(pdu, newfidp);
1510 v9fs_path_free(&dpath);
1511 v9fs_path_free(&path);
1513 pdu_complete(pdu, err);
1514 if (nwnames && nwnames <= P9_MAXWELEM) {
1515 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1516 v9fs_string_free(&wnames[name_idx]);
1523 static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
1525 struct statfs stbuf;
1527 V9fsState *s = pdu->s;
1530 * iounit should be multiples of f_bsize (host filesystem block size
1531 * and as well as less than (client msize - P9_IOHDRSZ))
1533 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1534 iounit = stbuf.f_bsize;
1535 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1538 iounit = s->msize - P9_IOHDRSZ;
1543 static void coroutine_fn v9fs_open(void *opaque)
1545 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1555 V9fsPDU *pdu = opaque;
1556 V9fsState *s = pdu->s;
1558 if (s->proto_version == V9FS_PROTO_2000L) {
1559 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1562 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1566 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1569 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1571 fidp = get_fid(pdu, fid);
1573 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1577 if (fidp->fid_type != P9_FID_NONE) {
1578 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1583 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1585 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1588 stat_to_qid(&stbuf, &qid);
1589 if (S_ISDIR(stbuf.st_mode)) {
1590 err = v9fs_co_opendir(pdu, fidp);
1592 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1595 fidp->fid_type = P9_FID_DIR;
1596 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1598 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1603 if (s->proto_version == V9FS_PROTO_2000L) {
1604 flags = get_dotl_openflags(s, mode);
1606 flags = omode_to_uflags(mode);
1608 if (is_ro_export(&s->ctx)) {
1609 if (mode & O_WRONLY || mode & O_RDWR ||
1610 mode & O_APPEND || mode & O_TRUNC) {
1611 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1616 err = v9fs_co_open(pdu, fidp, flags);
1618 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1621 fidp->fid_type = P9_FID_FILE;
1622 fidp->open_flags = flags;
1623 if (flags & O_EXCL) {
1625 * We let the host file system do O_EXCL check
1626 * We should not reclaim such fd
1628 fidp->flags |= FID_NON_RECLAIMABLE;
1630 iounit = get_iounit(pdu, &fidp->path);
1631 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1633 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1638 trace_v9fs_open_return(pdu->tag, pdu->id,
1639 qid.type, qid.version, qid.path, iounit);
1643 pdu_complete(pdu, err);
1646 static void coroutine_fn v9fs_lcreate(void *opaque)
1648 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1649 int32_t dfid, flags, mode;
1658 V9fsPDU *pdu = opaque;
1660 v9fs_string_init(&name);
1661 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1662 &name, &flags, &mode, &gid);
1664 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1667 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1669 if (name_is_illegal(name.data)) {
1674 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
1679 fidp = get_fid(pdu, dfid);
1681 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1686 flags = get_dotl_openflags(pdu->s, flags);
1687 err = v9fs_co_open2(pdu, fidp, &name, gid,
1688 flags | O_CREAT, mode, &stbuf);
1690 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1693 fidp->fid_type = P9_FID_FILE;
1694 fidp->open_flags = flags;
1695 if (flags & O_EXCL) {
1697 * We let the host file system do O_EXCL check
1698 * We should not reclaim such fd
1700 fidp->flags |= FID_NON_RECLAIMABLE;
1702 iounit = get_iounit(pdu, &fidp->path);
1703 stat_to_qid(&stbuf, &qid);
1704 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1706 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1710 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
1711 qid.type, qid.version, qid.path, iounit);
1715 pdu_complete(pdu, err);
1716 v9fs_string_free(&name);
1719 static void v9fs_fsync(void *opaque)
1721 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1727 V9fsPDU *pdu = opaque;
1729 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1731 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1734 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
1736 fidp = get_fid(pdu, fid);
1739 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1742 err = v9fs_co_fsync(pdu, fidp, datasync);
1748 pdu_complete(pdu, err);
1751 static void coroutine_fn v9fs_clunk(void *opaque)
1757 V9fsPDU *pdu = opaque;
1758 V9fsState *s = pdu->s;
1760 err = pdu_unmarshal(pdu, offset, "d", &fid);
1764 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
1766 fidp = clunk_fid(s, fid);
1772 * Bump the ref so that put_fid will
1776 err = put_fid(pdu, fidp);
1781 pdu_complete(pdu, err);
1784 #ifndef CONFIG_WIN32
1785 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1786 uint64_t off, uint32_t max_count)
1790 uint64_t read_count;
1791 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
1792 VirtQueueElement *elem = v->elems[pdu->idx];
1794 if (fidp->fs.xattr.len < off) {
1797 read_count = fidp->fs.xattr.len - off;
1799 if (read_count > max_count) {
1800 read_count = max_count;
1802 err = pdu_marshal(pdu, offset, "d", read_count);
1808 err = v9fs_pack(elem->in_sg, elem->in_num, offset,
1809 ((char *)fidp->fs.xattr.value) + off,
1819 static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1823 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1829 off_t saved_dir_pos;
1830 struct dirent *dent;
1832 /* save the directory position */
1833 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1834 if (saved_dir_pos < 0) {
1835 return saved_dir_pos;
1839 v9fs_path_init(&path);
1841 v9fs_readdir_lock(&fidp->fs.dir);
1843 err = v9fs_co_readdir(pdu, fidp, &dent);
1845 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1848 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
1850 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1853 err = v9fs_co_lstat(pdu, &path, &stbuf);
1855 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1858 err = stat_to_v9stat(pdu, &path, &stbuf, &v9stat);
1860 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1863 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1864 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1866 v9fs_readdir_unlock(&fidp->fs.dir);
1868 if ((len != (v9stat.size + 2)) || ((count + len) > max_count)) {
1869 /* Ran out of buffer. Set dir back to old position and return */
1870 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1871 v9fs_stat_free(&v9stat);
1872 v9fs_path_free(&path);
1876 v9fs_stat_free(&v9stat);
1877 v9fs_path_free(&path);
1879 saved_dir_pos = dent->d_off;
1881 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1885 v9fs_readdir_unlock(&fidp->fs.dir);
1887 v9fs_path_free(&path);
1895 * Create a QEMUIOVector for a sub-region of PDU iovecs
1897 * @qiov: uninitialized QEMUIOVector
1898 * @skip: number of bytes to skip from beginning of PDU
1899 * @size: number of bytes to include
1900 * @is_write: true - write, false - read
1902 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1903 * with qemu_iovec_destroy().
1905 static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1906 size_t skip, size_t size,
1913 virtio_init_iov_from_pdu(pdu, &iov, &niov, is_write);
1915 qemu_iovec_init_external(&elem, iov, niov);
1916 qemu_iovec_init(qiov, niov);
1917 qemu_iovec_concat(qiov, &elem, skip, size);
1920 static void coroutine_fn v9fs_read(void *opaque)
1922 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1930 V9fsPDU *pdu = opaque;
1931 #ifndef CONFIG_WIN32
1932 V9fsState *s = pdu->s;
1935 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1937 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1940 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
1942 fidp = get_fid(pdu, fid);
1944 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1948 if (fidp->fid_type == P9_FID_DIR) {
1951 v9fs_co_rewinddir(pdu, fidp);
1953 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
1955 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1959 err = pdu_marshal(pdu, offset, "d", count);
1961 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1964 err += offset + count;
1965 } else if (fidp->fid_type == P9_FID_FILE) {
1966 QEMUIOVector qiov_full;
1970 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
1971 qemu_iovec_init(&qiov, qiov_full.niov);
1973 qemu_iovec_reset(&qiov);
1974 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
1976 print_sg(qiov.iov, qiov.niov);
1978 /* Loop in case of EINTR */
1980 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
1985 } while (len == -EINTR && !pdu->cancelled);
1987 /* IO error return the error */
1988 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1990 goto out_free_iovec;
1992 } while (count < max_count && len > 0);
1993 err = pdu_marshal(pdu, offset, "d", count);
1995 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1996 goto out_free_iovec;
1998 err += offset + count;
2000 qemu_iovec_destroy(&qiov);
2001 qemu_iovec_destroy(&qiov_full);
2002 #ifndef CONFIG_WIN32
2003 } else if (fidp->fid_type == P9_FID_XATTR) {
2004 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
2007 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2010 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
2014 pdu_complete(pdu, err);
2017 static size_t v9fs_readdir_data_size(V9fsString *name)
2020 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
2021 * size of type (1) + size of name.size (2) + strlen(name.data)
2023 return 24 + v9fs_string_size(name);
2026 static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
2029 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2035 off_t saved_dir_pos;
2036 struct dirent *dent;
2037 #ifndef CONFIG_LINUX
2038 uint64_t d_offset = 0;
2041 /* save the directory position */
2042 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
2043 if (saved_dir_pos < 0) {
2044 return saved_dir_pos;
2048 v9fs_readdir_lock(&fidp->fs.dir);
2050 err = v9fs_co_readdir(pdu, fidp, &dent);
2054 v9fs_string_init(&name);
2055 v9fs_string_sprintf(&name, "%s", dent->d_name);
2056 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
2057 v9fs_readdir_unlock(&fidp->fs.dir);
2059 /* Ran out of buffer. Set dir back to old position and return */
2060 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2061 v9fs_string_free(&name);
2065 * Fill up just the path field of qid because the client uses
2066 * only that. To fill the entire qid structure we will have
2067 * to stat each dirent found, which is expensive
2069 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
2070 memcpy(&qid.path, &dent->d_ino, size);
2071 /* Fill the other fields with dummy values */
2075 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2077 len = pdu_marshal(pdu, 11 + count, "Qqbs",
2079 dent->d_type, &name);
2081 d_offset = v9fs_co_telldir(pdu, fidp);
2082 #ifndef CONFIG_WIN32
2083 len = pdu_marshal(pdu, 11 + count, "Qqbs",
2085 dent->d_type, &name);
2087 len = pdu_marshal(pdu, 11 + count, "Qqbs",
2089 WIN32_D_TYPE, &name);
2093 v9fs_readdir_unlock(&fidp->fs.dir);
2096 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2097 v9fs_string_free(&name);
2101 v9fs_string_free(&name);
2103 saved_dir_pos = dent->d_off;
2105 saved_dir_pos = d_offset;
2109 v9fs_readdir_unlock(&fidp->fs.dir);
2117 static void coroutine_fn v9fs_readdir(void *opaque)
2119 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2124 uint64_t initial_offset;
2127 V9fsPDU *pdu = opaque;
2129 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
2130 &initial_offset, &max_count);
2132 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2135 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
2137 fidp = get_fid(pdu, fid);
2139 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2143 if (!fidp->fs.dir.stream) {
2144 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2148 if (initial_offset == 0) {
2149 v9fs_co_rewinddir(pdu, fidp);
2151 v9fs_co_seekdir(pdu, fidp, initial_offset);
2153 count = v9fs_do_readdir(pdu, fidp, max_count);
2155 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2159 retval = pdu_marshal(pdu, offset, "d", count);
2161 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2164 retval += count + offset;
2165 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
2169 pdu_complete(pdu, retval);
2172 #ifndef CONFIG_WIN32
2173 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
2174 uint64_t off, uint32_t count,
2175 struct iovec *sg, int cnt)
2179 uint64_t write_count;
2183 if (fidp->fs.xattr.len < off) {
2187 write_count = fidp->fs.xattr.len - off;
2188 if (write_count > count) {
2189 write_count = count;
2191 err = pdu_marshal(pdu, offset, "d", write_count);
2196 fidp->fs.xattr.copied_len += write_count;
2198 * Now copy the content from sg list
2200 for (i = 0; i < cnt; i++) {
2201 if (write_count > sg[i].iov_len) {
2202 to_copy = sg[i].iov_len;
2204 to_copy = write_count;
2206 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
2207 /* updating vs->off since we are not using below */
2209 write_count -= to_copy;
2216 static void coroutine_fn v9fs_write(void *opaque)
2218 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2227 V9fsPDU *pdu = opaque;
2228 #ifndef CONFIG_WIN32
2229 V9fsState *s = pdu->s;
2231 QEMUIOVector qiov_full;
2234 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
2236 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2237 pdu_complete(pdu, err);
2241 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
2242 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
2244 fidp = get_fid(pdu, fid);
2246 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2250 if (fidp->fid_type == P9_FID_FILE) {
2251 if (fidp->fs.fd == -1) {
2252 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2256 } else if (fidp->fid_type == P9_FID_XATTR) {
2258 * setxattr operation
2260 #ifndef CONFIG_WIN32
2261 err = v9fs_xattr_write(s, pdu, fidp, off, count,
2262 qiov_full.iov, qiov_full.niov);
2266 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2270 qemu_iovec_init(&qiov, qiov_full.niov);
2272 qemu_iovec_reset(&qiov);
2273 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
2275 print_sg(qiov.iov, qiov.niov);
2277 /* Loop in case of EINTR */
2279 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
2284 } while (len == -EINTR && !pdu->cancelled);
2286 /* IO error return the error */
2287 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2291 } while (total < count && len > 0);
2294 err = pdu_marshal(pdu, offset, "d", total);
2296 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2300 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2302 qemu_iovec_destroy(&qiov);
2306 qemu_iovec_destroy(&qiov_full);
2307 pdu_complete(pdu, err);
2310 static void coroutine_fn v9fs_create(void *opaque)
2312 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2323 V9fsString extension;
2325 V9fsPDU *pdu = opaque;
2327 v9fs_path_init(&path);
2328 v9fs_string_init(&name);
2329 v9fs_string_init(&extension);
2330 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2331 &perm, &mode, &extension);
2333 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2336 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2338 if (name_is_illegal(name.data)) {
2343 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2348 fidp = get_fid(pdu, fid);
2350 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2354 if (perm & P9_STAT_MODE_DIR) {
2355 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2356 fidp->uid, -1, &stbuf);
2358 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2361 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2363 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2366 v9fs_path_copy(&fidp->path, &path);
2367 err = v9fs_co_opendir(pdu, fidp);
2369 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2372 fidp->fid_type = P9_FID_DIR;
2373 #ifndef CONFIG_WIN32
2374 } else if (perm & P9_STAT_MODE_SYMLINK) {
2375 err = v9fs_co_symlink(pdu, fidp, &name,
2376 extension.data, -1 , &stbuf);
2378 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2381 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2383 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2386 v9fs_path_copy(&fidp->path, &path);
2387 } else if (perm & P9_STAT_MODE_LINK) {
2388 int32_t ofid = atoi(extension.data);
2389 V9fsFidState *ofidp = get_fid(pdu, ofid);
2390 if (ofidp == NULL) {
2391 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2395 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2396 put_fid(pdu, ofidp);
2398 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2401 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2403 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2404 fidp->fid_type = P9_FID_NONE;
2407 v9fs_path_copy(&fidp->path, &path);
2408 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2410 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2411 fidp->fid_type = P9_FID_NONE;
2414 } else if (perm & P9_STAT_MODE_DEVICE) {
2416 uint32_t major, minor;
2419 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2420 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2437 nmode |= perm & 0777;
2438 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2439 makedev(major, minor), nmode, &stbuf);
2441 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2444 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2446 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2449 v9fs_path_copy(&fidp->path, &path);
2450 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2451 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2452 0, S_IFIFO | (perm & 0777), &stbuf);
2454 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2457 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2459 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2462 v9fs_path_copy(&fidp->path, &path);
2463 } else if (perm & P9_STAT_MODE_SOCKET) {
2464 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2465 0, S_IFSOCK | (perm & 0777), &stbuf);
2467 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2470 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2472 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2475 v9fs_path_copy(&fidp->path, &path);
2478 err = v9fs_co_open2(pdu, fidp, &name, -1,
2479 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2481 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2484 fidp->fid_type = P9_FID_FILE;
2485 fidp->open_flags = omode_to_uflags(mode);
2486 if (fidp->open_flags & O_EXCL) {
2488 * We let the host file system do O_EXCL check
2489 * We should not reclaim such fd
2491 fidp->flags |= FID_NON_RECLAIMABLE;
2494 iounit = get_iounit(pdu, &fidp->path);
2495 stat_to_qid(&stbuf, &qid);
2496 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2498 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2502 trace_v9fs_create_return(pdu->tag, pdu->id,
2503 qid.type, qid.version, qid.path, iounit);
2507 pdu_complete(pdu, err);
2508 v9fs_string_free(&name);
2509 v9fs_string_free(&extension);
2510 v9fs_path_free(&path);
2513 static void coroutine_fn v9fs_symlink(void *opaque)
2515 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2516 V9fsPDU *pdu = opaque;
2519 V9fsFidState *dfidp;
2527 v9fs_string_init(&name);
2528 v9fs_string_init(&symname);
2529 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2533 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2535 if (name_is_illegal(name.data)) {
2540 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2545 dfidp = get_fid(pdu, dfid);
2546 if (dfidp == NULL) {
2550 #ifndef CONFIG_WIN32
2551 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2558 stat_to_qid(&stbuf, &qid);
2559 err = pdu_marshal(pdu, offset, "Q", &qid);
2564 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2565 qid.type, qid.version, qid.path);
2567 put_fid(pdu, dfidp);
2569 pdu_complete(pdu, err);
2570 v9fs_string_free(&name);
2571 v9fs_string_free(&symname);
2574 static void v9fs_flush(void *opaque)
2576 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2580 V9fsPDU *cancel_pdu;
2581 V9fsPDU *pdu = opaque;
2582 V9fsState *s = pdu->s;
2584 err = pdu_unmarshal(pdu, offset, "w", &tag);
2586 pdu_complete(pdu, err);
2589 trace_v9fs_flush(pdu->tag, pdu->id, tag);
2591 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2592 if (cancel_pdu->tag == tag) {
2597 cancel_pdu->cancelled = 1;
2599 * Wait for pdu to complete.
2601 qemu_co_queue_wait(&cancel_pdu->complete);
2602 cancel_pdu->cancelled = 0;
2603 pdu_free(cancel_pdu);
2605 pdu_complete(pdu, 7);
2608 static void coroutine_fn v9fs_link(void *opaque)
2610 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2611 V9fsPDU *pdu = opaque;
2612 int32_t dfid, oldfid;
2613 V9fsFidState *dfidp, *oldfidp;
2618 v9fs_string_init(&name);
2619 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2623 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2625 if (name_is_illegal(name.data)) {
2630 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2635 dfidp = get_fid(pdu, dfid);
2636 if (dfidp == NULL) {
2641 oldfidp = get_fid(pdu, oldfid);
2642 if (oldfidp == NULL) {
2646 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2650 put_fid(pdu, oldfidp);
2652 put_fid(pdu, dfidp);
2654 v9fs_string_free(&name);
2655 pdu_complete(pdu, err);
2658 /* Only works with path name based fid */
2659 static void coroutine_fn v9fs_remove(void *opaque)
2661 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2666 V9fsPDU *pdu = opaque;
2668 err = pdu_unmarshal(pdu, offset, "d", &fid);
2672 trace_v9fs_remove(pdu->tag, pdu->id, fid);
2674 fidp = get_fid(pdu, fid);
2679 /* if fs driver is not path based, return EOPNOTSUPP */
2680 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2685 * IF the file is unlinked, we cannot reopen
2686 * the file later. So don't reclaim fd
2688 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2692 err = v9fs_co_remove(pdu, &fidp->path);
2697 /* For TREMOVE we need to clunk the fid even on failed remove */
2698 clunk_fid(pdu->s, fidp->fid);
2701 pdu_complete(pdu, err);
2704 static void coroutine_fn v9fs_unlinkat(void *opaque)
2706 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2709 int32_t dfid, flags;
2712 V9fsFidState *dfidp;
2713 V9fsPDU *pdu = opaque;
2715 v9fs_string_init(&name);
2716 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2721 if (name_is_illegal(name.data)) {
2726 if (!strcmp(".", name.data)) {
2731 if (!strcmp("..", name.data)) {
2736 dfidp = get_fid(pdu, dfid);
2737 if (dfidp == NULL) {
2742 * IF the file is unlinked, we cannot reopen
2743 * the file later. So don't reclaim fd
2745 v9fs_path_init(&path);
2746 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2750 err = v9fs_mark_fids_unreclaim(pdu, &path);
2754 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, flags);
2759 put_fid(pdu, dfidp);
2760 v9fs_path_free(&path);
2762 pdu_complete(pdu, err);
2763 v9fs_string_free(&name);
2767 /* Only works with path name based fid */
2768 static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2772 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2776 V9fsFidState *tfidp;
2777 V9fsState *s = pdu->s;
2778 V9fsFidState *dirfidp = NULL;
2779 char *old_name, *new_name;
2781 v9fs_path_init(&new_path);
2782 if (newdirfid != -1) {
2783 dirfidp = get_fid(pdu, newdirfid);
2784 if (dirfidp == NULL) {
2788 if (fidp->fid_type != P9_FID_NONE) {
2792 v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2794 old_name = fidp->path.data;
2795 end = strrchr(old_name, '/');
2801 new_name = g_malloc0(end - old_name + name->size + 1);
2802 strncat(new_name, old_name, end - old_name);
2803 strncat(new_name + (end - old_name), name->data, name->size);
2804 v9fs_co_name_to_path(pdu, NULL, new_name, &new_path);
2807 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2812 * Fixup fid's pointing to the old name to
2813 * start pointing to the new name
2815 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2816 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2817 /* replace the name */
2818 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
2823 put_fid(pdu, dirfidp);
2825 v9fs_path_free(&new_path);
2830 /* Only works with path name based fid */
2831 static void coroutine_fn v9fs_rename(void *opaque)
2833 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2840 V9fsPDU *pdu = opaque;
2841 V9fsState *s = pdu->s;
2843 v9fs_string_init(&name);
2844 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2849 if (name_is_illegal(name.data)) {
2854 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2859 fidp = get_fid(pdu, fid);
2864 if (fidp->fid_type != P9_FID_NONE) {
2868 /* if fs driver is not path based, return EOPNOTSUPP */
2869 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2873 v9fs_path_write_lock(s);
2874 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
2875 v9fs_path_unlock(s);
2882 pdu_complete(pdu, err);
2883 v9fs_string_free(&name);
2886 static void coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2887 V9fsString *old_name,
2889 V9fsString *new_name)
2891 V9fsFidState *tfidp;
2892 V9fsPath oldpath, newpath;
2893 V9fsState *s = pdu->s;
2896 v9fs_path_init(&oldpath);
2897 v9fs_path_init(&newpath);
2898 v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
2899 v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2902 * Fixup fid's pointing to the old name to
2903 * start pointing to the new name
2905 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2906 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
2907 /* replace the name */
2908 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
2911 v9fs_path_free(&oldpath);
2912 v9fs_path_free(&newpath);
2915 static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2916 V9fsString *old_name,
2918 V9fsString *new_name)
2920 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2922 V9fsState *s = pdu->s;
2923 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2925 olddirfidp = get_fid(pdu, olddirfid);
2926 if (olddirfidp == NULL) {
2930 if (newdirfid != -1) {
2931 newdirfidp = get_fid(pdu, newdirfid);
2932 if (newdirfidp == NULL) {
2937 newdirfidp = get_fid(pdu, olddirfid);
2940 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2941 &newdirfidp->path, new_name);
2945 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
2946 /* Only for path based fid we need to do the below fixup */
2947 v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2948 &newdirfidp->path, new_name);
2952 put_fid(pdu, olddirfidp);
2955 put_fid(pdu, newdirfidp);
2960 static void coroutine_fn v9fs_renameat(void *opaque)
2962 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2965 V9fsPDU *pdu = opaque;
2966 V9fsState *s = pdu->s;
2967 int32_t olddirfid, newdirfid;
2968 V9fsString old_name, new_name;
2970 v9fs_string_init(&old_name);
2971 v9fs_string_init(&new_name);
2972 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2973 &old_name, &newdirfid, &new_name);
2978 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
2983 if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
2984 !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
2989 v9fs_path_write_lock(s);
2990 err = v9fs_complete_renameat(pdu, olddirfid,
2991 &old_name, newdirfid, &new_name);
2992 v9fs_path_unlock(s);
2998 pdu_complete(pdu, err);
2999 v9fs_string_free(&old_name);
3000 v9fs_string_free(&new_name);
3003 static void coroutine_fn v9fs_wstat(void *opaque)
3005 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3013 V9fsPDU *pdu = opaque;
3015 v9fs_stat_init(&v9stat);
3016 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
3020 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
3021 v9stat.mode, v9stat.atime, v9stat.mtime);
3023 fidp = get_fid(pdu, fid);
3028 /* do we need to sync the file? */
3029 if (donttouch_stat(&v9stat)) {
3030 err = v9fs_co_fsync(pdu, fidp, 0);
3033 if (v9stat.mode != -1) {
3035 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
3039 v9_mode = stat_to_v9mode(&stbuf);
3040 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
3041 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
3042 /* Attempting to change the type */
3046 #ifndef CONFIG_WIN32
3047 err = v9fs_co_chmod(pdu, &fidp->path,
3048 v9mode_to_mode(v9stat.mode,
3049 &v9stat.extension));
3057 if (v9stat.mtime != -1 || v9stat.atime != -1) {
3058 struct timespec times[2];
3059 if (v9stat.atime != -1) {
3060 times[0].tv_sec = v9stat.atime;
3061 times[0].tv_nsec = 0;
3063 times[0].tv_nsec = UTIME_OMIT;
3065 if (v9stat.mtime != -1) {
3066 times[1].tv_sec = v9stat.mtime;
3067 times[1].tv_nsec = 0;
3069 times[1].tv_nsec = UTIME_OMIT;
3071 err = v9fs_co_utimensat(pdu, &fidp->path, times);
3076 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
3077 #ifndef CONFIG_WIN32
3078 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
3086 if (v9stat.name.size != 0) {
3087 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
3092 if (v9stat.length != -1) {
3093 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
3102 v9fs_stat_free(&v9stat);
3103 pdu_complete(pdu, err);
3106 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
3118 int32_t bsize_factor;
3121 * compute bsize factor based on host file system block size
3124 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
3125 if (!bsize_factor) {
3128 f_type = stbuf->f_type;
3129 f_bsize = stbuf->f_bsize;
3130 f_bsize *= bsize_factor;
3132 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3133 * adjust(divide) the number of blocks, free blocks and available
3134 * blocks by bsize factor
3136 f_blocks = stbuf->f_blocks/bsize_factor;
3137 f_bfree = stbuf->f_bfree/bsize_factor;
3138 f_bavail = stbuf->f_bavail/bsize_factor;
3139 f_files = stbuf->f_files;
3140 f_ffree = stbuf->f_ffree;
3142 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
3143 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
3144 f_namelen = stbuf->f_namelen;
3146 fsid_val = (unsigned int) stbuf->f_fsid.val[0] |
3147 (unsigned long long)stbuf->f_fsid.val[1] << 32;
3151 return pdu_marshal(pdu, offset, "ddqqqqqqd",
3152 f_type, f_bsize, f_blocks, f_bfree,
3153 f_bavail, f_files, f_ffree,
3154 fsid_val, f_namelen);
3157 static void coroutine_fn v9fs_statfs(void *opaque)
3159 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3164 struct statfs stbuf;
3165 V9fsPDU *pdu = opaque;
3166 V9fsState *s = pdu->s;
3168 retval = pdu_unmarshal(pdu, offset, "d", &fid);
3172 fidp = get_fid(pdu, fid);
3177 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
3181 retval = v9fs_fill_statfs(s, pdu, &stbuf);
3189 pdu_complete(pdu, retval);
3192 static void coroutine_fn v9fs_mknod(void *opaque)
3194 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3206 V9fsPDU *pdu = opaque;
3208 v9fs_string_init(&name);
3209 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
3210 &major, &minor, &gid);
3214 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
3216 if (name_is_illegal(name.data)) {
3221 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3226 fidp = get_fid(pdu, fid);
3231 #ifndef CONFIG_WIN32
3232 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
3233 makedev(major, minor), mode, &stbuf);
3240 stat_to_qid(&stbuf, &qid);
3241 err = pdu_marshal(pdu, offset, "Q", &qid);
3246 trace_v9fs_mknod_return(pdu->tag, pdu->id,
3247 qid.type, qid.version, qid.path);
3251 pdu_complete(pdu, err);
3252 v9fs_string_free(&name);
3256 * Implement posix byte range locking code
3257 * Server side handling of locking code is very simple, because 9p server in
3258 * QEMU can handle only one client. And most of the lock handling
3259 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3260 * do any thing in * qemu 9p server side lock code path.
3261 * So when a TLOCK request comes, always return success
3263 static void coroutine_fn v9fs_lock(void *opaque)
3265 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3271 int32_t fid, err = 0;
3272 V9fsPDU *pdu = opaque;
3274 status = P9_LOCK_ERROR;
3275 v9fs_string_init(&flock.client_id);
3276 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
3277 &flock.flags, &flock.start, &flock.length,
3278 &flock.proc_id, &flock.client_id);
3282 trace_v9fs_lock(pdu->tag, pdu->id, fid,
3283 flock.type, flock.start, flock.length);
3286 /* We support only block flag now (that too ignored currently) */
3287 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
3291 fidp = get_fid(pdu, fid);
3296 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3300 status = P9_LOCK_SUCCESS;
3304 err = pdu_marshal(pdu, offset, "b", status);
3308 trace_v9fs_lock_return(pdu->tag, pdu->id, status);
3309 pdu_complete(pdu, err);
3310 v9fs_string_free(&flock.client_id);
3314 * When a TGETLOCK request comes, always return success because all lock
3315 * handling is done by client's VFS layer.
3317 static void coroutine_fn v9fs_getlock(void *opaque)
3319 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3324 int32_t fid, err = 0;
3325 V9fsPDU *pdu = opaque;
3327 v9fs_string_init(&glock.client_id);
3328 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
3329 &glock.start, &glock.length, &glock.proc_id,
3334 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
3335 glock.type, glock.start, glock.length);
3337 fidp = get_fid(pdu, fid);
3342 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3346 glock.type = P9_LOCK_TYPE_UNLCK;
3347 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
3348 glock.start, glock.length, glock.proc_id,
3354 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
3355 glock.length, glock.proc_id);
3359 pdu_complete(pdu, err);
3360 v9fs_string_free(&glock.client_id);
3363 static void coroutine_fn v9fs_mkdir(void *opaque)
3365 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3366 V9fsPDU *pdu = opaque;
3377 v9fs_string_init(&name);
3378 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
3382 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
3384 if (name_is_illegal(name.data)) {
3389 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3394 fidp = get_fid(pdu, fid);
3399 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
3403 stat_to_qid(&stbuf, &qid);
3404 err = pdu_marshal(pdu, offset, "Q", &qid);
3409 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
3410 qid.type, qid.version, qid.path, err);
3414 pdu_complete(pdu, err);
3415 v9fs_string_free(&name);
3418 #ifndef CONFIG_WIN32
3419 static void coroutine_fn v9fs_xattrwalk(void *opaque)
3425 int32_t fid, newfid;
3426 V9fsFidState *file_fidp;
3427 V9fsFidState *xattr_fidp = NULL;
3428 V9fsPDU *pdu = opaque;
3429 V9fsState *s = pdu->s;
3431 v9fs_string_init(&name);
3432 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3436 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3438 file_fidp = get_fid(pdu, fid);
3439 if (file_fidp == NULL) {
3443 xattr_fidp = alloc_fid(s, newfid);
3444 if (xattr_fidp == NULL) {
3448 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3449 if (!v9fs_string_size(&name)) {
3451 * listxattr request. Get the size first
3453 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3456 clunk_fid(s, xattr_fidp->fid);
3460 * Read the xattr value
3462 xattr_fidp->fs.xattr.len = size;
3463 xattr_fidp->fid_type = P9_FID_XATTR;
3464 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3466 xattr_fidp->fs.xattr.value = g_malloc(size);
3467 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3468 xattr_fidp->fs.xattr.value,
3469 xattr_fidp->fs.xattr.len);
3471 clunk_fid(s, xattr_fidp->fid);
3475 err = pdu_marshal(pdu, offset, "q", size);
3482 * specific xattr fid. We check for xattr
3483 * presence also collect the xattr size
3485 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3489 clunk_fid(s, xattr_fidp->fid);
3493 * Read the xattr value
3495 xattr_fidp->fs.xattr.len = size;
3496 xattr_fidp->fid_type = P9_FID_XATTR;
3497 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3499 xattr_fidp->fs.xattr.value = g_malloc(size);
3500 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3501 &name, xattr_fidp->fs.xattr.value,
3502 xattr_fidp->fs.xattr.len);
3504 clunk_fid(s, xattr_fidp->fid);
3508 err = pdu_marshal(pdu, offset, "q", size);
3514 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3516 put_fid(pdu, file_fidp);
3518 put_fid(pdu, xattr_fidp);
3521 pdu_complete(pdu, err);
3522 v9fs_string_free(&name);
3525 static void coroutine_fn v9fs_xattrcreate(void *opaque)
3533 V9fsFidState *file_fidp;
3534 V9fsFidState *xattr_fidp;
3535 V9fsPDU *pdu = opaque;
3537 v9fs_string_init(&name);
3538 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3542 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3544 if (size > XATTR_SIZE_MAX) {
3549 file_fidp = get_fid(pdu, fid);
3550 if (file_fidp == NULL) {
3554 if (file_fidp->fid_type != P9_FID_NONE) {
3559 /* Make the file fid point to xattr */
3560 xattr_fidp = file_fidp;
3561 xattr_fidp->fid_type = P9_FID_XATTR;
3562 xattr_fidp->fs.xattr.copied_len = 0;
3563 xattr_fidp->fs.xattr.xattrwalk_fid = false;
3564 xattr_fidp->fs.xattr.len = size;
3565 xattr_fidp->fs.xattr.flags = flags;
3566 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3567 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3568 xattr_fidp->fs.xattr.value = g_malloc0(size);
3571 put_fid(pdu, file_fidp);
3573 pdu_complete(pdu, err);
3574 v9fs_string_free(&name);
3578 static void coroutine_fn v9fs_readlink(void *opaque)
3580 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3581 V9fsPDU *pdu = opaque;
3588 err = pdu_unmarshal(pdu, offset, "d", &fid);
3592 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
3593 fidp = get_fid(pdu, fid);
3599 v9fs_string_init(&target);
3600 err = v9fs_co_readlink(pdu, &fidp->path, &target);
3604 err = pdu_marshal(pdu, offset, "s", &target);
3606 v9fs_string_free(&target);
3610 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
3611 v9fs_string_free(&target);
3615 pdu_complete(pdu, err);
3618 static CoroutineEntry *pdu_co_handlers[] = {
3619 [P9_TREADDIR] = v9fs_readdir,
3620 [P9_TSTATFS] = v9fs_statfs,
3621 [P9_TGETATTR] = v9fs_getattr,
3622 [P9_TSETATTR] = v9fs_setattr,
3623 #ifndef CONFIG_WIN32
3624 [P9_TXATTRWALK] = v9fs_xattrwalk,
3625 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3627 [P9_TMKNOD] = v9fs_mknod,
3628 [P9_TRENAME] = v9fs_rename,
3629 [P9_TLOCK] = v9fs_lock,
3630 [P9_TGETLOCK] = v9fs_getlock,
3631 [P9_TRENAMEAT] = v9fs_renameat,
3632 [P9_TREADLINK] = v9fs_readlink,
3633 [P9_TUNLINKAT] = v9fs_unlinkat,
3634 [P9_TMKDIR] = v9fs_mkdir,
3635 [P9_TVERSION] = v9fs_version,
3636 [P9_TLOPEN] = v9fs_open,
3637 [P9_TATTACH] = v9fs_attach,
3638 [P9_TSTAT] = v9fs_stat,
3639 [P9_TWALK] = v9fs_walk,
3640 [P9_TCLUNK] = v9fs_clunk,
3641 [P9_TFSYNC] = v9fs_fsync,
3642 [P9_TOPEN] = v9fs_open,
3643 [P9_TREAD] = v9fs_read,
3645 [P9_TAUTH] = v9fs_auth,
3647 [P9_TFLUSH] = v9fs_flush,
3648 [P9_TLINK] = v9fs_link,
3649 [P9_TSYMLINK] = v9fs_symlink,
3650 [P9_TCREATE] = v9fs_create,
3651 [P9_TLCREATE] = v9fs_lcreate,
3652 [P9_TWRITE] = v9fs_write,
3653 [P9_TWSTAT] = v9fs_wstat,
3654 [P9_TREMOVE] = v9fs_remove,
3657 static void coroutine_fn v9fs_op_not_supp(void *opaque)
3659 WARN("[%d][%s] >> This operation is not supported.\n", __LINE__, __func__);
3660 V9fsPDU *pdu = opaque;
3661 pdu_complete(pdu, -EOPNOTSUPP);
3664 static void coroutine_fn v9fs_fs_ro(void *opaque)
3666 WARN("[%d][%s] >> This is the read-only operation.\n", __LINE__, __func__);
3667 V9fsPDU *pdu = opaque;
3668 pdu_complete(pdu, -EROFS);
3671 static inline bool is_read_only_op(V9fsPDU *pdu)
3698 void pdu_submit(V9fsPDU *pdu)
3701 CoroutineEntry *handler;
3702 V9fsState *s = pdu->s;
3704 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3705 (pdu_co_handlers[pdu->id] == NULL)) {
3706 handler = v9fs_op_not_supp;
3708 handler = pdu_co_handlers[pdu->id];
3711 if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3712 handler = v9fs_fs_ro;
3714 co = qemu_coroutine_create(handler, pdu);
3715 qemu_coroutine_enter(co);
3718 /* Returns 0 on success, 1 on failure. */
3719 int v9fs_device_realize_common(V9fsState *s, Error **errp)
3721 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3722 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
3729 /* initialize pdu allocator */
3730 QLIST_INIT(&s->free_list);
3731 QLIST_INIT(&s->active_list);
3732 for (i = 0; i < (MAX_REQ - 1); i++) {
3733 QLIST_INSERT_HEAD(&s->free_list, &v->pdus[i], next);
3738 v9fs_path_init(&path);
3740 fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
3743 /* We don't have a fsdev identified by fsdev_id */
3744 error_setg(errp, "9pfs device couldn't find fsdev with the "
3746 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
3750 if (!s->fsconf.tag) {
3751 /* we haven't specified a mount_tag */
3752 error_setg(errp, "fsdev with id %s needs mount_tag arguments",
3753 s->fsconf.fsdev_id);
3757 s->ctx.export_flags = fse->export_flags;
3758 s->ctx.fs_root = g_strdup(fse->path);
3759 s->ctx.exops.get_st_gen = NULL;
3760 len = strlen(s->fsconf.tag);
3761 if (len > MAX_TAG_LEN - 1) {
3762 error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
3763 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
3767 s->tag = g_strdup(s->fsconf.tag);
3773 qemu_co_rwlock_init(&s->rename_lock);
3775 if (s->ops->init(&s->ctx) < 0) {
3776 error_setg(errp, "9pfs Failed to initialize fs-driver with id:%s"
3777 " and export path:%s", s->fsconf.fsdev_id, s->ctx.fs_root);
3782 * Check details of export path, We need to use fs driver
3783 * call back to do that. Since we are in the init path, we don't
3784 * use co-routines here.
3786 #ifndef CONFIG_WIN32
3787 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
3789 if (s->ops->name_to_path(&s->ctx, NULL, "\\", &path) < 0) {
3792 "error in converting name to path %s", strerror(errno));
3795 if (s->ops->lstat(&s->ctx, &path, &stat)) {
3796 error_setg(errp, "share path %s does not exist", fse->path);
3798 } else if (!S_ISDIR(stat.st_mode)) {
3799 error_setg(errp, "share path %s is not a directory", fse->path);
3802 v9fs_path_free(&path);
3807 if (s->ops->cleanup && s->ctx.private) {
3808 s->ops->cleanup(&s->ctx);
3811 g_free(s->ctx.fs_root);
3812 v9fs_path_free(&path);
3817 void v9fs_device_unrealize_common(V9fsState *s, Error **errp)
3819 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3820 if (s->ops->cleanup) {
3821 s->ops->cleanup(&s->ctx);
3824 g_free(s->ctx.fs_root);
3827 typedef struct VirtfsCoResetData {
3830 } VirtfsCoResetData;
3832 static void coroutine_fn virtfs_co_reset(void *opaque)
3834 VirtfsCoResetData *data = opaque;
3836 virtfs_reset(&data->pdu);
3840 void v9fs_reset(V9fsState *s)
3842 VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
3845 while (!QLIST_EMPTY(&s->active_list)) {
3846 aio_poll(qemu_get_aio_context(), true);
3849 co = qemu_coroutine_create(virtfs_co_reset, &data);
3850 qemu_coroutine_enter(co);
3852 while (!data.done) {
3853 aio_poll(qemu_get_aio_context(), true);
3857 static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
3859 #ifndef CONFIG_WIN32
3861 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
3862 error_report("Failed to get the resource limit");
3865 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
3866 open_fd_rc = rlim.rlim_cur/2;
3868 open_fd_hw = 3696; // 4096 - 400
3869 open_fd_rc = 2048; // 4096 / 2