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/main-loop.h"
21 #include "qemu/sockets.h"
22 #include "virtio-9p.h"
23 #include "fsdev/qemu-fsdev.h"
27 #include "migration/blocker.h"
28 #include "sysemu/qtest.h"
32 static int open_fd_rc;
46 static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
52 ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap);
58 static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
64 ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap);
70 static int omode_to_uflags(int8_t mode)
104 typedef struct DotlOpenflagMap {
109 static int dotl_to_open_flags(int flags)
113 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
114 * and P9_DOTL_NOACCESS
116 int oflags = flags & O_ACCMODE;
118 DotlOpenflagMap dotl_oflag_map[] = {
119 { P9_DOTL_CREATE, O_CREAT },
120 { P9_DOTL_EXCL, O_EXCL },
121 { P9_DOTL_NOCTTY , O_NOCTTY },
122 { P9_DOTL_TRUNC, O_TRUNC },
123 { P9_DOTL_APPEND, O_APPEND },
124 { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
125 { P9_DOTL_DSYNC, O_DSYNC },
126 { P9_DOTL_FASYNC, FASYNC },
127 { P9_DOTL_DIRECT, O_DIRECT },
128 { P9_DOTL_LARGEFILE, O_LARGEFILE },
129 { P9_DOTL_DIRECTORY, O_DIRECTORY },
130 { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
131 { P9_DOTL_NOATIME, O_NOATIME },
132 { P9_DOTL_SYNC, O_SYNC },
135 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
136 if (flags & dotl_oflag_map[i].dotl_flag) {
137 oflags |= dotl_oflag_map[i].open_flag;
144 void cred_init(FsCred *credp)
152 static int get_dotl_openflags(V9fsState *s, int oflags)
156 * Filter the client open flags
158 flags = dotl_to_open_flags(oflags);
159 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
161 * Ignore direct disk access hint until the server supports it.
167 void v9fs_path_init(V9fsPath *path)
173 void v9fs_path_free(V9fsPath *path)
181 void GCC_FMT_ATTR(2, 3)
182 v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
186 v9fs_path_free(path);
189 /* Bump the size for including terminating NULL */
190 path->size = g_vasprintf(&path->data, fmt, ap) + 1;
194 void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src)
197 dst->size = src->size;
198 dst->data = g_memdup(src->data, src->size);
201 int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
202 const char *name, V9fsPath *path)
205 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
213 * Return TRUE if s1 is an ancestor of s2.
215 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
216 * As a special case, We treat s1 as ancestor of s2 if they are same!
218 static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
220 if (!strncmp(s1->data, s2->data, s1->size - 1)) {
221 if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
228 static size_t v9fs_string_size(V9fsString *str)
234 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
235 static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
238 if (f->fid_type == P9_FID_FILE) {
239 if (f->fs.fd == -1) {
241 err = v9fs_co_open(pdu, f, f->open_flags);
242 } while (err == -EINTR && !pdu->cancelled);
244 } else if (f->fid_type == P9_FID_DIR) {
245 if (f->fs.dir.stream == NULL) {
247 err = v9fs_co_opendir(pdu, f);
248 } while (err == -EINTR && !pdu->cancelled);
254 static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid)
258 V9fsState *s = pdu->s;
260 for (f = s->fid_list; f; f = f->next) {
264 * Update the fid ref upfront so that
265 * we don't get reclaimed when we yield
270 * check whether we need to reopen the
271 * file. We might have closed the fd
272 * while trying to free up some file
275 err = v9fs_reopen_fid(pdu, f);
281 * Mark the fid as referenced so that the LRU
282 * reclaim won't close the file descriptor
284 f->flags |= FID_REFERENCED;
291 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
295 for (f = s->fid_list; f; f = f->next) {
296 /* If fid is already there return NULL */
302 f = g_malloc0(sizeof(V9fsFidState));
304 f->fid_type = P9_FID_NONE;
307 * Mark the fid as referenced so that the LRU
308 * reclaim won't close the file descriptor
310 f->flags |= FID_REFERENCED;
311 f->next = s->fid_list;
314 v9fs_readdir_init(&f->fs.dir);
315 v9fs_readdir_init(&f->fs_reclaim.dir);
320 static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
324 if (fidp->fs.xattr.xattrwalk_fid) {
325 /* getxattr/listxattr fid */
329 * if this is fid for setxattr. clunk should
330 * result in setxattr localcall
332 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
333 /* clunk after partial write */
337 if (fidp->fs.xattr.len) {
338 retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
339 fidp->fs.xattr.value,
341 fidp->fs.xattr.flags);
343 retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
346 v9fs_string_free(&fidp->fs.xattr.name);
348 g_free(fidp->fs.xattr.value);
352 static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
356 if (fidp->fid_type == P9_FID_FILE) {
357 /* If we reclaimed the fd no need to close */
358 if (fidp->fs.fd != -1) {
359 retval = v9fs_co_close(pdu, &fidp->fs);
361 } else if (fidp->fid_type == P9_FID_DIR) {
362 if (fidp->fs.dir.stream != NULL) {
363 retval = v9fs_co_closedir(pdu, &fidp->fs);
365 } else if (fidp->fid_type == P9_FID_XATTR) {
366 retval = v9fs_xattr_fid_clunk(pdu, fidp);
368 v9fs_path_free(&fidp->path);
373 static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
378 * Don't free the fid if it is in reclaim list
380 if (!fidp->ref && fidp->clunked) {
381 if (fidp->fid == pdu->s->root_fid) {
383 * if the clunked fid is root fid then we
384 * have unmounted the fs on the client side.
385 * delete the migration blocker. Ideally, this
386 * should be hooked to transport close notification
388 if (pdu->s->migration_blocker) {
389 migrate_del_blocker(pdu->s->migration_blocker);
390 error_free(pdu->s->migration_blocker);
391 pdu->s->migration_blocker = NULL;
394 return free_fid(pdu, fidp);
399 static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
401 V9fsFidState **fidpp, *fidp;
403 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
404 if ((*fidpp)->fid == fid) {
408 if (*fidpp == NULL) {
417 void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)
419 int reclaim_count = 0;
420 V9fsState *s = pdu->s;
421 V9fsFidState *f, *reclaim_list = NULL;
423 for (f = s->fid_list; f; f = f->next) {
425 * Unlink fids cannot be reclaimed. Check
426 * for them and skip them. Also skip fids
427 * currently being operated on.
429 if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
433 * if it is a recently referenced fid
434 * we leave the fid untouched and clear the
435 * reference bit. We come back to it later
436 * in the next iteration. (a simple LRU without
437 * moving list elements around)
439 if (f->flags & FID_REFERENCED) {
440 f->flags &= ~FID_REFERENCED;
444 * Add fids to reclaim list.
446 if (f->fid_type == P9_FID_FILE) {
447 if (f->fs.fd != -1) {
449 * Up the reference count so that
450 * a clunk request won't free this fid
453 f->rclm_lst = reclaim_list;
455 f->fs_reclaim.fd = f->fs.fd;
459 } else if (f->fid_type == P9_FID_DIR) {
460 if (f->fs.dir.stream != NULL) {
462 * Up the reference count so that
463 * a clunk request won't free this fid
466 f->rclm_lst = reclaim_list;
468 f->fs_reclaim.dir.stream = f->fs.dir.stream;
469 f->fs.dir.stream = NULL;
473 if (reclaim_count >= open_fd_rc) {
478 * Now close the fid in reclaim list. Free them if they
479 * are already clunked.
481 while (reclaim_list) {
483 reclaim_list = f->rclm_lst;
484 if (f->fid_type == P9_FID_FILE) {
485 v9fs_co_close(pdu, &f->fs_reclaim);
486 } else if (f->fid_type == P9_FID_DIR) {
487 v9fs_co_closedir(pdu, &f->fs_reclaim);
491 * Now drop the fid reference, free it
498 static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
501 V9fsState *s = pdu->s;
502 V9fsFidState *fidp, head_fid;
504 head_fid.next = s->fid_list;
505 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
506 if (fidp->path.size != path->size) {
509 if (!memcmp(fidp->path.data, path->data, path->size)) {
510 /* Mark the fid non reclaimable. */
511 fidp->flags |= FID_NON_RECLAIMABLE;
513 /* reopen the file/dir if already closed */
514 err = v9fs_reopen_fid(pdu, fidp);
519 * Go back to head of fid list because
520 * the list could have got updated when
521 * switched to the worker thread
531 static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
533 V9fsState *s = pdu->s;
537 while (s->fid_list) {
543 s->fid_list = fidp->next;
550 #define P9_QID_TYPE_DIR 0x80
551 #define P9_QID_TYPE_SYMLINK 0x02
553 #define P9_STAT_MODE_DIR 0x80000000
554 #define P9_STAT_MODE_APPEND 0x40000000
555 #define P9_STAT_MODE_EXCL 0x20000000
556 #define P9_STAT_MODE_MOUNT 0x10000000
557 #define P9_STAT_MODE_AUTH 0x08000000
558 #define P9_STAT_MODE_TMP 0x04000000
559 #define P9_STAT_MODE_SYMLINK 0x02000000
560 #define P9_STAT_MODE_LINK 0x01000000
561 #define P9_STAT_MODE_DEVICE 0x00800000
562 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
563 #define P9_STAT_MODE_SOCKET 0x00100000
564 #define P9_STAT_MODE_SETUID 0x00080000
565 #define P9_STAT_MODE_SETGID 0x00040000
566 #define P9_STAT_MODE_SETVTX 0x00010000
568 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
569 P9_STAT_MODE_SYMLINK | \
570 P9_STAT_MODE_LINK | \
571 P9_STAT_MODE_DEVICE | \
572 P9_STAT_MODE_NAMED_PIPE | \
575 /* This is the algorithm from ufs in spfs */
576 static 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 trace_v9fs_setattr(pdu->tag, pdu->id, fid,
1200 v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid,
1201 v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec);
1203 fidp = get_fid(pdu, fid);
1208 if (v9iattr.valid & P9_ATTR_MODE) {
1209 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1214 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1215 struct timespec times[2];
1216 if (v9iattr.valid & P9_ATTR_ATIME) {
1217 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1218 times[0].tv_sec = v9iattr.atime_sec;
1219 times[0].tv_nsec = v9iattr.atime_nsec;
1221 times[0].tv_nsec = UTIME_NOW;
1224 times[0].tv_nsec = UTIME_OMIT;
1226 if (v9iattr.valid & P9_ATTR_MTIME) {
1227 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1228 times[1].tv_sec = v9iattr.mtime_sec;
1229 times[1].tv_nsec = v9iattr.mtime_nsec;
1231 times[1].tv_nsec = UTIME_NOW;
1234 times[1].tv_nsec = UTIME_OMIT;
1236 err = v9fs_co_utimensat(pdu, &fidp->path, times);
1242 * If the only valid entry in iattr is ctime we can call
1243 * chown(-1,-1) to update the ctime of the file
1245 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1246 ((v9iattr.valid & P9_ATTR_CTIME)
1247 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1248 if (!(v9iattr.valid & P9_ATTR_UID)) {
1251 if (!(v9iattr.valid & P9_ATTR_GID)) {
1254 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1260 if (v9iattr.valid & (P9_ATTR_SIZE)) {
1261 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1267 trace_v9fs_setattr_return(pdu->tag, pdu->id);
1271 pdu_complete(pdu, err);
1274 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1280 err = pdu_marshal(pdu, offset, "w", nwnames);
1285 for (i = 0; i < nwnames; i++) {
1286 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1295 static bool name_is_illegal(const char *name)
1297 return !*name || strchr(name, '/') != NULL;
1300 static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
1303 qid1->type != qid2->type ||
1304 qid1->version != qid2->version ||
1305 qid1->path != qid2->path;
1308 static void coroutine_fn v9fs_walk(void *opaque)
1311 V9fsQID *qids = NULL;
1313 V9fsPath dpath, path;
1317 int32_t fid, newfid;
1318 V9fsString *wnames = NULL;
1320 V9fsFidState *newfidp = NULL;
1321 V9fsPDU *pdu = opaque;
1322 V9fsState *s = pdu->s;
1325 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1327 pdu_complete(pdu, err);
1332 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1334 if (nwnames && nwnames <= P9_MAXWELEM) {
1335 wnames = g_new0(V9fsString, nwnames);
1336 qids = g_new0(V9fsQID, nwnames);
1337 for (i = 0; i < nwnames; i++) {
1338 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1342 if (name_is_illegal(wnames[i].data)) {
1348 } else if (nwnames > P9_MAXWELEM) {
1352 fidp = get_fid(pdu, fid);
1358 v9fs_path_init(&dpath);
1359 v9fs_path_init(&path);
1361 err = fid_to_qid(pdu, fidp, &qid);
1367 * Both dpath and path initially poin to fidp.
1368 * Needed to handle request with nwnames == 0
1370 v9fs_path_copy(&dpath, &fidp->path);
1371 v9fs_path_copy(&path, &fidp->path);
1372 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1373 if (not_same_qid(&pdu->s->root_qid, &qid) ||
1374 strcmp("..", wnames[name_idx].data)) {
1375 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
1381 err = v9fs_co_lstat(pdu, &path, &stbuf);
1385 stat_to_qid(&stbuf, &qid);
1386 v9fs_path_copy(&dpath, &path);
1388 memcpy(&qids[name_idx], &qid, sizeof(qid));
1390 if (fid == newfid) {
1391 if (fidp->fid_type != P9_FID_NONE) {
1395 v9fs_path_write_lock(s);
1396 v9fs_path_copy(&fidp->path, &path);
1397 v9fs_path_unlock(s);
1399 newfidp = alloc_fid(s, newfid);
1400 if (newfidp == NULL) {
1404 newfidp->uid = fidp->uid;
1405 v9fs_path_copy(&newfidp->path, &path);
1407 err = v9fs_walk_marshal(pdu, nwnames, qids);
1408 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1412 put_fid(pdu, newfidp);
1414 v9fs_path_free(&dpath);
1415 v9fs_path_free(&path);
1417 pdu_complete(pdu, err);
1418 if (nwnames && nwnames <= P9_MAXWELEM) {
1419 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1420 v9fs_string_free(&wnames[name_idx]);
1427 static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
1429 struct statfs stbuf;
1431 V9fsState *s = pdu->s;
1434 * iounit should be multiples of f_bsize (host filesystem block size
1435 * and as well as less than (client msize - P9_IOHDRSZ))
1437 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1438 iounit = stbuf.f_bsize;
1439 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1442 iounit = s->msize - P9_IOHDRSZ;
1447 static void coroutine_fn v9fs_open(void *opaque)
1458 V9fsPDU *pdu = opaque;
1459 V9fsState *s = pdu->s;
1461 if (s->proto_version == V9FS_PROTO_2000L) {
1462 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1465 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1471 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1473 fidp = get_fid(pdu, fid);
1478 if (fidp->fid_type != P9_FID_NONE) {
1483 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1487 stat_to_qid(&stbuf, &qid);
1488 if (S_ISDIR(stbuf.st_mode)) {
1489 err = v9fs_co_opendir(pdu, fidp);
1493 fidp->fid_type = P9_FID_DIR;
1494 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1500 if (s->proto_version == V9FS_PROTO_2000L) {
1501 flags = get_dotl_openflags(s, mode);
1503 flags = omode_to_uflags(mode);
1505 if (is_ro_export(&s->ctx)) {
1506 if (mode & O_WRONLY || mode & O_RDWR ||
1507 mode & O_APPEND || mode & O_TRUNC) {
1512 err = v9fs_co_open(pdu, fidp, flags);
1516 fidp->fid_type = P9_FID_FILE;
1517 fidp->open_flags = flags;
1518 if (flags & O_EXCL) {
1520 * We let the host file system do O_EXCL check
1521 * We should not reclaim such fd
1523 fidp->flags |= FID_NON_RECLAIMABLE;
1525 iounit = get_iounit(pdu, &fidp->path);
1526 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1532 trace_v9fs_open_return(pdu->tag, pdu->id,
1533 qid.type, qid.version, qid.path, iounit);
1537 pdu_complete(pdu, err);
1540 static void coroutine_fn v9fs_lcreate(void *opaque)
1542 int32_t dfid, flags, mode;
1551 V9fsPDU *pdu = opaque;
1553 v9fs_string_init(&name);
1554 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1555 &name, &flags, &mode, &gid);
1559 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1561 if (name_is_illegal(name.data)) {
1566 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
1571 fidp = get_fid(pdu, dfid);
1576 if (fidp->fid_type != P9_FID_NONE) {
1581 flags = get_dotl_openflags(pdu->s, flags);
1582 err = v9fs_co_open2(pdu, fidp, &name, gid,
1583 flags | O_CREAT, mode, &stbuf);
1587 fidp->fid_type = P9_FID_FILE;
1588 fidp->open_flags = flags;
1589 if (flags & O_EXCL) {
1591 * We let the host file system do O_EXCL check
1592 * We should not reclaim such fd
1594 fidp->flags |= FID_NON_RECLAIMABLE;
1596 iounit = get_iounit(pdu, &fidp->path);
1597 stat_to_qid(&stbuf, &qid);
1598 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1603 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
1604 qid.type, qid.version, qid.path, iounit);
1608 pdu_complete(pdu, err);
1609 v9fs_string_free(&name);
1612 static void coroutine_fn v9fs_fsync(void *opaque)
1619 V9fsPDU *pdu = opaque;
1621 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1625 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
1627 fidp = get_fid(pdu, fid);
1632 err = v9fs_co_fsync(pdu, fidp, datasync);
1638 pdu_complete(pdu, err);
1641 static void coroutine_fn v9fs_clunk(void *opaque)
1647 V9fsPDU *pdu = opaque;
1648 V9fsState *s = pdu->s;
1650 err = pdu_unmarshal(pdu, offset, "d", &fid);
1654 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
1656 fidp = clunk_fid(s, fid);
1662 * Bump the ref so that put_fid will
1666 err = put_fid(pdu, fidp);
1671 pdu_complete(pdu, err);
1675 * Create a QEMUIOVector for a sub-region of PDU iovecs
1677 * @qiov: uninitialized QEMUIOVector
1678 * @skip: number of bytes to skip from beginning of PDU
1679 * @size: number of bytes to include
1680 * @is_write: true - write, false - read
1682 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1683 * with qemu_iovec_destroy().
1685 static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1686 size_t skip, size_t size,
1694 pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip);
1696 pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip);
1699 qemu_iovec_init_external(&elem, iov, niov);
1700 qemu_iovec_init(qiov, niov);
1701 qemu_iovec_concat(qiov, &elem, skip, size);
1704 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1705 uint64_t off, uint32_t max_count)
1709 uint64_t read_count;
1710 QEMUIOVector qiov_full;
1712 if (fidp->fs.xattr.len < off) {
1715 read_count = fidp->fs.xattr.len - off;
1717 if (read_count > max_count) {
1718 read_count = max_count;
1720 err = pdu_marshal(pdu, offset, "d", read_count);
1726 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false);
1727 err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0,
1728 ((char *)fidp->fs.xattr.value) + off,
1730 qemu_iovec_destroy(&qiov_full);
1738 static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1747 off_t saved_dir_pos;
1748 struct dirent *dent;
1750 /* save the directory position */
1751 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1752 if (saved_dir_pos < 0) {
1753 return saved_dir_pos;
1757 v9fs_path_init(&path);
1759 v9fs_readdir_lock(&fidp->fs.dir);
1761 err = v9fs_co_readdir(pdu, fidp, &dent);
1765 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
1769 err = v9fs_co_lstat(pdu, &path, &stbuf);
1773 err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat);
1777 if ((count + v9stat.size + 2) > max_count) {
1778 v9fs_readdir_unlock(&fidp->fs.dir);
1780 /* Ran out of buffer. Set dir back to old position and return */
1781 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1782 v9fs_stat_free(&v9stat);
1783 v9fs_path_free(&path);
1787 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1788 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1790 v9fs_readdir_unlock(&fidp->fs.dir);
1793 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1794 v9fs_stat_free(&v9stat);
1795 v9fs_path_free(&path);
1799 v9fs_stat_free(&v9stat);
1800 v9fs_path_free(&path);
1801 saved_dir_pos = dent->d_off;
1804 v9fs_readdir_unlock(&fidp->fs.dir);
1806 v9fs_path_free(&path);
1813 static void coroutine_fn v9fs_read(void *opaque)
1822 V9fsPDU *pdu = opaque;
1823 V9fsState *s = pdu->s;
1825 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1829 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
1831 fidp = get_fid(pdu, fid);
1836 if (fidp->fid_type == P9_FID_DIR) {
1839 v9fs_co_rewinddir(pdu, fidp);
1841 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
1846 err = pdu_marshal(pdu, offset, "d", count);
1850 err += offset + count;
1851 } else if (fidp->fid_type == P9_FID_FILE) {
1852 QEMUIOVector qiov_full;
1856 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
1857 qemu_iovec_init(&qiov, qiov_full.niov);
1859 qemu_iovec_reset(&qiov);
1860 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
1862 print_sg(qiov.iov, qiov.niov);
1864 /* Loop in case of EINTR */
1866 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
1871 } while (len == -EINTR && !pdu->cancelled);
1873 /* IO error return the error */
1875 goto out_free_iovec;
1877 } while (count < max_count && len > 0);
1878 err = pdu_marshal(pdu, offset, "d", count);
1880 goto out_free_iovec;
1882 err += offset + count;
1884 qemu_iovec_destroy(&qiov);
1885 qemu_iovec_destroy(&qiov_full);
1886 } else if (fidp->fid_type == P9_FID_XATTR) {
1887 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
1891 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
1895 pdu_complete(pdu, err);
1898 static size_t v9fs_readdir_data_size(V9fsString *name)
1901 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1902 * size of type (1) + size of name.size (2) + strlen(name.data)
1904 return 24 + v9fs_string_size(name);
1907 static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
1915 off_t saved_dir_pos;
1916 struct dirent *dent;
1918 /* save the directory position */
1919 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1920 if (saved_dir_pos < 0) {
1921 return saved_dir_pos;
1925 v9fs_readdir_lock(&fidp->fs.dir);
1927 err = v9fs_co_readdir(pdu, fidp, &dent);
1931 v9fs_string_init(&name);
1932 v9fs_string_sprintf(&name, "%s", dent->d_name);
1933 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
1934 v9fs_readdir_unlock(&fidp->fs.dir);
1936 /* Ran out of buffer. Set dir back to old position and return */
1937 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1938 v9fs_string_free(&name);
1942 * Fill up just the path field of qid because the client uses
1943 * only that. To fill the entire qid structure we will have
1944 * to stat each dirent found, which is expensive
1946 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1947 memcpy(&qid.path, &dent->d_ino, size);
1948 /* Fill the other fields with dummy values */
1952 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1953 len = pdu_marshal(pdu, 11 + count, "Qqbs",
1955 dent->d_type, &name);
1957 v9fs_readdir_unlock(&fidp->fs.dir);
1960 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1961 v9fs_string_free(&name);
1965 v9fs_string_free(&name);
1966 saved_dir_pos = dent->d_off;
1969 v9fs_readdir_unlock(&fidp->fs.dir);
1977 static void coroutine_fn v9fs_readdir(void *opaque)
1983 uint64_t initial_offset;
1986 V9fsPDU *pdu = opaque;
1988 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
1989 &initial_offset, &max_count);
1993 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
1995 fidp = get_fid(pdu, fid);
2000 if (!fidp->fs.dir.stream) {
2004 if (initial_offset == 0) {
2005 v9fs_co_rewinddir(pdu, fidp);
2007 v9fs_co_seekdir(pdu, fidp, initial_offset);
2009 count = v9fs_do_readdir(pdu, fidp, max_count);
2014 retval = pdu_marshal(pdu, offset, "d", count);
2018 retval += count + offset;
2019 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
2023 pdu_complete(pdu, retval);
2026 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
2027 uint64_t off, uint32_t count,
2028 struct iovec *sg, int cnt)
2032 uint64_t write_count;
2036 if (fidp->fs.xattr.len < off) {
2040 write_count = fidp->fs.xattr.len - off;
2041 if (write_count > count) {
2042 write_count = count;
2044 err = pdu_marshal(pdu, offset, "d", write_count);
2049 fidp->fs.xattr.copied_len += write_count;
2051 * Now copy the content from sg list
2053 for (i = 0; i < cnt; i++) {
2054 if (write_count > sg[i].iov_len) {
2055 to_copy = sg[i].iov_len;
2057 to_copy = write_count;
2059 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
2060 /* updating vs->off since we are not using below */
2062 write_count -= to_copy;
2068 static void coroutine_fn v9fs_write(void *opaque)
2078 V9fsPDU *pdu = opaque;
2079 V9fsState *s = pdu->s;
2080 QEMUIOVector qiov_full;
2083 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
2085 pdu_complete(pdu, err);
2089 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
2090 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
2092 fidp = get_fid(pdu, fid);
2097 if (fidp->fid_type == P9_FID_FILE) {
2098 if (fidp->fs.fd == -1) {
2102 } else if (fidp->fid_type == P9_FID_XATTR) {
2104 * setxattr operation
2106 err = v9fs_xattr_write(s, pdu, fidp, off, count,
2107 qiov_full.iov, qiov_full.niov);
2113 qemu_iovec_init(&qiov, qiov_full.niov);
2115 qemu_iovec_reset(&qiov);
2116 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
2118 print_sg(qiov.iov, qiov.niov);
2120 /* Loop in case of EINTR */
2122 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
2127 } while (len == -EINTR && !pdu->cancelled);
2129 /* IO error return the error */
2133 } while (total < count && len > 0);
2136 err = pdu_marshal(pdu, offset, "d", total);
2141 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2143 qemu_iovec_destroy(&qiov);
2147 qemu_iovec_destroy(&qiov_full);
2148 pdu_complete(pdu, err);
2151 static void coroutine_fn v9fs_create(void *opaque)
2163 V9fsString extension;
2165 V9fsPDU *pdu = opaque;
2166 V9fsState *s = pdu->s;
2168 v9fs_path_init(&path);
2169 v9fs_string_init(&name);
2170 v9fs_string_init(&extension);
2171 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2172 &perm, &mode, &extension);
2176 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2178 if (name_is_illegal(name.data)) {
2183 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2188 fidp = get_fid(pdu, fid);
2193 if (fidp->fid_type != P9_FID_NONE) {
2197 if (perm & P9_STAT_MODE_DIR) {
2198 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2199 fidp->uid, -1, &stbuf);
2203 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2207 v9fs_path_write_lock(s);
2208 v9fs_path_copy(&fidp->path, &path);
2209 v9fs_path_unlock(s);
2210 err = v9fs_co_opendir(pdu, fidp);
2214 fidp->fid_type = P9_FID_DIR;
2215 } else if (perm & P9_STAT_MODE_SYMLINK) {
2216 err = v9fs_co_symlink(pdu, fidp, &name,
2217 extension.data, -1 , &stbuf);
2221 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2225 v9fs_path_write_lock(s);
2226 v9fs_path_copy(&fidp->path, &path);
2227 v9fs_path_unlock(s);
2228 } else if (perm & P9_STAT_MODE_LINK) {
2229 int32_t ofid = atoi(extension.data);
2230 V9fsFidState *ofidp = get_fid(pdu, ofid);
2231 if (ofidp == NULL) {
2235 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2236 put_fid(pdu, ofidp);
2240 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2242 fidp->fid_type = P9_FID_NONE;
2245 v9fs_path_write_lock(s);
2246 v9fs_path_copy(&fidp->path, &path);
2247 v9fs_path_unlock(s);
2248 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2250 fidp->fid_type = P9_FID_NONE;
2253 } else if (perm & P9_STAT_MODE_DEVICE) {
2255 uint32_t major, minor;
2258 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2275 nmode |= perm & 0777;
2276 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2277 makedev(major, minor), nmode, &stbuf);
2281 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2285 v9fs_path_write_lock(s);
2286 v9fs_path_copy(&fidp->path, &path);
2287 v9fs_path_unlock(s);
2288 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2289 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2290 0, S_IFIFO | (perm & 0777), &stbuf);
2294 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2298 v9fs_path_write_lock(s);
2299 v9fs_path_copy(&fidp->path, &path);
2300 v9fs_path_unlock(s);
2301 } else if (perm & P9_STAT_MODE_SOCKET) {
2302 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2303 0, S_IFSOCK | (perm & 0777), &stbuf);
2307 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2311 v9fs_path_write_lock(s);
2312 v9fs_path_copy(&fidp->path, &path);
2313 v9fs_path_unlock(s);
2315 err = v9fs_co_open2(pdu, fidp, &name, -1,
2316 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2320 fidp->fid_type = P9_FID_FILE;
2321 fidp->open_flags = omode_to_uflags(mode);
2322 if (fidp->open_flags & O_EXCL) {
2324 * We let the host file system do O_EXCL check
2325 * We should not reclaim such fd
2327 fidp->flags |= FID_NON_RECLAIMABLE;
2330 iounit = get_iounit(pdu, &fidp->path);
2331 stat_to_qid(&stbuf, &qid);
2332 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2337 trace_v9fs_create_return(pdu->tag, pdu->id,
2338 qid.type, qid.version, qid.path, iounit);
2342 pdu_complete(pdu, err);
2343 v9fs_string_free(&name);
2344 v9fs_string_free(&extension);
2345 v9fs_path_free(&path);
2348 static void coroutine_fn v9fs_symlink(void *opaque)
2350 V9fsPDU *pdu = opaque;
2353 V9fsFidState *dfidp;
2361 v9fs_string_init(&name);
2362 v9fs_string_init(&symname);
2363 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2367 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2369 if (name_is_illegal(name.data)) {
2374 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2379 dfidp = get_fid(pdu, dfid);
2380 if (dfidp == NULL) {
2384 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2388 stat_to_qid(&stbuf, &qid);
2389 err = pdu_marshal(pdu, offset, "Q", &qid);
2394 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2395 qid.type, qid.version, qid.path);
2397 put_fid(pdu, dfidp);
2399 pdu_complete(pdu, err);
2400 v9fs_string_free(&name);
2401 v9fs_string_free(&symname);
2404 static void coroutine_fn v9fs_flush(void *opaque)
2409 V9fsPDU *cancel_pdu = NULL;
2410 V9fsPDU *pdu = opaque;
2411 V9fsState *s = pdu->s;
2413 err = pdu_unmarshal(pdu, offset, "w", &tag);
2415 pdu_complete(pdu, err);
2418 trace_v9fs_flush(pdu->tag, pdu->id, tag);
2420 if (pdu->tag == tag) {
2421 warn_report("the guest sent a self-referencing 9P flush request");
2423 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2424 if (cancel_pdu->tag == tag) {
2430 cancel_pdu->cancelled = 1;
2432 * Wait for pdu to complete.
2434 qemu_co_queue_wait(&cancel_pdu->complete, NULL);
2435 if (!qemu_co_queue_next(&cancel_pdu->complete)) {
2436 cancel_pdu->cancelled = 0;
2437 pdu_free(cancel_pdu);
2440 pdu_complete(pdu, 7);
2443 static void coroutine_fn v9fs_link(void *opaque)
2445 V9fsPDU *pdu = opaque;
2446 int32_t dfid, oldfid;
2447 V9fsFidState *dfidp, *oldfidp;
2452 v9fs_string_init(&name);
2453 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2457 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2459 if (name_is_illegal(name.data)) {
2464 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2469 dfidp = get_fid(pdu, dfid);
2470 if (dfidp == NULL) {
2475 oldfidp = get_fid(pdu, oldfid);
2476 if (oldfidp == NULL) {
2480 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2484 put_fid(pdu, oldfidp);
2486 put_fid(pdu, dfidp);
2488 v9fs_string_free(&name);
2489 pdu_complete(pdu, err);
2492 /* Only works with path name based fid */
2493 static void coroutine_fn v9fs_remove(void *opaque)
2499 V9fsPDU *pdu = opaque;
2501 err = pdu_unmarshal(pdu, offset, "d", &fid);
2505 trace_v9fs_remove(pdu->tag, pdu->id, fid);
2507 fidp = get_fid(pdu, fid);
2512 /* if fs driver is not path based, return EOPNOTSUPP */
2513 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2518 * IF the file is unlinked, we cannot reopen
2519 * the file later. So don't reclaim fd
2521 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2525 err = v9fs_co_remove(pdu, &fidp->path);
2530 /* For TREMOVE we need to clunk the fid even on failed remove */
2531 clunk_fid(pdu->s, fidp->fid);
2534 pdu_complete(pdu, err);
2537 static void coroutine_fn v9fs_unlinkat(void *opaque)
2541 int32_t dfid, flags, rflags = 0;
2544 V9fsFidState *dfidp;
2545 V9fsPDU *pdu = opaque;
2547 v9fs_string_init(&name);
2548 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2553 if (name_is_illegal(name.data)) {
2558 if (!strcmp(".", name.data)) {
2563 if (!strcmp("..", name.data)) {
2568 if (flags & ~P9_DOTL_AT_REMOVEDIR) {
2573 if (flags & P9_DOTL_AT_REMOVEDIR) {
2574 rflags |= AT_REMOVEDIR;
2577 dfidp = get_fid(pdu, dfid);
2578 if (dfidp == NULL) {
2583 * IF the file is unlinked, we cannot reopen
2584 * the file later. So don't reclaim fd
2586 v9fs_path_init(&path);
2587 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2591 err = v9fs_mark_fids_unreclaim(pdu, &path);
2595 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags);
2600 put_fid(pdu, dfidp);
2601 v9fs_path_free(&path);
2603 pdu_complete(pdu, err);
2604 v9fs_string_free(&name);
2608 /* Only works with path name based fid */
2609 static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2615 V9fsFidState *tfidp;
2616 V9fsState *s = pdu->s;
2617 V9fsFidState *dirfidp = NULL;
2619 v9fs_path_init(&new_path);
2620 if (newdirfid != -1) {
2621 dirfidp = get_fid(pdu, newdirfid);
2622 if (dirfidp == NULL) {
2626 if (fidp->fid_type != P9_FID_NONE) {
2630 err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2635 char *dir_name = g_path_get_dirname(fidp->path.data);
2638 v9fs_path_init(&dir_path);
2639 v9fs_path_sprintf(&dir_path, "%s", dir_name);
2642 err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path);
2643 v9fs_path_free(&dir_path);
2648 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2653 * Fixup fid's pointing to the old name to
2654 * start pointing to the new name
2656 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2657 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2658 /* replace the name */
2659 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
2664 put_fid(pdu, dirfidp);
2666 v9fs_path_free(&new_path);
2671 /* Only works with path name based fid */
2672 static void coroutine_fn v9fs_rename(void *opaque)
2680 V9fsPDU *pdu = opaque;
2681 V9fsState *s = pdu->s;
2683 v9fs_string_init(&name);
2684 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2689 if (name_is_illegal(name.data)) {
2694 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2699 fidp = get_fid(pdu, fid);
2704 if (fidp->fid_type != P9_FID_NONE) {
2708 /* if fs driver is not path based, return EOPNOTSUPP */
2709 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2713 v9fs_path_write_lock(s);
2714 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
2715 v9fs_path_unlock(s);
2722 pdu_complete(pdu, err);
2723 v9fs_string_free(&name);
2726 static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2727 V9fsString *old_name,
2729 V9fsString *new_name)
2731 V9fsFidState *tfidp;
2732 V9fsPath oldpath, newpath;
2733 V9fsState *s = pdu->s;
2736 v9fs_path_init(&oldpath);
2737 v9fs_path_init(&newpath);
2738 err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
2742 err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2748 * Fixup fid's pointing to the old name to
2749 * start pointing to the new name
2751 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2752 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
2753 /* replace the name */
2754 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
2758 v9fs_path_free(&oldpath);
2759 v9fs_path_free(&newpath);
2763 static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2764 V9fsString *old_name,
2766 V9fsString *new_name)
2769 V9fsState *s = pdu->s;
2770 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2772 olddirfidp = get_fid(pdu, olddirfid);
2773 if (olddirfidp == NULL) {
2777 if (newdirfid != -1) {
2778 newdirfidp = get_fid(pdu, newdirfid);
2779 if (newdirfidp == NULL) {
2784 newdirfidp = get_fid(pdu, olddirfid);
2787 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2788 &newdirfidp->path, new_name);
2792 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
2793 /* Only for path based fid we need to do the below fixup */
2794 err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2795 &newdirfidp->path, new_name);
2799 put_fid(pdu, olddirfidp);
2802 put_fid(pdu, newdirfidp);
2807 static void coroutine_fn v9fs_renameat(void *opaque)
2811 V9fsPDU *pdu = opaque;
2812 V9fsState *s = pdu->s;
2813 int32_t olddirfid, newdirfid;
2814 V9fsString old_name, new_name;
2816 v9fs_string_init(&old_name);
2817 v9fs_string_init(&new_name);
2818 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2819 &old_name, &newdirfid, &new_name);
2824 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
2829 if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
2830 !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
2835 v9fs_path_write_lock(s);
2836 err = v9fs_complete_renameat(pdu, olddirfid,
2837 &old_name, newdirfid, &new_name);
2838 v9fs_path_unlock(s);
2844 pdu_complete(pdu, err);
2845 v9fs_string_free(&old_name);
2846 v9fs_string_free(&new_name);
2849 static void coroutine_fn v9fs_wstat(void *opaque)
2858 V9fsPDU *pdu = opaque;
2859 V9fsState *s = pdu->s;
2861 v9fs_stat_init(&v9stat);
2862 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
2866 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
2867 v9stat.mode, v9stat.atime, v9stat.mtime);
2869 fidp = get_fid(pdu, fid);
2874 /* do we need to sync the file? */
2875 if (donttouch_stat(&v9stat)) {
2876 err = v9fs_co_fsync(pdu, fidp, 0);
2879 if (v9stat.mode != -1) {
2881 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2885 v9_mode = stat_to_v9mode(&stbuf);
2886 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2887 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2888 /* Attempting to change the type */
2892 err = v9fs_co_chmod(pdu, &fidp->path,
2893 v9mode_to_mode(v9stat.mode,
2894 &v9stat.extension));
2899 if (v9stat.mtime != -1 || v9stat.atime != -1) {
2900 struct timespec times[2];
2901 if (v9stat.atime != -1) {
2902 times[0].tv_sec = v9stat.atime;
2903 times[0].tv_nsec = 0;
2905 times[0].tv_nsec = UTIME_OMIT;
2907 if (v9stat.mtime != -1) {
2908 times[1].tv_sec = v9stat.mtime;
2909 times[1].tv_nsec = 0;
2911 times[1].tv_nsec = UTIME_OMIT;
2913 err = v9fs_co_utimensat(pdu, &fidp->path, times);
2918 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
2919 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
2924 if (v9stat.name.size != 0) {
2925 v9fs_path_write_lock(s);
2926 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
2927 v9fs_path_unlock(s);
2932 if (v9stat.length != -1) {
2933 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
2942 v9fs_stat_free(&v9stat);
2943 pdu_complete(pdu, err);
2946 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2958 int32_t bsize_factor;
2961 * compute bsize factor based on host file system block size
2964 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2965 if (!bsize_factor) {
2968 f_type = stbuf->f_type;
2969 f_bsize = stbuf->f_bsize;
2970 f_bsize *= bsize_factor;
2972 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2973 * adjust(divide) the number of blocks, free blocks and available
2974 * blocks by bsize factor
2976 f_blocks = stbuf->f_blocks/bsize_factor;
2977 f_bfree = stbuf->f_bfree/bsize_factor;
2978 f_bavail = stbuf->f_bavail/bsize_factor;
2979 f_files = stbuf->f_files;
2980 f_ffree = stbuf->f_ffree;
2981 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
2982 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
2983 f_namelen = stbuf->f_namelen;
2985 return pdu_marshal(pdu, offset, "ddqqqqqqd",
2986 f_type, f_bsize, f_blocks, f_bfree,
2987 f_bavail, f_files, f_ffree,
2988 fsid_val, f_namelen);
2991 static void coroutine_fn v9fs_statfs(void *opaque)
2997 struct statfs stbuf;
2998 V9fsPDU *pdu = opaque;
2999 V9fsState *s = pdu->s;
3001 retval = pdu_unmarshal(pdu, offset, "d", &fid);
3005 fidp = get_fid(pdu, fid);
3010 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
3014 retval = v9fs_fill_statfs(s, pdu, &stbuf);
3022 pdu_complete(pdu, retval);
3025 static void coroutine_fn v9fs_mknod(void *opaque)
3038 V9fsPDU *pdu = opaque;
3040 v9fs_string_init(&name);
3041 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
3042 &major, &minor, &gid);
3046 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
3048 if (name_is_illegal(name.data)) {
3053 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3058 fidp = get_fid(pdu, fid);
3063 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
3064 makedev(major, minor), mode, &stbuf);
3068 stat_to_qid(&stbuf, &qid);
3069 err = pdu_marshal(pdu, offset, "Q", &qid);
3074 trace_v9fs_mknod_return(pdu->tag, pdu->id,
3075 qid.type, qid.version, qid.path);
3079 pdu_complete(pdu, err);
3080 v9fs_string_free(&name);
3084 * Implement posix byte range locking code
3085 * Server side handling of locking code is very simple, because 9p server in
3086 * QEMU can handle only one client. And most of the lock handling
3087 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3088 * do any thing in * qemu 9p server side lock code path.
3089 * So when a TLOCK request comes, always return success
3091 static void coroutine_fn v9fs_lock(void *opaque)
3097 int32_t fid, err = 0;
3098 V9fsPDU *pdu = opaque;
3100 v9fs_string_init(&flock.client_id);
3101 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
3102 &flock.flags, &flock.start, &flock.length,
3103 &flock.proc_id, &flock.client_id);
3107 trace_v9fs_lock(pdu->tag, pdu->id, fid,
3108 flock.type, flock.start, flock.length);
3111 /* We support only block flag now (that too ignored currently) */
3112 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
3116 fidp = get_fid(pdu, fid);
3121 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3125 err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS);
3130 trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS);
3134 pdu_complete(pdu, err);
3135 v9fs_string_free(&flock.client_id);
3139 * When a TGETLOCK request comes, always return success because all lock
3140 * handling is done by client's VFS layer.
3142 static void coroutine_fn v9fs_getlock(void *opaque)
3148 int32_t fid, err = 0;
3149 V9fsPDU *pdu = opaque;
3151 v9fs_string_init(&glock.client_id);
3152 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
3153 &glock.start, &glock.length, &glock.proc_id,
3158 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
3159 glock.type, glock.start, glock.length);
3161 fidp = get_fid(pdu, fid);
3166 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3170 glock.type = P9_LOCK_TYPE_UNLCK;
3171 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
3172 glock.start, glock.length, glock.proc_id,
3178 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
3179 glock.length, glock.proc_id);
3183 pdu_complete(pdu, err);
3184 v9fs_string_free(&glock.client_id);
3187 static void coroutine_fn v9fs_mkdir(void *opaque)
3189 V9fsPDU *pdu = opaque;
3200 v9fs_string_init(&name);
3201 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
3205 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
3207 if (name_is_illegal(name.data)) {
3212 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3217 fidp = get_fid(pdu, fid);
3222 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
3226 stat_to_qid(&stbuf, &qid);
3227 err = pdu_marshal(pdu, offset, "Q", &qid);
3232 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
3233 qid.type, qid.version, qid.path, err);
3237 pdu_complete(pdu, err);
3238 v9fs_string_free(&name);
3241 static void coroutine_fn v9fs_xattrwalk(void *opaque)
3247 int32_t fid, newfid;
3248 V9fsFidState *file_fidp;
3249 V9fsFidState *xattr_fidp = NULL;
3250 V9fsPDU *pdu = opaque;
3251 V9fsState *s = pdu->s;
3253 v9fs_string_init(&name);
3254 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3258 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3260 file_fidp = get_fid(pdu, fid);
3261 if (file_fidp == NULL) {
3265 xattr_fidp = alloc_fid(s, newfid);
3266 if (xattr_fidp == NULL) {
3270 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3271 if (!v9fs_string_size(&name)) {
3273 * listxattr request. Get the size first
3275 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3278 clunk_fid(s, xattr_fidp->fid);
3282 * Read the xattr value
3284 xattr_fidp->fs.xattr.len = size;
3285 xattr_fidp->fid_type = P9_FID_XATTR;
3286 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3287 xattr_fidp->fs.xattr.value = g_malloc0(size);
3289 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3290 xattr_fidp->fs.xattr.value,
3291 xattr_fidp->fs.xattr.len);
3293 clunk_fid(s, xattr_fidp->fid);
3297 err = pdu_marshal(pdu, offset, "q", size);
3304 * specific xattr fid. We check for xattr
3305 * presence also collect the xattr size
3307 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3311 clunk_fid(s, xattr_fidp->fid);
3315 * Read the xattr value
3317 xattr_fidp->fs.xattr.len = size;
3318 xattr_fidp->fid_type = P9_FID_XATTR;
3319 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3320 xattr_fidp->fs.xattr.value = g_malloc0(size);
3322 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3323 &name, xattr_fidp->fs.xattr.value,
3324 xattr_fidp->fs.xattr.len);
3326 clunk_fid(s, xattr_fidp->fid);
3330 err = pdu_marshal(pdu, offset, "q", size);
3336 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3338 put_fid(pdu, file_fidp);
3340 put_fid(pdu, xattr_fidp);
3343 pdu_complete(pdu, err);
3344 v9fs_string_free(&name);
3347 static void coroutine_fn v9fs_xattrcreate(void *opaque)
3349 int flags, rflags = 0;
3355 V9fsFidState *file_fidp;
3356 V9fsFidState *xattr_fidp;
3357 V9fsPDU *pdu = opaque;
3359 v9fs_string_init(&name);
3360 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3364 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3366 if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) {
3371 if (flags & P9_XATTR_CREATE) {
3372 rflags |= XATTR_CREATE;
3375 if (flags & P9_XATTR_REPLACE) {
3376 rflags |= XATTR_REPLACE;
3379 if (size > XATTR_SIZE_MAX) {
3384 file_fidp = get_fid(pdu, fid);
3385 if (file_fidp == NULL) {
3389 if (file_fidp->fid_type != P9_FID_NONE) {
3394 /* Make the file fid point to xattr */
3395 xattr_fidp = file_fidp;
3396 xattr_fidp->fid_type = P9_FID_XATTR;
3397 xattr_fidp->fs.xattr.copied_len = 0;
3398 xattr_fidp->fs.xattr.xattrwalk_fid = false;
3399 xattr_fidp->fs.xattr.len = size;
3400 xattr_fidp->fs.xattr.flags = rflags;
3401 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3402 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3403 xattr_fidp->fs.xattr.value = g_malloc0(size);
3406 put_fid(pdu, file_fidp);
3408 pdu_complete(pdu, err);
3409 v9fs_string_free(&name);
3412 static void coroutine_fn v9fs_readlink(void *opaque)
3414 V9fsPDU *pdu = opaque;
3421 err = pdu_unmarshal(pdu, offset, "d", &fid);
3425 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
3426 fidp = get_fid(pdu, fid);
3432 v9fs_string_init(&target);
3433 err = v9fs_co_readlink(pdu, &fidp->path, &target);
3437 err = pdu_marshal(pdu, offset, "s", &target);
3439 v9fs_string_free(&target);
3443 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
3444 v9fs_string_free(&target);
3448 pdu_complete(pdu, err);
3451 static CoroutineEntry *pdu_co_handlers[] = {
3452 [P9_TREADDIR] = v9fs_readdir,
3453 [P9_TSTATFS] = v9fs_statfs,
3454 [P9_TGETATTR] = v9fs_getattr,
3455 [P9_TSETATTR] = v9fs_setattr,
3456 [P9_TXATTRWALK] = v9fs_xattrwalk,
3457 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3458 [P9_TMKNOD] = v9fs_mknod,
3459 [P9_TRENAME] = v9fs_rename,
3460 [P9_TLOCK] = v9fs_lock,
3461 [P9_TGETLOCK] = v9fs_getlock,
3462 [P9_TRENAMEAT] = v9fs_renameat,
3463 [P9_TREADLINK] = v9fs_readlink,
3464 [P9_TUNLINKAT] = v9fs_unlinkat,
3465 [P9_TMKDIR] = v9fs_mkdir,
3466 [P9_TVERSION] = v9fs_version,
3467 [P9_TLOPEN] = v9fs_open,
3468 [P9_TATTACH] = v9fs_attach,
3469 [P9_TSTAT] = v9fs_stat,
3470 [P9_TWALK] = v9fs_walk,
3471 [P9_TCLUNK] = v9fs_clunk,
3472 [P9_TFSYNC] = v9fs_fsync,
3473 [P9_TOPEN] = v9fs_open,
3474 [P9_TREAD] = v9fs_read,
3476 [P9_TAUTH] = v9fs_auth,
3478 [P9_TFLUSH] = v9fs_flush,
3479 [P9_TLINK] = v9fs_link,
3480 [P9_TSYMLINK] = v9fs_symlink,
3481 [P9_TCREATE] = v9fs_create,
3482 [P9_TLCREATE] = v9fs_lcreate,
3483 [P9_TWRITE] = v9fs_write,
3484 [P9_TWSTAT] = v9fs_wstat,
3485 [P9_TREMOVE] = v9fs_remove,
3488 static void coroutine_fn v9fs_op_not_supp(void *opaque)
3490 V9fsPDU *pdu = opaque;
3491 pdu_complete(pdu, -EOPNOTSUPP);
3494 static void coroutine_fn v9fs_fs_ro(void *opaque)
3496 V9fsPDU *pdu = opaque;
3497 pdu_complete(pdu, -EROFS);
3500 static inline bool is_read_only_op(V9fsPDU *pdu)
3527 void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr)
3530 CoroutineEntry *handler;
3531 V9fsState *s = pdu->s;
3533 pdu->size = le32_to_cpu(hdr->size_le);
3535 pdu->tag = le16_to_cpu(hdr->tag_le);
3537 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3538 (pdu_co_handlers[pdu->id] == NULL)) {
3539 handler = v9fs_op_not_supp;
3540 } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3541 handler = v9fs_fs_ro;
3543 handler = pdu_co_handlers[pdu->id];
3546 qemu_co_queue_init(&pdu->complete);
3547 co = qemu_coroutine_create(handler, pdu);
3548 qemu_coroutine_enter(co);
3551 /* Returns 0 on success, 1 on failure. */
3552 int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
3561 assert(!s->transport);
3564 /* initialize pdu allocator */
3565 QLIST_INIT(&s->free_list);
3566 QLIST_INIT(&s->active_list);
3567 for (i = 0; i < MAX_REQ; i++) {
3568 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
3573 v9fs_path_init(&path);
3575 fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
3578 /* We don't have a fsdev identified by fsdev_id */
3579 error_setg(errp, "9pfs device couldn't find fsdev with the "
3581 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
3585 if (!s->fsconf.tag) {
3586 /* we haven't specified a mount_tag */
3587 error_setg(errp, "fsdev with id %s needs mount_tag arguments",
3588 s->fsconf.fsdev_id);
3592 s->ctx.export_flags = fse->export_flags;
3593 s->ctx.fs_root = g_strdup(fse->path);
3594 s->ctx.exops.get_st_gen = NULL;
3595 len = strlen(s->fsconf.tag);
3596 if (len > MAX_TAG_LEN - 1) {
3597 error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
3598 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
3602 s->tag = g_strdup(s->fsconf.tag);
3607 s->ctx.fmode = fse->fmode;
3608 s->ctx.dmode = fse->dmode;
3611 qemu_co_rwlock_init(&s->rename_lock);
3613 if (s->ops->init(&s->ctx, errp) < 0) {
3614 error_prepend(errp, "cannot initialize fsdev '%s': ",
3615 s->fsconf.fsdev_id);
3620 * Check details of export path, We need to use fs driver
3621 * call back to do that. Since we are in the init path, we don't
3622 * use co-routines here.
3624 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
3626 "error in converting name to path %s", strerror(errno));
3629 if (s->ops->lstat(&s->ctx, &path, &stat)) {
3630 error_setg(errp, "share path %s does not exist", fse->path);
3632 } else if (!S_ISDIR(stat.st_mode)) {
3633 error_setg(errp, "share path %s is not a directory", fse->path);
3637 s->ctx.fst = &fse->fst;
3638 fsdev_throttle_init(s->ctx.fst);
3640 v9fs_path_free(&path);
3645 if (s->ops && s->ops->cleanup && s->ctx.private) {
3646 s->ops->cleanup(&s->ctx);
3649 g_free(s->ctx.fs_root);
3650 v9fs_path_free(&path);
3655 void v9fs_device_unrealize_common(V9fsState *s, Error **errp)
3657 if (s->ops->cleanup) {
3658 s->ops->cleanup(&s->ctx);
3660 fsdev_throttle_cleanup(s->ctx.fst);
3662 g_free(s->ctx.fs_root);
3665 typedef struct VirtfsCoResetData {
3668 } VirtfsCoResetData;
3670 static void coroutine_fn virtfs_co_reset(void *opaque)
3672 VirtfsCoResetData *data = opaque;
3674 virtfs_reset(&data->pdu);
3678 void v9fs_reset(V9fsState *s)
3680 VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
3683 while (!QLIST_EMPTY(&s->active_list)) {
3684 aio_poll(qemu_get_aio_context(), true);
3687 co = qemu_coroutine_create(virtfs_co_reset, &data);
3688 qemu_coroutine_enter(co);
3690 while (!data.done) {
3691 aio_poll(qemu_get_aio_context(), true);
3695 static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
3698 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
3699 error_report("Failed to get the resource limit");
3702 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
3703 open_fd_rc = rlim.rlim_cur/2;