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"
37 static int omode_to_uflags(int8_t mode)
71 void cred_init(FsCred *credp)
79 static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf)
81 return s->ops->lstat(&s->ctx, path->data, stbuf);
84 static ssize_t v9fs_do_readlink(V9fsState *s, V9fsString *path, V9fsString *buf)
88 buf->data = qemu_malloc(1024);
90 len = s->ops->readlink(&s->ctx, path->data, buf->data, 1024 - 1);
99 static int v9fs_do_close(V9fsState *s, int fd)
101 return s->ops->close(&s->ctx, fd);
104 static int v9fs_do_closedir(V9fsState *s, DIR *dir)
106 return s->ops->closedir(&s->ctx, dir);
109 static int v9fs_do_open(V9fsState *s, V9fsString *path, int flags)
111 return s->ops->open(&s->ctx, path->data, flags);
114 static DIR *v9fs_do_opendir(V9fsState *s, V9fsString *path)
116 return s->ops->opendir(&s->ctx, path->data);
119 static void v9fs_do_rewinddir(V9fsState *s, DIR *dir)
121 return s->ops->rewinddir(&s->ctx, dir);
124 static off_t v9fs_do_telldir(V9fsState *s, DIR *dir)
126 return s->ops->telldir(&s->ctx, dir);
129 static struct dirent *v9fs_do_readdir(V9fsState *s, DIR *dir)
131 return s->ops->readdir(&s->ctx, dir);
134 static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off)
136 return s->ops->seekdir(&s->ctx, dir, off);
139 static int v9fs_do_preadv(V9fsState *s, int fd, const struct iovec *iov,
140 int iovcnt, int64_t offset)
142 return s->ops->preadv(&s->ctx, fd, iov, iovcnt, offset);
145 static int v9fs_do_pwritev(V9fsState *s, int fd, const struct iovec *iov,
146 int iovcnt, int64_t offset)
148 return s->ops->pwritev(&s->ctx, fd, iov, iovcnt, offset);
151 static int v9fs_do_chmod(V9fsState *s, V9fsString *path, mode_t mode)
156 return s->ops->chmod(&s->ctx, path->data, &cred);
159 static int v9fs_do_mknod(V9fsState *s, char *name,
160 mode_t mode, dev_t dev, uid_t uid, gid_t gid)
168 return s->ops->mknod(&s->ctx, name, &cred);
171 static int v9fs_do_mkdir(V9fsState *s, char *name, mode_t mode,
172 uid_t uid, gid_t gid)
181 return s->ops->mkdir(&s->ctx, name, &cred);
184 static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
186 return s->ops->fstat(&s->ctx, fd, stbuf);
189 static int v9fs_do_open2(V9fsState *s, char *fullname, uid_t uid, gid_t gid,
197 cred.fc_mode = mode & 07777;
200 return s->ops->open2(&s->ctx, fullname, flags, &cred);
203 static int v9fs_do_symlink(V9fsState *s, V9fsFidState *fidp,
204 const char *oldpath, const char *newpath, gid_t gid)
208 cred.fc_uid = fidp->uid;
212 return s->ops->symlink(&s->ctx, oldpath, newpath, &cred);
215 static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
217 return s->ops->link(&s->ctx, oldpath->data, newpath->data);
220 static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
222 return s->ops->truncate(&s->ctx, path->data, size);
225 static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath,
228 return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
231 static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
238 return s->ops->chown(&s->ctx, path->data, &cred);
241 static int v9fs_do_utimensat(V9fsState *s, V9fsString *path,
242 const struct timespec times[2])
244 return s->ops->utimensat(&s->ctx, path->data, times);
247 static int v9fs_do_remove(V9fsState *s, V9fsString *path)
249 return s->ops->remove(&s->ctx, path->data);
252 static int v9fs_do_fsync(V9fsState *s, int fd, int datasync)
254 return s->ops->fsync(&s->ctx, fd, datasync);
257 static int v9fs_do_statfs(V9fsState *s, V9fsString *path, struct statfs *stbuf)
259 return s->ops->statfs(&s->ctx, path->data, stbuf);
262 static ssize_t v9fs_do_lgetxattr(V9fsState *s, V9fsString *path,
263 V9fsString *xattr_name,
264 void *value, size_t size)
266 return s->ops->lgetxattr(&s->ctx, path->data,
267 xattr_name->data, value, size);
270 static ssize_t v9fs_do_llistxattr(V9fsState *s, V9fsString *path,
271 void *value, size_t size)
273 return s->ops->llistxattr(&s->ctx, path->data,
277 static int v9fs_do_lsetxattr(V9fsState *s, V9fsString *path,
278 V9fsString *xattr_name,
279 void *value, size_t size, int flags)
281 return s->ops->lsetxattr(&s->ctx, path->data,
282 xattr_name->data, value, size, flags);
285 static int v9fs_do_lremovexattr(V9fsState *s, V9fsString *path,
286 V9fsString *xattr_name)
288 return s->ops->lremovexattr(&s->ctx, path->data,
293 static void v9fs_string_init(V9fsString *str)
299 static void v9fs_string_free(V9fsString *str)
301 qemu_free(str->data);
306 static void v9fs_string_null(V9fsString *str)
308 v9fs_string_free(str);
311 static int number_to_string(void *arg, char type)
313 unsigned int ret = 0;
317 unsigned int num = *(unsigned int *)arg;
326 unsigned long num = *(unsigned long *)arg;
334 printf("Number_to_string: Unknown number format\n");
341 static int GCC_FMT_ATTR(2, 0)
342 v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
345 char *iter = (char *)fmt;
349 unsigned int arg_uint;
350 unsigned long arg_ulong;
352 /* Find the number of %'s that denotes an argument */
353 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
358 len = strlen(fmt) - 2*nr_args;
368 /* Now parse the format string */
369 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
373 arg_uint = va_arg(ap2, unsigned int);
374 len += number_to_string((void *)&arg_uint, 'u');
377 if (*++iter == 'u') {
378 arg_ulong = va_arg(ap2, unsigned long);
379 len += number_to_string((void *)&arg_ulong, 'U');
385 arg_char_ptr = va_arg(ap2, char *);
386 len += strlen(arg_char_ptr);
393 "v9fs_string_alloc_printf:Incorrect format %c", *iter);
400 *strp = qemu_malloc((len + 1) * sizeof(**strp));
402 return vsprintf(*strp, fmt, ap);
405 static void GCC_FMT_ATTR(2, 3)
406 v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
411 v9fs_string_free(str);
414 err = v9fs_string_alloc_printf(&str->data, fmt, ap);
421 static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
423 v9fs_string_free(lhs);
424 v9fs_string_sprintf(lhs, "%s", rhs->data);
428 * Return TRUE if s1 is an ancestor of s2.
430 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
431 * As a special case, We treat s1 as ancestor of s2 if they are same!
433 static int v9fs_path_is_ancestor(V9fsString *s1, V9fsString *s2)
435 if (!strncmp(s1->data, s2->data, s1->size)) {
436 if (s2->data[s1->size] == '\0' || s2->data[s1->size] == '/') {
443 static size_t v9fs_string_size(V9fsString *str)
448 static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
452 for (f = s->fid_list; f; f = f->next) {
461 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
465 f = lookup_fid(s, fid);
470 f = qemu_mallocz(sizeof(V9fsFidState));
473 f->fid_type = P9_FID_NONE;
475 f->next = s->fid_list;
481 static int v9fs_xattr_fid_clunk(V9fsState *s, V9fsFidState *fidp)
485 if (fidp->fs.xattr.copied_len == -1) {
486 /* getxattr/listxattr fid */
490 * if this is fid for setxattr. clunk should
491 * result in setxattr localcall
493 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
494 /* clunk after partial write */
498 if (fidp->fs.xattr.len) {
499 retval = v9fs_do_lsetxattr(s, &fidp->path, &fidp->fs.xattr.name,
500 fidp->fs.xattr.value,
502 fidp->fs.xattr.flags);
504 retval = v9fs_do_lremovexattr(s, &fidp->path, &fidp->fs.xattr.name);
507 v9fs_string_free(&fidp->fs.xattr.name);
509 if (fidp->fs.xattr.value) {
510 qemu_free(fidp->fs.xattr.value);
515 static int free_fid(V9fsState *s, int32_t fid)
518 V9fsFidState **fidpp, *fidp;
520 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
521 if ((*fidpp)->fid == fid) {
526 if (*fidpp == NULL) {
533 if (fidp->fid_type == P9_FID_FILE) {
534 v9fs_do_close(s, fidp->fs.fd);
535 } else if (fidp->fid_type == P9_FID_DIR) {
536 v9fs_do_closedir(s, fidp->fs.dir);
537 } else if (fidp->fid_type == P9_FID_XATTR) {
538 retval = v9fs_xattr_fid_clunk(s, fidp);
540 v9fs_string_free(&fidp->path);
546 #define P9_QID_TYPE_DIR 0x80
547 #define P9_QID_TYPE_SYMLINK 0x02
549 #define P9_STAT_MODE_DIR 0x80000000
550 #define P9_STAT_MODE_APPEND 0x40000000
551 #define P9_STAT_MODE_EXCL 0x20000000
552 #define P9_STAT_MODE_MOUNT 0x10000000
553 #define P9_STAT_MODE_AUTH 0x08000000
554 #define P9_STAT_MODE_TMP 0x04000000
555 #define P9_STAT_MODE_SYMLINK 0x02000000
556 #define P9_STAT_MODE_LINK 0x01000000
557 #define P9_STAT_MODE_DEVICE 0x00800000
558 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
559 #define P9_STAT_MODE_SOCKET 0x00100000
560 #define P9_STAT_MODE_SETUID 0x00080000
561 #define P9_STAT_MODE_SETGID 0x00040000
562 #define P9_STAT_MODE_SETVTX 0x00010000
564 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
565 P9_STAT_MODE_SYMLINK | \
566 P9_STAT_MODE_LINK | \
567 P9_STAT_MODE_DEVICE | \
568 P9_STAT_MODE_NAMED_PIPE | \
571 /* This is the algorithm from ufs in spfs */
572 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
576 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
577 memcpy(&qidp->path, &stbuf->st_ino, size);
578 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
580 if (S_ISDIR(stbuf->st_mode)) {
581 qidp->type |= P9_QID_TYPE_DIR;
583 if (S_ISLNK(stbuf->st_mode)) {
584 qidp->type |= P9_QID_TYPE_SYMLINK;
588 static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
593 err = v9fs_do_lstat(s, &fidp->path, &stbuf);
598 stat_to_qid(&stbuf, qidp);
602 static V9fsPDU *alloc_pdu(V9fsState *s)
606 if (!QLIST_EMPTY(&s->free_list)) {
607 pdu = QLIST_FIRST(&s->free_list);
608 QLIST_REMOVE(pdu, next);
613 static void free_pdu(V9fsState *s, V9fsPDU *pdu)
619 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
623 size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
624 size_t offset, size_t size, int pack)
629 for (i = 0; size && i < sg_count; i++) {
631 if (offset >= sg[i].iov_len) {
633 offset -= sg[i].iov_len;
636 len = MIN(sg[i].iov_len - offset, size);
638 memcpy(sg[i].iov_base + offset, addr, len);
640 memcpy(addr, sg[i].iov_base + offset, len);
655 static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
657 return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
661 static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
664 return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
668 static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
672 struct iovec *src_sg;
676 src_sg = pdu->elem.in_sg;
677 num = pdu->elem.in_num;
679 src_sg = pdu->elem.out_sg;
680 num = pdu->elem.out_num;
684 for (i = 0; i < num; i++) {
686 sg[j].iov_base = src_sg[i].iov_base;
687 sg[j].iov_len = src_sg[i].iov_len;
689 } else if (offset < (src_sg[i].iov_len + pos)) {
690 sg[j].iov_base = src_sg[i].iov_base;
691 sg[j].iov_len = src_sg[i].iov_len;
692 sg[j].iov_base += (offset - pos);
693 sg[j].iov_len -= (offset - pos);
696 pos += src_sg[i].iov_len;
702 static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
704 size_t old_offset = offset;
709 for (i = 0; fmt[i]; i++) {
712 uint8_t *valp = va_arg(ap, uint8_t *);
713 offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
718 valp = va_arg(ap, uint16_t *);
719 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
720 *valp = le16_to_cpu(val);
725 valp = va_arg(ap, uint32_t *);
726 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
727 *valp = le32_to_cpu(val);
732 valp = va_arg(ap, uint64_t *);
733 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
734 *valp = le64_to_cpu(val);
738 struct iovec *iov = va_arg(ap, struct iovec *);
739 int *iovcnt = va_arg(ap, int *);
740 *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
744 V9fsString *str = va_arg(ap, V9fsString *);
745 offset += pdu_unmarshal(pdu, offset, "w", &str->size);
746 /* FIXME: sanity check str->size */
747 str->data = qemu_malloc(str->size + 1);
748 offset += pdu_unpack(str->data, pdu, offset, str->size);
749 str->data[str->size] = 0;
753 V9fsQID *qidp = va_arg(ap, V9fsQID *);
754 offset += pdu_unmarshal(pdu, offset, "bdq",
755 &qidp->type, &qidp->version, &qidp->path);
759 V9fsStat *statp = va_arg(ap, V9fsStat *);
760 offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
761 &statp->size, &statp->type, &statp->dev,
762 &statp->qid, &statp->mode, &statp->atime,
763 &statp->mtime, &statp->length,
764 &statp->name, &statp->uid, &statp->gid,
765 &statp->muid, &statp->extension,
766 &statp->n_uid, &statp->n_gid,
771 V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
772 offset += pdu_unmarshal(pdu, offset, "ddddqqqqq",
773 &iattr->valid, &iattr->mode,
774 &iattr->uid, &iattr->gid, &iattr->size,
775 &iattr->atime_sec, &iattr->atime_nsec,
776 &iattr->mtime_sec, &iattr->mtime_nsec);
786 return offset - old_offset;
789 static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
791 size_t old_offset = offset;
796 for (i = 0; fmt[i]; i++) {
799 uint8_t val = va_arg(ap, int);
800 offset += pdu_pack(pdu, offset, &val, sizeof(val));
805 cpu_to_le16w(&val, va_arg(ap, int));
806 offset += pdu_pack(pdu, offset, &val, sizeof(val));
811 cpu_to_le32w(&val, va_arg(ap, uint32_t));
812 offset += pdu_pack(pdu, offset, &val, sizeof(val));
817 cpu_to_le64w(&val, va_arg(ap, uint64_t));
818 offset += pdu_pack(pdu, offset, &val, sizeof(val));
822 struct iovec *iov = va_arg(ap, struct iovec *);
823 int *iovcnt = va_arg(ap, int *);
824 *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
828 V9fsString *str = va_arg(ap, V9fsString *);
829 offset += pdu_marshal(pdu, offset, "w", str->size);
830 offset += pdu_pack(pdu, offset, str->data, str->size);
834 V9fsQID *qidp = va_arg(ap, V9fsQID *);
835 offset += pdu_marshal(pdu, offset, "bdq",
836 qidp->type, qidp->version, qidp->path);
840 V9fsStat *statp = va_arg(ap, V9fsStat *);
841 offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
842 statp->size, statp->type, statp->dev,
843 &statp->qid, statp->mode, statp->atime,
844 statp->mtime, statp->length, &statp->name,
845 &statp->uid, &statp->gid, &statp->muid,
846 &statp->extension, statp->n_uid,
847 statp->n_gid, statp->n_muid);
851 V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
852 offset += pdu_marshal(pdu, offset, "qQdddqqqqqqqqqqqqqqq",
853 statp->st_result_mask,
854 &statp->qid, statp->st_mode,
855 statp->st_uid, statp->st_gid,
856 statp->st_nlink, statp->st_rdev,
857 statp->st_size, statp->st_blksize, statp->st_blocks,
858 statp->st_atime_sec, statp->st_atime_nsec,
859 statp->st_mtime_sec, statp->st_mtime_nsec,
860 statp->st_ctime_sec, statp->st_ctime_nsec,
861 statp->st_btime_sec, statp->st_btime_nsec,
862 statp->st_gen, statp->st_data_version);
871 return offset - old_offset;
874 static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
876 int8_t id = pdu->id + 1; /* Response */
882 if (s->proto_version != V9FS_PROTO_2000L) {
885 str.data = strerror(err);
886 str.size = strlen(str.data);
888 len += pdu_marshal(pdu, len, "s", &str);
892 len += pdu_marshal(pdu, len, "d", err);
894 if (s->proto_version == V9FS_PROTO_2000L) {
899 /* fill out the header */
900 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
902 /* keep these in sync */
906 /* push onto queue and notify */
907 virtqueue_push(s->vq, &pdu->elem, len);
909 /* FIXME: we should batch these completions */
910 virtio_notify(&s->vdev, s->vq);
915 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
920 if (mode & P9_STAT_MODE_DIR) {
924 if (mode & P9_STAT_MODE_SYMLINK) {
927 if (mode & P9_STAT_MODE_SOCKET) {
930 if (mode & P9_STAT_MODE_NAMED_PIPE) {
933 if (mode & P9_STAT_MODE_DEVICE) {
934 if (extension && extension->data[0] == 'c') {
945 if (mode & P9_STAT_MODE_SETUID) {
948 if (mode & P9_STAT_MODE_SETGID) {
951 if (mode & P9_STAT_MODE_SETVTX) {
958 static int donttouch_stat(V9fsStat *stat)
960 if (stat->type == -1 &&
962 stat->qid.type == -1 &&
963 stat->qid.version == -1 &&
964 stat->qid.path == -1 &&
968 stat->length == -1 &&
975 stat->n_muid == -1) {
982 static void v9fs_stat_free(V9fsStat *stat)
984 v9fs_string_free(&stat->name);
985 v9fs_string_free(&stat->uid);
986 v9fs_string_free(&stat->gid);
987 v9fs_string_free(&stat->muid);
988 v9fs_string_free(&stat->extension);
991 static uint32_t stat_to_v9mode(const struct stat *stbuf)
995 mode = stbuf->st_mode & 0777;
996 if (S_ISDIR(stbuf->st_mode)) {
997 mode |= P9_STAT_MODE_DIR;
1000 if (S_ISLNK(stbuf->st_mode)) {
1001 mode |= P9_STAT_MODE_SYMLINK;
1004 if (S_ISSOCK(stbuf->st_mode)) {
1005 mode |= P9_STAT_MODE_SOCKET;
1008 if (S_ISFIFO(stbuf->st_mode)) {
1009 mode |= P9_STAT_MODE_NAMED_PIPE;
1012 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
1013 mode |= P9_STAT_MODE_DEVICE;
1016 if (stbuf->st_mode & S_ISUID) {
1017 mode |= P9_STAT_MODE_SETUID;
1020 if (stbuf->st_mode & S_ISGID) {
1021 mode |= P9_STAT_MODE_SETGID;
1024 if (stbuf->st_mode & S_ISVTX) {
1025 mode |= P9_STAT_MODE_SETVTX;
1031 static int stat_to_v9stat(V9fsState *s, V9fsString *name,
1032 const struct stat *stbuf,
1038 memset(v9stat, 0, sizeof(*v9stat));
1040 stat_to_qid(stbuf, &v9stat->qid);
1041 v9stat->mode = stat_to_v9mode(stbuf);
1042 v9stat->atime = stbuf->st_atime;
1043 v9stat->mtime = stbuf->st_mtime;
1044 v9stat->length = stbuf->st_size;
1046 v9fs_string_null(&v9stat->uid);
1047 v9fs_string_null(&v9stat->gid);
1048 v9fs_string_null(&v9stat->muid);
1050 v9stat->n_uid = stbuf->st_uid;
1051 v9stat->n_gid = stbuf->st_gid;
1054 v9fs_string_null(&v9stat->extension);
1056 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
1057 err = v9fs_do_readlink(s, name, &v9stat->extension);
1062 v9stat->extension.data[err] = 0;
1063 v9stat->extension.size = err;
1064 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
1065 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
1066 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
1067 major(stbuf->st_rdev), minor(stbuf->st_rdev));
1068 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
1069 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
1070 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
1073 str = strrchr(name->data, '/');
1080 v9fs_string_sprintf(&v9stat->name, "%s", str);
1083 v9fs_string_size(&v9stat->name) +
1084 v9fs_string_size(&v9stat->uid) +
1085 v9fs_string_size(&v9stat->gid) +
1086 v9fs_string_size(&v9stat->muid) +
1087 v9fs_string_size(&v9stat->extension);
1091 #define P9_STATS_MODE 0x00000001ULL
1092 #define P9_STATS_NLINK 0x00000002ULL
1093 #define P9_STATS_UID 0x00000004ULL
1094 #define P9_STATS_GID 0x00000008ULL
1095 #define P9_STATS_RDEV 0x00000010ULL
1096 #define P9_STATS_ATIME 0x00000020ULL
1097 #define P9_STATS_MTIME 0x00000040ULL
1098 #define P9_STATS_CTIME 0x00000080ULL
1099 #define P9_STATS_INO 0x00000100ULL
1100 #define P9_STATS_SIZE 0x00000200ULL
1101 #define P9_STATS_BLOCKS 0x00000400ULL
1103 #define P9_STATS_BTIME 0x00000800ULL
1104 #define P9_STATS_GEN 0x00001000ULL
1105 #define P9_STATS_DATA_VERSION 0x00002000ULL
1107 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1108 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1111 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
1112 V9fsStatDotl *v9lstat)
1114 memset(v9lstat, 0, sizeof(*v9lstat));
1116 v9lstat->st_mode = stbuf->st_mode;
1117 v9lstat->st_nlink = stbuf->st_nlink;
1118 v9lstat->st_uid = stbuf->st_uid;
1119 v9lstat->st_gid = stbuf->st_gid;
1120 v9lstat->st_rdev = stbuf->st_rdev;
1121 v9lstat->st_size = stbuf->st_size;
1122 v9lstat->st_blksize = stbuf->st_blksize;
1123 v9lstat->st_blocks = stbuf->st_blocks;
1124 v9lstat->st_atime_sec = stbuf->st_atime;
1125 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1126 v9lstat->st_mtime_sec = stbuf->st_mtime;
1127 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
1128 v9lstat->st_ctime_sec = stbuf->st_ctime;
1129 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1130 /* Currently we only support BASIC fields in stat */
1131 v9lstat->st_result_mask = P9_STATS_BASIC;
1133 stat_to_qid(stbuf, &v9lstat->qid);
1136 static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
1138 while (len && *iovcnt) {
1139 if (len < sg->iov_len) {
1141 sg->iov_base += len;
1153 static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
1158 for (i = 0; i < *cnt; i++) {
1159 if ((total + sg[i].iov_len) > cap) {
1160 sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
1164 total += sg[i].iov_len;
1172 static void print_sg(struct iovec *sg, int cnt)
1176 printf("sg[%d]: {", cnt);
1177 for (i = 0; i < cnt; i++) {
1181 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1186 static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
1189 v9fs_string_init(&str);
1190 v9fs_string_copy(&str, dst);
1191 v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
1192 v9fs_string_free(&str);
1195 static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
1200 pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1202 if (!strcmp(version.data, "9P2000.u")) {
1203 s->proto_version = V9FS_PROTO_2000U;
1204 } else if (!strcmp(version.data, "9P2000.L")) {
1205 s->proto_version = V9FS_PROTO_2000L;
1207 v9fs_string_sprintf(&version, "unknown");
1210 offset += pdu_marshal(pdu, offset, "ds", s->msize, &version);
1211 complete_pdu(s, pdu, offset);
1213 v9fs_string_free(&version);
1216 static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
1218 int32_t fid, afid, n_uname;
1219 V9fsString uname, aname;
1225 pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1227 fidp = alloc_fid(s, fid);
1233 fidp->uid = n_uname;
1235 v9fs_string_sprintf(&fidp->path, "%s", "/");
1236 err = fid_to_qid(s, fidp, &qid);
1243 offset += pdu_marshal(pdu, offset, "Q", &qid);
1247 complete_pdu(s, pdu, err);
1248 v9fs_string_free(&uname);
1249 v9fs_string_free(&aname);
1252 static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1259 err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1263 vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1267 complete_pdu(s, vs->pdu, err);
1268 v9fs_stat_free(&vs->v9stat);
1272 static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
1278 vs = qemu_malloc(sizeof(*vs));
1282 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1284 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1286 vs->fidp = lookup_fid(s, fid);
1287 if (vs->fidp == NULL) {
1292 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1293 v9fs_stat_post_lstat(s, vs, err);
1297 complete_pdu(s, vs->pdu, err);
1298 v9fs_stat_free(&vs->v9stat);
1302 static void v9fs_getattr_post_lstat(V9fsState *s, V9fsStatStateDotl *vs,
1310 stat_to_v9stat_dotl(s, &vs->stbuf, &vs->v9stat_dotl);
1311 vs->offset += pdu_marshal(vs->pdu, vs->offset, "A", &vs->v9stat_dotl);
1315 complete_pdu(s, vs->pdu, err);
1319 static void v9fs_getattr(V9fsState *s, V9fsPDU *pdu)
1322 V9fsStatStateDotl *vs;
1325 uint64_t request_mask;
1327 vs = qemu_malloc(sizeof(*vs));
1331 memset(&vs->v9stat_dotl, 0, sizeof(vs->v9stat_dotl));
1333 pdu_unmarshal(vs->pdu, vs->offset, "dq", &fid, &request_mask);
1335 fidp = lookup_fid(s, fid);
1341 /* Currently we only support BASIC fields in stat, so there is no
1342 * need to look at request_mask.
1344 err = v9fs_do_lstat(s, &fidp->path, &vs->stbuf);
1345 v9fs_getattr_post_lstat(s, vs, err);
1349 complete_pdu(s, vs->pdu, err);
1353 /* From Linux kernel code */
1354 #define ATTR_MODE (1 << 0)
1355 #define ATTR_UID (1 << 1)
1356 #define ATTR_GID (1 << 2)
1357 #define ATTR_SIZE (1 << 3)
1358 #define ATTR_ATIME (1 << 4)
1359 #define ATTR_MTIME (1 << 5)
1360 #define ATTR_CTIME (1 << 6)
1361 #define ATTR_MASK 127
1362 #define ATTR_ATIME_SET (1 << 7)
1363 #define ATTR_MTIME_SET (1 << 8)
1365 static void v9fs_setattr_post_truncate(V9fsState *s, V9fsSetattrState *vs,
1375 complete_pdu(s, vs->pdu, err);
1379 static void v9fs_setattr_post_chown(V9fsState *s, V9fsSetattrState *vs, int err)
1386 if (vs->v9iattr.valid & (ATTR_SIZE)) {
1387 err = v9fs_do_truncate(s, &vs->fidp->path, vs->v9iattr.size);
1389 v9fs_setattr_post_truncate(s, vs, err);
1393 complete_pdu(s, vs->pdu, err);
1397 static void v9fs_setattr_post_utimensat(V9fsState *s, V9fsSetattrState *vs,
1405 /* If the only valid entry in iattr is ctime we can call
1406 * chown(-1,-1) to update the ctime of the file
1408 if ((vs->v9iattr.valid & (ATTR_UID | ATTR_GID)) ||
1409 ((vs->v9iattr.valid & ATTR_CTIME)
1410 && !((vs->v9iattr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
1411 if (!(vs->v9iattr.valid & ATTR_UID)) {
1412 vs->v9iattr.uid = -1;
1414 if (!(vs->v9iattr.valid & ATTR_GID)) {
1415 vs->v9iattr.gid = -1;
1417 err = v9fs_do_chown(s, &vs->fidp->path, vs->v9iattr.uid,
1420 v9fs_setattr_post_chown(s, vs, err);
1424 complete_pdu(s, vs->pdu, err);
1428 static void v9fs_setattr_post_chmod(V9fsState *s, V9fsSetattrState *vs, int err)
1435 if (vs->v9iattr.valid & (ATTR_ATIME | ATTR_MTIME)) {
1436 struct timespec times[2];
1437 if (vs->v9iattr.valid & ATTR_ATIME) {
1438 if (vs->v9iattr.valid & ATTR_ATIME_SET) {
1439 times[0].tv_sec = vs->v9iattr.atime_sec;
1440 times[0].tv_nsec = vs->v9iattr.atime_nsec;
1442 times[0].tv_nsec = UTIME_NOW;
1445 times[0].tv_nsec = UTIME_OMIT;
1448 if (vs->v9iattr.valid & ATTR_MTIME) {
1449 if (vs->v9iattr.valid & ATTR_MTIME_SET) {
1450 times[1].tv_sec = vs->v9iattr.mtime_sec;
1451 times[1].tv_nsec = vs->v9iattr.mtime_nsec;
1453 times[1].tv_nsec = UTIME_NOW;
1456 times[1].tv_nsec = UTIME_OMIT;
1458 err = v9fs_do_utimensat(s, &vs->fidp->path, times);
1460 v9fs_setattr_post_utimensat(s, vs, err);
1464 complete_pdu(s, vs->pdu, err);
1468 static void v9fs_setattr(V9fsState *s, V9fsPDU *pdu)
1471 V9fsSetattrState *vs;
1474 vs = qemu_malloc(sizeof(*vs));
1478 pdu_unmarshal(pdu, vs->offset, "dI", &fid, &vs->v9iattr);
1480 vs->fidp = lookup_fid(s, fid);
1481 if (vs->fidp == NULL) {
1486 if (vs->v9iattr.valid & ATTR_MODE) {
1487 err = v9fs_do_chmod(s, &vs->fidp->path, vs->v9iattr.mode);
1490 v9fs_setattr_post_chmod(s, vs, err);
1494 complete_pdu(s, vs->pdu, err);
1498 static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1500 complete_pdu(s, vs->pdu, err);
1502 if (vs->nwnames && vs->nwnames <= P9_MAXWELEM) {
1503 for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1504 v9fs_string_free(&vs->wnames[vs->name_idx]);
1507 qemu_free(vs->wnames);
1508 qemu_free(vs->qids);
1512 static void v9fs_walk_marshal(V9fsWalkState *vs)
1516 vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1518 for (i = 0; i < vs->nwnames; i++) {
1519 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1523 static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1527 free_fid(s, vs->newfidp->fid);
1528 v9fs_string_free(&vs->path);
1533 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1536 if (vs->name_idx < vs->nwnames) {
1537 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1538 vs->wnames[vs->name_idx].data);
1539 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1541 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1542 v9fs_walk_post_newfid_lstat(s, vs, err);
1546 v9fs_string_free(&vs->path);
1547 v9fs_walk_marshal(vs);
1550 v9fs_walk_complete(s, vs, err);
1553 static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1557 v9fs_string_free(&vs->path);
1562 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1564 if (vs->name_idx < vs->nwnames) {
1566 v9fs_string_sprintf(&vs->path, "%s/%s",
1567 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1568 v9fs_string_copy(&vs->fidp->path, &vs->path);
1570 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1571 v9fs_walk_post_oldfid_lstat(s, vs, err);
1575 v9fs_string_free(&vs->path);
1576 v9fs_walk_marshal(vs);
1579 v9fs_walk_complete(s, vs, err);
1582 static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1584 int32_t fid, newfid;
1589 vs = qemu_malloc(sizeof(*vs));
1595 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1596 &newfid, &vs->nwnames);
1598 if (vs->nwnames && vs->nwnames <= P9_MAXWELEM) {
1599 vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1601 vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1603 for (i = 0; i < vs->nwnames; i++) {
1604 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1607 } else if (vs->nwnames > P9_MAXWELEM) {
1612 vs->fidp = lookup_fid(s, fid);
1613 if (vs->fidp == NULL) {
1618 /* FIXME: is this really valid? */
1619 if (fid == newfid) {
1621 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
1622 v9fs_string_init(&vs->path);
1625 if (vs->name_idx < vs->nwnames) {
1626 v9fs_string_sprintf(&vs->path, "%s/%s",
1627 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1628 v9fs_string_copy(&vs->fidp->path, &vs->path);
1630 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1631 v9fs_walk_post_oldfid_lstat(s, vs, err);
1635 vs->newfidp = alloc_fid(s, newfid);
1636 if (vs->newfidp == NULL) {
1641 vs->newfidp->uid = vs->fidp->uid;
1642 v9fs_string_init(&vs->path);
1644 v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1646 if (vs->name_idx < vs->nwnames) {
1647 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1648 vs->wnames[vs->name_idx].data);
1649 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1651 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1652 v9fs_walk_post_newfid_lstat(s, vs, err);
1657 v9fs_walk_marshal(vs);
1660 v9fs_walk_complete(s, vs, err);
1663 static int32_t get_iounit(V9fsState *s, V9fsString *name)
1665 struct statfs stbuf;
1669 * iounit should be multiples of f_bsize (host filesystem block size
1670 * and as well as less than (client msize - P9_IOHDRSZ))
1672 if (!v9fs_do_statfs(s, name, &stbuf)) {
1673 iounit = stbuf.f_bsize;
1674 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1678 iounit = s->msize - P9_IOHDRSZ;
1683 static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1685 if (vs->fidp->fs.dir == NULL) {
1689 vs->fidp->fid_type = P9_FID_DIR;
1690 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1693 complete_pdu(s, vs->pdu, err);
1698 static void v9fs_open_post_getiounit(V9fsState *s, V9fsOpenState *vs)
1701 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
1703 complete_pdu(s, vs->pdu, err);
1707 static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1709 if (vs->fidp->fs.fd == -1) {
1713 vs->fidp->fid_type = P9_FID_FILE;
1714 vs->iounit = get_iounit(s, &vs->fidp->path);
1715 v9fs_open_post_getiounit(s, vs);
1718 complete_pdu(s, vs->pdu, err);
1722 static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1731 stat_to_qid(&vs->stbuf, &vs->qid);
1733 if (S_ISDIR(vs->stbuf.st_mode)) {
1734 vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fidp->path);
1735 v9fs_open_post_opendir(s, vs, err);
1737 if (s->proto_version == V9FS_PROTO_2000L) {
1739 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
1740 /* Ignore direct disk access hint until the server supports it. */
1743 flags = omode_to_uflags(vs->mode);
1745 vs->fidp->fs.fd = v9fs_do_open(s, &vs->fidp->path, flags);
1746 v9fs_open_post_open(s, vs, err);
1750 complete_pdu(s, vs->pdu, err);
1754 static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
1760 vs = qemu_malloc(sizeof(*vs));
1765 if (s->proto_version == V9FS_PROTO_2000L) {
1766 pdu_unmarshal(vs->pdu, vs->offset, "dd", &fid, &vs->mode);
1768 pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1771 vs->fidp = lookup_fid(s, fid);
1772 if (vs->fidp == NULL) {
1777 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
1779 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1781 v9fs_open_post_lstat(s, vs, err);
1784 complete_pdu(s, pdu, err);
1788 static void v9fs_post_lcreate(V9fsState *s, V9fsLcreateState *vs, int err)
1791 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1792 stat_to_qid(&vs->stbuf, &vs->qid);
1793 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid,
1797 vs->fidp->fid_type = P9_FID_NONE;
1799 if (vs->fidp->fs.fd > 0) {
1800 close(vs->fidp->fs.fd);
1804 complete_pdu(s, vs->pdu, err);
1805 v9fs_string_free(&vs->name);
1806 v9fs_string_free(&vs->fullname);
1810 static void v9fs_lcreate_post_get_iounit(V9fsState *s, V9fsLcreateState *vs,
1817 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1820 v9fs_post_lcreate(s, vs, err);
1823 static void v9fs_lcreate_post_do_open2(V9fsState *s, V9fsLcreateState *vs,
1826 if (vs->fidp->fs.fd == -1) {
1830 vs->fidp->fid_type = P9_FID_FILE;
1831 vs->iounit = get_iounit(s, &vs->fullname);
1832 v9fs_lcreate_post_get_iounit(s, vs, err);
1836 v9fs_post_lcreate(s, vs, err);
1839 static void v9fs_lcreate(V9fsState *s, V9fsPDU *pdu)
1841 int32_t dfid, flags, mode;
1843 V9fsLcreateState *vs;
1846 vs = qemu_malloc(sizeof(*vs));
1850 v9fs_string_init(&vs->fullname);
1852 pdu_unmarshal(vs->pdu, vs->offset, "dsddd", &dfid, &vs->name, &flags,
1855 vs->fidp = lookup_fid(s, dfid);
1856 if (vs->fidp == NULL) {
1861 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
1864 /* Ignore direct disk access hint until the server supports it. */
1867 vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
1869 v9fs_lcreate_post_do_open2(s, vs, err);
1873 complete_pdu(s, vs->pdu, err);
1874 v9fs_string_free(&vs->name);
1878 static void v9fs_post_do_fsync(V9fsState *s, V9fsPDU *pdu, int err)
1883 complete_pdu(s, pdu, err);
1886 static void v9fs_fsync(V9fsState *s, V9fsPDU *pdu)
1894 pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1895 fidp = lookup_fid(s, fid);
1898 v9fs_post_do_fsync(s, pdu, err);
1901 err = v9fs_do_fsync(s, fidp->fs.fd, datasync);
1902 v9fs_post_do_fsync(s, pdu, err);
1905 static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1911 pdu_unmarshal(pdu, offset, "d", &fid);
1913 err = free_fid(s, fid);
1921 complete_pdu(s, pdu, err);
1924 static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1926 static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1931 v9fs_stat_free(&vs->v9stat);
1932 v9fs_string_free(&vs->name);
1933 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1934 vs->offset += vs->count;
1937 complete_pdu(s, vs->pdu, err);
1942 static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1949 err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1954 vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1956 if ((vs->len != (vs->v9stat.size + 2)) ||
1957 ((vs->count + vs->len) > vs->max_count)) {
1958 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1959 v9fs_read_post_seekdir(s, vs, err);
1962 vs->count += vs->len;
1963 v9fs_stat_free(&vs->v9stat);
1964 v9fs_string_free(&vs->name);
1965 vs->dir_pos = vs->dent->d_off;
1966 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
1967 v9fs_read_post_readdir(s, vs, err);
1970 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1971 v9fs_read_post_seekdir(s, vs, err);
1976 static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1979 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1980 v9fs_string_init(&vs->name);
1981 v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1983 err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1984 v9fs_read_post_dir_lstat(s, vs, err);
1988 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1989 vs->offset += vs->count;
1991 complete_pdu(s, vs->pdu, err);
1996 static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1998 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
1999 v9fs_read_post_readdir(s, vs, err);
2003 static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
2006 vs->dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
2007 v9fs_read_post_telldir(s, vs, err);
2011 static void v9fs_read_post_preadv(V9fsState *s, V9fsReadState *vs, ssize_t err)
2014 /* IO error return the error */
2018 vs->total += vs->len;
2019 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
2020 if (vs->total < vs->count && vs->len > 0) {
2023 print_sg(vs->sg, vs->cnt);
2025 vs->len = v9fs_do_preadv(s, vs->fidp->fs.fd, vs->sg, vs->cnt,
2030 } while (vs->len == -1 && errno == EINTR);
2031 if (vs->len == -1) {
2034 v9fs_read_post_preadv(s, vs, err);
2037 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
2038 vs->offset += vs->count;
2042 complete_pdu(s, vs->pdu, err);
2046 static void v9fs_xattr_read(V9fsState *s, V9fsReadState *vs)
2052 xattr_len = vs->fidp->fs.xattr.len;
2053 read_count = xattr_len - vs->off;
2054 if (read_count > vs->count) {
2055 read_count = vs->count;
2056 } else if (read_count < 0) {
2058 * read beyond XATTR value
2062 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", read_count);
2063 vs->offset += pdu_pack(vs->pdu, vs->offset,
2064 ((char *)vs->fidp->fs.xattr.value) + vs->off,
2067 complete_pdu(s, vs->pdu, err);
2071 static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
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 typedef struct V9fsReadDirState {
2129 off_t saved_dir_pos;
2130 struct dirent *dent;
2134 int64_t initial_offset;
2138 static void v9fs_readdir_post_seekdir(V9fsState *s, V9fsReadDirState *vs)
2140 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
2141 vs->offset += vs->count;
2142 complete_pdu(s, vs->pdu, vs->offset);
2147 /* Size of each dirent on the wire: size of qid (13) + size of offset (8)
2148 * size of type (1) + size of name.size (2) + strlen(name.data)
2150 #define V9_READDIR_DATA_SZ (24 + strlen(vs->name.data))
2152 static void v9fs_readdir_post_readdir(V9fsState *s, V9fsReadDirState *vs)
2158 v9fs_string_init(&vs->name);
2159 v9fs_string_sprintf(&vs->name, "%s", vs->dent->d_name);
2161 if ((vs->count + V9_READDIR_DATA_SZ) > vs->max_count) {
2162 /* Ran out of buffer. Set dir back to old position and return */
2163 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->saved_dir_pos);
2164 v9fs_readdir_post_seekdir(s, vs);
2168 /* Fill up just the path field of qid because the client uses
2169 * only that. To fill the entire qid structure we will have
2170 * to stat each dirent found, which is expensive
2172 size = MIN(sizeof(vs->dent->d_ino), sizeof(vs->qid.path));
2173 memcpy(&vs->qid.path, &vs->dent->d_ino, size);
2174 /* Fill the other fields with dummy values */
2176 vs->qid.version = 0;
2178 len = pdu_marshal(vs->pdu, vs->offset+4+vs->count, "Qqbs",
2179 &vs->qid, vs->dent->d_off,
2180 vs->dent->d_type, &vs->name);
2182 v9fs_string_free(&vs->name);
2183 vs->saved_dir_pos = vs->dent->d_off;
2184 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
2185 v9fs_readdir_post_readdir(s, vs);
2189 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
2190 vs->offset += vs->count;
2191 complete_pdu(s, vs->pdu, vs->offset);
2196 static void v9fs_readdir_post_telldir(V9fsState *s, V9fsReadDirState *vs)
2198 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
2199 v9fs_readdir_post_readdir(s, vs);
2203 static void v9fs_readdir_post_setdir(V9fsState *s, V9fsReadDirState *vs)
2205 vs->saved_dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
2206 v9fs_readdir_post_telldir(s, vs);
2210 static void v9fs_readdir(V9fsState *s, V9fsPDU *pdu)
2213 V9fsReadDirState *vs;
2217 vs = qemu_malloc(sizeof(*vs));
2222 pdu_unmarshal(vs->pdu, offset, "dqd", &fid, &vs->initial_offset,
2225 vs->fidp = lookup_fid(s, fid);
2226 if (vs->fidp == NULL || !(vs->fidp->fs.dir)) {
2231 if (vs->initial_offset == 0) {
2232 v9fs_do_rewinddir(s, vs->fidp->fs.dir);
2234 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->initial_offset);
2237 v9fs_readdir_post_setdir(s, vs);
2241 complete_pdu(s, pdu, err);
2246 static void v9fs_write_post_pwritev(V9fsState *s, V9fsWriteState *vs,
2250 /* IO error return the error */
2254 vs->total += vs->len;
2255 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
2256 if (vs->total < vs->count && vs->len > 0) {
2259 print_sg(vs->sg, vs->cnt);
2261 vs->len = v9fs_do_pwritev(s, vs->fidp->fs.fd, vs->sg, vs->cnt,
2266 } while (vs->len == -1 && errno == EINTR);
2267 if (vs->len == -1) {
2270 v9fs_write_post_pwritev(s, vs, err);
2273 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
2276 complete_pdu(s, vs->pdu, err);
2280 static void v9fs_xattr_write(V9fsState *s, V9fsWriteState *vs)
2287 xattr_len = vs->fidp->fs.xattr.len;
2288 write_count = xattr_len - vs->off;
2289 if (write_count > vs->count) {
2290 write_count = vs->count;
2291 } else if (write_count < 0) {
2293 * write beyond XATTR value len specified in
2299 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", write_count);
2301 vs->fidp->fs.xattr.copied_len += write_count;
2303 * Now copy the content from sg list
2305 for (i = 0; i < vs->cnt; i++) {
2306 if (write_count > vs->sg[i].iov_len) {
2307 to_copy = vs->sg[i].iov_len;
2309 to_copy = write_count;
2311 memcpy((char *)vs->fidp->fs.xattr.value + vs->off,
2312 vs->sg[i].iov_base, to_copy);
2313 /* updating vs->off since we are not using below */
2315 write_count -= to_copy;
2318 complete_pdu(s, vs->pdu, err);
2322 static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
2328 vs = qemu_malloc(sizeof(*vs));
2336 pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
2339 vs->fidp = lookup_fid(s, fid);
2340 if (vs->fidp == NULL) {
2345 if (vs->fidp->fid_type == P9_FID_FILE) {
2346 if (vs->fidp->fs.fd == -1) {
2350 } else if (vs->fidp->fid_type == P9_FID_XATTR) {
2352 * setxattr operation
2354 v9fs_xattr_write(s, vs);
2360 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
2361 if (vs->total <= vs->count) {
2362 vs->len = v9fs_do_pwritev(s, vs->fidp->fs.fd, vs->sg, vs->cnt, vs->off);
2367 v9fs_write_post_pwritev(s, vs, err);
2371 complete_pdu(s, vs->pdu, err);
2375 static void v9fs_create_post_getiounit(V9fsState *s, V9fsCreateState *vs)
2378 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
2379 stat_to_qid(&vs->stbuf, &vs->qid);
2381 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
2384 complete_pdu(s, vs->pdu, err);
2385 v9fs_string_free(&vs->name);
2386 v9fs_string_free(&vs->extension);
2387 v9fs_string_free(&vs->fullname);
2391 static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
2394 vs->iounit = get_iounit(s, &vs->fidp->path);
2395 v9fs_create_post_getiounit(s, vs);
2399 complete_pdu(s, vs->pdu, err);
2400 v9fs_string_free(&vs->name);
2401 v9fs_string_free(&vs->extension);
2402 v9fs_string_free(&vs->fullname);
2406 static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
2411 v9fs_post_create(s, vs, err);
2414 static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
2417 if (!vs->fidp->fs.dir) {
2420 vs->fidp->fid_type = P9_FID_DIR;
2421 v9fs_post_create(s, vs, err);
2424 static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
2432 vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fullname);
2433 v9fs_create_post_opendir(s, vs, err);
2437 v9fs_post_create(s, vs, err);
2440 static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
2447 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2448 v9fs_create_post_dir_lstat(s, vs, err);
2452 v9fs_post_create(s, vs, err);
2455 static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
2458 vs->fidp->fid_type = P9_FID_NONE;
2459 close(vs->fidp->fs.fd);
2462 v9fs_post_create(s, vs, err);
2466 static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
2468 if (vs->fidp->fs.fd == -1) {
2472 vs->fidp->fid_type = P9_FID_FILE;
2473 err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
2474 v9fs_create_post_fstat(s, vs, err);
2479 v9fs_post_create(s, vs, err);
2483 static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
2486 if (err == 0 || errno != ENOENT) {
2491 if (vs->perm & P9_STAT_MODE_DIR) {
2492 err = v9fs_do_mkdir(s, vs->fullname.data, vs->perm & 0777,
2494 v9fs_create_post_mkdir(s, vs, err);
2495 } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
2496 err = v9fs_do_symlink(s, vs->fidp, vs->extension.data,
2497 vs->fullname.data, -1);
2498 v9fs_create_post_perms(s, vs, err);
2499 } else if (vs->perm & P9_STAT_MODE_LINK) {
2500 int32_t nfid = atoi(vs->extension.data);
2501 V9fsFidState *nfidp = lookup_fid(s, nfid);
2502 if (nfidp == NULL) {
2504 v9fs_post_create(s, vs, err);
2506 err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
2507 v9fs_create_post_perms(s, vs, err);
2508 } else if (vs->perm & P9_STAT_MODE_DEVICE) {
2510 uint32_t major, minor;
2513 if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
2516 v9fs_post_create(s, vs, err);
2528 v9fs_post_create(s, vs, err);
2531 nmode |= vs->perm & 0777;
2532 err = v9fs_do_mknod(s, vs->fullname.data, nmode,
2533 makedev(major, minor), vs->fidp->uid, -1);
2534 v9fs_create_post_perms(s, vs, err);
2535 } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
2536 err = v9fs_do_mknod(s, vs->fullname.data, S_IFIFO | (vs->perm & 0777),
2537 0, vs->fidp->uid, -1);
2538 v9fs_post_create(s, vs, err);
2539 } else if (vs->perm & P9_STAT_MODE_SOCKET) {
2540 err = v9fs_do_mknod(s, vs->fullname.data, S_IFSOCK | (vs->perm & 0777),
2541 0, vs->fidp->uid, -1);
2542 v9fs_post_create(s, vs, err);
2544 vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
2545 -1, omode_to_uflags(vs->mode)|O_CREAT, vs->perm);
2547 v9fs_create_post_open2(s, vs, err);
2553 v9fs_post_create(s, vs, err);
2556 static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
2559 V9fsCreateState *vs;
2562 vs = qemu_malloc(sizeof(*vs));
2566 v9fs_string_init(&vs->fullname);
2568 pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
2569 &vs->perm, &vs->mode, &vs->extension);
2571 vs->fidp = lookup_fid(s, fid);
2572 if (vs->fidp == NULL) {
2577 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
2580 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2581 v9fs_create_post_lstat(s, vs, err);
2585 complete_pdu(s, vs->pdu, err);
2586 v9fs_string_free(&vs->name);
2587 v9fs_string_free(&vs->extension);
2591 static void v9fs_post_symlink(V9fsState *s, V9fsSymlinkState *vs, int err)
2594 stat_to_qid(&vs->stbuf, &vs->qid);
2595 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
2600 complete_pdu(s, vs->pdu, err);
2601 v9fs_string_free(&vs->name);
2602 v9fs_string_free(&vs->symname);
2603 v9fs_string_free(&vs->fullname);
2607 static void v9fs_symlink_post_do_symlink(V9fsState *s, V9fsSymlinkState *vs,
2613 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2615 v9fs_post_symlink(s, vs, err);
2618 static void v9fs_symlink(V9fsState *s, V9fsPDU *pdu)
2621 V9fsSymlinkState *vs;
2625 vs = qemu_malloc(sizeof(*vs));
2629 v9fs_string_init(&vs->fullname);
2631 pdu_unmarshal(vs->pdu, vs->offset, "dssd", &dfid, &vs->name,
2632 &vs->symname, &gid);
2634 vs->dfidp = lookup_fid(s, dfid);
2635 if (vs->dfidp == NULL) {
2640 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->dfidp->path.data,
2642 err = v9fs_do_symlink(s, vs->dfidp, vs->symname.data,
2643 vs->fullname.data, gid);
2644 v9fs_symlink_post_do_symlink(s, vs, err);
2648 complete_pdu(s, vs->pdu, err);
2649 v9fs_string_free(&vs->name);
2650 v9fs_string_free(&vs->symname);
2654 static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
2656 /* A nop call with no return */
2657 complete_pdu(s, pdu, 7);
2660 static void v9fs_link(V9fsState *s, V9fsPDU *pdu)
2662 int32_t dfid, oldfid;
2663 V9fsFidState *dfidp, *oldfidp;
2664 V9fsString name, fullname;
2668 v9fs_string_init(&fullname);
2670 pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2672 dfidp = lookup_fid(s, dfid);
2673 if (dfidp == NULL) {
2678 oldfidp = lookup_fid(s, oldfid);
2679 if (oldfidp == NULL) {
2684 v9fs_string_sprintf(&fullname, "%s/%s", dfidp->path.data, name.data);
2686 err = v9fs_do_link(s, &oldfidp->path, &fullname);
2690 v9fs_string_free(&fullname);
2693 v9fs_string_free(&name);
2694 complete_pdu(s, pdu, err);
2697 static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
2706 /* For TREMOVE we need to clunk the fid even on failed remove */
2707 free_fid(s, vs->fidp->fid);
2709 complete_pdu(s, vs->pdu, err);
2713 static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
2716 V9fsRemoveState *vs;
2719 vs = qemu_malloc(sizeof(*vs));
2723 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
2725 vs->fidp = lookup_fid(s, fid);
2726 if (vs->fidp == NULL) {
2731 err = v9fs_do_remove(s, &vs->fidp->path);
2732 v9fs_remove_post_remove(s, vs, err);
2736 complete_pdu(s, pdu, err);
2740 static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
2749 v9fs_stat_free(&vs->v9stat);
2750 complete_pdu(s, vs->pdu, err);
2754 static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
2759 if (vs->v9stat.length != -1) {
2760 if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
2764 v9fs_wstat_post_truncate(s, vs, err);
2768 v9fs_stat_free(&vs->v9stat);
2769 complete_pdu(s, vs->pdu, err);
2773 static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs)
2776 char *old_name, *new_name;
2779 if (vs->newdirfid != -1) {
2780 V9fsFidState *dirfidp;
2781 dirfidp = lookup_fid(s, vs->newdirfid);
2783 if (dirfidp == NULL) {
2788 BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2790 new_name = qemu_mallocz(dirfidp->path.size + vs->name.size + 2);
2792 strcpy(new_name, dirfidp->path.data);
2793 strcat(new_name, "/");
2794 strcat(new_name + dirfidp->path.size, vs->name.data);
2796 old_name = vs->fidp->path.data;
2797 end = strrchr(old_name, '/');
2803 new_name = qemu_mallocz(end - old_name + vs->name.size + 1);
2805 strncat(new_name, old_name, end - old_name);
2806 strncat(new_name + (end - old_name), vs->name.data, vs->name.size);
2809 v9fs_string_free(&vs->name);
2810 vs->name.data = qemu_strdup(new_name);
2811 vs->name.size = strlen(new_name);
2813 if (strcmp(new_name, vs->fidp->path.data) != 0) {
2814 if (v9fs_do_rename(s, &vs->fidp->path, &vs->name)) {
2819 * Fixup fid's pointing to the old name to
2820 * start pointing to the new name
2822 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2823 if (vs->fidp == fidp) {
2825 * we replace name of this fid towards the end so
2826 * that our below v9fs_path_is_ancestor check will
2831 if (v9fs_path_is_ancestor(&vs->fidp->path, &fidp->path)) {
2832 /* replace the name */
2833 v9fs_fix_path(&fidp->path, &vs->name,
2834 strlen(vs->fidp->path.data));
2837 v9fs_string_copy(&vs->fidp->path, &vs->name);
2841 v9fs_string_free(&vs->name);
2845 static void v9fs_rename_post_rename(V9fsState *s, V9fsRenameState *vs, int err)
2847 complete_pdu(s, vs->pdu, err);
2851 static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
2857 if (vs->v9stat.name.size != 0) {
2858 V9fsRenameState *vr;
2860 vr = qemu_mallocz(sizeof(V9fsRenameState));
2863 vr->fidp = vs->fidp;
2864 vr->offset = vs->offset;
2865 vr->name.size = vs->v9stat.name.size;
2866 vr->name.data = qemu_strdup(vs->v9stat.name.data);
2868 err = v9fs_complete_rename(s, vr);
2871 v9fs_wstat_post_rename(s, vs, err);
2875 v9fs_stat_free(&vs->v9stat);
2876 complete_pdu(s, vs->pdu, err);
2880 static void v9fs_rename(V9fsState *s, V9fsPDU *pdu)
2883 V9fsRenameState *vs;
2886 vs = qemu_malloc(sizeof(*vs));
2890 pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &vs->newdirfid, &vs->name);
2892 vs->fidp = lookup_fid(s, fid);
2893 if (vs->fidp == NULL) {
2898 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
2900 err = v9fs_complete_rename(s, vs);
2901 v9fs_rename_post_rename(s, vs, err);
2904 complete_pdu(s, vs->pdu, err);
2908 static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2914 if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) {
2915 if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2916 vs->v9stat.n_gid)) {
2920 v9fs_wstat_post_chown(s, vs, err);
2924 v9fs_stat_free(&vs->v9stat);
2925 complete_pdu(s, vs->pdu, err);
2929 static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2935 if (vs->v9stat.mtime != -1 || vs->v9stat.atime != -1) {
2936 struct timespec times[2];
2937 if (vs->v9stat.atime != -1) {
2938 times[0].tv_sec = vs->v9stat.atime;
2939 times[0].tv_nsec = 0;
2941 times[0].tv_nsec = UTIME_OMIT;
2943 if (vs->v9stat.mtime != -1) {
2944 times[1].tv_sec = vs->v9stat.mtime;
2945 times[1].tv_nsec = 0;
2947 times[1].tv_nsec = UTIME_OMIT;
2950 if (v9fs_do_utimensat(s, &vs->fidp->path, times)) {
2955 v9fs_wstat_post_utime(s, vs, err);
2959 v9fs_stat_free(&vs->v9stat);
2960 complete_pdu(s, vs->pdu, err);
2964 static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2969 v9fs_stat_free(&vs->v9stat);
2970 complete_pdu(s, vs->pdu, err);
2974 static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2983 v9_mode = stat_to_v9mode(&vs->stbuf);
2985 if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2986 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2987 /* Attempting to change the type */
2992 if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2993 &vs->v9stat.extension))) {
2996 v9fs_wstat_post_chmod(s, vs, err);
3000 v9fs_stat_free(&vs->v9stat);
3001 complete_pdu(s, vs->pdu, err);
3005 static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
3011 vs = qemu_malloc(sizeof(*vs));
3015 pdu_unmarshal(pdu, vs->offset, "dwS", &fid, &vs->unused, &vs->v9stat);
3017 vs->fidp = lookup_fid(s, fid);
3018 if (vs->fidp == NULL) {
3023 /* do we need to sync the file? */
3024 if (donttouch_stat(&vs->v9stat)) {
3025 err = v9fs_do_fsync(s, vs->fidp->fs.fd, 0);
3026 v9fs_wstat_post_fsync(s, vs, err);
3030 if (vs->v9stat.mode != -1) {
3031 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
3032 v9fs_wstat_post_lstat(s, vs, err);
3036 v9fs_wstat_post_chmod(s, vs, err);
3040 v9fs_stat_free(&vs->v9stat);
3041 complete_pdu(s, vs->pdu, err);
3045 static void v9fs_statfs_post_statfs(V9fsState *s, V9fsStatfsState *vs, int err)
3047 int32_t bsize_factor;
3055 * compute bsize factor based on host file system block size
3058 bsize_factor = (s->msize - P9_IOHDRSZ)/vs->stbuf.f_bsize;
3059 if (!bsize_factor) {
3062 vs->v9statfs.f_type = vs->stbuf.f_type;
3063 vs->v9statfs.f_bsize = vs->stbuf.f_bsize;
3064 vs->v9statfs.f_bsize *= bsize_factor;
3066 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3067 * adjust(divide) the number of blocks, free blocks and available
3068 * blocks by bsize factor
3070 vs->v9statfs.f_blocks = vs->stbuf.f_blocks/bsize_factor;
3071 vs->v9statfs.f_bfree = vs->stbuf.f_bfree/bsize_factor;
3072 vs->v9statfs.f_bavail = vs->stbuf.f_bavail/bsize_factor;
3073 vs->v9statfs.f_files = vs->stbuf.f_files;
3074 vs->v9statfs.f_ffree = vs->stbuf.f_ffree;
3075 vs->v9statfs.fsid_val = (unsigned int) vs->stbuf.f_fsid.__val[0] |
3076 (unsigned long long)vs->stbuf.f_fsid.__val[1] << 32;
3077 vs->v9statfs.f_namelen = vs->stbuf.f_namelen;
3079 vs->offset += pdu_marshal(vs->pdu, vs->offset, "ddqqqqqqd",
3080 vs->v9statfs.f_type, vs->v9statfs.f_bsize, vs->v9statfs.f_blocks,
3081 vs->v9statfs.f_bfree, vs->v9statfs.f_bavail, vs->v9statfs.f_files,
3082 vs->v9statfs.f_ffree, vs->v9statfs.fsid_val,
3083 vs->v9statfs.f_namelen);
3086 complete_pdu(s, vs->pdu, vs->offset);
3090 static void v9fs_statfs(V9fsState *s, V9fsPDU *pdu)
3092 V9fsStatfsState *vs;
3095 vs = qemu_malloc(sizeof(*vs));
3099 memset(&vs->v9statfs, 0, sizeof(vs->v9statfs));
3101 pdu_unmarshal(vs->pdu, vs->offset, "d", &vs->fid);
3103 vs->fidp = lookup_fid(s, vs->fid);
3104 if (vs->fidp == NULL) {
3109 err = v9fs_do_statfs(s, &vs->fidp->path, &vs->stbuf);
3110 v9fs_statfs_post_statfs(s, vs, err);
3114 complete_pdu(s, vs->pdu, err);
3118 static void v9fs_mknod_post_lstat(V9fsState *s, V9fsMkState *vs, int err)
3125 stat_to_qid(&vs->stbuf, &vs->qid);
3126 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
3129 complete_pdu(s, vs->pdu, err);
3130 v9fs_string_free(&vs->fullname);
3131 v9fs_string_free(&vs->name);
3135 static void v9fs_mknod_post_mknod(V9fsState *s, V9fsMkState *vs, int err)
3142 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
3143 v9fs_mknod_post_lstat(s, vs, err);
3146 complete_pdu(s, vs->pdu, err);
3147 v9fs_string_free(&vs->fullname);
3148 v9fs_string_free(&vs->name);
3152 static void v9fs_mknod(V9fsState *s, V9fsPDU *pdu)
3162 vs = qemu_malloc(sizeof(*vs));
3166 v9fs_string_init(&vs->fullname);
3167 pdu_unmarshal(vs->pdu, vs->offset, "dsdddd", &fid, &vs->name, &mode,
3168 &major, &minor, &gid);
3170 fidp = lookup_fid(s, fid);
3176 v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data);
3177 err = v9fs_do_mknod(s, vs->fullname.data, mode, makedev(major, minor),
3179 v9fs_mknod_post_mknod(s, vs, err);
3183 complete_pdu(s, vs->pdu, err);
3184 v9fs_string_free(&vs->fullname);
3185 v9fs_string_free(&vs->name);
3190 * Implement posix byte range locking code
3191 * Server side handling of locking code is very simple, because 9p server in
3192 * QEMU can handle only one client. And most of the lock handling
3193 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3194 * do any thing in * qemu 9p server side lock code path.
3195 * So when a TLOCK request comes, always return success
3198 static void v9fs_lock(V9fsState *s, V9fsPDU *pdu)
3200 int32_t fid, err = 0;
3203 vs = qemu_mallocz(sizeof(*vs));
3207 vs->flock = qemu_malloc(sizeof(*vs->flock));
3208 pdu_unmarshal(vs->pdu, vs->offset, "dbdqqds", &fid, &vs->flock->type,
3209 &vs->flock->flags, &vs->flock->start, &vs->flock->length,
3210 &vs->flock->proc_id, &vs->flock->client_id);
3212 vs->status = P9_LOCK_ERROR;
3214 /* We support only block flag now (that too ignored currently) */
3215 if (vs->flock->flags & ~P9_LOCK_FLAGS_BLOCK) {
3219 vs->fidp = lookup_fid(s, fid);
3220 if (vs->fidp == NULL) {
3225 err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
3230 vs->status = P9_LOCK_SUCCESS;
3232 vs->offset += pdu_marshal(vs->pdu, vs->offset, "b", vs->status);
3233 complete_pdu(s, vs->pdu, err);
3234 qemu_free(vs->flock);
3239 * When a TGETLOCK request comes, always return success because all lock
3240 * handling is done by client's VFS layer.
3243 static void v9fs_getlock(V9fsState *s, V9fsPDU *pdu)
3245 int32_t fid, err = 0;
3246 V9fsGetlockState *vs;
3248 vs = qemu_mallocz(sizeof(*vs));
3252 vs->glock = qemu_malloc(sizeof(*vs->glock));
3253 pdu_unmarshal(vs->pdu, vs->offset, "dbqqds", &fid, &vs->glock->type,
3254 &vs->glock->start, &vs->glock->length, &vs->glock->proc_id,
3255 &vs->glock->client_id);
3257 vs->fidp = lookup_fid(s, fid);
3258 if (vs->fidp == NULL) {
3263 err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
3268 vs->glock->type = F_UNLCK;
3269 vs->offset += pdu_marshal(vs->pdu, vs->offset, "bqqds", vs->glock->type,
3270 vs->glock->start, vs->glock->length, vs->glock->proc_id,
3271 &vs->glock->client_id);
3273 complete_pdu(s, vs->pdu, err);
3274 qemu_free(vs->glock);
3278 static void v9fs_mkdir_post_lstat(V9fsState *s, V9fsMkState *vs, int err)
3285 stat_to_qid(&vs->stbuf, &vs->qid);
3286 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
3289 complete_pdu(s, vs->pdu, err);
3290 v9fs_string_free(&vs->fullname);
3291 v9fs_string_free(&vs->name);
3295 static void v9fs_mkdir_post_mkdir(V9fsState *s, V9fsMkState *vs, int err)
3302 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
3303 v9fs_mkdir_post_lstat(s, vs, err);
3306 complete_pdu(s, vs->pdu, err);
3307 v9fs_string_free(&vs->fullname);
3308 v9fs_string_free(&vs->name);
3312 static void v9fs_mkdir(V9fsState *s, V9fsPDU *pdu)
3321 vs = qemu_malloc(sizeof(*vs));
3325 v9fs_string_init(&vs->fullname);
3326 pdu_unmarshal(vs->pdu, vs->offset, "dsdd", &fid, &vs->name, &mode,
3329 fidp = lookup_fid(s, fid);
3335 v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data);
3336 err = v9fs_do_mkdir(s, vs->fullname.data, mode, fidp->uid, gid);
3337 v9fs_mkdir_post_mkdir(s, vs, err);
3341 complete_pdu(s, vs->pdu, err);
3342 v9fs_string_free(&vs->fullname);
3343 v9fs_string_free(&vs->name);
3347 static void v9fs_post_xattr_getvalue(V9fsState *s, V9fsXattrState *vs, int err)
3352 free_fid(s, vs->xattr_fidp->fid);
3355 vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size);
3358 complete_pdu(s, vs->pdu, err);
3359 v9fs_string_free(&vs->name);
3364 static void v9fs_post_xattr_check(V9fsState *s, V9fsXattrState *vs, ssize_t err)
3368 free_fid(s, vs->xattr_fidp->fid);
3372 * Read the xattr value
3374 vs->xattr_fidp->fs.xattr.len = vs->size;
3375 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3376 vs->xattr_fidp->fs.xattr.copied_len = -1;
3378 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3379 err = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path,
3380 &vs->name, vs->xattr_fidp->fs.xattr.value,
3381 vs->xattr_fidp->fs.xattr.len);
3383 v9fs_post_xattr_getvalue(s, vs, err);
3386 complete_pdu(s, vs->pdu, err);
3387 v9fs_string_free(&vs->name);
3391 static void v9fs_post_lxattr_getvalue(V9fsState *s,
3392 V9fsXattrState *vs, int err)
3396 free_fid(s, vs->xattr_fidp->fid);
3399 vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size);
3402 complete_pdu(s, vs->pdu, err);
3403 v9fs_string_free(&vs->name);
3408 static void v9fs_post_lxattr_check(V9fsState *s,
3409 V9fsXattrState *vs, ssize_t err)
3413 free_fid(s, vs->xattr_fidp->fid);
3417 * Read the xattr value
3419 vs->xattr_fidp->fs.xattr.len = vs->size;
3420 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3421 vs->xattr_fidp->fs.xattr.copied_len = -1;
3423 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3424 err = v9fs_do_llistxattr(s, &vs->xattr_fidp->path,
3425 vs->xattr_fidp->fs.xattr.value,
3426 vs->xattr_fidp->fs.xattr.len);
3428 v9fs_post_lxattr_getvalue(s, vs, err);
3431 complete_pdu(s, vs->pdu, err);
3432 v9fs_string_free(&vs->name);
3436 static void v9fs_xattrwalk(V9fsState *s, V9fsPDU *pdu)
3440 int32_t fid, newfid;
3442 vs = qemu_malloc(sizeof(*vs));
3446 pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &newfid, &vs->name);
3447 vs->file_fidp = lookup_fid(s, fid);
3448 if (vs->file_fidp == NULL) {
3453 vs->xattr_fidp = alloc_fid(s, newfid);
3454 if (vs->xattr_fidp == NULL) {
3459 v9fs_string_copy(&vs->xattr_fidp->path, &vs->file_fidp->path);
3460 if (vs->name.data[0] == 0) {
3462 * listxattr request. Get the size first
3464 vs->size = v9fs_do_llistxattr(s, &vs->xattr_fidp->path,
3469 v9fs_post_lxattr_check(s, vs, err);
3473 * specific xattr fid. We check for xattr
3474 * presence also collect the xattr size
3476 vs->size = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path,
3477 &vs->name, NULL, 0);
3481 v9fs_post_xattr_check(s, vs, err);
3485 complete_pdu(s, vs->pdu, err);
3486 v9fs_string_free(&vs->name);
3490 static void v9fs_xattrcreate(V9fsState *s, V9fsPDU *pdu)
3497 vs = qemu_malloc(sizeof(*vs));
3501 pdu_unmarshal(vs->pdu, vs->offset, "dsqd",
3502 &fid, &vs->name, &vs->size, &flags);
3504 vs->file_fidp = lookup_fid(s, fid);
3505 if (vs->file_fidp == NULL) {
3510 /* Make the file fid point to xattr */
3511 vs->xattr_fidp = vs->file_fidp;
3512 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3513 vs->xattr_fidp->fs.xattr.copied_len = 0;
3514 vs->xattr_fidp->fs.xattr.len = vs->size;
3515 vs->xattr_fidp->fs.xattr.flags = flags;
3516 v9fs_string_init(&vs->xattr_fidp->fs.xattr.name);
3517 v9fs_string_copy(&vs->xattr_fidp->fs.xattr.name, &vs->name);
3519 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3521 vs->xattr_fidp->fs.xattr.value = NULL;
3524 complete_pdu(s, vs->pdu, err);
3525 v9fs_string_free(&vs->name);
3529 static void v9fs_readlink_post_readlink(V9fsState *s, V9fsReadLinkState *vs,
3536 vs->offset += pdu_marshal(vs->pdu, vs->offset, "s", &vs->target);
3539 complete_pdu(s, vs->pdu, err);
3540 v9fs_string_free(&vs->target);
3544 static void v9fs_readlink(V9fsState *s, V9fsPDU *pdu)
3547 V9fsReadLinkState *vs;
3551 vs = qemu_malloc(sizeof(*vs));
3555 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
3557 fidp = lookup_fid(s, fid);
3563 v9fs_string_init(&vs->target);
3564 err = v9fs_do_readlink(s, &fidp->path, &vs->target);
3565 v9fs_readlink_post_readlink(s, vs, err);
3568 complete_pdu(s, vs->pdu, err);
3572 typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
3574 static pdu_handler_t *pdu_handlers[] = {
3575 [P9_TREADDIR] = v9fs_readdir,
3576 [P9_TSTATFS] = v9fs_statfs,
3577 [P9_TGETATTR] = v9fs_getattr,
3578 [P9_TSETATTR] = v9fs_setattr,
3579 [P9_TXATTRWALK] = v9fs_xattrwalk,
3580 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3581 [P9_TMKNOD] = v9fs_mknod,
3582 [P9_TRENAME] = v9fs_rename,
3583 [P9_TLOCK] = v9fs_lock,
3584 [P9_TGETLOCK] = v9fs_getlock,
3585 [P9_TREADLINK] = v9fs_readlink,
3586 [P9_TMKDIR] = v9fs_mkdir,
3587 [P9_TVERSION] = v9fs_version,
3588 [P9_TLOPEN] = v9fs_open,
3589 [P9_TATTACH] = v9fs_attach,
3590 [P9_TSTAT] = v9fs_stat,
3591 [P9_TWALK] = v9fs_walk,
3592 [P9_TCLUNK] = v9fs_clunk,
3593 [P9_TFSYNC] = v9fs_fsync,
3594 [P9_TOPEN] = v9fs_open,
3595 [P9_TREAD] = v9fs_read,
3597 [P9_TAUTH] = v9fs_auth,
3599 [P9_TFLUSH] = v9fs_flush,
3600 [P9_TLINK] = v9fs_link,
3601 [P9_TSYMLINK] = v9fs_symlink,
3602 [P9_TCREATE] = v9fs_create,
3603 [P9_TLCREATE] = v9fs_lcreate,
3604 [P9_TWRITE] = v9fs_write,
3605 [P9_TWSTAT] = v9fs_wstat,
3606 [P9_TREMOVE] = v9fs_remove,
3609 static void v9fs_op_not_supp(V9fsState *s, V9fsPDU *pdu)
3611 complete_pdu(s, pdu, -EOPNOTSUPP);
3614 static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
3616 pdu_handler_t *handler;
3621 if (pdu->id >= ARRAY_SIZE(pdu_handlers) ||
3622 (pdu_handlers[pdu->id] == NULL)) {
3623 handler = v9fs_op_not_supp;
3625 handler = pdu_handlers[pdu->id];
3630 void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
3632 V9fsState *s = (V9fsState *)vdev;
3636 while ((pdu = alloc_pdu(s)) &&
3637 (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
3640 BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
3641 BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
3643 ptr = pdu->elem.out_sg[0].iov_base;
3645 memcpy(&pdu->size, ptr, 4);
3647 memcpy(&pdu->tag, ptr + 5, 2);