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 struct dirent *v9fs_do_readdir(V9fsState *s, DIR *dir)
117 return s->ops->readdir(&s->ctx, dir);
120 static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off)
122 return s->ops->seekdir(&s->ctx, dir, off);
125 static int v9fs_do_preadv(V9fsState *s, int fd, const struct iovec *iov,
126 int iovcnt, int64_t offset)
128 return s->ops->preadv(&s->ctx, fd, iov, iovcnt, offset);
131 static int v9fs_do_pwritev(V9fsState *s, int fd, const struct iovec *iov,
132 int iovcnt, int64_t offset)
134 return s->ops->pwritev(&s->ctx, fd, iov, iovcnt, offset);
137 static int v9fs_do_chmod(V9fsState *s, V9fsString *path, mode_t mode)
142 return s->ops->chmod(&s->ctx, path->data, &cred);
145 static int v9fs_do_mknod(V9fsState *s, char *name,
146 mode_t mode, dev_t dev, uid_t uid, gid_t gid)
154 return s->ops->mknod(&s->ctx, name, &cred);
157 static int v9fs_do_mkdir(V9fsState *s, char *name, mode_t mode,
158 uid_t uid, gid_t gid)
167 return s->ops->mkdir(&s->ctx, name, &cred);
170 static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
172 return s->ops->fstat(&s->ctx, fd, stbuf);
175 static int v9fs_do_open2(V9fsState *s, char *fullname, uid_t uid, gid_t gid,
183 cred.fc_mode = mode & 07777;
185 return s->ops->open2(&s->ctx, fullname, flags, &cred);
188 static int v9fs_do_symlink(V9fsState *s, V9fsFidState *fidp,
189 const char *oldpath, const char *newpath, gid_t gid)
193 cred.fc_uid = fidp->uid;
197 return s->ops->symlink(&s->ctx, oldpath, newpath, &cred);
200 static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
202 return s->ops->link(&s->ctx, oldpath->data, newpath->data);
205 static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
207 return s->ops->truncate(&s->ctx, path->data, size);
210 static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath,
213 return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
216 static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
223 return s->ops->chown(&s->ctx, path->data, &cred);
226 static int v9fs_do_utimensat(V9fsState *s, V9fsString *path,
227 const struct timespec times[2])
229 return s->ops->utimensat(&s->ctx, path->data, times);
232 static int v9fs_do_remove(V9fsState *s, V9fsString *path)
234 return s->ops->remove(&s->ctx, path->data);
237 static int v9fs_do_fsync(V9fsState *s, int fd, int datasync)
239 return s->ops->fsync(&s->ctx, fd, datasync);
242 static int v9fs_do_statfs(V9fsState *s, V9fsString *path, struct statfs *stbuf)
244 return s->ops->statfs(&s->ctx, path->data, stbuf);
247 static ssize_t v9fs_do_lgetxattr(V9fsState *s, V9fsString *path,
248 V9fsString *xattr_name,
249 void *value, size_t size)
251 return s->ops->lgetxattr(&s->ctx, path->data,
252 xattr_name->data, value, size);
255 static ssize_t v9fs_do_llistxattr(V9fsState *s, V9fsString *path,
256 void *value, size_t size)
258 return s->ops->llistxattr(&s->ctx, path->data,
262 static int v9fs_do_lsetxattr(V9fsState *s, V9fsString *path,
263 V9fsString *xattr_name,
264 void *value, size_t size, int flags)
266 return s->ops->lsetxattr(&s->ctx, path->data,
267 xattr_name->data, value, size, flags);
270 static int v9fs_do_lremovexattr(V9fsState *s, V9fsString *path,
271 V9fsString *xattr_name)
273 return s->ops->lremovexattr(&s->ctx, path->data,
278 static void v9fs_string_init(V9fsString *str)
284 static void v9fs_string_free(V9fsString *str)
286 qemu_free(str->data);
291 static void v9fs_string_null(V9fsString *str)
293 v9fs_string_free(str);
296 static int number_to_string(void *arg, char type)
298 unsigned int ret = 0;
302 unsigned int num = *(unsigned int *)arg;
311 unsigned long num = *(unsigned long *)arg;
319 printf("Number_to_string: Unknown number format\n");
326 static int GCC_FMT_ATTR(2, 0)
327 v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
330 char *iter = (char *)fmt;
334 unsigned int arg_uint;
335 unsigned long arg_ulong;
337 /* Find the number of %'s that denotes an argument */
338 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
343 len = strlen(fmt) - 2*nr_args;
353 /* Now parse the format string */
354 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
358 arg_uint = va_arg(ap2, unsigned int);
359 len += number_to_string((void *)&arg_uint, 'u');
362 if (*++iter == 'u') {
363 arg_ulong = va_arg(ap2, unsigned long);
364 len += number_to_string((void *)&arg_ulong, 'U');
370 arg_char_ptr = va_arg(ap2, char *);
371 len += strlen(arg_char_ptr);
378 "v9fs_string_alloc_printf:Incorrect format %c", *iter);
385 *strp = qemu_malloc((len + 1) * sizeof(**strp));
387 return vsprintf(*strp, fmt, ap);
390 static void GCC_FMT_ATTR(2, 3)
391 v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
396 v9fs_string_free(str);
399 err = v9fs_string_alloc_printf(&str->data, fmt, ap);
406 static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
408 v9fs_string_free(lhs);
409 v9fs_string_sprintf(lhs, "%s", rhs->data);
413 * Return TRUE if s1 is an ancestor of s2.
415 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
416 * As a special case, We treat s1 as ancestor of s2 if they are same!
418 static int v9fs_path_is_ancestor(V9fsString *s1, V9fsString *s2)
420 if (!strncmp(s1->data, s2->data, s1->size)) {
421 if (s2->data[s1->size] == '\0' || s2->data[s1->size] == '/') {
428 static size_t v9fs_string_size(V9fsString *str)
433 static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
437 for (f = s->fid_list; f; f = f->next) {
446 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
450 f = lookup_fid(s, fid);
455 f = qemu_mallocz(sizeof(V9fsFidState));
458 f->fid_type = P9_FID_NONE;
460 f->next = s->fid_list;
466 static int v9fs_xattr_fid_clunk(V9fsState *s, V9fsFidState *fidp)
470 if (fidp->fs.xattr.copied_len == -1) {
471 /* getxattr/listxattr fid */
475 * if this is fid for setxattr. clunk should
476 * result in setxattr localcall
478 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
479 /* clunk after partial write */
483 if (fidp->fs.xattr.len) {
484 retval = v9fs_do_lsetxattr(s, &fidp->path, &fidp->fs.xattr.name,
485 fidp->fs.xattr.value,
487 fidp->fs.xattr.flags);
489 retval = v9fs_do_lremovexattr(s, &fidp->path, &fidp->fs.xattr.name);
492 v9fs_string_free(&fidp->fs.xattr.name);
494 if (fidp->fs.xattr.value) {
495 qemu_free(fidp->fs.xattr.value);
500 static int free_fid(V9fsState *s, int32_t fid)
503 V9fsFidState **fidpp, *fidp;
505 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
506 if ((*fidpp)->fid == fid) {
511 if (*fidpp == NULL) {
518 if (fidp->fid_type == P9_FID_FILE) {
519 v9fs_do_close(s, fidp->fs.fd);
520 } else if (fidp->fid_type == P9_FID_DIR) {
521 v9fs_do_closedir(s, fidp->fs.dir);
522 } else if (fidp->fid_type == P9_FID_XATTR) {
523 retval = v9fs_xattr_fid_clunk(s, fidp);
525 v9fs_string_free(&fidp->path);
531 #define P9_QID_TYPE_DIR 0x80
532 #define P9_QID_TYPE_SYMLINK 0x02
534 #define P9_STAT_MODE_DIR 0x80000000
535 #define P9_STAT_MODE_APPEND 0x40000000
536 #define P9_STAT_MODE_EXCL 0x20000000
537 #define P9_STAT_MODE_MOUNT 0x10000000
538 #define P9_STAT_MODE_AUTH 0x08000000
539 #define P9_STAT_MODE_TMP 0x04000000
540 #define P9_STAT_MODE_SYMLINK 0x02000000
541 #define P9_STAT_MODE_LINK 0x01000000
542 #define P9_STAT_MODE_DEVICE 0x00800000
543 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
544 #define P9_STAT_MODE_SOCKET 0x00100000
545 #define P9_STAT_MODE_SETUID 0x00080000
546 #define P9_STAT_MODE_SETGID 0x00040000
547 #define P9_STAT_MODE_SETVTX 0x00010000
549 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
550 P9_STAT_MODE_SYMLINK | \
551 P9_STAT_MODE_LINK | \
552 P9_STAT_MODE_DEVICE | \
553 P9_STAT_MODE_NAMED_PIPE | \
556 /* This is the algorithm from ufs in spfs */
557 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
561 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
562 memcpy(&qidp->path, &stbuf->st_ino, size);
563 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
565 if (S_ISDIR(stbuf->st_mode)) {
566 qidp->type |= P9_QID_TYPE_DIR;
568 if (S_ISLNK(stbuf->st_mode)) {
569 qidp->type |= P9_QID_TYPE_SYMLINK;
573 static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
578 err = v9fs_do_lstat(s, &fidp->path, &stbuf);
583 stat_to_qid(&stbuf, qidp);
587 static V9fsPDU *alloc_pdu(V9fsState *s)
591 if (!QLIST_EMPTY(&s->free_list)) {
592 pdu = QLIST_FIRST(&s->free_list);
593 QLIST_REMOVE(pdu, next);
598 static void free_pdu(V9fsState *s, V9fsPDU *pdu)
604 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
608 size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
609 size_t offset, size_t size, int pack)
614 for (i = 0; size && i < sg_count; i++) {
616 if (offset >= sg[i].iov_len) {
618 offset -= sg[i].iov_len;
621 len = MIN(sg[i].iov_len - offset, size);
623 memcpy(sg[i].iov_base + offset, addr, len);
625 memcpy(addr, sg[i].iov_base + offset, len);
640 static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
642 return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
646 static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
649 return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
653 static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
657 struct iovec *src_sg;
661 src_sg = pdu->elem.in_sg;
662 num = pdu->elem.in_num;
664 src_sg = pdu->elem.out_sg;
665 num = pdu->elem.out_num;
669 for (i = 0; i < num; i++) {
671 sg[j].iov_base = src_sg[i].iov_base;
672 sg[j].iov_len = src_sg[i].iov_len;
674 } else if (offset < (src_sg[i].iov_len + pos)) {
675 sg[j].iov_base = src_sg[i].iov_base;
676 sg[j].iov_len = src_sg[i].iov_len;
677 sg[j].iov_base += (offset - pos);
678 sg[j].iov_len -= (offset - pos);
681 pos += src_sg[i].iov_len;
687 static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
689 size_t old_offset = offset;
694 for (i = 0; fmt[i]; i++) {
697 uint8_t *valp = va_arg(ap, uint8_t *);
698 offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
703 valp = va_arg(ap, uint16_t *);
704 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
705 *valp = le16_to_cpu(val);
710 valp = va_arg(ap, uint32_t *);
711 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
712 *valp = le32_to_cpu(val);
717 valp = va_arg(ap, uint64_t *);
718 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
719 *valp = le64_to_cpu(val);
723 struct iovec *iov = va_arg(ap, struct iovec *);
724 int *iovcnt = va_arg(ap, int *);
725 *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
729 V9fsString *str = va_arg(ap, V9fsString *);
730 offset += pdu_unmarshal(pdu, offset, "w", &str->size);
731 /* FIXME: sanity check str->size */
732 str->data = qemu_malloc(str->size + 1);
733 offset += pdu_unpack(str->data, pdu, offset, str->size);
734 str->data[str->size] = 0;
738 V9fsQID *qidp = va_arg(ap, V9fsQID *);
739 offset += pdu_unmarshal(pdu, offset, "bdq",
740 &qidp->type, &qidp->version, &qidp->path);
744 V9fsStat *statp = va_arg(ap, V9fsStat *);
745 offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
746 &statp->size, &statp->type, &statp->dev,
747 &statp->qid, &statp->mode, &statp->atime,
748 &statp->mtime, &statp->length,
749 &statp->name, &statp->uid, &statp->gid,
750 &statp->muid, &statp->extension,
751 &statp->n_uid, &statp->n_gid,
756 V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
757 offset += pdu_unmarshal(pdu, offset, "ddddqqqqq",
758 &iattr->valid, &iattr->mode,
759 &iattr->uid, &iattr->gid, &iattr->size,
760 &iattr->atime_sec, &iattr->atime_nsec,
761 &iattr->mtime_sec, &iattr->mtime_nsec);
771 return offset - old_offset;
774 static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
776 size_t old_offset = offset;
781 for (i = 0; fmt[i]; i++) {
784 uint8_t val = va_arg(ap, int);
785 offset += pdu_pack(pdu, offset, &val, sizeof(val));
790 cpu_to_le16w(&val, va_arg(ap, int));
791 offset += pdu_pack(pdu, offset, &val, sizeof(val));
796 cpu_to_le32w(&val, va_arg(ap, uint32_t));
797 offset += pdu_pack(pdu, offset, &val, sizeof(val));
802 cpu_to_le64w(&val, va_arg(ap, uint64_t));
803 offset += pdu_pack(pdu, offset, &val, sizeof(val));
807 struct iovec *iov = va_arg(ap, struct iovec *);
808 int *iovcnt = va_arg(ap, int *);
809 *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
813 V9fsString *str = va_arg(ap, V9fsString *);
814 offset += pdu_marshal(pdu, offset, "w", str->size);
815 offset += pdu_pack(pdu, offset, str->data, str->size);
819 V9fsQID *qidp = va_arg(ap, V9fsQID *);
820 offset += pdu_marshal(pdu, offset, "bdq",
821 qidp->type, qidp->version, qidp->path);
825 V9fsStat *statp = va_arg(ap, V9fsStat *);
826 offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
827 statp->size, statp->type, statp->dev,
828 &statp->qid, statp->mode, statp->atime,
829 statp->mtime, statp->length, &statp->name,
830 &statp->uid, &statp->gid, &statp->muid,
831 &statp->extension, statp->n_uid,
832 statp->n_gid, statp->n_muid);
836 V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
837 offset += pdu_marshal(pdu, offset, "qQdddqqqqqqqqqqqqqqq",
838 statp->st_result_mask,
839 &statp->qid, statp->st_mode,
840 statp->st_uid, statp->st_gid,
841 statp->st_nlink, statp->st_rdev,
842 statp->st_size, statp->st_blksize, statp->st_blocks,
843 statp->st_atime_sec, statp->st_atime_nsec,
844 statp->st_mtime_sec, statp->st_mtime_nsec,
845 statp->st_ctime_sec, statp->st_ctime_nsec,
846 statp->st_btime_sec, statp->st_btime_nsec,
847 statp->st_gen, statp->st_data_version);
856 return offset - old_offset;
859 static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
861 int8_t id = pdu->id + 1; /* Response */
867 if (s->proto_version != V9FS_PROTO_2000L) {
870 str.data = strerror(err);
871 str.size = strlen(str.data);
873 len += pdu_marshal(pdu, len, "s", &str);
877 len += pdu_marshal(pdu, len, "d", err);
879 if (s->proto_version == V9FS_PROTO_2000L) {
884 /* fill out the header */
885 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
887 /* keep these in sync */
891 /* push onto queue and notify */
892 virtqueue_push(s->vq, &pdu->elem, len);
894 /* FIXME: we should batch these completions */
895 virtio_notify(&s->vdev, s->vq);
900 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
905 if (mode & P9_STAT_MODE_DIR) {
909 if (mode & P9_STAT_MODE_SYMLINK) {
912 if (mode & P9_STAT_MODE_SOCKET) {
915 if (mode & P9_STAT_MODE_NAMED_PIPE) {
918 if (mode & P9_STAT_MODE_DEVICE) {
919 if (extension && extension->data[0] == 'c') {
930 if (mode & P9_STAT_MODE_SETUID) {
933 if (mode & P9_STAT_MODE_SETGID) {
936 if (mode & P9_STAT_MODE_SETVTX) {
943 static int donttouch_stat(V9fsStat *stat)
945 if (stat->type == -1 &&
947 stat->qid.type == -1 &&
948 stat->qid.version == -1 &&
949 stat->qid.path == -1 &&
953 stat->length == -1 &&
960 stat->n_muid == -1) {
967 static void v9fs_stat_free(V9fsStat *stat)
969 v9fs_string_free(&stat->name);
970 v9fs_string_free(&stat->uid);
971 v9fs_string_free(&stat->gid);
972 v9fs_string_free(&stat->muid);
973 v9fs_string_free(&stat->extension);
976 static uint32_t stat_to_v9mode(const struct stat *stbuf)
980 mode = stbuf->st_mode & 0777;
981 if (S_ISDIR(stbuf->st_mode)) {
982 mode |= P9_STAT_MODE_DIR;
985 if (S_ISLNK(stbuf->st_mode)) {
986 mode |= P9_STAT_MODE_SYMLINK;
989 if (S_ISSOCK(stbuf->st_mode)) {
990 mode |= P9_STAT_MODE_SOCKET;
993 if (S_ISFIFO(stbuf->st_mode)) {
994 mode |= P9_STAT_MODE_NAMED_PIPE;
997 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
998 mode |= P9_STAT_MODE_DEVICE;
1001 if (stbuf->st_mode & S_ISUID) {
1002 mode |= P9_STAT_MODE_SETUID;
1005 if (stbuf->st_mode & S_ISGID) {
1006 mode |= P9_STAT_MODE_SETGID;
1009 if (stbuf->st_mode & S_ISVTX) {
1010 mode |= P9_STAT_MODE_SETVTX;
1016 static int stat_to_v9stat(V9fsState *s, V9fsString *name,
1017 const struct stat *stbuf,
1023 memset(v9stat, 0, sizeof(*v9stat));
1025 stat_to_qid(stbuf, &v9stat->qid);
1026 v9stat->mode = stat_to_v9mode(stbuf);
1027 v9stat->atime = stbuf->st_atime;
1028 v9stat->mtime = stbuf->st_mtime;
1029 v9stat->length = stbuf->st_size;
1031 v9fs_string_null(&v9stat->uid);
1032 v9fs_string_null(&v9stat->gid);
1033 v9fs_string_null(&v9stat->muid);
1035 v9stat->n_uid = stbuf->st_uid;
1036 v9stat->n_gid = stbuf->st_gid;
1039 v9fs_string_null(&v9stat->extension);
1041 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
1042 err = v9fs_co_readlink(s, name, &v9stat->extension);
1046 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
1047 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
1048 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
1049 major(stbuf->st_rdev), minor(stbuf->st_rdev));
1050 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
1051 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
1052 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
1055 str = strrchr(name->data, '/');
1062 v9fs_string_sprintf(&v9stat->name, "%s", str);
1065 v9fs_string_size(&v9stat->name) +
1066 v9fs_string_size(&v9stat->uid) +
1067 v9fs_string_size(&v9stat->gid) +
1068 v9fs_string_size(&v9stat->muid) +
1069 v9fs_string_size(&v9stat->extension);
1073 #define P9_STATS_MODE 0x00000001ULL
1074 #define P9_STATS_NLINK 0x00000002ULL
1075 #define P9_STATS_UID 0x00000004ULL
1076 #define P9_STATS_GID 0x00000008ULL
1077 #define P9_STATS_RDEV 0x00000010ULL
1078 #define P9_STATS_ATIME 0x00000020ULL
1079 #define P9_STATS_MTIME 0x00000040ULL
1080 #define P9_STATS_CTIME 0x00000080ULL
1081 #define P9_STATS_INO 0x00000100ULL
1082 #define P9_STATS_SIZE 0x00000200ULL
1083 #define P9_STATS_BLOCKS 0x00000400ULL
1085 #define P9_STATS_BTIME 0x00000800ULL
1086 #define P9_STATS_GEN 0x00001000ULL
1087 #define P9_STATS_DATA_VERSION 0x00002000ULL
1089 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1090 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1093 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
1094 V9fsStatDotl *v9lstat)
1096 memset(v9lstat, 0, sizeof(*v9lstat));
1098 v9lstat->st_mode = stbuf->st_mode;
1099 v9lstat->st_nlink = stbuf->st_nlink;
1100 v9lstat->st_uid = stbuf->st_uid;
1101 v9lstat->st_gid = stbuf->st_gid;
1102 v9lstat->st_rdev = stbuf->st_rdev;
1103 v9lstat->st_size = stbuf->st_size;
1104 v9lstat->st_blksize = stbuf->st_blksize;
1105 v9lstat->st_blocks = stbuf->st_blocks;
1106 v9lstat->st_atime_sec = stbuf->st_atime;
1107 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1108 v9lstat->st_mtime_sec = stbuf->st_mtime;
1109 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
1110 v9lstat->st_ctime_sec = stbuf->st_ctime;
1111 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1112 /* Currently we only support BASIC fields in stat */
1113 v9lstat->st_result_mask = P9_STATS_BASIC;
1115 stat_to_qid(stbuf, &v9lstat->qid);
1118 static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
1120 while (len && *iovcnt) {
1121 if (len < sg->iov_len) {
1123 sg->iov_base += len;
1135 static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
1140 for (i = 0; i < *cnt; i++) {
1141 if ((total + sg[i].iov_len) > cap) {
1142 sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
1146 total += sg[i].iov_len;
1154 static void print_sg(struct iovec *sg, int cnt)
1158 printf("sg[%d]: {", cnt);
1159 for (i = 0; i < cnt; i++) {
1163 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1168 static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
1171 v9fs_string_init(&str);
1172 v9fs_string_copy(&str, dst);
1173 v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
1174 v9fs_string_free(&str);
1177 static void v9fs_version(void *opaque)
1179 V9fsPDU *pdu = opaque;
1180 V9fsState *s = pdu->s;
1184 pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1186 if (!strcmp(version.data, "9P2000.u")) {
1187 s->proto_version = V9FS_PROTO_2000U;
1188 } else if (!strcmp(version.data, "9P2000.L")) {
1189 s->proto_version = V9FS_PROTO_2000L;
1191 v9fs_string_sprintf(&version, "unknown");
1194 offset += pdu_marshal(pdu, offset, "ds", s->msize, &version);
1195 complete_pdu(s, pdu, offset);
1197 v9fs_string_free(&version);
1201 static void v9fs_attach(void *opaque)
1203 V9fsPDU *pdu = opaque;
1204 V9fsState *s = pdu->s;
1205 int32_t fid, afid, n_uname;
1206 V9fsString uname, aname;
1212 pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1214 fidp = alloc_fid(s, fid);
1220 fidp->uid = n_uname;
1222 v9fs_string_sprintf(&fidp->path, "%s", "/");
1223 err = fid_to_qid(s, fidp, &qid);
1230 offset += pdu_marshal(pdu, offset, "Q", &qid);
1234 complete_pdu(s, pdu, err);
1235 v9fs_string_free(&uname);
1236 v9fs_string_free(&aname);
1239 static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1246 err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1250 vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1254 complete_pdu(s, vs->pdu, err);
1255 v9fs_stat_free(&vs->v9stat);
1259 static void v9fs_stat(void *opaque)
1261 V9fsPDU *pdu = opaque;
1262 V9fsState *s = pdu->s;
1267 vs = qemu_malloc(sizeof(*vs));
1271 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1273 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1275 vs->fidp = lookup_fid(s, fid);
1276 if (vs->fidp == NULL) {
1281 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1282 v9fs_stat_post_lstat(s, vs, err);
1286 complete_pdu(s, vs->pdu, err);
1287 v9fs_stat_free(&vs->v9stat);
1291 static void v9fs_getattr_post_lstat(V9fsState *s, V9fsStatStateDotl *vs,
1299 stat_to_v9stat_dotl(s, &vs->stbuf, &vs->v9stat_dotl);
1300 vs->offset += pdu_marshal(vs->pdu, vs->offset, "A", &vs->v9stat_dotl);
1304 complete_pdu(s, vs->pdu, err);
1308 static void v9fs_getattr(void *opaque)
1310 V9fsPDU *pdu = opaque;
1311 V9fsState *s = pdu->s;
1313 V9fsStatStateDotl *vs;
1316 uint64_t request_mask;
1318 vs = qemu_malloc(sizeof(*vs));
1322 memset(&vs->v9stat_dotl, 0, sizeof(vs->v9stat_dotl));
1324 pdu_unmarshal(vs->pdu, vs->offset, "dq", &fid, &request_mask);
1326 fidp = lookup_fid(s, fid);
1332 /* Currently we only support BASIC fields in stat, so there is no
1333 * need to look at request_mask.
1335 err = v9fs_do_lstat(s, &fidp->path, &vs->stbuf);
1336 v9fs_getattr_post_lstat(s, vs, err);
1340 complete_pdu(s, vs->pdu, err);
1344 /* From Linux kernel code */
1345 #define ATTR_MODE (1 << 0)
1346 #define ATTR_UID (1 << 1)
1347 #define ATTR_GID (1 << 2)
1348 #define ATTR_SIZE (1 << 3)
1349 #define ATTR_ATIME (1 << 4)
1350 #define ATTR_MTIME (1 << 5)
1351 #define ATTR_CTIME (1 << 6)
1352 #define ATTR_MASK 127
1353 #define ATTR_ATIME_SET (1 << 7)
1354 #define ATTR_MTIME_SET (1 << 8)
1356 static void v9fs_setattr_post_truncate(V9fsState *s, V9fsSetattrState *vs,
1366 complete_pdu(s, vs->pdu, err);
1370 static void v9fs_setattr_post_chown(V9fsState *s, V9fsSetattrState *vs, int err)
1377 if (vs->v9iattr.valid & (ATTR_SIZE)) {
1378 err = v9fs_do_truncate(s, &vs->fidp->path, vs->v9iattr.size);
1380 v9fs_setattr_post_truncate(s, vs, err);
1384 complete_pdu(s, vs->pdu, err);
1388 static void v9fs_setattr_post_utimensat(V9fsState *s, V9fsSetattrState *vs,
1396 /* If the only valid entry in iattr is ctime we can call
1397 * chown(-1,-1) to update the ctime of the file
1399 if ((vs->v9iattr.valid & (ATTR_UID | ATTR_GID)) ||
1400 ((vs->v9iattr.valid & ATTR_CTIME)
1401 && !((vs->v9iattr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
1402 if (!(vs->v9iattr.valid & ATTR_UID)) {
1403 vs->v9iattr.uid = -1;
1405 if (!(vs->v9iattr.valid & ATTR_GID)) {
1406 vs->v9iattr.gid = -1;
1408 err = v9fs_do_chown(s, &vs->fidp->path, vs->v9iattr.uid,
1411 v9fs_setattr_post_chown(s, vs, err);
1415 complete_pdu(s, vs->pdu, err);
1419 static void v9fs_setattr_post_chmod(V9fsState *s, V9fsSetattrState *vs, int err)
1426 if (vs->v9iattr.valid & (ATTR_ATIME | ATTR_MTIME)) {
1427 struct timespec times[2];
1428 if (vs->v9iattr.valid & ATTR_ATIME) {
1429 if (vs->v9iattr.valid & ATTR_ATIME_SET) {
1430 times[0].tv_sec = vs->v9iattr.atime_sec;
1431 times[0].tv_nsec = vs->v9iattr.atime_nsec;
1433 times[0].tv_nsec = UTIME_NOW;
1436 times[0].tv_nsec = UTIME_OMIT;
1439 if (vs->v9iattr.valid & ATTR_MTIME) {
1440 if (vs->v9iattr.valid & ATTR_MTIME_SET) {
1441 times[1].tv_sec = vs->v9iattr.mtime_sec;
1442 times[1].tv_nsec = vs->v9iattr.mtime_nsec;
1444 times[1].tv_nsec = UTIME_NOW;
1447 times[1].tv_nsec = UTIME_OMIT;
1449 err = v9fs_do_utimensat(s, &vs->fidp->path, times);
1451 v9fs_setattr_post_utimensat(s, vs, err);
1455 complete_pdu(s, vs->pdu, err);
1459 static void v9fs_setattr(void *opaque)
1461 V9fsPDU *pdu = opaque;
1462 V9fsState *s = pdu->s;
1464 V9fsSetattrState *vs;
1467 vs = qemu_malloc(sizeof(*vs));
1471 pdu_unmarshal(pdu, vs->offset, "dI", &fid, &vs->v9iattr);
1473 vs->fidp = lookup_fid(s, fid);
1474 if (vs->fidp == NULL) {
1479 if (vs->v9iattr.valid & ATTR_MODE) {
1480 err = v9fs_do_chmod(s, &vs->fidp->path, vs->v9iattr.mode);
1483 v9fs_setattr_post_chmod(s, vs, err);
1487 complete_pdu(s, vs->pdu, err);
1491 static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1493 complete_pdu(s, vs->pdu, err);
1495 if (vs->nwnames && vs->nwnames <= P9_MAXWELEM) {
1496 for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1497 v9fs_string_free(&vs->wnames[vs->name_idx]);
1500 qemu_free(vs->wnames);
1501 qemu_free(vs->qids);
1505 static void v9fs_walk_marshal(V9fsWalkState *vs)
1509 vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1511 for (i = 0; i < vs->nwnames; i++) {
1512 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1516 static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1520 free_fid(s, vs->newfidp->fid);
1521 v9fs_string_free(&vs->path);
1526 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1529 if (vs->name_idx < vs->nwnames) {
1530 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1531 vs->wnames[vs->name_idx].data);
1532 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1534 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1535 v9fs_walk_post_newfid_lstat(s, vs, err);
1539 v9fs_string_free(&vs->path);
1540 v9fs_walk_marshal(vs);
1543 v9fs_walk_complete(s, vs, err);
1546 static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1550 v9fs_string_free(&vs->path);
1555 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1557 if (vs->name_idx < vs->nwnames) {
1559 v9fs_string_sprintf(&vs->path, "%s/%s",
1560 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1561 v9fs_string_copy(&vs->fidp->path, &vs->path);
1563 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1564 v9fs_walk_post_oldfid_lstat(s, vs, err);
1568 v9fs_string_free(&vs->path);
1569 v9fs_walk_marshal(vs);
1572 v9fs_walk_complete(s, vs, err);
1575 static void v9fs_walk(void *opaque)
1577 V9fsPDU *pdu = opaque;
1578 V9fsState *s = pdu->s;
1579 int32_t fid, newfid;
1584 vs = qemu_malloc(sizeof(*vs));
1590 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1591 &newfid, &vs->nwnames);
1593 if (vs->nwnames && vs->nwnames <= P9_MAXWELEM) {
1594 vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1596 vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1598 for (i = 0; i < vs->nwnames; i++) {
1599 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1602 } else if (vs->nwnames > P9_MAXWELEM) {
1607 vs->fidp = lookup_fid(s, fid);
1608 if (vs->fidp == NULL) {
1613 /* FIXME: is this really valid? */
1614 if (fid == newfid) {
1616 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
1617 v9fs_string_init(&vs->path);
1620 if (vs->name_idx < vs->nwnames) {
1621 v9fs_string_sprintf(&vs->path, "%s/%s",
1622 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1623 v9fs_string_copy(&vs->fidp->path, &vs->path);
1625 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1626 v9fs_walk_post_oldfid_lstat(s, vs, err);
1630 vs->newfidp = alloc_fid(s, newfid);
1631 if (vs->newfidp == NULL) {
1636 vs->newfidp->uid = vs->fidp->uid;
1637 v9fs_string_init(&vs->path);
1639 v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1641 if (vs->name_idx < vs->nwnames) {
1642 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1643 vs->wnames[vs->name_idx].data);
1644 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1646 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1647 v9fs_walk_post_newfid_lstat(s, vs, err);
1652 v9fs_walk_marshal(vs);
1655 v9fs_walk_complete(s, vs, err);
1658 static int32_t get_iounit(V9fsState *s, V9fsString *name)
1660 struct statfs stbuf;
1664 * iounit should be multiples of f_bsize (host filesystem block size
1665 * and as well as less than (client msize - P9_IOHDRSZ))
1667 if (!v9fs_do_statfs(s, name, &stbuf)) {
1668 iounit = stbuf.f_bsize;
1669 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1673 iounit = s->msize - P9_IOHDRSZ;
1678 static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1680 if (vs->fidp->fs.dir == NULL) {
1684 vs->fidp->fid_type = P9_FID_DIR;
1685 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1688 complete_pdu(s, vs->pdu, err);
1693 static void v9fs_open_post_getiounit(V9fsState *s, V9fsOpenState *vs)
1696 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
1698 complete_pdu(s, vs->pdu, err);
1702 static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1704 if (vs->fidp->fs.fd == -1) {
1708 vs->fidp->fid_type = P9_FID_FILE;
1709 vs->iounit = get_iounit(s, &vs->fidp->path);
1710 v9fs_open_post_getiounit(s, vs);
1713 complete_pdu(s, vs->pdu, err);
1717 static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1726 stat_to_qid(&vs->stbuf, &vs->qid);
1728 if (S_ISDIR(vs->stbuf.st_mode)) {
1729 vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fidp->path);
1730 v9fs_open_post_opendir(s, vs, err);
1732 if (s->proto_version == V9FS_PROTO_2000L) {
1734 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
1735 /* Ignore direct disk access hint until the server supports it. */
1738 flags = omode_to_uflags(vs->mode);
1740 vs->fidp->fs.fd = v9fs_do_open(s, &vs->fidp->path, flags);
1741 v9fs_open_post_open(s, vs, err);
1745 complete_pdu(s, vs->pdu, err);
1749 static void v9fs_open(void *opaque)
1751 V9fsPDU *pdu = opaque;
1752 V9fsState *s = pdu->s;
1757 vs = qemu_malloc(sizeof(*vs));
1762 if (s->proto_version == V9FS_PROTO_2000L) {
1763 pdu_unmarshal(vs->pdu, vs->offset, "dd", &fid, &vs->mode);
1765 pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1768 vs->fidp = lookup_fid(s, fid);
1769 if (vs->fidp == NULL) {
1774 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
1776 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1778 v9fs_open_post_lstat(s, vs, err);
1781 complete_pdu(s, pdu, err);
1785 static void v9fs_post_lcreate(V9fsState *s, V9fsLcreateState *vs, int err)
1788 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1789 stat_to_qid(&vs->stbuf, &vs->qid);
1790 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid,
1794 vs->fidp->fid_type = P9_FID_NONE;
1796 if (vs->fidp->fs.fd > 0) {
1797 close(vs->fidp->fs.fd);
1801 complete_pdu(s, vs->pdu, err);
1802 v9fs_string_free(&vs->name);
1803 v9fs_string_free(&vs->fullname);
1807 static void v9fs_lcreate_post_get_iounit(V9fsState *s, V9fsLcreateState *vs,
1814 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1817 v9fs_post_lcreate(s, vs, err);
1820 static void v9fs_lcreate_post_do_open2(V9fsState *s, V9fsLcreateState *vs,
1823 if (vs->fidp->fs.fd == -1) {
1827 vs->fidp->fid_type = P9_FID_FILE;
1828 vs->iounit = get_iounit(s, &vs->fullname);
1829 v9fs_lcreate_post_get_iounit(s, vs, err);
1833 v9fs_post_lcreate(s, vs, err);
1836 static void v9fs_lcreate(void *opaque)
1838 V9fsPDU *pdu = opaque;
1839 V9fsState *s = pdu->s;
1840 int32_t dfid, flags, mode;
1842 V9fsLcreateState *vs;
1845 vs = qemu_malloc(sizeof(*vs));
1849 v9fs_string_init(&vs->fullname);
1851 pdu_unmarshal(vs->pdu, vs->offset, "dsddd", &dfid, &vs->name, &flags,
1854 vs->fidp = lookup_fid(s, dfid);
1855 if (vs->fidp == NULL) {
1860 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
1863 /* Ignore direct disk access hint until the server supports it. */
1866 vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
1868 v9fs_lcreate_post_do_open2(s, vs, err);
1872 complete_pdu(s, vs->pdu, err);
1873 v9fs_string_free(&vs->name);
1877 static void v9fs_post_do_fsync(V9fsState *s, V9fsPDU *pdu, int err)
1882 complete_pdu(s, pdu, err);
1885 static void v9fs_fsync(void *opaque)
1887 V9fsPDU *pdu = opaque;
1888 V9fsState *s = pdu->s;
1895 pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1896 fidp = lookup_fid(s, fid);
1899 v9fs_post_do_fsync(s, pdu, err);
1902 err = v9fs_do_fsync(s, fidp->fs.fd, datasync);
1903 v9fs_post_do_fsync(s, pdu, err);
1906 static void v9fs_clunk(void *opaque)
1908 V9fsPDU *pdu = opaque;
1909 V9fsState *s = pdu->s;
1914 pdu_unmarshal(pdu, offset, "d", &fid);
1916 err = free_fid(s, fid);
1924 complete_pdu(s, pdu, err);
1927 static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1929 static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1934 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1935 vs->offset += vs->count;
1938 complete_pdu(s, vs->pdu, err);
1939 v9fs_stat_free(&vs->v9stat);
1940 v9fs_string_free(&vs->name);
1945 static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1952 err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1957 vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1959 if ((vs->len != (vs->v9stat.size + 2)) ||
1960 ((vs->count + vs->len) > vs->max_count)) {
1961 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1962 v9fs_read_post_seekdir(s, vs, err);
1965 vs->count += vs->len;
1966 v9fs_stat_free(&vs->v9stat);
1967 v9fs_string_free(&vs->name);
1968 vs->dir_pos = vs->dent->d_off;
1969 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
1970 v9fs_read_post_readdir(s, vs, err);
1973 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1974 v9fs_read_post_seekdir(s, vs, err);
1979 static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1982 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1983 v9fs_string_init(&vs->name);
1984 v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1986 err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1987 v9fs_read_post_dir_lstat(s, vs, err);
1991 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1992 vs->offset += vs->count;
1994 complete_pdu(s, vs->pdu, err);
1999 static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
2001 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
2002 v9fs_read_post_readdir(s, vs, err);
2006 static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
2009 vs->dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
2010 v9fs_read_post_telldir(s, vs, err);
2014 static void v9fs_read_post_preadv(V9fsState *s, V9fsReadState *vs, ssize_t err)
2017 /* IO error return the error */
2021 vs->total += vs->len;
2022 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
2023 if (vs->total < vs->count && vs->len > 0) {
2026 print_sg(vs->sg, vs->cnt);
2028 vs->len = v9fs_do_preadv(s, vs->fidp->fs.fd, vs->sg, vs->cnt,
2033 } while (vs->len == -1 && errno == EINTR);
2034 if (vs->len == -1) {
2037 v9fs_read_post_preadv(s, vs, err);
2040 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
2041 vs->offset += vs->count;
2045 complete_pdu(s, vs->pdu, err);
2049 static void v9fs_xattr_read(V9fsState *s, V9fsReadState *vs)
2055 xattr_len = vs->fidp->fs.xattr.len;
2056 read_count = xattr_len - vs->off;
2057 if (read_count > vs->count) {
2058 read_count = vs->count;
2059 } else if (read_count < 0) {
2061 * read beyond XATTR value
2065 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", read_count);
2066 vs->offset += pdu_pack(vs->pdu, vs->offset,
2067 ((char *)vs->fidp->fs.xattr.value) + vs->off,
2070 complete_pdu(s, vs->pdu, err);
2074 static void v9fs_read(void *opaque)
2076 V9fsPDU *pdu = opaque;
2077 V9fsState *s = pdu->s;
2082 vs = qemu_malloc(sizeof(*vs));
2089 pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count);
2091 vs->fidp = lookup_fid(s, fid);
2092 if (vs->fidp == NULL) {
2097 if (vs->fidp->fid_type == P9_FID_DIR) {
2098 vs->max_count = vs->count;
2101 v9fs_do_rewinddir(s, vs->fidp->fs.dir);
2103 v9fs_read_post_rewinddir(s, vs, err);
2105 } else if (vs->fidp->fid_type == P9_FID_FILE) {
2107 pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
2108 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
2109 if (vs->total <= vs->count) {
2110 vs->len = v9fs_do_preadv(s, vs->fidp->fs.fd, vs->sg, vs->cnt,
2116 v9fs_read_post_preadv(s, vs, err);
2119 } else if (vs->fidp->fid_type == P9_FID_XATTR) {
2120 v9fs_xattr_read(s, vs);
2126 complete_pdu(s, pdu, err);
2130 typedef struct V9fsReadDirState {
2134 off_t saved_dir_pos;
2135 struct dirent *dent;
2139 int64_t initial_offset;
2143 static void v9fs_readdir_post_seekdir(V9fsState *s, V9fsReadDirState *vs)
2145 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
2146 vs->offset += vs->count;
2147 complete_pdu(s, vs->pdu, vs->offset);
2152 /* Size of each dirent on the wire: size of qid (13) + size of offset (8)
2153 * size of type (1) + size of name.size (2) + strlen(name.data)
2155 #define V9_READDIR_DATA_SZ (24 + strlen(vs->name.data))
2157 static void v9fs_readdir_post_readdir(V9fsState *s, V9fsReadDirState *vs)
2163 v9fs_string_init(&vs->name);
2164 v9fs_string_sprintf(&vs->name, "%s", vs->dent->d_name);
2166 if ((vs->count + V9_READDIR_DATA_SZ) > vs->max_count) {
2167 /* Ran out of buffer. Set dir back to old position and return */
2168 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->saved_dir_pos);
2169 v9fs_readdir_post_seekdir(s, vs);
2173 /* Fill up just the path field of qid because the client uses
2174 * only that. To fill the entire qid structure we will have
2175 * to stat each dirent found, which is expensive
2177 size = MIN(sizeof(vs->dent->d_ino), sizeof(vs->qid.path));
2178 memcpy(&vs->qid.path, &vs->dent->d_ino, size);
2179 /* Fill the other fields with dummy values */
2181 vs->qid.version = 0;
2183 len = pdu_marshal(vs->pdu, vs->offset+4+vs->count, "Qqbs",
2184 &vs->qid, vs->dent->d_off,
2185 vs->dent->d_type, &vs->name);
2187 v9fs_string_free(&vs->name);
2188 vs->saved_dir_pos = vs->dent->d_off;
2189 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
2190 v9fs_readdir_post_readdir(s, vs);
2194 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
2195 vs->offset += vs->count;
2196 complete_pdu(s, vs->pdu, vs->offset);
2201 static void v9fs_readdir_post_telldir(V9fsState *s, V9fsReadDirState *vs)
2203 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
2204 v9fs_readdir_post_readdir(s, vs);
2208 static void v9fs_readdir_post_setdir(V9fsState *s, V9fsReadDirState *vs)
2210 vs->saved_dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
2211 v9fs_readdir_post_telldir(s, vs);
2215 static void v9fs_readdir(void *opaque)
2217 V9fsPDU *pdu = opaque;
2218 V9fsState *s = pdu->s;
2220 V9fsReadDirState *vs;
2224 vs = qemu_malloc(sizeof(*vs));
2229 pdu_unmarshal(vs->pdu, offset, "dqd", &fid, &vs->initial_offset,
2232 vs->fidp = lookup_fid(s, fid);
2233 if (vs->fidp == NULL || !(vs->fidp->fs.dir)) {
2238 if (vs->initial_offset == 0) {
2239 v9fs_do_rewinddir(s, vs->fidp->fs.dir);
2241 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->initial_offset);
2244 v9fs_readdir_post_setdir(s, vs);
2248 complete_pdu(s, pdu, err);
2252 static void v9fs_write_post_pwritev(V9fsState *s, V9fsWriteState *vs,
2256 /* IO error return the error */
2260 vs->total += vs->len;
2261 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
2262 if (vs->total < vs->count && vs->len > 0) {
2265 print_sg(vs->sg, vs->cnt);
2267 vs->len = v9fs_do_pwritev(s, vs->fidp->fs.fd, vs->sg, vs->cnt,
2272 } while (vs->len == -1 && errno == EINTR);
2273 if (vs->len == -1) {
2276 v9fs_write_post_pwritev(s, vs, err);
2279 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
2282 complete_pdu(s, vs->pdu, err);
2286 static void v9fs_xattr_write(V9fsState *s, V9fsWriteState *vs)
2293 xattr_len = vs->fidp->fs.xattr.len;
2294 write_count = xattr_len - vs->off;
2295 if (write_count > vs->count) {
2296 write_count = vs->count;
2297 } else if (write_count < 0) {
2299 * write beyond XATTR value len specified in
2305 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", write_count);
2307 vs->fidp->fs.xattr.copied_len += write_count;
2309 * Now copy the content from sg list
2311 for (i = 0; i < vs->cnt; i++) {
2312 if (write_count > vs->sg[i].iov_len) {
2313 to_copy = vs->sg[i].iov_len;
2315 to_copy = write_count;
2317 memcpy((char *)vs->fidp->fs.xattr.value + vs->off,
2318 vs->sg[i].iov_base, to_copy);
2319 /* updating vs->off since we are not using below */
2321 write_count -= to_copy;
2324 complete_pdu(s, vs->pdu, err);
2328 static void v9fs_write(void *opaque)
2330 V9fsPDU *pdu = opaque;
2331 V9fsState *s = pdu->s;
2336 vs = qemu_malloc(sizeof(*vs));
2344 pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
2347 vs->fidp = lookup_fid(s, fid);
2348 if (vs->fidp == NULL) {
2353 if (vs->fidp->fid_type == P9_FID_FILE) {
2354 if (vs->fidp->fs.fd == -1) {
2358 } else if (vs->fidp->fid_type == P9_FID_XATTR) {
2360 * setxattr operation
2362 v9fs_xattr_write(s, vs);
2368 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
2369 if (vs->total <= vs->count) {
2370 vs->len = v9fs_do_pwritev(s, vs->fidp->fs.fd, vs->sg, vs->cnt, vs->off);
2375 v9fs_write_post_pwritev(s, vs, err);
2379 complete_pdu(s, vs->pdu, err);
2383 static void v9fs_create_post_getiounit(V9fsState *s, V9fsCreateState *vs)
2386 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
2387 stat_to_qid(&vs->stbuf, &vs->qid);
2389 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
2392 complete_pdu(s, vs->pdu, err);
2393 v9fs_string_free(&vs->name);
2394 v9fs_string_free(&vs->extension);
2395 v9fs_string_free(&vs->fullname);
2399 static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
2402 vs->iounit = get_iounit(s, &vs->fidp->path);
2403 v9fs_create_post_getiounit(s, vs);
2407 complete_pdu(s, vs->pdu, err);
2408 v9fs_string_free(&vs->name);
2409 v9fs_string_free(&vs->extension);
2410 v9fs_string_free(&vs->fullname);
2414 static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
2419 v9fs_post_create(s, vs, err);
2422 static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
2425 if (!vs->fidp->fs.dir) {
2428 vs->fidp->fid_type = P9_FID_DIR;
2429 v9fs_post_create(s, vs, err);
2432 static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
2440 vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fullname);
2441 v9fs_create_post_opendir(s, vs, err);
2445 v9fs_post_create(s, vs, err);
2448 static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
2455 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2456 v9fs_create_post_dir_lstat(s, vs, err);
2460 v9fs_post_create(s, vs, err);
2463 static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
2466 vs->fidp->fid_type = P9_FID_NONE;
2467 close(vs->fidp->fs.fd);
2470 v9fs_post_create(s, vs, err);
2474 static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
2476 if (vs->fidp->fs.fd == -1) {
2480 vs->fidp->fid_type = P9_FID_FILE;
2481 err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
2482 v9fs_create_post_fstat(s, vs, err);
2487 v9fs_post_create(s, vs, err);
2491 static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
2494 if (err == 0 || errno != ENOENT) {
2499 if (vs->perm & P9_STAT_MODE_DIR) {
2500 err = v9fs_do_mkdir(s, vs->fullname.data, vs->perm & 0777,
2502 v9fs_create_post_mkdir(s, vs, err);
2503 } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
2504 err = v9fs_do_symlink(s, vs->fidp, vs->extension.data,
2505 vs->fullname.data, -1);
2506 v9fs_create_post_perms(s, vs, err);
2507 } else if (vs->perm & P9_STAT_MODE_LINK) {
2508 int32_t nfid = atoi(vs->extension.data);
2509 V9fsFidState *nfidp = lookup_fid(s, nfid);
2510 if (nfidp == NULL) {
2512 v9fs_post_create(s, vs, err);
2514 err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
2515 v9fs_create_post_perms(s, vs, err);
2516 } else if (vs->perm & P9_STAT_MODE_DEVICE) {
2518 uint32_t major, minor;
2521 if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
2524 v9fs_post_create(s, vs, err);
2536 v9fs_post_create(s, vs, err);
2539 nmode |= vs->perm & 0777;
2540 err = v9fs_do_mknod(s, vs->fullname.data, nmode,
2541 makedev(major, minor), vs->fidp->uid, -1);
2542 v9fs_create_post_perms(s, vs, err);
2543 } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
2544 err = v9fs_do_mknod(s, vs->fullname.data, S_IFIFO | (vs->perm & 0777),
2545 0, vs->fidp->uid, -1);
2546 v9fs_post_create(s, vs, err);
2547 } else if (vs->perm & P9_STAT_MODE_SOCKET) {
2548 err = v9fs_do_mknod(s, vs->fullname.data, S_IFSOCK | (vs->perm & 0777),
2549 0, vs->fidp->uid, -1);
2550 v9fs_post_create(s, vs, err);
2552 vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
2553 -1, omode_to_uflags(vs->mode)|O_CREAT, vs->perm);
2555 v9fs_create_post_open2(s, vs, err);
2561 v9fs_post_create(s, vs, err);
2564 static void v9fs_create(void *opaque)
2566 V9fsPDU *pdu = opaque;
2567 V9fsState *s = pdu->s;
2569 V9fsCreateState *vs;
2572 vs = qemu_malloc(sizeof(*vs));
2576 v9fs_string_init(&vs->fullname);
2578 pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
2579 &vs->perm, &vs->mode, &vs->extension);
2581 vs->fidp = lookup_fid(s, fid);
2582 if (vs->fidp == NULL) {
2587 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
2590 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2591 v9fs_create_post_lstat(s, vs, err);
2595 complete_pdu(s, vs->pdu, err);
2596 v9fs_string_free(&vs->name);
2597 v9fs_string_free(&vs->extension);
2601 static void v9fs_post_symlink(V9fsState *s, V9fsSymlinkState *vs, int err)
2604 stat_to_qid(&vs->stbuf, &vs->qid);
2605 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
2610 complete_pdu(s, vs->pdu, err);
2611 v9fs_string_free(&vs->name);
2612 v9fs_string_free(&vs->symname);
2613 v9fs_string_free(&vs->fullname);
2617 static void v9fs_symlink_post_do_symlink(V9fsState *s, V9fsSymlinkState *vs,
2623 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2625 v9fs_post_symlink(s, vs, err);
2628 static void v9fs_symlink(void *opaque)
2630 V9fsPDU *pdu = opaque;
2631 V9fsState *s = pdu->s;
2633 V9fsSymlinkState *vs;
2637 vs = qemu_malloc(sizeof(*vs));
2641 v9fs_string_init(&vs->fullname);
2643 pdu_unmarshal(vs->pdu, vs->offset, "dssd", &dfid, &vs->name,
2644 &vs->symname, &gid);
2646 vs->dfidp = lookup_fid(s, dfid);
2647 if (vs->dfidp == NULL) {
2652 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->dfidp->path.data,
2654 err = v9fs_do_symlink(s, vs->dfidp, vs->symname.data,
2655 vs->fullname.data, gid);
2656 v9fs_symlink_post_do_symlink(s, vs, err);
2660 complete_pdu(s, vs->pdu, err);
2661 v9fs_string_free(&vs->name);
2662 v9fs_string_free(&vs->symname);
2666 static void v9fs_flush(void *opaque)
2668 V9fsPDU *pdu = opaque;
2669 V9fsState *s = pdu->s;
2670 /* A nop call with no return */
2671 complete_pdu(s, pdu, 7);
2675 static void v9fs_link(void *opaque)
2677 V9fsPDU *pdu = opaque;
2678 V9fsState *s = pdu->s;
2679 int32_t dfid, oldfid;
2680 V9fsFidState *dfidp, *oldfidp;
2681 V9fsString name, fullname;
2685 v9fs_string_init(&fullname);
2687 pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2689 dfidp = lookup_fid(s, dfid);
2690 if (dfidp == NULL) {
2695 oldfidp = lookup_fid(s, oldfid);
2696 if (oldfidp == NULL) {
2701 v9fs_string_sprintf(&fullname, "%s/%s", dfidp->path.data, name.data);
2703 err = v9fs_do_link(s, &oldfidp->path, &fullname);
2707 v9fs_string_free(&fullname);
2710 v9fs_string_free(&name);
2711 complete_pdu(s, pdu, err);
2714 static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
2723 /* For TREMOVE we need to clunk the fid even on failed remove */
2724 free_fid(s, vs->fidp->fid);
2726 complete_pdu(s, vs->pdu, err);
2730 static void v9fs_remove(void *opaque)
2732 V9fsPDU *pdu = opaque;
2733 V9fsState *s = pdu->s;
2735 V9fsRemoveState *vs;
2738 vs = qemu_malloc(sizeof(*vs));
2742 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
2744 vs->fidp = lookup_fid(s, fid);
2745 if (vs->fidp == NULL) {
2750 err = v9fs_do_remove(s, &vs->fidp->path);
2751 v9fs_remove_post_remove(s, vs, err);
2755 complete_pdu(s, pdu, err);
2759 static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
2768 v9fs_stat_free(&vs->v9stat);
2769 complete_pdu(s, vs->pdu, err);
2773 static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
2778 if (vs->v9stat.length != -1) {
2779 if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
2783 v9fs_wstat_post_truncate(s, vs, err);
2787 v9fs_stat_free(&vs->v9stat);
2788 complete_pdu(s, vs->pdu, err);
2792 static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs)
2795 char *old_name, *new_name;
2798 if (vs->newdirfid != -1) {
2799 V9fsFidState *dirfidp;
2800 dirfidp = lookup_fid(s, vs->newdirfid);
2802 if (dirfidp == NULL) {
2807 BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2809 new_name = qemu_mallocz(dirfidp->path.size + vs->name.size + 2);
2811 strcpy(new_name, dirfidp->path.data);
2812 strcat(new_name, "/");
2813 strcat(new_name + dirfidp->path.size, vs->name.data);
2815 old_name = vs->fidp->path.data;
2816 end = strrchr(old_name, '/');
2822 new_name = qemu_mallocz(end - old_name + vs->name.size + 1);
2824 strncat(new_name, old_name, end - old_name);
2825 strncat(new_name + (end - old_name), vs->name.data, vs->name.size);
2828 v9fs_string_free(&vs->name);
2829 vs->name.data = qemu_strdup(new_name);
2830 vs->name.size = strlen(new_name);
2832 if (strcmp(new_name, vs->fidp->path.data) != 0) {
2833 if (v9fs_do_rename(s, &vs->fidp->path, &vs->name)) {
2838 * Fixup fid's pointing to the old name to
2839 * start pointing to the new name
2841 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2842 if (vs->fidp == fidp) {
2844 * we replace name of this fid towards the end so
2845 * that our below v9fs_path_is_ancestor check will
2850 if (v9fs_path_is_ancestor(&vs->fidp->path, &fidp->path)) {
2851 /* replace the name */
2852 v9fs_fix_path(&fidp->path, &vs->name,
2853 strlen(vs->fidp->path.data));
2856 v9fs_string_copy(&vs->fidp->path, &vs->name);
2860 v9fs_string_free(&vs->name);
2864 static void v9fs_rename_post_rename(V9fsState *s, V9fsRenameState *vs, int err)
2866 complete_pdu(s, vs->pdu, err);
2870 static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
2876 if (vs->v9stat.name.size != 0) {
2877 V9fsRenameState *vr;
2879 vr = qemu_mallocz(sizeof(V9fsRenameState));
2882 vr->fidp = vs->fidp;
2883 vr->offset = vs->offset;
2884 vr->name.size = vs->v9stat.name.size;
2885 vr->name.data = qemu_strdup(vs->v9stat.name.data);
2887 err = v9fs_complete_rename(s, vr);
2890 v9fs_wstat_post_rename(s, vs, err);
2894 v9fs_stat_free(&vs->v9stat);
2895 complete_pdu(s, vs->pdu, err);
2899 static void v9fs_rename(void *opaque)
2901 V9fsPDU *pdu = opaque;
2902 V9fsState *s = pdu->s;
2904 V9fsRenameState *vs;
2907 vs = qemu_malloc(sizeof(*vs));
2911 pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &vs->newdirfid, &vs->name);
2913 vs->fidp = lookup_fid(s, fid);
2914 if (vs->fidp == NULL) {
2919 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
2921 err = v9fs_complete_rename(s, vs);
2922 v9fs_rename_post_rename(s, vs, err);
2925 complete_pdu(s, vs->pdu, err);
2929 static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2935 if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) {
2936 if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2937 vs->v9stat.n_gid)) {
2941 v9fs_wstat_post_chown(s, vs, err);
2945 v9fs_stat_free(&vs->v9stat);
2946 complete_pdu(s, vs->pdu, err);
2950 static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2956 if (vs->v9stat.mtime != -1 || vs->v9stat.atime != -1) {
2957 struct timespec times[2];
2958 if (vs->v9stat.atime != -1) {
2959 times[0].tv_sec = vs->v9stat.atime;
2960 times[0].tv_nsec = 0;
2962 times[0].tv_nsec = UTIME_OMIT;
2964 if (vs->v9stat.mtime != -1) {
2965 times[1].tv_sec = vs->v9stat.mtime;
2966 times[1].tv_nsec = 0;
2968 times[1].tv_nsec = UTIME_OMIT;
2971 if (v9fs_do_utimensat(s, &vs->fidp->path, times)) {
2976 v9fs_wstat_post_utime(s, vs, err);
2980 v9fs_stat_free(&vs->v9stat);
2981 complete_pdu(s, vs->pdu, err);
2985 static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2990 v9fs_stat_free(&vs->v9stat);
2991 complete_pdu(s, vs->pdu, err);
2995 static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
3004 v9_mode = stat_to_v9mode(&vs->stbuf);
3006 if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
3007 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
3008 /* Attempting to change the type */
3013 if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
3014 &vs->v9stat.extension))) {
3017 v9fs_wstat_post_chmod(s, vs, err);
3021 v9fs_stat_free(&vs->v9stat);
3022 complete_pdu(s, vs->pdu, err);
3026 static void v9fs_wstat(void *opaque)
3028 V9fsPDU *pdu = opaque;
3029 V9fsState *s = pdu->s;
3034 vs = qemu_malloc(sizeof(*vs));
3038 pdu_unmarshal(pdu, vs->offset, "dwS", &fid, &vs->unused, &vs->v9stat);
3040 vs->fidp = lookup_fid(s, fid);
3041 if (vs->fidp == NULL) {
3046 /* do we need to sync the file? */
3047 if (donttouch_stat(&vs->v9stat)) {
3048 err = v9fs_do_fsync(s, vs->fidp->fs.fd, 0);
3049 v9fs_wstat_post_fsync(s, vs, err);
3053 if (vs->v9stat.mode != -1) {
3054 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
3055 v9fs_wstat_post_lstat(s, vs, err);
3059 v9fs_wstat_post_chmod(s, vs, err);
3063 v9fs_stat_free(&vs->v9stat);
3064 complete_pdu(s, vs->pdu, err);
3068 static void v9fs_statfs_post_statfs(V9fsState *s, V9fsStatfsState *vs, int err)
3070 int32_t bsize_factor;
3078 * compute bsize factor based on host file system block size
3081 bsize_factor = (s->msize - P9_IOHDRSZ)/vs->stbuf.f_bsize;
3082 if (!bsize_factor) {
3085 vs->v9statfs.f_type = vs->stbuf.f_type;
3086 vs->v9statfs.f_bsize = vs->stbuf.f_bsize;
3087 vs->v9statfs.f_bsize *= bsize_factor;
3089 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3090 * adjust(divide) the number of blocks, free blocks and available
3091 * blocks by bsize factor
3093 vs->v9statfs.f_blocks = vs->stbuf.f_blocks/bsize_factor;
3094 vs->v9statfs.f_bfree = vs->stbuf.f_bfree/bsize_factor;
3095 vs->v9statfs.f_bavail = vs->stbuf.f_bavail/bsize_factor;
3096 vs->v9statfs.f_files = vs->stbuf.f_files;
3097 vs->v9statfs.f_ffree = vs->stbuf.f_ffree;
3098 vs->v9statfs.fsid_val = (unsigned int) vs->stbuf.f_fsid.__val[0] |
3099 (unsigned long long)vs->stbuf.f_fsid.__val[1] << 32;
3100 vs->v9statfs.f_namelen = vs->stbuf.f_namelen;
3102 vs->offset += pdu_marshal(vs->pdu, vs->offset, "ddqqqqqqd",
3103 vs->v9statfs.f_type, vs->v9statfs.f_bsize, vs->v9statfs.f_blocks,
3104 vs->v9statfs.f_bfree, vs->v9statfs.f_bavail, vs->v9statfs.f_files,
3105 vs->v9statfs.f_ffree, vs->v9statfs.fsid_val,
3106 vs->v9statfs.f_namelen);
3109 complete_pdu(s, vs->pdu, vs->offset);
3113 static void v9fs_statfs(void *opaque)
3115 V9fsPDU *pdu = opaque;
3116 V9fsState *s = pdu->s;
3117 V9fsStatfsState *vs;
3120 vs = qemu_malloc(sizeof(*vs));
3124 memset(&vs->v9statfs, 0, sizeof(vs->v9statfs));
3126 pdu_unmarshal(vs->pdu, vs->offset, "d", &vs->fid);
3128 vs->fidp = lookup_fid(s, vs->fid);
3129 if (vs->fidp == NULL) {
3134 err = v9fs_do_statfs(s, &vs->fidp->path, &vs->stbuf);
3135 v9fs_statfs_post_statfs(s, vs, err);
3139 complete_pdu(s, vs->pdu, err);
3144 static void v9fs_mknod_post_lstat(V9fsState *s, V9fsMkState *vs, int err)
3151 stat_to_qid(&vs->stbuf, &vs->qid);
3152 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
3155 complete_pdu(s, vs->pdu, err);
3156 v9fs_string_free(&vs->fullname);
3157 v9fs_string_free(&vs->name);
3161 static void v9fs_mknod_post_mknod(V9fsState *s, V9fsMkState *vs, int err)
3168 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
3169 v9fs_mknod_post_lstat(s, vs, err);
3172 complete_pdu(s, vs->pdu, err);
3173 v9fs_string_free(&vs->fullname);
3174 v9fs_string_free(&vs->name);
3178 static void v9fs_mknod(void *opaque)
3180 V9fsPDU *pdu = opaque;
3181 V9fsState *s = pdu->s;
3190 vs = qemu_malloc(sizeof(*vs));
3194 v9fs_string_init(&vs->fullname);
3195 pdu_unmarshal(vs->pdu, vs->offset, "dsdddd", &fid, &vs->name, &mode,
3196 &major, &minor, &gid);
3198 fidp = lookup_fid(s, fid);
3204 v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data);
3205 err = v9fs_do_mknod(s, vs->fullname.data, mode, makedev(major, minor),
3207 v9fs_mknod_post_mknod(s, vs, err);
3211 complete_pdu(s, vs->pdu, err);
3212 v9fs_string_free(&vs->fullname);
3213 v9fs_string_free(&vs->name);
3218 * Implement posix byte range locking code
3219 * Server side handling of locking code is very simple, because 9p server in
3220 * QEMU can handle only one client. And most of the lock handling
3221 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3222 * do any thing in * qemu 9p server side lock code path.
3223 * So when a TLOCK request comes, always return success
3226 static void v9fs_lock(void *opaque)
3228 V9fsPDU *pdu = opaque;
3229 V9fsState *s = pdu->s;
3230 int32_t fid, err = 0;
3233 vs = qemu_mallocz(sizeof(*vs));
3237 vs->flock = qemu_malloc(sizeof(*vs->flock));
3238 pdu_unmarshal(vs->pdu, vs->offset, "dbdqqds", &fid, &vs->flock->type,
3239 &vs->flock->flags, &vs->flock->start, &vs->flock->length,
3240 &vs->flock->proc_id, &vs->flock->client_id);
3242 vs->status = P9_LOCK_ERROR;
3244 /* We support only block flag now (that too ignored currently) */
3245 if (vs->flock->flags & ~P9_LOCK_FLAGS_BLOCK) {
3249 vs->fidp = lookup_fid(s, fid);
3250 if (vs->fidp == NULL) {
3255 err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
3260 vs->status = P9_LOCK_SUCCESS;
3262 vs->offset += pdu_marshal(vs->pdu, vs->offset, "b", vs->status);
3263 complete_pdu(s, vs->pdu, err);
3264 qemu_free(vs->flock);
3269 * When a TGETLOCK request comes, always return success because all lock
3270 * handling is done by client's VFS layer.
3273 static void v9fs_getlock(void *opaque)
3275 V9fsPDU *pdu = opaque;
3276 V9fsState *s = pdu->s;
3277 int32_t fid, err = 0;
3278 V9fsGetlockState *vs;
3280 vs = qemu_mallocz(sizeof(*vs));
3284 vs->glock = qemu_malloc(sizeof(*vs->glock));
3285 pdu_unmarshal(vs->pdu, vs->offset, "dbqqds", &fid, &vs->glock->type,
3286 &vs->glock->start, &vs->glock->length, &vs->glock->proc_id,
3287 &vs->glock->client_id);
3289 vs->fidp = lookup_fid(s, fid);
3290 if (vs->fidp == NULL) {
3295 err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
3300 vs->glock->type = F_UNLCK;
3301 vs->offset += pdu_marshal(vs->pdu, vs->offset, "bqqds", vs->glock->type,
3302 vs->glock->start, vs->glock->length, vs->glock->proc_id,
3303 &vs->glock->client_id);
3305 complete_pdu(s, vs->pdu, err);
3306 qemu_free(vs->glock);
3310 static void v9fs_mkdir_post_lstat(V9fsState *s, V9fsMkState *vs, int err)
3317 stat_to_qid(&vs->stbuf, &vs->qid);
3318 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
3321 complete_pdu(s, vs->pdu, err);
3322 v9fs_string_free(&vs->fullname);
3323 v9fs_string_free(&vs->name);
3327 static void v9fs_mkdir_post_mkdir(V9fsState *s, V9fsMkState *vs, int err)
3334 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
3335 v9fs_mkdir_post_lstat(s, vs, err);
3338 complete_pdu(s, vs->pdu, err);
3339 v9fs_string_free(&vs->fullname);
3340 v9fs_string_free(&vs->name);
3344 static void v9fs_mkdir(void *opaque)
3346 V9fsPDU *pdu = opaque;
3347 V9fsState *s = pdu->s;
3355 vs = qemu_malloc(sizeof(*vs));
3359 v9fs_string_init(&vs->fullname);
3360 pdu_unmarshal(vs->pdu, vs->offset, "dsdd", &fid, &vs->name, &mode,
3363 fidp = lookup_fid(s, fid);
3369 v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data);
3370 err = v9fs_do_mkdir(s, vs->fullname.data, mode, fidp->uid, gid);
3371 v9fs_mkdir_post_mkdir(s, vs, err);
3375 complete_pdu(s, vs->pdu, err);
3376 v9fs_string_free(&vs->fullname);
3377 v9fs_string_free(&vs->name);
3381 static void v9fs_post_xattr_getvalue(V9fsState *s, V9fsXattrState *vs, int err)
3386 free_fid(s, vs->xattr_fidp->fid);
3389 vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size);
3392 complete_pdu(s, vs->pdu, err);
3393 v9fs_string_free(&vs->name);
3398 static void v9fs_post_xattr_check(V9fsState *s, V9fsXattrState *vs, ssize_t err)
3402 free_fid(s, vs->xattr_fidp->fid);
3406 * Read the xattr value
3408 vs->xattr_fidp->fs.xattr.len = vs->size;
3409 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3410 vs->xattr_fidp->fs.xattr.copied_len = -1;
3412 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3413 err = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path,
3414 &vs->name, vs->xattr_fidp->fs.xattr.value,
3415 vs->xattr_fidp->fs.xattr.len);
3417 v9fs_post_xattr_getvalue(s, vs, err);
3420 complete_pdu(s, vs->pdu, err);
3421 v9fs_string_free(&vs->name);
3425 static void v9fs_post_lxattr_getvalue(V9fsState *s,
3426 V9fsXattrState *vs, int err)
3430 free_fid(s, vs->xattr_fidp->fid);
3433 vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size);
3436 complete_pdu(s, vs->pdu, err);
3437 v9fs_string_free(&vs->name);
3442 static void v9fs_post_lxattr_check(V9fsState *s,
3443 V9fsXattrState *vs, ssize_t err)
3447 free_fid(s, vs->xattr_fidp->fid);
3451 * Read the xattr value
3453 vs->xattr_fidp->fs.xattr.len = vs->size;
3454 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3455 vs->xattr_fidp->fs.xattr.copied_len = -1;
3457 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3458 err = v9fs_do_llistxattr(s, &vs->xattr_fidp->path,
3459 vs->xattr_fidp->fs.xattr.value,
3460 vs->xattr_fidp->fs.xattr.len);
3462 v9fs_post_lxattr_getvalue(s, vs, err);
3465 complete_pdu(s, vs->pdu, err);
3466 v9fs_string_free(&vs->name);
3470 static void v9fs_xattrwalk(void *opaque)
3472 V9fsPDU *pdu = opaque;
3473 V9fsState *s = pdu->s;
3476 int32_t fid, newfid;
3478 vs = qemu_malloc(sizeof(*vs));
3482 pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &newfid, &vs->name);
3483 vs->file_fidp = lookup_fid(s, fid);
3484 if (vs->file_fidp == NULL) {
3489 vs->xattr_fidp = alloc_fid(s, newfid);
3490 if (vs->xattr_fidp == NULL) {
3495 v9fs_string_copy(&vs->xattr_fidp->path, &vs->file_fidp->path);
3496 if (vs->name.data[0] == 0) {
3498 * listxattr request. Get the size first
3500 vs->size = v9fs_do_llistxattr(s, &vs->xattr_fidp->path,
3505 v9fs_post_lxattr_check(s, vs, err);
3509 * specific xattr fid. We check for xattr
3510 * presence also collect the xattr size
3512 vs->size = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path,
3513 &vs->name, NULL, 0);
3517 v9fs_post_xattr_check(s, vs, err);
3521 complete_pdu(s, vs->pdu, err);
3522 v9fs_string_free(&vs->name);
3526 static void v9fs_xattrcreate(void *opaque)
3528 V9fsPDU *pdu = opaque;
3529 V9fsState *s = pdu->s;
3535 vs = qemu_malloc(sizeof(*vs));
3539 pdu_unmarshal(vs->pdu, vs->offset, "dsqd",
3540 &fid, &vs->name, &vs->size, &flags);
3542 vs->file_fidp = lookup_fid(s, fid);
3543 if (vs->file_fidp == NULL) {
3548 /* Make the file fid point to xattr */
3549 vs->xattr_fidp = vs->file_fidp;
3550 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3551 vs->xattr_fidp->fs.xattr.copied_len = 0;
3552 vs->xattr_fidp->fs.xattr.len = vs->size;
3553 vs->xattr_fidp->fs.xattr.flags = flags;
3554 v9fs_string_init(&vs->xattr_fidp->fs.xattr.name);
3555 v9fs_string_copy(&vs->xattr_fidp->fs.xattr.name, &vs->name);
3557 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3559 vs->xattr_fidp->fs.xattr.value = NULL;
3562 complete_pdu(s, vs->pdu, err);
3563 v9fs_string_free(&vs->name);
3567 static void v9fs_readlink(void *opaque)
3569 V9fsPDU *pdu = opaque;
3576 pdu_unmarshal(pdu, offset, "d", &fid);
3577 fidp = lookup_fid(pdu->s, fid);
3583 v9fs_string_init(&target);
3584 err = v9fs_co_readlink(pdu->s, &fidp->path, &target);
3588 offset += pdu_marshal(pdu, offset, "s", &target);
3590 v9fs_string_free(&target);
3592 complete_pdu(pdu->s, pdu, err);
3595 static CoroutineEntry *pdu_co_handlers[] = {
3596 [P9_TREADDIR] = v9fs_readdir,
3597 [P9_TSTATFS] = v9fs_statfs,
3598 [P9_TGETATTR] = v9fs_getattr,
3599 [P9_TSETATTR] = v9fs_setattr,
3600 [P9_TXATTRWALK] = v9fs_xattrwalk,
3601 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3602 [P9_TMKNOD] = v9fs_mknod,
3603 [P9_TRENAME] = v9fs_rename,
3604 [P9_TLOCK] = v9fs_lock,
3605 [P9_TGETLOCK] = v9fs_getlock,
3606 [P9_TREADLINK] = v9fs_readlink,
3607 [P9_TMKDIR] = v9fs_mkdir,
3608 [P9_TVERSION] = v9fs_version,
3609 [P9_TLOPEN] = v9fs_open,
3610 [P9_TATTACH] = v9fs_attach,
3611 [P9_TSTAT] = v9fs_stat,
3612 [P9_TWALK] = v9fs_walk,
3613 [P9_TCLUNK] = v9fs_clunk,
3614 [P9_TFSYNC] = v9fs_fsync,
3615 [P9_TOPEN] = v9fs_open,
3616 [P9_TREAD] = v9fs_read,
3618 [P9_TAUTH] = v9fs_auth,
3620 [P9_TFLUSH] = v9fs_flush,
3621 [P9_TLINK] = v9fs_link,
3622 [P9_TSYMLINK] = v9fs_symlink,
3623 [P9_TCREATE] = v9fs_create,
3624 [P9_TLCREATE] = v9fs_lcreate,
3625 [P9_TWRITE] = v9fs_write,
3626 [P9_TWSTAT] = v9fs_wstat,
3627 [P9_TREMOVE] = v9fs_remove,
3630 static void v9fs_op_not_supp(void *opaque)
3632 V9fsPDU *pdu = opaque;
3633 complete_pdu(pdu->s, pdu, -EOPNOTSUPP);
3636 static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
3639 CoroutineEntry *handler;
3644 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3645 (pdu_co_handlers[pdu->id] == NULL)) {
3646 handler = v9fs_op_not_supp;
3648 handler = pdu_co_handlers[pdu->id];
3650 co = qemu_coroutine_create(handler);
3651 qemu_coroutine_enter(co, pdu);
3654 void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
3656 V9fsState *s = (V9fsState *)vdev;
3660 while ((pdu = alloc_pdu(s)) &&
3661 (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
3664 BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
3665 BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
3667 ptr = pdu->elem.out_sg[0].iov_base;
3669 memcpy(&pdu->size, ptr, 4);
3671 memcpy(&pdu->tag, ptr + 5, 2);