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"
30 static int open_fd_rc;
44 ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
50 ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap);
56 ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
62 ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap);
68 static int omode_to_uflags(int8_t mode)
102 struct dotl_openflag_map {
107 static int dotl_to_open_flags(int flags)
111 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
112 * and P9_DOTL_NOACCESS
114 int oflags = flags & O_ACCMODE;
116 struct dotl_openflag_map dotl_oflag_map[] = {
117 { P9_DOTL_CREATE, O_CREAT },
118 { P9_DOTL_EXCL, O_EXCL },
119 { P9_DOTL_NOCTTY , O_NOCTTY },
120 { P9_DOTL_TRUNC, O_TRUNC },
121 { P9_DOTL_APPEND, O_APPEND },
122 { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
123 { P9_DOTL_DSYNC, O_DSYNC },
124 { P9_DOTL_FASYNC, FASYNC },
125 { P9_DOTL_DIRECT, O_DIRECT },
126 { P9_DOTL_LARGEFILE, O_LARGEFILE },
127 { P9_DOTL_DIRECTORY, O_DIRECTORY },
128 { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
129 { P9_DOTL_NOATIME, O_NOATIME },
130 { P9_DOTL_SYNC, O_SYNC },
133 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
134 if (flags & dotl_oflag_map[i].dotl_flag) {
135 oflags |= dotl_oflag_map[i].open_flag;
142 void cred_init(FsCred *credp)
150 static int get_dotl_openflags(V9fsState *s, int oflags)
154 * Filter the client open flags
156 flags = dotl_to_open_flags(oflags);
157 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
159 * Ignore direct disk access hint until the server supports it.
165 void v9fs_path_init(V9fsPath *path)
171 void v9fs_path_free(V9fsPath *path)
179 void GCC_FMT_ATTR(2, 3)
180 v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
184 v9fs_path_free(path);
187 /* Bump the size for including terminating NULL */
188 path->size = g_vasprintf(&path->data, fmt, ap) + 1;
192 void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs)
195 lhs->data = g_malloc(rhs->size);
196 memcpy(lhs->data, rhs->data, rhs->size);
197 lhs->size = rhs->size;
200 int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
201 const char *name, V9fsPath *path)
204 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
212 * Return TRUE if s1 is an ancestor of s2.
214 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
215 * As a special case, We treat s1 as ancestor of s2 if they are same!
217 static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
219 if (!strncmp(s1->data, s2->data, s1->size - 1)) {
220 if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
227 static size_t v9fs_string_size(V9fsString *str)
233 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
234 static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
237 if (f->fid_type == P9_FID_FILE) {
238 if (f->fs.fd == -1) {
240 err = v9fs_co_open(pdu, f, f->open_flags);
241 } while (err == -EINTR && !pdu->cancelled);
243 } else if (f->fid_type == P9_FID_DIR) {
244 if (f->fs.dir.stream == NULL) {
246 err = v9fs_co_opendir(pdu, f);
247 } while (err == -EINTR && !pdu->cancelled);
253 static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid)
257 V9fsState *s = pdu->s;
259 for (f = s->fid_list; f; f = f->next) {
263 * Update the fid ref upfront so that
264 * we don't get reclaimed when we yield
269 * check whether we need to reopen the
270 * file. We might have closed the fd
271 * while trying to free up some file
274 err = v9fs_reopen_fid(pdu, f);
280 * Mark the fid as referenced so that the LRU
281 * reclaim won't close the file descriptor
283 f->flags |= FID_REFERENCED;
290 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
294 for (f = s->fid_list; f; f = f->next) {
295 /* If fid is already there return NULL */
301 f = g_malloc0(sizeof(V9fsFidState));
303 f->fid_type = P9_FID_NONE;
306 * Mark the fid as referenced so that the LRU
307 * reclaim won't close the file descriptor
309 f->flags |= FID_REFERENCED;
310 f->next = s->fid_list;
313 v9fs_readdir_init(&f->fs.dir);
314 v9fs_readdir_init(&f->fs_reclaim.dir);
319 static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
323 if (fidp->fs.xattr.xattrwalk_fid) {
324 /* getxattr/listxattr fid */
328 * if this is fid for setxattr. clunk should
329 * result in setxattr localcall
331 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
332 /* clunk after partial write */
336 if (fidp->fs.xattr.len) {
337 retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
338 fidp->fs.xattr.value,
340 fidp->fs.xattr.flags);
342 retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
345 v9fs_string_free(&fidp->fs.xattr.name);
347 g_free(fidp->fs.xattr.value);
351 static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
355 if (fidp->fid_type == P9_FID_FILE) {
356 /* If we reclaimed the fd no need to close */
357 if (fidp->fs.fd != -1) {
358 retval = v9fs_co_close(pdu, &fidp->fs);
360 } else if (fidp->fid_type == P9_FID_DIR) {
361 if (fidp->fs.dir.stream != NULL) {
362 retval = v9fs_co_closedir(pdu, &fidp->fs);
364 } else if (fidp->fid_type == P9_FID_XATTR) {
365 retval = v9fs_xattr_fid_clunk(pdu, fidp);
367 v9fs_path_free(&fidp->path);
372 static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
377 * Don't free the fid if it is in reclaim list
379 if (!fidp->ref && fidp->clunked) {
380 if (fidp->fid == pdu->s->root_fid) {
382 * if the clunked fid is root fid then we
383 * have unmounted the fs on the client side.
384 * delete the migration blocker. Ideally, this
385 * should be hooked to transport close notification
387 if (pdu->s->migration_blocker) {
388 migrate_del_blocker(pdu->s->migration_blocker);
389 error_free(pdu->s->migration_blocker);
390 pdu->s->migration_blocker = NULL;
393 return free_fid(pdu, fidp);
398 static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
400 V9fsFidState **fidpp, *fidp;
402 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
403 if ((*fidpp)->fid == fid) {
407 if (*fidpp == NULL) {
416 void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)
418 int reclaim_count = 0;
419 V9fsState *s = pdu->s;
420 V9fsFidState *f, *reclaim_list = NULL;
422 for (f = s->fid_list; f; f = f->next) {
424 * Unlink fids cannot be reclaimed. Check
425 * for them and skip them. Also skip fids
426 * currently being operated on.
428 if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
432 * if it is a recently referenced fid
433 * we leave the fid untouched and clear the
434 * reference bit. We come back to it later
435 * in the next iteration. (a simple LRU without
436 * moving list elements around)
438 if (f->flags & FID_REFERENCED) {
439 f->flags &= ~FID_REFERENCED;
443 * Add fids to reclaim list.
445 if (f->fid_type == P9_FID_FILE) {
446 if (f->fs.fd != -1) {
448 * Up the reference count so that
449 * a clunk request won't free this fid
452 f->rclm_lst = reclaim_list;
454 f->fs_reclaim.fd = f->fs.fd;
458 } else if (f->fid_type == P9_FID_DIR) {
459 if (f->fs.dir.stream != NULL) {
461 * Up the reference count so that
462 * a clunk request won't free this fid
465 f->rclm_lst = reclaim_list;
467 f->fs_reclaim.dir.stream = f->fs.dir.stream;
468 f->fs.dir.stream = NULL;
472 if (reclaim_count >= open_fd_rc) {
477 * Now close the fid in reclaim list. Free them if they
478 * are already clunked.
480 while (reclaim_list) {
482 reclaim_list = f->rclm_lst;
483 if (f->fid_type == P9_FID_FILE) {
484 v9fs_co_close(pdu, &f->fs_reclaim);
485 } else if (f->fid_type == P9_FID_DIR) {
486 v9fs_co_closedir(pdu, &f->fs_reclaim);
490 * Now drop the fid reference, free it
497 static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
500 V9fsState *s = pdu->s;
501 V9fsFidState *fidp, head_fid;
503 head_fid.next = s->fid_list;
504 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
505 if (fidp->path.size != path->size) {
508 if (!memcmp(fidp->path.data, path->data, path->size)) {
509 /* Mark the fid non reclaimable. */
510 fidp->flags |= FID_NON_RECLAIMABLE;
512 /* reopen the file/dir if already closed */
513 err = v9fs_reopen_fid(pdu, fidp);
518 * Go back to head of fid list because
519 * the list could have got updated when
520 * switched to the worker thread
530 static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
532 V9fsState *s = pdu->s;
536 while (s->fid_list) {
542 s->fid_list = fidp->next;
549 #define P9_QID_TYPE_DIR 0x80
550 #define P9_QID_TYPE_SYMLINK 0x02
552 #define P9_STAT_MODE_DIR 0x80000000
553 #define P9_STAT_MODE_APPEND 0x40000000
554 #define P9_STAT_MODE_EXCL 0x20000000
555 #define P9_STAT_MODE_MOUNT 0x10000000
556 #define P9_STAT_MODE_AUTH 0x08000000
557 #define P9_STAT_MODE_TMP 0x04000000
558 #define P9_STAT_MODE_SYMLINK 0x02000000
559 #define P9_STAT_MODE_LINK 0x01000000
560 #define P9_STAT_MODE_DEVICE 0x00800000
561 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
562 #define P9_STAT_MODE_SOCKET 0x00100000
563 #define P9_STAT_MODE_SETUID 0x00080000
564 #define P9_STAT_MODE_SETGID 0x00040000
565 #define P9_STAT_MODE_SETVTX 0x00010000
567 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
568 P9_STAT_MODE_SYMLINK | \
569 P9_STAT_MODE_LINK | \
570 P9_STAT_MODE_DEVICE | \
571 P9_STAT_MODE_NAMED_PIPE | \
574 /* This is the algorithm from ufs in spfs */
575 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
579 memset(&qidp->path, 0, sizeof(qidp->path));
580 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
581 memcpy(&qidp->path, &stbuf->st_ino, size);
582 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
584 if (S_ISDIR(stbuf->st_mode)) {
585 qidp->type |= P9_QID_TYPE_DIR;
587 if (S_ISLNK(stbuf->st_mode)) {
588 qidp->type |= P9_QID_TYPE_SYMLINK;
592 static int coroutine_fn fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp,
598 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
602 stat_to_qid(&stbuf, qidp);
606 V9fsPDU *pdu_alloc(V9fsState *s)
610 if (!QLIST_EMPTY(&s->free_list)) {
611 pdu = QLIST_FIRST(&s->free_list);
612 QLIST_REMOVE(pdu, next);
613 QLIST_INSERT_HEAD(&s->active_list, pdu, next);
618 void pdu_free(V9fsPDU *pdu)
620 V9fsState *s = pdu->s;
622 g_assert(!pdu->cancelled);
623 QLIST_REMOVE(pdu, next);
624 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
627 static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
629 int8_t id = pdu->id + 1; /* Response */
630 V9fsState *s = pdu->s;
637 if (s->proto_version != V9FS_PROTO_2000L) {
640 str.data = strerror(err);
641 str.size = strlen(str.data);
643 ret = pdu_marshal(pdu, len, "s", &str);
651 ret = pdu_marshal(pdu, len, "d", err);
657 if (s->proto_version == V9FS_PROTO_2000L) {
660 trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
663 /* fill out the header */
664 if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) {
668 /* keep these in sync */
673 pdu->s->transport->push_and_notify(pdu);
675 /* Now wakeup anybody waiting in flush for this request */
676 if (!qemu_co_queue_next(&pdu->complete)) {
681 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
686 if (mode & P9_STAT_MODE_DIR) {
690 if (mode & P9_STAT_MODE_SYMLINK) {
693 if (mode & P9_STAT_MODE_SOCKET) {
696 if (mode & P9_STAT_MODE_NAMED_PIPE) {
699 if (mode & P9_STAT_MODE_DEVICE) {
700 if (extension->size && extension->data[0] == 'c') {
711 if (mode & P9_STAT_MODE_SETUID) {
714 if (mode & P9_STAT_MODE_SETGID) {
717 if (mode & P9_STAT_MODE_SETVTX) {
724 static int donttouch_stat(V9fsStat *stat)
726 if (stat->type == -1 &&
728 stat->qid.type == -1 &&
729 stat->qid.version == -1 &&
730 stat->qid.path == -1 &&
734 stat->length == -1 &&
741 stat->n_muid == -1) {
748 static void v9fs_stat_init(V9fsStat *stat)
750 v9fs_string_init(&stat->name);
751 v9fs_string_init(&stat->uid);
752 v9fs_string_init(&stat->gid);
753 v9fs_string_init(&stat->muid);
754 v9fs_string_init(&stat->extension);
757 static void v9fs_stat_free(V9fsStat *stat)
759 v9fs_string_free(&stat->name);
760 v9fs_string_free(&stat->uid);
761 v9fs_string_free(&stat->gid);
762 v9fs_string_free(&stat->muid);
763 v9fs_string_free(&stat->extension);
766 static uint32_t stat_to_v9mode(const struct stat *stbuf)
770 mode = stbuf->st_mode & 0777;
771 if (S_ISDIR(stbuf->st_mode)) {
772 mode |= P9_STAT_MODE_DIR;
775 if (S_ISLNK(stbuf->st_mode)) {
776 mode |= P9_STAT_MODE_SYMLINK;
779 if (S_ISSOCK(stbuf->st_mode)) {
780 mode |= P9_STAT_MODE_SOCKET;
783 if (S_ISFIFO(stbuf->st_mode)) {
784 mode |= P9_STAT_MODE_NAMED_PIPE;
787 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
788 mode |= P9_STAT_MODE_DEVICE;
791 if (stbuf->st_mode & S_ISUID) {
792 mode |= P9_STAT_MODE_SETUID;
795 if (stbuf->st_mode & S_ISGID) {
796 mode |= P9_STAT_MODE_SETGID;
799 if (stbuf->st_mode & S_ISVTX) {
800 mode |= P9_STAT_MODE_SETVTX;
806 static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *name,
807 const struct stat *stbuf,
813 memset(v9stat, 0, sizeof(*v9stat));
815 stat_to_qid(stbuf, &v9stat->qid);
816 v9stat->mode = stat_to_v9mode(stbuf);
817 v9stat->atime = stbuf->st_atime;
818 v9stat->mtime = stbuf->st_mtime;
819 v9stat->length = stbuf->st_size;
821 v9fs_string_free(&v9stat->uid);
822 v9fs_string_free(&v9stat->gid);
823 v9fs_string_free(&v9stat->muid);
825 v9stat->n_uid = stbuf->st_uid;
826 v9stat->n_gid = stbuf->st_gid;
829 v9fs_string_free(&v9stat->extension);
831 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
832 err = v9fs_co_readlink(pdu, name, &v9stat->extension);
836 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
837 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
838 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
839 major(stbuf->st_rdev), minor(stbuf->st_rdev));
840 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
841 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
842 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
845 str = strrchr(name->data, '/');
852 v9fs_string_sprintf(&v9stat->name, "%s", str);
855 v9fs_string_size(&v9stat->name) +
856 v9fs_string_size(&v9stat->uid) +
857 v9fs_string_size(&v9stat->gid) +
858 v9fs_string_size(&v9stat->muid) +
859 v9fs_string_size(&v9stat->extension);
863 #define P9_STATS_MODE 0x00000001ULL
864 #define P9_STATS_NLINK 0x00000002ULL
865 #define P9_STATS_UID 0x00000004ULL
866 #define P9_STATS_GID 0x00000008ULL
867 #define P9_STATS_RDEV 0x00000010ULL
868 #define P9_STATS_ATIME 0x00000020ULL
869 #define P9_STATS_MTIME 0x00000040ULL
870 #define P9_STATS_CTIME 0x00000080ULL
871 #define P9_STATS_INO 0x00000100ULL
872 #define P9_STATS_SIZE 0x00000200ULL
873 #define P9_STATS_BLOCKS 0x00000400ULL
875 #define P9_STATS_BTIME 0x00000800ULL
876 #define P9_STATS_GEN 0x00001000ULL
877 #define P9_STATS_DATA_VERSION 0x00002000ULL
879 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
880 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
883 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
884 V9fsStatDotl *v9lstat)
886 memset(v9lstat, 0, sizeof(*v9lstat));
888 v9lstat->st_mode = stbuf->st_mode;
889 v9lstat->st_nlink = stbuf->st_nlink;
890 v9lstat->st_uid = stbuf->st_uid;
891 v9lstat->st_gid = stbuf->st_gid;
892 v9lstat->st_rdev = stbuf->st_rdev;
893 v9lstat->st_size = stbuf->st_size;
894 v9lstat->st_blksize = stbuf->st_blksize;
895 v9lstat->st_blocks = stbuf->st_blocks;
896 v9lstat->st_atime_sec = stbuf->st_atime;
897 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
898 v9lstat->st_mtime_sec = stbuf->st_mtime;
899 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
900 v9lstat->st_ctime_sec = stbuf->st_ctime;
901 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
902 /* Currently we only support BASIC fields in stat */
903 v9lstat->st_result_mask = P9_STATS_BASIC;
905 stat_to_qid(stbuf, &v9lstat->qid);
908 static void print_sg(struct iovec *sg, int cnt)
912 printf("sg[%d]: {", cnt);
913 for (i = 0; i < cnt; i++) {
917 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
922 /* Will call this only for path name based fid */
923 static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
926 v9fs_path_init(&str);
927 v9fs_path_copy(&str, dst);
928 v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
929 v9fs_path_free(&str);
932 static inline bool is_ro_export(FsContext *ctx)
934 return ctx->export_flags & V9FS_RDONLY;
937 static void coroutine_fn v9fs_version(void *opaque)
940 V9fsPDU *pdu = opaque;
941 V9fsState *s = pdu->s;
945 v9fs_string_init(&version);
946 err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
951 trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
955 if (!strcmp(version.data, "9P2000.u")) {
956 s->proto_version = V9FS_PROTO_2000U;
957 } else if (!strcmp(version.data, "9P2000.L")) {
958 s->proto_version = V9FS_PROTO_2000L;
960 v9fs_string_sprintf(&version, "unknown");
963 err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
969 trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
971 pdu_complete(pdu, offset);
972 v9fs_string_free(&version);
975 static void coroutine_fn v9fs_attach(void *opaque)
977 V9fsPDU *pdu = opaque;
978 V9fsState *s = pdu->s;
979 int32_t fid, afid, n_uname;
980 V9fsString uname, aname;
985 Error *local_err = NULL;
987 v9fs_string_init(&uname);
988 v9fs_string_init(&aname);
989 err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
990 &afid, &uname, &aname, &n_uname);
994 trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
996 fidp = alloc_fid(s, fid);
1001 fidp->uid = n_uname;
1002 err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
1008 err = fid_to_qid(pdu, fidp, &qid);
1016 * disable migration if we haven't done already.
1017 * attach could get called multiple times for the same export.
1019 if (!s->migration_blocker) {
1020 error_setg(&s->migration_blocker,
1021 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1022 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
1023 err = migrate_add_blocker(s->migration_blocker, &local_err);
1025 error_free(local_err);
1026 error_free(s->migration_blocker);
1027 s->migration_blocker = NULL;
1034 err = pdu_marshal(pdu, offset, "Q", &qid);
1041 memcpy(&s->root_qid, &qid, sizeof(qid));
1042 trace_v9fs_attach_return(pdu->tag, pdu->id,
1043 qid.type, qid.version, qid.path);
1047 pdu_complete(pdu, err);
1048 v9fs_string_free(&uname);
1049 v9fs_string_free(&aname);
1052 static void coroutine_fn v9fs_stat(void *opaque)
1060 V9fsPDU *pdu = opaque;
1062 err = pdu_unmarshal(pdu, offset, "d", &fid);
1066 trace_v9fs_stat(pdu->tag, pdu->id, fid);
1068 fidp = get_fid(pdu, fid);
1073 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1077 err = stat_to_v9stat(pdu, &fidp->path, &stbuf, &v9stat);
1081 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1083 v9fs_stat_free(&v9stat);
1086 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1087 v9stat.atime, v9stat.mtime, v9stat.length);
1089 v9fs_stat_free(&v9stat);
1093 pdu_complete(pdu, err);
1096 static void coroutine_fn v9fs_getattr(void *opaque)
1103 uint64_t request_mask;
1104 V9fsStatDotl v9stat_dotl;
1105 V9fsPDU *pdu = opaque;
1106 V9fsState *s = pdu->s;
1108 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1112 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
1114 fidp = get_fid(pdu, fid);
1120 * Currently we only support BASIC fields in stat, so there is no
1121 * need to look at request_mask.
1123 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1127 stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
1129 /* fill st_gen if requested and supported by underlying fs */
1130 if (request_mask & P9_STATS_GEN) {
1131 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
1134 /* we have valid st_gen: update result mask */
1135 v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1138 /* request cancelled, e.g. by Tflush */
1141 /* failed to get st_gen: not fatal, ignore */
1145 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1150 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1151 v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1152 v9stat_dotl.st_gid);
1156 pdu_complete(pdu, retval);
1159 /* Attribute flags */
1160 #define P9_ATTR_MODE (1 << 0)
1161 #define P9_ATTR_UID (1 << 1)
1162 #define P9_ATTR_GID (1 << 2)
1163 #define P9_ATTR_SIZE (1 << 3)
1164 #define P9_ATTR_ATIME (1 << 4)
1165 #define P9_ATTR_MTIME (1 << 5)
1166 #define P9_ATTR_CTIME (1 << 6)
1167 #define P9_ATTR_ATIME_SET (1 << 7)
1168 #define P9_ATTR_MTIME_SET (1 << 8)
1170 #define P9_ATTR_MASK 127
1172 static void coroutine_fn v9fs_setattr(void *opaque)
1179 V9fsPDU *pdu = opaque;
1181 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1186 fidp = get_fid(pdu, fid);
1191 if (v9iattr.valid & P9_ATTR_MODE) {
1192 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1197 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1198 struct timespec times[2];
1199 if (v9iattr.valid & P9_ATTR_ATIME) {
1200 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1201 times[0].tv_sec = v9iattr.atime_sec;
1202 times[0].tv_nsec = v9iattr.atime_nsec;
1204 times[0].tv_nsec = UTIME_NOW;
1207 times[0].tv_nsec = UTIME_OMIT;
1209 if (v9iattr.valid & P9_ATTR_MTIME) {
1210 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1211 times[1].tv_sec = v9iattr.mtime_sec;
1212 times[1].tv_nsec = v9iattr.mtime_nsec;
1214 times[1].tv_nsec = UTIME_NOW;
1217 times[1].tv_nsec = UTIME_OMIT;
1219 err = v9fs_co_utimensat(pdu, &fidp->path, times);
1225 * If the only valid entry in iattr is ctime we can call
1226 * chown(-1,-1) to update the ctime of the file
1228 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1229 ((v9iattr.valid & P9_ATTR_CTIME)
1230 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1231 if (!(v9iattr.valid & P9_ATTR_UID)) {
1234 if (!(v9iattr.valid & P9_ATTR_GID)) {
1237 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1243 if (v9iattr.valid & (P9_ATTR_SIZE)) {
1244 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1253 pdu_complete(pdu, err);
1256 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1262 err = pdu_marshal(pdu, offset, "w", nwnames);
1267 for (i = 0; i < nwnames; i++) {
1268 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1277 static bool name_is_illegal(const char *name)
1279 return !*name || strchr(name, '/') != NULL;
1282 static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
1285 qid1->type != qid2->type ||
1286 qid1->version != qid2->version ||
1287 qid1->path != qid2->path;
1290 static void coroutine_fn v9fs_walk(void *opaque)
1293 V9fsQID *qids = NULL;
1295 V9fsPath dpath, path;
1299 int32_t fid, newfid;
1300 V9fsString *wnames = NULL;
1302 V9fsFidState *newfidp = NULL;
1303 V9fsPDU *pdu = opaque;
1304 V9fsState *s = pdu->s;
1307 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1309 pdu_complete(pdu, err);
1314 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1316 if (nwnames && nwnames <= P9_MAXWELEM) {
1317 wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
1318 qids = g_malloc0(sizeof(qids[0]) * nwnames);
1319 for (i = 0; i < nwnames; i++) {
1320 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1324 if (name_is_illegal(wnames[i].data)) {
1330 } else if (nwnames > P9_MAXWELEM) {
1334 fidp = get_fid(pdu, fid);
1340 v9fs_path_init(&dpath);
1341 v9fs_path_init(&path);
1343 err = fid_to_qid(pdu, fidp, &qid);
1349 * Both dpath and path initially poin to fidp.
1350 * Needed to handle request with nwnames == 0
1352 v9fs_path_copy(&dpath, &fidp->path);
1353 v9fs_path_copy(&path, &fidp->path);
1354 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1355 if (not_same_qid(&pdu->s->root_qid, &qid) ||
1356 strcmp("..", wnames[name_idx].data)) {
1357 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
1363 err = v9fs_co_lstat(pdu, &path, &stbuf);
1367 stat_to_qid(&stbuf, &qid);
1368 v9fs_path_copy(&dpath, &path);
1370 memcpy(&qids[name_idx], &qid, sizeof(qid));
1372 if (fid == newfid) {
1373 if (fidp->fid_type != P9_FID_NONE) {
1377 v9fs_path_copy(&fidp->path, &path);
1379 newfidp = alloc_fid(s, newfid);
1380 if (newfidp == NULL) {
1384 newfidp->uid = fidp->uid;
1385 v9fs_path_copy(&newfidp->path, &path);
1387 err = v9fs_walk_marshal(pdu, nwnames, qids);
1388 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1392 put_fid(pdu, newfidp);
1394 v9fs_path_free(&dpath);
1395 v9fs_path_free(&path);
1397 pdu_complete(pdu, err);
1398 if (nwnames && nwnames <= P9_MAXWELEM) {
1399 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1400 v9fs_string_free(&wnames[name_idx]);
1407 static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
1409 struct statfs stbuf;
1411 V9fsState *s = pdu->s;
1414 * iounit should be multiples of f_bsize (host filesystem block size
1415 * and as well as less than (client msize - P9_IOHDRSZ))
1417 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1418 iounit = stbuf.f_bsize;
1419 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1422 iounit = s->msize - P9_IOHDRSZ;
1427 static void coroutine_fn v9fs_open(void *opaque)
1438 V9fsPDU *pdu = opaque;
1439 V9fsState *s = pdu->s;
1441 if (s->proto_version == V9FS_PROTO_2000L) {
1442 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1445 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1451 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1453 fidp = get_fid(pdu, fid);
1458 if (fidp->fid_type != P9_FID_NONE) {
1463 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1467 stat_to_qid(&stbuf, &qid);
1468 if (S_ISDIR(stbuf.st_mode)) {
1469 err = v9fs_co_opendir(pdu, fidp);
1473 fidp->fid_type = P9_FID_DIR;
1474 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1480 if (s->proto_version == V9FS_PROTO_2000L) {
1481 flags = get_dotl_openflags(s, mode);
1483 flags = omode_to_uflags(mode);
1485 if (is_ro_export(&s->ctx)) {
1486 if (mode & O_WRONLY || mode & O_RDWR ||
1487 mode & O_APPEND || mode & O_TRUNC) {
1492 err = v9fs_co_open(pdu, fidp, flags);
1496 fidp->fid_type = P9_FID_FILE;
1497 fidp->open_flags = flags;
1498 if (flags & O_EXCL) {
1500 * We let the host file system do O_EXCL check
1501 * We should not reclaim such fd
1503 fidp->flags |= FID_NON_RECLAIMABLE;
1505 iounit = get_iounit(pdu, &fidp->path);
1506 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1512 trace_v9fs_open_return(pdu->tag, pdu->id,
1513 qid.type, qid.version, qid.path, iounit);
1517 pdu_complete(pdu, err);
1520 static void coroutine_fn v9fs_lcreate(void *opaque)
1522 int32_t dfid, flags, mode;
1531 V9fsPDU *pdu = opaque;
1533 v9fs_string_init(&name);
1534 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1535 &name, &flags, &mode, &gid);
1539 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1541 if (name_is_illegal(name.data)) {
1546 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
1551 fidp = get_fid(pdu, dfid);
1556 if (fidp->fid_type != P9_FID_NONE) {
1561 flags = get_dotl_openflags(pdu->s, flags);
1562 err = v9fs_co_open2(pdu, fidp, &name, gid,
1563 flags | O_CREAT, mode, &stbuf);
1567 fidp->fid_type = P9_FID_FILE;
1568 fidp->open_flags = flags;
1569 if (flags & O_EXCL) {
1571 * We let the host file system do O_EXCL check
1572 * We should not reclaim such fd
1574 fidp->flags |= FID_NON_RECLAIMABLE;
1576 iounit = get_iounit(pdu, &fidp->path);
1577 stat_to_qid(&stbuf, &qid);
1578 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1583 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
1584 qid.type, qid.version, qid.path, iounit);
1588 pdu_complete(pdu, err);
1589 v9fs_string_free(&name);
1592 static void coroutine_fn v9fs_fsync(void *opaque)
1599 V9fsPDU *pdu = opaque;
1601 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1605 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
1607 fidp = get_fid(pdu, fid);
1612 err = v9fs_co_fsync(pdu, fidp, datasync);
1618 pdu_complete(pdu, err);
1621 static void coroutine_fn v9fs_clunk(void *opaque)
1627 V9fsPDU *pdu = opaque;
1628 V9fsState *s = pdu->s;
1630 err = pdu_unmarshal(pdu, offset, "d", &fid);
1634 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
1636 fidp = clunk_fid(s, fid);
1642 * Bump the ref so that put_fid will
1646 err = put_fid(pdu, fidp);
1651 pdu_complete(pdu, err);
1655 * Create a QEMUIOVector for a sub-region of PDU iovecs
1657 * @qiov: uninitialized QEMUIOVector
1658 * @skip: number of bytes to skip from beginning of PDU
1659 * @size: number of bytes to include
1660 * @is_write: true - write, false - read
1662 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1663 * with qemu_iovec_destroy().
1665 static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1666 size_t skip, size_t size,
1674 pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip);
1676 pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip);
1679 qemu_iovec_init_external(&elem, iov, niov);
1680 qemu_iovec_init(qiov, niov);
1681 qemu_iovec_concat(qiov, &elem, skip, size);
1684 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1685 uint64_t off, uint32_t max_count)
1689 uint64_t read_count;
1690 QEMUIOVector qiov_full;
1692 if (fidp->fs.xattr.len < off) {
1695 read_count = fidp->fs.xattr.len - off;
1697 if (read_count > max_count) {
1698 read_count = max_count;
1700 err = pdu_marshal(pdu, offset, "d", read_count);
1706 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false);
1707 err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0,
1708 ((char *)fidp->fs.xattr.value) + off,
1710 qemu_iovec_destroy(&qiov_full);
1718 static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1727 off_t saved_dir_pos;
1728 struct dirent *dent;
1730 /* save the directory position */
1731 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1732 if (saved_dir_pos < 0) {
1733 return saved_dir_pos;
1737 v9fs_path_init(&path);
1739 v9fs_readdir_lock(&fidp->fs.dir);
1741 err = v9fs_co_readdir(pdu, fidp, &dent);
1745 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
1749 err = v9fs_co_lstat(pdu, &path, &stbuf);
1753 err = stat_to_v9stat(pdu, &path, &stbuf, &v9stat);
1757 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1758 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1760 v9fs_readdir_unlock(&fidp->fs.dir);
1762 if ((len != (v9stat.size + 2)) || ((count + len) > max_count)) {
1763 /* Ran out of buffer. Set dir back to old position and return */
1764 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1765 v9fs_stat_free(&v9stat);
1766 v9fs_path_free(&path);
1770 v9fs_stat_free(&v9stat);
1771 v9fs_path_free(&path);
1772 saved_dir_pos = dent->d_off;
1775 v9fs_readdir_unlock(&fidp->fs.dir);
1777 v9fs_path_free(&path);
1784 static void coroutine_fn v9fs_read(void *opaque)
1793 V9fsPDU *pdu = opaque;
1794 V9fsState *s = pdu->s;
1796 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1800 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
1802 fidp = get_fid(pdu, fid);
1807 if (fidp->fid_type == P9_FID_DIR) {
1810 v9fs_co_rewinddir(pdu, fidp);
1812 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
1817 err = pdu_marshal(pdu, offset, "d", count);
1821 err += offset + count;
1822 } else if (fidp->fid_type == P9_FID_FILE) {
1823 QEMUIOVector qiov_full;
1827 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
1828 qemu_iovec_init(&qiov, qiov_full.niov);
1830 qemu_iovec_reset(&qiov);
1831 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
1833 print_sg(qiov.iov, qiov.niov);
1835 /* Loop in case of EINTR */
1837 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
1842 } while (len == -EINTR && !pdu->cancelled);
1844 /* IO error return the error */
1846 goto out_free_iovec;
1848 } while (count < max_count && len > 0);
1849 err = pdu_marshal(pdu, offset, "d", count);
1851 goto out_free_iovec;
1853 err += offset + count;
1855 qemu_iovec_destroy(&qiov);
1856 qemu_iovec_destroy(&qiov_full);
1857 } else if (fidp->fid_type == P9_FID_XATTR) {
1858 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
1862 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
1866 pdu_complete(pdu, err);
1869 static size_t v9fs_readdir_data_size(V9fsString *name)
1872 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1873 * size of type (1) + size of name.size (2) + strlen(name.data)
1875 return 24 + v9fs_string_size(name);
1878 static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
1886 off_t saved_dir_pos;
1887 struct dirent *dent;
1889 /* save the directory position */
1890 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1891 if (saved_dir_pos < 0) {
1892 return saved_dir_pos;
1896 v9fs_readdir_lock(&fidp->fs.dir);
1898 err = v9fs_co_readdir(pdu, fidp, &dent);
1902 v9fs_string_init(&name);
1903 v9fs_string_sprintf(&name, "%s", dent->d_name);
1904 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
1905 v9fs_readdir_unlock(&fidp->fs.dir);
1907 /* Ran out of buffer. Set dir back to old position and return */
1908 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1909 v9fs_string_free(&name);
1913 * Fill up just the path field of qid because the client uses
1914 * only that. To fill the entire qid structure we will have
1915 * to stat each dirent found, which is expensive
1917 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1918 memcpy(&qid.path, &dent->d_ino, size);
1919 /* Fill the other fields with dummy values */
1923 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1924 len = pdu_marshal(pdu, 11 + count, "Qqbs",
1926 dent->d_type, &name);
1928 v9fs_readdir_unlock(&fidp->fs.dir);
1931 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1932 v9fs_string_free(&name);
1936 v9fs_string_free(&name);
1937 saved_dir_pos = dent->d_off;
1940 v9fs_readdir_unlock(&fidp->fs.dir);
1948 static void coroutine_fn v9fs_readdir(void *opaque)
1954 uint64_t initial_offset;
1957 V9fsPDU *pdu = opaque;
1959 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
1960 &initial_offset, &max_count);
1964 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
1966 fidp = get_fid(pdu, fid);
1971 if (!fidp->fs.dir.stream) {
1975 if (initial_offset == 0) {
1976 v9fs_co_rewinddir(pdu, fidp);
1978 v9fs_co_seekdir(pdu, fidp, initial_offset);
1980 count = v9fs_do_readdir(pdu, fidp, max_count);
1985 retval = pdu_marshal(pdu, offset, "d", count);
1989 retval += count + offset;
1990 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
1994 pdu_complete(pdu, retval);
1997 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1998 uint64_t off, uint32_t count,
1999 struct iovec *sg, int cnt)
2003 uint64_t write_count;
2007 if (fidp->fs.xattr.len < off) {
2011 write_count = fidp->fs.xattr.len - off;
2012 if (write_count > count) {
2013 write_count = count;
2015 err = pdu_marshal(pdu, offset, "d", write_count);
2020 fidp->fs.xattr.copied_len += write_count;
2022 * Now copy the content from sg list
2024 for (i = 0; i < cnt; i++) {
2025 if (write_count > sg[i].iov_len) {
2026 to_copy = sg[i].iov_len;
2028 to_copy = write_count;
2030 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
2031 /* updating vs->off since we are not using below */
2033 write_count -= to_copy;
2039 static void coroutine_fn v9fs_write(void *opaque)
2049 V9fsPDU *pdu = opaque;
2050 V9fsState *s = pdu->s;
2051 QEMUIOVector qiov_full;
2054 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
2056 pdu_complete(pdu, err);
2060 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
2061 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
2063 fidp = get_fid(pdu, fid);
2068 if (fidp->fid_type == P9_FID_FILE) {
2069 if (fidp->fs.fd == -1) {
2073 } else if (fidp->fid_type == P9_FID_XATTR) {
2075 * setxattr operation
2077 err = v9fs_xattr_write(s, pdu, fidp, off, count,
2078 qiov_full.iov, qiov_full.niov);
2084 qemu_iovec_init(&qiov, qiov_full.niov);
2086 qemu_iovec_reset(&qiov);
2087 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
2089 print_sg(qiov.iov, qiov.niov);
2091 /* Loop in case of EINTR */
2093 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
2098 } while (len == -EINTR && !pdu->cancelled);
2100 /* IO error return the error */
2104 } while (total < count && len > 0);
2107 err = pdu_marshal(pdu, offset, "d", total);
2112 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2114 qemu_iovec_destroy(&qiov);
2118 qemu_iovec_destroy(&qiov_full);
2119 pdu_complete(pdu, err);
2122 static void coroutine_fn v9fs_create(void *opaque)
2134 V9fsString extension;
2136 V9fsPDU *pdu = opaque;
2138 v9fs_path_init(&path);
2139 v9fs_string_init(&name);
2140 v9fs_string_init(&extension);
2141 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2142 &perm, &mode, &extension);
2146 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2148 if (name_is_illegal(name.data)) {
2153 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2158 fidp = get_fid(pdu, fid);
2163 if (fidp->fid_type != P9_FID_NONE) {
2167 if (perm & P9_STAT_MODE_DIR) {
2168 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2169 fidp->uid, -1, &stbuf);
2173 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2177 v9fs_path_copy(&fidp->path, &path);
2178 err = v9fs_co_opendir(pdu, fidp);
2182 fidp->fid_type = P9_FID_DIR;
2183 } else if (perm & P9_STAT_MODE_SYMLINK) {
2184 err = v9fs_co_symlink(pdu, fidp, &name,
2185 extension.data, -1 , &stbuf);
2189 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2193 v9fs_path_copy(&fidp->path, &path);
2194 } else if (perm & P9_STAT_MODE_LINK) {
2195 int32_t ofid = atoi(extension.data);
2196 V9fsFidState *ofidp = get_fid(pdu, ofid);
2197 if (ofidp == NULL) {
2201 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2202 put_fid(pdu, ofidp);
2206 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2208 fidp->fid_type = P9_FID_NONE;
2211 v9fs_path_copy(&fidp->path, &path);
2212 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2214 fidp->fid_type = P9_FID_NONE;
2217 } else if (perm & P9_STAT_MODE_DEVICE) {
2219 uint32_t major, minor;
2222 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2239 nmode |= perm & 0777;
2240 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2241 makedev(major, minor), nmode, &stbuf);
2245 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2249 v9fs_path_copy(&fidp->path, &path);
2250 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2251 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2252 0, S_IFIFO | (perm & 0777), &stbuf);
2256 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2260 v9fs_path_copy(&fidp->path, &path);
2261 } else if (perm & P9_STAT_MODE_SOCKET) {
2262 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2263 0, S_IFSOCK | (perm & 0777), &stbuf);
2267 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2271 v9fs_path_copy(&fidp->path, &path);
2273 err = v9fs_co_open2(pdu, fidp, &name, -1,
2274 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2278 fidp->fid_type = P9_FID_FILE;
2279 fidp->open_flags = omode_to_uflags(mode);
2280 if (fidp->open_flags & O_EXCL) {
2282 * We let the host file system do O_EXCL check
2283 * We should not reclaim such fd
2285 fidp->flags |= FID_NON_RECLAIMABLE;
2288 iounit = get_iounit(pdu, &fidp->path);
2289 stat_to_qid(&stbuf, &qid);
2290 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2295 trace_v9fs_create_return(pdu->tag, pdu->id,
2296 qid.type, qid.version, qid.path, iounit);
2300 pdu_complete(pdu, err);
2301 v9fs_string_free(&name);
2302 v9fs_string_free(&extension);
2303 v9fs_path_free(&path);
2306 static void coroutine_fn v9fs_symlink(void *opaque)
2308 V9fsPDU *pdu = opaque;
2311 V9fsFidState *dfidp;
2319 v9fs_string_init(&name);
2320 v9fs_string_init(&symname);
2321 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2325 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2327 if (name_is_illegal(name.data)) {
2332 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2337 dfidp = get_fid(pdu, dfid);
2338 if (dfidp == NULL) {
2342 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2346 stat_to_qid(&stbuf, &qid);
2347 err = pdu_marshal(pdu, offset, "Q", &qid);
2352 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2353 qid.type, qid.version, qid.path);
2355 put_fid(pdu, dfidp);
2357 pdu_complete(pdu, err);
2358 v9fs_string_free(&name);
2359 v9fs_string_free(&symname);
2362 static void coroutine_fn v9fs_flush(void *opaque)
2367 V9fsPDU *cancel_pdu = NULL;
2368 V9fsPDU *pdu = opaque;
2369 V9fsState *s = pdu->s;
2371 err = pdu_unmarshal(pdu, offset, "w", &tag);
2373 pdu_complete(pdu, err);
2376 trace_v9fs_flush(pdu->tag, pdu->id, tag);
2378 if (pdu->tag == tag) {
2379 warn_report("the guest sent a self-referencing 9P flush request");
2381 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2382 if (cancel_pdu->tag == tag) {
2388 cancel_pdu->cancelled = 1;
2390 * Wait for pdu to complete.
2392 qemu_co_queue_wait(&cancel_pdu->complete, NULL);
2393 if (!qemu_co_queue_next(&cancel_pdu->complete)) {
2394 cancel_pdu->cancelled = 0;
2395 pdu_free(cancel_pdu);
2398 pdu_complete(pdu, 7);
2401 static void coroutine_fn v9fs_link(void *opaque)
2403 V9fsPDU *pdu = opaque;
2404 int32_t dfid, oldfid;
2405 V9fsFidState *dfidp, *oldfidp;
2410 v9fs_string_init(&name);
2411 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2415 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2417 if (name_is_illegal(name.data)) {
2422 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2427 dfidp = get_fid(pdu, dfid);
2428 if (dfidp == NULL) {
2433 oldfidp = get_fid(pdu, oldfid);
2434 if (oldfidp == NULL) {
2438 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2442 put_fid(pdu, oldfidp);
2444 put_fid(pdu, dfidp);
2446 v9fs_string_free(&name);
2447 pdu_complete(pdu, err);
2450 /* Only works with path name based fid */
2451 static void coroutine_fn v9fs_remove(void *opaque)
2457 V9fsPDU *pdu = opaque;
2459 err = pdu_unmarshal(pdu, offset, "d", &fid);
2463 trace_v9fs_remove(pdu->tag, pdu->id, fid);
2465 fidp = get_fid(pdu, fid);
2470 /* if fs driver is not path based, return EOPNOTSUPP */
2471 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2476 * IF the file is unlinked, we cannot reopen
2477 * the file later. So don't reclaim fd
2479 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2483 err = v9fs_co_remove(pdu, &fidp->path);
2488 /* For TREMOVE we need to clunk the fid even on failed remove */
2489 clunk_fid(pdu->s, fidp->fid);
2492 pdu_complete(pdu, err);
2495 static void coroutine_fn v9fs_unlinkat(void *opaque)
2499 int32_t dfid, flags;
2502 V9fsFidState *dfidp;
2503 V9fsPDU *pdu = opaque;
2505 v9fs_string_init(&name);
2506 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2511 if (name_is_illegal(name.data)) {
2516 if (!strcmp(".", name.data)) {
2521 if (!strcmp("..", name.data)) {
2526 dfidp = get_fid(pdu, dfid);
2527 if (dfidp == NULL) {
2532 * IF the file is unlinked, we cannot reopen
2533 * the file later. So don't reclaim fd
2535 v9fs_path_init(&path);
2536 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2540 err = v9fs_mark_fids_unreclaim(pdu, &path);
2544 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, flags);
2549 put_fid(pdu, dfidp);
2550 v9fs_path_free(&path);
2552 pdu_complete(pdu, err);
2553 v9fs_string_free(&name);
2557 /* Only works with path name based fid */
2558 static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2565 V9fsFidState *tfidp;
2566 V9fsState *s = pdu->s;
2567 V9fsFidState *dirfidp = NULL;
2568 char *old_name, *new_name;
2570 v9fs_path_init(&new_path);
2571 if (newdirfid != -1) {
2572 dirfidp = get_fid(pdu, newdirfid);
2573 if (dirfidp == NULL) {
2577 if (fidp->fid_type != P9_FID_NONE) {
2581 err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2586 old_name = fidp->path.data;
2587 end = strrchr(old_name, '/');
2593 new_name = g_malloc0(end - old_name + name->size + 1);
2594 strncat(new_name, old_name, end - old_name);
2595 strncat(new_name + (end - old_name), name->data, name->size);
2596 err = v9fs_co_name_to_path(pdu, NULL, new_name, &new_path);
2602 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2607 * Fixup fid's pointing to the old name to
2608 * start pointing to the new name
2610 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2611 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2612 /* replace the name */
2613 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
2618 put_fid(pdu, dirfidp);
2620 v9fs_path_free(&new_path);
2625 /* Only works with path name based fid */
2626 static void coroutine_fn v9fs_rename(void *opaque)
2634 V9fsPDU *pdu = opaque;
2635 V9fsState *s = pdu->s;
2637 v9fs_string_init(&name);
2638 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2643 if (name_is_illegal(name.data)) {
2648 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2653 fidp = get_fid(pdu, fid);
2658 if (fidp->fid_type != P9_FID_NONE) {
2662 /* if fs driver is not path based, return EOPNOTSUPP */
2663 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2667 v9fs_path_write_lock(s);
2668 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
2669 v9fs_path_unlock(s);
2676 pdu_complete(pdu, err);
2677 v9fs_string_free(&name);
2680 static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2681 V9fsString *old_name,
2683 V9fsString *new_name)
2685 V9fsFidState *tfidp;
2686 V9fsPath oldpath, newpath;
2687 V9fsState *s = pdu->s;
2690 v9fs_path_init(&oldpath);
2691 v9fs_path_init(&newpath);
2692 err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
2696 err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2702 * Fixup fid's pointing to the old name to
2703 * start pointing to the new name
2705 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2706 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
2707 /* replace the name */
2708 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
2712 v9fs_path_free(&oldpath);
2713 v9fs_path_free(&newpath);
2717 static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2718 V9fsString *old_name,
2720 V9fsString *new_name)
2723 V9fsState *s = pdu->s;
2724 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2726 olddirfidp = get_fid(pdu, olddirfid);
2727 if (olddirfidp == NULL) {
2731 if (newdirfid != -1) {
2732 newdirfidp = get_fid(pdu, newdirfid);
2733 if (newdirfidp == NULL) {
2738 newdirfidp = get_fid(pdu, olddirfid);
2741 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2742 &newdirfidp->path, new_name);
2746 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
2747 /* Only for path based fid we need to do the below fixup */
2748 err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2749 &newdirfidp->path, new_name);
2753 put_fid(pdu, olddirfidp);
2756 put_fid(pdu, newdirfidp);
2761 static void coroutine_fn v9fs_renameat(void *opaque)
2765 V9fsPDU *pdu = opaque;
2766 V9fsState *s = pdu->s;
2767 int32_t olddirfid, newdirfid;
2768 V9fsString old_name, new_name;
2770 v9fs_string_init(&old_name);
2771 v9fs_string_init(&new_name);
2772 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2773 &old_name, &newdirfid, &new_name);
2778 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
2783 if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
2784 !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
2789 v9fs_path_write_lock(s);
2790 err = v9fs_complete_renameat(pdu, olddirfid,
2791 &old_name, newdirfid, &new_name);
2792 v9fs_path_unlock(s);
2798 pdu_complete(pdu, err);
2799 v9fs_string_free(&old_name);
2800 v9fs_string_free(&new_name);
2803 static void coroutine_fn v9fs_wstat(void *opaque)
2812 V9fsPDU *pdu = opaque;
2814 v9fs_stat_init(&v9stat);
2815 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
2819 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
2820 v9stat.mode, v9stat.atime, v9stat.mtime);
2822 fidp = get_fid(pdu, fid);
2827 /* do we need to sync the file? */
2828 if (donttouch_stat(&v9stat)) {
2829 err = v9fs_co_fsync(pdu, fidp, 0);
2832 if (v9stat.mode != -1) {
2834 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2838 v9_mode = stat_to_v9mode(&stbuf);
2839 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2840 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2841 /* Attempting to change the type */
2845 err = v9fs_co_chmod(pdu, &fidp->path,
2846 v9mode_to_mode(v9stat.mode,
2847 &v9stat.extension));
2852 if (v9stat.mtime != -1 || v9stat.atime != -1) {
2853 struct timespec times[2];
2854 if (v9stat.atime != -1) {
2855 times[0].tv_sec = v9stat.atime;
2856 times[0].tv_nsec = 0;
2858 times[0].tv_nsec = UTIME_OMIT;
2860 if (v9stat.mtime != -1) {
2861 times[1].tv_sec = v9stat.mtime;
2862 times[1].tv_nsec = 0;
2864 times[1].tv_nsec = UTIME_OMIT;
2866 err = v9fs_co_utimensat(pdu, &fidp->path, times);
2871 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
2872 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
2877 if (v9stat.name.size != 0) {
2878 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
2883 if (v9stat.length != -1) {
2884 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
2893 v9fs_stat_free(&v9stat);
2894 pdu_complete(pdu, err);
2897 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2909 int32_t bsize_factor;
2912 * compute bsize factor based on host file system block size
2915 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2916 if (!bsize_factor) {
2919 f_type = stbuf->f_type;
2920 f_bsize = stbuf->f_bsize;
2921 f_bsize *= bsize_factor;
2923 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2924 * adjust(divide) the number of blocks, free blocks and available
2925 * blocks by bsize factor
2927 f_blocks = stbuf->f_blocks/bsize_factor;
2928 f_bfree = stbuf->f_bfree/bsize_factor;
2929 f_bavail = stbuf->f_bavail/bsize_factor;
2930 f_files = stbuf->f_files;
2931 f_ffree = stbuf->f_ffree;
2932 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
2933 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
2934 f_namelen = stbuf->f_namelen;
2936 return pdu_marshal(pdu, offset, "ddqqqqqqd",
2937 f_type, f_bsize, f_blocks, f_bfree,
2938 f_bavail, f_files, f_ffree,
2939 fsid_val, f_namelen);
2942 static void coroutine_fn v9fs_statfs(void *opaque)
2948 struct statfs stbuf;
2949 V9fsPDU *pdu = opaque;
2950 V9fsState *s = pdu->s;
2952 retval = pdu_unmarshal(pdu, offset, "d", &fid);
2956 fidp = get_fid(pdu, fid);
2961 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
2965 retval = v9fs_fill_statfs(s, pdu, &stbuf);
2973 pdu_complete(pdu, retval);
2976 static void coroutine_fn v9fs_mknod(void *opaque)
2989 V9fsPDU *pdu = opaque;
2991 v9fs_string_init(&name);
2992 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
2993 &major, &minor, &gid);
2997 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
2999 if (name_is_illegal(name.data)) {
3004 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3009 fidp = get_fid(pdu, fid);
3014 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
3015 makedev(major, minor), mode, &stbuf);
3019 stat_to_qid(&stbuf, &qid);
3020 err = pdu_marshal(pdu, offset, "Q", &qid);
3025 trace_v9fs_mknod_return(pdu->tag, pdu->id,
3026 qid.type, qid.version, qid.path);
3030 pdu_complete(pdu, err);
3031 v9fs_string_free(&name);
3035 * Implement posix byte range locking code
3036 * Server side handling of locking code is very simple, because 9p server in
3037 * QEMU can handle only one client. And most of the lock handling
3038 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3039 * do any thing in * qemu 9p server side lock code path.
3040 * So when a TLOCK request comes, always return success
3042 static void coroutine_fn v9fs_lock(void *opaque)
3048 int32_t fid, err = 0;
3049 V9fsPDU *pdu = opaque;
3051 v9fs_string_init(&flock.client_id);
3052 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
3053 &flock.flags, &flock.start, &flock.length,
3054 &flock.proc_id, &flock.client_id);
3058 trace_v9fs_lock(pdu->tag, pdu->id, fid,
3059 flock.type, flock.start, flock.length);
3062 /* We support only block flag now (that too ignored currently) */
3063 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
3067 fidp = get_fid(pdu, fid);
3072 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3076 err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS);
3081 trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS);
3085 pdu_complete(pdu, err);
3086 v9fs_string_free(&flock.client_id);
3090 * When a TGETLOCK request comes, always return success because all lock
3091 * handling is done by client's VFS layer.
3093 static void coroutine_fn v9fs_getlock(void *opaque)
3099 int32_t fid, err = 0;
3100 V9fsPDU *pdu = opaque;
3102 v9fs_string_init(&glock.client_id);
3103 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
3104 &glock.start, &glock.length, &glock.proc_id,
3109 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
3110 glock.type, glock.start, glock.length);
3112 fidp = get_fid(pdu, fid);
3117 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3121 glock.type = P9_LOCK_TYPE_UNLCK;
3122 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
3123 glock.start, glock.length, glock.proc_id,
3129 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
3130 glock.length, glock.proc_id);
3134 pdu_complete(pdu, err);
3135 v9fs_string_free(&glock.client_id);
3138 static void coroutine_fn v9fs_mkdir(void *opaque)
3140 V9fsPDU *pdu = opaque;
3151 v9fs_string_init(&name);
3152 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
3156 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
3158 if (name_is_illegal(name.data)) {
3163 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3168 fidp = get_fid(pdu, fid);
3173 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
3177 stat_to_qid(&stbuf, &qid);
3178 err = pdu_marshal(pdu, offset, "Q", &qid);
3183 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
3184 qid.type, qid.version, qid.path, err);
3188 pdu_complete(pdu, err);
3189 v9fs_string_free(&name);
3192 static void coroutine_fn v9fs_xattrwalk(void *opaque)
3198 int32_t fid, newfid;
3199 V9fsFidState *file_fidp;
3200 V9fsFidState *xattr_fidp = NULL;
3201 V9fsPDU *pdu = opaque;
3202 V9fsState *s = pdu->s;
3204 v9fs_string_init(&name);
3205 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3209 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3211 file_fidp = get_fid(pdu, fid);
3212 if (file_fidp == NULL) {
3216 xattr_fidp = alloc_fid(s, newfid);
3217 if (xattr_fidp == NULL) {
3221 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3222 if (!v9fs_string_size(&name)) {
3224 * listxattr request. Get the size first
3226 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3229 clunk_fid(s, xattr_fidp->fid);
3233 * Read the xattr value
3235 xattr_fidp->fs.xattr.len = size;
3236 xattr_fidp->fid_type = P9_FID_XATTR;
3237 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3239 xattr_fidp->fs.xattr.value = g_malloc(size);
3240 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3241 xattr_fidp->fs.xattr.value,
3242 xattr_fidp->fs.xattr.len);
3244 clunk_fid(s, xattr_fidp->fid);
3248 err = pdu_marshal(pdu, offset, "q", size);
3255 * specific xattr fid. We check for xattr
3256 * presence also collect the xattr size
3258 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3262 clunk_fid(s, xattr_fidp->fid);
3266 * Read the xattr value
3268 xattr_fidp->fs.xattr.len = size;
3269 xattr_fidp->fid_type = P9_FID_XATTR;
3270 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3272 xattr_fidp->fs.xattr.value = g_malloc(size);
3273 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3274 &name, xattr_fidp->fs.xattr.value,
3275 xattr_fidp->fs.xattr.len);
3277 clunk_fid(s, xattr_fidp->fid);
3281 err = pdu_marshal(pdu, offset, "q", size);
3287 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3289 put_fid(pdu, file_fidp);
3291 put_fid(pdu, xattr_fidp);
3294 pdu_complete(pdu, err);
3295 v9fs_string_free(&name);
3298 static void coroutine_fn v9fs_xattrcreate(void *opaque)
3306 V9fsFidState *file_fidp;
3307 V9fsFidState *xattr_fidp;
3308 V9fsPDU *pdu = opaque;
3310 v9fs_string_init(&name);
3311 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3315 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3317 if (size > XATTR_SIZE_MAX) {
3322 file_fidp = get_fid(pdu, fid);
3323 if (file_fidp == NULL) {
3327 if (file_fidp->fid_type != P9_FID_NONE) {
3332 /* Make the file fid point to xattr */
3333 xattr_fidp = file_fidp;
3334 xattr_fidp->fid_type = P9_FID_XATTR;
3335 xattr_fidp->fs.xattr.copied_len = 0;
3336 xattr_fidp->fs.xattr.xattrwalk_fid = false;
3337 xattr_fidp->fs.xattr.len = size;
3338 xattr_fidp->fs.xattr.flags = flags;
3339 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3340 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3341 xattr_fidp->fs.xattr.value = g_malloc0(size);
3344 put_fid(pdu, file_fidp);
3346 pdu_complete(pdu, err);
3347 v9fs_string_free(&name);
3350 static void coroutine_fn v9fs_readlink(void *opaque)
3352 V9fsPDU *pdu = opaque;
3359 err = pdu_unmarshal(pdu, offset, "d", &fid);
3363 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
3364 fidp = get_fid(pdu, fid);
3370 v9fs_string_init(&target);
3371 err = v9fs_co_readlink(pdu, &fidp->path, &target);
3375 err = pdu_marshal(pdu, offset, "s", &target);
3377 v9fs_string_free(&target);
3381 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
3382 v9fs_string_free(&target);
3386 pdu_complete(pdu, err);
3389 static CoroutineEntry *pdu_co_handlers[] = {
3390 [P9_TREADDIR] = v9fs_readdir,
3391 [P9_TSTATFS] = v9fs_statfs,
3392 [P9_TGETATTR] = v9fs_getattr,
3393 [P9_TSETATTR] = v9fs_setattr,
3394 [P9_TXATTRWALK] = v9fs_xattrwalk,
3395 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3396 [P9_TMKNOD] = v9fs_mknod,
3397 [P9_TRENAME] = v9fs_rename,
3398 [P9_TLOCK] = v9fs_lock,
3399 [P9_TGETLOCK] = v9fs_getlock,
3400 [P9_TRENAMEAT] = v9fs_renameat,
3401 [P9_TREADLINK] = v9fs_readlink,
3402 [P9_TUNLINKAT] = v9fs_unlinkat,
3403 [P9_TMKDIR] = v9fs_mkdir,
3404 [P9_TVERSION] = v9fs_version,
3405 [P9_TLOPEN] = v9fs_open,
3406 [P9_TATTACH] = v9fs_attach,
3407 [P9_TSTAT] = v9fs_stat,
3408 [P9_TWALK] = v9fs_walk,
3409 [P9_TCLUNK] = v9fs_clunk,
3410 [P9_TFSYNC] = v9fs_fsync,
3411 [P9_TOPEN] = v9fs_open,
3412 [P9_TREAD] = v9fs_read,
3414 [P9_TAUTH] = v9fs_auth,
3416 [P9_TFLUSH] = v9fs_flush,
3417 [P9_TLINK] = v9fs_link,
3418 [P9_TSYMLINK] = v9fs_symlink,
3419 [P9_TCREATE] = v9fs_create,
3420 [P9_TLCREATE] = v9fs_lcreate,
3421 [P9_TWRITE] = v9fs_write,
3422 [P9_TWSTAT] = v9fs_wstat,
3423 [P9_TREMOVE] = v9fs_remove,
3426 static void coroutine_fn v9fs_op_not_supp(void *opaque)
3428 V9fsPDU *pdu = opaque;
3429 pdu_complete(pdu, -EOPNOTSUPP);
3432 static void coroutine_fn v9fs_fs_ro(void *opaque)
3434 V9fsPDU *pdu = opaque;
3435 pdu_complete(pdu, -EROFS);
3438 static inline bool is_read_only_op(V9fsPDU *pdu)
3465 void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr)
3468 CoroutineEntry *handler;
3469 V9fsState *s = pdu->s;
3471 pdu->size = le32_to_cpu(hdr->size_le);
3473 pdu->tag = le16_to_cpu(hdr->tag_le);
3475 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3476 (pdu_co_handlers[pdu->id] == NULL)) {
3477 handler = v9fs_op_not_supp;
3479 handler = pdu_co_handlers[pdu->id];
3482 if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3483 handler = v9fs_fs_ro;
3486 qemu_co_queue_init(&pdu->complete);
3487 co = qemu_coroutine_create(handler, pdu);
3488 qemu_coroutine_enter(co);
3491 /* Returns 0 on success, 1 on failure. */
3492 int v9fs_device_realize_common(V9fsState *s, Error **errp)
3500 /* initialize pdu allocator */
3501 QLIST_INIT(&s->free_list);
3502 QLIST_INIT(&s->active_list);
3503 for (i = 0; i < MAX_REQ; i++) {
3504 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
3509 v9fs_path_init(&path);
3511 fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
3514 /* We don't have a fsdev identified by fsdev_id */
3515 error_setg(errp, "9pfs device couldn't find fsdev with the "
3517 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
3521 if (!s->fsconf.tag) {
3522 /* we haven't specified a mount_tag */
3523 error_setg(errp, "fsdev with id %s needs mount_tag arguments",
3524 s->fsconf.fsdev_id);
3528 s->ctx.export_flags = fse->export_flags;
3529 s->ctx.fs_root = g_strdup(fse->path);
3530 s->ctx.exops.get_st_gen = NULL;
3531 len = strlen(s->fsconf.tag);
3532 if (len > MAX_TAG_LEN - 1) {
3533 error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
3534 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
3538 s->tag = g_strdup(s->fsconf.tag);
3543 s->ctx.fmode = fse->fmode;
3544 s->ctx.dmode = fse->dmode;
3547 qemu_co_rwlock_init(&s->rename_lock);
3549 if (s->ops->init(&s->ctx) < 0) {
3550 error_setg(errp, "9pfs Failed to initialize fs-driver with id:%s"
3551 " and export path:%s", s->fsconf.fsdev_id, s->ctx.fs_root);
3556 * Check details of export path, We need to use fs driver
3557 * call back to do that. Since we are in the init path, we don't
3558 * use co-routines here.
3560 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
3562 "error in converting name to path %s", strerror(errno));
3565 if (s->ops->lstat(&s->ctx, &path, &stat)) {
3566 error_setg(errp, "share path %s does not exist", fse->path);
3568 } else if (!S_ISDIR(stat.st_mode)) {
3569 error_setg(errp, "share path %s is not a directory", fse->path);
3573 s->ctx.fst = &fse->fst;
3574 fsdev_throttle_init(s->ctx.fst);
3576 v9fs_path_free(&path);
3581 if (s->ops && s->ops->cleanup && s->ctx.private) {
3582 s->ops->cleanup(&s->ctx);
3585 g_free(s->ctx.fs_root);
3586 v9fs_path_free(&path);
3591 void v9fs_device_unrealize_common(V9fsState *s, Error **errp)
3593 if (s->ops->cleanup) {
3594 s->ops->cleanup(&s->ctx);
3596 fsdev_throttle_cleanup(s->ctx.fst);
3598 g_free(s->ctx.fs_root);
3601 typedef struct VirtfsCoResetData {
3604 } VirtfsCoResetData;
3606 static void coroutine_fn virtfs_co_reset(void *opaque)
3608 VirtfsCoResetData *data = opaque;
3610 virtfs_reset(&data->pdu);
3614 void v9fs_reset(V9fsState *s)
3616 VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
3619 while (!QLIST_EMPTY(&s->active_list)) {
3620 aio_poll(qemu_get_aio_context(), true);
3623 co = qemu_coroutine_create(virtfs_co_reset, &data);
3624 qemu_coroutine_enter(co);
3626 while (!data.done) {
3627 aio_poll(qemu_get_aio_context(), true);
3631 static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
3634 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
3635 error_report("Failed to get the resource limit");
3638 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
3639 open_fd_rc = rlim.rlim_cur/2;