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 int v9fs_do_close(V9fsState *s, int fd)
87 return s->ops->close(&s->ctx, fd);
90 static int v9fs_do_closedir(V9fsState *s, DIR *dir)
92 return s->ops->closedir(&s->ctx, dir);
95 static int v9fs_do_open(V9fsState *s, V9fsString *path, int flags)
97 return s->ops->open(&s->ctx, path->data, flags);
100 static DIR *v9fs_do_opendir(V9fsState *s, V9fsString *path)
102 return s->ops->opendir(&s->ctx, path->data);
105 static void v9fs_do_rewinddir(V9fsState *s, DIR *dir)
107 return s->ops->rewinddir(&s->ctx, dir);
110 static off_t v9fs_do_telldir(V9fsState *s, DIR *dir)
112 return s->ops->telldir(&s->ctx, dir);
115 static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off)
117 return s->ops->seekdir(&s->ctx, dir, off);
120 static int v9fs_do_preadv(V9fsState *s, int fd, const struct iovec *iov,
121 int iovcnt, int64_t offset)
123 return s->ops->preadv(&s->ctx, fd, iov, iovcnt, offset);
126 static int v9fs_do_pwritev(V9fsState *s, int fd, const struct iovec *iov,
127 int iovcnt, int64_t offset)
129 return s->ops->pwritev(&s->ctx, fd, iov, iovcnt, offset);
132 static int v9fs_do_chmod(V9fsState *s, V9fsString *path, mode_t mode)
137 return s->ops->chmod(&s->ctx, path->data, &cred);
140 static int v9fs_do_mknod(V9fsState *s, char *name,
141 mode_t mode, dev_t dev, uid_t uid, gid_t gid)
149 return s->ops->mknod(&s->ctx, name, &cred);
152 static int v9fs_do_mkdir(V9fsState *s, char *name, mode_t mode,
153 uid_t uid, gid_t gid)
162 return s->ops->mkdir(&s->ctx, name, &cred);
165 static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
167 return s->ops->fstat(&s->ctx, fd, stbuf);
170 static int v9fs_do_open2(V9fsState *s, char *fullname, uid_t uid, gid_t gid,
178 cred.fc_mode = mode & 07777;
180 return s->ops->open2(&s->ctx, fullname, flags, &cred);
183 static int v9fs_do_symlink(V9fsState *s, V9fsFidState *fidp,
184 const char *oldpath, const char *newpath, gid_t gid)
188 cred.fc_uid = fidp->uid;
192 return s->ops->symlink(&s->ctx, oldpath, newpath, &cred);
195 static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
197 return s->ops->link(&s->ctx, oldpath->data, newpath->data);
200 static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
202 return s->ops->truncate(&s->ctx, path->data, size);
205 static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath,
208 return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
211 static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
218 return s->ops->chown(&s->ctx, path->data, &cred);
221 static int v9fs_do_utimensat(V9fsState *s, V9fsString *path,
222 const struct timespec times[2])
224 return s->ops->utimensat(&s->ctx, path->data, times);
227 static int v9fs_do_remove(V9fsState *s, V9fsString *path)
229 return s->ops->remove(&s->ctx, path->data);
232 static int v9fs_do_fsync(V9fsState *s, int fd, int datasync)
234 return s->ops->fsync(&s->ctx, fd, datasync);
237 static int v9fs_do_statfs(V9fsState *s, V9fsString *path, struct statfs *stbuf)
239 return s->ops->statfs(&s->ctx, path->data, stbuf);
242 static ssize_t v9fs_do_lgetxattr(V9fsState *s, V9fsString *path,
243 V9fsString *xattr_name,
244 void *value, size_t size)
246 return s->ops->lgetxattr(&s->ctx, path->data,
247 xattr_name->data, value, size);
250 static ssize_t v9fs_do_llistxattr(V9fsState *s, V9fsString *path,
251 void *value, size_t size)
253 return s->ops->llistxattr(&s->ctx, path->data,
257 static int v9fs_do_lsetxattr(V9fsState *s, V9fsString *path,
258 V9fsString *xattr_name,
259 void *value, size_t size, int flags)
261 return s->ops->lsetxattr(&s->ctx, path->data,
262 xattr_name->data, value, size, flags);
265 static int v9fs_do_lremovexattr(V9fsState *s, V9fsString *path,
266 V9fsString *xattr_name)
268 return s->ops->lremovexattr(&s->ctx, path->data,
273 static void v9fs_string_init(V9fsString *str)
279 static void v9fs_string_free(V9fsString *str)
281 qemu_free(str->data);
286 static void v9fs_string_null(V9fsString *str)
288 v9fs_string_free(str);
291 static int number_to_string(void *arg, char type)
293 unsigned int ret = 0;
297 unsigned int num = *(unsigned int *)arg;
306 unsigned long num = *(unsigned long *)arg;
314 printf("Number_to_string: Unknown number format\n");
321 static int GCC_FMT_ATTR(2, 0)
322 v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
325 char *iter = (char *)fmt;
329 unsigned int arg_uint;
330 unsigned long arg_ulong;
332 /* Find the number of %'s that denotes an argument */
333 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
338 len = strlen(fmt) - 2*nr_args;
348 /* Now parse the format string */
349 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
353 arg_uint = va_arg(ap2, unsigned int);
354 len += number_to_string((void *)&arg_uint, 'u');
357 if (*++iter == 'u') {
358 arg_ulong = va_arg(ap2, unsigned long);
359 len += number_to_string((void *)&arg_ulong, 'U');
365 arg_char_ptr = va_arg(ap2, char *);
366 len += strlen(arg_char_ptr);
373 "v9fs_string_alloc_printf:Incorrect format %c", *iter);
380 *strp = qemu_malloc((len + 1) * sizeof(**strp));
382 return vsprintf(*strp, fmt, ap);
385 static void GCC_FMT_ATTR(2, 3)
386 v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
391 v9fs_string_free(str);
394 err = v9fs_string_alloc_printf(&str->data, fmt, ap);
401 static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
403 v9fs_string_free(lhs);
404 v9fs_string_sprintf(lhs, "%s", rhs->data);
408 * Return TRUE if s1 is an ancestor of s2.
410 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
411 * As a special case, We treat s1 as ancestor of s2 if they are same!
413 static int v9fs_path_is_ancestor(V9fsString *s1, V9fsString *s2)
415 if (!strncmp(s1->data, s2->data, s1->size)) {
416 if (s2->data[s1->size] == '\0' || s2->data[s1->size] == '/') {
423 static size_t v9fs_string_size(V9fsString *str)
428 static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
432 for (f = s->fid_list; f; f = f->next) {
441 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
445 f = lookup_fid(s, fid);
450 f = qemu_mallocz(sizeof(V9fsFidState));
453 f->fid_type = P9_FID_NONE;
455 f->next = s->fid_list;
461 static int v9fs_xattr_fid_clunk(V9fsState *s, V9fsFidState *fidp)
465 if (fidp->fs.xattr.copied_len == -1) {
466 /* getxattr/listxattr fid */
470 * if this is fid for setxattr. clunk should
471 * result in setxattr localcall
473 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
474 /* clunk after partial write */
478 if (fidp->fs.xattr.len) {
479 retval = v9fs_do_lsetxattr(s, &fidp->path, &fidp->fs.xattr.name,
480 fidp->fs.xattr.value,
482 fidp->fs.xattr.flags);
484 retval = v9fs_do_lremovexattr(s, &fidp->path, &fidp->fs.xattr.name);
487 v9fs_string_free(&fidp->fs.xattr.name);
489 if (fidp->fs.xattr.value) {
490 qemu_free(fidp->fs.xattr.value);
495 static int free_fid(V9fsState *s, int32_t fid)
498 V9fsFidState **fidpp, *fidp;
500 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
501 if ((*fidpp)->fid == fid) {
506 if (*fidpp == NULL) {
513 if (fidp->fid_type == P9_FID_FILE) {
514 v9fs_do_close(s, fidp->fs.fd);
515 } else if (fidp->fid_type == P9_FID_DIR) {
516 v9fs_do_closedir(s, fidp->fs.dir);
517 } else if (fidp->fid_type == P9_FID_XATTR) {
518 retval = v9fs_xattr_fid_clunk(s, fidp);
520 v9fs_string_free(&fidp->path);
526 #define P9_QID_TYPE_DIR 0x80
527 #define P9_QID_TYPE_SYMLINK 0x02
529 #define P9_STAT_MODE_DIR 0x80000000
530 #define P9_STAT_MODE_APPEND 0x40000000
531 #define P9_STAT_MODE_EXCL 0x20000000
532 #define P9_STAT_MODE_MOUNT 0x10000000
533 #define P9_STAT_MODE_AUTH 0x08000000
534 #define P9_STAT_MODE_TMP 0x04000000
535 #define P9_STAT_MODE_SYMLINK 0x02000000
536 #define P9_STAT_MODE_LINK 0x01000000
537 #define P9_STAT_MODE_DEVICE 0x00800000
538 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
539 #define P9_STAT_MODE_SOCKET 0x00100000
540 #define P9_STAT_MODE_SETUID 0x00080000
541 #define P9_STAT_MODE_SETGID 0x00040000
542 #define P9_STAT_MODE_SETVTX 0x00010000
544 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
545 P9_STAT_MODE_SYMLINK | \
546 P9_STAT_MODE_LINK | \
547 P9_STAT_MODE_DEVICE | \
548 P9_STAT_MODE_NAMED_PIPE | \
551 /* This is the algorithm from ufs in spfs */
552 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
556 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
557 memcpy(&qidp->path, &stbuf->st_ino, size);
558 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
560 if (S_ISDIR(stbuf->st_mode)) {
561 qidp->type |= P9_QID_TYPE_DIR;
563 if (S_ISLNK(stbuf->st_mode)) {
564 qidp->type |= P9_QID_TYPE_SYMLINK;
568 static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
573 err = v9fs_do_lstat(s, &fidp->path, &stbuf);
578 stat_to_qid(&stbuf, qidp);
582 static V9fsPDU *alloc_pdu(V9fsState *s)
586 if (!QLIST_EMPTY(&s->free_list)) {
587 pdu = QLIST_FIRST(&s->free_list);
588 QLIST_REMOVE(pdu, next);
593 static void free_pdu(V9fsState *s, V9fsPDU *pdu)
599 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
603 size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
604 size_t offset, size_t size, int pack)
609 for (i = 0; size && i < sg_count; i++) {
611 if (offset >= sg[i].iov_len) {
613 offset -= sg[i].iov_len;
616 len = MIN(sg[i].iov_len - offset, size);
618 memcpy(sg[i].iov_base + offset, addr, len);
620 memcpy(addr, sg[i].iov_base + offset, len);
635 static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
637 return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
641 static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
644 return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
648 static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
652 struct iovec *src_sg;
656 src_sg = pdu->elem.in_sg;
657 num = pdu->elem.in_num;
659 src_sg = pdu->elem.out_sg;
660 num = pdu->elem.out_num;
664 for (i = 0; i < num; i++) {
666 sg[j].iov_base = src_sg[i].iov_base;
667 sg[j].iov_len = src_sg[i].iov_len;
669 } else if (offset < (src_sg[i].iov_len + pos)) {
670 sg[j].iov_base = src_sg[i].iov_base;
671 sg[j].iov_len = src_sg[i].iov_len;
672 sg[j].iov_base += (offset - pos);
673 sg[j].iov_len -= (offset - pos);
676 pos += src_sg[i].iov_len;
682 static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
684 size_t old_offset = offset;
689 for (i = 0; fmt[i]; i++) {
692 uint8_t *valp = va_arg(ap, uint8_t *);
693 offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
698 valp = va_arg(ap, uint16_t *);
699 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
700 *valp = le16_to_cpu(val);
705 valp = va_arg(ap, uint32_t *);
706 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
707 *valp = le32_to_cpu(val);
712 valp = va_arg(ap, uint64_t *);
713 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
714 *valp = le64_to_cpu(val);
718 struct iovec *iov = va_arg(ap, struct iovec *);
719 int *iovcnt = va_arg(ap, int *);
720 *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
724 V9fsString *str = va_arg(ap, V9fsString *);
725 offset += pdu_unmarshal(pdu, offset, "w", &str->size);
726 /* FIXME: sanity check str->size */
727 str->data = qemu_malloc(str->size + 1);
728 offset += pdu_unpack(str->data, pdu, offset, str->size);
729 str->data[str->size] = 0;
733 V9fsQID *qidp = va_arg(ap, V9fsQID *);
734 offset += pdu_unmarshal(pdu, offset, "bdq",
735 &qidp->type, &qidp->version, &qidp->path);
739 V9fsStat *statp = va_arg(ap, V9fsStat *);
740 offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
741 &statp->size, &statp->type, &statp->dev,
742 &statp->qid, &statp->mode, &statp->atime,
743 &statp->mtime, &statp->length,
744 &statp->name, &statp->uid, &statp->gid,
745 &statp->muid, &statp->extension,
746 &statp->n_uid, &statp->n_gid,
751 V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
752 offset += pdu_unmarshal(pdu, offset, "ddddqqqqq",
753 &iattr->valid, &iattr->mode,
754 &iattr->uid, &iattr->gid, &iattr->size,
755 &iattr->atime_sec, &iattr->atime_nsec,
756 &iattr->mtime_sec, &iattr->mtime_nsec);
766 return offset - old_offset;
769 static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
771 size_t old_offset = offset;
776 for (i = 0; fmt[i]; i++) {
779 uint8_t val = va_arg(ap, int);
780 offset += pdu_pack(pdu, offset, &val, sizeof(val));
785 cpu_to_le16w(&val, va_arg(ap, int));
786 offset += pdu_pack(pdu, offset, &val, sizeof(val));
791 cpu_to_le32w(&val, va_arg(ap, uint32_t));
792 offset += pdu_pack(pdu, offset, &val, sizeof(val));
797 cpu_to_le64w(&val, va_arg(ap, uint64_t));
798 offset += pdu_pack(pdu, offset, &val, sizeof(val));
802 struct iovec *iov = va_arg(ap, struct iovec *);
803 int *iovcnt = va_arg(ap, int *);
804 *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
808 V9fsString *str = va_arg(ap, V9fsString *);
809 offset += pdu_marshal(pdu, offset, "w", str->size);
810 offset += pdu_pack(pdu, offset, str->data, str->size);
814 V9fsQID *qidp = va_arg(ap, V9fsQID *);
815 offset += pdu_marshal(pdu, offset, "bdq",
816 qidp->type, qidp->version, qidp->path);
820 V9fsStat *statp = va_arg(ap, V9fsStat *);
821 offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
822 statp->size, statp->type, statp->dev,
823 &statp->qid, statp->mode, statp->atime,
824 statp->mtime, statp->length, &statp->name,
825 &statp->uid, &statp->gid, &statp->muid,
826 &statp->extension, statp->n_uid,
827 statp->n_gid, statp->n_muid);
831 V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
832 offset += pdu_marshal(pdu, offset, "qQdddqqqqqqqqqqqqqqq",
833 statp->st_result_mask,
834 &statp->qid, statp->st_mode,
835 statp->st_uid, statp->st_gid,
836 statp->st_nlink, statp->st_rdev,
837 statp->st_size, statp->st_blksize, statp->st_blocks,
838 statp->st_atime_sec, statp->st_atime_nsec,
839 statp->st_mtime_sec, statp->st_mtime_nsec,
840 statp->st_ctime_sec, statp->st_ctime_nsec,
841 statp->st_btime_sec, statp->st_btime_nsec,
842 statp->st_gen, statp->st_data_version);
851 return offset - old_offset;
854 static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
856 int8_t id = pdu->id + 1; /* Response */
862 if (s->proto_version != V9FS_PROTO_2000L) {
865 str.data = strerror(err);
866 str.size = strlen(str.data);
868 len += pdu_marshal(pdu, len, "s", &str);
872 len += pdu_marshal(pdu, len, "d", err);
874 if (s->proto_version == V9FS_PROTO_2000L) {
879 /* fill out the header */
880 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
882 /* keep these in sync */
886 /* push onto queue and notify */
887 virtqueue_push(s->vq, &pdu->elem, len);
889 /* FIXME: we should batch these completions */
890 virtio_notify(&s->vdev, s->vq);
895 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
900 if (mode & P9_STAT_MODE_DIR) {
904 if (mode & P9_STAT_MODE_SYMLINK) {
907 if (mode & P9_STAT_MODE_SOCKET) {
910 if (mode & P9_STAT_MODE_NAMED_PIPE) {
913 if (mode & P9_STAT_MODE_DEVICE) {
914 if (extension && extension->data[0] == 'c') {
925 if (mode & P9_STAT_MODE_SETUID) {
928 if (mode & P9_STAT_MODE_SETGID) {
931 if (mode & P9_STAT_MODE_SETVTX) {
938 static int donttouch_stat(V9fsStat *stat)
940 if (stat->type == -1 &&
942 stat->qid.type == -1 &&
943 stat->qid.version == -1 &&
944 stat->qid.path == -1 &&
948 stat->length == -1 &&
955 stat->n_muid == -1) {
962 static void v9fs_stat_free(V9fsStat *stat)
964 v9fs_string_free(&stat->name);
965 v9fs_string_free(&stat->uid);
966 v9fs_string_free(&stat->gid);
967 v9fs_string_free(&stat->muid);
968 v9fs_string_free(&stat->extension);
971 static uint32_t stat_to_v9mode(const struct stat *stbuf)
975 mode = stbuf->st_mode & 0777;
976 if (S_ISDIR(stbuf->st_mode)) {
977 mode |= P9_STAT_MODE_DIR;
980 if (S_ISLNK(stbuf->st_mode)) {
981 mode |= P9_STAT_MODE_SYMLINK;
984 if (S_ISSOCK(stbuf->st_mode)) {
985 mode |= P9_STAT_MODE_SOCKET;
988 if (S_ISFIFO(stbuf->st_mode)) {
989 mode |= P9_STAT_MODE_NAMED_PIPE;
992 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
993 mode |= P9_STAT_MODE_DEVICE;
996 if (stbuf->st_mode & S_ISUID) {
997 mode |= P9_STAT_MODE_SETUID;
1000 if (stbuf->st_mode & S_ISGID) {
1001 mode |= P9_STAT_MODE_SETGID;
1004 if (stbuf->st_mode & S_ISVTX) {
1005 mode |= P9_STAT_MODE_SETVTX;
1011 static int stat_to_v9stat(V9fsState *s, V9fsString *name,
1012 const struct stat *stbuf,
1018 memset(v9stat, 0, sizeof(*v9stat));
1020 stat_to_qid(stbuf, &v9stat->qid);
1021 v9stat->mode = stat_to_v9mode(stbuf);
1022 v9stat->atime = stbuf->st_atime;
1023 v9stat->mtime = stbuf->st_mtime;
1024 v9stat->length = stbuf->st_size;
1026 v9fs_string_null(&v9stat->uid);
1027 v9fs_string_null(&v9stat->gid);
1028 v9fs_string_null(&v9stat->muid);
1030 v9stat->n_uid = stbuf->st_uid;
1031 v9stat->n_gid = stbuf->st_gid;
1034 v9fs_string_null(&v9stat->extension);
1036 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
1037 err = v9fs_co_readlink(s, name, &v9stat->extension);
1041 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
1042 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
1043 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
1044 major(stbuf->st_rdev), minor(stbuf->st_rdev));
1045 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
1046 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
1047 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
1050 str = strrchr(name->data, '/');
1057 v9fs_string_sprintf(&v9stat->name, "%s", str);
1060 v9fs_string_size(&v9stat->name) +
1061 v9fs_string_size(&v9stat->uid) +
1062 v9fs_string_size(&v9stat->gid) +
1063 v9fs_string_size(&v9stat->muid) +
1064 v9fs_string_size(&v9stat->extension);
1068 #define P9_STATS_MODE 0x00000001ULL
1069 #define P9_STATS_NLINK 0x00000002ULL
1070 #define P9_STATS_UID 0x00000004ULL
1071 #define P9_STATS_GID 0x00000008ULL
1072 #define P9_STATS_RDEV 0x00000010ULL
1073 #define P9_STATS_ATIME 0x00000020ULL
1074 #define P9_STATS_MTIME 0x00000040ULL
1075 #define P9_STATS_CTIME 0x00000080ULL
1076 #define P9_STATS_INO 0x00000100ULL
1077 #define P9_STATS_SIZE 0x00000200ULL
1078 #define P9_STATS_BLOCKS 0x00000400ULL
1080 #define P9_STATS_BTIME 0x00000800ULL
1081 #define P9_STATS_GEN 0x00001000ULL
1082 #define P9_STATS_DATA_VERSION 0x00002000ULL
1084 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1085 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1088 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
1089 V9fsStatDotl *v9lstat)
1091 memset(v9lstat, 0, sizeof(*v9lstat));
1093 v9lstat->st_mode = stbuf->st_mode;
1094 v9lstat->st_nlink = stbuf->st_nlink;
1095 v9lstat->st_uid = stbuf->st_uid;
1096 v9lstat->st_gid = stbuf->st_gid;
1097 v9lstat->st_rdev = stbuf->st_rdev;
1098 v9lstat->st_size = stbuf->st_size;
1099 v9lstat->st_blksize = stbuf->st_blksize;
1100 v9lstat->st_blocks = stbuf->st_blocks;
1101 v9lstat->st_atime_sec = stbuf->st_atime;
1102 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1103 v9lstat->st_mtime_sec = stbuf->st_mtime;
1104 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
1105 v9lstat->st_ctime_sec = stbuf->st_ctime;
1106 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1107 /* Currently we only support BASIC fields in stat */
1108 v9lstat->st_result_mask = P9_STATS_BASIC;
1110 stat_to_qid(stbuf, &v9lstat->qid);
1113 static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
1115 while (len && *iovcnt) {
1116 if (len < sg->iov_len) {
1118 sg->iov_base += len;
1130 static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
1135 for (i = 0; i < *cnt; i++) {
1136 if ((total + sg[i].iov_len) > cap) {
1137 sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
1141 total += sg[i].iov_len;
1149 static void print_sg(struct iovec *sg, int cnt)
1153 printf("sg[%d]: {", cnt);
1154 for (i = 0; i < cnt; i++) {
1158 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1163 static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
1166 v9fs_string_init(&str);
1167 v9fs_string_copy(&str, dst);
1168 v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
1169 v9fs_string_free(&str);
1172 static void v9fs_version(void *opaque)
1174 V9fsPDU *pdu = opaque;
1175 V9fsState *s = pdu->s;
1179 pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1181 if (!strcmp(version.data, "9P2000.u")) {
1182 s->proto_version = V9FS_PROTO_2000U;
1183 } else if (!strcmp(version.data, "9P2000.L")) {
1184 s->proto_version = V9FS_PROTO_2000L;
1186 v9fs_string_sprintf(&version, "unknown");
1189 offset += pdu_marshal(pdu, offset, "ds", s->msize, &version);
1190 complete_pdu(s, pdu, offset);
1192 v9fs_string_free(&version);
1196 static void v9fs_attach(void *opaque)
1198 V9fsPDU *pdu = opaque;
1199 V9fsState *s = pdu->s;
1200 int32_t fid, afid, n_uname;
1201 V9fsString uname, aname;
1207 pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1209 fidp = alloc_fid(s, fid);
1215 fidp->uid = n_uname;
1217 v9fs_string_sprintf(&fidp->path, "%s", "/");
1218 err = fid_to_qid(s, fidp, &qid);
1225 offset += pdu_marshal(pdu, offset, "Q", &qid);
1229 complete_pdu(s, pdu, err);
1230 v9fs_string_free(&uname);
1231 v9fs_string_free(&aname);
1234 static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1241 err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1245 vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1249 complete_pdu(s, vs->pdu, err);
1250 v9fs_stat_free(&vs->v9stat);
1254 static void v9fs_stat(void *opaque)
1256 V9fsPDU *pdu = opaque;
1257 V9fsState *s = pdu->s;
1262 vs = qemu_malloc(sizeof(*vs));
1266 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1268 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1270 vs->fidp = lookup_fid(s, fid);
1271 if (vs->fidp == NULL) {
1276 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1277 v9fs_stat_post_lstat(s, vs, err);
1281 complete_pdu(s, vs->pdu, err);
1282 v9fs_stat_free(&vs->v9stat);
1286 static void v9fs_getattr_post_lstat(V9fsState *s, V9fsStatStateDotl *vs,
1294 stat_to_v9stat_dotl(s, &vs->stbuf, &vs->v9stat_dotl);
1295 vs->offset += pdu_marshal(vs->pdu, vs->offset, "A", &vs->v9stat_dotl);
1299 complete_pdu(s, vs->pdu, err);
1303 static void v9fs_getattr(void *opaque)
1305 V9fsPDU *pdu = opaque;
1306 V9fsState *s = pdu->s;
1308 V9fsStatStateDotl *vs;
1311 uint64_t request_mask;
1313 vs = qemu_malloc(sizeof(*vs));
1317 memset(&vs->v9stat_dotl, 0, sizeof(vs->v9stat_dotl));
1319 pdu_unmarshal(vs->pdu, vs->offset, "dq", &fid, &request_mask);
1321 fidp = lookup_fid(s, fid);
1327 /* Currently we only support BASIC fields in stat, so there is no
1328 * need to look at request_mask.
1330 err = v9fs_do_lstat(s, &fidp->path, &vs->stbuf);
1331 v9fs_getattr_post_lstat(s, vs, err);
1335 complete_pdu(s, vs->pdu, err);
1339 /* From Linux kernel code */
1340 #define ATTR_MODE (1 << 0)
1341 #define ATTR_UID (1 << 1)
1342 #define ATTR_GID (1 << 2)
1343 #define ATTR_SIZE (1 << 3)
1344 #define ATTR_ATIME (1 << 4)
1345 #define ATTR_MTIME (1 << 5)
1346 #define ATTR_CTIME (1 << 6)
1347 #define ATTR_MASK 127
1348 #define ATTR_ATIME_SET (1 << 7)
1349 #define ATTR_MTIME_SET (1 << 8)
1351 static void v9fs_setattr_post_truncate(V9fsState *s, V9fsSetattrState *vs,
1361 complete_pdu(s, vs->pdu, err);
1365 static void v9fs_setattr_post_chown(V9fsState *s, V9fsSetattrState *vs, int err)
1372 if (vs->v9iattr.valid & (ATTR_SIZE)) {
1373 err = v9fs_do_truncate(s, &vs->fidp->path, vs->v9iattr.size);
1375 v9fs_setattr_post_truncate(s, vs, err);
1379 complete_pdu(s, vs->pdu, err);
1383 static void v9fs_setattr_post_utimensat(V9fsState *s, V9fsSetattrState *vs,
1391 /* If the only valid entry in iattr is ctime we can call
1392 * chown(-1,-1) to update the ctime of the file
1394 if ((vs->v9iattr.valid & (ATTR_UID | ATTR_GID)) ||
1395 ((vs->v9iattr.valid & ATTR_CTIME)
1396 && !((vs->v9iattr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
1397 if (!(vs->v9iattr.valid & ATTR_UID)) {
1398 vs->v9iattr.uid = -1;
1400 if (!(vs->v9iattr.valid & ATTR_GID)) {
1401 vs->v9iattr.gid = -1;
1403 err = v9fs_do_chown(s, &vs->fidp->path, vs->v9iattr.uid,
1406 v9fs_setattr_post_chown(s, vs, err);
1410 complete_pdu(s, vs->pdu, err);
1414 static void v9fs_setattr_post_chmod(V9fsState *s, V9fsSetattrState *vs, int err)
1421 if (vs->v9iattr.valid & (ATTR_ATIME | ATTR_MTIME)) {
1422 struct timespec times[2];
1423 if (vs->v9iattr.valid & ATTR_ATIME) {
1424 if (vs->v9iattr.valid & ATTR_ATIME_SET) {
1425 times[0].tv_sec = vs->v9iattr.atime_sec;
1426 times[0].tv_nsec = vs->v9iattr.atime_nsec;
1428 times[0].tv_nsec = UTIME_NOW;
1431 times[0].tv_nsec = UTIME_OMIT;
1434 if (vs->v9iattr.valid & ATTR_MTIME) {
1435 if (vs->v9iattr.valid & ATTR_MTIME_SET) {
1436 times[1].tv_sec = vs->v9iattr.mtime_sec;
1437 times[1].tv_nsec = vs->v9iattr.mtime_nsec;
1439 times[1].tv_nsec = UTIME_NOW;
1442 times[1].tv_nsec = UTIME_OMIT;
1444 err = v9fs_do_utimensat(s, &vs->fidp->path, times);
1446 v9fs_setattr_post_utimensat(s, vs, err);
1450 complete_pdu(s, vs->pdu, err);
1454 static void v9fs_setattr(void *opaque)
1456 V9fsPDU *pdu = opaque;
1457 V9fsState *s = pdu->s;
1459 V9fsSetattrState *vs;
1462 vs = qemu_malloc(sizeof(*vs));
1466 pdu_unmarshal(pdu, vs->offset, "dI", &fid, &vs->v9iattr);
1468 vs->fidp = lookup_fid(s, fid);
1469 if (vs->fidp == NULL) {
1474 if (vs->v9iattr.valid & ATTR_MODE) {
1475 err = v9fs_do_chmod(s, &vs->fidp->path, vs->v9iattr.mode);
1478 v9fs_setattr_post_chmod(s, vs, err);
1482 complete_pdu(s, vs->pdu, err);
1486 static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1488 complete_pdu(s, vs->pdu, err);
1490 if (vs->nwnames && vs->nwnames <= P9_MAXWELEM) {
1491 for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1492 v9fs_string_free(&vs->wnames[vs->name_idx]);
1495 qemu_free(vs->wnames);
1496 qemu_free(vs->qids);
1500 static void v9fs_walk_marshal(V9fsWalkState *vs)
1504 vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1506 for (i = 0; i < vs->nwnames; i++) {
1507 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1511 static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1515 free_fid(s, vs->newfidp->fid);
1516 v9fs_string_free(&vs->path);
1521 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1524 if (vs->name_idx < vs->nwnames) {
1525 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1526 vs->wnames[vs->name_idx].data);
1527 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1529 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1530 v9fs_walk_post_newfid_lstat(s, vs, err);
1534 v9fs_string_free(&vs->path);
1535 v9fs_walk_marshal(vs);
1538 v9fs_walk_complete(s, vs, err);
1541 static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1545 v9fs_string_free(&vs->path);
1550 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1552 if (vs->name_idx < vs->nwnames) {
1554 v9fs_string_sprintf(&vs->path, "%s/%s",
1555 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1556 v9fs_string_copy(&vs->fidp->path, &vs->path);
1558 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1559 v9fs_walk_post_oldfid_lstat(s, vs, err);
1563 v9fs_string_free(&vs->path);
1564 v9fs_walk_marshal(vs);
1567 v9fs_walk_complete(s, vs, err);
1570 static void v9fs_walk(void *opaque)
1572 V9fsPDU *pdu = opaque;
1573 V9fsState *s = pdu->s;
1574 int32_t fid, newfid;
1579 vs = qemu_malloc(sizeof(*vs));
1585 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1586 &newfid, &vs->nwnames);
1588 if (vs->nwnames && vs->nwnames <= P9_MAXWELEM) {
1589 vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1591 vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1593 for (i = 0; i < vs->nwnames; i++) {
1594 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1597 } else if (vs->nwnames > P9_MAXWELEM) {
1602 vs->fidp = lookup_fid(s, fid);
1603 if (vs->fidp == NULL) {
1608 /* FIXME: is this really valid? */
1609 if (fid == newfid) {
1611 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
1612 v9fs_string_init(&vs->path);
1615 if (vs->name_idx < vs->nwnames) {
1616 v9fs_string_sprintf(&vs->path, "%s/%s",
1617 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1618 v9fs_string_copy(&vs->fidp->path, &vs->path);
1620 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1621 v9fs_walk_post_oldfid_lstat(s, vs, err);
1625 vs->newfidp = alloc_fid(s, newfid);
1626 if (vs->newfidp == NULL) {
1631 vs->newfidp->uid = vs->fidp->uid;
1632 v9fs_string_init(&vs->path);
1634 v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1636 if (vs->name_idx < vs->nwnames) {
1637 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1638 vs->wnames[vs->name_idx].data);
1639 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1641 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1642 v9fs_walk_post_newfid_lstat(s, vs, err);
1647 v9fs_walk_marshal(vs);
1650 v9fs_walk_complete(s, vs, err);
1653 static int32_t get_iounit(V9fsState *s, V9fsString *name)
1655 struct statfs stbuf;
1659 * iounit should be multiples of f_bsize (host filesystem block size
1660 * and as well as less than (client msize - P9_IOHDRSZ))
1662 if (!v9fs_do_statfs(s, name, &stbuf)) {
1663 iounit = stbuf.f_bsize;
1664 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1668 iounit = s->msize - P9_IOHDRSZ;
1673 static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1675 if (vs->fidp->fs.dir == NULL) {
1679 vs->fidp->fid_type = P9_FID_DIR;
1680 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1683 complete_pdu(s, vs->pdu, err);
1688 static void v9fs_open_post_getiounit(V9fsState *s, V9fsOpenState *vs)
1691 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
1693 complete_pdu(s, vs->pdu, err);
1697 static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1699 if (vs->fidp->fs.fd == -1) {
1703 vs->fidp->fid_type = P9_FID_FILE;
1704 vs->iounit = get_iounit(s, &vs->fidp->path);
1705 v9fs_open_post_getiounit(s, vs);
1708 complete_pdu(s, vs->pdu, err);
1712 static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1721 stat_to_qid(&vs->stbuf, &vs->qid);
1723 if (S_ISDIR(vs->stbuf.st_mode)) {
1724 vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fidp->path);
1725 v9fs_open_post_opendir(s, vs, err);
1727 if (s->proto_version == V9FS_PROTO_2000L) {
1729 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
1730 /* Ignore direct disk access hint until the server supports it. */
1733 flags = omode_to_uflags(vs->mode);
1735 vs->fidp->fs.fd = v9fs_do_open(s, &vs->fidp->path, flags);
1736 v9fs_open_post_open(s, vs, err);
1740 complete_pdu(s, vs->pdu, err);
1744 static void v9fs_open(void *opaque)
1746 V9fsPDU *pdu = opaque;
1747 V9fsState *s = pdu->s;
1752 vs = qemu_malloc(sizeof(*vs));
1757 if (s->proto_version == V9FS_PROTO_2000L) {
1758 pdu_unmarshal(vs->pdu, vs->offset, "dd", &fid, &vs->mode);
1760 pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1763 vs->fidp = lookup_fid(s, fid);
1764 if (vs->fidp == NULL) {
1769 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
1771 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1773 v9fs_open_post_lstat(s, vs, err);
1776 complete_pdu(s, pdu, err);
1780 static void v9fs_post_lcreate(V9fsState *s, V9fsLcreateState *vs, int err)
1783 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1784 stat_to_qid(&vs->stbuf, &vs->qid);
1785 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid,
1789 vs->fidp->fid_type = P9_FID_NONE;
1791 if (vs->fidp->fs.fd > 0) {
1792 close(vs->fidp->fs.fd);
1796 complete_pdu(s, vs->pdu, err);
1797 v9fs_string_free(&vs->name);
1798 v9fs_string_free(&vs->fullname);
1802 static void v9fs_lcreate_post_get_iounit(V9fsState *s, V9fsLcreateState *vs,
1809 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1812 v9fs_post_lcreate(s, vs, err);
1815 static void v9fs_lcreate_post_do_open2(V9fsState *s, V9fsLcreateState *vs,
1818 if (vs->fidp->fs.fd == -1) {
1822 vs->fidp->fid_type = P9_FID_FILE;
1823 vs->iounit = get_iounit(s, &vs->fullname);
1824 v9fs_lcreate_post_get_iounit(s, vs, err);
1828 v9fs_post_lcreate(s, vs, err);
1831 static void v9fs_lcreate(void *opaque)
1833 V9fsPDU *pdu = opaque;
1834 V9fsState *s = pdu->s;
1835 int32_t dfid, flags, mode;
1837 V9fsLcreateState *vs;
1840 vs = qemu_malloc(sizeof(*vs));
1844 v9fs_string_init(&vs->fullname);
1846 pdu_unmarshal(vs->pdu, vs->offset, "dsddd", &dfid, &vs->name, &flags,
1849 vs->fidp = lookup_fid(s, dfid);
1850 if (vs->fidp == NULL) {
1855 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
1858 /* Ignore direct disk access hint until the server supports it. */
1861 vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
1863 v9fs_lcreate_post_do_open2(s, vs, err);
1867 complete_pdu(s, vs->pdu, err);
1868 v9fs_string_free(&vs->name);
1872 static void v9fs_post_do_fsync(V9fsState *s, V9fsPDU *pdu, int err)
1877 complete_pdu(s, pdu, err);
1880 static void v9fs_fsync(void *opaque)
1882 V9fsPDU *pdu = opaque;
1883 V9fsState *s = pdu->s;
1890 pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1891 fidp = lookup_fid(s, fid);
1894 v9fs_post_do_fsync(s, pdu, err);
1897 err = v9fs_do_fsync(s, fidp->fs.fd, datasync);
1898 v9fs_post_do_fsync(s, pdu, err);
1901 static void v9fs_clunk(void *opaque)
1903 V9fsPDU *pdu = opaque;
1904 V9fsState *s = pdu->s;
1909 pdu_unmarshal(pdu, offset, "d", &fid);
1911 err = free_fid(s, fid);
1919 complete_pdu(s, pdu, err);
1922 static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1924 static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1929 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1930 vs->offset += vs->count;
1933 complete_pdu(s, vs->pdu, err);
1934 v9fs_stat_free(&vs->v9stat);
1935 v9fs_string_free(&vs->name);
1940 static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1947 err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1952 vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1954 if ((vs->len != (vs->v9stat.size + 2)) ||
1955 ((vs->count + vs->len) > vs->max_count)) {
1956 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1957 v9fs_read_post_seekdir(s, vs, err);
1960 vs->count += vs->len;
1961 v9fs_stat_free(&vs->v9stat);
1962 v9fs_string_free(&vs->name);
1963 vs->dir_pos = vs->dent->d_off;
1964 v9fs_co_readdir(s, vs->fidp, &vs->dent);
1965 v9fs_read_post_readdir(s, vs, err);
1968 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1969 v9fs_read_post_seekdir(s, vs, err);
1974 static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1977 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1978 v9fs_string_init(&vs->name);
1979 v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1981 err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1982 v9fs_read_post_dir_lstat(s, vs, err);
1986 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1987 vs->offset += vs->count;
1989 complete_pdu(s, vs->pdu, err);
1994 static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1996 v9fs_co_readdir(s, vs->fidp, &vs->dent);
1997 v9fs_read_post_readdir(s, vs, err);
2001 static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
2004 vs->dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
2005 v9fs_read_post_telldir(s, vs, err);
2009 static void v9fs_read_post_preadv(V9fsState *s, V9fsReadState *vs, ssize_t err)
2012 /* IO error return the error */
2016 vs->total += vs->len;
2017 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
2018 if (vs->total < vs->count && vs->len > 0) {
2021 print_sg(vs->sg, vs->cnt);
2023 vs->len = v9fs_do_preadv(s, vs->fidp->fs.fd, vs->sg, vs->cnt,
2028 } while (vs->len == -1 && errno == EINTR);
2029 if (vs->len == -1) {
2032 v9fs_read_post_preadv(s, vs, err);
2035 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
2036 vs->offset += vs->count;
2040 complete_pdu(s, vs->pdu, err);
2044 static void v9fs_xattr_read(V9fsState *s, V9fsReadState *vs)
2050 xattr_len = vs->fidp->fs.xattr.len;
2051 read_count = xattr_len - vs->off;
2052 if (read_count > vs->count) {
2053 read_count = vs->count;
2054 } else if (read_count < 0) {
2056 * read beyond XATTR value
2060 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", read_count);
2061 vs->offset += pdu_pack(vs->pdu, vs->offset,
2062 ((char *)vs->fidp->fs.xattr.value) + vs->off,
2065 complete_pdu(s, vs->pdu, err);
2069 static void v9fs_read(void *opaque)
2071 V9fsPDU *pdu = opaque;
2072 V9fsState *s = pdu->s;
2077 vs = qemu_malloc(sizeof(*vs));
2084 pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count);
2086 vs->fidp = lookup_fid(s, fid);
2087 if (vs->fidp == NULL) {
2092 if (vs->fidp->fid_type == P9_FID_DIR) {
2093 vs->max_count = vs->count;
2096 v9fs_do_rewinddir(s, vs->fidp->fs.dir);
2098 v9fs_read_post_rewinddir(s, vs, err);
2100 } else if (vs->fidp->fid_type == P9_FID_FILE) {
2102 pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
2103 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
2104 if (vs->total <= vs->count) {
2105 vs->len = v9fs_do_preadv(s, vs->fidp->fs.fd, vs->sg, vs->cnt,
2111 v9fs_read_post_preadv(s, vs, err);
2114 } else if (vs->fidp->fid_type == P9_FID_XATTR) {
2115 v9fs_xattr_read(s, vs);
2121 complete_pdu(s, pdu, err);
2125 static size_t v9fs_readdir_data_size(V9fsString *name)
2128 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
2129 * size of type (1) + size of name.size (2) + strlen(name.data)
2131 return 24 + v9fs_string_size(name);
2134 static int v9fs_do_readdir(V9fsState *s, V9fsPDU *pdu,
2135 V9fsFidState *fidp, int32_t max_count)
2142 off_t saved_dir_pos;
2143 struct dirent *dent;
2145 /* save the directory position */
2146 saved_dir_pos = v9fs_co_telldir(s, fidp);
2147 if (saved_dir_pos < 0) {
2148 return saved_dir_pos;
2151 err = v9fs_co_readdir(s, fidp, &dent);
2155 v9fs_string_init(&name);
2156 v9fs_string_sprintf(&name, "%s", dent->d_name);
2157 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
2158 /* Ran out of buffer. Set dir back to old position and return */
2159 v9fs_co_seekdir(s, fidp, saved_dir_pos);
2160 v9fs_string_free(&name);
2164 * Fill up just the path field of qid because the client uses
2165 * only that. To fill the entire qid structure we will have
2166 * to stat each dirent found, which is expensive
2168 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
2169 memcpy(&qid.path, &dent->d_ino, size);
2170 /* Fill the other fields with dummy values */
2174 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2175 len = pdu_marshal(pdu, 11 + count, "Qqbs",
2177 dent->d_type, &name);
2179 v9fs_string_free(&name);
2180 saved_dir_pos = dent->d_off;
2188 static void v9fs_readdir(void *opaque)
2194 int64_t initial_offset;
2195 int32_t count, max_count;
2196 V9fsPDU *pdu = opaque;
2197 V9fsState *s = pdu->s;
2199 pdu_unmarshal(pdu, offset, "dqd", &fid, &initial_offset, &max_count);
2201 fidp = lookup_fid(s, fid);
2202 if (fidp == NULL || !fidp->fs.dir) {
2206 if (initial_offset == 0) {
2207 v9fs_co_rewinddir(s, fidp);
2209 v9fs_co_seekdir(s, fidp, initial_offset);
2211 count = v9fs_do_readdir(s, pdu, fidp, max_count);
2217 retval += pdu_marshal(pdu, offset, "d", count);
2220 complete_pdu(s, pdu, retval);
2223 static void v9fs_write_post_pwritev(V9fsState *s, V9fsWriteState *vs,
2227 /* IO error return the error */
2231 vs->total += vs->len;
2232 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
2233 if (vs->total < vs->count && vs->len > 0) {
2236 print_sg(vs->sg, vs->cnt);
2238 vs->len = v9fs_do_pwritev(s, vs->fidp->fs.fd, vs->sg, vs->cnt,
2243 } while (vs->len == -1 && errno == EINTR);
2244 if (vs->len == -1) {
2247 v9fs_write_post_pwritev(s, vs, err);
2250 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
2253 complete_pdu(s, vs->pdu, err);
2257 static void v9fs_xattr_write(V9fsState *s, V9fsWriteState *vs)
2264 xattr_len = vs->fidp->fs.xattr.len;
2265 write_count = xattr_len - vs->off;
2266 if (write_count > vs->count) {
2267 write_count = vs->count;
2268 } else if (write_count < 0) {
2270 * write beyond XATTR value len specified in
2276 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", write_count);
2278 vs->fidp->fs.xattr.copied_len += write_count;
2280 * Now copy the content from sg list
2282 for (i = 0; i < vs->cnt; i++) {
2283 if (write_count > vs->sg[i].iov_len) {
2284 to_copy = vs->sg[i].iov_len;
2286 to_copy = write_count;
2288 memcpy((char *)vs->fidp->fs.xattr.value + vs->off,
2289 vs->sg[i].iov_base, to_copy);
2290 /* updating vs->off since we are not using below */
2292 write_count -= to_copy;
2295 complete_pdu(s, vs->pdu, err);
2299 static void v9fs_write(void *opaque)
2301 V9fsPDU *pdu = opaque;
2302 V9fsState *s = pdu->s;
2307 vs = qemu_malloc(sizeof(*vs));
2315 pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
2318 vs->fidp = lookup_fid(s, fid);
2319 if (vs->fidp == NULL) {
2324 if (vs->fidp->fid_type == P9_FID_FILE) {
2325 if (vs->fidp->fs.fd == -1) {
2329 } else if (vs->fidp->fid_type == P9_FID_XATTR) {
2331 * setxattr operation
2333 v9fs_xattr_write(s, vs);
2339 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
2340 if (vs->total <= vs->count) {
2341 vs->len = v9fs_do_pwritev(s, vs->fidp->fs.fd, vs->sg, vs->cnt, vs->off);
2346 v9fs_write_post_pwritev(s, vs, err);
2350 complete_pdu(s, vs->pdu, err);
2354 static void v9fs_create_post_getiounit(V9fsState *s, V9fsCreateState *vs)
2357 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
2358 stat_to_qid(&vs->stbuf, &vs->qid);
2360 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
2363 complete_pdu(s, vs->pdu, err);
2364 v9fs_string_free(&vs->name);
2365 v9fs_string_free(&vs->extension);
2366 v9fs_string_free(&vs->fullname);
2370 static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
2373 vs->iounit = get_iounit(s, &vs->fidp->path);
2374 v9fs_create_post_getiounit(s, vs);
2378 complete_pdu(s, vs->pdu, err);
2379 v9fs_string_free(&vs->name);
2380 v9fs_string_free(&vs->extension);
2381 v9fs_string_free(&vs->fullname);
2385 static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
2390 v9fs_post_create(s, vs, err);
2393 static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
2396 if (!vs->fidp->fs.dir) {
2399 vs->fidp->fid_type = P9_FID_DIR;
2400 v9fs_post_create(s, vs, err);
2403 static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
2411 vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fullname);
2412 v9fs_create_post_opendir(s, vs, err);
2416 v9fs_post_create(s, vs, err);
2419 static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
2426 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2427 v9fs_create_post_dir_lstat(s, vs, err);
2431 v9fs_post_create(s, vs, err);
2434 static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
2437 vs->fidp->fid_type = P9_FID_NONE;
2438 close(vs->fidp->fs.fd);
2441 v9fs_post_create(s, vs, err);
2445 static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
2447 if (vs->fidp->fs.fd == -1) {
2451 vs->fidp->fid_type = P9_FID_FILE;
2452 err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
2453 v9fs_create_post_fstat(s, vs, err);
2458 v9fs_post_create(s, vs, err);
2462 static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
2465 if (err == 0 || errno != ENOENT) {
2470 if (vs->perm & P9_STAT_MODE_DIR) {
2471 err = v9fs_do_mkdir(s, vs->fullname.data, vs->perm & 0777,
2473 v9fs_create_post_mkdir(s, vs, err);
2474 } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
2475 err = v9fs_do_symlink(s, vs->fidp, vs->extension.data,
2476 vs->fullname.data, -1);
2477 v9fs_create_post_perms(s, vs, err);
2478 } else if (vs->perm & P9_STAT_MODE_LINK) {
2479 int32_t nfid = atoi(vs->extension.data);
2480 V9fsFidState *nfidp = lookup_fid(s, nfid);
2481 if (nfidp == NULL) {
2483 v9fs_post_create(s, vs, err);
2485 err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
2486 v9fs_create_post_perms(s, vs, err);
2487 } else if (vs->perm & P9_STAT_MODE_DEVICE) {
2489 uint32_t major, minor;
2492 if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
2495 v9fs_post_create(s, vs, err);
2507 v9fs_post_create(s, vs, err);
2510 nmode |= vs->perm & 0777;
2511 err = v9fs_do_mknod(s, vs->fullname.data, nmode,
2512 makedev(major, minor), vs->fidp->uid, -1);
2513 v9fs_create_post_perms(s, vs, err);
2514 } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
2515 err = v9fs_do_mknod(s, vs->fullname.data, S_IFIFO | (vs->perm & 0777),
2516 0, vs->fidp->uid, -1);
2517 v9fs_post_create(s, vs, err);
2518 } else if (vs->perm & P9_STAT_MODE_SOCKET) {
2519 err = v9fs_do_mknod(s, vs->fullname.data, S_IFSOCK | (vs->perm & 0777),
2520 0, vs->fidp->uid, -1);
2521 v9fs_post_create(s, vs, err);
2523 vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
2524 -1, omode_to_uflags(vs->mode)|O_CREAT, vs->perm);
2526 v9fs_create_post_open2(s, vs, err);
2532 v9fs_post_create(s, vs, err);
2535 static void v9fs_create(void *opaque)
2537 V9fsPDU *pdu = opaque;
2538 V9fsState *s = pdu->s;
2540 V9fsCreateState *vs;
2543 vs = qemu_malloc(sizeof(*vs));
2547 v9fs_string_init(&vs->fullname);
2549 pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
2550 &vs->perm, &vs->mode, &vs->extension);
2552 vs->fidp = lookup_fid(s, fid);
2553 if (vs->fidp == NULL) {
2558 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
2561 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2562 v9fs_create_post_lstat(s, vs, err);
2566 complete_pdu(s, vs->pdu, err);
2567 v9fs_string_free(&vs->name);
2568 v9fs_string_free(&vs->extension);
2572 static void v9fs_post_symlink(V9fsState *s, V9fsSymlinkState *vs, int err)
2575 stat_to_qid(&vs->stbuf, &vs->qid);
2576 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
2581 complete_pdu(s, vs->pdu, err);
2582 v9fs_string_free(&vs->name);
2583 v9fs_string_free(&vs->symname);
2584 v9fs_string_free(&vs->fullname);
2588 static void v9fs_symlink_post_do_symlink(V9fsState *s, V9fsSymlinkState *vs,
2594 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2596 v9fs_post_symlink(s, vs, err);
2599 static void v9fs_symlink(void *opaque)
2601 V9fsPDU *pdu = opaque;
2602 V9fsState *s = pdu->s;
2604 V9fsSymlinkState *vs;
2608 vs = qemu_malloc(sizeof(*vs));
2612 v9fs_string_init(&vs->fullname);
2614 pdu_unmarshal(vs->pdu, vs->offset, "dssd", &dfid, &vs->name,
2615 &vs->symname, &gid);
2617 vs->dfidp = lookup_fid(s, dfid);
2618 if (vs->dfidp == NULL) {
2623 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->dfidp->path.data,
2625 err = v9fs_do_symlink(s, vs->dfidp, vs->symname.data,
2626 vs->fullname.data, gid);
2627 v9fs_symlink_post_do_symlink(s, vs, err);
2631 complete_pdu(s, vs->pdu, err);
2632 v9fs_string_free(&vs->name);
2633 v9fs_string_free(&vs->symname);
2637 static void v9fs_flush(void *opaque)
2639 V9fsPDU *pdu = opaque;
2640 V9fsState *s = pdu->s;
2641 /* A nop call with no return */
2642 complete_pdu(s, pdu, 7);
2646 static void v9fs_link(void *opaque)
2648 V9fsPDU *pdu = opaque;
2649 V9fsState *s = pdu->s;
2650 int32_t dfid, oldfid;
2651 V9fsFidState *dfidp, *oldfidp;
2652 V9fsString name, fullname;
2656 v9fs_string_init(&fullname);
2658 pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2660 dfidp = lookup_fid(s, dfid);
2661 if (dfidp == NULL) {
2666 oldfidp = lookup_fid(s, oldfid);
2667 if (oldfidp == NULL) {
2672 v9fs_string_sprintf(&fullname, "%s/%s", dfidp->path.data, name.data);
2674 err = v9fs_do_link(s, &oldfidp->path, &fullname);
2678 v9fs_string_free(&fullname);
2681 v9fs_string_free(&name);
2682 complete_pdu(s, pdu, err);
2685 static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
2694 /* For TREMOVE we need to clunk the fid even on failed remove */
2695 free_fid(s, vs->fidp->fid);
2697 complete_pdu(s, vs->pdu, err);
2701 static void v9fs_remove(void *opaque)
2703 V9fsPDU *pdu = opaque;
2704 V9fsState *s = pdu->s;
2706 V9fsRemoveState *vs;
2709 vs = qemu_malloc(sizeof(*vs));
2713 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
2715 vs->fidp = lookup_fid(s, fid);
2716 if (vs->fidp == NULL) {
2721 err = v9fs_do_remove(s, &vs->fidp->path);
2722 v9fs_remove_post_remove(s, vs, err);
2726 complete_pdu(s, pdu, err);
2730 static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
2739 v9fs_stat_free(&vs->v9stat);
2740 complete_pdu(s, vs->pdu, err);
2744 static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
2749 if (vs->v9stat.length != -1) {
2750 if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
2754 v9fs_wstat_post_truncate(s, vs, err);
2758 v9fs_stat_free(&vs->v9stat);
2759 complete_pdu(s, vs->pdu, err);
2763 static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs)
2766 char *old_name, *new_name;
2769 if (vs->newdirfid != -1) {
2770 V9fsFidState *dirfidp;
2771 dirfidp = lookup_fid(s, vs->newdirfid);
2773 if (dirfidp == NULL) {
2778 BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2780 new_name = qemu_mallocz(dirfidp->path.size + vs->name.size + 2);
2782 strcpy(new_name, dirfidp->path.data);
2783 strcat(new_name, "/");
2784 strcat(new_name + dirfidp->path.size, vs->name.data);
2786 old_name = vs->fidp->path.data;
2787 end = strrchr(old_name, '/');
2793 new_name = qemu_mallocz(end - old_name + vs->name.size + 1);
2795 strncat(new_name, old_name, end - old_name);
2796 strncat(new_name + (end - old_name), vs->name.data, vs->name.size);
2799 v9fs_string_free(&vs->name);
2800 vs->name.data = qemu_strdup(new_name);
2801 vs->name.size = strlen(new_name);
2803 if (strcmp(new_name, vs->fidp->path.data) != 0) {
2804 if (v9fs_do_rename(s, &vs->fidp->path, &vs->name)) {
2809 * Fixup fid's pointing to the old name to
2810 * start pointing to the new name
2812 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2813 if (vs->fidp == fidp) {
2815 * we replace name of this fid towards the end so
2816 * that our below v9fs_path_is_ancestor check will
2821 if (v9fs_path_is_ancestor(&vs->fidp->path, &fidp->path)) {
2822 /* replace the name */
2823 v9fs_fix_path(&fidp->path, &vs->name,
2824 strlen(vs->fidp->path.data));
2827 v9fs_string_copy(&vs->fidp->path, &vs->name);
2831 v9fs_string_free(&vs->name);
2835 static void v9fs_rename_post_rename(V9fsState *s, V9fsRenameState *vs, int err)
2837 complete_pdu(s, vs->pdu, err);
2841 static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
2847 if (vs->v9stat.name.size != 0) {
2848 V9fsRenameState *vr;
2850 vr = qemu_mallocz(sizeof(V9fsRenameState));
2853 vr->fidp = vs->fidp;
2854 vr->offset = vs->offset;
2855 vr->name.size = vs->v9stat.name.size;
2856 vr->name.data = qemu_strdup(vs->v9stat.name.data);
2858 err = v9fs_complete_rename(s, vr);
2861 v9fs_wstat_post_rename(s, vs, err);
2865 v9fs_stat_free(&vs->v9stat);
2866 complete_pdu(s, vs->pdu, err);
2870 static void v9fs_rename(void *opaque)
2872 V9fsPDU *pdu = opaque;
2873 V9fsState *s = pdu->s;
2875 V9fsRenameState *vs;
2878 vs = qemu_malloc(sizeof(*vs));
2882 pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &vs->newdirfid, &vs->name);
2884 vs->fidp = lookup_fid(s, fid);
2885 if (vs->fidp == NULL) {
2890 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
2892 err = v9fs_complete_rename(s, vs);
2893 v9fs_rename_post_rename(s, vs, err);
2896 complete_pdu(s, vs->pdu, err);
2900 static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2906 if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) {
2907 if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2908 vs->v9stat.n_gid)) {
2912 v9fs_wstat_post_chown(s, vs, err);
2916 v9fs_stat_free(&vs->v9stat);
2917 complete_pdu(s, vs->pdu, err);
2921 static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2927 if (vs->v9stat.mtime != -1 || vs->v9stat.atime != -1) {
2928 struct timespec times[2];
2929 if (vs->v9stat.atime != -1) {
2930 times[0].tv_sec = vs->v9stat.atime;
2931 times[0].tv_nsec = 0;
2933 times[0].tv_nsec = UTIME_OMIT;
2935 if (vs->v9stat.mtime != -1) {
2936 times[1].tv_sec = vs->v9stat.mtime;
2937 times[1].tv_nsec = 0;
2939 times[1].tv_nsec = UTIME_OMIT;
2942 if (v9fs_do_utimensat(s, &vs->fidp->path, times)) {
2947 v9fs_wstat_post_utime(s, vs, err);
2951 v9fs_stat_free(&vs->v9stat);
2952 complete_pdu(s, vs->pdu, err);
2956 static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2961 v9fs_stat_free(&vs->v9stat);
2962 complete_pdu(s, vs->pdu, err);
2966 static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2975 v9_mode = stat_to_v9mode(&vs->stbuf);
2977 if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2978 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2979 /* Attempting to change the type */
2984 if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2985 &vs->v9stat.extension))) {
2988 v9fs_wstat_post_chmod(s, vs, err);
2992 v9fs_stat_free(&vs->v9stat);
2993 complete_pdu(s, vs->pdu, err);
2997 static void v9fs_wstat(void *opaque)
2999 V9fsPDU *pdu = opaque;
3000 V9fsState *s = pdu->s;
3005 vs = qemu_malloc(sizeof(*vs));
3009 pdu_unmarshal(pdu, vs->offset, "dwS", &fid, &vs->unused, &vs->v9stat);
3011 vs->fidp = lookup_fid(s, fid);
3012 if (vs->fidp == NULL) {
3017 /* do we need to sync the file? */
3018 if (donttouch_stat(&vs->v9stat)) {
3019 err = v9fs_do_fsync(s, vs->fidp->fs.fd, 0);
3020 v9fs_wstat_post_fsync(s, vs, err);
3024 if (vs->v9stat.mode != -1) {
3025 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
3026 v9fs_wstat_post_lstat(s, vs, err);
3030 v9fs_wstat_post_chmod(s, vs, err);
3034 v9fs_stat_free(&vs->v9stat);
3035 complete_pdu(s, vs->pdu, err);
3039 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
3051 int32_t bsize_factor;
3054 * compute bsize factor based on host file system block size
3057 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
3058 if (!bsize_factor) {
3061 f_type = stbuf->f_type;
3062 f_bsize = stbuf->f_bsize;
3063 f_bsize *= bsize_factor;
3065 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3066 * adjust(divide) the number of blocks, free blocks and available
3067 * blocks by bsize factor
3069 f_blocks = stbuf->f_blocks/bsize_factor;
3070 f_bfree = stbuf->f_bfree/bsize_factor;
3071 f_bavail = stbuf->f_bavail/bsize_factor;
3072 f_files = stbuf->f_files;
3073 f_ffree = stbuf->f_ffree;
3074 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
3075 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
3076 f_namelen = stbuf->f_namelen;
3078 return pdu_marshal(pdu, offset, "ddqqqqqqd",
3079 f_type, f_bsize, f_blocks, f_bfree,
3080 f_bavail, f_files, f_ffree,
3081 fsid_val, f_namelen);
3084 static void v9fs_statfs(void *opaque)
3090 struct statfs stbuf;
3091 V9fsPDU *pdu = opaque;
3092 V9fsState *s = pdu->s;
3094 pdu_unmarshal(pdu, offset, "d", &fid);
3095 fidp = lookup_fid(s, fid);
3100 retval = v9fs_co_statfs(s, &fidp->path, &stbuf);
3105 retval += v9fs_fill_statfs(s, pdu, &stbuf);
3107 complete_pdu(s, pdu, retval);
3111 static void v9fs_mknod_post_lstat(V9fsState *s, V9fsMkState *vs, int err)
3118 stat_to_qid(&vs->stbuf, &vs->qid);
3119 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
3122 complete_pdu(s, vs->pdu, err);
3123 v9fs_string_free(&vs->fullname);
3124 v9fs_string_free(&vs->name);
3128 static void v9fs_mknod_post_mknod(V9fsState *s, V9fsMkState *vs, int err)
3135 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
3136 v9fs_mknod_post_lstat(s, vs, err);
3139 complete_pdu(s, vs->pdu, err);
3140 v9fs_string_free(&vs->fullname);
3141 v9fs_string_free(&vs->name);
3145 static void v9fs_mknod(void *opaque)
3147 V9fsPDU *pdu = opaque;
3148 V9fsState *s = pdu->s;
3157 vs = qemu_malloc(sizeof(*vs));
3161 v9fs_string_init(&vs->fullname);
3162 pdu_unmarshal(vs->pdu, vs->offset, "dsdddd", &fid, &vs->name, &mode,
3163 &major, &minor, &gid);
3165 fidp = lookup_fid(s, fid);
3171 v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data);
3172 err = v9fs_do_mknod(s, vs->fullname.data, mode, makedev(major, minor),
3174 v9fs_mknod_post_mknod(s, vs, err);
3178 complete_pdu(s, vs->pdu, err);
3179 v9fs_string_free(&vs->fullname);
3180 v9fs_string_free(&vs->name);
3185 * Implement posix byte range locking code
3186 * Server side handling of locking code is very simple, because 9p server in
3187 * QEMU can handle only one client. And most of the lock handling
3188 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3189 * do any thing in * qemu 9p server side lock code path.
3190 * So when a TLOCK request comes, always return success
3193 static void v9fs_lock(void *opaque)
3195 V9fsPDU *pdu = opaque;
3196 V9fsState *s = pdu->s;
3197 int32_t fid, err = 0;
3200 vs = qemu_mallocz(sizeof(*vs));
3204 vs->flock = qemu_malloc(sizeof(*vs->flock));
3205 pdu_unmarshal(vs->pdu, vs->offset, "dbdqqds", &fid, &vs->flock->type,
3206 &vs->flock->flags, &vs->flock->start, &vs->flock->length,
3207 &vs->flock->proc_id, &vs->flock->client_id);
3209 vs->status = P9_LOCK_ERROR;
3211 /* We support only block flag now (that too ignored currently) */
3212 if (vs->flock->flags & ~P9_LOCK_FLAGS_BLOCK) {
3216 vs->fidp = lookup_fid(s, fid);
3217 if (vs->fidp == NULL) {
3222 err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
3227 vs->status = P9_LOCK_SUCCESS;
3229 vs->offset += pdu_marshal(vs->pdu, vs->offset, "b", vs->status);
3230 complete_pdu(s, vs->pdu, err);
3231 qemu_free(vs->flock);
3236 * When a TGETLOCK request comes, always return success because all lock
3237 * handling is done by client's VFS layer.
3240 static void v9fs_getlock(void *opaque)
3242 V9fsPDU *pdu = opaque;
3243 V9fsState *s = pdu->s;
3244 int32_t fid, err = 0;
3245 V9fsGetlockState *vs;
3247 vs = qemu_mallocz(sizeof(*vs));
3251 vs->glock = qemu_malloc(sizeof(*vs->glock));
3252 pdu_unmarshal(vs->pdu, vs->offset, "dbqqds", &fid, &vs->glock->type,
3253 &vs->glock->start, &vs->glock->length, &vs->glock->proc_id,
3254 &vs->glock->client_id);
3256 vs->fidp = lookup_fid(s, fid);
3257 if (vs->fidp == NULL) {
3262 err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
3267 vs->glock->type = F_UNLCK;
3268 vs->offset += pdu_marshal(vs->pdu, vs->offset, "bqqds", vs->glock->type,
3269 vs->glock->start, vs->glock->length, vs->glock->proc_id,
3270 &vs->glock->client_id);
3272 complete_pdu(s, vs->pdu, err);
3273 qemu_free(vs->glock);
3277 static void v9fs_mkdir_post_lstat(V9fsState *s, V9fsMkState *vs, int err)
3284 stat_to_qid(&vs->stbuf, &vs->qid);
3285 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
3288 complete_pdu(s, vs->pdu, err);
3289 v9fs_string_free(&vs->fullname);
3290 v9fs_string_free(&vs->name);
3294 static void v9fs_mkdir_post_mkdir(V9fsState *s, V9fsMkState *vs, int err)
3301 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
3302 v9fs_mkdir_post_lstat(s, vs, err);
3305 complete_pdu(s, vs->pdu, err);
3306 v9fs_string_free(&vs->fullname);
3307 v9fs_string_free(&vs->name);
3311 static void v9fs_mkdir(void *opaque)
3313 V9fsPDU *pdu = opaque;
3314 V9fsState *s = pdu->s;
3322 vs = qemu_malloc(sizeof(*vs));
3326 v9fs_string_init(&vs->fullname);
3327 pdu_unmarshal(vs->pdu, vs->offset, "dsdd", &fid, &vs->name, &mode,
3330 fidp = lookup_fid(s, fid);
3336 v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data);
3337 err = v9fs_do_mkdir(s, vs->fullname.data, mode, fidp->uid, gid);
3338 v9fs_mkdir_post_mkdir(s, vs, err);
3342 complete_pdu(s, vs->pdu, err);
3343 v9fs_string_free(&vs->fullname);
3344 v9fs_string_free(&vs->name);
3348 static void v9fs_post_xattr_getvalue(V9fsState *s, V9fsXattrState *vs, int err)
3353 free_fid(s, vs->xattr_fidp->fid);
3356 vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size);
3359 complete_pdu(s, vs->pdu, err);
3360 v9fs_string_free(&vs->name);
3365 static void v9fs_post_xattr_check(V9fsState *s, V9fsXattrState *vs, ssize_t err)
3369 free_fid(s, vs->xattr_fidp->fid);
3373 * Read the xattr value
3375 vs->xattr_fidp->fs.xattr.len = vs->size;
3376 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3377 vs->xattr_fidp->fs.xattr.copied_len = -1;
3379 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3380 err = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path,
3381 &vs->name, vs->xattr_fidp->fs.xattr.value,
3382 vs->xattr_fidp->fs.xattr.len);
3384 v9fs_post_xattr_getvalue(s, vs, err);
3387 complete_pdu(s, vs->pdu, err);
3388 v9fs_string_free(&vs->name);
3392 static void v9fs_post_lxattr_getvalue(V9fsState *s,
3393 V9fsXattrState *vs, int err)
3397 free_fid(s, vs->xattr_fidp->fid);
3400 vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size);
3403 complete_pdu(s, vs->pdu, err);
3404 v9fs_string_free(&vs->name);
3409 static void v9fs_post_lxattr_check(V9fsState *s,
3410 V9fsXattrState *vs, ssize_t err)
3414 free_fid(s, vs->xattr_fidp->fid);
3418 * Read the xattr value
3420 vs->xattr_fidp->fs.xattr.len = vs->size;
3421 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3422 vs->xattr_fidp->fs.xattr.copied_len = -1;
3424 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3425 err = v9fs_do_llistxattr(s, &vs->xattr_fidp->path,
3426 vs->xattr_fidp->fs.xattr.value,
3427 vs->xattr_fidp->fs.xattr.len);
3429 v9fs_post_lxattr_getvalue(s, vs, err);
3432 complete_pdu(s, vs->pdu, err);
3433 v9fs_string_free(&vs->name);
3437 static void v9fs_xattrwalk(void *opaque)
3439 V9fsPDU *pdu = opaque;
3440 V9fsState *s = pdu->s;
3443 int32_t fid, newfid;
3445 vs = qemu_malloc(sizeof(*vs));
3449 pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &newfid, &vs->name);
3450 vs->file_fidp = lookup_fid(s, fid);
3451 if (vs->file_fidp == NULL) {
3456 vs->xattr_fidp = alloc_fid(s, newfid);
3457 if (vs->xattr_fidp == NULL) {
3462 v9fs_string_copy(&vs->xattr_fidp->path, &vs->file_fidp->path);
3463 if (vs->name.data[0] == 0) {
3465 * listxattr request. Get the size first
3467 vs->size = v9fs_do_llistxattr(s, &vs->xattr_fidp->path,
3472 v9fs_post_lxattr_check(s, vs, err);
3476 * specific xattr fid. We check for xattr
3477 * presence also collect the xattr size
3479 vs->size = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path,
3480 &vs->name, NULL, 0);
3484 v9fs_post_xattr_check(s, vs, err);
3488 complete_pdu(s, vs->pdu, err);
3489 v9fs_string_free(&vs->name);
3493 static void v9fs_xattrcreate(void *opaque)
3495 V9fsPDU *pdu = opaque;
3496 V9fsState *s = pdu->s;
3502 vs = qemu_malloc(sizeof(*vs));
3506 pdu_unmarshal(vs->pdu, vs->offset, "dsqd",
3507 &fid, &vs->name, &vs->size, &flags);
3509 vs->file_fidp = lookup_fid(s, fid);
3510 if (vs->file_fidp == NULL) {
3515 /* Make the file fid point to xattr */
3516 vs->xattr_fidp = vs->file_fidp;
3517 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3518 vs->xattr_fidp->fs.xattr.copied_len = 0;
3519 vs->xattr_fidp->fs.xattr.len = vs->size;
3520 vs->xattr_fidp->fs.xattr.flags = flags;
3521 v9fs_string_init(&vs->xattr_fidp->fs.xattr.name);
3522 v9fs_string_copy(&vs->xattr_fidp->fs.xattr.name, &vs->name);
3524 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3526 vs->xattr_fidp->fs.xattr.value = NULL;
3529 complete_pdu(s, vs->pdu, err);
3530 v9fs_string_free(&vs->name);
3534 static void v9fs_readlink(void *opaque)
3536 V9fsPDU *pdu = opaque;
3543 pdu_unmarshal(pdu, offset, "d", &fid);
3544 fidp = lookup_fid(pdu->s, fid);
3550 v9fs_string_init(&target);
3551 err = v9fs_co_readlink(pdu->s, &fidp->path, &target);
3555 offset += pdu_marshal(pdu, offset, "s", &target);
3557 v9fs_string_free(&target);
3559 complete_pdu(pdu->s, pdu, err);
3562 static CoroutineEntry *pdu_co_handlers[] = {
3563 [P9_TREADDIR] = v9fs_readdir,
3564 [P9_TSTATFS] = v9fs_statfs,
3565 [P9_TGETATTR] = v9fs_getattr,
3566 [P9_TSETATTR] = v9fs_setattr,
3567 [P9_TXATTRWALK] = v9fs_xattrwalk,
3568 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3569 [P9_TMKNOD] = v9fs_mknod,
3570 [P9_TRENAME] = v9fs_rename,
3571 [P9_TLOCK] = v9fs_lock,
3572 [P9_TGETLOCK] = v9fs_getlock,
3573 [P9_TREADLINK] = v9fs_readlink,
3574 [P9_TMKDIR] = v9fs_mkdir,
3575 [P9_TVERSION] = v9fs_version,
3576 [P9_TLOPEN] = v9fs_open,
3577 [P9_TATTACH] = v9fs_attach,
3578 [P9_TSTAT] = v9fs_stat,
3579 [P9_TWALK] = v9fs_walk,
3580 [P9_TCLUNK] = v9fs_clunk,
3581 [P9_TFSYNC] = v9fs_fsync,
3582 [P9_TOPEN] = v9fs_open,
3583 [P9_TREAD] = v9fs_read,
3585 [P9_TAUTH] = v9fs_auth,
3587 [P9_TFLUSH] = v9fs_flush,
3588 [P9_TLINK] = v9fs_link,
3589 [P9_TSYMLINK] = v9fs_symlink,
3590 [P9_TCREATE] = v9fs_create,
3591 [P9_TLCREATE] = v9fs_lcreate,
3592 [P9_TWRITE] = v9fs_write,
3593 [P9_TWSTAT] = v9fs_wstat,
3594 [P9_TREMOVE] = v9fs_remove,
3597 static void v9fs_op_not_supp(void *opaque)
3599 V9fsPDU *pdu = opaque;
3600 complete_pdu(pdu->s, pdu, -EOPNOTSUPP);
3603 static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
3606 CoroutineEntry *handler;
3611 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3612 (pdu_co_handlers[pdu->id] == NULL)) {
3613 handler = v9fs_op_not_supp;
3615 handler = pdu_co_handlers[pdu->id];
3617 co = qemu_coroutine_create(handler);
3618 qemu_coroutine_enter(co, pdu);
3621 void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
3623 V9fsState *s = (V9fsState *)vdev;
3627 while ((pdu = alloc_pdu(s)) &&
3628 (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
3631 BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
3632 BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
3634 ptr = pdu->elem.out_sg[0].iov_base;
3636 memcpy(&pdu->size, ptr, 4);
3638 memcpy(&pdu->tag, ptr + 5, 2);