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;
60 #define O_DIRECT 040000 /* Direct disk access */
61 #define O_NOATIME 01000000 /* Do not set atime */
62 #ifndef XATTR_SIZE_MAX
63 #define XATTR_SIZE_MAX 65536
67 #include "../../tizen/src/debug_ch.h"
68 MULTI_DEBUG_CHANNEL(tizen, 9pfs);
72 static int open_fd_rc;
86 ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
92 ret = virtio_pdu_vmarshal(pdu, offset, fmt, ap);
98 ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
104 ret = virtio_pdu_vunmarshal(pdu, offset, fmt, ap);
110 static void pdu_push_and_notify(V9fsPDU *pdu)
112 virtio_9p_push_and_notify(pdu);
115 static int omode_to_uflags(int8_t mode)
138 if (mode & Oappend) {
149 struct dotl_openflag_map {
154 static int dotl_to_open_flags(int flags)
158 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
159 * and P9_DOTL_NOACCESS
161 int oflags = flags & O_ACCMODE;
163 struct dotl_openflag_map dotl_oflag_map[] = {
164 { P9_DOTL_CREATE, O_CREAT },
165 { P9_DOTL_EXCL, O_EXCL },
166 { P9_DOTL_NOCTTY , O_NOCTTY },
167 { P9_DOTL_TRUNC, O_TRUNC },
168 { P9_DOTL_APPEND, O_APPEND },
169 { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
170 { P9_DOTL_DSYNC, O_DSYNC },
171 { P9_DOTL_FASYNC, FASYNC },
172 { P9_DOTL_DIRECT, O_DIRECT },
173 { P9_DOTL_LARGEFILE, O_LARGEFILE },
174 { P9_DOTL_DIRECTORY, O_DIRECTORY },
175 { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
176 { P9_DOTL_NOATIME, O_NOATIME },
177 { P9_DOTL_SYNC, O_SYNC },
180 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
181 if (flags & dotl_oflag_map[i].dotl_flag) {
182 oflags |= dotl_oflag_map[i].open_flag;
189 void cred_init(FsCred *credp)
197 static int get_dotl_openflags(V9fsState *s, int oflags)
201 * Filter the client open flags
203 flags = dotl_to_open_flags(oflags);
204 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
206 * Ignore direct disk access hint until the server supports it.
212 void v9fs_path_init(V9fsPath *path)
218 void v9fs_path_free(V9fsPath *path)
226 void GCC_FMT_ATTR(2, 3)
227 v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
231 v9fs_path_free(path);
234 /* Bump the size for including terminating NULL */
235 path->size = g_vasprintf(&path->data, fmt, ap) + 1;
239 void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs)
242 lhs->data = g_malloc(rhs->size);
243 memcpy(lhs->data, rhs->data, rhs->size);
244 lhs->size = rhs->size;
247 int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
248 const char *name, V9fsPath *path)
250 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
252 err = s->ops->name_to_path(&s->ctx, dirpath, name, path); // local_name_to_path
260 * Return TRUE if s1 is an ancestor of s2.
262 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
263 * As a special case, We treat s1 as ancestor of s2 if they are same!
265 static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
267 if (!strncmp(s1->data, s2->data, s1->size - 1)) {
269 if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
271 if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '\\') {
279 static size_t v9fs_string_size(V9fsString *str)
285 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
286 static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
288 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
290 if (f->fid_type == P9_FID_FILE) {
291 if (f->fs.fd == -1) {
293 err = v9fs_co_open(pdu, f, f->open_flags);
294 } while (err == -EINTR && !pdu->cancelled);
296 } else if (f->fid_type == P9_FID_DIR) {
297 if (f->fs.dir.stream == NULL) {
299 err = v9fs_co_opendir(pdu, f);
300 } while (err == -EINTR && !pdu->cancelled);
306 static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid)
308 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
311 V9fsState *s = pdu->s;
313 for (f = s->fid_list; f; f = f->next) {
317 * Update the fid ref upfront so that
318 * we don't get reclaimed when we yield
323 * check whether we need to reopen the
324 * file. We might have closed the fd
325 * while trying to free up some file
328 err = v9fs_reopen_fid(pdu, f);
330 ERR("[%d][ >> %s]\n", __LINE__, __func__);
335 * Mark the fid as referenced so that the LRU
336 * reclaim won't close the file descriptor
338 f->flags |= FID_REFERENCED;
345 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
349 for (f = s->fid_list; f; f = f->next) {
350 /* If fid is already there return NULL */
353 ERR("[%d][ >> %s]\n", __LINE__, __func__);
357 f = g_malloc0(sizeof(V9fsFidState));
359 f->fid_type = P9_FID_NONE;
362 * Mark the fid as referenced so that the LRU
363 * reclaim won't close the file descriptor
365 f->flags |= FID_REFERENCED;
366 f->next = s->fid_list;
369 v9fs_readdir_init(&f->fs.dir);
370 v9fs_readdir_init(&f->fs_reclaim.dir);
376 static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
378 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
381 if (fidp->fs.xattr.xattrwalk_fid) {
382 /* getxattr/listxattr fid */
386 * if this is fid for setxattr. clunk should
387 * result in setxattr localcall
389 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
390 /* clunk after partial write */
394 if (fidp->fs.xattr.len) {
395 retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
396 fidp->fs.xattr.value,
398 fidp->fs.xattr.flags);
400 retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
403 v9fs_string_free(&fidp->fs.xattr.name);
405 g_free(fidp->fs.xattr.value);
410 static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
412 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
415 if (fidp->fid_type == P9_FID_FILE) {
416 /* If we reclaimed the fd no need to close */
417 if (fidp->fs.fd != -1) {
418 retval = v9fs_co_close(pdu, &fidp->fs);
420 } else if (fidp->fid_type == P9_FID_DIR) {
421 if (fidp->fs.dir.stream != NULL) {
422 retval = v9fs_co_closedir(pdu, &fidp->fs);
424 } else if (fidp->fid_type == P9_FID_XATTR) {
426 retval = v9fs_xattr_fid_clunk(pdu, fidp);
431 v9fs_path_free(&fidp->path);
436 static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
441 * Don't free the fid if it is in reclaim list
443 if (!fidp->ref && fidp->clunked) {
444 if (fidp->fid == pdu->s->root_fid) {
446 * if the clunked fid is root fid then we
447 * have unmounted the fs on the client side.
448 * delete the migration blocker. Ideally, this
449 * should be hooked to transport close notification
451 if (pdu->s->migration_blocker) {
452 migrate_del_blocker(pdu->s->migration_blocker);
453 error_free(pdu->s->migration_blocker);
454 pdu->s->migration_blocker = NULL;
457 return free_fid(pdu, fidp);
462 static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
464 V9fsFidState **fidpp, *fidp;
466 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
467 if ((*fidpp)->fid == fid) {
471 if (*fidpp == NULL) {
480 void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)
482 int reclaim_count = 0;
483 V9fsState *s = pdu->s;
484 V9fsFidState *f, *reclaim_list = NULL;
486 for (f = s->fid_list; f; f = f->next) {
488 * Unlink fids cannot be reclaimed. Check
489 * for them and skip them. Also skip fids
490 * currently being operated on.
492 if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
496 * if it is a recently referenced fid
497 * we leave the fid untouched and clear the
498 * reference bit. We come back to it later
499 * in the next iteration. (a simple LRU without
500 * moving list elements around)
502 if (f->flags & FID_REFERENCED) {
503 f->flags &= ~FID_REFERENCED;
507 * Add fids to reclaim list.
509 if (f->fid_type == P9_FID_FILE) {
510 if (f->fs.fd != -1) {
512 * Up the reference count so that
513 * a clunk request won't free this fid
516 f->rclm_lst = reclaim_list;
518 f->fs_reclaim.fd = f->fs.fd;
522 } else if (f->fid_type == P9_FID_DIR) {
523 if (f->fs.dir.stream != NULL) {
525 * Up the reference count so that
526 * a clunk request won't free this fid
529 f->rclm_lst = reclaim_list;
531 f->fs_reclaim.dir.stream = f->fs.dir.stream;
532 f->fs.dir.stream = NULL;
536 if (reclaim_count >= open_fd_rc) {
541 * Now close the fid in reclaim list. Free them if they
542 * are already clunked.
544 while (reclaim_list) {
546 reclaim_list = f->rclm_lst;
547 if (f->fid_type == P9_FID_FILE) {
548 v9fs_co_close(pdu, &f->fs_reclaim);
549 } else if (f->fid_type == P9_FID_DIR) {
550 v9fs_co_closedir(pdu, &f->fs_reclaim);
554 * Now drop the fid reference, free it
561 static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
563 TRACE("[%d][ >> %s]\n", __LINE__, __func__);
565 V9fsState *s = pdu->s;
566 V9fsFidState *fidp, head_fid;
568 head_fid.next = s->fid_list;
569 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
570 if (fidp->path.size != path->size) {
573 if (!memcmp(fidp->path.data, path->data, path->size)) {
574 /* Mark the fid non reclaimable. */
575 fidp->flags |= FID_NON_RECLAIMABLE;
577 /* reopen the file/dir if already closed */
578 err = v9fs_reopen_fid(pdu, fidp);
583 * Go back to head of fid list because
584 * the list could have got updated when
585 * switched to the worker thread
595 static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
597 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
598 V9fsState *s = pdu->s;
602 while (s->fid_list) {
604 s->fid_list = fidp->next;
614 #define P9_QID_TYPE_DIR 0x80
615 #define P9_QID_TYPE_SYMLINK 0x02
617 #define P9_STAT_MODE_DIR 0x80000000
618 #define P9_STAT_MODE_APPEND 0x40000000
619 #define P9_STAT_MODE_EXCL 0x20000000
620 #define P9_STAT_MODE_MOUNT 0x10000000
621 #define P9_STAT_MODE_AUTH 0x08000000
622 #define P9_STAT_MODE_TMP 0x04000000
623 #define P9_STAT_MODE_SYMLINK 0x02000000
624 #define P9_STAT_MODE_LINK 0x01000000
625 #define P9_STAT_MODE_DEVICE 0x00800000
626 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
627 #define P9_STAT_MODE_SOCKET 0x00100000
628 #define P9_STAT_MODE_SETUID 0x00080000
629 #define P9_STAT_MODE_SETGID 0x00040000
630 #define P9_STAT_MODE_SETVTX 0x00010000
632 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
633 P9_STAT_MODE_SYMLINK | \
634 P9_STAT_MODE_LINK | \
635 P9_STAT_MODE_DEVICE | \
636 P9_STAT_MODE_NAMED_PIPE | \
639 /* This is the algorithm from ufs in spfs */
640 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
644 memset(&qidp->path, 0, sizeof(qidp->path));
645 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
646 memcpy(&qidp->path, &stbuf->st_ino, size);
647 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
649 if (S_ISDIR(stbuf->st_mode)) {
650 qidp->type |= P9_QID_TYPE_DIR;
653 if (S_ISLNK(stbuf->st_mode)) {
655 if (((stbuf->st_mode) & S_IFMT) == 0xA000) {
657 qidp->type |= P9_QID_TYPE_SYMLINK;
661 static int coroutine_fn fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp,
664 TRACE("[%d][ >> %s]\n", __LINE__, __func__);
668 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
672 stat_to_qid(&stbuf, qidp);
676 V9fsPDU *pdu_alloc(V9fsState *s)
680 if (!QLIST_EMPTY(&s->free_list)) {
681 pdu = QLIST_FIRST(&s->free_list);
682 QLIST_REMOVE(pdu, next);
683 QLIST_INSERT_HEAD(&s->active_list, pdu, next);
688 void pdu_free(V9fsPDU *pdu)
690 V9fsState *s = pdu->s;
692 g_assert(!pdu->cancelled);
693 QLIST_REMOVE(pdu, next);
694 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
698 * We don't do error checking for pdu_marshal/unmarshal here
699 * because we always expect to have enough space to encode
702 static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
704 int8_t id = pdu->id + 1; /* Response */
705 V9fsState *s = pdu->s;
708 WARN("[%d][ >> %s]\n", __LINE__, __func__);
712 if (s->proto_version != V9FS_PROTO_2000L) {
715 str.data = strerror(err);
716 str.size = strlen(str.data);
718 len += pdu_marshal(pdu, len, "s", &str);
722 len += pdu_marshal(pdu, len, "d", err);
724 if (s->proto_version == V9FS_PROTO_2000L) {
727 trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
730 /* fill out the header */
731 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
733 /* keep these in sync */
737 pdu_push_and_notify(pdu);
739 /* Now wakeup anybody waiting in flush for this request */
740 if (!qemu_co_queue_next(&pdu->complete)) {
746 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
751 if (mode & P9_STAT_MODE_DIR) {
755 if (mode & P9_STAT_MODE_SYMLINK) {
758 if (mode & P9_STAT_MODE_SOCKET) {
761 if (mode & P9_STAT_MODE_NAMED_PIPE) {
764 if (mode & P9_STAT_MODE_DEVICE) {
765 if (extension->size && extension->data[0] == 'c') {
776 if (mode & P9_STAT_MODE_SETUID) {
779 if (mode & P9_STAT_MODE_SETGID) {
782 if (mode & P9_STAT_MODE_SETVTX) {
790 static int donttouch_stat(V9fsStat *stat)
792 if (stat->type == -1 &&
794 stat->qid.type == -1 &&
795 stat->qid.version == -1 &&
796 stat->qid.path == -1 &&
800 stat->length == -1 &&
807 stat->n_muid == -1) {
814 static void v9fs_stat_init(V9fsStat *stat)
816 v9fs_string_init(&stat->name);
817 v9fs_string_init(&stat->uid);
818 v9fs_string_init(&stat->gid);
819 v9fs_string_init(&stat->muid);
820 v9fs_string_init(&stat->extension);
823 static void v9fs_stat_free(V9fsStat *stat)
825 v9fs_string_free(&stat->name);
826 v9fs_string_free(&stat->uid);
827 v9fs_string_free(&stat->gid);
828 v9fs_string_free(&stat->muid);
829 v9fs_string_free(&stat->extension);
832 static uint32_t stat_to_v9mode(const struct stat *stbuf)
836 mode = stbuf->st_mode & 0777;
838 if (S_ISDIR(stbuf->st_mode)) {
839 mode |= P9_STAT_MODE_DIR;
842 if (S_ISLNK(stbuf->st_mode)) {
843 mode |= P9_STAT_MODE_SYMLINK;
846 if (S_ISSOCK(stbuf->st_mode)) {
847 mode |= P9_STAT_MODE_SOCKET;
850 if (S_ISFIFO(stbuf->st_mode)) {
851 mode |= P9_STAT_MODE_NAMED_PIPE;
854 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
855 mode |= P9_STAT_MODE_DEVICE;
858 if (stbuf->st_mode & S_ISUID) {
859 mode |= P9_STAT_MODE_SETUID;
862 if (stbuf->st_mode & S_ISGID) {
863 mode |= P9_STAT_MODE_SETGID;
866 if (stbuf->st_mode & S_ISVTX) {
867 mode |= P9_STAT_MODE_SETVTX;
874 static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *name,
875 const struct stat *stbuf,
878 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
882 memset(v9stat, 0, sizeof(*v9stat));
884 stat_to_qid(stbuf, &v9stat->qid);
885 v9stat->mode = stat_to_v9mode(stbuf);
886 v9stat->atime = stbuf->st_atime;
887 v9stat->mtime = stbuf->st_mtime;
888 v9stat->length = stbuf->st_size;
890 v9fs_string_free(&v9stat->uid);
891 v9fs_string_free(&v9stat->gid);
892 v9fs_string_free(&v9stat->muid);
894 v9stat->n_uid = stbuf->st_uid;
895 v9stat->n_gid = stbuf->st_gid;
898 v9fs_string_free(&v9stat->extension);
900 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
901 err = v9fs_co_readlink(pdu, name, &v9stat->extension);
905 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
906 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
907 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
908 major(stbuf->st_rdev), minor(stbuf->st_rdev));
909 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
910 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
911 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
914 str = strrchr(name->data, '/');
921 v9fs_string_sprintf(&v9stat->name, "%s", str);
924 v9fs_string_size(&v9stat->name) +
925 v9fs_string_size(&v9stat->uid) +
926 v9fs_string_size(&v9stat->gid) +
927 v9fs_string_size(&v9stat->muid) +
928 v9fs_string_size(&v9stat->extension);
932 #define P9_STATS_MODE 0x00000001ULL
933 #define P9_STATS_NLINK 0x00000002ULL
934 #define P9_STATS_UID 0x00000004ULL
935 #define P9_STATS_GID 0x00000008ULL
936 #define P9_STATS_RDEV 0x00000010ULL
937 #define P9_STATS_ATIME 0x00000020ULL
938 #define P9_STATS_MTIME 0x00000040ULL
939 #define P9_STATS_CTIME 0x00000080ULL
940 #define P9_STATS_INO 0x00000100ULL
941 #define P9_STATS_SIZE 0x00000200ULL
942 #define P9_STATS_BLOCKS 0x00000400ULL
944 #define P9_STATS_BTIME 0x00000800ULL
945 #define P9_STATS_GEN 0x00001000ULL
946 #define P9_STATS_DATA_VERSION 0x00002000ULL
948 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
949 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
952 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
953 V9fsStatDotl *v9lstat)
955 memset(v9lstat, 0, sizeof(*v9lstat));
957 v9lstat->st_mode = stbuf->st_mode;
958 v9lstat->st_nlink = stbuf->st_nlink;
959 v9lstat->st_uid = stbuf->st_uid;
960 v9lstat->st_gid = stbuf->st_gid;
961 v9lstat->st_rdev = stbuf->st_rdev;
962 v9lstat->st_size = stbuf->st_size;
964 v9lstat->st_blksize = stbuf->st_blksize;
965 v9lstat->st_blocks = stbuf->st_blocks;
967 v9lstat->st_blksize = hostBytesPerSector;
968 v9lstat->st_blocks = (uint64_t)(v9lstat->st_size / 512
969 + (v9lstat->st_size % 512 ? 1 : 0)); //round up
972 v9lstat->st_atime_sec = stbuf->st_atime;
973 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
974 v9lstat->st_mtime_sec = stbuf->st_mtime;
975 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
976 v9lstat->st_ctime_sec = stbuf->st_ctime;
977 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
980 v9lstat->st_atime_sec = stbuf->st_atimespec.tv_sec;
981 v9lstat->st_atime_nsec = stbuf->st_atimespec.tv_nsec;
982 v9lstat->st_mtime_sec = stbuf->st_mtimespec.tv_sec;
983 v9lstat->st_mtime_nsec = stbuf->st_mtimespec.tv_nsec;
984 v9lstat->st_ctime_sec = stbuf->st_ctimespec.tv_sec;
985 v9lstat->st_ctime_nsec = stbuf->st_ctimespec.tv_nsec;
987 v9lstat->st_atime_sec = stbuf->st_atime;
988 v9lstat->st_atime_nsec = 0;
989 v9lstat->st_mtime_sec = stbuf->st_mtime;
990 v9lstat->st_mtime_nsec = 0;
991 v9lstat->st_ctime_sec = stbuf->st_ctime;
992 v9lstat->st_ctime_nsec = 0;
996 /* Currently we only support BASIC fields in stat */
997 v9lstat->st_result_mask = P9_STATS_BASIC;
999 stat_to_qid(stbuf, &v9lstat->qid);
1002 static void print_sg(struct iovec *sg, int cnt)
1006 printf("sg[%d]: {", cnt);
1007 for (i = 0; i < cnt; i++) {
1011 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1016 /* Will call this only for path name based fid */
1017 static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
1020 v9fs_path_init(&str);
1021 v9fs_path_copy(&str, dst);
1022 v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
1023 v9fs_path_free(&str);
1026 static inline bool is_ro_export(FsContext *ctx)
1028 return ctx->export_flags & V9FS_RDONLY;
1031 static void coroutine_fn v9fs_version(void *opaque)
1033 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1035 V9fsPDU *pdu = opaque;
1036 V9fsState *s = pdu->s;
1040 v9fs_string_init(&version);
1041 err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1046 trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
1050 if (!strcmp(version.data, "9P2000.u")) {
1051 s->proto_version = V9FS_PROTO_2000U;
1052 } else if (!strcmp(version.data, "9P2000.L")) {
1053 s->proto_version = V9FS_PROTO_2000L;
1055 v9fs_string_sprintf(&version, "unknown");
1058 err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
1064 trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
1066 pdu_complete(pdu, offset);
1067 v9fs_string_free(&version);
1070 static void coroutine_fn v9fs_attach(void *opaque)
1072 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1073 V9fsPDU *pdu = opaque;
1074 V9fsState *s = pdu->s;
1075 int32_t fid, afid, n_uname;
1076 V9fsString uname, aname;
1082 v9fs_string_init(&uname);
1083 v9fs_string_init(&aname);
1084 err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
1085 &afid, &uname, &aname, &n_uname);
1089 trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
1091 fidp = alloc_fid(s, fid);
1096 fidp->uid = n_uname;
1097 #ifndef CONFIG_WIN32
1098 err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
1100 err = v9fs_co_name_to_path(pdu, NULL, "\\", &fidp->path);
1107 err = fid_to_qid(pdu, fidp, &qid);
1113 err = pdu_marshal(pdu, offset, "Q", &qid);
1119 memcpy(&s->root_qid, &qid, sizeof(qid));
1120 trace_v9fs_attach_return(pdu->tag, pdu->id,
1121 qid.type, qid.version, qid.path);
1123 * disable migration if we haven't done already.
1124 * attach could get called multiple times for the same export.
1126 if (!s->migration_blocker) {
1128 error_setg(&s->migration_blocker,
1129 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1130 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
1131 migrate_add_blocker(s->migration_blocker);
1136 pdu_complete(pdu, err);
1137 v9fs_string_free(&uname);
1138 v9fs_string_free(&aname);
1141 static void coroutine_fn v9fs_stat(void *opaque)
1143 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1150 V9fsPDU *pdu = opaque;
1152 err = pdu_unmarshal(pdu, offset, "d", &fid);
1156 trace_v9fs_stat(pdu->tag, pdu->id, fid);
1158 fidp = get_fid(pdu, fid);
1163 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1167 err = stat_to_v9stat(pdu, &fidp->path, &stbuf, &v9stat);
1171 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1173 v9fs_stat_free(&v9stat);
1176 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1177 v9stat.atime, v9stat.mtime, v9stat.length);
1179 v9fs_stat_free(&v9stat);
1183 pdu_complete(pdu, err);
1186 static void coroutine_fn v9fs_getattr(void *opaque)
1188 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1194 uint64_t request_mask;
1195 V9fsStatDotl v9stat_dotl;
1196 V9fsPDU *pdu = opaque;
1197 V9fsState *s = pdu->s;
1199 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1203 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
1205 fidp = get_fid(pdu, fid);
1211 * Currently we only support BASIC fields in stat, so there is no
1212 * need to look at request_mask.
1214 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1218 stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
1220 /* fill st_gen if requested and supported by underlying fs */
1221 if (request_mask & P9_STATS_GEN) {
1222 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
1225 /* we have valid st_gen: update result mask */
1226 v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1229 /* request cancelled, e.g. by Tflush */
1232 /* failed to get st_gen: not fatal, ignore */
1236 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1241 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1242 v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1243 v9stat_dotl.st_gid);
1247 pdu_complete(pdu, retval);
1250 /* Attribute flags */
1251 #define P9_ATTR_MODE (1 << 0)
1252 #define P9_ATTR_UID (1 << 1)
1253 #define P9_ATTR_GID (1 << 2)
1254 #define P9_ATTR_SIZE (1 << 3)
1255 #define P9_ATTR_ATIME (1 << 4)
1256 #define P9_ATTR_MTIME (1 << 5)
1257 #define P9_ATTR_CTIME (1 << 6)
1258 #define P9_ATTR_ATIME_SET (1 << 7)
1259 #define P9_ATTR_MTIME_SET (1 << 8)
1261 #define P9_ATTR_MASK 127
1263 static void coroutine_fn v9fs_setattr(void *opaque)
1265 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1271 V9fsPDU *pdu = opaque;
1273 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1278 fidp = get_fid(pdu, fid);
1283 if (v9iattr.valid & P9_ATTR_MODE) {
1284 #ifndef CONFIG_WIN32
1285 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1293 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1294 struct timespec times[2];
1295 if (v9iattr.valid & P9_ATTR_ATIME) {
1296 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1297 times[0].tv_sec = v9iattr.atime_sec;
1298 times[0].tv_nsec = v9iattr.atime_nsec;
1300 times[0].tv_nsec = UTIME_NOW;
1303 times[0].tv_nsec = UTIME_OMIT;
1305 if (v9iattr.valid & P9_ATTR_MTIME) {
1306 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1307 times[1].tv_sec = v9iattr.mtime_sec;
1308 times[1].tv_nsec = v9iattr.mtime_nsec;
1310 times[1].tv_nsec = UTIME_NOW;
1313 times[1].tv_nsec = UTIME_OMIT;
1315 err = v9fs_co_utimensat(pdu, &fidp->path, times);
1321 * If the only valid entry in iattr is ctime we can call
1322 * chown(-1,-1) to update the ctime of the file
1324 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1325 ((v9iattr.valid & P9_ATTR_CTIME)
1326 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1327 if (!(v9iattr.valid & P9_ATTR_UID)) {
1330 if (!(v9iattr.valid & P9_ATTR_GID)) {
1333 #ifndef CONFIG_WIN32
1334 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1343 if (v9iattr.valid & (P9_ATTR_SIZE)) {
1344 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1353 pdu_complete(pdu, err);
1356 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1362 err = pdu_marshal(pdu, offset, "w", nwnames);
1364 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1368 for (i = 0; i < nwnames; i++) {
1369 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1371 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1379 static bool name_is_illegal(const char *name)
1381 #ifndef CONFIG_WIN32
1382 return !*name || strchr(name, '/') != NULL;
1384 return !*name || strchr(name, '\\') != NULL;
1388 static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
1391 qid1->type != qid2->type ||
1392 qid1->version != qid2->version ||
1393 qid1->path != qid2->path;
1396 static void coroutine_fn v9fs_walk(void *opaque)
1398 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1400 V9fsQID *qids = NULL;
1402 V9fsPath dpath, path;
1406 int32_t fid, newfid;
1407 V9fsString *wnames = NULL;
1409 V9fsFidState *newfidp = NULL;
1410 V9fsPDU *pdu = opaque;
1411 V9fsState *s = pdu->s;
1414 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1416 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1417 pdu_complete(pdu, err);
1422 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1424 if (nwnames && nwnames <= P9_MAXWELEM) {
1425 wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
1426 qids = g_malloc0(sizeof(qids[0]) * nwnames);
1427 for (i = 0; i < nwnames; i++) {
1428 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1430 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1433 if (name_is_illegal(wnames[i].data)) {
1439 } else if (nwnames > P9_MAXWELEM) {
1440 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1444 fidp = get_fid(pdu, fid);
1446 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1451 v9fs_path_init(&dpath);
1452 v9fs_path_init(&path);
1454 err = fid_to_qid(pdu, fidp, &qid);
1460 * Both dpath and path initially poin to fidp.
1461 * Needed to handle request with nwnames == 0
1463 v9fs_path_copy(&dpath, &fidp->path);
1464 v9fs_path_copy(&path, &fidp->path);
1465 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1466 if (not_same_qid(&pdu->s->root_qid, &qid) ||
1467 strcmp("..", wnames[name_idx].data)) {
1468 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
1471 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1475 err = v9fs_co_lstat(pdu, &path, &stbuf);
1479 stat_to_qid(&stbuf, &qid);
1480 v9fs_path_copy(&dpath, &path);
1482 memcpy(&qids[name_idx], &qid, sizeof(qid));
1484 if (fid == newfid) {
1485 if (fidp->fid_type != P9_FID_NONE) {
1489 WARN("[%d][ >> %s]\n", __LINE__, __func__);
1490 v9fs_path_copy(&fidp->path, &path);
1492 newfidp = alloc_fid(s, newfid);
1493 if (newfidp == NULL) {
1494 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1498 newfidp->uid = fidp->uid;
1499 v9fs_path_copy(&newfidp->path, &path);
1501 err = v9fs_walk_marshal(pdu, nwnames, qids);
1502 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1506 put_fid(pdu, newfidp);
1508 v9fs_path_free(&dpath);
1509 v9fs_path_free(&path);
1511 pdu_complete(pdu, err);
1512 if (nwnames && nwnames <= P9_MAXWELEM) {
1513 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1514 v9fs_string_free(&wnames[name_idx]);
1521 static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
1523 struct statfs stbuf;
1525 V9fsState *s = pdu->s;
1528 * iounit should be multiples of f_bsize (host filesystem block size
1529 * and as well as less than (client msize - P9_IOHDRSZ))
1531 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1532 iounit = stbuf.f_bsize;
1533 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1536 iounit = s->msize - P9_IOHDRSZ;
1541 static void coroutine_fn v9fs_open(void *opaque)
1543 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1553 V9fsPDU *pdu = opaque;
1554 V9fsState *s = pdu->s;
1556 if (s->proto_version == V9FS_PROTO_2000L) {
1557 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1560 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1564 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1567 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1569 fidp = get_fid(pdu, fid);
1571 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1575 if (fidp->fid_type != P9_FID_NONE) {
1576 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1581 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1583 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1586 stat_to_qid(&stbuf, &qid);
1587 if (S_ISDIR(stbuf.st_mode)) {
1588 err = v9fs_co_opendir(pdu, fidp);
1590 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1593 fidp->fid_type = P9_FID_DIR;
1594 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1596 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1601 if (s->proto_version == V9FS_PROTO_2000L) {
1602 flags = get_dotl_openflags(s, mode);
1604 flags = omode_to_uflags(mode);
1606 if (is_ro_export(&s->ctx)) {
1607 if (mode & O_WRONLY || mode & O_RDWR ||
1608 mode & O_APPEND || mode & O_TRUNC) {
1609 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1614 err = v9fs_co_open(pdu, fidp, flags);
1616 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1619 fidp->fid_type = P9_FID_FILE;
1620 fidp->open_flags = flags;
1621 if (flags & O_EXCL) {
1623 * We let the host file system do O_EXCL check
1624 * We should not reclaim such fd
1626 fidp->flags |= FID_NON_RECLAIMABLE;
1628 iounit = get_iounit(pdu, &fidp->path);
1629 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1631 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1636 trace_v9fs_open_return(pdu->tag, pdu->id,
1637 qid.type, qid.version, qid.path, iounit);
1641 pdu_complete(pdu, err);
1644 static void coroutine_fn v9fs_lcreate(void *opaque)
1646 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1647 int32_t dfid, flags, mode;
1656 V9fsPDU *pdu = opaque;
1658 v9fs_string_init(&name);
1659 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1660 &name, &flags, &mode, &gid);
1662 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1665 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1667 if (name_is_illegal(name.data)) {
1672 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
1677 fidp = get_fid(pdu, dfid);
1679 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1684 flags = get_dotl_openflags(pdu->s, flags);
1685 err = v9fs_co_open2(pdu, fidp, &name, gid,
1686 flags | O_CREAT, mode, &stbuf);
1688 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1691 fidp->fid_type = P9_FID_FILE;
1692 fidp->open_flags = flags;
1693 if (flags & O_EXCL) {
1695 * We let the host file system do O_EXCL check
1696 * We should not reclaim such fd
1698 fidp->flags |= FID_NON_RECLAIMABLE;
1700 iounit = get_iounit(pdu, &fidp->path);
1701 stat_to_qid(&stbuf, &qid);
1702 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1704 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1708 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
1709 qid.type, qid.version, qid.path, iounit);
1713 pdu_complete(pdu, err);
1714 v9fs_string_free(&name);
1717 static void v9fs_fsync(void *opaque)
1719 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1725 V9fsPDU *pdu = opaque;
1727 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1729 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1732 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
1734 fidp = get_fid(pdu, fid);
1737 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1740 err = v9fs_co_fsync(pdu, fidp, datasync);
1746 pdu_complete(pdu, err);
1749 static void coroutine_fn v9fs_clunk(void *opaque)
1755 V9fsPDU *pdu = opaque;
1756 V9fsState *s = pdu->s;
1758 err = pdu_unmarshal(pdu, offset, "d", &fid);
1762 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
1764 fidp = clunk_fid(s, fid);
1770 * Bump the ref so that put_fid will
1774 err = put_fid(pdu, fidp);
1779 pdu_complete(pdu, err);
1782 #ifndef CONFIG_WIN32
1783 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1784 uint64_t off, uint32_t max_count)
1788 uint64_t read_count;
1789 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
1790 VirtQueueElement *elem = v->elems[pdu->idx];
1792 if (fidp->fs.xattr.len < off) {
1795 read_count = fidp->fs.xattr.len - off;
1797 if (read_count > max_count) {
1798 read_count = max_count;
1800 err = pdu_marshal(pdu, offset, "d", read_count);
1806 err = v9fs_pack(elem->in_sg, elem->in_num, offset,
1807 ((char *)fidp->fs.xattr.value) + off,
1817 static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1821 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1827 off_t saved_dir_pos;
1828 struct dirent *dent;
1830 /* save the directory position */
1831 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1832 if (saved_dir_pos < 0) {
1833 return saved_dir_pos;
1837 v9fs_path_init(&path);
1839 v9fs_readdir_lock(&fidp->fs.dir);
1841 err = v9fs_co_readdir(pdu, fidp, &dent);
1843 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1846 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
1848 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1851 err = v9fs_co_lstat(pdu, &path, &stbuf);
1853 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1856 err = stat_to_v9stat(pdu, &path, &stbuf, &v9stat);
1858 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1861 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1862 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1864 v9fs_readdir_unlock(&fidp->fs.dir);
1866 if ((len != (v9stat.size + 2)) || ((count + len) > max_count)) {
1867 /* Ran out of buffer. Set dir back to old position and return */
1868 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1869 v9fs_stat_free(&v9stat);
1870 v9fs_path_free(&path);
1874 v9fs_stat_free(&v9stat);
1875 v9fs_path_free(&path);
1877 saved_dir_pos = dent->d_off;
1879 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1883 v9fs_readdir_unlock(&fidp->fs.dir);
1885 v9fs_path_free(&path);
1893 * Create a QEMUIOVector for a sub-region of PDU iovecs
1895 * @qiov: uninitialized QEMUIOVector
1896 * @skip: number of bytes to skip from beginning of PDU
1897 * @size: number of bytes to include
1898 * @is_write: true - write, false - read
1900 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1901 * with qemu_iovec_destroy().
1903 static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1904 size_t skip, size_t size,
1911 virtio_init_iov_from_pdu(pdu, &iov, &niov, is_write);
1913 qemu_iovec_init_external(&elem, iov, niov);
1914 qemu_iovec_init(qiov, niov);
1915 qemu_iovec_concat(qiov, &elem, skip, size);
1918 static void coroutine_fn v9fs_read(void *opaque)
1920 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
1928 V9fsPDU *pdu = opaque;
1929 #ifndef CONFIG_WIN32
1930 V9fsState *s = pdu->s;
1933 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1935 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1938 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
1940 fidp = get_fid(pdu, fid);
1942 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1946 if (fidp->fid_type == P9_FID_DIR) {
1949 v9fs_co_rewinddir(pdu, fidp);
1951 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
1953 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1957 err = pdu_marshal(pdu, offset, "d", count);
1959 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1962 err += offset + count;
1963 } else if (fidp->fid_type == P9_FID_FILE) {
1964 QEMUIOVector qiov_full;
1968 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
1969 qemu_iovec_init(&qiov, qiov_full.niov);
1971 qemu_iovec_reset(&qiov);
1972 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
1974 print_sg(qiov.iov, qiov.niov);
1976 /* Loop in case of EINTR */
1978 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
1983 } while (len == -EINTR && !pdu->cancelled);
1985 /* IO error return the error */
1986 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1988 goto out_free_iovec;
1990 } while (count < max_count && len > 0);
1991 err = pdu_marshal(pdu, offset, "d", count);
1993 ERR("[%d][ >> %s]\n", __LINE__, __func__);
1994 goto out_free_iovec;
1996 err += offset + count;
1998 qemu_iovec_destroy(&qiov);
1999 qemu_iovec_destroy(&qiov_full);
2000 #ifndef CONFIG_WIN32
2001 } else if (fidp->fid_type == P9_FID_XATTR) {
2002 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
2005 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2008 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
2012 pdu_complete(pdu, err);
2015 static size_t v9fs_readdir_data_size(V9fsString *name)
2018 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
2019 * size of type (1) + size of name.size (2) + strlen(name.data)
2021 return 24 + v9fs_string_size(name);
2024 static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
2027 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2033 off_t saved_dir_pos;
2034 struct dirent *dent;
2035 #ifndef CONFIG_LINUX
2036 uint64_t d_offset = 0;
2039 /* save the directory position */
2040 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
2041 if (saved_dir_pos < 0) {
2042 return saved_dir_pos;
2046 v9fs_readdir_lock(&fidp->fs.dir);
2048 err = v9fs_co_readdir(pdu, fidp, &dent);
2052 v9fs_string_init(&name);
2053 v9fs_string_sprintf(&name, "%s", dent->d_name);
2054 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
2055 v9fs_readdir_unlock(&fidp->fs.dir);
2057 /* Ran out of buffer. Set dir back to old position and return */
2058 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2059 v9fs_string_free(&name);
2063 * Fill up just the path field of qid because the client uses
2064 * only that. To fill the entire qid structure we will have
2065 * to stat each dirent found, which is expensive
2067 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
2068 memcpy(&qid.path, &dent->d_ino, size);
2069 /* Fill the other fields with dummy values */
2073 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2075 len = pdu_marshal(pdu, 11 + count, "Qqbs",
2077 dent->d_type, &name);
2079 d_offset = v9fs_co_telldir(pdu, fidp);
2080 #ifndef CONFIG_WIN32
2081 len = pdu_marshal(pdu, 11 + count, "Qqbs",
2083 dent->d_type, &name);
2085 len = pdu_marshal(pdu, 11 + count, "Qqbs",
2087 WIN32_D_TYPE, &name);
2091 v9fs_readdir_unlock(&fidp->fs.dir);
2094 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2095 v9fs_string_free(&name);
2099 v9fs_string_free(&name);
2101 saved_dir_pos = dent->d_off;
2103 saved_dir_pos = d_offset;
2107 v9fs_readdir_unlock(&fidp->fs.dir);
2115 static void coroutine_fn v9fs_readdir(void *opaque)
2117 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2122 uint64_t initial_offset;
2125 V9fsPDU *pdu = opaque;
2127 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
2128 &initial_offset, &max_count);
2130 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2133 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
2135 fidp = get_fid(pdu, fid);
2137 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2141 if (!fidp->fs.dir.stream) {
2142 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2146 if (initial_offset == 0) {
2147 v9fs_co_rewinddir(pdu, fidp);
2149 v9fs_co_seekdir(pdu, fidp, initial_offset);
2151 count = v9fs_do_readdir(pdu, fidp, max_count);
2153 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2157 retval = pdu_marshal(pdu, offset, "d", count);
2159 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2162 retval += count + offset;
2163 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
2167 pdu_complete(pdu, retval);
2170 #ifndef CONFIG_WIN32
2171 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
2172 uint64_t off, uint32_t count,
2173 struct iovec *sg, int cnt)
2177 uint64_t write_count;
2181 if (fidp->fs.xattr.len < off) {
2185 write_count = fidp->fs.xattr.len - off;
2186 if (write_count > count) {
2187 write_count = count;
2189 err = pdu_marshal(pdu, offset, "d", write_count);
2194 fidp->fs.xattr.copied_len += write_count;
2196 * Now copy the content from sg list
2198 for (i = 0; i < cnt; i++) {
2199 if (write_count > sg[i].iov_len) {
2200 to_copy = sg[i].iov_len;
2202 to_copy = write_count;
2204 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
2205 /* updating vs->off since we are not using below */
2207 write_count -= to_copy;
2214 static void coroutine_fn v9fs_write(void *opaque)
2216 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2225 V9fsPDU *pdu = opaque;
2226 #ifndef CONFIG_WIN32
2227 V9fsState *s = pdu->s;
2229 QEMUIOVector qiov_full;
2232 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
2234 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2235 pdu_complete(pdu, err);
2239 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
2240 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
2242 fidp = get_fid(pdu, fid);
2244 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2248 if (fidp->fid_type == P9_FID_FILE) {
2249 if (fidp->fs.fd == -1) {
2250 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2254 } else if (fidp->fid_type == P9_FID_XATTR) {
2256 * setxattr operation
2258 #ifndef CONFIG_WIN32
2259 err = v9fs_xattr_write(s, pdu, fidp, off, count,
2260 qiov_full.iov, qiov_full.niov);
2264 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2268 qemu_iovec_init(&qiov, qiov_full.niov);
2270 qemu_iovec_reset(&qiov);
2271 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
2273 print_sg(qiov.iov, qiov.niov);
2275 /* Loop in case of EINTR */
2277 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
2282 } while (len == -EINTR && !pdu->cancelled);
2284 /* IO error return the error */
2285 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2289 } while (total < count && len > 0);
2292 err = pdu_marshal(pdu, offset, "d", total);
2294 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2298 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2300 qemu_iovec_destroy(&qiov);
2304 qemu_iovec_destroy(&qiov_full);
2305 pdu_complete(pdu, err);
2308 static void coroutine_fn v9fs_create(void *opaque)
2310 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2321 V9fsString extension;
2323 V9fsPDU *pdu = opaque;
2325 v9fs_path_init(&path);
2326 v9fs_string_init(&name);
2327 v9fs_string_init(&extension);
2328 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2329 &perm, &mode, &extension);
2331 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2334 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2336 if (name_is_illegal(name.data)) {
2341 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2346 fidp = get_fid(pdu, fid);
2348 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2352 if (perm & P9_STAT_MODE_DIR) {
2353 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2354 fidp->uid, -1, &stbuf);
2356 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2359 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2361 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2364 v9fs_path_copy(&fidp->path, &path);
2365 err = v9fs_co_opendir(pdu, fidp);
2367 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2370 fidp->fid_type = P9_FID_DIR;
2371 #ifndef CONFIG_WIN32
2372 } else if (perm & P9_STAT_MODE_SYMLINK) {
2373 err = v9fs_co_symlink(pdu, fidp, &name,
2374 extension.data, -1 , &stbuf);
2376 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2379 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2381 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2384 v9fs_path_copy(&fidp->path, &path);
2385 } else if (perm & P9_STAT_MODE_LINK) {
2386 int32_t ofid = atoi(extension.data);
2387 V9fsFidState *ofidp = get_fid(pdu, ofid);
2388 if (ofidp == NULL) {
2389 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2393 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2394 put_fid(pdu, ofidp);
2396 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2399 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2401 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2402 fidp->fid_type = P9_FID_NONE;
2405 v9fs_path_copy(&fidp->path, &path);
2406 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2408 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2409 fidp->fid_type = P9_FID_NONE;
2412 } else if (perm & P9_STAT_MODE_DEVICE) {
2414 uint32_t major, minor;
2417 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2418 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2435 nmode |= perm & 0777;
2436 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2437 makedev(major, minor), nmode, &stbuf);
2439 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2442 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2444 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2447 v9fs_path_copy(&fidp->path, &path);
2448 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2449 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2450 0, S_IFIFO | (perm & 0777), &stbuf);
2452 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2455 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2457 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2460 v9fs_path_copy(&fidp->path, &path);
2461 } else if (perm & P9_STAT_MODE_SOCKET) {
2462 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2463 0, S_IFSOCK | (perm & 0777), &stbuf);
2465 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2468 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2470 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2473 v9fs_path_copy(&fidp->path, &path);
2476 err = v9fs_co_open2(pdu, fidp, &name, -1,
2477 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2479 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2482 fidp->fid_type = P9_FID_FILE;
2483 fidp->open_flags = omode_to_uflags(mode);
2484 if (fidp->open_flags & O_EXCL) {
2486 * We let the host file system do O_EXCL check
2487 * We should not reclaim such fd
2489 fidp->flags |= FID_NON_RECLAIMABLE;
2492 iounit = get_iounit(pdu, &fidp->path);
2493 stat_to_qid(&stbuf, &qid);
2494 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2496 ERR("[%d][ >> %s]\n", __LINE__, __func__);
2500 trace_v9fs_create_return(pdu->tag, pdu->id,
2501 qid.type, qid.version, qid.path, iounit);
2505 pdu_complete(pdu, err);
2506 v9fs_string_free(&name);
2507 v9fs_string_free(&extension);
2508 v9fs_path_free(&path);
2511 static void coroutine_fn v9fs_symlink(void *opaque)
2513 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2514 V9fsPDU *pdu = opaque;
2517 V9fsFidState *dfidp;
2525 v9fs_string_init(&name);
2526 v9fs_string_init(&symname);
2527 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2531 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2533 if (name_is_illegal(name.data)) {
2538 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2543 dfidp = get_fid(pdu, dfid);
2544 if (dfidp == NULL) {
2548 #ifndef CONFIG_WIN32
2549 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2556 stat_to_qid(&stbuf, &qid);
2557 err = pdu_marshal(pdu, offset, "Q", &qid);
2562 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2563 qid.type, qid.version, qid.path);
2565 put_fid(pdu, dfidp);
2567 pdu_complete(pdu, err);
2568 v9fs_string_free(&name);
2569 v9fs_string_free(&symname);
2572 static void v9fs_flush(void *opaque)
2574 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2578 V9fsPDU *cancel_pdu;
2579 V9fsPDU *pdu = opaque;
2580 V9fsState *s = pdu->s;
2582 err = pdu_unmarshal(pdu, offset, "w", &tag);
2584 pdu_complete(pdu, err);
2587 trace_v9fs_flush(pdu->tag, pdu->id, tag);
2589 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2590 if (cancel_pdu->tag == tag) {
2595 cancel_pdu->cancelled = 1;
2597 * Wait for pdu to complete.
2599 qemu_co_queue_wait(&cancel_pdu->complete);
2600 cancel_pdu->cancelled = 0;
2601 pdu_free(cancel_pdu);
2603 pdu_complete(pdu, 7);
2606 static void coroutine_fn v9fs_link(void *opaque)
2608 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2609 V9fsPDU *pdu = opaque;
2610 int32_t dfid, oldfid;
2611 V9fsFidState *dfidp, *oldfidp;
2616 v9fs_string_init(&name);
2617 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2621 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2623 if (name_is_illegal(name.data)) {
2628 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2633 dfidp = get_fid(pdu, dfid);
2634 if (dfidp == NULL) {
2639 oldfidp = get_fid(pdu, oldfid);
2640 if (oldfidp == NULL) {
2644 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2648 put_fid(pdu, oldfidp);
2650 put_fid(pdu, dfidp);
2652 v9fs_string_free(&name);
2653 pdu_complete(pdu, err);
2656 /* Only works with path name based fid */
2657 static void coroutine_fn v9fs_remove(void *opaque)
2659 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2664 V9fsPDU *pdu = opaque;
2666 err = pdu_unmarshal(pdu, offset, "d", &fid);
2670 trace_v9fs_remove(pdu->tag, pdu->id, fid);
2672 fidp = get_fid(pdu, fid);
2677 /* if fs driver is not path based, return EOPNOTSUPP */
2678 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2683 * IF the file is unlinked, we cannot reopen
2684 * the file later. So don't reclaim fd
2686 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2690 err = v9fs_co_remove(pdu, &fidp->path);
2695 /* For TREMOVE we need to clunk the fid even on failed remove */
2696 clunk_fid(pdu->s, fidp->fid);
2699 pdu_complete(pdu, err);
2702 static void coroutine_fn v9fs_unlinkat(void *opaque)
2704 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2707 int32_t dfid, flags;
2710 V9fsFidState *dfidp;
2711 V9fsPDU *pdu = opaque;
2713 v9fs_string_init(&name);
2714 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2719 if (name_is_illegal(name.data)) {
2724 if (!strcmp(".", name.data)) {
2729 if (!strcmp("..", name.data)) {
2734 dfidp = get_fid(pdu, dfid);
2735 if (dfidp == NULL) {
2740 * IF the file is unlinked, we cannot reopen
2741 * the file later. So don't reclaim fd
2743 v9fs_path_init(&path);
2744 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2748 err = v9fs_mark_fids_unreclaim(pdu, &path);
2752 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, flags);
2757 put_fid(pdu, dfidp);
2758 v9fs_path_free(&path);
2760 pdu_complete(pdu, err);
2761 v9fs_string_free(&name);
2765 /* Only works with path name based fid */
2766 static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2770 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2774 V9fsFidState *tfidp;
2775 V9fsState *s = pdu->s;
2776 V9fsFidState *dirfidp = NULL;
2777 char *old_name, *new_name;
2779 v9fs_path_init(&new_path);
2780 if (newdirfid != -1) {
2781 dirfidp = get_fid(pdu, newdirfid);
2782 if (dirfidp == NULL) {
2786 if (fidp->fid_type != P9_FID_NONE) {
2790 v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2792 old_name = fidp->path.data;
2793 end = strrchr(old_name, '/');
2799 new_name = g_malloc0(end - old_name + name->size + 1);
2800 strncat(new_name, old_name, end - old_name);
2801 strncat(new_name + (end - old_name), name->data, name->size);
2802 v9fs_co_name_to_path(pdu, NULL, new_name, &new_path);
2805 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2810 * Fixup fid's pointing to the old name to
2811 * start pointing to the new name
2813 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2814 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2815 /* replace the name */
2816 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
2821 put_fid(pdu, dirfidp);
2823 v9fs_path_free(&new_path);
2828 /* Only works with path name based fid */
2829 static void coroutine_fn v9fs_rename(void *opaque)
2831 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2838 V9fsPDU *pdu = opaque;
2839 V9fsState *s = pdu->s;
2841 v9fs_string_init(&name);
2842 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2847 if (name_is_illegal(name.data)) {
2852 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2857 fidp = get_fid(pdu, fid);
2862 if (fidp->fid_type != P9_FID_NONE) {
2866 /* if fs driver is not path based, return EOPNOTSUPP */
2867 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2871 v9fs_path_write_lock(s);
2872 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
2873 v9fs_path_unlock(s);
2880 pdu_complete(pdu, err);
2881 v9fs_string_free(&name);
2884 static void coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2885 V9fsString *old_name,
2887 V9fsString *new_name)
2889 V9fsFidState *tfidp;
2890 V9fsPath oldpath, newpath;
2891 V9fsState *s = pdu->s;
2894 v9fs_path_init(&oldpath);
2895 v9fs_path_init(&newpath);
2896 v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
2897 v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2900 * Fixup fid's pointing to the old name to
2901 * start pointing to the new name
2903 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2904 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
2905 /* replace the name */
2906 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
2909 v9fs_path_free(&oldpath);
2910 v9fs_path_free(&newpath);
2913 static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2914 V9fsString *old_name,
2916 V9fsString *new_name)
2918 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2920 V9fsState *s = pdu->s;
2921 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2923 olddirfidp = get_fid(pdu, olddirfid);
2924 if (olddirfidp == NULL) {
2928 if (newdirfid != -1) {
2929 newdirfidp = get_fid(pdu, newdirfid);
2930 if (newdirfidp == NULL) {
2935 newdirfidp = get_fid(pdu, olddirfid);
2938 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2939 &newdirfidp->path, new_name);
2943 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
2944 /* Only for path based fid we need to do the below fixup */
2945 v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2946 &newdirfidp->path, new_name);
2950 put_fid(pdu, olddirfidp);
2953 put_fid(pdu, newdirfidp);
2958 static void coroutine_fn v9fs_renameat(void *opaque)
2960 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
2963 V9fsPDU *pdu = opaque;
2964 V9fsState *s = pdu->s;
2965 int32_t olddirfid, newdirfid;
2966 V9fsString old_name, new_name;
2968 v9fs_string_init(&old_name);
2969 v9fs_string_init(&new_name);
2970 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2971 &old_name, &newdirfid, &new_name);
2976 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
2981 if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
2982 !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
2987 v9fs_path_write_lock(s);
2988 err = v9fs_complete_renameat(pdu, olddirfid,
2989 &old_name, newdirfid, &new_name);
2990 v9fs_path_unlock(s);
2996 pdu_complete(pdu, err);
2997 v9fs_string_free(&old_name);
2998 v9fs_string_free(&new_name);
3001 static void coroutine_fn v9fs_wstat(void *opaque)
3003 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3011 V9fsPDU *pdu = opaque;
3013 v9fs_stat_init(&v9stat);
3014 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
3018 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
3019 v9stat.mode, v9stat.atime, v9stat.mtime);
3021 fidp = get_fid(pdu, fid);
3026 /* do we need to sync the file? */
3027 if (donttouch_stat(&v9stat)) {
3028 err = v9fs_co_fsync(pdu, fidp, 0);
3031 if (v9stat.mode != -1) {
3033 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
3037 v9_mode = stat_to_v9mode(&stbuf);
3038 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
3039 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
3040 /* Attempting to change the type */
3044 #ifndef CONFIG_WIN32
3045 err = v9fs_co_chmod(pdu, &fidp->path,
3046 v9mode_to_mode(v9stat.mode,
3047 &v9stat.extension));
3055 if (v9stat.mtime != -1 || v9stat.atime != -1) {
3056 struct timespec times[2];
3057 if (v9stat.atime != -1) {
3058 times[0].tv_sec = v9stat.atime;
3059 times[0].tv_nsec = 0;
3061 times[0].tv_nsec = UTIME_OMIT;
3063 if (v9stat.mtime != -1) {
3064 times[1].tv_sec = v9stat.mtime;
3065 times[1].tv_nsec = 0;
3067 times[1].tv_nsec = UTIME_OMIT;
3069 err = v9fs_co_utimensat(pdu, &fidp->path, times);
3074 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
3075 #ifndef CONFIG_WIN32
3076 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
3084 if (v9stat.name.size != 0) {
3085 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
3090 if (v9stat.length != -1) {
3091 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
3100 v9fs_stat_free(&v9stat);
3101 pdu_complete(pdu, err);
3104 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
3116 int32_t bsize_factor;
3119 * compute bsize factor based on host file system block size
3122 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
3123 if (!bsize_factor) {
3126 f_type = stbuf->f_type;
3127 f_bsize = stbuf->f_bsize;
3128 f_bsize *= bsize_factor;
3130 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3131 * adjust(divide) the number of blocks, free blocks and available
3132 * blocks by bsize factor
3134 f_blocks = stbuf->f_blocks/bsize_factor;
3135 f_bfree = stbuf->f_bfree/bsize_factor;
3136 f_bavail = stbuf->f_bavail/bsize_factor;
3137 f_files = stbuf->f_files;
3138 f_ffree = stbuf->f_ffree;
3140 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
3141 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
3142 f_namelen = stbuf->f_namelen;
3144 fsid_val = (unsigned int) stbuf->f_fsid.val[0] |
3145 (unsigned long long)stbuf->f_fsid.val[1] << 32;
3149 return pdu_marshal(pdu, offset, "ddqqqqqqd",
3150 f_type, f_bsize, f_blocks, f_bfree,
3151 f_bavail, f_files, f_ffree,
3152 fsid_val, f_namelen);
3155 static void coroutine_fn v9fs_statfs(void *opaque)
3157 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3162 struct statfs stbuf;
3163 V9fsPDU *pdu = opaque;
3164 V9fsState *s = pdu->s;
3166 retval = pdu_unmarshal(pdu, offset, "d", &fid);
3170 fidp = get_fid(pdu, fid);
3175 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
3179 retval = v9fs_fill_statfs(s, pdu, &stbuf);
3187 pdu_complete(pdu, retval);
3190 static void coroutine_fn v9fs_mknod(void *opaque)
3192 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3204 V9fsPDU *pdu = opaque;
3206 v9fs_string_init(&name);
3207 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
3208 &major, &minor, &gid);
3212 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
3214 if (name_is_illegal(name.data)) {
3219 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3224 fidp = get_fid(pdu, fid);
3229 #ifndef CONFIG_WIN32
3230 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
3231 makedev(major, minor), mode, &stbuf);
3238 stat_to_qid(&stbuf, &qid);
3239 err = pdu_marshal(pdu, offset, "Q", &qid);
3244 trace_v9fs_mknod_return(pdu->tag, pdu->id,
3245 qid.type, qid.version, qid.path);
3249 pdu_complete(pdu, err);
3250 v9fs_string_free(&name);
3254 * Implement posix byte range locking code
3255 * Server side handling of locking code is very simple, because 9p server in
3256 * QEMU can handle only one client. And most of the lock handling
3257 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3258 * do any thing in * qemu 9p server side lock code path.
3259 * So when a TLOCK request comes, always return success
3261 static void coroutine_fn v9fs_lock(void *opaque)
3263 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3269 int32_t fid, err = 0;
3270 V9fsPDU *pdu = opaque;
3272 status = P9_LOCK_ERROR;
3273 v9fs_string_init(&flock.client_id);
3274 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
3275 &flock.flags, &flock.start, &flock.length,
3276 &flock.proc_id, &flock.client_id);
3280 trace_v9fs_lock(pdu->tag, pdu->id, fid,
3281 flock.type, flock.start, flock.length);
3284 /* We support only block flag now (that too ignored currently) */
3285 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
3289 fidp = get_fid(pdu, fid);
3294 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3298 status = P9_LOCK_SUCCESS;
3302 err = pdu_marshal(pdu, offset, "b", status);
3306 trace_v9fs_lock_return(pdu->tag, pdu->id, status);
3307 pdu_complete(pdu, err);
3308 v9fs_string_free(&flock.client_id);
3312 * When a TGETLOCK request comes, always return success because all lock
3313 * handling is done by client's VFS layer.
3315 static void coroutine_fn v9fs_getlock(void *opaque)
3317 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3322 int32_t fid, err = 0;
3323 V9fsPDU *pdu = opaque;
3325 v9fs_string_init(&glock.client_id);
3326 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
3327 &glock.start, &glock.length, &glock.proc_id,
3332 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
3333 glock.type, glock.start, glock.length);
3335 fidp = get_fid(pdu, fid);
3340 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3344 glock.type = P9_LOCK_TYPE_UNLCK;
3345 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
3346 glock.start, glock.length, glock.proc_id,
3352 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
3353 glock.length, glock.proc_id);
3357 pdu_complete(pdu, err);
3358 v9fs_string_free(&glock.client_id);
3361 static void coroutine_fn v9fs_mkdir(void *opaque)
3363 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3364 V9fsPDU *pdu = opaque;
3375 v9fs_string_init(&name);
3376 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
3380 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
3382 if (name_is_illegal(name.data)) {
3387 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3392 fidp = get_fid(pdu, fid);
3397 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
3401 stat_to_qid(&stbuf, &qid);
3402 err = pdu_marshal(pdu, offset, "Q", &qid);
3407 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
3408 qid.type, qid.version, qid.path, err);
3412 pdu_complete(pdu, err);
3413 v9fs_string_free(&name);
3416 #ifndef CONFIG_WIN32
3417 static void coroutine_fn v9fs_xattrwalk(void *opaque)
3423 int32_t fid, newfid;
3424 V9fsFidState *file_fidp;
3425 V9fsFidState *xattr_fidp = NULL;
3426 V9fsPDU *pdu = opaque;
3427 V9fsState *s = pdu->s;
3429 v9fs_string_init(&name);
3430 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3434 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3436 file_fidp = get_fid(pdu, fid);
3437 if (file_fidp == NULL) {
3441 xattr_fidp = alloc_fid(s, newfid);
3442 if (xattr_fidp == NULL) {
3446 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3447 if (!v9fs_string_size(&name)) {
3449 * listxattr request. Get the size first
3451 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3454 clunk_fid(s, xattr_fidp->fid);
3458 * Read the xattr value
3460 xattr_fidp->fs.xattr.len = size;
3461 xattr_fidp->fid_type = P9_FID_XATTR;
3462 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3464 xattr_fidp->fs.xattr.value = g_malloc(size);
3465 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3466 xattr_fidp->fs.xattr.value,
3467 xattr_fidp->fs.xattr.len);
3469 clunk_fid(s, xattr_fidp->fid);
3473 err = pdu_marshal(pdu, offset, "q", size);
3480 * specific xattr fid. We check for xattr
3481 * presence also collect the xattr size
3483 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3487 clunk_fid(s, xattr_fidp->fid);
3491 * Read the xattr value
3493 xattr_fidp->fs.xattr.len = size;
3494 xattr_fidp->fid_type = P9_FID_XATTR;
3495 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3497 xattr_fidp->fs.xattr.value = g_malloc(size);
3498 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3499 &name, xattr_fidp->fs.xattr.value,
3500 xattr_fidp->fs.xattr.len);
3502 clunk_fid(s, xattr_fidp->fid);
3506 err = pdu_marshal(pdu, offset, "q", size);
3512 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3514 put_fid(pdu, file_fidp);
3516 put_fid(pdu, xattr_fidp);
3519 pdu_complete(pdu, err);
3520 v9fs_string_free(&name);
3523 static void coroutine_fn v9fs_xattrcreate(void *opaque)
3531 V9fsFidState *file_fidp;
3532 V9fsFidState *xattr_fidp;
3533 V9fsPDU *pdu = opaque;
3535 v9fs_string_init(&name);
3536 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3540 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3542 if (size > XATTR_SIZE_MAX) {
3547 file_fidp = get_fid(pdu, fid);
3548 if (file_fidp == NULL) {
3552 if (file_fidp->fid_type != P9_FID_NONE) {
3557 /* Make the file fid point to xattr */
3558 xattr_fidp = file_fidp;
3559 xattr_fidp->fid_type = P9_FID_XATTR;
3560 xattr_fidp->fs.xattr.copied_len = 0;
3561 xattr_fidp->fs.xattr.xattrwalk_fid = false;
3562 xattr_fidp->fs.xattr.len = size;
3563 xattr_fidp->fs.xattr.flags = flags;
3564 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3565 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3566 xattr_fidp->fs.xattr.value = g_malloc0(size);
3569 put_fid(pdu, file_fidp);
3571 pdu_complete(pdu, err);
3572 v9fs_string_free(&name);
3576 static void coroutine_fn v9fs_readlink(void *opaque)
3578 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3579 V9fsPDU *pdu = opaque;
3586 err = pdu_unmarshal(pdu, offset, "d", &fid);
3590 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
3591 fidp = get_fid(pdu, fid);
3597 v9fs_string_init(&target);
3598 err = v9fs_co_readlink(pdu, &fidp->path, &target);
3602 err = pdu_marshal(pdu, offset, "s", &target);
3604 v9fs_string_free(&target);
3608 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
3609 v9fs_string_free(&target);
3613 pdu_complete(pdu, err);
3616 static CoroutineEntry *pdu_co_handlers[] = {
3617 [P9_TREADDIR] = v9fs_readdir,
3618 [P9_TSTATFS] = v9fs_statfs,
3619 [P9_TGETATTR] = v9fs_getattr,
3620 [P9_TSETATTR] = v9fs_setattr,
3621 #ifndef CONFIG_WIN32
3622 [P9_TXATTRWALK] = v9fs_xattrwalk,
3623 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3625 [P9_TMKNOD] = v9fs_mknod,
3626 [P9_TRENAME] = v9fs_rename,
3627 [P9_TLOCK] = v9fs_lock,
3628 [P9_TGETLOCK] = v9fs_getlock,
3629 [P9_TRENAMEAT] = v9fs_renameat,
3630 [P9_TREADLINK] = v9fs_readlink,
3631 [P9_TUNLINKAT] = v9fs_unlinkat,
3632 [P9_TMKDIR] = v9fs_mkdir,
3633 [P9_TVERSION] = v9fs_version,
3634 [P9_TLOPEN] = v9fs_open,
3635 [P9_TATTACH] = v9fs_attach,
3636 [P9_TSTAT] = v9fs_stat,
3637 [P9_TWALK] = v9fs_walk,
3638 [P9_TCLUNK] = v9fs_clunk,
3639 [P9_TFSYNC] = v9fs_fsync,
3640 [P9_TOPEN] = v9fs_open,
3641 [P9_TREAD] = v9fs_read,
3643 [P9_TAUTH] = v9fs_auth,
3645 [P9_TFLUSH] = v9fs_flush,
3646 [P9_TLINK] = v9fs_link,
3647 [P9_TSYMLINK] = v9fs_symlink,
3648 [P9_TCREATE] = v9fs_create,
3649 [P9_TLCREATE] = v9fs_lcreate,
3650 [P9_TWRITE] = v9fs_write,
3651 [P9_TWSTAT] = v9fs_wstat,
3652 [P9_TREMOVE] = v9fs_remove,
3655 static void coroutine_fn v9fs_op_not_supp(void *opaque)
3657 WARN("[%d][%s] >> This operation is not supported.\n", __LINE__, __func__);
3658 V9fsPDU *pdu = opaque;
3659 pdu_complete(pdu, -EOPNOTSUPP);
3662 static void coroutine_fn v9fs_fs_ro(void *opaque)
3664 WARN("[%d][%s] >> This is the read-only operation.\n", __LINE__, __func__);
3665 V9fsPDU *pdu = opaque;
3666 pdu_complete(pdu, -EROFS);
3669 static inline bool is_read_only_op(V9fsPDU *pdu)
3696 void pdu_submit(V9fsPDU *pdu)
3699 CoroutineEntry *handler;
3700 V9fsState *s = pdu->s;
3702 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3703 (pdu_co_handlers[pdu->id] == NULL)) {
3704 handler = v9fs_op_not_supp;
3706 handler = pdu_co_handlers[pdu->id];
3709 if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3710 handler = v9fs_fs_ro;
3712 co = qemu_coroutine_create(handler, pdu);
3713 qemu_coroutine_enter(co);
3716 /* Returns 0 on success, 1 on failure. */
3717 int v9fs_device_realize_common(V9fsState *s, Error **errp)
3719 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3720 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
3727 /* initialize pdu allocator */
3728 QLIST_INIT(&s->free_list);
3729 QLIST_INIT(&s->active_list);
3730 for (i = 0; i < (MAX_REQ - 1); i++) {
3731 QLIST_INSERT_HEAD(&s->free_list, &v->pdus[i], next);
3736 v9fs_path_init(&path);
3738 fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
3741 /* We don't have a fsdev identified by fsdev_id */
3742 error_setg(errp, "9pfs device couldn't find fsdev with the "
3744 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
3748 if (!s->fsconf.tag) {
3749 /* we haven't specified a mount_tag */
3750 error_setg(errp, "fsdev with id %s needs mount_tag arguments",
3751 s->fsconf.fsdev_id);
3755 s->ctx.export_flags = fse->export_flags;
3756 s->ctx.fs_root = g_strdup(fse->path);
3757 s->ctx.exops.get_st_gen = NULL;
3758 len = strlen(s->fsconf.tag);
3759 if (len > MAX_TAG_LEN - 1) {
3760 error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
3761 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
3765 s->tag = g_strdup(s->fsconf.tag);
3771 qemu_co_rwlock_init(&s->rename_lock);
3773 if (s->ops->init(&s->ctx) < 0) {
3774 error_setg(errp, "9pfs Failed to initialize fs-driver with id:%s"
3775 " and export path:%s", s->fsconf.fsdev_id, s->ctx.fs_root);
3780 * Check details of export path, We need to use fs driver
3781 * call back to do that. Since we are in the init path, we don't
3782 * use co-routines here.
3784 #ifndef CONFIG_WIN32
3785 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
3787 if (s->ops->name_to_path(&s->ctx, NULL, "\\", &path) < 0) {
3790 "error in converting name to path %s", strerror(errno));
3793 if (s->ops->lstat(&s->ctx, &path, &stat)) {
3794 error_setg(errp, "share path %s does not exist", fse->path);
3796 } else if (!S_ISDIR(stat.st_mode)) {
3797 error_setg(errp, "share path %s is not a directory", fse->path);
3800 v9fs_path_free(&path);
3805 if (s->ops->cleanup && s->ctx.private) {
3806 s->ops->cleanup(&s->ctx);
3809 g_free(s->ctx.fs_root);
3810 v9fs_path_free(&path);
3815 void v9fs_device_unrealize_common(V9fsState *s, Error **errp)
3817 TRACE("[%d][ Enter >> %s]\n", __LINE__, __func__);
3818 if (s->ops->cleanup) {
3819 s->ops->cleanup(&s->ctx);
3822 g_free(s->ctx.fs_root);
3825 typedef struct VirtfsCoResetData {
3828 } VirtfsCoResetData;
3830 static void coroutine_fn virtfs_co_reset(void *opaque)
3832 VirtfsCoResetData *data = opaque;
3834 virtfs_reset(&data->pdu);
3838 void v9fs_reset(V9fsState *s)
3840 VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
3843 while (!QLIST_EMPTY(&s->active_list)) {
3844 aio_poll(qemu_get_aio_context(), true);
3847 co = qemu_coroutine_create(virtfs_co_reset, &data);
3848 qemu_coroutine_enter(co);
3850 while (!data.done) {
3851 aio_poll(qemu_get_aio_context(), true);
3855 static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
3857 #ifndef CONFIG_WIN32
3859 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
3860 error_report("Failed to get the resource limit");
3863 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
3864 open_fd_rc = rlim.rlim_cur/2;
3866 open_fd_hw = 3696; // 4096 - 400
3867 open_fd_rc = 2048; // 4096 / 2