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.
16 #include "qemu_socket.h"
17 #include "virtio-9p.h"
18 #include "fsdev/qemu-fsdev.h"
19 #include "virtio-9p-debug.h"
20 #include "virtio-9p-xattr.h"
36 static int omode_to_uflags(int8_t mode)
70 void cred_init(FsCred *credp)
78 static int v9fs_do_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf)
80 return s->ops->lstat(&s->ctx, path->data, stbuf);
83 static ssize_t v9fs_do_readlink(V9fsState *s, V9fsString *path, V9fsString *buf)
87 buf->data = qemu_malloc(1024);
89 len = s->ops->readlink(&s->ctx, path->data, buf->data, 1024 - 1);
98 static int v9fs_do_close(V9fsState *s, int fd)
100 return s->ops->close(&s->ctx, fd);
103 static int v9fs_do_closedir(V9fsState *s, DIR *dir)
105 return s->ops->closedir(&s->ctx, dir);
108 static int v9fs_do_open(V9fsState *s, V9fsString *path, int flags)
110 return s->ops->open(&s->ctx, path->data, flags);
113 static DIR *v9fs_do_opendir(V9fsState *s, V9fsString *path)
115 return s->ops->opendir(&s->ctx, path->data);
118 static void v9fs_do_rewinddir(V9fsState *s, DIR *dir)
120 return s->ops->rewinddir(&s->ctx, dir);
123 static off_t v9fs_do_telldir(V9fsState *s, DIR *dir)
125 return s->ops->telldir(&s->ctx, dir);
128 static struct dirent *v9fs_do_readdir(V9fsState *s, DIR *dir)
130 return s->ops->readdir(&s->ctx, dir);
133 static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off)
135 return s->ops->seekdir(&s->ctx, dir, off);
138 static int v9fs_do_preadv(V9fsState *s, int fd, const struct iovec *iov,
139 int iovcnt, int64_t offset)
141 return s->ops->preadv(&s->ctx, fd, iov, iovcnt, offset);
144 static int v9fs_do_pwritev(V9fsState *s, int fd, const struct iovec *iov,
145 int iovcnt, int64_t offset)
147 return s->ops->pwritev(&s->ctx, fd, iov, iovcnt, offset);
150 static int v9fs_do_chmod(V9fsState *s, V9fsString *path, mode_t mode)
155 return s->ops->chmod(&s->ctx, path->data, &cred);
158 static int v9fs_do_mknod(V9fsState *s, char *name,
159 mode_t mode, dev_t dev, uid_t uid, gid_t gid)
167 return s->ops->mknod(&s->ctx, name, &cred);
170 static int v9fs_do_mkdir(V9fsState *s, char *name, mode_t mode,
171 uid_t uid, gid_t gid)
180 return s->ops->mkdir(&s->ctx, name, &cred);
183 static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
185 return s->ops->fstat(&s->ctx, fd, stbuf);
188 static int v9fs_do_open2(V9fsState *s, char *fullname, uid_t uid, gid_t gid,
196 cred.fc_mode = mode & 07777;
199 return s->ops->open2(&s->ctx, fullname, flags, &cred);
202 static int v9fs_do_symlink(V9fsState *s, V9fsFidState *fidp,
203 const char *oldpath, const char *newpath, gid_t gid)
207 cred.fc_uid = fidp->uid;
211 return s->ops->symlink(&s->ctx, oldpath, newpath, &cred);
214 static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
216 return s->ops->link(&s->ctx, oldpath->data, newpath->data);
219 static int v9fs_do_truncate(V9fsState *s, V9fsString *path, off_t size)
221 return s->ops->truncate(&s->ctx, path->data, size);
224 static int v9fs_do_rename(V9fsState *s, V9fsString *oldpath,
227 return s->ops->rename(&s->ctx, oldpath->data, newpath->data);
230 static int v9fs_do_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
237 return s->ops->chown(&s->ctx, path->data, &cred);
240 static int v9fs_do_utimensat(V9fsState *s, V9fsString *path,
241 const struct timespec times[2])
243 return s->ops->utimensat(&s->ctx, path->data, times);
246 static int v9fs_do_remove(V9fsState *s, V9fsString *path)
248 return s->ops->remove(&s->ctx, path->data);
251 static int v9fs_do_fsync(V9fsState *s, int fd, int datasync)
253 return s->ops->fsync(&s->ctx, fd, datasync);
256 static int v9fs_do_statfs(V9fsState *s, V9fsString *path, struct statfs *stbuf)
258 return s->ops->statfs(&s->ctx, path->data, stbuf);
261 static ssize_t v9fs_do_lgetxattr(V9fsState *s, V9fsString *path,
262 V9fsString *xattr_name,
263 void *value, size_t size)
265 return s->ops->lgetxattr(&s->ctx, path->data,
266 xattr_name->data, value, size);
269 static ssize_t v9fs_do_llistxattr(V9fsState *s, V9fsString *path,
270 void *value, size_t size)
272 return s->ops->llistxattr(&s->ctx, path->data,
276 static int v9fs_do_lsetxattr(V9fsState *s, V9fsString *path,
277 V9fsString *xattr_name,
278 void *value, size_t size, int flags)
280 return s->ops->lsetxattr(&s->ctx, path->data,
281 xattr_name->data, value, size, flags);
284 static int v9fs_do_lremovexattr(V9fsState *s, V9fsString *path,
285 V9fsString *xattr_name)
287 return s->ops->lremovexattr(&s->ctx, path->data,
292 static void v9fs_string_init(V9fsString *str)
298 static void v9fs_string_free(V9fsString *str)
300 qemu_free(str->data);
305 static void v9fs_string_null(V9fsString *str)
307 v9fs_string_free(str);
310 static int number_to_string(void *arg, char type)
312 unsigned int ret = 0;
316 unsigned int num = *(unsigned int *)arg;
325 unsigned long num = *(unsigned long *)arg;
333 printf("Number_to_string: Unknown number format\n");
340 static int GCC_FMT_ATTR(2, 0)
341 v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
344 char *iter = (char *)fmt;
348 unsigned int arg_uint;
349 unsigned long arg_ulong;
351 /* Find the number of %'s that denotes an argument */
352 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
357 len = strlen(fmt) - 2*nr_args;
367 /* Now parse the format string */
368 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
372 arg_uint = va_arg(ap2, unsigned int);
373 len += number_to_string((void *)&arg_uint, 'u');
376 if (*++iter == 'u') {
377 arg_ulong = va_arg(ap2, unsigned long);
378 len += number_to_string((void *)&arg_ulong, 'U');
384 arg_char_ptr = va_arg(ap2, char *);
385 len += strlen(arg_char_ptr);
392 "v9fs_string_alloc_printf:Incorrect format %c", *iter);
399 *strp = qemu_malloc((len + 1) * sizeof(**strp));
401 return vsprintf(*strp, fmt, ap);
404 static void GCC_FMT_ATTR(2, 3)
405 v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
410 v9fs_string_free(str);
413 err = v9fs_string_alloc_printf(&str->data, fmt, ap);
420 static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
422 v9fs_string_free(lhs);
423 v9fs_string_sprintf(lhs, "%s", rhs->data);
426 static size_t v9fs_string_size(V9fsString *str)
431 static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
435 for (f = s->fid_list; f; f = f->next) {
444 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
448 f = lookup_fid(s, fid);
453 f = qemu_mallocz(sizeof(V9fsFidState));
456 f->fid_type = P9_FID_NONE;
458 f->next = s->fid_list;
464 static int v9fs_xattr_fid_clunk(V9fsState *s, V9fsFidState *fidp)
468 if (fidp->fs.xattr.copied_len == -1) {
469 /* getxattr/listxattr fid */
473 * if this is fid for setxattr. clunk should
474 * result in setxattr localcall
476 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
477 /* clunk after partial write */
481 if (fidp->fs.xattr.len) {
482 retval = v9fs_do_lsetxattr(s, &fidp->path, &fidp->fs.xattr.name,
483 fidp->fs.xattr.value,
485 fidp->fs.xattr.flags);
487 retval = v9fs_do_lremovexattr(s, &fidp->path, &fidp->fs.xattr.name);
490 v9fs_string_free(&fidp->fs.xattr.name);
492 if (fidp->fs.xattr.value) {
493 qemu_free(fidp->fs.xattr.value);
498 static int free_fid(V9fsState *s, int32_t fid)
501 V9fsFidState **fidpp, *fidp;
503 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
504 if ((*fidpp)->fid == fid) {
509 if (*fidpp == NULL) {
516 if (fidp->fid_type == P9_FID_FILE) {
517 v9fs_do_close(s, fidp->fs.fd);
518 } else if (fidp->fid_type == P9_FID_DIR) {
519 v9fs_do_closedir(s, fidp->fs.dir);
520 } else if (fidp->fid_type == P9_FID_XATTR) {
521 retval = v9fs_xattr_fid_clunk(s, fidp);
523 v9fs_string_free(&fidp->path);
529 #define P9_QID_TYPE_DIR 0x80
530 #define P9_QID_TYPE_SYMLINK 0x02
532 #define P9_STAT_MODE_DIR 0x80000000
533 #define P9_STAT_MODE_APPEND 0x40000000
534 #define P9_STAT_MODE_EXCL 0x20000000
535 #define P9_STAT_MODE_MOUNT 0x10000000
536 #define P9_STAT_MODE_AUTH 0x08000000
537 #define P9_STAT_MODE_TMP 0x04000000
538 #define P9_STAT_MODE_SYMLINK 0x02000000
539 #define P9_STAT_MODE_LINK 0x01000000
540 #define P9_STAT_MODE_DEVICE 0x00800000
541 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
542 #define P9_STAT_MODE_SOCKET 0x00100000
543 #define P9_STAT_MODE_SETUID 0x00080000
544 #define P9_STAT_MODE_SETGID 0x00040000
545 #define P9_STAT_MODE_SETVTX 0x00010000
547 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
548 P9_STAT_MODE_SYMLINK | \
549 P9_STAT_MODE_LINK | \
550 P9_STAT_MODE_DEVICE | \
551 P9_STAT_MODE_NAMED_PIPE | \
554 /* This is the algorithm from ufs in spfs */
555 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
559 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
560 memcpy(&qidp->path, &stbuf->st_ino, size);
561 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
563 if (S_ISDIR(stbuf->st_mode)) {
564 qidp->type |= P9_QID_TYPE_DIR;
566 if (S_ISLNK(stbuf->st_mode)) {
567 qidp->type |= P9_QID_TYPE_SYMLINK;
571 static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
576 err = v9fs_do_lstat(s, &fidp->path, &stbuf);
581 stat_to_qid(&stbuf, qidp);
585 static V9fsPDU *alloc_pdu(V9fsState *s)
589 if (!QLIST_EMPTY(&s->free_list)) {
590 pdu = QLIST_FIRST(&s->free_list);
591 QLIST_REMOVE(pdu, next);
596 static void free_pdu(V9fsState *s, V9fsPDU *pdu)
602 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
606 size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
607 size_t offset, size_t size, int pack)
612 for (i = 0; size && i < sg_count; i++) {
614 if (offset >= sg[i].iov_len) {
616 offset -= sg[i].iov_len;
619 len = MIN(sg[i].iov_len - offset, size);
621 memcpy(sg[i].iov_base + offset, addr, len);
623 memcpy(addr, sg[i].iov_base + offset, len);
638 static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
640 return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
644 static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
647 return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
651 static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
655 struct iovec *src_sg;
659 src_sg = pdu->elem.in_sg;
660 num = pdu->elem.in_num;
662 src_sg = pdu->elem.out_sg;
663 num = pdu->elem.out_num;
667 for (i = 0; i < num; i++) {
669 sg[j].iov_base = src_sg[i].iov_base;
670 sg[j].iov_len = src_sg[i].iov_len;
672 } else if (offset < (src_sg[i].iov_len + pos)) {
673 sg[j].iov_base = src_sg[i].iov_base;
674 sg[j].iov_len = src_sg[i].iov_len;
675 sg[j].iov_base += (offset - pos);
676 sg[j].iov_len -= (offset - pos);
679 pos += src_sg[i].iov_len;
685 static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
687 size_t old_offset = offset;
692 for (i = 0; fmt[i]; i++) {
695 uint8_t *valp = va_arg(ap, uint8_t *);
696 offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
701 valp = va_arg(ap, uint16_t *);
702 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
703 *valp = le16_to_cpu(val);
708 valp = va_arg(ap, uint32_t *);
709 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
710 *valp = le32_to_cpu(val);
715 valp = va_arg(ap, uint64_t *);
716 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
717 *valp = le64_to_cpu(val);
721 struct iovec *iov = va_arg(ap, struct iovec *);
722 int *iovcnt = va_arg(ap, int *);
723 *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
727 V9fsString *str = va_arg(ap, V9fsString *);
728 offset += pdu_unmarshal(pdu, offset, "w", &str->size);
729 /* FIXME: sanity check str->size */
730 str->data = qemu_malloc(str->size + 1);
731 offset += pdu_unpack(str->data, pdu, offset, str->size);
732 str->data[str->size] = 0;
736 V9fsQID *qidp = va_arg(ap, V9fsQID *);
737 offset += pdu_unmarshal(pdu, offset, "bdq",
738 &qidp->type, &qidp->version, &qidp->path);
742 V9fsStat *statp = va_arg(ap, V9fsStat *);
743 offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
744 &statp->size, &statp->type, &statp->dev,
745 &statp->qid, &statp->mode, &statp->atime,
746 &statp->mtime, &statp->length,
747 &statp->name, &statp->uid, &statp->gid,
748 &statp->muid, &statp->extension,
749 &statp->n_uid, &statp->n_gid,
754 V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
755 offset += pdu_unmarshal(pdu, offset, "ddddqqqqq",
756 &iattr->valid, &iattr->mode,
757 &iattr->uid, &iattr->gid, &iattr->size,
758 &iattr->atime_sec, &iattr->atime_nsec,
759 &iattr->mtime_sec, &iattr->mtime_nsec);
769 return offset - old_offset;
772 static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
774 size_t old_offset = offset;
779 for (i = 0; fmt[i]; i++) {
782 uint8_t val = va_arg(ap, int);
783 offset += pdu_pack(pdu, offset, &val, sizeof(val));
788 cpu_to_le16w(&val, va_arg(ap, int));
789 offset += pdu_pack(pdu, offset, &val, sizeof(val));
794 cpu_to_le32w(&val, va_arg(ap, uint32_t));
795 offset += pdu_pack(pdu, offset, &val, sizeof(val));
800 cpu_to_le64w(&val, va_arg(ap, uint64_t));
801 offset += pdu_pack(pdu, offset, &val, sizeof(val));
805 struct iovec *iov = va_arg(ap, struct iovec *);
806 int *iovcnt = va_arg(ap, int *);
807 *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
811 V9fsString *str = va_arg(ap, V9fsString *);
812 offset += pdu_marshal(pdu, offset, "w", str->size);
813 offset += pdu_pack(pdu, offset, str->data, str->size);
817 V9fsQID *qidp = va_arg(ap, V9fsQID *);
818 offset += pdu_marshal(pdu, offset, "bdq",
819 qidp->type, qidp->version, qidp->path);
823 V9fsStat *statp = va_arg(ap, V9fsStat *);
824 offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
825 statp->size, statp->type, statp->dev,
826 &statp->qid, statp->mode, statp->atime,
827 statp->mtime, statp->length, &statp->name,
828 &statp->uid, &statp->gid, &statp->muid,
829 &statp->extension, statp->n_uid,
830 statp->n_gid, statp->n_muid);
834 V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
835 offset += pdu_marshal(pdu, offset, "qQdddqqqqqqqqqqqqqqq",
836 statp->st_result_mask,
837 &statp->qid, statp->st_mode,
838 statp->st_uid, statp->st_gid,
839 statp->st_nlink, statp->st_rdev,
840 statp->st_size, statp->st_blksize, statp->st_blocks,
841 statp->st_atime_sec, statp->st_atime_nsec,
842 statp->st_mtime_sec, statp->st_mtime_nsec,
843 statp->st_ctime_sec, statp->st_ctime_nsec,
844 statp->st_btime_sec, statp->st_btime_nsec,
845 statp->st_gen, statp->st_data_version);
854 return offset - old_offset;
857 static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
859 int8_t id = pdu->id + 1; /* Response */
865 if (s->proto_version != V9FS_PROTO_2000L) {
868 str.data = strerror(err);
869 str.size = strlen(str.data);
871 len += pdu_marshal(pdu, len, "s", &str);
875 len += pdu_marshal(pdu, len, "d", err);
877 if (s->proto_version == V9FS_PROTO_2000L) {
882 /* fill out the header */
883 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
885 /* keep these in sync */
889 /* push onto queue and notify */
890 virtqueue_push(s->vq, &pdu->elem, len);
892 /* FIXME: we should batch these completions */
893 virtio_notify(&s->vdev, s->vq);
898 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
903 if (mode & P9_STAT_MODE_DIR) {
907 if (mode & P9_STAT_MODE_SYMLINK) {
910 if (mode & P9_STAT_MODE_SOCKET) {
913 if (mode & P9_STAT_MODE_NAMED_PIPE) {
916 if (mode & P9_STAT_MODE_DEVICE) {
917 if (extension && extension->data[0] == 'c') {
928 if (mode & P9_STAT_MODE_SETUID) {
931 if (mode & P9_STAT_MODE_SETGID) {
934 if (mode & P9_STAT_MODE_SETVTX) {
941 static int donttouch_stat(V9fsStat *stat)
943 if (stat->type == -1 &&
945 stat->qid.type == -1 &&
946 stat->qid.version == -1 &&
947 stat->qid.path == -1 &&
951 stat->length == -1 &&
958 stat->n_muid == -1) {
965 static void v9fs_stat_free(V9fsStat *stat)
967 v9fs_string_free(&stat->name);
968 v9fs_string_free(&stat->uid);
969 v9fs_string_free(&stat->gid);
970 v9fs_string_free(&stat->muid);
971 v9fs_string_free(&stat->extension);
974 static uint32_t stat_to_v9mode(const struct stat *stbuf)
978 mode = stbuf->st_mode & 0777;
979 if (S_ISDIR(stbuf->st_mode)) {
980 mode |= P9_STAT_MODE_DIR;
983 if (S_ISLNK(stbuf->st_mode)) {
984 mode |= P9_STAT_MODE_SYMLINK;
987 if (S_ISSOCK(stbuf->st_mode)) {
988 mode |= P9_STAT_MODE_SOCKET;
991 if (S_ISFIFO(stbuf->st_mode)) {
992 mode |= P9_STAT_MODE_NAMED_PIPE;
995 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
996 mode |= P9_STAT_MODE_DEVICE;
999 if (stbuf->st_mode & S_ISUID) {
1000 mode |= P9_STAT_MODE_SETUID;
1003 if (stbuf->st_mode & S_ISGID) {
1004 mode |= P9_STAT_MODE_SETGID;
1007 if (stbuf->st_mode & S_ISVTX) {
1008 mode |= P9_STAT_MODE_SETVTX;
1014 static int stat_to_v9stat(V9fsState *s, V9fsString *name,
1015 const struct stat *stbuf,
1021 memset(v9stat, 0, sizeof(*v9stat));
1023 stat_to_qid(stbuf, &v9stat->qid);
1024 v9stat->mode = stat_to_v9mode(stbuf);
1025 v9stat->atime = stbuf->st_atime;
1026 v9stat->mtime = stbuf->st_mtime;
1027 v9stat->length = stbuf->st_size;
1029 v9fs_string_null(&v9stat->uid);
1030 v9fs_string_null(&v9stat->gid);
1031 v9fs_string_null(&v9stat->muid);
1033 v9stat->n_uid = stbuf->st_uid;
1034 v9stat->n_gid = stbuf->st_gid;
1037 v9fs_string_null(&v9stat->extension);
1039 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
1040 err = v9fs_do_readlink(s, name, &v9stat->extension);
1045 v9stat->extension.data[err] = 0;
1046 v9stat->extension.size = err;
1047 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
1048 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
1049 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
1050 major(stbuf->st_rdev), minor(stbuf->st_rdev));
1051 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
1052 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
1053 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
1056 str = strrchr(name->data, '/');
1063 v9fs_string_sprintf(&v9stat->name, "%s", str);
1066 v9fs_string_size(&v9stat->name) +
1067 v9fs_string_size(&v9stat->uid) +
1068 v9fs_string_size(&v9stat->gid) +
1069 v9fs_string_size(&v9stat->muid) +
1070 v9fs_string_size(&v9stat->extension);
1074 #define P9_STATS_MODE 0x00000001ULL
1075 #define P9_STATS_NLINK 0x00000002ULL
1076 #define P9_STATS_UID 0x00000004ULL
1077 #define P9_STATS_GID 0x00000008ULL
1078 #define P9_STATS_RDEV 0x00000010ULL
1079 #define P9_STATS_ATIME 0x00000020ULL
1080 #define P9_STATS_MTIME 0x00000040ULL
1081 #define P9_STATS_CTIME 0x00000080ULL
1082 #define P9_STATS_INO 0x00000100ULL
1083 #define P9_STATS_SIZE 0x00000200ULL
1084 #define P9_STATS_BLOCKS 0x00000400ULL
1086 #define P9_STATS_BTIME 0x00000800ULL
1087 #define P9_STATS_GEN 0x00001000ULL
1088 #define P9_STATS_DATA_VERSION 0x00002000ULL
1090 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1091 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1094 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
1095 V9fsStatDotl *v9lstat)
1097 memset(v9lstat, 0, sizeof(*v9lstat));
1099 v9lstat->st_mode = stbuf->st_mode;
1100 v9lstat->st_nlink = stbuf->st_nlink;
1101 v9lstat->st_uid = stbuf->st_uid;
1102 v9lstat->st_gid = stbuf->st_gid;
1103 v9lstat->st_rdev = stbuf->st_rdev;
1104 v9lstat->st_size = stbuf->st_size;
1105 v9lstat->st_blksize = stbuf->st_blksize;
1106 v9lstat->st_blocks = stbuf->st_blocks;
1107 v9lstat->st_atime_sec = stbuf->st_atime;
1108 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1109 v9lstat->st_mtime_sec = stbuf->st_mtime;
1110 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
1111 v9lstat->st_ctime_sec = stbuf->st_ctime;
1112 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1113 /* Currently we only support BASIC fields in stat */
1114 v9lstat->st_result_mask = P9_STATS_BASIC;
1116 stat_to_qid(stbuf, &v9lstat->qid);
1119 static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
1121 while (len && *iovcnt) {
1122 if (len < sg->iov_len) {
1124 sg->iov_base += len;
1136 static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
1141 for (i = 0; i < *cnt; i++) {
1142 if ((total + sg[i].iov_len) > cap) {
1143 sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
1147 total += sg[i].iov_len;
1155 static void print_sg(struct iovec *sg, int cnt)
1159 printf("sg[%d]: {", cnt);
1160 for (i = 0; i < cnt; i++) {
1164 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1169 static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
1172 v9fs_string_init(&str);
1173 v9fs_string_copy(&str, dst);
1174 v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
1175 v9fs_string_free(&str);
1178 static void v9fs_version(V9fsState *s, V9fsPDU *pdu)
1183 pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1185 if (!strcmp(version.data, "9P2000.u")) {
1186 s->proto_version = V9FS_PROTO_2000U;
1187 } else if (!strcmp(version.data, "9P2000.L")) {
1188 s->proto_version = V9FS_PROTO_2000L;
1190 v9fs_string_sprintf(&version, "unknown");
1193 offset += pdu_marshal(pdu, offset, "ds", s->msize, &version);
1194 complete_pdu(s, pdu, offset);
1196 v9fs_string_free(&version);
1199 static void v9fs_attach(V9fsState *s, V9fsPDU *pdu)
1201 int32_t fid, afid, n_uname;
1202 V9fsString uname, aname;
1208 pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1210 fidp = alloc_fid(s, fid);
1216 fidp->uid = n_uname;
1218 v9fs_string_sprintf(&fidp->path, "%s", "/");
1219 err = fid_to_qid(s, fidp, &qid);
1226 offset += pdu_marshal(pdu, offset, "Q", &qid);
1230 complete_pdu(s, pdu, err);
1231 v9fs_string_free(&uname);
1232 v9fs_string_free(&aname);
1235 static void v9fs_stat_post_lstat(V9fsState *s, V9fsStatState *vs, int err)
1242 err = stat_to_v9stat(s, &vs->fidp->path, &vs->stbuf, &vs->v9stat);
1246 vs->offset += pdu_marshal(vs->pdu, vs->offset, "wS", 0, &vs->v9stat);
1250 complete_pdu(s, vs->pdu, err);
1251 v9fs_stat_free(&vs->v9stat);
1255 static void v9fs_stat(V9fsState *s, V9fsPDU *pdu)
1261 vs = qemu_malloc(sizeof(*vs));
1265 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1267 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
1269 vs->fidp = lookup_fid(s, fid);
1270 if (vs->fidp == NULL) {
1275 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1276 v9fs_stat_post_lstat(s, vs, err);
1280 complete_pdu(s, vs->pdu, err);
1281 v9fs_stat_free(&vs->v9stat);
1285 static void v9fs_getattr_post_lstat(V9fsState *s, V9fsStatStateDotl *vs,
1293 stat_to_v9stat_dotl(s, &vs->stbuf, &vs->v9stat_dotl);
1294 vs->offset += pdu_marshal(vs->pdu, vs->offset, "A", &vs->v9stat_dotl);
1298 complete_pdu(s, vs->pdu, err);
1302 static void v9fs_getattr(V9fsState *s, V9fsPDU *pdu)
1305 V9fsStatStateDotl *vs;
1308 uint64_t request_mask;
1310 vs = qemu_malloc(sizeof(*vs));
1314 memset(&vs->v9stat_dotl, 0, sizeof(vs->v9stat_dotl));
1316 pdu_unmarshal(vs->pdu, vs->offset, "dq", &fid, &request_mask);
1318 fidp = lookup_fid(s, fid);
1324 /* Currently we only support BASIC fields in stat, so there is no
1325 * need to look at request_mask.
1327 err = v9fs_do_lstat(s, &fidp->path, &vs->stbuf);
1328 v9fs_getattr_post_lstat(s, vs, err);
1332 complete_pdu(s, vs->pdu, err);
1336 /* From Linux kernel code */
1337 #define ATTR_MODE (1 << 0)
1338 #define ATTR_UID (1 << 1)
1339 #define ATTR_GID (1 << 2)
1340 #define ATTR_SIZE (1 << 3)
1341 #define ATTR_ATIME (1 << 4)
1342 #define ATTR_MTIME (1 << 5)
1343 #define ATTR_CTIME (1 << 6)
1344 #define ATTR_MASK 127
1345 #define ATTR_ATIME_SET (1 << 7)
1346 #define ATTR_MTIME_SET (1 << 8)
1348 static void v9fs_setattr_post_truncate(V9fsState *s, V9fsSetattrState *vs,
1358 complete_pdu(s, vs->pdu, err);
1362 static void v9fs_setattr_post_chown(V9fsState *s, V9fsSetattrState *vs, int err)
1369 if (vs->v9iattr.valid & (ATTR_SIZE)) {
1370 err = v9fs_do_truncate(s, &vs->fidp->path, vs->v9iattr.size);
1372 v9fs_setattr_post_truncate(s, vs, err);
1376 complete_pdu(s, vs->pdu, err);
1380 static void v9fs_setattr_post_utimensat(V9fsState *s, V9fsSetattrState *vs,
1388 /* If the only valid entry in iattr is ctime we can call
1389 * chown(-1,-1) to update the ctime of the file
1391 if ((vs->v9iattr.valid & (ATTR_UID | ATTR_GID)) ||
1392 ((vs->v9iattr.valid & ATTR_CTIME)
1393 && !((vs->v9iattr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
1394 if (!(vs->v9iattr.valid & ATTR_UID)) {
1395 vs->v9iattr.uid = -1;
1397 if (!(vs->v9iattr.valid & ATTR_GID)) {
1398 vs->v9iattr.gid = -1;
1400 err = v9fs_do_chown(s, &vs->fidp->path, vs->v9iattr.uid,
1403 v9fs_setattr_post_chown(s, vs, err);
1407 complete_pdu(s, vs->pdu, err);
1411 static void v9fs_setattr_post_chmod(V9fsState *s, V9fsSetattrState *vs, int err)
1418 if (vs->v9iattr.valid & (ATTR_ATIME | ATTR_MTIME)) {
1419 struct timespec times[2];
1420 if (vs->v9iattr.valid & ATTR_ATIME) {
1421 if (vs->v9iattr.valid & ATTR_ATIME_SET) {
1422 times[0].tv_sec = vs->v9iattr.atime_sec;
1423 times[0].tv_nsec = vs->v9iattr.atime_nsec;
1425 times[0].tv_nsec = UTIME_NOW;
1428 times[0].tv_nsec = UTIME_OMIT;
1431 if (vs->v9iattr.valid & ATTR_MTIME) {
1432 if (vs->v9iattr.valid & ATTR_MTIME_SET) {
1433 times[1].tv_sec = vs->v9iattr.mtime_sec;
1434 times[1].tv_nsec = vs->v9iattr.mtime_nsec;
1436 times[1].tv_nsec = UTIME_NOW;
1439 times[1].tv_nsec = UTIME_OMIT;
1441 err = v9fs_do_utimensat(s, &vs->fidp->path, times);
1443 v9fs_setattr_post_utimensat(s, vs, err);
1447 complete_pdu(s, vs->pdu, err);
1451 static void v9fs_setattr(V9fsState *s, V9fsPDU *pdu)
1454 V9fsSetattrState *vs;
1457 vs = qemu_malloc(sizeof(*vs));
1461 pdu_unmarshal(pdu, vs->offset, "dI", &fid, &vs->v9iattr);
1463 vs->fidp = lookup_fid(s, fid);
1464 if (vs->fidp == NULL) {
1469 if (vs->v9iattr.valid & ATTR_MODE) {
1470 err = v9fs_do_chmod(s, &vs->fidp->path, vs->v9iattr.mode);
1473 v9fs_setattr_post_chmod(s, vs, err);
1477 complete_pdu(s, vs->pdu, err);
1481 static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)
1483 complete_pdu(s, vs->pdu, err);
1485 if (vs->nwnames && vs->nwnames <= P9_MAXWELEM) {
1486 for (vs->name_idx = 0; vs->name_idx < vs->nwnames; vs->name_idx++) {
1487 v9fs_string_free(&vs->wnames[vs->name_idx]);
1490 qemu_free(vs->wnames);
1491 qemu_free(vs->qids);
1495 static void v9fs_walk_marshal(V9fsWalkState *vs)
1499 vs->offset += pdu_marshal(vs->pdu, vs->offset, "w", vs->nwnames);
1501 for (i = 0; i < vs->nwnames; i++) {
1502 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qids[i]);
1506 static void v9fs_walk_post_newfid_lstat(V9fsState *s, V9fsWalkState *vs,
1510 free_fid(s, vs->newfidp->fid);
1511 v9fs_string_free(&vs->path);
1516 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1519 if (vs->name_idx < vs->nwnames) {
1520 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1521 vs->wnames[vs->name_idx].data);
1522 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1524 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1525 v9fs_walk_post_newfid_lstat(s, vs, err);
1529 v9fs_string_free(&vs->path);
1530 v9fs_walk_marshal(vs);
1533 v9fs_walk_complete(s, vs, err);
1536 static void v9fs_walk_post_oldfid_lstat(V9fsState *s, V9fsWalkState *vs,
1540 v9fs_string_free(&vs->path);
1545 stat_to_qid(&vs->stbuf, &vs->qids[vs->name_idx]);
1547 if (vs->name_idx < vs->nwnames) {
1549 v9fs_string_sprintf(&vs->path, "%s/%s",
1550 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1551 v9fs_string_copy(&vs->fidp->path, &vs->path);
1553 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1554 v9fs_walk_post_oldfid_lstat(s, vs, err);
1558 v9fs_string_free(&vs->path);
1559 v9fs_walk_marshal(vs);
1562 v9fs_walk_complete(s, vs, err);
1565 static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
1567 int32_t fid, newfid;
1572 vs = qemu_malloc(sizeof(*vs));
1578 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
1579 &newfid, &vs->nwnames);
1581 if (vs->nwnames && vs->nwnames <= P9_MAXWELEM) {
1582 vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
1584 vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
1586 for (i = 0; i < vs->nwnames; i++) {
1587 vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
1590 } else if (vs->nwnames > P9_MAXWELEM) {
1595 vs->fidp = lookup_fid(s, fid);
1596 if (vs->fidp == NULL) {
1601 /* FIXME: is this really valid? */
1602 if (fid == newfid) {
1604 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
1605 v9fs_string_init(&vs->path);
1608 if (vs->name_idx < vs->nwnames) {
1609 v9fs_string_sprintf(&vs->path, "%s/%s",
1610 vs->fidp->path.data, vs->wnames[vs->name_idx].data);
1611 v9fs_string_copy(&vs->fidp->path, &vs->path);
1613 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1614 v9fs_walk_post_oldfid_lstat(s, vs, err);
1618 vs->newfidp = alloc_fid(s, newfid);
1619 if (vs->newfidp == NULL) {
1624 vs->newfidp->uid = vs->fidp->uid;
1625 v9fs_string_init(&vs->path);
1627 v9fs_string_copy(&vs->newfidp->path, &vs->fidp->path);
1629 if (vs->name_idx < vs->nwnames) {
1630 v9fs_string_sprintf(&vs->path, "%s/%s", vs->newfidp->path.data,
1631 vs->wnames[vs->name_idx].data);
1632 v9fs_string_copy(&vs->newfidp->path, &vs->path);
1634 err = v9fs_do_lstat(s, &vs->newfidp->path, &vs->stbuf);
1635 v9fs_walk_post_newfid_lstat(s, vs, err);
1640 v9fs_walk_marshal(vs);
1643 v9fs_walk_complete(s, vs, err);
1646 static int32_t get_iounit(V9fsState *s, V9fsString *name)
1648 struct statfs stbuf;
1652 * iounit should be multiples of f_bsize (host filesystem block size
1653 * and as well as less than (client msize - P9_IOHDRSZ))
1655 if (!v9fs_do_statfs(s, name, &stbuf)) {
1656 iounit = stbuf.f_bsize;
1657 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1661 iounit = s->msize - P9_IOHDRSZ;
1666 static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
1668 if (vs->fidp->fs.dir == NULL) {
1672 vs->fidp->fid_type = P9_FID_DIR;
1673 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
1676 complete_pdu(s, vs->pdu, err);
1681 static void v9fs_open_post_getiounit(V9fsState *s, V9fsOpenState *vs)
1684 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
1686 complete_pdu(s, vs->pdu, err);
1690 static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
1692 if (vs->fidp->fs.fd == -1) {
1696 vs->fidp->fid_type = P9_FID_FILE;
1697 vs->iounit = get_iounit(s, &vs->fidp->path);
1698 v9fs_open_post_getiounit(s, vs);
1701 complete_pdu(s, vs->pdu, err);
1705 static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
1714 stat_to_qid(&vs->stbuf, &vs->qid);
1716 if (S_ISDIR(vs->stbuf.st_mode)) {
1717 vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fidp->path);
1718 v9fs_open_post_opendir(s, vs, err);
1720 if (s->proto_version == V9FS_PROTO_2000L) {
1722 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
1723 /* Ignore direct disk access hint until the server supports it. */
1726 flags = omode_to_uflags(vs->mode);
1728 vs->fidp->fs.fd = v9fs_do_open(s, &vs->fidp->path, flags);
1729 v9fs_open_post_open(s, vs, err);
1733 complete_pdu(s, vs->pdu, err);
1737 static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
1743 vs = qemu_malloc(sizeof(*vs));
1748 if (s->proto_version == V9FS_PROTO_2000L) {
1749 pdu_unmarshal(vs->pdu, vs->offset, "dd", &fid, &vs->mode);
1751 pdu_unmarshal(vs->pdu, vs->offset, "db", &fid, &vs->mode);
1754 vs->fidp = lookup_fid(s, fid);
1755 if (vs->fidp == NULL) {
1760 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
1762 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
1764 v9fs_open_post_lstat(s, vs, err);
1767 complete_pdu(s, pdu, err);
1771 static void v9fs_post_lcreate(V9fsState *s, V9fsLcreateState *vs, int err)
1774 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
1775 stat_to_qid(&vs->stbuf, &vs->qid);
1776 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid,
1780 vs->fidp->fid_type = P9_FID_NONE;
1782 if (vs->fidp->fs.fd > 0) {
1783 close(vs->fidp->fs.fd);
1787 complete_pdu(s, vs->pdu, err);
1788 v9fs_string_free(&vs->name);
1789 v9fs_string_free(&vs->fullname);
1793 static void v9fs_lcreate_post_get_iounit(V9fsState *s, V9fsLcreateState *vs,
1800 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
1803 v9fs_post_lcreate(s, vs, err);
1806 static void v9fs_lcreate_post_do_open2(V9fsState *s, V9fsLcreateState *vs,
1809 if (vs->fidp->fs.fd == -1) {
1813 vs->fidp->fid_type = P9_FID_FILE;
1814 vs->iounit = get_iounit(s, &vs->fullname);
1815 v9fs_lcreate_post_get_iounit(s, vs, err);
1819 v9fs_post_lcreate(s, vs, err);
1822 static void v9fs_lcreate(V9fsState *s, V9fsPDU *pdu)
1824 int32_t dfid, flags, mode;
1826 V9fsLcreateState *vs;
1829 vs = qemu_malloc(sizeof(*vs));
1833 v9fs_string_init(&vs->fullname);
1835 pdu_unmarshal(vs->pdu, vs->offset, "dsddd", &dfid, &vs->name, &flags,
1838 vs->fidp = lookup_fid(s, dfid);
1839 if (vs->fidp == NULL) {
1844 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
1847 /* Ignore direct disk access hint until the server supports it. */
1850 vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
1852 v9fs_lcreate_post_do_open2(s, vs, err);
1856 complete_pdu(s, vs->pdu, err);
1857 v9fs_string_free(&vs->name);
1861 static void v9fs_post_do_fsync(V9fsState *s, V9fsPDU *pdu, int err)
1866 complete_pdu(s, pdu, err);
1869 static void v9fs_fsync(V9fsState *s, V9fsPDU *pdu)
1877 pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1878 fidp = lookup_fid(s, fid);
1881 v9fs_post_do_fsync(s, pdu, err);
1884 err = v9fs_do_fsync(s, fidp->fs.fd, datasync);
1885 v9fs_post_do_fsync(s, pdu, err);
1888 static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
1894 pdu_unmarshal(pdu, offset, "d", &fid);
1896 err = free_fid(s, fid);
1904 complete_pdu(s, pdu, err);
1907 static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t);
1909 static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1914 v9fs_stat_free(&vs->v9stat);
1915 v9fs_string_free(&vs->name);
1916 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1917 vs->offset += vs->count;
1920 complete_pdu(s, vs->pdu, err);
1925 static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
1932 err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat);
1937 vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S",
1939 if ((vs->len != (vs->v9stat.size + 2)) ||
1940 ((vs->count + vs->len) > vs->max_count)) {
1941 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1942 v9fs_read_post_seekdir(s, vs, err);
1945 vs->count += vs->len;
1946 v9fs_stat_free(&vs->v9stat);
1947 v9fs_string_free(&vs->name);
1948 vs->dir_pos = vs->dent->d_off;
1949 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
1950 v9fs_read_post_readdir(s, vs, err);
1953 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
1954 v9fs_read_post_seekdir(s, vs, err);
1959 static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1962 memset(&vs->v9stat, 0, sizeof(vs->v9stat));
1963 v9fs_string_init(&vs->name);
1964 v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data,
1966 err = v9fs_do_lstat(s, &vs->name, &vs->stbuf);
1967 v9fs_read_post_dir_lstat(s, vs, err);
1971 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
1972 vs->offset += vs->count;
1974 complete_pdu(s, vs->pdu, err);
1979 static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
1981 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
1982 v9fs_read_post_readdir(s, vs, err);
1986 static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
1989 vs->dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
1990 v9fs_read_post_telldir(s, vs, err);
1994 static void v9fs_read_post_preadv(V9fsState *s, V9fsReadState *vs, ssize_t err)
1997 /* IO error return the error */
2001 vs->total += vs->len;
2002 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
2003 if (vs->total < vs->count && vs->len > 0) {
2006 print_sg(vs->sg, vs->cnt);
2008 vs->len = v9fs_do_preadv(s, vs->fidp->fs.fd, vs->sg, vs->cnt,
2013 } while (vs->len == -1 && errno == EINTR);
2014 if (vs->len == -1) {
2017 v9fs_read_post_preadv(s, vs, err);
2020 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
2021 vs->offset += vs->count;
2025 complete_pdu(s, vs->pdu, err);
2029 static void v9fs_xattr_read(V9fsState *s, V9fsReadState *vs)
2035 xattr_len = vs->fidp->fs.xattr.len;
2036 read_count = xattr_len - vs->off;
2037 if (read_count > vs->count) {
2038 read_count = vs->count;
2039 } else if (read_count < 0) {
2041 * read beyond XATTR value
2045 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", read_count);
2046 vs->offset += pdu_pack(vs->pdu, vs->offset,
2047 ((char *)vs->fidp->fs.xattr.value) + vs->off,
2050 complete_pdu(s, vs->pdu, err);
2054 static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
2060 vs = qemu_malloc(sizeof(*vs));
2067 pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count);
2069 vs->fidp = lookup_fid(s, fid);
2070 if (vs->fidp == NULL) {
2075 if (vs->fidp->fid_type == P9_FID_DIR) {
2076 vs->max_count = vs->count;
2079 v9fs_do_rewinddir(s, vs->fidp->fs.dir);
2081 v9fs_read_post_rewinddir(s, vs, err);
2083 } else if (vs->fidp->fid_type == P9_FID_FILE) {
2085 pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
2086 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
2087 if (vs->total <= vs->count) {
2088 vs->len = v9fs_do_preadv(s, vs->fidp->fs.fd, vs->sg, vs->cnt,
2094 v9fs_read_post_preadv(s, vs, err);
2097 } else if (vs->fidp->fid_type == P9_FID_XATTR) {
2098 v9fs_xattr_read(s, vs);
2104 complete_pdu(s, pdu, err);
2108 typedef struct V9fsReadDirState {
2112 off_t saved_dir_pos;
2113 struct dirent *dent;
2117 int64_t initial_offset;
2121 static void v9fs_readdir_post_seekdir(V9fsState *s, V9fsReadDirState *vs)
2123 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
2124 vs->offset += vs->count;
2125 complete_pdu(s, vs->pdu, vs->offset);
2130 /* Size of each dirent on the wire: size of qid (13) + size of offset (8)
2131 * size of type (1) + size of name.size (2) + strlen(name.data)
2133 #define V9_READDIR_DATA_SZ (24 + strlen(vs->name.data))
2135 static void v9fs_readdir_post_readdir(V9fsState *s, V9fsReadDirState *vs)
2141 v9fs_string_init(&vs->name);
2142 v9fs_string_sprintf(&vs->name, "%s", vs->dent->d_name);
2144 if ((vs->count + V9_READDIR_DATA_SZ) > vs->max_count) {
2145 /* Ran out of buffer. Set dir back to old position and return */
2146 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->saved_dir_pos);
2147 v9fs_readdir_post_seekdir(s, vs);
2151 /* Fill up just the path field of qid because the client uses
2152 * only that. To fill the entire qid structure we will have
2153 * to stat each dirent found, which is expensive
2155 size = MIN(sizeof(vs->dent->d_ino), sizeof(vs->qid.path));
2156 memcpy(&vs->qid.path, &vs->dent->d_ino, size);
2157 /* Fill the other fields with dummy values */
2159 vs->qid.version = 0;
2161 len = pdu_marshal(vs->pdu, vs->offset+4+vs->count, "Qqbs",
2162 &vs->qid, vs->dent->d_off,
2163 vs->dent->d_type, &vs->name);
2165 v9fs_string_free(&vs->name);
2166 vs->saved_dir_pos = vs->dent->d_off;
2167 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
2168 v9fs_readdir_post_readdir(s, vs);
2172 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count);
2173 vs->offset += vs->count;
2174 complete_pdu(s, vs->pdu, vs->offset);
2179 static void v9fs_readdir_post_telldir(V9fsState *s, V9fsReadDirState *vs)
2181 vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
2182 v9fs_readdir_post_readdir(s, vs);
2186 static void v9fs_readdir_post_setdir(V9fsState *s, V9fsReadDirState *vs)
2188 vs->saved_dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
2189 v9fs_readdir_post_telldir(s, vs);
2193 static void v9fs_readdir(V9fsState *s, V9fsPDU *pdu)
2196 V9fsReadDirState *vs;
2200 vs = qemu_malloc(sizeof(*vs));
2205 pdu_unmarshal(vs->pdu, offset, "dqd", &fid, &vs->initial_offset,
2208 vs->fidp = lookup_fid(s, fid);
2209 if (vs->fidp == NULL || !(vs->fidp->fs.dir)) {
2214 if (vs->initial_offset == 0) {
2215 v9fs_do_rewinddir(s, vs->fidp->fs.dir);
2217 v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->initial_offset);
2220 v9fs_readdir_post_setdir(s, vs);
2224 complete_pdu(s, pdu, err);
2229 static void v9fs_write_post_pwritev(V9fsState *s, V9fsWriteState *vs,
2233 /* IO error return the error */
2237 vs->total += vs->len;
2238 vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt);
2239 if (vs->total < vs->count && vs->len > 0) {
2242 print_sg(vs->sg, vs->cnt);
2244 vs->len = v9fs_do_pwritev(s, vs->fidp->fs.fd, vs->sg, vs->cnt,
2249 } while (vs->len == -1 && errno == EINTR);
2250 if (vs->len == -1) {
2253 v9fs_write_post_pwritev(s, vs, err);
2256 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total);
2259 complete_pdu(s, vs->pdu, err);
2263 static void v9fs_xattr_write(V9fsState *s, V9fsWriteState *vs)
2270 xattr_len = vs->fidp->fs.xattr.len;
2271 write_count = xattr_len - vs->off;
2272 if (write_count > vs->count) {
2273 write_count = vs->count;
2274 } else if (write_count < 0) {
2276 * write beyond XATTR value len specified in
2282 vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", write_count);
2284 vs->fidp->fs.xattr.copied_len += write_count;
2286 * Now copy the content from sg list
2288 for (i = 0; i < vs->cnt; i++) {
2289 if (write_count > vs->sg[i].iov_len) {
2290 to_copy = vs->sg[i].iov_len;
2292 to_copy = write_count;
2294 memcpy((char *)vs->fidp->fs.xattr.value + vs->off,
2295 vs->sg[i].iov_base, to_copy);
2296 /* updating vs->off since we are not using below */
2298 write_count -= to_copy;
2301 complete_pdu(s, vs->pdu, err);
2305 static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
2311 vs = qemu_malloc(sizeof(*vs));
2319 pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count,
2322 vs->fidp = lookup_fid(s, fid);
2323 if (vs->fidp == NULL) {
2328 if (vs->fidp->fid_type == P9_FID_FILE) {
2329 if (vs->fidp->fs.fd == -1) {
2333 } else if (vs->fidp->fid_type == P9_FID_XATTR) {
2335 * setxattr operation
2337 v9fs_xattr_write(s, vs);
2343 vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt);
2344 if (vs->total <= vs->count) {
2345 vs->len = v9fs_do_pwritev(s, vs->fidp->fs.fd, vs->sg, vs->cnt, vs->off);
2350 v9fs_write_post_pwritev(s, vs, err);
2354 complete_pdu(s, vs->pdu, err);
2358 static void v9fs_create_post_getiounit(V9fsState *s, V9fsCreateState *vs)
2361 v9fs_string_copy(&vs->fidp->path, &vs->fullname);
2362 stat_to_qid(&vs->stbuf, &vs->qid);
2364 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
2367 complete_pdu(s, vs->pdu, err);
2368 v9fs_string_free(&vs->name);
2369 v9fs_string_free(&vs->extension);
2370 v9fs_string_free(&vs->fullname);
2374 static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
2377 vs->iounit = get_iounit(s, &vs->fidp->path);
2378 v9fs_create_post_getiounit(s, vs);
2382 complete_pdu(s, vs->pdu, err);
2383 v9fs_string_free(&vs->name);
2384 v9fs_string_free(&vs->extension);
2385 v9fs_string_free(&vs->fullname);
2389 static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
2394 v9fs_post_create(s, vs, err);
2397 static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
2400 if (!vs->fidp->fs.dir) {
2403 vs->fidp->fid_type = P9_FID_DIR;
2404 v9fs_post_create(s, vs, err);
2407 static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
2415 vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fullname);
2416 v9fs_create_post_opendir(s, vs, err);
2420 v9fs_post_create(s, vs, err);
2423 static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
2430 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2431 v9fs_create_post_dir_lstat(s, vs, err);
2435 v9fs_post_create(s, vs, err);
2438 static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
2441 vs->fidp->fid_type = P9_FID_NONE;
2442 close(vs->fidp->fs.fd);
2445 v9fs_post_create(s, vs, err);
2449 static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
2451 if (vs->fidp->fs.fd == -1) {
2455 vs->fidp->fid_type = P9_FID_FILE;
2456 err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
2457 v9fs_create_post_fstat(s, vs, err);
2462 v9fs_post_create(s, vs, err);
2466 static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
2469 if (err == 0 || errno != ENOENT) {
2474 if (vs->perm & P9_STAT_MODE_DIR) {
2475 err = v9fs_do_mkdir(s, vs->fullname.data, vs->perm & 0777,
2477 v9fs_create_post_mkdir(s, vs, err);
2478 } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
2479 err = v9fs_do_symlink(s, vs->fidp, vs->extension.data,
2480 vs->fullname.data, -1);
2481 v9fs_create_post_perms(s, vs, err);
2482 } else if (vs->perm & P9_STAT_MODE_LINK) {
2483 int32_t nfid = atoi(vs->extension.data);
2484 V9fsFidState *nfidp = lookup_fid(s, nfid);
2485 if (nfidp == NULL) {
2487 v9fs_post_create(s, vs, err);
2489 err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
2490 v9fs_create_post_perms(s, vs, err);
2491 } else if (vs->perm & P9_STAT_MODE_DEVICE) {
2493 uint32_t major, minor;
2496 if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
2499 v9fs_post_create(s, vs, err);
2511 v9fs_post_create(s, vs, err);
2514 nmode |= vs->perm & 0777;
2515 err = v9fs_do_mknod(s, vs->fullname.data, nmode,
2516 makedev(major, minor), vs->fidp->uid, -1);
2517 v9fs_create_post_perms(s, vs, err);
2518 } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
2519 err = v9fs_do_mknod(s, vs->fullname.data, S_IFIFO | (vs->perm & 0777),
2520 0, vs->fidp->uid, -1);
2521 v9fs_post_create(s, vs, err);
2522 } else if (vs->perm & P9_STAT_MODE_SOCKET) {
2523 err = v9fs_do_mknod(s, vs->fullname.data, S_IFSOCK | (vs->perm & 0777),
2524 0, vs->fidp->uid, -1);
2525 v9fs_post_create(s, vs, err);
2527 vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
2528 -1, omode_to_uflags(vs->mode)|O_CREAT, vs->perm);
2530 v9fs_create_post_open2(s, vs, err);
2536 v9fs_post_create(s, vs, err);
2539 static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
2542 V9fsCreateState *vs;
2545 vs = qemu_malloc(sizeof(*vs));
2549 v9fs_string_init(&vs->fullname);
2551 pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
2552 &vs->perm, &vs->mode, &vs->extension);
2554 vs->fidp = lookup_fid(s, fid);
2555 if (vs->fidp == NULL) {
2560 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
2563 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2564 v9fs_create_post_lstat(s, vs, err);
2568 complete_pdu(s, vs->pdu, err);
2569 v9fs_string_free(&vs->name);
2570 v9fs_string_free(&vs->extension);
2574 static void v9fs_post_symlink(V9fsState *s, V9fsSymlinkState *vs, int err)
2577 stat_to_qid(&vs->stbuf, &vs->qid);
2578 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
2583 complete_pdu(s, vs->pdu, err);
2584 v9fs_string_free(&vs->name);
2585 v9fs_string_free(&vs->symname);
2586 v9fs_string_free(&vs->fullname);
2590 static void v9fs_symlink_post_do_symlink(V9fsState *s, V9fsSymlinkState *vs,
2596 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
2598 v9fs_post_symlink(s, vs, err);
2601 static void v9fs_symlink(V9fsState *s, V9fsPDU *pdu)
2604 V9fsSymlinkState *vs;
2608 vs = qemu_malloc(sizeof(*vs));
2612 v9fs_string_init(&vs->fullname);
2614 pdu_unmarshal(vs->pdu, vs->offset, "dssd", &dfid, &vs->name,
2615 &vs->symname, &gid);
2617 vs->dfidp = lookup_fid(s, dfid);
2618 if (vs->dfidp == NULL) {
2623 v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->dfidp->path.data,
2625 err = v9fs_do_symlink(s, vs->dfidp, vs->symname.data,
2626 vs->fullname.data, gid);
2627 v9fs_symlink_post_do_symlink(s, vs, err);
2631 complete_pdu(s, vs->pdu, err);
2632 v9fs_string_free(&vs->name);
2633 v9fs_string_free(&vs->symname);
2637 static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
2639 /* A nop call with no return */
2640 complete_pdu(s, pdu, 7);
2643 static void v9fs_link(V9fsState *s, V9fsPDU *pdu)
2645 int32_t dfid, oldfid;
2646 V9fsFidState *dfidp, *oldfidp;
2647 V9fsString name, fullname;
2651 v9fs_string_init(&fullname);
2653 pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2655 dfidp = lookup_fid(s, dfid);
2656 if (dfidp == NULL) {
2661 oldfidp = lookup_fid(s, oldfid);
2662 if (oldfidp == NULL) {
2667 v9fs_string_sprintf(&fullname, "%s/%s", dfidp->path.data, name.data);
2669 err = v9fs_do_link(s, &oldfidp->path, &fullname);
2673 v9fs_string_free(&fullname);
2676 v9fs_string_free(&name);
2677 complete_pdu(s, pdu, err);
2680 static void v9fs_remove_post_remove(V9fsState *s, V9fsRemoveState *vs,
2689 /* For TREMOVE we need to clunk the fid even on failed remove */
2690 free_fid(s, vs->fidp->fid);
2692 complete_pdu(s, vs->pdu, err);
2696 static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
2699 V9fsRemoveState *vs;
2702 vs = qemu_malloc(sizeof(*vs));
2706 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
2708 vs->fidp = lookup_fid(s, fid);
2709 if (vs->fidp == NULL) {
2714 err = v9fs_do_remove(s, &vs->fidp->path);
2715 v9fs_remove_post_remove(s, vs, err);
2719 complete_pdu(s, pdu, err);
2723 static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
2732 v9fs_stat_free(&vs->v9stat);
2733 complete_pdu(s, vs->pdu, err);
2737 static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
2742 if (vs->v9stat.length != -1) {
2743 if (v9fs_do_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
2747 v9fs_wstat_post_truncate(s, vs, err);
2751 v9fs_stat_free(&vs->v9stat);
2752 complete_pdu(s, vs->pdu, err);
2756 static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs)
2759 char *old_name, *new_name;
2762 if (vs->newdirfid != -1) {
2763 V9fsFidState *dirfidp;
2764 dirfidp = lookup_fid(s, vs->newdirfid);
2766 if (dirfidp == NULL) {
2771 BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2773 new_name = qemu_mallocz(dirfidp->path.size + vs->name.size + 2);
2775 strcpy(new_name, dirfidp->path.data);
2776 strcat(new_name, "/");
2777 strcat(new_name + dirfidp->path.size, vs->name.data);
2779 old_name = vs->fidp->path.data;
2780 end = strrchr(old_name, '/');
2786 new_name = qemu_mallocz(end - old_name + vs->name.size + 1);
2788 strncat(new_name, old_name, end - old_name);
2789 strncat(new_name + (end - old_name), vs->name.data, vs->name.size);
2792 v9fs_string_free(&vs->name);
2793 vs->name.data = qemu_strdup(new_name);
2794 vs->name.size = strlen(new_name);
2796 if (strcmp(new_name, vs->fidp->path.data) != 0) {
2797 if (v9fs_do_rename(s, &vs->fidp->path, &vs->name)) {
2802 * Fixup fid's pointing to the old name to
2803 * start pointing to the new name
2805 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2806 if (vs->fidp == fidp) {
2808 * we replace name of this fid towards the end
2809 * so that our below strcmp will work
2813 if (!strncmp(vs->fidp->path.data, fidp->path.data,
2814 strlen(vs->fidp->path.data))) {
2815 /* replace the name */
2816 v9fs_fix_path(&fidp->path, &vs->name,
2817 strlen(vs->fidp->path.data));
2820 v9fs_string_copy(&vs->fidp->path, &vs->name);
2824 v9fs_string_free(&vs->name);
2828 static void v9fs_rename_post_rename(V9fsState *s, V9fsRenameState *vs, int err)
2830 complete_pdu(s, vs->pdu, err);
2834 static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
2840 if (vs->v9stat.name.size != 0) {
2841 V9fsRenameState *vr;
2843 vr = qemu_mallocz(sizeof(V9fsRenameState));
2846 vr->fidp = vs->fidp;
2847 vr->offset = vs->offset;
2848 vr->name.size = vs->v9stat.name.size;
2849 vr->name.data = qemu_strdup(vs->v9stat.name.data);
2851 err = v9fs_complete_rename(s, vr);
2854 v9fs_wstat_post_rename(s, vs, err);
2858 v9fs_stat_free(&vs->v9stat);
2859 complete_pdu(s, vs->pdu, err);
2863 static void v9fs_rename(V9fsState *s, V9fsPDU *pdu)
2866 V9fsRenameState *vs;
2869 vs = qemu_malloc(sizeof(*vs));
2873 pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &vs->newdirfid, &vs->name);
2875 vs->fidp = lookup_fid(s, fid);
2876 if (vs->fidp == NULL) {
2881 BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
2883 err = v9fs_complete_rename(s, vs);
2884 v9fs_rename_post_rename(s, vs, err);
2887 complete_pdu(s, vs->pdu, err);
2891 static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
2897 if (vs->v9stat.n_gid != -1 || vs->v9stat.n_uid != -1) {
2898 if (v9fs_do_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
2899 vs->v9stat.n_gid)) {
2903 v9fs_wstat_post_chown(s, vs, err);
2907 v9fs_stat_free(&vs->v9stat);
2908 complete_pdu(s, vs->pdu, err);
2912 static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
2918 if (vs->v9stat.mtime != -1 || vs->v9stat.atime != -1) {
2919 struct timespec times[2];
2920 if (vs->v9stat.atime != -1) {
2921 times[0].tv_sec = vs->v9stat.atime;
2922 times[0].tv_nsec = 0;
2924 times[0].tv_nsec = UTIME_OMIT;
2926 if (vs->v9stat.mtime != -1) {
2927 times[1].tv_sec = vs->v9stat.mtime;
2928 times[1].tv_nsec = 0;
2930 times[1].tv_nsec = UTIME_OMIT;
2933 if (v9fs_do_utimensat(s, &vs->fidp->path, times)) {
2938 v9fs_wstat_post_utime(s, vs, err);
2942 v9fs_stat_free(&vs->v9stat);
2943 complete_pdu(s, vs->pdu, err);
2947 static void v9fs_wstat_post_fsync(V9fsState *s, V9fsWstatState *vs, int err)
2952 v9fs_stat_free(&vs->v9stat);
2953 complete_pdu(s, vs->pdu, err);
2957 static void v9fs_wstat_post_lstat(V9fsState *s, V9fsWstatState *vs, int err)
2966 v9_mode = stat_to_v9mode(&vs->stbuf);
2968 if ((vs->v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2969 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2970 /* Attempting to change the type */
2975 if (v9fs_do_chmod(s, &vs->fidp->path, v9mode_to_mode(vs->v9stat.mode,
2976 &vs->v9stat.extension))) {
2979 v9fs_wstat_post_chmod(s, vs, err);
2983 v9fs_stat_free(&vs->v9stat);
2984 complete_pdu(s, vs->pdu, err);
2988 static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
2994 vs = qemu_malloc(sizeof(*vs));
2998 pdu_unmarshal(pdu, vs->offset, "dwS", &fid, &vs->unused, &vs->v9stat);
3000 vs->fidp = lookup_fid(s, fid);
3001 if (vs->fidp == NULL) {
3006 /* do we need to sync the file? */
3007 if (donttouch_stat(&vs->v9stat)) {
3008 err = v9fs_do_fsync(s, vs->fidp->fs.fd, 0);
3009 v9fs_wstat_post_fsync(s, vs, err);
3013 if (vs->v9stat.mode != -1) {
3014 err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
3015 v9fs_wstat_post_lstat(s, vs, err);
3019 v9fs_wstat_post_chmod(s, vs, err);
3023 v9fs_stat_free(&vs->v9stat);
3024 complete_pdu(s, vs->pdu, err);
3028 static void v9fs_statfs_post_statfs(V9fsState *s, V9fsStatfsState *vs, int err)
3030 int32_t bsize_factor;
3038 * compute bsize factor based on host file system block size
3041 bsize_factor = (s->msize - P9_IOHDRSZ)/vs->stbuf.f_bsize;
3042 if (!bsize_factor) {
3045 vs->v9statfs.f_type = vs->stbuf.f_type;
3046 vs->v9statfs.f_bsize = vs->stbuf.f_bsize;
3047 vs->v9statfs.f_bsize *= bsize_factor;
3049 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3050 * adjust(divide) the number of blocks, free blocks and available
3051 * blocks by bsize factor
3053 vs->v9statfs.f_blocks = vs->stbuf.f_blocks/bsize_factor;
3054 vs->v9statfs.f_bfree = vs->stbuf.f_bfree/bsize_factor;
3055 vs->v9statfs.f_bavail = vs->stbuf.f_bavail/bsize_factor;
3056 vs->v9statfs.f_files = vs->stbuf.f_files;
3057 vs->v9statfs.f_ffree = vs->stbuf.f_ffree;
3058 vs->v9statfs.fsid_val = (unsigned int) vs->stbuf.f_fsid.__val[0] |
3059 (unsigned long long)vs->stbuf.f_fsid.__val[1] << 32;
3060 vs->v9statfs.f_namelen = vs->stbuf.f_namelen;
3062 vs->offset += pdu_marshal(vs->pdu, vs->offset, "ddqqqqqqd",
3063 vs->v9statfs.f_type, vs->v9statfs.f_bsize, vs->v9statfs.f_blocks,
3064 vs->v9statfs.f_bfree, vs->v9statfs.f_bavail, vs->v9statfs.f_files,
3065 vs->v9statfs.f_ffree, vs->v9statfs.fsid_val,
3066 vs->v9statfs.f_namelen);
3069 complete_pdu(s, vs->pdu, vs->offset);
3073 static void v9fs_statfs(V9fsState *s, V9fsPDU *pdu)
3075 V9fsStatfsState *vs;
3078 vs = qemu_malloc(sizeof(*vs));
3082 memset(&vs->v9statfs, 0, sizeof(vs->v9statfs));
3084 pdu_unmarshal(vs->pdu, vs->offset, "d", &vs->fid);
3086 vs->fidp = lookup_fid(s, vs->fid);
3087 if (vs->fidp == NULL) {
3092 err = v9fs_do_statfs(s, &vs->fidp->path, &vs->stbuf);
3093 v9fs_statfs_post_statfs(s, vs, err);
3097 complete_pdu(s, vs->pdu, err);
3101 static void v9fs_mknod_post_lstat(V9fsState *s, V9fsMkState *vs, int err)
3108 stat_to_qid(&vs->stbuf, &vs->qid);
3109 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
3112 complete_pdu(s, vs->pdu, err);
3113 v9fs_string_free(&vs->fullname);
3114 v9fs_string_free(&vs->name);
3118 static void v9fs_mknod_post_mknod(V9fsState *s, V9fsMkState *vs, int err)
3125 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
3126 v9fs_mknod_post_lstat(s, vs, err);
3129 complete_pdu(s, vs->pdu, err);
3130 v9fs_string_free(&vs->fullname);
3131 v9fs_string_free(&vs->name);
3135 static void v9fs_mknod(V9fsState *s, V9fsPDU *pdu)
3145 vs = qemu_malloc(sizeof(*vs));
3149 v9fs_string_init(&vs->fullname);
3150 pdu_unmarshal(vs->pdu, vs->offset, "dsdddd", &fid, &vs->name, &mode,
3151 &major, &minor, &gid);
3153 fidp = lookup_fid(s, fid);
3159 v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data);
3160 err = v9fs_do_mknod(s, vs->fullname.data, mode, makedev(major, minor),
3162 v9fs_mknod_post_mknod(s, vs, err);
3166 complete_pdu(s, vs->pdu, err);
3167 v9fs_string_free(&vs->fullname);
3168 v9fs_string_free(&vs->name);
3173 * Implement posix byte range locking code
3174 * Server side handling of locking code is very simple, because 9p server in
3175 * QEMU can handle only one client. And most of the lock handling
3176 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3177 * do any thing in * qemu 9p server side lock code path.
3178 * So when a TLOCK request comes, always return success
3181 static void v9fs_lock(V9fsState *s, V9fsPDU *pdu)
3183 int32_t fid, err = 0;
3186 vs = qemu_mallocz(sizeof(*vs));
3190 vs->flock = qemu_malloc(sizeof(*vs->flock));
3191 pdu_unmarshal(vs->pdu, vs->offset, "dbdqqds", &fid, &vs->flock->type,
3192 &vs->flock->flags, &vs->flock->start, &vs->flock->length,
3193 &vs->flock->proc_id, &vs->flock->client_id);
3195 vs->status = P9_LOCK_ERROR;
3197 /* We support only block flag now (that too ignored currently) */
3198 if (vs->flock->flags & ~P9_LOCK_FLAGS_BLOCK) {
3202 vs->fidp = lookup_fid(s, fid);
3203 if (vs->fidp == NULL) {
3208 err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
3213 vs->status = P9_LOCK_SUCCESS;
3215 vs->offset += pdu_marshal(vs->pdu, vs->offset, "b", vs->status);
3216 complete_pdu(s, vs->pdu, err);
3217 qemu_free(vs->flock);
3222 * When a TGETLOCK request comes, always return success because all lock
3223 * handling is done by client's VFS layer.
3226 static void v9fs_getlock(V9fsState *s, V9fsPDU *pdu)
3228 int32_t fid, err = 0;
3229 V9fsGetlockState *vs;
3231 vs = qemu_mallocz(sizeof(*vs));
3235 vs->glock = qemu_malloc(sizeof(*vs->glock));
3236 pdu_unmarshal(vs->pdu, vs->offset, "dbqqds", &fid, &vs->glock->type,
3237 &vs->glock->start, &vs->glock->length, &vs->glock->proc_id,
3238 &vs->glock->client_id);
3240 vs->fidp = lookup_fid(s, fid);
3241 if (vs->fidp == NULL) {
3246 err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
3251 vs->glock->type = F_UNLCK;
3252 vs->offset += pdu_marshal(vs->pdu, vs->offset, "bqqds", vs->glock->type,
3253 vs->glock->start, vs->glock->length, vs->glock->proc_id,
3254 &vs->glock->client_id);
3256 complete_pdu(s, vs->pdu, err);
3257 qemu_free(vs->glock);
3261 static void v9fs_mkdir_post_lstat(V9fsState *s, V9fsMkState *vs, int err)
3268 stat_to_qid(&vs->stbuf, &vs->qid);
3269 vs->offset += pdu_marshal(vs->pdu, vs->offset, "Q", &vs->qid);
3272 complete_pdu(s, vs->pdu, err);
3273 v9fs_string_free(&vs->fullname);
3274 v9fs_string_free(&vs->name);
3278 static void v9fs_mkdir_post_mkdir(V9fsState *s, V9fsMkState *vs, int err)
3285 err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
3286 v9fs_mkdir_post_lstat(s, vs, err);
3289 complete_pdu(s, vs->pdu, err);
3290 v9fs_string_free(&vs->fullname);
3291 v9fs_string_free(&vs->name);
3295 static void v9fs_mkdir(V9fsState *s, V9fsPDU *pdu)
3304 vs = qemu_malloc(sizeof(*vs));
3308 v9fs_string_init(&vs->fullname);
3309 pdu_unmarshal(vs->pdu, vs->offset, "dsdd", &fid, &vs->name, &mode,
3312 fidp = lookup_fid(s, fid);
3318 v9fs_string_sprintf(&vs->fullname, "%s/%s", fidp->path.data, vs->name.data);
3319 err = v9fs_do_mkdir(s, vs->fullname.data, mode, fidp->uid, gid);
3320 v9fs_mkdir_post_mkdir(s, vs, err);
3324 complete_pdu(s, vs->pdu, err);
3325 v9fs_string_free(&vs->fullname);
3326 v9fs_string_free(&vs->name);
3330 static void v9fs_post_xattr_getvalue(V9fsState *s, V9fsXattrState *vs, int err)
3335 free_fid(s, vs->xattr_fidp->fid);
3338 vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size);
3341 complete_pdu(s, vs->pdu, err);
3342 v9fs_string_free(&vs->name);
3347 static void v9fs_post_xattr_check(V9fsState *s, V9fsXattrState *vs, ssize_t err)
3351 free_fid(s, vs->xattr_fidp->fid);
3355 * Read the xattr value
3357 vs->xattr_fidp->fs.xattr.len = vs->size;
3358 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3359 vs->xattr_fidp->fs.xattr.copied_len = -1;
3361 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3362 err = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path,
3363 &vs->name, vs->xattr_fidp->fs.xattr.value,
3364 vs->xattr_fidp->fs.xattr.len);
3366 v9fs_post_xattr_getvalue(s, vs, err);
3369 complete_pdu(s, vs->pdu, err);
3370 v9fs_string_free(&vs->name);
3374 static void v9fs_post_lxattr_getvalue(V9fsState *s,
3375 V9fsXattrState *vs, int err)
3379 free_fid(s, vs->xattr_fidp->fid);
3382 vs->offset += pdu_marshal(vs->pdu, vs->offset, "q", vs->size);
3385 complete_pdu(s, vs->pdu, err);
3386 v9fs_string_free(&vs->name);
3391 static void v9fs_post_lxattr_check(V9fsState *s,
3392 V9fsXattrState *vs, ssize_t err)
3396 free_fid(s, vs->xattr_fidp->fid);
3400 * Read the xattr value
3402 vs->xattr_fidp->fs.xattr.len = vs->size;
3403 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3404 vs->xattr_fidp->fs.xattr.copied_len = -1;
3406 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3407 err = v9fs_do_llistxattr(s, &vs->xattr_fidp->path,
3408 vs->xattr_fidp->fs.xattr.value,
3409 vs->xattr_fidp->fs.xattr.len);
3411 v9fs_post_lxattr_getvalue(s, vs, err);
3414 complete_pdu(s, vs->pdu, err);
3415 v9fs_string_free(&vs->name);
3419 static void v9fs_xattrwalk(V9fsState *s, V9fsPDU *pdu)
3423 int32_t fid, newfid;
3425 vs = qemu_malloc(sizeof(*vs));
3429 pdu_unmarshal(vs->pdu, vs->offset, "dds", &fid, &newfid, &vs->name);
3430 vs->file_fidp = lookup_fid(s, fid);
3431 if (vs->file_fidp == NULL) {
3436 vs->xattr_fidp = alloc_fid(s, newfid);
3437 if (vs->xattr_fidp == NULL) {
3442 v9fs_string_copy(&vs->xattr_fidp->path, &vs->file_fidp->path);
3443 if (vs->name.data[0] == 0) {
3445 * listxattr request. Get the size first
3447 vs->size = v9fs_do_llistxattr(s, &vs->xattr_fidp->path,
3452 v9fs_post_lxattr_check(s, vs, err);
3456 * specific xattr fid. We check for xattr
3457 * presence also collect the xattr size
3459 vs->size = v9fs_do_lgetxattr(s, &vs->xattr_fidp->path,
3460 &vs->name, NULL, 0);
3464 v9fs_post_xattr_check(s, vs, err);
3468 complete_pdu(s, vs->pdu, err);
3469 v9fs_string_free(&vs->name);
3473 static void v9fs_xattrcreate(V9fsState *s, V9fsPDU *pdu)
3480 vs = qemu_malloc(sizeof(*vs));
3484 pdu_unmarshal(vs->pdu, vs->offset, "dsqd",
3485 &fid, &vs->name, &vs->size, &flags);
3487 vs->file_fidp = lookup_fid(s, fid);
3488 if (vs->file_fidp == NULL) {
3493 /* Make the file fid point to xattr */
3494 vs->xattr_fidp = vs->file_fidp;
3495 vs->xattr_fidp->fid_type = P9_FID_XATTR;
3496 vs->xattr_fidp->fs.xattr.copied_len = 0;
3497 vs->xattr_fidp->fs.xattr.len = vs->size;
3498 vs->xattr_fidp->fs.xattr.flags = flags;
3499 v9fs_string_init(&vs->xattr_fidp->fs.xattr.name);
3500 v9fs_string_copy(&vs->xattr_fidp->fs.xattr.name, &vs->name);
3502 vs->xattr_fidp->fs.xattr.value = qemu_malloc(vs->size);
3504 vs->xattr_fidp->fs.xattr.value = NULL;
3507 complete_pdu(s, vs->pdu, err);
3508 v9fs_string_free(&vs->name);
3512 static void v9fs_readlink_post_readlink(V9fsState *s, V9fsReadLinkState *vs,
3519 vs->offset += pdu_marshal(vs->pdu, vs->offset, "s", &vs->target);
3522 complete_pdu(s, vs->pdu, err);
3523 v9fs_string_free(&vs->target);
3527 static void v9fs_readlink(V9fsState *s, V9fsPDU *pdu)
3530 V9fsReadLinkState *vs;
3534 vs = qemu_malloc(sizeof(*vs));
3538 pdu_unmarshal(vs->pdu, vs->offset, "d", &fid);
3540 fidp = lookup_fid(s, fid);
3546 v9fs_string_init(&vs->target);
3547 err = v9fs_do_readlink(s, &fidp->path, &vs->target);
3548 v9fs_readlink_post_readlink(s, vs, err);
3551 complete_pdu(s, vs->pdu, err);
3555 typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
3557 static pdu_handler_t *pdu_handlers[] = {
3558 [P9_TREADDIR] = v9fs_readdir,
3559 [P9_TSTATFS] = v9fs_statfs,
3560 [P9_TGETATTR] = v9fs_getattr,
3561 [P9_TSETATTR] = v9fs_setattr,
3562 [P9_TXATTRWALK] = v9fs_xattrwalk,
3563 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3564 [P9_TMKNOD] = v9fs_mknod,
3565 [P9_TRENAME] = v9fs_rename,
3566 [P9_TLOCK] = v9fs_lock,
3567 [P9_TGETLOCK] = v9fs_getlock,
3568 [P9_TREADLINK] = v9fs_readlink,
3569 [P9_TMKDIR] = v9fs_mkdir,
3570 [P9_TVERSION] = v9fs_version,
3571 [P9_TLOPEN] = v9fs_open,
3572 [P9_TATTACH] = v9fs_attach,
3573 [P9_TSTAT] = v9fs_stat,
3574 [P9_TWALK] = v9fs_walk,
3575 [P9_TCLUNK] = v9fs_clunk,
3576 [P9_TFSYNC] = v9fs_fsync,
3577 [P9_TOPEN] = v9fs_open,
3578 [P9_TREAD] = v9fs_read,
3580 [P9_TAUTH] = v9fs_auth,
3582 [P9_TFLUSH] = v9fs_flush,
3583 [P9_TLINK] = v9fs_link,
3584 [P9_TSYMLINK] = v9fs_symlink,
3585 [P9_TCREATE] = v9fs_create,
3586 [P9_TLCREATE] = v9fs_lcreate,
3587 [P9_TWRITE] = v9fs_write,
3588 [P9_TWSTAT] = v9fs_wstat,
3589 [P9_TREMOVE] = v9fs_remove,
3592 static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
3594 pdu_handler_t *handler;
3600 BUG_ON(pdu->id >= ARRAY_SIZE(pdu_handlers));
3602 handler = pdu_handlers[pdu->id];
3603 BUG_ON(handler == NULL);
3608 static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
3610 V9fsState *s = (V9fsState *)vdev;
3614 while ((pdu = alloc_pdu(s)) &&
3615 (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
3618 BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
3619 BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
3621 ptr = pdu->elem.out_sg[0].iov_base;
3623 memcpy(&pdu->size, ptr, 4);
3625 memcpy(&pdu->tag, ptr + 5, 2);
3633 static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features)
3635 features |= 1 << VIRTIO_9P_MOUNT_TAG;
3639 static V9fsState *to_virtio_9p(VirtIODevice *vdev)
3641 return (V9fsState *)vdev;
3644 static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
3646 struct virtio_9p_config *cfg;
3647 V9fsState *s = to_virtio_9p(vdev);
3649 cfg = qemu_mallocz(sizeof(struct virtio_9p_config) +
3651 stw_raw(&cfg->tag_len, s->tag_len);
3652 memcpy(cfg->tag, s->tag, s->tag_len);
3653 memcpy(config, cfg, s->config_size);
3657 VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
3665 s = (V9fsState *)virtio_common_init("virtio-9p",
3667 sizeof(struct virtio_9p_config)+
3671 /* initialize pdu allocator */
3672 QLIST_INIT(&s->free_list);
3673 for (i = 0; i < (MAX_REQ - 1); i++) {
3674 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
3677 s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
3679 fse = get_fsdev_fsentry(conf->fsdev_id);
3682 /* We don't have a fsdev identified by fsdev_id */
3683 fprintf(stderr, "Virtio-9p device couldn't find fsdev with the "
3684 "id = %s\n", conf->fsdev_id ? conf->fsdev_id : "NULL");
3688 if (!fse->path || !conf->tag) {
3689 /* we haven't specified a mount_tag or the path */
3690 fprintf(stderr, "fsdev with id %s needs path "
3691 "and Virtio-9p device needs mount_tag arguments\n",
3696 if (!strcmp(fse->security_model, "passthrough")) {
3697 /* Files on the Fileserver set to client user credentials */
3698 s->ctx.fs_sm = SM_PASSTHROUGH;
3699 s->ctx.xops = passthrough_xattr_ops;
3700 } else if (!strcmp(fse->security_model, "mapped")) {
3701 /* Files on the fileserver are set to QEMU credentials.
3702 * Client user credentials are saved in extended attributes.
3704 s->ctx.fs_sm = SM_MAPPED;
3705 s->ctx.xops = mapped_xattr_ops;
3706 } else if (!strcmp(fse->security_model, "none")) {
3708 * Files on the fileserver are set to QEMU credentials.
3710 s->ctx.fs_sm = SM_NONE;
3711 s->ctx.xops = none_xattr_ops;
3713 fprintf(stderr, "Default to security_model=none. You may want"
3714 " enable advanced security model using "
3715 "security option:\n\t security_model=passthrough \n\t "
3716 "security_model=mapped\n");
3717 s->ctx.fs_sm = SM_NONE;
3718 s->ctx.xops = none_xattr_ops;
3721 if (lstat(fse->path, &stat)) {
3722 fprintf(stderr, "share path %s does not exist\n", fse->path);
3724 } else if (!S_ISDIR(stat.st_mode)) {
3725 fprintf(stderr, "share path %s is not a directory \n", fse->path);
3729 s->ctx.fs_root = qemu_strdup(fse->path);
3730 len = strlen(conf->tag);
3731 if (len > MAX_TAG_LEN) {
3734 /* s->tag is non-NULL terminated string */
3735 s->tag = qemu_malloc(len);
3736 memcpy(s->tag, conf->tag, len);
3741 s->vdev.get_features = virtio_9p_get_features;
3742 s->config_size = sizeof(struct virtio_9p_config) +
3744 s->vdev.get_config = virtio_9p_get_config;