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);
628 * We don't do error checking for pdu_marshal/unmarshal here
629 * because we always expect to have enough space to encode
632 static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
634 int8_t id = pdu->id + 1; /* Response */
635 V9fsState *s = pdu->s;
641 if (s->proto_version != V9FS_PROTO_2000L) {
644 str.data = strerror(err);
645 str.size = strlen(str.data);
647 len += pdu_marshal(pdu, len, "s", &str);
651 len += pdu_marshal(pdu, len, "d", err);
653 if (s->proto_version == V9FS_PROTO_2000L) {
656 trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
659 /* fill out the header */
660 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
662 /* keep these in sync */
666 pdu->s->transport->push_and_notify(pdu);
668 /* Now wakeup anybody waiting in flush for this request */
669 if (!qemu_co_queue_next(&pdu->complete)) {
674 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
679 if (mode & P9_STAT_MODE_DIR) {
683 if (mode & P9_STAT_MODE_SYMLINK) {
686 if (mode & P9_STAT_MODE_SOCKET) {
689 if (mode & P9_STAT_MODE_NAMED_PIPE) {
692 if (mode & P9_STAT_MODE_DEVICE) {
693 if (extension->size && extension->data[0] == 'c') {
704 if (mode & P9_STAT_MODE_SETUID) {
707 if (mode & P9_STAT_MODE_SETGID) {
710 if (mode & P9_STAT_MODE_SETVTX) {
717 static int donttouch_stat(V9fsStat *stat)
719 if (stat->type == -1 &&
721 stat->qid.type == -1 &&
722 stat->qid.version == -1 &&
723 stat->qid.path == -1 &&
727 stat->length == -1 &&
734 stat->n_muid == -1) {
741 static void v9fs_stat_init(V9fsStat *stat)
743 v9fs_string_init(&stat->name);
744 v9fs_string_init(&stat->uid);
745 v9fs_string_init(&stat->gid);
746 v9fs_string_init(&stat->muid);
747 v9fs_string_init(&stat->extension);
750 static void v9fs_stat_free(V9fsStat *stat)
752 v9fs_string_free(&stat->name);
753 v9fs_string_free(&stat->uid);
754 v9fs_string_free(&stat->gid);
755 v9fs_string_free(&stat->muid);
756 v9fs_string_free(&stat->extension);
759 static uint32_t stat_to_v9mode(const struct stat *stbuf)
763 mode = stbuf->st_mode & 0777;
764 if (S_ISDIR(stbuf->st_mode)) {
765 mode |= P9_STAT_MODE_DIR;
768 if (S_ISLNK(stbuf->st_mode)) {
769 mode |= P9_STAT_MODE_SYMLINK;
772 if (S_ISSOCK(stbuf->st_mode)) {
773 mode |= P9_STAT_MODE_SOCKET;
776 if (S_ISFIFO(stbuf->st_mode)) {
777 mode |= P9_STAT_MODE_NAMED_PIPE;
780 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
781 mode |= P9_STAT_MODE_DEVICE;
784 if (stbuf->st_mode & S_ISUID) {
785 mode |= P9_STAT_MODE_SETUID;
788 if (stbuf->st_mode & S_ISGID) {
789 mode |= P9_STAT_MODE_SETGID;
792 if (stbuf->st_mode & S_ISVTX) {
793 mode |= P9_STAT_MODE_SETVTX;
799 static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *name,
800 const struct stat *stbuf,
806 memset(v9stat, 0, sizeof(*v9stat));
808 stat_to_qid(stbuf, &v9stat->qid);
809 v9stat->mode = stat_to_v9mode(stbuf);
810 v9stat->atime = stbuf->st_atime;
811 v9stat->mtime = stbuf->st_mtime;
812 v9stat->length = stbuf->st_size;
814 v9fs_string_free(&v9stat->uid);
815 v9fs_string_free(&v9stat->gid);
816 v9fs_string_free(&v9stat->muid);
818 v9stat->n_uid = stbuf->st_uid;
819 v9stat->n_gid = stbuf->st_gid;
822 v9fs_string_free(&v9stat->extension);
824 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
825 err = v9fs_co_readlink(pdu, name, &v9stat->extension);
829 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
830 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
831 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
832 major(stbuf->st_rdev), minor(stbuf->st_rdev));
833 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
834 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
835 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
838 str = strrchr(name->data, '/');
845 v9fs_string_sprintf(&v9stat->name, "%s", str);
848 v9fs_string_size(&v9stat->name) +
849 v9fs_string_size(&v9stat->uid) +
850 v9fs_string_size(&v9stat->gid) +
851 v9fs_string_size(&v9stat->muid) +
852 v9fs_string_size(&v9stat->extension);
856 #define P9_STATS_MODE 0x00000001ULL
857 #define P9_STATS_NLINK 0x00000002ULL
858 #define P9_STATS_UID 0x00000004ULL
859 #define P9_STATS_GID 0x00000008ULL
860 #define P9_STATS_RDEV 0x00000010ULL
861 #define P9_STATS_ATIME 0x00000020ULL
862 #define P9_STATS_MTIME 0x00000040ULL
863 #define P9_STATS_CTIME 0x00000080ULL
864 #define P9_STATS_INO 0x00000100ULL
865 #define P9_STATS_SIZE 0x00000200ULL
866 #define P9_STATS_BLOCKS 0x00000400ULL
868 #define P9_STATS_BTIME 0x00000800ULL
869 #define P9_STATS_GEN 0x00001000ULL
870 #define P9_STATS_DATA_VERSION 0x00002000ULL
872 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
873 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
876 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
877 V9fsStatDotl *v9lstat)
879 memset(v9lstat, 0, sizeof(*v9lstat));
881 v9lstat->st_mode = stbuf->st_mode;
882 v9lstat->st_nlink = stbuf->st_nlink;
883 v9lstat->st_uid = stbuf->st_uid;
884 v9lstat->st_gid = stbuf->st_gid;
885 v9lstat->st_rdev = stbuf->st_rdev;
886 v9lstat->st_size = stbuf->st_size;
887 v9lstat->st_blksize = stbuf->st_blksize;
888 v9lstat->st_blocks = stbuf->st_blocks;
889 v9lstat->st_atime_sec = stbuf->st_atime;
890 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
891 v9lstat->st_mtime_sec = stbuf->st_mtime;
892 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
893 v9lstat->st_ctime_sec = stbuf->st_ctime;
894 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
895 /* Currently we only support BASIC fields in stat */
896 v9lstat->st_result_mask = P9_STATS_BASIC;
898 stat_to_qid(stbuf, &v9lstat->qid);
901 static void print_sg(struct iovec *sg, int cnt)
905 printf("sg[%d]: {", cnt);
906 for (i = 0; i < cnt; i++) {
910 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
915 /* Will call this only for path name based fid */
916 static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
919 v9fs_path_init(&str);
920 v9fs_path_copy(&str, dst);
921 v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
922 v9fs_path_free(&str);
925 static inline bool is_ro_export(FsContext *ctx)
927 return ctx->export_flags & V9FS_RDONLY;
930 static void coroutine_fn v9fs_version(void *opaque)
933 V9fsPDU *pdu = opaque;
934 V9fsState *s = pdu->s;
938 v9fs_string_init(&version);
939 err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
944 trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
948 if (!strcmp(version.data, "9P2000.u")) {
949 s->proto_version = V9FS_PROTO_2000U;
950 } else if (!strcmp(version.data, "9P2000.L")) {
951 s->proto_version = V9FS_PROTO_2000L;
953 v9fs_string_sprintf(&version, "unknown");
956 err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
962 trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
964 pdu_complete(pdu, offset);
965 v9fs_string_free(&version);
968 static void coroutine_fn v9fs_attach(void *opaque)
970 V9fsPDU *pdu = opaque;
971 V9fsState *s = pdu->s;
972 int32_t fid, afid, n_uname;
973 V9fsString uname, aname;
978 Error *local_err = NULL;
980 v9fs_string_init(&uname);
981 v9fs_string_init(&aname);
982 err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
983 &afid, &uname, &aname, &n_uname);
987 trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
989 fidp = alloc_fid(s, fid);
995 err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
1001 err = fid_to_qid(pdu, fidp, &qid);
1009 * disable migration if we haven't done already.
1010 * attach could get called multiple times for the same export.
1012 if (!s->migration_blocker) {
1013 error_setg(&s->migration_blocker,
1014 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1015 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
1016 err = migrate_add_blocker(s->migration_blocker, &local_err);
1018 error_free(local_err);
1019 error_free(s->migration_blocker);
1020 s->migration_blocker = NULL;
1027 err = pdu_marshal(pdu, offset, "Q", &qid);
1034 memcpy(&s->root_qid, &qid, sizeof(qid));
1035 trace_v9fs_attach_return(pdu->tag, pdu->id,
1036 qid.type, qid.version, qid.path);
1040 pdu_complete(pdu, err);
1041 v9fs_string_free(&uname);
1042 v9fs_string_free(&aname);
1045 static void coroutine_fn v9fs_stat(void *opaque)
1053 V9fsPDU *pdu = opaque;
1055 err = pdu_unmarshal(pdu, offset, "d", &fid);
1059 trace_v9fs_stat(pdu->tag, pdu->id, fid);
1061 fidp = get_fid(pdu, fid);
1066 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1070 err = stat_to_v9stat(pdu, &fidp->path, &stbuf, &v9stat);
1074 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1076 v9fs_stat_free(&v9stat);
1079 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1080 v9stat.atime, v9stat.mtime, v9stat.length);
1082 v9fs_stat_free(&v9stat);
1086 pdu_complete(pdu, err);
1089 static void coroutine_fn v9fs_getattr(void *opaque)
1096 uint64_t request_mask;
1097 V9fsStatDotl v9stat_dotl;
1098 V9fsPDU *pdu = opaque;
1099 V9fsState *s = pdu->s;
1101 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1105 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
1107 fidp = get_fid(pdu, fid);
1113 * Currently we only support BASIC fields in stat, so there is no
1114 * need to look at request_mask.
1116 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1120 stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
1122 /* fill st_gen if requested and supported by underlying fs */
1123 if (request_mask & P9_STATS_GEN) {
1124 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
1127 /* we have valid st_gen: update result mask */
1128 v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1131 /* request cancelled, e.g. by Tflush */
1134 /* failed to get st_gen: not fatal, ignore */
1138 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1143 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1144 v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1145 v9stat_dotl.st_gid);
1149 pdu_complete(pdu, retval);
1152 /* Attribute flags */
1153 #define P9_ATTR_MODE (1 << 0)
1154 #define P9_ATTR_UID (1 << 1)
1155 #define P9_ATTR_GID (1 << 2)
1156 #define P9_ATTR_SIZE (1 << 3)
1157 #define P9_ATTR_ATIME (1 << 4)
1158 #define P9_ATTR_MTIME (1 << 5)
1159 #define P9_ATTR_CTIME (1 << 6)
1160 #define P9_ATTR_ATIME_SET (1 << 7)
1161 #define P9_ATTR_MTIME_SET (1 << 8)
1163 #define P9_ATTR_MASK 127
1165 static void coroutine_fn v9fs_setattr(void *opaque)
1172 V9fsPDU *pdu = opaque;
1174 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1179 fidp = get_fid(pdu, fid);
1184 if (v9iattr.valid & P9_ATTR_MODE) {
1185 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1190 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1191 struct timespec times[2];
1192 if (v9iattr.valid & P9_ATTR_ATIME) {
1193 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1194 times[0].tv_sec = v9iattr.atime_sec;
1195 times[0].tv_nsec = v9iattr.atime_nsec;
1197 times[0].tv_nsec = UTIME_NOW;
1200 times[0].tv_nsec = UTIME_OMIT;
1202 if (v9iattr.valid & P9_ATTR_MTIME) {
1203 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1204 times[1].tv_sec = v9iattr.mtime_sec;
1205 times[1].tv_nsec = v9iattr.mtime_nsec;
1207 times[1].tv_nsec = UTIME_NOW;
1210 times[1].tv_nsec = UTIME_OMIT;
1212 err = v9fs_co_utimensat(pdu, &fidp->path, times);
1218 * If the only valid entry in iattr is ctime we can call
1219 * chown(-1,-1) to update the ctime of the file
1221 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1222 ((v9iattr.valid & P9_ATTR_CTIME)
1223 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1224 if (!(v9iattr.valid & P9_ATTR_UID)) {
1227 if (!(v9iattr.valid & P9_ATTR_GID)) {
1230 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1236 if (v9iattr.valid & (P9_ATTR_SIZE)) {
1237 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1246 pdu_complete(pdu, err);
1249 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1255 err = pdu_marshal(pdu, offset, "w", nwnames);
1260 for (i = 0; i < nwnames; i++) {
1261 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1270 static bool name_is_illegal(const char *name)
1272 return !*name || strchr(name, '/') != NULL;
1275 static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
1278 qid1->type != qid2->type ||
1279 qid1->version != qid2->version ||
1280 qid1->path != qid2->path;
1283 static void coroutine_fn v9fs_walk(void *opaque)
1286 V9fsQID *qids = NULL;
1288 V9fsPath dpath, path;
1292 int32_t fid, newfid;
1293 V9fsString *wnames = NULL;
1295 V9fsFidState *newfidp = NULL;
1296 V9fsPDU *pdu = opaque;
1297 V9fsState *s = pdu->s;
1300 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1302 pdu_complete(pdu, err);
1307 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1309 if (nwnames && nwnames <= P9_MAXWELEM) {
1310 wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
1311 qids = g_malloc0(sizeof(qids[0]) * nwnames);
1312 for (i = 0; i < nwnames; i++) {
1313 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1317 if (name_is_illegal(wnames[i].data)) {
1323 } else if (nwnames > P9_MAXWELEM) {
1327 fidp = get_fid(pdu, fid);
1333 v9fs_path_init(&dpath);
1334 v9fs_path_init(&path);
1336 err = fid_to_qid(pdu, fidp, &qid);
1342 * Both dpath and path initially poin to fidp.
1343 * Needed to handle request with nwnames == 0
1345 v9fs_path_copy(&dpath, &fidp->path);
1346 v9fs_path_copy(&path, &fidp->path);
1347 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1348 if (not_same_qid(&pdu->s->root_qid, &qid) ||
1349 strcmp("..", wnames[name_idx].data)) {
1350 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
1356 err = v9fs_co_lstat(pdu, &path, &stbuf);
1360 stat_to_qid(&stbuf, &qid);
1361 v9fs_path_copy(&dpath, &path);
1363 memcpy(&qids[name_idx], &qid, sizeof(qid));
1365 if (fid == newfid) {
1366 if (fidp->fid_type != P9_FID_NONE) {
1370 v9fs_path_copy(&fidp->path, &path);
1372 newfidp = alloc_fid(s, newfid);
1373 if (newfidp == NULL) {
1377 newfidp->uid = fidp->uid;
1378 v9fs_path_copy(&newfidp->path, &path);
1380 err = v9fs_walk_marshal(pdu, nwnames, qids);
1381 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1385 put_fid(pdu, newfidp);
1387 v9fs_path_free(&dpath);
1388 v9fs_path_free(&path);
1390 pdu_complete(pdu, err);
1391 if (nwnames && nwnames <= P9_MAXWELEM) {
1392 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1393 v9fs_string_free(&wnames[name_idx]);
1400 static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
1402 struct statfs stbuf;
1404 V9fsState *s = pdu->s;
1407 * iounit should be multiples of f_bsize (host filesystem block size
1408 * and as well as less than (client msize - P9_IOHDRSZ))
1410 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1411 iounit = stbuf.f_bsize;
1412 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1415 iounit = s->msize - P9_IOHDRSZ;
1420 static void coroutine_fn v9fs_open(void *opaque)
1431 V9fsPDU *pdu = opaque;
1432 V9fsState *s = pdu->s;
1434 if (s->proto_version == V9FS_PROTO_2000L) {
1435 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1438 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1444 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1446 fidp = get_fid(pdu, fid);
1451 if (fidp->fid_type != P9_FID_NONE) {
1456 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1460 stat_to_qid(&stbuf, &qid);
1461 if (S_ISDIR(stbuf.st_mode)) {
1462 err = v9fs_co_opendir(pdu, fidp);
1466 fidp->fid_type = P9_FID_DIR;
1467 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1473 if (s->proto_version == V9FS_PROTO_2000L) {
1474 flags = get_dotl_openflags(s, mode);
1476 flags = omode_to_uflags(mode);
1478 if (is_ro_export(&s->ctx)) {
1479 if (mode & O_WRONLY || mode & O_RDWR ||
1480 mode & O_APPEND || mode & O_TRUNC) {
1485 err = v9fs_co_open(pdu, fidp, flags);
1489 fidp->fid_type = P9_FID_FILE;
1490 fidp->open_flags = flags;
1491 if (flags & O_EXCL) {
1493 * We let the host file system do O_EXCL check
1494 * We should not reclaim such fd
1496 fidp->flags |= FID_NON_RECLAIMABLE;
1498 iounit = get_iounit(pdu, &fidp->path);
1499 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1505 trace_v9fs_open_return(pdu->tag, pdu->id,
1506 qid.type, qid.version, qid.path, iounit);
1510 pdu_complete(pdu, err);
1513 static void coroutine_fn v9fs_lcreate(void *opaque)
1515 int32_t dfid, flags, mode;
1524 V9fsPDU *pdu = opaque;
1526 v9fs_string_init(&name);
1527 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1528 &name, &flags, &mode, &gid);
1532 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1534 if (name_is_illegal(name.data)) {
1539 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
1544 fidp = get_fid(pdu, dfid);
1549 if (fidp->fid_type != P9_FID_NONE) {
1554 flags = get_dotl_openflags(pdu->s, flags);
1555 err = v9fs_co_open2(pdu, fidp, &name, gid,
1556 flags | O_CREAT, mode, &stbuf);
1560 fidp->fid_type = P9_FID_FILE;
1561 fidp->open_flags = flags;
1562 if (flags & O_EXCL) {
1564 * We let the host file system do O_EXCL check
1565 * We should not reclaim such fd
1567 fidp->flags |= FID_NON_RECLAIMABLE;
1569 iounit = get_iounit(pdu, &fidp->path);
1570 stat_to_qid(&stbuf, &qid);
1571 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1576 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
1577 qid.type, qid.version, qid.path, iounit);
1581 pdu_complete(pdu, err);
1582 v9fs_string_free(&name);
1585 static void coroutine_fn v9fs_fsync(void *opaque)
1592 V9fsPDU *pdu = opaque;
1594 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1598 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
1600 fidp = get_fid(pdu, fid);
1605 err = v9fs_co_fsync(pdu, fidp, datasync);
1611 pdu_complete(pdu, err);
1614 static void coroutine_fn v9fs_clunk(void *opaque)
1620 V9fsPDU *pdu = opaque;
1621 V9fsState *s = pdu->s;
1623 err = pdu_unmarshal(pdu, offset, "d", &fid);
1627 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
1629 fidp = clunk_fid(s, fid);
1635 * Bump the ref so that put_fid will
1639 err = put_fid(pdu, fidp);
1644 pdu_complete(pdu, err);
1648 * Create a QEMUIOVector for a sub-region of PDU iovecs
1650 * @qiov: uninitialized QEMUIOVector
1651 * @skip: number of bytes to skip from beginning of PDU
1652 * @size: number of bytes to include
1653 * @is_write: true - write, false - read
1655 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1656 * with qemu_iovec_destroy().
1658 static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1659 size_t skip, size_t size,
1667 pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip);
1669 pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip);
1672 qemu_iovec_init_external(&elem, iov, niov);
1673 qemu_iovec_init(qiov, niov);
1674 qemu_iovec_concat(qiov, &elem, skip, size);
1677 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1678 uint64_t off, uint32_t max_count)
1682 uint64_t read_count;
1683 QEMUIOVector qiov_full;
1685 if (fidp->fs.xattr.len < off) {
1688 read_count = fidp->fs.xattr.len - off;
1690 if (read_count > max_count) {
1691 read_count = max_count;
1693 err = pdu_marshal(pdu, offset, "d", read_count);
1699 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false);
1700 err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0,
1701 ((char *)fidp->fs.xattr.value) + off,
1703 qemu_iovec_destroy(&qiov_full);
1711 static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1720 off_t saved_dir_pos;
1721 struct dirent *dent;
1723 /* save the directory position */
1724 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1725 if (saved_dir_pos < 0) {
1726 return saved_dir_pos;
1730 v9fs_path_init(&path);
1732 v9fs_readdir_lock(&fidp->fs.dir);
1734 err = v9fs_co_readdir(pdu, fidp, &dent);
1738 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
1742 err = v9fs_co_lstat(pdu, &path, &stbuf);
1746 err = stat_to_v9stat(pdu, &path, &stbuf, &v9stat);
1750 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1751 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1753 v9fs_readdir_unlock(&fidp->fs.dir);
1755 if ((len != (v9stat.size + 2)) || ((count + len) > max_count)) {
1756 /* Ran out of buffer. Set dir back to old position and return */
1757 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1758 v9fs_stat_free(&v9stat);
1759 v9fs_path_free(&path);
1763 v9fs_stat_free(&v9stat);
1764 v9fs_path_free(&path);
1765 saved_dir_pos = dent->d_off;
1768 v9fs_readdir_unlock(&fidp->fs.dir);
1770 v9fs_path_free(&path);
1777 static void coroutine_fn v9fs_read(void *opaque)
1786 V9fsPDU *pdu = opaque;
1787 V9fsState *s = pdu->s;
1789 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1793 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
1795 fidp = get_fid(pdu, fid);
1800 if (fidp->fid_type == P9_FID_DIR) {
1803 v9fs_co_rewinddir(pdu, fidp);
1805 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
1810 err = pdu_marshal(pdu, offset, "d", count);
1814 err += offset + count;
1815 } else if (fidp->fid_type == P9_FID_FILE) {
1816 QEMUIOVector qiov_full;
1820 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
1821 qemu_iovec_init(&qiov, qiov_full.niov);
1823 qemu_iovec_reset(&qiov);
1824 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
1826 print_sg(qiov.iov, qiov.niov);
1828 /* Loop in case of EINTR */
1830 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
1835 } while (len == -EINTR && !pdu->cancelled);
1837 /* IO error return the error */
1839 goto out_free_iovec;
1841 } while (count < max_count && len > 0);
1842 err = pdu_marshal(pdu, offset, "d", count);
1844 goto out_free_iovec;
1846 err += offset + count;
1848 qemu_iovec_destroy(&qiov);
1849 qemu_iovec_destroy(&qiov_full);
1850 } else if (fidp->fid_type == P9_FID_XATTR) {
1851 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
1855 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
1859 pdu_complete(pdu, err);
1862 static size_t v9fs_readdir_data_size(V9fsString *name)
1865 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1866 * size of type (1) + size of name.size (2) + strlen(name.data)
1868 return 24 + v9fs_string_size(name);
1871 static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
1879 off_t saved_dir_pos;
1880 struct dirent *dent;
1882 /* save the directory position */
1883 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1884 if (saved_dir_pos < 0) {
1885 return saved_dir_pos;
1889 v9fs_readdir_lock(&fidp->fs.dir);
1891 err = v9fs_co_readdir(pdu, fidp, &dent);
1895 v9fs_string_init(&name);
1896 v9fs_string_sprintf(&name, "%s", dent->d_name);
1897 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
1898 v9fs_readdir_unlock(&fidp->fs.dir);
1900 /* Ran out of buffer. Set dir back to old position and return */
1901 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1902 v9fs_string_free(&name);
1906 * Fill up just the path field of qid because the client uses
1907 * only that. To fill the entire qid structure we will have
1908 * to stat each dirent found, which is expensive
1910 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1911 memcpy(&qid.path, &dent->d_ino, size);
1912 /* Fill the other fields with dummy values */
1916 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1917 len = pdu_marshal(pdu, 11 + count, "Qqbs",
1919 dent->d_type, &name);
1921 v9fs_readdir_unlock(&fidp->fs.dir);
1924 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1925 v9fs_string_free(&name);
1929 v9fs_string_free(&name);
1930 saved_dir_pos = dent->d_off;
1933 v9fs_readdir_unlock(&fidp->fs.dir);
1941 static void coroutine_fn v9fs_readdir(void *opaque)
1947 uint64_t initial_offset;
1950 V9fsPDU *pdu = opaque;
1952 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
1953 &initial_offset, &max_count);
1957 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
1959 fidp = get_fid(pdu, fid);
1964 if (!fidp->fs.dir.stream) {
1968 if (initial_offset == 0) {
1969 v9fs_co_rewinddir(pdu, fidp);
1971 v9fs_co_seekdir(pdu, fidp, initial_offset);
1973 count = v9fs_do_readdir(pdu, fidp, max_count);
1978 retval = pdu_marshal(pdu, offset, "d", count);
1982 retval += count + offset;
1983 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
1987 pdu_complete(pdu, retval);
1990 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1991 uint64_t off, uint32_t count,
1992 struct iovec *sg, int cnt)
1996 uint64_t write_count;
2000 if (fidp->fs.xattr.len < off) {
2004 write_count = fidp->fs.xattr.len - off;
2005 if (write_count > count) {
2006 write_count = count;
2008 err = pdu_marshal(pdu, offset, "d", write_count);
2013 fidp->fs.xattr.copied_len += write_count;
2015 * Now copy the content from sg list
2017 for (i = 0; i < cnt; i++) {
2018 if (write_count > sg[i].iov_len) {
2019 to_copy = sg[i].iov_len;
2021 to_copy = write_count;
2023 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
2024 /* updating vs->off since we are not using below */
2026 write_count -= to_copy;
2032 static void coroutine_fn v9fs_write(void *opaque)
2042 V9fsPDU *pdu = opaque;
2043 V9fsState *s = pdu->s;
2044 QEMUIOVector qiov_full;
2047 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
2049 pdu_complete(pdu, err);
2053 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
2054 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
2056 fidp = get_fid(pdu, fid);
2061 if (fidp->fid_type == P9_FID_FILE) {
2062 if (fidp->fs.fd == -1) {
2066 } else if (fidp->fid_type == P9_FID_XATTR) {
2068 * setxattr operation
2070 err = v9fs_xattr_write(s, pdu, fidp, off, count,
2071 qiov_full.iov, qiov_full.niov);
2077 qemu_iovec_init(&qiov, qiov_full.niov);
2079 qemu_iovec_reset(&qiov);
2080 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
2082 print_sg(qiov.iov, qiov.niov);
2084 /* Loop in case of EINTR */
2086 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
2091 } while (len == -EINTR && !pdu->cancelled);
2093 /* IO error return the error */
2097 } while (total < count && len > 0);
2100 err = pdu_marshal(pdu, offset, "d", total);
2105 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2107 qemu_iovec_destroy(&qiov);
2111 qemu_iovec_destroy(&qiov_full);
2112 pdu_complete(pdu, err);
2115 static void coroutine_fn v9fs_create(void *opaque)
2127 V9fsString extension;
2129 V9fsPDU *pdu = opaque;
2131 v9fs_path_init(&path);
2132 v9fs_string_init(&name);
2133 v9fs_string_init(&extension);
2134 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2135 &perm, &mode, &extension);
2139 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2141 if (name_is_illegal(name.data)) {
2146 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2151 fidp = get_fid(pdu, fid);
2156 if (fidp->fid_type != P9_FID_NONE) {
2160 if (perm & P9_STAT_MODE_DIR) {
2161 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2162 fidp->uid, -1, &stbuf);
2166 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2170 v9fs_path_copy(&fidp->path, &path);
2171 err = v9fs_co_opendir(pdu, fidp);
2175 fidp->fid_type = P9_FID_DIR;
2176 } else if (perm & P9_STAT_MODE_SYMLINK) {
2177 err = v9fs_co_symlink(pdu, fidp, &name,
2178 extension.data, -1 , &stbuf);
2182 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2186 v9fs_path_copy(&fidp->path, &path);
2187 } else if (perm & P9_STAT_MODE_LINK) {
2188 int32_t ofid = atoi(extension.data);
2189 V9fsFidState *ofidp = get_fid(pdu, ofid);
2190 if (ofidp == NULL) {
2194 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2195 put_fid(pdu, ofidp);
2199 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2201 fidp->fid_type = P9_FID_NONE;
2204 v9fs_path_copy(&fidp->path, &path);
2205 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2207 fidp->fid_type = P9_FID_NONE;
2210 } else if (perm & P9_STAT_MODE_DEVICE) {
2212 uint32_t major, minor;
2215 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2232 nmode |= perm & 0777;
2233 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2234 makedev(major, minor), nmode, &stbuf);
2238 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2242 v9fs_path_copy(&fidp->path, &path);
2243 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2244 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2245 0, S_IFIFO | (perm & 0777), &stbuf);
2249 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2253 v9fs_path_copy(&fidp->path, &path);
2254 } else if (perm & P9_STAT_MODE_SOCKET) {
2255 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2256 0, S_IFSOCK | (perm & 0777), &stbuf);
2260 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2264 v9fs_path_copy(&fidp->path, &path);
2266 err = v9fs_co_open2(pdu, fidp, &name, -1,
2267 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2271 fidp->fid_type = P9_FID_FILE;
2272 fidp->open_flags = omode_to_uflags(mode);
2273 if (fidp->open_flags & O_EXCL) {
2275 * We let the host file system do O_EXCL check
2276 * We should not reclaim such fd
2278 fidp->flags |= FID_NON_RECLAIMABLE;
2281 iounit = get_iounit(pdu, &fidp->path);
2282 stat_to_qid(&stbuf, &qid);
2283 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2288 trace_v9fs_create_return(pdu->tag, pdu->id,
2289 qid.type, qid.version, qid.path, iounit);
2293 pdu_complete(pdu, err);
2294 v9fs_string_free(&name);
2295 v9fs_string_free(&extension);
2296 v9fs_path_free(&path);
2299 static void coroutine_fn v9fs_symlink(void *opaque)
2301 V9fsPDU *pdu = opaque;
2304 V9fsFidState *dfidp;
2312 v9fs_string_init(&name);
2313 v9fs_string_init(&symname);
2314 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2318 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2320 if (name_is_illegal(name.data)) {
2325 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2330 dfidp = get_fid(pdu, dfid);
2331 if (dfidp == NULL) {
2335 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2339 stat_to_qid(&stbuf, &qid);
2340 err = pdu_marshal(pdu, offset, "Q", &qid);
2345 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2346 qid.type, qid.version, qid.path);
2348 put_fid(pdu, dfidp);
2350 pdu_complete(pdu, err);
2351 v9fs_string_free(&name);
2352 v9fs_string_free(&symname);
2355 static void coroutine_fn v9fs_flush(void *opaque)
2360 V9fsPDU *cancel_pdu = NULL;
2361 V9fsPDU *pdu = opaque;
2362 V9fsState *s = pdu->s;
2364 err = pdu_unmarshal(pdu, offset, "w", &tag);
2366 pdu_complete(pdu, err);
2369 trace_v9fs_flush(pdu->tag, pdu->id, tag);
2371 if (pdu->tag == tag) {
2372 error_report("Warning: the guest sent a self-referencing 9P flush request");
2374 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2375 if (cancel_pdu->tag == tag) {
2381 cancel_pdu->cancelled = 1;
2383 * Wait for pdu to complete.
2385 qemu_co_queue_wait(&cancel_pdu->complete, NULL);
2386 if (!qemu_co_queue_next(&cancel_pdu->complete)) {
2387 cancel_pdu->cancelled = 0;
2388 pdu_free(cancel_pdu);
2391 pdu_complete(pdu, 7);
2394 static void coroutine_fn v9fs_link(void *opaque)
2396 V9fsPDU *pdu = opaque;
2397 int32_t dfid, oldfid;
2398 V9fsFidState *dfidp, *oldfidp;
2403 v9fs_string_init(&name);
2404 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2408 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2410 if (name_is_illegal(name.data)) {
2415 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2420 dfidp = get_fid(pdu, dfid);
2421 if (dfidp == NULL) {
2426 oldfidp = get_fid(pdu, oldfid);
2427 if (oldfidp == NULL) {
2431 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2435 put_fid(pdu, oldfidp);
2437 put_fid(pdu, dfidp);
2439 v9fs_string_free(&name);
2440 pdu_complete(pdu, err);
2443 /* Only works with path name based fid */
2444 static void coroutine_fn v9fs_remove(void *opaque)
2450 V9fsPDU *pdu = opaque;
2452 err = pdu_unmarshal(pdu, offset, "d", &fid);
2456 trace_v9fs_remove(pdu->tag, pdu->id, fid);
2458 fidp = get_fid(pdu, fid);
2463 /* if fs driver is not path based, return EOPNOTSUPP */
2464 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2469 * IF the file is unlinked, we cannot reopen
2470 * the file later. So don't reclaim fd
2472 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2476 err = v9fs_co_remove(pdu, &fidp->path);
2481 /* For TREMOVE we need to clunk the fid even on failed remove */
2482 clunk_fid(pdu->s, fidp->fid);
2485 pdu_complete(pdu, err);
2488 static void coroutine_fn v9fs_unlinkat(void *opaque)
2492 int32_t dfid, flags;
2495 V9fsFidState *dfidp;
2496 V9fsPDU *pdu = opaque;
2498 v9fs_string_init(&name);
2499 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2504 if (name_is_illegal(name.data)) {
2509 if (!strcmp(".", name.data)) {
2514 if (!strcmp("..", name.data)) {
2519 dfidp = get_fid(pdu, dfid);
2520 if (dfidp == NULL) {
2525 * IF the file is unlinked, we cannot reopen
2526 * the file later. So don't reclaim fd
2528 v9fs_path_init(&path);
2529 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2533 err = v9fs_mark_fids_unreclaim(pdu, &path);
2537 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, flags);
2542 put_fid(pdu, dfidp);
2543 v9fs_path_free(&path);
2545 pdu_complete(pdu, err);
2546 v9fs_string_free(&name);
2550 /* Only works with path name based fid */
2551 static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2558 V9fsFidState *tfidp;
2559 V9fsState *s = pdu->s;
2560 V9fsFidState *dirfidp = NULL;
2561 char *old_name, *new_name;
2563 v9fs_path_init(&new_path);
2564 if (newdirfid != -1) {
2565 dirfidp = get_fid(pdu, newdirfid);
2566 if (dirfidp == NULL) {
2570 if (fidp->fid_type != P9_FID_NONE) {
2574 err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2579 old_name = fidp->path.data;
2580 end = strrchr(old_name, '/');
2586 new_name = g_malloc0(end - old_name + name->size + 1);
2587 strncat(new_name, old_name, end - old_name);
2588 strncat(new_name + (end - old_name), name->data, name->size);
2589 err = v9fs_co_name_to_path(pdu, NULL, new_name, &new_path);
2595 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2600 * Fixup fid's pointing to the old name to
2601 * start pointing to the new name
2603 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2604 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2605 /* replace the name */
2606 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
2611 put_fid(pdu, dirfidp);
2613 v9fs_path_free(&new_path);
2618 /* Only works with path name based fid */
2619 static void coroutine_fn v9fs_rename(void *opaque)
2627 V9fsPDU *pdu = opaque;
2628 V9fsState *s = pdu->s;
2630 v9fs_string_init(&name);
2631 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2636 if (name_is_illegal(name.data)) {
2641 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2646 fidp = get_fid(pdu, fid);
2651 if (fidp->fid_type != P9_FID_NONE) {
2655 /* if fs driver is not path based, return EOPNOTSUPP */
2656 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2660 v9fs_path_write_lock(s);
2661 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
2662 v9fs_path_unlock(s);
2669 pdu_complete(pdu, err);
2670 v9fs_string_free(&name);
2673 static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2674 V9fsString *old_name,
2676 V9fsString *new_name)
2678 V9fsFidState *tfidp;
2679 V9fsPath oldpath, newpath;
2680 V9fsState *s = pdu->s;
2683 v9fs_path_init(&oldpath);
2684 v9fs_path_init(&newpath);
2685 err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
2689 err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2695 * Fixup fid's pointing to the old name to
2696 * start pointing to the new name
2698 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2699 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
2700 /* replace the name */
2701 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
2705 v9fs_path_free(&oldpath);
2706 v9fs_path_free(&newpath);
2710 static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2711 V9fsString *old_name,
2713 V9fsString *new_name)
2716 V9fsState *s = pdu->s;
2717 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2719 olddirfidp = get_fid(pdu, olddirfid);
2720 if (olddirfidp == NULL) {
2724 if (newdirfid != -1) {
2725 newdirfidp = get_fid(pdu, newdirfid);
2726 if (newdirfidp == NULL) {
2731 newdirfidp = get_fid(pdu, olddirfid);
2734 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2735 &newdirfidp->path, new_name);
2739 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
2740 /* Only for path based fid we need to do the below fixup */
2741 err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2742 &newdirfidp->path, new_name);
2746 put_fid(pdu, olddirfidp);
2749 put_fid(pdu, newdirfidp);
2754 static void coroutine_fn v9fs_renameat(void *opaque)
2758 V9fsPDU *pdu = opaque;
2759 V9fsState *s = pdu->s;
2760 int32_t olddirfid, newdirfid;
2761 V9fsString old_name, new_name;
2763 v9fs_string_init(&old_name);
2764 v9fs_string_init(&new_name);
2765 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2766 &old_name, &newdirfid, &new_name);
2771 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
2776 if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
2777 !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
2782 v9fs_path_write_lock(s);
2783 err = v9fs_complete_renameat(pdu, olddirfid,
2784 &old_name, newdirfid, &new_name);
2785 v9fs_path_unlock(s);
2791 pdu_complete(pdu, err);
2792 v9fs_string_free(&old_name);
2793 v9fs_string_free(&new_name);
2796 static void coroutine_fn v9fs_wstat(void *opaque)
2805 V9fsPDU *pdu = opaque;
2807 v9fs_stat_init(&v9stat);
2808 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
2812 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
2813 v9stat.mode, v9stat.atime, v9stat.mtime);
2815 fidp = get_fid(pdu, fid);
2820 /* do we need to sync the file? */
2821 if (donttouch_stat(&v9stat)) {
2822 err = v9fs_co_fsync(pdu, fidp, 0);
2825 if (v9stat.mode != -1) {
2827 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2831 v9_mode = stat_to_v9mode(&stbuf);
2832 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2833 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2834 /* Attempting to change the type */
2838 err = v9fs_co_chmod(pdu, &fidp->path,
2839 v9mode_to_mode(v9stat.mode,
2840 &v9stat.extension));
2845 if (v9stat.mtime != -1 || v9stat.atime != -1) {
2846 struct timespec times[2];
2847 if (v9stat.atime != -1) {
2848 times[0].tv_sec = v9stat.atime;
2849 times[0].tv_nsec = 0;
2851 times[0].tv_nsec = UTIME_OMIT;
2853 if (v9stat.mtime != -1) {
2854 times[1].tv_sec = v9stat.mtime;
2855 times[1].tv_nsec = 0;
2857 times[1].tv_nsec = UTIME_OMIT;
2859 err = v9fs_co_utimensat(pdu, &fidp->path, times);
2864 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
2865 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
2870 if (v9stat.name.size != 0) {
2871 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
2876 if (v9stat.length != -1) {
2877 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
2886 v9fs_stat_free(&v9stat);
2887 pdu_complete(pdu, err);
2890 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2902 int32_t bsize_factor;
2905 * compute bsize factor based on host file system block size
2908 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2909 if (!bsize_factor) {
2912 f_type = stbuf->f_type;
2913 f_bsize = stbuf->f_bsize;
2914 f_bsize *= bsize_factor;
2916 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2917 * adjust(divide) the number of blocks, free blocks and available
2918 * blocks by bsize factor
2920 f_blocks = stbuf->f_blocks/bsize_factor;
2921 f_bfree = stbuf->f_bfree/bsize_factor;
2922 f_bavail = stbuf->f_bavail/bsize_factor;
2923 f_files = stbuf->f_files;
2924 f_ffree = stbuf->f_ffree;
2925 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
2926 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
2927 f_namelen = stbuf->f_namelen;
2929 return pdu_marshal(pdu, offset, "ddqqqqqqd",
2930 f_type, f_bsize, f_blocks, f_bfree,
2931 f_bavail, f_files, f_ffree,
2932 fsid_val, f_namelen);
2935 static void coroutine_fn v9fs_statfs(void *opaque)
2941 struct statfs stbuf;
2942 V9fsPDU *pdu = opaque;
2943 V9fsState *s = pdu->s;
2945 retval = pdu_unmarshal(pdu, offset, "d", &fid);
2949 fidp = get_fid(pdu, fid);
2954 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
2958 retval = v9fs_fill_statfs(s, pdu, &stbuf);
2966 pdu_complete(pdu, retval);
2969 static void coroutine_fn v9fs_mknod(void *opaque)
2982 V9fsPDU *pdu = opaque;
2984 v9fs_string_init(&name);
2985 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
2986 &major, &minor, &gid);
2990 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
2992 if (name_is_illegal(name.data)) {
2997 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3002 fidp = get_fid(pdu, fid);
3007 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
3008 makedev(major, minor), mode, &stbuf);
3012 stat_to_qid(&stbuf, &qid);
3013 err = pdu_marshal(pdu, offset, "Q", &qid);
3018 trace_v9fs_mknod_return(pdu->tag, pdu->id,
3019 qid.type, qid.version, qid.path);
3023 pdu_complete(pdu, err);
3024 v9fs_string_free(&name);
3028 * Implement posix byte range locking code
3029 * Server side handling of locking code is very simple, because 9p server in
3030 * QEMU can handle only one client. And most of the lock handling
3031 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3032 * do any thing in * qemu 9p server side lock code path.
3033 * So when a TLOCK request comes, always return success
3035 static void coroutine_fn v9fs_lock(void *opaque)
3041 int32_t fid, err = 0;
3042 V9fsPDU *pdu = opaque;
3044 v9fs_string_init(&flock.client_id);
3045 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
3046 &flock.flags, &flock.start, &flock.length,
3047 &flock.proc_id, &flock.client_id);
3051 trace_v9fs_lock(pdu->tag, pdu->id, fid,
3052 flock.type, flock.start, flock.length);
3055 /* We support only block flag now (that too ignored currently) */
3056 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
3060 fidp = get_fid(pdu, fid);
3065 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3069 err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS);
3074 trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS);
3078 pdu_complete(pdu, err);
3079 v9fs_string_free(&flock.client_id);
3083 * When a TGETLOCK request comes, always return success because all lock
3084 * handling is done by client's VFS layer.
3086 static void coroutine_fn v9fs_getlock(void *opaque)
3092 int32_t fid, err = 0;
3093 V9fsPDU *pdu = opaque;
3095 v9fs_string_init(&glock.client_id);
3096 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
3097 &glock.start, &glock.length, &glock.proc_id,
3102 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
3103 glock.type, glock.start, glock.length);
3105 fidp = get_fid(pdu, fid);
3110 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3114 glock.type = P9_LOCK_TYPE_UNLCK;
3115 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
3116 glock.start, glock.length, glock.proc_id,
3122 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
3123 glock.length, glock.proc_id);
3127 pdu_complete(pdu, err);
3128 v9fs_string_free(&glock.client_id);
3131 static void coroutine_fn v9fs_mkdir(void *opaque)
3133 V9fsPDU *pdu = opaque;
3144 v9fs_string_init(&name);
3145 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
3149 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
3151 if (name_is_illegal(name.data)) {
3156 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3161 fidp = get_fid(pdu, fid);
3166 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
3170 stat_to_qid(&stbuf, &qid);
3171 err = pdu_marshal(pdu, offset, "Q", &qid);
3176 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
3177 qid.type, qid.version, qid.path, err);
3181 pdu_complete(pdu, err);
3182 v9fs_string_free(&name);
3185 static void coroutine_fn v9fs_xattrwalk(void *opaque)
3191 int32_t fid, newfid;
3192 V9fsFidState *file_fidp;
3193 V9fsFidState *xattr_fidp = NULL;
3194 V9fsPDU *pdu = opaque;
3195 V9fsState *s = pdu->s;
3197 v9fs_string_init(&name);
3198 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3202 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3204 file_fidp = get_fid(pdu, fid);
3205 if (file_fidp == NULL) {
3209 xattr_fidp = alloc_fid(s, newfid);
3210 if (xattr_fidp == NULL) {
3214 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3215 if (!v9fs_string_size(&name)) {
3217 * listxattr request. Get the size first
3219 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3222 clunk_fid(s, xattr_fidp->fid);
3226 * Read the xattr value
3228 xattr_fidp->fs.xattr.len = size;
3229 xattr_fidp->fid_type = P9_FID_XATTR;
3230 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3232 xattr_fidp->fs.xattr.value = g_malloc(size);
3233 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3234 xattr_fidp->fs.xattr.value,
3235 xattr_fidp->fs.xattr.len);
3237 clunk_fid(s, xattr_fidp->fid);
3241 err = pdu_marshal(pdu, offset, "q", size);
3248 * specific xattr fid. We check for xattr
3249 * presence also collect the xattr size
3251 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3255 clunk_fid(s, xattr_fidp->fid);
3259 * Read the xattr value
3261 xattr_fidp->fs.xattr.len = size;
3262 xattr_fidp->fid_type = P9_FID_XATTR;
3263 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3265 xattr_fidp->fs.xattr.value = g_malloc(size);
3266 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3267 &name, xattr_fidp->fs.xattr.value,
3268 xattr_fidp->fs.xattr.len);
3270 clunk_fid(s, xattr_fidp->fid);
3274 err = pdu_marshal(pdu, offset, "q", size);
3280 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3282 put_fid(pdu, file_fidp);
3284 put_fid(pdu, xattr_fidp);
3287 pdu_complete(pdu, err);
3288 v9fs_string_free(&name);
3291 static void coroutine_fn v9fs_xattrcreate(void *opaque)
3299 V9fsFidState *file_fidp;
3300 V9fsFidState *xattr_fidp;
3301 V9fsPDU *pdu = opaque;
3303 v9fs_string_init(&name);
3304 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3308 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3310 if (size > XATTR_SIZE_MAX) {
3315 file_fidp = get_fid(pdu, fid);
3316 if (file_fidp == NULL) {
3320 if (file_fidp->fid_type != P9_FID_NONE) {
3325 /* Make the file fid point to xattr */
3326 xattr_fidp = file_fidp;
3327 xattr_fidp->fid_type = P9_FID_XATTR;
3328 xattr_fidp->fs.xattr.copied_len = 0;
3329 xattr_fidp->fs.xattr.xattrwalk_fid = false;
3330 xattr_fidp->fs.xattr.len = size;
3331 xattr_fidp->fs.xattr.flags = flags;
3332 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3333 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3334 xattr_fidp->fs.xattr.value = g_malloc0(size);
3337 put_fid(pdu, file_fidp);
3339 pdu_complete(pdu, err);
3340 v9fs_string_free(&name);
3343 static void coroutine_fn v9fs_readlink(void *opaque)
3345 V9fsPDU *pdu = opaque;
3352 err = pdu_unmarshal(pdu, offset, "d", &fid);
3356 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
3357 fidp = get_fid(pdu, fid);
3363 v9fs_string_init(&target);
3364 err = v9fs_co_readlink(pdu, &fidp->path, &target);
3368 err = pdu_marshal(pdu, offset, "s", &target);
3370 v9fs_string_free(&target);
3374 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
3375 v9fs_string_free(&target);
3379 pdu_complete(pdu, err);
3382 static CoroutineEntry *pdu_co_handlers[] = {
3383 [P9_TREADDIR] = v9fs_readdir,
3384 [P9_TSTATFS] = v9fs_statfs,
3385 [P9_TGETATTR] = v9fs_getattr,
3386 [P9_TSETATTR] = v9fs_setattr,
3387 [P9_TXATTRWALK] = v9fs_xattrwalk,
3388 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3389 [P9_TMKNOD] = v9fs_mknod,
3390 [P9_TRENAME] = v9fs_rename,
3391 [P9_TLOCK] = v9fs_lock,
3392 [P9_TGETLOCK] = v9fs_getlock,
3393 [P9_TRENAMEAT] = v9fs_renameat,
3394 [P9_TREADLINK] = v9fs_readlink,
3395 [P9_TUNLINKAT] = v9fs_unlinkat,
3396 [P9_TMKDIR] = v9fs_mkdir,
3397 [P9_TVERSION] = v9fs_version,
3398 [P9_TLOPEN] = v9fs_open,
3399 [P9_TATTACH] = v9fs_attach,
3400 [P9_TSTAT] = v9fs_stat,
3401 [P9_TWALK] = v9fs_walk,
3402 [P9_TCLUNK] = v9fs_clunk,
3403 [P9_TFSYNC] = v9fs_fsync,
3404 [P9_TOPEN] = v9fs_open,
3405 [P9_TREAD] = v9fs_read,
3407 [P9_TAUTH] = v9fs_auth,
3409 [P9_TFLUSH] = v9fs_flush,
3410 [P9_TLINK] = v9fs_link,
3411 [P9_TSYMLINK] = v9fs_symlink,
3412 [P9_TCREATE] = v9fs_create,
3413 [P9_TLCREATE] = v9fs_lcreate,
3414 [P9_TWRITE] = v9fs_write,
3415 [P9_TWSTAT] = v9fs_wstat,
3416 [P9_TREMOVE] = v9fs_remove,
3419 static void coroutine_fn v9fs_op_not_supp(void *opaque)
3421 V9fsPDU *pdu = opaque;
3422 pdu_complete(pdu, -EOPNOTSUPP);
3425 static void coroutine_fn v9fs_fs_ro(void *opaque)
3427 V9fsPDU *pdu = opaque;
3428 pdu_complete(pdu, -EROFS);
3431 static inline bool is_read_only_op(V9fsPDU *pdu)
3458 void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr)
3461 CoroutineEntry *handler;
3462 V9fsState *s = pdu->s;
3464 pdu->size = le32_to_cpu(hdr->size_le);
3466 pdu->tag = le16_to_cpu(hdr->tag_le);
3468 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3469 (pdu_co_handlers[pdu->id] == NULL)) {
3470 handler = v9fs_op_not_supp;
3472 handler = pdu_co_handlers[pdu->id];
3475 if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3476 handler = v9fs_fs_ro;
3479 qemu_co_queue_init(&pdu->complete);
3480 co = qemu_coroutine_create(handler, pdu);
3481 qemu_coroutine_enter(co);
3484 /* Returns 0 on success, 1 on failure. */
3485 int v9fs_device_realize_common(V9fsState *s, Error **errp)
3493 /* initialize pdu allocator */
3494 QLIST_INIT(&s->free_list);
3495 QLIST_INIT(&s->active_list);
3496 for (i = 0; i < MAX_REQ; i++) {
3497 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
3502 v9fs_path_init(&path);
3504 fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
3507 /* We don't have a fsdev identified by fsdev_id */
3508 error_setg(errp, "9pfs device couldn't find fsdev with the "
3510 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
3514 if (!s->fsconf.tag) {
3515 /* we haven't specified a mount_tag */
3516 error_setg(errp, "fsdev with id %s needs mount_tag arguments",
3517 s->fsconf.fsdev_id);
3521 s->ctx.export_flags = fse->export_flags;
3522 s->ctx.fs_root = g_strdup(fse->path);
3523 s->ctx.exops.get_st_gen = NULL;
3524 len = strlen(s->fsconf.tag);
3525 if (len > MAX_TAG_LEN - 1) {
3526 error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
3527 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
3531 s->tag = g_strdup(s->fsconf.tag);
3536 s->ctx.fmode = fse->fmode;
3537 s->ctx.dmode = fse->dmode;
3540 qemu_co_rwlock_init(&s->rename_lock);
3542 if (s->ops->init(&s->ctx) < 0) {
3543 error_setg(errp, "9pfs Failed to initialize fs-driver with id:%s"
3544 " and export path:%s", s->fsconf.fsdev_id, s->ctx.fs_root);
3549 * Check details of export path, We need to use fs driver
3550 * call back to do that. Since we are in the init path, we don't
3551 * use co-routines here.
3553 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
3555 "error in converting name to path %s", strerror(errno));
3558 if (s->ops->lstat(&s->ctx, &path, &stat)) {
3559 error_setg(errp, "share path %s does not exist", fse->path);
3561 } else if (!S_ISDIR(stat.st_mode)) {
3562 error_setg(errp, "share path %s is not a directory", fse->path);
3566 s->ctx.fst = &fse->fst;
3567 fsdev_throttle_init(s->ctx.fst);
3569 v9fs_path_free(&path);
3574 if (s->ops && s->ops->cleanup && s->ctx.private) {
3575 s->ops->cleanup(&s->ctx);
3578 g_free(s->ctx.fs_root);
3579 v9fs_path_free(&path);
3584 void v9fs_device_unrealize_common(V9fsState *s, Error **errp)
3586 if (s->ops->cleanup) {
3587 s->ops->cleanup(&s->ctx);
3589 fsdev_throttle_cleanup(s->ctx.fst);
3591 g_free(s->ctx.fs_root);
3594 typedef struct VirtfsCoResetData {
3597 } VirtfsCoResetData;
3599 static void coroutine_fn virtfs_co_reset(void *opaque)
3601 VirtfsCoResetData *data = opaque;
3603 virtfs_reset(&data->pdu);
3607 void v9fs_reset(V9fsState *s)
3609 VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
3612 while (!QLIST_EMPTY(&s->active_list)) {
3613 aio_poll(qemu_get_aio_context(), true);
3616 co = qemu_coroutine_create(virtfs_co_reset, &data);
3617 qemu_coroutine_enter(co);
3619 while (!data.done) {
3620 aio_poll(qemu_get_aio_context(), true);
3624 static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
3627 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
3628 error_report("Failed to get the resource limit");
3631 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
3632 open_fd_rc = rlim.rlim_cur/2;