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 "hw/virtio.h"
16 #include "qemu_socket.h"
17 #include "hw/virtio-pci.h"
18 #include "virtio-9p.h"
19 #include "fsdev/qemu-fsdev.h"
20 #include "virtio-9p-debug.h"
21 #include "virtio-9p-xattr.h"
22 #include "virtio-9p-coth.h"
38 static int omode_to_uflags(int8_t mode)
72 void cred_init(FsCred *credp)
80 static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf)
82 return s->ops->lstat(&s->ctx, path->data, stbuf);
85 static DIR *v9fs_do_opendir(V9fsState *s, V9fsString *path)
87 return s->ops->opendir(&s->ctx, path->data);
90 static void v9fs_do_rewinddir(V9fsState *s, DIR *dir)
92 return s->ops->rewinddir(&s->ctx, dir);
95 static off_t v9fs_do_telldir(V9fsState *s, DIR *dir)
97 return s->ops->telldir(&s->ctx, dir);
100 static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off)
102 return s->ops->seekdir(&s->ctx, dir, off);
105 static int v9fs_do_preadv(V9fsState *s, int fd, const struct iovec *iov,
106 int iovcnt, int64_t offset)
108 return s->ops->preadv(&s->ctx, fd, iov, iovcnt, offset);
111 static int v9fs_do_pwritev(V9fsState *s, int fd, const struct iovec *iov,
112 int iovcnt, int64_t offset)
114 return s->ops->pwritev(&s->ctx, fd, iov, iovcnt, offset);
117 static int v9fs_do_chmod(V9fsState *s, V9fsString *path, mode_t mode)
122 return s->ops->chmod(&s->ctx, path->data, &cred);
125 static int v9fs_do_mknod(V9fsState *s, char *name,
126 mode_t mode, dev_t dev, uid_t uid, gid_t gid)
134 return s->ops->mknod(&s->ctx, name, &cred);
137 static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
139 return s->ops->fstat(&s->ctx, fd, stbuf);
142 static int v9fs_do_symlink(V9fsState *s, V9fsFidState *fidp,
143 const char *oldpath, const char *newpath, gid_t gid)
147 cred.fc_uid = fidp->uid;
151 return s->ops->symlink(&s->ctx, oldpath, newpath, &cred);
154 static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
156 return s->ops->link(&s->ctx, oldpath->data, newpath->data);
159 static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
161 return s->ops->truncate(&s->ctx, path->data, size);
164 static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
171 return s->ops->chown(&s->ctx, path->data, &cred);
174 static int v9fs_do_utimensat(V9fsState *s, V9fsString *path,
175 const struct timespec times[2])
177 return s->ops->utimensat(&s->ctx, path->data, times);
180 static int v9fs_do_fsync(V9fsState *s, int fd, int datasync)
182 return s->ops->fsync(&s->ctx, fd, datasync);
185 static void v9fs_string_init(V9fsString *str)
191 static void v9fs_string_free(V9fsString *str)
198 static void v9fs_string_null(V9fsString *str)
200 v9fs_string_free(str);
203 static int number_to_string(void *arg, char type)
205 unsigned int ret = 0;
209 unsigned int num = *(unsigned int *)arg;
218 unsigned long num = *(unsigned long *)arg;
226 printf("Number_to_string: Unknown number format\n");
233 static int GCC_FMT_ATTR(2, 0)
234 v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
237 char *iter = (char *)fmt;
241 unsigned int arg_uint;
242 unsigned long arg_ulong;
244 /* Find the number of %'s that denotes an argument */
245 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
250 len = strlen(fmt) - 2*nr_args;
260 /* Now parse the format string */
261 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
265 arg_uint = va_arg(ap2, unsigned int);
266 len += number_to_string((void *)&arg_uint, 'u');
269 if (*++iter == 'u') {
270 arg_ulong = va_arg(ap2, unsigned long);
271 len += number_to_string((void *)&arg_ulong, 'U');
277 arg_char_ptr = va_arg(ap2, char *);
278 len += strlen(arg_char_ptr);
285 "v9fs_string_alloc_printf:Incorrect format %c", *iter);
292 *strp = g_malloc((len + 1) * sizeof(**strp));
294 return vsprintf(*strp, fmt, ap);
297 static void GCC_FMT_ATTR(2, 3)
298 v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
303 v9fs_string_free(str);
306 err = v9fs_string_alloc_printf(&str->data, fmt, ap);
313 static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
315 v9fs_string_free(lhs);
316 v9fs_string_sprintf(lhs, "%s", rhs->data);
320 * Return TRUE if s1 is an ancestor of s2.
322 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
323 * As a special case, We treat s1 as ancestor of s2 if they are same!
325 static int v9fs_path_is_ancestor(V9fsString *s1, V9fsString *s2)
327 if (!strncmp(s1->data, s2->data, s1->size)) {
328 if (s2->data[s1->size] == '\0' || s2->data[s1->size] == '/') {
335 static size_t v9fs_string_size(V9fsString *str)
340 static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
344 for (f = s->fid_list; f; f = f->next) {
353 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
357 f = lookup_fid(s, fid);
362 f = g_malloc0(sizeof(V9fsFidState));
365 f->fid_type = P9_FID_NONE;
367 f->next = s->fid_list;
373 static int v9fs_xattr_fid_clunk(V9fsState *s, V9fsFidState *fidp)
377 if (fidp->fs.xattr.copied_len == -1) {
378 /* getxattr/listxattr fid */
382 * if this is fid for setxattr. clunk should
383 * result in setxattr localcall
385 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
386 /* clunk after partial write */
390 if (fidp->fs.xattr.len) {
391 retval = v9fs_co_lsetxattr(s, &fidp->path, &fidp->fs.xattr.name,
392 fidp->fs.xattr.value,
394 fidp->fs.xattr.flags);
396 retval = v9fs_co_lremovexattr(s, &fidp->path, &fidp->fs.xattr.name);
399 v9fs_string_free(&fidp->fs.xattr.name);
401 if (fidp->fs.xattr.value) {
402 g_free(fidp->fs.xattr.value);
407 static int free_fid(V9fsState *s, int32_t fid)
410 V9fsFidState **fidpp, *fidp;
412 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
413 if ((*fidpp)->fid == fid) {
418 if (*fidpp == NULL) {
425 if (fidp->fid_type == P9_FID_FILE) {
426 retval = v9fs_co_close(s, fidp);
427 } else if (fidp->fid_type == P9_FID_DIR) {
428 retval = v9fs_co_closedir(s, fidp);
429 } else if (fidp->fid_type == P9_FID_XATTR) {
430 retval = v9fs_xattr_fid_clunk(s, fidp);
432 v9fs_string_free(&fidp->path);
437 #define P9_QID_TYPE_DIR 0x80
438 #define P9_QID_TYPE_SYMLINK 0x02
440 #define P9_STAT_MODE_DIR 0x80000000
441 #define P9_STAT_MODE_APPEND 0x40000000
442 #define P9_STAT_MODE_EXCL 0x20000000
443 #define P9_STAT_MODE_MOUNT 0x10000000
444 #define P9_STAT_MODE_AUTH 0x08000000
445 #define P9_STAT_MODE_TMP 0x04000000
446 #define P9_STAT_MODE_SYMLINK 0x02000000
447 #define P9_STAT_MODE_LINK 0x01000000
448 #define P9_STAT_MODE_DEVICE 0x00800000
449 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
450 #define P9_STAT_MODE_SOCKET 0x00100000
451 #define P9_STAT_MODE_SETUID 0x00080000
452 #define P9_STAT_MODE_SETGID 0x00040000
453 #define P9_STAT_MODE_SETVTX 0x00010000
455 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
456 P9_STAT_MODE_SYMLINK | \
457 P9_STAT_MODE_LINK | \
458 P9_STAT_MODE_DEVICE | \
459 P9_STAT_MODE_NAMED_PIPE | \
462 /* This is the algorithm from ufs in spfs */
463 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
467 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
468 memcpy(&qidp->path, &stbuf->st_ino, size);
469 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
471 if (S_ISDIR(stbuf->st_mode)) {
472 qidp->type |= P9_QID_TYPE_DIR;
474 if (S_ISLNK(stbuf->st_mode)) {
475 qidp->type |= P9_QID_TYPE_SYMLINK;
479 static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
484 err = v9fs_do_lstat(s, &fidp->path, &stbuf);
489 stat_to_qid(&stbuf, qidp);
493 static V9fsPDU *alloc_pdu(V9fsState *s)
497 if (!QLIST_EMPTY(&s->free_list)) {
498 pdu = QLIST_FIRST(&s->free_list);
499 QLIST_REMOVE(pdu, next);
504 static void free_pdu(V9fsState *s, V9fsPDU *pdu)
510 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
514 size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
515 size_t offset, size_t size, int pack)
520 for (i = 0; size && i < sg_count; i++) {
522 if (offset >= sg[i].iov_len) {
524 offset -= sg[i].iov_len;
527 len = MIN(sg[i].iov_len - offset, size);
529 memcpy(sg[i].iov_base + offset, addr, len);
531 memcpy(addr, sg[i].iov_base + offset, len);
546 static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
548 return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
552 static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
555 return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
559 static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
563 struct iovec *src_sg;
567 src_sg = pdu->elem.in_sg;
568 num = pdu->elem.in_num;
570 src_sg = pdu->elem.out_sg;
571 num = pdu->elem.out_num;
575 for (i = 0; i < num; i++) {
577 sg[j].iov_base = src_sg[i].iov_base;
578 sg[j].iov_len = src_sg[i].iov_len;
580 } else if (offset < (src_sg[i].iov_len + pos)) {
581 sg[j].iov_base = src_sg[i].iov_base;
582 sg[j].iov_len = src_sg[i].iov_len;
583 sg[j].iov_base += (offset - pos);
584 sg[j].iov_len -= (offset - pos);
587 pos += src_sg[i].iov_len;
593 static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
595 size_t old_offset = offset;
600 for (i = 0; fmt[i]; i++) {
603 uint8_t *valp = va_arg(ap, uint8_t *);
604 offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
609 valp = va_arg(ap, uint16_t *);
610 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
611 *valp = le16_to_cpu(val);
616 valp = va_arg(ap, uint32_t *);
617 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
618 *valp = le32_to_cpu(val);
623 valp = va_arg(ap, uint64_t *);
624 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
625 *valp = le64_to_cpu(val);
629 struct iovec *iov = va_arg(ap, struct iovec *);
630 int *iovcnt = va_arg(ap, int *);
631 *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
635 V9fsString *str = va_arg(ap, V9fsString *);
636 offset += pdu_unmarshal(pdu, offset, "w", &str->size);
637 /* FIXME: sanity check str->size */
638 str->data = g_malloc(str->size + 1);
639 offset += pdu_unpack(str->data, pdu, offset, str->size);
640 str->data[str->size] = 0;
644 V9fsQID *qidp = va_arg(ap, V9fsQID *);
645 offset += pdu_unmarshal(pdu, offset, "bdq",
646 &qidp->type, &qidp->version, &qidp->path);
650 V9fsStat *statp = va_arg(ap, V9fsStat *);
651 offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
652 &statp->size, &statp->type, &statp->dev,
653 &statp->qid, &statp->mode, &statp->atime,
654 &statp->mtime, &statp->length,
655 &statp->name, &statp->uid, &statp->gid,
656 &statp->muid, &statp->extension,
657 &statp->n_uid, &statp->n_gid,
662 V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
663 offset += pdu_unmarshal(pdu, offset, "ddddqqqqq",
664 &iattr->valid, &iattr->mode,
665 &iattr->uid, &iattr->gid, &iattr->size,
666 &iattr->atime_sec, &iattr->atime_nsec,
667 &iattr->mtime_sec, &iattr->mtime_nsec);
677 return offset - old_offset;
680 static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
682 size_t old_offset = offset;
687 for (i = 0; fmt[i]; i++) {
690 uint8_t val = va_arg(ap, int);
691 offset += pdu_pack(pdu, offset, &val, sizeof(val));
696 cpu_to_le16w(&val, va_arg(ap, int));
697 offset += pdu_pack(pdu, offset, &val, sizeof(val));
702 cpu_to_le32w(&val, va_arg(ap, uint32_t));
703 offset += pdu_pack(pdu, offset, &val, sizeof(val));
708 cpu_to_le64w(&val, va_arg(ap, uint64_t));
709 offset += pdu_pack(pdu, offset, &val, sizeof(val));
713 struct iovec *iov = va_arg(ap, struct iovec *);
714 int *iovcnt = va_arg(ap, int *);
715 *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
719 V9fsString *str = va_arg(ap, V9fsString *);
720 offset += pdu_marshal(pdu, offset, "w", str->size);
721 offset += pdu_pack(pdu, offset, str->data, str->size);
725 V9fsQID *qidp = va_arg(ap, V9fsQID *);
726 offset += pdu_marshal(pdu, offset, "bdq",
727 qidp->type, qidp->version, qidp->path);
731 V9fsStat *statp = va_arg(ap, V9fsStat *);
732 offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
733 statp->size, statp->type, statp->dev,
734 &statp->qid, statp->mode, statp->atime,
735 statp->mtime, statp->length, &statp->name,
736 &statp->uid, &statp->gid, &statp->muid,
737 &statp->extension, statp->n_uid,
738 statp->n_gid, statp->n_muid);
742 V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
743 offset += pdu_marshal(pdu, offset, "qQdddqqqqqqqqqqqqqqq",
744 statp->st_result_mask,
745 &statp->qid, statp->st_mode,
746 statp->st_uid, statp->st_gid,
747 statp->st_nlink, statp->st_rdev,
748 statp->st_size, statp->st_blksize, statp->st_blocks,
749 statp->st_atime_sec, statp->st_atime_nsec,
750 statp->st_mtime_sec, statp->st_mtime_nsec,
751 statp->st_ctime_sec, statp->st_ctime_nsec,
752 statp->st_btime_sec, statp->st_btime_nsec,
753 statp->st_gen, statp->st_data_version);
762 return offset - old_offset;
765 static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
767 int8_t id = pdu->id + 1; /* Response */
773 if (s->proto_version != V9FS_PROTO_2000L) {
776 str.data = strerror(err);
777 str.size = strlen(str.data);
779 len += pdu_marshal(pdu, len, "s", &str);
783 len += pdu_marshal(pdu, len, "d", err);
785 if (s->proto_version == V9FS_PROTO_2000L) {
790 /* fill out the header */
791 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
793 /* keep these in sync */
797 /* push onto queue and notify */
798 virtqueue_push(s->vq, &pdu->elem, len);
800 /* FIXME: we should batch these completions */
801 virtio_notify(&s->vdev, s->vq);
806 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
811 if (mode & P9_STAT_MODE_DIR) {
815 if (mode & P9_STAT_MODE_SYMLINK) {
818 if (mode & P9_STAT_MODE_SOCKET) {
821 if (mode & P9_STAT_MODE_NAMED_PIPE) {
824 if (mode & P9_STAT_MODE_DEVICE) {
825 if (extension && extension->data[0] == 'c') {
836 if (mode & P9_STAT_MODE_SETUID) {
839 if (mode & P9_STAT_MODE_SETGID) {
842 if (mode & P9_STAT_MODE_SETVTX) {
849 static int donttouch_stat(V9fsStat *stat)
851 if (stat->type == -1 &&
853 stat->qid.type == -1 &&
854 stat->qid.version == -1 &&
855 stat->qid.path == -1 &&
859 stat->length == -1 &&
866 stat->n_muid == -1) {
873 static void v9fs_stat_free(V9fsStat *stat)
875 v9fs_string_free(&stat->name);
876 v9fs_string_free(&stat->uid);
877 v9fs_string_free(&stat->gid);
878 v9fs_string_free(&stat->muid);
879 v9fs_string_free(&stat->extension);
882 static uint32_t stat_to_v9mode(const struct stat *stbuf)
886 mode = stbuf->st_mode & 0777;
887 if (S_ISDIR(stbuf->st_mode)) {
888 mode |= P9_STAT_MODE_DIR;
891 if (S_ISLNK(stbuf->st_mode)) {
892 mode |= P9_STAT_MODE_SYMLINK;
895 if (S_ISSOCK(stbuf->st_mode)) {
896 mode |= P9_STAT_MODE_SOCKET;
899 if (S_ISFIFO(stbuf->st_mode)) {
900 mode |= P9_STAT_MODE_NAMED_PIPE;
903 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
904 mode |= P9_STAT_MODE_DEVICE;
907 if (stbuf->st_mode & S_ISUID) {
908 mode |= P9_STAT_MODE_SETUID;
911 if (stbuf->st_mode & S_ISGID) {
912 mode |= P9_STAT_MODE_SETGID;
915 if (stbuf->st_mode & S_ISVTX) {
916 mode |= P9_STAT_MODE_SETVTX;
922 static int stat_to_v9stat(V9fsState *s, V9fsString *name,
923 const struct stat *stbuf,
929 memset(v9stat, 0, sizeof(*v9stat));
931 stat_to_qid(stbuf, &v9stat->qid);
932 v9stat->mode = stat_to_v9mode(stbuf);
933 v9stat->atime = stbuf->st_atime;
934 v9stat->mtime = stbuf->st_mtime;
935 v9stat->length = stbuf->st_size;
937 v9fs_string_null(&v9stat->uid);
938 v9fs_string_null(&v9stat->gid);
939 v9fs_string_null(&v9stat->muid);
941 v9stat->n_uid = stbuf->st_uid;
942 v9stat->n_gid = stbuf->st_gid;
945 v9fs_string_null(&v9stat->extension);
947 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
948 err = v9fs_co_readlink(s, name, &v9stat->extension);
952 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
953 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
954 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
955 major(stbuf->st_rdev), minor(stbuf->st_rdev));
956 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
957 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
958 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
961 str = strrchr(name->data, '/');
968 v9fs_string_sprintf(&v9stat->name, "%s", str);
971 v9fs_string_size(&v9stat->name) +
972 v9fs_string_size(&v9stat->uid) +
973 v9fs_string_size(&v9stat->gid) +
974 v9fs_string_size(&v9stat->muid) +
975 v9fs_string_size(&v9stat->extension);
979 #define P9_STATS_MODE 0x00000001ULL
980 #define P9_STATS_NLINK 0x00000002ULL
981 #define P9_STATS_UID 0x00000004ULL
982 #define P9_STATS_GID 0x00000008ULL
983 #define P9_STATS_RDEV 0x00000010ULL
984 #define P9_STATS_ATIME 0x00000020ULL
985 #define P9_STATS_MTIME 0x00000040ULL
986 #define P9_STATS_CTIME 0x00000080ULL
987 #define P9_STATS_INO 0x00000100ULL
988 #define P9_STATS_SIZE 0x00000200ULL
989 #define P9_STATS_BLOCKS 0x00000400ULL
991 #define P9_STATS_BTIME 0x00000800ULL
992 #define P9_STATS_GEN 0x00001000ULL
993 #define P9_STATS_DATA_VERSION 0x00002000ULL
995 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
996 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
999 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
1000 V9fsStatDotl *v9lstat)
1002 memset(v9lstat, 0, sizeof(*v9lstat));
1004 v9lstat->st_mode = stbuf->st_mode;
1005 v9lstat->st_nlink = stbuf->st_nlink;
1006 v9lstat->st_uid = stbuf->st_uid;
1007 v9lstat->st_gid = stbuf->st_gid;
1008 v9lstat->st_rdev = stbuf->st_rdev;
1009 v9lstat->st_size = stbuf->st_size;
1010 v9lstat->st_blksize = stbuf->st_blksize;
1011 v9lstat->st_blocks = stbuf->st_blocks;
1012 v9lstat->st_atime_sec = stbuf->st_atime;
1013 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1014 v9lstat->st_mtime_sec = stbuf->st_mtime;
1015 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
1016 v9lstat->st_ctime_sec = stbuf->st_ctime;
1017 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1018 /* Currently we only support BASIC fields in stat */
1019 v9lstat->st_result_mask = P9_STATS_BASIC;
1021 stat_to_qid(stbuf, &v9lstat->qid);
1024 static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
1026 while (len && *iovcnt) {
1027 if (len < sg->iov_len) {
1029 sg->iov_base += len;
1041 static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
1046 for (i = 0; i < *cnt; i++) {
1047 if ((total + sg[i].iov_len) > cap) {
1048 sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
1052 total += sg[i].iov_len;
1060 static void print_sg(struct iovec *sg, int cnt)
1064 printf("sg[%d]: {", cnt);
1065 for (i = 0; i < cnt; i++) {
1069 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1074 static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
1077 v9fs_string_init(&str);
1078 v9fs_string_copy(&str, dst);
1079 v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
1080 v9fs_string_free(&str);
1083 static void v9fs_version(void *opaque)
1085 V9fsPDU *pdu = opaque;
1086 V9fsState *s = pdu->s;
1090 pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1092 if (!strcmp(version.data, "9P2000.u")) {
1093 s->proto_version = V9FS_PROTO_2000U;
1094 } else if (!strcmp(version.data, "9P2000.L")) {
1095 s->proto_version = V9FS_PROTO_2000L;
1097 v9fs_string_sprintf(&version, "unknown");
1100 offset += pdu_marshal(pdu, offset, "ds", s->msize, &version);
1101 complete_pdu(s, pdu, offset);
1103 v9fs_string_free(&version);
1107 static void v9fs_attach(void *opaque)
1109 V9fsPDU *pdu = opaque;
1110 V9fsState *s = pdu->s;
1111 int32_t fid, afid, n_uname;
1112 V9fsString uname, aname;
1118 pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1120 fidp = alloc_fid(s, fid);
1126 fidp->uid = n_uname;
1128 v9fs_string_sprintf(&fidp->path, "%s", "/");
1129 err = fid_to_qid(s, fidp, &qid);
1136 offset += pdu_marshal(pdu, offset, "Q", &qid);
1140 complete_pdu(s, pdu, err);
1141 v9fs_string_free(&uname);
1142 v9fs_string_free(&aname);
1145 static void v9fs_stat(void *opaque)
1153 V9fsPDU *pdu = opaque;
1154 V9fsState *s = pdu->s;
1156 pdu_unmarshal(pdu, offset, "d", &fid);
1157 fidp = lookup_fid(s, fid);
1162 err = v9fs_co_lstat(s, &fidp->path, &stbuf);
1166 err = stat_to_v9stat(s, &fidp->path, &stbuf, &v9stat);
1170 offset += pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1172 v9fs_stat_free(&v9stat);
1174 complete_pdu(s, pdu, err);
1177 static void v9fs_getattr(void *opaque)
1184 uint64_t request_mask;
1185 V9fsStatDotl v9stat_dotl;
1186 V9fsPDU *pdu = opaque;
1187 V9fsState *s = pdu->s;
1189 pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1191 fidp = lookup_fid(s, fid);
1197 * Currently we only support BASIC fields in stat, so there is no
1198 * need to look at request_mask.
1200 retval = v9fs_co_lstat(s, &fidp->path, &stbuf);
1204 stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
1206 retval += pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1208 complete_pdu(s, pdu, retval);
1211 /* From Linux kernel code */
1212 #define ATTR_MODE (1 << 0)
1213 #define ATTR_UID (1 << 1)
1214 #define ATTR_GID (1 << 2)
1215 #define ATTR_SIZE (1 << 3)
1216 #define ATTR_ATIME (1 << 4)
1217 #define ATTR_MTIME (1 << 5)
1218 #define ATTR_CTIME (1 << 6)
1219 #define ATTR_MASK 127
1220 #define ATTR_ATIME_SET (1 << 7)
1221 #define ATTR_MTIME_SET (1 << 8)
1223 static void v9fs_setattr(void *opaque)
1230 V9fsPDU *pdu = opaque;
1231 V9fsState *s = pdu->s;
1233 pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1235 fidp = lookup_fid(s, fid);
1240 if (v9iattr.valid & ATTR_MODE) {
1241 err = v9fs_co_chmod(s, &fidp->path, v9iattr.mode);
1246 if (v9iattr.valid & (ATTR_ATIME | ATTR_MTIME)) {
1247 struct timespec times[2];
1248 if (v9iattr.valid & ATTR_ATIME) {
1249 if (v9iattr.valid & ATTR_ATIME_SET) {
1250 times[0].tv_sec = v9iattr.atime_sec;
1251 times[0].tv_nsec = v9iattr.atime_nsec;
1253 times[0].tv_nsec = UTIME_NOW;
1256 times[0].tv_nsec = UTIME_OMIT;
1258 if (v9iattr.valid & ATTR_MTIME) {
1259 if (v9iattr.valid & ATTR_MTIME_SET) {
1260 times[1].tv_sec = v9iattr.mtime_sec;
1261 times[1].tv_nsec = v9iattr.mtime_nsec;
1263 times[1].tv_nsec = UTIME_NOW;
1266 times[1].tv_nsec = UTIME_OMIT;
1268 err = v9fs_co_utimensat(s, &fidp->path, times);
1274 * If the only valid entry in iattr is ctime we can call
1275 * chown(-1,-1) to update the ctime of the file
1277 if ((v9iattr.valid & (ATTR_UID | ATTR_GID)) ||
1278 ((v9iattr.valid & ATTR_CTIME)
1279 && !((v9iattr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
1280 if (!(v9iattr.valid & ATTR_UID)) {
1283 if (!(v9iattr.valid & ATTR_GID)) {
1286 err = v9fs_co_chown(s, &fidp->path, v9iattr.uid,
1292 if (v9iattr.valid & (ATTR_SIZE)) {
1293 err = v9fs_co_truncate(s, &fidp->path, v9iattr.size);
1300 complete_pdu(s, pdu, err);
1303 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1307 offset += pdu_marshal(pdu, offset, "w", nwnames);
1308 for (i = 0; i < nwnames; i++) {
1309 offset += pdu_marshal(pdu, offset, "Q", &qids[i]);
1314 static void v9fs_walk(void *opaque)
1317 V9fsQID *qids = NULL;
1323 int32_t fid, newfid;
1324 V9fsString *wnames = NULL;
1326 V9fsFidState *newfidp;
1327 V9fsPDU *pdu = opaque;
1328 V9fsState *s = pdu->s;
1330 offset += pdu_unmarshal(pdu, offset, "ddw", &fid,
1333 if (nwnames && nwnames <= P9_MAXWELEM) {
1334 wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
1335 qids = g_malloc0(sizeof(qids[0]) * nwnames);
1336 for (i = 0; i < nwnames; i++) {
1337 offset += pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1340 } else if (nwnames > P9_MAXWELEM) {
1344 fidp = lookup_fid(s, fid);
1349 if (fid == newfid) {
1350 BUG_ON(fidp->fid_type != P9_FID_NONE);
1351 v9fs_string_init(&path);
1352 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1353 v9fs_string_sprintf(&path, "%s/%s",
1354 fidp->path.data, wnames[name_idx].data);
1355 v9fs_string_copy(&fidp->path, &path);
1357 err = v9fs_co_lstat(s, &fidp->path, &stbuf);
1359 v9fs_string_free(&path);
1362 stat_to_qid(&stbuf, &qids[name_idx]);
1364 v9fs_string_free(&path);
1366 newfidp = alloc_fid(s, newfid);
1367 if (newfidp == NULL) {
1371 newfidp->uid = fidp->uid;
1372 v9fs_string_init(&path);
1373 v9fs_string_copy(&newfidp->path, &fidp->path);
1374 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1375 v9fs_string_sprintf(&path, "%s/%s", newfidp->path.data,
1376 wnames[name_idx].data);
1377 v9fs_string_copy(&newfidp->path, &path);
1378 err = v9fs_co_lstat(s, &newfidp->path, &stbuf);
1380 free_fid(s, newfidp->fid);
1381 v9fs_string_free(&path);
1384 stat_to_qid(&stbuf, &qids[name_idx]);
1386 v9fs_string_free(&path);
1388 err = v9fs_walk_marshal(pdu, nwnames, qids);
1390 complete_pdu(s, 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 get_iounit(V9fsState *s, V9fsString *name)
1402 struct statfs stbuf;
1406 * iounit should be multiples of f_bsize (host filesystem block size
1407 * and as well as less than (client msize - P9_IOHDRSZ))
1409 if (!v9fs_co_statfs(s, name, &stbuf)) {
1410 iounit = stbuf.f_bsize;
1411 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1414 iounit = s->msize - P9_IOHDRSZ;
1419 static void v9fs_open(void *opaque)
1430 V9fsPDU *pdu = opaque;
1431 V9fsState *s = pdu->s;
1433 if (s->proto_version == V9FS_PROTO_2000L) {
1434 pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1436 pdu_unmarshal(pdu, offset, "db", &fid, &mode);
1438 fidp = lookup_fid(s, fid);
1443 BUG_ON(fidp->fid_type != P9_FID_NONE);
1445 err = v9fs_co_lstat(s, &fidp->path, &stbuf);
1449 stat_to_qid(&stbuf, &qid);
1450 if (S_ISDIR(stbuf.st_mode)) {
1451 err = v9fs_co_opendir(s, fidp);
1455 fidp->fid_type = P9_FID_DIR;
1456 offset += pdu_marshal(pdu, offset, "Qd", &qid, 0);
1459 if (s->proto_version == V9FS_PROTO_2000L) {
1461 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
1462 /* Ignore direct disk access hint until the server supports it. */
1465 flags = omode_to_uflags(mode);
1467 err = v9fs_co_open(s, fidp, flags);
1471 fidp->fid_type = P9_FID_FILE;
1472 iounit = get_iounit(s, &fidp->path);
1473 offset += pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1477 complete_pdu(s, pdu, err);
1480 static void v9fs_lcreate(void *opaque)
1482 int32_t dfid, flags, mode;
1486 V9fsString fullname;
1492 V9fsPDU *pdu = opaque;
1494 v9fs_string_init(&fullname);
1495 pdu_unmarshal(pdu, offset, "dsddd", &dfid, &name, &flags,
1498 fidp = lookup_fid(pdu->s, dfid);
1503 v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data);
1505 /* Ignore direct disk access hint until the server supports it. */
1508 err = v9fs_co_open2(pdu->s, fidp, fullname.data, gid, flags, mode);
1512 fidp->fid_type = P9_FID_FILE;
1513 iounit = get_iounit(pdu->s, &fullname);
1515 err = v9fs_co_lstat(pdu->s, &fullname, &stbuf);
1517 fidp->fid_type = P9_FID_NONE;
1518 if (fidp->fs.fd > 0) {
1523 v9fs_string_copy(&fidp->path, &fullname);
1524 stat_to_qid(&stbuf, &qid);
1525 offset += pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1528 complete_pdu(pdu->s, pdu, err);
1529 v9fs_string_free(&name);
1530 v9fs_string_free(&fullname);
1533 static void v9fs_post_do_fsync(V9fsState *s, V9fsPDU *pdu, int err)
1538 complete_pdu(s, pdu, err);
1541 static void v9fs_fsync(void *opaque)
1543 V9fsPDU *pdu = opaque;
1544 V9fsState *s = pdu->s;
1551 pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1552 fidp = lookup_fid(s, fid);
1555 v9fs_post_do_fsync(s, pdu, err);
1558 err = v9fs_do_fsync(s, fidp->fs.fd, datasync);
1559 v9fs_post_do_fsync(s, pdu, err);
1562 static void v9fs_clunk(void *opaque)
1567 V9fsPDU *pdu = opaque;
1568 V9fsState *s = pdu->s;
1570 pdu_unmarshal(pdu, offset, "d", &fid);
1571 err = free_fid(s, fid);
1577 complete_pdu(s, pdu, err);
1580 static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1582 static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1587 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1588 vs->offset += vs->count;
1591 complete_pdu(s, vs->pdu, err);
1592 v9fs_stat_free(&vs->v9stat);
1593 v9fs_string_free(&vs->name);
1598 static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1605 err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1610 vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1612 if ((vs->len != (vs->v9stat.size + 2)) ||
1613 ((vs->count + vs->len) > vs->max_count)) {
1614 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1615 v9fs_read_post_seekdir(s, vs, err);
1618 vs->count += vs->len;
1619 v9fs_stat_free(&vs->v9stat);
1620 v9fs_string_free(&vs->name);
1621 vs->dir_pos = vs->dent->d_off;
1622 v9fs_co_readdir(s, vs->fidp, &vs->dent);
1623 v9fs_read_post_readdir(s, vs, err);
1626 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1627 v9fs_read_post_seekdir(s, vs, err);
1632 static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1635 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1636 v9fs_string_init(&vs->name);
1637 v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1639 err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1640 v9fs_read_post_dir_lstat(s, vs, err);
1644 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1645 vs->offset += vs->count;
1647 complete_pdu(s, vs->pdu, err);
1652 static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1654 v9fs_co_readdir(s, vs->fidp, &vs->dent);
1655 v9fs_read_post_readdir(s, vs, err);
1659 static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
1662 vs->dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
1663 v9fs_read_post_telldir(s, vs, err);
1667 static void v9fs_read_post_preadv(V9fsState *s, V9fsReadState *vs, ssize_t err)
1670 /* IO error return the error */
1674 vs->total += vs->len;
1675 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1676 if (vs->total < vs->count && vs->len > 0) {
1679 print_sg(vs->sg, vs->cnt);
1681 vs->len = v9fs_do_preadv(s, vs->fidp->fs.fd, vs->sg, vs->cnt,
1686 } while (vs->len == -1 && errno == EINTR);
1687 if (vs->len == -1) {
1690 v9fs_read_post_preadv(s, vs, err);
1693 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1694 vs->offset += vs->count;
1698 complete_pdu(s, vs->pdu, err);
1702 static void v9fs_xattr_read(V9fsState *s, V9fsReadState *vs)
1708 xattr_len = vs->fidp->fs.xattr.len;
1709 read_count = xattr_len - vs->off;
1710 if (read_count > vs->count) {
1711 read_count = vs->count;
1712 } else if (read_count < 0) {
1714 * read beyond XATTR value
1718 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", read_count);
1719 vs->offset += pdu_pack(vs->pdu, vs->offset,
1720 ((char *)vs->fidp->fs.xattr.value) + vs->off,
1723 complete_pdu(s, vs->pdu, err);
1727 static void v9fs_read(void *opaque)
1729 V9fsPDU *pdu = opaque;
1730 V9fsState *s = pdu->s;
1735 vs = g_malloc(sizeof(*vs));
1742 pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count);
1744 vs->fidp = lookup_fid(s, fid);
1745 if (vs->fidp == NULL) {
1750 if (vs->fidp->fid_type == P9_FID_DIR) {
1751 vs->max_count = vs->count;
1754 v9fs_do_rewinddir(s, vs->fidp->fs.dir);
1756 v9fs_read_post_rewinddir(s, vs, err);
1758 } else if (vs->fidp->fid_type == P9_FID_FILE) {
1760 pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
1761 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1762 if (vs->total <= vs->count) {
1763 vs->len = v9fs_do_preadv(s, vs->fidp->fs.fd, vs->sg, vs->cnt,
1769 v9fs_read_post_preadv(s, vs, err);
1772 } else if (vs->fidp->fid_type == P9_FID_XATTR) {
1773 v9fs_xattr_read(s, vs);
1779 complete_pdu(s, pdu, err);
1783 static size_t v9fs_readdir_data_size(V9fsString *name)
1786 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1787 * size of type (1) + size of name.size (2) + strlen(name.data)
1789 return 24 + v9fs_string_size(name);
1792 static int v9fs_do_readdir(V9fsState *s, V9fsPDU *pdu,
1793 V9fsFidState *fidp, int32_t max_count)
1800 off_t saved_dir_pos;
1801 struct dirent *dent;
1803 /* save the directory position */
1804 saved_dir_pos = v9fs_co_telldir(s, fidp);
1805 if (saved_dir_pos < 0) {
1806 return saved_dir_pos;
1809 err = v9fs_co_readdir(s, fidp, &dent);
1813 v9fs_string_init(&name);
1814 v9fs_string_sprintf(&name, "%s", dent->d_name);
1815 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
1816 /* Ran out of buffer. Set dir back to old position and return */
1817 v9fs_co_seekdir(s, fidp, saved_dir_pos);
1818 v9fs_string_free(&name);
1822 * Fill up just the path field of qid because the client uses
1823 * only that. To fill the entire qid structure we will have
1824 * to stat each dirent found, which is expensive
1826 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1827 memcpy(&qid.path, &dent->d_ino, size);
1828 /* Fill the other fields with dummy values */
1832 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1833 len = pdu_marshal(pdu, 11 + count, "Qqbs",
1835 dent->d_type, &name);
1837 v9fs_string_free(&name);
1838 saved_dir_pos = dent->d_off;
1846 static void v9fs_readdir(void *opaque)
1852 int64_t initial_offset;
1853 int32_t count, max_count;
1854 V9fsPDU *pdu = opaque;
1855 V9fsState *s = pdu->s;
1857 pdu_unmarshal(pdu, offset, "dqd", &fid, &initial_offset, &max_count);
1859 fidp = lookup_fid(s, fid);
1860 if (fidp == NULL || !fidp->fs.dir) {
1864 if (initial_offset == 0) {
1865 v9fs_co_rewinddir(s, fidp);
1867 v9fs_co_seekdir(s, fidp, initial_offset);
1869 count = v9fs_do_readdir(s, pdu, fidp, max_count);
1875 retval += pdu_marshal(pdu, offset, "d", count);
1878 complete_pdu(s, pdu, retval);
1881 static void v9fs_write_post_pwritev(V9fsState *s, V9fsWriteState *vs,
1885 /* IO error return the error */
1889 vs->total += vs->len;
1890 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
1891 if (vs->total < vs->count && vs->len > 0) {
1894 print_sg(vs->sg, vs->cnt);
1896 vs->len = v9fs_do_pwritev(s, vs->fidp->fs.fd, vs->sg, vs->cnt,
1901 } while (vs->len == -1 && errno == EINTR);
1902 if (vs->len == -1) {
1905 v9fs_write_post_pwritev(s, vs, err);
1908 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
1911 complete_pdu(s, vs->pdu, err);
1915 static void v9fs_xattr_write(V9fsState *s, V9fsWriteState *vs)
1922 xattr_len = vs->fidp->fs.xattr.len;
1923 write_count = xattr_len - vs->off;
1924 if (write_count > vs->count) {
1925 write_count = vs->count;
1926 } else if (write_count < 0) {
1928 * write beyond XATTR value len specified in
1934 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", write_count);
1936 vs->fidp->fs.xattr.copied_len += write_count;
1938 * Now copy the content from sg list
1940 for (i = 0; i < vs->cnt; i++) {
1941 if (write_count > vs->sg[i].iov_len) {
1942 to_copy = vs->sg[i].iov_len;
1944 to_copy = write_count;
1946 memcpy((char *)vs->fidp->fs.xattr.value + vs->off,
1947 vs->sg[i].iov_base, to_copy);
1948 /* updating vs->off since we are not using below */
1950 write_count -= to_copy;
1953 complete_pdu(s, vs->pdu, err);
1957 static void v9fs_write(void *opaque)
1959 V9fsPDU *pdu = opaque;
1960 V9fsState *s = pdu->s;
1965 vs = g_malloc(sizeof(*vs));
1973 pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
1976 vs->fidp = lookup_fid(s, fid);
1977 if (vs->fidp == NULL) {
1982 if (vs->fidp->fid_type == P9_FID_FILE) {
1983 if (vs->fidp->fs.fd == -1) {
1987 } else if (vs->fidp->fid_type == P9_FID_XATTR) {
1989 * setxattr operation
1991 v9fs_xattr_write(s, vs);
1997 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
1998 if (vs->total <= vs->count) {
1999 vs->len = v9fs_do_pwritev(s, vs->fidp->fs.fd, vs->sg, vs->cnt, vs->off);
2004 v9fs_write_post_pwritev(s, vs, err);
2008 complete_pdu(s, vs->pdu, err);
2012 static void v9fs_create_post_getiounit(V9fsState *s, V9fsCreateState *vs)
2015 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
2016 stat_to_qid(&vs->stbuf, &vs->qid);
2018 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
2021 complete_pdu(s, vs->pdu, err);
2022 v9fs_string_free(&vs->name);
2023 v9fs_string_free(&vs->extension);
2024 v9fs_string_free(&vs->fullname);
2028 static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
2031 vs->iounit = get_iounit(s, &vs->fidp->path);
2032 v9fs_create_post_getiounit(s, vs);
2036 complete_pdu(s, vs->pdu, err);
2037 v9fs_string_free(&vs->name);
2038 v9fs_string_free(&vs->extension);
2039 v9fs_string_free(&vs->fullname);
2043 static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
2048 v9fs_post_create(s, vs, err);
2051 static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
2054 if (!vs->fidp->fs.dir) {
2057 vs->fidp->fid_type = P9_FID_DIR;
2058 v9fs_post_create(s, vs, err);
2061 static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
2069 vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fullname);
2070 v9fs_create_post_opendir(s, vs, err);
2074 v9fs_post_create(s, vs, err);
2077 static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
2083 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2084 v9fs_create_post_dir_lstat(s, vs, err);
2088 v9fs_post_create(s, vs, err);
2091 static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
2094 vs->fidp->fid_type = P9_FID_NONE;
2095 close(vs->fidp->fs.fd);
2098 v9fs_post_create(s, vs, err);
2102 static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
2107 vs->fidp->fid_type = P9_FID_FILE;
2108 err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
2109 v9fs_create_post_fstat(s, vs, err);
2114 v9fs_post_create(s, vs, err);
2118 static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
2121 if (err == 0 || errno != ENOENT) {
2126 if (vs->perm & P9_STAT_MODE_DIR) {
2127 err = v9fs_co_mkdir(s, vs->fullname.data, vs->perm & 0777,
2129 v9fs_create_post_mkdir(s, vs, err);
2130 } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
2131 err = v9fs_do_symlink(s, vs->fidp, vs->extension.data,
2132 vs->fullname.data, -1);
2133 v9fs_create_post_perms(s, vs, err);
2134 } else if (vs->perm & P9_STAT_MODE_LINK) {
2135 int32_t nfid = atoi(vs->extension.data);
2136 V9fsFidState *nfidp = lookup_fid(s, nfid);
2137 if (nfidp == NULL) {
2139 v9fs_post_create(s, vs, err);
2141 err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
2142 v9fs_create_post_perms(s, vs, err);
2143 } else if (vs->perm & P9_STAT_MODE_DEVICE) {
2145 uint32_t major, minor;
2148 if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
2151 v9fs_post_create(s, vs, err);
2163 v9fs_post_create(s, vs, err);
2166 nmode |= vs->perm & 0777;
2167 err = v9fs_do_mknod(s, vs->fullname.data, nmode,
2168 makedev(major, minor), vs->fidp->uid, -1);
2169 v9fs_create_post_perms(s, vs, err);
2170 } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
2171 err = v9fs_do_mknod(s, vs->fullname.data, S_IFIFO | (vs->perm & 0777),
2172 0, vs->fidp->uid, -1);
2173 v9fs_post_create(s, vs, err);
2174 } else if (vs->perm & P9_STAT_MODE_SOCKET) {
2175 err = v9fs_do_mknod(s, vs->fullname.data, S_IFSOCK | (vs->perm & 0777),
2176 0, vs->fidp->uid, -1);
2177 v9fs_post_create(s, vs, err);
2179 err = v9fs_co_open2(s, vs->fidp, vs->fullname.data, -1,
2180 omode_to_uflags(vs->mode)|O_CREAT, vs->perm);
2182 v9fs_create_post_open2(s, vs, err);
2188 v9fs_post_create(s, vs, err);
2191 static void v9fs_create(void *opaque)
2193 V9fsPDU *pdu = opaque;
2194 V9fsState *s = pdu->s;
2196 V9fsCreateState *vs;
2199 vs = g_malloc(sizeof(*vs));
2203 v9fs_string_init(&vs->fullname);
2205 pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
2206 &vs->perm, &vs->mode, &vs->extension);
2208 vs->fidp = lookup_fid(s, fid);
2209 if (vs->fidp == NULL) {
2214 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
2217 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2218 v9fs_create_post_lstat(s, vs, err);
2222 complete_pdu(s, vs->pdu, err);
2223 v9fs_string_free(&vs->name);
2224 v9fs_string_free(&vs->extension);
2228 static void v9fs_post_symlink(V9fsState *s, V9fsSymlinkState *vs, int err)
2231 stat_to_qid(&vs->stbuf, &vs->qid);
2232 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
2237 complete_pdu(s, vs->pdu, err);
2238 v9fs_string_free(&vs->name);
2239 v9fs_string_free(&vs->symname);
2240 v9fs_string_free(&vs->fullname);
2244 static void v9fs_symlink_post_do_symlink(V9fsState *s, V9fsSymlinkState *vs,
2250 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2252 v9fs_post_symlink(s, vs, err);
2255 static void v9fs_symlink(void *opaque)
2257 V9fsPDU *pdu = opaque;
2258 V9fsState *s = pdu->s;
2260 V9fsSymlinkState *vs;
2264 vs = g_malloc(sizeof(*vs));
2268 v9fs_string_init(&vs->fullname);
2270 pdu_unmarshal(vs->pdu, vs->offset, "dssd", &dfid, &vs->name,
2271 &vs->symname, &gid);
2273 vs->dfidp = lookup_fid(s, dfid);
2274 if (vs->dfidp == NULL) {
2279 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->dfidp->path.data,
2281 err = v9fs_do_symlink(s, vs->dfidp, vs->symname.data,
2282 vs->fullname.data, gid);
2283 v9fs_symlink_post_do_symlink(s, vs, err);
2287 complete_pdu(s, vs->pdu, err);
2288 v9fs_string_free(&vs->name);
2289 v9fs_string_free(&vs->symname);
2293 static void v9fs_flush(void *opaque)
2295 V9fsPDU *pdu = opaque;
2296 V9fsState *s = pdu->s;
2297 /* A nop call with no return */
2298 complete_pdu(s, pdu, 7);
2302 static void v9fs_link(void *opaque)
2304 V9fsPDU *pdu = opaque;
2305 V9fsState *s = pdu->s;
2306 int32_t dfid, oldfid;
2307 V9fsFidState *dfidp, *oldfidp;
2308 V9fsString name, fullname;
2312 v9fs_string_init(&fullname);
2314 pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2316 dfidp = lookup_fid(s, dfid);
2317 if (dfidp == NULL) {
2322 oldfidp = lookup_fid(s, oldfid);
2323 if (oldfidp == NULL) {
2328 v9fs_string_sprintf(&fullname, "%s/%s", dfidp->path.data, name.data);
2330 err = v9fs_do_link(s, &oldfidp->path, &fullname);
2334 v9fs_string_free(&fullname);
2337 v9fs_string_free(&name);
2338 complete_pdu(s, pdu, err);
2341 static void v9fs_remove(void *opaque)
2347 V9fsPDU *pdu = opaque;
2349 pdu_unmarshal(pdu, offset, "d", &fid);
2351 fidp = lookup_fid(pdu->s, fid);
2356 err = v9fs_co_remove(pdu->s, &fidp->path);
2361 /* For TREMOVE we need to clunk the fid even on failed remove */
2362 free_fid(pdu->s, fidp->fid);
2364 complete_pdu(pdu->s, pdu, err);
2367 static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
2376 v9fs_stat_free(&vs->v9stat);
2377 complete_pdu(s, vs->pdu, err);
2381 static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
2386 if (vs->v9stat.length != -1) {
2387 if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
2391 v9fs_wstat_post_truncate(s, vs, err);
2395 v9fs_stat_free(&vs->v9stat);
2396 complete_pdu(s, vs->pdu, err);
2400 static int v9fs_complete_rename(V9fsState *s, V9fsFidState *fidp,
2401 int32_t newdirfid, V9fsString *name)
2405 char *old_name, *new_name;
2407 if (newdirfid != -1) {
2408 V9fsFidState *dirfidp;
2409 dirfidp = lookup_fid(s, newdirfid);
2410 if (dirfidp == NULL) {
2414 BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2416 new_name = g_malloc0(dirfidp->path.size + name->size + 2);
2418 strcpy(new_name, dirfidp->path.data);
2419 strcat(new_name, "/");
2420 strcat(new_name + dirfidp->path.size, name->data);
2422 old_name = fidp->path.data;
2423 end = strrchr(old_name, '/');
2429 new_name = g_malloc0(end - old_name + name->size + 1);
2431 strncat(new_name, old_name, end - old_name);
2432 strncat(new_name + (end - old_name), name->data, name->size);
2435 v9fs_string_free(name);
2436 name->data = new_name;
2437 name->size = strlen(new_name);
2439 if (strcmp(new_name, fidp->path.data) != 0) {
2440 err = v9fs_co_rename(s, &fidp->path, name);
2444 V9fsFidState *tfidp;
2446 * Fixup fid's pointing to the old name to
2447 * start pointing to the new name
2449 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2450 if (fidp == tfidp) {
2452 * we replace name of this fid towards the end
2453 * so that our below strcmp will work
2457 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2458 /* replace the name */
2459 v9fs_fix_path(&tfidp->path, name, strlen(fidp->path.data));
2462 v9fs_string_copy(&fidp->path, name);
2468 static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
2474 if (vs->v9stat.name.size != 0) {
2475 err = v9fs_complete_rename(s, vs->fidp, -1, &vs->v9stat.name);
2477 v9fs_wstat_post_rename(s, vs, err);
2481 v9fs_stat_free(&vs->v9stat);
2482 complete_pdu(s, vs->pdu, err);
2486 static void v9fs_rename(void *opaque)
2494 V9fsPDU *pdu = opaque;
2495 V9fsState *s = pdu->s;
2497 pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2499 fidp = lookup_fid(s, fid);
2504 BUG_ON(fidp->fid_type != P9_FID_NONE);
2506 err = v9fs_complete_rename(s, fidp, newdirfid, &name);
2511 complete_pdu(s, pdu, err);
2512 v9fs_string_free(&name);
2515 static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2521 if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) {
2522 if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2523 vs->v9stat.n_gid)) {
2527 v9fs_wstat_post_chown(s, vs, err);
2531 v9fs_stat_free(&vs->v9stat);
2532 complete_pdu(s, vs->pdu, err);
2536 static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2542 if (vs->v9stat.mtime != -1 || vs->v9stat.atime != -1) {
2543 struct timespec times[2];
2544 if (vs->v9stat.atime != -1) {
2545 times[0].tv_sec = vs->v9stat.atime;
2546 times[0].tv_nsec = 0;
2548 times[0].tv_nsec = UTIME_OMIT;
2550 if (vs->v9stat.mtime != -1) {
2551 times[1].tv_sec = vs->v9stat.mtime;
2552 times[1].tv_nsec = 0;
2554 times[1].tv_nsec = UTIME_OMIT;
2557 if (v9fs_do_utimensat(s, &vs->fidp->path, times)) {
2562 v9fs_wstat_post_utime(s, vs, err);
2566 v9fs_stat_free(&vs->v9stat);
2567 complete_pdu(s, vs->pdu, err);
2571 static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2576 v9fs_stat_free(&vs->v9stat);
2577 complete_pdu(s, vs->pdu, err);
2581 static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2590 v9_mode = stat_to_v9mode(&vs->stbuf);
2592 if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2593 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2594 /* Attempting to change the type */
2599 if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2600 &vs->v9stat.extension))) {
2603 v9fs_wstat_post_chmod(s, vs, err);
2607 v9fs_stat_free(&vs->v9stat);
2608 complete_pdu(s, vs->pdu, err);
2612 static void v9fs_wstat(void *opaque)
2614 V9fsPDU *pdu = opaque;
2615 V9fsState *s = pdu->s;
2620 vs = g_malloc(sizeof(*vs));
2624 pdu_unmarshal(pdu, vs->offset, "dwS", &fid, &vs->unused, &vs->v9stat);
2626 vs->fidp = lookup_fid(s, fid);
2627 if (vs->fidp == NULL) {
2632 /* do we need to sync the file? */
2633 if (donttouch_stat(&vs->v9stat)) {
2634 err = v9fs_do_fsync(s, vs->fidp->fs.fd, 0);
2635 v9fs_wstat_post_fsync(s, vs, err);
2639 if (vs->v9stat.mode != -1) {
2640 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
2641 v9fs_wstat_post_lstat(s, vs, err);
2645 v9fs_wstat_post_chmod(s, vs, err);
2649 v9fs_stat_free(&vs->v9stat);
2650 complete_pdu(s, vs->pdu, err);
2654 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2666 int32_t bsize_factor;
2669 * compute bsize factor based on host file system block size
2672 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2673 if (!bsize_factor) {
2676 f_type = stbuf->f_type;
2677 f_bsize = stbuf->f_bsize;
2678 f_bsize *= bsize_factor;
2680 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2681 * adjust(divide) the number of blocks, free blocks and available
2682 * blocks by bsize factor
2684 f_blocks = stbuf->f_blocks/bsize_factor;
2685 f_bfree = stbuf->f_bfree/bsize_factor;
2686 f_bavail = stbuf->f_bavail/bsize_factor;
2687 f_files = stbuf->f_files;
2688 f_ffree = stbuf->f_ffree;
2689 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
2690 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
2691 f_namelen = stbuf->f_namelen;
2693 return pdu_marshal(pdu, offset, "ddqqqqqqd",
2694 f_type, f_bsize, f_blocks, f_bfree,
2695 f_bavail, f_files, f_ffree,
2696 fsid_val, f_namelen);
2699 static void v9fs_statfs(void *opaque)
2705 struct statfs stbuf;
2706 V9fsPDU *pdu = opaque;
2707 V9fsState *s = pdu->s;
2709 pdu_unmarshal(pdu, offset, "d", &fid);
2710 fidp = lookup_fid(s, fid);
2715 retval = v9fs_co_statfs(s, &fidp->path, &stbuf);
2720 retval += v9fs_fill_statfs(s, pdu, &stbuf);
2722 complete_pdu(s, pdu, retval);
2726 static void v9fs_mknod(void *opaque)
2738 V9fsString fullname;
2740 V9fsPDU *pdu = opaque;
2741 V9fsState *s = pdu->s;
2743 v9fs_string_init(&fullname);
2744 pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
2745 &major, &minor, &gid);
2747 fidp = lookup_fid(s, fid);
2752 v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data);
2753 err = v9fs_co_mknod(s, &fullname, fidp->uid, gid,
2754 makedev(major, minor), mode);
2758 err = v9fs_co_lstat(s, &fullname, &stbuf);
2762 stat_to_qid(&stbuf, &qid);
2764 err += pdu_marshal(pdu, offset, "Q", &qid);
2766 complete_pdu(s, pdu, err);
2767 v9fs_string_free(&fullname);
2768 v9fs_string_free(&name);
2772 * Implement posix byte range locking code
2773 * Server side handling of locking code is very simple, because 9p server in
2774 * QEMU can handle only one client. And most of the lock handling
2775 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2776 * do any thing in * qemu 9p server side lock code path.
2777 * So when a TLOCK request comes, always return success
2779 static void v9fs_lock(void *opaque)
2786 int32_t fid, err = 0;
2787 V9fsPDU *pdu = opaque;
2788 V9fsState *s = pdu->s;
2790 flock = g_malloc(sizeof(*flock));
2791 pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock->type,
2792 &flock->flags, &flock->start, &flock->length,
2793 &flock->proc_id, &flock->client_id);
2794 status = P9_LOCK_ERROR;
2796 /* We support only block flag now (that too ignored currently) */
2797 if (flock->flags & ~P9_LOCK_FLAGS_BLOCK) {
2801 fidp = lookup_fid(s, fid);
2806 err = v9fs_co_fstat(s, fidp->fs.fd, &stbuf);
2810 status = P9_LOCK_SUCCESS;
2813 err += pdu_marshal(pdu, offset, "b", status);
2814 complete_pdu(s, pdu, err);
2819 * When a TGETLOCK request comes, always return success because all lock
2820 * handling is done by client's VFS layer.
2822 static void v9fs_getlock(void *opaque)
2828 int32_t fid, err = 0;
2829 V9fsPDU *pdu = opaque;
2830 V9fsState *s = pdu->s;
2832 glock = g_malloc(sizeof(*glock));
2833 pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock->type,
2834 &glock->start, &glock->length, &glock->proc_id,
2837 fidp = lookup_fid(s, fid);
2842 err = v9fs_co_fstat(s, fidp->fs.fd, &stbuf);
2846 glock->type = F_UNLCK;
2847 offset += pdu_marshal(pdu, offset, "bqqds", glock->type,
2848 glock->start, glock->length, glock->proc_id,
2852 complete_pdu(s, pdu, err);
2856 static void v9fs_mkdir(void *opaque)
2858 V9fsPDU *pdu = opaque;
2862 V9fsString name, fullname;
2869 v9fs_string_init(&fullname);
2870 pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
2872 fidp = lookup_fid(pdu->s, fid);
2877 v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data);
2878 err = v9fs_co_mkdir(pdu->s, fullname.data, mode, fidp->uid, gid);
2882 err = v9fs_co_lstat(pdu->s, &fullname, &stbuf);
2886 stat_to_qid(&stbuf, &qid);
2887 offset += pdu_marshal(pdu, offset, "Q", &qid);
2890 complete_pdu(pdu->s, pdu, err);
2891 v9fs_string_free(&fullname);
2892 v9fs_string_free(&name);
2895 static void v9fs_xattrwalk(void *opaque)
2901 int32_t fid, newfid;
2902 V9fsFidState *file_fidp;
2903 V9fsFidState *xattr_fidp;
2904 V9fsPDU *pdu = opaque;
2905 V9fsState *s = pdu->s;
2907 pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
2908 file_fidp = lookup_fid(s, fid);
2909 if (file_fidp == NULL) {
2913 xattr_fidp = alloc_fid(s, newfid);
2914 if (xattr_fidp == NULL) {
2918 v9fs_string_copy(&xattr_fidp->path, &file_fidp->path);
2919 if (name.data[0] == 0) {
2921 * listxattr request. Get the size first
2923 size = v9fs_co_llistxattr(s, &xattr_fidp->path, NULL, 0);
2926 free_fid(s, xattr_fidp->fid);
2930 * Read the xattr value
2932 xattr_fidp->fs.xattr.len = size;
2933 xattr_fidp->fid_type = P9_FID_XATTR;
2934 xattr_fidp->fs.xattr.copied_len = -1;
2936 xattr_fidp->fs.xattr.value = g_malloc(size);
2937 err = v9fs_co_llistxattr(s, &xattr_fidp->path,
2938 xattr_fidp->fs.xattr.value,
2939 xattr_fidp->fs.xattr.len);
2941 free_fid(s, xattr_fidp->fid);
2945 offset += pdu_marshal(pdu, offset, "q", size);
2949 * specific xattr fid. We check for xattr
2950 * presence also collect the xattr size
2952 size = v9fs_co_lgetxattr(s, &xattr_fidp->path,
2956 free_fid(s, xattr_fidp->fid);
2960 * Read the xattr value
2962 xattr_fidp->fs.xattr.len = size;
2963 xattr_fidp->fid_type = P9_FID_XATTR;
2964 xattr_fidp->fs.xattr.copied_len = -1;
2966 xattr_fidp->fs.xattr.value = g_malloc(size);
2967 err = v9fs_co_lgetxattr(s, &xattr_fidp->path,
2968 &name, xattr_fidp->fs.xattr.value,
2969 xattr_fidp->fs.xattr.len);
2971 free_fid(s, xattr_fidp->fid);
2975 offset += pdu_marshal(pdu, offset, "q", size);
2979 complete_pdu(s, pdu, err);
2980 v9fs_string_free(&name);
2983 static void v9fs_xattrcreate(void *opaque)
2991 V9fsFidState *file_fidp;
2992 V9fsFidState *xattr_fidp;
2993 V9fsPDU *pdu = opaque;
2994 V9fsState *s = pdu->s;
2996 pdu_unmarshal(pdu, offset, "dsqd",
2997 &fid, &name, &size, &flags);
2999 file_fidp = lookup_fid(s, fid);
3000 if (file_fidp == NULL) {
3004 /* Make the file fid point to xattr */
3005 xattr_fidp = file_fidp;
3006 xattr_fidp->fid_type = P9_FID_XATTR;
3007 xattr_fidp->fs.xattr.copied_len = 0;
3008 xattr_fidp->fs.xattr.len = size;
3009 xattr_fidp->fs.xattr.flags = flags;
3010 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3011 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3013 xattr_fidp->fs.xattr.value = g_malloc(size);
3015 xattr_fidp->fs.xattr.value = NULL;
3019 complete_pdu(s, pdu, err);
3020 v9fs_string_free(&name);
3023 static void v9fs_readlink(void *opaque)
3025 V9fsPDU *pdu = opaque;
3032 pdu_unmarshal(pdu, offset, "d", &fid);
3033 fidp = lookup_fid(pdu->s, fid);
3039 v9fs_string_init(&target);
3040 err = v9fs_co_readlink(pdu->s, &fidp->path, &target);
3044 offset += pdu_marshal(pdu, offset, "s", &target);
3046 v9fs_string_free(&target);
3048 complete_pdu(pdu->s, pdu, err);
3051 static CoroutineEntry *pdu_co_handlers[] = {
3052 [P9_TREADDIR] = v9fs_readdir,
3053 [P9_TSTATFS] = v9fs_statfs,
3054 [P9_TGETATTR] = v9fs_getattr,
3055 [P9_TSETATTR] = v9fs_setattr,
3056 [P9_TXATTRWALK] = v9fs_xattrwalk,
3057 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3058 [P9_TMKNOD] = v9fs_mknod,
3059 [P9_TRENAME] = v9fs_rename,
3060 [P9_TLOCK] = v9fs_lock,
3061 [P9_TGETLOCK] = v9fs_getlock,
3062 [P9_TREADLINK] = v9fs_readlink,
3063 [P9_TMKDIR] = v9fs_mkdir,
3064 [P9_TVERSION] = v9fs_version,
3065 [P9_TLOPEN] = v9fs_open,
3066 [P9_TATTACH] = v9fs_attach,
3067 [P9_TSTAT] = v9fs_stat,
3068 [P9_TWALK] = v9fs_walk,
3069 [P9_TCLUNK] = v9fs_clunk,
3070 [P9_TFSYNC] = v9fs_fsync,
3071 [P9_TOPEN] = v9fs_open,
3072 [P9_TREAD] = v9fs_read,
3074 [P9_TAUTH] = v9fs_auth,
3076 [P9_TFLUSH] = v9fs_flush,
3077 [P9_TLINK] = v9fs_link,
3078 [P9_TSYMLINK] = v9fs_symlink,
3079 [P9_TCREATE] = v9fs_create,
3080 [P9_TLCREATE] = v9fs_lcreate,
3081 [P9_TWRITE] = v9fs_write,
3082 [P9_TWSTAT] = v9fs_wstat,
3083 [P9_TREMOVE] = v9fs_remove,
3086 static void v9fs_op_not_supp(void *opaque)
3088 V9fsPDU *pdu = opaque;
3089 complete_pdu(pdu->s, pdu, -EOPNOTSUPP);
3092 static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
3095 CoroutineEntry *handler;
3100 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3101 (pdu_co_handlers[pdu->id] == NULL)) {
3102 handler = v9fs_op_not_supp;
3104 handler = pdu_co_handlers[pdu->id];
3106 co = qemu_coroutine_create(handler);
3107 qemu_coroutine_enter(co, pdu);
3110 void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
3112 V9fsState *s = (V9fsState *)vdev;
3116 while ((pdu = alloc_pdu(s)) &&
3117 (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
3120 BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
3121 BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
3123 ptr = pdu->elem.out_sg[0].iov_base;
3125 memcpy(&pdu->size, ptr, 4);
3127 memcpy(&pdu->tag, ptr + 5, 2);