4 * Copyright IBM, Corp. 2010
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include <glib/gprintf.h>
16 #include "hw/virtio/virtio.h"
17 #include "qapi/error.h"
18 #include "qemu/error-report.h"
20 #include "qemu/sockets.h"
21 #include "virtio-9p.h"
22 #include "fsdev/qemu-fsdev.h"
26 #include "migration/blocker.h"
27 #include "sysemu/qtest.h"
31 static int open_fd_rc;
45 static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
51 ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap);
57 static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
63 ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap);
69 static int omode_to_uflags(int8_t mode)
103 typedef struct DotlOpenflagMap {
108 static int dotl_to_open_flags(int flags)
112 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
113 * and P9_DOTL_NOACCESS
115 int oflags = flags & O_ACCMODE;
117 DotlOpenflagMap dotl_oflag_map[] = {
118 { P9_DOTL_CREATE, O_CREAT },
119 { P9_DOTL_EXCL, O_EXCL },
120 { P9_DOTL_NOCTTY , O_NOCTTY },
121 { P9_DOTL_TRUNC, O_TRUNC },
122 { P9_DOTL_APPEND, O_APPEND },
123 { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
124 { P9_DOTL_DSYNC, O_DSYNC },
125 { P9_DOTL_FASYNC, FASYNC },
126 { P9_DOTL_DIRECT, O_DIRECT },
127 { P9_DOTL_LARGEFILE, O_LARGEFILE },
128 { P9_DOTL_DIRECTORY, O_DIRECTORY },
129 { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
130 { P9_DOTL_NOATIME, O_NOATIME },
131 { P9_DOTL_SYNC, O_SYNC },
134 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
135 if (flags & dotl_oflag_map[i].dotl_flag) {
136 oflags |= dotl_oflag_map[i].open_flag;
143 void cred_init(FsCred *credp)
151 static int get_dotl_openflags(V9fsState *s, int oflags)
155 * Filter the client open flags
157 flags = dotl_to_open_flags(oflags);
158 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
160 * Ignore direct disk access hint until the server supports it.
166 void v9fs_path_init(V9fsPath *path)
172 void v9fs_path_free(V9fsPath *path)
180 void GCC_FMT_ATTR(2, 3)
181 v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
185 v9fs_path_free(path);
188 /* Bump the size for including terminating NULL */
189 path->size = g_vasprintf(&path->data, fmt, ap) + 1;
193 void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs)
196 lhs->data = g_malloc(rhs->size);
197 memcpy(lhs->data, rhs->data, rhs->size);
198 lhs->size = rhs->size;
201 int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
202 const char *name, V9fsPath *path)
205 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
213 * Return TRUE if s1 is an ancestor of s2.
215 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
216 * As a special case, We treat s1 as ancestor of s2 if they are same!
218 static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
220 if (!strncmp(s1->data, s2->data, s1->size - 1)) {
221 if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
228 static size_t v9fs_string_size(V9fsString *str)
234 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
235 static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
238 if (f->fid_type == P9_FID_FILE) {
239 if (f->fs.fd == -1) {
241 err = v9fs_co_open(pdu, f, f->open_flags);
242 } while (err == -EINTR && !pdu->cancelled);
244 } else if (f->fid_type == P9_FID_DIR) {
245 if (f->fs.dir.stream == NULL) {
247 err = v9fs_co_opendir(pdu, f);
248 } while (err == -EINTR && !pdu->cancelled);
254 static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid)
258 V9fsState *s = pdu->s;
260 for (f = s->fid_list; f; f = f->next) {
264 * Update the fid ref upfront so that
265 * we don't get reclaimed when we yield
270 * check whether we need to reopen the
271 * file. We might have closed the fd
272 * while trying to free up some file
275 err = v9fs_reopen_fid(pdu, f);
281 * Mark the fid as referenced so that the LRU
282 * reclaim won't close the file descriptor
284 f->flags |= FID_REFERENCED;
291 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
295 for (f = s->fid_list; f; f = f->next) {
296 /* If fid is already there return NULL */
302 f = g_malloc0(sizeof(V9fsFidState));
304 f->fid_type = P9_FID_NONE;
307 * Mark the fid as referenced so that the LRU
308 * reclaim won't close the file descriptor
310 f->flags |= FID_REFERENCED;
311 f->next = s->fid_list;
314 v9fs_readdir_init(&f->fs.dir);
315 v9fs_readdir_init(&f->fs_reclaim.dir);
320 static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
324 if (fidp->fs.xattr.xattrwalk_fid) {
325 /* getxattr/listxattr fid */
329 * if this is fid for setxattr. clunk should
330 * result in setxattr localcall
332 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
333 /* clunk after partial write */
337 if (fidp->fs.xattr.len) {
338 retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
339 fidp->fs.xattr.value,
341 fidp->fs.xattr.flags);
343 retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
346 v9fs_string_free(&fidp->fs.xattr.name);
348 g_free(fidp->fs.xattr.value);
352 static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
356 if (fidp->fid_type == P9_FID_FILE) {
357 /* If we reclaimed the fd no need to close */
358 if (fidp->fs.fd != -1) {
359 retval = v9fs_co_close(pdu, &fidp->fs);
361 } else if (fidp->fid_type == P9_FID_DIR) {
362 if (fidp->fs.dir.stream != NULL) {
363 retval = v9fs_co_closedir(pdu, &fidp->fs);
365 } else if (fidp->fid_type == P9_FID_XATTR) {
366 retval = v9fs_xattr_fid_clunk(pdu, fidp);
368 v9fs_path_free(&fidp->path);
373 static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
378 * Don't free the fid if it is in reclaim list
380 if (!fidp->ref && fidp->clunked) {
381 if (fidp->fid == pdu->s->root_fid) {
383 * if the clunked fid is root fid then we
384 * have unmounted the fs on the client side.
385 * delete the migration blocker. Ideally, this
386 * should be hooked to transport close notification
388 if (pdu->s->migration_blocker) {
389 migrate_del_blocker(pdu->s->migration_blocker);
390 error_free(pdu->s->migration_blocker);
391 pdu->s->migration_blocker = NULL;
394 return free_fid(pdu, fidp);
399 static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
401 V9fsFidState **fidpp, *fidp;
403 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
404 if ((*fidpp)->fid == fid) {
408 if (*fidpp == NULL) {
417 void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)
419 int reclaim_count = 0;
420 V9fsState *s = pdu->s;
421 V9fsFidState *f, *reclaim_list = NULL;
423 for (f = s->fid_list; f; f = f->next) {
425 * Unlink fids cannot be reclaimed. Check
426 * for them and skip them. Also skip fids
427 * currently being operated on.
429 if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
433 * if it is a recently referenced fid
434 * we leave the fid untouched and clear the
435 * reference bit. We come back to it later
436 * in the next iteration. (a simple LRU without
437 * moving list elements around)
439 if (f->flags & FID_REFERENCED) {
440 f->flags &= ~FID_REFERENCED;
444 * Add fids to reclaim list.
446 if (f->fid_type == P9_FID_FILE) {
447 if (f->fs.fd != -1) {
449 * Up the reference count so that
450 * a clunk request won't free this fid
453 f->rclm_lst = reclaim_list;
455 f->fs_reclaim.fd = f->fs.fd;
459 } else if (f->fid_type == P9_FID_DIR) {
460 if (f->fs.dir.stream != NULL) {
462 * Up the reference count so that
463 * a clunk request won't free this fid
466 f->rclm_lst = reclaim_list;
468 f->fs_reclaim.dir.stream = f->fs.dir.stream;
469 f->fs.dir.stream = NULL;
473 if (reclaim_count >= open_fd_rc) {
478 * Now close the fid in reclaim list. Free them if they
479 * are already clunked.
481 while (reclaim_list) {
483 reclaim_list = f->rclm_lst;
484 if (f->fid_type == P9_FID_FILE) {
485 v9fs_co_close(pdu, &f->fs_reclaim);
486 } else if (f->fid_type == P9_FID_DIR) {
487 v9fs_co_closedir(pdu, &f->fs_reclaim);
491 * Now drop the fid reference, free it
498 static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
501 V9fsState *s = pdu->s;
502 V9fsFidState *fidp, head_fid;
504 head_fid.next = s->fid_list;
505 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
506 if (fidp->path.size != path->size) {
509 if (!memcmp(fidp->path.data, path->data, path->size)) {
510 /* Mark the fid non reclaimable. */
511 fidp->flags |= FID_NON_RECLAIMABLE;
513 /* reopen the file/dir if already closed */
514 err = v9fs_reopen_fid(pdu, fidp);
519 * Go back to head of fid list because
520 * the list could have got updated when
521 * switched to the worker thread
531 static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
533 V9fsState *s = pdu->s;
537 while (s->fid_list) {
543 s->fid_list = fidp->next;
550 #define P9_QID_TYPE_DIR 0x80
551 #define P9_QID_TYPE_SYMLINK 0x02
553 #define P9_STAT_MODE_DIR 0x80000000
554 #define P9_STAT_MODE_APPEND 0x40000000
555 #define P9_STAT_MODE_EXCL 0x20000000
556 #define P9_STAT_MODE_MOUNT 0x10000000
557 #define P9_STAT_MODE_AUTH 0x08000000
558 #define P9_STAT_MODE_TMP 0x04000000
559 #define P9_STAT_MODE_SYMLINK 0x02000000
560 #define P9_STAT_MODE_LINK 0x01000000
561 #define P9_STAT_MODE_DEVICE 0x00800000
562 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
563 #define P9_STAT_MODE_SOCKET 0x00100000
564 #define P9_STAT_MODE_SETUID 0x00080000
565 #define P9_STAT_MODE_SETGID 0x00040000
566 #define P9_STAT_MODE_SETVTX 0x00010000
568 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
569 P9_STAT_MODE_SYMLINK | \
570 P9_STAT_MODE_LINK | \
571 P9_STAT_MODE_DEVICE | \
572 P9_STAT_MODE_NAMED_PIPE | \
575 /* This is the algorithm from ufs in spfs */
576 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
580 memset(&qidp->path, 0, sizeof(qidp->path));
581 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
582 memcpy(&qidp->path, &stbuf->st_ino, size);
583 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
585 if (S_ISDIR(stbuf->st_mode)) {
586 qidp->type |= P9_QID_TYPE_DIR;
588 if (S_ISLNK(stbuf->st_mode)) {
589 qidp->type |= P9_QID_TYPE_SYMLINK;
593 static int coroutine_fn fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp,
599 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
603 stat_to_qid(&stbuf, qidp);
607 V9fsPDU *pdu_alloc(V9fsState *s)
611 if (!QLIST_EMPTY(&s->free_list)) {
612 pdu = QLIST_FIRST(&s->free_list);
613 QLIST_REMOVE(pdu, next);
614 QLIST_INSERT_HEAD(&s->active_list, pdu, next);
619 void pdu_free(V9fsPDU *pdu)
621 V9fsState *s = pdu->s;
623 g_assert(!pdu->cancelled);
624 QLIST_REMOVE(pdu, next);
625 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
628 static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
630 int8_t id = pdu->id + 1; /* Response */
631 V9fsState *s = pdu->s;
635 * The 9p spec requires that successfully cancelled pdus receive no reply.
636 * Sending a reply would confuse clients because they would
637 * assume that any EINTR is the actual result of the operation,
638 * rather than a consequence of the cancellation. However, if
639 * the operation completed (succesfully or with an error other
640 * than caused be cancellation), we do send out that reply, both
641 * for efficiency and to avoid confusing the rest of the state machine
642 * that assumes passing a non-error here will mean a successful
643 * transmission of the reply.
645 bool discard = pdu->cancelled && len == -EINTR;
647 trace_v9fs_rcancel(pdu->tag, pdu->id);
656 if (s->proto_version != V9FS_PROTO_2000L) {
659 str.data = strerror(err);
660 str.size = strlen(str.data);
662 ret = pdu_marshal(pdu, len, "s", &str);
670 ret = pdu_marshal(pdu, len, "d", err);
676 if (s->proto_version == V9FS_PROTO_2000L) {
679 trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
682 /* fill out the header */
683 if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) {
687 /* keep these in sync */
692 pdu->s->transport->push_and_notify(pdu);
694 /* Now wakeup anybody waiting in flush for this request */
695 if (!qemu_co_queue_next(&pdu->complete)) {
700 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
705 if (mode & P9_STAT_MODE_DIR) {
709 if (mode & P9_STAT_MODE_SYMLINK) {
712 if (mode & P9_STAT_MODE_SOCKET) {
715 if (mode & P9_STAT_MODE_NAMED_PIPE) {
718 if (mode & P9_STAT_MODE_DEVICE) {
719 if (extension->size && extension->data[0] == 'c') {
730 if (mode & P9_STAT_MODE_SETUID) {
733 if (mode & P9_STAT_MODE_SETGID) {
736 if (mode & P9_STAT_MODE_SETVTX) {
743 static int donttouch_stat(V9fsStat *stat)
745 if (stat->type == -1 &&
747 stat->qid.type == -1 &&
748 stat->qid.version == -1 &&
749 stat->qid.path == -1 &&
753 stat->length == -1 &&
760 stat->n_muid == -1) {
767 static void v9fs_stat_init(V9fsStat *stat)
769 v9fs_string_init(&stat->name);
770 v9fs_string_init(&stat->uid);
771 v9fs_string_init(&stat->gid);
772 v9fs_string_init(&stat->muid);
773 v9fs_string_init(&stat->extension);
776 static void v9fs_stat_free(V9fsStat *stat)
778 v9fs_string_free(&stat->name);
779 v9fs_string_free(&stat->uid);
780 v9fs_string_free(&stat->gid);
781 v9fs_string_free(&stat->muid);
782 v9fs_string_free(&stat->extension);
785 static uint32_t stat_to_v9mode(const struct stat *stbuf)
789 mode = stbuf->st_mode & 0777;
790 if (S_ISDIR(stbuf->st_mode)) {
791 mode |= P9_STAT_MODE_DIR;
794 if (S_ISLNK(stbuf->st_mode)) {
795 mode |= P9_STAT_MODE_SYMLINK;
798 if (S_ISSOCK(stbuf->st_mode)) {
799 mode |= P9_STAT_MODE_SOCKET;
802 if (S_ISFIFO(stbuf->st_mode)) {
803 mode |= P9_STAT_MODE_NAMED_PIPE;
806 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
807 mode |= P9_STAT_MODE_DEVICE;
810 if (stbuf->st_mode & S_ISUID) {
811 mode |= P9_STAT_MODE_SETUID;
814 if (stbuf->st_mode & S_ISGID) {
815 mode |= P9_STAT_MODE_SETGID;
818 if (stbuf->st_mode & S_ISVTX) {
819 mode |= P9_STAT_MODE_SETVTX;
825 static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path,
826 const char *basename,
827 const struct stat *stbuf,
832 memset(v9stat, 0, sizeof(*v9stat));
834 stat_to_qid(stbuf, &v9stat->qid);
835 v9stat->mode = stat_to_v9mode(stbuf);
836 v9stat->atime = stbuf->st_atime;
837 v9stat->mtime = stbuf->st_mtime;
838 v9stat->length = stbuf->st_size;
840 v9fs_string_free(&v9stat->uid);
841 v9fs_string_free(&v9stat->gid);
842 v9fs_string_free(&v9stat->muid);
844 v9stat->n_uid = stbuf->st_uid;
845 v9stat->n_gid = stbuf->st_gid;
848 v9fs_string_free(&v9stat->extension);
850 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
851 err = v9fs_co_readlink(pdu, path, &v9stat->extension);
855 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
856 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
857 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
858 major(stbuf->st_rdev), minor(stbuf->st_rdev));
859 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
860 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
861 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
864 v9fs_string_sprintf(&v9stat->name, "%s", basename);
867 v9fs_string_size(&v9stat->name) +
868 v9fs_string_size(&v9stat->uid) +
869 v9fs_string_size(&v9stat->gid) +
870 v9fs_string_size(&v9stat->muid) +
871 v9fs_string_size(&v9stat->extension);
875 #define P9_STATS_MODE 0x00000001ULL
876 #define P9_STATS_NLINK 0x00000002ULL
877 #define P9_STATS_UID 0x00000004ULL
878 #define P9_STATS_GID 0x00000008ULL
879 #define P9_STATS_RDEV 0x00000010ULL
880 #define P9_STATS_ATIME 0x00000020ULL
881 #define P9_STATS_MTIME 0x00000040ULL
882 #define P9_STATS_CTIME 0x00000080ULL
883 #define P9_STATS_INO 0x00000100ULL
884 #define P9_STATS_SIZE 0x00000200ULL
885 #define P9_STATS_BLOCKS 0x00000400ULL
887 #define P9_STATS_BTIME 0x00000800ULL
888 #define P9_STATS_GEN 0x00001000ULL
889 #define P9_STATS_DATA_VERSION 0x00002000ULL
891 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
892 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
895 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
896 V9fsStatDotl *v9lstat)
898 memset(v9lstat, 0, sizeof(*v9lstat));
900 v9lstat->st_mode = stbuf->st_mode;
901 v9lstat->st_nlink = stbuf->st_nlink;
902 v9lstat->st_uid = stbuf->st_uid;
903 v9lstat->st_gid = stbuf->st_gid;
904 v9lstat->st_rdev = stbuf->st_rdev;
905 v9lstat->st_size = stbuf->st_size;
906 v9lstat->st_blksize = stbuf->st_blksize;
907 v9lstat->st_blocks = stbuf->st_blocks;
908 v9lstat->st_atime_sec = stbuf->st_atime;
909 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
910 v9lstat->st_mtime_sec = stbuf->st_mtime;
911 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
912 v9lstat->st_ctime_sec = stbuf->st_ctime;
913 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
914 /* Currently we only support BASIC fields in stat */
915 v9lstat->st_result_mask = P9_STATS_BASIC;
917 stat_to_qid(stbuf, &v9lstat->qid);
920 static void print_sg(struct iovec *sg, int cnt)
924 printf("sg[%d]: {", cnt);
925 for (i = 0; i < cnt; i++) {
929 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
934 /* Will call this only for path name based fid */
935 static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
938 v9fs_path_init(&str);
939 v9fs_path_copy(&str, dst);
940 v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
941 v9fs_path_free(&str);
944 static inline bool is_ro_export(FsContext *ctx)
946 return ctx->export_flags & V9FS_RDONLY;
949 static void coroutine_fn v9fs_version(void *opaque)
952 V9fsPDU *pdu = opaque;
953 V9fsState *s = pdu->s;
957 v9fs_string_init(&version);
958 err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
962 trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
966 if (!strcmp(version.data, "9P2000.u")) {
967 s->proto_version = V9FS_PROTO_2000U;
968 } else if (!strcmp(version.data, "9P2000.L")) {
969 s->proto_version = V9FS_PROTO_2000L;
971 v9fs_string_sprintf(&version, "unknown");
974 err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
979 trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
981 pdu_complete(pdu, err);
982 v9fs_string_free(&version);
985 static void coroutine_fn v9fs_attach(void *opaque)
987 V9fsPDU *pdu = opaque;
988 V9fsState *s = pdu->s;
989 int32_t fid, afid, n_uname;
990 V9fsString uname, aname;
995 Error *local_err = NULL;
997 v9fs_string_init(&uname);
998 v9fs_string_init(&aname);
999 err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
1000 &afid, &uname, &aname, &n_uname);
1004 trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
1006 fidp = alloc_fid(s, fid);
1011 fidp->uid = n_uname;
1012 err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
1018 err = fid_to_qid(pdu, fidp, &qid);
1026 * disable migration if we haven't done already.
1027 * attach could get called multiple times for the same export.
1029 if (!s->migration_blocker) {
1030 error_setg(&s->migration_blocker,
1031 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1032 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
1033 err = migrate_add_blocker(s->migration_blocker, &local_err);
1035 error_free(local_err);
1036 error_free(s->migration_blocker);
1037 s->migration_blocker = NULL;
1044 err = pdu_marshal(pdu, offset, "Q", &qid);
1051 memcpy(&s->root_qid, &qid, sizeof(qid));
1052 trace_v9fs_attach_return(pdu->tag, pdu->id,
1053 qid.type, qid.version, qid.path);
1057 pdu_complete(pdu, err);
1058 v9fs_string_free(&uname);
1059 v9fs_string_free(&aname);
1062 static void coroutine_fn v9fs_stat(void *opaque)
1070 V9fsPDU *pdu = opaque;
1073 err = pdu_unmarshal(pdu, offset, "d", &fid);
1077 trace_v9fs_stat(pdu->tag, pdu->id, fid);
1079 fidp = get_fid(pdu, fid);
1084 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1088 basename = g_path_get_basename(fidp->path.data);
1089 err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat);
1094 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1096 v9fs_stat_free(&v9stat);
1099 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1100 v9stat.atime, v9stat.mtime, v9stat.length);
1102 v9fs_stat_free(&v9stat);
1106 pdu_complete(pdu, err);
1109 static void coroutine_fn v9fs_getattr(void *opaque)
1116 uint64_t request_mask;
1117 V9fsStatDotl v9stat_dotl;
1118 V9fsPDU *pdu = opaque;
1119 V9fsState *s = pdu->s;
1121 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1125 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
1127 fidp = get_fid(pdu, fid);
1133 * Currently we only support BASIC fields in stat, so there is no
1134 * need to look at request_mask.
1136 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1140 stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
1142 /* fill st_gen if requested and supported by underlying fs */
1143 if (request_mask & P9_STATS_GEN) {
1144 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
1147 /* we have valid st_gen: update result mask */
1148 v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1151 /* request cancelled, e.g. by Tflush */
1154 /* failed to get st_gen: not fatal, ignore */
1158 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1163 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1164 v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1165 v9stat_dotl.st_gid);
1169 pdu_complete(pdu, retval);
1172 /* Attribute flags */
1173 #define P9_ATTR_MODE (1 << 0)
1174 #define P9_ATTR_UID (1 << 1)
1175 #define P9_ATTR_GID (1 << 2)
1176 #define P9_ATTR_SIZE (1 << 3)
1177 #define P9_ATTR_ATIME (1 << 4)
1178 #define P9_ATTR_MTIME (1 << 5)
1179 #define P9_ATTR_CTIME (1 << 6)
1180 #define P9_ATTR_ATIME_SET (1 << 7)
1181 #define P9_ATTR_MTIME_SET (1 << 8)
1183 #define P9_ATTR_MASK 127
1185 static void coroutine_fn v9fs_setattr(void *opaque)
1192 V9fsPDU *pdu = opaque;
1194 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1199 fidp = get_fid(pdu, fid);
1204 if (v9iattr.valid & P9_ATTR_MODE) {
1205 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1210 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1211 struct timespec times[2];
1212 if (v9iattr.valid & P9_ATTR_ATIME) {
1213 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1214 times[0].tv_sec = v9iattr.atime_sec;
1215 times[0].tv_nsec = v9iattr.atime_nsec;
1217 times[0].tv_nsec = UTIME_NOW;
1220 times[0].tv_nsec = UTIME_OMIT;
1222 if (v9iattr.valid & P9_ATTR_MTIME) {
1223 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1224 times[1].tv_sec = v9iattr.mtime_sec;
1225 times[1].tv_nsec = v9iattr.mtime_nsec;
1227 times[1].tv_nsec = UTIME_NOW;
1230 times[1].tv_nsec = UTIME_OMIT;
1232 err = v9fs_co_utimensat(pdu, &fidp->path, times);
1238 * If the only valid entry in iattr is ctime we can call
1239 * chown(-1,-1) to update the ctime of the file
1241 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1242 ((v9iattr.valid & P9_ATTR_CTIME)
1243 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1244 if (!(v9iattr.valid & P9_ATTR_UID)) {
1247 if (!(v9iattr.valid & P9_ATTR_GID)) {
1250 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1256 if (v9iattr.valid & (P9_ATTR_SIZE)) {
1257 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1266 pdu_complete(pdu, err);
1269 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1275 err = pdu_marshal(pdu, offset, "w", nwnames);
1280 for (i = 0; i < nwnames; i++) {
1281 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1290 static bool name_is_illegal(const char *name)
1292 return !*name || strchr(name, '/') != NULL;
1295 static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
1298 qid1->type != qid2->type ||
1299 qid1->version != qid2->version ||
1300 qid1->path != qid2->path;
1303 static void coroutine_fn v9fs_walk(void *opaque)
1306 V9fsQID *qids = NULL;
1308 V9fsPath dpath, path;
1312 int32_t fid, newfid;
1313 V9fsString *wnames = NULL;
1315 V9fsFidState *newfidp = NULL;
1316 V9fsPDU *pdu = opaque;
1317 V9fsState *s = pdu->s;
1320 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1322 pdu_complete(pdu, err);
1327 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1329 if (nwnames && nwnames <= P9_MAXWELEM) {
1330 wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
1331 qids = g_malloc0(sizeof(qids[0]) * nwnames);
1332 for (i = 0; i < nwnames; i++) {
1333 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1337 if (name_is_illegal(wnames[i].data)) {
1343 } else if (nwnames > P9_MAXWELEM) {
1347 fidp = get_fid(pdu, fid);
1353 v9fs_path_init(&dpath);
1354 v9fs_path_init(&path);
1356 err = fid_to_qid(pdu, fidp, &qid);
1362 * Both dpath and path initially poin to fidp.
1363 * Needed to handle request with nwnames == 0
1365 v9fs_path_copy(&dpath, &fidp->path);
1366 v9fs_path_copy(&path, &fidp->path);
1367 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1368 if (not_same_qid(&pdu->s->root_qid, &qid) ||
1369 strcmp("..", wnames[name_idx].data)) {
1370 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
1376 err = v9fs_co_lstat(pdu, &path, &stbuf);
1380 stat_to_qid(&stbuf, &qid);
1381 v9fs_path_copy(&dpath, &path);
1383 memcpy(&qids[name_idx], &qid, sizeof(qid));
1385 if (fid == newfid) {
1386 if (fidp->fid_type != P9_FID_NONE) {
1390 v9fs_path_copy(&fidp->path, &path);
1392 newfidp = alloc_fid(s, newfid);
1393 if (newfidp == NULL) {
1397 newfidp->uid = fidp->uid;
1398 v9fs_path_copy(&newfidp->path, &path);
1400 err = v9fs_walk_marshal(pdu, nwnames, qids);
1401 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1405 put_fid(pdu, newfidp);
1407 v9fs_path_free(&dpath);
1408 v9fs_path_free(&path);
1410 pdu_complete(pdu, err);
1411 if (nwnames && nwnames <= P9_MAXWELEM) {
1412 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1413 v9fs_string_free(&wnames[name_idx]);
1420 static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
1422 struct statfs stbuf;
1424 V9fsState *s = pdu->s;
1427 * iounit should be multiples of f_bsize (host filesystem block size
1428 * and as well as less than (client msize - P9_IOHDRSZ))
1430 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1431 iounit = stbuf.f_bsize;
1432 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1435 iounit = s->msize - P9_IOHDRSZ;
1440 static void coroutine_fn v9fs_open(void *opaque)
1451 V9fsPDU *pdu = opaque;
1452 V9fsState *s = pdu->s;
1454 if (s->proto_version == V9FS_PROTO_2000L) {
1455 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1458 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1464 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1466 fidp = get_fid(pdu, fid);
1471 if (fidp->fid_type != P9_FID_NONE) {
1476 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1480 stat_to_qid(&stbuf, &qid);
1481 if (S_ISDIR(stbuf.st_mode)) {
1482 err = v9fs_co_opendir(pdu, fidp);
1486 fidp->fid_type = P9_FID_DIR;
1487 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1493 if (s->proto_version == V9FS_PROTO_2000L) {
1494 flags = get_dotl_openflags(s, mode);
1496 flags = omode_to_uflags(mode);
1498 if (is_ro_export(&s->ctx)) {
1499 if (mode & O_WRONLY || mode & O_RDWR ||
1500 mode & O_APPEND || mode & O_TRUNC) {
1505 err = v9fs_co_open(pdu, fidp, flags);
1509 fidp->fid_type = P9_FID_FILE;
1510 fidp->open_flags = flags;
1511 if (flags & O_EXCL) {
1513 * We let the host file system do O_EXCL check
1514 * We should not reclaim such fd
1516 fidp->flags |= FID_NON_RECLAIMABLE;
1518 iounit = get_iounit(pdu, &fidp->path);
1519 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1525 trace_v9fs_open_return(pdu->tag, pdu->id,
1526 qid.type, qid.version, qid.path, iounit);
1530 pdu_complete(pdu, err);
1533 static void coroutine_fn v9fs_lcreate(void *opaque)
1535 int32_t dfid, flags, mode;
1544 V9fsPDU *pdu = opaque;
1546 v9fs_string_init(&name);
1547 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1548 &name, &flags, &mode, &gid);
1552 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1554 if (name_is_illegal(name.data)) {
1559 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
1564 fidp = get_fid(pdu, dfid);
1569 if (fidp->fid_type != P9_FID_NONE) {
1574 flags = get_dotl_openflags(pdu->s, flags);
1575 err = v9fs_co_open2(pdu, fidp, &name, gid,
1576 flags | O_CREAT, mode, &stbuf);
1580 fidp->fid_type = P9_FID_FILE;
1581 fidp->open_flags = flags;
1582 if (flags & O_EXCL) {
1584 * We let the host file system do O_EXCL check
1585 * We should not reclaim such fd
1587 fidp->flags |= FID_NON_RECLAIMABLE;
1589 iounit = get_iounit(pdu, &fidp->path);
1590 stat_to_qid(&stbuf, &qid);
1591 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1596 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
1597 qid.type, qid.version, qid.path, iounit);
1601 pdu_complete(pdu, err);
1602 v9fs_string_free(&name);
1605 static void coroutine_fn v9fs_fsync(void *opaque)
1612 V9fsPDU *pdu = opaque;
1614 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1618 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
1620 fidp = get_fid(pdu, fid);
1625 err = v9fs_co_fsync(pdu, fidp, datasync);
1631 pdu_complete(pdu, err);
1634 static void coroutine_fn v9fs_clunk(void *opaque)
1640 V9fsPDU *pdu = opaque;
1641 V9fsState *s = pdu->s;
1643 err = pdu_unmarshal(pdu, offset, "d", &fid);
1647 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
1649 fidp = clunk_fid(s, fid);
1655 * Bump the ref so that put_fid will
1659 err = put_fid(pdu, fidp);
1664 pdu_complete(pdu, err);
1668 * Create a QEMUIOVector for a sub-region of PDU iovecs
1670 * @qiov: uninitialized QEMUIOVector
1671 * @skip: number of bytes to skip from beginning of PDU
1672 * @size: number of bytes to include
1673 * @is_write: true - write, false - read
1675 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1676 * with qemu_iovec_destroy().
1678 static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1679 size_t skip, size_t size,
1687 pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip);
1689 pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip);
1692 qemu_iovec_init_external(&elem, iov, niov);
1693 qemu_iovec_init(qiov, niov);
1694 qemu_iovec_concat(qiov, &elem, skip, size);
1697 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1698 uint64_t off, uint32_t max_count)
1702 uint64_t read_count;
1703 QEMUIOVector qiov_full;
1705 if (fidp->fs.xattr.len < off) {
1708 read_count = fidp->fs.xattr.len - off;
1710 if (read_count > max_count) {
1711 read_count = max_count;
1713 err = pdu_marshal(pdu, offset, "d", read_count);
1719 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false);
1720 err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0,
1721 ((char *)fidp->fs.xattr.value) + off,
1723 qemu_iovec_destroy(&qiov_full);
1731 static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1740 off_t saved_dir_pos;
1741 struct dirent *dent;
1743 /* save the directory position */
1744 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1745 if (saved_dir_pos < 0) {
1746 return saved_dir_pos;
1750 v9fs_path_init(&path);
1752 v9fs_readdir_lock(&fidp->fs.dir);
1754 err = v9fs_co_readdir(pdu, fidp, &dent);
1758 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
1762 err = v9fs_co_lstat(pdu, &path, &stbuf);
1766 err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat);
1770 if ((count + v9stat.size + 2) > max_count) {
1771 v9fs_readdir_unlock(&fidp->fs.dir);
1773 /* Ran out of buffer. Set dir back to old position and return */
1774 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1775 v9fs_stat_free(&v9stat);
1776 v9fs_path_free(&path);
1780 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1781 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1783 v9fs_readdir_unlock(&fidp->fs.dir);
1786 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1787 v9fs_stat_free(&v9stat);
1788 v9fs_path_free(&path);
1792 v9fs_stat_free(&v9stat);
1793 v9fs_path_free(&path);
1794 saved_dir_pos = dent->d_off;
1797 v9fs_readdir_unlock(&fidp->fs.dir);
1799 v9fs_path_free(&path);
1806 static void coroutine_fn v9fs_read(void *opaque)
1815 V9fsPDU *pdu = opaque;
1816 V9fsState *s = pdu->s;
1818 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1822 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
1824 fidp = get_fid(pdu, fid);
1829 if (fidp->fid_type == P9_FID_DIR) {
1832 v9fs_co_rewinddir(pdu, fidp);
1834 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
1839 err = pdu_marshal(pdu, offset, "d", count);
1843 err += offset + count;
1844 } else if (fidp->fid_type == P9_FID_FILE) {
1845 QEMUIOVector qiov_full;
1849 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
1850 qemu_iovec_init(&qiov, qiov_full.niov);
1852 qemu_iovec_reset(&qiov);
1853 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
1855 print_sg(qiov.iov, qiov.niov);
1857 /* Loop in case of EINTR */
1859 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
1864 } while (len == -EINTR && !pdu->cancelled);
1866 /* IO error return the error */
1868 goto out_free_iovec;
1870 } while (count < max_count && len > 0);
1871 err = pdu_marshal(pdu, offset, "d", count);
1873 goto out_free_iovec;
1875 err += offset + count;
1877 qemu_iovec_destroy(&qiov);
1878 qemu_iovec_destroy(&qiov_full);
1879 } else if (fidp->fid_type == P9_FID_XATTR) {
1880 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
1884 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
1888 pdu_complete(pdu, err);
1891 static size_t v9fs_readdir_data_size(V9fsString *name)
1894 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1895 * size of type (1) + size of name.size (2) + strlen(name.data)
1897 return 24 + v9fs_string_size(name);
1900 static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
1908 off_t saved_dir_pos;
1909 struct dirent *dent;
1911 /* save the directory position */
1912 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1913 if (saved_dir_pos < 0) {
1914 return saved_dir_pos;
1918 v9fs_readdir_lock(&fidp->fs.dir);
1920 err = v9fs_co_readdir(pdu, fidp, &dent);
1924 v9fs_string_init(&name);
1925 v9fs_string_sprintf(&name, "%s", dent->d_name);
1926 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
1927 v9fs_readdir_unlock(&fidp->fs.dir);
1929 /* Ran out of buffer. Set dir back to old position and return */
1930 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1931 v9fs_string_free(&name);
1935 * Fill up just the path field of qid because the client uses
1936 * only that. To fill the entire qid structure we will have
1937 * to stat each dirent found, which is expensive
1939 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1940 memcpy(&qid.path, &dent->d_ino, size);
1941 /* Fill the other fields with dummy values */
1945 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1946 len = pdu_marshal(pdu, 11 + count, "Qqbs",
1948 dent->d_type, &name);
1950 v9fs_readdir_unlock(&fidp->fs.dir);
1953 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1954 v9fs_string_free(&name);
1958 v9fs_string_free(&name);
1959 saved_dir_pos = dent->d_off;
1962 v9fs_readdir_unlock(&fidp->fs.dir);
1970 static void coroutine_fn v9fs_readdir(void *opaque)
1976 uint64_t initial_offset;
1979 V9fsPDU *pdu = opaque;
1981 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
1982 &initial_offset, &max_count);
1986 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
1988 fidp = get_fid(pdu, fid);
1993 if (!fidp->fs.dir.stream) {
1997 if (initial_offset == 0) {
1998 v9fs_co_rewinddir(pdu, fidp);
2000 v9fs_co_seekdir(pdu, fidp, initial_offset);
2002 count = v9fs_do_readdir(pdu, fidp, max_count);
2007 retval = pdu_marshal(pdu, offset, "d", count);
2011 retval += count + offset;
2012 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
2016 pdu_complete(pdu, retval);
2019 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
2020 uint64_t off, uint32_t count,
2021 struct iovec *sg, int cnt)
2025 uint64_t write_count;
2029 if (fidp->fs.xattr.len < off) {
2033 write_count = fidp->fs.xattr.len - off;
2034 if (write_count > count) {
2035 write_count = count;
2037 err = pdu_marshal(pdu, offset, "d", write_count);
2042 fidp->fs.xattr.copied_len += write_count;
2044 * Now copy the content from sg list
2046 for (i = 0; i < cnt; i++) {
2047 if (write_count > sg[i].iov_len) {
2048 to_copy = sg[i].iov_len;
2050 to_copy = write_count;
2052 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
2053 /* updating vs->off since we are not using below */
2055 write_count -= to_copy;
2061 static void coroutine_fn v9fs_write(void *opaque)
2071 V9fsPDU *pdu = opaque;
2072 V9fsState *s = pdu->s;
2073 QEMUIOVector qiov_full;
2076 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
2078 pdu_complete(pdu, err);
2082 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
2083 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
2085 fidp = get_fid(pdu, fid);
2090 if (fidp->fid_type == P9_FID_FILE) {
2091 if (fidp->fs.fd == -1) {
2095 } else if (fidp->fid_type == P9_FID_XATTR) {
2097 * setxattr operation
2099 err = v9fs_xattr_write(s, pdu, fidp, off, count,
2100 qiov_full.iov, qiov_full.niov);
2106 qemu_iovec_init(&qiov, qiov_full.niov);
2108 qemu_iovec_reset(&qiov);
2109 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
2111 print_sg(qiov.iov, qiov.niov);
2113 /* Loop in case of EINTR */
2115 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
2120 } while (len == -EINTR && !pdu->cancelled);
2122 /* IO error return the error */
2126 } while (total < count && len > 0);
2129 err = pdu_marshal(pdu, offset, "d", total);
2134 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2136 qemu_iovec_destroy(&qiov);
2140 qemu_iovec_destroy(&qiov_full);
2141 pdu_complete(pdu, err);
2144 static void coroutine_fn v9fs_create(void *opaque)
2156 V9fsString extension;
2158 V9fsPDU *pdu = opaque;
2160 v9fs_path_init(&path);
2161 v9fs_string_init(&name);
2162 v9fs_string_init(&extension);
2163 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2164 &perm, &mode, &extension);
2168 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2170 if (name_is_illegal(name.data)) {
2175 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2180 fidp = get_fid(pdu, fid);
2185 if (fidp->fid_type != P9_FID_NONE) {
2189 if (perm & P9_STAT_MODE_DIR) {
2190 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2191 fidp->uid, -1, &stbuf);
2195 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2199 v9fs_path_copy(&fidp->path, &path);
2200 err = v9fs_co_opendir(pdu, fidp);
2204 fidp->fid_type = P9_FID_DIR;
2205 } else if (perm & P9_STAT_MODE_SYMLINK) {
2206 err = v9fs_co_symlink(pdu, fidp, &name,
2207 extension.data, -1 , &stbuf);
2211 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2215 v9fs_path_copy(&fidp->path, &path);
2216 } else if (perm & P9_STAT_MODE_LINK) {
2217 int32_t ofid = atoi(extension.data);
2218 V9fsFidState *ofidp = get_fid(pdu, ofid);
2219 if (ofidp == NULL) {
2223 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2224 put_fid(pdu, ofidp);
2228 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2230 fidp->fid_type = P9_FID_NONE;
2233 v9fs_path_copy(&fidp->path, &path);
2234 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2236 fidp->fid_type = P9_FID_NONE;
2239 } else if (perm & P9_STAT_MODE_DEVICE) {
2241 uint32_t major, minor;
2244 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2261 nmode |= perm & 0777;
2262 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2263 makedev(major, minor), nmode, &stbuf);
2267 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2271 v9fs_path_copy(&fidp->path, &path);
2272 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2273 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2274 0, S_IFIFO | (perm & 0777), &stbuf);
2278 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2282 v9fs_path_copy(&fidp->path, &path);
2283 } else if (perm & P9_STAT_MODE_SOCKET) {
2284 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2285 0, S_IFSOCK | (perm & 0777), &stbuf);
2289 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2293 v9fs_path_copy(&fidp->path, &path);
2295 err = v9fs_co_open2(pdu, fidp, &name, -1,
2296 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2300 fidp->fid_type = P9_FID_FILE;
2301 fidp->open_flags = omode_to_uflags(mode);
2302 if (fidp->open_flags & O_EXCL) {
2304 * We let the host file system do O_EXCL check
2305 * We should not reclaim such fd
2307 fidp->flags |= FID_NON_RECLAIMABLE;
2310 iounit = get_iounit(pdu, &fidp->path);
2311 stat_to_qid(&stbuf, &qid);
2312 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2317 trace_v9fs_create_return(pdu->tag, pdu->id,
2318 qid.type, qid.version, qid.path, iounit);
2322 pdu_complete(pdu, err);
2323 v9fs_string_free(&name);
2324 v9fs_string_free(&extension);
2325 v9fs_path_free(&path);
2328 static void coroutine_fn v9fs_symlink(void *opaque)
2330 V9fsPDU *pdu = opaque;
2333 V9fsFidState *dfidp;
2341 v9fs_string_init(&name);
2342 v9fs_string_init(&symname);
2343 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2347 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2349 if (name_is_illegal(name.data)) {
2354 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2359 dfidp = get_fid(pdu, dfid);
2360 if (dfidp == NULL) {
2364 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2368 stat_to_qid(&stbuf, &qid);
2369 err = pdu_marshal(pdu, offset, "Q", &qid);
2374 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2375 qid.type, qid.version, qid.path);
2377 put_fid(pdu, dfidp);
2379 pdu_complete(pdu, err);
2380 v9fs_string_free(&name);
2381 v9fs_string_free(&symname);
2384 static void coroutine_fn v9fs_flush(void *opaque)
2389 V9fsPDU *cancel_pdu = NULL;
2390 V9fsPDU *pdu = opaque;
2391 V9fsState *s = pdu->s;
2393 err = pdu_unmarshal(pdu, offset, "w", &tag);
2395 pdu_complete(pdu, err);
2398 trace_v9fs_flush(pdu->tag, pdu->id, tag);
2400 if (pdu->tag == tag) {
2401 warn_report("the guest sent a self-referencing 9P flush request");
2403 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2404 if (cancel_pdu->tag == tag) {
2410 cancel_pdu->cancelled = 1;
2412 * Wait for pdu to complete.
2414 qemu_co_queue_wait(&cancel_pdu->complete, NULL);
2415 if (!qemu_co_queue_next(&cancel_pdu->complete)) {
2416 cancel_pdu->cancelled = 0;
2417 pdu_free(cancel_pdu);
2420 pdu_complete(pdu, 7);
2423 static void coroutine_fn v9fs_link(void *opaque)
2425 V9fsPDU *pdu = opaque;
2426 int32_t dfid, oldfid;
2427 V9fsFidState *dfidp, *oldfidp;
2432 v9fs_string_init(&name);
2433 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2437 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2439 if (name_is_illegal(name.data)) {
2444 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2449 dfidp = get_fid(pdu, dfid);
2450 if (dfidp == NULL) {
2455 oldfidp = get_fid(pdu, oldfid);
2456 if (oldfidp == NULL) {
2460 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2464 put_fid(pdu, oldfidp);
2466 put_fid(pdu, dfidp);
2468 v9fs_string_free(&name);
2469 pdu_complete(pdu, err);
2472 /* Only works with path name based fid */
2473 static void coroutine_fn v9fs_remove(void *opaque)
2479 V9fsPDU *pdu = opaque;
2481 err = pdu_unmarshal(pdu, offset, "d", &fid);
2485 trace_v9fs_remove(pdu->tag, pdu->id, fid);
2487 fidp = get_fid(pdu, fid);
2492 /* if fs driver is not path based, return EOPNOTSUPP */
2493 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2498 * IF the file is unlinked, we cannot reopen
2499 * the file later. So don't reclaim fd
2501 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2505 err = v9fs_co_remove(pdu, &fidp->path);
2510 /* For TREMOVE we need to clunk the fid even on failed remove */
2511 clunk_fid(pdu->s, fidp->fid);
2514 pdu_complete(pdu, err);
2517 static void coroutine_fn v9fs_unlinkat(void *opaque)
2521 int32_t dfid, flags;
2524 V9fsFidState *dfidp;
2525 V9fsPDU *pdu = opaque;
2527 v9fs_string_init(&name);
2528 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2533 if (name_is_illegal(name.data)) {
2538 if (!strcmp(".", name.data)) {
2543 if (!strcmp("..", name.data)) {
2548 dfidp = get_fid(pdu, dfid);
2549 if (dfidp == NULL) {
2554 * IF the file is unlinked, we cannot reopen
2555 * the file later. So don't reclaim fd
2557 v9fs_path_init(&path);
2558 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2562 err = v9fs_mark_fids_unreclaim(pdu, &path);
2566 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, flags);
2571 put_fid(pdu, dfidp);
2572 v9fs_path_free(&path);
2574 pdu_complete(pdu, err);
2575 v9fs_string_free(&name);
2579 /* Only works with path name based fid */
2580 static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2586 V9fsFidState *tfidp;
2587 V9fsState *s = pdu->s;
2588 V9fsFidState *dirfidp = NULL;
2590 v9fs_path_init(&new_path);
2591 if (newdirfid != -1) {
2592 dirfidp = get_fid(pdu, newdirfid);
2593 if (dirfidp == NULL) {
2597 if (fidp->fid_type != P9_FID_NONE) {
2601 err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2606 char *dir_name = g_path_get_dirname(fidp->path.data);
2609 v9fs_path_init(&dir_path);
2610 v9fs_path_sprintf(&dir_path, "%s", dir_name);
2613 err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path);
2614 v9fs_path_free(&dir_path);
2619 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2624 * Fixup fid's pointing to the old name to
2625 * start pointing to the new name
2627 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2628 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2629 /* replace the name */
2630 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
2635 put_fid(pdu, dirfidp);
2637 v9fs_path_free(&new_path);
2642 /* Only works with path name based fid */
2643 static void coroutine_fn v9fs_rename(void *opaque)
2651 V9fsPDU *pdu = opaque;
2652 V9fsState *s = pdu->s;
2654 v9fs_string_init(&name);
2655 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2660 if (name_is_illegal(name.data)) {
2665 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2670 fidp = get_fid(pdu, fid);
2675 if (fidp->fid_type != P9_FID_NONE) {
2679 /* if fs driver is not path based, return EOPNOTSUPP */
2680 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2684 v9fs_path_write_lock(s);
2685 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
2686 v9fs_path_unlock(s);
2693 pdu_complete(pdu, err);
2694 v9fs_string_free(&name);
2697 static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2698 V9fsString *old_name,
2700 V9fsString *new_name)
2702 V9fsFidState *tfidp;
2703 V9fsPath oldpath, newpath;
2704 V9fsState *s = pdu->s;
2707 v9fs_path_init(&oldpath);
2708 v9fs_path_init(&newpath);
2709 err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
2713 err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2719 * Fixup fid's pointing to the old name to
2720 * start pointing to the new name
2722 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2723 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
2724 /* replace the name */
2725 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
2729 v9fs_path_free(&oldpath);
2730 v9fs_path_free(&newpath);
2734 static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2735 V9fsString *old_name,
2737 V9fsString *new_name)
2740 V9fsState *s = pdu->s;
2741 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2743 olddirfidp = get_fid(pdu, olddirfid);
2744 if (olddirfidp == NULL) {
2748 if (newdirfid != -1) {
2749 newdirfidp = get_fid(pdu, newdirfid);
2750 if (newdirfidp == NULL) {
2755 newdirfidp = get_fid(pdu, olddirfid);
2758 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2759 &newdirfidp->path, new_name);
2763 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
2764 /* Only for path based fid we need to do the below fixup */
2765 err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2766 &newdirfidp->path, new_name);
2770 put_fid(pdu, olddirfidp);
2773 put_fid(pdu, newdirfidp);
2778 static void coroutine_fn v9fs_renameat(void *opaque)
2782 V9fsPDU *pdu = opaque;
2783 V9fsState *s = pdu->s;
2784 int32_t olddirfid, newdirfid;
2785 V9fsString old_name, new_name;
2787 v9fs_string_init(&old_name);
2788 v9fs_string_init(&new_name);
2789 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2790 &old_name, &newdirfid, &new_name);
2795 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
2800 if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
2801 !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
2806 v9fs_path_write_lock(s);
2807 err = v9fs_complete_renameat(pdu, olddirfid,
2808 &old_name, newdirfid, &new_name);
2809 v9fs_path_unlock(s);
2815 pdu_complete(pdu, err);
2816 v9fs_string_free(&old_name);
2817 v9fs_string_free(&new_name);
2820 static void coroutine_fn v9fs_wstat(void *opaque)
2829 V9fsPDU *pdu = opaque;
2831 v9fs_stat_init(&v9stat);
2832 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
2836 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
2837 v9stat.mode, v9stat.atime, v9stat.mtime);
2839 fidp = get_fid(pdu, fid);
2844 /* do we need to sync the file? */
2845 if (donttouch_stat(&v9stat)) {
2846 err = v9fs_co_fsync(pdu, fidp, 0);
2849 if (v9stat.mode != -1) {
2851 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2855 v9_mode = stat_to_v9mode(&stbuf);
2856 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2857 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2858 /* Attempting to change the type */
2862 err = v9fs_co_chmod(pdu, &fidp->path,
2863 v9mode_to_mode(v9stat.mode,
2864 &v9stat.extension));
2869 if (v9stat.mtime != -1 || v9stat.atime != -1) {
2870 struct timespec times[2];
2871 if (v9stat.atime != -1) {
2872 times[0].tv_sec = v9stat.atime;
2873 times[0].tv_nsec = 0;
2875 times[0].tv_nsec = UTIME_OMIT;
2877 if (v9stat.mtime != -1) {
2878 times[1].tv_sec = v9stat.mtime;
2879 times[1].tv_nsec = 0;
2881 times[1].tv_nsec = UTIME_OMIT;
2883 err = v9fs_co_utimensat(pdu, &fidp->path, times);
2888 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
2889 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
2894 if (v9stat.name.size != 0) {
2895 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
2900 if (v9stat.length != -1) {
2901 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
2910 v9fs_stat_free(&v9stat);
2911 pdu_complete(pdu, err);
2914 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2926 int32_t bsize_factor;
2929 * compute bsize factor based on host file system block size
2932 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2933 if (!bsize_factor) {
2936 f_type = stbuf->f_type;
2937 f_bsize = stbuf->f_bsize;
2938 f_bsize *= bsize_factor;
2940 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2941 * adjust(divide) the number of blocks, free blocks and available
2942 * blocks by bsize factor
2944 f_blocks = stbuf->f_blocks/bsize_factor;
2945 f_bfree = stbuf->f_bfree/bsize_factor;
2946 f_bavail = stbuf->f_bavail/bsize_factor;
2947 f_files = stbuf->f_files;
2948 f_ffree = stbuf->f_ffree;
2949 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
2950 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
2951 f_namelen = stbuf->f_namelen;
2953 return pdu_marshal(pdu, offset, "ddqqqqqqd",
2954 f_type, f_bsize, f_blocks, f_bfree,
2955 f_bavail, f_files, f_ffree,
2956 fsid_val, f_namelen);
2959 static void coroutine_fn v9fs_statfs(void *opaque)
2965 struct statfs stbuf;
2966 V9fsPDU *pdu = opaque;
2967 V9fsState *s = pdu->s;
2969 retval = pdu_unmarshal(pdu, offset, "d", &fid);
2973 fidp = get_fid(pdu, fid);
2978 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
2982 retval = v9fs_fill_statfs(s, pdu, &stbuf);
2990 pdu_complete(pdu, retval);
2993 static void coroutine_fn v9fs_mknod(void *opaque)
3006 V9fsPDU *pdu = opaque;
3008 v9fs_string_init(&name);
3009 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
3010 &major, &minor, &gid);
3014 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
3016 if (name_is_illegal(name.data)) {
3021 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3026 fidp = get_fid(pdu, fid);
3031 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
3032 makedev(major, minor), mode, &stbuf);
3036 stat_to_qid(&stbuf, &qid);
3037 err = pdu_marshal(pdu, offset, "Q", &qid);
3042 trace_v9fs_mknod_return(pdu->tag, pdu->id,
3043 qid.type, qid.version, qid.path);
3047 pdu_complete(pdu, err);
3048 v9fs_string_free(&name);
3052 * Implement posix byte range locking code
3053 * Server side handling of locking code is very simple, because 9p server in
3054 * QEMU can handle only one client. And most of the lock handling
3055 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3056 * do any thing in * qemu 9p server side lock code path.
3057 * So when a TLOCK request comes, always return success
3059 static void coroutine_fn v9fs_lock(void *opaque)
3065 int32_t fid, err = 0;
3066 V9fsPDU *pdu = opaque;
3068 v9fs_string_init(&flock.client_id);
3069 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
3070 &flock.flags, &flock.start, &flock.length,
3071 &flock.proc_id, &flock.client_id);
3075 trace_v9fs_lock(pdu->tag, pdu->id, fid,
3076 flock.type, flock.start, flock.length);
3079 /* We support only block flag now (that too ignored currently) */
3080 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
3084 fidp = get_fid(pdu, fid);
3089 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3093 err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS);
3098 trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS);
3102 pdu_complete(pdu, err);
3103 v9fs_string_free(&flock.client_id);
3107 * When a TGETLOCK request comes, always return success because all lock
3108 * handling is done by client's VFS layer.
3110 static void coroutine_fn v9fs_getlock(void *opaque)
3116 int32_t fid, err = 0;
3117 V9fsPDU *pdu = opaque;
3119 v9fs_string_init(&glock.client_id);
3120 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
3121 &glock.start, &glock.length, &glock.proc_id,
3126 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
3127 glock.type, glock.start, glock.length);
3129 fidp = get_fid(pdu, fid);
3134 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3138 glock.type = P9_LOCK_TYPE_UNLCK;
3139 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
3140 glock.start, glock.length, glock.proc_id,
3146 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
3147 glock.length, glock.proc_id);
3151 pdu_complete(pdu, err);
3152 v9fs_string_free(&glock.client_id);
3155 static void coroutine_fn v9fs_mkdir(void *opaque)
3157 V9fsPDU *pdu = opaque;
3168 v9fs_string_init(&name);
3169 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
3173 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
3175 if (name_is_illegal(name.data)) {
3180 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3185 fidp = get_fid(pdu, fid);
3190 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
3194 stat_to_qid(&stbuf, &qid);
3195 err = pdu_marshal(pdu, offset, "Q", &qid);
3200 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
3201 qid.type, qid.version, qid.path, err);
3205 pdu_complete(pdu, err);
3206 v9fs_string_free(&name);
3209 static void coroutine_fn v9fs_xattrwalk(void *opaque)
3215 int32_t fid, newfid;
3216 V9fsFidState *file_fidp;
3217 V9fsFidState *xattr_fidp = NULL;
3218 V9fsPDU *pdu = opaque;
3219 V9fsState *s = pdu->s;
3221 v9fs_string_init(&name);
3222 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3226 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3228 file_fidp = get_fid(pdu, fid);
3229 if (file_fidp == NULL) {
3233 xattr_fidp = alloc_fid(s, newfid);
3234 if (xattr_fidp == NULL) {
3238 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3239 if (!v9fs_string_size(&name)) {
3241 * listxattr request. Get the size first
3243 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3246 clunk_fid(s, xattr_fidp->fid);
3250 * Read the xattr value
3252 xattr_fidp->fs.xattr.len = size;
3253 xattr_fidp->fid_type = P9_FID_XATTR;
3254 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3256 xattr_fidp->fs.xattr.value = g_malloc0(size);
3257 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3258 xattr_fidp->fs.xattr.value,
3259 xattr_fidp->fs.xattr.len);
3261 clunk_fid(s, xattr_fidp->fid);
3265 err = pdu_marshal(pdu, offset, "q", size);
3272 * specific xattr fid. We check for xattr
3273 * presence also collect the xattr size
3275 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3279 clunk_fid(s, xattr_fidp->fid);
3283 * Read the xattr value
3285 xattr_fidp->fs.xattr.len = size;
3286 xattr_fidp->fid_type = P9_FID_XATTR;
3287 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3289 xattr_fidp->fs.xattr.value = g_malloc0(size);
3290 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3291 &name, xattr_fidp->fs.xattr.value,
3292 xattr_fidp->fs.xattr.len);
3294 clunk_fid(s, xattr_fidp->fid);
3298 err = pdu_marshal(pdu, offset, "q", size);
3304 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3306 put_fid(pdu, file_fidp);
3308 put_fid(pdu, xattr_fidp);
3311 pdu_complete(pdu, err);
3312 v9fs_string_free(&name);
3315 static void coroutine_fn v9fs_xattrcreate(void *opaque)
3323 V9fsFidState *file_fidp;
3324 V9fsFidState *xattr_fidp;
3325 V9fsPDU *pdu = opaque;
3327 v9fs_string_init(&name);
3328 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3332 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3334 if (size > XATTR_SIZE_MAX) {
3339 file_fidp = get_fid(pdu, fid);
3340 if (file_fidp == NULL) {
3344 if (file_fidp->fid_type != P9_FID_NONE) {
3349 /* Make the file fid point to xattr */
3350 xattr_fidp = file_fidp;
3351 xattr_fidp->fid_type = P9_FID_XATTR;
3352 xattr_fidp->fs.xattr.copied_len = 0;
3353 xattr_fidp->fs.xattr.xattrwalk_fid = false;
3354 xattr_fidp->fs.xattr.len = size;
3355 xattr_fidp->fs.xattr.flags = flags;
3356 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3357 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3358 xattr_fidp->fs.xattr.value = g_malloc0(size);
3361 put_fid(pdu, file_fidp);
3363 pdu_complete(pdu, err);
3364 v9fs_string_free(&name);
3367 static void coroutine_fn v9fs_readlink(void *opaque)
3369 V9fsPDU *pdu = opaque;
3376 err = pdu_unmarshal(pdu, offset, "d", &fid);
3380 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
3381 fidp = get_fid(pdu, fid);
3387 v9fs_string_init(&target);
3388 err = v9fs_co_readlink(pdu, &fidp->path, &target);
3392 err = pdu_marshal(pdu, offset, "s", &target);
3394 v9fs_string_free(&target);
3398 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
3399 v9fs_string_free(&target);
3403 pdu_complete(pdu, err);
3406 static CoroutineEntry *pdu_co_handlers[] = {
3407 [P9_TREADDIR] = v9fs_readdir,
3408 [P9_TSTATFS] = v9fs_statfs,
3409 [P9_TGETATTR] = v9fs_getattr,
3410 [P9_TSETATTR] = v9fs_setattr,
3411 [P9_TXATTRWALK] = v9fs_xattrwalk,
3412 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3413 [P9_TMKNOD] = v9fs_mknod,
3414 [P9_TRENAME] = v9fs_rename,
3415 [P9_TLOCK] = v9fs_lock,
3416 [P9_TGETLOCK] = v9fs_getlock,
3417 [P9_TRENAMEAT] = v9fs_renameat,
3418 [P9_TREADLINK] = v9fs_readlink,
3419 [P9_TUNLINKAT] = v9fs_unlinkat,
3420 [P9_TMKDIR] = v9fs_mkdir,
3421 [P9_TVERSION] = v9fs_version,
3422 [P9_TLOPEN] = v9fs_open,
3423 [P9_TATTACH] = v9fs_attach,
3424 [P9_TSTAT] = v9fs_stat,
3425 [P9_TWALK] = v9fs_walk,
3426 [P9_TCLUNK] = v9fs_clunk,
3427 [P9_TFSYNC] = v9fs_fsync,
3428 [P9_TOPEN] = v9fs_open,
3429 [P9_TREAD] = v9fs_read,
3431 [P9_TAUTH] = v9fs_auth,
3433 [P9_TFLUSH] = v9fs_flush,
3434 [P9_TLINK] = v9fs_link,
3435 [P9_TSYMLINK] = v9fs_symlink,
3436 [P9_TCREATE] = v9fs_create,
3437 [P9_TLCREATE] = v9fs_lcreate,
3438 [P9_TWRITE] = v9fs_write,
3439 [P9_TWSTAT] = v9fs_wstat,
3440 [P9_TREMOVE] = v9fs_remove,
3443 static void coroutine_fn v9fs_op_not_supp(void *opaque)
3445 V9fsPDU *pdu = opaque;
3446 pdu_complete(pdu, -EOPNOTSUPP);
3449 static void coroutine_fn v9fs_fs_ro(void *opaque)
3451 V9fsPDU *pdu = opaque;
3452 pdu_complete(pdu, -EROFS);
3455 static inline bool is_read_only_op(V9fsPDU *pdu)
3482 void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr)
3485 CoroutineEntry *handler;
3486 V9fsState *s = pdu->s;
3488 pdu->size = le32_to_cpu(hdr->size_le);
3490 pdu->tag = le16_to_cpu(hdr->tag_le);
3492 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3493 (pdu_co_handlers[pdu->id] == NULL)) {
3494 handler = v9fs_op_not_supp;
3495 } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3496 handler = v9fs_fs_ro;
3498 handler = pdu_co_handlers[pdu->id];
3501 qemu_co_queue_init(&pdu->complete);
3502 co = qemu_coroutine_create(handler, pdu);
3503 qemu_coroutine_enter(co);
3506 /* Returns 0 on success, 1 on failure. */
3507 int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
3516 assert(!s->transport);
3519 /* initialize pdu allocator */
3520 QLIST_INIT(&s->free_list);
3521 QLIST_INIT(&s->active_list);
3522 for (i = 0; i < MAX_REQ; i++) {
3523 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
3528 v9fs_path_init(&path);
3530 fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
3533 /* We don't have a fsdev identified by fsdev_id */
3534 error_setg(errp, "9pfs device couldn't find fsdev with the "
3536 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
3540 if (!s->fsconf.tag) {
3541 /* we haven't specified a mount_tag */
3542 error_setg(errp, "fsdev with id %s needs mount_tag arguments",
3543 s->fsconf.fsdev_id);
3547 s->ctx.export_flags = fse->export_flags;
3548 s->ctx.fs_root = g_strdup(fse->path);
3549 s->ctx.exops.get_st_gen = NULL;
3550 len = strlen(s->fsconf.tag);
3551 if (len > MAX_TAG_LEN - 1) {
3552 error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
3553 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
3557 s->tag = g_strdup(s->fsconf.tag);
3562 s->ctx.fmode = fse->fmode;
3563 s->ctx.dmode = fse->dmode;
3566 qemu_co_rwlock_init(&s->rename_lock);
3568 if (s->ops->init(&s->ctx, errp) < 0) {
3569 error_prepend(errp, "cannot initialize fsdev '%s': ",
3570 s->fsconf.fsdev_id);
3575 * Check details of export path, We need to use fs driver
3576 * call back to do that. Since we are in the init path, we don't
3577 * use co-routines here.
3579 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
3581 "error in converting name to path %s", strerror(errno));
3584 if (s->ops->lstat(&s->ctx, &path, &stat)) {
3585 error_setg(errp, "share path %s does not exist", fse->path);
3587 } else if (!S_ISDIR(stat.st_mode)) {
3588 error_setg(errp, "share path %s is not a directory", fse->path);
3592 s->ctx.fst = &fse->fst;
3593 fsdev_throttle_init(s->ctx.fst);
3595 v9fs_path_free(&path);
3600 if (s->ops && s->ops->cleanup && s->ctx.private) {
3601 s->ops->cleanup(&s->ctx);
3604 g_free(s->ctx.fs_root);
3605 v9fs_path_free(&path);
3610 void v9fs_device_unrealize_common(V9fsState *s, Error **errp)
3612 if (s->ops->cleanup) {
3613 s->ops->cleanup(&s->ctx);
3615 fsdev_throttle_cleanup(s->ctx.fst);
3617 g_free(s->ctx.fs_root);
3620 typedef struct VirtfsCoResetData {
3623 } VirtfsCoResetData;
3625 static void coroutine_fn virtfs_co_reset(void *opaque)
3627 VirtfsCoResetData *data = opaque;
3629 virtfs_reset(&data->pdu);
3633 void v9fs_reset(V9fsState *s)
3635 VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
3638 while (!QLIST_EMPTY(&s->active_list)) {
3639 aio_poll(qemu_get_aio_context(), true);
3642 co = qemu_coroutine_create(virtfs_co_reset, &data);
3643 qemu_coroutine_enter(co);
3645 while (!data.done) {
3646 aio_poll(qemu_get_aio_context(), true);
3650 static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
3653 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
3654 error_report("Failed to get the resource limit");
3657 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
3658 open_fd_rc = rlim.rlim_cur/2;